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.125 by root, Mon Apr 17 20:52:15 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) = @_;
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 ()
263} 330}
264 331
265sub check_size { 332sub check_size {
266 my ($self) = @_; 333 my ($self) = @_;
267 334
335 return unless $self->{parent};
336
268 my ($w, $h) = $self->size_request; 337 my ($w, $h) = $self->size_request;
269 338
270 if ($w != $self->{req_w} || $h != $self->{req_h}) { 339 if ($w != $self->{req_w} || $h != $self->{req_h}) {
271 $self->{req_w} = $w; 340 $self->{req_w} = $w;
272 $self->{req_h} = $h; 341 $self->{req_h} = $h;
273 342
274 $self->{parent}->check_size 343 $self->{parent}->check_size;
275 if $self->{parent};
276 } 344 }
277} 345}
278 346
279sub update { 347sub update {
280 my ($self) = @_; 348 my ($self) = @_;
284} 352}
285 353
286sub connect { 354sub connect {
287 my ($self, $signal, $cb) = @_; 355 my ($self, $signal, $cb) = @_;
288 356
289 push @{ $self->{cb}{$signal} }, $cb; 357 push @{ $self->{signal_cb}{$signal} }, $cb;
290} 358}
291 359
292sub emit { 360sub emit {
293 my ($self, $signal, @args) = @_; 361 my ($self, $signal, @args) = @_;
294 362
363 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
295 $_->($self, @args) 364 $cb->($self, @args);
296 for @{$self->{cb}{$signal} || []}; 365 }
297} 366}
298 367
299sub DESTROY { 368sub DESTROY {
300 my ($self) = @_; 369 my ($self) = @_;
301 370
307package CFClient::UI::DrawBG; 376package CFClient::UI::DrawBG;
308 377
309our @ISA = CFClient::UI::Base::; 378our @ISA = CFClient::UI::Base::;
310 379
311use strict; 380use strict;
312use SDL::OpenGL; 381use CFClient::OpenGL;
313 382
314sub new { 383sub new {
315 my $class = shift; 384 my $class = shift;
316 385
317 # range [value, low, high, page] 386 # range [value, low, high, page]
346 415
347package CFClient::UI::Empty; 416package CFClient::UI::Empty;
348 417
349our @ISA = CFClient::UI::Base::; 418our @ISA = CFClient::UI::Base::;
350 419
420sub new {
421 my ($class, %arg) = @_;
422 $class->SUPER::new (can_events => 0, %arg);
423}
424
351sub size_request { 425sub size_request {
352 (0, 0) 426 (0, 0)
353} 427}
354 428
355sub draw { } 429sub draw { }
363sub new { 437sub new {
364 my ($class, %arg) = @_; 438 my ($class, %arg) = @_;
365 439
366 my $children = delete $arg{children} || []; 440 my $children = delete $arg{children} || [];
367 441
368 my $self = $class->SUPER::new (children => [], %arg); 442 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
369 $self->add ($_) for @$children; 443 $self->add ($_) for @$children;
370 444
371 $self 445 $self
372} 446}
373 447
381 $self->{children} = [ 455 $self->{children} = [
382 sort { $a->{z} <=> $b->{z} } 456 sort { $a->{z} <=> $b->{z} }
383 @{$self->{children}}, $child 457 @{$self->{children}}, $child
384 ]; 458 ];
385 459
386 $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;
387 $self->update; 471 $self->update;
388}
389
390sub remove {
391 my ($self, $widget) = @_;
392
393 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
394
395 $self->check_size;
396} 472}
397 473
398sub find_widget { 474sub find_widget {
399 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
400 476
430 506
431 $class->SUPER::new (children => [$child], %arg) 507 $class->SUPER::new (children => [$child], %arg)
432} 508}
433 509
434sub add { 510sub add {
435 my ($self, $widget) = @_; 511 my ($self, $child) = @_;
436 512
437 $self->{children} = []; 513 $self->{children} = [];
438 514
439 $self->SUPER::add ($widget); 515 $self->SUPER::add ($child);
440} 516}
441 517
442sub remove { 518sub remove {
443 my ($self, $widget) = @_; 519 my ($self, $widget) = @_;
444 520
453sub size_request { 529sub size_request {
454 $_[0]{children}[0]->size_request 530 $_[0]{children}[0]->size_request
455} 531}
456 532
457sub size_allocate { 533sub size_allocate {
458 my ($self, $x, $y, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
459 535
460 $self->_size_allocate ($x, $y, $w, $h) or return;
461
462 $self->{children}[0]->size_allocate (0, 0, $w, $h); 536 $self->{children}[0]->configure (0, 0, $w, $h);
463} 537}
464 538
465############################################################################# 539#############################################################################
466 540
467package CFClient::UI::Window; 541package CFClient::UI::Window;
468 542
469our @ISA = CFClient::UI::Bin::; 543our @ISA = CFClient::UI::Bin::;
470 544
471use SDL::OpenGL; 545use CFClient::OpenGL;
472 546
473sub new { 547sub new {
474 my ($class, %arg) = @_; 548 my ($class, %arg) = @_;
475 549
476 my $self = $class->SUPER::new (%arg); 550 my $self = $class->SUPER::new (%arg);
493 $self->child->draw; 567 $self->child->draw;
494 }; 568 };
495} 569}
496 570
497sub size_allocate { 571sub size_allocate {
498 my ($self, $x, $y, $w, $h) = @_; 572 my ($self, $w, $h) = @_;
499 573
500 $self->_size_allocate ($x, $y, $w, $h) or return;
501
502 $self->child->size_allocate (0, 0, $w, $h); 574 $self->child->configure (0, 0, $w, $h);
503 575
504 $self->render_chld; 576 $self->render_chld;
505} 577}
506 578
507sub _draw { 579sub _draw {
532sub new { die } 604sub new { die }
533 605
534sub size_request { 606sub size_request {
535 my ($self) = @_; 607 my ($self) = @_;
536 608
537 @$self{qw(child_w child_h)} = $self->child->size_request; 609 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
538 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 610 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
539 611
540 @$self{qw(child_w child_h)} 612 @$self{qw(child_w child_h)}
541} 613}
542 614
543sub size_allocate {
544 my ($self, $x, $y, $w, $h) = @_;
545
546 $self->_size_allocate ($x, $y, $w, $h) or return;
547}
548
549sub _draw { 615sub _draw {
550 my ($self) = @_; 616 my ($self) = @_;
551 617
552 $self->{children}[1]->draw; 618 $self->{children}[1]->draw;
553} 619}
557 623
558package CFClient::UI::Frame; 624package CFClient::UI::Frame;
559 625
560our @ISA = CFClient::UI::Bin::; 626our @ISA = CFClient::UI::Bin::;
561 627
562use SDL::OpenGL; 628use CFClient::OpenGL;
563
564sub size_request {
565 my ($self) = @_;
566 my $chld = $self->child
567 or return (0, 0);
568
569 $chld->move (2, 2);
570
571 map { $_ + 4 } $chld->size_request;
572}
573
574sub size_allocate {
575 my ($self, $x, $y, $w, $h) = @_;
576
577 $self->_size_allocate ($x, $y, $w, $h) or return;
578
579 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
580}
581
582sub _draw {
583 my ($self) = @_;
584
585 my $chld = $self->child;
586
587 my ($w, $h) = $chld->size_request;
588
589 glBegin GL_QUADS;
590 glColor 0, 0, 0;
591 glVertex 0 , 0;
592 glVertex 0 , $h + 4;
593 glVertex $w + 4 , $h + 4;
594 glVertex $w + 4 , 0;
595 glEnd;
596
597 $chld->draw;
598}
599
600#############################################################################
601
602package CFClient::UI::FancyFrame;
603
604our @ISA = CFClient::UI::Bin::;
605
606use SDL::OpenGL;
607
608my @tex =
609 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
610 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
611 629
612sub new { 630sub new {
613 my $class = shift; 631 my $class = shift;
614 632
615 # TODO: user_x, user_y, overwrite moveto?
616
617 $class->SUPER::new ( 633 my $self = $class->SUPER::new (
618 bg => [1, 1, 1, 1], 634 bg => [1, 1, 1, 1],
619 border_bg => [1, 1, 1, 1], 635 border_bg => [1, 1, 1, 1],
620 border => int $::FONTSIZE * 0.8, 636 border => 0.8,
621 @_ 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,
622 ) 734 )
623} 735}
624 736
625sub size_request {
626 my ($self) = @_;
627
628 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
629
630 my ($w, $h) = $self->SUPER::size_request;
631
632 (
633 $w + $self->{border} * 2,
634 $h + $self->{border} * 2,
635 )
636}
637
638sub size_allocate { 737sub size_allocate {
639 my ($self, $x, $y, $w, $h) = @_; 738 my ($self, $w, $h) = @_;
640 739
641 $self->_size_allocate ($x, $y, $w, $h) or return;
642
643 $h -= List::Util::max 0, $self->{border} * 2; 740 $h -= List::Util::max 0, $self->border * 2;
644 $w -= List::Util::max 0, $self->{border} * 2; 741 $w -= List::Util::max 0, $self->border * 2;
645 742
743 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
744 if $self->{title};
745
646 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 746 $self->child->configure ($self->border, $self->border, $w, $h);
647} 747}
648 748
649sub button_down { 749sub button_down {
650 my ($self, $ev, $x, $y) = @_; 750 my ($self, $ev, $x, $y) = @_;
651 751
752 my $border = $self->border;
753
652 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 754 if ($x < $self->{w} && $x >= $self->{w} - $border
653 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 755 && $y < $self->{h} && $y >= $self->{h} - $border) {
654 756
655 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 757 my ($ox, $oy) = ($ev->{x}, $ev->{y});
656 my ($bw, $bh) = ($self->{w}, $self->{h}); 758 my ($bw, $bh) = ($self->{w}, $self->{h});
657 759
658 $self->{motion} = sub { 760 $self->{motion} = sub {
659 my ($ev, $x, $y) = @_; 761 my ($ev, $x, $y) = @_;
660 762
661 ($x, $y) = ($ev->motion_x, $ev->motion_y); 763 ($x, $y) = ($ev->{x}, $ev->{y});
662 764
663 $self->{user_w} = $bw + $x - $ox; 765 $self->{user_w} = $bw + $x - $ox;
664 $self->{user_h} = $bh + $y - $oy; 766 $self->{user_h} = $bh + $y - $oy;
665 $self->update; 767 $self->check_size;
666 }; 768 };
667 769
668 } elsif ($x >= 0 && $x < $self->{w} 770 } elsif ($x >= 0 && $x < $self->{w}
669 && $y >= 0 && $y < $self->{border}) { 771 && $y >= 0 && $y < $border) {
670 772
671 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 773 my ($ox, $oy) = ($ev->{x}, $ev->{y});
672 my ($bx, $by) = ($self->{x}, $self->{y}); 774 my ($bx, $by) = ($self->{x}, $self->{y});
673 775
674 $self->{motion} = sub { 776 $self->{motion} = sub {
675 my ($ev, $x, $y) = @_; 777 my ($ev, $x, $y) = @_;
676 778
677 ($x, $y) = ($ev->motion_x, $ev->motion_y); 779 ($x, $y) = ($ev->{x}, $ev->{y});
678 780
679 $self->move ($bx + $x - $ox, $by + $y - $oy); 781 $self->move ($bx + $x - $ox, $by + $y - $oy);
680 $self->update; 782 $self->update;
681 }; 783 };
682 } 784 }
703 glEnable GL_BLEND; 805 glEnable GL_BLEND;
704 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
705 glEnable GL_TEXTURE_2D; 807 glEnable GL_TEXTURE_2D;
706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
707 809
810 my $border = $self->border;
811
708 glColor @{ $self->{border_bg} }; 812 glColor @{ $self->{border_bg} };
709 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 813 $tex[1]->draw_quad (0, 0, $w, $border);
710 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 814 $tex[3]->draw_quad (0, $border, $border, $ch);
711 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 815 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
712 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 816 $tex[4]->draw_quad (0, $h - $border, $w, $border);
713 817
714 my $bg = $tex[0]; 818 my $bg = $tex[0];
715 819
716 # TODO: repeat texture not scale 820 # TODO: repeat texture not scale
717 my $rep_x = $cw / $bg->{w}; 821 my $rep_x = $cw / $bg->{w};
720 glColor @{ $self->{bg} }; 824 glColor @{ $self->{bg} };
721 825
722 $bg->{s} = $rep_x; 826 $bg->{s} = $rep_x;
723 $bg->{t} = $rep_y; 827 $bg->{t} = $rep_y;
724 $bg->{wrap_mode} = 1; 828 $bg->{wrap_mode} = 1;
725 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 829 $bg->draw_quad ($border, $border, $cw, $ch);
726 830
727 glDisable GL_TEXTURE_2D; 831 glDisable GL_TEXTURE_2D;
728 glDisable GL_BLEND; 832 glDisable GL_BLEND;
729 833
834 $self->{title}->draw if $self->{title};
730 $self->child->draw; 835 $self->child->draw;
731} 836}
732 837
733############################################################################# 838#############################################################################
734 839
736 841
737our @ISA = CFClient::UI::Base::; 842our @ISA = CFClient::UI::Base::;
738 843
739use List::Util qw(max sum); 844use List::Util qw(max sum);
740 845
741use SDL::OpenGL; 846use CFClient::OpenGL;
742 847
743sub new { 848sub new {
744 my $class = shift; 849 my $class = shift;
745 850
746 $class->SUPER::new ( 851 $class->SUPER::new (
753 my ($self, $x, $y, $child) = @_; 858 my ($self, $x, $y, $child) = @_;
754 859
755 $child->set_parent ($self); 860 $child->set_parent ($self);
756 $self->{children}[$y][$x] = $child; 861 $self->{children}[$y][$x] = $child;
757 862
758 $self->{w} = $self->{h} = -1; 863 $child->check_size;
759 $self->update;
760} 864}
761 865
762# 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?
763sub clear { 867sub clear {
764 my ($self) = @_; 868 my ($self) = @_;
777 or next; 881 or next;
778 882
779 for my $x (0 .. $#$row) { 883 for my $x (0 .. $#$row) {
780 my $widget = $row->[$x] 884 my $widget = $row->[$x]
781 or next; 885 or next;
782 my ($w, $h) = $widget->size_request; 886 my ($w, $h) = @$widget{qw(req_w req_h)};
783 887
784 $w[$x] = max $w[$x], $w; 888 $w[$x] = max $w[$x], $w;
785 $h[$y] = max $h[$y], $h; 889 $h[$y] = max $h[$y], $h;
786 } 890 }
787 } 891 }
799 (sum @$hs), 903 (sum @$hs),
800 ) 904 )
801} 905}
802 906
803sub size_allocate { 907sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_; 908 my ($self, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h) or return;
807 909
808 my ($ws, $hs) = $self->get_wh; 910 my ($ws, $hs) = $self->get_wh;
809 911
810 my $req_w = sum @$ws; 912 my $req_w = sum @$ws;
811 my $req_h = sum @$hs; 913 my $req_h = sum @$hs;
833 935
834 for my $c (0 .. $#$row) { 936 for my $c (0 .. $#$row) {
835 my $col_w = $ws->[$c]; 937 my $col_w = $ws->[$c];
836 938
837 if (my $widget = $row->[$c]) { 939 if (my $widget = $row->[$c]) {
838 $widget->size_allocate ($x, $y, $col_w, $row_h); 940 $widget->configure ($x, $y, $col_w, $row_h);
839 } 941 }
840 942
841 $x += $col_w; 943 $x += $col_w;
842 } 944 }
843 945
888 (List::Util::max map $_->[1], @alloc), 990 (List::Util::max map $_->[1], @alloc),
889 ) 991 )
890} 992}
891 993
892sub size_allocate { 994sub size_allocate {
893 my ($self, $x, $y, $w, $h) = @_; 995 my ($self, $w, $h) = @_;
894
895 $self->_size_allocate ($x, $y, $w, $h) or return;
896 996
897 ($h, $w) = ($w, $h); 997 ($h, $w) = ($w, $h);
898 998
899 my $children = $self->{children}; 999 my $children = $self->{children};
900 1000
901 my @h = map +($_->size_request)[0], @$children; 1001 my @h = map $_->{req_w}, @$children;
902 1002
903 my $req_h = List::Util::sum @h; 1003 my $req_h = List::Util::sum @h;
904 1004
905 if ($req_h > $h) { 1005 if ($req_h > $h) {
906 # ah well, not enough space 1006 # ah well, not enough space
922 1022
923 my $y = 0; 1023 my $y = 0;
924 for (0 .. $#$children) { 1024 for (0 .. $#$children) {
925 my $child = $children->[$_]; 1025 my $child = $children->[$_];
926 my $h = $h[$_]; 1026 my $h = $h[$_];
927 $child->size_allocate ($y, 0, $h, $w); 1027 $child->configure ($y, 0, $h, $w);
928 1028
929 $y += $h; 1029 $y += $h;
930 } 1030 }
931 1031
932 1 1032 1
950 (List::Util::sum map $_->[1], @alloc), 1050 (List::Util::sum map $_->[1], @alloc),
951 ) 1051 )
952} 1052}
953 1053
954sub size_allocate { 1054sub size_allocate {
955 my ($self, $x, $y, $w, $h) = @_; 1055 my ($self, $w, $h) = @_;
956
957 $self->_size_allocate ($x, $y, $w, $h) or return;
958 1056
959 my $children = $self->{children}; 1057 my $children = $self->{children};
960 1058
961 my @h = map +($_->size_request)[1], @$children; 1059 my @h = map $_->{req_h}, @$children;
962 1060
963 my $req_h = List::Util::sum @h; 1061 my $req_h = List::Util::sum @h;
964 1062
965 if ($req_h > $h) { 1063 if ($req_h > $h) {
966 # ah well, not enough space 1064 # ah well, not enough space
980 1078
981 my $y = 0; 1079 my $y = 0;
982 for (0 .. $#$children) { 1080 for (0 .. $#$children) {
983 my $child = $children->[$_]; 1081 my $child = $children->[$_];
984 my $h = $h[$_]; 1082 my $h = $h[$_];
985 $child->size_allocate (0, $y, $w, $h); 1083 $child->configure (0, $y, $w, $h);
986 1084
987 $y += $h; 1085 $y += $h;
988 } 1086 }
989 1087
990 1 1088 1
994 1092
995package CFClient::UI::Label; 1093package CFClient::UI::Label;
996 1094
997our @ISA = CFClient::UI::Base::; 1095our @ISA = CFClient::UI::Base::;
998 1096
999use SDL::OpenGL; 1097use CFClient::OpenGL;
1000 1098
1001sub new { 1099sub new {
1002 my ($class, %arg) = @_; 1100 my ($class, %arg) = @_;
1003 1101
1004 my $self = $class->SUPER::new ( 1102 my $self = $class->SUPER::new (
1005 fg => [1, 1, 1], 1103 fg => [1, 1, 1],
1006 fontsize => $::FONTSIZE, 1104 fontsize => 1,
1007 text => "", 1105 text => "",
1008 align => -1, 1106 align => -1,
1009 valign => -1, 1107 valign => -1,
1010 padding => 2, 1108 padding => 2,
1011 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
1012 %arg 1111 %arg
1013 ); 1112 );
1014 1113
1015 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1114 if (exists $self->{template}) {
1115 my $layout = new CFClient::Layout;
1116 $layout->set_text (delete $self->{template});
1117 $self->{template} = $layout;
1118 }
1016 1119
1017 $self->set_text (delete $self->{text}) if exists $self->{text}; 1120 $self->set_text (delete $self->{text}) if exists $self->{text};
1018 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1121 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1019 1122
1020 $self 1123 $self
1034 my ($self, $text) = @_; 1137 my ($self, $text) = @_;
1035 1138
1036 $self->{layout}->set_text ($text); 1139 $self->{layout}->set_text ($text);
1037 1140
1038 delete $self->{texture}; 1141 delete $self->{texture};
1142 $self->check_size;
1039 $self->update; 1143 $self->update;
1040} 1144}
1041 1145
1042sub set_markup { 1146sub set_markup {
1043 my ($self, $markup) = @_; 1147 my ($self, $markup) = @_;
1044 1148
1045 $self->{layout}->set_markup ($markup); 1149 $self->{layout}->set_markup ($markup);
1046 1150
1047 delete $self->{texture}; 1151 delete $self->{texture};
1152 $self->check_size;
1048 $self->update; 1153 $self->update;
1049} 1154}
1050 1155
1051sub size_request { 1156sub size_request {
1052 my ($self) = @_; 1157 my ($self) = @_;
1053 1158
1054 $self->{layout}->set_width; 1159 $self->{layout}->set_width;
1055 $self->{layout}->set_height ($self->{fontsize}); 1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1056 1161
1057 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 }
1058 1172
1059 ( 1173 (
1060 $w + $self->{padding} * 2, 1174 $w + $self->{padding} * 2,
1061 $h + $self->{padding} * 2, 1175 $h + $self->{padding} * 2,
1062 ) 1176 )
1063} 1177}
1064 1178
1065sub size_allocate { 1179sub size_allocate {
1066 my ($self, $x, $y, $w, $h) = @_; 1180 my ($self, $w, $h) = @_;
1067
1068 $self->_size_allocate ($x, $y, $w, $h) or return;
1069 1181
1070 delete $self->{texture}; 1182 delete $self->{texture};
1071} 1183}
1072 1184
1073sub update { 1185sub set_fontsize {
1074 my ($self) = @_; 1186 my ($self, $fontsize) = @_;
1075 1187
1188 $self->{fontsize} = $fontsize;
1076 delete $self->{texture}; 1189 delete $self->{texture};
1190 $self->check_size;
1077 $self->SUPER::update; 1191 $self->update;
1078} 1192}
1079 1193
1080sub _draw { 1194sub _draw {
1081 my ($self) = @_; 1195 my ($self) = @_;
1082 1196
1083 my $tex = $self->{texture} ||= do { 1197 my $tex = $self->{texture} ||= do {
1084 $self->{layout}->set_width ($self->{w}); 1198 $self->{layout}->set_width ($self->{w});
1085 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1199 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1086 new_from_layout CFClient::Texture $self->{layout} 1200 new_from_layout CFClient::Texture $self->{layout}
1087 }; 1201 };
1088 1202
1089 glEnable GL_BLEND; 1203 glEnable GL_BLEND;
1090 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1115 1229
1116package CFClient::UI::EntryBase; 1230package CFClient::UI::EntryBase;
1117 1231
1118our @ISA = CFClient::UI::Label::; 1232our @ISA = CFClient::UI::Label::;
1119 1233
1120use SDL; 1234use CFClient::OpenGL;
1121use SDL::OpenGL;
1122 1235
1123sub new { 1236sub new {
1124 my $class = shift; 1237 my $class = shift;
1125 1238
1126 $class->SUPER::new ( 1239 $class->SUPER::new (
1129 active_bg => [1, 1, 1, 0.5], 1242 active_bg => [1, 1, 1, 0.5],
1130 active_fg => [0, 0, 0], 1243 active_fg => [0, 0, 0],
1131 can_hover => 1, 1244 can_hover => 1,
1132 can_focus => 1, 1245 can_focus => 1,
1133 valign => 0, 1246 valign => 0,
1247 can_events => 1,
1134 @_ 1248 @_
1135 ) 1249 )
1136} 1250}
1137 1251
1138sub _set_text { 1252sub _set_text {
1140 1254
1141 delete $self->{cur_h}; 1255 delete $self->{cur_h};
1142 1256
1143 return if $self->{text} eq $text; 1257 return if $self->{text} eq $text;
1144 1258
1259 delete $self->{texture};
1260
1145 $self->{last_activity} = $::NOW; 1261 $self->{last_activity} = $::NOW;
1146 $self->{text} = $text; 1262 $self->{text} = $text;
1147 1263
1148 $text =~ s/./*/g if $self->{hidden}; 1264 $text =~ s/./*/g if $self->{hidden};
1149 $self->{layout}->set_text ("$text "); 1265 $self->{layout}->set_text ("$text ");
1162 1278
1163 ($w + 1, $h) # add 1 for cursor 1279 ($w + 1, $h) # add 1 for cursor
1164} 1280}
1165 1281
1166sub size_allocate { 1282sub size_allocate {
1167 my ($self, $x, $y, $w, $h) = @_; 1283 my ($self, $w, $h) = @_;
1168 1284
1169 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1285 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1170
1171 $self->_set_text ($self->{text});
1172} 1286}
1173 1287
1174sub set_text { 1288sub set_text {
1175 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1176 1290
1180} 1294}
1181 1295
1182sub key_down { 1296sub key_down {
1183 my ($self, $ev) = @_; 1297 my ($self, $ev) = @_;
1184 1298
1185 my $mod = $ev->key_mod; 1299 my $mod = $ev->{mod};
1186 my $sym = $ev->key_sym; 1300 my $sym = $ev->{sym};
1187
1188 my $uni = $ev->key_unicode; 1301 my $uni = $ev->{unicode};
1189 1302
1190 my $text = $self->get_text; 1303 my $text = $self->get_text;
1191 1304
1192 if ($sym == SDLK_BACKSPACE) { 1305 if ($sym == 8) {
1193 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1306 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1194 } elsif ($sym == SDLK_DELETE) { 1307 } elsif ($sym == 127) {
1195 substr $text, $self->{cursor}, 1, ""; 1308 substr $text, $self->{cursor}, 1, "";
1196 } elsif ($sym == SDLK_LEFT) { 1309 } elsif ($sym == CFClient::SDLK_LEFT) {
1197 --$self->{cursor} if $self->{cursor}; 1310 --$self->{cursor} if $self->{cursor};
1198 } elsif ($sym == SDLK_RIGHT) { 1311 } elsif ($sym == CFClient::SDLK_RIGHT) {
1199 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1312 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1200 } elsif ($sym == SDLK_HOME) { 1313 } elsif ($sym == CFClient::SDLK_HOME) {
1201 $self->{cursor} = 0; 1314 $self->{cursor} = 0;
1202 } elsif ($sym == SDLK_END) { 1315 } elsif ($sym == CFClient::SDLK_END) {
1203 $self->{cursor} = length $text; 1316 $self->{cursor} = length $text;
1204 } elsif ($sym == SDLK_ESCAPE) { 1317 } elsif ($sym == 27) {
1205 $self->emit ('escape'); 1318 $self->emit ('escape');
1206 } elsif ($uni) { 1319 } elsif ($uni) {
1207 substr $text, $self->{cursor}++, 0, chr $uni; 1320 substr $text, $self->{cursor}++, 0, chr $uni;
1208 } 1321 }
1209 1322
1284 1397
1285package CFClient::UI::Entry; 1398package CFClient::UI::Entry;
1286 1399
1287our @ISA = CFClient::UI::EntryBase::; 1400our @ISA = CFClient::UI::EntryBase::;
1288 1401
1289use SDL; 1402use CFClient::OpenGL;
1290use SDL::OpenGL;
1291 1403
1292sub key_down { 1404sub key_down {
1293 my ($self, $ev) = @_; 1405 my ($self, $ev) = @_;
1294 1406
1295 my $sym = $ev->key_sym; 1407 my $sym = $ev->{sym};
1296 1408
1297 if ($sym == SDLK_RETURN) { 1409 if ($sym == 13) {
1298 $self->emit (activate => $self->get_text); 1410 $self->emit (activate => $self->get_text);
1299 $self->update; 1411 $self->update;
1300 1412
1301 } else { 1413 } else {
1302 $self->SUPER::key_down ($ev); 1414 $self->SUPER::key_down ($ev);
1308 1420
1309package CFClient::UI::Button; 1421package CFClient::UI::Button;
1310 1422
1311our @ISA = CFClient::UI::Label::; 1423our @ISA = CFClient::UI::Label::;
1312 1424
1313use SDL; 1425use CFClient::OpenGL;
1314use SDL::OpenGL;
1315 1426
1316my @tex = 1427my @tex =
1317 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1318 qw(b1_button_active.png); 1429 qw(b1_button_active.png);
1319 1430
1320sub new { 1431sub new {
1321 my $class = shift; 1432 my $class = shift;
1322 1433
1324 padding => 4, 1435 padding => 4,
1325 fg => [1, 1, 1], 1436 fg => [1, 1, 1],
1326 bg => [1, 1, 1, 0.2], 1437 bg => [1, 1, 1, 0.2],
1327 active_fg => [0, 0, 1], 1438 active_fg => [0, 0, 1],
1328 can_hover => 1, 1439 can_hover => 1,
1440 align => 0,
1441 valign => 0,
1442 can_events => 1,
1329 @_ 1443 @_
1330 ) 1444 )
1331} 1445}
1332 1446
1333sub button_up { 1447sub button_up {
1367package CFClient::UI::CheckBox; 1481package CFClient::UI::CheckBox;
1368 1482
1369our @ISA = CFClient::UI::DrawBG::; 1483our @ISA = CFClient::UI::DrawBG::;
1370 1484
1371my @tex = 1485my @tex =
1372 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1486 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1373 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1487 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1374 1488
1375use SDL; 1489use CFClient::OpenGL;
1376use SDL::OpenGL;
1377 1490
1378sub new { 1491sub new {
1379 my $class = shift; 1492 my $class = shift;
1380 1493
1381 $class->SUPER::new ( 1494 $class->SUPER::new (
1392 my ($self) = @_; 1505 my ($self) = @_;
1393 1506
1394 ($self->{padding} * 2 + 6) x 2 1507 ($self->{padding} * 2 + 6) x 2
1395} 1508}
1396 1509
1397sub size_allocate {
1398 my ($self, $x, $y, $w, $h) = @_;
1399
1400 $self->_size_allocate ($x, $y, $w, $h) or return;
1401}
1402
1403sub button_down { 1510sub button_down {
1404 my ($self, $ev, $x, $y) = @_; 1511 my ($self, $ev, $x, $y) = @_;
1405 1512
1406 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1513 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1407 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1514 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1433 glDisable GL_BLEND; 1540 glDisable GL_BLEND;
1434} 1541}
1435 1542
1436############################################################################# 1543#############################################################################
1437 1544
1438package CFClient::UI::VGauge; 1545package CFClient::UI::Image;
1439 1546
1440our @ISA = CFClient::UI::Base::; 1547our @ISA = CFClient::UI::Base::;
1441 1548
1442use SDL::OpenGL; 1549use CFClient::OpenGL;
1550use Carp qw/confess/;
1443 1551
1444my %tex = ( 1552our %loaded_images;
1445 food => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1448 ],
1449 grace => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1452 ],
1453 hp => [
1454 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1455 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1456 ],
1457 mana => [
1458 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1459 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1460 ],
1461);
1462 1553
1463# eg. VGauge->new (gauge => 'food'), default gauge: food
1464sub new { 1554sub new {
1465 my $class = shift; 1555 my $class = shift;
1466 1556
1467 my $self = $class->SUPER::new (gauge => 'food', @_); 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};
1468 1569
1469 $self 1570 $self
1470} 1571}
1471 1572
1472sub size_request { 1573sub size_request {
1473 my ($self) = @_; 1574 my ($self) = @_;
1474 1575
1475 ($self->{w}, $self->{h}) 1576 ($self->{tex}->{w}, $self->{tex}->{h})
1476}
1477
1478sub set_max {
1479 my ($self, $max) = @_;
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493} 1577}
1494 1578
1495sub _draw { 1579sub _draw {
1496 my ($self) = @_; 1580 my ($self) = @_;
1497 1581
1498 my $tex = $tex{$self->{gauge}}; 1582 my $tex = $self->{tex};
1499 1583
1500 my ($w, $h) = ($self->{w}, $self->{h}); 1584 my ($w, $h) = ($self->{w}, $self->{h});
1501 1585
1502 my $ycut = $self->{val} / ($self->{max_val} || 1); 1586 if ($self->{rot90}) {
1503 $ycut = 1 if $self->{val} > $self->{max_val}; 1587 glRotate 90, 0, 0, 1;
1588 glTranslate 0, -$self->{w}, 0;
1504 1589
1505 my $t1 = $tex->[0]; 1590 ($w, $h) = ($h, $w);
1506 my $t2 = $tex->[1]; 1591 }
1507 1592
1508 glEnable GL_BLEND; 1593 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1594 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D; 1595 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 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
1513 my $h1 = $self->{h} - $ycut * $self->{h}; 1696 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h}; 1697 my $h2 = $ycut * $self->{h};
1515 1698
1516 my $yp = 0;
1517
1518 glBindTexture (GL_TEXTURE_2D, $t1->{name}); 1699 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin (GL_QUADS); 1700 glBegin GL_QUADS;
1520 glTexCoord (0, 0); glVertex (0 , $yp); 1701 glTexCoord 0 , 0; glVertex 0 , 0;
1521 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); 1702 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1522 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); 1703 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1523 glTexCoord (1, 0); glVertex (0 + $w, $yp); 1704 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1524 glEnd (); 1705 glEnd;
1525 1706
1526 $yp += $h1;
1527
1528 glBindTexture (GL_TEXTURE_2D, $t2->{name}); 1707 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin (GL_QUADS); 1708 glBegin GL_QUADS;
1530 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); 1709 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1531 glTexCoord (0, 1); glVertex (0 , $yp + $h2); 1710 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1532 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); 1711 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1533 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); 1712 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1534 glEnd (); 1713 glEnd;
1535 1714
1536 glDisable GL_BLEND; 1715 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D; 1716 glDisable GL_TEXTURE_2D;
1538} 1717}
1539 1718
1540############################################################################# 1719#############################################################################
1541 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
1542package CFClient::UI::Slider; 1766package CFClient::UI::Slider;
1543 1767
1544use strict; 1768use strict;
1545 1769
1546use SDL::OpenGL; 1770use CFClient::OpenGL;
1547 1771
1548our @ISA = CFClient::UI::DrawBG::; 1772our @ISA = CFClient::UI::DrawBG::;
1549 1773
1550my @tex = 1774my @tex =
1551 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1775 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1561 # TODO: calculations are off 1785 # TODO: calculations are off
1562 my $self = $class->SUPER::new ( 1786 my $self = $class->SUPER::new (
1563 fg => [1, 1, 1], 1787 fg => [1, 1, 1],
1564 active_fg => [0, 0, 0], 1788 active_fg => [0, 0, 0],
1565 range => [0, 0, 100, 10], 1789 range => [0, 0, 100, 10],
1566 req_w => 40, 1790 req_w => 20,
1567 req_h => 13, 1791 req_h => 20,
1568 vertical => 0, 1792 vertical => 0,
1569 can_hover => 1, 1793 can_hover => 1,
1570 inner_pad => 5, 1794 inner_pad => 5,
1571 @_ 1795 @_
1572 ); 1796 );
1676 1900
1677package CFClient::UI::TextView; 1901package CFClient::UI::TextView;
1678 1902
1679our @ISA = CFClient::UI::HBox::; 1903our @ISA = CFClient::UI::HBox::;
1680 1904
1681use SDL::OpenGL; 1905use CFClient::OpenGL;
1682 1906
1683sub new { 1907sub new {
1684 my $class = shift; 1908 my $class = shift;
1685 1909
1686 my $self = $class->SUPER::new ( 1910 my $self = $class->SUPER::new (
1687 req_w => $::WIDTH / 6,
1688 req_h => $::HEIGHT / 6,
1689 fontsize => $::FONTSIZE, 1911 fontsize => 1,
1690 @_, 1912 @_,
1691 1913
1692 layout => (new CFClient::Layout), 1914 layout => (new CFClient::Layout),
1693 par => [], 1915 par => [],
1694 height => 0, 1916 height => 0,
1715sub text_height { 1937sub text_height {
1716 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1717 1939
1718 my $layout = $self->{layout}; 1940 my $layout = $self->{layout};
1719 1941
1720 $layout->set_height ($self->{fontsize}); 1942 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1721 $layout->set_width ($self->{w}); 1943 $layout->set_width ($self->{w});
1722 $layout->set_text ($text); 1944 $layout->set_text ($text);
1723 1945
1724 ($layout->size)[1] 1946 ($layout->size)[1]
1725} 1947}
1729 1951
1730 $self->{need_reflow}++; 1952 $self->{need_reflow}++;
1731 $self->update; 1953 $self->update;
1732} 1954}
1733 1955
1734sub size_request {
1735 my ($self) = @_;
1736
1737 ($self->{req_w}, $self->{req_h})
1738}
1739
1740sub size_allocate { 1956sub size_allocate {
1741 my ($self, $x, $y, $w, $h) = @_; 1957 my ($self, $w, $h) = @_;
1742 1958
1743 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1959 $self->SUPER::size_allocate ($w, $h);
1744 1960
1745 $self->{layout}->set_height ($self->{fontsize}); 1961 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1746 $self->{layout}->set_width ($self->{w}); 1962 $self->{layout}->set_width ($self->{children}[0]{w});
1747 1963
1748 $self->reflow; 1964 $self->reflow;
1749 $self->update;
1750} 1965}
1751 1966
1752sub add_paragraph { 1967sub add_paragraph {
1753 my ($self, $color, $text) = @_; 1968 my ($self, $color, $text) = @_;
1754 1969
1839 2054
1840} 2055}
1841 2056
1842############################################################################# 2057#############################################################################
1843 2058
1844package CFClient::UI::MapWidget;
1845
1846use strict;
1847
1848use List::Util qw(min max);
1849
1850use SDL;
1851use SDL::OpenGL;
1852
1853our @ISA = CFClient::UI::Base::;
1854
1855sub new {
1856 my $class = shift;
1857
1858 $class->SUPER::new (
1859 z => -1,
1860 can_focus => 1,
1861 list => (glGenLists 1),
1862 @_
1863 )
1864}
1865
1866sub key_down {
1867 print "MAPKEYDOWN\n";
1868}
1869
1870sub key_up {
1871}
1872
1873sub button_down {
1874 my ($self, $ev, $x, $y) = @_;
1875
1876 $self->focus_in;
1877
1878 if ($ev->button == 2) {
1879 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1880 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1881
1882 $self->{motion} = sub {
1883 my ($ev, $x, $y) = @_;
1884
1885 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1886
1887 $::CFG->{map_shift_x} = $bw + $x - $ox;
1888 $::CFG->{map_shift_y} = $bh + $y - $oy;
1889
1890 $self->update;
1891 };
1892 }
1893}
1894
1895sub button_up {
1896 my ($self, $ev, $x, $y) = @_;
1897
1898 delete $self->{motion};
1899}
1900
1901sub mouse_motion {
1902 my ($self, $ev, $x, $y) = @_;
1903
1904 $self->{motion}->($ev, $x, $y) if $self->{motion};
1905}
1906
1907sub size_request {
1908 (
1909 1 + 32 * int $::WIDTH / 32,
1910 1 + 32 * int $::HEIGHT / 32,
1911 )
1912}
1913
1914sub update {
1915 my ($self) = @_;
1916
1917 $self->{need_update} = 1;
1918 $self->SUPER::update;
1919}
1920
1921sub draw {
1922 my ($self) = @_;
1923
1924 if (delete $self->{need_update}) {
1925 glNewList $self->{list}, GL_COMPILE;
1926
1927 if ($::MAP) {
1928 my $sw = int $::WIDTH / 32;
1929 my $sh = int $::HEIGHT / 32;
1930
1931 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1932 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1933
1934 glTranslate $sx0 - 32, $sy0 - 32, 0;
1935
1936 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1937
1938 if ($::CFG->{fow_enable}) {
1939 if ($::CFG->{fow_smooth}) { # smooth fog of war
1940 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1941 glConvolutionFilter2D
1942 GL_CONVOLUTION_2D,
1943 GL_ALPHA,
1944 3, 3,
1945 GL_ALPHA, GL_FLOAT,
1946 pack "f*",
1947 0.1, 0.1, 0.1,
1948 0.1, 0.2, 0.1,
1949 0.1, 0.1, 0.1,
1950 ;
1951 glEnable GL_CONVOLUTION_2D;
1952 }
1953
1954 $self->{fow_texture} = new CFClient::Texture
1955 w => $w,
1956 h => $h,
1957 data => $data,
1958 internalformat => GL_ALPHA,
1959 format => GL_ALPHA;
1960
1961 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1962
1963 glEnable GL_BLEND;
1964 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1965 glEnable GL_TEXTURE_2D;
1966 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1967
1968 glColor +($::CFG->{fow_intensity}) x 3, 1;
1969 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1970
1971 glDisable GL_TEXTURE_2D;
1972 glDisable GL_BLEND;
1973 }
1974
1975 # HACK BEGIN
1976 {
1977 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1978 my ($w, $h) = (250, 250);
1979
1980 glEnable GL_BLEND;
1981 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1982 glEnable GL_TEXTURE_2D;
1983 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1984
1985 $self->{mapmap_texture} =
1986 new CFClient::Texture
1987 w => $w,
1988 h => $h,
1989 data => $::MAP->mapmap ($w, $h),
1990 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1991
1992 $self->{mapmap_texture}->draw_quad (100, 100);
1993
1994 glDisable GL_TEXTURE_2D;
1995 glDisable GL_BLEND;
1996 }
1997 # HACK END
1998 }
1999
2000 glEndList;
2001 }
2002
2003 glPushMatrix;
2004 glCallList $self->{list};
2005 glPopMatrix;
2006
2007 if ($FOCUS != $self) {
2008 glColor 64/255, 64/255, 64/255;
2009 glLogicOp GL_AND;
2010 glEnable GL_COLOR_LOGIC_OP;
2011 glBegin GL_QUADS;
2012 glVertex 0, 0;
2013 glVertex 0, $::HEIGHT;
2014 glVertex $::WIDTH, $::HEIGHT;
2015 glVertex $::WIDTH, 0;
2016 glEnd;
2017 glDisable GL_COLOR_LOGIC_OP;
2018 }
2019}
2020
2021my %DIR = (
2022 SDLK_KP8, [1, "north"],
2023 SDLK_KP9, [2, "northeast"],
2024 SDLK_KP6, [3, "east"],
2025 SDLK_KP3, [4, "southeast"],
2026 SDLK_KP2, [5, "south"],
2027 SDLK_KP1, [6, "southwest"],
2028 SDLK_KP4, [7, "west"],
2029 SDLK_KP7, [8, "northwest"],
2030
2031 SDLK_UP, [1, "north"],
2032 SDLK_RIGHT, [3, "east"],
2033 SDLK_DOWN, [5, "south"],
2034 SDLK_LEFT, [7, "west"],
2035);
2036
2037sub key_down {
2038 my ($self, $ev) = @_;
2039
2040 my $mod = $ev->key_mod;
2041 my $sym = $ev->key_sym;
2042
2043 if ($sym == SDLK_KP5) {
2044 $::CONN->user_send ("stay fire");
2045 } elsif ($sym == SDLK_a) {
2046 $::CONN->user_send ("apply");
2047 } elsif ($sym == SDLK_QUOTE) {
2048 $self->emit ('activate_console');
2049 } elsif ($sym == SDLK_SLASH) {
2050 $self->emit ('activate_console' => '/');
2051 } elsif (exists $DIR{$sym}) {
2052 if ($mod & KMOD_SHIFT) {
2053 $self->{shft}++;
2054 $::CONN->user_send ("fire $DIR{$sym}[0]");
2055 } elsif ($mod & KMOD_CTRL) {
2056 $self->{ctrl}++;
2057 $::CONN->user_send ("run $DIR{$sym}[0]");
2058 } else {
2059 $::CONN->user_send ("$DIR{$sym}[1]");
2060 }
2061 }
2062}
2063
2064sub key_up {
2065 my ($self, $ev) = @_;
2066
2067 my $mod = $ev->key_mod;
2068 my $sym = $ev->key_sym;
2069
2070 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2071 $::CONN->user_send ("fire_stop");
2072 }
2073 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2074 $::CONN->user_send ("run_stop");
2075 }
2076}
2077
2078#############################################################################
2079
2080package CFClient::UI::Animator; 2059package CFClient::UI::Animator;
2081 2060
2082use SDL::OpenGL; 2061use CFClient::OpenGL;
2083 2062
2084our @ISA = CFClient::UI::Bin::; 2063our @ISA = CFClient::UI::Bin::;
2085 2064
2086sub moveto { 2065sub moveto {
2087 my ($self, $x, $y) = @_; 2066 my ($self, $x, $y) = @_;
2127 my $class = shift; 2106 my $class = shift;
2128 2107
2129 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
2130 state => 0, 2109 state => 0,
2131 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
2132 @_ 2112 @_
2133 ); 2113 );
2134 2114
2135 if ($self->{state}) { 2115 if ($self->{state}) {
2136 $self->{state} = 0; 2116 $self->{state} = 0;
2156 $self->emit (changed => $self->{state}); 2136 $self->emit (changed => $self->{state});
2157} 2137}
2158 2138
2159############################################################################# 2139#############################################################################
2160 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
2161package CFClient::UI::Root; 2207package CFClient::UI::Root;
2162 2208
2163our @ISA = CFClient::UI::Container::; 2209our @ISA = CFClient::UI::Container::;
2164 2210
2165use SDL::OpenGL; 2211use CFClient::OpenGL;
2166 2212
2167sub check_size { 2213sub check_size {
2168 my ($self) = @_; 2214 my ($self) = @_;
2169 2215
2170 $self->size_allocate (0, 0, $::WITH, $::HEIGHT); 2216 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2171} 2217}
2172 2218
2173sub size_request { 2219sub size_request {
2174 ($::WIDTH, $::HEIGHT) 2220 ($::WIDTH, $::HEIGHT)
2175} 2221}
2176 2222
2177sub size_allocate { 2223sub configure {
2178 my ($self, $x, $y, $w, $h) = @_; 2224 my ($self, $x, $y, $w, $h) = @_;
2179 2225
2180 $self->_size_allocate ($x, $y, $w, $h); 2226 $self->SUPER::configure ($x, $y, $w, $h);
2181 2227
2182 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2183 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 }
2184} 2235}
2185 2236
2186sub _topleft { 2237sub _topleft {
2187 my ($self, $x, $y) = @_; 2238 my ($self, $x, $y) = @_;
2188 2239
2195 $self->check_size; 2246 $self->check_size;
2196 ::refresh (); 2247 ::refresh ();
2197} 2248}
2198 2249
2199sub add { 2250sub add {
2200 my ($self, $widget) = @_; 2251 my ($self, $child) = @_;
2201 2252
2253 # integerize window positions
2254 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y};
2256
2202 $self->SUPER::add ($widget); 2257 $self->SUPER::add ($child);
2203
2204 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2205} 2258}
2206 2259
2207sub on_refresh { 2260sub on_refresh {
2208 my ($self, $id, $cb) = @_; 2261 my ($self, $id, $cb) = @_;
2209 2262
2233############################################################################# 2286#############################################################################
2234 2287
2235package CFClient::UI; 2288package CFClient::UI;
2236 2289
2237$ROOT = new CFClient::UI::Root; 2290$ROOT = new CFClient::UI::Root;
2291$TOOLTIP = new CFClient::UI::Tooltip;
2238 2292
22391 22931
2240 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines