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.137 by root, Wed Apr 19 21:38:05 2006 UTC vs.
Revision 1.164 by root, Mon Apr 24 06:57:39 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 (length $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_markup ($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->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
45 if $FOCUS;
18} 46}
19 47
20sub feed_sdl_key_up_event { 48sub feed_sdl_key_up_event {
21 $FOCUS->key_up ($_[0]) if $FOCUS; 49 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
50 if $FOCUS;
22} 51}
23 52
24sub feed_sdl_button_down_event { 53sub feed_sdl_button_down_event {
25 my ($ev) = @_; 54 my ($ev) = @_;
26 my ($x, $y) = ($ev->{x}, $ev->{y}); 55 my ($x, $y) = ($ev->{x}, $ev->{y});
28 if (!$BUTTON_STATE) { 57 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 58 my $widget = $ROOT->find_widget ($x, $y);
30 59
31 $GRAB = $widget; 60 $GRAB = $widget;
32 $GRAB->update if $GRAB; 61 $GRAB->update if $GRAB;
62
63 check_tooltip;
33 } 64 }
34 65
35 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 66 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 67
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 68 if ($GRAB) {
69 ($x, $y) = $GRAB->coord2local ($x, $y);
70 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
71 }
38} 72}
39 73
40sub feed_sdl_button_up_event { 74sub feed_sdl_button_up_event {
41 my ($ev) = @_; 75 my ($ev) = @_;
42 my ($x, $y) = ($ev->{x}, $ev->{y}); 76 my ($x, $y) = ($ev->{x}, $ev->{y});
43 77
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 78 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 79
46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 80 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 81
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 82 if ($GRAB) {
83 ($x, $y) = $GRAB->coord2local ($x, $y);
84 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
85 }
49 86
50 if (!$BUTTON_STATE) { 87 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 88 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 89 $grab->update if $grab;
53 $GRAB->update if $GRAB; 90 $GRAB->update if $GRAB;
91
92 check_tooltip;
54 } 93 }
55} 94}
56 95
57sub feed_sdl_motion_event { 96sub feed_sdl_motion_event {
58 my ($ev) = @_; 97 my ($ev) = @_;
63 if ($widget != $HOVER) { 102 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 103 my $hover = $HOVER; $HOVER = $widget;
65 104
66 $hover->update if $hover && $hover->{can_hover}; 105 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 106 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 }
69 107
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 108 check_tooltip;
109 }
110
111 if ($HOVER) {
112 ($x, $y) = $HOVER->coord2local ($x, $y);
113 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
114 }
71} 115}
72 116
73# convert position array to integers 117# convert position array to integers
74sub harmonize { 118sub harmonize {
75 my ($vals) = @_; 119 my ($vals) = @_;
87 131
88package CFClient::UI::Base; 132package CFClient::UI::Base;
89 133
90use strict; 134use strict;
91 135
92use SDL::OpenGL; 136use CFClient::OpenGL;
93 137
94sub new { 138sub new {
95 my $class = shift; 139 my $class = shift;
96 140
97 my $self = bless { 141 my $self = bless {
98 x => 0, 142 x => 0,
99 y => 0, 143 y => 0,
100 z => 0, 144 z => 0,
145 can_events => 1,
101 @_ 146 @_
102 }, $class; 147 }, $class;
103 148
104 for (keys %$self) { 149 for (keys %$self) {
105 if (/^connect_(.*)$/) { 150 if (/^connect_(.*)$/) {
108 } 153 }
109 154
110 $self 155 $self
111} 156}
112 157
158sub destroy {
159 my ($self) = @_;
160
161 $self->hide;
162 %$self = ();
163}
164
113sub show { 165sub show {
114 my ($self) = @_; 166 my ($self) = @_;
115 167
116 return if $self->{parent}; 168 return if $self->{parent};
117 169
119} 171}
120 172
121sub hide { 173sub hide {
122 my ($self) = @_; 174 my ($self) = @_;
123 175
124 return unless $self->{parent}; 176 undef $GRAB if $GRAB == $self;
177 undef $HOVER if $HOVER == $self;
125 178
126 $self->{parent}->remove ($self); 179 $self->{parent}->remove ($self)
180 if $self->{parent};
127} 181}
128 182
129sub move { 183sub move {
130 my ($self, $x, $y, $z) = @_; 184 my ($self, $x, $y, $z) = @_;
131 185
134 $self->{z} = $z if defined $z; 188 $self->{z} = $z if defined $z;
135 189
136 $self->update; 190 $self->update;
137} 191}
138 192
139sub needs_redraw { 193sub set_size {
140 0 194 my ($self, $w, $h) = @_;
195
196 $self->{user_w} = $w;
197 $self->{user_h} = $h;
198
199 $self->check_size;
141} 200}
142 201
143sub size_request { 202sub size_request {
144 require Carp; 203 require Carp;
145 Carp::confess "size_request is abtract"; 204 Carp::confess "size_request is abstract";
146} 205}
147 206
148sub configure { 207sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 208 my ($self, $x, $y, $w, $h) = @_;
150 209
210 if ($self->{aspect}) {
211 my $w2 = List::Util::min $w, int $h * $self->{aspect};
212 my $h2 = List::Util::min $h, int $w / $self->{aspect};
213
214 # use alignment to adjust x, y
215
216 $x += int +($w - $w2) * 0.5;
217 $y += int +($h - $h2) * 0.5;
218
219 ($w, $h) = ($w2, $h2);
220 }
221
222 if ($self->{x} != $x || $self->{y} != $y) {
151 $self->{x} = $x; 223 $self->{x} = $x;
152 $self->{y} = $y; 224 $self->{y} = $y;
225 $self->update;
226 }
153 227
154 return unless $self->{w} != $w || $self->{h} != $h; 228 if ($self->{w} != $w || $self->{h} != $h) {
155
156 $self->{w} = $w; 229 $self->{w} = $w;
157 $self->{h} = $h; 230 $self->{h} = $h;
158 231
159 $self->size_allocate ($w, $h); 232 $self->size_allocate ($w, $h);
160 $self->update; 233 $self->update;
234 }
161} 235}
162 236
163sub size_allocate { 237sub size_allocate {
164 # nothing to be done 238 # nothing to be done
239}
240
241sub set_max_size {
242 my ($self, $w, $h) = @_;
243
244 delete $self->{max_w}; $self->{max_w} = $w if $w;
245 delete $self->{max_h}; $self->{max_h} = $h if $h;
165} 246}
166 247
167# return top left coordinates 248# return top left coordinates
168sub _topleft { 249sub _topleft {
169 my ($self, $x, $y) = @_; 250 my ($self, $x, $y) = @_;
252 glVertex $x + $self->{w}, $y + $self->{h}; 333 glVertex $x + $self->{w}, $y + $self->{h};
253 glVertex $x , $y + $self->{h}; 334 glVertex $x , $y + $self->{h};
254 glEnd; 335 glEnd;
255 glDisable GL_BLEND; 336 glDisable GL_BLEND;
256 } 337 }
338
339 if ($ENV{PCLIENT_DEBUG}) {
340 glPushMatrix;
341 glColor 1, 1, 0, 1;
342 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
343 glBegin GL_LINE_LOOP;
344 glVertex 0 , 0;
345 glVertex $self->{w}, 0;
346 glVertex $self->{w}, $self->{h};
347 glVertex 0 , $self->{h};
348 glEnd;
349 glPopMatrix;
350 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
351 }
257} 352}
258 353
259sub _draw { 354sub _draw {
260 my ($self) = @_; 355 my ($self) = @_;
261 356
262 warn "no draw defined for $self\n"; 357 warn "no draw defined for $self\n";
263} 358}
264 359
265sub find_widget { 360sub find_widget {
266 my ($self, $x, $y) = @_; 361 my ($self, $x, $y) = @_;
362
363 return () unless $self->{can_events};
267 364
268 return $self 365 return $self
269 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 366 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
270 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 367 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
271 368
279} 376}
280 377
281sub check_size { 378sub check_size {
282 my ($self) = @_; 379 my ($self) = @_;
283 380
284 my ($w, $h) = $self->size_request; 381 $self->{parent}
382 or return 1;
285 383
384 my ($w, $h) = $self->{user_w} && $self->{user_h}
385 ? @$self{qw(user_w user_h)}
386 : $self->size_request;
387
286 if ($w != $self->{req_w} || $h != $self->{req_h}) { 388 if ($w != $self->{req_w} || $h != $self->{req_h}) {
287 $self->{req_w} = $w; 389 $self->{req_w} = $w;
288 $self->{req_h} = $h; 390 $self->{req_h} = $h;
289 391
290 $self->{parent}->check_size 392 $self->{parent}->check_size
291 if $self->{parent}; 393 or $self->size_allocate (
394 (List::Util::max $self->{w}, $w),
395 (List::Util::max $self->{h}, $h),
396 );
397
398 1
399 } else {
400 0
292 } 401 }
293} 402}
294 403
295sub update { 404sub update {
296 my ($self) = @_; 405 my ($self) = @_;
306} 415}
307 416
308sub emit { 417sub emit {
309 my ($self, $signal, @args) = @_; 418 my ($self, $signal, @args) = @_;
310 419
311 for my $cb (@{$self->{signal_cb}{$signal} || []}) { 420 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
312 $cb->($self, @args);
313 }
314} 421}
315 422
316sub DESTROY { 423sub DESTROY {
317 my ($self) = @_; 424 my ($self) = @_;
318 425
324package CFClient::UI::DrawBG; 431package CFClient::UI::DrawBG;
325 432
326our @ISA = CFClient::UI::Base::; 433our @ISA = CFClient::UI::Base::;
327 434
328use strict; 435use strict;
329use SDL::OpenGL; 436use CFClient::OpenGL;
330 437
331sub new { 438sub new {
332 my $class = shift; 439 my $class = shift;
333 440
334 # range [value, low, high, page] 441 # range [value, low, high, page]
363 470
364package CFClient::UI::Empty; 471package CFClient::UI::Empty;
365 472
366our @ISA = CFClient::UI::Base::; 473our @ISA = CFClient::UI::Base::;
367 474
475sub new {
476 my ($class, %arg) = @_;
477 $class->SUPER::new (can_events => 0, %arg);
478}
479
368sub size_request { 480sub size_request {
369 (0, 0) 481 (0, 0)
370} 482}
371 483
372sub draw { } 484sub draw { }
380sub new { 492sub new {
381 my ($class, %arg) = @_; 493 my ($class, %arg) = @_;
382 494
383 my $children = delete $arg{children} || []; 495 my $children = delete $arg{children} || [];
384 496
385 my $self = $class->SUPER::new (children => [], %arg); 497 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
386 $self->add ($_) for @$children; 498 $self->add ($_) for @$children;
387 499
388 $self 500 $self
389} 501}
390 502
405 517
406sub remove { 518sub remove {
407 my ($self, $child) = @_; 519 my ($self, $child) = @_;
408 520
409 delete $child->{parent}; 521 delete $child->{parent};
522 $child->hide;
410 523
411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 524 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
412 525
413 $self->check_size; 526 $self->check_size;
527 $self->update;
528}
529
530sub clear {
531 my ($self) = @_;
532
533 my $children = delete $self->{children};
534 $self->{children} = [];
535
536 for (@$children) {
537 delete $_->{parent};
538 $_->hide;
539 }
414} 540}
415 541
416sub find_widget { 542sub find_widget {
417 my ($self, $x, $y) = @_; 543 my ($self, $x, $y) = @_;
418 544
482 608
483package CFClient::UI::Window; 609package CFClient::UI::Window;
484 610
485our @ISA = CFClient::UI::Bin::; 611our @ISA = CFClient::UI::Bin::;
486 612
487use SDL::OpenGL; 613use CFClient::OpenGL;
488 614
489sub new { 615sub new {
490 my ($class, %arg) = @_; 616 my ($class, %arg) = @_;
491 617
492 my $self = $class->SUPER::new (%arg); 618 my $self = $class->SUPER::new (%arg);
546sub new { die } 672sub new { die }
547 673
548sub size_request { 674sub size_request {
549 my ($self) = @_; 675 my ($self) = @_;
550 676
551 @$self{qw(child_w child_h)} = $self->child->size_request; 677 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
552 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 678 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
553 679
554 @$self{qw(child_w child_h)} 680 @$self{qw(child_w child_h)}
555} 681}
556 682
565 691
566package CFClient::UI::Frame; 692package CFClient::UI::Frame;
567 693
568our @ISA = CFClient::UI::Bin::; 694our @ISA = CFClient::UI::Bin::;
569 695
570use SDL::OpenGL; 696use CFClient::OpenGL;
571
572sub size_request {
573 my ($self) = @_;
574 my $chld = $self->child
575 or return (0, 0);
576
577 $chld->move (2, 2);
578
579 map { $_ + 4 } $chld->size_request;
580}
581
582sub size_allocate {
583 my ($self, $x, $y, $w, $h) = @_;
584
585 $self->child->configure (2, 2, $w - 4, $h - 4);
586}
587
588sub _draw {
589 my ($self) = @_;
590
591 my $chld = $self->child;
592
593 my ($w, $h) = $chld->size_request;
594
595 glBegin GL_QUADS;
596 glColor 0, 0, 0;
597 glVertex 0 , 0;
598 glVertex 0 , $h + 4;
599 glVertex $w + 4 , $h + 4;
600 glVertex $w + 4 , 0;
601 glEnd;
602
603 $chld->draw;
604}
605
606#############################################################################
607
608package CFClient::UI::FancyFrame;
609
610our @ISA = CFClient::UI::Bin::;
611
612use SDL::OpenGL;
613
614my @tex =
615 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
616 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
617 697
618sub new { 698sub new {
619 my $class = shift; 699 my $class = shift;
620 700
621 # TODO: user_x, user_y, overwrite moveto?
622
623 $class->SUPER::new ( 701 my $self = $class->SUPER::new (
624 bg => [1, 1, 1, 1], 702 bg => [1, 1, 1, 1],
625 border_bg => [1, 1, 1, 1], 703 border_bg => [1, 1, 1, 1],
626 border => 0.8, 704 border => 0.8,
627 @_ 705 @_
628 ) 706 );
707
708 $self
709}
710
711sub _draw {
712 my ($self) = @_;
713
714 my ($w, $h) = ($self->{w}, $self->{h});
715
716 glEnable GL_BLEND;
717 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
718 glEnable GL_TEXTURE_2D;
719 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
720
721# glBegin GL_QUADS;
722# glColor 0, 0, 0, 0;
723# glVertex 0 , 0;
724# glVertex 0 , $h;
725# glVertex $w, $h;
726# glVertex $w, 0;
727# glEnd;
728
729
730 $self->child->draw;
731 glDisable GL_BLEND;
732 glDisable GL_TEXTURE_2D;
733}
734
735#############################################################################
736
737package CFClient::UI::FancyFrame;
738
739our @ISA = CFClient::UI::Bin::;
740
741use CFClient::OpenGL;
742
743my @tex =
744 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
745 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
746
747sub new {
748 my $class = shift;
749
750 # TODO: user_x, user_y, overwrite moveto?
751
752 my $self = $class->SUPER::new (
753 bg => [1, 1, 1, 1],
754 border_bg => [1, 1, 1, 1],
755 border => 0.8,
756 can_events => 0,
757 @_
758 );
759
760 $self->{title} &&= new CFClient::UI::Label
761 align => 0,
762 valign => 1,
763 text => $self->{title},
764 fontsize => 1;
765
766 $self
629} 767}
630 768
631sub border { 769sub border {
632 int $_[0]{border} * $::FONTSIZE 770 int $_[0]{border} * $::FONTSIZE
633} 771}
634 772
635sub size_request { 773sub size_request {
636 my ($self) = @_; 774 my ($self) = @_;
637
638 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
639 775
640 my ($w, $h) = $self->SUPER::size_request; 776 my ($w, $h) = $self->SUPER::size_request;
641 777
642 ( 778 (
643 $w + $self->border * 2, 779 $w + $self->border * 2,
649 my ($self, $w, $h) = @_; 785 my ($self, $w, $h) = @_;
650 786
651 $h -= List::Util::max 0, $self->border * 2; 787 $h -= List::Util::max 0, $self->border * 2;
652 $w -= List::Util::max 0, $self->border * 2; 788 $w -= List::Util::max 0, $self->border * 2;
653 789
790 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
791 if $self->{title};
792
654 $self->child->configure ($self->border, $self->border, $w, $h); 793 $self->child->configure ($self->border, $self->border, $w, $h);
655} 794}
656 795
657sub button_down { 796sub button_down {
658 my ($self, $ev, $x, $y) = @_; 797 my ($self, $ev, $x, $y) = @_;
660 my $border = $self->border; 799 my $border = $self->border;
661 800
662 if ($x < $self->{w} && $x >= $self->{w} - $border 801 if ($x < $self->{w} && $x >= $self->{w} - $border
663 && $y < $self->{h} && $y >= $self->{h} - $border) { 802 && $y < $self->{h} && $y >= $self->{h} - $border) {
664 803
665 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 804 my ($ox, $oy) = ($ev->{x}, $ev->{y});
666 my ($bw, $bh) = ($self->{w}, $self->{h}); 805 my ($bw, $bh) = ($self->{w}, $self->{h});
667 806
668 $self->{motion} = sub { 807 $self->{motion} = sub {
669 my ($ev, $x, $y) = @_; 808 my ($ev, $x, $y) = @_;
670 809
671 ($x, $y) = ($ev->motion_x, $ev->motion_y); 810 ($x, $y) = ($ev->{x}, $ev->{y});
672 811
673 $self->{user_w} = $bw + $x - $ox; 812 $self->{user_w} = $bw + $x - $ox;
674 $self->{user_h} = $bh + $y - $oy; 813 $self->{user_h} = $bh + $y - $oy;
675 $self->check_size; 814 $self->check_size;
676 }; 815 };
677 816
678 } elsif ($x >= 0 && $x < $self->{w} 817 } elsif ($x >= 0 && $x < $self->{w}
679 && $y >= 0 && $y < $border) { 818 && $y >= 0 && $y < $border) {
680 819
681 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 820 my ($ox, $oy) = ($ev->{x}, $ev->{y});
682 my ($bx, $by) = ($self->{x}, $self->{y}); 821 my ($bx, $by) = ($self->{x}, $self->{y});
683 822
684 $self->{motion} = sub { 823 $self->{motion} = sub {
685 my ($ev, $x, $y) = @_; 824 my ($ev, $x, $y) = @_;
686 825
687 ($x, $y) = ($ev->motion_x, $ev->motion_y); 826 ($x, $y) = ($ev->{x}, $ev->{y});
688 827
689 $self->move ($bx + $x - $ox, $by + $y - $oy); 828 $self->move ($bx + $x - $ox, $by + $y - $oy);
690 $self->update; 829 $self->update;
691 }; 830 };
692 } 831 }
737 $bg->draw_quad ($border, $border, $cw, $ch); 876 $bg->draw_quad ($border, $border, $cw, $ch);
738 877
739 glDisable GL_TEXTURE_2D; 878 glDisable GL_TEXTURE_2D;
740 glDisable GL_BLEND; 879 glDisable GL_BLEND;
741 880
881 $self->{title}->draw if $self->{title};
742 $self->child->draw; 882 $self->child->draw;
743} 883}
744 884
745############################################################################# 885#############################################################################
746 886
748 888
749our @ISA = CFClient::UI::Base::; 889our @ISA = CFClient::UI::Base::;
750 890
751use List::Util qw(max sum); 891use List::Util qw(max sum);
752 892
753use SDL::OpenGL; 893use CFClient::OpenGL;
754 894
755sub new { 895sub new {
756 my $class = shift; 896 my $class = shift;
757 897
758 $class->SUPER::new ( 898 $class->SUPER::new (
772 912
773# TODO: move to container class maybe? send childs a signal on removal? 913# TODO: move to container class maybe? send childs a signal on removal?
774sub clear { 914sub clear {
775 my ($self) = @_; 915 my ($self) = @_;
776 916
777 delete $self->{children}; 917 my $children = delete $self->{children};
918
919 for (grep $_, map @$_, grep $_, @$children) {
920 delete $_->{parent};
921 $_->hide;
922 }
923
778 $self->update; 924 $self->update;
779} 925}
780 926
781sub get_wh { 927sub get_wh {
782 my ($self) = @_; 928 my ($self) = @_;
788 or next; 934 or next;
789 935
790 for my $x (0 .. $#$row) { 936 for my $x (0 .. $#$row) {
791 my $widget = $row->[$x] 937 my $widget = $row->[$x]
792 or next; 938 or next;
793 my ($w, $h) = $widget->size_request; 939 my ($w, $h) = @$widget{qw(req_w req_h)};
794 940
795 $w[$x] = max $w[$x], $w; 941 $w[$x] = max $w[$x], $w;
796 $h[$y] = max $h[$y], $h; 942 $h[$y] = max $h[$y], $h;
797 } 943 }
798 } 944 }
903 1049
904 ($h, $w) = ($w, $h); 1050 ($h, $w) = ($w, $h);
905 1051
906 my $children = $self->{children}; 1052 my $children = $self->{children};
907 1053
908 my @h = map +($_->size_request)[0], @$children; 1054 my @h = map $_->{req_w}, @$children;
909 1055
910 my $req_h = List::Util::sum @h; 1056 my $req_h = List::Util::sum @h;
911 1057
912 if ($req_h > $h) { 1058 if ($req_h > $h) {
913 # ah well, not enough space 1059 # ah well, not enough space
961sub size_allocate { 1107sub size_allocate {
962 my ($self, $w, $h) = @_; 1108 my ($self, $w, $h) = @_;
963 1109
964 my $children = $self->{children}; 1110 my $children = $self->{children};
965 1111
966 my @h = map +($_->size_request)[1], @$children; 1112 my @h = map $_->{req_h}, @$children;
967 1113
968 my $req_h = List::Util::sum @h; 1114 my $req_h = List::Util::sum @h;
969 1115
970 if ($req_h > $h) { 1116 if ($req_h > $h) {
971 # ah well, not enough space 1117 # ah well, not enough space
999 1145
1000package CFClient::UI::Label; 1146package CFClient::UI::Label;
1001 1147
1002our @ISA = CFClient::UI::Base::; 1148our @ISA = CFClient::UI::Base::;
1003 1149
1004use SDL::OpenGL; 1150use CFClient::OpenGL;
1005 1151
1006sub new { 1152sub new {
1007 my ($class, %arg) = @_; 1153 my ($class, %arg) = @_;
1008 1154
1009 my $self = $class->SUPER::new ( 1155 my $self = $class->SUPER::new (
1010 fg => [1, 1, 1], 1156 fg => [1, 1, 1],
1157 #font => default_font
1011 fontsize => 1, 1158 fontsize => 1,
1012 text => "", 1159 text => "",
1013 align => -1, 1160 align => -1,
1014 valign => -1, 1161 valign => -1,
1015 padding => 2, 1162 padding => 2,
1016 layout => new CFClient::Layout, 1163 layout => new CFClient::Layout,
1164 can_events => 0,
1017 %arg 1165 %arg
1018 ); 1166 );
1019 1167
1020 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1168 if (exists $self->{template}) {
1169 my $layout = new CFClient::Layout;
1170 $layout->set_text (delete $self->{template});
1171 $self->{template} = $layout;
1172 }
1021 1173
1022 $self->set_text (delete $self->{text}) if exists $self->{text}; 1174 $self->set_text (delete $self->{text}) if exists $self->{text};
1023 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1175 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1024 1176
1025 $self 1177 $self
1026} 1178}
1027 1179
1028sub escape_text { 1180sub escape {
1029 local $_ = $_[1]; 1181 local $_ = $_[1];
1030 1182
1031 s/&/&amp;/g; 1183 s/&/&amp;/g;
1032 s/>/&gt;/g; 1184 s/>/&gt;/g;
1033 s/</&lt;/g; 1185 s/</&lt;/g;
1039 my ($self, $text) = @_; 1191 my ($self, $text) = @_;
1040 1192
1041 $self->{layout}->set_text ($text); 1193 $self->{layout}->set_text ($text);
1042 1194
1043 delete $self->{texture}; 1195 delete $self->{texture};
1196 $self->check_size;
1044 $self->update; 1197 $self->update;
1045} 1198}
1046 1199
1047sub set_markup { 1200sub set_markup {
1048 my ($self, $markup) = @_; 1201 my ($self, $markup) = @_;
1049 1202
1050 $self->{layout}->set_markup ($markup); 1203 $self->{layout}->set_markup ($markup);
1051 1204
1052 delete $self->{texture}; 1205 delete $self->{texture};
1206 $self->check_size;
1053 $self->update; 1207 $self->update;
1054} 1208}
1055 1209
1056sub size_request { 1210sub size_request {
1057 my ($self) = @_; 1211 my ($self) = @_;
1058 1212
1059 $self->{layout}->set_width; 1213 $self->{layout}->set_font ($self->{font}) if $self->{font};
1214 $self->{layout}->set_width ($self->{max_w} || -1);
1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1215 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061 1216
1062 my ($w, $h) = $self->{layout}->size; 1217 my ($w, $h) = $self->{layout}->size;
1218
1219 if (exists $self->{template}) {
1220 $self->{template}->set_font ($self->{font}) if $self->{font};
1221 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1222
1223 my ($w2, $h2) = $self->{template}->size;
1224
1225 $w = List::Util::max $w, $w2;
1226 $h = List::Util::max $h, $h2;
1227 }
1063 1228
1064 ( 1229 (
1065 $w + $self->{padding} * 2, 1230 $w + $self->{padding} * 2,
1066 $h + $self->{padding} * 2, 1231 $h + $self->{padding} * 2,
1067 ) 1232 )
1071 my ($self, $w, $h) = @_; 1236 my ($self, $w, $h) = @_;
1072 1237
1073 delete $self->{texture}; 1238 delete $self->{texture};
1074} 1239}
1075 1240
1241sub set_fontsize {
1242 my ($self, $fontsize) = @_;
1243
1244 $self->{fontsize} = $fontsize;
1245 delete $self->{texture};
1246 $self->check_size;
1247 $self->update;
1248}
1249
1076sub _draw { 1250sub _draw {
1077 my ($self) = @_; 1251 my ($self) = @_;
1078 1252
1079 my $tex = $self->{texture} ||= do { 1253 my $tex = $self->{texture} ||= do {
1254 $self->{layout}->set_font ($self->{font}) if $self->{font};
1080 $self->{layout}->set_width ($self->{w}); 1255 $self->{layout}->set_width ($self->{w});
1081 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1256 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1082 new_from_layout CFClient::Texture $self->{layout} 1257 new_from_layout CFClient::Texture $self->{layout}
1083 }; 1258 };
1084 1259
1111 1286
1112package CFClient::UI::EntryBase; 1287package CFClient::UI::EntryBase;
1113 1288
1114our @ISA = CFClient::UI::Label::; 1289our @ISA = CFClient::UI::Label::;
1115 1290
1116use SDL::OpenGL; 1291use CFClient::OpenGL;
1117 1292
1118sub new { 1293sub new {
1119 my $class = shift; 1294 my $class = shift;
1120 1295
1121 $class->SUPER::new ( 1296 $class->SUPER::new (
1122 fg => [1, 1, 1], 1297 fg => [1, 1, 1],
1123 bg => [0, 0, 0, 0.2], 1298 bg => [0, 0, 0, 0.2],
1124 active_bg => [1, 1, 1, 0.5], 1299 active_bg => [1, 1, 1, 0.5],
1125 active_fg => [0, 0, 0], 1300 active_fg => [0, 0, 0],
1126 can_hover => 1, 1301 can_hover => 1,
1127 can_focus => 1, 1302 can_focus => 1,
1128 valign => 0, 1303 valign => 0,
1304 can_events => 1,
1129 @_ 1305 @_
1130 ) 1306 )
1131} 1307}
1132 1308
1133sub _set_text { 1309sub _set_text {
1161} 1337}
1162 1338
1163sub size_allocate { 1339sub size_allocate {
1164 my ($self, $w, $h) = @_; 1340 my ($self, $w, $h) = @_;
1165 1341
1166 $self->_set_text ($self->{text}); 1342 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1167} 1343}
1168 1344
1169sub set_text { 1345sub set_text {
1170 my ($self, $text) = @_; 1346 my ($self, $text) = @_;
1171 1347
1278 1454
1279package CFClient::UI::Entry; 1455package CFClient::UI::Entry;
1280 1456
1281our @ISA = CFClient::UI::EntryBase::; 1457our @ISA = CFClient::UI::EntryBase::;
1282 1458
1283use SDL::OpenGL; 1459use CFClient::OpenGL;
1284 1460
1285sub key_down { 1461sub key_down {
1286 my ($self, $ev) = @_; 1462 my ($self, $ev) = @_;
1287 1463
1288 my $sym = $ev->{sym}; 1464 my $sym = $ev->{sym};
1301 1477
1302package CFClient::UI::Button; 1478package CFClient::UI::Button;
1303 1479
1304our @ISA = CFClient::UI::Label::; 1480our @ISA = CFClient::UI::Label::;
1305 1481
1306use SDL::OpenGL; 1482use CFClient::OpenGL;
1307 1483
1308my @tex = 1484my @tex =
1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1485 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1310 qw(b1_button_active.png); 1486 qw(b1_button_active.png);
1311 1487
1312sub new { 1488sub new {
1313 my $class = shift; 1489 my $class = shift;
1314 1490
1315 $class->SUPER::new ( 1491 $class->SUPER::new (
1316 padding => 4, 1492 padding => 4,
1317 fg => [1, 1, 1], 1493 fg => [1, 1, 1],
1318 bg => [1, 1, 1, 0.2], 1494 bg => [1, 1, 1, 0.2],
1319 active_fg => [0, 0, 1], 1495 active_fg => [0, 0, 1],
1320 can_hover => 1, 1496 can_hover => 1,
1321 align => 0, 1497 align => 0,
1322 valign => 0, 1498 valign => 0,
1499 can_events => 1,
1323 @_ 1500 @_
1324 ) 1501 )
1325} 1502}
1326 1503
1327sub button_up { 1504sub button_up {
1361package CFClient::UI::CheckBox; 1538package CFClient::UI::CheckBox;
1362 1539
1363our @ISA = CFClient::UI::DrawBG::; 1540our @ISA = CFClient::UI::DrawBG::;
1364 1541
1365my @tex = 1542my @tex =
1366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1543 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1367 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1544 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1368 1545
1369use SDL::OpenGL; 1546use CFClient::OpenGL;
1370 1547
1371sub new { 1548sub new {
1372 my $class = shift; 1549 my $class = shift;
1373 1550
1374 $class->SUPER::new ( 1551 $class->SUPER::new (
1420 glDisable GL_BLEND; 1597 glDisable GL_BLEND;
1421} 1598}
1422 1599
1423############################################################################# 1600#############################################################################
1424 1601
1425package CFClient::UI::VGauge; 1602package CFClient::UI::Image;
1426 1603
1427our @ISA = CFClient::UI::Base::; 1604our @ISA = CFClient::UI::Base::;
1428 1605
1429use SDL::OpenGL; 1606use CFClient::OpenGL;
1607use Carp qw/confess/;
1430 1608
1431my %tex = ( 1609our %loaded_images;
1432 food => [
1433 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1434 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1435 ],
1436 grace => [
1437 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1438 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1439 ],
1440 hp => [
1441 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1442 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1443 ],
1444 mana => [
1445 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1446 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1447 ],
1448);
1449 1610
1450# eg. VGauge->new (gauge => 'food'), default gauge: food
1451sub new { 1611sub new {
1452 my $class = shift; 1612 my $class = shift;
1453 1613
1454 my $self = $class->SUPER::new (gauge => 'food', @_); 1614 my $self = $class->SUPER::new (can_events => 0, @_);
1615
1616 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1617
1618 $loaded_images{$self->{image}} ||=
1619 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1620
1621 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1622
1623 Scalar::Util::weaken $loaded_images{$self->{image}};
1624
1625 $self->{aspect} = $tex->{w} / $tex->{h};
1455 1626
1456 $self 1627 $self
1457} 1628}
1458 1629
1459sub size_request { 1630sub size_request {
1460 my ($self) = @_; 1631 my ($self) = @_;
1461 1632
1462 my $tex = $tex{$self->{gauge}}[0]; 1633 ($self->{tex}->{w}, $self->{tex}->{h})
1463
1464 @$tex{qw(w h)}
1465}
1466
1467sub set_max {
1468 my ($self, $max) = @_;
1469
1470 $self->{max_val} = $max;
1471}
1472
1473sub set_value {
1474 my ($self, $val, $max) = @_;
1475
1476 $self->set_max ($max)
1477 if defined $max;
1478
1479 $max = $self->{max_val};
1480 $self->{val} = $val;
1481
1482 $self->update;
1483} 1634}
1484 1635
1485sub _draw { 1636sub _draw {
1486 my ($self) = @_; 1637 my ($self) = @_;
1487 1638
1488 my $tex = $tex{$self->{gauge}}; 1639 my $tex = $self->{tex};
1489 1640
1490 my ($w, $h) = ($self->{w}, $self->{h}); 1641 my ($w, $h) = ($self->{w}, $self->{h});
1491 1642
1492 my $ycut = $self->{val} / ($self->{max_val} || 1); 1643 if ($self->{rot90}) {
1493 $ycut = 1 if $self->{val} > $self->{max_val}; 1644 glRotate 90, 0, 0, 1;
1645 glTranslate 0, -$self->{w}, 0;
1494 1646
1495 my $t1 = $tex->[0]; 1647 ($w, $h) = ($h, $w);
1496 my $t2 = $tex->[1]; 1648 }
1497 1649
1498 glEnable GL_BLEND; 1650 glEnable GL_BLEND;
1499 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1651 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1500 glEnable GL_TEXTURE_2D; 1652 glEnable GL_TEXTURE_2D;
1501 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1653 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1502 1654
1655 $tex->draw_quad (0, 0, $w, $h);
1656
1657 glDisable GL_BLEND;
1658 glDisable GL_TEXTURE_2D;
1659}
1660
1661#############################################################################
1662
1663package CFClient::UI::VGauge;
1664
1665our @ISA = CFClient::UI::Base::;
1666
1667use List::Util qw(min max);
1668
1669use CFClient::OpenGL;
1670
1671my %tex = (
1672 food => [
1673 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1674 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1675 ],
1676 grace => [
1677 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1678 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1679 ],
1680 hp => [
1681 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1682 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1683 ],
1684 mana => [
1685 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1686 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1687 ],
1688);
1689
1690# eg. VGauge->new (gauge => 'food'), default gauge: food
1691sub new {
1692 my $class = shift;
1693
1694 my $self = $class->SUPER::new (
1695 type => 'food',
1696 @_
1697 );
1698
1699 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1700
1701 $self
1702}
1703
1704sub size_request {
1705 my ($self) = @_;
1706
1707 #my $tex = $tex{$self->{type}}[0];
1708 #@$tex{qw(w h)}
1709 (0, 0)
1710}
1711
1712sub set_max {
1713 my ($self, $max) = @_;
1714
1715 $self->{max_val} = $max;
1716}
1717
1718sub set_value {
1719 my ($self, $val, $max) = @_;
1720
1721 $self->set_max ($max)
1722 if defined $max;
1723
1724 $max = $self->{max_val};
1725 $self->{val} = $val;
1726
1727 $self->update;
1728}
1729
1730sub _draw {
1731 my ($self) = @_;
1732
1733 my $tex = $tex{$self->{type}};
1734 my ($t1, $t2, $t3) = @$tex;
1735
1736 my ($w, $h) = ($self->{w}, $self->{h});
1737
1738 if ($self->{vertical}) {
1739 glRotate 90, 0, 0, 1;
1740 glTranslate 0, -$self->{w}, 0;
1741
1742 ($w, $h) = ($h, $w);
1743 }
1744
1745 my $ycut = $self->{val} / ($self->{max_val} || 1);
1746
1747 my $ycut1 = max 0, min 1, $ycut;
1748 my $ycut2 = max 0, min 1, $ycut - 1;
1749
1503 my $h1 = $self->{h} - $ycut * $self->{h}; 1750 my $h1 = $self->{h} * (1 - $ycut1);
1504 my $h2 = $ycut * $self->{h}; 1751 my $h2 = $self->{h} * (1 - $ycut2);
1505 1752
1506 my $yp = 0; 1753 glEnable GL_BLEND;
1754 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1755 glEnable GL_TEXTURE_2D;
1756 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1507 1757
1508 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1758 glBindTexture GL_TEXTURE_2D, $t1->{name};
1509 glBegin GL_QUADS; 1759 glBegin GL_QUADS;
1510 glTexCoord 0 , 0; glVertex 0 , $yp; 1760 glTexCoord 0 , 0; glVertex 0 , 0;
1511 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1761 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1512 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1762 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1513 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1763 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1514 glEnd; 1764 glEnd;
1515 1765
1516 $yp += $h1; 1766 my $ycut1 = List::Util::min 1, $ycut;
1517
1518 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1767 glBindTexture GL_TEXTURE_2D, $t2->{name};
1519 glBegin GL_QUADS; 1768 glBegin GL_QUADS;
1520 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1769 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1521 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1770 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1522 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1523 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1771 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1772 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1524 glEnd; 1773 glEnd;
1774
1775 if ($t3) {
1776 glBindTexture GL_TEXTURE_2D, $t3->{name};
1777 glBegin GL_QUADS;
1778 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1779 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1780 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1781 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1782 glEnd;
1783 }
1525 1784
1526 glDisable GL_BLEND; 1785 glDisable GL_BLEND;
1527 glDisable GL_TEXTURE_2D; 1786 glDisable GL_TEXTURE_2D;
1528} 1787}
1529 1788
1530############################################################################# 1789#############################################################################
1531 1790
1791package CFClient::UI::Gauge;
1792
1793our @ISA = CFClient::UI::VBox::;
1794
1795sub new {
1796 my ($class, %arg) = @_;
1797
1798 my $self = $class->SUPER::new (
1799 tooltip => $arg{type},
1800 can_hover => 1,
1801 %arg,
1802 );
1803
1804 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1805 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1806 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1807
1808 $self
1809}
1810
1811sub set_fontsize {
1812 my ($self, $fsize) = @_;
1813
1814 $self->{value}->set_fontsize ($fsize);
1815 $self->{max} ->set_fontsize ($fsize);
1816}
1817
1818sub set_value {
1819 my ($self, $val, $max) = @_;
1820
1821 $self->set_max ($max)
1822 if defined $max;
1823
1824 $self->{gauge}->set_value ($val, $max);
1825 $self->{value}->set_text ($val);
1826}
1827
1828sub set_max {
1829 my ($self, $max) = @_;
1830
1831 $self->{gauge}->set_max ($max);
1832 $self->{max}->set_text ($max);
1833}
1834
1835#############################################################################
1836
1532package CFClient::UI::Slider; 1837package CFClient::UI::Slider;
1533 1838
1534use strict; 1839use strict;
1535 1840
1536use SDL::OpenGL; 1841use CFClient::OpenGL;
1537 1842
1538our @ISA = CFClient::UI::DrawBG::; 1843our @ISA = CFClient::UI::DrawBG::;
1539 1844
1540my @tex = 1845my @tex =
1541 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1846 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1666 1971
1667package CFClient::UI::TextView; 1972package CFClient::UI::TextView;
1668 1973
1669our @ISA = CFClient::UI::HBox::; 1974our @ISA = CFClient::UI::HBox::;
1670 1975
1671use SDL::OpenGL; 1976use CFClient::OpenGL;
1672 1977
1673sub new { 1978sub new {
1674 my $class = shift; 1979 my $class = shift;
1675 1980
1676 my $self = $class->SUPER::new ( 1981 my $self = $class->SUPER::new (
1677 fontsize => 1, 1982 fontsize => 1,
1983 can_events => 0,
1984 #font => default_font
1678 @_, 1985 @_,
1679 1986
1680 layout => (new CFClient::Layout), 1987 layout => (new CFClient::Layout),
1681 par => [], 1988 par => [],
1682 height => 0, 1989 height => 0,
1683 children => [ 1990 children => [
1684 (new CFClient::UI::Empty expand => 1), 1991 (new CFClient::UI::Empty expand => 1),
1685 (new CFClient::UI::Slider vertical => 1), 1992 (new CFClient::UI::Slider vertical => 1),
1686 ], 1993 ],
1687 ); 1994 );
1688 1995
1722sub size_allocate { 2029sub size_allocate {
1723 my ($self, $w, $h) = @_; 2030 my ($self, $w, $h) = @_;
1724 2031
1725 $self->SUPER::size_allocate ($w, $h); 2032 $self->SUPER::size_allocate ($w, $h);
1726 2033
2034 $self->{layout}->set_font ($self->{font}) if $self->{font};
1727 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2035 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1728 $self->{layout}->set_width ($self->{children}[0]{w}); 2036 $self->{layout}->set_width ($self->{children}[0]{w});
1729 2037
1730 $self->reflow; 2038 $self->reflow;
1731} 2039}
1783 my $y1 = $top + $self->{h}; 2091 my $y1 = $top + $self->{h};
1784 2092
1785 my $y = 0; 2093 my $y = 0;
1786 2094
1787 my $layout = $self->{layout}; 2095 my $layout = $self->{layout};
2096
2097 $layout->set_font ($self->{font}) if $self->{font};
1788 2098
1789 for my $par (@{$self->{par}}) { 2099 for my $par (@{$self->{par}}) {
1790 my $h = $par->[0]; 2100 my $h = $par->[0];
1791 2101
1792 if ($y0 < $y + $h && $y < $y1) { 2102 if ($y0 < $y + $h && $y < $y1) {
1822 2132
1823############################################################################# 2133#############################################################################
1824 2134
1825package CFClient::UI::Animator; 2135package CFClient::UI::Animator;
1826 2136
1827use SDL::OpenGL; 2137use CFClient::OpenGL;
1828 2138
1829our @ISA = CFClient::UI::Bin::; 2139our @ISA = CFClient::UI::Bin::;
1830 2140
1831sub moveto { 2141sub moveto {
1832 my ($self, $x, $y) = @_; 2142 my ($self, $x, $y) = @_;
1870 2180
1871sub new { 2181sub new {
1872 my $class = shift; 2182 my $class = shift;
1873 2183
1874 my $self = $class->SUPER::new ( 2184 my $self = $class->SUPER::new (
1875 state => 0, 2185 state => 0,
1876 connect_activate => \&toggle_flopper, 2186 connect_activate => \&toggle_flopper,
1877 @_ 2187 @_
1878 ); 2188 );
1879 2189
1880 if ($self->{state}) { 2190 if ($self->{state}) {
1901 $self->emit (changed => $self->{state}); 2211 $self->emit (changed => $self->{state});
1902} 2212}
1903 2213
1904############################################################################# 2214#############################################################################
1905 2215
2216package CFClient::UI::Tooltip;
2217
2218our @ISA = CFClient::UI::Bin::;
2219
2220use CFClient::OpenGL;
2221
2222sub new {
2223 my $class = shift;
2224
2225 $class->SUPER::new (
2226 @_,
2227 can_events => 0,
2228 )
2229}
2230
2231sub set_markup {
2232 my ($self, $text) = @_;
2233
2234 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2235 $self->{label}->set_markup ($text);
2236 $self->add ($self->{label});
2237}
2238
2239sub size_request {
2240 my ($self) = @_;
2241
2242 $self->child->set_max_size ($::WIDTH * 0.3);
2243
2244 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2245
2246 ($w + 4, $h + 4)
2247}
2248
2249sub size_allocate {
2250 my ($self, $w, $h) = @_;
2251
2252 $self->SUPER::size_allocate ($w - 4, $h - 4);
2253}
2254
2255sub _draw {
2256 my ($self) = @_;
2257
2258 glPushMatrix;
2259 glTranslate 0.375, 0.375;
2260
2261 my ($w, $h) = @$self{qw(w h)};
2262
2263 glColor 1, 0.8, 0.4;
2264 glBegin GL_QUADS;
2265 glVertex 0 , 0;
2266 glVertex 0 , $h;
2267 glVertex $w, $h;
2268 glVertex $w, 0;
2269 glEnd;
2270
2271 glColor 0, 0, 0;
2272 glBegin GL_LINE_LOOP;
2273 glVertex 0 , 0;
2274 glVertex 0 , $h;
2275 glVertex $w, $h;
2276 glVertex $w, 0;
2277 glEnd;
2278
2279 glPopMatrix;
2280
2281 glTranslate 2, 2;
2282 $self->SUPER::_draw;
2283}
2284
2285#############################################################################
2286
2287package CFClient::UI::Face;
2288
2289our @ISA = CFClient::UI::Base::;
2290
2291use CFClient::OpenGL;
2292
2293sub new {
2294 my $class = shift;
2295
2296 $class->SUPER::new (
2297 aspect => 1,
2298 @_,
2299 )
2300}
2301
2302sub size_request {
2303 (32, 8)
2304}
2305
2306sub draw {
2307 my ($self) = @_;
2308
2309 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2310
2311 if ($tex) {
2312 glEnable GL_BLEND;
2313 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2314 glEnable GL_TEXTURE_2D;
2315 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2316 glColor 1, 1, 1, 1;
2317 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2318 glDisable GL_TEXTURE_2D;
2319 glDisable GL_BLEND;
2320 }
2321}
2322
2323#############################################################################
2324
1906package CFClient::UI::Root; 2325package CFClient::UI::Root;
1907 2326
1908our @ISA = CFClient::UI::Container::; 2327our @ISA = CFClient::UI::Container::;
1909 2328
1910use SDL::OpenGL; 2329use CFClient::OpenGL;
1911 2330
1912sub check_size { 2331sub check_size {
1913 my ($self) = @_; 2332 my ($self) = @_;
1914 2333
1915 $self->configure (0, 0, $::WITH, $::HEIGHT); 2334 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
1916} 2335}
1917 2336
1918sub size_request { 2337sub size_request {
1919 ($::WIDTH, $::HEIGHT) 2338 ($::WIDTH, $::HEIGHT)
1920} 2339}
1922sub configure { 2341sub configure {
1923 my ($self, $x, $y, $w, $h) = @_; 2342 my ($self, $x, $y, $w, $h) = @_;
1924 2343
1925 $self->SUPER::configure ($x, $y, $w, $h); 2344 $self->SUPER::configure ($x, $y, $w, $h);
1926 2345
1927 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1928 for @{$self->{children}}; 2346 for my $child (@{$self->{children}}) {
2347 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2348
2349 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2350 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2351 $child->configure ($X, $Y, $W,$H);
2352 }
1929} 2353}
1930 2354
1931sub _topleft { 2355sub _topleft {
1932 my ($self, $x, $y) = @_; 2356 my ($self, $x, $y) = @_;
1933 2357
1941 ::refresh (); 2365 ::refresh ();
1942} 2366}
1943 2367
1944sub add { 2368sub add {
1945 my ($self, $child) = @_; 2369 my ($self, $child) = @_;
2370
2371 # integerize window positions
2372 $child->{x} = int $child->{x};
2373 $child->{y} = int $child->{y};
1946 2374
1947 $self->SUPER::add ($child); 2375 $self->SUPER::add ($child);
1948} 2376}
1949 2377
1950sub on_refresh { 2378sub on_refresh {
1976############################################################################# 2404#############################################################################
1977 2405
1978package CFClient::UI; 2406package CFClient::UI;
1979 2407
1980$ROOT = new CFClient::UI::Root; 2408$ROOT = new CFClient::UI::Root;
2409$TOOLTIP = new CFClient::UI::Tooltip;
1981 2410
19821 24111
1983 2412

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines