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.179 by elmex, Fri Apr 28 05:17:21 2006 UTC vs.
Revision 1.218 by root, Sat May 20 15:37:27 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
30 $TOOLTIP->show; 32 $TOOLTIP->show;
31 33
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 35
34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
36 } 37 if $x + $TOOLTIP->{w} > $::WIDTH;
37 38
38 $TOOLTIP->move ($x, $y); 39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
39 } 42 }
40 43
41 return; 44 return;
42 } 45 }
43 } 46 }
130 133
131 for (@$vals) { 134 for (@$vals) {
132 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
133 $rem += $_ - $i; 136 $rem += $_ - $i;
134 $_ = $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;
135 } 161 }
136} 162}
137 163
138############################################################################# 164#############################################################################
139 165
158 if (/^connect_(.*)$/) { 184 if (/^connect_(.*)$/) {
159 $self->connect ($1 => delete $self->{$_}); 185 $self->connect ($1 => delete $self->{$_});
160 } 186 }
161 } 187 }
162 188
189 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
190
163 $self 191 $self
164} 192}
165 193
166sub destroy { 194sub destroy {
167 my ($self) = @_; 195 my ($self) = @_;
232 $self->{y} = $y; 260 $self->{y} = $y;
233 $self->update; 261 $self->update;
234 } 262 }
235 263
236 if ($self->{w} != $w || $self->{h} != $h) { 264 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 265 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
238 $self->{h} = $h;
239
240 $self->size_allocate ($w, $h);
241 $self->update;
242 } 266 }
243} 267}
244 268
245sub size_allocate { 269sub size_allocate {
246 # nothing to be done 270 # nothing to be done
247} 271}
248 272
273sub reconfigure {
274 my ($self) = @_;
275
276 $self->check_size (1);
277 $self->update;
278}
279
249sub children { 280sub children {
250}
251
252# call when resoltuion changes etc.
253sub reconfigure {
254 my ($self) = @_;
255
256 $_->reconfigure
257 for $self->children;
258
259 $_->check_size;
260} 281}
261 282
262sub set_max_size { 283sub set_max_size {
263 my ($self, $w, $h) = @_; 284 my ($self, $w, $h) = @_;
264 285
265 delete $self->{max_w}; $self->{max_w} = $w if $w; 286 delete $self->{max_w}; $self->{max_w} = $w if $w;
266 delete $self->{max_h}; $self->{max_h} = $h if $h; 287 delete $self->{max_h}; $self->{max_h} = $h if $h;
267} 288}
268 289
269# return top left coordinates 290sub set_tooltip {
270sub _topleft {
271 my ($self, $x, $y) = @_; 291 my ($self, $tooltip) = @_;
272 292
273 $self->{parent} 293 $self->{tooltip} = $tooltip;
274 or Carp::confess "no parent widget in _topleft\n";#d#
275 294
276 $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 }
277} 299}
278 300
279# translate global coordinates to local coordinate system 301# translate global coordinates to local coordinate system
280sub coord2local { 302sub coord2local {
281 my ($self, $x, $y) = @_; 303 my ($self, $x, $y) = @_;
282 304
283 my ($X, $Y) = $self->_topleft; 305 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
284 ($x - $X, $y - $Y)
285} 306}
286 307
287# translate local coordinates to global coordinate system 308# translate local coordinates to global coordinate system
288sub coord2global { 309sub coord2global {
289 my ($self, $x, $y) = @_; 310 my ($self, $x, $y) = @_;
290 311
291 my ($X, $Y) = $self->_topleft; 312 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
292 ($x + $X, $y + $Y)
293} 313}
294 314
295sub focus_in { 315sub focus_in {
296 my ($self) = @_; 316 my ($self) = @_;
297 317
369 glVertex $self->{w}, 0; 389 glVertex $self->{w}, 0;
370 glVertex $self->{w}, $self->{h}; 390 glVertex $self->{w}, $self->{h};
371 glVertex 0 , $self->{h}; 391 glVertex 0 , $self->{h};
372 glEnd; 392 glEnd;
373 glPopMatrix; 393 glPopMatrix;
374 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;
375 } 395 }
376} 396}
377 397
378sub _draw { 398sub _draw {
379 my ($self) = @_; 399 my ($self) = @_;
395 415
396sub set_parent { 416sub set_parent {
397 my ($self, $parent) = @_; 417 my ($self, $parent) = @_;
398 418
399 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};
400} 424}
401 425
402sub check_size { 426sub check_size {
403 my ($self) = @_; 427 my ($self, $forced) = @_;
404 428
405 $self->{parent} 429 $self->{force_alloc} = 1 if $forced;
406 or return 1; 430 $CFClient::UI::ROOT->{check_size}{$self} = $self;
407
408 my ($w, $h) = $self->{user_w} && $self->{user_h}
409 ? @$self{qw(user_w user_h)}
410 : $self->size_request;
411
412 if ($w != $self->{req_w} || $h != $self->{req_h}) {
413 $self->{req_w} = $w;
414 $self->{req_h} = $h;
415
416 $self->{parent}->check_size
417 or $self->size_allocate (
418 (List::Util::max $self->{w}, $w),
419 (List::Util::max $self->{h}, $h),
420 );
421
422 1
423 } else {
424 0
425 }
426} 431}
427 432
428sub update { 433sub update {
429 my ($self) = @_; 434 my ($self) = @_;
430 435
445} 450}
446 451
447sub DESTROY { 452sub DESTROY {
448 my ($self) = @_; 453 my ($self) = @_;
449 454
455 delete $WIDGET{$self+0};
450 #$self->deactivate; 456 #$self->deactivate;
451} 457}
452 458
453############################################################################# 459#############################################################################
454 460
463 my $class = shift; 469 my $class = shift;
464 470
465 # range [value, low, high, page] 471 # range [value, low, high, page]
466 472
467 $class->SUPER::new ( 473 $class->SUPER::new (
468 bg => [0, 0, 0, 0.2], 474 #bg => [0, 0, 0, 0.2],
469 active_bg => [1, 1, 1, 0.5], 475 #active_bg => [1, 1, 1, 0.5],
470 @_ 476 @_
471 ) 477 )
472} 478}
473 479
474sub _draw { 480sub _draw {
475 my ($self) = @_; 481 my ($self) = @_;
476 482
483 my $color = $FOCUS == $self && $self->{active_bg}
484 ? $self->{active_bg}
485 : $self->{bg};
486
487 if ($color && (@$color < 4 || $color->[3])) {
477 my ($w, $h) = @$self{qw(w h)}; 488 my ($w, $h) = @$self{qw(w h)};
478 489
479 glEnable GL_BLEND; 490 glEnable GL_BLEND;
480 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
481 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 492 glColor @$color;
482 493
483 glBegin GL_QUADS; 494 glBegin GL_QUADS;
484 glVertex 0 , 0; 495 glVertex 0 , 0;
485 glVertex 0 , $h; 496 glVertex 0 , $h;
486 glVertex $w, $h; 497 glVertex $w, $h;
487 glVertex $w, 0; 498 glVertex $w, 0;
488 glEnd; 499 glEnd;
489 500
490 glDisable GL_BLEND; 501 glDisable GL_BLEND;
502 }
491} 503}
492 504
493############################################################################# 505#############################################################################
494 506
495package CFClient::UI::Empty; 507package CFClient::UI::Empty;
527 539
528 $self 540 $self
529} 541}
530 542
531sub add { 543sub add {
532 my ($self, $child) = @_; 544 my ($self, @widgets) = @_;
533 545
534 $child->set_parent ($self); 546 $_->set_parent ($self)
547 for @widgets;
535 548
536 use sort 'stable'; 549 use sort 'stable';
537 550
538 $self->{children} = [ 551 $self->{children} = [
539 sort { $a->{z} <=> $b->{z} } 552 sort { $a->{z} <=> $b->{z} }
540 @{$self->{children}}, $child 553 @{$self->{children}}, @widgets
541 ]; 554 ];
542 555
543 $child->check_size; 556 $self->check_size (1);
557 $self->update;
544} 558}
545 559
546sub children { 560sub children {
547 @{ $_[0]{children} } 561 @{ $_[0]{children} }
548} 562}
567 581
568 for (@$children) { 582 for (@$children) {
569 delete $_->{parent}; 583 delete $_->{parent};
570 $_->hide; 584 $_->hide;
571 } 585 }
586
587 $self->check_size;
588 $self->update;
572} 589}
573 590
574sub find_widget { 591sub find_widget {
575 my ($self, $x, $y) = @_; 592 my ($self, $x, $y) = @_;
576 593
651} 668}
652 669
653sub update { 670sub update {
654 my ($self) = @_; 671 my ($self) = @_;
655 672
656 $ROOT->on_refresh ($self => sub { $self->render_child }); 673 $ROOT->on_post_alloc ($self => sub { $self->render_child });
657 $self->SUPER::update; 674 $self->SUPER::update;
658} 675}
659 676
660sub size_allocate { 677sub size_allocate {
661 my ($self, $w, $h) = @_; 678 my ($self, $w, $h) = @_;
662 679
663 $self->SUPER::size_allocate ($w, $h); 680 $self->SUPER::size_allocate ($w, $h);
664 $self->update; 681 $self->update;
682}
683
684sub _render {
685 $_[0]{children}[0]->draw;
665} 686}
666 687
667sub render_child { 688sub render_child {
668 my ($self) = @_; 689 my ($self) = @_;
669 690
670 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 691 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
671 glClearColor 0, 0, 0, 0; 692 glClearColor 0, 0, 0, 0;
672 glClear GL_COLOR_BUFFER_BIT; 693 glClear GL_COLOR_BUFFER_BIT;
673 $self->child->draw; 694
674# glColorMask 1, 1, 1, 0; 695 $self->_render;
675# glEnable GL_BLEND;
676# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
677# glRasterPos 0, 0;
678# glCopyPixels 0, 0, $self->{w}, $self->{h};
679# glDisable GL_BLEND;
680# glColorMask 1, 1, 1, 1;
681 }; 696 };
682} 697}
683 698
684sub _draw { 699sub _draw {
685 my ($self) = @_; 700 my ($self) = @_;
687 my ($w, $h) = ($self->w, $self->h); 702 my ($w, $h) = ($self->w, $self->h);
688 703
689 my $tex = $self->{texture} 704 my $tex = $self->{texture}
690 or return; 705 or return;
691 706
692 glEnable GL_BLEND;
693 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
694 glEnable GL_TEXTURE_2D; 707 glEnable GL_TEXTURE_2D;
695 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 708 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
696 glColor 0, 0, 0, 1; 709 glColor 1, 1, 1, 1;
697 710
698 $tex->draw_quad (0, 0, $w, $h); 711 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
699 712
700 glDisable GL_BLEND;
701 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
702} 714}
703 715
704############################################################################# 716#############################################################################
705 717
706package CFClient::UI::ViewPort; 718package CFClient::UI::ViewPort;
707 719
708our @ISA = CFClient::UI::Window::; 720our @ISA = CFClient::UI::Window::;
709 721
710sub new { die }
711
712sub size_request { 722sub size_request {
713 my ($self) = @_; 723 my ($self) = @_;
714 724
715 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 725 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
716 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 726 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
717 727
718 @$self{qw(child_w child_h)} 728 @$self{qw(child_w child_h)}
719} 729}
720 730
721sub _draw { 731sub size_allocate {
722 my ($self) = @_; 732 my ($self, $w, $h) = @_;
723 733
724 $self->{children}[1]->draw; 734 $self->update;
725} 735}
726 736
737sub set_offset {
738 my ($self, $x, $y) = @_;
727 739
728############################################################################# 740 $self->{view_x} = int $x;
741 $self->{view_y} = int $y;
729 742
743 $self->update;
744}
745
746# hmm, this does not work for topleft of $self... but we should not ask for that
747sub coord2local {
748 my ($self, $x, $y) = @_;
749
750 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
751}
752
753sub coord2global {
754 my ($self, $x, $y) = @_;
755
756 $x = List::Util::min $self->{w}, $x - $self->{view_x};
757 $y = List::Util::min $self->{h}, $y - $self->{view_y};
758
759 $self->SUPER::coord2global ($x, $y)
760}
761
762sub find_widget {
763 my ($self, $x, $y) = @_;
764
765 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
766 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
767 ) {
768 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
769 } else {
770 $self->CFClient::UI::Base::find_widget ($x, $y)
771 }
772}
773
774sub _render {
775 my ($self) = @_;
776
777 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
778
779 $self->SUPER::_render;
780}
781
782#############################################################################
783
730package CFClient::UI::Frame; 784package CFClient::UI::ScrolledWindow;
731 785
732our @ISA = CFClient::UI::Bin::; 786our @ISA = CFClient::UI::HBox::;
733
734use CFClient::OpenGL;
735 787
736sub new { 788sub new {
737 my $class = shift; 789 my $class = shift;
738 790
791 my $self;
792
793 my $slider = new CFClient::UI::Slider
794 vertical => 1,
795 range => [0, 0, 1, 0.01], # HACK fix
796 connect_changed => sub {
797 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
798 },
799 ;
800
739 my $self = $class->SUPER::new ( 801 $self = $class->SUPER::new (
740 bg => [1, 1, 1, 1], 802 vp => (new CFClient::UI::ViewPort expand => 1),
741 border_bg => [1, 1, 1, 1], 803 slider => $slider,
742 border => 0.8,
743 @_ 804 @_,
744 ); 805 );
745 806
807 $self->{vp}->add ($self->{scrolled});
808 $self->add ($self->{vp});
809 $self->add ($self->{slider});
810
746 $self 811 $self
747} 812}
748 813
814#TODO# update range on size_allocate depending on child
815# update viewport offset on scroll
816
817#############################################################################
818
819package CFClient::UI::Frame;
820
821our @ISA = CFClient::UI::Bin::;
822
823use CFClient::OpenGL;
824
825sub new {
826 my $class = shift;
827
828 $class->SUPER::new (
829 bg => undef,
830 @_,
831 )
832}
833
749sub _draw { 834sub _draw {
750 my ($self) = @_; 835 my ($self) = @_;
751 836
752 my ($w, $h) = ($self->{w}, $self->{h}); 837 if ($self->{bg}) {
838 my ($w, $h) = @$self{qw(w h)};
753 839
754 glEnable GL_BLEND; 840 glEnable GL_BLEND;
755 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 841 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
756 glEnable GL_TEXTURE_2D; 842 glColor @{ $self->{bg} };
757 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
758 843
759# glBegin GL_QUADS; 844 glBegin GL_QUADS;
760# glColor 0, 0, 0, 0;
761# glVertex 0 , 0; 845 glVertex 0 , 0;
762# glVertex 0 , $h; 846 glVertex 0 , $h;
763# glVertex $w, $h; 847 glVertex $w, $h;
764# glVertex $w, 0; 848 glVertex $w, 0;
765# glEnd; 849 glEnd;
766 850
767
768 $self->child->draw;
769 glDisable GL_BLEND; 851 glDisable GL_BLEND;
770 glDisable GL_TEXTURE_2D; 852 }
853
854 $self->SUPER::_draw;
771} 855}
772 856
773############################################################################# 857#############################################################################
774 858
775package CFClient::UI::FancyFrame; 859package CFClient::UI::FancyFrame;
789 873
790 my $self = $class->SUPER::new ( 874 my $self = $class->SUPER::new (
791 bg => [1, 1, 1, 1], 875 bg => [1, 1, 1, 1],
792 border_bg => [1, 1, 1, 1], 876 border_bg => [1, 1, 1, 1],
793 border => 0.6, 877 border => 0.6,
878 toplevel => 1,
794 can_events => 1, 879 can_events => 1,
795 @_ 880 @_
796 ); 881 );
797 882
798 $self->{title} &&= new CFClient::UI::Label 883 $self->{title} &&= new CFClient::UI::Label
823 my ($self, $w, $h) = @_; 908 my ($self, $w, $h) = @_;
824 909
825 $h -= List::Util::max 0, $self->border * 2; 910 $h -= List::Util::max 0, $self->border * 2;
826 $w -= List::Util::max 0, $self->border * 2; 911 $w -= List::Util::max 0, $self->border * 2;
827 912
828 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 913 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
829 if $self->{title}; 914 if $self->{title};
830 915
831 $self->child->configure ($self->border, $self->border, $w, $h); 916 $self->child->configure ($self->border, $self->border, $w, $h);
832} 917}
833 918
891 my ($self) = @_; 976 my ($self) = @_;
892 977
893 my ($w, $h ) = ($self->{w}, $self->{h}); 978 my ($w, $h ) = ($self->{w}, $self->{h});
894 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 979 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
895 980
896 glEnable GL_BLEND;
897 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
898 glEnable GL_TEXTURE_2D; 981 glEnable GL_TEXTURE_2D;
899 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 982 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
900 983
901 my $border = $self->border; 984 my $border = $self->border;
902 985
903 glColor @{ $self->{border_bg} }; 986 glColor @{ $self->{border_bg} };
904 $tex[1]->draw_quad (0, 0, $w, $border); 987 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
905 $tex[3]->draw_quad (0, $border, $border, $ch); 988 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
906 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 989 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
907 $tex[4]->draw_quad (0, $h - $border, $w, $border); 990 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
908 991
909 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 992 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
910 my $bg = $tex[0]; 993 my $bg = $tex[0];
911 994
912 # TODO: repeat texture not scale 995 # TODO: repeat texture not scale
916 glColor @{ $self->{bg} }; 999 glColor @{ $self->{bg} };
917 1000
918 $bg->{s} = $rep_x; 1001 $bg->{s} = $rep_x;
919 $bg->{t} = $rep_y; 1002 $bg->{t} = $rep_y;
920 $bg->{wrap_mode} = 1; 1003 $bg->{wrap_mode} = 1;
921 $bg->draw_quad ($border, $border, $cw, $ch); 1004 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1005 }
922 1006
923 glDisable GL_TEXTURE_2D; 1007 glDisable GL_TEXTURE_2D;
924 glDisable GL_BLEND;
925 }
926 1008
927 $self->{title}->draw if $self->{title}; 1009 $self->{title}->draw if $self->{title};
928 1010
929 $self->child->draw; 1011 $self->child->draw;
930} 1012}
1157} 1239}
1158 1240
1159sub size_allocate { 1241sub size_allocate {
1160 my ($self, $w, $h) = @_; 1242 my ($self, $w, $h) = @_;
1161 1243
1244 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1245
1162 my $children = $self->{children}; 1246 my $children = $self->{children};
1163 1247
1164 my @h = map $_->{req_h}, @$children; 1248 my @h = map $_->{req_h}, @$children;
1165 1249
1166 my $req_h = List::Util::sum @h; 1250 my $req_h = List::Util::sum @h;
1195 1279
1196############################################################################# 1280#############################################################################
1197 1281
1198package CFClient::UI::Label; 1282package CFClient::UI::Label;
1199 1283
1200our @ISA = CFClient::UI::Base::; 1284our @ISA = CFClient::UI::DrawBG::;
1201 1285
1202use CFClient::OpenGL; 1286use CFClient::OpenGL;
1203 1287
1204sub new { 1288sub new {
1205 my ($class, %arg) = @_; 1289 my ($class, %arg) = @_;
1206 1290
1207 my $self = $class->SUPER::new ( 1291 my $self = $class->SUPER::new (
1208 fg => [1, 1, 1], 1292 fg => [1, 1, 1],
1293 #bg => none
1294 #active_bg => none
1209 #font => default_font 1295 #font => default_font
1296 #text => initial text
1297 #markup => initial narkup
1298 #max_w => maximum pixel width
1299 ellipsise => 3, # end
1300 layout => (new CFClient::Layout),
1210 fontsize => 1, 1301 fontsize => 1,
1211 text => "",
1212 align => -1, 1302 align => -1,
1213 valign => -1, 1303 valign => -1,
1214 padding => 2, 1304 padding => 2,
1215 layout => new CFClient::Layout,
1216 can_events => 0, 1305 can_events => 0,
1217 %arg 1306 %arg
1218 ); 1307 );
1219 1308
1220 if (exists $self->{template}) { 1309 if (exists $self->{template}) {
1221 my $layout = new CFClient::Layout; 1310 my $layout = new CFClient::Layout;
1222 $layout->set_text (delete $self->{template}); 1311 $layout->set_text (delete $self->{template});
1223 $self->{template} = $layout; 1312 $self->{template} = $layout;
1224 } 1313 }
1225 1314
1226 $self->set_text (delete $self->{text}) if exists $self->{text}; 1315 if (exists $self->{markup}) {
1227 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1316 $self->set_markup (delete $self->{markup});
1317 } else {
1318 $self->set_text (delete $self->{text});
1319 }
1228 1320
1229 $self 1321 $self
1230} 1322}
1231 1323
1232sub escape { 1324sub escape($) {
1233 local $_ = $_[1]; 1325 local $_ = $_[0];
1234 1326
1235 s/&/&amp;/g; 1327 s/&/&amp;/g;
1236 s/>/&gt;/g; 1328 s/>/&gt;/g;
1237 s/</&lt;/g; 1329 s/</&lt;/g;
1238 1330
1239 $_[1] 1331 $_
1240} 1332}
1241 1333
1242sub update { 1334sub update {
1243 my ($self) = @_; 1335 my ($self) = @_;
1244 1336
1245 delete $self->{texture}; 1337 delete $self->{texture};
1246 $self->SUPER::update; 1338 $self->SUPER::update;
1247} 1339}
1248 1340
1249sub reconfigure {
1250 my ($self) = @_;
1251
1252 delete $self->{texture};
1253}
1254
1255sub set_text { 1341sub set_text {
1256 my ($self, $text) = @_; 1342 my ($self, $text) = @_;
1257 1343
1258 return if $self->{text} eq "T$text"; 1344 return if $self->{text} eq "T$text";
1259 $self->{text} = "T$text"; 1345 $self->{text} = "T$text";
1260 1346
1347 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1261 $self->{layout}->set_text ($text); 1348 $self->{layout}->set_text ($text);
1262 1349
1263 delete $self->{texture};
1264 $self->update; 1350 $self->update;
1265 $self->check_size; 1351 $self->check_size;
1266} 1352}
1267 1353
1268sub set_markup { 1354sub set_markup {
1269 my ($self, $markup) = @_; 1355 my ($self, $markup) = @_;
1270 1356
1271 return if $self->{text} eq "M$markup"; 1357 return if $self->{text} eq "M$markup";
1272 $self->{text} = "M$markup"; 1358 $self->{text} = "M$markup";
1273 1359
1360 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1361
1362 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1274 $self->{layout}->set_markup ($markup); 1363 $self->{layout}->set_markup ($markup);
1275 1364
1276 delete $self->{texture};
1277 $self->update; 1365 $self->update;
1278 $self->check_size; 1366 $self->check_size;
1279} 1367}
1280 1368
1281sub size_request { 1369sub size_request {
1282 my ($self) = @_; 1370 my ($self) = @_;
1283 1371
1284 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1372 $self->{layout}->set_font ($self->{font}) if $self->{font};
1285 $self->{layout}->set_width ($self->{max_w} || -1); 1373 $self->{layout}->set_width ($self->{max_w} || -1);
1374 $self->{layout}->set_ellipsise ($self->{ellipsise});
1375 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1286 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1376 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1287 1377
1288 my ($w, $h) = $self->{layout}->size; 1378 my ($w, $h) = $self->{layout}->size;
1289 1379
1290 if (exists $self->{template}) { 1380 if (exists $self->{template}) {
1312sub set_fontsize { 1402sub set_fontsize {
1313 my ($self, $fontsize) = @_; 1403 my ($self, $fontsize) = @_;
1314 1404
1315 $self->{fontsize} = $fontsize; 1405 $self->{fontsize} = $fontsize;
1316 delete $self->{texture}; 1406 delete $self->{texture};
1407
1408 $self->update;
1317 $self->check_size; 1409 $self->check_size;
1318 $self->update;
1319} 1410}
1320 1411
1321sub _draw { 1412sub _draw {
1322 my ($self) = @_; 1413 my ($self) = @_;
1323 1414
1415 $self->SUPER::_draw; # draw background, if applicable
1416
1324 my $tex = $self->{texture} ||= do { 1417 my $tex = $self->{texture} ||= do {
1418 $self->{layout}->set_foreground (@{$self->{fg}});
1325 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1419 $self->{layout}->set_font ($self->{font}) if $self->{font};
1326 $self->{layout}->set_width ($self->{w}); 1420 $self->{layout}->set_width ($self->{w});
1421 $self->{layout}->set_ellipsise ($self->{ellipsise});
1422 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1327 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1423 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1424
1328 new_from_layout CFClient::Texture $self->{layout} 1425 my $tex = new_from_layout CFClient::Texture $self->{layout};
1426
1427 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1428 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1429 : ($self->{w} - $tex->{w}) * 0.5);
1430
1431 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1432 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1433 : ($self->{h} - $tex->{h}) * 0.5);
1434
1435 $tex
1329 }; 1436 };
1330 1437
1331 glEnable GL_BLEND;
1332 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1333 glEnable GL_TEXTURE_2D; 1438 glEnable GL_TEXTURE_2D;
1334 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1439 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1335 1440
1441 if ($tex->{format} == GL_ALPHA) {
1336 glColor @{$self->{fg}}; 1442 glColor @{$self->{fg}};
1337
1338 $self->{ox} = int (
1339 $self->{align} < 0 ? $self->{padding}
1340 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1341 : ($self->{w} - $tex->{w}) * 0.5
1342 );
1343
1344 $self->{oy} = int (
1345 $self->{valign} < 0 ? $self->{padding}
1346 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1347 : ($self->{h} - $tex->{h}) * 0.5
1348 );
1349
1350 $tex->draw_quad ($self->{ox}, $self->{oy}); 1443 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1444 } else {
1445 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1446 }
1351 1447
1352 glDisable GL_TEXTURE_2D; 1448 glDisable GL_TEXTURE_2D;
1353 glDisable GL_BLEND;
1354} 1449}
1355 1450
1356############################################################################# 1451#############################################################################
1357 1452
1358package CFClient::UI::EntryBase; 1453package CFClient::UI::EntryBase;
1393 $self->{layout}->set_text ("$text "); 1488 $self->{layout}->set_text ("$text ");
1394 1489
1395 $self->emit (changed => $self->{text}); 1490 $self->emit (changed => $self->{text});
1396} 1491}
1397 1492
1398sub get_text {
1399 $_[0]{text}
1400}
1401
1402sub size_request {
1403 my ($self) = @_;
1404
1405 my ($w, $h) = $self->SUPER::size_request;
1406
1407 ($w + 1, $h) # add 1 for cursor
1408}
1409
1410sub size_allocate {
1411 my ($self, $w, $h) = @_;
1412
1413 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1414}
1415
1416sub set_text { 1493sub set_text {
1417 my ($self, $text) = @_; 1494 my ($self, $text) = @_;
1418 1495
1419 $self->{cursor} = length $text; 1496 $self->{cursor} = length $text;
1420 $self->_set_text ($text); 1497 $self->_set_text ($text);
1421 $self->update; 1498 $self->update;
1499 $self->check_size;
1500}
1501
1502sub get_text {
1503 $_[0]{text}
1504}
1505
1506sub size_request {
1507 my ($self) = @_;
1508
1509 my ($w, $h) = $self->SUPER::size_request;
1510
1511 ($w + 1, $h) # add 1 for cursor
1512}
1513
1514sub size_allocate {
1515 my ($self, $w, $h) = @_;
1516
1517 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1422} 1518}
1423 1519
1424sub key_down { 1520sub key_down {
1425 my ($self, $ev) = @_; 1521 my ($self, $ev) = @_;
1426 1522
1428 my $sym = $ev->{sym}; 1524 my $sym = $ev->{sym};
1429 my $uni = $ev->{unicode}; 1525 my $uni = $ev->{unicode};
1430 1526
1431 my $text = $self->get_text; 1527 my $text = $self->get_text;
1432 1528
1433 if ($sym == 8) { 1529 if ($uni == 8) {
1434 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1530 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1435 } elsif ($sym == 127) { 1531 } elsif ($uni == 127) {
1436 substr $text, $self->{cursor}, 1, ""; 1532 substr $text, $self->{cursor}, 1, "";
1437 } elsif ($sym == CFClient::SDLK_LEFT) { 1533 } elsif ($sym == CFClient::SDLK_LEFT) {
1438 --$self->{cursor} if $self->{cursor}; 1534 --$self->{cursor} if $self->{cursor};
1439 } elsif ($sym == CFClient::SDLK_RIGHT) { 1535 } elsif ($sym == CFClient::SDLK_RIGHT) {
1440 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1536 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1441 } elsif ($sym == CFClient::SDLK_HOME) { 1537 } elsif ($sym == CFClient::SDLK_HOME) {
1442 $self->{cursor} = 0; 1538 $self->{cursor} = 0;
1443 } elsif ($sym == CFClient::SDLK_END) { 1539 } elsif ($sym == CFClient::SDLK_END) {
1444 $self->{cursor} = length $text; 1540 $self->{cursor} = length $text;
1445 } elsif ($sym == 27) { 1541 } elsif ($uni == 27) {
1446 $self->emit ('escape'); 1542 $self->emit ('escape');
1447 } elsif ($uni) { 1543 } elsif ($uni) {
1448 substr $text, $self->{cursor}++, 0, chr $uni; 1544 substr $text, $self->{cursor}++, 0, chr $uni;
1449 } 1545 }
1450 1546
1451 $self->_set_text ($text); 1547 $self->_set_text ($text);
1452 $self->update; 1548 $self->update;
1549 $self->check_size;
1453} 1550}
1454 1551
1455sub focus_in { 1552sub focus_in {
1456 my ($self) = @_; 1553 my ($self) = @_;
1457 1554
1586 my $class = shift; 1683 my $class = shift;
1587 1684
1588 $class->SUPER::new ( 1685 $class->SUPER::new (
1589 padding => 4, 1686 padding => 4,
1590 fg => [1, 1, 1], 1687 fg => [1, 1, 1],
1591 bg => [1, 1, 1, 0.2],
1592 active_fg => [0, 0, 1], 1688 active_fg => [0, 0, 1],
1593 can_hover => 1, 1689 can_hover => 1,
1594 align => 0, 1690 align => 0,
1595 valign => 0, 1691 valign => 0,
1596 can_events => 1, 1692 can_events => 1,
1614 1710
1615 if ($GRAB == $self) { 1711 if ($GRAB == $self) {
1616 $self->{fg} = $self->{active_fg}; 1712 $self->{fg} = $self->{active_fg};
1617 } 1713 }
1618 1714
1619 glEnable GL_BLEND;
1620 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1621 glEnable GL_TEXTURE_2D; 1715 glEnable GL_TEXTURE_2D;
1622 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1623 glColor 0, 0, 0, 1; 1717 glColor 0, 0, 0, 1;
1624 1718
1625 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1719 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1626 1720
1627 glDisable GL_TEXTURE_2D; 1721 glDisable GL_TEXTURE_2D;
1628 glDisable GL_BLEND;
1629 1722
1630 $self->SUPER::_draw; 1723 $self->SUPER::_draw;
1631} 1724}
1632 1725
1633############################################################################# 1726#############################################################################
1647 1740
1648 $class->SUPER::new ( 1741 $class->SUPER::new (
1649 padding => 2, 1742 padding => 2,
1650 fg => [1, 1, 1], 1743 fg => [1, 1, 1],
1651 active_fg => [1, 1, 0], 1744 active_fg => [1, 1, 0],
1745 bg => [0, 0, 0, 0.2],
1746 active_bg => [1, 1, 1, 0.5],
1652 state => 0, 1747 state => 0,
1653 can_hover => 1, 1748 can_hover => 1,
1654 @_ 1749 @_
1655 ) 1750 )
1656} 1751}
1680 1775
1681 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1776 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1682 1777
1683 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1778 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1684 1779
1685 glEnable GL_BLEND; 1780 my $tex = $self->{state} ? $tex[1] : $tex[0];
1781
1686 glEnable GL_TEXTURE_2D; 1782 glEnable GL_TEXTURE_2D;
1687 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1688
1689 my $tex = $self->{state} ? $tex[1] : $tex[0];
1690
1691 $tex->draw_quad (0, 0, $s, $s); 1783 $tex->draw_quad_alpha (0, 0, $s, $s);
1692
1693 glDisable GL_TEXTURE_2D; 1784 glDisable GL_TEXTURE_2D;
1694 glDisable GL_BLEND;
1695} 1785}
1696 1786
1697############################################################################# 1787#############################################################################
1698 1788
1699package CFClient::UI::Image; 1789package CFClient::UI::Image;
1742 glTranslate 0, -$self->{w}, 0; 1832 glTranslate 0, -$self->{w}, 0;
1743 1833
1744 ($w, $h) = ($h, $w); 1834 ($w, $h) = ($h, $w);
1745 } 1835 }
1746 1836
1747 glEnable GL_BLEND;
1748 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1749 glEnable GL_TEXTURE_2D; 1837 glEnable GL_TEXTURE_2D;
1750 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1838 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1751 1839
1752 $tex->draw_quad (0, 0, $w, $h); 1840 $tex->draw_quad_alpha (0, 0, $w, $h);
1753 1841
1754 glDisable GL_BLEND;
1755 glDisable GL_TEXTURE_2D; 1842 glDisable GL_TEXTURE_2D;
1756} 1843}
1757 1844
1758############################################################################# 1845#############################################################################
1759 1846
1948 qw(s1_slider.png s1_slider_bg.png); 2035 qw(s1_slider.png s1_slider_bg.png);
1949 2036
1950sub new { 2037sub new {
1951 my $class = shift; 2038 my $class = shift;
1952 2039
1953 # range [value, low, high, page] 2040 # range [value, low, high, page, unit]
1954 2041
1955 # TODO: 0-width page 2042 # TODO: 0-width page
1956 # TODO: req_w/h are wrong with vertical 2043 # TODO: req_w/h are wrong with vertical
1957 # TODO: calculations are off 2044 # TODO: calculations are off
1958 my $self = $class->SUPER::new ( 2045 my $self = $class->SUPER::new (
1959 fg => [1, 1, 1], 2046 fg => [1, 1, 1],
1960 active_fg => [0, 0, 0], 2047 active_fg => [0, 0, 0],
2048 bg => [0, 0, 0, 0.2],
2049 active_bg => [1, 1, 1, 0.5],
1961 range => [0, 0, 100, 10], 2050 range => [0, 0, 100, 10],
1962 req_w => 20, 2051 req_w => $::WIDTH / 80,
1963 req_h => 20, 2052 req_h => $::WIDTH / 80,
1964 vertical => 0, 2053 vertical => 0,
1965 can_hover => 1, 2054 can_hover => 1,
1966 inner_pad => 5, 2055 inner_pad => 0.02,
1967 @_ 2056 @_
1968 ); 2057 );
1969 2058
2059 $self->set_value ($self->{range}[0]);
2060 $self->update;
2061
1970 $self 2062 $self
2063}
2064
2065sub set_value {
2066 my ($self, $value) = @_;
2067
2068 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2069
2070 $hi = $lo + 1 if $hi <= $lo;
2071
2072 $value = $lo if $value < $lo;
2073 $value = $hi if $value > $hi;
2074
2075 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2076 if $unit;
2077
2078 $page = $hi - $lo if $page > $hi - $lo;
2079
2080 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2081
2082 if ($value != $old_value) {
2083 $self->emit (changed => $value);
2084 $self->update;
2085 }
1971} 2086}
1972 2087
1973sub size_request { 2088sub size_request {
1974 my ($self) = @_; 2089 my ($self) = @_;
1975 2090
1988 2103
1989sub mouse_motion { 2104sub mouse_motion {
1990 my ($self, $ev, $x, $y) = @_; 2105 my ($self, $ev, $x, $y) = @_;
1991 2106
1992 if ($GRAB == $self) { 2107 if ($GRAB == $self) {
2108 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2109
2110 my (undef, $lo, $hi, $page) = @{$self->{range}};
2111
2112 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2113
2114 $self->set_value ($x * ($hi - $lo) + $lo);
2115 }
2116}
2117
2118sub update {
2119 my ($self) = @_;
2120
2121 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2122 $self->set_value ($self->{range}[0]);
2123
1993 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2124 my ($value, $lo, $hi, $page) = @{$self->{range}};
1994 2125
1995 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2126 my $inner_w = 1 - 2 * $self->{inner_pad};
1996 2127
1997 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2128 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
1998 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1999 2129
2000 $x -= $inner_pad_px; # substract the padding 2130 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2001 $x = $x * ($hi - $lo) / $inner_w + $lo; 2131 $value = $self->{scale} * ($value - $lo);
2002 $x = $lo if $x < $lo;
2003 $x = $hi - $page if $x > $hi - $page;
2004 $self->{range}[0] = $x;
2005 2132
2006 $self->emit (changed => $x); 2133 $value = $self->{inner_pad} + ($value - $page * 0.5);
2007 $self->update; 2134
2135 $value = 0 if $value < 0;
2136 $page = 1 - $value if $value + $page > 1;
2137
2138 $self->{knob_x} = $value;
2139 $self->{knob_w} = $page;
2008 } 2140 });
2009}
2010 2141
2011# the inner_* stuff is for generating a padding for the slider handle, 2142 $self->SUPER::update;
2012# so that the handle doesn't leave the texture. This calculation isn't 100%
2013# correct propably, but it does the job for now
2014sub _calc_inner_pad_px {
2015 my ($self, $w) = @_;
2016 ($w / 100) * $self->{inner_pad} # % to pixels
2017} 2143}
2018 2144
2019sub _draw { 2145sub _draw {
2020 my ($self) = @_; 2146 my ($self) = @_;
2021 2147
2022 $self->SUPER::_draw (); 2148 $self->SUPER::_draw ();
2023 2149
2024 my ($w, $h) = @$self{qw(w h)}; 2150 glScale $self->{w}, $self->{h};
2025 2151
2026 if ($self->{vertical}) { 2152 if ($self->{vertical}) {
2027 # draw a vertical slider like a rotated horizontal slider 2153 # draw a vertical slider like a rotated horizontal slider
2028 2154
2155 glTranslate 1, 0, 0;
2029 glRotate 90, 0, 0, 1; 2156 glRotate 90, 0, 0, 1;
2030 glTranslate 0, -$self->{w}, 0;
2031
2032 ($w, $h) = ($h, $w);
2033 } 2157 }
2034 2158
2035 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2159 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2036 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2160 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2037 2161
2038 my ($value, $lo, $hi, $page) = @{$self->{range}};
2039
2040 $hi = $value + 1 if $lo == $hi;
2041
2042 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2043 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2044
2045 $page = int $page * $inner_w / ($hi - $lo);
2046 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2047
2048 $w -= $page;
2049 $page &= ~1;
2050 glTranslate $page * 0.5, 0, 0;
2051 $page ||= 2;
2052
2053 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2054 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2055
2056 glEnable GL_BLEND;
2057 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2058 glEnable GL_TEXTURE_2D; 2162 glEnable GL_TEXTURE_2D;
2059 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2163 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2060 2164
2061 # draw background 2165 # draw background
2062 $tex[1]->draw_quad (0, 0, $w, $h); 2166 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2063 2167
2064 # draw handle 2168 # draw handle
2065 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2169 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2066 2170
2067 glDisable GL_BLEND;
2068 glDisable GL_TEXTURE_2D; 2171 glDisable GL_TEXTURE_2D;
2069} 2172}
2070 2173
2071############################################################################# 2174#############################################################################
2072 2175
2083 fontsize => 1, 2186 fontsize => 1,
2084 can_events => 0, 2187 can_events => 0,
2085 #font => default_font 2188 #font => default_font
2086 @_, 2189 @_,
2087 2190
2088 layout => (new CFClient::Layout), 2191 layout => (new CFClient::Layout 1),
2089 par => [], 2192 par => [],
2090 height => 0, 2193 height => 0,
2091 children => [ 2194 children => [
2092 (new CFClient::UI::Empty expand => 1), 2195 (new CFClient::UI::Empty expand => 1),
2093 (new CFClient::UI::Slider vertical => 1), 2196 (new CFClient::UI::Slider vertical => 1),
2111 2214
2112 my $layout = $self->{layout}; 2215 my $layout = $self->{layout};
2113 2216
2114 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2217 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2115 $layout->set_width ($self->{children}[0]{w}); 2218 $layout->set_width ($self->{children}[0]{w});
2116 $layout->set_text ($text); 2219 $layout->set_markup ($text);
2117 2220
2118 ($layout->size)[1] 2221 ($layout->size)[1]
2119} 2222}
2120 2223
2121sub reflow { 2224sub reflow {
2159 2262
2160 return unless $self->{h} > 0; 2263 return unless $self->{h} > 0;
2161 2264
2162 delete $self->{texture}; 2265 delete $self->{texture};
2163 2266
2164 $ROOT->on_refresh ($self, sub { 2267 $ROOT->on_post_alloc ($self, sub {
2165 if (delete $self->{need_reflow}) { 2268 if (delete $self->{need_reflow}) {
2166 my $height = 0; 2269 my $height = 0;
2167 2270
2168 $height += $_->[0] = $self->text_height ($_->[2]) 2271 $height += $_->[0] = $self->text_height ($_->[2])
2169 for @{$self->{par}}; 2272 for @{$self->{par}};
2174 2277
2175 delete $self->{texture}; 2278 delete $self->{texture};
2176 } 2279 }
2177 2280
2178 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2281 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2179 glClearColor 0, 0, 0, 0; 2282 glClearColor 0.5, 0.5, 0.5, 0;
2180 glClear GL_COLOR_BUFFER_BIT; 2283 glClear GL_COLOR_BUFFER_BIT;
2181 2284
2182 glEnable GL_BLEND;
2183 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2184 glEnable GL_TEXTURE_2D; 2285 glEnable GL_TEXTURE_2D;
2185 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2286 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2186 2287
2187 my $top = int $self->{children}[1]{range}[0]; 2288 my $top = int $self->{children}[1]{range}[0];
2188 2289
2197 2298
2198 for my $par (@{$self->{par}}) { 2299 for my $par (@{$self->{par}}) {
2199 my $h = $par->[0]; 2300 my $h = $par->[0];
2200 2301
2201 if ($y0 < $y + $h && $y < $y1) { 2302 if ($y0 < $y + $h && $y < $y1) {
2303 $layout->set_foreground (@{ $par->[1] });
2202 $layout->set_text ($par->[2]); 2304 $layout->set_markup ($par->[2]);
2203 2305
2204 glColor @{ $par->[1] };
2205 my ($W, $H) = $layout->size; 2306 my ($W, $H) = $layout->size;
2206 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2307 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2207 } 2308 }
2208 2309
2209 $y += $h; 2310 $y += $h;
2210 } 2311 }
2211 2312
2212 glDisable GL_TEXTURE_2D; 2313 glDisable GL_TEXTURE_2D;
2213 glDisable GL_BLEND;
2214 }; 2314 };
2215 }); 2315 });
2216} 2316}
2217 2317
2218sub _draw { 2318sub _draw {
2219 my ($self) = @_; 2319 my ($self) = @_;
2220 2320
2221 glEnable GL_BLEND;
2222 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2223 glEnable GL_TEXTURE_2D; 2321 glEnable GL_TEXTURE_2D;
2224 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2322 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2225 glColor 1, 1, 1, 1; 2323 glColor 1, 1, 1, 1;
2226 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2324 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2227 glDisable GL_TEXTURE_2D; 2325 glDisable GL_TEXTURE_2D;
2228 glDisable GL_BLEND;
2229 2326
2230 $self->{children}[1]->draw; 2327 $self->{children}[1]->draw;
2231 2328
2232} 2329}
2233 2330
2327 @_, 2424 @_,
2328 can_events => 0, 2425 can_events => 0,
2329 ) 2426 )
2330} 2427}
2331 2428
2332sub set_markup { 2429sub set_tooltip_from {
2333 my ($self, $text) = @_; 2430 my ($self, $widget) = @_;
2334 2431
2335 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2432 $self->add (new CFClient::UI::Label
2336 $self->{label}->set_markup ($text); 2433 markup => $widget->{tooltip},
2337 $self->add ($self->{label}); 2434 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2435 fontsize => 0.8,
2436 fg => [0, 0, 0, 1],
2437 ellipsise => 0,
2438 font => ($widget->{tooltip_font} || $::FONT_PROP),
2439 );
2338} 2440}
2339 2441
2340sub size_request { 2442sub size_request {
2341 my ($self) = @_; 2443 my ($self) = @_;
2342
2343 $self->child->set_max_size ($::WIDTH * 0.3);
2344 2444
2345 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2445 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2346 2446
2347 ($w + 4, $h + 4) 2447 ($w + 4, $h + 4)
2348} 2448}
2354} 2454}
2355 2455
2356sub _draw { 2456sub _draw {
2357 my ($self) = @_; 2457 my ($self) = @_;
2358 2458
2359 glPushMatrix;
2360 glTranslate 0.375, 0.375; 2459 glTranslate 0.375, 0.375;
2361 2460
2362 my ($w, $h) = @$self{qw(w h)}; 2461 my ($w, $h) = @$self{qw(w h)};
2363 2462
2364 glColor 1, 0.8, 0.4; 2463 glColor 1, 0.8, 0.4;
2375 glVertex 0 , $h; 2474 glVertex 0 , $h;
2376 glVertex $w, $h; 2475 glVertex $w, $h;
2377 glVertex $w, 0; 2476 glVertex $w, 0;
2378 glEnd; 2477 glEnd;
2379 2478
2380 glPopMatrix; 2479 glTranslate 2 - 0.375, 2 - 0.375;
2381
2382 glTranslate 2, 2;
2383 $self->SUPER::_draw; 2480 $self->SUPER::_draw;
2384} 2481}
2385 2482
2386############################################################################# 2483#############################################################################
2387 2484
2392use CFClient::OpenGL; 2489use CFClient::OpenGL;
2393 2490
2394sub new { 2491sub new {
2395 my $class = shift; 2492 my $class = shift;
2396 2493
2397 $class->SUPER::new ( 2494 my $self = $class->SUPER::new (
2398 aspect => 1, 2495 aspect => 1,
2399 @_, 2496 @_,
2400 ) 2497 );
2498
2499 if ($self->{anim} && $self->{animspeed}) {
2500 Scalar::Util::weaken (my $widget = $self);
2501
2502 $self->{timer} = Event->timer (
2503 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2504 hard => 1,
2505 interval => $self->{animspeed},
2506 cb => sub {
2507 ++$widget->{frame};
2508 $widget->update;
2509 },
2510 );
2511 }
2512
2513 $self
2401} 2514}
2402 2515
2403sub size_request { 2516sub size_request {
2404 (32, 8) 2517 (32, 8)
2405} 2518}
2406 2519
2407sub _draw { 2520sub _draw {
2408 my ($self) = @_; 2521 my ($self) = @_;
2409 2522
2523 return unless $::CONN;#d# manage and cache textures differently
2524
2525 my $face;
2526
2527 if ($self->{frame}) {
2528 my $anim = $::CONN->{anim}[$self->{anim}];
2529
2530 $face = $anim->[ $self->{frame} % @$anim ]
2531 if $anim && @$anim;
2532 }
2533
2410 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2534 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2411 2535
2412 # TODO animation
2413 if ($tex) { 2536 if ($tex) {
2414 glEnable GL_BLEND;
2415 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2416 glEnable GL_TEXTURE_2D; 2537 glEnable GL_TEXTURE_2D;
2417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2538 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2418 glColor 1, 1, 1, 1; 2539 glColor 1, 1, 1, 1;
2419 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2540 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2420 glDisable GL_TEXTURE_2D; 2541 glDisable GL_TEXTURE_2D;
2421 glDisable GL_BLEND;
2422 } 2542 }
2543}
2544
2545sub DESTROY {
2546 my ($self) = @_;
2547
2548 $self->{timer}->cancel
2549 if $self->{timer};
2550
2551 $self->SUPER::DESTROY;
2552}
2553
2554#############################################################################
2555
2556package CFClient::UI::InventoryItem;
2557
2558our @ISA = CFClient::UI::HBox::;
2559
2560sub _item_to_desc {
2561 my ($item) = @_;
2562
2563 my $desc =
2564 $item->{nrof} < 2
2565 ? $item->{name}
2566 : "$item->{nrof} × $item->{name_pl}";
2567
2568 $item->{flags} & Crossfire::Protocol::F_OPEN
2569 and $desc .= " (open)";
2570 $item->{flags} & Crossfire::Protocol::F_APPLIED
2571 and $desc .= " (applied)";
2572 $item->{flags} & Crossfire::Protocol::F_UNPAID
2573 and $desc .= " (unpaid)";
2574 $item->{flags} & Crossfire::Protocol::F_MAGIC
2575 and $desc .= " (magic)";
2576 $item->{flags} & Crossfire::Protocol::F_CURSED
2577 and $desc .= " (cursed)";
2578 $item->{flags} & Crossfire::Protocol::F_DAMNED
2579 and $desc .= " (damned)";
2580 $item->{flags} & Crossfire::Protocol::F_LOCKED
2581 and $desc .= " *";
2582
2583 $desc
2584}
2585
2586sub new {
2587 my $class = shift;
2588
2589 my %args = @_;
2590
2591 my $item = delete $args{item};
2592
2593 my $desc = _item_to_desc ($item);
2594
2595 my $self = $class->SUPER::new (
2596 can_hover => 1,
2597 can_events => 1,
2598 tooltip => ((CFClient::UI::Label::escape $desc)
2599 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2600 connect_button_down => sub {
2601 my ($self, $ev, $x, $y) = @_;
2602
2603 # todo: maybe put examine on 1? but should just be a tooltip :(
2604 if ($ev->{button} == 1) {
2605 my $targ = $::CONN->{player}{tag};
2606
2607 if ($item->{container} == $::CONN->{player}{tag}) {
2608 $targ = $main::OPENCONT;
2609 }
2610
2611 $::CONN->send ("move $targ $item->{tag} 0");
2612 } elsif ($ev->{button} == 2) {
2613 $::CONN->send ("apply $item->{tag}");
2614 } elsif ($ev->{button} == 3) {
2615 my @menu_items = (
2616 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2617 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2618 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2619 (
2620 $item->{flags} & Crossfire::Protocol::F_LOCKED
2621 ? (
2622 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2623 )
2624 : (
2625 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2626 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2627 )
2628 ),
2629 );
2630
2631 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2632 }
2633
2634 1
2635 },
2636 %args
2637 );
2638
2639
2640 $self->add (new CFClient::UI::Face
2641 can_events => 0,
2642 face => $item->{face},
2643 anim => $item->{anim},
2644 animspeed => $item->{animspeed},
2645 );
2646
2647 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2648
2649 $self->{item} = $item;
2650
2651 $self->update_item;
2652
2653 $self
2654}
2655
2656sub update_item {
2657 my ($self) = @_;
2658
2659 my $desc = _item_to_desc ($self->{item});
2660
2661 $self->{name_lbl}->set_text ($desc);
2662}
2663
2664#############################################################################
2665
2666package CFClient::UI::Inventory;
2667
2668our @ISA = CFClient::UI::ScrolledWindow::;
2669
2670sub new {
2671 my $class = shift;
2672
2673 my $self = $class->SUPER::new (
2674 scrolled => (new CFClient::UI::VBox),
2675 @_,
2676 );
2677
2678 $self
2679}
2680
2681sub set_items {
2682 my ($self, $items) = @_;
2683
2684 $self->{scrolled}->clear;
2685 return unless $items;
2686
2687 my @items = sort {
2688 ($a->{type} <=> $b->{type})
2689 or ($a->{name} cmp $b->{name})
2690 } @$items;
2691
2692 $self->{real_items} = \@items;
2693
2694 for my $item (@items) {
2695 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2696 $item->update_item ();
2697 }
2698
2699 $self->{scrolled}->add (@items);
2700
2701# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2702}
2703
2704sub size_request {
2705 my ($self) = @_;
2706 ($self->{req_w}, $self->{req_h});
2423} 2707}
2424 2708
2425############################################################################# 2709#############################################################################
2426 2710
2427package CFClient::UI::Menu; 2711package CFClient::UI::Menu;
2475} 2759}
2476 2760
2477sub mouse_motion { 2761sub mouse_motion {
2478 my ($self, $ev, $x, $y) = @_; 2762 my ($self, $ev, $x, $y) = @_;
2479 2763
2480 # TODO: should use vbox->fdind_widget or so 2764 # TODO: should use vbox->find_widget or so
2481 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 2765 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2482 $self->{hover} = $self->{item}{$HOVER}; 2766 $self->{hover} = $self->{item}{$HOVER};
2483} 2767}
2484 2768
2485sub button_up { 2769sub button_up {
2494 } 2778 }
2495} 2779}
2496 2780
2497############################################################################# 2781#############################################################################
2498 2782
2783package CFClient::UI::Statusbox;
2784
2785our @ISA = CFClient::UI::VBox::;
2786
2787sub new {
2788 my $class = shift;
2789
2790 $class->SUPER::new (
2791 fontsize => 0.8,
2792 @_,
2793 )
2794}
2795
2796sub reorder {
2797 my ($self) = @_;
2798 my $NOW = time;
2799
2800 while (my ($k, $v) = each %{ $self->{item} }) {
2801 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2802 }
2803
2804 my @widgets;
2805
2806 my @items = sort {
2807 $a->{pri} <=> $b->{pri}
2808 or $b->{id} <=> $a->{id}
2809 } values %{ $self->{item} };
2810
2811 my $count = 10 + 1;
2812 for my $item (@items) {
2813 last unless --$count;
2814
2815 push @widgets, $item->{label} ||= do {
2816 # TODO: doesn't handle markup well (read as: at all)
2817 my $short = $item->{count} > 1
2818 ? "<b>$item->{count} ×</b> $item->{text}"
2819 : $item->{text};
2820
2821 for ($short) {
2822 s/^\s+//;
2823 s/\s+/ /g;
2824 }
2825
2826 new CFClient::UI::Label
2827 markup => $short,
2828 tooltip => $item->{tooltip},
2829 tooltip_font => $::FONT_PROP,
2830 tooltip_width => 0.67,
2831 fontsize => $item->{fontsize} || $self->{fontsize},
2832 max_w => $::WIDTH * 0.44,
2833 fg => $item->{fg},
2834 can_events => 1,
2835 can_hover => 1
2836 };
2837 }
2838
2839 $self->clear;
2840 $self->SUPER::add (reverse @widgets);
2841}
2842
2843sub add {
2844 my ($self, $text, %arg) = @_;
2845
2846 $text =~ s/^\s+//;
2847 $text =~ s/\s+$//;
2848
2849 my $timeout = time + ((delete $arg{timeout}) || 60);
2850
2851 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2852
2853 if (my $item = $self->{item}{$group}) {
2854 if ($item->{text} eq $text) {
2855 $item->{count}++;
2856 } else {
2857 $item->{count} = 1;
2858 $item->{text} = $item->{tooltip} = $text;
2859 }
2860 $item->{id} = ++$self->{id};
2861 $item->{timeout} = $timeout;
2862 delete $item->{label};
2863 } else {
2864 $self->{item}{$group} = {
2865 id => ++$self->{id},
2866 text => $text,
2867 timeout => $timeout,
2868 tooltip => $text,
2869 fg => [0.8, 0.8, 0.8, 0.8],
2870 pri => 0,
2871 count => 1,
2872 %arg,
2873 };
2874 }
2875
2876 $self->reorder;
2877}
2878
2879sub reconfigure {
2880 my ($self) = @_;
2881
2882 delete $_->{label}
2883 for values %{ $self->{item} || {} };
2884
2885 $self->reorder;
2886 $self->SUPER::reconfigure;
2887}
2888
2889#############################################################################
2890
2499package CFClient::UI::Root; 2891package CFClient::UI::Root;
2500 2892
2501our @ISA = CFClient::UI::Container::; 2893our @ISA = CFClient::UI::Container::;
2502 2894
2503use CFClient::OpenGL; 2895use CFClient::OpenGL;
2504 2896
2505sub check_size { 2897sub new {
2506 my ($self) = @_; 2898 my $class = shift;
2507 2899
2508 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2900 $class->SUPER::new (
2509} 2901 @_,
2510 2902 )
2511sub size_request {
2512 ($::WIDTH, $::HEIGHT)
2513} 2903}
2514 2904
2515sub configure { 2905sub configure {
2516 my ($self, $x, $y, $w, $h) = @_; 2906 my ($self, $x, $y, $w, $h) = @_;
2517 2907
2518 $self->SUPER::configure ($x, $y, $w, $h); 2908 $self->{w} = $w;
2909 $self->{h} = $h;
2910}
2519 2911
2912sub check_size {
2913 my ($self) = @_;
2914
2915 $self->size_allocate ($self->{w}, $self->{h})
2916 if $self->{w};
2917}
2918
2919sub size_request {
2920 my ($self) = @_;
2921
2922 ($self->{w}, $self->{h})
2923}
2924
2925sub size_allocate {
2926 my ($self, $w, $h) = @_;
2927
2928 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2929 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2930
2931 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2932 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2933
2520 for my $child (@{$self->{children}}) { 2934 for my $child ($self->children) {
2521 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2935 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2522 2936
2937 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2938 if exists $child->{req_x};
2939
2940 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2941 if exists $child->{req_y};
2942
2523 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2943 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2524 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2944 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2945
2525 $child->configure ($X, $Y, $W,$H); 2946 $child->configure ($X, $Y, $W, $H);
2526 } 2947 }
2527} 2948}
2528 2949
2529sub _topleft { 2950sub coord2local {
2530 my ($self, $x, $y) = @_; 2951 my ($self, $x, $y) = @_;
2531 2952
2532 ($x, $y) 2953 ($x, $y)
2533} 2954}
2534 2955
2956sub coord2global {
2957 my ($self, $x, $y) = @_;
2958
2959 ($x, $y)
2960}
2961
2535sub update { 2962sub update {
2536 my ($self) = @_; 2963 my ($self) = @_;
2537 2964
2538 $self->check_size; 2965 $self->check_size;
2539 ::refresh (); 2966 $::WANT_REFRESH++;
2540} 2967}
2541 2968
2542sub add { 2969sub add {
2543 my ($self, $child) = @_; 2970 my ($self, @children) = @_;
2544 2971
2972 for my $child (@children) {
2973 $child->{toplevel} = 1;
2974
2545 # integerize window positions 2975 # integerise window positions
2546 $child->{x} = int $child->{x}; 2976 $child->{x} = int $child->{x};
2547 $child->{y} = int $child->{y}; 2977 $child->{y} = int $child->{y};
2978 }
2548 2979
2549 $self->SUPER::add ($child); 2980 $self->SUPER::add (@children);
2550} 2981}
2551 2982
2552sub on_refresh { 2983sub on_refresh {
2553 my ($self, $id, $cb) = @_; 2984 my ($self, $id, $cb) = @_;
2554 2985
2555 $self->{refresh_hook}{$id} = $cb; 2986 $self->{refresh_hook}{$id} = $cb;
2556} 2987}
2557 2988
2989sub on_post_alloc {
2990 my ($self, $id, $cb) = @_;
2991
2992 $self->{post_alloc_hook}{$id} = $cb;
2993}
2994
2558sub draw { 2995sub draw {
2559 my ($self) = @_; 2996 my ($self) = @_;
2560 2997
2561 while (my $rcb = delete $self->{refresh_hook}) { 2998 while ($self->{refresh_hook}) {
2562 $_->() for values %$rcb; 2999 $_->()
3000 for values %{delete $self->{refresh_hook}};
3001 }
3002
3003 if ($self->{check_size}) {
3004 my @queue = ([], []);
3005
3006 for (;;) {
3007 if ($self->{check_size}) {
3008 # heuristic: check containers last
3009 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
3010 for values %{delete $self->{check_size}}
3011 }
3012
3013 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
3014
3015 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
3016 ? @$widget{qw(user_w user_h)}
3017 : $widget->size_request;
3018
3019 if (delete $widget->{force_alloc}
3020 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3021 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3022
3023 $widget->{req_w} = $w;
3024 $widget->{req_h} = $h;
3025
3026 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3027
3028 $widget->{parent}->check_size
3029 if $widget->{parent};
3030 }
3031 }
3032 }
3033
3034 while ($self->{size_alloc}) {
3035 for (values %{delete $self->{size_alloc}}) {
3036 my ($widget, $w, $h) = @$_;
3037
3038 $w = 0 if $w < 0;
3039 $h = 0 if $h < 0;
3040
3041 $widget->{w} = $w;
3042 $widget->{h} = $h;
3043 $widget->size_allocate ($w, $h);
3044 $widget->emit (size_allocate => $w, $h);
3045 }
3046 }
3047
3048 while ($self->{post_alloc_hook}) {
3049 $_->()
3050 for values %{delete $self->{post_alloc_hook}};
2563 } 3051 }
2564 3052
2565 glViewport 0, 0, $::WIDTH, $::HEIGHT; 3053 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2566 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3054 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2567 glClear GL_COLOR_BUFFER_BIT; 3055 glClear GL_COLOR_BUFFER_BIT;
2575 $self->_draw; 3063 $self->_draw;
2576} 3064}
2577 3065
2578############################################################################# 3066#############################################################################
2579 3067
2580package CFClient::UI::Inventory;
2581
2582our @ISA = CFClient::UI::Container::;
2583
2584use CFClient::OpenGL;
2585
2586sub new {
2587 my $class = shift;
2588
2589 my $self = $class->SUPER::new (@_);
2590
2591 $self
2592}
2593
2594sub size_allocate {
2595 my ($self, $w, $h) = @_;
2596
2597 $self->{w} = $w;
2598 $self->{h} = $h;
2599
2600 $self->check_size;
2601}
2602
2603sub set_items {
2604 my ($self, $items) = @_;
2605 my @items = values %{$items};
2606 @items = sort { $a->{type} <=> $b->{type} } @items;
2607
2608 $self->{real_items} = \@items;
2609
2610 for my $item (@items) {
2611 my $desc = $item->{nrof} < 2
2612 ? $item->{name}
2613 : "$item->{nrof} $item->{name_pl}";
2614
2615 $self->add (my $hb = new CFClient::UI::HBox);
2616
2617 $hb->add (my $f = new CFClient::UI::Face
2618 can_events => 0,
2619 face => $item->{face},
2620 anim => $item->{anim},
2621 animspeed => $item->{animspeed},
2622 expand => 1
2623 );
2624 $hb->add (new CFClient::UI::Label text => $desc, expand => 1);
2625 }
2626
2627 $self->{max_pos} = (scalar @items) - 1;
2628
2629 my $range = $self->{range};
2630 my $page = $self->{h} / 32; # waht information souce to use for face size?
2631 # it should be configurable i guess...
2632 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2633 $range->update;
2634}
2635
2636sub size_request {
2637 my ($self) = @_;
2638 (100, 200)
2639}
2640
2641sub set_range {
2642 my ($self, $range) = @_;
2643
2644 $self->{range} = $range;
2645 $range->connect (changed => sub { $self->update });
2646
2647 my $page = $self->{h} / 32; # waht information souce to use for face size?
2648 # it should be configurable i guess...
2649
2650 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2651 $range->update;
2652
2653 $self->update;
2654}
2655
2656sub _draw {
2657 my ($self) = @_;
2658
2659 my ($w, $h) = ($self->{w}, $self->{h});
2660
2661 $self->{pos} = int $self->{range}{range}[0];
2662
2663 my $y = 0;
2664 my $cnt = 0;
2665 my $hrem = $self->{h}; # horiz. remaining space
2666
2667 for (my $i = $self->{pos}; $i < @{$self->{children} || []}; $i++) {
2668 my $chld = $self->{children}->[$i];
2669
2670 if ($hrem >= $chld->{h}) {
2671 $chld->configure (0, $y, $chld->{w}, $chld->{h});
2672
2673 $chld->show;
2674 $chld->draw;
2675
2676 $hrem -= $chld->{h};
2677 $y += $chld->{h};
2678 } else {
2679 last
2680 }
2681 }
2682}
2683
2684
2685#############################################################################
2686
2687package CFClient::UI; 3068package CFClient::UI;
2688 3069
2689$ROOT = new CFClient::UI::Root; 3070$ROOT = new CFClient::UI::Root;
2690$TOOLTIP = new CFClient::UI::Tooltip; 3071$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2691 3072
26921 30731
2693 3074

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines