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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines