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.114 by root, Sat Apr 15 13:56:25 2006 UTC vs.
Revision 1.155 by root, Sun Apr 23 02:22:54 2006 UTC

8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $ROOT; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_text ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 48 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 49}
23 50
24sub feed_sdl_button_down_event { 51sub feed_sdl_button_down_event {
25 my ($ev) = @_; 52 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 53 my ($x, $y) = ($ev->{x}, $ev->{y});
27 54
28 if (!$BUTTON_STATE) { 55 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 56 my $widget = $ROOT->find_widget ($x, $y);
30 57
31 $GRAB = $widget; 58 $GRAB = $widget;
32 $GRAB->update if $GRAB; 59 $GRAB->update if $GRAB;
33 }
34 60
61 check_tooltip;
62 }
63
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 65
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 67}
39 68
40sub feed_sdl_button_up_event { 69sub feed_sdl_button_up_event {
41 my ($ev) = @_; 70 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 71 my ($x, $y) = ($ev->{x}, $ev->{y});
43 72
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 73 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 74
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 76
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 78
50 if (!$BUTTON_STATE) { 79 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 80 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 81 $grab->update if $grab;
53 $GRAB->update if $GRAB; 82 $GRAB->update if $GRAB;
83
84 check_tooltip;
54 } 85 }
55} 86}
56 87
57sub feed_sdl_motion_event { 88sub feed_sdl_motion_event {
58 my ($ev) = @_; 89 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 90 my ($x, $y) = ($ev->{x}, $ev->{y});
60 91
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 92 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 93
63 if ($widget != $HOVER) { 94 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 95 my $hover = $HOVER; $HOVER = $widget;
65 96
66 $hover->update if $hover && $hover->{can_hover}; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 98 $HOVER->update if $HOVER && $HOVER->{can_hover};
99
100 check_tooltip;
68 } 101 }
69 102
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 104}
72 105
74sub harmonize { 107sub harmonize {
75 my ($vals) = @_; 108 my ($vals) = @_;
76 109
77 my $rem = 0; 110 my $rem = 0;
78 111
79 for ($vals) { 112 for (@$vals) {
80 my $i = int $_ + $rem; 113 my $i = int $_ + $rem;
81 $rem += $_ - $i; 114 $rem += $_ - $i;
82 $_ = $i; 115 $_ = $i;
83 } 116 }
84} 117}
87 120
88package CFClient::UI::Base; 121package CFClient::UI::Base;
89 122
90use strict; 123use strict;
91 124
92use SDL::OpenGL; 125use CFClient::OpenGL;
93 126
94sub new { 127sub new {
95 my $class = shift; 128 my $class = shift;
96 129
97 my $self = bless { 130 my $self = bless {
98 x => 0, 131 x => 0,
99 y => 0, 132 y => 0,
100 z => 0, 133 z => 0,
101 w => -1, 134 can_events => 1,
102 h => -1,
103 @_ 135 @_
104 }, $class; 136 }, $class;
105 137
106 for (keys %$self) { 138 for (keys %$self) {
107 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
110 } 142 }
111 143
112 $self 144 $self
113} 145}
114 146
147sub show {
148 my ($self) = @_;
149
150 return if $self->{parent};
151
152 $CFClient::UI::ROOT->add ($self);
153}
154
155sub hide {
156 my ($self) = @_;
157
158 return unless $self->{parent};
159
160 $self->{parent}->remove ($self);
161}
162
115sub move { 163sub move {
116 my ($self, $x, $y, $z) = @_; 164 my ($self, $x, $y, $z) = @_;
165
117 $self->{x} = $x; 166 $self->{x} = int $x;
118 $self->{y} = $y; 167 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 168 $self->{z} = $z if defined $z;
169
170 $self->update;
120} 171}
121 172
122sub needs_redraw { 173sub needs_redraw {
123 0 174 0
124} 175}
125 176
126sub size_request { 177sub size_request {
127 require Carp; 178 require Carp;
128 Carp::confess "size_request is abtract"; 179 Carp::confess "size_request is abstract";
129} 180}
130 181
131sub _size_allocate { 182sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 183 my ($self, $x, $y, $w, $h) = @_;
133 184
185 if ($self->{aspect}) {
186 my $w2 = List::Util::min $w, int $h * $self->{aspect};
187 my $h2 = List::Util::min $h, int $w / $self->{aspect};
188
189 # use alignment to adjust x, y
190
191 $x += int +($w - $w2) * 0.5;
192 $y += int +($h - $h2) * 0.5;
193
194 ($w, $h) = ($w2, $h2);
195 }
196
197 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 198 $self->{x} = $x;
135 $self->{y} = $y; 199 $self->{y} = $y;
200 $self->update;
201 }
136 202
137 return unless $self->{w} != $w || $self->{h} != $h; 203 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 204 $self->{w} = $w;
140 $self->{h} = $h; 205 $self->{h} = $h;
141 206
207 $self->size_allocate ($w, $h);
208 $self->update;
142 1 209 }
143} 210}
144 211
145sub size_allocate { 212sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 213 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 214}
150 215
151# return top left coordinates 216# return top left coordinates
152sub _topleft { 217sub _topleft {
153 my ($self, $x, $y) = @_; 218 my ($self, $x, $y) = @_;
176 241
177 return if $FOCUS == $self; 242 return if $FOCUS == $self;
178 return unless $self->{can_focus}; 243 return unless $self->{can_focus};
179 244
180 my $focus = $FOCUS; $FOCUS = $self; 245 my $focus = $FOCUS; $FOCUS = $self;
246
247 $self->emit (focus_in => $focus);
248
181 $focus->update if $focus; 249 $focus->update if $focus;
182 $FOCUS->update; 250 $FOCUS->update;
183} 251}
184 252
185sub focus_out { 253sub focus_out {
186 my ($self) = @_; 254 my ($self) = @_;
187 255
188 return unless $FOCUS == $self; 256 return unless $FOCUS == $self;
189 257
190 my $focus = $FOCUS; undef $FOCUS; 258 my $focus = $FOCUS; undef $FOCUS;
259
260 $self->emit (focus_out => $focus);
261
191 $focus->update if $focus; #? 262 $focus->update if $focus; #?
192} 263}
193 264
194sub mouse_motion { } 265sub mouse_motion { }
195sub button_up { } 266sub button_up { }
219 glPopMatrix; 290 glPopMatrix;
220 291
221 if ($self == $HOVER && $self->{can_hover}) { 292 if ($self == $HOVER && $self->{can_hover}) {
222 my ($x, $y) = @$self{qw(x y)}; 293 my ($x, $y) = @$self{qw(x y)};
223 294
224 glColor 0, 0, 1, 0.2; 295 glColor 1, 0.8, 0.5, 0.2;
225 glEnable GL_BLEND; 296 glEnable GL_BLEND;
226 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 297 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
227 glBegin GL_QUADS; 298 glBegin GL_QUADS;
228 glVertex $x , $y; 299 glVertex $x , $y;
229 glVertex $x + $self->{w}, $y; 300 glVertex $x + $self->{w}, $y;
241} 312}
242 313
243sub find_widget { 314sub find_widget {
244 my ($self, $x, $y) = @_; 315 my ($self, $x, $y) = @_;
245 316
317 return () unless $self->{can_events};
318
246 return $self 319 return $self
247 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 320 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
248 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 321 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
249 322
250 () 323 ()
254 my ($self, $parent) = @_; 327 my ($self, $parent) = @_;
255 328
256 Scalar::Util::weaken ($self->{parent} = $parent); 329 Scalar::Util::weaken ($self->{parent} = $parent);
257} 330}
258 331
332sub check_size {
333 my ($self) = @_;
334
335 return unless $self->{parent};
336
337 my ($w, $h) = $self->size_request;
338
339 if ($w != $self->{req_w} || $h != $self->{req_h}) {
340 $self->{req_w} = $w;
341 $self->{req_h} = $h;
342
343 $self->{parent}->check_size;
344 }
345}
346
259sub update { 347sub update {
260 my ($self) = @_; 348 my ($self) = @_;
261 349
262 $self->{parent}->update 350 $self->{parent}->update
263 if $self->{parent}; 351 if $self->{parent};
264} 352}
265 353
266sub connect { 354sub connect {
267 my ($self, $signal, $cb) = @_; 355 my ($self, $signal, $cb) = @_;
268 356
269 push @{ $self->{cb}{$signal} }, $cb; 357 push @{ $self->{signal_cb}{$signal} }, $cb;
270} 358}
271 359
272sub emit { 360sub emit {
273 my ($self, $signal, @args) = @_; 361 my ($self, $signal, @args) = @_;
274 362
363 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
275 $_->($self, @args) 364 $cb->($self, @args);
276 for @{$self->{cb}{$signal} || []}; 365 }
277} 366}
278 367
279sub DESTROY { 368sub DESTROY {
280 my ($self) = @_; 369 my ($self) = @_;
281 370
287package CFClient::UI::DrawBG; 376package CFClient::UI::DrawBG;
288 377
289our @ISA = CFClient::UI::Base::; 378our @ISA = CFClient::UI::Base::;
290 379
291use strict; 380use strict;
292use SDL::OpenGL; 381use CFClient::OpenGL;
293 382
294sub new { 383sub new {
295 my $class = shift; 384 my $class = shift;
296 385
297 # range [value, low, high, page] 386 # range [value, low, high, page]
326 415
327package CFClient::UI::Empty; 416package CFClient::UI::Empty;
328 417
329our @ISA = CFClient::UI::Base::; 418our @ISA = CFClient::UI::Base::;
330 419
420sub new {
421 my ($class, %arg) = @_;
422 $class->SUPER::new (can_events => 0, %arg);
423}
424
331sub size_request { 425sub size_request {
332 (0, 0) 426 (0, 0)
333} 427}
334 428
335sub draw { } 429sub draw { }
343sub new { 437sub new {
344 my ($class, %arg) = @_; 438 my ($class, %arg) = @_;
345 439
346 my $children = delete $arg{children} || []; 440 my $children = delete $arg{children} || [];
347 441
348 my $self = $class->SUPER::new (children => [], %arg); 442 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
349 $self->add ($_) for @$children; 443 $self->add ($_) for @$children;
350 444
351 $self 445 $self
352} 446}
353 447
361 $self->{children} = [ 455 $self->{children} = [
362 sort { $a->{z} <=> $b->{z} } 456 sort { $a->{z} <=> $b->{z} }
363 @{$self->{children}}, $child 457 @{$self->{children}}, $child
364 ]; 458 ];
365 459
366 $self->{w} = $self->{h} = -1; 460 $child->check_size;
461}
462
463sub remove {
464 my ($self, $child) = @_;
465
466 delete $child->{parent};
467
468 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
469
470 $self->check_size;
367 $self->update; 471 $self->update;
368}
369
370sub remove {
371 my ($self, $widget) = @_;
372
373 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
374
375 $self->size_allocate (0, 0, $self->{w}, $self->{h});
376} 472}
377 473
378sub find_widget { 474sub find_widget {
379 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
380 476
410 506
411 $class->SUPER::new (children => [$child], %arg) 507 $class->SUPER::new (children => [$child], %arg)
412} 508}
413 509
414sub add { 510sub add {
415 my ($self, $widget) = @_; 511 my ($self, $child) = @_;
416 512
417 $self->{children} = []; 513 $self->{children} = [];
418 514
419 $self->SUPER::add ($widget); 515 $self->SUPER::add ($child);
420} 516}
421 517
422sub remove { 518sub remove {
423 my ($self, $widget) = @_; 519 my ($self, $widget) = @_;
424 520
433sub size_request { 529sub size_request {
434 $_[0]{children}[0]->size_request 530 $_[0]{children}[0]->size_request
435} 531}
436 532
437sub size_allocate { 533sub size_allocate {
438 my ($self, $x, $y, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
439 535
440 $self->_size_allocate ($x, $y, $w, $h) or return;
441
442 $self->{children}[0]->size_allocate (0, 0, $w, $h); 536 $self->{children}[0]->configure (0, 0, $w, $h);
443} 537}
444 538
445############################################################################# 539#############################################################################
446 540
447package CFClient::UI::Window; 541package CFClient::UI::Window;
448 542
449our @ISA = CFClient::UI::Bin::; 543our @ISA = CFClient::UI::Bin::;
450 544
451use SDL::OpenGL; 545use CFClient::OpenGL;
452 546
453sub new { 547sub new {
454 my ($class, %arg) = @_; 548 my ($class, %arg) = @_;
455 549
456 my $self = $class->SUPER::new (%arg); 550 my $self = $class->SUPER::new (%arg);
473 $self->child->draw; 567 $self->child->draw;
474 }; 568 };
475} 569}
476 570
477sub size_allocate { 571sub size_allocate {
478 my ($self, $x, $y, $w, $h) = @_; 572 my ($self, $w, $h) = @_;
479 573
480 $self->_size_allocate ($x, $y, $w, $h) or return;
481
482 $self->child->size_allocate (0, 0, $w, $h); 574 $self->child->configure (0, 0, $w, $h);
483 575
484 $self->render_chld; 576 $self->render_chld;
485} 577}
486 578
487sub _draw { 579sub _draw {
503 glDisable GL_TEXTURE_2D; 595 glDisable GL_TEXTURE_2D;
504} 596}
505 597
506############################################################################# 598#############################################################################
507 599
600package CFClient::UI::ViewPort;
601
602our @ISA = CFClient::UI::Window::;
603
604sub new { die }
605
606sub size_request {
607 my ($self) = @_;
608
609 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
610 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
611
612 @$self{qw(child_w child_h)}
613}
614
615sub _draw {
616 my ($self) = @_;
617
618 $self->{children}[1]->draw;
619}
620
621
622#############################################################################
623
508package CFClient::UI::Frame; 624package CFClient::UI::Frame;
509 625
510our @ISA = CFClient::UI::Bin::; 626our @ISA = CFClient::UI::Bin::;
511 627
512use SDL::OpenGL; 628use CFClient::OpenGL;
513
514sub size_request {
515 my ($self) = @_;
516 my $chld = $self->child
517 or return (0, 0);
518
519 $chld->move (2, 2);
520
521 map { $_ + 4 } $chld->size_request;
522}
523
524sub size_allocate {
525 my ($self, $x, $y, $w, $h) = @_;
526
527 $self->_size_allocate ($x, $y, $w, $h) or return;
528
529 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
530}
531
532sub _draw {
533 my ($self) = @_;
534
535 my $chld = $self->child;
536
537 my ($w, $h) = $chld->size_request;
538
539 glBegin GL_QUADS;
540 glColor 0, 0, 0;
541 glVertex 0 , 0;
542 glVertex 0 , $h + 4;
543 glVertex $w + 4 , $h + 4;
544 glVertex $w + 4 , 0;
545 glEnd;
546
547 $chld->draw;
548}
549
550#############################################################################
551
552package CFClient::UI::FancyFrame;
553
554our @ISA = CFClient::UI::Bin::;
555
556use SDL::OpenGL;
557
558my @tex =
559 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
560 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
561 629
562sub new { 630sub new {
563 my $class = shift; 631 my $class = shift;
564 632
565 # TODO: user_x, user_y, overwrite moveto?
566
567 $class->SUPER::new ( 633 my $self = $class->SUPER::new (
568 bg => [1, 1, 1, 1], 634 bg => [1, 1, 1, 1],
569 border_bg => [1, 1, 1, 1], 635 border_bg => [1, 1, 1, 1],
570 border => $::FONTSIZE * 0.8, 636 border => 0.8,
571 @_ 637 @_
638 );
639
640 $self
641}
642
643sub set_size {
644 my ($self, $w, $h) = @_;
645 $self->{req_w} = $w;
646 $self->{req_h} = $h;
647 $self->check_size;
648}
649
650sub size_request {
651 my ($self) = @_;
652 ($self->{req_w}, $self->{req_h})
653}
654
655sub size_allocate {
656 my ($self, $w, $h) = @_;
657 $self->{w} = $w;
658 $self->{h} = $h;
659 $self->child->configure (0, 0, $w, $h);
660}
661
662sub _draw {
663 my ($self) = @_;
664
665 my ($w, $h) = ($self->{w}, $self->{h});
666
667 glEnable GL_BLEND;
668 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
669 glEnable GL_TEXTURE_2D;
670 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
671
672# glBegin GL_QUADS;
673# glColor 0, 0, 0, 0;
674# glVertex 0 , 0;
675# glVertex 0 , $h;
676# glVertex $w, $h;
677# glVertex $w, 0;
678# glEnd;
679
680
681 $self->child->draw;
682 glDisable GL_BLEND;
683 glDisable GL_TEXTURE_2D;
684}
685
686#############################################################################
687
688package CFClient::UI::FancyFrame;
689
690our @ISA = CFClient::UI::Bin::;
691
692use CFClient::OpenGL;
693
694my @tex =
695 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
696 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
697
698sub new {
699 my $class = shift;
700
701 # TODO: user_x, user_y, overwrite moveto?
702
703 my $self = $class->SUPER::new (
704 bg => [1, 1, 1, 1],
705 border_bg => [1, 1, 1, 1],
706 border => 0.8,
707 can_events => 1,
708 @_
709 );
710
711 $self->{title} &&= new CFClient::UI::Label
712 align => 0,
713 valign => 1,
714 text => $self->{title},
715 fontsize => 1;
716
717 $self
718}
719
720sub border {
721 int $_[0]{border} * $::FONTSIZE
722}
723
724sub size_request {
725 my ($self) = @_;
726
727 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
728
729 my ($w, $h) = $self->SUPER::size_request;
730
731 (
732 $w + $self->border * 2,
733 $h + $self->border * 2,
572 ) 734 )
573} 735}
574 736
575sub size_request {
576 my ($self) = @_;
577
578 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
579
580 my ($w, $h) = $self->SUPER::size_request;
581
582 (
583 $w + $self->{border} * 2,
584 $h + $self->{border} * 2,
585 )
586}
587
588sub size_allocate { 737sub size_allocate {
589 my ($self, $x, $y, $w, $h) = @_; 738 my ($self, $w, $h) = @_;
590 739
591 $self->_size_allocate ($x, $y, $w, $h) or return;
592
593 $h -= List::Util::max 0, $self->{border} * 2; 740 $h -= List::Util::max 0, $self->border * 2;
594 $w -= List::Util::max 0, $self->{border} * 2; 741 $w -= List::Util::max 0, $self->border * 2;
595 742
743 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
744 if $self->{title};
745
596 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 746 $self->child->configure ($self->border, $self->border, $w, $h);
597} 747}
598 748
599sub button_down { 749sub button_down {
600 my ($self, $ev, $x, $y) = @_; 750 my ($self, $ev, $x, $y) = @_;
601 751
752 my $border = $self->border;
753
602 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 754 if ($x < $self->{w} && $x >= $self->{w} - $border
603 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 755 && $y < $self->{h} && $y >= $self->{h} - $border) {
604 756
605 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 757 my ($ox, $oy) = ($ev->{x}, $ev->{y});
606 my ($bw, $bh) = ($self->{w}, $self->{h}); 758 my ($bw, $bh) = ($self->{w}, $self->{h});
607 759
608 $self->{motion} = sub { 760 $self->{motion} = sub {
609 my ($ev, $x, $y) = @_; 761 my ($ev, $x, $y) = @_;
610 762
611 ($x, $y) = ($ev->motion_x, $ev->motion_y); 763 ($x, $y) = ($ev->{x}, $ev->{y});
612 764
613 $self->{user_w} = $bw + $x - $ox; 765 $self->{user_w} = $bw + $x - $ox;
614 $self->{user_h} = $bh + $y - $oy; 766 $self->{user_h} = $bh + $y - $oy;
615 $self->update; 767 $self->check_size;
616 }; 768 };
617 769
618 } elsif ($x >= 0 && $x < $self->{w} 770 } elsif ($x >= 0 && $x < $self->{w}
619 && $y >= 0 && $y < $self->{border}) { 771 && $y >= 0 && $y < $border) {
620 772
621 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 773 my ($ox, $oy) = ($ev->{x}, $ev->{y});
622 my ($bx, $by) = ($self->{x}, $self->{y}); 774 my ($bx, $by) = ($self->{x}, $self->{y});
623 775
624 $self->{motion} = sub { 776 $self->{motion} = sub {
625 my ($ev, $x, $y) = @_; 777 my ($ev, $x, $y) = @_;
626 778
627 ($x, $y) = ($ev->motion_x, $ev->motion_y); 779 ($x, $y) = ($ev->{x}, $ev->{y});
628 780
629 $self->move ($bx + $x - $ox, $by + $y - $oy); 781 $self->move ($bx + $x - $ox, $by + $y - $oy);
630 $self->update; 782 $self->update;
631 }; 783 };
632 } 784 }
653 glEnable GL_BLEND; 805 glEnable GL_BLEND;
654 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
655 glEnable GL_TEXTURE_2D; 807 glEnable GL_TEXTURE_2D;
656 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
657 809
810 my $border = $self->border;
811
658 glColor @{ $self->{border_bg} }; 812 glColor @{ $self->{border_bg} };
659 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 813 $tex[1]->draw_quad (0, 0, $w, $border);
660 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 814 $tex[3]->draw_quad (0, $border, $border, $ch);
661 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 815 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
662 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 816 $tex[4]->draw_quad (0, $h - $border, $w, $border);
663 817
664 my $bg = $tex[0]; 818 my $bg = $tex[0];
665 819
666 # TODO: repeat texture not scale 820 # TODO: repeat texture not scale
667 my $rep_x = $cw / $bg->{w}; 821 my $rep_x = $cw / $bg->{w};
670 glColor @{ $self->{bg} }; 824 glColor @{ $self->{bg} };
671 825
672 $bg->{s} = $rep_x; 826 $bg->{s} = $rep_x;
673 $bg->{t} = $rep_y; 827 $bg->{t} = $rep_y;
674 $bg->{wrap_mode} = 1; 828 $bg->{wrap_mode} = 1;
675 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 829 $bg->draw_quad ($border, $border, $cw, $ch);
676 830
677 glDisable GL_TEXTURE_2D; 831 glDisable GL_TEXTURE_2D;
678 glDisable GL_BLEND; 832 glDisable GL_BLEND;
679 833
834 $self->{title}->draw if $self->{title};
680 $self->child->draw; 835 $self->child->draw;
681} 836}
682 837
683############################################################################# 838#############################################################################
684 839
686 841
687our @ISA = CFClient::UI::Base::; 842our @ISA = CFClient::UI::Base::;
688 843
689use List::Util qw(max sum); 844use List::Util qw(max sum);
690 845
691use SDL::OpenGL; 846use CFClient::OpenGL;
692 847
693sub new { 848sub new {
694 my $class = shift; 849 my $class = shift;
695 850
696 $class->SUPER::new ( 851 $class->SUPER::new (
703 my ($self, $x, $y, $child) = @_; 858 my ($self, $x, $y, $child) = @_;
704 859
705 $child->set_parent ($self); 860 $child->set_parent ($self);
706 $self->{children}[$y][$x] = $child; 861 $self->{children}[$y][$x] = $child;
707 862
708 $self->{w} = $self->{h} = -1; 863 $child->check_size;
864}
865
866# TODO: move to container class maybe? send childs a signal on removal?
867sub clear {
868 my ($self) = @_;
869
870 delete $self->{children};
709 $self->update; 871 $self->update;
710} 872}
711 873
712sub get_wh { 874sub get_wh {
713 my ($self) = @_; 875 my ($self) = @_;
719 or next; 881 or next;
720 882
721 for my $x (0 .. $#$row) { 883 for my $x (0 .. $#$row) {
722 my $widget = $row->[$x] 884 my $widget = $row->[$x]
723 or next; 885 or next;
724 my ($w, $h) = $widget->size_request; 886 my ($w, $h) = @$widget{qw(req_w req_h)};
725 887
726 $w[$x] = max $w[$x], $w; 888 $w[$x] = max $w[$x], $w;
727 $h[$y] = max $h[$y], $h; 889 $h[$y] = max $h[$y], $h;
728 } 890 }
729 } 891 }
741 (sum @$hs), 903 (sum @$hs),
742 ) 904 )
743} 905}
744 906
745sub size_allocate { 907sub size_allocate {
746 my ($self, $x, $y, $w, $h) = @_; 908 my ($self, $w, $h) = @_;
747
748 $self->_size_allocate ($x, $y, $w, $h) or return;
749 909
750 my ($ws, $hs) = $self->get_wh; 910 my ($ws, $hs) = $self->get_wh;
751 911
752 my $req_w = sum @$ws; 912 my $req_w = sum @$ws;
753 my $req_h = sum @$hs; 913 my $req_h = sum @$hs;
775 935
776 for my $c (0 .. $#$row) { 936 for my $c (0 .. $#$row) {
777 my $col_w = $ws->[$c]; 937 my $col_w = $ws->[$c];
778 938
779 if (my $widget = $row->[$c]) { 939 if (my $widget = $row->[$c]) {
780 $widget->size_allocate ($x, $y, $col_w, $row_h); 940 $widget->configure ($x, $y, $col_w, $row_h);
781 } 941 }
782 942
783 $x += $col_w; 943 $x += $col_w;
784 } 944 }
785 945
830 (List::Util::max map $_->[1], @alloc), 990 (List::Util::max map $_->[1], @alloc),
831 ) 991 )
832} 992}
833 993
834sub size_allocate { 994sub size_allocate {
835 my ($self, $x, $y, $w, $h) = @_; 995 my ($self, $w, $h) = @_;
836
837 $self->_size_allocate ($x, $y, $w, $h);
838 996
839 ($h, $w) = ($w, $h); 997 ($h, $w) = ($w, $h);
840 998
841 my $children = $self->{children}; 999 my $children = $self->{children};
842 1000
843 my @h = map +($_->size_request)[0], @$children; 1001 my @h = map $_->{req_w}, @$children;
844 1002
845 my $req_h = List::Util::sum @h; 1003 my $req_h = List::Util::sum @h;
846 1004
847 if ($req_h > $h) { 1005 if ($req_h > $h) {
848 # ah well, not enough space 1006 # ah well, not enough space
864 1022
865 my $y = 0; 1023 my $y = 0;
866 for (0 .. $#$children) { 1024 for (0 .. $#$children) {
867 my $child = $children->[$_]; 1025 my $child = $children->[$_];
868 my $h = $h[$_]; 1026 my $h = $h[$_];
869 $child->size_allocate ($y, 0, $h, $w); 1027 $child->configure ($y, 0, $h, $w);
870 1028
871 $y += $h; 1029 $y += $h;
872 } 1030 }
1031
1032 1
873} 1033}
874 1034
875############################################################################# 1035#############################################################################
876 1036
877package CFClient::UI::VBox; 1037package CFClient::UI::VBox;
890 (List::Util::sum map $_->[1], @alloc), 1050 (List::Util::sum map $_->[1], @alloc),
891 ) 1051 )
892} 1052}
893 1053
894sub size_allocate { 1054sub size_allocate {
895 my ($self, $x, $y, $w, $h) = @_; 1055 my ($self, $w, $h) = @_;
896
897 $self->_size_allocate ($x, $y, $w, $h);
898 1056
899 my $children = $self->{children}; 1057 my $children = $self->{children};
900 1058
901 my @h = map +($_->size_request)[1], @$children; 1059 my @h = map $_->{req_h}, @$children;
902 1060
903 my $req_h = List::Util::sum @h; 1061 my $req_h = List::Util::sum @h;
904 1062
905 if ($req_h > $h) { 1063 if ($req_h > $h) {
906 # ah well, not enough space 1064 # ah well, not enough space
920 1078
921 my $y = 0; 1079 my $y = 0;
922 for (0 .. $#$children) { 1080 for (0 .. $#$children) {
923 my $child = $children->[$_]; 1081 my $child = $children->[$_];
924 my $h = $h[$_]; 1082 my $h = $h[$_];
925 $child->size_allocate (0, $y, $w, $h); 1083 $child->configure (0, $y, $w, $h);
926 1084
927 $y += $h; 1085 $y += $h;
928 } 1086 }
1087
1088 1
929} 1089}
930 1090
931############################################################################# 1091#############################################################################
932 1092
933package CFClient::UI::Label; 1093package CFClient::UI::Label;
934 1094
935our @ISA = CFClient::UI::Base::; 1095our @ISA = CFClient::UI::Base::;
936 1096
937use SDL::OpenGL; 1097use CFClient::OpenGL;
938 1098
939sub new { 1099sub new {
940 my ($class, %arg) = @_; 1100 my ($class, %arg) = @_;
941 1101
942 my $self = $class->SUPER::new ( 1102 my $self = $class->SUPER::new (
943 fg => [1, 1, 1], 1103 fg => [1, 1, 1],
944 fontsize => $::FONTSIZE, 1104 fontsize => 1,
945 text => "", 1105 text => "",
946 align => -1, 1106 align => -1,
1107 valign => -1,
947 padding => 2, 1108 padding => 2,
948 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
949 %arg 1111 %arg
950 ); 1112 );
951 1113
952 $self->set_text ($self->{text}); 1114 if (exists $self->{template}) {
1115 my $layout = new CFClient::Layout;
1116 $layout->set_text (delete $self->{template});
1117 $self->{template} = $layout;
1118 }
1119
1120 $self->set_text (delete $self->{text}) if exists $self->{text};
1121 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
953 1122
954 $self 1123 $self
955} 1124}
956 1125
957sub escape_text { 1126sub escape_text {
965} 1134}
966 1135
967sub set_text { 1136sub set_text {
968 my ($self, $text) = @_; 1137 my ($self, $text) = @_;
969 1138
970 $self->{text} = $text;
971 $self->{layout}->set_markup ($text); 1139 $self->{layout}->set_text ($text);
972 1140
973 delete $self->{texture}; 1141 delete $self->{texture};
974# $self->{w} = $self->{h} = -1; 1142 $self->check_size;
975 $self->update; 1143 $self->update;
976} 1144}
977 1145
978sub get_text { 1146sub set_markup {
979 my ($self, $text) = @_; 1147 my ($self, $markup) = @_;
980 1148
981 $self->{text} 1149 $self->{layout}->set_markup ($markup);
1150
1151 delete $self->{texture};
1152 $self->check_size;
1153 $self->update;
982} 1154}
983 1155
984sub size_request { 1156sub size_request {
985 my ($self) = @_; 1157 my ($self) = @_;
986 1158
987 $self->{layout}->set_width; 1159 $self->{layout}->set_width;
988 $self->{layout}->set_height ($self->{fontsize}); 1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1161
989 my ($w, $h) = $self->{layout}->size; 1162 my ($w, $h) = $self->{layout}->size;
1163
1164 if (exists $self->{template}) {
1165 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1166
1167 my ($w2, $h2) = $self->{template}->size;
1168
1169 $w = List::Util::max $w, $w2;
1170 $h = List::Util::max $h, $h2;
1171 }
990 1172
991 ( 1173 (
992 $w + $self->{padding} * 2, 1174 $w + $self->{padding} * 2,
993 $h + $self->{padding} * 2, 1175 $h + $self->{padding} * 2,
994 ) 1176 )
995} 1177}
996 1178
997sub size_allocate { 1179sub size_allocate {
998 my ($self, $x, $y, $w, $h) = @_; 1180 my ($self, $w, $h) = @_;
999
1000 $self->_size_allocate ($x, $y, $w, $h) or return;
1001 1181
1002 delete $self->{texture}; 1182 delete $self->{texture};
1003} 1183}
1004 1184
1005sub update { 1185sub set_fontsize {
1006 my ($self) = @_; 1186 my ($self, $fontsize) = @_;
1007 1187
1188 $self->{fontsize} = $fontsize;
1008 delete $self->{texture}; 1189 delete $self->{texture};
1190 $self->check_size;
1009 $self->SUPER::update; 1191 $self->update;
1010} 1192}
1011 1193
1012sub _draw { 1194sub _draw {
1013 my ($self) = @_; 1195 my ($self) = @_;
1014 1196
1015 my $tex = $self->{texture} ||= do { 1197 my $tex = $self->{texture} ||= do {
1016 $self->{layout}->set_width ($self->{w}); 1198 $self->{layout}->set_width ($self->{w});
1017 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1199 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1018 new_from_layout CFClient::Texture $self->{layout} 1200 new_from_layout CFClient::Texture $self->{layout}
1019 }; 1201 };
1020 1202
1021 glEnable GL_BLEND; 1203 glEnable GL_BLEND;
1022 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1023 glEnable GL_TEXTURE_2D; 1205 glEnable GL_TEXTURE_2D;
1024 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1025 1207
1026 glColor @{$self->{fg}}; 1208 glColor @{$self->{fg}};
1027 1209
1028 my $x = 1210 $self->{ox} = int (
1029 $self->{align} < 0 ? $self->{padding} 1211 $self->{align} < 0 ? $self->{padding}
1030 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1212 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1031 : ($self->{w} - $tex->{w}) * 0.5; 1213 : ($self->{w} - $tex->{w}) * 0.5
1214 );
1032 1215
1033 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1216 $self->{oy} = int (
1217 $self->{valign} < 0 ? $self->{padding}
1218 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1219 : ($self->{h} - $tex->{h}) * 0.5
1220 );
1221
1222 $tex->draw_quad ($self->{ox}, $self->{oy});
1034 1223
1035 glDisable GL_TEXTURE_2D; 1224 glDisable GL_TEXTURE_2D;
1036 glDisable GL_BLEND; 1225 glDisable GL_BLEND;
1037} 1226}
1038 1227
1039############################################################################# 1228#############################################################################
1040 1229
1041package CFClient::UI::Entry; 1230package CFClient::UI::EntryBase;
1042 1231
1043our @ISA = CFClient::UI::Label::; 1232our @ISA = CFClient::UI::Label::;
1044 1233
1045use SDL; 1234use CFClient::OpenGL;
1046use SDL::OpenGL;
1047 1235
1048sub new { 1236sub new {
1049 my $class = shift; 1237 my $class = shift;
1050 1238
1051 $class->SUPER::new ( 1239 $class->SUPER::new (
1053 bg => [0, 0, 0, 0.2], 1241 bg => [0, 0, 0, 0.2],
1054 active_bg => [1, 1, 1, 0.5], 1242 active_bg => [1, 1, 1, 0.5],
1055 active_fg => [0, 0, 0], 1243 active_fg => [0, 0, 0],
1056 can_hover => 1, 1244 can_hover => 1,
1057 can_focus => 1, 1245 can_focus => 1,
1246 valign => 0,
1247 can_events => 1,
1058 @_ 1248 @_
1059 ) 1249 )
1060} 1250}
1061 1251
1062sub _set_text { 1252sub _set_text {
1063 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1064 1254
1065 my $old_text = $self->{text}; 1255 delete $self->{cur_h};
1256
1257 return if $self->{text} eq $text;
1258
1259 delete $self->{texture};
1066 1260
1067 $self->{last_activity} = $::NOW; 1261 $self->{last_activity} = $::NOW;
1068
1069 $self->{text} = $text; 1262 $self->{text} = $text;
1070 $self->{layout}->set_width ($self->{w});
1071 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1072 1263
1073 $text =~ s/./*/g if $self->{hidden}; 1264 $text =~ s/./*/g if $self->{hidden};
1265 $self->{layout}->set_text ("$text ");
1074 1266
1075 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1267 $self->emit (changed => $self->{text});
1268}
1076 1269
1077 $text = substr $text, 0, $self->{cursor}; 1270sub get_text {
1078 utf8::encode $text; 1271 $_[0]{text}
1079
1080 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1081
1082 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1083 if $old_text ne $self->{text};
1084} 1272}
1085 1273
1086sub size_request { 1274sub size_request {
1087 my ($self) = @_; 1275 my ($self) = @_;
1088 1276
1090 1278
1091 ($w + 1, $h) # add 1 for cursor 1279 ($w + 1, $h) # add 1 for cursor
1092} 1280}
1093 1281
1094sub size_allocate { 1282sub size_allocate {
1095 my ($self, $x, $y, $w, $h) = @_; 1283 my ($self, $w, $h) = @_;
1096 1284
1097 $self->SUPER::size_allocate ($x, $y, $w, $h); 1285 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1098
1099 $self->_set_text ($self->{text});
1100} 1286}
1101 1287
1102sub set_text { 1288sub set_text {
1103 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1104 1290
1108} 1294}
1109 1295
1110sub key_down { 1296sub key_down {
1111 my ($self, $ev) = @_; 1297 my ($self, $ev) = @_;
1112 1298
1113 my $mod = $ev->key_mod; 1299 my $mod = $ev->{mod};
1114 my $sym = $ev->key_sym; 1300 my $sym = $ev->{sym};
1115
1116 my $uni = $ev->key_unicode; 1301 my $uni = $ev->{unicode};
1117 1302
1118 my $text = $self->get_text; 1303 my $text = $self->get_text;
1119 1304
1120 if ($sym == SDLK_BACKSPACE) { 1305 if ($sym == 8) {
1121 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1306 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1122 } elsif ($sym == SDLK_DELETE) { 1307 } elsif ($sym == 127) {
1123 substr $text, $self->{cursor}, 1, ""; 1308 substr $text, $self->{cursor}, 1, "";
1124 } elsif ($sym == SDLK_LEFT) { 1309 } elsif ($sym == CFClient::SDLK_LEFT) {
1125 --$self->{cursor} if $self->{cursor}; 1310 --$self->{cursor} if $self->{cursor};
1126 } elsif ($sym == SDLK_RIGHT) { 1311 } elsif ($sym == CFClient::SDLK_RIGHT) {
1127 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1312 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1128 } elsif ($sym == SDLK_HOME) { 1313 } elsif ($sym == CFClient::SDLK_HOME) {
1129 $self->{cursor} = 0; 1314 $self->{cursor} = 0;
1130 } elsif ($sym == SDLK_END) { 1315 } elsif ($sym == CFClient::SDLK_END) {
1131 $self->{cursor} = length $text; 1316 $self->{cursor} = length $text;
1132 } elsif ($sym == SDLK_ESCAPE) { 1317 } elsif ($sym == 27) {
1133 $self->emit ('escape'); 1318 $self->emit ('escape');
1134 } elsif ($uni) { 1319 } elsif ($uni) {
1135 substr $text, $self->{cursor}++, 0, chr $uni; 1320 substr $text, $self->{cursor}++, 0, chr $uni;
1136 } 1321 }
1137 1322
1192 1377
1193 $self->SUPER::_draw; 1378 $self->SUPER::_draw;
1194 1379
1195 #TODO: force update every cursor change :( 1380 #TODO: force update every cursor change :(
1196 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1381 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1382
1383 unless (exists $self->{cur_h}) {
1384 my $text = substr $self->{text}, 0, $self->{cursor};
1385 utf8::encode $text;
1386
1387 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1388 }
1389
1197 glColor @{$self->{fg}}; 1390 glColor @{$self->{fg}};
1198 glBegin GL_LINES; 1391 glBegin GL_LINES;
1199 glVertex $self->{cur_x}, $self->{cur_y};
1200 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1392 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1393 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1201 glEnd; 1394 glEnd;
1202 } 1395 }
1203} 1396}
1204 1397
1205package CFClient::UI::LineEntry; 1398package CFClient::UI::Entry;
1206 1399
1207our @ISA = CFClient::UI::Entry::; 1400our @ISA = CFClient::UI::EntryBase::;
1208 1401
1209use SDL; 1402use CFClient::OpenGL;
1210use SDL::OpenGL;
1211 1403
1212sub key_down { 1404sub key_down {
1213 my ($self, $ev) = @_; 1405 my ($self, $ev) = @_;
1214 1406
1215 my $sym = $ev->key_sym; 1407 my $sym = $ev->{sym};
1216 1408
1217 if ($sym == SDLK_RETURN) { 1409 if ($sym == 13) {
1218 $self->emit (activate => $self->get_text); 1410 $self->emit (activate => $self->get_text);
1219 $self->update; 1411 $self->update;
1220 1412
1221 } else { 1413 } else {
1222 $self->SUPER::key_down ($ev); 1414 $self->SUPER::key_down ($ev);
1228 1420
1229package CFClient::UI::Button; 1421package CFClient::UI::Button;
1230 1422
1231our @ISA = CFClient::UI::Label::; 1423our @ISA = CFClient::UI::Label::;
1232 1424
1233use SDL; 1425use CFClient::OpenGL;
1234use SDL::OpenGL;
1235 1426
1236my @tex = 1427my @tex =
1237 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1238 qw(b1_button_active.png); 1429 qw(b1_button_active.png);
1239 1430
1240sub new { 1431sub new {
1241 my $class = shift; 1432 my $class = shift;
1242 1433
1243 $class->SUPER::new ( 1434 $class->SUPER::new (
1244 padding => 4, 1435 padding => 4,
1245 fg => [1, 1, 1], 1436 fg => [1, 1, 1],
1246 bg => [1, 1, 1, 0.2], 1437 bg => [1, 1, 1, 0.2],
1247 active_fg => [1, 1, 0], 1438 active_fg => [0, 0, 1],
1248 can_hover => 1, 1439 can_hover => 1,
1440 align => 0,
1441 valign => 0,
1442 can_events => 1,
1249 @_ 1443 @_
1250 ) 1444 )
1251} 1445}
1252 1446
1253sub button_up { 1447sub button_up {
1261 1455
1262sub _draw { 1456sub _draw {
1263 my ($self) = @_; 1457 my ($self) = @_;
1264 1458
1265 local $self->{fg} = $self->{fg}; 1459 local $self->{fg} = $self->{fg};
1266 my $tex = $tex[0];
1267
1268 glEnable GL_BLEND;
1269 glEnable GL_TEXTURE_2D;
1270 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1271 1460
1272 if ($GRAB == $self) { 1461 if ($GRAB == $self) {
1273 $self->{fg} = $self->{active_fg}; 1462 $self->{fg} = $self->{active_fg};
1274 } 1463 }
1275 1464
1276 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1465 glEnable GL_BLEND;
1466 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1467 glEnable GL_TEXTURE_2D;
1277 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1468 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1469 glColor 0, 0, 0, 1;
1278 1470
1279 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1471 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1280 1472
1281 glDisable GL_TEXTURE_2D; 1473 glDisable GL_TEXTURE_2D;
1282 glDisable GL_BLEND; 1474 glDisable GL_BLEND;
1283 1475
1284 $self->SUPER::_draw; 1476 $self->SUPER::_draw;
1289package CFClient::UI::CheckBox; 1481package CFClient::UI::CheckBox;
1290 1482
1291our @ISA = CFClient::UI::DrawBG::; 1483our @ISA = CFClient::UI::DrawBG::;
1292 1484
1293my @tex = 1485my @tex =
1294 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1486 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1295 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1487 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1296 1488
1297use SDL; 1489use CFClient::OpenGL;
1298use SDL::OpenGL;
1299 1490
1300sub new { 1491sub new {
1301 my $class = shift; 1492 my $class = shift;
1302 1493
1303 $class->SUPER::new ( 1494 $class->SUPER::new (
1314 my ($self) = @_; 1505 my ($self) = @_;
1315 1506
1316 ($self->{padding} * 2 + 6) x 2 1507 ($self->{padding} * 2 + 6) x 2
1317} 1508}
1318 1509
1319sub size_allocate {
1320 my ($self, $x, $y, $w, $h) = @_;
1321
1322 $self->_size_allocate ($x, $y, $w, $h);
1323}
1324
1325sub button_down { 1510sub button_down {
1326 my ($self, $ev, $x, $y) = @_; 1511 my ($self, $ev, $x, $y) = @_;
1327 1512
1328 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1513 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1329 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1514 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1355 glDisable GL_BLEND; 1540 glDisable GL_BLEND;
1356} 1541}
1357 1542
1358############################################################################# 1543#############################################################################
1359 1544
1545package CFClient::UI::Image;
1546
1547our @ISA = CFClient::UI::Base::;
1548
1549use CFClient::OpenGL;
1550use Carp qw/confess/;
1551
1552our %loaded_images;
1553
1554sub new {
1555 my $class = shift;
1556
1557 my $self = $class->SUPER::new (can_events => 0, @_);
1558
1559 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1560
1561 $loaded_images{$self->{image}} ||=
1562 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1563
1564 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1565
1566 Scalar::Util::weaken $loaded_images{$self->{image}};
1567
1568 $self->{aspect} = $tex->{w} / $tex->{h};
1569
1570 $self
1571}
1572
1573sub size_request {
1574 my ($self) = @_;
1575
1576 ($self->{tex}->{w}, $self->{tex}->{h})
1577}
1578
1579sub _draw {
1580 my ($self) = @_;
1581
1582 my $tex = $self->{tex};
1583
1584 my ($w, $h) = ($self->{w}, $self->{h});
1585
1586 if ($self->{rot90}) {
1587 glRotate 90, 0, 0, 1;
1588 glTranslate 0, -$self->{w}, 0;
1589
1590 ($w, $h) = ($h, $w);
1591 }
1592
1593 glEnable GL_BLEND;
1594 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1595 glEnable GL_TEXTURE_2D;
1596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1597
1598 $tex->draw_quad (0, 0, $w, $h);
1599
1600 glDisable GL_BLEND;
1601 glDisable GL_TEXTURE_2D;
1602}
1603
1604#############################################################################
1605
1606package CFClient::UI::VGauge;
1607
1608our @ISA = CFClient::UI::Base::;
1609
1610use CFClient::OpenGL;
1611
1612my %tex = (
1613 food => [
1614 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1615 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1616 ],
1617 grace => [
1618 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1619 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1620 ],
1621 hp => [
1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1623 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1624 ],
1625 mana => [
1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1627 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1628 ],
1629);
1630
1631# eg. VGauge->new (gauge => 'food'), default gauge: food
1632sub new {
1633 my $class = shift;
1634
1635 my $self = $class->SUPER::new (
1636 type => 'food',
1637 @_
1638 );
1639
1640 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1641
1642 $self
1643}
1644
1645sub size_request {
1646 my ($self) = @_;
1647
1648 #my $tex = $tex{$self->{type}}[0];
1649 #@$tex{qw(w h)}
1650 (0, 0)
1651}
1652
1653sub set_max {
1654 my ($self, $max) = @_;
1655
1656 $self->{max_val} = $max;
1657}
1658
1659sub set_value {
1660 my ($self, $val, $max) = @_;
1661
1662 $self->set_max ($max)
1663 if defined $max;
1664
1665 $max = $self->{max_val};
1666 $self->{val} = $val;
1667
1668 $self->update;
1669}
1670
1671sub _draw {
1672 my ($self) = @_;
1673
1674 my $tex = $tex{$self->{type}};
1675
1676 my ($w, $h) = ($self->{w}, $self->{h});
1677
1678 if ($self->{vertical}) {
1679 glRotate 90, 0, 0, 1;
1680 glTranslate 0, -$self->{w}, 0;
1681
1682 ($w, $h) = ($h, $w);
1683 }
1684
1685 my $ycut = $self->{val} / ($self->{max_val} || 1);
1686 $ycut = 1 if $self->{val} > $self->{max_val};
1687
1688 my $t1 = $tex->[0];
1689 my $t2 = $tex->[1];
1690
1691 glEnable GL_BLEND;
1692 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1693 glEnable GL_TEXTURE_2D;
1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1695
1696 my $h1 = $self->{h} - $ycut * $self->{h};
1697 my $h2 = $ycut * $self->{h};
1698
1699 glBindTexture GL_TEXTURE_2D, $t1->{name};
1700 glBegin GL_QUADS;
1701 glTexCoord 0 , 0; glVertex 0 , 0;
1702 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1703 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1704 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1705 glEnd;
1706
1707 glBindTexture GL_TEXTURE_2D, $t2->{name};
1708 glBegin GL_QUADS;
1709 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1710 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1711 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1712 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1713 glEnd;
1714
1715 glDisable GL_BLEND;
1716 glDisable GL_TEXTURE_2D;
1717}
1718
1719#############################################################################
1720
1721package CFClient::UI::Gauge;
1722
1723our @ISA = CFClient::UI::VBox::;
1724
1725sub new {
1726 my ($class, %arg) = @_;
1727
1728 my $self = $class->SUPER::new (
1729 tooltip => $arg{type},
1730 %arg,
1731 );
1732
1733 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1734 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1735 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1736
1737 $self
1738}
1739
1740sub set_fontsize {
1741 my ($self, $fsize) = @_;
1742
1743 $self->{value}->set_fontsize ($fsize);
1744 $self->{max} ->set_fontsize ($fsize);
1745}
1746
1747sub set_value {
1748 my ($self, $val, $max) = @_;
1749
1750 $self->set_max ($max)
1751 if defined $max;
1752
1753 $self->{gauge}->set_value ($val, $max);
1754 $self->{value}->set_text ($val);
1755}
1756
1757sub set_max {
1758 my ($self, $max) = @_;
1759
1760 $self->{gauge}->set_max ($max);
1761 $self->{max}->set_text ($max);
1762}
1763
1764#############################################################################
1765
1360package CFClient::UI::Slider; 1766package CFClient::UI::Slider;
1361 1767
1362use strict; 1768use strict;
1363 1769
1364use SDL::OpenGL; 1770use CFClient::OpenGL;
1365 1771
1366our @ISA = CFClient::UI::DrawBG::; 1772our @ISA = CFClient::UI::DrawBG::;
1367 1773
1368my @tex = 1774my @tex =
1369 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1775 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1379 # TODO: calculations are off 1785 # TODO: calculations are off
1380 my $self = $class->SUPER::new ( 1786 my $self = $class->SUPER::new (
1381 fg => [1, 1, 1], 1787 fg => [1, 1, 1],
1382 active_fg => [0, 0, 0], 1788 active_fg => [0, 0, 0],
1383 range => [0, 0, 100, 10], 1789 range => [0, 0, 100, 10],
1384 req_w => 40, 1790 req_w => 20,
1385 req_h => 13, 1791 req_h => 20,
1386 vertical => 0, 1792 vertical => 0,
1387 can_hover => 1, 1793 can_hover => 1,
1388 inner_pad => 5, 1794 inner_pad => 5,
1389 @_ 1795 @_
1390 ); 1796 );
1494 1900
1495package CFClient::UI::TextView; 1901package CFClient::UI::TextView;
1496 1902
1497our @ISA = CFClient::UI::HBox::; 1903our @ISA = CFClient::UI::HBox::;
1498 1904
1499use SDL::OpenGL; 1905use CFClient::OpenGL;
1500 1906
1501sub new { 1907sub new {
1502 my $class = shift; 1908 my $class = shift;
1503 1909
1504 my $self = $class->SUPER::new ( 1910 my $self = $class->SUPER::new (
1505 req_w => $::WIDTH / 6,
1506 req_h => $::HEIGHT / 6,
1507 fontsize => $::FONTSIZE, 1911 fontsize => 1,
1508 @_, 1912 @_,
1509 1913
1510 layout => (new CFClient::Layout), 1914 layout => (new CFClient::Layout),
1511 par => [], 1915 par => [],
1512 height => 0, 1916 height => 0,
1533sub text_height { 1937sub text_height {
1534 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1535 1939
1536 my $layout = $self->{layout}; 1940 my $layout = $self->{layout};
1537 1941
1538 $layout->set_height ($self->{fontsize}); 1942 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1539 $layout->set_width ($self->{w}); 1943 $layout->set_width ($self->{w});
1540 $layout->set_text ($text); 1944 $layout->set_text ($text);
1541 1945
1542 ($layout->size)[1] 1946 ($layout->size)[1]
1543} 1947}
1547 1951
1548 $self->{need_reflow}++; 1952 $self->{need_reflow}++;
1549 $self->update; 1953 $self->update;
1550} 1954}
1551 1955
1552sub size_request {
1553 my ($self) = @_;
1554
1555 ($self->{req_w}, $self->{req_h})
1556}
1557
1558sub size_allocate { 1956sub size_allocate {
1559 my ($self, $x, $y, $w, $h) = @_; 1957 my ($self, $w, $h) = @_;
1560 1958
1561 $self->SUPER::size_allocate ($x, $y, $w, $h); 1959 $self->SUPER::size_allocate ($w, $h);
1562 1960
1563 $self->{layout}->set_height ($self->{fontsize}); 1961 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1564 $self->{layout}->set_width ($self->{w}); 1962 $self->{layout}->set_width ($self->{children}[0]{w});
1565 1963
1566 $self->reflow; 1964 $self->reflow;
1567} 1965}
1568 1966
1569sub add_paragraph { 1967sub add_paragraph {
1656 2054
1657} 2055}
1658 2056
1659############################################################################# 2057#############################################################################
1660 2058
1661package CFClient::UI::MapWidget;
1662
1663use strict;
1664
1665use List::Util qw(min max);
1666
1667use SDL;
1668use SDL::OpenGL;
1669
1670our @ISA = CFClient::UI::Base::;
1671
1672sub new {
1673 my $class = shift;
1674
1675 $class->SUPER::new (
1676 z => -1,
1677 can_focus => 1,
1678 list => (glGenLists 1),
1679 @_
1680 )
1681}
1682
1683sub key_down {
1684 print "MAPKEYDOWN\n";
1685}
1686
1687sub key_up {
1688}
1689
1690sub button_down {
1691 my ($self, $ev, $x, $y) = @_;
1692
1693 $self->focus_in;
1694
1695 if ($ev->button == 2) {
1696 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1697 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1698
1699 $self->{motion} = sub {
1700 my ($ev, $x, $y) = @_;
1701
1702 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1703
1704 $::CFG->{map_shift_x} = $bw + $x - $ox;
1705 $::CFG->{map_shift_y} = $bh + $y - $oy;
1706
1707 $self->update;
1708 };
1709 }
1710}
1711
1712sub button_up {
1713 my ($self, $ev, $x, $y) = @_;
1714
1715 delete $self->{motion};
1716}
1717
1718sub mouse_motion {
1719 my ($self, $ev, $x, $y) = @_;
1720
1721 $self->{motion}->($ev, $x, $y) if $self->{motion};
1722}
1723
1724sub size_request {
1725 (
1726 1 + 32 * int $::WIDTH / 32,
1727 1 + 32 * int $::HEIGHT / 32,
1728 )
1729}
1730
1731sub update {
1732 my ($self) = @_;
1733
1734 $self->{need_update} = 1;
1735 $self->SUPER::update;
1736}
1737
1738sub draw {
1739 my ($self) = @_;
1740
1741 if (delete $self->{need_update}) {
1742 glNewList $self->{list}, GL_COMPILE;
1743
1744 if ($::MAP) {
1745 my $sw = int $::WIDTH / 32;
1746 my $sh = int $::HEIGHT / 32;
1747
1748 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1749 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1750
1751 glTranslate $sx0 - 32, $sy0 - 32, 0;
1752
1753 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1754
1755 if ($::CFG->{fow_enable}) {
1756 if ($::CFG->{fow_smooth}) { # smooth fog of war
1757 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1758 glConvolutionFilter2D
1759 GL_CONVOLUTION_2D,
1760 GL_ALPHA,
1761 3, 3,
1762 GL_ALPHA, GL_FLOAT,
1763 pack "f*",
1764 0.1, 0.1, 0.1,
1765 0.1, 0.2, 0.1,
1766 0.1, 0.1, 0.1,
1767 ;
1768 glEnable GL_CONVOLUTION_2D;
1769 }
1770
1771 my $tex = new CFClient::Texture
1772 w => $w,
1773 h => $h,
1774 data => $data,
1775 internalformat => GL_ALPHA,
1776 format => GL_ALPHA;
1777
1778 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1779
1780 glEnable GL_BLEND;
1781 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1782 glEnable GL_TEXTURE_2D;
1783 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1784
1785 glColor +($::CFG->{fow_intensity}) x 3, 1;
1786 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1787
1788 glDisable GL_TEXTURE_2D;
1789 glDisable GL_BLEND;
1790 }
1791
1792 # HACK BEGIN
1793 {
1794 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1795 my ($w, $h) = (250, 250);
1796
1797 glEnable GL_BLEND;
1798 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1799 glEnable GL_TEXTURE_2D;
1800 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1801
1802 CFClient::Texture->new (
1803 w => $w,
1804 h => $h,
1805 data => $::MAP->mapmap ($w, $h),
1806 type => GL_UNSIGNED_INT_8_8_8_8_REV
1807 )->draw_quad (100, 100);
1808
1809 glDisable GL_TEXTURE_2D;
1810 glDisable GL_BLEND;
1811 }
1812 # HACK END
1813 }
1814
1815 glEndList;
1816 }
1817
1818 glPushMatrix;
1819 glCallList $self->{list};
1820 glPopMatrix;
1821
1822 if ($FOCUS != $self) {
1823 glColor 64/255, 64/255, 64/255;
1824 glLogicOp GL_AND;
1825 glEnable GL_COLOR_LOGIC_OP;
1826 glBegin GL_QUADS;
1827 glVertex 0, 0;
1828 glVertex 0, $::HEIGHT;
1829 glVertex $::WIDTH, $::HEIGHT;
1830 glVertex $::WIDTH, 0;
1831 glEnd;
1832 glDisable GL_COLOR_LOGIC_OP;
1833 }
1834}
1835
1836my %DIR = (
1837 SDLK_KP8, [1, "north"],
1838 SDLK_KP9, [2, "northeast"],
1839 SDLK_KP6, [3, "east"],
1840 SDLK_KP3, [4, "southeast"],
1841 SDLK_KP2, [5, "south"],
1842 SDLK_KP1, [6, "southwest"],
1843 SDLK_KP4, [7, "west"],
1844 SDLK_KP7, [8, "northwest"],
1845
1846 SDLK_UP, [1, "north"],
1847 SDLK_RIGHT, [3, "east"],
1848 SDLK_DOWN, [5, "south"],
1849 SDLK_LEFT, [7, "west"],
1850);
1851
1852sub key_down {
1853 my ($self, $ev) = @_;
1854
1855 my $mod = $ev->key_mod;
1856 my $sym = $ev->key_sym;
1857
1858 if ($sym == SDLK_KP5) {
1859 $::CONN->user_send ("command stay fire");
1860 } elsif ($sym == SDLK_a) {
1861 $::CONN->user_send ("command apply");
1862 } elsif ($sym == SDLK_QUOTE) {
1863 $self->emit ('activate_console');
1864 } elsif ($sym == SDLK_SLASH) {
1865 $self->emit ('activate_console' => '/');
1866 } elsif (exists $DIR{$sym}) {
1867 if ($mod & KMOD_SHIFT) {
1868 $self->{shft}++;
1869 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1870 } elsif ($mod & KMOD_CTRL) {
1871 $self->{ctrl}++;
1872 $::CONN->user_send ("command run $DIR{$sym}[0]");
1873 } else {
1874 $::CONN->user_send ("command $DIR{$sym}[1]");
1875 }
1876 }
1877}
1878
1879sub key_up {
1880 my ($self, $ev) = @_;
1881
1882 my $mod = $ev->key_mod;
1883 my $sym = $ev->key_sym;
1884
1885 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1886 $::CONN->user_send ("command fire_stop");
1887 }
1888 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1889 $::CONN->user_send ("command run_stop");
1890 }
1891}
1892
1893#############################################################################
1894
1895package CFClient::UI::Animator; 2059package CFClient::UI::Animator;
1896 2060
1897use SDL::OpenGL; 2061use CFClient::OpenGL;
1898 2062
1899our @ISA = CFClient::UI::Bin::; 2063our @ISA = CFClient::UI::Bin::;
1900 2064
1901sub moveto { 2065sub moveto {
1902 my ($self, $x, $y) = @_; 2066 my ($self, $x, $y) = @_;
1942 my $class = shift; 2106 my $class = shift;
1943 2107
1944 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
1945 state => 0, 2109 state => 0,
1946 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
1947 @_ 2112 @_
1948 ); 2113 );
1949 2114
1950 if ($self->{state}) { 2115 if ($self->{state}) {
1951 $self->{state} = 0; 2116 $self->{state} = 0;
1971 $self->emit (changed => $self->{state}); 2136 $self->emit (changed => $self->{state});
1972} 2137}
1973 2138
1974############################################################################# 2139#############################################################################
1975 2140
2141package CFClient::UI::Tooltip;
2142
2143our @ISA = CFClient::UI::Bin::;
2144
2145use CFClient::OpenGL;
2146
2147sub new {
2148 my $class = shift;
2149
2150 $class->SUPER::new (
2151 @_,
2152 can_events => 0,
2153 )
2154}
2155
2156sub set_text {
2157 my ($self, $text) = @_;
2158
2159 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2160 $self->{label}->set_text ($text);
2161 $self->add ($self->{label});
2162}
2163
2164sub size_request {
2165 my ($self) = @_;
2166
2167 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2168
2169 $w = List::Util::min $::WIDTH * 0.2, $w;
2170 $h = List::Util::max $::HEIGHT * 0.2, $h;
2171
2172 ($w + 4, $h + 4)
2173}
2174
2175sub _draw {
2176 my ($self) = @_;
2177
2178 glPushMatrix;
2179 glTranslate 0.375, 0.375;
2180
2181 my ($w, $h) = @$self{qw(w h)};
2182
2183 glColor 1, 0.8, 0.4;
2184 glBegin GL_QUADS;
2185 glVertex 0 , 0;
2186 glVertex 0 , $h;
2187 glVertex $w, $h;
2188 glVertex $w, 0;
2189 glEnd;
2190
2191 glColor 0, 0, 0;
2192 glBegin GL_LINE_LOOP;
2193 glVertex 0 , 0;
2194 glVertex 0 , $h;
2195 glVertex $w, $h;
2196 glVertex $w, 0;
2197 glEnd;
2198
2199 glPopMatrix;
2200
2201 glTranslate 2, 2;
2202 $self->SUPER::_draw;
2203}
2204
2205#############################################################################
2206
1976package CFClient::UI::Root; 2207package CFClient::UI::Root;
1977 2208
1978our @ISA = CFClient::UI::Container::; 2209our @ISA = CFClient::UI::Container::;
1979 2210
1980use SDL::OpenGL; 2211use CFClient::OpenGL;
2212
2213sub check_size {
2214 my ($self) = @_;
2215
2216 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2217}
1981 2218
1982sub size_request { 2219sub size_request {
1983 ($::WIDTH, $::HEIGHT) 2220 ($::WIDTH, $::HEIGHT)
1984} 2221}
1985 2222
1986sub size_allocate { 2223sub configure {
1987 my ($self, $x, $y, $w, $h) = @_; 2224 my ($self, $x, $y, $w, $h) = @_;
1988 2225
1989 $self->_size_allocate ($x, $y, $w, $h); 2226 $self->SUPER::configure ($x, $y, $w, $h);
1990 2227
1991 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1992 for @{$self->{children}}; 2228 for my $child (@{$self->{children}}) {
2229 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2230
2231 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2232 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2233 $child->configure ($X, $Y, $W,$H);
2234 }
1993} 2235}
1994 2236
1995sub _topleft { 2237sub _topleft {
1996 my ($self, $x, $y) = @_; 2238 my ($self, $x, $y) = @_;
1997 2239
1999} 2241}
2000 2242
2001sub update { 2243sub update {
2002 my ($self) = @_; 2244 my ($self) = @_;
2003 2245
2004 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2246 $self->check_size;
2005 ::refresh (); 2247 ::refresh ();
2006} 2248}
2007 2249
2008sub add { 2250sub add {
2009 my ($self, $widget) = @_; 2251 my ($self, $child) = @_;
2010 2252
2253 # integerize window positions
2254 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y};
2256
2011 $self->SUPER::add ($widget); 2257 $self->SUPER::add ($child);
2012
2013 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2014} 2258}
2015 2259
2016sub on_refresh { 2260sub on_refresh {
2017 my ($self, $id, $cb) = @_; 2261 my ($self, $id, $cb) = @_;
2018 2262
2042############################################################################# 2286#############################################################################
2043 2287
2044package CFClient::UI; 2288package CFClient::UI;
2045 2289
2046$ROOT = new CFClient::UI::Root; 2290$ROOT = new CFClient::UI::Root;
2291$TOOLTIP = new CFClient::UI::Tooltip;
2047 2292
20481 22931
2049 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines