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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines