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.120 by elmex, Sun Apr 16 17:43:17 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
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} = int $x; 166 $self->{x} = int $x;
118 $self->{y} = int $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) = @_;
225 glPopMatrix; 290 glPopMatrix;
226 291
227 if ($self == $HOVER && $self->{can_hover}) { 292 if ($self == $HOVER && $self->{can_hover}) {
228 my ($x, $y) = @$self{qw(x y)}; 293 my ($x, $y) = @$self{qw(x y)};
229 294
230 glColor 0, 0, 1, 0.2; 295 glColor 1, 0.8, 0.5, 0.2;
231 glEnable GL_BLEND; 296 glEnable GL_BLEND;
232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 297 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
233 glBegin GL_QUADS; 298 glBegin GL_QUADS;
234 glVertex $x , $y; 299 glVertex $x , $y;
235 glVertex $x + $self->{w}, $y; 300 glVertex $x + $self->{w}, $y;
247} 312}
248 313
249sub find_widget { 314sub find_widget {
250 my ($self, $x, $y) = @_; 315 my ($self, $x, $y) = @_;
251 316
317 return () unless $self->{can_events};
318
252 return $self 319 return $self
253 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 320 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
254 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 321 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
255 322
256 () 323 ()
260 my ($self, $parent) = @_; 327 my ($self, $parent) = @_;
261 328
262 Scalar::Util::weaken ($self->{parent} = $parent); 329 Scalar::Util::weaken ($self->{parent} = $parent);
263} 330}
264 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
265sub update { 347sub update {
266 my ($self) = @_; 348 my ($self) = @_;
267 349
268 $self->{parent}->update 350 $self->{parent}->update
269 if $self->{parent}; 351 if $self->{parent};
270} 352}
271 353
272sub connect { 354sub connect {
273 my ($self, $signal, $cb) = @_; 355 my ($self, $signal, $cb) = @_;
274 356
275 push @{ $self->{cb}{$signal} }, $cb; 357 push @{ $self->{signal_cb}{$signal} }, $cb;
276} 358}
277 359
278sub emit { 360sub emit {
279 my ($self, $signal, @args) = @_; 361 my ($self, $signal, @args) = @_;
280 362
363 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
281 $_->($self, @args) 364 $cb->($self, @args);
282 for @{$self->{cb}{$signal} || []}; 365 }
283} 366}
284 367
285sub DESTROY { 368sub DESTROY {
286 my ($self) = @_; 369 my ($self) = @_;
287 370
293package CFClient::UI::DrawBG; 376package CFClient::UI::DrawBG;
294 377
295our @ISA = CFClient::UI::Base::; 378our @ISA = CFClient::UI::Base::;
296 379
297use strict; 380use strict;
298use SDL::OpenGL; 381use CFClient::OpenGL;
299 382
300sub new { 383sub new {
301 my $class = shift; 384 my $class = shift;
302 385
303 # range [value, low, high, page] 386 # range [value, low, high, page]
332 415
333package CFClient::UI::Empty; 416package CFClient::UI::Empty;
334 417
335our @ISA = CFClient::UI::Base::; 418our @ISA = CFClient::UI::Base::;
336 419
420sub new {
421 my ($class, %arg) = @_;
422 $class->SUPER::new (can_events => 0, %arg);
423}
424
337sub size_request { 425sub size_request {
338 (0, 0) 426 (0, 0)
339} 427}
340 428
341sub draw { } 429sub draw { }
349sub new { 437sub new {
350 my ($class, %arg) = @_; 438 my ($class, %arg) = @_;
351 439
352 my $children = delete $arg{children} || []; 440 my $children = delete $arg{children} || [];
353 441
354 my $self = $class->SUPER::new (children => [], %arg); 442 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
355 $self->add ($_) for @$children; 443 $self->add ($_) for @$children;
356 444
357 $self 445 $self
358} 446}
359 447
367 $self->{children} = [ 455 $self->{children} = [
368 sort { $a->{z} <=> $b->{z} } 456 sort { $a->{z} <=> $b->{z} }
369 @{$self->{children}}, $child 457 @{$self->{children}}, $child
370 ]; 458 ];
371 459
372 $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;
373 $self->update; 471 $self->update;
374}
375
376sub remove {
377 my ($self, $widget) = @_;
378
379 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
380
381 $self->size_allocate (0, 0, $self->{w}, $self->{h});
382} 472}
383 473
384sub find_widget { 474sub find_widget {
385 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
386 476
416 506
417 $class->SUPER::new (children => [$child], %arg) 507 $class->SUPER::new (children => [$child], %arg)
418} 508}
419 509
420sub add { 510sub add {
421 my ($self, $widget) = @_; 511 my ($self, $child) = @_;
422 512
423 $self->{children} = []; 513 $self->{children} = [];
424 514
425 $self->SUPER::add ($widget); 515 $self->SUPER::add ($child);
426} 516}
427 517
428sub remove { 518sub remove {
429 my ($self, $widget) = @_; 519 my ($self, $widget) = @_;
430 520
439sub size_request { 529sub size_request {
440 $_[0]{children}[0]->size_request 530 $_[0]{children}[0]->size_request
441} 531}
442 532
443sub size_allocate { 533sub size_allocate {
444 my ($self, $x, $y, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
445 535
446 $self->_size_allocate ($x, $y, $w, $h) or return;
447
448 $self->{children}[0]->size_allocate (0, 0, $w, $h); 536 $self->{children}[0]->configure (0, 0, $w, $h);
449} 537}
450 538
451############################################################################# 539#############################################################################
452 540
453package CFClient::UI::Window; 541package CFClient::UI::Window;
454 542
455our @ISA = CFClient::UI::Bin::; 543our @ISA = CFClient::UI::Bin::;
456 544
457use SDL::OpenGL; 545use CFClient::OpenGL;
458 546
459sub new { 547sub new {
460 my ($class, %arg) = @_; 548 my ($class, %arg) = @_;
461 549
462 my $self = $class->SUPER::new (%arg); 550 my $self = $class->SUPER::new (%arg);
479 $self->child->draw; 567 $self->child->draw;
480 }; 568 };
481} 569}
482 570
483sub size_allocate { 571sub size_allocate {
484 my ($self, $x, $y, $w, $h) = @_; 572 my ($self, $w, $h) = @_;
485 573
486 $self->_size_allocate ($x, $y, $w, $h) or return;
487
488 $self->child->size_allocate (0, 0, $w, $h); 574 $self->child->configure (0, 0, $w, $h);
489 575
490 $self->render_chld; 576 $self->render_chld;
491} 577}
492 578
493sub _draw { 579sub _draw {
509 glDisable GL_TEXTURE_2D; 595 glDisable GL_TEXTURE_2D;
510} 596}
511 597
512############################################################################# 598#############################################################################
513 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
514package CFClient::UI::Frame; 624package CFClient::UI::Frame;
515 625
516our @ISA = CFClient::UI::Bin::; 626our @ISA = CFClient::UI::Bin::;
517 627
518use SDL::OpenGL; 628use CFClient::OpenGL;
519
520sub size_request {
521 my ($self) = @_;
522 my $chld = $self->child
523 or return (0, 0);
524
525 $chld->move (2, 2);
526
527 map { $_ + 4 } $chld->size_request;
528}
529
530sub size_allocate {
531 my ($self, $x, $y, $w, $h) = @_;
532
533 $self->_size_allocate ($x, $y, $w, $h) or return;
534
535 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
536}
537
538sub _draw {
539 my ($self) = @_;
540
541 my $chld = $self->child;
542
543 my ($w, $h) = $chld->size_request;
544
545 glBegin GL_QUADS;
546 glColor 0, 0, 0;
547 glVertex 0 , 0;
548 glVertex 0 , $h + 4;
549 glVertex $w + 4 , $h + 4;
550 glVertex $w + 4 , 0;
551 glEnd;
552
553 $chld->draw;
554}
555
556#############################################################################
557
558package CFClient::UI::FancyFrame;
559
560our @ISA = CFClient::UI::Bin::;
561
562use SDL::OpenGL;
563
564my @tex =
565 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
566 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
567 629
568sub new { 630sub new {
569 my $class = shift; 631 my $class = shift;
570 632
571 # TODO: user_x, user_y, overwrite moveto?
572
573 $class->SUPER::new ( 633 my $self = $class->SUPER::new (
574 bg => [1, 1, 1, 1], 634 bg => [1, 1, 1, 1],
575 border_bg => [1, 1, 1, 1], 635 border_bg => [1, 1, 1, 1],
576 border => int $::FONTSIZE * 0.8, 636 border => 0.8,
577 @_ 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,
578 ) 734 )
579} 735}
580 736
581sub size_request {
582 my ($self) = @_;
583
584 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
585
586 my ($w, $h) = $self->SUPER::size_request;
587
588 (
589 $w + $self->{border} * 2,
590 $h + $self->{border} * 2,
591 )
592}
593
594sub size_allocate { 737sub size_allocate {
595 my ($self, $x, $y, $w, $h) = @_; 738 my ($self, $w, $h) = @_;
596 739
597 $self->_size_allocate ($x, $y, $w, $h) or return;
598
599 $h -= List::Util::max 0, $self->{border} * 2; 740 $h -= List::Util::max 0, $self->border * 2;
600 $w -= List::Util::max 0, $self->{border} * 2; 741 $w -= List::Util::max 0, $self->border * 2;
601 742
743 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
744 if $self->{title};
745
602 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 746 $self->child->configure ($self->border, $self->border, $w, $h);
603} 747}
604 748
605sub button_down { 749sub button_down {
606 my ($self, $ev, $x, $y) = @_; 750 my ($self, $ev, $x, $y) = @_;
607 751
752 my $border = $self->border;
753
608 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 754 if ($x < $self->{w} && $x >= $self->{w} - $border
609 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 755 && $y < $self->{h} && $y >= $self->{h} - $border) {
610 756
611 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 757 my ($ox, $oy) = ($ev->{x}, $ev->{y});
612 my ($bw, $bh) = ($self->{w}, $self->{h}); 758 my ($bw, $bh) = ($self->{w}, $self->{h});
613 759
614 $self->{motion} = sub { 760 $self->{motion} = sub {
615 my ($ev, $x, $y) = @_; 761 my ($ev, $x, $y) = @_;
616 762
617 ($x, $y) = ($ev->motion_x, $ev->motion_y); 763 ($x, $y) = ($ev->{x}, $ev->{y});
618 764
619 $self->{user_w} = $bw + $x - $ox; 765 $self->{user_w} = $bw + $x - $ox;
620 $self->{user_h} = $bh + $y - $oy; 766 $self->{user_h} = $bh + $y - $oy;
621 $self->update; 767 $self->check_size;
622 }; 768 };
623 769
624 } elsif ($x >= 0 && $x < $self->{w} 770 } elsif ($x >= 0 && $x < $self->{w}
625 && $y >= 0 && $y < $self->{border}) { 771 && $y >= 0 && $y < $border) {
626 772
627 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 773 my ($ox, $oy) = ($ev->{x}, $ev->{y});
628 my ($bx, $by) = ($self->{x}, $self->{y}); 774 my ($bx, $by) = ($self->{x}, $self->{y});
629 775
630 $self->{motion} = sub { 776 $self->{motion} = sub {
631 my ($ev, $x, $y) = @_; 777 my ($ev, $x, $y) = @_;
632 778
633 ($x, $y) = ($ev->motion_x, $ev->motion_y); 779 ($x, $y) = ($ev->{x}, $ev->{y});
634 780
635 $self->move ($bx + $x - $ox, $by + $y - $oy); 781 $self->move ($bx + $x - $ox, $by + $y - $oy);
636 $self->update; 782 $self->update;
637 }; 783 };
638 } 784 }
659 glEnable GL_BLEND; 805 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 807 glEnable GL_TEXTURE_2D;
662 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
663 809
810 my $border = $self->border;
811
664 glColor @{ $self->{border_bg} }; 812 glColor @{ $self->{border_bg} };
665 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 813 $tex[1]->draw_quad (0, 0, $w, $border);
666 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 814 $tex[3]->draw_quad (0, $border, $border, $ch);
667 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 815 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
668 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 816 $tex[4]->draw_quad (0, $h - $border, $w, $border);
669 817
670 my $bg = $tex[0]; 818 my $bg = $tex[0];
671 819
672 # TODO: repeat texture not scale 820 # TODO: repeat texture not scale
673 my $rep_x = $cw / $bg->{w}; 821 my $rep_x = $cw / $bg->{w};
676 glColor @{ $self->{bg} }; 824 glColor @{ $self->{bg} };
677 825
678 $bg->{s} = $rep_x; 826 $bg->{s} = $rep_x;
679 $bg->{t} = $rep_y; 827 $bg->{t} = $rep_y;
680 $bg->{wrap_mode} = 1; 828 $bg->{wrap_mode} = 1;
681 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 829 $bg->draw_quad ($border, $border, $cw, $ch);
682 830
683 glDisable GL_TEXTURE_2D; 831 glDisable GL_TEXTURE_2D;
684 glDisable GL_BLEND; 832 glDisable GL_BLEND;
685 833
834 $self->{title}->draw if $self->{title};
686 $self->child->draw; 835 $self->child->draw;
687} 836}
688 837
689############################################################################# 838#############################################################################
690 839
692 841
693our @ISA = CFClient::UI::Base::; 842our @ISA = CFClient::UI::Base::;
694 843
695use List::Util qw(max sum); 844use List::Util qw(max sum);
696 845
697use SDL::OpenGL; 846use CFClient::OpenGL;
698 847
699sub new { 848sub new {
700 my $class = shift; 849 my $class = shift;
701 850
702 $class->SUPER::new ( 851 $class->SUPER::new (
709 my ($self, $x, $y, $child) = @_; 858 my ($self, $x, $y, $child) = @_;
710 859
711 $child->set_parent ($self); 860 $child->set_parent ($self);
712 $self->{children}[$y][$x] = $child; 861 $self->{children}[$y][$x] = $child;
713 862
714 $self->{w} = $self->{h} = -1; 863 $child->check_size;
715 $self->update;
716} 864}
717 865
718# TODO: move to container class maybe? send childs a signal on removal? 866# TODO: move to container class maybe? send childs a signal on removal?
719sub clear { 867sub clear {
720 my ($self) = @_; 868 my ($self) = @_;
733 or next; 881 or next;
734 882
735 for my $x (0 .. $#$row) { 883 for my $x (0 .. $#$row) {
736 my $widget = $row->[$x] 884 my $widget = $row->[$x]
737 or next; 885 or next;
738 my ($w, $h) = $widget->size_request; 886 my ($w, $h) = @$widget{qw(req_w req_h)};
739 887
740 $w[$x] = max $w[$x], $w; 888 $w[$x] = max $w[$x], $w;
741 $h[$y] = max $h[$y], $h; 889 $h[$y] = max $h[$y], $h;
742 } 890 }
743 } 891 }
755 (sum @$hs), 903 (sum @$hs),
756 ) 904 )
757} 905}
758 906
759sub size_allocate { 907sub size_allocate {
760 my ($self, $x, $y, $w, $h) = @_; 908 my ($self, $w, $h) = @_;
761
762 $self->_size_allocate ($x, $y, $w, $h) or return;
763 909
764 my ($ws, $hs) = $self->get_wh; 910 my ($ws, $hs) = $self->get_wh;
765 911
766 my $req_w = sum @$ws; 912 my $req_w = sum @$ws;
767 my $req_h = sum @$hs; 913 my $req_h = sum @$hs;
789 935
790 for my $c (0 .. $#$row) { 936 for my $c (0 .. $#$row) {
791 my $col_w = $ws->[$c]; 937 my $col_w = $ws->[$c];
792 938
793 if (my $widget = $row->[$c]) { 939 if (my $widget = $row->[$c]) {
794 $widget->size_allocate ($x, $y, $col_w, $row_h); 940 $widget->configure ($x, $y, $col_w, $row_h);
795 } 941 }
796 942
797 $x += $col_w; 943 $x += $col_w;
798 } 944 }
799 945
844 (List::Util::max map $_->[1], @alloc), 990 (List::Util::max map $_->[1], @alloc),
845 ) 991 )
846} 992}
847 993
848sub size_allocate { 994sub size_allocate {
849 my ($self, $x, $y, $w, $h) = @_; 995 my ($self, $w, $h) = @_;
850
851 $self->_size_allocate ($x, $y, $w, $h);
852 996
853 ($h, $w) = ($w, $h); 997 ($h, $w) = ($w, $h);
854 998
855 my $children = $self->{children}; 999 my $children = $self->{children};
856 1000
857 my @h = map +($_->size_request)[0], @$children; 1001 my @h = map $_->{req_w}, @$children;
858 1002
859 my $req_h = List::Util::sum @h; 1003 my $req_h = List::Util::sum @h;
860 1004
861 if ($req_h > $h) { 1005 if ($req_h > $h) {
862 # ah well, not enough space 1006 # ah well, not enough space
878 1022
879 my $y = 0; 1023 my $y = 0;
880 for (0 .. $#$children) { 1024 for (0 .. $#$children) {
881 my $child = $children->[$_]; 1025 my $child = $children->[$_];
882 my $h = $h[$_]; 1026 my $h = $h[$_];
883 $child->size_allocate ($y, 0, $h, $w); 1027 $child->configure ($y, 0, $h, $w);
884 1028
885 $y += $h; 1029 $y += $h;
886 } 1030 }
1031
1032 1
887} 1033}
888 1034
889############################################################################# 1035#############################################################################
890 1036
891package CFClient::UI::VBox; 1037package CFClient::UI::VBox;
904 (List::Util::sum map $_->[1], @alloc), 1050 (List::Util::sum map $_->[1], @alloc),
905 ) 1051 )
906} 1052}
907 1053
908sub size_allocate { 1054sub size_allocate {
909 my ($self, $x, $y, $w, $h) = @_; 1055 my ($self, $w, $h) = @_;
910
911 $self->_size_allocate ($x, $y, $w, $h);
912 1056
913 my $children = $self->{children}; 1057 my $children = $self->{children};
914 1058
915 my @h = map +($_->size_request)[1], @$children; 1059 my @h = map $_->{req_h}, @$children;
916 1060
917 my $req_h = List::Util::sum @h; 1061 my $req_h = List::Util::sum @h;
918 1062
919 if ($req_h > $h) { 1063 if ($req_h > $h) {
920 # ah well, not enough space 1064 # ah well, not enough space
934 1078
935 my $y = 0; 1079 my $y = 0;
936 for (0 .. $#$children) { 1080 for (0 .. $#$children) {
937 my $child = $children->[$_]; 1081 my $child = $children->[$_];
938 my $h = $h[$_]; 1082 my $h = $h[$_];
939 $child->size_allocate (0, $y, $w, $h); 1083 $child->configure (0, $y, $w, $h);
940 1084
941 $y += $h; 1085 $y += $h;
942 } 1086 }
1087
1088 1
943} 1089}
944 1090
945############################################################################# 1091#############################################################################
946 1092
947package CFClient::UI::Label; 1093package CFClient::UI::Label;
948 1094
949our @ISA = CFClient::UI::Base::; 1095our @ISA = CFClient::UI::Base::;
950 1096
951use SDL::OpenGL; 1097use CFClient::OpenGL;
952 1098
953sub new { 1099sub new {
954 my ($class, %arg) = @_; 1100 my ($class, %arg) = @_;
955 1101
956 my $self = $class->SUPER::new ( 1102 my $self = $class->SUPER::new (
957 fg => [1, 1, 1], 1103 fg => [1, 1, 1],
958 fontsize => $::FONTSIZE, 1104 fontsize => 1,
959 text => "", 1105 text => "",
960 align => -1, 1106 align => -1,
961 valign => -1, 1107 valign => -1,
962 padding => 2, 1108 padding => 2,
963 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
964 %arg 1111 %arg
965 ); 1112 );
966 1113
967 $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};
968 1122
969 $self 1123 $self
970} 1124}
971 1125
972sub escape_text { 1126sub escape_text {
980} 1134}
981 1135
982sub set_text { 1136sub set_text {
983 my ($self, $text) = @_; 1137 my ($self, $text) = @_;
984 1138
985 $self->{text} = $text;
986 $self->{layout}->set_markup ($text); 1139 $self->{layout}->set_text ($text);
987 1140
988 delete $self->{texture}; 1141 delete $self->{texture};
989# $self->{w} = $self->{h} = -1; 1142 $self->check_size;
990 $self->update; 1143 $self->update;
991} 1144}
992 1145
993sub get_text { 1146sub set_markup {
994 my ($self, $text) = @_; 1147 my ($self, $markup) = @_;
995 1148
996 $self->{text} 1149 $self->{layout}->set_markup ($markup);
1150
1151 delete $self->{texture};
1152 $self->check_size;
1153 $self->update;
997} 1154}
998 1155
999sub size_request { 1156sub size_request {
1000 my ($self) = @_; 1157 my ($self) = @_;
1001 1158
1002 $self->{layout}->set_width; 1159 $self->{layout}->set_width;
1003 $self->{layout}->set_height ($self->{fontsize}); 1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1161
1004 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 }
1005 1172
1006 ( 1173 (
1007 $w + $self->{padding} * 2, 1174 $w + $self->{padding} * 2,
1008 $h + $self->{padding} * 2, 1175 $h + $self->{padding} * 2,
1009 ) 1176 )
1010} 1177}
1011 1178
1012sub size_allocate { 1179sub size_allocate {
1013 my ($self, $x, $y, $w, $h) = @_; 1180 my ($self, $w, $h) = @_;
1014
1015 $self->_size_allocate ($x, $y, $w, $h) or return;
1016 1181
1017 delete $self->{texture}; 1182 delete $self->{texture};
1018} 1183}
1019 1184
1020sub update { 1185sub set_fontsize {
1021 my ($self) = @_; 1186 my ($self, $fontsize) = @_;
1022 1187
1188 $self->{fontsize} = $fontsize;
1023 delete $self->{texture}; 1189 delete $self->{texture};
1190 $self->check_size;
1024 $self->SUPER::update; 1191 $self->update;
1025} 1192}
1026 1193
1027sub _draw { 1194sub _draw {
1028 my ($self) = @_; 1195 my ($self) = @_;
1029 1196
1030 my $tex = $self->{texture} ||= do { 1197 my $tex = $self->{texture} ||= do {
1031 $self->{layout}->set_width ($self->{w}); 1198 $self->{layout}->set_width ($self->{w});
1032 $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);
1033 new_from_layout CFClient::Texture $self->{layout} 1200 new_from_layout CFClient::Texture $self->{layout}
1034 }; 1201 };
1035 1202
1036 glEnable GL_BLEND; 1203 glEnable GL_BLEND;
1037 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1038 glEnable GL_TEXTURE_2D; 1205 glEnable GL_TEXTURE_2D;
1039 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1040 1207
1041 glColor @{$self->{fg}}; 1208 glColor @{$self->{fg}};
1042 1209
1043 my $x = 1210 $self->{ox} = int (
1044 $self->{align} < 0 ? $self->{padding} 1211 $self->{align} < 0 ? $self->{padding}
1045 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1212 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1046 : ($self->{w} - $tex->{w}) * 0.5; 1213 : ($self->{w} - $tex->{w}) * 0.5
1214 );
1047 1215
1048 my $y = 1216 $self->{oy} = int (
1049 $self->{valign} < 0 ? $self->{padding} 1217 $self->{valign} < 0 ? $self->{padding}
1050 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1218 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1051 : ($self->{h} - $tex->{h}) * 0.5; 1219 : ($self->{h} - $tex->{h}) * 0.5
1220 );
1052 1221
1053 $tex->draw_quad (int $x, int $y); 1222 $tex->draw_quad ($self->{ox}, $self->{oy});
1054 1223
1055 glDisable GL_TEXTURE_2D; 1224 glDisable GL_TEXTURE_2D;
1056 glDisable GL_BLEND; 1225 glDisable GL_BLEND;
1057} 1226}
1058 1227
1059############################################################################# 1228#############################################################################
1060 1229
1061package CFClient::UI::Entry; 1230package CFClient::UI::EntryBase;
1062 1231
1063our @ISA = CFClient::UI::Label::; 1232our @ISA = CFClient::UI::Label::;
1064 1233
1065use SDL; 1234use CFClient::OpenGL;
1066use SDL::OpenGL;
1067 1235
1068sub new { 1236sub new {
1069 my $class = shift; 1237 my $class = shift;
1070 1238
1071 $class->SUPER::new ( 1239 $class->SUPER::new (
1074 active_bg => [1, 1, 1, 0.5], 1242 active_bg => [1, 1, 1, 0.5],
1075 active_fg => [0, 0, 0], 1243 active_fg => [0, 0, 0],
1076 can_hover => 1, 1244 can_hover => 1,
1077 can_focus => 1, 1245 can_focus => 1,
1078 valign => 0, 1246 valign => 0,
1247 can_events => 1,
1079 @_ 1248 @_
1080 ) 1249 )
1081} 1250}
1082 1251
1083sub _set_text { 1252sub _set_text {
1084 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1085 1254
1086 my $old_text = $self->{text}; 1255 delete $self->{cur_h};
1256
1257 return if $self->{text} eq $text;
1258
1259 delete $self->{texture};
1087 1260
1088 $self->{last_activity} = $::NOW; 1261 $self->{last_activity} = $::NOW;
1089
1090 $self->{text} = $text; 1262 $self->{text} = $text;
1091 $self->{layout}->set_width ($self->{w});
1092 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1093 1263
1094 $text =~ s/./*/g if $self->{hidden}; 1264 $text =~ s/./*/g if $self->{hidden};
1265 $self->{layout}->set_text ("$text ");
1095 1266
1096 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1267 $self->emit (changed => $self->{text});
1268}
1097 1269
1098 $text = substr $text, 0, $self->{cursor}; 1270sub get_text {
1099 utf8::encode $text; 1271 $_[0]{text}
1100
1101 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1102
1103 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1104 if $old_text ne $self->{text};
1105} 1272}
1106 1273
1107sub size_request { 1274sub size_request {
1108 my ($self) = @_; 1275 my ($self) = @_;
1109 1276
1111 1278
1112 ($w + 1, $h) # add 1 for cursor 1279 ($w + 1, $h) # add 1 for cursor
1113} 1280}
1114 1281
1115sub size_allocate { 1282sub size_allocate {
1116 my ($self, $x, $y, $w, $h) = @_; 1283 my ($self, $w, $h) = @_;
1117 1284
1118 $self->SUPER::size_allocate ($x, $y, $w, $h); 1285 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1119
1120 $self->_set_text ($self->{text});
1121} 1286}
1122 1287
1123sub set_text { 1288sub set_text {
1124 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1125 1290
1129} 1294}
1130 1295
1131sub key_down { 1296sub key_down {
1132 my ($self, $ev) = @_; 1297 my ($self, $ev) = @_;
1133 1298
1134 my $mod = $ev->key_mod; 1299 my $mod = $ev->{mod};
1135 my $sym = $ev->key_sym; 1300 my $sym = $ev->{sym};
1136
1137 my $uni = $ev->key_unicode; 1301 my $uni = $ev->{unicode};
1138 1302
1139 my $text = $self->get_text; 1303 my $text = $self->get_text;
1140 1304
1141 if ($sym == SDLK_BACKSPACE) { 1305 if ($sym == 8) {
1142 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1306 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1143 } elsif ($sym == SDLK_DELETE) { 1307 } elsif ($sym == 127) {
1144 substr $text, $self->{cursor}, 1, ""; 1308 substr $text, $self->{cursor}, 1, "";
1145 } elsif ($sym == SDLK_LEFT) { 1309 } elsif ($sym == CFClient::SDLK_LEFT) {
1146 --$self->{cursor} if $self->{cursor}; 1310 --$self->{cursor} if $self->{cursor};
1147 } elsif ($sym == SDLK_RIGHT) { 1311 } elsif ($sym == CFClient::SDLK_RIGHT) {
1148 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1312 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1149 } elsif ($sym == SDLK_HOME) { 1313 } elsif ($sym == CFClient::SDLK_HOME) {
1150 $self->{cursor} = 0; 1314 $self->{cursor} = 0;
1151 } elsif ($sym == SDLK_END) { 1315 } elsif ($sym == CFClient::SDLK_END) {
1152 $self->{cursor} = length $text; 1316 $self->{cursor} = length $text;
1153 } elsif ($sym == SDLK_ESCAPE) { 1317 } elsif ($sym == 27) {
1154 $self->emit ('escape'); 1318 $self->emit ('escape');
1155 } elsif ($uni) { 1319 } elsif ($uni) {
1156 substr $text, $self->{cursor}++, 0, chr $uni; 1320 substr $text, $self->{cursor}++, 0, chr $uni;
1157 } 1321 }
1158 1322
1213 1377
1214 $self->SUPER::_draw; 1378 $self->SUPER::_draw;
1215 1379
1216 #TODO: force update every cursor change :( 1380 #TODO: force update every cursor change :(
1217 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
1218 glColor @{$self->{fg}}; 1390 glColor @{$self->{fg}};
1219 glBegin GL_LINES; 1391 glBegin GL_LINES;
1220 glVertex $self->{cur_x}, $self->{cur_y};
1221 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};
1222 glEnd; 1394 glEnd;
1223 } 1395 }
1224} 1396}
1225 1397
1226package CFClient::UI::LineEntry; 1398package CFClient::UI::Entry;
1227 1399
1228our @ISA = CFClient::UI::Entry::; 1400our @ISA = CFClient::UI::EntryBase::;
1229 1401
1230use SDL; 1402use CFClient::OpenGL;
1231use SDL::OpenGL;
1232 1403
1233sub key_down { 1404sub key_down {
1234 my ($self, $ev) = @_; 1405 my ($self, $ev) = @_;
1235 1406
1236 my $sym = $ev->key_sym; 1407 my $sym = $ev->{sym};
1237 1408
1238 if ($sym == SDLK_RETURN) { 1409 if ($sym == 13) {
1239 $self->emit (activate => $self->get_text); 1410 $self->emit (activate => $self->get_text);
1240 $self->update; 1411 $self->update;
1241 1412
1242 } else { 1413 } else {
1243 $self->SUPER::key_down ($ev); 1414 $self->SUPER::key_down ($ev);
1249 1420
1250package CFClient::UI::Button; 1421package CFClient::UI::Button;
1251 1422
1252our @ISA = CFClient::UI::Label::; 1423our @ISA = CFClient::UI::Label::;
1253 1424
1254use SDL; 1425use CFClient::OpenGL;
1255use SDL::OpenGL;
1256 1426
1257my @tex = 1427my @tex =
1258 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1259 qw(b1_button_active.png); 1429 qw(b1_button_active.png);
1260 1430
1261sub new { 1431sub new {
1262 my $class = shift; 1432 my $class = shift;
1263 1433
1264 $class->SUPER::new ( 1434 $class->SUPER::new (
1265 padding => 4, 1435 padding => 4,
1266 fg => [1, 1, 1], 1436 fg => [1, 1, 1],
1267 bg => [1, 1, 1, 0.2], 1437 bg => [1, 1, 1, 0.2],
1268 active_fg => [1, 1, 0], 1438 active_fg => [0, 0, 1],
1269 can_hover => 1, 1439 can_hover => 1,
1440 align => 0,
1441 valign => 0,
1442 can_events => 1,
1270 @_ 1443 @_
1271 ) 1444 )
1272} 1445}
1273 1446
1274sub button_up { 1447sub button_up {
1308package CFClient::UI::CheckBox; 1481package CFClient::UI::CheckBox;
1309 1482
1310our @ISA = CFClient::UI::DrawBG::; 1483our @ISA = CFClient::UI::DrawBG::;
1311 1484
1312my @tex = 1485my @tex =
1313 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1486 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1314 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1487 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1315 1488
1316use SDL; 1489use CFClient::OpenGL;
1317use SDL::OpenGL;
1318 1490
1319sub new { 1491sub new {
1320 my $class = shift; 1492 my $class = shift;
1321 1493
1322 $class->SUPER::new ( 1494 $class->SUPER::new (
1333 my ($self) = @_; 1505 my ($self) = @_;
1334 1506
1335 ($self->{padding} * 2 + 6) x 2 1507 ($self->{padding} * 2 + 6) x 2
1336} 1508}
1337 1509
1338sub size_allocate {
1339 my ($self, $x, $y, $w, $h) = @_;
1340
1341 $self->_size_allocate ($x, $y, $w, $h);
1342}
1343
1344sub button_down { 1510sub button_down {
1345 my ($self, $ev, $x, $y) = @_; 1511 my ($self, $ev, $x, $y) = @_;
1346 1512
1347 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1513 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1348 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1514 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1374 glDisable GL_BLEND; 1540 glDisable GL_BLEND;
1375} 1541}
1376 1542
1377############################################################################# 1543#############################################################################
1378 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
1379package CFClient::UI::Slider; 1766package CFClient::UI::Slider;
1380 1767
1381use strict; 1768use strict;
1382 1769
1383use SDL::OpenGL; 1770use CFClient::OpenGL;
1384 1771
1385our @ISA = CFClient::UI::DrawBG::; 1772our @ISA = CFClient::UI::DrawBG::;
1386 1773
1387my @tex = 1774my @tex =
1388 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1775 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1398 # TODO: calculations are off 1785 # TODO: calculations are off
1399 my $self = $class->SUPER::new ( 1786 my $self = $class->SUPER::new (
1400 fg => [1, 1, 1], 1787 fg => [1, 1, 1],
1401 active_fg => [0, 0, 0], 1788 active_fg => [0, 0, 0],
1402 range => [0, 0, 100, 10], 1789 range => [0, 0, 100, 10],
1403 req_w => 40, 1790 req_w => 20,
1404 req_h => 13, 1791 req_h => 20,
1405 vertical => 0, 1792 vertical => 0,
1406 can_hover => 1, 1793 can_hover => 1,
1407 inner_pad => 5, 1794 inner_pad => 5,
1408 @_ 1795 @_
1409 ); 1796 );
1513 1900
1514package CFClient::UI::TextView; 1901package CFClient::UI::TextView;
1515 1902
1516our @ISA = CFClient::UI::HBox::; 1903our @ISA = CFClient::UI::HBox::;
1517 1904
1518use SDL::OpenGL; 1905use CFClient::OpenGL;
1519 1906
1520sub new { 1907sub new {
1521 my $class = shift; 1908 my $class = shift;
1522 1909
1523 my $self = $class->SUPER::new ( 1910 my $self = $class->SUPER::new (
1524 req_w => $::WIDTH / 6,
1525 req_h => $::HEIGHT / 6,
1526 fontsize => $::FONTSIZE, 1911 fontsize => 1,
1527 @_, 1912 @_,
1528 1913
1529 layout => (new CFClient::Layout), 1914 layout => (new CFClient::Layout),
1530 par => [], 1915 par => [],
1531 height => 0, 1916 height => 0,
1552sub text_height { 1937sub text_height {
1553 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1554 1939
1555 my $layout = $self->{layout}; 1940 my $layout = $self->{layout};
1556 1941
1557 $layout->set_height ($self->{fontsize}); 1942 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1558 $layout->set_width ($self->{w}); 1943 $layout->set_width ($self->{w});
1559 $layout->set_text ($text); 1944 $layout->set_text ($text);
1560 1945
1561 ($layout->size)[1] 1946 ($layout->size)[1]
1562} 1947}
1566 1951
1567 $self->{need_reflow}++; 1952 $self->{need_reflow}++;
1568 $self->update; 1953 $self->update;
1569} 1954}
1570 1955
1571sub size_request {
1572 my ($self) = @_;
1573
1574 ($self->{req_w}, $self->{req_h})
1575}
1576
1577sub size_allocate { 1956sub size_allocate {
1578 my ($self, $x, $y, $w, $h) = @_; 1957 my ($self, $w, $h) = @_;
1579 1958
1580 $self->SUPER::size_allocate ($x, $y, $w, $h); 1959 $self->SUPER::size_allocate ($w, $h);
1581 1960
1582 $self->{layout}->set_height ($self->{fontsize}); 1961 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1583 $self->{layout}->set_width ($self->{w}); 1962 $self->{layout}->set_width ($self->{children}[0]{w});
1584 1963
1585 $self->reflow; 1964 $self->reflow;
1586} 1965}
1587 1966
1588sub add_paragraph { 1967sub add_paragraph {
1675 2054
1676} 2055}
1677 2056
1678############################################################################# 2057#############################################################################
1679 2058
1680package CFClient::UI::MapWidget;
1681
1682use strict;
1683
1684use List::Util qw(min max);
1685
1686use SDL;
1687use SDL::OpenGL;
1688
1689our @ISA = CFClient::UI::Base::;
1690
1691sub new {
1692 my $class = shift;
1693
1694 $class->SUPER::new (
1695 z => -1,
1696 can_focus => 1,
1697 list => (glGenLists 1),
1698 @_
1699 )
1700}
1701
1702sub key_down {
1703 print "MAPKEYDOWN\n";
1704}
1705
1706sub key_up {
1707}
1708
1709sub button_down {
1710 my ($self, $ev, $x, $y) = @_;
1711
1712 $self->focus_in;
1713
1714 if ($ev->button == 2) {
1715 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1716 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1717
1718 $self->{motion} = sub {
1719 my ($ev, $x, $y) = @_;
1720
1721 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1722
1723 $::CFG->{map_shift_x} = $bw + $x - $ox;
1724 $::CFG->{map_shift_y} = $bh + $y - $oy;
1725
1726 $self->update;
1727 };
1728 }
1729}
1730
1731sub button_up {
1732 my ($self, $ev, $x, $y) = @_;
1733
1734 delete $self->{motion};
1735}
1736
1737sub mouse_motion {
1738 my ($self, $ev, $x, $y) = @_;
1739
1740 $self->{motion}->($ev, $x, $y) if $self->{motion};
1741}
1742
1743sub size_request {
1744 (
1745 1 + 32 * int $::WIDTH / 32,
1746 1 + 32 * int $::HEIGHT / 32,
1747 )
1748}
1749
1750sub update {
1751 my ($self) = @_;
1752
1753 $self->{need_update} = 1;
1754 $self->SUPER::update;
1755}
1756
1757sub draw {
1758 my ($self) = @_;
1759
1760 if (delete $self->{need_update}) {
1761 glNewList $self->{list}, GL_COMPILE;
1762
1763 if ($::MAP) {
1764 my $sw = int $::WIDTH / 32;
1765 my $sh = int $::HEIGHT / 32;
1766
1767 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1768 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1769
1770 glTranslate $sx0 - 32, $sy0 - 32, 0;
1771
1772 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1773
1774 if ($::CFG->{fow_enable}) {
1775 if ($::CFG->{fow_smooth}) { # smooth fog of war
1776 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1777 glConvolutionFilter2D
1778 GL_CONVOLUTION_2D,
1779 GL_ALPHA,
1780 3, 3,
1781 GL_ALPHA, GL_FLOAT,
1782 pack "f*",
1783 0.1, 0.1, 0.1,
1784 0.1, 0.2, 0.1,
1785 0.1, 0.1, 0.1,
1786 ;
1787 glEnable GL_CONVOLUTION_2D;
1788 }
1789
1790 $self->{fow_texture} = new CFClient::Texture
1791 w => $w,
1792 h => $h,
1793 data => $data,
1794 internalformat => GL_ALPHA,
1795 format => GL_ALPHA;
1796
1797 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1798
1799 glEnable GL_BLEND;
1800 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1801 glEnable GL_TEXTURE_2D;
1802 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1803
1804 glColor +($::CFG->{fow_intensity}) x 3, 1;
1805 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1806
1807 glDisable GL_TEXTURE_2D;
1808 glDisable GL_BLEND;
1809 }
1810
1811 # HACK BEGIN
1812 {
1813 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1814 my ($w, $h) = (250, 250);
1815
1816 glEnable GL_BLEND;
1817 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1818 glEnable GL_TEXTURE_2D;
1819 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1820
1821 $self->{mapmap_texture} =
1822 new CFClient::Texture
1823 w => $w,
1824 h => $h,
1825 data => $::MAP->mapmap ($w, $h),
1826 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1827
1828 $self->{mapmap_texture}->draw_quad (100, 100);
1829
1830 glDisable GL_TEXTURE_2D;
1831 glDisable GL_BLEND;
1832 }
1833 # HACK END
1834 }
1835
1836 glEndList;
1837 }
1838
1839 glPushMatrix;
1840 glCallList $self->{list};
1841 glPopMatrix;
1842
1843 if ($FOCUS != $self) {
1844 glColor 64/255, 64/255, 64/255;
1845 glLogicOp GL_AND;
1846 glEnable GL_COLOR_LOGIC_OP;
1847 glBegin GL_QUADS;
1848 glVertex 0, 0;
1849 glVertex 0, $::HEIGHT;
1850 glVertex $::WIDTH, $::HEIGHT;
1851 glVertex $::WIDTH, 0;
1852 glEnd;
1853 glDisable GL_COLOR_LOGIC_OP;
1854 }
1855}
1856
1857my %DIR = (
1858 SDLK_KP8, [1, "north"],
1859 SDLK_KP9, [2, "northeast"],
1860 SDLK_KP6, [3, "east"],
1861 SDLK_KP3, [4, "southeast"],
1862 SDLK_KP2, [5, "south"],
1863 SDLK_KP1, [6, "southwest"],
1864 SDLK_KP4, [7, "west"],
1865 SDLK_KP7, [8, "northwest"],
1866
1867 SDLK_UP, [1, "north"],
1868 SDLK_RIGHT, [3, "east"],
1869 SDLK_DOWN, [5, "south"],
1870 SDLK_LEFT, [7, "west"],
1871);
1872
1873sub key_down {
1874 my ($self, $ev) = @_;
1875
1876 my $mod = $ev->key_mod;
1877 my $sym = $ev->key_sym;
1878
1879 if ($sym == SDLK_KP5) {
1880 $::CONN->user_send ("command stay fire");
1881 } elsif ($sym == SDLK_a) {
1882 $::CONN->user_send ("command apply");
1883 } elsif ($sym == SDLK_QUOTE) {
1884 $self->emit ('activate_console');
1885 } elsif ($sym == SDLK_SLASH) {
1886 $self->emit ('activate_console' => '/');
1887 } elsif (exists $DIR{$sym}) {
1888 if ($mod & KMOD_SHIFT) {
1889 $self->{shft}++;
1890 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1891 } elsif ($mod & KMOD_CTRL) {
1892 $self->{ctrl}++;
1893 $::CONN->user_send ("command run $DIR{$sym}[0]");
1894 } else {
1895 $::CONN->user_send ("command $DIR{$sym}[1]");
1896 }
1897 }
1898}
1899
1900sub key_up {
1901 my ($self, $ev) = @_;
1902
1903 my $mod = $ev->key_mod;
1904 my $sym = $ev->key_sym;
1905
1906 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1907 $::CONN->user_send ("command fire_stop");
1908 }
1909 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1910 $::CONN->user_send ("command run_stop");
1911 }
1912}
1913
1914#############################################################################
1915
1916package CFClient::UI::Animator; 2059package CFClient::UI::Animator;
1917 2060
1918use SDL::OpenGL; 2061use CFClient::OpenGL;
1919 2062
1920our @ISA = CFClient::UI::Bin::; 2063our @ISA = CFClient::UI::Bin::;
1921 2064
1922sub moveto { 2065sub moveto {
1923 my ($self, $x, $y) = @_; 2066 my ($self, $x, $y) = @_;
1963 my $class = shift; 2106 my $class = shift;
1964 2107
1965 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
1966 state => 0, 2109 state => 0,
1967 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
1968 @_ 2112 @_
1969 ); 2113 );
1970 2114
1971 if ($self->{state}) { 2115 if ($self->{state}) {
1972 $self->{state} = 0; 2116 $self->{state} = 0;
1992 $self->emit (changed => $self->{state}); 2136 $self->emit (changed => $self->{state});
1993} 2137}
1994 2138
1995############################################################################# 2139#############################################################################
1996 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
1997package CFClient::UI::Root; 2207package CFClient::UI::Root;
1998 2208
1999our @ISA = CFClient::UI::Container::; 2209our @ISA = CFClient::UI::Container::;
2000 2210
2001use SDL::OpenGL; 2211use CFClient::OpenGL;
2212
2213sub check_size {
2214 my ($self) = @_;
2215
2216 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2217}
2002 2218
2003sub size_request { 2219sub size_request {
2004 ($::WIDTH, $::HEIGHT) 2220 ($::WIDTH, $::HEIGHT)
2005} 2221}
2006 2222
2007sub size_allocate { 2223sub configure {
2008 my ($self, $x, $y, $w, $h) = @_; 2224 my ($self, $x, $y, $w, $h) = @_;
2009 2225
2010 $self->_size_allocate ($x, $y, $w, $h); 2226 $self->SUPER::configure ($x, $y, $w, $h);
2011 2227
2012 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2013 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 }
2014} 2235}
2015 2236
2016sub _topleft { 2237sub _topleft {
2017 my ($self, $x, $y) = @_; 2238 my ($self, $x, $y) = @_;
2018 2239
2020} 2241}
2021 2242
2022sub update { 2243sub update {
2023 my ($self) = @_; 2244 my ($self) = @_;
2024 2245
2025 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2246 $self->check_size;
2026 ::refresh (); 2247 ::refresh ();
2027} 2248}
2028 2249
2029sub add { 2250sub add {
2030 my ($self, $widget) = @_; 2251 my ($self, $child) = @_;
2031 2252
2253 # integerize window positions
2254 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y};
2256
2032 $self->SUPER::add ($widget); 2257 $self->SUPER::add ($child);
2033
2034 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2035} 2258}
2036 2259
2037sub on_refresh { 2260sub on_refresh {
2038 my ($self, $id, $cb) = @_; 2261 my ($self, $id, $cb) = @_;
2039 2262
2063############################################################################# 2286#############################################################################
2064 2287
2065package CFClient::UI; 2288package CFClient::UI;
2066 2289
2067$ROOT = new CFClient::UI::Root; 2290$ROOT = new CFClient::UI::Root;
2291$TOOLTIP = new CFClient::UI::Tooltip;
2068 2292
20691 22931
2070 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines