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.134 by root, Wed Apr 19 06:06:45 2006 UTC vs.
Revision 1.162 by root, Mon Apr 24 06:05:35 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->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,
134 can_events => 1,
101 @_ 135 @_
102 }, $class; 136 }, $class;
103 137
104 for (keys %$self) { 138 for (keys %$self) {
105 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
108 } 142 }
109 143
110 $self 144 $self
111} 145}
112 146
147sub destroy {
148 my ($self) = @_;
149
150 $self->hide;
151 %$self = ();
152}
153
113sub show { 154sub show {
114 my ($self) = @_; 155 my ($self) = @_;
115 156
116 return if $self->{parent}; 157 return if $self->{parent};
117 158
134 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
135 176
136 $self->update; 177 $self->update;
137} 178}
138 179
139sub needs_redraw { 180sub set_size {
140 0 181 my ($self, $w, $h) = @_;
182
183 $self->{user_w} = $w;
184 $self->{user_h} = $h;
185
186 $self->check_size;
141} 187}
142 188
143sub size_request { 189sub size_request {
144 require Carp; 190 require Carp;
145 Carp::confess "size_request is abtract"; 191 Carp::confess "size_request is abstract";
146} 192}
147 193
148sub configure { 194sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 195 my ($self, $x, $y, $w, $h) = @_;
150 196
197 if ($self->{aspect}) {
198 my $w2 = List::Util::min $w, int $h * $self->{aspect};
199 my $h2 = List::Util::min $h, int $w / $self->{aspect};
200
201 # use alignment to adjust x, y
202
203 $x += int +($w - $w2) * 0.5;
204 $y += int +($h - $h2) * 0.5;
205
206 ($w, $h) = ($w2, $h2);
207 }
208
209 if ($self->{x} != $x || $self->{y} != $y) {
151 $self->{x} = $x; 210 $self->{x} = $x;
152 $self->{y} = $y; 211 $self->{y} = $y;
212 $self->update;
213 }
153 214
154 return unless $self->{w} != $w || $self->{h} != $h; 215 if ($self->{w} != $w || $self->{h} != $h) {
155
156 $self->{w} = $w; 216 $self->{w} = $w;
157 $self->{h} = $h; 217 $self->{h} = $h;
158 218
159 $self->size_allocate ($w, $h); 219 $self->size_allocate ($w, $h);
160 $self->update; 220 $self->update;
221 }
161} 222}
162 223
163sub size_allocate { 224sub size_allocate {
164 # nothing to be done 225 # nothing to be done
226}
227
228sub set_max_size {
229 my ($self, $w, $h) = @_;
230
231 delete $self->{max_w}; $self->{max_w} = $w if $w;
232 delete $self->{max_h}; $self->{max_h} = $h if $h;
165} 233}
166 234
167# return top left coordinates 235# return top left coordinates
168sub _topleft { 236sub _topleft {
169 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
252 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
253 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
254 glEnd; 322 glEnd;
255 glDisable GL_BLEND; 323 glDisable GL_BLEND;
256 } 324 }
325
326 if ($ENV{PCLIENT_DEBUG}) {
327 glPushMatrix;
328 glColor 1, 1, 0, 1;
329 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
330 glBegin GL_LINE_LOOP;
331 glVertex 0 , 0;
332 glVertex $self->{w}, 0;
333 glVertex $self->{w}, $self->{h};
334 glVertex 0 , $self->{h};
335 glEnd;
336 glPopMatrix;
337 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
338 }
257} 339}
258 340
259sub _draw { 341sub _draw {
260 my ($self) = @_; 342 my ($self) = @_;
261 343
262 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
263} 345}
264 346
265sub find_widget { 347sub find_widget {
266 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
267 351
268 return $self 352 return $self
269 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
270 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
271 355
279} 363}
280 364
281sub check_size { 365sub check_size {
282 my ($self) = @_; 366 my ($self) = @_;
283 367
284 my ($w, $h) = $self->size_request; 368 $self->{parent}
369 or return 1;
285 370
371 my ($w, $h) = $self->{user_w} && $self->{user_h}
372 ? @$self{qw(user_w user_h)}
373 : $self->size_request;
374
286 if ($w != $self->{req_w} || $h != $self->{req_h}) { 375 if ($w != $self->{req_w} || $h != $self->{req_h}) {
287 $self->{req_w} = $w; 376 $self->{req_w} = $w;
288 $self->{req_h} = $h; 377 $self->{req_h} = $h;
289 378
290 $self->{parent}->check_size 379 $self->{parent}->check_size
291 if $self->{parent}; 380 or $self->size_allocate (
381 (List::Util::max $self->{w}, $w),
382 (List::Util::max $self->{h}, $h),
383 );
384
385 1
386 } else {
387 0
292 } 388 }
293} 389}
294 390
295sub update { 391sub update {
296 my ($self) = @_; 392 my ($self) = @_;
324package CFClient::UI::DrawBG; 420package CFClient::UI::DrawBG;
325 421
326our @ISA = CFClient::UI::Base::; 422our @ISA = CFClient::UI::Base::;
327 423
328use strict; 424use strict;
329use SDL::OpenGL; 425use CFClient::OpenGL;
330 426
331sub new { 427sub new {
332 my $class = shift; 428 my $class = shift;
333 429
334 # range [value, low, high, page] 430 # range [value, low, high, page]
363 459
364package CFClient::UI::Empty; 460package CFClient::UI::Empty;
365 461
366our @ISA = CFClient::UI::Base::; 462our @ISA = CFClient::UI::Base::;
367 463
464sub new {
465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
468
368sub size_request { 469sub size_request {
369 (0, 0) 470 (0, 0)
370} 471}
371 472
372sub draw { } 473sub draw { }
380sub new { 481sub new {
381 my ($class, %arg) = @_; 482 my ($class, %arg) = @_;
382 483
383 my $children = delete $arg{children} || []; 484 my $children = delete $arg{children} || [];
384 485
385 my $self = $class->SUPER::new (children => [], %arg); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
386 $self->add ($_) for @$children; 487 $self->add ($_) for @$children;
387 488
388 $self 489 $self
389} 490}
390 491
409 delete $child->{parent}; 510 delete $child->{parent};
410 511
411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
412 513
413 $self->check_size; 514 $self->check_size;
515 $self->update;
516}
517
518sub clear {
519 my ($self) = @_;
520
521 delete $_->{parent}
522 for @{ delete $self->{children} };
523
524 $self->{children} = [];
414} 525}
415 526
416sub find_widget { 527sub find_widget {
417 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
418 529
482 593
483package CFClient::UI::Window; 594package CFClient::UI::Window;
484 595
485our @ISA = CFClient::UI::Bin::; 596our @ISA = CFClient::UI::Bin::;
486 597
487use SDL::OpenGL; 598use CFClient::OpenGL;
488 599
489sub new { 600sub new {
490 my ($class, %arg) = @_; 601 my ($class, %arg) = @_;
491 602
492 my $self = $class->SUPER::new (%arg); 603 my $self = $class->SUPER::new (%arg);
546sub new { die } 657sub new { die }
547 658
548sub size_request { 659sub size_request {
549 my ($self) = @_; 660 my ($self) = @_;
550 661
551 @$self{qw(child_w child_h)} = $self->child->size_request; 662 @$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)}); 663 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
553 664
554 @$self{qw(child_w child_h)} 665 @$self{qw(child_w child_h)}
555} 666}
556 667
565 676
566package CFClient::UI::Frame; 677package CFClient::UI::Frame;
567 678
568our @ISA = CFClient::UI::Bin::; 679our @ISA = CFClient::UI::Bin::;
569 680
570use SDL::OpenGL; 681use 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 682
618sub new { 683sub new {
619 my $class = shift; 684 my $class = shift;
620 685
621 # TODO: user_x, user_y, overwrite moveto?
622
623 $class->SUPER::new ( 686 my $self = $class->SUPER::new (
624 bg => [1, 1, 1, 1], 687 bg => [1, 1, 1, 1],
625 border_bg => [1, 1, 1, 1], 688 border_bg => [1, 1, 1, 1],
626 border => 0.8, 689 border => 0.8,
627 @_ 690 @_
628 ) 691 );
692
693 $self
694}
695
696sub _draw {
697 my ($self) = @_;
698
699 my ($w, $h) = ($self->{w}, $self->{h});
700
701 glEnable GL_BLEND;
702 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
703 glEnable GL_TEXTURE_2D;
704 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
705
706# glBegin GL_QUADS;
707# glColor 0, 0, 0, 0;
708# glVertex 0 , 0;
709# glVertex 0 , $h;
710# glVertex $w, $h;
711# glVertex $w, 0;
712# glEnd;
713
714
715 $self->child->draw;
716 glDisable GL_BLEND;
717 glDisable GL_TEXTURE_2D;
718}
719
720#############################################################################
721
722package CFClient::UI::FancyFrame;
723
724our @ISA = CFClient::UI::Bin::;
725
726use CFClient::OpenGL;
727
728my @tex =
729 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
730 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
731
732sub new {
733 my $class = shift;
734
735 # TODO: user_x, user_y, overwrite moveto?
736
737 my $self = $class->SUPER::new (
738 bg => [1, 1, 1, 1],
739 border_bg => [1, 1, 1, 1],
740 border => 0.8,
741 can_events => 1,
742 @_
743 );
744
745 $self->{title} &&= new CFClient::UI::Label
746 align => 0,
747 valign => 1,
748 text => $self->{title},
749 fontsize => 1;
750
751 $self
629} 752}
630 753
631sub border { 754sub border {
632 int $_[0]{border} * $::FONTSIZE 755 int $_[0]{border} * $::FONTSIZE
633} 756}
634 757
635sub size_request { 758sub size_request {
636 my ($self) = @_; 759 my ($self) = @_;
637
638 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
639 760
640 my ($w, $h) = $self->SUPER::size_request; 761 my ($w, $h) = $self->SUPER::size_request;
641 762
642 ( 763 (
643 $w + $self->border * 2, 764 $w + $self->border * 2,
649 my ($self, $w, $h) = @_; 770 my ($self, $w, $h) = @_;
650 771
651 $h -= List::Util::max 0, $self->border * 2; 772 $h -= List::Util::max 0, $self->border * 2;
652 $w -= List::Util::max 0, $self->border * 2; 773 $w -= List::Util::max 0, $self->border * 2;
653 774
775 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
776 if $self->{title};
777
654 $self->child->configure ($self->border, $self->border, $w, $h); 778 $self->child->configure ($self->border, $self->border, $w, $h);
655} 779}
656 780
657sub button_down { 781sub button_down {
658 my ($self, $ev, $x, $y) = @_; 782 my ($self, $ev, $x, $y) = @_;
660 my $border = $self->border; 784 my $border = $self->border;
661 785
662 if ($x < $self->{w} && $x >= $self->{w} - $border 786 if ($x < $self->{w} && $x >= $self->{w} - $border
663 && $y < $self->{h} && $y >= $self->{h} - $border) { 787 && $y < $self->{h} && $y >= $self->{h} - $border) {
664 788
665 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 789 my ($ox, $oy) = ($ev->{x}, $ev->{y});
666 my ($bw, $bh) = ($self->{w}, $self->{h}); 790 my ($bw, $bh) = ($self->{w}, $self->{h});
667 791
668 $self->{motion} = sub { 792 $self->{motion} = sub {
669 my ($ev, $x, $y) = @_; 793 my ($ev, $x, $y) = @_;
670 794
671 ($x, $y) = ($ev->motion_x, $ev->motion_y); 795 ($x, $y) = ($ev->{x}, $ev->{y});
672 796
673 $self->{user_w} = $bw + $x - $ox; 797 $self->{user_w} = $bw + $x - $ox;
674 $self->{user_h} = $bh + $y - $oy; 798 $self->{user_h} = $bh + $y - $oy;
675 $self->check_size; 799 $self->check_size;
676 }; 800 };
677 801
678 } elsif ($x >= 0 && $x < $self->{w} 802 } elsif ($x >= 0 && $x < $self->{w}
679 && $y >= 0 && $y < $border) { 803 && $y >= 0 && $y < $border) {
680 804
681 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 805 my ($ox, $oy) = ($ev->{x}, $ev->{y});
682 my ($bx, $by) = ($self->{x}, $self->{y}); 806 my ($bx, $by) = ($self->{x}, $self->{y});
683 807
684 $self->{motion} = sub { 808 $self->{motion} = sub {
685 my ($ev, $x, $y) = @_; 809 my ($ev, $x, $y) = @_;
686 810
687 ($x, $y) = ($ev->motion_x, $ev->motion_y); 811 ($x, $y) = ($ev->{x}, $ev->{y});
688 812
689 $self->move ($bx + $x - $ox, $by + $y - $oy); 813 $self->move ($bx + $x - $ox, $by + $y - $oy);
690 $self->update; 814 $self->update;
691 }; 815 };
692 } 816 }
737 $bg->draw_quad ($border, $border, $cw, $ch); 861 $bg->draw_quad ($border, $border, $cw, $ch);
738 862
739 glDisable GL_TEXTURE_2D; 863 glDisable GL_TEXTURE_2D;
740 glDisable GL_BLEND; 864 glDisable GL_BLEND;
741 865
866 $self->{title}->draw if $self->{title};
742 $self->child->draw; 867 $self->child->draw;
743} 868}
744 869
745############################################################################# 870#############################################################################
746 871
748 873
749our @ISA = CFClient::UI::Base::; 874our @ISA = CFClient::UI::Base::;
750 875
751use List::Util qw(max sum); 876use List::Util qw(max sum);
752 877
753use SDL::OpenGL; 878use CFClient::OpenGL;
754 879
755sub new { 880sub new {
756 my $class = shift; 881 my $class = shift;
757 882
758 $class->SUPER::new ( 883 $class->SUPER::new (
788 or next; 913 or next;
789 914
790 for my $x (0 .. $#$row) { 915 for my $x (0 .. $#$row) {
791 my $widget = $row->[$x] 916 my $widget = $row->[$x]
792 or next; 917 or next;
793 my ($w, $h) = $widget->size_request; 918 my ($w, $h) = @$widget{qw(req_w req_h)};
794 919
795 $w[$x] = max $w[$x], $w; 920 $w[$x] = max $w[$x], $w;
796 $h[$y] = max $h[$y], $h; 921 $h[$y] = max $h[$y], $h;
797 } 922 }
798 } 923 }
903 1028
904 ($h, $w) = ($w, $h); 1029 ($h, $w) = ($w, $h);
905 1030
906 my $children = $self->{children}; 1031 my $children = $self->{children};
907 1032
908 my @h = map +($_->size_request)[0], @$children; 1033 my @h = map $_->{req_w}, @$children;
909 1034
910 my $req_h = List::Util::sum @h; 1035 my $req_h = List::Util::sum @h;
911 1036
912 if ($req_h > $h) { 1037 if ($req_h > $h) {
913 # ah well, not enough space 1038 # ah well, not enough space
961sub size_allocate { 1086sub size_allocate {
962 my ($self, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
963 1088
964 my $children = $self->{children}; 1089 my $children = $self->{children};
965 1090
966 my @h = map +($_->size_request)[1], @$children; 1091 my @h = map $_->{req_h}, @$children;
967 1092
968 my $req_h = List::Util::sum @h; 1093 my $req_h = List::Util::sum @h;
969 1094
970 if ($req_h > $h) { 1095 if ($req_h > $h) {
971 # ah well, not enough space 1096 # ah well, not enough space
999 1124
1000package CFClient::UI::Label; 1125package CFClient::UI::Label;
1001 1126
1002our @ISA = CFClient::UI::Base::; 1127our @ISA = CFClient::UI::Base::;
1003 1128
1004use SDL::OpenGL; 1129use CFClient::OpenGL;
1005 1130
1006sub new { 1131sub new {
1007 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
1008 1133
1009 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
1010 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
1136 #font => default_font
1011 fontsize => 1, 1137 fontsize => 1,
1012 text => "", 1138 text => "",
1013 align => -1, 1139 align => -1,
1014 valign => -1, 1140 valign => -1,
1015 padding => 2, 1141 padding => 2,
1016 layout => new CFClient::Layout, 1142 layout => new CFClient::Layout,
1143 can_events => 0,
1017 %arg 1144 %arg
1018 ); 1145 );
1019 1146
1020 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1147 if (exists $self->{template}) {
1148 my $layout = new CFClient::Layout;
1149 $layout->set_text (delete $self->{template});
1150 $self->{template} = $layout;
1151 }
1021 1152
1022 $self->set_text (delete $self->{text}) if exists $self->{text}; 1153 $self->set_text (delete $self->{text}) if exists $self->{text};
1023 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1154 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1024 1155
1025 $self 1156 $self
1026} 1157}
1027 1158
1028sub escape_text { 1159sub escape {
1029 local $_ = $_[1]; 1160 local $_ = $_[1];
1030 1161
1031 s/&/&amp;/g; 1162 s/&/&amp;/g;
1032 s/>/&gt;/g; 1163 s/>/&gt;/g;
1033 s/</&lt;/g; 1164 s/</&lt;/g;
1039 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
1040 1171
1041 $self->{layout}->set_text ($text); 1172 $self->{layout}->set_text ($text);
1042 1173
1043 delete $self->{texture}; 1174 delete $self->{texture};
1175 $self->check_size;
1044 $self->update; 1176 $self->update;
1045} 1177}
1046 1178
1047sub set_markup { 1179sub set_markup {
1048 my ($self, $markup) = @_; 1180 my ($self, $markup) = @_;
1049 1181
1050 $self->{layout}->set_markup ($markup); 1182 $self->{layout}->set_markup ($markup);
1051 1183
1052 delete $self->{texture}; 1184 delete $self->{texture};
1185 $self->check_size;
1053 $self->update; 1186 $self->update;
1054} 1187}
1055 1188
1056sub size_request { 1189sub size_request {
1057 my ($self) = @_; 1190 my ($self) = @_;
1058 1191
1059 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061 1195
1062 my ($w, $h) = $self->{layout}->size; 1196 my ($w, $h) = $self->{layout}->size;
1197
1198 if (exists $self->{template}) {
1199 $self->{template}->set_font ($self->{font}) if $self->{font};
1200 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1201
1202 my ($w2, $h2) = $self->{template}->size;
1203
1204 $w = List::Util::max $w, $w2;
1205 $h = List::Util::max $h, $h2;
1206 }
1063 1207
1064 ( 1208 (
1065 $w + $self->{padding} * 2, 1209 $w + $self->{padding} * 2,
1066 $h + $self->{padding} * 2, 1210 $h + $self->{padding} * 2,
1067 ) 1211 )
1071 my ($self, $w, $h) = @_; 1215 my ($self, $w, $h) = @_;
1072 1216
1073 delete $self->{texture}; 1217 delete $self->{texture};
1074} 1218}
1075 1219
1220sub set_fontsize {
1221 my ($self, $fontsize) = @_;
1222
1223 $self->{fontsize} = $fontsize;
1224 delete $self->{texture};
1225 $self->check_size;
1226 $self->update;
1227}
1228
1076sub _draw { 1229sub _draw {
1077 my ($self) = @_; 1230 my ($self) = @_;
1078 1231
1079 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
1080 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
1081 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1235 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1082 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
1083 }; 1237 };
1084 1238
1111 1265
1112package CFClient::UI::EntryBase; 1266package CFClient::UI::EntryBase;
1113 1267
1114our @ISA = CFClient::UI::Label::; 1268our @ISA = CFClient::UI::Label::;
1115 1269
1116use SDL; 1270use CFClient::OpenGL;
1117use SDL::OpenGL;
1118 1271
1119sub new { 1272sub new {
1120 my $class = shift; 1273 my $class = shift;
1121 1274
1122 $class->SUPER::new ( 1275 $class->SUPER::new (
1125 active_bg => [1, 1, 1, 0.5], 1278 active_bg => [1, 1, 1, 0.5],
1126 active_fg => [0, 0, 0], 1279 active_fg => [0, 0, 0],
1127 can_hover => 1, 1280 can_hover => 1,
1128 can_focus => 1, 1281 can_focus => 1,
1129 valign => 0, 1282 valign => 0,
1283 can_events => 1,
1130 @_ 1284 @_
1131 ) 1285 )
1132} 1286}
1133 1287
1134sub _set_text { 1288sub _set_text {
1162} 1316}
1163 1317
1164sub size_allocate { 1318sub size_allocate {
1165 my ($self, $w, $h) = @_; 1319 my ($self, $w, $h) = @_;
1166 1320
1167 $self->_set_text ($self->{text}); 1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1168} 1322}
1169 1323
1170sub set_text { 1324sub set_text {
1171 my ($self, $text) = @_; 1325 my ($self, $text) = @_;
1172 1326
1176} 1330}
1177 1331
1178sub key_down { 1332sub key_down {
1179 my ($self, $ev) = @_; 1333 my ($self, $ev) = @_;
1180 1334
1181 my $mod = $ev->key_mod; 1335 my $mod = $ev->{mod};
1182 my $sym = $ev->key_sym; 1336 my $sym = $ev->{sym};
1183
1184 my $uni = $ev->key_unicode; 1337 my $uni = $ev->{unicode};
1185 1338
1186 my $text = $self->get_text; 1339 my $text = $self->get_text;
1187 1340
1188 if ($sym == SDLK_BACKSPACE) { 1341 if ($sym == 8) {
1189 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1342 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1190 } elsif ($sym == SDLK_DELETE) { 1343 } elsif ($sym == 127) {
1191 substr $text, $self->{cursor}, 1, ""; 1344 substr $text, $self->{cursor}, 1, "";
1192 } elsif ($sym == SDLK_LEFT) { 1345 } elsif ($sym == CFClient::SDLK_LEFT) {
1193 --$self->{cursor} if $self->{cursor}; 1346 --$self->{cursor} if $self->{cursor};
1194 } elsif ($sym == SDLK_RIGHT) { 1347 } elsif ($sym == CFClient::SDLK_RIGHT) {
1195 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1348 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1196 } elsif ($sym == SDLK_HOME) { 1349 } elsif ($sym == CFClient::SDLK_HOME) {
1197 $self->{cursor} = 0; 1350 $self->{cursor} = 0;
1198 } elsif ($sym == SDLK_END) { 1351 } elsif ($sym == CFClient::SDLK_END) {
1199 $self->{cursor} = length $text; 1352 $self->{cursor} = length $text;
1200 } elsif ($sym == SDLK_ESCAPE) { 1353 } elsif ($sym == 27) {
1201 $self->emit ('escape'); 1354 $self->emit ('escape');
1202 } elsif ($uni) { 1355 } elsif ($uni) {
1203 substr $text, $self->{cursor}++, 0, chr $uni; 1356 substr $text, $self->{cursor}++, 0, chr $uni;
1204 } 1357 }
1205 1358
1280 1433
1281package CFClient::UI::Entry; 1434package CFClient::UI::Entry;
1282 1435
1283our @ISA = CFClient::UI::EntryBase::; 1436our @ISA = CFClient::UI::EntryBase::;
1284 1437
1285use SDL; 1438use CFClient::OpenGL;
1286use SDL::OpenGL;
1287 1439
1288sub key_down { 1440sub key_down {
1289 my ($self, $ev) = @_; 1441 my ($self, $ev) = @_;
1290 1442
1291 my $sym = $ev->key_sym; 1443 my $sym = $ev->{sym};
1292 1444
1293 if ($sym == SDLK_RETURN) { 1445 if ($sym == 13) {
1294 $self->emit (activate => $self->get_text); 1446 $self->emit (activate => $self->get_text);
1295 $self->update; 1447 $self->update;
1296 1448
1297 } else { 1449 } else {
1298 $self->SUPER::key_down ($ev); 1450 $self->SUPER::key_down ($ev);
1304 1456
1305package CFClient::UI::Button; 1457package CFClient::UI::Button;
1306 1458
1307our @ISA = CFClient::UI::Label::; 1459our @ISA = CFClient::UI::Label::;
1308 1460
1309use SDL; 1461use CFClient::OpenGL;
1310use SDL::OpenGL;
1311 1462
1312my @tex = 1463my @tex =
1313 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1464 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1314 qw(b1_button_active.png); 1465 qw(b1_button_active.png);
1315 1466
1316sub new { 1467sub new {
1317 my $class = shift; 1468 my $class = shift;
1318 1469
1322 bg => [1, 1, 1, 0.2], 1473 bg => [1, 1, 1, 0.2],
1323 active_fg => [0, 0, 1], 1474 active_fg => [0, 0, 1],
1324 can_hover => 1, 1475 can_hover => 1,
1325 align => 0, 1476 align => 0,
1326 valign => 0, 1477 valign => 0,
1478 can_events => 1,
1327 @_ 1479 @_
1328 ) 1480 )
1329} 1481}
1330 1482
1331sub button_up { 1483sub button_up {
1365package CFClient::UI::CheckBox; 1517package CFClient::UI::CheckBox;
1366 1518
1367our @ISA = CFClient::UI::DrawBG::; 1519our @ISA = CFClient::UI::DrawBG::;
1368 1520
1369my @tex = 1521my @tex =
1370 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1522 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1371 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1523 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1372 1524
1373use SDL; 1525use CFClient::OpenGL;
1374use SDL::OpenGL;
1375 1526
1376sub new { 1527sub new {
1377 my $class = shift; 1528 my $class = shift;
1378 1529
1379 $class->SUPER::new ( 1530 $class->SUPER::new (
1425 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1426} 1577}
1427 1578
1428############################################################################# 1579#############################################################################
1429 1580
1430package CFClient::UI::VGauge; 1581package CFClient::UI::Image;
1431 1582
1432our @ISA = CFClient::UI::Base::; 1583our @ISA = CFClient::UI::Base::;
1433 1584
1434use SDL::OpenGL; 1585use CFClient::OpenGL;
1586use Carp qw/confess/;
1435 1587
1436my %tex = ( 1588our %loaded_images;
1437 food => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1440 ],
1441 grace => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1444 ],
1445 hp => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1448 ],
1449 mana => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1452 ],
1453);
1454 1589
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new { 1590sub new {
1457 my $class = shift; 1591 my $class = shift;
1458 1592
1459 my $self = $class->SUPER::new (gauge => 'food', @_); 1593 my $self = $class->SUPER::new (can_events => 0, @_);
1594
1595 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1596
1597 $loaded_images{$self->{image}} ||=
1598 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1599
1600 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1601
1602 Scalar::Util::weaken $loaded_images{$self->{image}};
1603
1604 $self->{aspect} = $tex->{w} / $tex->{h};
1460 1605
1461 $self 1606 $self
1462} 1607}
1463 1608
1464sub size_request { 1609sub size_request {
1465 my ($self) = @_; 1610 my ($self) = @_;
1466 1611
1467 my $tex = $tex{$self->{gauge}}[0]; 1612 ($self->{tex}->{w}, $self->{tex}->{h})
1468
1469 @$tex{qw(w h)}
1470}
1471
1472sub set_max {
1473 my ($self, $max) = @_;
1474
1475 $self->{max_val} = $max;
1476}
1477
1478sub set_value {
1479 my ($self, $val, $max) = @_;
1480
1481 $self->set_max ($max)
1482 if defined $max;
1483
1484 $max = $self->{max_val};
1485 $self->{val} = $val;
1486
1487 $self->update;
1488} 1613}
1489 1614
1490sub _draw { 1615sub _draw {
1491 my ($self) = @_; 1616 my ($self) = @_;
1492 1617
1493 my $tex = $tex{$self->{gauge}}; 1618 my $tex = $self->{tex};
1494 1619
1495 my ($w, $h) = ($self->{w}, $self->{h}); 1620 my ($w, $h) = ($self->{w}, $self->{h});
1496 1621
1497 my $ycut = $self->{val} / ($self->{max_val} || 1); 1622 if ($self->{rot90}) {
1498 $ycut = 1 if $self->{val} > $self->{max_val}; 1623 glRotate 90, 0, 0, 1;
1624 glTranslate 0, -$self->{w}, 0;
1499 1625
1500 my $t1 = $tex->[0]; 1626 ($w, $h) = ($h, $w);
1501 my $t2 = $tex->[1]; 1627 }
1502 1628
1503 glEnable GL_BLEND; 1629 glEnable GL_BLEND;
1504 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1630 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1505 glEnable GL_TEXTURE_2D; 1631 glEnable GL_TEXTURE_2D;
1506 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1632 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1507 1633
1634 $tex->draw_quad (0, 0, $w, $h);
1635
1636 glDisable GL_BLEND;
1637 glDisable GL_TEXTURE_2D;
1638}
1639
1640#############################################################################
1641
1642package CFClient::UI::VGauge;
1643
1644our @ISA = CFClient::UI::Base::;
1645
1646use List::Util qw(min max);
1647
1648use CFClient::OpenGL;
1649
1650my %tex = (
1651 food => [
1652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1653 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1654 ],
1655 grace => [
1656 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1657 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1658 ],
1659 hp => [
1660 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1661 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1662 ],
1663 mana => [
1664 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1665 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1666 ],
1667);
1668
1669# eg. VGauge->new (gauge => 'food'), default gauge: food
1670sub new {
1671 my $class = shift;
1672
1673 my $self = $class->SUPER::new (
1674 type => 'food',
1675 @_
1676 );
1677
1678 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1679
1680 $self
1681}
1682
1683sub size_request {
1684 my ($self) = @_;
1685
1686 #my $tex = $tex{$self->{type}}[0];
1687 #@$tex{qw(w h)}
1688 (0, 0)
1689}
1690
1691sub set_max {
1692 my ($self, $max) = @_;
1693
1694 $self->{max_val} = $max;
1695}
1696
1697sub set_value {
1698 my ($self, $val, $max) = @_;
1699
1700 $self->set_max ($max)
1701 if defined $max;
1702
1703 $max = $self->{max_val};
1704 $self->{val} = $val;
1705
1706 $self->update;
1707}
1708
1709sub _draw {
1710 my ($self) = @_;
1711
1712 my $tex = $tex{$self->{type}};
1713 my ($t1, $t2, $t3) = @$tex;
1714
1715 my ($w, $h) = ($self->{w}, $self->{h});
1716
1717 if ($self->{vertical}) {
1718 glRotate 90, 0, 0, 1;
1719 glTranslate 0, -$self->{w}, 0;
1720
1721 ($w, $h) = ($h, $w);
1722 }
1723
1724 my $ycut = $self->{val} / ($self->{max_val} || 1);
1725
1726 my $ycut1 = max 0, min 1, $ycut;
1727 my $ycut2 = max 0, min 1, $ycut - 1;
1728
1508 my $h1 = $self->{h} - $ycut * $self->{h}; 1729 my $h1 = $self->{h} * (1 - $ycut1);
1509 my $h2 = $ycut * $self->{h}; 1730 my $h2 = $self->{h} * (1 - $ycut2);
1510 1731
1511 my $yp = 0; 1732 glEnable GL_BLEND;
1733 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1734 glEnable GL_TEXTURE_2D;
1735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 1736
1513 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1737 glBindTexture GL_TEXTURE_2D, $t1->{name};
1514 glBegin GL_QUADS; 1738 glBegin GL_QUADS;
1515 glTexCoord 0 , 0; glVertex 0 , $yp; 1739 glTexCoord 0 , 0; glVertex 0 , 0;
1516 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1740 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1517 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1741 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1518 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1742 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1519 glEnd; 1743 glEnd;
1520 1744
1521 $yp += $h1; 1745 my $ycut1 = List::Util::min 1, $ycut;
1522
1523 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1746 glBindTexture GL_TEXTURE_2D, $t2->{name};
1524 glBegin GL_QUADS; 1747 glBegin GL_QUADS;
1525 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1748 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1526 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1749 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1527 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1528 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1750 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1751 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1529 glEnd; 1752 glEnd;
1753
1754 if ($t3) {
1755 glBindTexture GL_TEXTURE_2D, $t3->{name};
1756 glBegin GL_QUADS;
1757 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1758 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1759 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1760 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1761 glEnd;
1762 }
1530 1763
1531 glDisable GL_BLEND; 1764 glDisable GL_BLEND;
1532 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1533} 1766}
1534 1767
1535############################################################################# 1768#############################################################################
1536 1769
1770package CFClient::UI::Gauge;
1771
1772our @ISA = CFClient::UI::VBox::;
1773
1774sub new {
1775 my ($class, %arg) = @_;
1776
1777 my $self = $class->SUPER::new (
1778 tooltip => $arg{type},
1779 can_events => 1,
1780 can_hover => 1,
1781 %arg,
1782 );
1783
1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1786 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1787
1788 $self
1789}
1790
1791sub set_fontsize {
1792 my ($self, $fsize) = @_;
1793
1794 $self->{value}->set_fontsize ($fsize);
1795 $self->{max} ->set_fontsize ($fsize);
1796}
1797
1798sub set_value {
1799 my ($self, $val, $max) = @_;
1800
1801 $self->set_max ($max)
1802 if defined $max;
1803
1804 $self->{gauge}->set_value ($val, $max);
1805 $self->{value}->set_text ($val);
1806}
1807
1808sub set_max {
1809 my ($self, $max) = @_;
1810
1811 $self->{gauge}->set_max ($max);
1812 $self->{max}->set_text ($max);
1813}
1814
1815#############################################################################
1816
1537package CFClient::UI::Slider; 1817package CFClient::UI::Slider;
1538 1818
1539use strict; 1819use strict;
1540 1820
1541use SDL::OpenGL; 1821use CFClient::OpenGL;
1542 1822
1543our @ISA = CFClient::UI::DrawBG::; 1823our @ISA = CFClient::UI::DrawBG::;
1544 1824
1545my @tex = 1825my @tex =
1546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1826 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1671 1951
1672package CFClient::UI::TextView; 1952package CFClient::UI::TextView;
1673 1953
1674our @ISA = CFClient::UI::HBox::; 1954our @ISA = CFClient::UI::HBox::;
1675 1955
1676use SDL::OpenGL; 1956use CFClient::OpenGL;
1677 1957
1678sub new { 1958sub new {
1679 my $class = shift; 1959 my $class = shift;
1680 1960
1681 my $self = $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1682 fontsize => 1, 1962 fontsize => 1,
1963 #font => default_font
1683 @_, 1964 @_,
1684 1965
1685 layout => (new CFClient::Layout), 1966 layout => (new CFClient::Layout),
1686 par => [], 1967 par => [],
1687 height => 0, 1968 height => 0,
1727sub size_allocate { 2008sub size_allocate {
1728 my ($self, $w, $h) = @_; 2009 my ($self, $w, $h) = @_;
1729 2010
1730 $self->SUPER::size_allocate ($w, $h); 2011 $self->SUPER::size_allocate ($w, $h);
1731 2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
1732 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1733 $self->{layout}->set_width ($self->{children}[0]{w}); 2015 $self->{layout}->set_width ($self->{children}[0]{w});
1734 2016
1735 $self->reflow; 2017 $self->reflow;
1736} 2018}
1788 my $y1 = $top + $self->{h}; 2070 my $y1 = $top + $self->{h};
1789 2071
1790 my $y = 0; 2072 my $y = 0;
1791 2073
1792 my $layout = $self->{layout}; 2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
1793 2077
1794 for my $par (@{$self->{par}}) { 2078 for my $par (@{$self->{par}}) {
1795 my $h = $par->[0]; 2079 my $h = $par->[0];
1796 2080
1797 if ($y0 < $y + $h && $y < $y1) { 2081 if ($y0 < $y + $h && $y < $y1) {
1825 2109
1826} 2110}
1827 2111
1828############################################################################# 2112#############################################################################
1829 2113
1830package CFClient::UI::MapWidget;
1831
1832use strict;
1833
1834use List::Util qw(min max);
1835
1836use SDL;
1837use SDL::OpenGL;
1838
1839our @ISA = CFClient::UI::Base::;
1840
1841sub new {
1842 my $class = shift;
1843
1844 $class->SUPER::new (
1845 z => -1,
1846 can_focus => 1,
1847 list => (glGenLists 1),
1848 @_
1849 )
1850}
1851
1852sub key_down {
1853 print "MAPKEYDOWN\n";
1854}
1855
1856sub key_up {
1857}
1858
1859sub button_down {
1860 my ($self, $ev, $x, $y) = @_;
1861
1862 $self->focus_in;
1863
1864 if ($ev->button == 2) {
1865 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1866 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1867
1868 $self->{motion} = sub {
1869 my ($ev, $x, $y) = @_;
1870
1871 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1872
1873 $::CFG->{map_shift_x} = $bw + $x - $ox;
1874 $::CFG->{map_shift_y} = $bh + $y - $oy;
1875
1876 $self->update;
1877 };
1878 }
1879}
1880
1881sub button_up {
1882 my ($self, $ev, $x, $y) = @_;
1883
1884 delete $self->{motion};
1885}
1886
1887sub mouse_motion {
1888 my ($self, $ev, $x, $y) = @_;
1889
1890 $self->{motion}->($ev, $x, $y) if $self->{motion};
1891}
1892
1893sub size_request {
1894 (
1895 1 + 32 * int $::WIDTH / 32,
1896 1 + 32 * int $::HEIGHT / 32,
1897 )
1898}
1899
1900sub update {
1901 my ($self) = @_;
1902
1903 $self->{need_update} = 1;
1904 $self->SUPER::update;
1905}
1906
1907sub draw {
1908 my ($self) = @_;
1909
1910 if (delete $self->{need_update}) {
1911 glNewList $self->{list}, GL_COMPILE;
1912
1913 if ($::MAP) {
1914 my $sw = int $::WIDTH / 32;
1915 my $sh = int $::HEIGHT / 32;
1916
1917 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1918 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1919
1920 glTranslate $sx0 - 32, $sy0 - 32, 0;
1921
1922 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1923
1924 if ($::CFG->{fow_enable}) {
1925 if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war
1926 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
1927 glConvolutionFilter2D (
1928 GL_CONVOLUTION_2D,
1929 GL_ALPHA,
1930 3, 3,
1931 GL_ALPHA, GL_FLOAT,
1932 pack "f*",
1933 0.1, 0.1, 0.1,
1934 0.1, 0.2, 0.1,
1935 0.1, 0.1, 0.1,
1936 );
1937 glEnable GL_CONVOLUTION_2D;
1938 }
1939
1940 $self->{fow_texture} = new CFClient::Texture
1941 w => $w,
1942 h => $h,
1943 data => $data,
1944 internalformat => GL_ALPHA,
1945 format => GL_ALPHA;
1946
1947 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1948
1949 glEnable GL_BLEND;
1950 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1951 glEnable GL_TEXTURE_2D;
1952 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1953
1954 glColor +($::CFG->{fow_intensity}) x 3, 1;
1955 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1956
1957 glDisable GL_TEXTURE_2D;
1958 glDisable GL_BLEND;
1959 }
1960
1961 # HACK BEGIN
1962 {
1963 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1964 my ($w, $h) = (250, 250);
1965
1966 glEnable GL_BLEND;
1967 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1968 glEnable GL_TEXTURE_2D;
1969 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1970
1971 $self->{mapmap_texture} =
1972 new CFClient::Texture
1973 w => $w,
1974 h => $h,
1975 data => $::MAP->mapmap ($w, $h),
1976 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
1977
1978 $self->{mapmap_texture}->draw_quad (100, 100);
1979
1980 glDisable GL_TEXTURE_2D;
1981 glDisable GL_BLEND;
1982 }
1983 # HACK END
1984 }
1985
1986 glEndList;
1987 }
1988
1989 glPushMatrix;
1990 glCallList $self->{list};
1991 glPopMatrix;
1992
1993 if ($FOCUS != $self) {
1994 glColor 64/255, 64/255, 64/255;
1995 glLogicOp GL_AND;
1996 glEnable GL_COLOR_LOGIC_OP;
1997 glBegin GL_QUADS;
1998 glVertex 0, 0;
1999 glVertex 0, $::HEIGHT;
2000 glVertex $::WIDTH, $::HEIGHT;
2001 glVertex $::WIDTH, 0;
2002 glEnd;
2003 glDisable GL_COLOR_LOGIC_OP;
2004 }
2005}
2006
2007my %DIR = (
2008 SDLK_KP8, [1, "north"],
2009 SDLK_KP9, [2, "northeast"],
2010 SDLK_KP6, [3, "east"],
2011 SDLK_KP3, [4, "southeast"],
2012 SDLK_KP2, [5, "south"],
2013 SDLK_KP1, [6, "southwest"],
2014 SDLK_KP4, [7, "west"],
2015 SDLK_KP7, [8, "northwest"],
2016
2017 SDLK_UP, [1, "north"],
2018 SDLK_RIGHT, [3, "east"],
2019 SDLK_DOWN, [5, "south"],
2020 SDLK_LEFT, [7, "west"],
2021);
2022
2023sub key_down {
2024 my ($self, $ev) = @_;
2025
2026 my $mod = $ev->key_mod;
2027 my $sym = $ev->key_sym;
2028
2029 if ($sym == SDLK_KP5) {
2030 $::CONN->user_send ("stay fire");
2031 } elsif ($sym == SDLK_a) {
2032 $::CONN->user_send ("apply");
2033 } elsif ($sym == SDLK_QUOTE) {
2034 $self->emit ('activate_console');
2035 } elsif ($sym == SDLK_SLASH) {
2036 $self->emit ('activate_console' => '/');
2037 } elsif (exists $DIR{$sym}) {
2038 if ($mod & KMOD_SHIFT) {
2039 $self->{shft}++;
2040 $::CONN->user_send ("fire $DIR{$sym}[0]");
2041 } elsif ($mod & KMOD_CTRL) {
2042 $self->{ctrl}++;
2043 $::CONN->user_send ("run $DIR{$sym}[0]");
2044 } else {
2045 $::CONN->user_send ("$DIR{$sym}[1]");
2046 }
2047 }
2048}
2049
2050sub key_up {
2051 my ($self, $ev) = @_;
2052
2053 my $mod = $ev->key_mod;
2054 my $sym = $ev->key_sym;
2055
2056 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2057 $::CONN->user_send ("fire_stop");
2058 }
2059 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2060 $::CONN->user_send ("run_stop");
2061 }
2062}
2063
2064#############################################################################
2065
2066package CFClient::UI::Animator; 2114package CFClient::UI::Animator;
2067 2115
2068use SDL::OpenGL; 2116use CFClient::OpenGL;
2069 2117
2070our @ISA = CFClient::UI::Bin::; 2118our @ISA = CFClient::UI::Bin::;
2071 2119
2072sub moveto { 2120sub moveto {
2073 my ($self, $x, $y) = @_; 2121 my ($self, $x, $y) = @_;
2113 my $class = shift; 2161 my $class = shift;
2114 2162
2115 my $self = $class->SUPER::new ( 2163 my $self = $class->SUPER::new (
2116 state => 0, 2164 state => 0,
2117 connect_activate => \&toggle_flopper, 2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
2118 @_ 2167 @_
2119 ); 2168 );
2120 2169
2121 if ($self->{state}) { 2170 if ($self->{state}) {
2122 $self->{state} = 0; 2171 $self->{state} = 0;
2142 $self->emit (changed => $self->{state}); 2191 $self->emit (changed => $self->{state});
2143} 2192}
2144 2193
2145############################################################################# 2194#############################################################################
2146 2195
2196package CFClient::UI::Tooltip;
2197
2198our @ISA = CFClient::UI::Bin::;
2199
2200use CFClient::OpenGL;
2201
2202sub new {
2203 my $class = shift;
2204
2205 $class->SUPER::new (
2206 @_,
2207 can_events => 0,
2208 )
2209}
2210
2211sub set_markup {
2212 my ($self, $text) = @_;
2213
2214 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2215 $self->{label}->set_markup ($text);
2216 $self->add ($self->{label});
2217}
2218
2219sub size_request {
2220 my ($self) = @_;
2221
2222 $self->child->set_max_size ($::WIDTH * 0.3);
2223
2224 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2225
2226 ($w + 4, $h + 4)
2227}
2228
2229sub size_allocate {
2230 my ($self, $w, $h) = @_;
2231
2232 $self->SUPER::size_allocate ($w - 4, $h - 4);
2233}
2234
2235sub _draw {
2236 my ($self) = @_;
2237
2238 glPushMatrix;
2239 glTranslate 0.375, 0.375;
2240
2241 my ($w, $h) = @$self{qw(w h)};
2242
2243 glColor 1, 0.8, 0.4;
2244 glBegin GL_QUADS;
2245 glVertex 0 , 0;
2246 glVertex 0 , $h;
2247 glVertex $w, $h;
2248 glVertex $w, 0;
2249 glEnd;
2250
2251 glColor 0, 0, 0;
2252 glBegin GL_LINE_LOOP;
2253 glVertex 0 , 0;
2254 glVertex 0 , $h;
2255 glVertex $w, $h;
2256 glVertex $w, 0;
2257 glEnd;
2258
2259 glPopMatrix;
2260
2261 glTranslate 2, 2;
2262 $self->SUPER::_draw;
2263}
2264
2265#############################################################################
2266
2267package CFClient::UI::Face;
2268
2269our @ISA = CFClient::UI::Base::;
2270
2271use CFClient::OpenGL;
2272
2273sub new {
2274 my $class = shift;
2275
2276 $class->SUPER::new (
2277 aspect => 1,
2278 @_,
2279 )
2280}
2281
2282sub size_request {
2283 (32, 8)
2284}
2285
2286sub draw {
2287 my ($self) = @_;
2288
2289 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2290
2291 if ($tex) {
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 glColor 1, 1, 1, 1;
2297 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2298 glDisable GL_TEXTURE_2D;
2299 glDisable GL_BLEND;
2300 }
2301}
2302
2303#############################################################################
2304
2147package CFClient::UI::Root; 2305package CFClient::UI::Root;
2148 2306
2149our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
2150 2308
2151use SDL::OpenGL; 2309use CFClient::OpenGL;
2152 2310
2153sub check_size { 2311sub check_size {
2154 my ($self) = @_; 2312 my ($self) = @_;
2155 2313
2156 $self->configure (0, 0, $::WITH, $::HEIGHT); 2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2157} 2315}
2158 2316
2159sub size_request { 2317sub size_request {
2160 ($::WIDTH, $::HEIGHT) 2318 ($::WIDTH, $::HEIGHT)
2161} 2319}
2163sub configure { 2321sub configure {
2164 my ($self, $x, $y, $w, $h) = @_; 2322 my ($self, $x, $y, $w, $h) = @_;
2165 2323
2166 $self->SUPER::configure ($x, $y, $w, $h); 2324 $self->SUPER::configure ($x, $y, $w, $h);
2167 2325
2168 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2169 for @{$self->{children}}; 2326 for my $child (@{$self->{children}}) {
2327 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2328
2329 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2330 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2331 $child->configure ($X, $Y, $W,$H);
2332 }
2170} 2333}
2171 2334
2172sub _topleft { 2335sub _topleft {
2173 my ($self, $x, $y) = @_; 2336 my ($self, $x, $y) = @_;
2174 2337
2182 ::refresh (); 2345 ::refresh ();
2183} 2346}
2184 2347
2185sub add { 2348sub add {
2186 my ($self, $child) = @_; 2349 my ($self, $child) = @_;
2350
2351 # integerize window positions
2352 $child->{x} = int $child->{x};
2353 $child->{y} = int $child->{y};
2187 2354
2188 $self->SUPER::add ($child); 2355 $self->SUPER::add ($child);
2189} 2356}
2190 2357
2191sub on_refresh { 2358sub on_refresh {
2217############################################################################# 2384#############################################################################
2218 2385
2219package CFClient::UI; 2386package CFClient::UI;
2220 2387
2221$ROOT = new CFClient::UI::Root; 2388$ROOT = new CFClient::UI::Root;
2389$TOOLTIP = new CFClient::UI::Tooltip;
2222 2390
22231 23911
2224 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines