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.158 by root, Mon Apr 24 02:41:48 2006 UTC vs.
Revision 1.209 by root, Wed May 17 00:26:55 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 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 if $x + $TOOLTIP->{w} > $::WIDTH;
38
39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
31 } 42 }
32 43
33 return; 44 return;
34 } 45 }
35 } 46 }
39 delete $TOOLTIP->{owner}; 50 delete $TOOLTIP->{owner};
40} 51}
41 52
42# class methods for events 53# class methods for events
43sub feed_sdl_key_down_event { 54sub feed_sdl_key_down_event {
44 $FOCUS->key_down ($_[0]) if $FOCUS; 55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
56 if $FOCUS;
45} 57}
46 58
47sub feed_sdl_key_up_event { 59sub feed_sdl_key_up_event {
48 $FOCUS->key_up ($_[0]) if $FOCUS; 60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
61 if $FOCUS;
49} 62}
50 63
51sub feed_sdl_button_down_event { 64sub feed_sdl_button_down_event {
52 my ($ev) = @_; 65 my ($ev) = @_;
53 my ($x, $y) = ($ev->{x}, $ev->{y}); 66 my ($x, $y) = ($ev->{x}, $ev->{y});
61 check_tooltip; 74 check_tooltip;
62 } 75 }
63 76
64 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 77 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
65 78
66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 79 if ($GRAB) {
80 ($x, $y) = $GRAB->coord2local ($x, $y);
81 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
82 }
67} 83}
68 84
69sub feed_sdl_button_up_event { 85sub feed_sdl_button_up_event {
70 my ($ev) = @_; 86 my ($ev) = @_;
71 my ($x, $y) = ($ev->{x}, $ev->{y}); 87 my ($x, $y) = ($ev->{x}, $ev->{y});
72 88
73 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 89 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
74 90
75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
76 92
77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 93 if ($GRAB) {
94 ($x, $y) = $GRAB->coord2local ($x, $y);
95 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
96 }
78 97
79 if (!$BUTTON_STATE) { 98 if (!$BUTTON_STATE) {
80 my $grab = $GRAB; undef $GRAB; 99 my $grab = $GRAB; undef $GRAB;
81 $grab->update if $grab; 100 $grab->update if $grab;
82 $GRAB->update if $GRAB; 101 $GRAB->update if $GRAB;
98 $HOVER->update if $HOVER && $HOVER->{can_hover}; 117 $HOVER->update if $HOVER && $HOVER->{can_hover};
99 118
100 check_tooltip; 119 check_tooltip;
101 } 120 }
102 121
103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 122 if ($HOVER) {
123 ($x, $y) = $HOVER->coord2local ($x, $y);
124 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
125 }
104} 126}
105 127
106# convert position array to integers 128# convert position array to integers
107sub harmonize { 129sub harmonize {
108 my ($vals) = @_; 130 my ($vals) = @_;
114 $rem += $_ - $i; 136 $rem += $_ - $i;
115 $_ = $i; 137 $_ = $i;
116 } 138 }
117} 139}
118 140
141# call when resolution changes etc.
142sub rescale_widgets {
143 my ($sx, $sy) = @_;
144
145 for my $widget (values %WIDGET) {
146 if ($widget->{toplevel}) {
147 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
148 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
149 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
150 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
151 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
152 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
153 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
154 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
155 }
156
157 $widget->reconfigure;
158 }
159}
160
119############################################################################# 161#############################################################################
120 162
121package CFClient::UI::Base; 163package CFClient::UI::Base;
122 164
123use strict; 165use strict;
126 168
127sub new { 169sub new {
128 my $class = shift; 170 my $class = shift;
129 171
130 my $self = bless { 172 my $self = bless {
131 x => 0, 173 x => 0,
132 y => 0, 174 y => 0,
133 z => 0, 175 z => 0,
134 can_events => 1, 176 can_events => 1,
135 @_ 177 @_
136 }, $class; 178 }, $class;
137 179
138 for (keys %$self) { 180 for (keys %$self) {
139 if (/^connect_(.*)$/) { 181 if (/^connect_(.*)$/) {
140 $self->connect ($1 => delete $self->{$_}); 182 $self->connect ($1 => delete $self->{$_});
141 } 183 }
142 } 184 }
143 185
186 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
187
144 $self 188 $self
145} 189}
146 190
191sub destroy {
192 my ($self) = @_;
193
194 $self->hide;
195 %$self = ();
196}
197
147sub show { 198sub show {
148 my ($self) = @_; 199 my ($self) = @_;
149 200
150 return if $self->{parent}; 201 return if $self->{parent};
151 202
153} 204}
154 205
155sub hide { 206sub hide {
156 my ($self) = @_; 207 my ($self) = @_;
157 208
158 return unless $self->{parent}; 209 undef $GRAB if $GRAB == $self;
210 undef $HOVER if $HOVER == $self;
159 211
160 $self->{parent}->remove ($self); 212 $self->{parent}->remove ($self)
213 if $self->{parent};
161} 214}
162 215
163sub move { 216sub move {
164 my ($self, $x, $y, $z) = @_; 217 my ($self, $x, $y, $z) = @_;
165 218
204 $self->{y} = $y; 257 $self->{y} = $y;
205 $self->update; 258 $self->update;
206 } 259 }
207 260
208 if ($self->{w} != $w || $self->{h} != $h) { 261 if ($self->{w} != $w || $self->{h} != $h) {
209 $self->{w} = $w; 262 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
210 $self->{h} = $h;
211
212 $self->size_allocate ($w, $h);
213 $self->update;
214 } 263 }
215} 264}
216 265
217sub size_allocate { 266sub size_allocate {
218 # nothing to be done 267 # nothing to be done
219} 268}
220 269
270sub reconfigure {
271 my ($self) = @_;
272
273 $self->check_size (1);
274 $self->update;
275}
276
277sub children {
278}
279
221sub set_max_size { 280sub set_max_size {
222 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
223 282
224 delete $self->{max_w}; $self->{max_w} = $w if $w; 283 delete $self->{max_w}; $self->{max_w} = $w if $w;
225 delete $self->{max_h}; $self->{max_h} = $h if $h; 284 delete $self->{max_h}; $self->{max_h} = $h if $h;
226} 285}
227 286
228# return top left coordinates 287sub set_tooltip {
229sub _topleft {
230 my ($self, $x, $y) = @_; 288 my ($self, $tooltip) = @_;
231 289
232 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 290 $self->{tooltip} = $tooltip;
291
292 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
293 delete $CFClient::UI::TOOLTIP->{owner};
294 CFClient::UI::check_tooltip;
295 }
233} 296}
234 297
235# translate global coordinates to local coordinate system 298# translate global coordinates to local coordinate system
236sub coord2local { 299sub coord2local {
237 my ($self, $x, $y) = @_; 300 my ($self, $x, $y) = @_;
238 301
239 my ($X, $Y) = $self->_topleft; 302 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
240 ($x - $X, $y - $Y)
241} 303}
242 304
243# translate local coordinates to global coordinate system 305# translate local coordinates to global coordinate system
244sub coord2global { 306sub coord2global {
245 my ($self, $x, $y) = @_; 307 my ($self, $x, $y) = @_;
246 308
247 my ($X, $Y) = $self->_topleft; 309 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
248 ($x + $X, $y + $Y)
249} 310}
250 311
251sub focus_in { 312sub focus_in {
252 my ($self) = @_; 313 my ($self) = @_;
253 314
313 glVertex $x + $self->{w}, $y + $self->{h}; 374 glVertex $x + $self->{w}, $y + $self->{h};
314 glVertex $x , $y + $self->{h}; 375 glVertex $x , $y + $self->{h};
315 glEnd; 376 glEnd;
316 glDisable GL_BLEND; 377 glDisable GL_BLEND;
317 } 378 }
379
380 if ($ENV{PCLIENT_DEBUG}) {
381 glPushMatrix;
382 glColor 1, 1, 0, 1;
383 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
384 glBegin GL_LINE_LOOP;
385 glVertex 0 , 0;
386 glVertex $self->{w}, 0;
387 glVertex $self->{w}, $self->{h};
388 glVertex 0 , $self->{h};
389 glEnd;
390 glPopMatrix;
391 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
392 }
318} 393}
319 394
320sub _draw { 395sub _draw {
321 my ($self) = @_; 396 my ($self) = @_;
322 397
337 412
338sub set_parent { 413sub set_parent {
339 my ($self, $parent) = @_; 414 my ($self, $parent) = @_;
340 415
341 Scalar::Util::weaken ($self->{parent} = $parent); 416 Scalar::Util::weaken ($self->{parent} = $parent);
417
418 # TODO: req_w _does_change after ->reconfigure
419 $self->check_size
420 unless exists $self->{req_w};
342} 421}
343 422
344sub check_size { 423sub check_size {
345 my ($self) = @_; 424 my ($self, $forced) = @_;
346 425
347 $self->{parent} 426 $self->{force_alloc} = 1 if $forced;
348 or return 1; 427 $CFClient::UI::ROOT->{check_size}{$self} = $self;
349
350 my ($w, $h) = $self->{user_w} && $self->{user_h}
351 ? @$self{qw(user_w user_h)}
352 : $self->size_request;
353
354 if ($w != $self->{req_w} || $h != $self->{req_h}) {
355 $self->{req_w} = $w;
356 $self->{req_h} = $h;
357
358 $self->{parent}->check_size
359 or $self->size_allocate (
360 (List::Util::max $self->{w}, $w),
361 (List::Util::max $self->{h}, $h),
362 );
363
364 1
365 } else {
366 0
367 }
368} 428}
369 429
370sub update { 430sub update {
371 my ($self) = @_; 431 my ($self) = @_;
372 432
381} 441}
382 442
383sub emit { 443sub emit {
384 my ($self, $signal, @args) = @_; 444 my ($self, $signal, @args) = @_;
385 445
386 for my $cb (@{$self->{signal_cb}{$signal} || []}) { 446 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
387 $cb->($self, @args);
388 }
389} 447}
390 448
391sub DESTROY { 449sub DESTROY {
392 my ($self) = @_; 450 my ($self) = @_;
393 451
452 delete $WIDGET{$self+0};
394 #$self->deactivate; 453 #$self->deactivate;
395} 454}
396 455
397############################################################################# 456#############################################################################
398 457
407 my $class = shift; 466 my $class = shift;
408 467
409 # range [value, low, high, page] 468 # range [value, low, high, page]
410 469
411 $class->SUPER::new ( 470 $class->SUPER::new (
412 bg => [0, 0, 0, 0.2], 471 #bg => [0, 0, 0, 0.2],
413 active_bg => [1, 1, 1, 0.5], 472 #active_bg => [1, 1, 1, 0.5],
414 @_ 473 @_
415 ) 474 )
416} 475}
417 476
418sub _draw { 477sub _draw {
419 my ($self) = @_; 478 my ($self) = @_;
420 479
480 my $color = $FOCUS == $self && $self->{active_bg}
481 ? $self->{active_bg}
482 : $self->{bg};
483
484 if ($color && (@$color < 4 || $color->[3])) {
421 my ($w, $h) = @$self{qw(w h)}; 485 my ($w, $h) = @$self{qw(w h)};
422 486
423 glEnable GL_BLEND; 487 glEnable GL_BLEND;
424 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 488 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
425 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 489 glColor @$color;
426 490
427 glBegin GL_QUADS; 491 glBegin GL_QUADS;
428 glVertex 0 , 0; 492 glVertex 0 , 0;
429 glVertex 0 , $h; 493 glVertex 0 , $h;
430 glVertex $w, $h; 494 glVertex $w, $h;
431 glVertex $w, 0; 495 glVertex $w, 0;
432 glEnd; 496 glEnd;
433 497
434 glDisable GL_BLEND; 498 glDisable GL_BLEND;
499 }
435} 500}
436 501
437############################################################################# 502#############################################################################
438 503
439package CFClient::UI::Empty; 504package CFClient::UI::Empty;
460sub new { 525sub new {
461 my ($class, %arg) = @_; 526 my ($class, %arg) = @_;
462 527
463 my $children = delete $arg{children} || []; 528 my $children = delete $arg{children} || [];
464 529
465 my $self = $class->SUPER::new (children => [], can_events => 0, %arg); 530 my $self = $class->SUPER::new (
531 children => [],
532 can_events => 0,
533 %arg,
534 );
466 $self->add ($_) for @$children; 535 $self->add ($_) for @$children;
467 536
468 $self 537 $self
469} 538}
470 539
471sub add { 540sub add {
472 my ($self, $child) = @_; 541 my ($self, @widgets) = @_;
473 542
474 $child->set_parent ($self); 543 $_->set_parent ($self)
544 for @widgets;
475 545
476 use sort 'stable'; 546 use sort 'stable';
477 547
478 $self->{children} = [ 548 $self->{children} = [
479 sort { $a->{z} <=> $b->{z} } 549 sort { $a->{z} <=> $b->{z} }
480 @{$self->{children}}, $child 550 @{$self->{children}}, @widgets
481 ]; 551 ];
482 552
483 $child->check_size; 553 $self->check_size (1);
554 $self->update;
555}
556
557sub children {
558 @{ $_[0]{children} }
484} 559}
485 560
486sub remove { 561sub remove {
487 my ($self, $child) = @_; 562 my ($self, $child) = @_;
488 563
489 delete $child->{parent}; 564 delete $child->{parent};
565 $child->hide;
490 566
491 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 567 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
568
569 $self->check_size;
570 $self->update;
571}
572
573sub clear {
574 my ($self) = @_;
575
576 my $children = delete $self->{children};
577 $self->{children} = [];
578
579 for (@$children) {
580 delete $_->{parent};
581 $_->hide;
582 }
492 583
493 $self->check_size; 584 $self->check_size;
494 $self->update; 585 $self->update;
495} 586}
496 587
574} 665}
575 666
576sub update { 667sub update {
577 my ($self) = @_; 668 my ($self) = @_;
578 669
579 # we want to do this delayed... 670 $ROOT->on_post_alloc ($self => sub { $self->render_child });
580 $self->render_chld;
581 $self->SUPER::update; 671 $self->SUPER::update;
582}
583
584sub render_chld {
585 my ($self) = @_;
586
587 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
588 glClearColor 0, 0, 0, 1;
589 glClear GL_COLOR_BUFFER_BIT;
590 $self->child->draw;
591 };
592} 672}
593 673
594sub size_allocate { 674sub size_allocate {
595 my ($self, $w, $h) = @_; 675 my ($self, $w, $h) = @_;
596 676
597 $self->child->configure (0, 0, $w, $h); 677 $self->SUPER::size_allocate ($w, $h);
678 $self->update;
679}
598 680
681sub _render {
682 $_[0]{children}[0]->draw;
683}
684
685sub render_child {
686 my ($self) = @_;
687
688 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
689 glClearColor 0, 0, 0, 0;
690 glClear GL_COLOR_BUFFER_BIT;
691
599 $self->render_chld; 692 $self->_render;
693# glColorMask 1, 1, 1, 0;
694# glEnable GL_BLEND;
695# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
696# glRasterPos 0, 0;
697# glCopyPixels 0, 0, $self->{w}, $self->{h};
698# glDisable GL_BLEND;
699# glColorMask 1, 1, 1, 1;
700 };
600} 701}
601 702
602sub _draw { 703sub _draw {
603 my ($self) = @_; 704 my ($self) = @_;
604 705
605 my ($w, $h) = ($self->w, $self->h); 706 my ($w, $h) = ($self->w, $self->h);
606 707
607 my $tex = $self->{texture} 708 my $tex = $self->{texture}
608 or return; 709 or return;
609 710
610 glEnable GL_BLEND;
611 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
612 glEnable GL_TEXTURE_2D; 711 glEnable GL_TEXTURE_2D;
613 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 712 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
713 glColor 0, 0, 0, 1;
614 714
615 $tex->draw_quad (0, 0, $w, $h); 715 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
616 716
617 glDisable GL_BLEND;
618 glDisable GL_TEXTURE_2D; 717 glDisable GL_TEXTURE_2D;
619} 718}
620 719
621############################################################################# 720#############################################################################
622 721
623package CFClient::UI::ViewPort; 722package CFClient::UI::ViewPort;
624 723
625our @ISA = CFClient::UI::Window::; 724our @ISA = CFClient::UI::Window::;
626 725
627sub new { die }
628
629sub size_request { 726sub size_request {
630 my ($self) = @_; 727 my ($self) = @_;
631 728
632 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 729 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
633 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 730 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
634 731
635 @$self{qw(child_w child_h)} 732 @$self{qw(child_w child_h)}
636} 733}
637 734
638sub _draw { 735sub size_allocate {
639 my ($self) = @_; 736 my ($self, $w, $h) = @_;
640 737
641 $self->{children}[1]->draw; 738 $self->update;
642} 739}
643 740
741sub set_offset {
742 my ($self, $x, $y) = @_;
644 743
645############################################################################# 744 $self->{view_x} = int $x;
745 $self->{view_y} = int $y;
646 746
747 $self->update;
748}
749
750# hmm, this does not work for topleft of $self... but we should not ask for that
751sub coord2local {
752 my ($self, $x, $y) = @_;
753
754 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
755}
756
757sub coord2global {
758 my ($self, $x, $y) = @_;
759
760 $x = List::Util::min $self->{w}, $x - $self->{view_x};
761 $y = List::Util::min $self->{h}, $y - $self->{view_y};
762
763 $self->SUPER::coord2global ($x, $y)
764}
765
766sub find_widget {
767 my ($self, $x, $y) = @_;
768
769 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
770 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
771 ) {
772 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
773 } else {
774 $self->CFClient::UI::Base::find_widget ($x, $y)
775 }
776}
777
778sub _render {
779 my ($self) = @_;
780
781 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
782
783 $self->SUPER::_render;
784}
785
786#############################################################################
787
647package CFClient::UI::Frame; 788package CFClient::UI::ScrolledWindow;
648 789
649our @ISA = CFClient::UI::Bin::; 790our @ISA = CFClient::UI::HBox::;
650
651use CFClient::OpenGL;
652 791
653sub new { 792sub new {
654 my $class = shift; 793 my $class = shift;
655 794
795 my $self;
796
797 my $slider = new CFClient::UI::Slider
798 vertical => 1,
799 range => [0, 0, 1, 0.01], # HACK fix
800 connect_changed => sub {
801 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
802 },
803 ;
804
656 my $self = $class->SUPER::new ( 805 $self = $class->SUPER::new (
657 bg => [1, 1, 1, 1], 806 vp => (new CFClient::UI::ViewPort),
658 border_bg => [1, 1, 1, 1], 807 slider => $slider,
659 border => 0.8,
660 @_ 808 @_,
661 ); 809 );
662 810
811 $self->{vp}->add ($self->{scrolled});
812 $self->add ($self->{vp});
813 $self->add ($self->{slider});
814
663 $self 815 $self
664} 816}
665 817
818#TODO# update range on size_allocate depending on child
819# update viewport offset on scroll
820
821#############################################################################
822
823package CFClient::UI::Frame;
824
825our @ISA = CFClient::UI::Bin::;
826
827use CFClient::OpenGL;
828
829sub new {
830 my $class = shift;
831
832 $class->SUPER::new (
833 bg => undef,
834 @_,
835 )
836}
837
666sub _draw { 838sub _draw {
667 my ($self) = @_; 839 my ($self) = @_;
668 840
669 my ($w, $h) = ($self->{w}, $self->{h}); 841 if ($self->{bg}) {
842 my ($w, $h) = @$self{qw(w h)};
670 843
671 glEnable GL_BLEND; 844 glEnable GL_BLEND;
672 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 845 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
673 glEnable GL_TEXTURE_2D; 846 glColor @{ $self->{bg} };
674 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
675 847
676# glBegin GL_QUADS; 848 glBegin GL_QUADS;
677# glColor 0, 0, 0, 0;
678# glVertex 0 , 0; 849 glVertex 0 , 0;
679# glVertex 0 , $h; 850 glVertex 0 , $h;
680# glVertex $w, $h; 851 glVertex $w, $h;
681# glVertex $w, 0; 852 glVertex $w, 0;
682# glEnd; 853 glEnd;
683 854
684
685 $self->child->draw;
686 glDisable GL_BLEND; 855 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D; 856 }
857
858 $self->SUPER::_draw;
688} 859}
689 860
690############################################################################# 861#############################################################################
691 862
692package CFClient::UI::FancyFrame; 863package CFClient::UI::FancyFrame;
703 my $class = shift; 874 my $class = shift;
704 875
705 # TODO: user_x, user_y, overwrite moveto? 876 # TODO: user_x, user_y, overwrite moveto?
706 877
707 my $self = $class->SUPER::new ( 878 my $self = $class->SUPER::new (
708 bg => [1, 1, 1, 1], 879 bg => [1, 1, 1, 1],
709 border_bg => [1, 1, 1, 1], 880 border_bg => [1, 1, 1, 1],
710 border => 0.8, 881 border => 0.6,
882 toplevel => 1,
711 can_events => 1, 883 can_events => 1,
712 @_ 884 @_
713 ); 885 );
714 886
715 $self->{title} &&= new CFClient::UI::Label 887 $self->{title} &&= new CFClient::UI::Label
716 align => 0, 888 align => 0,
717 valign => 1, 889 valign => 1,
718 text => $self->{title}, 890 text => $self->{title},
719 fontsize => 1; 891 fontsize => $self->{border};
720 892
721 $self 893 $self
722} 894}
723 895
724sub border { 896sub border {
740 my ($self, $w, $h) = @_; 912 my ($self, $w, $h) = @_;
741 913
742 $h -= List::Util::max 0, $self->border * 2; 914 $h -= List::Util::max 0, $self->border * 2;
743 $w -= List::Util::max 0, $self->border * 2; 915 $w -= List::Util::max 0, $self->border * 2;
744 916
745 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 917 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
746 if $self->{title}; 918 if $self->{title};
747 919
748 $self->child->configure ($self->border, $self->border, $w, $h); 920 $self->child->configure ($self->border, $self->border, $w, $h);
749} 921}
750 922
751sub button_down { 923sub button_down {
752 my ($self, $ev, $x, $y) = @_; 924 my ($self, $ev, $x, $y) = @_;
753 925
926 my ($w, $h) = @$self{qw(w h)};
754 my $border = $self->border; 927 my $border = $self->border;
755 928
756 if ($x < $self->{w} && $x >= $self->{w} - $border 929 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
757 && $y < $self->{h} && $y >= $self->{h} - $border) { 930 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
758 931
932 if ($lr & $td) {
933 my ($wx, $wy) = ($self->{x}, $self->{y});
759 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 934 my ($ox, $oy) = ($ev->{x}, $ev->{y});
760 my ($bw, $bh) = ($self->{w}, $self->{h}); 935 my ($bw, $bh) = ($self->{w}, $self->{h});
761 936
937 my $mx = $x < $border;
938 my $my = $y < $border;
939
762 $self->{motion} = sub { 940 $self->{motion} = sub {
763 my ($ev, $x, $y) = @_; 941 my ($ev, $x, $y) = @_;
764 942
765 ($x, $y) = ($ev->{x}, $ev->{y}); 943 my $dx = $ev->{x} - $ox;
944 my $dy = $ev->{y} - $oy;
766 945
767 $self->{user_w} = $bw + $x - $ox; 946 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
768 $self->{user_h} = $bh + $y - $oy; 947 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
948 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
769 $self->check_size; 949 $self->check_size;
770 }; 950 };
771 951
772 } elsif ($x >= 0 && $x < $self->{w} 952 } elsif ($lr ^ $td) {
773 && $y >= 0 && $y < $border) {
774
775 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 953 my ($ox, $oy) = ($ev->{x}, $ev->{y});
776 my ($bx, $by) = ($self->{x}, $self->{y}); 954 my ($bx, $by) = ($self->{x}, $self->{y});
777 955
778 $self->{motion} = sub { 956 $self->{motion} = sub {
779 my ($ev, $x, $y) = @_; 957 my ($ev, $x, $y) = @_;
802 my ($self) = @_; 980 my ($self) = @_;
803 981
804 my ($w, $h ) = ($self->{w}, $self->{h}); 982 my ($w, $h ) = ($self->{w}, $self->{h});
805 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 983 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
806 984
807 glEnable GL_BLEND;
808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
809 glEnable GL_TEXTURE_2D; 985 glEnable GL_TEXTURE_2D;
810 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 986 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
811 987
812 my $border = $self->border; 988 my $border = $self->border;
813 989
814 glColor @{ $self->{border_bg} }; 990 glColor @{ $self->{border_bg} };
815 $tex[1]->draw_quad (0, 0, $w, $border); 991 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
816 $tex[3]->draw_quad (0, $border, $border, $ch); 992 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
817 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 993 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
818 $tex[4]->draw_quad (0, $h - $border, $w, $border); 994 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
819 995
996 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
820 my $bg = $tex[0]; 997 my $bg = $tex[0];
821 998
822 # TODO: repeat texture not scale 999 # TODO: repeat texture not scale
823 my $rep_x = $cw / $bg->{w}; 1000 my $rep_x = $cw / $bg->{w};
824 my $rep_y = $ch / $bg->{h}; 1001 my $rep_y = $ch / $bg->{h};
825 1002
826 glColor @{ $self->{bg} }; 1003 glColor @{ $self->{bg} };
827 1004
828 $bg->{s} = $rep_x; 1005 $bg->{s} = $rep_x;
829 $bg->{t} = $rep_y; 1006 $bg->{t} = $rep_y;
830 $bg->{wrap_mode} = 1; 1007 $bg->{wrap_mode} = 1;
831 $bg->draw_quad ($border, $border, $cw, $ch); 1008 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1009 }
832 1010
833 glDisable GL_TEXTURE_2D; 1011 glDisable GL_TEXTURE_2D;
834 glDisable GL_BLEND;
835 1012
836 $self->{title}->draw if $self->{title}; 1013 $self->{title}->draw if $self->{title};
1014
837 $self->child->draw; 1015 $self->child->draw;
838} 1016}
839 1017
840############################################################################# 1018#############################################################################
841 1019
863 $self->{children}[$y][$x] = $child; 1041 $self->{children}[$y][$x] = $child;
864 1042
865 $child->check_size; 1043 $child->check_size;
866} 1044}
867 1045
1046sub children {
1047 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1048}
1049
868# TODO: move to container class maybe? send childs a signal on removal? 1050# TODO: move to container class maybe? send childs a signal on removal?
869sub clear { 1051sub clear {
870 my ($self) = @_; 1052 my ($self) = @_;
871 1053
1054 my @children = $self->children;
872 delete $self->{children}; 1055 delete $self->{children};
1056
1057 for (@children) {
1058 delete $_->{parent};
1059 $_->hide;
1060 }
1061
873 $self->update; 1062 $self->update;
874} 1063}
875 1064
876sub get_wh { 1065sub get_wh {
877 my ($self) = @_; 1066 my ($self) = @_;
1054} 1243}
1055 1244
1056sub size_allocate { 1245sub size_allocate {
1057 my ($self, $w, $h) = @_; 1246 my ($self, $w, $h) = @_;
1058 1247
1248 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1249
1059 my $children = $self->{children}; 1250 my $children = $self->{children};
1060 1251
1061 my @h = map $_->{req_h}, @$children; 1252 my @h = map $_->{req_h}, @$children;
1062 1253
1063 my $req_h = List::Util::sum @h; 1254 my $req_h = List::Util::sum @h;
1092 1283
1093############################################################################# 1284#############################################################################
1094 1285
1095package CFClient::UI::Label; 1286package CFClient::UI::Label;
1096 1287
1097our @ISA = CFClient::UI::Base::; 1288our @ISA = CFClient::UI::DrawBG::;
1098 1289
1099use CFClient::OpenGL; 1290use CFClient::OpenGL;
1100 1291
1101sub new { 1292sub new {
1102 my ($class, %arg) = @_; 1293 my ($class, %arg) = @_;
1103 1294
1104 my $self = $class->SUPER::new ( 1295 my $self = $class->SUPER::new (
1105 fg => [1, 1, 1], 1296 fg => [1, 1, 1],
1297 #bg => none
1298 #active_bg => none
1106 #font => default_font 1299 #font => default_font
1300 #text => initial text
1301 #markup => initial narkup
1302 layout => (new CFClient::Layout),
1107 fontsize => 1, 1303 fontsize => 1,
1108 text => "",
1109 align => -1, 1304 align => -1,
1110 valign => -1, 1305 valign => -1,
1111 padding => 2, 1306 padding => 2,
1112 layout => new CFClient::Layout,
1113 can_events => 0, 1307 can_events => 0,
1114 %arg 1308 %arg
1115 ); 1309 );
1116 1310
1117 if (exists $self->{template}) { 1311 if (exists $self->{template}) {
1118 my $layout = new CFClient::Layout; 1312 my $layout = new CFClient::Layout;
1119 $layout->set_text (delete $self->{template}); 1313 $layout->set_text (delete $self->{template});
1120 $self->{template} = $layout; 1314 $self->{template} = $layout;
1121 } 1315 }
1122 1316
1123 $self->set_text (delete $self->{text}) if exists $self->{text}; 1317 if (exists $self->{markup}) {
1124 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1318 $self->set_markup (delete $self->{markup});
1319 } else {
1320 $self->set_text (delete $self->{text});
1321 }
1125 1322
1126 $self 1323 $self
1127} 1324}
1128 1325
1129sub escape_text { 1326sub escape($) {
1130 local $_ = $_[1]; 1327 local $_ = $_[0];
1131 1328
1132 s/&/&amp;/g; 1329 s/&/&amp;/g;
1133 s/>/&gt;/g; 1330 s/>/&gt;/g;
1134 s/</&lt;/g; 1331 s/</&lt;/g;
1135 1332
1136 $_[1] 1333 $_
1334}
1335
1336sub update {
1337 my ($self) = @_;
1338
1339 delete $self->{texture};
1340 $self->SUPER::update;
1137} 1341}
1138 1342
1139sub set_text { 1343sub set_text {
1140 my ($self, $text) = @_; 1344 my ($self, $text) = @_;
1141 1345
1346 return if $self->{text} eq "T$text";
1347 $self->{text} = "T$text";
1348
1349 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1142 $self->{layout}->set_text ($text); 1350 $self->{layout}->set_text ($text);
1143 1351
1144 delete $self->{texture}; 1352 $self->update;
1145 $self->check_size; 1353 $self->check_size;
1146 $self->update;
1147} 1354}
1148 1355
1149sub set_markup { 1356sub set_markup {
1150 my ($self, $markup) = @_; 1357 my ($self, $markup) = @_;
1151 1358
1359 return if $self->{text} eq "M$markup";
1360 $self->{text} = "M$markup";
1361
1362 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1363
1364 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1152 $self->{layout}->set_markup ($markup); 1365 $self->{layout}->set_markup ($markup);
1153 1366
1154 delete $self->{texture}; 1367 $self->update;
1155 $self->check_size; 1368 $self->check_size;
1156 $self->update;
1157} 1369}
1158 1370
1159sub size_request { 1371sub size_request {
1160 my ($self) = @_; 1372 my ($self) = @_;
1161 1373
1190sub set_fontsize { 1402sub set_fontsize {
1191 my ($self, $fontsize) = @_; 1403 my ($self, $fontsize) = @_;
1192 1404
1193 $self->{fontsize} = $fontsize; 1405 $self->{fontsize} = $fontsize;
1194 delete $self->{texture}; 1406 delete $self->{texture};
1407
1408 $self->update;
1195 $self->check_size; 1409 $self->check_size;
1196 $self->update;
1197} 1410}
1198 1411
1199sub _draw { 1412sub _draw {
1200 my ($self) = @_; 1413 my ($self) = @_;
1201 1414
1415 $self->SUPER::_draw; # draw background, if applicable
1416
1202 my $tex = $self->{texture} ||= do { 1417 my $tex = $self->{texture} ||= do {
1418 $self->{layout}->set_foreground (@{$self->{fg}});
1203 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1419 $self->{layout}->set_font ($self->{font}) if $self->{font};
1204 $self->{layout}->set_width ($self->{w}); 1420 $self->{layout}->set_width ($self->{w});
1205 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1421 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1422
1206 new_from_layout CFClient::Texture $self->{layout} 1423 my $tex = new_from_layout CFClient::Texture $self->{layout};
1424
1425 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1426 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1427 : ($self->{w} - $tex->{w}) * 0.5);
1428
1429 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1430 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1431 : ($self->{h} - $tex->{h}) * 0.5);
1432
1433 $tex
1207 }; 1434 };
1208 1435
1209 glEnable GL_BLEND;
1210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1211 glEnable GL_TEXTURE_2D; 1436 glEnable GL_TEXTURE_2D;
1212 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1437 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1213 1438
1439 if ($tex->{format} == GL_ALPHA) {
1214 glColor @{$self->{fg}}; 1440 glColor @{$self->{fg}};
1215
1216 $self->{ox} = int (
1217 $self->{align} < 0 ? $self->{padding}
1218 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1219 : ($self->{w} - $tex->{w}) * 0.5
1220 );
1221
1222 $self->{oy} = int (
1223 $self->{valign} < 0 ? $self->{padding}
1224 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1225 : ($self->{h} - $tex->{h}) * 0.5
1226 );
1227
1228 $tex->draw_quad ($self->{ox}, $self->{oy}); 1441 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1442 } else {
1443 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1444 }
1229 1445
1230 glDisable GL_TEXTURE_2D; 1446 glDisable GL_TEXTURE_2D;
1231 glDisable GL_BLEND;
1232} 1447}
1233 1448
1234############################################################################# 1449#############################################################################
1235 1450
1236package CFClient::UI::EntryBase; 1451package CFClient::UI::EntryBase;
1241 1456
1242sub new { 1457sub new {
1243 my $class = shift; 1458 my $class = shift;
1244 1459
1245 $class->SUPER::new ( 1460 $class->SUPER::new (
1246 fg => [1, 1, 1], 1461 fg => [1, 1, 1],
1247 bg => [0, 0, 0, 0.2], 1462 bg => [0, 0, 0, 0.2],
1248 active_bg => [1, 1, 1, 0.5], 1463 active_bg => [1, 1, 1, 0.5],
1249 active_fg => [0, 0, 0], 1464 active_fg => [0, 0, 0],
1250 can_hover => 1, 1465 can_hover => 1,
1251 can_focus => 1, 1466 can_focus => 1,
1252 valign => 0, 1467 valign => 0,
1253 can_events => 1, 1468 can_events => 1,
1254 @_ 1469 @_
1255 ) 1470 )
1256} 1471}
1257 1472
1271 $self->{layout}->set_text ("$text "); 1486 $self->{layout}->set_text ("$text ");
1272 1487
1273 $self->emit (changed => $self->{text}); 1488 $self->emit (changed => $self->{text});
1274} 1489}
1275 1490
1276sub get_text {
1277 $_[0]{text}
1278}
1279
1280sub size_request {
1281 my ($self) = @_;
1282
1283 my ($w, $h) = $self->SUPER::size_request;
1284
1285 ($w + 1, $h) # add 1 for cursor
1286}
1287
1288sub size_allocate {
1289 my ($self, $w, $h) = @_;
1290
1291 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1292}
1293
1294sub set_text { 1491sub set_text {
1295 my ($self, $text) = @_; 1492 my ($self, $text) = @_;
1296 1493
1297 $self->{cursor} = length $text; 1494 $self->{cursor} = length $text;
1298 $self->_set_text ($text); 1495 $self->_set_text ($text);
1299 $self->update; 1496 $self->update;
1497 $self->check_size;
1498}
1499
1500sub get_text {
1501 $_[0]{text}
1502}
1503
1504sub size_request {
1505 my ($self) = @_;
1506
1507 my ($w, $h) = $self->SUPER::size_request;
1508
1509 ($w + 1, $h) # add 1 for cursor
1510}
1511
1512sub size_allocate {
1513 my ($self, $w, $h) = @_;
1514
1515 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1300} 1516}
1301 1517
1302sub key_down { 1518sub key_down {
1303 my ($self, $ev) = @_; 1519 my ($self, $ev) = @_;
1304 1520
1306 my $sym = $ev->{sym}; 1522 my $sym = $ev->{sym};
1307 my $uni = $ev->{unicode}; 1523 my $uni = $ev->{unicode};
1308 1524
1309 my $text = $self->get_text; 1525 my $text = $self->get_text;
1310 1526
1311 if ($sym == 8) { 1527 if ($uni == 8) {
1312 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1528 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1313 } elsif ($sym == 127) { 1529 } elsif ($uni == 127) {
1314 substr $text, $self->{cursor}, 1, ""; 1530 substr $text, $self->{cursor}, 1, "";
1315 } elsif ($sym == CFClient::SDLK_LEFT) { 1531 } elsif ($sym == CFClient::SDLK_LEFT) {
1316 --$self->{cursor} if $self->{cursor}; 1532 --$self->{cursor} if $self->{cursor};
1317 } elsif ($sym == CFClient::SDLK_RIGHT) { 1533 } elsif ($sym == CFClient::SDLK_RIGHT) {
1318 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1534 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1319 } elsif ($sym == CFClient::SDLK_HOME) { 1535 } elsif ($sym == CFClient::SDLK_HOME) {
1320 $self->{cursor} = 0; 1536 $self->{cursor} = 0;
1321 } elsif ($sym == CFClient::SDLK_END) { 1537 } elsif ($sym == CFClient::SDLK_END) {
1322 $self->{cursor} = length $text; 1538 $self->{cursor} = length $text;
1323 } elsif ($sym == 27) { 1539 } elsif ($uni == 27) {
1324 $self->emit ('escape'); 1540 $self->emit ('escape');
1325 } elsif ($uni) { 1541 } elsif ($uni) {
1326 substr $text, $self->{cursor}++, 0, chr $uni; 1542 substr $text, $self->{cursor}++, 0, chr $uni;
1327 } 1543 }
1328 1544
1329 $self->_set_text ($text); 1545 $self->_set_text ($text);
1330 $self->update; 1546 $self->update;
1547 $self->check_size;
1331} 1548}
1332 1549
1333sub focus_in { 1550sub focus_in {
1334 my ($self) = @_; 1551 my ($self) = @_;
1335 1552
1411 my ($self, $ev) = @_; 1628 my ($self, $ev) = @_;
1412 1629
1413 my $sym = $ev->{sym}; 1630 my $sym = $ev->{sym};
1414 1631
1415 if ($sym == 13) { 1632 if ($sym == 13) {
1633 unshift @{$self->{history}},
1634 my $txt = $self->get_text;
1635 $self->{history_pointer} = -1;
1636 $self->{history_saveback} = '';
1416 $self->emit (activate => $self->get_text); 1637 $self->emit (activate => $txt);
1417 $self->update; 1638 $self->update;
1639
1640 } elsif ($sym == CFClient::SDLK_UP) {
1641 if ($self->{history_pointer} < 0) {
1642 $self->{history_saveback} = $self->get_text;
1643 }
1644 if (@{$self->{history} || []} > 0) {
1645 $self->{history_pointer}++;
1646 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1647 $self->{history_pointer} = @{$self->{history} || []} - 1;
1648 }
1649 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1650 }
1651
1652 } elsif ($sym == CFClient::SDLK_DOWN) {
1653 $self->{history_pointer}--;
1654 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1655
1656 if ($self->{history_pointer} >= 0) {
1657 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1658 } else {
1659 $self->set_text ($self->{history_saveback});
1660 }
1418 1661
1419 } else { 1662 } else {
1420 $self->SUPER::key_down ($ev); 1663 $self->SUPER::key_down ($ev);
1421 } 1664 }
1422 1665
1436 1679
1437sub new { 1680sub new {
1438 my $class = shift; 1681 my $class = shift;
1439 1682
1440 $class->SUPER::new ( 1683 $class->SUPER::new (
1441 padding => 4, 1684 padding => 4,
1442 fg => [1, 1, 1], 1685 fg => [1, 1, 1],
1443 bg => [1, 1, 1, 0.2],
1444 active_fg => [0, 0, 1], 1686 active_fg => [0, 0, 1],
1445 can_hover => 1, 1687 can_hover => 1,
1446 align => 0, 1688 align => 0,
1447 valign => 0, 1689 valign => 0,
1448 can_events => 1, 1690 can_events => 1,
1449 @_ 1691 @_
1450 ) 1692 )
1451} 1693}
1452 1694
1466 1708
1467 if ($GRAB == $self) { 1709 if ($GRAB == $self) {
1468 $self->{fg} = $self->{active_fg}; 1710 $self->{fg} = $self->{active_fg};
1469 } 1711 }
1470 1712
1471 glEnable GL_BLEND;
1472 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1473 glEnable GL_TEXTURE_2D; 1713 glEnable GL_TEXTURE_2D;
1474 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1714 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1475 glColor 0, 0, 0, 1; 1715 glColor 0, 0, 0, 1;
1476 1716
1477 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1717 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1478 1718
1479 glDisable GL_TEXTURE_2D; 1719 glDisable GL_TEXTURE_2D;
1480 glDisable GL_BLEND;
1481 1720
1482 $self->SUPER::_draw; 1721 $self->SUPER::_draw;
1483} 1722}
1484 1723
1485############################################################################# 1724#############################################################################
1499 1738
1500 $class->SUPER::new ( 1739 $class->SUPER::new (
1501 padding => 2, 1740 padding => 2,
1502 fg => [1, 1, 1], 1741 fg => [1, 1, 1],
1503 active_fg => [1, 1, 0], 1742 active_fg => [1, 1, 0],
1743 bg => [0, 0, 0, 0.2],
1744 active_bg => [1, 1, 1, 0.5],
1504 state => 0, 1745 state => 0,
1505 can_hover => 1, 1746 can_hover => 1,
1506 @_ 1747 @_
1507 ) 1748 )
1508} 1749}
1532 1773
1533 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1774 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1534 1775
1535 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1776 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1536 1777
1537 glEnable GL_BLEND; 1778 my $tex = $self->{state} ? $tex[1] : $tex[0];
1779
1538 glEnable GL_TEXTURE_2D; 1780 glEnable GL_TEXTURE_2D;
1539 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1540
1541 my $tex = $self->{state} ? $tex[1] : $tex[0];
1542
1543 $tex->draw_quad (0, 0, $s, $s); 1781 $tex->draw_quad_alpha (0, 0, $s, $s);
1544
1545 glDisable GL_TEXTURE_2D; 1782 glDisable GL_TEXTURE_2D;
1546 glDisable GL_BLEND;
1547} 1783}
1548 1784
1549############################################################################# 1785#############################################################################
1550 1786
1551package CFClient::UI::Image; 1787package CFClient::UI::Image;
1594 glTranslate 0, -$self->{w}, 0; 1830 glTranslate 0, -$self->{w}, 0;
1595 1831
1596 ($w, $h) = ($h, $w); 1832 ($w, $h) = ($h, $w);
1597 } 1833 }
1598 1834
1599 glEnable GL_BLEND;
1600 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1601 glEnable GL_TEXTURE_2D; 1835 glEnable GL_TEXTURE_2D;
1602 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1836 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1603 1837
1604 $tex->draw_quad (0, 0, $w, $h); 1838 $tex->draw_quad_alpha (0, 0, $w, $h);
1605 1839
1606 glDisable GL_BLEND;
1607 glDisable GL_TEXTURE_2D; 1840 glDisable GL_TEXTURE_2D;
1608} 1841}
1609 1842
1610############################################################################# 1843#############################################################################
1611 1844
1659} 1892}
1660 1893
1661sub set_max { 1894sub set_max {
1662 my ($self, $max) = @_; 1895 my ($self, $max) = @_;
1663 1896
1897 return if $self->{max_val} == $max;
1898
1664 $self->{max_val} = $max; 1899 $self->{max_val} = $max;
1900 $self->update;
1665} 1901}
1666 1902
1667sub set_value { 1903sub set_value {
1668 my ($self, $val, $max) = @_; 1904 my ($self, $val, $max) = @_;
1669 1905
1670 $self->set_max ($max) 1906 $self->set_max ($max)
1671 if defined $max; 1907 if defined $max;
1672 1908
1673 $max = $self->{max_val}; 1909 return if $self->{val} == $val;
1910
1674 $self->{val} = $val; 1911 $self->{val} = $val;
1675
1676 $self->update; 1912 $self->update;
1677} 1913}
1678 1914
1679sub _draw { 1915sub _draw {
1680 my ($self) = @_; 1916 my ($self) = @_;
1743 1979
1744sub new { 1980sub new {
1745 my ($class, %arg) = @_; 1981 my ($class, %arg) = @_;
1746 1982
1747 my $self = $class->SUPER::new ( 1983 my $self = $class->SUPER::new (
1748 tooltip => $arg{type}, 1984 tooltip => $arg{type},
1985 can_hover => 1,
1986 can_events => 1,
1749 %arg, 1987 %arg,
1750 ); 1988 );
1751 1989
1752 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1); 1990 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1753 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1); 1991 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1754 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1); 1992 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1755 1993
1756 $self 1994 $self
1757} 1995}
1758 1996
1759sub set_fontsize { 1997sub set_fontsize {
1761 1999
1762 $self->{value}->set_fontsize ($fsize); 2000 $self->{value}->set_fontsize ($fsize);
1763 $self->{max} ->set_fontsize ($fsize); 2001 $self->{max} ->set_fontsize ($fsize);
1764} 2002}
1765 2003
2004sub set_max {
2005 my ($self, $max) = @_;
2006
2007 $self->{gauge}->set_max ($max);
2008 $self->{max}->set_text ($max);
2009}
2010
1766sub set_value { 2011sub set_value {
1767 my ($self, $val, $max) = @_; 2012 my ($self, $val, $max) = @_;
1768 2013
1769 $self->set_max ($max) 2014 $self->set_max ($max)
1770 if defined $max; 2015 if defined $max;
1771 2016
1772 $self->{gauge}->set_value ($val, $max); 2017 $self->{gauge}->set_value ($val, $max);
1773 $self->{value}->set_text ($val); 2018 $self->{value}->set_text ($val);
1774}
1775
1776sub set_max {
1777 my ($self, $max) = @_;
1778
1779 $self->{gauge}->set_max ($max);
1780 $self->{max}->set_text ($max);
1781} 2019}
1782 2020
1783############################################################################# 2021#############################################################################
1784 2022
1785package CFClient::UI::Slider; 2023package CFClient::UI::Slider;
1795 qw(s1_slider.png s1_slider_bg.png); 2033 qw(s1_slider.png s1_slider_bg.png);
1796 2034
1797sub new { 2035sub new {
1798 my $class = shift; 2036 my $class = shift;
1799 2037
1800 # range [value, low, high, page] 2038 # range [value, low, high, page, unit]
1801 2039
1802 # TODO: 0-width page 2040 # TODO: 0-width page
1803 # TODO: req_w/h are wrong with vertical 2041 # TODO: req_w/h are wrong with vertical
1804 # TODO: calculations are off 2042 # TODO: calculations are off
1805 my $self = $class->SUPER::new ( 2043 my $self = $class->SUPER::new (
1806 fg => [1, 1, 1], 2044 fg => [1, 1, 1],
1807 active_fg => [0, 0, 0], 2045 active_fg => [0, 0, 0],
2046 bg => [0, 0, 0, 0.2],
2047 active_bg => [1, 1, 1, 0.5],
1808 range => [0, 0, 100, 10], 2048 range => [0, 0, 100, 10],
1809 req_w => 20, 2049 req_w => $::WIDTH / 80,
1810 req_h => 20, 2050 req_h => $::WIDTH / 80,
1811 vertical => 0, 2051 vertical => 0,
1812 can_hover => 1, 2052 can_hover => 1,
1813 inner_pad => 5, 2053 inner_pad => .05,
1814 @_ 2054 @_
1815 ); 2055 );
1816 2056
2057 $self->set_value ($self->{range}[0]);
2058 $self->update;
2059
1817 $self 2060 $self
2061}
2062
2063sub set_value {
2064 my ($self, $value) = @_;
2065
2066 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2067
2068 $hi = $lo + 1 if $hi <= $lo;
2069
2070 $value = $lo if $value < $lo;
2071 $value = $hi if $value > $hi;
2072
2073 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2074 if $unit;
2075
2076 $page = $hi - $lo if $page > $hi - $lo;
2077
2078 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2079
2080 if ($value != $old_value) {
2081 $self->emit (changed => $value);
2082 $self->update;
2083 }
1818} 2084}
1819 2085
1820sub size_request { 2086sub size_request {
1821 my ($self) = @_; 2087 my ($self) = @_;
1822 2088
1835 2101
1836sub mouse_motion { 2102sub mouse_motion {
1837 my ($self, $ev, $x, $y) = @_; 2103 my ($self, $ev, $x, $y) = @_;
1838 2104
1839 if ($GRAB == $self) { 2105 if ($GRAB == $self) {
2106 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2107
2108 my (undef, $lo, $hi, $page) = @{$self->{range}};
2109
2110 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2111
2112 $self->set_value ($x * ($hi - $lo) + $lo);
2113 }
2114}
2115
2116sub update {
2117 my ($self) = @_;
2118
2119 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2120 $self->set_value ($self->{range}[0]);
2121
1840 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2122 my ($value, $lo, $hi, $page) = @{$self->{range}};
1841 2123
1842 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2124 my $inner_w = 1 - 2 * $self->{inner_pad};
1843 2125
1844 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2126 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
1845 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1846 2127
1847 $x -= $inner_pad_px; # substract the padding 2128 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
1848 $x = $x * ($hi - $lo) / $inner_w + $lo; 2129 $value = $self->{scale} * ($value - $lo);
1849 $x = $lo if $x < $lo;
1850 $x = $hi - $page if $x > $hi - $page;
1851 $self->{range}[0] = $x;
1852 2130
1853 $self->emit (changed => $x); 2131 $value = $self->{inner_pad} + ($value - $page * 0.5);
1854 $self->update; 2132
2133 $value = 0 if $value < 0;
2134 $page = 1 - $value if $value + $page > 1;
2135
2136 $self->{knob_x} = $value;
2137 $self->{knob_w} = $page;
1855 } 2138 });
1856}
1857 2139
1858# the inner_* stuff is for generating a padding for the slider handle, 2140 $self->SUPER::update;
1859# so that the handle doesn't leave the texture. This calculation isn't 100%
1860# correct propably, but it does the job for now
1861sub _calc_inner_pad_px {
1862 my ($self, $w) = @_;
1863 ($w / 100) * $self->{inner_pad} # % to pixels
1864} 2141}
1865 2142
1866sub _draw { 2143sub _draw {
1867 my ($self) = @_; 2144 my ($self) = @_;
1868 2145
1869 $self->SUPER::_draw (); 2146 $self->SUPER::_draw ();
1870 2147
1871 my ($w, $h) = @$self{qw(w h)}; 2148 glScale $self->{w}, $self->{h};
1872 2149
1873 if ($self->{vertical}) { 2150 if ($self->{vertical}) {
1874 # draw a vertical slider like a rotated horizontal slider 2151 # draw a vertical slider like a rotated horizontal slider
1875 2152
1876 glRotate 90, 0, 0, 1; 2153 glRotate 90, 0, 0, 1;
1877 glTranslate 0, -$self->{w}, 0; 2154 glTranslate 0, 1, 0;
1878
1879 ($w, $h) = ($h, $w);
1880 } 2155 }
1881 2156
1882 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2157 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1883 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2158 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1884 2159
1885 my ($value, $lo, $hi, $page) = @{$self->{range}};
1886
1887 $hi = $value + 1 if $lo == $hi;
1888
1889 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1890 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1891
1892 $page = int $page * $inner_w / ($hi - $lo);
1893 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1894
1895 $w -= $page;
1896 $page &= ~1;
1897 glTranslate $page * 0.5, 0, 0;
1898 $page ||= 2;
1899
1900 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1901 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1902
1903 glEnable GL_BLEND;
1904 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1905 glEnable GL_TEXTURE_2D; 2160 glEnable GL_TEXTURE_2D;
1906 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2161 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1907 2162
1908 # draw background 2163 # draw background
1909 $tex[1]->draw_quad (0, 0, $w, $h); 2164 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
1910 2165
1911 # draw handle 2166 # draw handle
1912 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2167 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
1913 2168
1914 glDisable GL_BLEND;
1915 glDisable GL_TEXTURE_2D; 2169 glDisable GL_TEXTURE_2D;
1916} 2170}
1917 2171
1918############################################################################# 2172#############################################################################
1919 2173
1925 2179
1926sub new { 2180sub new {
1927 my $class = shift; 2181 my $class = shift;
1928 2182
1929 my $self = $class->SUPER::new ( 2183 my $self = $class->SUPER::new (
1930 fontsize => 1, 2184 fontsize => 1,
2185 can_events => 0,
1931 #font => default_font 2186 #font => default_font
1932 @_, 2187 @_,
1933 2188
1934 layout => (new CFClient::Layout), 2189 layout => (new CFClient::Layout 1),
1935 par => [], 2190 par => [],
1936 height => 0, 2191 height => 0,
1937 children => [ 2192 children => [
1938 (new CFClient::UI::Empty expand => 1), 2193 (new CFClient::UI::Empty expand => 1),
1939 (new CFClient::UI::Slider vertical => 1), 2194 (new CFClient::UI::Slider vertical => 1),
1940 ], 2195 ],
1941 ); 2196 );
1942 2197
1943 $self->{children}[1]->connect (changed => sub { 2198 $self->{children}[1]->connect (changed => sub { $self->update });
1944 $self->update;
1945 });
1946 2199
1947 $self 2200 $self
1948} 2201}
1949 2202
1950sub set_fontsize { 2203sub set_fontsize {
1958 my ($self, $text) = @_; 2211 my ($self, $text) = @_;
1959 2212
1960 my $layout = $self->{layout}; 2213 my $layout = $self->{layout};
1961 2214
1962 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2215 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1963 $layout->set_width ($self->{w}); 2216 $layout->set_width ($self->{children}[0]{w});
1964 $layout->set_text ($text); 2217 $layout->set_markup ($text);
1965 2218
1966 ($layout->size)[1] 2219 ($layout->size)[1]
1967} 2220}
1968 2221
1969sub reflow { 2222sub reflow {
2007 2260
2008 return unless $self->{h} > 0; 2261 return unless $self->{h} > 0;
2009 2262
2010 delete $self->{texture}; 2263 delete $self->{texture};
2011 2264
2012 $ROOT->on_refresh ($self, sub { 2265 $ROOT->on_post_alloc ($self, sub {
2013 if (delete $self->{need_reflow}) { 2266 if (delete $self->{need_reflow}) {
2014 my $height = 0; 2267 my $height = 0;
2015 2268
2016 $height += $_->[0] = $self->text_height ($_->[2]) 2269 $height += $_->[0] = $self->text_height ($_->[2])
2017 for @{$self->{par}}; 2270 for @{$self->{par}};
2021 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2274 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2022 2275
2023 delete $self->{texture}; 2276 delete $self->{texture};
2024 } 2277 }
2025 2278
2026 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2279 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2027 glClearColor 0, 0, 0, 1; 2280 glClearColor 0, 0, 0, 0;
2028 glClear GL_COLOR_BUFFER_BIT; 2281 glClear GL_COLOR_BUFFER_BIT;
2029 2282
2030 glEnable GL_BLEND;
2031 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2032 glEnable GL_TEXTURE_2D; 2283 glEnable GL_TEXTURE_2D;
2033 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2284 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2034 2285
2035 my $top = int $self->{children}[1]{range}[0]; 2286 my $top = int $self->{children}[1]{range}[0];
2036 2287
2045 2296
2046 for my $par (@{$self->{par}}) { 2297 for my $par (@{$self->{par}}) {
2047 my $h = $par->[0]; 2298 my $h = $par->[0];
2048 2299
2049 if ($y0 < $y + $h && $y < $y1) { 2300 if ($y0 < $y + $h && $y < $y1) {
2301 $layout->set_foreground (@{ $par->[1] });
2050 $layout->set_text ($par->[2]); 2302 $layout->set_markup ($par->[2]);
2051 2303
2052 glColor @{ $par->[1] };
2053 my ($W, $H) = $layout->size; 2304 my ($W, $H) = $layout->size;
2054 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2305 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2055 } 2306 }
2056 2307
2057 $y += $h; 2308 $y += $h;
2058 } 2309 }
2059 2310
2060 glDisable GL_TEXTURE_2D; 2311 glDisable GL_TEXTURE_2D;
2061 glDisable GL_BLEND;
2062 }; 2312 };
2063 }); 2313 });
2064} 2314}
2065 2315
2066sub _draw { 2316sub _draw {
2067 my ($self) = @_; 2317 my ($self) = @_;
2068 2318
2069 if ($self->{texture}) {
2070 glEnable GL_TEXTURE_2D; 2319 glEnable GL_TEXTURE_2D;
2071 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2320 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2072 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2321 glColor 1, 1, 1, 1;
2322 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2073 glDisable GL_TEXTURE_2D; 2323 glDisable GL_TEXTURE_2D;
2074 }
2075 2324
2076 $self->{children}[1]->draw; 2325 $self->{children}[1]->draw;
2077 2326
2078} 2327}
2079 2328
2127 2376
2128sub new { 2377sub new {
2129 my $class = shift; 2378 my $class = shift;
2130 2379
2131 my $self = $class->SUPER::new ( 2380 my $self = $class->SUPER::new (
2132 state => 0, 2381 state => 0,
2133 connect_activate => \&toggle_flopper, 2382 connect_activate => \&toggle_flopper,
2134 can_events => 1,
2135 @_ 2383 @_
2136 ); 2384 );
2137 2385
2138 if ($self->{state}) { 2386 if ($self->{state}) {
2139 $self->{state} = 0; 2387 $self->{state} = 0;
2174 @_, 2422 @_,
2175 can_events => 0, 2423 can_events => 0,
2176 ) 2424 )
2177} 2425}
2178 2426
2179sub set_text { 2427sub set_tooltip_from {
2180 my ($self, $text) = @_; 2428 my ($self, $widget) = @_;
2181 2429
2182 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2430 $self->add (new CFClient::UI::Label
2183 $self->{label}->set_text ($text); 2431 markup => $widget->{tooltip},
2184 $self->add ($self->{label}); 2432 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2433 fontsize => 0.8,
2434 fg => [0, 0, 0, 1],
2435 font => ($widget->{tooltip_font} || $::FONT_PROP),
2436 );
2185} 2437}
2186 2438
2187sub size_request { 2439sub size_request {
2188 my ($self) = @_; 2440 my ($self) = @_;
2189 2441
2190 $self->child->set_max_size ($::WIDTH * 0.2);
2191
2192 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2442 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2193 2443
2194 ($w + 4, $h + 4) 2444 ($w + 4, $h + 4)
2195} 2445}
2196 2446
2447sub size_allocate {
2448 my ($self, $w, $h) = @_;
2449
2450 $self->SUPER::size_allocate ($w - 4, $h - 4);
2451}
2452
2197sub _draw { 2453sub _draw {
2198 my ($self) = @_; 2454 my ($self) = @_;
2199 2455
2200 glPushMatrix;
2201 glTranslate 0.375, 0.375; 2456 glTranslate 0.375, 0.375;
2202 2457
2203 my ($w, $h) = @$self{qw(w h)}; 2458 my ($w, $h) = @$self{qw(w h)};
2204 2459
2205 glColor 1, 0.8, 0.4; 2460 glColor 1, 0.8, 0.4;
2216 glVertex 0 , $h; 2471 glVertex 0 , $h;
2217 glVertex $w, $h; 2472 glVertex $w, $h;
2218 glVertex $w, 0; 2473 glVertex $w, 0;
2219 glEnd; 2474 glEnd;
2220 2475
2221 glPopMatrix; 2476 glTranslate 2 - 0.375, 2 - 0.375;
2222
2223 glTranslate 2, 2;
2224 $self->SUPER::_draw; 2477 $self->SUPER::_draw;
2225} 2478}
2226 2479
2227############################################################################# 2480#############################################################################
2228 2481
2482package CFClient::UI::Face;
2483
2484our @ISA = CFClient::UI::Base::;
2485
2486use CFClient::OpenGL;
2487
2488sub new {
2489 my $class = shift;
2490
2491 $class->SUPER::new (
2492 aspect => 1,
2493 @_,
2494 )
2495}
2496
2497sub size_request {
2498 (32, 8)
2499}
2500
2501sub _draw {
2502 my ($self) = @_;
2503
2504 return unless $::CONN;#d# manage and cache textures differently
2505 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2506
2507 # TODO animation
2508 if ($tex) {
2509 glEnable GL_TEXTURE_2D;
2510 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2511 glColor 1, 1, 1, 1;
2512 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2513 glDisable GL_TEXTURE_2D;
2514 }
2515}
2516
2517#############################################################################
2518
2519package CFClient::UI::InventoryItem;
2520
2521our @ISA = CFClient::UI::HBox::;
2522
2523sub new {
2524 my $class = shift;
2525
2526 my %args = @_;
2527
2528 my $item = delete $args{item};
2529
2530 my $desc = $item->{nrof} < 2
2531 ? $item->{name}
2532 : "$item->{nrof} $item->{name_pl}";
2533
2534 $item->{flags} & Crossfire::Protocol::F_OPEN
2535 and $desc .= " (open)";
2536 $item->{flags} & Crossfire::Protocol::F_APPLIED
2537 and $desc .= " (applied)";
2538 $item->{flags} & Crossfire::Protocol::F_UNPAID
2539 and $desc .= " (unpaid)";
2540 $item->{flags} & Crossfire::Protocol::F_MAGIC
2541 and $desc .= " (magic)";
2542 $item->{flags} & Crossfire::Protocol::F_CURSED
2543 and $desc .= " (cursed)";
2544 $item->{flags} & Crossfire::Protocol::F_DAMNED
2545 and $desc .= " (damned)";
2546 $item->{flags} & Crossfire::Protocol::F_LOCKED
2547 and $desc .= " *";
2548
2549 my $self = $class->SUPER::new (
2550 can_hover => 1,
2551 can_events => 1,
2552 tooltip => ((CFClient::UI::Label::escape $desc)
2553 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2554 connect_button_down => sub {
2555 my ($self, $ev, $x, $y) = @_;
2556
2557 # todo: maybe put examine on 1? but should just be a tooltip :(
2558 if ($ev->{button} == 1) {
2559 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2560 } elsif ($ev->{button} == 2) {
2561 $::CONN->send ("apply $item->{tag}");
2562 } elsif ($ev->{button} == 3) {
2563 my @menu_items = (
2564 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2565 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2566 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2567 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2568 [
2569 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2570 sub { $::CONN->send ("lock ". () ."$item->{tag}") },
2571 ],
2572 );
2573
2574 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2575 }
2576
2577 1
2578 },
2579 %args
2580 );
2581
2582 $self->add (new CFClient::UI::Face
2583 can_events => 0,
2584 face => $item->{face},
2585 anim => $item->{anim},
2586 animspeed => $item->{animspeed},
2587 );
2588
2589 $self->add (new CFClient::UI::Label
2590 can_events => 0,
2591 text => $desc,
2592 );
2593
2594 $self
2595}
2596
2597#############################################################################
2598
2599package CFClient::UI::Inventory;
2600
2601our @ISA = CFClient::UI::ScrolledWindow::;
2602
2603sub new {
2604 my $class = shift;
2605
2606 my $self = $class->SUPER::new (
2607 scrolled => (new CFClient::UI::VBox),
2608 @_,
2609 );
2610
2611 $self
2612}
2613
2614sub set_items {
2615 my ($self, $items) = @_;
2616
2617 $self->{scrolled}->clear;
2618 return unless $items;
2619
2620 my @items = sort {
2621 ($a->{type} <=> $b->{type})
2622 or ($a->{name} cmp $b->{name})
2623 } @$items;
2624
2625 $self->{real_items} = \@items;
2626
2627 for my $item (@items) {
2628 my $desc = $item->{nrof} < 2
2629 ? $item->{name}
2630 : "$item->{nrof} $item->{name_pl}";
2631
2632 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2633 }
2634
2635 $self->{scrolled}->add (@items);
2636
2637# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2638}
2639
2640sub size_request {
2641 my ($self) = @_;
2642 ($self->{req_w}, $self->{req_h});
2643}
2644
2645#############################################################################
2646
2647package CFClient::UI::Menu;
2648
2649our @ISA = CFClient::UI::FancyFrame::;
2650
2651use CFClient::OpenGL;
2652
2653sub new {
2654 my $class = shift;
2655
2656 my $self = $class->SUPER::new (
2657 items => [],
2658 z => 100,
2659 @_,
2660 );
2661
2662 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2663
2664 for my $item (@{ $self->{items} }) {
2665 my ($widget, $cb) = @$item;
2666
2667 # handle various types of items, only text for now
2668 if (!ref $widget) {
2669 $widget = new CFClient::UI::Label
2670 can_hover => 1,
2671 can_events => 1,
2672 text => $widget;
2673 }
2674
2675 $self->{item}{$widget} = $item;
2676
2677 $self->{vbox}->add ($widget);
2678 }
2679
2680 $self
2681}
2682
2683# popup given the event (must be a mouse button down event currently)
2684sub popup {
2685 my ($self, $ev) = @_;
2686
2687 $self->emit ("popdown");
2688
2689 # maybe save $GRAB? must be careful about events...
2690 $GRAB = $self;
2691 $self->{button} = $ev->{button};
2692
2693 $self->show;
2694 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2695}
2696
2697sub mouse_motion {
2698 my ($self, $ev, $x, $y) = @_;
2699
2700 # TODO: should use vbox->find_widget or so
2701 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2702 $self->{hover} = $self->{item}{$HOVER};
2703}
2704
2705sub button_up {
2706 my ($self, $ev, $x, $y) = @_;
2707
2708 if ($ev->{button} == $self->{button}) {
2709 undef $GRAB;
2710 $self->hide;
2711
2712 $self->emit ("popdown");
2713 $self->{hover}[1]->() if $self->{hover};
2714 }
2715}
2716
2717#############################################################################
2718
2719package CFClient::UI::Statusbox;
2720
2721our @ISA = CFClient::UI::VBox::;
2722
2723sub reorder {
2724 my ($self) = @_;
2725 my $NOW = time;
2726
2727 while (my ($k, $v) = each %{ $self->{item} }) {
2728 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2729 }
2730
2731 my @widgets;
2732
2733 my @items = sort {
2734 $a->{pri} <=> $b->{pri}
2735 or $b->{id} <=> $a->{id}
2736 } values %{ $self->{item} };
2737
2738 my $count = 10 + 1;
2739 for my $item (@items) {
2740 last unless --$count;
2741
2742 push @widgets, $item->{label} ||= do {
2743 # TODO: doesn't handle markup well (read as: at all)
2744 my $short = $item->{count} > 1
2745 ? "<b>$item->{count} ×</b> $item->{text}"
2746 : $item->{text};
2747
2748 for ($short) {
2749 s/^\s+//;
2750 s/\s+/ /g;
2751 my $len = int 40 / $item->{fontsize};
2752 substr $_, $len, length, "…" if $len < length;
2753 }
2754
2755 new CFClient::UI::Label
2756 markup => $short,
2757 tooltip => $item->{tooltip},
2758 tooltip_font => $::FONT_PROP,
2759 tooltip_width => 0.67,
2760 fontsize => $item->{fontsize},
2761 fg => $item->{fg},
2762 can_events => 1,
2763 can_hover => 1
2764 };
2765 }
2766
2767 $self->clear;
2768 $self->SUPER::add (reverse @widgets);
2769}
2770
2771sub add {
2772 my ($self, $text, %arg) = @_;
2773
2774 $text =~ s/^\s+//;
2775 $text =~ s/\s+$//;
2776
2777 my $timeout = time + ((delete $arg{timeout}) || 60);
2778
2779 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2780
2781 if (my $item = $self->{item}{$group}) {
2782 if ($item->{text} eq $text) {
2783 $item->{count}++;
2784 } else {
2785 $item->{count} = 1;
2786 $item->{text} = $item->{tooltip} = $text;
2787 }
2788 $item->{id} = ++$self->{id};
2789 $item->{timeout} = $timeout;
2790 delete $item->{label};
2791 } else {
2792 $self->{item}{$group} = {
2793 id => ++$self->{id},
2794 text => $text,
2795 timeout => $timeout,
2796 tooltip => $text,
2797 fontsize => 0.8,
2798 fg => [0.8, 0.8, 0.8, 0.8],
2799 pri => 0,
2800 count => 1,
2801 %arg,
2802 };
2803 }
2804
2805 $self->reorder;
2806}
2807
2808#############################################################################
2809
2229package CFClient::UI::Root; 2810package CFClient::UI::Root;
2230 2811
2231our @ISA = CFClient::UI::Container::; 2812our @ISA = CFClient::UI::Container::;
2232 2813
2233use CFClient::OpenGL; 2814use CFClient::OpenGL;
2234 2815
2235sub check_size { 2816sub new {
2236 my ($self) = @_; 2817 my $class = shift;
2237 2818
2238 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2819 $class->SUPER::new (
2239} 2820 @_,
2240 2821 )
2241sub size_request {
2242 ($::WIDTH, $::HEIGHT)
2243} 2822}
2244 2823
2245sub configure { 2824sub configure {
2246 my ($self, $x, $y, $w, $h) = @_; 2825 my ($self, $x, $y, $w, $h) = @_;
2247 2826
2248 $self->SUPER::configure ($x, $y, $w, $h); 2827 $self->{w} = $w;
2828 $self->{h} = $h;
2829}
2249 2830
2831sub check_size {
2832 my ($self) = @_;
2833
2834 $self->size_allocate ($self->{w}, $self->{h})
2835 if $self->{w};
2836}
2837
2838sub size_request {
2839 my ($self) = @_;
2840
2841 ($self->{w}, $self->{h})
2842}
2843
2844sub size_allocate {
2845 my ($self, $w, $h) = @_;
2846
2847 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2848 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2849
2850 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2851 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2852
2250 for my $child (@{$self->{children}}) { 2853 for my $child ($self->children) {
2251 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2854 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2252 2855
2856 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2857 if exists $child->{req_x};
2858
2859 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2860 if exists $child->{req_y};
2861
2253 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2862 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2254 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2863 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2864
2255 $child->configure ($X, $Y, $W,$H); 2865 $child->configure ($X, $Y, $W, $H);
2256 } 2866 }
2257} 2867}
2258 2868
2259sub _topleft { 2869sub coord2local {
2260 my ($self, $x, $y) = @_; 2870 my ($self, $x, $y) = @_;
2261 2871
2262 ($x, $y) 2872 ($x, $y)
2263} 2873}
2264 2874
2875sub coord2global {
2876 my ($self, $x, $y) = @_;
2877
2878 ($x, $y)
2879}
2880
2265sub update { 2881sub update {
2266 my ($self) = @_; 2882 my ($self) = @_;
2267 2883
2268 $self->check_size; 2884 $self->check_size;
2269 ::refresh (); 2885 $::WANT_REFRESH++;
2270} 2886}
2271 2887
2272sub add { 2888sub add {
2273 my ($self, $child) = @_; 2889 my ($self, @children) = @_;
2274 2890
2891 for my $child (@children) {
2892 $child->{toplevel} = 1;
2893
2275 # integerize window positions 2894 # integerise window positions
2276 $child->{x} = int $child->{x}; 2895 $child->{x} = int $child->{x};
2277 $child->{y} = int $child->{y}; 2896 $child->{y} = int $child->{y};
2897 }
2278 2898
2279 $self->SUPER::add ($child); 2899 $self->SUPER::add (@children);
2280} 2900}
2281 2901
2282sub on_refresh { 2902sub on_refresh {
2283 my ($self, $id, $cb) = @_; 2903 my ($self, $id, $cb) = @_;
2284 2904
2285 $self->{refresh_hook}{$id} = $cb; 2905 $self->{refresh_hook}{$id} = $cb;
2286} 2906}
2287 2907
2908sub on_post_alloc {
2909 my ($self, $id, $cb) = @_;
2910
2911 $self->{post_alloc_hook}{$id} = $cb;
2912}
2913
2288sub draw { 2914sub draw {
2289 my ($self) = @_; 2915 my ($self) = @_;
2290 2916
2291 while (my $rcb = delete $self->{refresh_hook}) { 2917 while ($self->{refresh_hook}) {
2292 $_->() for values %$rcb; 2918 $_->()
2919 for values %{delete $self->{refresh_hook}};
2920 }
2921
2922 if ($self->{check_size}) {
2923 my @queue = ([], []);
2924
2925 for (;;) {
2926 if ($self->{check_size}) {
2927 # heuristic: check containers last
2928 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2929 for values %{delete $self->{check_size}}
2930 }
2931
2932 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2933
2934 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2935 ? @$widget{qw(user_w user_h)}
2936 : $widget->size_request;
2937
2938 if (delete $widget->{force_alloc}
2939 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2940 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2941
2942 $widget->{req_w} = $w;
2943 $widget->{req_h} = $h;
2944
2945 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2946
2947 $widget->{parent}->check_size
2948 if $widget->{parent};
2949 }
2950 }
2951 }
2952
2953 while ($self->{size_alloc}) {
2954 for (values %{delete $self->{size_alloc}}) {
2955 my ($widget, $w, $h) = @$_;
2956
2957 $w = 0 if $w < 0;
2958 $h = 0 if $h < 0;
2959
2960 $widget->{w} = $w;
2961 $widget->{h} = $h;
2962 $widget->size_allocate ($w, $h);
2963 $widget->emit (size_allocate => $w, $h);
2964 }
2965 }
2966
2967 while ($self->{post_alloc_hook}) {
2968 $_->()
2969 for values %{delete $self->{post_alloc_hook}};
2293 } 2970 }
2294 2971
2295 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2972 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2296 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2973 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2297 glClear GL_COLOR_BUFFER_BIT; 2974 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines