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.170 by root, Mon Apr 24 11:45:17 2006 UTC vs.
Revision 1.214 by root, Wed May 17 22:59:13 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_markup ($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 }
122 133
123 for (@$vals) { 134 for (@$vals) {
124 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
125 $rem += $_ - $i; 136 $rem += $_ - $i;
126 $_ = $i; 137 $_ = $i;
138 }
139}
140
141# call when resolution changes etc.
142sub rescale_widgets {
143 my ($sx, $sy) = @_;
144
145 # make a copy, otherwise for complains about freed values.
146 my @widgets = values %WIDGET;
147
148 for my $widget (@widgets) {
149 if ($widget->{toplevel}) {
150 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
151 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
152 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
153 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
154 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
155 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
156 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
157 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
158 }
159
160 $widget->reconfigure;
127 } 161 }
128} 162}
129 163
130############################################################################# 164#############################################################################
131 165
150 if (/^connect_(.*)$/) { 184 if (/^connect_(.*)$/) {
151 $self->connect ($1 => delete $self->{$_}); 185 $self->connect ($1 => delete $self->{$_});
152 } 186 }
153 } 187 }
154 188
189 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
190
155 $self 191 $self
156} 192}
157 193
158sub destroy { 194sub destroy {
159 my ($self) = @_; 195 my ($self) = @_;
224 $self->{y} = $y; 260 $self->{y} = $y;
225 $self->update; 261 $self->update;
226 } 262 }
227 263
228 if ($self->{w} != $w || $self->{h} != $h) { 264 if ($self->{w} != $w || $self->{h} != $h) {
229 $self->{w} = $w; 265 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
230 $self->{h} = $h;
231
232 $self->size_allocate ($w, $h);
233 $self->update;
234 } 266 }
235} 267}
236 268
237sub size_allocate { 269sub size_allocate {
238 # nothing to be done 270 # nothing to be done
239} 271}
240 272
273sub reconfigure {
274 my ($self) = @_;
275
276 $self->check_size (1);
277 $self->update;
278}
279
280sub children {
281}
282
241sub set_max_size { 283sub set_max_size {
242 my ($self, $w, $h) = @_; 284 my ($self, $w, $h) = @_;
243 285
244 delete $self->{max_w}; $self->{max_w} = $w if $w; 286 delete $self->{max_w}; $self->{max_w} = $w if $w;
245 delete $self->{max_h}; $self->{max_h} = $h if $h; 287 delete $self->{max_h}; $self->{max_h} = $h if $h;
246} 288}
247 289
248# return top left coordinates 290sub set_tooltip {
249sub _topleft {
250 my ($self, $x, $y) = @_; 291 my ($self, $tooltip) = @_;
251 292
252 $self->{parent} 293 $self->{tooltip} = $tooltip;
253 or Carp::confess "no parent widget in _topleft\n";#d#
254 294
255 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 295 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
296 delete $CFClient::UI::TOOLTIP->{owner};
297 CFClient::UI::check_tooltip;
298 }
256} 299}
257 300
258# translate global coordinates to local coordinate system 301# translate global coordinates to local coordinate system
259sub coord2local { 302sub coord2local {
260 my ($self, $x, $y) = @_; 303 my ($self, $x, $y) = @_;
261 304
262 my ($X, $Y) = $self->_topleft; 305 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
263 ($x - $X, $y - $Y)
264} 306}
265 307
266# translate local coordinates to global coordinate system 308# translate local coordinates to global coordinate system
267sub coord2global { 309sub coord2global {
268 my ($self, $x, $y) = @_; 310 my ($self, $x, $y) = @_;
269 311
270 my ($X, $Y) = $self->_topleft; 312 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
271 ($x + $X, $y + $Y)
272} 313}
273 314
274sub focus_in { 315sub focus_in {
275 my ($self) = @_; 316 my ($self) = @_;
276 317
348 glVertex $self->{w}, 0; 389 glVertex $self->{w}, 0;
349 glVertex $self->{w}, $self->{h}; 390 glVertex $self->{w}, $self->{h};
350 glVertex 0 , $self->{h}; 391 glVertex 0 , $self->{h};
351 glEnd; 392 glEnd;
352 glPopMatrix; 393 glPopMatrix;
353 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 394 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
354 } 395 }
355} 396}
356 397
357sub _draw { 398sub _draw {
358 my ($self) = @_; 399 my ($self) = @_;
374 415
375sub set_parent { 416sub set_parent {
376 my ($self, $parent) = @_; 417 my ($self, $parent) = @_;
377 418
378 Scalar::Util::weaken ($self->{parent} = $parent); 419 Scalar::Util::weaken ($self->{parent} = $parent);
420
421 # TODO: req_w _does_change after ->reconfigure
422 $self->check_size
423 unless exists $self->{req_w};
379} 424}
380 425
381sub check_size { 426sub check_size {
382 my ($self) = @_; 427 my ($self, $forced) = @_;
383 428
384 $self->{parent} 429 $self->{force_alloc} = 1 if $forced;
385 or return 1; 430 $CFClient::UI::ROOT->{check_size}{$self} = $self;
386
387 my ($w, $h) = $self->{user_w} && $self->{user_h}
388 ? @$self{qw(user_w user_h)}
389 : $self->size_request;
390
391 if ($w != $self->{req_w} || $h != $self->{req_h}) {
392 $self->{req_w} = $w;
393 $self->{req_h} = $h;
394
395 $self->{parent}->check_size
396 or $self->size_allocate (
397 (List::Util::max $self->{w}, $w),
398 (List::Util::max $self->{h}, $h),
399 );
400
401 1
402 } else {
403 0
404 }
405} 431}
406 432
407sub update { 433sub update {
408 my ($self) = @_; 434 my ($self) = @_;
409 435
424} 450}
425 451
426sub DESTROY { 452sub DESTROY {
427 my ($self) = @_; 453 my ($self) = @_;
428 454
455 delete $WIDGET{$self+0};
429 #$self->deactivate; 456 #$self->deactivate;
430} 457}
431 458
432############################################################################# 459#############################################################################
433 460
442 my $class = shift; 469 my $class = shift;
443 470
444 # range [value, low, high, page] 471 # range [value, low, high, page]
445 472
446 $class->SUPER::new ( 473 $class->SUPER::new (
447 bg => [0, 0, 0, 0.2], 474 #bg => [0, 0, 0, 0.2],
448 active_bg => [1, 1, 1, 0.5], 475 #active_bg => [1, 1, 1, 0.5],
449 @_ 476 @_
450 ) 477 )
451} 478}
452 479
453sub _draw { 480sub _draw {
454 my ($self) = @_; 481 my ($self) = @_;
455 482
483 my $color = $FOCUS == $self && $self->{active_bg}
484 ? $self->{active_bg}
485 : $self->{bg};
486
487 if ($color && (@$color < 4 || $color->[3])) {
456 my ($w, $h) = @$self{qw(w h)}; 488 my ($w, $h) = @$self{qw(w h)};
457 489
458 glEnable GL_BLEND; 490 glEnable GL_BLEND;
459 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
460 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 492 glColor @$color;
461 493
462 glBegin GL_QUADS; 494 glBegin GL_QUADS;
463 glVertex 0 , 0; 495 glVertex 0 , 0;
464 glVertex 0 , $h; 496 glVertex 0 , $h;
465 glVertex $w, $h; 497 glVertex $w, $h;
466 glVertex $w, 0; 498 glVertex $w, 0;
467 glEnd; 499 glEnd;
468 500
469 glDisable GL_BLEND; 501 glDisable GL_BLEND;
502 }
470} 503}
471 504
472############################################################################# 505#############################################################################
473 506
474package CFClient::UI::Empty; 507package CFClient::UI::Empty;
506 539
507 $self 540 $self
508} 541}
509 542
510sub add { 543sub add {
511 my ($self, $child) = @_; 544 my ($self, @widgets) = @_;
512 545
513 $child->set_parent ($self); 546 $_->set_parent ($self)
547 for @widgets;
514 548
515 use sort 'stable'; 549 use sort 'stable';
516 550
517 $self->{children} = [ 551 $self->{children} = [
518 sort { $a->{z} <=> $b->{z} } 552 sort { $a->{z} <=> $b->{z} }
519 @{$self->{children}}, $child 553 @{$self->{children}}, @widgets
520 ]; 554 ];
521 555
522 $child->check_size; 556 $self->check_size (1);
557 $self->update;
558}
559
560sub children {
561 @{ $_[0]{children} }
523} 562}
524 563
525sub remove { 564sub remove {
526 my ($self, $child) = @_; 565 my ($self, $child) = @_;
527 566
542 581
543 for (@$children) { 582 for (@$children) {
544 delete $_->{parent}; 583 delete $_->{parent};
545 $_->hide; 584 $_->hide;
546 } 585 }
586
587 $self->check_size;
588 $self->update;
547} 589}
548 590
549sub find_widget { 591sub find_widget {
550 my ($self, $x, $y) = @_; 592 my ($self, $x, $y) = @_;
551 593
626} 668}
627 669
628sub update { 670sub update {
629 my ($self) = @_; 671 my ($self) = @_;
630 672
631 # we want to do this delayed... 673 $ROOT->on_post_alloc ($self => sub { $self->render_child });
632 $self->render_chld;
633 $self->SUPER::update; 674 $self->SUPER::update;
634}
635
636sub render_chld {
637 my ($self) = @_;
638
639 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
640 glClearColor 0, 0, 0, 1;
641 glClear GL_COLOR_BUFFER_BIT;
642 $self->child->draw;
643 };
644} 675}
645 676
646sub size_allocate { 677sub size_allocate {
647 my ($self, $w, $h) = @_; 678 my ($self, $w, $h) = @_;
648 679
649 $self->child->configure (0, 0, $w, $h); 680 $self->SUPER::size_allocate ($w, $h);
681 $self->update;
682}
650 683
684sub _render {
685 $_[0]{children}[0]->draw;
686}
687
688sub render_child {
689 my ($self) = @_;
690
691 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
692 glClearColor 0, 0, 0, 0;
693 glClear GL_COLOR_BUFFER_BIT;
694
651 $self->render_chld; 695 $self->_render;
696# glColorMask 1, 1, 1, 0;
697# glEnable GL_BLEND;
698# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
699# glRasterPos 0, 0;
700# glCopyPixels 0, 0, $self->{w}, $self->{h};
701# glDisable GL_BLEND;
702# glColorMask 1, 1, 1, 1;
703 };
652} 704}
653 705
654sub _draw { 706sub _draw {
655 my ($self) = @_; 707 my ($self) = @_;
656 708
657 my ($w, $h) = ($self->w, $self->h); 709 my ($w, $h) = ($self->w, $self->h);
658 710
659 my $tex = $self->{texture} 711 my $tex = $self->{texture}
660 or return; 712 or return;
661 713
662 glEnable GL_BLEND;
663 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
664 glEnable GL_TEXTURE_2D; 714 glEnable GL_TEXTURE_2D;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 715 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
716 glColor 0, 0, 0, 1;
666 717
667 $tex->draw_quad (0, 0, $w, $h); 718 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
668 719
669 glDisable GL_BLEND;
670 glDisable GL_TEXTURE_2D; 720 glDisable GL_TEXTURE_2D;
671} 721}
672 722
673############################################################################# 723#############################################################################
674 724
675package CFClient::UI::ViewPort; 725package CFClient::UI::ViewPort;
676 726
677our @ISA = CFClient::UI::Window::; 727our @ISA = CFClient::UI::Window::;
678 728
679sub new { die }
680
681sub size_request { 729sub size_request {
682 my ($self) = @_; 730 my ($self) = @_;
683 731
684 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 732 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
685 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 733 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
686 734
687 @$self{qw(child_w child_h)} 735 @$self{qw(child_w child_h)}
688} 736}
689 737
690sub _draw { 738sub size_allocate {
691 my ($self) = @_; 739 my ($self, $w, $h) = @_;
692 740
693 $self->{children}[1]->draw; 741 $self->update;
694} 742}
695 743
744sub set_offset {
745 my ($self, $x, $y) = @_;
696 746
697############################################################################# 747 $self->{view_x} = int $x;
748 $self->{view_y} = int $y;
698 749
750 $self->update;
751}
752
753# hmm, this does not work for topleft of $self... but we should not ask for that
754sub coord2local {
755 my ($self, $x, $y) = @_;
756
757 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
758}
759
760sub coord2global {
761 my ($self, $x, $y) = @_;
762
763 $x = List::Util::min $self->{w}, $x - $self->{view_x};
764 $y = List::Util::min $self->{h}, $y - $self->{view_y};
765
766 $self->SUPER::coord2global ($x, $y)
767}
768
769sub find_widget {
770 my ($self, $x, $y) = @_;
771
772 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
773 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
774 ) {
775 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
776 } else {
777 $self->CFClient::UI::Base::find_widget ($x, $y)
778 }
779}
780
781sub _render {
782 my ($self) = @_;
783
784 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
785
786 $self->SUPER::_render;
787}
788
789#############################################################################
790
699package CFClient::UI::Frame; 791package CFClient::UI::ScrolledWindow;
700 792
701our @ISA = CFClient::UI::Bin::; 793our @ISA = CFClient::UI::HBox::;
702
703use CFClient::OpenGL;
704 794
705sub new { 795sub new {
706 my $class = shift; 796 my $class = shift;
707 797
798 my $self;
799
800 my $slider = new CFClient::UI::Slider
801 vertical => 1,
802 range => [0, 0, 1, 0.01], # HACK fix
803 connect_changed => sub {
804 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
805 },
806 ;
807
708 my $self = $class->SUPER::new ( 808 $self = $class->SUPER::new (
709 bg => [1, 1, 1, 1], 809 vp => (new CFClient::UI::ViewPort),
710 border_bg => [1, 1, 1, 1], 810 slider => $slider,
711 border => 0.8,
712 @_ 811 @_,
713 ); 812 );
714 813
814 $self->{vp}->add ($self->{scrolled});
815 $self->add ($self->{vp});
816 $self->add ($self->{slider});
817
715 $self 818 $self
716} 819}
717 820
821#TODO# update range on size_allocate depending on child
822# update viewport offset on scroll
823
824#############################################################################
825
826package CFClient::UI::Frame;
827
828our @ISA = CFClient::UI::Bin::;
829
830use CFClient::OpenGL;
831
832sub new {
833 my $class = shift;
834
835 $class->SUPER::new (
836 bg => undef,
837 @_,
838 )
839}
840
718sub _draw { 841sub _draw {
719 my ($self) = @_; 842 my ($self) = @_;
720 843
721 my ($w, $h) = ($self->{w}, $self->{h}); 844 if ($self->{bg}) {
845 my ($w, $h) = @$self{qw(w h)};
722 846
723 glEnable GL_BLEND; 847 glEnable GL_BLEND;
724 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 848 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
725 glEnable GL_TEXTURE_2D; 849 glColor @{ $self->{bg} };
726 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
727 850
728# glBegin GL_QUADS; 851 glBegin GL_QUADS;
729# glColor 0, 0, 0, 0;
730# glVertex 0 , 0; 852 glVertex 0 , 0;
731# glVertex 0 , $h; 853 glVertex 0 , $h;
732# glVertex $w, $h; 854 glVertex $w, $h;
733# glVertex $w, 0; 855 glVertex $w, 0;
734# glEnd; 856 glEnd;
735 857
736
737 $self->child->draw;
738 glDisable GL_BLEND; 858 glDisable GL_BLEND;
739 glDisable GL_TEXTURE_2D; 859 }
860
861 $self->SUPER::_draw;
740} 862}
741 863
742############################################################################# 864#############################################################################
743 865
744package CFClient::UI::FancyFrame; 866package CFClient::UI::FancyFrame;
757 # TODO: user_x, user_y, overwrite moveto? 879 # TODO: user_x, user_y, overwrite moveto?
758 880
759 my $self = $class->SUPER::new ( 881 my $self = $class->SUPER::new (
760 bg => [1, 1, 1, 1], 882 bg => [1, 1, 1, 1],
761 border_bg => [1, 1, 1, 1], 883 border_bg => [1, 1, 1, 1],
762 border => 0.8, 884 border => 0.6,
885 toplevel => 1,
763 can_events => 1, 886 can_events => 1,
764 @_ 887 @_
765 ); 888 );
766 889
767 $self->{title} &&= new CFClient::UI::Label 890 $self->{title} &&= new CFClient::UI::Label
768 align => 0, 891 align => 0,
769 valign => 1, 892 valign => 1,
770 text => $self->{title}, 893 text => $self->{title},
771 fontsize => 1; 894 fontsize => $self->{border};
772 895
773 $self 896 $self
774} 897}
775 898
776sub border { 899sub border {
792 my ($self, $w, $h) = @_; 915 my ($self, $w, $h) = @_;
793 916
794 $h -= List::Util::max 0, $self->border * 2; 917 $h -= List::Util::max 0, $self->border * 2;
795 $w -= List::Util::max 0, $self->border * 2; 918 $w -= List::Util::max 0, $self->border * 2;
796 919
797 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 920 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
798 if $self->{title}; 921 if $self->{title};
799 922
800 $self->child->configure ($self->border, $self->border, $w, $h); 923 $self->child->configure ($self->border, $self->border, $w, $h);
801} 924}
802 925
803sub button_down { 926sub button_down {
804 my ($self, $ev, $x, $y) = @_; 927 my ($self, $ev, $x, $y) = @_;
805 928
929 my ($w, $h) = @$self{qw(w h)};
806 my $border = $self->border; 930 my $border = $self->border;
807 931
808 if ($x < $self->{w} && $x >= $self->{w} - $border 932 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
809 && $y < $self->{h} && $y >= $self->{h} - $border) { 933 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
810 934
935 if ($lr & $td) {
936 my ($wx, $wy) = ($self->{x}, $self->{y});
811 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 937 my ($ox, $oy) = ($ev->{x}, $ev->{y});
812 my ($bw, $bh) = ($self->{w}, $self->{h}); 938 my ($bw, $bh) = ($self->{w}, $self->{h});
813 939
940 my $mx = $x < $border;
941 my $my = $y < $border;
942
814 $self->{motion} = sub { 943 $self->{motion} = sub {
815 my ($ev, $x, $y) = @_; 944 my ($ev, $x, $y) = @_;
816 945
817 ($x, $y) = ($ev->{x}, $ev->{y}); 946 my $dx = $ev->{x} - $ox;
947 my $dy = $ev->{y} - $oy;
818 948
819 $self->{user_w} = $bw + $x - $ox; 949 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
820 $self->{user_h} = $bh + $y - $oy; 950 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
951 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
821 $self->check_size; 952 $self->check_size;
822 }; 953 };
823 954
824 } elsif ($x >= 0 && $x < $self->{w} 955 } elsif ($lr ^ $td) {
825 && $y >= 0 && $y < $border) {
826
827 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 956 my ($ox, $oy) = ($ev->{x}, $ev->{y});
828 my ($bx, $by) = ($self->{x}, $self->{y}); 957 my ($bx, $by) = ($self->{x}, $self->{y});
829 958
830 $self->{motion} = sub { 959 $self->{motion} = sub {
831 my ($ev, $x, $y) = @_; 960 my ($ev, $x, $y) = @_;
854 my ($self) = @_; 983 my ($self) = @_;
855 984
856 my ($w, $h ) = ($self->{w}, $self->{h}); 985 my ($w, $h ) = ($self->{w}, $self->{h});
857 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 986 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
858 987
859 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
861 glEnable GL_TEXTURE_2D; 988 glEnable GL_TEXTURE_2D;
862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 989 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
863 990
864 my $border = $self->border; 991 my $border = $self->border;
865 992
866 glColor @{ $self->{border_bg} }; 993 glColor @{ $self->{border_bg} };
867 $tex[1]->draw_quad (0, 0, $w, $border); 994 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
868 $tex[3]->draw_quad (0, $border, $border, $ch); 995 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
869 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 996 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
870 $tex[4]->draw_quad (0, $h - $border, $w, $border); 997 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
871 998
999 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
872 my $bg = $tex[0]; 1000 my $bg = $tex[0];
873 1001
874 # TODO: repeat texture not scale 1002 # TODO: repeat texture not scale
875 my $rep_x = $cw / $bg->{w}; 1003 my $rep_x = $cw / $bg->{w};
876 my $rep_y = $ch / $bg->{h}; 1004 my $rep_y = $ch / $bg->{h};
877 1005
878 glColor @{ $self->{bg} }; 1006 glColor @{ $self->{bg} };
879 1007
880 $bg->{s} = $rep_x; 1008 $bg->{s} = $rep_x;
881 $bg->{t} = $rep_y; 1009 $bg->{t} = $rep_y;
882 $bg->{wrap_mode} = 1; 1010 $bg->{wrap_mode} = 1;
883 $bg->draw_quad ($border, $border, $cw, $ch); 1011 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1012 }
884 1013
885 glDisable GL_TEXTURE_2D; 1014 glDisable GL_TEXTURE_2D;
886 glDisable GL_BLEND;
887 1015
888 $self->{title}->draw if $self->{title}; 1016 $self->{title}->draw if $self->{title};
1017
889 $self->child->draw; 1018 $self->child->draw;
890} 1019}
891 1020
892############################################################################# 1021#############################################################################
893 1022
915 $self->{children}[$y][$x] = $child; 1044 $self->{children}[$y][$x] = $child;
916 1045
917 $child->check_size; 1046 $child->check_size;
918} 1047}
919 1048
1049sub children {
1050 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1051}
1052
920# TODO: move to container class maybe? send childs a signal on removal? 1053# TODO: move to container class maybe? send childs a signal on removal?
921sub clear { 1054sub clear {
922 my ($self) = @_; 1055 my ($self) = @_;
923 1056
924 my $children = delete $self->{children}; 1057 my @children = $self->children;
1058 delete $self->{children};
925 1059
926 for (grep $_, map @$_, grep $_, @$children) { 1060 for (@children) {
927 delete $_->{parent}; 1061 delete $_->{parent};
928 $_->hide; 1062 $_->hide;
929 } 1063 }
930 1064
931 $self->update; 1065 $self->update;
1112} 1246}
1113 1247
1114sub size_allocate { 1248sub size_allocate {
1115 my ($self, $w, $h) = @_; 1249 my ($self, $w, $h) = @_;
1116 1250
1251 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1252
1117 my $children = $self->{children}; 1253 my $children = $self->{children};
1118 1254
1119 my @h = map $_->{req_h}, @$children; 1255 my @h = map $_->{req_h}, @$children;
1120 1256
1121 my $req_h = List::Util::sum @h; 1257 my $req_h = List::Util::sum @h;
1150 1286
1151############################################################################# 1287#############################################################################
1152 1288
1153package CFClient::UI::Label; 1289package CFClient::UI::Label;
1154 1290
1155our @ISA = CFClient::UI::Base::; 1291our @ISA = CFClient::UI::DrawBG::;
1156 1292
1157use CFClient::OpenGL; 1293use CFClient::OpenGL;
1158 1294
1159sub new { 1295sub new {
1160 my ($class, %arg) = @_; 1296 my ($class, %arg) = @_;
1161 1297
1162 my $self = $class->SUPER::new ( 1298 my $self = $class->SUPER::new (
1163 fg => [1, 1, 1], 1299 fg => [1, 1, 1],
1300 #bg => none
1301 #active_bg => none
1164 #font => default_font 1302 #font => default_font
1303 #text => initial text
1304 #markup => initial narkup
1305 #max_w => maximum pixel width
1306 ellipsise => 3, # end
1307 layout => (new CFClient::Layout),
1165 fontsize => 1, 1308 fontsize => 1,
1166 text => "",
1167 align => -1, 1309 align => -1,
1168 valign => -1, 1310 valign => -1,
1169 padding => 2, 1311 padding => 2,
1170 layout => new CFClient::Layout,
1171 can_events => 0, 1312 can_events => 0,
1172 %arg 1313 %arg
1173 ); 1314 );
1174 1315
1175 if (exists $self->{template}) { 1316 if (exists $self->{template}) {
1176 my $layout = new CFClient::Layout; 1317 my $layout = new CFClient::Layout;
1177 $layout->set_text (delete $self->{template}); 1318 $layout->set_text (delete $self->{template});
1178 $self->{template} = $layout; 1319 $self->{template} = $layout;
1179 } 1320 }
1180 1321
1181 $self->set_text (delete $self->{text}) if exists $self->{text}; 1322 if (exists $self->{markup}) {
1182 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1323 $self->set_markup (delete $self->{markup});
1324 } else {
1325 $self->set_text (delete $self->{text});
1326 }
1183 1327
1184 $self 1328 $self
1185} 1329}
1186 1330
1187sub escape { 1331sub escape($) {
1188 local $_ = $_[1]; 1332 local $_ = $_[0];
1189 1333
1190 s/&/&amp;/g; 1334 s/&/&amp;/g;
1191 s/>/&gt;/g; 1335 s/>/&gt;/g;
1192 s/</&lt;/g; 1336 s/</&lt;/g;
1193 1337
1194 $_[1] 1338 $_
1339}
1340
1341sub update {
1342 my ($self) = @_;
1343
1344 delete $self->{texture};
1345 $self->SUPER::update;
1195} 1346}
1196 1347
1197sub set_text { 1348sub set_text {
1198 my ($self, $text) = @_; 1349 my ($self, $text) = @_;
1199 1350
1351 return if $self->{text} eq "T$text";
1352 $self->{text} = "T$text";
1353
1354 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1200 $self->{layout}->set_text ($text); 1355 $self->{layout}->set_text ($text);
1201 1356
1202 delete $self->{texture}; 1357 $self->update;
1203 $self->check_size; 1358 $self->check_size;
1204 $self->update;
1205} 1359}
1206 1360
1207sub set_markup { 1361sub set_markup {
1208 my ($self, $markup) = @_; 1362 my ($self, $markup) = @_;
1209 1363
1364 return if $self->{text} eq "M$markup";
1365 $self->{text} = "M$markup";
1366
1367 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1368
1369 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1210 $self->{layout}->set_markup ($markup); 1370 $self->{layout}->set_markup ($markup);
1211 1371
1212 delete $self->{texture}; 1372 $self->update;
1213 $self->check_size; 1373 $self->check_size;
1214 $self->update;
1215} 1374}
1216 1375
1217sub size_request { 1376sub size_request {
1218 my ($self) = @_; 1377 my ($self) = @_;
1219 1378
1220 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1379 $self->{layout}->set_font ($self->{font}) if $self->{font};
1221 $self->{layout}->set_width ($self->{max_w} || -1); 1380 $self->{layout}->set_width ($self->{max_w} || -1);
1381 $self->{layout}->set_ellipsise ($self->{ellipsise});
1382 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1222 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1383 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1223 1384
1224 my ($w, $h) = $self->{layout}->size; 1385 my ($w, $h) = $self->{layout}->size;
1225 1386
1226 if (exists $self->{template}) { 1387 if (exists $self->{template}) {
1248sub set_fontsize { 1409sub set_fontsize {
1249 my ($self, $fontsize) = @_; 1410 my ($self, $fontsize) = @_;
1250 1411
1251 $self->{fontsize} = $fontsize; 1412 $self->{fontsize} = $fontsize;
1252 delete $self->{texture}; 1413 delete $self->{texture};
1414
1415 $self->update;
1253 $self->check_size; 1416 $self->check_size;
1254 $self->update;
1255} 1417}
1256 1418
1257sub _draw { 1419sub _draw {
1258 my ($self) = @_; 1420 my ($self) = @_;
1259 1421
1422 $self->SUPER::_draw; # draw background, if applicable
1423
1260 my $tex = $self->{texture} ||= do { 1424 my $tex = $self->{texture} ||= do {
1425 $self->{layout}->set_foreground (@{$self->{fg}});
1261 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1426 $self->{layout}->set_font ($self->{font}) if $self->{font};
1262 $self->{layout}->set_width ($self->{w}); 1427 $self->{layout}->set_width ($self->{w});
1428 $self->{layout}->set_ellipsise ($self->{ellipsise});
1429 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1263 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1430 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1431
1264 new_from_layout CFClient::Texture $self->{layout} 1432 my $tex = new_from_layout CFClient::Texture $self->{layout};
1433
1434 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1435 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1436 : ($self->{w} - $tex->{w}) * 0.5);
1437
1438 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1439 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1440 : ($self->{h} - $tex->{h}) * 0.5);
1441
1442 $tex
1265 }; 1443 };
1266 1444
1267 glEnable GL_BLEND;
1268 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1269 glEnable GL_TEXTURE_2D; 1445 glEnable GL_TEXTURE_2D;
1270 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1446 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1271 1447
1448 if ($tex->{format} == GL_ALPHA) {
1272 glColor @{$self->{fg}}; 1449 glColor @{$self->{fg}};
1273
1274 $self->{ox} = int (
1275 $self->{align} < 0 ? $self->{padding}
1276 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1277 : ($self->{w} - $tex->{w}) * 0.5
1278 );
1279
1280 $self->{oy} = int (
1281 $self->{valign} < 0 ? $self->{padding}
1282 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1283 : ($self->{h} - $tex->{h}) * 0.5
1284 );
1285
1286 $tex->draw_quad ($self->{ox}, $self->{oy}); 1450 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1451 } else {
1452 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1453 }
1287 1454
1288 glDisable GL_TEXTURE_2D; 1455 glDisable GL_TEXTURE_2D;
1289 glDisable GL_BLEND;
1290} 1456}
1291 1457
1292############################################################################# 1458#############################################################################
1293 1459
1294package CFClient::UI::EntryBase; 1460package CFClient::UI::EntryBase;
1329 $self->{layout}->set_text ("$text "); 1495 $self->{layout}->set_text ("$text ");
1330 1496
1331 $self->emit (changed => $self->{text}); 1497 $self->emit (changed => $self->{text});
1332} 1498}
1333 1499
1334sub get_text {
1335 $_[0]{text}
1336}
1337
1338sub size_request {
1339 my ($self) = @_;
1340
1341 my ($w, $h) = $self->SUPER::size_request;
1342
1343 ($w + 1, $h) # add 1 for cursor
1344}
1345
1346sub size_allocate {
1347 my ($self, $w, $h) = @_;
1348
1349 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1350}
1351
1352sub set_text { 1500sub set_text {
1353 my ($self, $text) = @_; 1501 my ($self, $text) = @_;
1354 1502
1355 $self->{cursor} = length $text; 1503 $self->{cursor} = length $text;
1356 $self->_set_text ($text); 1504 $self->_set_text ($text);
1357 $self->update; 1505 $self->update;
1506 $self->check_size;
1507}
1508
1509sub get_text {
1510 $_[0]{text}
1511}
1512
1513sub size_request {
1514 my ($self) = @_;
1515
1516 my ($w, $h) = $self->SUPER::size_request;
1517
1518 ($w + 1, $h) # add 1 for cursor
1519}
1520
1521sub size_allocate {
1522 my ($self, $w, $h) = @_;
1523
1524 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1358} 1525}
1359 1526
1360sub key_down { 1527sub key_down {
1361 my ($self, $ev) = @_; 1528 my ($self, $ev) = @_;
1362 1529
1364 my $sym = $ev->{sym}; 1531 my $sym = $ev->{sym};
1365 my $uni = $ev->{unicode}; 1532 my $uni = $ev->{unicode};
1366 1533
1367 my $text = $self->get_text; 1534 my $text = $self->get_text;
1368 1535
1369 if ($sym == 8) { 1536 if ($uni == 8) {
1370 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1537 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1371 } elsif ($sym == 127) { 1538 } elsif ($uni == 127) {
1372 substr $text, $self->{cursor}, 1, ""; 1539 substr $text, $self->{cursor}, 1, "";
1373 } elsif ($sym == CFClient::SDLK_LEFT) { 1540 } elsif ($sym == CFClient::SDLK_LEFT) {
1374 --$self->{cursor} if $self->{cursor}; 1541 --$self->{cursor} if $self->{cursor};
1375 } elsif ($sym == CFClient::SDLK_RIGHT) { 1542 } elsif ($sym == CFClient::SDLK_RIGHT) {
1376 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1543 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1377 } elsif ($sym == CFClient::SDLK_HOME) { 1544 } elsif ($sym == CFClient::SDLK_HOME) {
1378 $self->{cursor} = 0; 1545 $self->{cursor} = 0;
1379 } elsif ($sym == CFClient::SDLK_END) { 1546 } elsif ($sym == CFClient::SDLK_END) {
1380 $self->{cursor} = length $text; 1547 $self->{cursor} = length $text;
1381 } elsif ($sym == 27) { 1548 } elsif ($uni == 27) {
1382 $self->emit ('escape'); 1549 $self->emit ('escape');
1383 } elsif ($uni) { 1550 } elsif ($uni) {
1384 substr $text, $self->{cursor}++, 0, chr $uni; 1551 substr $text, $self->{cursor}++, 0, chr $uni;
1385 } 1552 }
1386 1553
1387 $self->_set_text ($text); 1554 $self->_set_text ($text);
1388 $self->update; 1555 $self->update;
1556 $self->check_size;
1389} 1557}
1390 1558
1391sub focus_in { 1559sub focus_in {
1392 my ($self) = @_; 1560 my ($self) = @_;
1393 1561
1522 my $class = shift; 1690 my $class = shift;
1523 1691
1524 $class->SUPER::new ( 1692 $class->SUPER::new (
1525 padding => 4, 1693 padding => 4,
1526 fg => [1, 1, 1], 1694 fg => [1, 1, 1],
1527 bg => [1, 1, 1, 0.2],
1528 active_fg => [0, 0, 1], 1695 active_fg => [0, 0, 1],
1529 can_hover => 1, 1696 can_hover => 1,
1530 align => 0, 1697 align => 0,
1531 valign => 0, 1698 valign => 0,
1532 can_events => 1, 1699 can_events => 1,
1550 1717
1551 if ($GRAB == $self) { 1718 if ($GRAB == $self) {
1552 $self->{fg} = $self->{active_fg}; 1719 $self->{fg} = $self->{active_fg};
1553 } 1720 }
1554 1721
1555 glEnable GL_BLEND;
1556 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1557 glEnable GL_TEXTURE_2D; 1722 glEnable GL_TEXTURE_2D;
1558 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1723 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1559 glColor 0, 0, 0, 1; 1724 glColor 0, 0, 0, 1;
1560 1725
1561 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1726 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1562 1727
1563 glDisable GL_TEXTURE_2D; 1728 glDisable GL_TEXTURE_2D;
1564 glDisable GL_BLEND;
1565 1729
1566 $self->SUPER::_draw; 1730 $self->SUPER::_draw;
1567} 1731}
1568 1732
1569############################################################################# 1733#############################################################################
1583 1747
1584 $class->SUPER::new ( 1748 $class->SUPER::new (
1585 padding => 2, 1749 padding => 2,
1586 fg => [1, 1, 1], 1750 fg => [1, 1, 1],
1587 active_fg => [1, 1, 0], 1751 active_fg => [1, 1, 0],
1752 bg => [0, 0, 0, 0.2],
1753 active_bg => [1, 1, 1, 0.5],
1588 state => 0, 1754 state => 0,
1589 can_hover => 1, 1755 can_hover => 1,
1590 @_ 1756 @_
1591 ) 1757 )
1592} 1758}
1616 1782
1617 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1783 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1618 1784
1619 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1785 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1620 1786
1621 glEnable GL_BLEND; 1787 my $tex = $self->{state} ? $tex[1] : $tex[0];
1788
1622 glEnable GL_TEXTURE_2D; 1789 glEnable GL_TEXTURE_2D;
1623 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1624
1625 my $tex = $self->{state} ? $tex[1] : $tex[0];
1626
1627 $tex->draw_quad (0, 0, $s, $s); 1790 $tex->draw_quad_alpha (0, 0, $s, $s);
1628
1629 glDisable GL_TEXTURE_2D; 1791 glDisable GL_TEXTURE_2D;
1630 glDisable GL_BLEND;
1631} 1792}
1632 1793
1633############################################################################# 1794#############################################################################
1634 1795
1635package CFClient::UI::Image; 1796package CFClient::UI::Image;
1678 glTranslate 0, -$self->{w}, 0; 1839 glTranslate 0, -$self->{w}, 0;
1679 1840
1680 ($w, $h) = ($h, $w); 1841 ($w, $h) = ($h, $w);
1681 } 1842 }
1682 1843
1683 glEnable GL_BLEND;
1684 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1685 glEnable GL_TEXTURE_2D; 1844 glEnable GL_TEXTURE_2D;
1686 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1845 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1687 1846
1688 $tex->draw_quad (0, 0, $w, $h); 1847 $tex->draw_quad_alpha (0, 0, $w, $h);
1689 1848
1690 glDisable GL_BLEND;
1691 glDisable GL_TEXTURE_2D; 1849 glDisable GL_TEXTURE_2D;
1692} 1850}
1693 1851
1694############################################################################# 1852#############################################################################
1695 1853
1743} 1901}
1744 1902
1745sub set_max { 1903sub set_max {
1746 my ($self, $max) = @_; 1904 my ($self, $max) = @_;
1747 1905
1906 return if $self->{max_val} == $max;
1907
1748 $self->{max_val} = $max; 1908 $self->{max_val} = $max;
1909 $self->update;
1749} 1910}
1750 1911
1751sub set_value { 1912sub set_value {
1752 my ($self, $val, $max) = @_; 1913 my ($self, $val, $max) = @_;
1753 1914
1754 $self->set_max ($max) 1915 $self->set_max ($max)
1755 if defined $max; 1916 if defined $max;
1756 1917
1757 $max = $self->{max_val}; 1918 return if $self->{val} == $val;
1919
1758 $self->{val} = $val; 1920 $self->{val} = $val;
1759
1760 $self->update; 1921 $self->update;
1761} 1922}
1762 1923
1763sub _draw { 1924sub _draw {
1764 my ($self) = @_; 1925 my ($self) = @_;
1827 1988
1828sub new { 1989sub new {
1829 my ($class, %arg) = @_; 1990 my ($class, %arg) = @_;
1830 1991
1831 my $self = $class->SUPER::new ( 1992 my $self = $class->SUPER::new (
1832 tooltip => $arg{type}, 1993 tooltip => $arg{type},
1833 can_hover => 1, 1994 can_hover => 1,
1995 can_events => 1,
1834 %arg, 1996 %arg,
1835 ); 1997 );
1836 1998
1837 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1999 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1838 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2000 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1846 2008
1847 $self->{value}->set_fontsize ($fsize); 2009 $self->{value}->set_fontsize ($fsize);
1848 $self->{max} ->set_fontsize ($fsize); 2010 $self->{max} ->set_fontsize ($fsize);
1849} 2011}
1850 2012
2013sub set_max {
2014 my ($self, $max) = @_;
2015
2016 $self->{gauge}->set_max ($max);
2017 $self->{max}->set_text ($max);
2018}
2019
1851sub set_value { 2020sub set_value {
1852 my ($self, $val, $max) = @_; 2021 my ($self, $val, $max) = @_;
1853 2022
1854 $self->set_max ($max) 2023 $self->set_max ($max)
1855 if defined $max; 2024 if defined $max;
1856 2025
1857 $self->{gauge}->set_value ($val, $max); 2026 $self->{gauge}->set_value ($val, $max);
1858 $self->{value}->set_text ($val); 2027 $self->{value}->set_text ($val);
1859}
1860
1861sub set_max {
1862 my ($self, $max) = @_;
1863
1864 $self->{gauge}->set_max ($max);
1865 $self->{max}->set_text ($max);
1866} 2028}
1867 2029
1868############################################################################# 2030#############################################################################
1869 2031
1870package CFClient::UI::Slider; 2032package CFClient::UI::Slider;
1880 qw(s1_slider.png s1_slider_bg.png); 2042 qw(s1_slider.png s1_slider_bg.png);
1881 2043
1882sub new { 2044sub new {
1883 my $class = shift; 2045 my $class = shift;
1884 2046
1885 # range [value, low, high, page] 2047 # range [value, low, high, page, unit]
1886 2048
1887 # TODO: 0-width page 2049 # TODO: 0-width page
1888 # TODO: req_w/h are wrong with vertical 2050 # TODO: req_w/h are wrong with vertical
1889 # TODO: calculations are off 2051 # TODO: calculations are off
1890 my $self = $class->SUPER::new ( 2052 my $self = $class->SUPER::new (
1891 fg => [1, 1, 1], 2053 fg => [1, 1, 1],
1892 active_fg => [0, 0, 0], 2054 active_fg => [0, 0, 0],
2055 bg => [0, 0, 0, 0.2],
2056 active_bg => [1, 1, 1, 0.5],
1893 range => [0, 0, 100, 10], 2057 range => [0, 0, 100, 10],
1894 req_w => 20, 2058 req_w => $::WIDTH / 80,
1895 req_h => 20, 2059 req_h => $::WIDTH / 80,
1896 vertical => 0, 2060 vertical => 0,
1897 can_hover => 1, 2061 can_hover => 1,
1898 inner_pad => 5, 2062 inner_pad => .05,
1899 @_ 2063 @_
1900 ); 2064 );
1901 2065
2066 $self->set_value ($self->{range}[0]);
2067 $self->update;
2068
1902 $self 2069 $self
2070}
2071
2072sub set_value {
2073 my ($self, $value) = @_;
2074
2075 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2076
2077 $hi = $lo + 1 if $hi <= $lo;
2078
2079 $value = $lo if $value < $lo;
2080 $value = $hi if $value > $hi;
2081
2082 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2083 if $unit;
2084
2085 $page = $hi - $lo if $page > $hi - $lo;
2086
2087 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2088
2089 if ($value != $old_value) {
2090 $self->emit (changed => $value);
2091 $self->update;
2092 }
1903} 2093}
1904 2094
1905sub size_request { 2095sub size_request {
1906 my ($self) = @_; 2096 my ($self) = @_;
1907 2097
1920 2110
1921sub mouse_motion { 2111sub mouse_motion {
1922 my ($self, $ev, $x, $y) = @_; 2112 my ($self, $ev, $x, $y) = @_;
1923 2113
1924 if ($GRAB == $self) { 2114 if ($GRAB == $self) {
2115 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2116
2117 my (undef, $lo, $hi, $page) = @{$self->{range}};
2118
2119 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2120
2121 $self->set_value ($x * ($hi - $lo) + $lo);
2122 }
2123}
2124
2125sub update {
2126 my ($self) = @_;
2127
2128 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2129 $self->set_value ($self->{range}[0]);
2130
1925 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2131 my ($value, $lo, $hi, $page) = @{$self->{range}};
1926 2132
1927 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2133 my $inner_w = 1 - 2 * $self->{inner_pad};
1928 2134
1929 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2135 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
1930 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1931 2136
1932 $x -= $inner_pad_px; # substract the padding 2137 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
1933 $x = $x * ($hi - $lo) / $inner_w + $lo; 2138 $value = $self->{scale} * ($value - $lo);
1934 $x = $lo if $x < $lo;
1935 $x = $hi - $page if $x > $hi - $page;
1936 $self->{range}[0] = $x;
1937 2139
1938 $self->emit (changed => $x); 2140 $value = $self->{inner_pad} + ($value - $page * 0.5);
1939 $self->update; 2141
2142 $value = 0 if $value < 0;
2143 $page = 1 - $value if $value + $page > 1;
2144
2145 $self->{knob_x} = $value;
2146 $self->{knob_w} = $page;
1940 } 2147 });
1941}
1942 2148
1943# the inner_* stuff is for generating a padding for the slider handle, 2149 $self->SUPER::update;
1944# so that the handle doesn't leave the texture. This calculation isn't 100%
1945# correct propably, but it does the job for now
1946sub _calc_inner_pad_px {
1947 my ($self, $w) = @_;
1948 ($w / 100) * $self->{inner_pad} # % to pixels
1949} 2150}
1950 2151
1951sub _draw { 2152sub _draw {
1952 my ($self) = @_; 2153 my ($self) = @_;
1953 2154
1954 $self->SUPER::_draw (); 2155 $self->SUPER::_draw ();
1955 2156
1956 my ($w, $h) = @$self{qw(w h)}; 2157 glScale $self->{w}, $self->{h};
1957 2158
1958 if ($self->{vertical}) { 2159 if ($self->{vertical}) {
1959 # draw a vertical slider like a rotated horizontal slider 2160 # draw a vertical slider like a rotated horizontal slider
1960 2161
2162 glTranslate 1, 0, 0;
1961 glRotate 90, 0, 0, 1; 2163 glRotate 90, 0, 0, 1;
1962 glTranslate 0, -$self->{w}, 0;
1963
1964 ($w, $h) = ($h, $w);
1965 } 2164 }
1966 2165
1967 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2166 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1968 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2167 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1969 2168
1970 my ($value, $lo, $hi, $page) = @{$self->{range}};
1971
1972 $hi = $value + 1 if $lo == $hi;
1973
1974 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1975 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1976
1977 $page = int $page * $inner_w / ($hi - $lo);
1978 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1979
1980 $w -= $page;
1981 $page &= ~1;
1982 glTranslate $page * 0.5, 0, 0;
1983 $page ||= 2;
1984
1985 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1986 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1987
1988 glEnable GL_BLEND;
1989 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1990 glEnable GL_TEXTURE_2D; 2169 glEnable GL_TEXTURE_2D;
1991 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1992 2171
1993 # draw background 2172 # draw background
1994 $tex[1]->draw_quad (0, 0, $w, $h); 2173 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
1995 2174
1996 # draw handle 2175 # draw handle
1997 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2176 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
1998 2177
1999 glDisable GL_BLEND;
2000 glDisable GL_TEXTURE_2D; 2178 glDisable GL_TEXTURE_2D;
2001} 2179}
2002 2180
2003############################################################################# 2181#############################################################################
2004 2182
2015 fontsize => 1, 2193 fontsize => 1,
2016 can_events => 0, 2194 can_events => 0,
2017 #font => default_font 2195 #font => default_font
2018 @_, 2196 @_,
2019 2197
2020 layout => (new CFClient::Layout), 2198 layout => (new CFClient::Layout 1),
2021 par => [], 2199 par => [],
2022 height => 0, 2200 height => 0,
2023 children => [ 2201 children => [
2024 (new CFClient::UI::Empty expand => 1), 2202 (new CFClient::UI::Empty expand => 1),
2025 (new CFClient::UI::Slider vertical => 1), 2203 (new CFClient::UI::Slider vertical => 1),
2026 ], 2204 ],
2027 ); 2205 );
2028 2206
2029 $self->{children}[1]->connect (changed => sub { 2207 $self->{children}[1]->connect (changed => sub { $self->update });
2030 $self->update;
2031 });
2032 2208
2033 $self 2209 $self
2034} 2210}
2035 2211
2036sub set_fontsize { 2212sub set_fontsize {
2044 my ($self, $text) = @_; 2220 my ($self, $text) = @_;
2045 2221
2046 my $layout = $self->{layout}; 2222 my $layout = $self->{layout};
2047 2223
2048 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2224 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2049 $layout->set_width ($self->{w}); 2225 $layout->set_width ($self->{children}[0]{w});
2050 $layout->set_text ($text); 2226 $layout->set_markup ($text);
2051 2227
2052 ($layout->size)[1] 2228 ($layout->size)[1]
2053} 2229}
2054 2230
2055sub reflow { 2231sub reflow {
2093 2269
2094 return unless $self->{h} > 0; 2270 return unless $self->{h} > 0;
2095 2271
2096 delete $self->{texture}; 2272 delete $self->{texture};
2097 2273
2098 $ROOT->on_refresh ($self, sub { 2274 $ROOT->on_post_alloc ($self, sub {
2099 if (delete $self->{need_reflow}) { 2275 if (delete $self->{need_reflow}) {
2100 my $height = 0; 2276 my $height = 0;
2101 2277
2102 $height += $_->[0] = $self->text_height ($_->[2]) 2278 $height += $_->[0] = $self->text_height ($_->[2])
2103 for @{$self->{par}}; 2279 for @{$self->{par}};
2107 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2283 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2108 2284
2109 delete $self->{texture}; 2285 delete $self->{texture};
2110 } 2286 }
2111 2287
2112 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2288 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2113 glClearColor 0, 0, 0, 1; 2289 glClearColor 0, 0, 0, 0;
2114 glClear GL_COLOR_BUFFER_BIT; 2290 glClear GL_COLOR_BUFFER_BIT;
2115 2291
2116 glEnable GL_BLEND;
2117 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2118 glEnable GL_TEXTURE_2D; 2292 glEnable GL_TEXTURE_2D;
2119 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2293 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2120 2294
2121 my $top = int $self->{children}[1]{range}[0]; 2295 my $top = int $self->{children}[1]{range}[0];
2122 2296
2131 2305
2132 for my $par (@{$self->{par}}) { 2306 for my $par (@{$self->{par}}) {
2133 my $h = $par->[0]; 2307 my $h = $par->[0];
2134 2308
2135 if ($y0 < $y + $h && $y < $y1) { 2309 if ($y0 < $y + $h && $y < $y1) {
2310 $layout->set_foreground (@{ $par->[1] });
2136 $layout->set_text ($par->[2]); 2311 $layout->set_markup ($par->[2]);
2137 2312
2138 glColor @{ $par->[1] };
2139 my ($W, $H) = $layout->size; 2313 my ($W, $H) = $layout->size;
2140 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2314 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2141 } 2315 }
2142 2316
2143 $y += $h; 2317 $y += $h;
2144 } 2318 }
2145 2319
2146 glDisable GL_TEXTURE_2D; 2320 glDisable GL_TEXTURE_2D;
2147 glDisable GL_BLEND;
2148 }; 2321 };
2149 }); 2322 });
2150} 2323}
2151 2324
2152sub _draw { 2325sub _draw {
2153 my ($self) = @_; 2326 my ($self) = @_;
2154 2327
2155 if ($self->{texture}) {
2156 glEnable GL_TEXTURE_2D; 2328 glEnable GL_TEXTURE_2D;
2157 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2329 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2158 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2330 glColor 1, 1, 1, 1;
2331 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2159 glDisable GL_TEXTURE_2D; 2332 glDisable GL_TEXTURE_2D;
2160 }
2161 2333
2162 $self->{children}[1]->draw; 2334 $self->{children}[1]->draw;
2163 2335
2164} 2336}
2165 2337
2259 @_, 2431 @_,
2260 can_events => 0, 2432 can_events => 0,
2261 ) 2433 )
2262} 2434}
2263 2435
2264sub set_markup { 2436sub set_tooltip_from {
2265 my ($self, $text) = @_; 2437 my ($self, $widget) = @_;
2266 2438
2267 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2439 $self->add (new CFClient::UI::Label
2268 $self->{label}->set_markup ($text); 2440 markup => $widget->{tooltip},
2269 $self->add ($self->{label}); 2441 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2442 fontsize => 0.8,
2443 fg => [0, 0, 0, 1],
2444 ellipsise => 0,
2445 font => ($widget->{tooltip_font} || $::FONT_PROP),
2446 );
2270} 2447}
2271 2448
2272sub size_request { 2449sub size_request {
2273 my ($self) = @_; 2450 my ($self) = @_;
2274
2275 $self->child->set_max_size ($::WIDTH * 0.3);
2276 2451
2277 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2452 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2278 2453
2279 ($w + 4, $h + 4) 2454 ($w + 4, $h + 4)
2280} 2455}
2286} 2461}
2287 2462
2288sub _draw { 2463sub _draw {
2289 my ($self) = @_; 2464 my ($self) = @_;
2290 2465
2291 glPushMatrix;
2292 glTranslate 0.375, 0.375; 2466 glTranslate 0.375, 0.375;
2293 2467
2294 my ($w, $h) = @$self{qw(w h)}; 2468 my ($w, $h) = @$self{qw(w h)};
2295 2469
2296 glColor 1, 0.8, 0.4; 2470 glColor 1, 0.8, 0.4;
2307 glVertex 0 , $h; 2481 glVertex 0 , $h;
2308 glVertex $w, $h; 2482 glVertex $w, $h;
2309 glVertex $w, 0; 2483 glVertex $w, 0;
2310 glEnd; 2484 glEnd;
2311 2485
2312 glPopMatrix; 2486 glTranslate 2 - 0.375, 2 - 0.375;
2313
2314 glTranslate 2, 2;
2315 $self->SUPER::_draw; 2487 $self->SUPER::_draw;
2316} 2488}
2317 2489
2318############################################################################# 2490#############################################################################
2319 2491
2334 2506
2335sub size_request { 2507sub size_request {
2336 (32, 8) 2508 (32, 8)
2337} 2509}
2338 2510
2339sub draw { 2511sub _draw {
2340 my ($self) = @_; 2512 my ($self) = @_;
2341 2513
2514 return unless $::CONN;#d# manage and cache textures differently
2342 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2515 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2343 2516
2517 # TODO animation
2344 if ($tex) { 2518 if ($tex) {
2345 glEnable GL_BLEND;
2346 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2347 glEnable GL_TEXTURE_2D; 2519 glEnable GL_TEXTURE_2D;
2348 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2520 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2349 glColor 1, 1, 1, 1; 2521 glColor 1, 1, 1, 1;
2350 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2522 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2351 glDisable GL_TEXTURE_2D; 2523 glDisable GL_TEXTURE_2D;
2352 glDisable GL_BLEND; 2524 }
2525}
2526
2527#############################################################################
2528
2529package CFClient::UI::InventoryItem;
2530
2531our @ISA = CFClient::UI::HBox::;
2532
2533sub _item_to_desc {
2534 my ($item) = @_;
2535
2536 my $desc =
2537 $item->{nrof} < 2
2538 ? $item->{name}
2539 : "$item->{nrof} $item->{name_pl}";
2540
2541 $item->{flags} & Crossfire::Protocol::F_OPEN
2542 and $desc .= " (open)";
2543 $item->{flags} & Crossfire::Protocol::F_APPLIED
2544 and $desc .= " (applied)";
2545 $item->{flags} & Crossfire::Protocol::F_UNPAID
2546 and $desc .= " (unpaid)";
2547 $item->{flags} & Crossfire::Protocol::F_MAGIC
2548 and $desc .= " (magic)";
2549 $item->{flags} & Crossfire::Protocol::F_CURSED
2550 and $desc .= " (cursed)";
2551 $item->{flags} & Crossfire::Protocol::F_DAMNED
2552 and $desc .= " (damned)";
2553 $item->{flags} & Crossfire::Protocol::F_LOCKED
2554 and $desc .= " *";
2555
2556 $desc
2557}
2558
2559sub new {
2560 my $class = shift;
2561
2562 my %args = @_;
2563
2564 my $item = delete $args{item};
2565
2566 my $desc = _item_to_desc ($item);
2567
2568 my $self = $class->SUPER::new (
2569 can_hover => 1,
2570 can_events => 1,
2571 tooltip => ((CFClient::UI::Label::escape $desc)
2572 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2573 connect_button_down => sub {
2574 my ($self, $ev, $x, $y) = @_;
2575
2576 # todo: maybe put examine on 1? but should just be a tooltip :(
2577 if ($ev->{button} == 1) {
2578 my $targ = $::CONN->{player}{tag};
2579
2580 if ($item->{container} == $::CONN->{player}{tag}) {
2581 $targ = $main::OPENCONT;
2582 }
2583
2584 $::CONN->send ("move $targ $item->{tag} 0");
2585 } elsif ($ev->{button} == 2) {
2586 $::CONN->send ("apply $item->{tag}");
2587 } elsif ($ev->{button} == 3) {
2588 my @menu_items = (
2589 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2590 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2591 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2592 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2593 [
2594 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2595 sub { $::CONN->send ("lock $item->{tag}") },
2596 ],
2597 );
2598
2599 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2600 }
2601
2602 1
2603 },
2604 %args
2605 );
2606
2607
2608 $self->add (new CFClient::UI::Face
2609 can_events => 0,
2610 face => $item->{face},
2611 anim => $item->{anim},
2612 animspeed => $item->{animspeed},
2613 );
2614
2615 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2616
2617 $self->{item} = $item;
2618
2619 $self->update_item;
2620
2621 $self
2622}
2623
2624sub update_item {
2625 my ($self) = @_;
2626
2627 my $desc = _item_to_desc ($self->{item});
2628
2629 $self->{name_lbl}->set_text ($desc);
2630}
2631
2632#############################################################################
2633
2634package CFClient::UI::Inventory;
2635
2636our @ISA = CFClient::UI::ScrolledWindow::;
2637
2638sub new {
2639 my $class = shift;
2640
2641 my $self = $class->SUPER::new (
2642 scrolled => (new CFClient::UI::VBox),
2643 @_,
2644 );
2645
2646 $self
2647}
2648
2649sub set_items {
2650 my ($self, $items) = @_;
2651
2652 $self->{scrolled}->clear;
2653 return unless $items;
2654
2655 my @items = sort {
2656 ($a->{type} <=> $b->{type})
2657 or ($a->{name} cmp $b->{name})
2658 } @$items;
2659
2660 $self->{real_items} = \@items;
2661
2662 for my $item (@items) {
2663 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2664 $item->update_item ();
2665 }
2666
2667 $self->{scrolled}->add (@items);
2668
2669# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2670}
2671
2672sub size_request {
2673 my ($self) = @_;
2674 ($self->{req_w}, $self->{req_h});
2675}
2676
2677#############################################################################
2678
2679package CFClient::UI::Menu;
2680
2681our @ISA = CFClient::UI::FancyFrame::;
2682
2683use CFClient::OpenGL;
2684
2685sub new {
2686 my $class = shift;
2687
2688 my $self = $class->SUPER::new (
2689 items => [],
2690 z => 100,
2691 @_,
2692 );
2693
2694 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2695
2696 for my $item (@{ $self->{items} }) {
2697 my ($widget, $cb) = @$item;
2698
2699 # handle various types of items, only text for now
2700 if (!ref $widget) {
2701 $widget = new CFClient::UI::Label
2702 can_hover => 1,
2703 can_events => 1,
2704 text => $widget;
2705 }
2706
2707 $self->{item}{$widget} = $item;
2708
2709 $self->{vbox}->add ($widget);
2710 }
2711
2712 $self
2713}
2714
2715# popup given the event (must be a mouse button down event currently)
2716sub popup {
2717 my ($self, $ev) = @_;
2718
2719 $self->emit ("popdown");
2720
2721 # maybe save $GRAB? must be careful about events...
2722 $GRAB = $self;
2723 $self->{button} = $ev->{button};
2724
2725 $self->show;
2726 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2727}
2728
2729sub mouse_motion {
2730 my ($self, $ev, $x, $y) = @_;
2731
2732 # TODO: should use vbox->find_widget or so
2733 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2734 $self->{hover} = $self->{item}{$HOVER};
2735}
2736
2737sub button_up {
2738 my ($self, $ev, $x, $y) = @_;
2739
2740 if ($ev->{button} == $self->{button}) {
2741 undef $GRAB;
2742 $self->hide;
2743
2744 $self->emit ("popdown");
2745 $self->{hover}[1]->() if $self->{hover};
2746 }
2747}
2748
2749#############################################################################
2750
2751package CFClient::UI::Statusbox;
2752
2753our @ISA = CFClient::UI::VBox::;
2754
2755sub new {
2756 my $class = shift;
2757
2758 $class->SUPER::new (
2759 fontsize => 0.8,
2760 @_,
2353 } 2761 )
2762}
2763
2764sub reorder {
2765 my ($self) = @_;
2766 my $NOW = time;
2767
2768 while (my ($k, $v) = each %{ $self->{item} }) {
2769 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2770 }
2771
2772 my @widgets;
2773
2774 my @items = sort {
2775 $a->{pri} <=> $b->{pri}
2776 or $b->{id} <=> $a->{id}
2777 } values %{ $self->{item} };
2778
2779 my $count = 10 + 1;
2780 for my $item (@items) {
2781 last unless --$count;
2782
2783 push @widgets, $item->{label} ||= do {
2784 # TODO: doesn't handle markup well (read as: at all)
2785 my $short = $item->{count} > 1
2786 ? "<b>$item->{count} ×</b> $item->{text}"
2787 : $item->{text};
2788
2789 for ($short) {
2790 s/^\s+//;
2791 s/\s+/ /g;
2792 }
2793
2794 new CFClient::UI::Label
2795 markup => $short,
2796 tooltip => $item->{tooltip},
2797 tooltip_font => $::FONT_PROP,
2798 tooltip_width => 0.67,
2799 fontsize => $item->{fontsize} || $self->{fontsize},
2800 max_w => $::WIDTH * 0.44,
2801 fg => $item->{fg},
2802 can_events => 1,
2803 can_hover => 1
2804 };
2805 }
2806
2807 $self->clear;
2808 $self->SUPER::add (reverse @widgets);
2809}
2810
2811sub add {
2812 my ($self, $text, %arg) = @_;
2813
2814 $text =~ s/^\s+//;
2815 $text =~ s/\s+$//;
2816
2817 my $timeout = time + ((delete $arg{timeout}) || 60);
2818
2819 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2820
2821 if (my $item = $self->{item}{$group}) {
2822 if ($item->{text} eq $text) {
2823 $item->{count}++;
2824 } else {
2825 $item->{count} = 1;
2826 $item->{text} = $item->{tooltip} = $text;
2827 }
2828 $item->{id} = ++$self->{id};
2829 $item->{timeout} = $timeout;
2830 delete $item->{label};
2831 } else {
2832 $self->{item}{$group} = {
2833 id => ++$self->{id},
2834 text => $text,
2835 timeout => $timeout,
2836 tooltip => $text,
2837 fg => [0.8, 0.8, 0.8, 0.8],
2838 pri => 0,
2839 count => 1,
2840 %arg,
2841 };
2842 }
2843
2844 $self->reorder;
2845}
2846
2847sub reconfigure {
2848 my ($self) = @_;
2849
2850 delete $_->{label}
2851 for values %{ $self->{item} || {} };
2852
2853 $self->reorder;
2854 $self->SUPER::reconfigure;
2354} 2855}
2355 2856
2356############################################################################# 2857#############################################################################
2357 2858
2358package CFClient::UI::Root; 2859package CFClient::UI::Root;
2359 2860
2360our @ISA = CFClient::UI::Container::; 2861our @ISA = CFClient::UI::Container::;
2361 2862
2362use CFClient::OpenGL; 2863use CFClient::OpenGL;
2363 2864
2364sub check_size { 2865sub new {
2365 my ($self) = @_; 2866 my $class = shift;
2366 2867
2367 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2868 $class->SUPER::new (
2368} 2869 @_,
2369 2870 )
2370sub size_request {
2371 ($::WIDTH, $::HEIGHT)
2372} 2871}
2373 2872
2374sub configure { 2873sub configure {
2375 my ($self, $x, $y, $w, $h) = @_; 2874 my ($self, $x, $y, $w, $h) = @_;
2376 2875
2377 $self->SUPER::configure ($x, $y, $w, $h); 2876 $self->{w} = $w;
2877 $self->{h} = $h;
2878}
2378 2879
2880sub check_size {
2881 my ($self) = @_;
2882
2883 $self->size_allocate ($self->{w}, $self->{h})
2884 if $self->{w};
2885}
2886
2887sub size_request {
2888 my ($self) = @_;
2889
2890 ($self->{w}, $self->{h})
2891}
2892
2893sub size_allocate {
2894 my ($self, $w, $h) = @_;
2895
2896 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2897 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2898
2899 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2900 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2901
2379 for my $child (@{$self->{children}}) { 2902 for my $child ($self->children) {
2380 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2903 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2381 2904
2905 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2906 if exists $child->{req_x};
2907
2908 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2909 if exists $child->{req_y};
2910
2382 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2911 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2383 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2912 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2913
2384 $child->configure ($X, $Y, $W,$H); 2914 $child->configure ($X, $Y, $W, $H);
2385 } 2915 }
2386} 2916}
2387 2917
2388sub _topleft { 2918sub coord2local {
2389 my ($self, $x, $y) = @_; 2919 my ($self, $x, $y) = @_;
2390 2920
2391 ($x, $y) 2921 ($x, $y)
2392} 2922}
2393 2923
2924sub coord2global {
2925 my ($self, $x, $y) = @_;
2926
2927 ($x, $y)
2928}
2929
2394sub update { 2930sub update {
2395 my ($self) = @_; 2931 my ($self) = @_;
2396 2932
2397 $self->check_size; 2933 $self->check_size;
2398 ::refresh (); 2934 $::WANT_REFRESH++;
2399} 2935}
2400 2936
2401sub add { 2937sub add {
2402 my ($self, $child) = @_; 2938 my ($self, @children) = @_;
2403 2939
2940 for my $child (@children) {
2941 $child->{toplevel} = 1;
2942
2404 # integerize window positions 2943 # integerise window positions
2405 $child->{x} = int $child->{x}; 2944 $child->{x} = int $child->{x};
2406 $child->{y} = int $child->{y}; 2945 $child->{y} = int $child->{y};
2946 }
2407 2947
2408 $self->SUPER::add ($child); 2948 $self->SUPER::add (@children);
2409} 2949}
2410 2950
2411sub on_refresh { 2951sub on_refresh {
2412 my ($self, $id, $cb) = @_; 2952 my ($self, $id, $cb) = @_;
2413 2953
2414 $self->{refresh_hook}{$id} = $cb; 2954 $self->{refresh_hook}{$id} = $cb;
2415} 2955}
2416 2956
2957sub on_post_alloc {
2958 my ($self, $id, $cb) = @_;
2959
2960 $self->{post_alloc_hook}{$id} = $cb;
2961}
2962
2417sub draw { 2963sub draw {
2418 my ($self) = @_; 2964 my ($self) = @_;
2419 2965
2420 while (my $rcb = delete $self->{refresh_hook}) { 2966 while ($self->{refresh_hook}) {
2421 $_->() for values %$rcb; 2967 $_->()
2968 for values %{delete $self->{refresh_hook}};
2969 }
2970
2971 if ($self->{check_size}) {
2972 my @queue = ([], []);
2973
2974 for (;;) {
2975 if ($self->{check_size}) {
2976 # heuristic: check containers last
2977 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2978 for values %{delete $self->{check_size}}
2979 }
2980
2981 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2982
2983 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2984 ? @$widget{qw(user_w user_h)}
2985 : $widget->size_request;
2986
2987 if (delete $widget->{force_alloc}
2988 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2989 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2990
2991 $widget->{req_w} = $w;
2992 $widget->{req_h} = $h;
2993
2994 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2995
2996 $widget->{parent}->check_size
2997 if $widget->{parent};
2998 }
2999 }
3000 }
3001
3002 while ($self->{size_alloc}) {
3003 for (values %{delete $self->{size_alloc}}) {
3004 my ($widget, $w, $h) = @$_;
3005
3006 $w = 0 if $w < 0;
3007 $h = 0 if $h < 0;
3008
3009 $widget->{w} = $w;
3010 $widget->{h} = $h;
3011 $widget->size_allocate ($w, $h);
3012 $widget->emit (size_allocate => $w, $h);
3013 }
3014 }
3015
3016 while ($self->{post_alloc_hook}) {
3017 $_->()
3018 for values %{delete $self->{post_alloc_hook}};
2422 } 3019 }
2423 3020
2424 glViewport 0, 0, $::WIDTH, $::HEIGHT; 3021 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2425 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3022 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2426 glClear GL_COLOR_BUFFER_BIT; 3023 glClear GL_COLOR_BUFFER_BIT;
2437############################################################################# 3034#############################################################################
2438 3035
2439package CFClient::UI; 3036package CFClient::UI;
2440 3037
2441$ROOT = new CFClient::UI::Root; 3038$ROOT = new CFClient::UI::Root;
2442$TOOLTIP = new CFClient::UI::Tooltip; 3039$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2443 3040
24441 30411
2445 3042

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines