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.156 by elmex, Sun Apr 23 02:50:42 2006 UTC vs.
Revision 1.202 by root, Sun May 14 20:51:19 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use utf8;
3use strict; 4use strict;
4 5
5use Scalar::Util (); 6use Scalar::Util ();
6use List::Util (); 7use List::Util ();
7 8
11 12
12our $ROOT; 13our $ROOT;
13our $TOOLTIP; 14our $TOOLTIP;
14our $BUTTON_STATE; 15our $BUTTON_STATE;
15 16
17our %WIDGET; # all widgets, weak-referenced
18
16sub check_tooltip { 19sub check_tooltip {
17 if (!$GRAB) { 20 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) { 22 if (length $widget->{tooltip}) {
20 23
21 if ($TOOLTIP->{owner} != $widget) { 24 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget; 25 $TOOLTIP->{owner} = $widget;
23 26
24 my $tip = $widget->{tooltip}; 27 my $tip = $widget->{tooltip};
25 28
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 29 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 30
28 $TOOLTIP->set_text ($widget->{tooltip}); 31 $TOOLTIP->set_tooltip_from ($widget);
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show; 32 $TOOLTIP->show;
33
34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
35
36 if ($x + $TOOLTIP->{w} > $::WIDTH) {
37 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
38 }
39
40 $TOOLTIP->move ($x, $y);
41 $TOOLTIP->check_size;
42 $TOOLTIP->update;
31 } 43 }
32 44
33 return; 45 return;
34 } 46 }
35 } 47 }
39 delete $TOOLTIP->{owner}; 51 delete $TOOLTIP->{owner};
40} 52}
41 53
42# class methods for events 54# class methods for events
43sub feed_sdl_key_down_event { 55sub feed_sdl_key_down_event {
44 $FOCUS->key_down ($_[0]) if $FOCUS; 56 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
57 if $FOCUS;
45} 58}
46 59
47sub feed_sdl_key_up_event { 60sub feed_sdl_key_up_event {
48 $FOCUS->key_up ($_[0]) if $FOCUS; 61 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
62 if $FOCUS;
49} 63}
50 64
51sub feed_sdl_button_down_event { 65sub feed_sdl_button_down_event {
52 my ($ev) = @_; 66 my ($ev) = @_;
53 my ($x, $y) = ($ev->{x}, $ev->{y}); 67 my ($x, $y) = ($ev->{x}, $ev->{y});
61 check_tooltip; 75 check_tooltip;
62 } 76 }
63 77
64 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 78 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
65 79
66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 80 if ($GRAB) {
81 ($x, $y) = $GRAB->coord2local ($x, $y);
82 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
83 }
67} 84}
68 85
69sub feed_sdl_button_up_event { 86sub feed_sdl_button_up_event {
70 my ($ev) = @_; 87 my ($ev) = @_;
71 my ($x, $y) = ($ev->{x}, $ev->{y}); 88 my ($x, $y) = ($ev->{x}, $ev->{y});
72 89
73 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 90 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
74 91
75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 92 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
76 93
77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 94 if ($GRAB) {
95 ($x, $y) = $GRAB->coord2local ($x, $y);
96 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
97 }
78 98
79 if (!$BUTTON_STATE) { 99 if (!$BUTTON_STATE) {
80 my $grab = $GRAB; undef $GRAB; 100 my $grab = $GRAB; undef $GRAB;
81 $grab->update if $grab; 101 $grab->update if $grab;
82 $GRAB->update if $GRAB; 102 $GRAB->update if $GRAB;
98 $HOVER->update if $HOVER && $HOVER->{can_hover}; 118 $HOVER->update if $HOVER && $HOVER->{can_hover};
99 119
100 check_tooltip; 120 check_tooltip;
101 } 121 }
102 122
103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 123 if ($HOVER) {
124 ($x, $y) = $HOVER->coord2local ($x, $y);
125 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
126 }
104} 127}
105 128
106# convert position array to integers 129# convert position array to integers
107sub harmonize { 130sub harmonize {
108 my ($vals) = @_; 131 my ($vals) = @_;
114 $rem += $_ - $i; 137 $rem += $_ - $i;
115 $_ = $i; 138 $_ = $i;
116 } 139 }
117} 140}
118 141
142# call when resolution changes etc.
143sub rescale_widgets {
144 my ($sx, $sy) = @_;
145
146 for my $widget (values %WIDGET) {
147 if ($widget->{toplevel}) {
148 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
149 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
150 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
151 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
152 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
153 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
154 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
155 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
156 }
157
158 $widget->reconfigure;
159 }
160}
161
119############################################################################# 162#############################################################################
120 163
121package CFClient::UI::Base; 164package CFClient::UI::Base;
122 165
123use strict; 166use strict;
126 169
127sub new { 170sub new {
128 my $class = shift; 171 my $class = shift;
129 172
130 my $self = bless { 173 my $self = bless {
131 x => 0, 174 x => 0,
132 y => 0, 175 y => 0,
133 z => 0, 176 z => 0,
134 can_events => 1, 177 can_events => 1,
135 @_ 178 @_
136 }, $class; 179 }, $class;
137 180
138 for (keys %$self) { 181 for (keys %$self) {
139 if (/^connect_(.*)$/) { 182 if (/^connect_(.*)$/) {
140 $self->connect ($1 => delete $self->{$_}); 183 $self->connect ($1 => delete $self->{$_});
141 } 184 }
142 } 185 }
143 186
187 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
188
144 $self 189 $self
145} 190}
146 191
192sub destroy {
193 my ($self) = @_;
194
195 $self->hide;
196 %$self = ();
197}
198
147sub show { 199sub show {
148 my ($self) = @_; 200 my ($self) = @_;
149 201
150 return if $self->{parent}; 202 return if $self->{parent};
151 203
204 $self->{toplevel} = 1;
152 $CFClient::UI::ROOT->add ($self); 205 $CFClient::UI::ROOT->add ($self);
153} 206}
154 207
155sub hide { 208sub hide {
156 my ($self) = @_; 209 my ($self) = @_;
157 210
158 return unless $self->{parent}; 211 undef $GRAB if $GRAB == $self;
212 undef $HOVER if $HOVER == $self;
159 213
160 $self->{parent}->remove ($self); 214 $self->{parent}->remove ($self)
215 if $self->{parent};
161} 216}
162 217
163sub move { 218sub move {
164 my ($self, $x, $y, $z) = @_; 219 my ($self, $x, $y, $z) = @_;
165 220
168 $self->{z} = $z if defined $z; 223 $self->{z} = $z if defined $z;
169 224
170 $self->update; 225 $self->update;
171} 226}
172 227
173sub needs_redraw { 228sub set_size {
174 0 229 my ($self, $w, $h) = @_;
230
231 $self->{user_w} = $w;
232 $self->{user_h} = $h;
233
234 $self->check_size;
175} 235}
176 236
177sub size_request { 237sub size_request {
178 require Carp; 238 require Carp;
179 Carp::confess "size_request is abstract"; 239 Carp::confess "size_request is abstract";
199 $self->{y} = $y; 259 $self->{y} = $y;
200 $self->update; 260 $self->update;
201 } 261 }
202 262
203 if ($self->{w} != $w || $self->{h} != $h) { 263 if ($self->{w} != $w || $self->{h} != $h) {
204 $self->{w} = $w; 264 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
205 $self->{h} = $h;
206
207 $self->size_allocate ($w, $h);
208 $self->update;
209 } 265 }
210} 266}
211 267
212sub size_allocate { 268sub size_allocate {
213 # nothing to be done 269 # nothing to be done
214} 270}
215 271
216# return top left coordinates 272sub reconfigure {
217sub _topleft { 273 my ($self) = @_;
274
275 $self->check_size (1);
276 $self->update;
277}
278
279sub children {
280}
281
282sub set_max_size {
218 my ($self, $x, $y) = @_; 283 my ($self, $w, $h) = @_;
219 284
220 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 285 delete $self->{max_w}; $self->{max_w} = $w if $w;
286 delete $self->{max_h}; $self->{max_h} = $h if $h;
221} 287}
222 288
223# translate global coordinates to local coordinate system 289# translate global coordinates to local coordinate system
224sub coord2local { 290sub coord2local {
225 my ($self, $x, $y) = @_; 291 my ($self, $x, $y) = @_;
226 292
227 my ($X, $Y) = $self->_topleft; 293 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
228 ($x - $X, $y - $Y)
229} 294}
230 295
231# translate local coordinates to global coordinate system 296# translate local coordinates to global coordinate system
232sub coord2global { 297sub coord2global {
233 my ($self, $x, $y) = @_; 298 my ($self, $x, $y) = @_;
234 299
235 my ($X, $Y) = $self->_topleft; 300 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
236 ($x + $X, $y + $Y)
237} 301}
238 302
239sub focus_in { 303sub focus_in {
240 my ($self) = @_; 304 my ($self) = @_;
241 305
301 glVertex $x + $self->{w}, $y + $self->{h}; 365 glVertex $x + $self->{w}, $y + $self->{h};
302 glVertex $x , $y + $self->{h}; 366 glVertex $x , $y + $self->{h};
303 glEnd; 367 glEnd;
304 glDisable GL_BLEND; 368 glDisable GL_BLEND;
305 } 369 }
370
371 if ($ENV{PCLIENT_DEBUG}) {
372 glPushMatrix;
373 glColor 1, 1, 0, 1;
374 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
375 glBegin GL_LINE_LOOP;
376 glVertex 0 , 0;
377 glVertex $self->{w}, 0;
378 glVertex $self->{w}, $self->{h};
379 glVertex 0 , $self->{h};
380 glEnd;
381 glPopMatrix;
382 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
383 }
306} 384}
307 385
308sub _draw { 386sub _draw {
309 my ($self) = @_; 387 my ($self) = @_;
310 388
325 403
326sub set_parent { 404sub set_parent {
327 my ($self, $parent) = @_; 405 my ($self, $parent) = @_;
328 406
329 Scalar::Util::weaken ($self->{parent} = $parent); 407 Scalar::Util::weaken ($self->{parent} = $parent);
408
409 # TODO: req_w _does_change after ->reconfigure
410 $self->check_size
411 unless exists $self->{req_w};
330} 412}
331 413
332sub check_size { 414sub check_size {
333 my ($self) = @_; 415 my ($self, $forced) = @_;
334 416
335 return unless $self->{parent}; 417 $self->{force_alloc} = 1 if $forced;
336 418 $CFClient::UI::ROOT->{check_size}{$self} = $self;
337 my ($w, $h) = $self->size_request;
338
339 if ($w != $self->{req_w} || $h != $self->{req_h}) {
340 $self->{req_w} = $w;
341 $self->{req_h} = $h;
342
343 $self->{parent}->check_size;
344 }
345} 419}
346 420
347sub update { 421sub update {
348 my ($self) = @_; 422 my ($self) = @_;
349 423
358} 432}
359 433
360sub emit { 434sub emit {
361 my ($self, $signal, @args) = @_; 435 my ($self, $signal, @args) = @_;
362 436
363 for my $cb (@{$self->{signal_cb}{$signal} || []}) { 437 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
364 $cb->($self, @args);
365 }
366} 438}
367 439
368sub DESTROY { 440sub DESTROY {
369 my ($self) = @_; 441 my ($self) = @_;
370 442
443 delete $WIDGET{$self+0};
371 #$self->deactivate; 444 #$self->deactivate;
372} 445}
373 446
374############################################################################# 447#############################################################################
375 448
437sub new { 510sub new {
438 my ($class, %arg) = @_; 511 my ($class, %arg) = @_;
439 512
440 my $children = delete $arg{children} || []; 513 my $children = delete $arg{children} || [];
441 514
442 my $self = $class->SUPER::new (children => [], can_events => 0, %arg); 515 my $self = $class->SUPER::new (
516 children => [],
517 can_events => 0,
518 %arg,
519 );
443 $self->add ($_) for @$children; 520 $self->add ($_) for @$children;
444 521
445 $self 522 $self
446} 523}
447 524
448sub add { 525sub add {
449 my ($self, $child) = @_; 526 my ($self, @widgets) = @_;
450 527
451 $child->set_parent ($self); 528 $_->set_parent ($self)
529 for @widgets;
452 530
453 use sort 'stable'; 531 use sort 'stable';
454 532
455 $self->{children} = [ 533 $self->{children} = [
456 sort { $a->{z} <=> $b->{z} } 534 sort { $a->{z} <=> $b->{z} }
457 @{$self->{children}}, $child 535 @{$self->{children}}, @widgets
458 ]; 536 ];
459 537
460 $child->check_size; 538 $self->check_size (1);
539 $self->update;
540}
541
542sub children {
543 @{ $_[0]{children} }
461} 544}
462 545
463sub remove { 546sub remove {
464 my ($self, $child) = @_; 547 my ($self, $child) = @_;
465 548
466 delete $child->{parent}; 549 delete $child->{parent};
550 $child->hide;
467 551
468 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 552 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
553
554 $self->check_size;
555 $self->update;
556}
557
558sub clear {
559 my ($self) = @_;
560
561 my $children = delete $self->{children};
562 $self->{children} = [];
563
564 for (@$children) {
565 delete $_->{parent};
566 $_->hide;
567 }
469 568
470 $self->check_size; 569 $self->check_size;
471 $self->update; 570 $self->update;
472} 571}
473 572
551} 650}
552 651
553sub update { 652sub update {
554 my ($self) = @_; 653 my ($self) = @_;
555 654
556 # we want to do this delayed... 655 $ROOT->on_post_alloc ($self => sub { $self->render_child });
557 $self->render_chld;
558 $self->SUPER::update; 656 $self->SUPER::update;
559}
560
561sub render_chld {
562 my ($self) = @_;
563
564 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
565 glClearColor 0, 0, 0, 1;
566 glClear GL_COLOR_BUFFER_BIT;
567 $self->child->draw;
568 };
569} 657}
570 658
571sub size_allocate { 659sub size_allocate {
572 my ($self, $w, $h) = @_; 660 my ($self, $w, $h) = @_;
573 661
574 $self->child->configure (0, 0, $w, $h); 662 $self->SUPER::size_allocate ($w, $h);
663 $self->update;
664}
575 665
666sub _render {
667 $_[0]{children}[0]->draw;
668}
669
670sub render_child {
671 my ($self) = @_;
672
673 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
674 glClearColor 0, 0, 0, 0;
675 glClear GL_COLOR_BUFFER_BIT;
676
576 $self->render_chld; 677 $self->_render;
678# glColorMask 1, 1, 1, 0;
679# glEnable GL_BLEND;
680# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
681# glRasterPos 0, 0;
682# glCopyPixels 0, 0, $self->{w}, $self->{h};
683# glDisable GL_BLEND;
684# glColorMask 1, 1, 1, 1;
685 };
577} 686}
578 687
579sub _draw { 688sub _draw {
580 my ($self) = @_; 689 my ($self) = @_;
581 690
582 my ($w, $h) = ($self->w, $self->h); 691 my ($w, $h) = ($self->w, $self->h);
583 692
584 my $tex = $self->{texture} 693 my $tex = $self->{texture}
585 or return; 694 or return;
586 695
587 glEnable GL_BLEND;
588 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
589 glEnable GL_TEXTURE_2D; 696 glEnable GL_TEXTURE_2D;
590 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
698 glColor 0, 0, 0, 1;
591 699
592 $tex->draw_quad (0, 0, $w, $h); 700 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
593 701
594 glDisable GL_BLEND;
595 glDisable GL_TEXTURE_2D; 702 glDisable GL_TEXTURE_2D;
596} 703}
597 704
598############################################################################# 705#############################################################################
599 706
600package CFClient::UI::ViewPort; 707package CFClient::UI::ViewPort;
601 708
602our @ISA = CFClient::UI::Window::; 709our @ISA = CFClient::UI::Window::;
603 710
604sub new { die }
605
606sub size_request { 711sub size_request {
607 my ($self) = @_; 712 my ($self) = @_;
608 713
609 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 714 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
610 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 715 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
611 716
612 @$self{qw(child_w child_h)} 717 @$self{qw(child_w child_h)}
613} 718}
614 719
615sub _draw { 720sub size_allocate {
616 my ($self) = @_; 721 my ($self, $w, $h) = @_;
617 722
618 $self->{children}[1]->draw; 723 $self->update;
619} 724}
620 725
726sub set_offset {
727 my ($self, $x, $y) = @_;
621 728
622############################################################################# 729 $self->{view_x} = int $x;
730 $self->{view_y} = int $y;
623 731
732 $self->update;
733}
734
735# hmm, this does not work for topleft of $self... but we should not ask for that
736sub coord2local {
737 my ($self, $x, $y) = @_;
738
739 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
740}
741
742sub coord2global {
743 my ($self, $x, $y) = @_;
744
745 $x = List::Util::min $self->{w}, $x - $self->{view_x};
746 $y = List::Util::min $self->{h}, $y - $self->{view_y};
747
748 $self->SUPER::coord2global ($x, $y)
749}
750
751sub find_widget {
752 my ($self, $x, $y) = @_;
753
754 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
755 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
756 ) {
757 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
758 } else {
759 $self->CFClient::UI::Base::find_widget ($x, $y)
760 }
761}
762
763sub _render {
764 my ($self) = @_;
765
766 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
767
768 $self->SUPER::_render;
769}
770
771#############################################################################
772
624package CFClient::UI::Frame; 773package CFClient::UI::ScrolledWindow;
625 774
626our @ISA = CFClient::UI::Bin::; 775our @ISA = CFClient::UI::HBox::;
627
628use CFClient::OpenGL;
629 776
630sub new { 777sub new {
631 my $class = shift; 778 my $class = shift;
632 779
780 my $self;
781
782 my $slider = new CFClient::UI::Slider
783 vertical => 1,
784 range => [0, 0, 1, 0.01], # HACK fix
785 connect_changed => sub {
786 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
787 },
788 ;
789
633 my $self = $class->SUPER::new ( 790 $self = $class->SUPER::new (
634 bg => [1, 1, 1, 1], 791 vp => (new CFClient::UI::ViewPort),
635 border_bg => [1, 1, 1, 1], 792 slider => $slider,
636 border => 0.8,
637 @_ 793 @_,
638 ); 794 );
639 795
796 $self->{vp}->add ($self->{scrolled});
797 $self->add ($self->{vp});
798 $self->add ($self->{slider});
799
640 $self 800 $self
641} 801}
642 802
643sub set_size { 803#TODO# update range on size_allocate depending on child
644 my ($self, $w, $h) = @_; 804# update viewport offset on scroll
645 $self->{req_w} = $w;
646 $self->{req_h} = $h;
647 $self->check_size;
648}
649 805
650sub size_request { 806#############################################################################
651 my ($self) = @_;
652 ($self->{req_w}, $self->{req_h})
653}
654 807
655sub size_allocate { 808package CFClient::UI::Frame;
656 my ($self, $w, $h) = @_; 809
657 $self->{w} = $w; 810our @ISA = CFClient::UI::Bin::;
658 $self->{h} = $h; 811
659 $self->child->configure (0, 0, $w, $h); 812use CFClient::OpenGL;
813
814sub new {
815 my $class = shift;
816
817 $class->SUPER::new (
818 bg => undef,
819 @_,
820 )
660} 821}
661 822
662sub _draw { 823sub _draw {
663 my ($self) = @_; 824 my ($self) = @_;
664 825
665 my ($w, $h) = ($self->{w}, $self->{h}); 826 if ($self->{bg}) {
827 my ($w, $h) = @$self{qw(w h)};
666 828
667 glEnable GL_BLEND; 829 glEnable GL_BLEND;
668 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 830 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
669 glEnable GL_TEXTURE_2D; 831 glColor @{ $self->{bg} };
670 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
671 832
672# glBegin GL_QUADS; 833 glBegin GL_QUADS;
673# glColor 0, 0, 0, 0;
674# glVertex 0 , 0; 834 glVertex 0 , 0;
675# glVertex 0 , $h; 835 glVertex 0 , $h;
676# glVertex $w, $h; 836 glVertex $w, $h;
677# glVertex $w, 0; 837 glVertex $w, 0;
678# glEnd; 838 glEnd;
679 839
680
681 $self->child->draw;
682 glDisable GL_BLEND; 840 glDisable GL_BLEND;
683 glDisable GL_TEXTURE_2D; 841 }
842
843 $self->SUPER::_draw;
684} 844}
685 845
686############################################################################# 846#############################################################################
687 847
688package CFClient::UI::FancyFrame; 848package CFClient::UI::FancyFrame;
699 my $class = shift; 859 my $class = shift;
700 860
701 # TODO: user_x, user_y, overwrite moveto? 861 # TODO: user_x, user_y, overwrite moveto?
702 862
703 my $self = $class->SUPER::new ( 863 my $self = $class->SUPER::new (
704 bg => [1, 1, 1, 1], 864 bg => [1, 1, 1, 1],
705 border_bg => [1, 1, 1, 1], 865 border_bg => [1, 1, 1, 1],
706 border => 0.8, 866 border => 0.6,
867 toplevel => 1,
707 can_events => 1, 868 can_events => 1,
708 @_ 869 @_
709 ); 870 );
710 871
711 $self->{title} &&= new CFClient::UI::Label 872 $self->{title} &&= new CFClient::UI::Label
712 align => 0, 873 align => 0,
713 valign => 1, 874 valign => 1,
714 text => $self->{title}, 875 text => $self->{title},
715 fontsize => 1; 876 fontsize => $self->{border};
716 877
717 $self 878 $self
718} 879}
719 880
720sub border { 881sub border {
721 int $_[0]{border} * $::FONTSIZE 882 int $_[0]{border} * $::FONTSIZE
722} 883}
723 884
724sub size_request { 885sub size_request {
725 my ($self) = @_; 886 my ($self) = @_;
726
727 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
728 887
729 my ($w, $h) = $self->SUPER::size_request; 888 my ($w, $h) = $self->SUPER::size_request;
730 889
731 ( 890 (
732 $w + $self->border * 2, 891 $w + $self->border * 2,
747} 906}
748 907
749sub button_down { 908sub button_down {
750 my ($self, $ev, $x, $y) = @_; 909 my ($self, $ev, $x, $y) = @_;
751 910
911 my ($w, $h) = @$self{qw(w h)};
752 my $border = $self->border; 912 my $border = $self->border;
753 913
754 if ($x < $self->{w} && $x >= $self->{w} - $border 914 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
755 && $y < $self->{h} && $y >= $self->{h} - $border) { 915 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
756 916
917 if ($lr & $td) {
918 my ($wx, $wy) = ($self->{x}, $self->{y});
757 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 919 my ($ox, $oy) = ($ev->{x}, $ev->{y});
758 my ($bw, $bh) = ($self->{w}, $self->{h}); 920 my ($bw, $bh) = ($self->{w}, $self->{h});
759 921
922 my $mx = $x < $border;
923 my $my = $y < $border;
924
760 $self->{motion} = sub { 925 $self->{motion} = sub {
761 my ($ev, $x, $y) = @_; 926 my ($ev, $x, $y) = @_;
762 927
763 ($x, $y) = ($ev->{x}, $ev->{y}); 928 my $dx = $ev->{x} - $ox;
929 my $dy = $ev->{y} - $oy;
764 930
765 $self->{user_w} = $bw + $x - $ox; 931 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
766 $self->{user_h} = $bh + $y - $oy; 932 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
933 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
767 $self->check_size; 934 $self->check_size;
768 }; 935 };
769 936
770 } elsif ($x >= 0 && $x < $self->{w} 937 } elsif ($lr ^ $td) {
771 && $y >= 0 && $y < $border) {
772
773 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 938 my ($ox, $oy) = ($ev->{x}, $ev->{y});
774 my ($bx, $by) = ($self->{x}, $self->{y}); 939 my ($bx, $by) = ($self->{x}, $self->{y});
775 940
776 $self->{motion} = sub { 941 $self->{motion} = sub {
777 my ($ev, $x, $y) = @_; 942 my ($ev, $x, $y) = @_;
800 my ($self) = @_; 965 my ($self) = @_;
801 966
802 my ($w, $h ) = ($self->{w}, $self->{h}); 967 my ($w, $h ) = ($self->{w}, $self->{h});
803 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 968 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
804 969
805 glEnable GL_BLEND;
806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
807 glEnable GL_TEXTURE_2D; 970 glEnable GL_TEXTURE_2D;
808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 971 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
809 972
810 my $border = $self->border; 973 my $border = $self->border;
811 974
812 glColor @{ $self->{border_bg} }; 975 glColor @{ $self->{border_bg} };
813 $tex[1]->draw_quad (0, 0, $w, $border); 976 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
814 $tex[3]->draw_quad (0, $border, $border, $ch); 977 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
815 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 978 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
816 $tex[4]->draw_quad (0, $h - $border, $w, $border); 979 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
817 980
981 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
818 my $bg = $tex[0]; 982 my $bg = $tex[0];
819 983
820 # TODO: repeat texture not scale 984 # TODO: repeat texture not scale
821 my $rep_x = $cw / $bg->{w}; 985 my $rep_x = $cw / $bg->{w};
822 my $rep_y = $ch / $bg->{h}; 986 my $rep_y = $ch / $bg->{h};
823 987
824 glColor @{ $self->{bg} }; 988 glColor @{ $self->{bg} };
825 989
826 $bg->{s} = $rep_x; 990 $bg->{s} = $rep_x;
827 $bg->{t} = $rep_y; 991 $bg->{t} = $rep_y;
828 $bg->{wrap_mode} = 1; 992 $bg->{wrap_mode} = 1;
829 $bg->draw_quad ($border, $border, $cw, $ch); 993 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
994 }
830 995
831 glDisable GL_TEXTURE_2D; 996 glDisable GL_TEXTURE_2D;
832 glDisable GL_BLEND;
833 997
834 $self->{title}->draw if $self->{title}; 998 $self->{title}->draw if $self->{title};
999
835 $self->child->draw; 1000 $self->child->draw;
836} 1001}
837 1002
838############################################################################# 1003#############################################################################
839 1004
861 $self->{children}[$y][$x] = $child; 1026 $self->{children}[$y][$x] = $child;
862 1027
863 $child->check_size; 1028 $child->check_size;
864} 1029}
865 1030
1031sub children {
1032 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1033}
1034
866# TODO: move to container class maybe? send childs a signal on removal? 1035# TODO: move to container class maybe? send childs a signal on removal?
867sub clear { 1036sub clear {
868 my ($self) = @_; 1037 my ($self) = @_;
869 1038
1039 my @children = $self->children;
870 delete $self->{children}; 1040 delete $self->{children};
1041
1042 for (@children) {
1043 delete $_->{parent};
1044 $_->hide;
1045 }
1046
871 $self->update; 1047 $self->update;
872} 1048}
873 1049
874sub get_wh { 1050sub get_wh {
875 my ($self) = @_; 1051 my ($self) = @_;
1052} 1228}
1053 1229
1054sub size_allocate { 1230sub size_allocate {
1055 my ($self, $w, $h) = @_; 1231 my ($self, $w, $h) = @_;
1056 1232
1233 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1234
1057 my $children = $self->{children}; 1235 my $children = $self->{children};
1058 1236
1059 my @h = map $_->{req_h}, @$children; 1237 my @h = map $_->{req_h}, @$children;
1060 1238
1061 my $req_h = List::Util::sum @h; 1239 my $req_h = List::Util::sum @h;
1098 1276
1099sub new { 1277sub new {
1100 my ($class, %arg) = @_; 1278 my ($class, %arg) = @_;
1101 1279
1102 my $self = $class->SUPER::new ( 1280 my $self = $class->SUPER::new (
1103 fg => [1, 1, 1], 1281 fg => [1, 1, 1],
1282 #font => default_font
1283 #text => initial text
1284 #markup => initial narkup
1285 layout => (new CFClient::Layout),
1104 fontsize => 1, 1286 fontsize => 1,
1105 text => "",
1106 align => -1, 1287 align => -1,
1107 valign => -1, 1288 valign => -1,
1108 padding => 2, 1289 padding => 2,
1109 layout => new CFClient::Layout,
1110 can_events => 0, 1290 can_events => 0,
1111 %arg 1291 %arg
1112 ); 1292 );
1113 1293
1114 if (exists $self->{template}) { 1294 if (exists $self->{template}) {
1115 my $layout = new CFClient::Layout; 1295 my $layout = new CFClient::Layout;
1116 $layout->set_text (delete $self->{template}); 1296 $layout->set_text (delete $self->{template});
1117 $self->{template} = $layout; 1297 $self->{template} = $layout;
1118 } 1298 }
1119 1299
1120 $self->set_text (delete $self->{text}) if exists $self->{text}; 1300 if (exists $self->{markup}) {
1121 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1301 $self->set_markup (delete $self->{markup});
1302 } else {
1303 $self->set_text (delete $self->{text});
1304 }
1122 1305
1123 $self 1306 $self
1124} 1307}
1125 1308
1126sub escape_text { 1309sub escape {
1127 local $_ = $_[1]; 1310 local $_ = $_[1];
1128 1311
1129 s/&/&amp;/g; 1312 s/&/&amp;/g;
1130 s/>/&gt;/g; 1313 s/>/&gt;/g;
1131 s/</&lt;/g; 1314 s/</&lt;/g;
1132 1315
1133 $_[1] 1316 $_[1]
1134} 1317}
1135 1318
1319sub update {
1320 my ($self) = @_;
1321
1322 delete $self->{texture};
1323 $self->SUPER::update;
1324}
1325
1136sub set_text { 1326sub set_text {
1137 my ($self, $text) = @_; 1327 my ($self, $text) = @_;
1138 1328
1329 return if $self->{text} eq "T$text";
1330 $self->{text} = "T$text";
1331
1332 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1139 $self->{layout}->set_text ($text); 1333 $self->{layout}->set_text ($text);
1140 1334
1141 delete $self->{texture}; 1335 $self->update;
1142 $self->check_size; 1336 $self->check_size;
1143 $self->update;
1144} 1337}
1145 1338
1146sub set_markup { 1339sub set_markup {
1147 my ($self, $markup) = @_; 1340 my ($self, $markup) = @_;
1148 1341
1342 return if $self->{text} eq "M$markup";
1343 $self->{text} = "M$markup";
1344
1345 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1346
1347 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1149 $self->{layout}->set_markup ($markup); 1348 $self->{layout}->set_markup ($markup);
1150 1349
1151 delete $self->{texture}; 1350 $self->update;
1152 $self->check_size; 1351 $self->check_size;
1153 $self->update;
1154} 1352}
1155 1353
1156sub size_request { 1354sub size_request {
1157 my ($self) = @_; 1355 my ($self) = @_;
1158 1356
1159 $self->{layout}->set_width; 1357 $self->{layout}->set_font ($self->{font}) if $self->{font};
1358 $self->{layout}->set_width ($self->{max_w} || -1);
1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1359 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1161 1360
1162 my ($w, $h) = $self->{layout}->size; 1361 my ($w, $h) = $self->{layout}->size;
1163 1362
1164 if (exists $self->{template}) { 1363 if (exists $self->{template}) {
1364 $self->{template}->set_font ($self->{font}) if $self->{font};
1165 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1365 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1166 1366
1167 my ($w2, $h2) = $self->{template}->size; 1367 my ($w2, $h2) = $self->{template}->size;
1168 1368
1169 $w = List::Util::max $w, $w2; 1369 $w = List::Util::max $w, $w2;
1185sub set_fontsize { 1385sub set_fontsize {
1186 my ($self, $fontsize) = @_; 1386 my ($self, $fontsize) = @_;
1187 1387
1188 $self->{fontsize} = $fontsize; 1388 $self->{fontsize} = $fontsize;
1189 delete $self->{texture}; 1389 delete $self->{texture};
1390
1391 $self->update;
1190 $self->check_size; 1392 $self->check_size;
1191 $self->update;
1192} 1393}
1193 1394
1194sub _draw { 1395sub _draw {
1195 my ($self) = @_; 1396 my ($self) = @_;
1196 1397
1197 my $tex = $self->{texture} ||= do { 1398 my $tex = $self->{texture} ||= do {
1399 $self->{layout}->set_foreground (@{$self->{fg}});
1400 $self->{layout}->set_font ($self->{font}) if $self->{font};
1198 $self->{layout}->set_width ($self->{w}); 1401 $self->{layout}->set_width ($self->{w});
1199 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1402 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1403
1200 new_from_layout CFClient::Texture $self->{layout} 1404 my $tex = new_from_layout CFClient::Texture $self->{layout};
1405
1406 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1407 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1408 : ($self->{w} - $tex->{w}) * 0.5;
1409
1410 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1411 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1412 : ($self->{h} - $tex->{h}) * 0.5;
1413
1414 $tex
1201 }; 1415 };
1202 1416
1203 glEnable GL_BLEND;
1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1205 glEnable GL_TEXTURE_2D; 1417 glEnable GL_TEXTURE_2D;
1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1418 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1207 1419
1420 if ($tex->{format} == GL_ALPHA) {
1208 glColor @{$self->{fg}}; 1421 glColor @{$self->{fg}};
1209
1210 $self->{ox} = int (
1211 $self->{align} < 0 ? $self->{padding}
1212 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1213 : ($self->{w} - $tex->{w}) * 0.5
1214 );
1215
1216 $self->{oy} = int (
1217 $self->{valign} < 0 ? $self->{padding}
1218 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1219 : ($self->{h} - $tex->{h}) * 0.5
1220 );
1221
1222 $tex->draw_quad ($self->{ox}, $self->{oy}); 1422 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1423 } else {
1424 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1425 }
1223 1426
1224 glDisable GL_TEXTURE_2D; 1427 glDisable GL_TEXTURE_2D;
1225 glDisable GL_BLEND;
1226} 1428}
1227 1429
1228############################################################################# 1430#############################################################################
1229 1431
1230package CFClient::UI::EntryBase; 1432package CFClient::UI::EntryBase;
1235 1437
1236sub new { 1438sub new {
1237 my $class = shift; 1439 my $class = shift;
1238 1440
1239 $class->SUPER::new ( 1441 $class->SUPER::new (
1240 fg => [1, 1, 1], 1442 fg => [1, 1, 1],
1241 bg => [0, 0, 0, 0.2], 1443 bg => [0, 0, 0, 0.2],
1242 active_bg => [1, 1, 1, 0.5], 1444 active_bg => [1, 1, 1, 0.5],
1243 active_fg => [0, 0, 0], 1445 active_fg => [0, 0, 0],
1244 can_hover => 1, 1446 can_hover => 1,
1245 can_focus => 1, 1447 can_focus => 1,
1246 valign => 0, 1448 valign => 0,
1247 can_events => 1, 1449 can_events => 1,
1248 @_ 1450 @_
1249 ) 1451 )
1250} 1452}
1251 1453
1265 $self->{layout}->set_text ("$text "); 1467 $self->{layout}->set_text ("$text ");
1266 1468
1267 $self->emit (changed => $self->{text}); 1469 $self->emit (changed => $self->{text});
1268} 1470}
1269 1471
1472sub set_text {
1473 my ($self, $text) = @_;
1474
1475 $self->{cursor} = length $text;
1476 $self->_set_text ($text);
1477 $self->check_size;
1478 $self->update;
1479}
1480
1270sub get_text { 1481sub get_text {
1271 $_[0]{text} 1482 $_[0]{text}
1272} 1483}
1273 1484
1274sub size_request { 1485sub size_request {
1281 1492
1282sub size_allocate { 1493sub size_allocate {
1283 my ($self, $w, $h) = @_; 1494 my ($self, $w, $h) = @_;
1284 1495
1285 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1496 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1286}
1287
1288sub set_text {
1289 my ($self, $text) = @_;
1290
1291 $self->{cursor} = length $text;
1292 $self->_set_text ($text);
1293 $self->update;
1294} 1497}
1295 1498
1296sub key_down { 1499sub key_down {
1297 my ($self, $ev) = @_; 1500 my ($self, $ev) = @_;
1298 1501
1300 my $sym = $ev->{sym}; 1503 my $sym = $ev->{sym};
1301 my $uni = $ev->{unicode}; 1504 my $uni = $ev->{unicode};
1302 1505
1303 my $text = $self->get_text; 1506 my $text = $self->get_text;
1304 1507
1305 if ($sym == 8) { 1508 if ($uni == 8) {
1306 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1509 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1307 } elsif ($sym == 127) { 1510 } elsif ($uni == 127) {
1308 substr $text, $self->{cursor}, 1, ""; 1511 substr $text, $self->{cursor}, 1, "";
1309 } elsif ($sym == CFClient::SDLK_LEFT) { 1512 } elsif ($sym == CFClient::SDLK_LEFT) {
1310 --$self->{cursor} if $self->{cursor}; 1513 --$self->{cursor} if $self->{cursor};
1311 } elsif ($sym == CFClient::SDLK_RIGHT) { 1514 } elsif ($sym == CFClient::SDLK_RIGHT) {
1312 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1515 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1313 } elsif ($sym == CFClient::SDLK_HOME) { 1516 } elsif ($sym == CFClient::SDLK_HOME) {
1314 $self->{cursor} = 0; 1517 $self->{cursor} = 0;
1315 } elsif ($sym == CFClient::SDLK_END) { 1518 } elsif ($sym == CFClient::SDLK_END) {
1316 $self->{cursor} = length $text; 1519 $self->{cursor} = length $text;
1317 } elsif ($sym == 27) { 1520 } elsif ($uni == 27) {
1318 $self->emit ('escape'); 1521 $self->emit ('escape');
1319 } elsif ($uni) { 1522 } elsif ($uni) {
1320 substr $text, $self->{cursor}++, 0, chr $uni; 1523 substr $text, $self->{cursor}++, 0, chr $uni;
1321 } 1524 }
1322 1525
1405 my ($self, $ev) = @_; 1608 my ($self, $ev) = @_;
1406 1609
1407 my $sym = $ev->{sym}; 1610 my $sym = $ev->{sym};
1408 1611
1409 if ($sym == 13) { 1612 if ($sym == 13) {
1613 unshift @{$self->{history}},
1614 my $txt = $self->get_text;
1615 $self->{history_pointer} = -1;
1616 $self->{history_saveback} = '';
1410 $self->emit (activate => $self->get_text); 1617 $self->emit (activate => $txt);
1411 $self->update; 1618 $self->update;
1619
1620 } elsif ($sym == CFClient::SDLK_UP) {
1621 if ($self->{history_pointer} < 0) {
1622 $self->{history_saveback} = $self->get_text;
1623 }
1624 if (@{$self->{history} || []} > 0) {
1625 $self->{history_pointer}++;
1626 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1627 $self->{history_pointer} = @{$self->{history} || []} - 1;
1628 }
1629 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1630 }
1631
1632 } elsif ($sym == CFClient::SDLK_DOWN) {
1633 $self->{history_pointer}--;
1634 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1635
1636 if ($self->{history_pointer} >= 0) {
1637 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1638 } else {
1639 $self->set_text ($self->{history_saveback});
1640 }
1412 1641
1413 } else { 1642 } else {
1414 $self->SUPER::key_down ($ev); 1643 $self->SUPER::key_down ($ev);
1415 } 1644 }
1416 1645
1430 1659
1431sub new { 1660sub new {
1432 my $class = shift; 1661 my $class = shift;
1433 1662
1434 $class->SUPER::new ( 1663 $class->SUPER::new (
1435 padding => 4, 1664 padding => 4,
1436 fg => [1, 1, 1], 1665 fg => [1, 1, 1],
1437 bg => [1, 1, 1, 0.2], 1666 bg => [1, 1, 1, 0.2],
1438 active_fg => [0, 0, 1], 1667 active_fg => [0, 0, 1],
1439 can_hover => 1, 1668 can_hover => 1,
1440 align => 0, 1669 align => 0,
1441 valign => 0, 1670 valign => 0,
1442 can_events => 1, 1671 can_events => 1,
1443 @_ 1672 @_
1444 ) 1673 )
1445} 1674}
1446 1675
1460 1689
1461 if ($GRAB == $self) { 1690 if ($GRAB == $self) {
1462 $self->{fg} = $self->{active_fg}; 1691 $self->{fg} = $self->{active_fg};
1463 } 1692 }
1464 1693
1465 glEnable GL_BLEND;
1466 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1467 glEnable GL_TEXTURE_2D; 1694 glEnable GL_TEXTURE_2D;
1468 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1695 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1469 glColor 0, 0, 0, 1; 1696 glColor 0, 0, 0, 1;
1470 1697
1471 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1698 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1472 1699
1473 glDisable GL_TEXTURE_2D; 1700 glDisable GL_TEXTURE_2D;
1474 glDisable GL_BLEND;
1475 1701
1476 $self->SUPER::_draw; 1702 $self->SUPER::_draw;
1477} 1703}
1478 1704
1479############################################################################# 1705#############################################################################
1526 1752
1527 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1753 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1528 1754
1529 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1755 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1530 1756
1531 glEnable GL_BLEND; 1757 my $tex = $self->{state} ? $tex[1] : $tex[0];
1758
1532 glEnable GL_TEXTURE_2D; 1759 glEnable GL_TEXTURE_2D;
1533 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1534
1535 my $tex = $self->{state} ? $tex[1] : $tex[0];
1536
1537 $tex->draw_quad (0, 0, $s, $s); 1760 $tex->draw_quad_alpha (0, 0, $s, $s);
1538
1539 glDisable GL_TEXTURE_2D; 1761 glDisable GL_TEXTURE_2D;
1540 glDisable GL_BLEND;
1541} 1762}
1542 1763
1543############################################################################# 1764#############################################################################
1544 1765
1545package CFClient::UI::Image; 1766package CFClient::UI::Image;
1588 glTranslate 0, -$self->{w}, 0; 1809 glTranslate 0, -$self->{w}, 0;
1589 1810
1590 ($w, $h) = ($h, $w); 1811 ($w, $h) = ($h, $w);
1591 } 1812 }
1592 1813
1593 glEnable GL_BLEND;
1594 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1595 glEnable GL_TEXTURE_2D; 1814 glEnable GL_TEXTURE_2D;
1596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1815 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1597 1816
1598 $tex->draw_quad (0, 0, $w, $h); 1817 $tex->draw_quad_alpha (0, 0, $w, $h);
1599 1818
1600 glDisable GL_BLEND;
1601 glDisable GL_TEXTURE_2D; 1819 glDisable GL_TEXTURE_2D;
1602} 1820}
1603 1821
1604############################################################################# 1822#############################################################################
1605 1823
1606package CFClient::UI::VGauge; 1824package CFClient::UI::VGauge;
1607 1825
1608our @ISA = CFClient::UI::Base::; 1826our @ISA = CFClient::UI::Base::;
1827
1828use List::Util qw(min max);
1609 1829
1610use CFClient::OpenGL; 1830use CFClient::OpenGL;
1611 1831
1612my %tex = ( 1832my %tex = (
1613 food => [ 1833 food => [
1614 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1834 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1615 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1835 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1616 ], 1836 ],
1617 grace => [ 1837 grace => [
1618 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1838 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1619 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1839 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1620 ], 1840 ],
1621 hp => [ 1841 hp => [
1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1842 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1623 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1843 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1624 ], 1844 ],
1625 mana => [ 1845 mana => [
1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1846 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1627 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1847 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1628 ], 1848 ],
1629); 1849);
1630 1850
1631# eg. VGauge->new (gauge => 'food'), default gauge: food 1851# eg. VGauge->new (gauge => 'food'), default gauge: food
1632sub new { 1852sub new {
1651} 1871}
1652 1872
1653sub set_max { 1873sub set_max {
1654 my ($self, $max) = @_; 1874 my ($self, $max) = @_;
1655 1875
1876 return if $self->{max_val} == $max;
1877
1656 $self->{max_val} = $max; 1878 $self->{max_val} = $max;
1879 $self->update;
1657} 1880}
1658 1881
1659sub set_value { 1882sub set_value {
1660 my ($self, $val, $max) = @_; 1883 my ($self, $val, $max) = @_;
1661 1884
1662 $self->set_max ($max) 1885 $self->set_max ($max)
1663 if defined $max; 1886 if defined $max;
1664 1887
1665 $max = $self->{max_val}; 1888 return if $self->{val} == $val;
1889
1666 $self->{val} = $val; 1890 $self->{val} = $val;
1667
1668 $self->update; 1891 $self->update;
1669} 1892}
1670 1893
1671sub _draw { 1894sub _draw {
1672 my ($self) = @_; 1895 my ($self) = @_;
1673 1896
1674 my $tex = $tex{$self->{type}}; 1897 my $tex = $tex{$self->{type}};
1898 my ($t1, $t2, $t3) = @$tex;
1675 1899
1676 my ($w, $h) = ($self->{w}, $self->{h}); 1900 my ($w, $h) = ($self->{w}, $self->{h});
1677 1901
1678 if ($self->{vertical}) { 1902 if ($self->{vertical}) {
1679 glRotate 90, 0, 0, 1; 1903 glRotate 90, 0, 0, 1;
1681 1905
1682 ($w, $h) = ($h, $w); 1906 ($w, $h) = ($h, $w);
1683 } 1907 }
1684 1908
1685 my $ycut = $self->{val} / ($self->{max_val} || 1); 1909 my $ycut = $self->{val} / ($self->{max_val} || 1);
1686 $ycut = 1 if $self->{val} > $self->{max_val};
1687 1910
1688 my $t1 = $tex->[0]; 1911 my $ycut1 = max 0, min 1, $ycut;
1689 my $t2 = $tex->[1]; 1912 my $ycut2 = max 0, min 1, $ycut - 1;
1913
1914 my $h1 = $self->{h} * (1 - $ycut1);
1915 my $h2 = $self->{h} * (1 - $ycut2);
1690 1916
1691 glEnable GL_BLEND; 1917 glEnable GL_BLEND;
1692 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1918 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1693 glEnable GL_TEXTURE_2D; 1919 glEnable GL_TEXTURE_2D;
1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1920 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1695 1921
1696 my $h1 = $self->{h} - $ycut * $self->{h};
1697 my $h2 = $ycut * $self->{h};
1698
1699 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1922 glBindTexture GL_TEXTURE_2D, $t1->{name};
1700 glBegin GL_QUADS; 1923 glBegin GL_QUADS;
1701 glTexCoord 0 , 0; glVertex 0 , 0; 1924 glTexCoord 0 , 0; glVertex 0 , 0;
1702 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1925 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1703 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1926 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1704 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1927 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1705 glEnd; 1928 glEnd;
1706 1929
1930 my $ycut1 = List::Util::min 1, $ycut;
1707 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1931 glBindTexture GL_TEXTURE_2D, $t2->{name};
1708 glBegin GL_QUADS; 1932 glBegin GL_QUADS;
1709 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1933 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1710 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1934 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1711 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1935 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1712 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1936 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1713 glEnd; 1937 glEnd;
1938
1939 if ($t3) {
1940 glBindTexture GL_TEXTURE_2D, $t3->{name};
1941 glBegin GL_QUADS;
1942 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1943 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1944 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1945 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1946 glEnd;
1947 }
1714 1948
1715 glDisable GL_BLEND; 1949 glDisable GL_BLEND;
1716 glDisable GL_TEXTURE_2D; 1950 glDisable GL_TEXTURE_2D;
1717} 1951}
1718 1952
1724 1958
1725sub new { 1959sub new {
1726 my ($class, %arg) = @_; 1960 my ($class, %arg) = @_;
1727 1961
1728 my $self = $class->SUPER::new ( 1962 my $self = $class->SUPER::new (
1729 tooltip => $arg{type}, 1963 tooltip => $arg{type},
1964 can_hover => 1,
1965 can_events => 1,
1730 %arg, 1966 %arg,
1731 ); 1967 );
1732 1968
1733 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1); 1969 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1734 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1); 1970 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1735 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1); 1971 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1736 1972
1737 $self 1973 $self
1738} 1974}
1739 1975
1740sub set_fontsize { 1976sub set_fontsize {
1742 1978
1743 $self->{value}->set_fontsize ($fsize); 1979 $self->{value}->set_fontsize ($fsize);
1744 $self->{max} ->set_fontsize ($fsize); 1980 $self->{max} ->set_fontsize ($fsize);
1745} 1981}
1746 1982
1983sub set_max {
1984 my ($self, $max) = @_;
1985
1986 $self->{gauge}->set_max ($max);
1987 $self->{max}->set_text ($max);
1988}
1989
1747sub set_value { 1990sub set_value {
1748 my ($self, $val, $max) = @_; 1991 my ($self, $val, $max) = @_;
1749 1992
1750 $self->set_max ($max) 1993 $self->set_max ($max)
1751 if defined $max; 1994 if defined $max;
1752 1995
1753 $self->{gauge}->set_value ($val, $max); 1996 $self->{gauge}->set_value ($val, $max);
1754 $self->{value}->set_text ($val); 1997 $self->{value}->set_text ($val);
1755}
1756
1757sub set_max {
1758 my ($self, $max) = @_;
1759
1760 $self->{gauge}->set_max ($max);
1761 $self->{max}->set_text ($max);
1762} 1998}
1763 1999
1764############################################################################# 2000#############################################################################
1765 2001
1766package CFClient::UI::Slider; 2002package CFClient::UI::Slider;
1785 # TODO: calculations are off 2021 # TODO: calculations are off
1786 my $self = $class->SUPER::new ( 2022 my $self = $class->SUPER::new (
1787 fg => [1, 1, 1], 2023 fg => [1, 1, 1],
1788 active_fg => [0, 0, 0], 2024 active_fg => [0, 0, 0],
1789 range => [0, 0, 100, 10], 2025 range => [0, 0, 100, 10],
1790 req_w => 20, 2026 req_w => $::WIDTH / 80,
1791 req_h => 20, 2027 req_h => $::WIDTH / 80,
1792 vertical => 0, 2028 vertical => 0,
1793 can_hover => 1, 2029 can_hover => 1,
1794 inner_pad => 5, 2030 inner_pad => 5,
1795 @_ 2031 @_
1796 ); 2032 );
1879 $page ||= 2; 2115 $page ||= 2;
1880 2116
1881 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2117 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1882 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2118 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1883 2119
1884 glEnable GL_BLEND;
1885 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1886 glEnable GL_TEXTURE_2D; 2120 glEnable GL_TEXTURE_2D;
1887 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2121 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1888 2122
1889 # draw background 2123 # draw background
1890 $tex[1]->draw_quad (0, 0, $w, $h); 2124 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
1891 2125
1892 # draw handle 2126 # draw handle
1893 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2127 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
1894 2128
1895 glDisable GL_BLEND;
1896 glDisable GL_TEXTURE_2D; 2129 glDisable GL_TEXTURE_2D;
1897} 2130}
1898 2131
1899############################################################################# 2132#############################################################################
1900 2133
1906 2139
1907sub new { 2140sub new {
1908 my $class = shift; 2141 my $class = shift;
1909 2142
1910 my $self = $class->SUPER::new ( 2143 my $self = $class->SUPER::new (
1911 fontsize => 1, 2144 fontsize => 1,
2145 can_events => 0,
2146 #font => default_font
1912 @_, 2147 @_,
1913 2148
1914 layout => (new CFClient::Layout), 2149 layout => (new CFClient::Layout 1),
1915 par => [], 2150 par => [],
1916 height => 0, 2151 height => 0,
1917 children => [ 2152 children => [
1918 (new CFClient::UI::Empty expand => 1), 2153 (new CFClient::UI::Empty expand => 1),
1919 (new CFClient::UI::Slider vertical => 1), 2154 (new CFClient::UI::Slider vertical => 1),
1920 ], 2155 ],
1921 ); 2156 );
1922 2157
1923 $self->{children}[1]->connect (changed => sub { 2158 $self->{children}[1]->connect (changed => sub { $self->update });
1924 $self->update;
1925 });
1926 2159
1927 $self 2160 $self
1928} 2161}
1929 2162
1930sub set_fontsize { 2163sub set_fontsize {
1938 my ($self, $text) = @_; 2171 my ($self, $text) = @_;
1939 2172
1940 my $layout = $self->{layout}; 2173 my $layout = $self->{layout};
1941 2174
1942 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2175 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1943 $layout->set_width ($self->{w}); 2176 $layout->set_width ($self->{children}[0]{w});
1944 $layout->set_text ($text); 2177 $layout->set_markup ($text);
1945 2178
1946 ($layout->size)[1] 2179 ($layout->size)[1]
1947} 2180}
1948 2181
1949sub reflow { 2182sub reflow {
1956sub size_allocate { 2189sub size_allocate {
1957 my ($self, $w, $h) = @_; 2190 my ($self, $w, $h) = @_;
1958 2191
1959 $self->SUPER::size_allocate ($w, $h); 2192 $self->SUPER::size_allocate ($w, $h);
1960 2193
2194 $self->{layout}->set_font ($self->{font}) if $self->{font};
1961 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2195 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1962 $self->{layout}->set_width ($self->{children}[0]{w}); 2196 $self->{layout}->set_width ($self->{children}[0]{w});
1963 2197
1964 $self->reflow; 2198 $self->reflow;
1965} 2199}
1986 2220
1987 return unless $self->{h} > 0; 2221 return unless $self->{h} > 0;
1988 2222
1989 delete $self->{texture}; 2223 delete $self->{texture};
1990 2224
1991 $ROOT->on_refresh ($self, sub { 2225 $ROOT->on_post_alloc ($self, sub {
1992 if (delete $self->{need_reflow}) { 2226 if (delete $self->{need_reflow}) {
1993 my $height = 0; 2227 my $height = 0;
1994 2228
1995 $height += $_->[0] = $self->text_height ($_->[2]) 2229 $height += $_->[0] = $self->text_height ($_->[2])
1996 for @{$self->{par}}; 2230 for @{$self->{par}};
2000 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2234 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2001 2235
2002 delete $self->{texture}; 2236 delete $self->{texture};
2003 } 2237 }
2004 2238
2005 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2239 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2006 glClearColor 0, 0, 0, 1; 2240 glClearColor 0, 0, 0, 0;
2007 glClear GL_COLOR_BUFFER_BIT; 2241 glClear GL_COLOR_BUFFER_BIT;
2008 2242
2009 glEnable GL_BLEND;
2010 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2011 glEnable GL_TEXTURE_2D; 2243 glEnable GL_TEXTURE_2D;
2012 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2244 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2013 2245
2014 my $top = int $self->{children}[1]{range}[0]; 2246 my $top = int $self->{children}[1]{range}[0];
2015 2247
2018 2250
2019 my $y = 0; 2251 my $y = 0;
2020 2252
2021 my $layout = $self->{layout}; 2253 my $layout = $self->{layout};
2022 2254
2255 $layout->set_font ($self->{font}) if $self->{font};
2256
2023 for my $par (@{$self->{par}}) { 2257 for my $par (@{$self->{par}}) {
2024 my $h = $par->[0]; 2258 my $h = $par->[0];
2025 2259
2026 if ($y0 < $y + $h && $y < $y1) { 2260 if ($y0 < $y + $h && $y < $y1) {
2261 $layout->set_foreground (@{ $par->[1] });
2027 $layout->set_text ($par->[2]); 2262 $layout->set_markup ($par->[2]);
2028 2263
2029 glColor @{ $par->[1] };
2030 my ($W, $H) = $layout->size; 2264 my ($W, $H) = $layout->size;
2031 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2265 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2032 } 2266 }
2033 2267
2034 $y += $h; 2268 $y += $h;
2035 } 2269 }
2036 2270
2037 glDisable GL_TEXTURE_2D; 2271 glDisable GL_TEXTURE_2D;
2038 glDisable GL_BLEND;
2039 }; 2272 };
2040 }); 2273 });
2041} 2274}
2042 2275
2043sub _draw { 2276sub _draw {
2044 my ($self) = @_; 2277 my ($self) = @_;
2045 2278
2046 if ($self->{texture}) {
2047 glEnable GL_TEXTURE_2D; 2279 glEnable GL_TEXTURE_2D;
2048 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2280 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2049 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2281 glColor 1, 1, 1, 1;
2282 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2050 glDisable GL_TEXTURE_2D; 2283 glDisable GL_TEXTURE_2D;
2051 }
2052 2284
2053 $self->{children}[1]->draw; 2285 $self->{children}[1]->draw;
2054 2286
2055} 2287}
2056 2288
2104 2336
2105sub new { 2337sub new {
2106 my $class = shift; 2338 my $class = shift;
2107 2339
2108 my $self = $class->SUPER::new ( 2340 my $self = $class->SUPER::new (
2109 state => 0, 2341 state => 0,
2110 connect_activate => \&toggle_flopper, 2342 connect_activate => \&toggle_flopper,
2111 can_events => 1,
2112 @_ 2343 @_
2113 ); 2344 );
2114 2345
2115 if ($self->{state}) { 2346 if ($self->{state}) {
2116 $self->{state} = 0; 2347 $self->{state} = 0;
2151 @_, 2382 @_,
2152 can_events => 0, 2383 can_events => 0,
2153 ) 2384 )
2154} 2385}
2155 2386
2156sub set_text { 2387sub set_tooltip_from {
2157 my ($self, $text) = @_; 2388 my ($self, $widget) = @_;
2158 2389
2159 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2390 $self->add (new CFClient::UI::Label
2160 $self->{label}->set_text ($text); 2391 markup => $widget->{tooltip},
2161 $self->add ($self->{label}); 2392 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2393 fontsize => 0.8,
2394 fg => [0, 0, 0, 1],
2395 font => ($widget->{tooltip_font} || $::FONT_PROP),
2396 );
2162} 2397}
2163 2398
2164sub size_request { 2399sub size_request {
2165 my ($self) = @_; 2400 my ($self) = @_;
2166 2401
2167 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2402 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2168 2403
2169 $w = List::Util::min $::WIDTH * 0.2, $w;
2170 $h = List::Util::max $::HEIGHT * 0.2, $h;
2171
2172 ($w + 4, $h + 4) 2404 ($w + 4, $h + 4)
2173} 2405}
2174 2406
2407sub size_allocate {
2408 my ($self, $w, $h) = @_;
2409
2410 $self->SUPER::size_allocate ($w - 4, $h - 4);
2411}
2412
2175sub _draw { 2413sub _draw {
2176 my ($self) = @_; 2414 my ($self) = @_;
2177 2415
2178 glPushMatrix;
2179 glTranslate 0.375, 0.375; 2416 glTranslate 0.375, 0.375;
2180 2417
2181 my ($w, $h) = @$self{qw(w h)}; 2418 my ($w, $h) = @$self{qw(w h)};
2182 2419
2183 glColor 1, 0.8, 0.4; 2420 glColor 1, 0.8, 0.4;
2194 glVertex 0 , $h; 2431 glVertex 0 , $h;
2195 glVertex $w, $h; 2432 glVertex $w, $h;
2196 glVertex $w, 0; 2433 glVertex $w, 0;
2197 glEnd; 2434 glEnd;
2198 2435
2199 glPopMatrix; 2436 glTranslate 2 - 0.375, 2 - 0.375;
2200
2201 glTranslate 2, 2;
2202 $self->SUPER::_draw; 2437 $self->SUPER::_draw;
2203} 2438}
2204 2439
2205############################################################################# 2440#############################################################################
2206 2441
2442package CFClient::UI::Face;
2443
2444our @ISA = CFClient::UI::Base::;
2445
2446use CFClient::OpenGL;
2447
2448sub new {
2449 my $class = shift;
2450
2451 $class->SUPER::new (
2452 aspect => 1,
2453 @_,
2454 )
2455}
2456
2457sub size_request {
2458 (32, 8)
2459}
2460
2461sub _draw {
2462 my ($self) = @_;
2463
2464 return unless $::CONN;#d# manage and cache textures differently
2465 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2466
2467 # TODO animation
2468 if ($tex) {
2469 glEnable GL_TEXTURE_2D;
2470 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2471 glColor 1, 1, 1, 1;
2472 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2473 glDisable GL_TEXTURE_2D;
2474 }
2475}
2476
2477#############################################################################
2478
2479package CFClient::UI::InventoryItem;
2480
2481our @ISA = CFClient::UI::HBox::;
2482
2483sub new {
2484 my $class = shift;
2485
2486 my %args = @_;
2487
2488 my $item = delete $args{item};
2489
2490 my $desc = $item->{nrof} < 2
2491 ? $item->{name}
2492 : "$item->{nrof} $item->{name_pl}";
2493
2494
2495 my $self = $class->SUPER::new (
2496 can_hover => 1,
2497 can_events => 1,
2498 tooltip => (CFClient::UI::Label->escape ($desc)
2499 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2500 connect_button_down => sub {
2501 my ($self, $ev, $x, $y) = @_;
2502
2503 # todo: maybe put examine on 1? but should just be a tooltip :(
2504 if ($ev->{button} == 1) {
2505 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2506 } elsif ($ev->{button} == 2) {
2507 $::CONN->send ("apply $item->{tag}");
2508 } elsif ($ev->{button} == 3) {
2509 CFClient::UI::Menu->new (
2510 items => [
2511 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2512 [
2513 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2514 sub { $::CONN->send ("lock $item->{tag}") },
2515 ],
2516 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2517 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2518 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2519 ],
2520 )->popup ($ev);
2521 }
2522
2523 1
2524 },
2525 %args
2526 );
2527
2528 $self->add (new CFClient::UI::Face
2529 can_events => 0,
2530 face => $item->{face},
2531 anim => $item->{anim},
2532 animspeed => $item->{animspeed},
2533 );
2534
2535 $self->add (new CFClient::UI::Label
2536 can_events => 0,
2537 text => $desc,
2538 );
2539
2540 $self
2541}
2542
2543#############################################################################
2544
2545package CFClient::UI::Inventory;
2546
2547our @ISA = CFClient::UI::ScrolledWindow::;
2548
2549sub new {
2550 my $class = shift;
2551
2552 my $self = $class->SUPER::new (
2553 scrolled => (new CFClient::UI::VBox),
2554 @_,
2555 );
2556
2557 $self
2558}
2559
2560sub set_items {
2561 my ($self, $items) = @_;
2562
2563 $self->{scrolled}->clear;
2564 return unless $items;
2565
2566 my @items = sort {
2567 ($a->{type} <=> $b->{type})
2568 or ($a->{name} cmp $b->{name})
2569 } @$items;
2570
2571 $self->{real_items} = \@items;
2572
2573 for my $item (@items) {
2574 my $desc = $item->{nrof} < 2
2575 ? $item->{name}
2576 : "$item->{nrof} $item->{name_pl}";
2577
2578 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2579 }
2580
2581 $self->{scrolled}->add (@items);
2582
2583# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2584}
2585
2586sub size_request {
2587 my ($self) = @_;
2588 ($self->{req_w}, $self->{req_h});
2589}
2590
2591#############################################################################
2592
2593package CFClient::UI::Menu;
2594
2595our @ISA = CFClient::UI::FancyFrame::;
2596
2597use CFClient::OpenGL;
2598
2599sub new {
2600 my $class = shift;
2601
2602 my $self = $class->SUPER::new (
2603 items => [],
2604 z => 100,
2605 @_,
2606 );
2607
2608 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2609
2610 for my $item (@{ $self->{items} }) {
2611 my ($widget, $cb) = @$item;
2612
2613 # handle various types of items, only text for now
2614 if (!ref $widget) {
2615 $widget = new CFClient::UI::Label
2616 can_hover => 1,
2617 can_events => 1,
2618 text => $widget;
2619 }
2620
2621 $self->{item}{$widget} = $item;
2622
2623 $self->{vbox}->add ($widget);
2624 }
2625
2626 $self
2627}
2628
2629# popup given the event (must be a mouse button down event currently)
2630sub popup {
2631 my ($self, $ev) = @_;
2632
2633 $self->emit ("popdown");
2634
2635 # maybe save $GRAB? must be careful about events...
2636 $GRAB = $self;
2637 $self->{button} = $ev->{button};
2638
2639 $self->show;
2640 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2641}
2642
2643sub mouse_motion {
2644 my ($self, $ev, $x, $y) = @_;
2645
2646 # TODO: should use vbox->find_widget or so
2647 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2648 $self->{hover} = $self->{item}{$HOVER};
2649}
2650
2651sub button_up {
2652 my ($self, $ev, $x, $y) = @_;
2653
2654 if ($ev->{button} == $self->{button}) {
2655 undef $GRAB;
2656 $self->hide;
2657
2658 $self->emit ("popdown");
2659 $self->{hover}[1]->() if $self->{hover};
2660 }
2661}
2662
2663#############################################################################
2664
2665package CFClient::UI::Statusbox;
2666
2667our @ISA = CFClient::UI::VBox::;
2668
2669sub reorder {
2670 my ($self) = @_;
2671 my $NOW = time;
2672
2673 while (my ($k, $v) = each %{ $self->{item} }) {
2674 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2675 }
2676
2677 my @widgets;
2678
2679 my @items = sort {
2680 $a->{pri} <=> $b->{pri}
2681 or $b->{id} <=> $a->{id}
2682 } values %{ $self->{item} };
2683
2684 my $count = 10 + 1;
2685 for my $item (@items) {
2686 last unless --$count;
2687
2688 push @widgets, $item->{label} ||= do {
2689 # TODO: doesn't handle markup well (read as: at all)
2690 my $short = $item->{count} > 1
2691 ? "<b>$item->{count} ×</b> $item->{text}"
2692 : $item->{text};
2693
2694 for ($short) {
2695 s/^\s+//;
2696 s/\012.*/…/s;
2697 my $len = int 40 / $item->{fontsize};
2698 substr $_, $len, length, "…" if $len < length;
2699 }
2700
2701 new CFClient::UI::Label
2702 markup => $short,
2703 tooltip => $item->{tooltip},
2704 tooltip_font => $::FONT_PROP,
2705 tooltip_width => 0.67,
2706 fontsize => $item->{fontsize},
2707 color => $item->{color},
2708 can_events => 1,
2709 can_hover => 1
2710 };
2711 }
2712
2713 $self->clear;
2714 $self->SUPER::add (reverse @widgets);
2715}
2716
2717sub add {
2718 my ($self, $text, %arg) = @_;
2719
2720 $text =~ s/^\s+//;
2721 $text =~ s/\s+$//;
2722
2723 my $timeout = time + ((delete $arg{timeout}) || 60);
2724
2725 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2726
2727 if (my $item = $self->{item}{$group}) {
2728 if ($item->{text} eq $text) {
2729 $item->{count}++;
2730 } else {
2731 $item->{count} = 1;
2732 $item->{text} = $item->{tooltip} = $text;
2733 }
2734 $item->{id} = ++$self->{id};
2735 $item->{timeout} = $timeout;
2736 delete $item->{label};
2737 } else {
2738 $self->{item}{$group} = {
2739 id => ++$self->{id},
2740 text => $text,
2741 timeout => $timeout,
2742 tooltip => $text,
2743 fontsize => 0.8,
2744 color => [0.8, 0.8, 0.8, 0.8],
2745 pri => 0,
2746 count => 1,
2747 %arg,
2748 };
2749 }
2750
2751 $self->reorder;
2752}
2753
2754#############################################################################
2755
2207package CFClient::UI::Root; 2756package CFClient::UI::Root;
2208 2757
2209our @ISA = CFClient::UI::Container::; 2758our @ISA = CFClient::UI::Container::;
2210 2759
2211use CFClient::OpenGL; 2760use CFClient::OpenGL;
2212 2761
2213sub check_size { 2762sub new {
2214 my ($self) = @_; 2763 my $class = shift;
2215 2764
2216 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2765 $class->SUPER::new (
2217} 2766 @_,
2218 2767 )
2219sub size_request {
2220 ($::WIDTH, $::HEIGHT)
2221} 2768}
2222 2769
2223sub configure { 2770sub configure {
2224 my ($self, $x, $y, $w, $h) = @_; 2771 my ($self, $x, $y, $w, $h) = @_;
2225 2772
2226 $self->SUPER::configure ($x, $y, $w, $h); 2773 $self->{w} = $w;
2774 $self->{h} = $h;
2775}
2227 2776
2777sub check_size {
2778 my ($self) = @_;
2779
2780 $self->size_allocate ($self->{w}, $self->{h})
2781 if $self->{w};
2782}
2783
2784sub size_request {
2785 my ($self) = @_;
2786
2787 ($self->{w}, $self->{h})
2788}
2789
2790sub size_allocate {
2791 my ($self, $w, $h) = @_;
2792
2793 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2794 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2795
2796 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2797 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2798
2228 for my $child (@{$self->{children}}) { 2799 for my $child ($self->children) {
2229 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2800 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2230 2801
2231 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2802 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2232 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2803 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2233 $child->configure ($X, $Y, $W,$H); 2804 $child->configure ($X, $Y, $W, $H);
2234 } 2805 }
2235} 2806}
2236 2807
2237sub _topleft { 2808sub coord2local {
2238 my ($self, $x, $y) = @_; 2809 my ($self, $x, $y) = @_;
2239 2810
2240 ($x, $y) 2811 ($x, $y)
2241} 2812}
2242 2813
2814sub coord2global {
2815 my ($self, $x, $y) = @_;
2816
2817 ($x, $y)
2818}
2819
2243sub update { 2820sub update {
2244 my ($self) = @_; 2821 my ($self) = @_;
2245 2822
2246 $self->check_size; 2823 $self->check_size;
2247 ::refresh (); 2824 $::WANT_REFRESH++;
2248} 2825}
2249 2826
2250sub add { 2827sub add {
2251 my ($self, $child) = @_; 2828 my ($self, $child) = @_;
2252 2829
2253 # integerize window positions 2830 # integerise window positions
2254 $child->{x} = int $child->{x}; 2831 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y}; 2832 $child->{y} = int $child->{y};
2256 2833
2257 $self->SUPER::add ($child); 2834 $self->SUPER::add ($child);
2258} 2835}
2261 my ($self, $id, $cb) = @_; 2838 my ($self, $id, $cb) = @_;
2262 2839
2263 $self->{refresh_hook}{$id} = $cb; 2840 $self->{refresh_hook}{$id} = $cb;
2264} 2841}
2265 2842
2843sub on_post_alloc {
2844 my ($self, $id, $cb) = @_;
2845
2846 $self->{post_alloc_hook}{$id} = $cb;
2847}
2848
2266sub draw { 2849sub draw {
2267 my ($self) = @_; 2850 my ($self) = @_;
2268 2851
2269 while (my $rcb = delete $self->{refresh_hook}) { 2852 while ($self->{refresh_hook}) {
2270 $_->() for values %$rcb; 2853 $_->()
2854 for values %{delete $self->{refresh_hook}};
2855 }
2856
2857 if ($self->{check_size}) {
2858 my @queue = ([], []);
2859
2860 for (;;) {
2861 if ($self->{check_size}) {
2862 # heuristic: check containers last
2863 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2864 for values %{delete $self->{check_size}}
2865 }
2866
2867 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2868
2869 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2870 ? @$widget{qw(user_w user_h)}
2871 : $widget->size_request;
2872
2873 if (delete $widget->{force_alloc}
2874 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2875 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2876
2877 $widget->{req_w} = $w;
2878 $widget->{req_h} = $h;
2879
2880 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2881
2882 $widget->{parent}->check_size
2883 if $widget->{parent};
2884 }
2885 }
2886 }
2887
2888 while ($self->{size_alloc}) {
2889 for (values %{delete $self->{size_alloc}}) {
2890 my ($widget, $w, $h) = @$_;
2891
2892 $w = 0 if $w < 0;
2893 $h = 0 if $h < 0;
2894
2895 $widget->{w} = $w;
2896 $widget->{h} = $h;
2897 $widget->size_allocate ($w, $h);
2898 $widget->emit (size_allocate => $w, $h);
2899 }
2900 }
2901
2902 while ($self->{post_alloc_hook}) {
2903 $_->()
2904 for values %{delete $self->{post_alloc_hook}};
2271 } 2905 }
2272 2906
2273 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2907 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2274 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2908 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2275 glClear GL_COLOR_BUFFER_BIT; 2909 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines