ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.76 by root, Tue Apr 11 21:24:09 2006 UTC vs.
Revision 1.140 by root, Thu Apr 20 04:20:52 2006 UTC

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71}
72
73# convert position array to integers
74sub harmonize {
75 my ($vals) = @_;
76
77 my $rem = 0;
78
79 for (@$vals) {
80 my $i = int $_ + $rem;
81 $rem += $_ - $i;
82 $_ = $i;
83 }
71} 84}
72 85
73############################################################################# 86#############################################################################
74 87
75package CFClient::UI::Base; 88package CFClient::UI::Base;
76 89
77use strict; 90use strict;
78 91
79use SDL::OpenGL; 92use CFClient::OpenGL;
80 93
81sub new { 94sub new {
82 my $class = shift; 95 my $class = shift;
83 96
84 bless { 97 my $self = bless {
85 x => 0, 98 x => 0,
86 y => 0, 99 y => 0,
87 z => 0, 100 z => 0,
88 w => -1,
89 h => -1,
90 @_ 101 @_
91 }, $class 102 }, $class;
103
104 for (keys %$self) {
105 if (/^connect_(.*)$/) {
106 $self->connect ($1 => delete $self->{$_});
107 }
108 }
109
110 $self
111}
112
113sub show {
114 my ($self) = @_;
115
116 return if $self->{parent};
117
118 $CFClient::UI::ROOT->add ($self);
119}
120
121sub hide {
122 my ($self) = @_;
123
124 return unless $self->{parent};
125
126 $self->{parent}->remove ($self);
92} 127}
93 128
94sub move { 129sub move {
95 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
96 $self->{x} = $x; 132 $self->{x} = int $x;
97 $self->{y} = $y; 133 $self->{y} = int $y;
98 $self->{z} = $z if defined $z; 134 $self->{z} = $z if defined $z;
135
136 $self->update;
99} 137}
100 138
101sub needs_redraw { 139sub needs_redraw {
102 0 140 0
103} 141}
105sub size_request { 143sub size_request {
106 require Carp; 144 require Carp;
107 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abtract";
108} 146}
109 147
110sub _size_allocate { 148sub configure {
111 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
112 150
113 $self->{x} = $x; 151 $self->{x} = $x;
114 $self->{y} = $y; 152 $self->{y} = $y;
115 153
154 if ($self->{aspect}) {
155 $w = List::Util::min $w, int $h * $self->{aspect};
156 $h = List::Util::min $h, int $w / $self->{aspect};
157 }
158
116 return unless $self->{w} != $w || $self->{h} != $h; 159 return unless $self->{w} != $w || $self->{h} != $h;
117 160
118 $self->{w} = $w; 161 $self->{w} = $w;
119 $self->{h} = $h; 162 $self->{h} = $h;
120 163
121 1 164 $self->size_allocate ($w, $h);
165 $self->update;
122} 166}
123 167
124sub size_allocate { 168sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_; 169 # nothing to be done
126
127 $self->_size_allocate ($x, $y, $w, $h);
128} 170}
129 171
130# translate global koordinates to local coordinate system 172# return top left coordinates
131sub translate { 173sub _topleft {
132 my ($self, $x, $y) = @_; 174 my ($self, $x, $y) = @_;
133 175
134 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 176 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
177}
178
179# translate global coordinates to local coordinate system
180sub coord2local {
181 my ($self, $x, $y) = @_;
182
183 my ($X, $Y) = $self->_topleft;
184 ($x - $X, $y - $Y)
185}
186
187# translate local coordinates to global coordinate system
188sub coord2global {
189 my ($self, $x, $y) = @_;
190
191 my ($X, $Y) = $self->_topleft;
192 ($x + $X, $y + $Y)
135} 193}
136 194
137sub focus_in { 195sub focus_in {
138 my ($self) = @_; 196 my ($self) = @_;
139 197
140 return if $FOCUS == $self; 198 return if $FOCUS == $self;
199 return unless $self->{can_focus};
141 200
142 my $focus = $FOCUS; $FOCUS = $self; 201 my $focus = $FOCUS; $FOCUS = $self;
202
203 $self->emit (focus_in => $focus);
204
143 $focus->update if $focus; 205 $focus->update if $focus;
144 $FOCUS->update; 206 $FOCUS->update;
145} 207}
146 208
147sub focus_out { 209sub focus_out {
148 my ($self) = @_; 210 my ($self) = @_;
149 211
150 return unless $FOCUS == $self; 212 return unless $FOCUS == $self;
151 213
152 my $focus = $FOCUS; undef $FOCUS; 214 my $focus = $FOCUS; undef $FOCUS;
215
216 $self->emit (focus_out => $focus);
217
153 $focus->update if $focus; #? 218 $focus->update if $focus; #?
154} 219}
155 220
156sub mouse_motion { } 221sub mouse_motion { }
157sub button_up { } 222sub button_up { }
178 glPushMatrix; 243 glPushMatrix;
179 glTranslate $self->{x}, $self->{y}, 0; 244 glTranslate $self->{x}, $self->{y}, 0;
180 $self->_draw; 245 $self->_draw;
181 glPopMatrix; 246 glPopMatrix;
182 247
183 if ($self == $HOVER) { 248 if ($self == $HOVER && $self->{can_hover}) {
184 my ($x, $y) = @$self{qw(x y)}; 249 my ($x, $y) = @$self{qw(x y)};
185 250
186 glColor 0, 0, 1, 0.2; 251 glColor 1, 0.8, 0.5, 0.2;
187 glEnable GL_BLEND; 252 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 253 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
189 glBegin GL_QUADS; 254 glBegin GL_QUADS;
190 glVertex $x , $y; 255 glVertex $x , $y;
191 glVertex $x + $self->{w}, $y; 256 glVertex $x + $self->{w}, $y;
200 my ($self) = @_; 265 my ($self) = @_;
201 266
202 warn "no draw defined for $self\n"; 267 warn "no draw defined for $self\n";
203} 268}
204 269
205sub bbox {
206 my ($self) = @_;
207 my ($w, $h) = $self->size_request;
208 (
209 $self->{x},
210 $self->{y},
211 $self->{x} = $w,
212 $self->{y} = $h
213 )
214}
215
216sub find_widget { 270sub find_widget {
217 my ($self, $x, $y) = @_; 271 my ($self, $x, $y) = @_;
218 272
219 return $self 273 return $self
220 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 274 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
221 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 275 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
222 276
223 () 277 ()
224} 278}
225 279
226sub del_parent { $_[0]->{parent} = undef }
227
228sub set_parent { 280sub set_parent {
229 my ($self, $par) = @_; 281 my ($self, $parent) = @_;
230 282
231 $self->{parent} = $par;
232 Scalar::Util::weaken $self->{parent}; 283 Scalar::Util::weaken ($self->{parent} = $parent);
233} 284}
234 285
235sub get_parent { 286sub check_size {
236 $_[0]->{parent} 287 my ($self) = @_;
288
289 my ($w, $h) = $self->size_request;
290
291 if ($w != $self->{req_w} || $h != $self->{req_h}) {
292 $self->{req_w} = $w;
293 $self->{req_h} = $h;
294
295 $self->{parent}->check_size
296 if $self->{parent};
297 }
237} 298}
238 299
239sub update { 300sub update {
240 my ($self) = @_; 301 my ($self) = @_;
241 302
244} 305}
245 306
246sub connect { 307sub connect {
247 my ($self, $signal, $cb) = @_; 308 my ($self, $signal, $cb) = @_;
248 309
249 push @{ $self->{cb}{$signal} }, $cb; 310 push @{ $self->{signal_cb}{$signal} }, $cb;
250} 311}
251 312
252sub emit { 313sub emit {
253 my ($self, $signal, @args) = @_; 314 my ($self, $signal, @args) = @_;
254 315
316 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
255 $_->($self, @args) 317 $cb->($self, @args);
256 for @{$self->{cb}{$signal} || []}; 318 }
257} 319}
258 320
259sub DESTROY { 321sub DESTROY {
260 my ($self) = @_; 322 my ($self) = @_;
261 323
267package CFClient::UI::DrawBG; 329package CFClient::UI::DrawBG;
268 330
269our @ISA = CFClient::UI::Base::; 331our @ISA = CFClient::UI::Base::;
270 332
271use strict; 333use strict;
272use SDL::OpenGL; 334use CFClient::OpenGL;
273 335
274sub new { 336sub new {
275 my $class = shift; 337 my $class = shift;
276 338
277 # range [value, low, high, page] 339 # range [value, low, high, page]
278 340
279 $class->SUPER::new ( 341 $class->SUPER::new (
280 bg => [0, 0, 0, 0.2], 342 bg => [0, 0, 0, 0.2],
281 active_bg => [1, 1, 1], 343 active_bg => [1, 1, 1, 0.5],
282 @_ 344 @_
283 ) 345 )
284} 346}
285 347
286sub _draw { 348sub _draw {
330 392
331 $self 393 $self
332} 394}
333 395
334sub add { 396sub add {
335 my ($self, $chld, $expand) = @_; 397 my ($self, $child) = @_;
336 398
337 $chld->{expand} = $expand;
338 $chld->set_parent ($self); 399 $child->set_parent ($self);
400
401 use sort 'stable';
339 402
340 $self->{children} = [ 403 $self->{children} = [
341 sort { $a->{z} <=> $b->{z} } 404 sort { $a->{z} <=> $b->{z} }
342 @{$self->{children}}, $chld 405 @{$self->{children}}, $child
343 ]; 406 ];
344 407
345 $self->{w} = $self->{h} = -1; 408 $child->check_size;
346 $self->update;
347} 409}
348 410
349sub remove { 411sub remove {
350 my ($self, $widget) = @_; 412 my ($self, $child) = @_;
351 413
414 delete $child->{parent};
415
352 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 416 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
353 417
354 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 418 $self->check_size;
355} 419}
356 420
357sub find_widget { 421sub find_widget {
358 my ($self, $x, $y) = @_; 422 my ($self, $x, $y) = @_;
359 423
389 453
390 $class->SUPER::new (children => [$child], %arg) 454 $class->SUPER::new (children => [$child], %arg)
391} 455}
392 456
393sub add { 457sub add {
394 my ($self, $widget) = @_; 458 my ($self, $child) = @_;
395 459
396 $self->{children} = []; 460 $self->{children} = [];
397 461
398 $self->SUPER::add ($widget); 462 $self->SUPER::add ($child);
399} 463}
400 464
401sub remove { 465sub remove {
402 my ($self, $widget) = @_; 466 my ($self, $widget) = @_;
403 467
412sub size_request { 476sub size_request {
413 $_[0]{children}[0]->size_request 477 $_[0]{children}[0]->size_request
414} 478}
415 479
416sub size_allocate { 480sub size_allocate {
417 my ($self, $x, $y, $w, $h) = @_; 481 my ($self, $w, $h) = @_;
418 482
419 $self->_size_allocate ($x, $y, $w, $h) or return;
420
421 $self->{children}[0]->size_allocate (0, 0, $w, $h); 483 $self->{children}[0]->configure (0, 0, $w, $h);
422} 484}
423 485
424############################################################################# 486#############################################################################
425 487
426package CFClient::UI::Window; 488package CFClient::UI::Window;
427 489
428our @ISA = CFClient::UI::Bin::; 490our @ISA = CFClient::UI::Bin::;
429 491
430use SDL::OpenGL; 492use CFClient::OpenGL;
431 493
432sub new { 494sub new {
433 my ($class, %arg) = @_; 495 my ($class, %arg) = @_;
434 496
435 my $self = $class->SUPER::new (%arg); 497 my $self = $class->SUPER::new (%arg);
444} 506}
445 507
446sub render_chld { 508sub render_chld {
447 my ($self) = @_; 509 my ($self) = @_;
448 510
449 $self->{texture} = 511 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
450 CFClient::Texture->new_from_opengl ( 512 glClearColor 0, 0, 0, 1;
451 $self->{w}, $self->{h}, sub { $self->child->draw } 513 glClear GL_COLOR_BUFFER_BIT;
452 ); 514 $self->child->draw;
515 };
453} 516}
454 517
455sub size_allocate { 518sub size_allocate {
456 my ($self, $x, $y, $w, $h) = @_; 519 my ($self, $w, $h) = @_;
457 520
458 $self->_size_allocate ($x, $y, $w, $h) or return;
459
460 $self->child->size_allocate (0, 0, $w, $h); 521 $self->child->configure (0, 0, $w, $h);
461 522
462 $self->render_chld; 523 $self->render_chld;
463} 524}
464 525
465sub _draw { 526sub _draw {
481 glDisable GL_TEXTURE_2D; 542 glDisable GL_TEXTURE_2D;
482} 543}
483 544
484############################################################################# 545#############################################################################
485 546
547package CFClient::UI::ViewPort;
548
549our @ISA = CFClient::UI::Window::;
550
551sub new { die }
552
553sub size_request {
554 my ($self) = @_;
555
556 @$self{qw(child_w child_h)} = $self->child->size_request;
557 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
558
559 @$self{qw(child_w child_h)}
560}
561
562sub _draw {
563 my ($self) = @_;
564
565 $self->{children}[1]->draw;
566}
567
568
569#############################################################################
570
486package CFClient::UI::Frame; 571package CFClient::UI::Frame;
487 572
488our @ISA = CFClient::UI::Bin::; 573our @ISA = CFClient::UI::Bin::;
489 574
490use SDL::OpenGL; 575use CFClient::OpenGL;
491 576
492sub size_request { 577sub size_request {
493 my ($self) = @_; 578 my ($self) = @_;
494 my $chld = $self->child 579 my $chld = $self->child
495 or return (0, 0); 580 or return (0, 0);
500} 585}
501 586
502sub size_allocate { 587sub size_allocate {
503 my ($self, $x, $y, $w, $h) = @_; 588 my ($self, $x, $y, $w, $h) = @_;
504 589
505 $self->_size_allocate ($x, $y, $w, $h) or return;
506
507 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 590 $self->child->configure (2, 2, $w - 4, $h - 4);
508} 591}
509 592
510sub _draw { 593sub _draw {
511 my ($self) = @_; 594 my ($self) = @_;
512 595
529 612
530package CFClient::UI::FancyFrame; 613package CFClient::UI::FancyFrame;
531 614
532our @ISA = CFClient::UI::Bin::; 615our @ISA = CFClient::UI::Bin::;
533 616
534use SDL::OpenGL; 617use CFClient::OpenGL;
535 618
536my @tex = 619my @tex =
537 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 620 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
538 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 621 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
539 622
623sub new {
624 my $class = shift;
625
626 # TODO: user_x, user_y, overwrite moveto?
627
628 $class->SUPER::new (
629 bg => [1, 1, 1, 1],
630 border_bg => [1, 1, 1, 1],
631 border => 0.8,
632 @_
633 )
634}
635
636sub border {
637 int $_[0]{border} * $::FONTSIZE
638}
639
540sub size_request { 640sub size_request {
541 my ($self) = @_; 641 my ($self) = @_;
542 642
643 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
644
543 my ($w, $h) = $self->SUPER::size_request; 645 my ($w, $h) = $self->SUPER::size_request;
544 646
545 $h += $tex[1]->{h}; 647 (
546 $h += $tex[4]->{h}; 648 $w + $self->border * 2,
547 $w += $tex[2]->{w}; 649 $h + $self->border * 2,
548 $w += $tex[3]->{w}; 650 )
549
550 ($w, $h)
551} 651}
552 652
553sub size_allocate { 653sub size_allocate {
554 my ($self, $x, $y, $w, $h) = @_; 654 my ($self, $w, $h) = @_;
555 655
556 $self->_size_allocate ($x, $y, $w, $h) or return; 656 $h -= List::Util::max 0, $self->border * 2;
657 $w -= List::Util::max 0, $self->border * 2;
557 658
558 $h -= $tex[1]->{h}; 659 $self->child->configure ($self->border, $self->border, $w, $h);
559 $h -= $tex[4]->{h}; 660}
560 $w -= $tex[2]->{w};
561 $w -= $tex[3]->{w};
562 661
563 $h = $h < 0 ? 0 : $h; 662sub button_down {
564 $w = $w < 0 ? 0 : $w; 663 my ($self, $ev, $x, $y) = @_;
565 664
566 my $child = $self->child; 665 my $border = $self->border;
567 666
568 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); 667 if ($x < $self->{w} && $x >= $self->{w} - $border
668 && $y < $self->{h} && $y >= $self->{h} - $border) {
669
670 my ($ox, $oy) = ($ev->{x}, $ev->{y});
671 my ($bw, $bh) = ($self->{w}, $self->{h});
672
673 $self->{motion} = sub {
674 my ($ev, $x, $y) = @_;
675
676 ($x, $y) = ($ev->{x}, $ev->{y});
677
678 $self->{user_w} = $bw + $x - $ox;
679 $self->{user_h} = $bh + $y - $oy;
680 $self->check_size;
681 };
682
683 } elsif ($x >= 0 && $x < $self->{w}
684 && $y >= 0 && $y < $border) {
685
686 my ($ox, $oy) = ($ev->{x}, $ev->{y});
687 my ($bx, $by) = ($self->{x}, $self->{y});
688
689 $self->{motion} = sub {
690 my ($ev, $x, $y) = @_;
691
692 ($x, $y) = ($ev->{x}, $ev->{y});
693
694 $self->move ($bx + $x - $ox, $by + $y - $oy);
695 $self->update;
696 };
697 }
698}
699
700sub button_up {
701 my ($self, $ev, $x, $y) = @_;
702
703 delete $self->{motion};
704}
705
706sub mouse_motion {
707 my ($self, $ev, $x, $y) = @_;
708
709 $self->{motion}->($ev, $x, $y) if $self->{motion};
569} 710}
570 711
571sub _draw { 712sub _draw {
572 my ($self) = @_; 713 my ($self) = @_;
573 714
574 my ($w, $h) = ($self->{w}, $self->{h}); 715 my ($w, $h ) = ($self->{w}, $self->{h});
575 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 716 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
717
718 glEnable GL_BLEND;
719 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
720 glEnable GL_TEXTURE_2D;
721 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
722
723 my $border = $self->border;
724
725 glColor @{ $self->{border_bg} };
726 $tex[1]->draw_quad (0, 0, $w, $border);
727 $tex[3]->draw_quad (0, $border, $border, $ch);
728 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
729 $tex[4]->draw_quad (0, $h - $border, $w, $border);
730
731 my $bg = $tex[0];
732
733 # TODO: repeat texture not scale
734 my $rep_x = $cw / $bg->{w};
735 my $rep_y = $ch / $bg->{h};
736
737 glColor @{ $self->{bg} };
738
739 $bg->{s} = $rep_x;
740 $bg->{t} = $rep_y;
741 $bg->{wrap_mode} = 1;
742 $bg->draw_quad ($border, $border, $cw, $ch);
743
744 glDisable GL_TEXTURE_2D;
745 glDisable GL_BLEND;
746
747 $self->child->draw;
748}
749
750#############################################################################
751
752package CFClient::UI::Table;
753
754our @ISA = CFClient::UI::Base::;
755
756use List::Util qw(max sum);
757
758use CFClient::OpenGL;
759
760sub new {
761 my $class = shift;
762
763 $class->SUPER::new (
764 col_expand => [],
765 @_
766 )
767}
768
769sub add {
770 my ($self, $x, $y, $child) = @_;
771
772 $child->set_parent ($self);
773 $self->{children}[$y][$x] = $child;
774
775 $child->check_size;
776}
777
778# TODO: move to container class maybe? send childs a signal on removal?
779sub clear {
780 my ($self) = @_;
781
782 delete $self->{children};
783 $self->update;
784}
785
786sub get_wh {
787 my ($self) = @_;
788
789 my (@w, @h);
790
791 for my $y (0 .. $#{$self->{children}}) {
792 my $row = $self->{children}[$y]
793 or next;
794
795 for my $x (0 .. $#$row) {
796 my $widget = $row->[$x]
797 or next;
798 my ($w, $h) = $widget->size_request;
799
800 $w[$x] = max $w[$x], $w;
801 $h[$y] = max $h[$y], $h;
802 }
803 }
804
805 (\@w, \@h)
806}
807
808sub size_request {
809 my ($self) = @_;
810
811 my ($ws, $hs) = $self->get_wh;
812
813 (
814 (sum @$ws),
815 (sum @$hs),
816 )
817}
818
819sub size_allocate {
820 my ($self, $w, $h) = @_;
821
822 my ($ws, $hs) = $self->get_wh;
823
824 my $req_w = sum @$ws;
825 my $req_h = sum @$hs;
826
827 # TODO: nicer code && do row_expand
828 my @col_expand = @{$self->{col_expand}};
829 @col_expand = (1) x @$ws unless @col_expand;
830 my $col_expand = (sum @col_expand) || 1;
831
832 # linearly scale sizes
833 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
834 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
835
836 CFClient::UI::harmonize $ws;
837 CFClient::UI::harmonize $hs;
838
839 my $y;
840
841 for my $r (0 .. $#{$self->{children}}) {
842 my $row = $self->{children}[$r]
843 or next;
844
845 my $x = 0;
846 my $row_h = $hs->[$r];
847
848 for my $c (0 .. $#$row) {
849 my $col_w = $ws->[$c];
850
851 if (my $widget = $row->[$c]) {
852 $widget->configure ($x, $y, $col_w, $row_h);
853 }
854
855 $x += $col_w;
856 }
857
858 $y += $row_h;
859 }
860
861}
862
863sub find_widget {
864 my ($self, $x, $y) = @_;
865
866 $x -= $self->{x};
867 $y -= $self->{y};
868
869 my $res;
870
871 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
872 $res = $_->find_widget ($x, $y)
873 and return $res;
874 }
875
876 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
877}
878
879sub _draw {
880 my ($self) = @_;
881
882 for (grep $_, @{$self->{children}}) {
883 $_->draw for grep $_, @$_;
884 }
885}
886
887#############################################################################
888
889package CFClient::UI::HBox;
890
891# TODO: wrap into common Box base class
892
893our @ISA = CFClient::UI::Container::;
894
895sub size_request {
896 my ($self) = @_;
897
898 my @alloc = map [$_->size_request], @{$self->{children}};
899
900 (
901 (List::Util::sum map $_->[0], @alloc),
902 (List::Util::max map $_->[1], @alloc),
903 )
904}
905
906sub size_allocate {
907 my ($self, $w, $h) = @_;
908
909 ($h, $w) = ($w, $h);
910
911 my $children = $self->{children};
912
913 my @h = map +($_->size_request)[0], @$children;
914
915 my $req_h = List::Util::sum @h;
916
917 if ($req_h > $h) {
918 # ah well, not enough space
919 $_ *= $h / $req_h for @h;
920 } else {
921 my $exp = List::Util::sum map $_->{expand}, @$children;
922 $exp ||= 1;
923
924 for (0 .. $#$children) {
925 my $child = $children->[$_];
926
927 my $alloc_h = $h[$_];
928 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
929 $h[$_] = $alloc_h;
930 }
931 }
932
933 CFClient::UI::harmonize \@h;
934
935 my $y = 0;
936 for (0 .. $#$children) {
937 my $child = $children->[$_];
938 my $h = $h[$_];
939 $child->configure ($y, 0, $h, $w);
940
941 $y += $h;
942 }
943
944 1
945}
946
947#############################################################################
948
949package CFClient::UI::VBox;
950
951# TODO: wrap into common Box base class
952
953our @ISA = CFClient::UI::Container::;
954
955sub size_request {
956 my ($self) = @_;
957
958 my @alloc = map [$_->size_request], @{$self->{children}};
959
960 (
961 (List::Util::max map $_->[0], @alloc),
962 (List::Util::sum map $_->[1], @alloc),
963 )
964}
965
966sub size_allocate {
967 my ($self, $w, $h) = @_;
968
969 my $children = $self->{children};
970
971 my @h = map +($_->size_request)[1], @$children;
972
973 my $req_h = List::Util::sum @h;
974
975 if ($req_h > $h) {
976 # ah well, not enough space
977 $_ *= $h / $req_h for @h;
978 } else {
979 my $exp = List::Util::sum map $_->{expand}, @$children;
980 $exp ||= 1;
981
982 for (0 .. $#$children) {
983 my $child = $children->[$_];
984
985 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
986 }
987 }
988
989 CFClient::UI::harmonize \@h;
990
991 my $y = 0;
992 for (0 .. $#$children) {
993 my $child = $children->[$_];
994 my $h = $h[$_];
995 $child->configure (0, $y, $w, $h);
996
997 $y += $h;
998 }
999
1000 1
1001}
1002
1003#############################################################################
1004
1005package CFClient::UI::Label;
1006
1007our @ISA = CFClient::UI::Base::;
1008
1009use CFClient::OpenGL;
1010
1011sub new {
1012 my ($class, %arg) = @_;
1013
1014 my $self = $class->SUPER::new (
1015 fg => [1, 1, 1],
1016 fontsize => 1,
1017 text => "",
1018 align => -1,
1019 valign => -1,
1020 padding => 2,
1021 layout => new CFClient::Layout,
1022 %arg
1023 );
1024
1025 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d#
1026
1027 $self->set_text (delete $self->{text}) if exists $self->{text};
1028 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1029
1030 $self
1031}
1032
1033sub escape_text {
1034 local $_ = $_[1];
1035
1036 s/&/&amp;/g;
1037 s/>/&gt;/g;
1038 s/</&lt;/g;
1039
1040 $_[1]
1041}
1042
1043sub set_text {
1044 my ($self, $text) = @_;
1045
1046 $self->{layout}->set_text ($text);
1047
1048 delete $self->{texture};
1049 $self->update;
1050}
1051
1052sub set_markup {
1053 my ($self, $markup) = @_;
1054
1055 $self->{layout}->set_markup ($markup);
1056
1057 delete $self->{texture};
1058 $self->update;
1059}
1060
1061sub size_request {
1062 my ($self) = @_;
1063
1064 $self->{layout}->set_width;
1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1066
1067 my ($w, $h) = $self->{layout}->size;
1068
1069 (
1070 $w + $self->{padding} * 2,
1071 $h + $self->{padding} * 2,
1072 )
1073}
1074
1075sub size_allocate {
1076 my ($self, $w, $h) = @_;
1077
1078 delete $self->{texture};
1079}
1080
1081sub _draw {
1082 my ($self) = @_;
1083
1084 my $tex = $self->{texture} ||= do {
1085 $self->{layout}->set_width ($self->{w});
1086 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1087 new_from_layout CFClient::Texture $self->{layout}
1088 };
576 1089
577 glEnable GL_BLEND; 1090 glEnable GL_BLEND;
578 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1091 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
579 glEnable GL_TEXTURE_2D; 1092 glEnable GL_TEXTURE_2D;
580 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1093 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
581 1094
582 my $top = $tex[1]; 1095 glColor @{$self->{fg}};
583 $top->draw_quad (0, 0, $w, $top->{h});
584 1096
585 my $left = $tex[3]; 1097 $self->{ox} = int (
586 $left->draw_quad (0, $top->{h}, $left->{w}, $ch); 1098 $self->{align} < 0 ? $self->{padding}
1099 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1100 : ($self->{w} - $tex->{w}) * 0.5
1101 );
587 1102
588 my $right = $tex[2]; 1103 $self->{oy} = int (
589 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); 1104 $self->{valign} < 0 ? $self->{padding}
1105 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1106 : ($self->{h} - $tex->{h}) * 0.5
1107 );
590 1108
591 my $bottom = $tex[4]; 1109 $tex->draw_quad ($self->{ox}, $self->{oy});
592 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
593
594 my $bg = $tex[0];
595
596 glBindTexture GL_TEXTURE_2D, $bg->{name};
597 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
598 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
599 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
600
601 my $rep_x = $cw / $bg->{w};
602 my $rep_y = $ch / $bg->{h};
603
604 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
605 1110
606 glDisable GL_TEXTURE_2D; 1111 glDisable GL_TEXTURE_2D;
607 glDisable GL_BLEND; 1112 glDisable GL_BLEND;
608
609 $self->child->draw;
610
611} 1113}
612 1114
613############################################################################# 1115#############################################################################
614 1116
615package CFClient::UI::Table;
616
617our @ISA = CFClient::UI::Base::;
618
619use List::Util qw(max sum);
620
621use SDL::OpenGL;
622
623sub add {
624 my ($self, $x, $y, $chld) = @_;
625
626 $self->{children}[$y][$x] = $chld;
627 $chld->set_parent ($self);
628
629 $self->{w} = $self->{h} = -1;
630 $self->update;
631}
632
633sub get_wh {
634 my ($self) = @_;
635
636 my (@w, @h);
637
638 for my $y (0 .. $#{$self->{children}}) {
639 my $row = $self->{children}[$y]
640 or next;
641
642 for my $x (0 .. $#$row) {
643 my $widget = $row->[$x]
644 or next;
645 my ($w, $h) = $widget->size_request;
646
647 $w[$x] = max $w[$x], $w;
648 $h[$y] = max $h[$y], $h;
649 }
650 }
651
652 (\@w, \@h)
653}
654
655sub size_request {
656 my ($self) = @_;
657
658 my ($ws, $hs) = $self->get_wh;
659
660 (
661 (List::Util::sum @$ws),
662 (List::Util::sum @$hs),
663 )
664}
665
666sub size_allocate {
667 my ($self, $x, $y, $w, $h) = @_;
668
669 $self->_size_allocate ($x, $y, $w, $h) or return;
670
671 my ($ws, $hs) = $self->get_wh;
672
673 my $req_w = List::Util::sum @$ws;
674 my $req_h = List::Util::sum @$hs;
675
676 # linearly scale sizes
677 $_ *= $req_w / $w for @$ws;
678 $_ *= $req_h / $h for @$hs;
679
680 my $y;
681
682 for my $r (0 .. $#{$self->{children}}) {
683 my $row = $self->{children}[$r]
684 or next;
685
686 my $x = 0;
687 my $row_h = $hs->[$r];
688
689 for my $c (0 .. $#$row) {
690 my $widget = $row->[$c]
691 or next;
692
693 my $col_w = $ws->[$c];
694
695 $widget->size_allocate ($x, $y, $col_w, $row_h);
696
697 $x += $col_w;
698 }
699
700 $y += $row_h;
701 }
702
703}
704
705sub find_widget {
706 my ($self, $x, $y) = @_;
707
708 $x -= $self->{x};
709 $y -= $self->{y};
710
711 my $res;
712
713 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
714 $res = $_->find_widget ($x, $y)
715 and return $res;
716 }
717
718 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
719}
720
721sub _draw {
722 my ($self) = @_;
723
724 for (grep $_, @{$self->{children}}) {
725 $_->draw for grep $_, @$_;
726 }
727}
728
729#############################################################################
730
731package CFClient::UI::HBox;
732
733# TODO: wrap into common Box base class
734
735our @ISA = CFClient::UI::Container::;
736
737sub size_request {
738 my ($self) = @_;
739
740 my @alloc = map [$_->size_request], @{$self->{children}};
741
742 (
743 (List::Util::sum map $_->[0], @alloc),
744 (List::Util::max map $_->[1], @alloc),
745 )
746}
747
748sub size_allocate {
749 my ($self, $x, $y, $w, $h) = @_;
750
751 $self->_size_allocate ($x, $y, $w, $h) or return;
752
753 return unless $self->{w};
754
755 ($h, $w) = ($w, $h);
756
757 my $children = $self->{children};
758
759 my @h = map +($_->size_request)[0], @$children;
760
761 my $req_h = List::Util::sum @h;
762
763 if ($req_h > $h) {
764 # ah well, not enough space
765 $_ = $h[$_] * $h / $req_h for @h;
766 } else {
767 my @exp = grep $_->{expand}, @$children;
768 @exp = @$children unless @exp;
769
770 my %exp = map +($_ => 1), @exp;
771
772 for (0 .. $#$children) {
773 my $child = $children->[$_];
774
775 my $alloc_h = $h[$_];
776 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
777 $h[$_] = $alloc_h;
778 }
779 }
780
781 my $y = 0;
782 for (0 .. $#$children) {
783 my $child = $children->[$_];
784 my $h = $h[$_];
785 $child->size_allocate ($y, 0, $h, $w);
786
787 $y += $h;
788 }
789}
790
791#############################################################################
792
793package CFClient::UI::VBox;
794
795# TODO: wrap into common Box base class
796
797our @ISA = CFClient::UI::Container::;
798
799sub size_request {
800 my ($self) = @_;
801
802 my @alloc = map [$_->size_request], @{$self->{children}};
803
804 (
805 (List::Util::max map $_->[0], @alloc),
806 (List::Util::sum map $_->[1], @alloc),
807 )
808}
809
810sub size_allocate {
811 my ($self, $x, $y, $w, $h) = @_;
812
813 $self->_size_allocate ($x, $y, $w, $h) or return;
814
815 return unless $self->{h};
816
817 my $children = $self->{children};
818
819 my @h = map +($_->size_request)[1], @$children;
820
821 my $req_h = List::Util::sum @h;
822
823 if ($req_h > $h) {
824 # ah well, not enough space
825 $_ = $h[$_] * $h / $req_h for @h;
826 } else {
827 my @exp = grep $_->{expand}, @$children;
828 @exp = @$children unless @exp;
829
830 my %exp = map +($_ => 1), @exp;
831
832 for (0 .. $#$children) {
833 my $child = $children->[$_];
834
835 my $alloc_h = $h[$_];
836 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
837 $h[$_] = $alloc_h;
838 }
839 }
840
841 my $y = 0;
842 for (0 .. $#$children) {
843 my $child = $children->[$_];
844 my $h = $h[$_];
845 $child->size_allocate (0, $y, $w, $h);
846
847 $y += $h;
848 }
849}
850
851#############################################################################
852
853package CFClient::UI::Label;
854
855our @ISA = CFClient::UI::Base::;
856
857use SDL::OpenGL;
858
859sub new {
860 my ($class, %arg) = @_;
861
862 my $self = $class->SUPER::new (
863 fg => [1, 1, 1],
864 height => $::FONTSIZE,
865 text => "",
866 align => -1,
867 padding => 2,
868 layout => new CFClient::Layout,
869 %arg
870 );
871
872 $self->set_text ($self->{text});
873
874 $self
875}
876
877sub escape_text {
878 local $_ = $_[1];
879
880 s/&/&amp;/g;
881 s/>/&gt;/g;
882 s/</&lt;/g;
883
884 $_[1]
885}
886
887sub set_text {
888 my ($self, $text) = @_;
889
890 $self->{text} = $text;
891 $self->{layout}->set_markup ($text);
892
893 delete $self->{texture};
894 $self->update;
895}
896
897sub get_text {
898 my ($self, $text) = @_;
899
900 $self->{text}
901}
902
903sub size_request {
904 my ($self) = @_;
905
906 $self->{layout}->set_width;
907 $self->{layout}->set_height ($self->{height});
908 my ($w, $h) = $self->{layout}->size;
909
910 (
911 $w + $self->{padding} * 2,
912 $h + $self->{padding} * 2,
913 )
914}
915
916sub size_allocate {
917 my ($self, $x, $y, $w, $h) = @_;
918
919 $self->_size_allocate ($x, $y, $w, $h) or return;
920
921 delete $self->{texture};
922}
923
924sub update {
925 my ($self) = @_;
926
927 delete $self->{texture};
928 $self->SUPER::update;
929}
930
931sub _draw {
932 my ($self) = @_;
933
934 my $tex = $self->{texture} ||= do {
935 $self->{layout}->set_width ($self->{w});
936 new_from_layout CFClient::Texture $self->{layout}
937 };
938
939 glEnable GL_BLEND;
940 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
941 glEnable GL_TEXTURE_2D;
942 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
943
944 glColor @{$self->{fg}};
945
946 my $x =
947 $self->{align} < 0 ? $self->{padding}
948 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
949 : ($self->{w} - $tex->{w}) * 0.5;
950
951 $tex->draw_quad ($x, 0);
952
953 glDisable GL_TEXTURE_2D;
954 glDisable GL_BLEND;
955}
956
957#############################################################################
958
959package CFClient::UI::Entry; 1117package CFClient::UI::EntryBase;
960 1118
961our @ISA = CFClient::UI::Label::; 1119our @ISA = CFClient::UI::Label::;
962 1120
963use SDL; 1121use CFClient::OpenGL;
964use SDL::OpenGL;
965 1122
966sub new { 1123sub new {
967 my $class = shift; 1124 my $class = shift;
968 1125
969 $class->SUPER::new ( 1126 $class->SUPER::new (
970 fg => [1, 1, 1], 1127 fg => [1, 1, 1],
971 bg => [0, 0, 0, 0.2], 1128 bg => [0, 0, 0, 0.2],
972 active_bg => [1, 1, 1], 1129 active_bg => [1, 1, 1, 0.5],
973 active_fg => [0, 0, 0], 1130 active_fg => [0, 0, 0],
1131 can_hover => 1,
1132 can_focus => 1,
1133 valign => 0,
974 @_ 1134 @_
975 ) 1135 )
976} 1136}
977 1137
978sub _set_text { 1138sub _set_text {
979 my ($self, $text) = @_; 1139 my ($self, $text) = @_;
980 1140
1141 delete $self->{cur_h};
1142
1143 return if $self->{text} eq $text;
1144
1145 delete $self->{texture};
1146
981 $self->{last_activity} = $::NOW; 1147 $self->{last_activity} = $::NOW;
982
983 $self->{text} = $text; 1148 $self->{text} = $text;
984 $self->{layout}->set_width ($self->{w});
985 1149
986 $text =~ s/./*/g if $self->{hidden}; 1150 $text =~ s/./*/g if $self->{hidden};
1151 $self->{layout}->set_text ("$text ");
987 1152
988 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1153 $self->emit (changed => $self->{text});
1154}
989 1155
990 $text = substr $text, 0, $self->{cursor}; 1156sub get_text {
991 utf8::encode $text; 1157 $_[0]{text}
992
993 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
994} 1158}
995 1159
996sub size_request { 1160sub size_request {
997 my ($self) = @_; 1161 my ($self) = @_;
998 1162
1000 1164
1001 ($w + 1, $h) # add 1 for cursor 1165 ($w + 1, $h) # add 1 for cursor
1002} 1166}
1003 1167
1004sub size_allocate { 1168sub size_allocate {
1005 my ($self, $x, $y, $w, $h) = @_; 1169 my ($self, $w, $h) = @_;
1006
1007 $self->SUPER::size_allocate ($x, $y, $w, $h);
1008 1170
1009 $self->_set_text ($self->{text}); 1171 $self->_set_text ($self->{text});
1010} 1172}
1011 1173
1012sub set_text { 1174sub set_text {
1018} 1180}
1019 1181
1020sub key_down { 1182sub key_down {
1021 my ($self, $ev) = @_; 1183 my ($self, $ev) = @_;
1022 1184
1023 my $mod = $ev->key_mod; 1185 my $mod = $ev->{mod};
1024 my $sym = $ev->key_sym; 1186 my $sym = $ev->{sym};
1025
1026 my $uni = $ev->key_unicode; 1187 my $uni = $ev->{unicode};
1027 1188
1028 my $text = $self->get_text; 1189 my $text = $self->get_text;
1029 1190
1030 if ($sym == SDLK_BACKSPACE) { 1191 if ($sym == 8) {
1031 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1192 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1032 } elsif ($sym == SDLK_DELETE) { 1193 } elsif ($sym == 127) {
1033 substr $text, $self->{cursor}, 1, ""; 1194 substr $text, $self->{cursor}, 1, "";
1034 } elsif ($sym == SDLK_LEFT) { 1195 } elsif ($sym == CFClient::SDLK_LEFT) {
1035 --$self->{cursor} if $self->{cursor}; 1196 --$self->{cursor} if $self->{cursor};
1036 } elsif ($sym == SDLK_RIGHT) { 1197 } elsif ($sym == CFClient::SDLK_RIGHT) {
1037 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1198 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1038 } elsif ($sym == SDLK_HOME) { 1199 } elsif ($sym == CFClient::SDLK_HOME) {
1039 $self->{cursor} = 0; 1200 $self->{cursor} = 0;
1040 } elsif ($sym == SDLK_END) { 1201 } elsif ($sym == CFClient::SDLK_END) {
1041 $self->{cursor} = length $text; 1202 $self->{cursor} = length $text;
1203 } elsif ($sym == 27) {
1204 $self->emit ('escape');
1042 } elsif ($uni) { 1205 } elsif ($uni) {
1043 substr $text, $self->{cursor}++, 0, chr $uni; 1206 substr $text, $self->{cursor}++, 0, chr $uni;
1044 } 1207 }
1045 1208
1046 $self->_set_text ($text); 1209 $self->_set_text ($text);
1100 1263
1101 $self->SUPER::_draw; 1264 $self->SUPER::_draw;
1102 1265
1103 #TODO: force update every cursor change :( 1266 #TODO: force update every cursor change :(
1104 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1267 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1268
1269 unless (exists $self->{cur_h}) {
1270 my $text = substr $self->{text}, 0, $self->{cursor};
1271 utf8::encode $text;
1272
1273 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1274 }
1275
1105 glColor @{$self->{fg}}; 1276 glColor @{$self->{fg}};
1106 glBegin GL_LINES; 1277 glBegin GL_LINES;
1107 glVertex $self->{cur_x}, $self->{cur_y};
1108 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1278 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1279 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1109 glEnd; 1280 glEnd;
1110 } 1281 }
1111} 1282}
1112 1283
1113#############################################################################
1114
1115package CFClient::UI::Slider; 1284package CFClient::UI::Entry;
1116 1285
1117use strict; 1286our @ISA = CFClient::UI::EntryBase::;
1118 1287
1119use SDL::OpenGL; 1288use CFClient::OpenGL;
1120 1289
1290sub key_down {
1291 my ($self, $ev) = @_;
1292
1293 my $sym = $ev->{sym};
1294
1295 if ($sym == 13) {
1296 $self->emit (activate => $self->get_text);
1297 $self->update;
1298
1299 } else {
1300 $self->SUPER::key_down ($ev);
1301 }
1302
1303}
1304
1305#############################################################################
1306
1307package CFClient::UI::Button;
1308
1121our @ISA = CFClient::UI::DrawBG::; 1309our @ISA = CFClient::UI::Label::;
1310
1311use CFClient::OpenGL;
1312
1313my @tex =
1314 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1315 qw(b1_button_active.png);
1122 1316
1123sub new { 1317sub new {
1124 my $class = shift; 1318 my $class = shift;
1125 1319
1320 $class->SUPER::new (
1321 padding => 4,
1322 fg => [1, 1, 1],
1323 bg => [1, 1, 1, 0.2],
1324 active_fg => [0, 0, 1],
1325 can_hover => 1,
1326 align => 0,
1327 valign => 0,
1328 @_
1329 )
1330}
1331
1332sub button_up {
1333 my ($self, $ev, $x, $y) = @_;
1334
1335 if ($x >= 0 && $x < $self->{w}
1336 && $y >= 0 && $y < $self->{h}) {
1337 $self->emit ("activate");
1338 }
1339}
1340
1341sub _draw {
1342 my ($self) = @_;
1343
1344 local $self->{fg} = $self->{fg};
1345
1346 if ($GRAB == $self) {
1347 $self->{fg} = $self->{active_fg};
1348 }
1349
1350 glEnable GL_BLEND;
1351 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1352 glEnable GL_TEXTURE_2D;
1353 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1354 glColor 0, 0, 0, 1;
1355
1356 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1357
1358 glDisable GL_TEXTURE_2D;
1359 glDisable GL_BLEND;
1360
1361 $self->SUPER::_draw;
1362}
1363
1364#############################################################################
1365
1366package CFClient::UI::CheckBox;
1367
1368our @ISA = CFClient::UI::DrawBG::;
1369
1370my @tex =
1371 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1372 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1373
1374use CFClient::OpenGL;
1375
1376sub new {
1377 my $class = shift;
1378
1379 $class->SUPER::new (
1380 padding => 2,
1381 fg => [1, 1, 1],
1382 active_fg => [1, 1, 0],
1383 state => 0,
1384 can_hover => 1,
1385 @_
1386 )
1387}
1388
1389sub size_request {
1390 my ($self) = @_;
1391
1392 ($self->{padding} * 2 + 6) x 2
1393}
1394
1395sub button_down {
1396 my ($self, $ev, $x, $y) = @_;
1397
1398 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1399 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1400 $self->{state} = !$self->{state};
1401 $self->emit (changed => $self->{state});
1402 }
1403}
1404
1405sub _draw {
1406 my ($self) = @_;
1407
1408 $self->SUPER::_draw;
1409
1410 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1411
1412 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1413
1414 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1415
1416 glEnable GL_BLEND;
1417 glEnable GL_TEXTURE_2D;
1418 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1419
1420 my $tex = $self->{state} ? $tex[1] : $tex[0];
1421
1422 $tex->draw_quad (0, 0, $s, $s);
1423
1424 glDisable GL_TEXTURE_2D;
1425 glDisable GL_BLEND;
1426}
1427
1428#############################################################################
1429
1430package CFClient::UI::VGauge;
1431
1432our @ISA = CFClient::UI::Base::;
1433
1434use CFClient::OpenGL;
1435
1436my %tex = (
1437 food => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1440 ],
1441 grace => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1444 ],
1445 hp => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1448 ],
1449 mana => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1452 ],
1453);
1454
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new {
1457 my $class = shift;
1458
1459 my $self = $class->SUPER::new (
1460 gauge => 'food',
1461 @_
1462 );
1463
1464 $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h};
1465
1466 $self
1467}
1468
1469sub size_request {
1470 my ($self) = @_;
1471
1472 my $tex = $tex{$self->{gauge}}[0];
1473
1474 @$tex{qw(w h)}
1475}
1476
1477sub set_max {
1478 my ($self, $max) = @_;
1479
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493}
1494
1495sub _draw {
1496 my ($self) = @_;
1497
1498 my $tex = $tex{$self->{gauge}};
1499
1500 my ($w, $h) = ($self->{w}, $self->{h});
1501
1502 my $ycut = $self->{val} / ($self->{max_val} || 1);
1503 $ycut = 1 if $self->{val} > $self->{max_val};
1504
1505 my $t1 = $tex->[0];
1506 my $t2 = $tex->[1];
1507
1508 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512
1513 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h};
1515
1516 my $yp = 0;
1517
1518 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin GL_QUADS;
1520 glTexCoord 0 , 0; glVertex 0 , $yp;
1521 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1;
1522 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1;
1523 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp;
1524 glEnd;
1525
1526 $yp += $h1;
1527
1528 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin GL_QUADS;
1530 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp;
1531 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2;
1532 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1533 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp;
1534 glEnd;
1535
1536 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D;
1538}
1539
1540#############################################################################
1541
1542package CFClient::UI::Slider;
1543
1544use strict;
1545
1546use CFClient::OpenGL;
1547
1548our @ISA = CFClient::UI::DrawBG::;
1549
1550my @tex =
1551 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1552 qw(s1_slider.png s1_slider_bg.png);
1553
1554sub new {
1555 my $class = shift;
1556
1126 # range [value, low, high, page] 1557 # range [value, low, high, page]
1127 1558
1559 # TODO: 0-width page
1560 # TODO: req_w/h are wrong with vertical
1561 # TODO: calculations are off
1128 my $self = $class->SUPER::new ( 1562 my $self = $class->SUPER::new (
1129 fg => [1, 1, 1], 1563 fg => [1, 1, 1],
1130 active_fg => [0, 0, 0], 1564 active_fg => [0, 0, 0],
1131 range => [0, 0, 100, 10], 1565 range => [0, 0, 100, 10],
1132 req_w => 40, 1566 req_w => 20,
1133 req_h => 10, 1567 req_h => 20,
1134 vertical => 0, 1568 vertical => 0,
1569 can_hover => 1,
1570 inner_pad => 5,
1135 @_ 1571 @_
1136 ); 1572 );
1137 1573
1138 $self 1574 $self
1139} 1575}
1160 if ($GRAB == $self) { 1596 if ($GRAB == $self) {
1161 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1597 my ($value, $lo, $hi, $page) = @{$self->{range}};
1162 1598
1163 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1599 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1164 1600
1601 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1602 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1603
1604 $x -= $inner_pad_px; # substract the padding
1165 $x = $x * ($hi - $lo) / $w + $lo; 1605 $x = $x * ($hi - $lo) / $inner_w + $lo;
1166 $x = $lo if $x < $lo; 1606 $x = $lo if $x < $lo;
1167 $x = $hi - $page if $x > $hi - $page; 1607 $x = $hi - $page if $x > $hi - $page;
1168 $self->{range}[0] = $x; 1608 $self->{range}[0] = $x;
1169 1609
1170 $self->emit (changed => $x); 1610 $self->emit (changed => $x);
1171 $self->update; 1611 $self->update;
1172 } 1612 }
1613}
1614
1615# the inner_* stuff is for generating a padding for the slider handle,
1616# so that the handle doesn't leave the texture. This calculation isn't 100%
1617# correct propably, but it does the job for now
1618sub _calc_inner_pad_px {
1619 my ($self, $w) = @_;
1620 ($w / 100) * $self->{inner_pad} # % to pixels
1173} 1621}
1174 1622
1175sub _draw { 1623sub _draw {
1176 my ($self) = @_; 1624 my ($self) = @_;
1177 1625
1191 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1639 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1192 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1640 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1193 1641
1194 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1642 my ($value, $lo, $hi, $page) = @{$self->{range}};
1195 1643
1644 $hi = $value + 1 if $lo == $hi;
1645
1646 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1647 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1648
1196 $page = int $page * $w / ($hi - $lo); 1649 $page = int $page * $inner_w / ($hi - $lo);
1197 $value = int +($value - $lo) * $w / ($hi - $lo); 1650 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1198 1651
1199 $w -= $page; 1652 $w -= $page;
1200 $page &= ~1; 1653 $page &= ~1;
1201 glTranslate $page * 0.5, 0, 0; 1654 glTranslate $page * 0.5, 0, 0;
1655 $page ||= 2;
1202 1656
1203 glColor @$fg;
1204 glBegin GL_LINES;
1205 glVertex 0, 0; glVertex 0, $h;
1206 glVertex $w - 1, 0; glVertex $w - 1, $h;
1207 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1208 glEnd;
1209
1210 my $knob_a = $value - $page * 0.5; 1657 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1211 my $knob_b = $value + $page * 0.5; 1658 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1212 1659
1213 glBegin GL_QUADS; 1660 glEnable GL_BLEND;
1214 glColor @$fg; 1661 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1215 glVertex $knob_a, 0; 1662 glEnable GL_TEXTURE_2D;
1216 glVertex $knob_a, $h; 1663 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1217 glVertex $knob_b, $h;
1218 glVertex $knob_b, 0;
1219 1664
1220 if ($knob_a < $knob_b - 2) { 1665 # draw background
1221 glColor @$bg; 1666 $tex[1]->draw_quad (0, 0, $w, $h);
1222 glVertex $knob_a + 1, 1;
1223 glVertex $knob_a + 1, $h - 1;
1224 glVertex $knob_b - 1, $h - 1;
1225 glVertex $knob_b - 1, 1;
1226 }
1227 glEnd;
1228}
1229 1667
1230############################################################################# 1668 # draw handle
1669 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1231 1670
1671 glDisable GL_BLEND;
1672 glDisable GL_TEXTURE_2D;
1673}
1674
1675#############################################################################
1676
1232package CFClient::UI::MapWidget; 1677package CFClient::UI::TextView;
1233 1678
1234use strict;
1235
1236use List::Util qw(min max);
1237
1238use SDL;
1239use SDL::OpenGL;
1240
1241our @ISA = CFClient::UI::Base::; 1679our @ISA = CFClient::UI::HBox::;
1680
1681use CFClient::OpenGL;
1242 1682
1243sub new { 1683sub new {
1244 my $class = shift; 1684 my $class = shift;
1245 1685
1246 $class->SUPER::new ( 1686 my $self = $class->SUPER::new (
1247 z => -1, 1687 fontsize => 1,
1248 list => (glGenLists 1),
1249 @_ 1688 @_,
1689
1690 layout => (new CFClient::Layout),
1691 par => [],
1692 height => 0,
1693 children => [
1694 (new CFClient::UI::Empty expand => 1),
1695 (new CFClient::UI::Slider vertical => 1),
1696 ],
1250 ) 1697 );
1251}
1252 1698
1253sub key_down { 1699 $self->{children}[1]->connect (changed => sub {
1254 print "MAPKEYDOWN\n"; 1700 $self->update;
1255} 1701 });
1256 1702
1257sub key_up { 1703 $self
1258} 1704}
1259 1705
1260sub size_request { 1706sub set_fontsize {
1707 my ($self, $fontsize) = @_;
1708
1709 $self->{fontsize} = $fontsize;
1710 $self->reflow;
1711}
1712
1713sub text_height {
1714 my ($self, $text) = @_;
1715
1716 my $layout = $self->{layout};
1717
1718 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1719 $layout->set_width ($self->{w});
1720 $layout->set_text ($text);
1261 ( 1721
1262 1 + int $::WIDTH / 32, 1722 ($layout->size)[1]
1263 1 + int $::HEIGHT / 32, 1723}
1264 ) 1724
1725sub reflow {
1726 my ($self) = @_;
1727
1728 $self->{need_reflow}++;
1729 $self->update;
1730}
1731
1732sub size_allocate {
1733 my ($self, $w, $h) = @_;
1734
1735 $self->SUPER::size_allocate ($w, $h);
1736
1737 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1738 $self->{layout}->set_width ($self->{children}[0]{w});
1739
1740 $self->reflow;
1741}
1742
1743sub add_paragraph {
1744 my ($self, $color, $text) = @_;
1745
1746 #TODO: intelligently "reformat" paragraph
1747
1748 my $height = $self->text_height ($text);
1749
1750 $self->{height} += $height;
1751
1752 push @{$self->{par}}, [$height, $color, $text];
1753
1754 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1755 $self->{children}[1]->update;
1265} 1756}
1266 1757
1267sub update { 1758sub update {
1268 my ($self) = @_; 1759 my ($self) = @_;
1269 1760
1270 $self->{need_update} = 1;
1271 $self->SUPER::update; 1761 $self->SUPER::update;
1762
1763 return unless $self->{h} > 0;
1764
1765 delete $self->{texture};
1766
1767 $ROOT->on_refresh ($self, sub {
1768 if (delete $self->{need_reflow}) {
1769 my $height = 0;
1770
1771 $height += $_->[0] = $self->text_height ($_->[2])
1772 for @{$self->{par}};
1773
1774 $self->{height} = $height;
1775
1776 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1777
1778 delete $self->{texture};
1779 }
1780
1781 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1782 glClearColor 0, 0, 0, 1;
1783 glClear GL_COLOR_BUFFER_BIT;
1784
1785 glEnable GL_BLEND;
1786 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1787 glEnable GL_TEXTURE_2D;
1788 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1789
1790 my $top = int $self->{children}[1]{range}[0];
1791
1792 my $y0 = $top;
1793 my $y1 = $top + $self->{h};
1794
1795 my $y = 0;
1796
1797 my $layout = $self->{layout};
1798
1799 for my $par (@{$self->{par}}) {
1800 my $h = $par->[0];
1801
1802 if ($y0 < $y + $h && $y < $y1) {
1803 $layout->set_text ($par->[2]);
1804
1805 glColor @{ $par->[1] };
1806 my ($W, $H) = $layout->size;
1807 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1808 }
1809
1810 $y += $h;
1811 }
1812
1813 glDisable GL_TEXTURE_2D;
1814 glDisable GL_BLEND;
1815 };
1816 });
1272} 1817}
1273 1818
1274sub _draw { 1819sub _draw {
1275 my ($self) = @_; 1820 my ($self) = @_;
1276 1821
1277 if (delete $self->{need_update}) { 1822 if ($self->{texture}) {
1278 glNewList $self->{list}, GL_COMPILE;
1279
1280 my $mx = $::CONN->{mapx};
1281 my $my = $::CONN->{mapy};
1282
1283 my $map = $::CONN->{map};
1284
1285 my ($xofs, $yofs);
1286
1287 my $sw = 1 + int $::WIDTH / 32;
1288 my $sh = 1 + int $::HEIGHT / 32;
1289
1290 if ($::CONN->{mapw} > $sw) {
1291 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
1292 } else {
1293 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
1294 }
1295
1296 if ($::CONN->{maph} > $sh) {
1297 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
1298 } else {
1299 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1300 }
1301
1302 glEnable GL_TEXTURE_2D; 1823 glEnable GL_TEXTURE_2D;
1303 glEnable GL_BLEND;
1304 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1305 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1824 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1306 1825 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1307 my $sw4 = ($sw + 3) & ~3;
1308 my $darkness = "\x00" x ($sw4 * $sh);
1309
1310 for my $x (0 .. $sw - 1) {
1311 my $row = $map->[$x + $xofs];
1312 for my $y (0 .. $sh - 1) {
1313
1314 my $cell = $row->[$y + $yofs]
1315 or next;
1316
1317 my $dark = $cell->[0];
1318 if ($dark < 0) {
1319 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1320 } else {
1321 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1322 }
1323
1324 for my $num (grep $_, @$cell[1,2,3]) {
1325 my $tex = $::CONN->{face}[$num]{texture} || next;
1326
1327 my ($w, $h) = @$tex{qw(w h)};
1328
1329 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1330 }
1331 }
1332 }
1333
1334# if (1) { # higher quality darkness
1335# $lighting =~ s/(.)/$1$1$1/gs;
1336# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1337#
1338# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1339#
1340# $lighting = $pb->get_pixels;
1341# $lighting =~ s/(.)../$1/gs;
1342# }
1343
1344 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1345
1346 $darkness = new CFClient::Texture
1347 w => $sw4,
1348 h => $sh,
1349 data => $darkness,
1350 internalformat => GL_ALPHA,
1351 format => GL_ALPHA;
1352
1353 glColor 0.45, 0.45, 0.45, 1;
1354 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1355
1356 glDisable GL_TEXTURE_2D; 1826 glDisable GL_TEXTURE_2D;
1357 glDisable GL_BLEND;
1358
1359 glEndList;
1360 } 1827 }
1361 1828
1362 glCallList $self->{list}; 1829 $self->{children}[1]->draw;
1363}
1364 1830
1365my %DIR = (
1366 SDLK_KP8, [1, "north"],
1367 SDLK_KP9, [2, "northeast"],
1368 SDLK_KP6, [3, "east"],
1369 SDLK_KP3, [4, "southeast"],
1370 SDLK_KP2, [5, "south"],
1371 SDLK_KP1, [6, "southwest"],
1372 SDLK_KP4, [7, "west"],
1373 SDLK_KP7, [8, "northwest"],
1374
1375 SDLK_UP, [1, "north"],
1376 SDLK_RIGHT, [3, "east"],
1377 SDLK_DOWN, [5, "south"],
1378 SDLK_LEFT, [7, "west"],
1379);
1380
1381sub key_down {
1382 my ($self, $ev) = @_;
1383
1384 my $mod = $ev->key_mod;
1385 my $sym = $ev->key_sym;
1386
1387 if ($sym == SDLK_KP5) {
1388 $::CONN->send ("command stay fire");
1389 } elsif (exists $DIR{$sym}) {
1390 if ($mod & KMOD_SHIFT) {
1391 $self->{shft}++;
1392 $::CONN->send ("command fire $DIR{$sym}[0]");
1393 } elsif ($mod & KMOD_CTRL) {
1394 $self->{ctrl}++;
1395 $::CONN->send ("command run $DIR{$sym}[0]");
1396 } else {
1397 $::CONN->send ("command $DIR{$sym}[1]");
1398 }
1399 }
1400}
1401
1402sub key_up {
1403 my ($self, $ev) = @_;
1404
1405 my $mod = $ev->key_mod;
1406 my $sym = $ev->key_sym;
1407
1408 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1409 $::CONN->send ("command fire_stop");
1410 }
1411 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1412 $::CONN->send ("command run_stop");
1413 }
1414} 1831}
1415 1832
1416############################################################################# 1833#############################################################################
1417 1834
1418package CFClient::UI::Animator; 1835package CFClient::UI::Animator;
1419 1836
1420use SDL::OpenGL; 1837use CFClient::OpenGL;
1421 1838
1422our @ISA = CFClient::UI::Bin::; 1839our @ISA = CFClient::UI::Bin::;
1423 1840
1424sub moveto { 1841sub moveto {
1425 my ($self, $x, $y) = @_; 1842 my ($self, $x, $y) = @_;
1455 glPopMatrix; 1872 glPopMatrix;
1456} 1873}
1457 1874
1458############################################################################# 1875#############################################################################
1459 1876
1460package CFClient::UI::Toplevel; 1877package CFClient::UI::Flopper;
1878
1879our @ISA = CFClient::UI::Button::;
1880
1881sub new {
1882 my $class = shift;
1883
1884 my $self = $class->SUPER::new (
1885 state => 0,
1886 connect_activate => \&toggle_flopper,
1887 @_
1888 );
1889
1890 if ($self->{state}) {
1891 $self->{state} = 0;
1892 $self->toggle_flopper;
1893 }
1894
1895 $self
1896}
1897
1898sub toggle_flopper {
1899 my ($self) = @_;
1900
1901 # TODO: use animation
1902 if ($self->{state} = !$self->{state}) {
1903 $CFClient::UI::ROOT->add ($self->{other});
1904 $self->{other}->move ($self->coord2global (0, $self->{h}));
1905 $self->emit ("open");
1906 } else {
1907 $CFClient::UI::ROOT->remove ($self->{other});
1908 $self->emit ("close");
1909 }
1910
1911 $self->emit (changed => $self->{state});
1912}
1913
1914#############################################################################
1915
1916package CFClient::UI::Root;
1461 1917
1462our @ISA = CFClient::UI::Container::; 1918our @ISA = CFClient::UI::Container::;
1919
1920use CFClient::OpenGL;
1921
1922sub check_size {
1923 my ($self) = @_;
1924
1925 $self->configure (0, 0, $::WITH, $::HEIGHT);
1926}
1463 1927
1464sub size_request { 1928sub size_request {
1465 ($::WIDTH, $::HEIGHT) 1929 ($::WIDTH, $::HEIGHT)
1466} 1930}
1467 1931
1468sub size_allocate { 1932sub configure {
1469 my ($self, $x, $y, $w, $h) = @_; 1933 my ($self, $x, $y, $w, $h) = @_;
1470 1934
1471 $self->_size_allocate ($x, $y, $w, $h); 1935 $self->SUPER::configure ($x, $y, $w, $h);
1472 1936
1473 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1937 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1474 for @{$self->{children}}; 1938 for @{$self->{children}};
1475} 1939}
1476 1940
1477sub translate { 1941sub _topleft {
1478 my ($self, $x, $y) = @_; 1942 my ($self, $x, $y) = @_;
1479 1943
1480 ($x, $y) 1944 ($x, $y)
1481} 1945}
1482 1946
1483sub update { 1947sub update {
1484 my ($self) = @_; 1948 my ($self) = @_;
1485 1949
1486 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 1950 $self->check_size;
1487 ::refresh (); 1951 ::refresh ();
1488} 1952}
1489 1953
1490sub add { 1954sub add {
1491 my ($self, $widget) = @_; 1955 my ($self, $child) = @_;
1492 1956
1493 $self->SUPER::add ($widget); 1957 $self->SUPER::add ($child);
1958}
1494 1959
1495 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 1960sub on_refresh {
1961 my ($self, $id, $cb) = @_;
1962
1963 $self->{refresh_hook}{$id} = $cb;
1496} 1964}
1497 1965
1498sub draw { 1966sub draw {
1499 my ($self) = @_; 1967 my ($self) = @_;
1500 1968
1969 while (my $rcb = delete $self->{refresh_hook}) {
1970 $_->() for values %$rcb;
1971 }
1972
1973 glViewport 0, 0, $::WIDTH, $::HEIGHT;
1974 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
1975 glClear GL_COLOR_BUFFER_BIT;
1976
1977 glMatrixMode GL_PROJECTION;
1978 glLoadIdentity;
1979 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
1980 glMatrixMode GL_MODELVIEW;
1981 glLoadIdentity;
1982
1501 $self->_draw; 1983 $self->_draw;
1502} 1984}
1503 1985
1504############################################################################# 1986#############################################################################
1505 1987
1506package CFClient::UI; 1988package CFClient::UI;
1507 1989
1508$TOPLEVEL = new CFClient::UI::Toplevel; 1990$ROOT = new CFClient::UI::Root;
1509 1991
15101 19921
1511 1993

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines