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.130 by root, Tue Apr 18 01:51:06 2006 UTC vs.
Revision 1.160 by root, Mon Apr 24 03:33:51 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_text ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 48 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 49}
23 50
24sub feed_sdl_button_down_event { 51sub feed_sdl_button_down_event {
25 my ($ev) = @_; 52 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 53 my ($x, $y) = ($ev->{x}, $ev->{y});
27 54
28 if (!$BUTTON_STATE) { 55 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 56 my $widget = $ROOT->find_widget ($x, $y);
30 57
31 $GRAB = $widget; 58 $GRAB = $widget;
32 $GRAB->update if $GRAB; 59 $GRAB->update if $GRAB;
33 }
34 60
61 check_tooltip;
62 }
63
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 65
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 67}
39 68
40sub feed_sdl_button_up_event { 69sub feed_sdl_button_up_event {
41 my ($ev) = @_; 70 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 71 my ($x, $y) = ($ev->{x}, $ev->{y});
43 72
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 73 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 74
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 76
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 78
50 if (!$BUTTON_STATE) { 79 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 80 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 81 $grab->update if $grab;
53 $GRAB->update if $GRAB; 82 $GRAB->update if $GRAB;
83
84 check_tooltip;
54 } 85 }
55} 86}
56 87
57sub feed_sdl_motion_event { 88sub feed_sdl_motion_event {
58 my ($ev) = @_; 89 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 90 my ($x, $y) = ($ev->{x}, $ev->{y});
60 91
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 92 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 93
63 if ($widget != $HOVER) { 94 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 95 my $hover = $HOVER; $HOVER = $widget;
65 96
66 $hover->update if $hover && $hover->{can_hover}; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 98 $HOVER->update if $HOVER && $HOVER->{can_hover};
99
100 check_tooltip;
68 } 101 }
69 102
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 104}
72 105
87 120
88package CFClient::UI::Base; 121package CFClient::UI::Base;
89 122
90use strict; 123use strict;
91 124
92use SDL::OpenGL; 125use CFClient::OpenGL;
93 126
94sub new { 127sub new {
95 my $class = shift; 128 my $class = shift;
96 129
97 my $self = bless { 130 my $self = bless {
98 x => 0, 131 x => 0,
99 y => 0, 132 y => 0,
100 z => 0, 133 z => 0,
101 w => -1, 134 can_events => 1,
102 h => -1,
103 @_ 135 @_
104 }, $class; 136 }, $class;
105 137
106 for (keys %$self) { 138 for (keys %$self) {
107 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
110 } 142 }
111 143
112 $self 144 $self
113} 145}
114 146
147sub destroy {
148 my ($self) = @_;
149
150 $self->hide;
151 %$self = ();
152}
153
154sub show {
155 my ($self) = @_;
156
157 return if $self->{parent};
158
159 $CFClient::UI::ROOT->add ($self);
160}
161
162sub hide {
163 my ($self) = @_;
164
165 return unless $self->{parent};
166
167 $self->{parent}->remove ($self);
168}
169
115sub move { 170sub move {
116 my ($self, $x, $y, $z) = @_; 171 my ($self, $x, $y, $z) = @_;
117 172
118 $self->{x} = int $x; 173 $self->{x} = int $x;
119 $self->{y} = int $y; 174 $self->{y} = int $y;
120 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
121 176
122 $self->update; 177 $self->update;
123} 178}
124 179
125sub needs_redraw { 180sub set_size {
126 0 181 my ($self, $w, $h) = @_;
182
183 $self->{user_w} = $w;
184 $self->{user_h} = $h;
185
186 $self->check_size;
127} 187}
128 188
129sub size_request { 189sub size_request {
130 require Carp; 190 require Carp;
131 Carp::confess "size_request is abtract"; 191 Carp::confess "size_request is abstract";
132} 192}
133 193
134sub configure { 194sub configure {
135 my ($self, $x, $y, $w, $h) = @_; 195 my ($self, $x, $y, $w, $h) = @_;
136 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) {
137 $self->{x} = $x; 210 $self->{x} = $x;
138 $self->{y} = $y; 211 $self->{y} = $y;
212 $self->update;
213 }
139 214
140 return unless $self->{w} != $w || $self->{h} != $h; 215 if ($self->{w} != $w || $self->{h} != $h) {
141
142 $self->{w} = $w; 216 $self->{w} = $w;
143 $self->{h} = $h; 217 $self->{h} = $h;
144 218
145 $self->size_allocate ($w, $h); 219 $self->size_allocate ($w, $h);
220 $self->update;
221 }
146} 222}
147 223
148sub size_allocate { 224sub size_allocate {
149 # 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;
150} 233}
151 234
152# return top left coordinates 235# return top left coordinates
153sub _topleft { 236sub _topleft {
154 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
248} 331}
249 332
250sub find_widget { 333sub find_widget {
251 my ($self, $x, $y) = @_; 334 my ($self, $x, $y) = @_;
252 335
336 return () unless $self->{can_events};
337
253 return $self 338 return $self
254 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 339 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
255 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 340 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
256 341
257 () 342 ()
264} 349}
265 350
266sub check_size { 351sub check_size {
267 my ($self) = @_; 352 my ($self) = @_;
268 353
269 my ($w, $h) = $self->size_request; 354 $self->{parent}
355 or return 1;
270 356
357 my ($w, $h) = $self->{user_w} && $self->{user_h}
358 ? @$self{qw(user_w user_h)}
359 : $self->size_request;
360
271 if ($w != $self->{req_w} || $h != $self->{req_h}) { 361 if ($w != $self->{req_w} || $h != $self->{req_h}) {
272 $self->{req_w} = $w; 362 $self->{req_w} = $w;
273 $self->{req_h} = $h; 363 $self->{req_h} = $h;
274 364
275 $self->{parent}->check_size 365 $self->{parent}->check_size
276 if $self->{parent};
277
278 $self->size_allocate ($w, $h); 366 or $self->size_allocate (
279 $self->update; 367 (List::Util::max $self->{w}, $w),
368 (List::Util::max $self->{h}, $h),
369 );
370
371 1
372 } else {
373 0
280 } 374 }
281} 375}
282 376
283sub update { 377sub update {
284 my ($self) = @_; 378 my ($self) = @_;
288} 382}
289 383
290sub connect { 384sub connect {
291 my ($self, $signal, $cb) = @_; 385 my ($self, $signal, $cb) = @_;
292 386
293 push @{ $self->{cb}{$signal} }, $cb; 387 push @{ $self->{signal_cb}{$signal} }, $cb;
294} 388}
295 389
296sub emit { 390sub emit {
297 my ($self, $signal, @args) = @_; 391 my ($self, $signal, @args) = @_;
298 392
393 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
299 $_->($self, @args) 394 $cb->($self, @args);
300 for @{$self->{cb}{$signal} || []}; 395 }
301} 396}
302 397
303sub DESTROY { 398sub DESTROY {
304 my ($self) = @_; 399 my ($self) = @_;
305 400
311package CFClient::UI::DrawBG; 406package CFClient::UI::DrawBG;
312 407
313our @ISA = CFClient::UI::Base::; 408our @ISA = CFClient::UI::Base::;
314 409
315use strict; 410use strict;
316use SDL::OpenGL; 411use CFClient::OpenGL;
317 412
318sub new { 413sub new {
319 my $class = shift; 414 my $class = shift;
320 415
321 # range [value, low, high, page] 416 # range [value, low, high, page]
350 445
351package CFClient::UI::Empty; 446package CFClient::UI::Empty;
352 447
353our @ISA = CFClient::UI::Base::; 448our @ISA = CFClient::UI::Base::;
354 449
450sub new {
451 my ($class, %arg) = @_;
452 $class->SUPER::new (can_events => 0, %arg);
453}
454
355sub size_request { 455sub size_request {
356 (0, 0) 456 (0, 0)
357} 457}
358 458
359sub draw { } 459sub draw { }
367sub new { 467sub new {
368 my ($class, %arg) = @_; 468 my ($class, %arg) = @_;
369 469
370 my $children = delete $arg{children} || []; 470 my $children = delete $arg{children} || [];
371 471
372 my $self = $class->SUPER::new (children => [], %arg); 472 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
373 $self->add ($_) for @$children; 473 $self->add ($_) for @$children;
374 474
375 $self 475 $self
376} 476}
377 477
385 $self->{children} = [ 485 $self->{children} = [
386 sort { $a->{z} <=> $b->{z} } 486 sort { $a->{z} <=> $b->{z} }
387 @{$self->{children}}, $child 487 @{$self->{children}}, $child
388 ]; 488 ];
389 489
390 $self->{w} = $self->{h} = -1;
391
392 $child->check_size; 490 $child->check_size;
393} 491}
394 492
395sub remove { 493sub remove {
396 my ($self, $widget) = @_; 494 my ($self, $child) = @_;
397 495
496 delete $child->{parent};
497
398 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 498 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
399 499
400 $self->check_size; 500 $self->check_size;
501 $self->update;
401} 502}
402 503
403sub find_widget { 504sub find_widget {
404 my ($self, $x, $y) = @_; 505 my ($self, $x, $y) = @_;
405 506
435 536
436 $class->SUPER::new (children => [$child], %arg) 537 $class->SUPER::new (children => [$child], %arg)
437} 538}
438 539
439sub add { 540sub add {
440 my ($self, $widget) = @_; 541 my ($self, $child) = @_;
441 542
442 $self->{children} = []; 543 $self->{children} = [];
443 544
444 $self->SUPER::add ($widget); 545 $self->SUPER::add ($child);
445} 546}
446 547
447sub remove { 548sub remove {
448 my ($self, $widget) = @_; 549 my ($self, $widget) = @_;
449 550
469 570
470package CFClient::UI::Window; 571package CFClient::UI::Window;
471 572
472our @ISA = CFClient::UI::Bin::; 573our @ISA = CFClient::UI::Bin::;
473 574
474use SDL::OpenGL; 575use CFClient::OpenGL;
475 576
476sub new { 577sub new {
477 my ($class, %arg) = @_; 578 my ($class, %arg) = @_;
478 579
479 my $self = $class->SUPER::new (%arg); 580 my $self = $class->SUPER::new (%arg);
533sub new { die } 634sub new { die }
534 635
535sub size_request { 636sub size_request {
536 my ($self) = @_; 637 my ($self) = @_;
537 638
538 @$self{qw(child_w child_h)} = $self->child->size_request; 639 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
539 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 640 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
540 641
541 @$self{qw(child_w child_h)} 642 @$self{qw(child_w child_h)}
542} 643}
543 644
552 653
553package CFClient::UI::Frame; 654package CFClient::UI::Frame;
554 655
555our @ISA = CFClient::UI::Bin::; 656our @ISA = CFClient::UI::Bin::;
556 657
557use SDL::OpenGL; 658use CFClient::OpenGL;
558
559sub size_request {
560 my ($self) = @_;
561 my $chld = $self->child
562 or return (0, 0);
563
564 $chld->move (2, 2);
565
566 map { $_ + 4 } $chld->size_request;
567}
568
569sub size_allocate {
570 my ($self, $x, $y, $w, $h) = @_;
571
572 $self->child->configure (2, 2, $w - 4, $h - 4);
573}
574
575sub _draw {
576 my ($self) = @_;
577
578 my $chld = $self->child;
579
580 my ($w, $h) = $chld->size_request;
581
582 glBegin GL_QUADS;
583 glColor 0, 0, 0;
584 glVertex 0 , 0;
585 glVertex 0 , $h + 4;
586 glVertex $w + 4 , $h + 4;
587 glVertex $w + 4 , 0;
588 glEnd;
589
590 $chld->draw;
591}
592
593#############################################################################
594
595package CFClient::UI::FancyFrame;
596
597our @ISA = CFClient::UI::Bin::;
598
599use SDL::OpenGL;
600
601my @tex =
602 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
603 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
604 659
605sub new { 660sub new {
606 my $class = shift; 661 my $class = shift;
607 662
608 # TODO: user_x, user_y, overwrite moveto?
609
610 $class->SUPER::new ( 663 my $self = $class->SUPER::new (
611 bg => [1, 1, 1, 1], 664 bg => [1, 1, 1, 1],
612 border_bg => [1, 1, 1, 1], 665 border_bg => [1, 1, 1, 1],
613 border => int $::FONTSIZE * 0.8, 666 border => 0.8,
614 @_ 667 @_
615 ) 668 );
669
670 $self
671}
672
673sub _draw {
674 my ($self) = @_;
675
676 my ($w, $h) = ($self->{w}, $self->{h});
677
678 glEnable GL_BLEND;
679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
680 glEnable GL_TEXTURE_2D;
681 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
682
683# glBegin GL_QUADS;
684# glColor 0, 0, 0, 0;
685# glVertex 0 , 0;
686# glVertex 0 , $h;
687# glVertex $w, $h;
688# glVertex $w, 0;
689# glEnd;
690
691
692 $self->child->draw;
693 glDisable GL_BLEND;
694 glDisable GL_TEXTURE_2D;
695}
696
697#############################################################################
698
699package CFClient::UI::FancyFrame;
700
701our @ISA = CFClient::UI::Bin::;
702
703use CFClient::OpenGL;
704
705my @tex =
706 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
707 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
708
709sub new {
710 my $class = shift;
711
712 # TODO: user_x, user_y, overwrite moveto?
713
714 my $self = $class->SUPER::new (
715 bg => [1, 1, 1, 1],
716 border_bg => [1, 1, 1, 1],
717 border => 0.8,
718 can_events => 1,
719 @_
720 );
721
722 $self->{title} &&= new CFClient::UI::Label
723 align => 0,
724 valign => 1,
725 text => $self->{title},
726 fontsize => 1;
727
728 $self
729}
730
731sub border {
732 int $_[0]{border} * $::FONTSIZE
616} 733}
617 734
618sub size_request { 735sub size_request {
619 my ($self) = @_; 736 my ($self) = @_;
620 737
621 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
622
623 my ($w, $h) = $self->SUPER::size_request; 738 my ($w, $h) = $self->SUPER::size_request;
624 739
625 ( 740 (
626 $w + $self->{border} * 2, 741 $w + $self->border * 2,
627 $h + $self->{border} * 2, 742 $h + $self->border * 2,
628 ) 743 )
629} 744}
630 745
631sub size_allocate { 746sub size_allocate {
632 my ($self, $w, $h) = @_; 747 my ($self, $w, $h) = @_;
633 748
634 $h -= List::Util::max 0, $self->{border} * 2; 749 $h -= List::Util::max 0, $self->border * 2;
635 $w -= List::Util::max 0, $self->{border} * 2; 750 $w -= List::Util::max 0, $self->border * 2;
636 751
752 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
753 if $self->{title};
754
637 $self->child->configure ($self->{border}, $self->{border}, $w, $h); 755 $self->child->configure ($self->border, $self->border, $w, $h);
638} 756}
639 757
640sub button_down { 758sub button_down {
641 my ($self, $ev, $x, $y) = @_; 759 my ($self, $ev, $x, $y) = @_;
642 760
761 my $border = $self->border;
762
643 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 763 if ($x < $self->{w} && $x >= $self->{w} - $border
644 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 764 && $y < $self->{h} && $y >= $self->{h} - $border) {
645 765
646 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 766 my ($ox, $oy) = ($ev->{x}, $ev->{y});
647 my ($bw, $bh) = ($self->{w}, $self->{h}); 767 my ($bw, $bh) = ($self->{w}, $self->{h});
648 768
649 $self->{motion} = sub { 769 $self->{motion} = sub {
650 my ($ev, $x, $y) = @_; 770 my ($ev, $x, $y) = @_;
651 771
652 ($x, $y) = ($ev->motion_x, $ev->motion_y); 772 ($x, $y) = ($ev->{x}, $ev->{y});
653 773
654 $self->{user_w} = $bw + $x - $ox; 774 $self->{user_w} = $bw + $x - $ox;
655 $self->{user_h} = $bh + $y - $oy; 775 $self->{user_h} = $bh + $y - $oy;
656 $self->check_size; 776 $self->check_size;
657 }; 777 };
658 778
659 } elsif ($x >= 0 && $x < $self->{w} 779 } elsif ($x >= 0 && $x < $self->{w}
660 && $y >= 0 && $y < $self->{border}) { 780 && $y >= 0 && $y < $border) {
661 781
662 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 782 my ($ox, $oy) = ($ev->{x}, $ev->{y});
663 my ($bx, $by) = ($self->{x}, $self->{y}); 783 my ($bx, $by) = ($self->{x}, $self->{y});
664 784
665 $self->{motion} = sub { 785 $self->{motion} = sub {
666 my ($ev, $x, $y) = @_; 786 my ($ev, $x, $y) = @_;
667 787
668 ($x, $y) = ($ev->motion_x, $ev->motion_y); 788 ($x, $y) = ($ev->{x}, $ev->{y});
669 789
670 $self->move ($bx + $x - $ox, $by + $y - $oy); 790 $self->move ($bx + $x - $ox, $by + $y - $oy);
671 $self->update; 791 $self->update;
672 }; 792 };
673 } 793 }
694 glEnable GL_BLEND; 814 glEnable GL_BLEND;
695 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 815 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
696 glEnable GL_TEXTURE_2D; 816 glEnable GL_TEXTURE_2D;
697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 817 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
698 818
819 my $border = $self->border;
820
699 glColor @{ $self->{border_bg} }; 821 glColor @{ $self->{border_bg} };
700 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 822 $tex[1]->draw_quad (0, 0, $w, $border);
701 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 823 $tex[3]->draw_quad (0, $border, $border, $ch);
702 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 824 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
703 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 825 $tex[4]->draw_quad (0, $h - $border, $w, $border);
704 826
705 my $bg = $tex[0]; 827 my $bg = $tex[0];
706 828
707 # TODO: repeat texture not scale 829 # TODO: repeat texture not scale
708 my $rep_x = $cw / $bg->{w}; 830 my $rep_x = $cw / $bg->{w};
711 glColor @{ $self->{bg} }; 833 glColor @{ $self->{bg} };
712 834
713 $bg->{s} = $rep_x; 835 $bg->{s} = $rep_x;
714 $bg->{t} = $rep_y; 836 $bg->{t} = $rep_y;
715 $bg->{wrap_mode} = 1; 837 $bg->{wrap_mode} = 1;
716 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 838 $bg->draw_quad ($border, $border, $cw, $ch);
717 839
718 glDisable GL_TEXTURE_2D; 840 glDisable GL_TEXTURE_2D;
719 glDisable GL_BLEND; 841 glDisable GL_BLEND;
720 842
843 $self->{title}->draw if $self->{title};
721 $self->child->draw; 844 $self->child->draw;
722} 845}
723 846
724############################################################################# 847#############################################################################
725 848
727 850
728our @ISA = CFClient::UI::Base::; 851our @ISA = CFClient::UI::Base::;
729 852
730use List::Util qw(max sum); 853use List::Util qw(max sum);
731 854
732use SDL::OpenGL; 855use CFClient::OpenGL;
733 856
734sub new { 857sub new {
735 my $class = shift; 858 my $class = shift;
736 859
737 $class->SUPER::new ( 860 $class->SUPER::new (
744 my ($self, $x, $y, $child) = @_; 867 my ($self, $x, $y, $child) = @_;
745 868
746 $child->set_parent ($self); 869 $child->set_parent ($self);
747 $self->{children}[$y][$x] = $child; 870 $self->{children}[$y][$x] = $child;
748 871
749 $self->{w} = $self->{h} = -1;
750
751 $child->check_size; 872 $child->check_size;
752} 873}
753 874
754# TODO: move to container class maybe? send childs a signal on removal? 875# TODO: move to container class maybe? send childs a signal on removal?
755sub clear { 876sub clear {
769 or next; 890 or next;
770 891
771 for my $x (0 .. $#$row) { 892 for my $x (0 .. $#$row) {
772 my $widget = $row->[$x] 893 my $widget = $row->[$x]
773 or next; 894 or next;
774 my ($w, $h) = $widget->size_request; 895 my ($w, $h) = @$widget{qw(req_w req_h)};
775 896
776 $w[$x] = max $w[$x], $w; 897 $w[$x] = max $w[$x], $w;
777 $h[$y] = max $h[$y], $h; 898 $h[$y] = max $h[$y], $h;
778 } 899 }
779 } 900 }
884 1005
885 ($h, $w) = ($w, $h); 1006 ($h, $w) = ($w, $h);
886 1007
887 my $children = $self->{children}; 1008 my $children = $self->{children};
888 1009
889 my @h = map +($_->size_request)[0], @$children; 1010 my @h = map $_->{req_w}, @$children;
890 1011
891 my $req_h = List::Util::sum @h; 1012 my $req_h = List::Util::sum @h;
892 1013
893 if ($req_h > $h) { 1014 if ($req_h > $h) {
894 # ah well, not enough space 1015 # ah well, not enough space
942sub size_allocate { 1063sub size_allocate {
943 my ($self, $w, $h) = @_; 1064 my ($self, $w, $h) = @_;
944 1065
945 my $children = $self->{children}; 1066 my $children = $self->{children};
946 1067
947 my @h = map +($_->size_request)[1], @$children; 1068 my @h = map $_->{req_h}, @$children;
948 1069
949 my $req_h = List::Util::sum @h; 1070 my $req_h = List::Util::sum @h;
950 1071
951 if ($req_h > $h) { 1072 if ($req_h > $h) {
952 # ah well, not enough space 1073 # ah well, not enough space
980 1101
981package CFClient::UI::Label; 1102package CFClient::UI::Label;
982 1103
983our @ISA = CFClient::UI::Base::; 1104our @ISA = CFClient::UI::Base::;
984 1105
985use SDL::OpenGL; 1106use CFClient::OpenGL;
986 1107
987sub new { 1108sub new {
988 my ($class, %arg) = @_; 1109 my ($class, %arg) = @_;
989 1110
990 my $self = $class->SUPER::new ( 1111 my $self = $class->SUPER::new (
991 fg => [1, 1, 1], 1112 fg => [1, 1, 1],
1113 #font => default_font
992 fontsize => $::FONTSIZE, 1114 fontsize => 1,
993 text => "", 1115 text => "",
994 align => -1, 1116 align => -1,
995 valign => -1, 1117 valign => -1,
996 padding => 2, 1118 padding => 2,
997 layout => new CFClient::Layout, 1119 layout => new CFClient::Layout,
1120 can_events => 0,
998 %arg 1121 %arg
999 ); 1122 );
1000 1123
1001 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1124 if (exists $self->{template}) {
1125 my $layout = new CFClient::Layout;
1126 $layout->set_text (delete $self->{template});
1127 $self->{template} = $layout;
1128 }
1002 1129
1003 $self->set_text (delete $self->{text}) if exists $self->{text}; 1130 $self->set_text (delete $self->{text}) if exists $self->{text};
1004 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1131 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1005 1132
1006 $self 1133 $self
1020 my ($self, $text) = @_; 1147 my ($self, $text) = @_;
1021 1148
1022 $self->{layout}->set_text ($text); 1149 $self->{layout}->set_text ($text);
1023 1150
1024 delete $self->{texture}; 1151 delete $self->{texture};
1152 $self->check_size;
1025 $self->update; 1153 $self->update;
1026} 1154}
1027 1155
1028sub set_markup { 1156sub set_markup {
1029 my ($self, $markup) = @_; 1157 my ($self, $markup) = @_;
1030 1158
1031 $self->{layout}->set_markup ($markup); 1159 $self->{layout}->set_markup ($markup);
1032 1160
1033 delete $self->{texture}; 1161 delete $self->{texture};
1162 $self->check_size;
1034 $self->update; 1163 $self->update;
1035} 1164}
1036 1165
1037sub size_request { 1166sub size_request {
1038 my ($self) = @_; 1167 my ($self) = @_;
1039 1168
1040 $self->{layout}->set_width; 1169 $self->{layout}->set_font ($self->{font}) if $self->{font};
1170 $self->{layout}->set_width ($self->{max_w} || -1);
1041 $self->{layout}->set_height ($self->{fontsize}); 1171 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1042 1172
1043 my ($w, $h) = $self->{layout}->size; 1173 my ($w, $h) = $self->{layout}->size;
1174
1175 if (exists $self->{template}) {
1176 $self->{template}->set_font ($self->{font}) if $self->{font};
1177 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1178
1179 my ($w2, $h2) = $self->{template}->size;
1180
1181 $w = List::Util::max $w, $w2;
1182 $h = List::Util::max $h, $h2;
1183 }
1044 1184
1045 ( 1185 (
1046 $w + $self->{padding} * 2, 1186 $w + $self->{padding} * 2,
1047 $h + $self->{padding} * 2, 1187 $h + $self->{padding} * 2,
1048 ) 1188 )
1052 my ($self, $w, $h) = @_; 1192 my ($self, $w, $h) = @_;
1053 1193
1054 delete $self->{texture}; 1194 delete $self->{texture};
1055} 1195}
1056 1196
1057sub update { 1197sub set_fontsize {
1058 my ($self) = @_; 1198 my ($self, $fontsize) = @_;
1059 1199
1200 $self->{fontsize} = $fontsize;
1060 delete $self->{texture}; 1201 delete $self->{texture};
1202 $self->check_size;
1061 $self->SUPER::update; 1203 $self->update;
1062} 1204}
1063 1205
1064sub _draw { 1206sub _draw {
1065 my ($self) = @_; 1207 my ($self) = @_;
1066 1208
1067 my $tex = $self->{texture} ||= do { 1209 my $tex = $self->{texture} ||= do {
1210 $self->{layout}->set_font ($self->{font}) if $self->{font};
1068 $self->{layout}->set_width ($self->{w}); 1211 $self->{layout}->set_width ($self->{w});
1069 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1212 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1070 new_from_layout CFClient::Texture $self->{layout} 1213 new_from_layout CFClient::Texture $self->{layout}
1071 }; 1214 };
1072 1215
1073 glEnable GL_BLEND; 1216 glEnable GL_BLEND;
1074 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1217 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1099 1242
1100package CFClient::UI::EntryBase; 1243package CFClient::UI::EntryBase;
1101 1244
1102our @ISA = CFClient::UI::Label::; 1245our @ISA = CFClient::UI::Label::;
1103 1246
1104use SDL; 1247use CFClient::OpenGL;
1105use SDL::OpenGL;
1106 1248
1107sub new { 1249sub new {
1108 my $class = shift; 1250 my $class = shift;
1109 1251
1110 $class->SUPER::new ( 1252 $class->SUPER::new (
1113 active_bg => [1, 1, 1, 0.5], 1255 active_bg => [1, 1, 1, 0.5],
1114 active_fg => [0, 0, 0], 1256 active_fg => [0, 0, 0],
1115 can_hover => 1, 1257 can_hover => 1,
1116 can_focus => 1, 1258 can_focus => 1,
1117 valign => 0, 1259 valign => 0,
1260 can_events => 1,
1118 @_ 1261 @_
1119 ) 1262 )
1120} 1263}
1121 1264
1122sub _set_text { 1265sub _set_text {
1124 1267
1125 delete $self->{cur_h}; 1268 delete $self->{cur_h};
1126 1269
1127 return if $self->{text} eq $text; 1270 return if $self->{text} eq $text;
1128 1271
1272 delete $self->{texture};
1273
1129 $self->{last_activity} = $::NOW; 1274 $self->{last_activity} = $::NOW;
1130 $self->{text} = $text; 1275 $self->{text} = $text;
1131 1276
1132 $text =~ s/./*/g if $self->{hidden}; 1277 $text =~ s/./*/g if $self->{hidden};
1133 $self->{layout}->set_text ("$text "); 1278 $self->{layout}->set_text ("$text ");
1148} 1293}
1149 1294
1150sub size_allocate { 1295sub size_allocate {
1151 my ($self, $w, $h) = @_; 1296 my ($self, $w, $h) = @_;
1152 1297
1153 $self->_set_text ($self->{text}); 1298 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1154} 1299}
1155 1300
1156sub set_text { 1301sub set_text {
1157 my ($self, $text) = @_; 1302 my ($self, $text) = @_;
1158 1303
1162} 1307}
1163 1308
1164sub key_down { 1309sub key_down {
1165 my ($self, $ev) = @_; 1310 my ($self, $ev) = @_;
1166 1311
1167 my $mod = $ev->key_mod; 1312 my $mod = $ev->{mod};
1168 my $sym = $ev->key_sym; 1313 my $sym = $ev->{sym};
1169
1170 my $uni = $ev->key_unicode; 1314 my $uni = $ev->{unicode};
1171 1315
1172 my $text = $self->get_text; 1316 my $text = $self->get_text;
1173 1317
1174 if ($sym == SDLK_BACKSPACE) { 1318 if ($sym == 8) {
1175 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1319 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1176 } elsif ($sym == SDLK_DELETE) { 1320 } elsif ($sym == 127) {
1177 substr $text, $self->{cursor}, 1, ""; 1321 substr $text, $self->{cursor}, 1, "";
1178 } elsif ($sym == SDLK_LEFT) { 1322 } elsif ($sym == CFClient::SDLK_LEFT) {
1179 --$self->{cursor} if $self->{cursor}; 1323 --$self->{cursor} if $self->{cursor};
1180 } elsif ($sym == SDLK_RIGHT) { 1324 } elsif ($sym == CFClient::SDLK_RIGHT) {
1181 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1325 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1182 } elsif ($sym == SDLK_HOME) { 1326 } elsif ($sym == CFClient::SDLK_HOME) {
1183 $self->{cursor} = 0; 1327 $self->{cursor} = 0;
1184 } elsif ($sym == SDLK_END) { 1328 } elsif ($sym == CFClient::SDLK_END) {
1185 $self->{cursor} = length $text; 1329 $self->{cursor} = length $text;
1186 } elsif ($sym == SDLK_ESCAPE) { 1330 } elsif ($sym == 27) {
1187 $self->emit ('escape'); 1331 $self->emit ('escape');
1188 } elsif ($uni) { 1332 } elsif ($uni) {
1189 substr $text, $self->{cursor}++, 0, chr $uni; 1333 substr $text, $self->{cursor}++, 0, chr $uni;
1190 } 1334 }
1191 1335
1266 1410
1267package CFClient::UI::Entry; 1411package CFClient::UI::Entry;
1268 1412
1269our @ISA = CFClient::UI::EntryBase::; 1413our @ISA = CFClient::UI::EntryBase::;
1270 1414
1271use SDL; 1415use CFClient::OpenGL;
1272use SDL::OpenGL;
1273 1416
1274sub key_down { 1417sub key_down {
1275 my ($self, $ev) = @_; 1418 my ($self, $ev) = @_;
1276 1419
1277 my $sym = $ev->key_sym; 1420 my $sym = $ev->{sym};
1278 1421
1279 if ($sym == SDLK_RETURN) { 1422 if ($sym == 13) {
1280 $self->emit (activate => $self->get_text); 1423 $self->emit (activate => $self->get_text);
1281 $self->update; 1424 $self->update;
1282 1425
1283 } else { 1426 } else {
1284 $self->SUPER::key_down ($ev); 1427 $self->SUPER::key_down ($ev);
1290 1433
1291package CFClient::UI::Button; 1434package CFClient::UI::Button;
1292 1435
1293our @ISA = CFClient::UI::Label::; 1436our @ISA = CFClient::UI::Label::;
1294 1437
1295use SDL; 1438use CFClient::OpenGL;
1296use SDL::OpenGL;
1297 1439
1298my @tex = 1440my @tex =
1299 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1441 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1300 qw(b1_button_active.png); 1442 qw(b1_button_active.png);
1301 1443
1302sub new { 1444sub new {
1303 my $class = shift; 1445 my $class = shift;
1304 1446
1308 bg => [1, 1, 1, 0.2], 1450 bg => [1, 1, 1, 0.2],
1309 active_fg => [0, 0, 1], 1451 active_fg => [0, 0, 1],
1310 can_hover => 1, 1452 can_hover => 1,
1311 align => 0, 1453 align => 0,
1312 valign => 0, 1454 valign => 0,
1455 can_events => 1,
1313 @_ 1456 @_
1314 ) 1457 )
1315} 1458}
1316 1459
1317sub button_up { 1460sub button_up {
1351package CFClient::UI::CheckBox; 1494package CFClient::UI::CheckBox;
1352 1495
1353our @ISA = CFClient::UI::DrawBG::; 1496our @ISA = CFClient::UI::DrawBG::;
1354 1497
1355my @tex = 1498my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1499 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1357 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1500 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1358 1501
1359use SDL; 1502use CFClient::OpenGL;
1360use SDL::OpenGL;
1361 1503
1362sub new { 1504sub new {
1363 my $class = shift; 1505 my $class = shift;
1364 1506
1365 $class->SUPER::new ( 1507 $class->SUPER::new (
1411 glDisable GL_BLEND; 1553 glDisable GL_BLEND;
1412} 1554}
1413 1555
1414############################################################################# 1556#############################################################################
1415 1557
1416package CFClient::UI::VGauge; 1558package CFClient::UI::Image;
1417 1559
1418our @ISA = CFClient::UI::Base::; 1560our @ISA = CFClient::UI::Base::;
1419 1561
1420use SDL::OpenGL; 1562use CFClient::OpenGL;
1563use Carp qw/confess/;
1421 1564
1422my %tex = ( 1565our %loaded_images;
1423 food => [
1424 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1425 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1426 ],
1427 grace => [
1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1429 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1430 ],
1431 hp => [
1432 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1433 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1434 ],
1435 mana => [
1436 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1437 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1438 ],
1439);
1440 1566
1441# eg. VGauge->new (gauge => 'food'), default gauge: food
1442sub new { 1567sub new {
1443 my $class = shift; 1568 my $class = shift;
1444 1569
1445 my $self = $class->SUPER::new (gauge => 'food', @_); 1570 my $self = $class->SUPER::new (can_events => 0, @_);
1571
1572 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1573
1574 $loaded_images{$self->{image}} ||=
1575 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1576
1577 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1578
1579 Scalar::Util::weaken $loaded_images{$self->{image}};
1580
1581 $self->{aspect} = $tex->{w} / $tex->{h};
1446 1582
1447 $self 1583 $self
1448} 1584}
1449 1585
1450sub size_request { 1586sub size_request {
1451 my ($self) = @_; 1587 my ($self) = @_;
1452 1588
1453 my $tex = $tex{$self->{gauge}}[0]; 1589 ($self->{tex}->{w}, $self->{tex}->{h})
1454
1455 @$tex{qw(w h)}
1456}
1457
1458sub set_max {
1459 my ($self, $max) = @_;
1460
1461 $self->{max_val} = $max;
1462}
1463
1464sub set_value {
1465 my ($self, $val, $max) = @_;
1466
1467 $self->set_max ($max)
1468 if defined $max;
1469
1470 $max = $self->{max_val};
1471 $self->{val} = $val;
1472
1473 $self->update;
1474} 1590}
1475 1591
1476sub _draw { 1592sub _draw {
1477 my ($self) = @_; 1593 my ($self) = @_;
1478 1594
1479 my $tex = $tex{$self->{gauge}}; 1595 my $tex = $self->{tex};
1480 1596
1481 my ($w, $h) = ($self->{w}, $self->{h}); 1597 my ($w, $h) = ($self->{w}, $self->{h});
1482 1598
1483 my $ycut = $self->{val} / ($self->{max_val} || 1); 1599 if ($self->{rot90}) {
1484 $ycut = 1 if $self->{val} > $self->{max_val}; 1600 glRotate 90, 0, 0, 1;
1601 glTranslate 0, -$self->{w}, 0;
1485 1602
1486 my $t1 = $tex->[0]; 1603 ($w, $h) = ($h, $w);
1487 my $t2 = $tex->[1]; 1604 }
1488 1605
1489 glEnable GL_BLEND; 1606 glEnable GL_BLEND;
1490 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1607 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1491 glEnable GL_TEXTURE_2D; 1608 glEnable GL_TEXTURE_2D;
1492 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1609 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1493 1610
1494 my $h1 = $self->{h} - $ycut * $self->{h}; 1611 $tex->draw_quad (0, 0, $w, $h);
1495 my $h2 = $ycut * $self->{h};
1496
1497 my $yp = 0;
1498
1499 glBindTexture (GL_TEXTURE_2D, $t1->{name});
1500 glBegin (GL_QUADS);
1501 glTexCoord (0, 0); glVertex (0 , $yp);
1502 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1);
1503 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1);
1504 glTexCoord (1, 0); glVertex (0 + $w, $yp);
1505 glEnd ();
1506
1507 $yp += $h1;
1508
1509 glBindTexture (GL_TEXTURE_2D, $t2->{name});
1510 glBegin (GL_QUADS);
1511 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp);
1512 glTexCoord (0, 1); glVertex (0 , $yp + $h2);
1513 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2);
1514 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp);
1515 glEnd ();
1516 1612
1517 glDisable GL_BLEND; 1613 glDisable GL_BLEND;
1518 glDisable GL_TEXTURE_2D; 1614 glDisable GL_TEXTURE_2D;
1519} 1615}
1520 1616
1521############################################################################# 1617#############################################################################
1522 1618
1619package CFClient::UI::VGauge;
1620
1621our @ISA = CFClient::UI::Base::;
1622
1623use List::Util qw(min max);
1624
1625use CFClient::OpenGL;
1626
1627my %tex = (
1628 food => [
1629 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1630 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1631 ],
1632 grace => [
1633 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1634 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1635 ],
1636 hp => [
1637 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1638 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1639 ],
1640 mana => [
1641 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1642 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1643 ],
1644);
1645
1646# eg. VGauge->new (gauge => 'food'), default gauge: food
1647sub new {
1648 my $class = shift;
1649
1650 my $self = $class->SUPER::new (
1651 type => 'food',
1652 @_
1653 );
1654
1655 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1656
1657 $self
1658}
1659
1660sub size_request {
1661 my ($self) = @_;
1662
1663 #my $tex = $tex{$self->{type}}[0];
1664 #@$tex{qw(w h)}
1665 (0, 0)
1666}
1667
1668sub set_max {
1669 my ($self, $max) = @_;
1670
1671 $self->{max_val} = $max;
1672}
1673
1674sub set_value {
1675 my ($self, $val, $max) = @_;
1676
1677 $self->set_max ($max)
1678 if defined $max;
1679
1680 $max = $self->{max_val};
1681 $self->{val} = $val;
1682
1683 $self->update;
1684}
1685
1686sub _draw {
1687 my ($self) = @_;
1688
1689 my $tex = $tex{$self->{type}};
1690 my ($t1, $t2, $t3) = @$tex;
1691
1692 my ($w, $h) = ($self->{w}, $self->{h});
1693
1694 if ($self->{vertical}) {
1695 glRotate 90, 0, 0, 1;
1696 glTranslate 0, -$self->{w}, 0;
1697
1698 ($w, $h) = ($h, $w);
1699 }
1700
1701 my $ycut = $self->{val} / ($self->{max_val} || 1);
1702
1703 my $ycut1 = max 0, min 1, $ycut;
1704 my $ycut2 = max 0, min 1, $ycut - 1;
1705
1706 my $h1 = $self->{h} * (1 - $ycut1);
1707 my $h2 = $self->{h} * (1 - $ycut2);
1708
1709 glEnable GL_BLEND;
1710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1711 glEnable GL_TEXTURE_2D;
1712 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1713
1714 glBindTexture GL_TEXTURE_2D, $t1->{name};
1715 glBegin GL_QUADS;
1716 glTexCoord 0 , 0; glVertex 0 , 0;
1717 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1718 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1719 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1720 glEnd;
1721
1722 my $ycut1 = List::Util::min 1, $ycut;
1723 glBindTexture GL_TEXTURE_2D, $t2->{name};
1724 glBegin GL_QUADS;
1725 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1726 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1727 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1728 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1729 glEnd;
1730
1731 if ($t3) {
1732 glBindTexture GL_TEXTURE_2D, $t3->{name};
1733 glBegin GL_QUADS;
1734 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1735 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1736 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1737 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1738 glEnd;
1739 }
1740
1741 glDisable GL_BLEND;
1742 glDisable GL_TEXTURE_2D;
1743}
1744
1745#############################################################################
1746
1747package CFClient::UI::Gauge;
1748
1749our @ISA = CFClient::UI::VBox::;
1750
1751sub new {
1752 my ($class, %arg) = @_;
1753
1754 my $self = $class->SUPER::new (
1755 tooltip => $arg{type},
1756 %arg,
1757 );
1758
1759 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1);
1760 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1761 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1762
1763 $self
1764}
1765
1766sub set_fontsize {
1767 my ($self, $fsize) = @_;
1768
1769 $self->{value}->set_fontsize ($fsize);
1770 $self->{max} ->set_fontsize ($fsize);
1771}
1772
1773sub set_value {
1774 my ($self, $val, $max) = @_;
1775
1776 $self->set_max ($max)
1777 if defined $max;
1778
1779 $self->{gauge}->set_value ($val, $max);
1780 $self->{value}->set_text ($val);
1781}
1782
1783sub set_max {
1784 my ($self, $max) = @_;
1785
1786 $self->{gauge}->set_max ($max);
1787 $self->{max}->set_text ($max);
1788}
1789
1790#############################################################################
1791
1523package CFClient::UI::Slider; 1792package CFClient::UI::Slider;
1524 1793
1525use strict; 1794use strict;
1526 1795
1527use SDL::OpenGL; 1796use CFClient::OpenGL;
1528 1797
1529our @ISA = CFClient::UI::DrawBG::; 1798our @ISA = CFClient::UI::DrawBG::;
1530 1799
1531my @tex = 1800my @tex =
1532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1801 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1542 # TODO: calculations are off 1811 # TODO: calculations are off
1543 my $self = $class->SUPER::new ( 1812 my $self = $class->SUPER::new (
1544 fg => [1, 1, 1], 1813 fg => [1, 1, 1],
1545 active_fg => [0, 0, 0], 1814 active_fg => [0, 0, 0],
1546 range => [0, 0, 100, 10], 1815 range => [0, 0, 100, 10],
1547 req_w => 40, 1816 req_w => 20,
1548 req_h => 13, 1817 req_h => 20,
1549 vertical => 0, 1818 vertical => 0,
1550 can_hover => 1, 1819 can_hover => 1,
1551 inner_pad => 5, 1820 inner_pad => 5,
1552 @_ 1821 @_
1553 ); 1822 );
1657 1926
1658package CFClient::UI::TextView; 1927package CFClient::UI::TextView;
1659 1928
1660our @ISA = CFClient::UI::HBox::; 1929our @ISA = CFClient::UI::HBox::;
1661 1930
1662use SDL::OpenGL; 1931use CFClient::OpenGL;
1663 1932
1664sub new { 1933sub new {
1665 my $class = shift; 1934 my $class = shift;
1666 1935
1667 my $self = $class->SUPER::new ( 1936 my $self = $class->SUPER::new (
1668 req_w => $::WIDTH / 6,
1669 req_h => $::HEIGHT / 6,
1670 fontsize => $::FONTSIZE, 1937 fontsize => 1,
1938 #font => default_font
1671 @_, 1939 @_,
1672 1940
1673 layout => (new CFClient::Layout), 1941 layout => (new CFClient::Layout),
1674 par => [], 1942 par => [],
1675 height => 0, 1943 height => 0,
1696sub text_height { 1964sub text_height {
1697 my ($self, $text) = @_; 1965 my ($self, $text) = @_;
1698 1966
1699 my $layout = $self->{layout}; 1967 my $layout = $self->{layout};
1700 1968
1701 $layout->set_height ($self->{fontsize}); 1969 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1702 $layout->set_width ($self->{w}); 1970 $layout->set_width ($self->{w});
1703 $layout->set_text ($text); 1971 $layout->set_text ($text);
1704 1972
1705 ($layout->size)[1] 1973 ($layout->size)[1]
1706} 1974}
1710 1978
1711 $self->{need_reflow}++; 1979 $self->{need_reflow}++;
1712 $self->update; 1980 $self->update;
1713} 1981}
1714 1982
1715sub size_request {
1716 my ($self) = @_;
1717
1718 ($self->{req_w}, $self->{req_h})
1719}
1720
1721sub size_allocate { 1983sub size_allocate {
1722 my ($self, $w, $h) = @_; 1984 my ($self, $w, $h) = @_;
1723 1985
1986 $self->SUPER::size_allocate ($w, $h);
1987
1988 $self->{layout}->set_font ($self->{font}) if $self->{font};
1724 $self->{layout}->set_height ($self->{fontsize}); 1989 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1725 $self->{layout}->set_width ($self->{w}); 1990 $self->{layout}->set_width ($self->{children}[0]{w});
1726 1991
1727 $self->reflow; 1992 $self->reflow;
1728 $self->update;
1729} 1993}
1730 1994
1731sub add_paragraph { 1995sub add_paragraph {
1732 my ($self, $color, $text) = @_; 1996 my ($self, $color, $text) = @_;
1733 1997
1781 my $y1 = $top + $self->{h}; 2045 my $y1 = $top + $self->{h};
1782 2046
1783 my $y = 0; 2047 my $y = 0;
1784 2048
1785 my $layout = $self->{layout}; 2049 my $layout = $self->{layout};
2050
2051 $layout->set_font ($self->{font}) if $self->{font};
1786 2052
1787 for my $par (@{$self->{par}}) { 2053 for my $par (@{$self->{par}}) {
1788 my $h = $par->[0]; 2054 my $h = $par->[0];
1789 2055
1790 if ($y0 < $y + $h && $y < $y1) { 2056 if ($y0 < $y + $h && $y < $y1) {
1818 2084
1819} 2085}
1820 2086
1821############################################################################# 2087#############################################################################
1822 2088
1823package CFClient::UI::MapWidget;
1824
1825use strict;
1826
1827use List::Util qw(min max);
1828
1829use SDL;
1830use SDL::OpenGL;
1831
1832our @ISA = CFClient::UI::Base::;
1833
1834sub new {
1835 my $class = shift;
1836
1837 $class->SUPER::new (
1838 z => -1,
1839 can_focus => 1,
1840 list => (glGenLists 1),
1841 @_
1842 )
1843}
1844
1845sub key_down {
1846 print "MAPKEYDOWN\n";
1847}
1848
1849sub key_up {
1850}
1851
1852sub button_down {
1853 my ($self, $ev, $x, $y) = @_;
1854
1855 $self->focus_in;
1856
1857 if ($ev->button == 2) {
1858 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1859 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1860
1861 $self->{motion} = sub {
1862 my ($ev, $x, $y) = @_;
1863
1864 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1865
1866 $::CFG->{map_shift_x} = $bw + $x - $ox;
1867 $::CFG->{map_shift_y} = $bh + $y - $oy;
1868
1869 $self->update;
1870 };
1871 }
1872}
1873
1874sub button_up {
1875 my ($self, $ev, $x, $y) = @_;
1876
1877 delete $self->{motion};
1878}
1879
1880sub mouse_motion {
1881 my ($self, $ev, $x, $y) = @_;
1882
1883 $self->{motion}->($ev, $x, $y) if $self->{motion};
1884}
1885
1886sub size_request {
1887 (
1888 1 + 32 * int $::WIDTH / 32,
1889 1 + 32 * int $::HEIGHT / 32,
1890 )
1891}
1892
1893sub update {
1894 my ($self) = @_;
1895
1896 $self->{need_update} = 1;
1897 $self->SUPER::update;
1898}
1899
1900sub draw {
1901 my ($self) = @_;
1902
1903 if (delete $self->{need_update}) {
1904 glNewList $self->{list}, GL_COMPILE;
1905
1906 if ($::MAP) {
1907 my $sw = int $::WIDTH / 32;
1908 my $sh = int $::HEIGHT / 32;
1909
1910 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1911 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1912
1913 glTranslate $sx0 - 32, $sy0 - 32, 0;
1914
1915 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1916
1917 if ($::CFG->{fow_enable}) {
1918 if ($::CFG->{fow_smooth}) { # smooth fog of war
1919 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
1920 glConvolutionFilter2D (
1921 GL_CONVOLUTION_2D,
1922 GL_ALPHA,
1923 3, 3,
1924 GL_ALPHA, GL_FLOAT,
1925 pack "f*",
1926 0.1, 0.1, 0.1,
1927 0.1, 0.2, 0.1,
1928 0.1, 0.1, 0.1,
1929 );
1930 glEnable GL_CONVOLUTION_2D;
1931 }
1932
1933 $self->{fow_texture} = new CFClient::Texture
1934 w => $w,
1935 h => $h,
1936 data => $data,
1937 internalformat => GL_ALPHA,
1938 format => GL_ALPHA;
1939
1940 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1941
1942 glEnable GL_BLEND;
1943 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1944 glEnable GL_TEXTURE_2D;
1945 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1946
1947 glColor +($::CFG->{fow_intensity}) x 3, 1;
1948 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1949
1950 glDisable GL_TEXTURE_2D;
1951 glDisable GL_BLEND;
1952 }
1953
1954 # HACK BEGIN
1955 {
1956 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1957 my ($w, $h) = (250, 250);
1958
1959 glEnable GL_BLEND;
1960 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1961 glEnable GL_TEXTURE_2D;
1962 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1963
1964 $self->{mapmap_texture} =
1965 new CFClient::Texture
1966 w => $w,
1967 h => $h,
1968 data => $::MAP->mapmap ($w, $h),
1969 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
1970
1971 $self->{mapmap_texture}->draw_quad (100, 100);
1972
1973 glDisable GL_TEXTURE_2D;
1974 glDisable GL_BLEND;
1975 }
1976 # HACK END
1977 }
1978
1979 glEndList;
1980 }
1981
1982 glPushMatrix;
1983 glCallList $self->{list};
1984 glPopMatrix;
1985
1986 if ($FOCUS != $self) {
1987 glColor 64/255, 64/255, 64/255;
1988 glLogicOp GL_AND;
1989 glEnable GL_COLOR_LOGIC_OP;
1990 glBegin GL_QUADS;
1991 glVertex 0, 0;
1992 glVertex 0, $::HEIGHT;
1993 glVertex $::WIDTH, $::HEIGHT;
1994 glVertex $::WIDTH, 0;
1995 glEnd;
1996 glDisable GL_COLOR_LOGIC_OP;
1997 }
1998}
1999
2000my %DIR = (
2001 SDLK_KP8, [1, "north"],
2002 SDLK_KP9, [2, "northeast"],
2003 SDLK_KP6, [3, "east"],
2004 SDLK_KP3, [4, "southeast"],
2005 SDLK_KP2, [5, "south"],
2006 SDLK_KP1, [6, "southwest"],
2007 SDLK_KP4, [7, "west"],
2008 SDLK_KP7, [8, "northwest"],
2009
2010 SDLK_UP, [1, "north"],
2011 SDLK_RIGHT, [3, "east"],
2012 SDLK_DOWN, [5, "south"],
2013 SDLK_LEFT, [7, "west"],
2014);
2015
2016sub key_down {
2017 my ($self, $ev) = @_;
2018
2019 my $mod = $ev->key_mod;
2020 my $sym = $ev->key_sym;
2021
2022 if ($sym == SDLK_KP5) {
2023 $::CONN->user_send ("stay fire");
2024 } elsif ($sym == SDLK_a) {
2025 $::CONN->user_send ("apply");
2026 } elsif ($sym == SDLK_QUOTE) {
2027 $self->emit ('activate_console');
2028 } elsif ($sym == SDLK_SLASH) {
2029 $self->emit ('activate_console' => '/');
2030 } elsif (exists $DIR{$sym}) {
2031 if ($mod & KMOD_SHIFT) {
2032 $self->{shft}++;
2033 $::CONN->user_send ("fire $DIR{$sym}[0]");
2034 } elsif ($mod & KMOD_CTRL) {
2035 $self->{ctrl}++;
2036 $::CONN->user_send ("run $DIR{$sym}[0]");
2037 } else {
2038 $::CONN->user_send ("$DIR{$sym}[1]");
2039 }
2040 }
2041}
2042
2043sub key_up {
2044 my ($self, $ev) = @_;
2045
2046 my $mod = $ev->key_mod;
2047 my $sym = $ev->key_sym;
2048
2049 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2050 $::CONN->user_send ("fire_stop");
2051 }
2052 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2053 $::CONN->user_send ("run_stop");
2054 }
2055}
2056
2057#############################################################################
2058
2059package CFClient::UI::Animator; 2089package CFClient::UI::Animator;
2060 2090
2061use SDL::OpenGL; 2091use CFClient::OpenGL;
2062 2092
2063our @ISA = CFClient::UI::Bin::; 2093our @ISA = CFClient::UI::Bin::;
2064 2094
2065sub moveto { 2095sub moveto {
2066 my ($self, $x, $y) = @_; 2096 my ($self, $x, $y) = @_;
2106 my $class = shift; 2136 my $class = shift;
2107 2137
2108 my $self = $class->SUPER::new ( 2138 my $self = $class->SUPER::new (
2109 state => 0, 2139 state => 0,
2110 connect_activate => \&toggle_flopper, 2140 connect_activate => \&toggle_flopper,
2141 can_events => 1,
2111 @_ 2142 @_
2112 ); 2143 );
2113 2144
2114 if ($self->{state}) { 2145 if ($self->{state}) {
2115 $self->{state} = 0; 2146 $self->{state} = 0;
2135 $self->emit (changed => $self->{state}); 2166 $self->emit (changed => $self->{state});
2136} 2167}
2137 2168
2138############################################################################# 2169#############################################################################
2139 2170
2171package CFClient::UI::Tooltip;
2172
2173our @ISA = CFClient::UI::Bin::;
2174
2175use CFClient::OpenGL;
2176
2177sub new {
2178 my $class = shift;
2179
2180 $class->SUPER::new (
2181 @_,
2182 can_events => 0,
2183 )
2184}
2185
2186sub set_text {
2187 my ($self, $text) = @_;
2188
2189 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2190 $self->{label}->set_text ($text);
2191 $self->add ($self->{label});
2192}
2193
2194sub size_request {
2195 my ($self) = @_;
2196
2197 $self->child->set_max_size ($::WIDTH * 0.3);
2198
2199 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2200
2201 ($w + 4, $h + 4)
2202}
2203
2204sub _draw {
2205 my ($self) = @_;
2206
2207 glPushMatrix;
2208 glTranslate 0.375, 0.375;
2209
2210 my ($w, $h) = @$self{qw(w h)};
2211
2212 glColor 1, 0.8, 0.4;
2213 glBegin GL_QUADS;
2214 glVertex 0 , 0;
2215 glVertex 0 , $h;
2216 glVertex $w, $h;
2217 glVertex $w, 0;
2218 glEnd;
2219
2220 glColor 0, 0, 0;
2221 glBegin GL_LINE_LOOP;
2222 glVertex 0 , 0;
2223 glVertex 0 , $h;
2224 glVertex $w, $h;
2225 glVertex $w, 0;
2226 glEnd;
2227
2228 glPopMatrix;
2229
2230 glTranslate 2, 2;
2231 $self->SUPER::_draw;
2232}
2233
2234#############################################################################
2235
2140package CFClient::UI::Root; 2236package CFClient::UI::Root;
2141 2237
2142our @ISA = CFClient::UI::Container::; 2238our @ISA = CFClient::UI::Container::;
2143 2239
2144use SDL::OpenGL; 2240use CFClient::OpenGL;
2145 2241
2146sub check_size { 2242sub check_size {
2147 my ($self) = @_; 2243 my ($self) = @_;
2148 2244
2149 $self->configure (0, 0, $::WITH, $::HEIGHT); 2245 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2150} 2246}
2151 2247
2152sub size_request { 2248sub size_request {
2153 ($::WIDTH, $::HEIGHT) 2249 ($::WIDTH, $::HEIGHT)
2154} 2250}
2156sub configure { 2252sub configure {
2157 my ($self, $x, $y, $w, $h) = @_; 2253 my ($self, $x, $y, $w, $h) = @_;
2158 2254
2159 $self->SUPER::configure ($x, $y, $w, $h); 2255 $self->SUPER::configure ($x, $y, $w, $h);
2160 2256
2161 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2162 for @{$self->{children}}; 2257 for my $child (@{$self->{children}}) {
2258 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2259
2260 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2261 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2262 $child->configure ($X, $Y, $W,$H);
2263 }
2163} 2264}
2164 2265
2165sub _topleft { 2266sub _topleft {
2166 my ($self, $x, $y) = @_; 2267 my ($self, $x, $y) = @_;
2167 2268
2175 ::refresh (); 2276 ::refresh ();
2176} 2277}
2177 2278
2178sub add { 2279sub add {
2179 my ($self, $child) = @_; 2280 my ($self, $child) = @_;
2281
2282 # integerize window positions
2283 $child->{x} = int $child->{x};
2284 $child->{y} = int $child->{y};
2180 2285
2181 $self->SUPER::add ($child); 2286 $self->SUPER::add ($child);
2182} 2287}
2183 2288
2184sub on_refresh { 2289sub on_refresh {
2210############################################################################# 2315#############################################################################
2211 2316
2212package CFClient::UI; 2317package CFClient::UI;
2213 2318
2214$ROOT = new CFClient::UI::Root; 2319$ROOT = new CFClient::UI::Root;
2320$TOOLTIP = new CFClient::UI::Tooltip;
2215 2321
22161 23221
2217 2323

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines