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.188 by root, Mon May 8 21:16:09 2006 UTC vs.
Revision 1.227 by root, Wed May 24 03:29:20 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
141sub full_refresh {
142 # make a copy, otherwise for complains about freed values.
143 my @widgets = values %WIDGET;
144
145 $_->update
146 for @widgets;
147}
148
149# call when resolution changes etc.
150sub rescale_widgets {
151 my ($sx, $sy) = @_;
152
153 # make a copy, otherwise for complains about freed values.
154 my @widgets = values %WIDGET;
155
156 for my $widget (@widgets) {
157 if ($widget->{toplevel}) {
158 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
159 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
160 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
161 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
162 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
163 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
164 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
165 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
166 }
167
168 $widget->reconfigure;
135 } 169 }
136} 170}
137 171
138############################################################################# 172#############################################################################
139 173
158 if (/^connect_(.*)$/) { 192 if (/^connect_(.*)$/) {
159 $self->connect ($1 => delete $self->{$_}); 193 $self->connect ($1 => delete $self->{$_});
160 } 194 }
161 } 195 }
162 196
197 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
198
163 $self 199 $self
164} 200}
165 201
166sub destroy { 202sub destroy {
167 my ($self) = @_; 203 my ($self) = @_;
170 %$self = (); 206 %$self = ();
171} 207}
172 208
173sub show { 209sub show {
174 my ($self) = @_; 210 my ($self) = @_;
175
176 return if $self->{parent}; 211 return if $self->{parent};
177 212
178 $CFClient::UI::ROOT->add ($self); 213 $CFClient::UI::ROOT->add ($self);
214}
215
216sub show_centered {
217 my ($self) = @_;
218 return if $self->{parent};
219
220 $self->show;
221
222 $CFClient::UI::ROOT->on_post_alloc (
223 "centered $self" => sub {
224 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
225 },
226 );
179} 227}
180 228
181sub hide { 229sub hide {
182 my ($self) = @_; 230 my ($self) = @_;
183 231
232 $self->{y} = $y; 280 $self->{y} = $y;
233 $self->update; 281 $self->update;
234 } 282 }
235 283
236 if ($self->{w} != $w || $self->{h} != $h) { 284 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 285 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
238 $self->{h} = $h;
239
240 $self->size_allocate ($w, $h);
241 $self->update;
242 $self->emit (size_allocate => $w, $h);
243 } 286 }
244} 287}
245 288
246sub size_allocate { 289sub size_allocate {
247 # nothing to be done 290 # nothing to be done
248} 291}
249 292
293sub reconfigure {
294 my ($self) = @_;
295
296 $self->check_size (1);
297 $self->update;
298}
299
250sub children { 300sub children {
251}
252
253# call when resolution changes etc.
254sub reconfigure {
255 my ($self) = @_;
256
257 $_->reconfigure
258 for $self->children;
259
260 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h});
262 $self->update;
263} 301}
264 302
265sub set_max_size { 303sub set_max_size {
266 my ($self, $w, $h) = @_; 304 my ($self, $w, $h) = @_;
267 305
268 delete $self->{max_w}; $self->{max_w} = $w if $w; 306 delete $self->{max_w}; $self->{max_w} = $w if $w;
269 delete $self->{max_h}; $self->{max_h} = $h if $h; 307 delete $self->{max_h}; $self->{max_h} = $h if $h;
270} 308}
271 309
272# return top left coordinates 310sub set_tooltip {
273sub _topleft {
274 my ($self, $x, $y) = @_; 311 my ($self, $tooltip) = @_;
275 312
276 $self->{parent} 313 $self->{tooltip} = $tooltip;
277 or Carp::confess "no parent widget in _topleft\n";#d#
278 314
279 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 315 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
316 delete $CFClient::UI::TOOLTIP->{owner};
317 CFClient::UI::check_tooltip;
318 }
280} 319}
281 320
282# translate global coordinates to local coordinate system 321# translate global coordinates to local coordinate system
283sub coord2local { 322sub coord2local {
284 my ($self, $x, $y) = @_; 323 my ($self, $x, $y) = @_;
285 324
286 my ($X, $Y) = $self->_topleft; 325 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
287 ($x - $X, $y - $Y)
288} 326}
289 327
290# translate local coordinates to global coordinate system 328# translate local coordinates to global coordinate system
291sub coord2global { 329sub coord2global {
292 my ($self, $x, $y) = @_; 330 my ($self, $x, $y) = @_;
293 331
294 my ($X, $Y) = $self->_topleft; 332 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
295 ($x + $X, $y + $Y)
296} 333}
297 334
298sub focus_in { 335sub focus_in {
299 my ($self) = @_; 336 my ($self) = @_;
300 337
372 glVertex $self->{w}, 0; 409 glVertex $self->{w}, 0;
373 glVertex $self->{w}, $self->{h}; 410 glVertex $self->{w}, $self->{h};
374 glVertex 0 , $self->{h}; 411 glVertex 0 , $self->{h};
375 glEnd; 412 glEnd;
376 glPopMatrix; 413 glPopMatrix;
377 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 414 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
378 } 415 }
379} 416}
380 417
381sub _draw { 418sub _draw {
382 my ($self) = @_; 419 my ($self) = @_;
398 435
399sub set_parent { 436sub set_parent {
400 my ($self, $parent) = @_; 437 my ($self, $parent) = @_;
401 438
402 Scalar::Util::weaken ($self->{parent} = $parent); 439 Scalar::Util::weaken ($self->{parent} = $parent);
440
441 # TODO: req_w _does_change after ->reconfigure
442 $self->check_size
443 unless exists $self->{req_w};
444
445 $self->show;
403} 446}
404 447
405sub check_size { 448sub check_size {
406 my ($self) = @_; 449 my ($self, $forced) = @_;
407 450
408 $self->{parent} 451 $self->{force_alloc} = 1 if $forced;
409 or return 1; 452 $CFClient::UI::ROOT->{check_size}{$self} = $self;
410
411 my ($w, $h) = $self->{user_w} && $self->{user_h}
412 ? @$self{qw(user_w user_h)}
413 : $self->size_request;
414
415 if ($w != $self->{req_w} || $h != $self->{req_h}) {
416 $self->{req_w} = $w;
417 $self->{req_h} = $h;
418
419 $self->{parent}->check_size
420 or $self->size_allocate (
421 (List::Util::max $self->{w}, $w),
422 (List::Util::max $self->{h}, $h),
423 );
424
425 1
426 } else {
427 0
428 }
429} 453}
430 454
431sub update { 455sub update {
432 my ($self) = @_; 456 my ($self) = @_;
433 457
448} 472}
449 473
450sub DESTROY { 474sub DESTROY {
451 my ($self) = @_; 475 my ($self) = @_;
452 476
477 delete $WIDGET{$self+0};
453 #$self->deactivate; 478 #$self->deactivate;
454} 479}
455 480
456############################################################################# 481#############################################################################
457 482
466 my $class = shift; 491 my $class = shift;
467 492
468 # range [value, low, high, page] 493 # range [value, low, high, page]
469 494
470 $class->SUPER::new ( 495 $class->SUPER::new (
471 bg => [0, 0, 0, 0.2], 496 #bg => [0, 0, 0, 0.2],
472 active_bg => [1, 1, 1, 0.5], 497 #active_bg => [1, 1, 1, 0.5],
473 @_ 498 @_
474 ) 499 )
475} 500}
476 501
477sub _draw { 502sub _draw {
478 my ($self) = @_; 503 my ($self) = @_;
479 504
505 my $color = $FOCUS == $self && $self->{active_bg}
506 ? $self->{active_bg}
507 : $self->{bg};
508
509 if ($color && (@$color < 4 || $color->[3])) {
480 my ($w, $h) = @$self{qw(w h)}; 510 my ($w, $h) = @$self{qw(w h)};
481 511
482 glEnable GL_BLEND; 512 glEnable GL_BLEND;
483 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 513 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
484 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 514 glColor @$color;
485 515
486 glBegin GL_QUADS; 516 glBegin GL_QUADS;
487 glVertex 0 , 0; 517 glVertex 0 , 0;
488 glVertex 0 , $h; 518 glVertex 0 , $h;
489 glVertex $w, $h; 519 glVertex $w, $h;
490 glVertex $w, 0; 520 glVertex $w, 0;
491 glEnd; 521 glEnd;
492 522
493 glDisable GL_BLEND; 523 glDisable GL_BLEND;
524 }
494} 525}
495 526
496############################################################################# 527#############################################################################
497 528
498package CFClient::UI::Empty; 529package CFClient::UI::Empty;
542 $self->{children} = [ 573 $self->{children} = [
543 sort { $a->{z} <=> $b->{z} } 574 sort { $a->{z} <=> $b->{z} }
544 @{$self->{children}}, @widgets 575 @{$self->{children}}, @widgets
545 ]; 576 ];
546 577
547 $_->check_size 578 $self->check_size (1);
548 for @widgets;
549
550 $self->update; 579 $self->update;
551} 580}
552 581
553sub children { 582sub children {
554 @{ $_[0]{children} } 583 @{ $_[0]{children} }
661} 690}
662 691
663sub update { 692sub update {
664 my ($self) = @_; 693 my ($self) = @_;
665 694
666 $ROOT->on_refresh ($self => sub { $self->render_child }); 695 $ROOT->on_post_alloc ($self => sub { $self->render_child });
667 $self->SUPER::update; 696 $self->SUPER::update;
668} 697}
669 698
670sub size_allocate { 699sub size_allocate {
671 my ($self, $w, $h) = @_; 700 my ($self, $w, $h) = @_;
684 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 713 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
685 glClearColor 0, 0, 0, 0; 714 glClearColor 0, 0, 0, 0;
686 glClear GL_COLOR_BUFFER_BIT; 715 glClear GL_COLOR_BUFFER_BIT;
687 716
688 $self->_render; 717 $self->_render;
689# glColorMask 1, 1, 1, 0;
690# glEnable GL_BLEND;
691# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
692# glRasterPos 0, 0;
693# glCopyPixels 0, 0, $self->{w}, $self->{h};
694# glDisable GL_BLEND;
695# glColorMask 1, 1, 1, 1;
696 }; 718 };
697} 719}
698 720
699sub _draw { 721sub _draw {
700 my ($self) = @_; 722 my ($self) = @_;
702 my ($w, $h) = ($self->w, $self->h); 724 my ($w, $h) = ($self->w, $self->h);
703 725
704 my $tex = $self->{texture} 726 my $tex = $self->{texture}
705 or return; 727 or return;
706 728
707 glEnable GL_BLEND;
708 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
709 glEnable GL_TEXTURE_2D; 729 glEnable GL_TEXTURE_2D;
710 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 730 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
711 glColor 0, 0, 0, 1; 731 glColor 1, 1, 1, 1;
712 732
713 $tex->draw_quad (0, 0, $w, $h); 733 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
714 734
715 glDisable GL_BLEND;
716 glDisable GL_TEXTURE_2D; 735 glDisable GL_TEXTURE_2D;
717} 736}
718 737
719############################################################################# 738#############################################################################
720 739
724 743
725sub size_request { 744sub size_request {
726 my ($self) = @_; 745 my ($self) = @_;
727 746
728 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 747 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
729 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
730 748
731 @$self{qw(child_w child_h)} 749 @$self{qw(child_w child_h)}
732} 750}
733 751
734sub size_allocate { 752sub size_allocate {
735 my ($self, $w, $h) = @_; 753 my ($self, $w, $h) = @_;
736 754
755 my ($cw, $ch) = @$self{qw(child_w child_h)};
756# $w = $self->{w};
757 $self->child->configure (0, 0, $cw, $ch);
737 $self->update; 758 $self->update;
738} 759}
739 760
740sub set_offset { 761sub set_offset {
741 my ($self, $x, $y) = @_; 762 my ($self, $x, $y) = @_;
744 $self->{view_y} = int $y; 765 $self->{view_y} = int $y;
745 766
746 $self->update; 767 $self->update;
747} 768}
748 769
749# hmm, this does not work for topleft of $self... but we should not aks for that 770# hmm, this does not work for topleft of $self... but we should not ask for that
750sub _topleft { 771sub coord2local {
751 my ($self, $x, $y) = @_; 772 my ($self, $x, $y) = @_;
752 773
753 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) 774 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
775}
776
777sub coord2global {
778 my ($self, $x, $y) = @_;
779
780 $x = List::Util::min $self->{w}, $x - $self->{view_x};
781 $y = List::Util::min $self->{h}, $y - $self->{view_y};
782
783 $self->SUPER::coord2global ($x, $y)
754} 784}
755 785
756sub find_widget { 786sub find_widget {
757 my ($self, $x, $y) = @_; 787 my ($self, $x, $y) = @_;
758 788
791 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 821 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
792 }, 822 },
793 ; 823 ;
794 824
795 $self = $class->SUPER::new ( 825 $self = $class->SUPER::new (
796 vp => (new CFClient::UI::ViewPort), 826 vp => (new CFClient::UI::ViewPort expand => 1),
797 slider => $slider, 827 slider => $slider,
798 @_, 828 @_,
799 ); 829 );
800 830
801 $self->{vp}->add ($self->{scrolled}); 831 $self->{vp}->add ($self->{scrolled});
803 $self->add ($self->{slider}); 833 $self->add ($self->{slider});
804 834
805 $self 835 $self
806} 836}
807 837
808#TODO# update range on size_allocate depeneing on child 838#TODO# update range on size_allocate depending on child
809# update viewport offset on scroll 839# update viewport offset on scroll
810 840
811############################################################################# 841#############################################################################
812 842
813package CFClient::UI::Frame; 843package CFClient::UI::Frame;
817use CFClient::OpenGL; 847use CFClient::OpenGL;
818 848
819sub new { 849sub new {
820 my $class = shift; 850 my $class = shift;
821 851
822 my $self = $class->SUPER::new ( 852 $class->SUPER::new (
823 bg => [1, 1, 1, 1], 853 bg => undef,
824 border_bg => [1, 1, 1, 1],
825 border => 0.8,
826 @_ 854 @_,
827 ); 855 )
828
829 $self
830} 856}
831 857
832sub _draw { 858sub _draw {
833 my ($self) = @_; 859 my ($self) = @_;
834 860
835 my ($w, $h) = ($self->{w}, $self->{h}); 861 if ($self->{bg}) {
862 my ($w, $h) = @$self{qw(w h)};
836 863
837 glEnable GL_BLEND; 864 glEnable GL_BLEND;
838 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 865 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
839 glEnable GL_TEXTURE_2D; 866 glColor @{ $self->{bg} };
840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
841 867
842# glBegin GL_QUADS; 868 glBegin GL_QUADS;
843# glColor 0, 0, 0, 0;
844# glVertex 0 , 0; 869 glVertex 0 , 0;
845# glVertex 0 , $h; 870 glVertex 0 , $h;
846# glVertex $w, $h; 871 glVertex $w, $h;
847# glVertex $w, 0; 872 glVertex $w, 0;
848# glEnd; 873 glEnd;
849 874
850
851 $self->child->draw;
852 glDisable GL_BLEND; 875 glDisable GL_BLEND;
853 glDisable GL_TEXTURE_2D; 876 }
877
878 $self->SUPER::_draw;
854} 879}
855 880
856############################################################################# 881#############################################################################
857 882
858package CFClient::UI::FancyFrame; 883package CFClient::UI::FancyFrame;
872 897
873 my $self = $class->SUPER::new ( 898 my $self = $class->SUPER::new (
874 bg => [1, 1, 1, 1], 899 bg => [1, 1, 1, 1],
875 border_bg => [1, 1, 1, 1], 900 border_bg => [1, 1, 1, 1],
876 border => 0.6, 901 border => 0.6,
902 toplevel => 1,
877 can_events => 1, 903 can_events => 1,
878 @_ 904 @_
879 ); 905 );
880 906
881 $self->{title} &&= new CFClient::UI::Label 907 $self->{title} &&= new CFClient::UI::Label
906 my ($self, $w, $h) = @_; 932 my ($self, $w, $h) = @_;
907 933
908 $h -= List::Util::max 0, $self->border * 2; 934 $h -= List::Util::max 0, $self->border * 2;
909 $w -= List::Util::max 0, $self->border * 2; 935 $w -= List::Util::max 0, $self->border * 2;
910 936
911 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 937 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
912 if $self->{title}; 938 if $self->{title};
913 939
914 $self->child->configure ($self->border, $self->border, $w, $h); 940 $self->child->configure ($self->border, $self->border, $w, $h);
915} 941}
916 942
974 my ($self) = @_; 1000 my ($self) = @_;
975 1001
976 my ($w, $h ) = ($self->{w}, $self->{h}); 1002 my ($w, $h ) = ($self->{w}, $self->{h});
977 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 1003 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
978 1004
979 glEnable GL_BLEND;
980 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
981 glEnable GL_TEXTURE_2D; 1005 glEnable GL_TEXTURE_2D;
982 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1006 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
983 1007
984 my $border = $self->border; 1008 my $border = $self->border;
985 1009
986 glColor @{ $self->{border_bg} }; 1010 glColor @{ $self->{border_bg} };
987 $tex[1]->draw_quad (0, 0, $w, $border); 1011 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
988 $tex[3]->draw_quad (0, $border, $border, $ch); 1012 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
989 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 1013 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
990 $tex[4]->draw_quad (0, $h - $border, $w, $border); 1014 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
991 1015
992 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1016 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
993 my $bg = $tex[0]; 1017 my $bg = $tex[0];
994 1018
995 # TODO: repeat texture not scale 1019 # TODO: repeat texture not scale
999 glColor @{ $self->{bg} }; 1023 glColor @{ $self->{bg} };
1000 1024
1001 $bg->{s} = $rep_x; 1025 $bg->{s} = $rep_x;
1002 $bg->{t} = $rep_y; 1026 $bg->{t} = $rep_y;
1003 $bg->{wrap_mode} = 1; 1027 $bg->{wrap_mode} = 1;
1004 $bg->draw_quad ($border, $border, $cw, $ch); 1028 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1029 }
1005 1030
1006 glDisable GL_TEXTURE_2D; 1031 glDisable GL_TEXTURE_2D;
1007 glDisable GL_BLEND;
1008 }
1009 1032
1010 $self->{title}->draw if $self->{title}; 1033 $self->{title}->draw if $self->{title};
1011 1034
1012 $self->child->draw; 1035 $self->child->draw;
1013} 1036}
1240} 1263}
1241 1264
1242sub size_allocate { 1265sub size_allocate {
1243 my ($self, $w, $h) = @_; 1266 my ($self, $w, $h) = @_;
1244 1267
1268 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1269
1245 my $children = $self->{children}; 1270 my $children = $self->{children};
1246 1271
1247 my @h = map $_->{req_h}, @$children; 1272 my @h = map $_->{req_h}, @$children;
1248 1273
1249 my $req_h = List::Util::sum @h; 1274 my $req_h = List::Util::sum @h;
1278 1303
1279############################################################################# 1304#############################################################################
1280 1305
1281package CFClient::UI::Label; 1306package CFClient::UI::Label;
1282 1307
1283our @ISA = CFClient::UI::Base::; 1308our @ISA = CFClient::UI::DrawBG::;
1284 1309
1285use CFClient::OpenGL; 1310use CFClient::OpenGL;
1286 1311
1287sub new { 1312sub new {
1288 my ($class, %arg) = @_; 1313 my ($class, %arg) = @_;
1289 1314
1290 my $self = $class->SUPER::new ( 1315 my $self = $class->SUPER::new (
1291 fg => [1, 1, 1], 1316 fg => [1, 1, 1],
1317 #bg => none
1318 #active_bg => none
1292 #font => default_font 1319 #font => default_font
1320 #text => initial text
1321 #markup => initial narkup
1322 #max_w => maximum pixel width
1323 ellipsise => 3, # end
1324 layout => (new CFClient::Layout),
1293 fontsize => 1, 1325 fontsize => 1,
1294 text => "",
1295 align => -1, 1326 align => -1,
1296 valign => -1, 1327 valign => -1,
1297 padding => 2, 1328 padding => 2,
1298 layout => new CFClient::Layout,
1299 can_events => 0, 1329 can_events => 0,
1300 %arg 1330 %arg
1301 ); 1331 );
1302 1332
1303 if (exists $self->{template}) { 1333 if (exists $self->{template}) {
1304 my $layout = new CFClient::Layout; 1334 my $layout = new CFClient::Layout;
1305 $layout->set_text (delete $self->{template}); 1335 $layout->set_text (delete $self->{template});
1306 $self->{template} = $layout; 1336 $self->{template} = $layout;
1307 } 1337 }
1308 1338
1309 $self->set_text (delete $self->{text}) if exists $self->{text}; 1339 if (exists $self->{markup}) {
1310 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1340 $self->set_markup (delete $self->{markup});
1341 } else {
1342 $self->set_text (delete $self->{text});
1343 }
1311 1344
1312 $self 1345 $self
1313} 1346}
1314 1347
1315sub escape { 1348sub escape($) {
1316 local $_ = $_[1]; 1349 local $_ = $_[0];
1317 1350
1318 s/&/&amp;/g; 1351 s/&/&amp;/g;
1319 s/>/&gt;/g; 1352 s/>/&gt;/g;
1320 s/</&lt;/g; 1353 s/</&lt;/g;
1321 1354
1322 $_[1] 1355 $_
1323} 1356}
1324 1357
1325sub update { 1358sub update {
1326 my ($self) = @_; 1359 my ($self) = @_;
1327 1360
1333 my ($self, $text) = @_; 1366 my ($self, $text) = @_;
1334 1367
1335 return if $self->{text} eq "T$text"; 1368 return if $self->{text} eq "T$text";
1336 $self->{text} = "T$text"; 1369 $self->{text} = "T$text";
1337 1370
1371 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1338 $self->{layout}->set_text ($text); 1372 $self->{layout}->set_text ($text);
1339 1373
1340 delete $self->{texture};
1341 $self->update; 1374 $self->update;
1342 $self->check_size; 1375 $self->check_size;
1343} 1376}
1344 1377
1345sub set_markup { 1378sub set_markup {
1346 my ($self, $markup) = @_; 1379 my ($self, $markup) = @_;
1347 1380
1348 return if $self->{text} eq "M$markup"; 1381 return if $self->{text} eq "M$markup";
1349 $self->{text} = "M$markup"; 1382 $self->{text} = "M$markup";
1350 1383
1384 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1385
1386 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1351 $self->{layout}->set_markup ($markup); 1387 $self->{layout}->set_markup ($markup);
1352 1388
1353 delete $self->{texture};
1354 $self->update; 1389 $self->update;
1355 $self->check_size; 1390 $self->check_size;
1356} 1391}
1357 1392
1358sub size_request { 1393sub size_request {
1359 my ($self) = @_; 1394 my ($self) = @_;
1360 1395
1361 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1396 $self->{layout}->set_font ($self->{font}) if $self->{font};
1362 $self->{layout}->set_width ($self->{max_w} || -1); 1397 $self->{layout}->set_width ($self->{max_w} || -1);
1398 $self->{layout}->set_ellipsise ($self->{ellipsise});
1399 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1363 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1400 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1364 1401
1365 my ($w, $h) = $self->{layout}->size; 1402 my ($w, $h) = $self->{layout}->size;
1366 1403
1367 if (exists $self->{template}) { 1404 if (exists $self->{template}) {
1397} 1434}
1398 1435
1399sub _draw { 1436sub _draw {
1400 my ($self) = @_; 1437 my ($self) = @_;
1401 1438
1439 $self->SUPER::_draw; # draw background, if applicable
1440
1402 my $tex = $self->{texture} ||= do { 1441 my $tex = $self->{texture} ||= do {
1442 $self->{layout}->set_foreground (@{$self->{fg}});
1403 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1443 $self->{layout}->set_font ($self->{font}) if $self->{font};
1404 $self->{layout}->set_width ($self->{w}); 1444 $self->{layout}->set_width ($self->{w});
1445 $self->{layout}->set_ellipsise ($self->{ellipsise});
1446 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1405 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1447 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1448
1406 new_from_layout CFClient::Texture $self->{layout} 1449 my $tex = new_from_layout CFClient::Texture $self->{layout};
1450
1451 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1452 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1453 : ($self->{w} - $tex->{w}) * 0.5);
1454
1455 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1456 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1457 : ($self->{h} - $tex->{h}) * 0.5);
1458
1459 $tex
1407 }; 1460 };
1408 1461
1409 glEnable GL_BLEND;
1410 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1411 glEnable GL_TEXTURE_2D; 1462 glEnable GL_TEXTURE_2D;
1412 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1463 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1413 1464
1465 if ($tex->{format} == GL_ALPHA) {
1414 glColor @{$self->{fg}}; 1466 glColor @{$self->{fg}};
1415
1416 $self->{ox} = int (
1417 $self->{align} < 0 ? $self->{padding}
1418 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1419 : ($self->{w} - $tex->{w}) * 0.5
1420 );
1421
1422 $self->{oy} = int (
1423 $self->{valign} < 0 ? $self->{padding}
1424 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1425 : ($self->{h} - $tex->{h}) * 0.5
1426 );
1427
1428 $tex->draw_quad ($self->{ox}, $self->{oy}); 1467 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1468 } else {
1469 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1470 }
1429 1471
1430 glDisable GL_TEXTURE_2D; 1472 glDisable GL_TEXTURE_2D;
1431 glDisable GL_BLEND;
1432} 1473}
1433 1474
1434############################################################################# 1475#############################################################################
1435 1476
1436package CFClient::UI::EntryBase; 1477package CFClient::UI::EntryBase;
1449 active_fg => [0, 0, 0], 1490 active_fg => [0, 0, 0],
1450 can_hover => 1, 1491 can_hover => 1,
1451 can_focus => 1, 1492 can_focus => 1,
1452 valign => 0, 1493 valign => 0,
1453 can_events => 1, 1494 can_events => 1,
1495 #text => ...
1454 @_ 1496 @_
1455 ) 1497 )
1456} 1498}
1457 1499
1458sub _set_text { 1500sub _set_text {
1469 1511
1470 $text =~ s/./*/g if $self->{hidden}; 1512 $text =~ s/./*/g if $self->{hidden};
1471 $self->{layout}->set_text ("$text "); 1513 $self->{layout}->set_text ("$text ");
1472 1514
1473 $self->emit (changed => $self->{text}); 1515 $self->emit (changed => $self->{text});
1474}
1475
1476sub get_text {
1477 $_[0]{text}
1478}
1479
1480sub size_request {
1481 my ($self) = @_;
1482
1483 my ($w, $h) = $self->SUPER::size_request;
1484
1485 ($w + 1, $h) # add 1 for cursor
1486}
1487
1488sub size_allocate {
1489 my ($self, $w, $h) = @_;
1490
1491 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1492} 1516}
1493 1517
1494sub set_text { 1518sub set_text {
1495 my ($self, $text) = @_; 1519 my ($self, $text) = @_;
1496 1520
1497 $self->{cursor} = length $text; 1521 $self->{cursor} = length $text;
1498 $self->_set_text ($text); 1522 $self->_set_text ($text);
1499 $self->update; 1523 $self->update;
1524 $self->check_size;
1525}
1526
1527sub get_text {
1528 $_[0]{text}
1529}
1530
1531sub size_request {
1532 my ($self) = @_;
1533
1534 my ($w, $h) = $self->SUPER::size_request;
1535
1536 ($w + 1, $h) # add 1 for cursor
1537}
1538
1539sub size_allocate {
1540 my ($self, $w, $h) = @_;
1541
1542 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1500} 1543}
1501 1544
1502sub key_down { 1545sub key_down {
1503 my ($self, $ev) = @_; 1546 my ($self, $ev) = @_;
1504 1547
1506 my $sym = $ev->{sym}; 1549 my $sym = $ev->{sym};
1507 my $uni = $ev->{unicode}; 1550 my $uni = $ev->{unicode};
1508 1551
1509 my $text = $self->get_text; 1552 my $text = $self->get_text;
1510 1553
1511 if ($sym == 8) { 1554 if ($uni == 8) {
1512 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1555 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1513 } elsif ($sym == 127) { 1556 } elsif ($uni == 127) {
1514 substr $text, $self->{cursor}, 1, ""; 1557 substr $text, $self->{cursor}, 1, "";
1515 } elsif ($sym == CFClient::SDLK_LEFT) { 1558 } elsif ($sym == CFClient::SDLK_LEFT) {
1516 --$self->{cursor} if $self->{cursor}; 1559 --$self->{cursor} if $self->{cursor};
1517 } elsif ($sym == CFClient::SDLK_RIGHT) { 1560 } elsif ($sym == CFClient::SDLK_RIGHT) {
1518 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1561 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1519 } elsif ($sym == CFClient::SDLK_HOME) { 1562 } elsif ($sym == CFClient::SDLK_HOME) {
1520 $self->{cursor} = 0; 1563 $self->{cursor} = 0;
1521 } elsif ($sym == CFClient::SDLK_END) { 1564 } elsif ($sym == CFClient::SDLK_END) {
1522 $self->{cursor} = length $text; 1565 $self->{cursor} = length $text;
1523 } elsif ($sym == 27) { 1566 } elsif ($uni == 27) {
1524 $self->emit ('escape'); 1567 $self->emit ('escape');
1525 } elsif ($uni) { 1568 } elsif ($uni) {
1526 substr $text, $self->{cursor}++, 0, chr $uni; 1569 substr $text, $self->{cursor}++, 0, chr $uni;
1527 } 1570 }
1528 1571
1529 $self->_set_text ($text); 1572 $self->_set_text ($text);
1530 $self->update; 1573 $self->update;
1574 $self->check_size;
1531} 1575}
1532 1576
1533sub focus_in { 1577sub focus_in {
1534 my ($self) = @_; 1578 my ($self) = @_;
1535 1579
1664 my $class = shift; 1708 my $class = shift;
1665 1709
1666 $class->SUPER::new ( 1710 $class->SUPER::new (
1667 padding => 4, 1711 padding => 4,
1668 fg => [1, 1, 1], 1712 fg => [1, 1, 1],
1669 bg => [1, 1, 1, 0.2],
1670 active_fg => [0, 0, 1], 1713 active_fg => [0, 0, 1],
1671 can_hover => 1, 1714 can_hover => 1,
1672 align => 0, 1715 align => 0,
1673 valign => 0, 1716 valign => 0,
1674 can_events => 1, 1717 can_events => 1,
1692 1735
1693 if ($GRAB == $self) { 1736 if ($GRAB == $self) {
1694 $self->{fg} = $self->{active_fg}; 1737 $self->{fg} = $self->{active_fg};
1695 } 1738 }
1696 1739
1697 glEnable GL_BLEND;
1698 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1699 glEnable GL_TEXTURE_2D; 1740 glEnable GL_TEXTURE_2D;
1700 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1741 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1701 glColor 0, 0, 0, 1; 1742 glColor 0, 0, 0, 1;
1702 1743
1703 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1744 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1704 1745
1705 glDisable GL_TEXTURE_2D; 1746 glDisable GL_TEXTURE_2D;
1706 glDisable GL_BLEND;
1707 1747
1708 $self->SUPER::_draw; 1748 $self->SUPER::_draw;
1709} 1749}
1710 1750
1711############################################################################# 1751#############################################################################
1725 1765
1726 $class->SUPER::new ( 1766 $class->SUPER::new (
1727 padding => 2, 1767 padding => 2,
1728 fg => [1, 1, 1], 1768 fg => [1, 1, 1],
1729 active_fg => [1, 1, 0], 1769 active_fg => [1, 1, 0],
1770 bg => [0, 0, 0, 0.2],
1771 active_bg => [1, 1, 1, 0.5],
1730 state => 0, 1772 state => 0,
1731 can_hover => 1, 1773 can_hover => 1,
1732 @_ 1774 @_
1733 ) 1775 )
1734} 1776}
1758 1800
1759 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1801 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1760 1802
1761 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1803 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1762 1804
1763 glEnable GL_BLEND; 1805 my $tex = $self->{state} ? $tex[1] : $tex[0];
1806
1764 glEnable GL_TEXTURE_2D; 1807 glEnable GL_TEXTURE_2D;
1765 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1766
1767 my $tex = $self->{state} ? $tex[1] : $tex[0];
1768
1769 $tex->draw_quad (0, 0, $s, $s); 1808 $tex->draw_quad_alpha (0, 0, $s, $s);
1770
1771 glDisable GL_TEXTURE_2D; 1809 glDisable GL_TEXTURE_2D;
1772 glDisable GL_BLEND;
1773} 1810}
1774 1811
1775############################################################################# 1812#############################################################################
1776 1813
1777package CFClient::UI::Image; 1814package CFClient::UI::Image;
1820 glTranslate 0, -$self->{w}, 0; 1857 glTranslate 0, -$self->{w}, 0;
1821 1858
1822 ($w, $h) = ($h, $w); 1859 ($w, $h) = ($h, $w);
1823 } 1860 }
1824 1861
1825 glEnable GL_BLEND;
1826 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1827 glEnable GL_TEXTURE_2D; 1862 glEnable GL_TEXTURE_2D;
1828 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1863 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1829 1864
1830 $tex->draw_quad (0, 0, $w, $h); 1865 $tex->draw_quad_alpha (0, 0, $w, $h);
1831 1866
1832 glDisable GL_BLEND;
1833 glDisable GL_TEXTURE_2D; 1867 glDisable GL_TEXTURE_2D;
1834} 1868}
1835 1869
1836############################################################################# 1870#############################################################################
1837 1871
2026 qw(s1_slider.png s1_slider_bg.png); 2060 qw(s1_slider.png s1_slider_bg.png);
2027 2061
2028sub new { 2062sub new {
2029 my $class = shift; 2063 my $class = shift;
2030 2064
2031 # range [value, low, high, page] 2065 # range [value, low, high, page, unit]
2032 2066
2033 # TODO: 0-width page 2067 # TODO: 0-width page
2034 # TODO: req_w/h are wrong with vertical 2068 # TODO: req_w/h are wrong with vertical
2035 # TODO: calculations are off 2069 # TODO: calculations are off
2036 my $self = $class->SUPER::new ( 2070 my $self = $class->SUPER::new (
2037 fg => [1, 1, 1], 2071 fg => [1, 1, 1],
2038 active_fg => [0, 0, 0], 2072 active_fg => [0, 0, 0],
2073 bg => [0, 0, 0, 0.2],
2074 active_bg => [1, 1, 1, 0.5],
2039 range => [0, 0, 100, 10], 2075 range => [0, 0, 100, 10, 0],
2040 req_w => $::WIDTH / 80, 2076 req_w => $::WIDTH / 80,
2041 req_h => $::WIDTH / 80, 2077 req_h => $::WIDTH / 80,
2042 vertical => 0, 2078 vertical => 0,
2043 can_hover => 1, 2079 can_hover => 1,
2044 inner_pad => 5, 2080 inner_pad => 0.02,
2045 @_ 2081 @_
2046 ); 2082 );
2047 2083
2084 $self->set_value ($self->{range}[0]);
2085 $self->update;
2086
2048 $self 2087 $self
2088}
2089
2090sub set_range {
2091 my ($self, $range) = @_;
2092
2093 $self->{range} = $range;
2094
2095 $self->update;
2096}
2097
2098sub set_value {
2099 my ($self, $value) = @_;
2100
2101 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2102
2103 $hi = $lo + 1 if $hi <= $lo;
2104
2105 $page = $hi - $lo if $page > $hi - $lo;
2106
2107 $value = $lo if $value < $lo;
2108 $value = $hi - $page if $value > $hi - $page;
2109
2110 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2111 if $unit;
2112
2113 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2114
2115 if ($value != $old_value) {
2116 $self->emit (changed => $value);
2117 $self->update;
2118 }
2049} 2119}
2050 2120
2051sub size_request { 2121sub size_request {
2052 my ($self) = @_; 2122 my ($self) = @_;
2053 2123
2059 2129
2060sub button_down { 2130sub button_down {
2061 my ($self, $ev, $x, $y) = @_; 2131 my ($self, $ev, $x, $y) = @_;
2062 2132
2063 $self->SUPER::button_down ($ev, $x, $y); 2133 $self->SUPER::button_down ($ev, $x, $y);
2134
2135 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2136
2064 $self->mouse_motion ($ev, $x, $y); 2137 $self->mouse_motion ($ev, $x, $y);
2065} 2138}
2066 2139
2067sub mouse_motion { 2140sub mouse_motion {
2068 my ($self, $ev, $x, $y) = @_; 2141 my ($self, $ev, $x, $y) = @_;
2069 2142
2070 if ($GRAB == $self) { 2143 if ($GRAB == $self) {
2144 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2145
2146 my (undef, $lo, $hi, $page) = @{$self->{range}};
2147
2148 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2149
2150 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2151 }
2152}
2153
2154sub update {
2155 my ($self) = @_;
2156
2157 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2158 $self->set_value ($self->{range}[0]);
2159
2071 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2160 my ($value, $lo, $hi, $page) = @{$self->{range}};
2161 my $range = ($hi - $page - $lo) || 1e-100;
2072 2162
2073 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2163 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
2074 2164
2075 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2165 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2076 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 2166 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2077 2167
2078 $x -= $inner_pad_px; # substract the padding 2168 $value = ($value - $lo) / $range;
2079 $x = $x * ($hi - $lo) / $inner_w + $lo; 2169 $value = $value * $self->{scale} + $self->{offset};
2080 $x = $lo if $x < $lo;
2081 $x = $hi - $page if $x > $hi - $page;
2082 $self->{range}[0] = $x;
2083 2170
2084 $self->emit (changed => $x); 2171 $self->{knob_x} = $value - $knob_w * 0.5;
2085 $self->update; 2172 $self->{knob_w} = $knob_w;
2086 } 2173 });
2087}
2088 2174
2089# the inner_* stuff is for generating a padding for the slider handle, 2175 $self->SUPER::update;
2090# so that the handle doesn't leave the texture. This calculation isn't 100%
2091# correct propably, but it does the job for now
2092sub _calc_inner_pad_px {
2093 my ($self, $w) = @_;
2094 ($w / 100) * $self->{inner_pad} # % to pixels
2095} 2176}
2096 2177
2097sub _draw { 2178sub _draw {
2098 my ($self) = @_; 2179 my ($self) = @_;
2099 2180
2100 $self->SUPER::_draw (); 2181 $self->SUPER::_draw ();
2101 2182
2102 my ($w, $h) = @$self{qw(w h)}; 2183 glScale $self->{w}, $self->{h};
2103 2184
2104 if ($self->{vertical}) { 2185 if ($self->{vertical}) {
2105 # draw a vertical slider like a rotated horizontal slider 2186 # draw a vertical slider like a rotated horizontal slider
2106 2187
2188 glTranslate 1, 0, 0;
2107 glRotate 90, 0, 0, 1; 2189 glRotate 90, 0, 0, 1;
2108 glTranslate 0, -$self->{w}, 0;
2109
2110 ($w, $h) = ($h, $w);
2111 } 2190 }
2112 2191
2113 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2192 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2114 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2193 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2115 2194
2116 my ($value, $lo, $hi, $page) = @{$self->{range}};
2117
2118 $hi = $value + 1 if $lo == $hi;
2119
2120 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2121 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2122
2123 $page = int $page * $inner_w / ($hi - $lo);
2124 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2125
2126 $w -= $page;
2127 $page &= ~1;
2128 glTranslate $page * 0.5, 0, 0;
2129 $page ||= 2;
2130
2131 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2132 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2133
2134 glEnable GL_BLEND;
2135 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2136 glEnable GL_TEXTURE_2D; 2195 glEnable GL_TEXTURE_2D;
2137 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2196 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2138 2197
2139 # draw background 2198 # draw background
2140 $tex[1]->draw_quad (0, 0, $w, $h); 2199 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2141 2200
2142 # draw handle 2201 # draw handle
2143 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2202 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2144 2203
2145 glDisable GL_BLEND;
2146 glDisable GL_TEXTURE_2D; 2204 glDisable GL_TEXTURE_2D;
2147} 2205}
2206
2207#############################################################################
2208
2209package CFClient::UI::ValSlider;
2210
2211our @ISA = CFClient::UI::HBox::;
2212
2213sub new {
2214 my ($class, %arg) = @_;
2215
2216 my $range = delete $arg{range};
2217
2218 my $self = $class->SUPER::new (
2219 slider => (new CFClient::UI::Slider expand => 1, range => $range),
2220 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}),
2221 to_value => sub { shift },
2222 from_value => sub { shift },
2223 %arg,
2224 );
2225
2226 $self->{slider}->connect (changed => sub {
2227 my ($self, $value) = @_;
2228 $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value));
2229 $self->{parent}->emit (changed => $value);
2230 });
2231
2232# $self->{entry}->connect (changed => sub {
2233# my ($self, $value) = @_;
2234# $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value));
2235# $self->{parent}->emit (changed => $value);
2236# });
2237
2238 $self->add ($self->{slider}, $self->{entry});
2239
2240 $self->{slider}->emit (changed => $self->{slider}{range}[0]);
2241
2242 $self
2243}
2244
2245sub set_range { shift->{slider}->set_range (@_) }
2246sub set_value { shift->{slider}->set_value (@_) }
2148 2247
2149############################################################################# 2248#############################################################################
2150 2249
2151package CFClient::UI::TextView; 2250package CFClient::UI::TextView;
2152 2251
2161 fontsize => 1, 2260 fontsize => 1,
2162 can_events => 0, 2261 can_events => 0,
2163 #font => default_font 2262 #font => default_font
2164 @_, 2263 @_,
2165 2264
2166 layout => (new CFClient::Layout), 2265 layout => (new CFClient::Layout 1),
2167 par => [], 2266 par => [],
2168 height => 0, 2267 height => 0,
2169 children => [ 2268 children => [
2170 (new CFClient::UI::Empty expand => 1), 2269 (new CFClient::UI::Empty expand => 1),
2171 (new CFClient::UI::Slider vertical => 1), 2270 (new CFClient::UI::Slider vertical => 1),
2182 2281
2183 $self->{fontsize} = $fontsize; 2282 $self->{fontsize} = $fontsize;
2184 $self->reflow; 2283 $self->reflow;
2185} 2284}
2186 2285
2187sub text_height {
2188 my ($self, $text) = @_;
2189
2190 my $layout = $self->{layout};
2191
2192 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2193 $layout->set_width ($self->{children}[0]{w});
2194 $layout->set_text ($text);
2195
2196 ($layout->size)[1]
2197}
2198
2199sub reflow {
2200 my ($self) = @_;
2201
2202 $self->{need_reflow}++;
2203 $self->update;
2204}
2205
2206sub size_allocate { 2286sub size_allocate {
2207 my ($self, $w, $h) = @_; 2287 my ($self, $w, $h) = @_;
2208 2288
2209 $self->SUPER::size_allocate ($w, $h); 2289 $self->SUPER::size_allocate ($w, $h);
2210 2290
2213 $self->{layout}->set_width ($self->{children}[0]{w}); 2293 $self->{layout}->set_width ($self->{children}[0]{w});
2214 2294
2215 $self->reflow; 2295 $self->reflow;
2216} 2296}
2217 2297
2298sub text_height {
2299 my ($self, $text, $indent) = @_;
2300
2301 my $layout = $self->{layout};
2302
2303 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2304 $layout->set_width ($self->{children}[0]{w} - $indent);
2305 $layout->set_markup ($text);
2306
2307 ($layout->size)[1]
2308}
2309
2310sub reflow {
2311 my ($self) = @_;
2312
2313 $self->{need_reflow}++;
2314 $self->update;
2315}
2316
2317sub set_offset {
2318 my ($self, $offset) = @_;
2319
2320 # todo: base offset on lines or so, not on pixels
2321 $self->{children}[1]->set_value ($offset);
2322}
2323
2324sub clear {
2325 my ($self) = @_;
2326
2327 $self->{par} = [];
2328 $self->{height} = 0;
2329 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2330}
2331
2218sub add_paragraph { 2332sub add_paragraph {
2219 my ($self, $color, $text) = @_; 2333 my ($self, $color, $text, $indent) = @_;
2220 2334
2221 #TODO: intelligently "reformat" paragraph 2335 for my $line (split /\n/, $text) {
2222
2223 my $height = $self->text_height ($text); 2336 my $height = $self->text_height ($line);
2224
2225 $self->{height} += $height; 2337 $self->{height} += $height;
2226
2227 push @{$self->{par}}, [$height, $color, $text]; 2338 push @{$self->{par}}, [$height, $color, $indent, $line];
2339 }
2228 2340
2229 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2341 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]);
2230 $self->{children}[1]->update;
2231} 2342}
2232 2343
2233sub update { 2344sub update {
2234 my ($self) = @_; 2345 my ($self) = @_;
2235 2346
2237 2348
2238 return unless $self->{h} > 0; 2349 return unless $self->{h} > 0;
2239 2350
2240 delete $self->{texture}; 2351 delete $self->{texture};
2241 2352
2242 $ROOT->on_refresh ($self, sub { 2353 $ROOT->on_post_alloc ($self, sub {
2243 if (delete $self->{need_reflow}) { 2354 if (delete $self->{need_reflow}) {
2244 my $height = 0; 2355 my $height = 0;
2245 2356
2246 $height += $_->[0] = $self->text_height ($_->[2]) 2357 $height += $_->[0] = $self->text_height ($_->[3], $_->[2])
2247 for @{$self->{par}}; 2358 for @{$self->{par}};
2248 2359
2249 $self->{height} = $height; 2360 $self->{height} = $height;
2250 2361
2251 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2362 $self->{children}[1]->set_range ([$height, 0, $height, $self->{h}, 1]);
2252 2363
2253 delete $self->{texture}; 2364 delete $self->{texture};
2254 } 2365 }
2255 2366
2256 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2367 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2257 glClearColor 0, 0, 0, 0; 2368 glClearColor 0.5, 0.5, 0.5, 0;
2258 glClear GL_COLOR_BUFFER_BIT; 2369 glClear GL_COLOR_BUFFER_BIT;
2259
2260 glEnable GL_BLEND;
2261 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2262 glEnable GL_TEXTURE_2D;
2263 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2264 2370
2265 my $top = int $self->{children}[1]{range}[0]; 2371 my $top = int $self->{children}[1]{range}[0];
2266 2372
2267 my $y0 = $top; 2373 my $y0 = $top;
2268 my $y1 = $top + $self->{h}; 2374 my $y1 = $top + $self->{h};
2271 2377
2272 my $layout = $self->{layout}; 2378 my $layout = $self->{layout};
2273 2379
2274 $layout->set_font ($self->{font}) if $self->{font}; 2380 $layout->set_font ($self->{font}) if $self->{font};
2275 2381
2382 glEnable GL_BLEND;
2383 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2384
2276 for my $par (@{$self->{par}}) { 2385 for my $par (@{$self->{par}}) {
2277 my $h = $par->[0]; 2386 my $h = $par->[0];
2278 2387
2279 if ($y0 < $y + $h && $y < $y1) { 2388 if ($y0 < $y + $h && $y < $y1) {
2389 $layout->set_foreground (@{ $par->[1] });
2390 $layout->set_width ($self->{w} - $par->[2]);
2280 $layout->set_text ($par->[2]); 2391 $layout->set_markup ($par->[3]);
2281 2392
2282 glColor @{ $par->[1] }; 2393 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2283 my ($W, $H) = $layout->size; 2394
2284 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2395 glRasterPos $par->[2], $y - $y0;
2396 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2285 } 2397 }
2286 2398
2287 $y += $h; 2399 $y += $h;
2288 } 2400 }
2289 2401
2290 glDisable GL_TEXTURE_2D;
2291 glDisable GL_BLEND; 2402 glDisable GL_BLEND;
2292 }; 2403 };
2293 }); 2404 });
2294} 2405}
2295 2406
2296sub _draw { 2407sub _draw {
2297 my ($self) = @_; 2408 my ($self) = @_;
2298 2409
2299 glEnable GL_BLEND;
2300 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2301 glEnable GL_TEXTURE_2D; 2410 glEnable GL_TEXTURE_2D;
2302 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2411 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2303 glColor 1, 1, 1, 1; 2412 glColor 1, 1, 1, 1;
2304 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2413 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2305 glDisable GL_TEXTURE_2D; 2414 glDisable GL_TEXTURE_2D;
2306 glDisable GL_BLEND;
2307 2415
2308 $self->{children}[1]->draw; 2416 $self->{children}[1]->draw;
2309 2417
2310} 2418}
2311 2419
2405 @_, 2513 @_,
2406 can_events => 0, 2514 can_events => 0,
2407 ) 2515 )
2408} 2516}
2409 2517
2410sub set_markup { 2518sub set_tooltip_from {
2411 my ($self, $text) = @_; 2519 my ($self, $widget) = @_;
2412 2520
2413 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2521 $self->add (new CFClient::UI::Label
2414 $self->{label}->set_markup ($text); 2522 markup => $widget->{tooltip},
2415 $self->add ($self->{label}); 2523 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2524 fontsize => 0.8,
2525 fg => [0, 0, 0, 1],
2526 ellipsise => 0,
2527 font => ($widget->{tooltip_font} || $::FONT_PROP),
2528 );
2416} 2529}
2417 2530
2418sub size_request { 2531sub size_request {
2419 my ($self) = @_; 2532 my ($self) = @_;
2420
2421 $self->child->set_max_size ($::WIDTH * 0.3);
2422 2533
2423 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2534 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2424 2535
2425 ($w + 4, $h + 4) 2536 ($w + 4, $h + 4)
2426} 2537}
2432} 2543}
2433 2544
2434sub _draw { 2545sub _draw {
2435 my ($self) = @_; 2546 my ($self) = @_;
2436 2547
2437 glPushMatrix;
2438 glTranslate 0.375, 0.375; 2548 glTranslate 0.375, 0.375;
2439 2549
2440 my ($w, $h) = @$self{qw(w h)}; 2550 my ($w, $h) = @$self{qw(w h)};
2441 2551
2442 glColor 1, 0.8, 0.4; 2552 glColor 1, 0.8, 0.4;
2453 glVertex 0 , $h; 2563 glVertex 0 , $h;
2454 glVertex $w, $h; 2564 glVertex $w, $h;
2455 glVertex $w, 0; 2565 glVertex $w, 0;
2456 glEnd; 2566 glEnd;
2457 2567
2458 glPopMatrix; 2568 glTranslate 2 - 0.375, 2 - 0.375;
2459
2460 glTranslate 2, 2;
2461 $self->SUPER::_draw; 2569 $self->SUPER::_draw;
2462} 2570}
2463 2571
2464############################################################################# 2572#############################################################################
2465 2573
2470use CFClient::OpenGL; 2578use CFClient::OpenGL;
2471 2579
2472sub new { 2580sub new {
2473 my $class = shift; 2581 my $class = shift;
2474 2582
2475 $class->SUPER::new ( 2583 my $self = $class->SUPER::new (
2476 aspect => 1, 2584 aspect => 1,
2477 @_, 2585 @_,
2478 ) 2586 );
2587
2588 if ($self->{anim} && $self->{animspeed}) {
2589 Scalar::Util::weaken (my $widget = $self);
2590
2591 $self->{timer} = Event->timer (
2592 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2593 hard => 1,
2594 interval => $self->{animspeed},
2595 cb => sub {
2596 ++$widget->{frame};
2597 $widget->update;
2598 },
2599 );
2600 }
2601
2602 $self
2479} 2603}
2480 2604
2481sub size_request { 2605sub size_request {
2482 (32, 8) 2606 (32, 8)
2483} 2607}
2484 2608
2609sub update {
2610 my ($self) = @_;
2611
2612 return unless $self->{visible};
2613
2614 $self->SUPER::update;
2615}
2616
2485sub _draw { 2617sub _draw {
2486 my ($self) = @_; 2618 my ($self) = @_;
2487 2619
2488 return unless $::CONN;#d# manage and cache textures differently 2620 return unless $::CONN;
2621
2622 my $face;
2623
2624 if ($self->{frame}) {
2625 my $anim = $::CONN->{anim}[$self->{anim}];
2626
2627 $face = $anim->[ $self->{frame} % @$anim ]
2628 if $anim && @$anim;
2629 }
2630
2489 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2631 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2490 2632
2491 # TODO animation
2492 if ($tex) { 2633 if ($tex) {
2493 glEnable GL_BLEND;
2494 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2495 glEnable GL_TEXTURE_2D; 2634 glEnable GL_TEXTURE_2D;
2496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2635 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2497 glColor 1, 1, 1, 1; 2636 glColor 1, 1, 1, 1;
2498 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2637 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2499 glDisable GL_TEXTURE_2D; 2638 glDisable GL_TEXTURE_2D;
2500 glDisable GL_BLEND;
2501 } 2639 }
2640}
2641
2642sub DESTROY {
2643 my ($self) = @_;
2644
2645 $self->{timer}->cancel
2646 if $self->{timer};
2647
2648 $self->SUPER::DESTROY;
2502} 2649}
2503 2650
2504############################################################################# 2651#############################################################################
2505 2652
2506package CFClient::UI::InventoryItem; 2653package CFClient::UI::InventoryItem;
2507 2654
2508our @ISA = CFClient::UI::HBox::; 2655our @ISA = CFClient::UI::HBox::;
2656
2657sub _item_to_desc {
2658 my ($item) = @_;
2659
2660 my $desc =
2661 $item->{nrof} < 2
2662 ? $item->{name}
2663 : "$item->{nrof} × $item->{name_pl}";
2664
2665 $item->{flags} & Crossfire::Protocol::F_OPEN
2666 and $desc .= " (open)";
2667 $item->{flags} & Crossfire::Protocol::F_APPLIED
2668 and $desc .= " (applied)";
2669 $item->{flags} & Crossfire::Protocol::F_UNPAID
2670 and $desc .= " (unpaid)";
2671 $item->{flags} & Crossfire::Protocol::F_MAGIC
2672 and $desc .= " (magic)";
2673 $item->{flags} & Crossfire::Protocol::F_CURSED
2674 and $desc .= " (cursed)";
2675 $item->{flags} & Crossfire::Protocol::F_DAMNED
2676 and $desc .= " (damned)";
2677 $item->{flags} & Crossfire::Protocol::F_LOCKED
2678 and $desc .= " *";
2679
2680 $desc
2681}
2509 2682
2510sub new { 2683sub new {
2511 my $class = shift; 2684 my $class = shift;
2512 2685
2513 my %args = @_; 2686 my %args = @_;
2514 2687
2515 my $item = $args{item}; 2688 my $item = delete $args{item};
2516 2689
2517 my $desc = $item->{nrof} < 2 2690 my $desc = _item_to_desc ($item);
2518 ? $item->{name}
2519 : "$item->{nrof} $item->{name_pl}";
2520
2521 2691
2522 my $self = $class->SUPER::new ( 2692 my $self = $class->SUPER::new (
2523 can_hover => 1, 2693 can_hover => 1,
2524 can_events => 1, 2694 can_events => 1,
2525 tooltip => (CFClient::UI::Label->escape ($desc) 2695 tooltip => ((CFClient::UI::Label::escape $desc)
2526 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2696 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2527 connect_button_down => sub { 2697 connect_button_down => sub {
2528 my ($self, $ev, $x, $y) = @_; 2698 my ($self, $ev, $x, $y) = @_;
2529 2699
2530 # todo: maybe put examine on 1? but should just be a tooltip :( 2700 # todo: maybe put examine on 1? but should just be a tooltip :(
2701 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2702 my $targ = $::CONN->{player}{tag};
2703
2704 if ($item->{container} == $::CONN->{player}{tag}) {
2705 $targ = $main::OPENCONT;
2706 }
2707
2708 $::CONN->send ("move $targ $item->{tag} 0");
2531 if ($ev->{button} == 1) { 2709 } elsif ($ev->{button} == 1) {
2532 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2710 $::CONN->send ("examine $item->{tag}");
2533 } elsif ($ev->{button} == 2) { 2711 } elsif ($ev->{button} == 2) {
2534 $::CONN->send ("apply $item->{tag}"); 2712 $::CONN->send ("apply $item->{tag}");
2535 } elsif ($ev->{button} == 3) { 2713 } elsif ($ev->{button} == 3) {
2536 CFClient::UI::Menu->new ( 2714 my @menu_items = (
2537 items => [
2538 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2715 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2539 [
2540 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2541 sub { $::CONN->send ("lock $item->{tag}") },
2542 ],
2543 ["mark", sub { $::CONN->send ("mark $item->{tag}") }], 2716 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2544 ["apply", sub { $::CONN->send ("apply $item->{tag}") }], 2717 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2718 (
2719 $item->{flags} & Crossfire::Protocol::F_LOCKED
2720 ? (
2721 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2722 )
2723 : (
2724 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2545 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], 2725 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2726 )
2546 ], 2727 ),
2547 )->popup ($ev); 2728 );
2729
2730 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2548 } 2731 }
2549 2732
2550 1 2733 1
2551 }, 2734 },
2552 %args 2735 %args
2553 ); 2736 );
2737
2738
2554 $self->add(new CFClient::UI::Face 2739 $self->add (new CFClient::UI::Face
2555 can_events => 0, 2740 can_events => 0,
2556 face => $item->{face}, 2741 face => $item->{face},
2557 anim => $item->{anim}, 2742 anim => $item->{anim},
2558 animspeed => $item->{animspeed}); 2743 animspeed => $item->{animspeed},
2559 $self->add(new CFClient::UI::Label 2744 );
2560 can_events => 0, 2745
2561 text => $desc); 2746 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2747
2748 $self->{item} = $item;
2749
2750 $self->update_item;
2562 2751
2563 $self 2752 $self
2753}
2754
2755sub update_item {
2756 my ($self) = @_;
2757
2758 my $desc = _item_to_desc ($self->{item});
2759
2760 $self->{name_lbl}->set_text ($desc);
2564} 2761}
2565 2762
2566############################################################################# 2763#############################################################################
2567 2764
2568package CFClient::UI::Inventory; 2765package CFClient::UI::Inventory;
2571 2768
2572sub new { 2769sub new {
2573 my $class = shift; 2770 my $class = shift;
2574 2771
2575 my $self = $class->SUPER::new ( 2772 my $self = $class->SUPER::new (
2576 scrolled => (new CFClient::UI::VBox), 2773 scrolled => (new CFClient::UI::Table),
2577 @_, 2774 @_,
2578 ); 2775 );
2579 2776
2580 $self 2777 $self
2581} 2778}
2584 my ($self, $items) = @_; 2781 my ($self, $items) = @_;
2585 2782
2586 $self->{scrolled}->clear; 2783 $self->{scrolled}->clear;
2587 return unless $items; 2784 return unless $items;
2588 2785
2589 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2786 my @items = sort {
2787 ($a->{type} <=> $b->{type})
2788 or ($a->{name} cmp $b->{name})
2789 } @$items;
2590 2790
2591 $self->{real_items} = \@items; 2791 $self->{real_items} = \@items;
2592 2792
2593 for my $item (@items) { 2793 for my $item (@items) {
2594 my $desc = $item->{nrof} < 2 2794 $item->{item} = $item;
2595 ? $item->{name}
2596 : "$item->{nrof} $item->{name_pl}";
2597
2598 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2795 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2796 $item->update_item ();
2797 }
2798
2799 my $i = 0;
2800 for (@items) {
2801 $self->{scrolled}->add (0, $i, $_);
2802 my $nrof = $_->{item}->{nrof} || 1;
2803 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2599 } 2804 }
2600 2805
2601# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2806# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2602} 2807}
2603 2808
2678 } 2883 }
2679} 2884}
2680 2885
2681############################################################################# 2886#############################################################################
2682 2887
2888package CFClient::UI::Statusbox;
2889
2890our @ISA = CFClient::UI::VBox::;
2891
2892sub new {
2893 my $class = shift;
2894
2895 $class->SUPER::new (
2896 fontsize => 0.8,
2897 @_,
2898 )
2899}
2900
2901sub reorder {
2902 my ($self) = @_;
2903 my $NOW = time;
2904
2905 while (my ($k, $v) = each %{ $self->{item} }) {
2906 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2907 }
2908
2909 my @widgets;
2910
2911 my @items = sort {
2912 $a->{pri} <=> $b->{pri}
2913 or $b->{id} <=> $a->{id}
2914 } values %{ $self->{item} };
2915
2916 my $count = 10 + 1;
2917 for my $item (@items) {
2918 last unless --$count;
2919
2920 push @widgets, $item->{label} ||= do {
2921 # TODO: doesn't handle markup well (read as: at all)
2922 my $short = $item->{count} > 1
2923 ? "<b>$item->{count} ×</b> $item->{text}"
2924 : $item->{text};
2925
2926 for ($short) {
2927 s/^\s+//;
2928 s/\s+/ /g;
2929 }
2930
2931 new CFClient::UI::Label
2932 markup => $short,
2933 tooltip => $item->{tooltip},
2934 tooltip_font => $::FONT_PROP,
2935 tooltip_width => 0.67,
2936 fontsize => $item->{fontsize} || $self->{fontsize},
2937 max_w => $::WIDTH * 0.44,
2938 fg => $item->{fg},
2939 can_events => 1,
2940 can_hover => 1
2941 };
2942 }
2943
2944 $self->clear;
2945 $self->SUPER::add (reverse @widgets);
2946}
2947
2948sub add {
2949 my ($self, $text, %arg) = @_;
2950
2951 $text =~ s/^\s+//;
2952 $text =~ s/\s+$//;
2953
2954 my $timeout = time + ((delete $arg{timeout}) || 60);
2955
2956 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2957
2958 if (my $item = $self->{item}{$group}) {
2959 if ($item->{text} eq $text) {
2960 $item->{count}++;
2961 } else {
2962 $item->{count} = 1;
2963 $item->{text} = $item->{tooltip} = $text;
2964 }
2965 $item->{id} = ++$self->{id};
2966 $item->{timeout} = $timeout;
2967 delete $item->{label};
2968 } else {
2969 $self->{item}{$group} = {
2970 id => ++$self->{id},
2971 text => $text,
2972 timeout => $timeout,
2973 tooltip => $text,
2974 fg => [0.8, 0.8, 0.8, 0.8],
2975 pri => 0,
2976 count => 1,
2977 %arg,
2978 };
2979 }
2980
2981 $self->reorder;
2982}
2983
2984sub reconfigure {
2985 my ($self) = @_;
2986
2987 delete $_->{label}
2988 for values %{ $self->{item} || {} };
2989
2990 $self->reorder;
2991 $self->SUPER::reconfigure;
2992}
2993
2994#############################################################################
2995
2683package CFClient::UI::Root; 2996package CFClient::UI::Root;
2684 2997
2685our @ISA = CFClient::UI::Container::; 2998our @ISA = CFClient::UI::Container::;
2686 2999
2687use CFClient::OpenGL; 3000use CFClient::OpenGL;
2688 3001
3002sub new {
3003 my $class = shift;
3004
3005 $class->SUPER::new (
3006 @_,
3007 )
3008}
3009
3010sub configure {
3011 my ($self, $x, $y, $w, $h) = @_;
3012
3013 $self->{w} = $w;
3014 $self->{h} = $h;
3015}
3016
2689sub check_size { 3017sub check_size {
2690 my ($self) = @_; 3018 my ($self) = @_;
2691 3019
2692 $self->configure (0, 0, $self->{w}, $self->{h}); 3020 $self->size_allocate ($self->{w}, $self->{h})
3021 if $self->{w};
2693} 3022}
2694 3023
2695sub size_request { 3024sub size_request {
2696 my ($self) = @_; 3025 my ($self) = @_;
2697 3026
2699} 3028}
2700 3029
2701sub size_allocate { 3030sub size_allocate {
2702 my ($self, $w, $h) = @_; 3031 my ($self, $w, $h) = @_;
2703 3032
2704 my $old_w = $self->{old_w}; 3033 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2705 my $old_h = $self->{old_h}; 3034 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2706 3035
2707 if ($old_w && $old_h) { 3036 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2708 for my $child ($self->children) { 3037 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2709 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2710 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2711 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2712 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2713 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2714 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2715 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2716 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2717 }
2718 }
2719
2720 $self->{old_w} = $w;
2721 $self->{old_h} = $h;
2722}
2723
2724sub configure {
2725 my ($self, $x, $y, $w, $h) = @_;
2726
2727 $self->SUPER::configure ($x, $y, $w, $h);
2728 3038
2729 for my $child ($self->children) { 3039 for my $child ($self->children) {
2730 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3040 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2731 3041
3042 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3043 if exists $child->{req_x};
3044
3045 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3046 if exists $child->{req_y};
3047
2732 $X = List::Util::max 0, List::Util::min $w - $W, $X; 3048 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2733 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 3049 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3050
2734 $child->configure ($X, $Y, $W, $H); 3051 $child->configure ($X, $Y, $W, $H);
2735 } 3052 }
2736} 3053}
2737 3054
2738sub _topleft { 3055sub coord2local {
2739 my ($self, $x, $y) = @_; 3056 my ($self, $x, $y) = @_;
2740 3057
2741 ($x, $y) 3058 ($x, $y)
2742} 3059}
2743 3060
3061sub coord2global {
3062 my ($self, $x, $y) = @_;
3063
3064 ($x, $y)
3065}
3066
2744sub update { 3067sub update {
2745 my ($self) = @_; 3068 my ($self) = @_;
2746 3069
2747 $self->check_size; 3070 $self->check_size;
2748 ::refresh (); 3071 $::WANT_REFRESH++;
2749} 3072}
2750 3073
2751sub add { 3074sub add {
2752 my ($self, $child) = @_; 3075 my ($self, @children) = @_;
2753 3076
3077 for my $child (@children) {
3078 $child->{toplevel} = 1;
3079
2754 # integerize window positions 3080 # integerise window positions
2755 $child->{x} = int $child->{x}; 3081 $child->{x} = int $child->{x};
2756 $child->{y} = int $child->{y}; 3082 $child->{y} = int $child->{y};
3083 }
2757 3084
2758 $self->SUPER::add ($child); 3085 $self->SUPER::add (@children);
3086
3087 while (@children) {
3088 my $w = pop @children;
3089 push @children, $w->children;
3090 $w->{visible} = 1;
3091 }
3092}
3093
3094sub remove {
3095 my ($self, @children) = @_;
3096
3097 $self->SUPER::remove (@children);
3098
3099 while (@children) {
3100 my $w = pop @children;
3101 push @children, $w->children;
3102 delete $w->{visible};
3103 }
2759} 3104}
2760 3105
2761sub on_refresh { 3106sub on_refresh {
2762 my ($self, $id, $cb) = @_; 3107 my ($self, $id, $cb) = @_;
2763 3108
2764 $self->{refresh_hook}{$id} = $cb; 3109 $self->{refresh_hook}{$id} = $cb;
2765} 3110}
2766 3111
3112sub on_post_alloc {
3113 my ($self, $id, $cb) = @_;
3114
3115 $self->{post_alloc_hook}{$id} = $cb;
3116}
3117
2767sub draw { 3118sub draw {
2768 my ($self) = @_; 3119 my ($self) = @_;
2769 3120
2770 while (my $rcb = delete $self->{refresh_hook}) { 3121 while ($self->{refresh_hook}) {
2771 $_->() for values %$rcb; 3122 $_->()
3123 for values %{delete $self->{refresh_hook}};
3124 }
3125
3126 if ($self->{check_size}) {
3127 my @queue = ([], []);
3128
3129 for (;;) {
3130 if ($self->{check_size}) {
3131 # heuristic: check containers last
3132 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
3133 for values %{delete $self->{check_size}}
3134 }
3135
3136 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
3137
3138 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
3139 ? @$widget{qw(user_w user_h)}
3140 : $widget->size_request;
3141
3142 if (delete $widget->{force_alloc}
3143 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3144 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3145
3146 $widget->{req_w} = $w;
3147 $widget->{req_h} = $h;
3148
3149 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3150
3151 $widget->{parent}->check_size
3152 if $widget->{parent};
3153 }
3154 }
3155 }
3156
3157 while ($self->{size_alloc}) {
3158 for (values %{delete $self->{size_alloc}}) {
3159 my ($widget, $w, $h) = @$_;
3160
3161 $w = 0 if $w < 0;
3162 $h = 0 if $h < 0;
3163
3164 $widget->{w} = $w;
3165 $widget->{h} = $h;
3166 $widget->size_allocate ($w, $h);
3167 $widget->emit (size_allocate => $w, $h);
3168 }
3169 }
3170
3171 while ($self->{post_alloc_hook}) {
3172 $_->()
3173 for values %{delete $self->{post_alloc_hook}};
2772 } 3174 }
2773 3175
2774 glViewport 0, 0, $::WIDTH, $::HEIGHT; 3176 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2775 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3177 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2776 glClear GL_COLOR_BUFFER_BIT; 3178 glClear GL_COLOR_BUFFER_BIT;
2777 3179
2778 glMatrixMode GL_PROJECTION; 3180 glMatrixMode GL_PROJECTION;
2779 glLoadIdentity; 3181 glLoadIdentity;
2780 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3182 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2781 glMatrixMode GL_MODELVIEW; 3183 glMatrixMode GL_MODELVIEW;
2782 glLoadIdentity; 3184 glLoadIdentity;
2783 3185
2784 $self->_draw; 3186 $self->_draw;
2785} 3187}
2787############################################################################# 3189#############################################################################
2788 3190
2789package CFClient::UI; 3191package CFClient::UI;
2790 3192
2791$ROOT = new CFClient::UI::Root; 3193$ROOT = new CFClient::UI::Root;
2792$TOOLTIP = new CFClient::UI::Tooltip; 3194$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2793 3195
27941 31961
2795 3197

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines