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.165 by root, Mon Apr 24 08:22:22 2006 UTC vs.
Revision 1.219 by root, Sun May 21 00:34:59 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use utf8;
3use strict; 4use strict;
4 5
5use Scalar::Util (); 6use Scalar::Util ();
6use List::Util (); 7use List::Util ();
7 8
10our ($FOCUS, $HOVER, $GRAB); # various widgets 11our ($FOCUS, $HOVER, $GRAB); # various widgets
11 12
12our $ROOT; 13our $ROOT;
13our $TOOLTIP; 14our $TOOLTIP;
14our $BUTTON_STATE; 15our $BUTTON_STATE;
16
17our %WIDGET; # all widgets, weak-referenced
15 18
16sub check_tooltip { 19sub check_tooltip {
17 if (!$GRAB) { 20 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (length $widget->{tooltip}) { 22 if (length $widget->{tooltip}) {
23 26
24 my $tip = $widget->{tooltip}; 27 my $tip = $widget->{tooltip};
25 28
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 29 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 30
28 $TOOLTIP->set_markup ($widget->{tooltip}); 31 $TOOLTIP->set_tooltip_from ($widget);
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show; 32 $TOOLTIP->show;
33
34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
35
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 if $x + $TOOLTIP->{w} > $::WIDTH;
38
39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
31 } 42 }
32 43
33 return; 44 return;
34 } 45 }
35 } 46 }
122 133
123 for (@$vals) { 134 for (@$vals) {
124 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
125 $rem += $_ - $i; 136 $rem += $_ - $i;
126 $_ = $i; 137 $_ = $i;
138 }
139}
140
141# call when resolution changes etc.
142sub rescale_widgets {
143 my ($sx, $sy) = @_;
144
145 # make a copy, otherwise for complains about freed values.
146 my @widgets = values %WIDGET;
147
148 for my $widget (@widgets) {
149 if ($widget->{toplevel}) {
150 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
151 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
152 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
153 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
154 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
155 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
156 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
157 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
158 }
159
160 $widget->reconfigure;
127 } 161 }
128} 162}
129 163
130############################################################################# 164#############################################################################
131 165
150 if (/^connect_(.*)$/) { 184 if (/^connect_(.*)$/) {
151 $self->connect ($1 => delete $self->{$_}); 185 $self->connect ($1 => delete $self->{$_});
152 } 186 }
153 } 187 }
154 188
189 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
190
155 $self 191 $self
156} 192}
157 193
158sub destroy { 194sub destroy {
159 my ($self) = @_; 195 my ($self) = @_;
224 $self->{y} = $y; 260 $self->{y} = $y;
225 $self->update; 261 $self->update;
226 } 262 }
227 263
228 if ($self->{w} != $w || $self->{h} != $h) { 264 if ($self->{w} != $w || $self->{h} != $h) {
229 $self->{w} = $w; 265 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
230 $self->{h} = $h;
231
232 $self->size_allocate ($w, $h);
233 $self->update;
234 } 266 }
235} 267}
236 268
237sub size_allocate { 269sub size_allocate {
238 # nothing to be done 270 # nothing to be done
239} 271}
240 272
273sub reconfigure {
274 my ($self) = @_;
275
276 $self->check_size (1);
277 $self->update;
278}
279
280sub children {
281}
282
241sub set_max_size { 283sub set_max_size {
242 my ($self, $w, $h) = @_; 284 my ($self, $w, $h) = @_;
243 285
244 delete $self->{max_w}; $self->{max_w} = $w if $w; 286 delete $self->{max_w}; $self->{max_w} = $w if $w;
245 delete $self->{max_h}; $self->{max_h} = $h if $h; 287 delete $self->{max_h}; $self->{max_h} = $h if $h;
246} 288}
247 289
248# return top left coordinates 290sub set_tooltip {
249sub _topleft {
250 my ($self, $x, $y) = @_; 291 my ($self, $tooltip) = @_;
251 292
252 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 293 $self->{tooltip} = $tooltip;
294
295 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
296 delete $CFClient::UI::TOOLTIP->{owner};
297 CFClient::UI::check_tooltip;
298 }
253} 299}
254 300
255# translate global coordinates to local coordinate system 301# translate global coordinates to local coordinate system
256sub coord2local { 302sub coord2local {
257 my ($self, $x, $y) = @_; 303 my ($self, $x, $y) = @_;
258 304
259 my ($X, $Y) = $self->_topleft; 305 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
260 ($x - $X, $y - $Y)
261} 306}
262 307
263# translate local coordinates to global coordinate system 308# translate local coordinates to global coordinate system
264sub coord2global { 309sub coord2global {
265 my ($self, $x, $y) = @_; 310 my ($self, $x, $y) = @_;
266 311
267 my ($X, $Y) = $self->_topleft; 312 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
268 ($x + $X, $y + $Y)
269} 313}
270 314
271sub focus_in { 315sub focus_in {
272 my ($self) = @_; 316 my ($self) = @_;
273 317
345 glVertex $self->{w}, 0; 389 glVertex $self->{w}, 0;
346 glVertex $self->{w}, $self->{h}; 390 glVertex $self->{w}, $self->{h};
347 glVertex 0 , $self->{h}; 391 glVertex 0 , $self->{h};
348 glEnd; 392 glEnd;
349 glPopMatrix; 393 glPopMatrix;
350 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;
351 } 395 }
352} 396}
353 397
354sub _draw { 398sub _draw {
355 my ($self) = @_; 399 my ($self) = @_;
371 415
372sub set_parent { 416sub set_parent {
373 my ($self, $parent) = @_; 417 my ($self, $parent) = @_;
374 418
375 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};
376} 424}
377 425
378sub check_size { 426sub check_size {
379 my ($self) = @_; 427 my ($self, $forced) = @_;
380 428
381 $self->{parent} 429 $self->{force_alloc} = 1 if $forced;
382 or return 1; 430 $CFClient::UI::ROOT->{check_size}{$self} = $self;
383
384 my ($w, $h) = $self->{user_w} && $self->{user_h}
385 ? @$self{qw(user_w user_h)}
386 : $self->size_request;
387
388 if ($w != $self->{req_w} || $h != $self->{req_h}) {
389 $self->{req_w} = $w;
390 $self->{req_h} = $h;
391
392 $self->{parent}->check_size
393 or $self->size_allocate (
394 (List::Util::max $self->{w}, $w),
395 (List::Util::max $self->{h}, $h),
396 );
397
398 1
399 } else {
400 0
401 }
402} 431}
403 432
404sub update { 433sub update {
405 my ($self) = @_; 434 my ($self) = @_;
406 435
421} 450}
422 451
423sub DESTROY { 452sub DESTROY {
424 my ($self) = @_; 453 my ($self) = @_;
425 454
455 delete $WIDGET{$self+0};
426 #$self->deactivate; 456 #$self->deactivate;
427} 457}
428 458
429############################################################################# 459#############################################################################
430 460
439 my $class = shift; 469 my $class = shift;
440 470
441 # range [value, low, high, page] 471 # range [value, low, high, page]
442 472
443 $class->SUPER::new ( 473 $class->SUPER::new (
444 bg => [0, 0, 0, 0.2], 474 #bg => [0, 0, 0, 0.2],
445 active_bg => [1, 1, 1, 0.5], 475 #active_bg => [1, 1, 1, 0.5],
446 @_ 476 @_
447 ) 477 )
448} 478}
449 479
450sub _draw { 480sub _draw {
451 my ($self) = @_; 481 my ($self) = @_;
452 482
483 my $color = $FOCUS == $self && $self->{active_bg}
484 ? $self->{active_bg}
485 : $self->{bg};
486
487 if ($color && (@$color < 4 || $color->[3])) {
453 my ($w, $h) = @$self{qw(w h)}; 488 my ($w, $h) = @$self{qw(w h)};
454 489
455 glEnable GL_BLEND; 490 glEnable GL_BLEND;
456 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
457 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 492 glColor @$color;
458 493
459 glBegin GL_QUADS; 494 glBegin GL_QUADS;
460 glVertex 0 , 0; 495 glVertex 0 , 0;
461 glVertex 0 , $h; 496 glVertex 0 , $h;
462 glVertex $w, $h; 497 glVertex $w, $h;
463 glVertex $w, 0; 498 glVertex $w, 0;
464 glEnd; 499 glEnd;
465 500
466 glDisable GL_BLEND; 501 glDisable GL_BLEND;
502 }
467} 503}
468 504
469############################################################################# 505#############################################################################
470 506
471package CFClient::UI::Empty; 507package CFClient::UI::Empty;
492sub new { 528sub new {
493 my ($class, %arg) = @_; 529 my ($class, %arg) = @_;
494 530
495 my $children = delete $arg{children} || []; 531 my $children = delete $arg{children} || [];
496 532
497 my $self = $class->SUPER::new (children => [], can_events => 0, %arg); 533 my $self = $class->SUPER::new (
534 children => [],
535 can_events => 0,
536 %arg,
537 );
498 $self->add ($_) for @$children; 538 $self->add ($_) for @$children;
499 539
500 $self 540 $self
501} 541}
502 542
503sub add { 543sub add {
504 my ($self, $child) = @_; 544 my ($self, @widgets) = @_;
505 545
506 $child->set_parent ($self); 546 $_->set_parent ($self)
547 for @widgets;
507 548
508 use sort 'stable'; 549 use sort 'stable';
509 550
510 $self->{children} = [ 551 $self->{children} = [
511 sort { $a->{z} <=> $b->{z} } 552 sort { $a->{z} <=> $b->{z} }
512 @{$self->{children}}, $child 553 @{$self->{children}}, @widgets
513 ]; 554 ];
514 555
515 $child->check_size; 556 $self->check_size (1);
557 $self->update;
558}
559
560sub children {
561 @{ $_[0]{children} }
516} 562}
517 563
518sub remove { 564sub remove {
519 my ($self, $child) = @_; 565 my ($self, $child) = @_;
520 566
535 581
536 for (@$children) { 582 for (@$children) {
537 delete $_->{parent}; 583 delete $_->{parent};
538 $_->hide; 584 $_->hide;
539 } 585 }
586
587 $self->check_size;
588 $self->update;
540} 589}
541 590
542sub find_widget { 591sub find_widget {
543 my ($self, $x, $y) = @_; 592 my ($self, $x, $y) = @_;
544 593
619} 668}
620 669
621sub update { 670sub update {
622 my ($self) = @_; 671 my ($self) = @_;
623 672
624 # we want to do this delayed... 673 $ROOT->on_post_alloc ($self => sub { $self->render_child });
625 $self->render_chld;
626 $self->SUPER::update; 674 $self->SUPER::update;
627}
628
629sub render_chld {
630 my ($self) = @_;
631
632 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
633 glClearColor 0, 0, 0, 1;
634 glClear GL_COLOR_BUFFER_BIT;
635 $self->child->draw;
636 };
637} 675}
638 676
639sub size_allocate { 677sub size_allocate {
640 my ($self, $w, $h) = @_; 678 my ($self, $w, $h) = @_;
641 679
642 $self->child->configure (0, 0, $w, $h); 680 $self->SUPER::size_allocate ($w, $h);
681 $self->update;
682}
643 683
684sub _render {
685 $_[0]{children}[0]->draw;
686}
687
688sub render_child {
689 my ($self) = @_;
690
691 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
692 glClearColor 0, 0, 0, 0;
693 glClear GL_COLOR_BUFFER_BIT;
694
644 $self->render_chld; 695 $self->_render;
696 };
645} 697}
646 698
647sub _draw { 699sub _draw {
648 my ($self) = @_; 700 my ($self) = @_;
649 701
650 my ($w, $h) = ($self->w, $self->h); 702 my ($w, $h) = ($self->w, $self->h);
651 703
652 my $tex = $self->{texture} 704 my $tex = $self->{texture}
653 or return; 705 or return;
654 706
655 glEnable GL_BLEND;
656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
657 glEnable GL_TEXTURE_2D; 707 glEnable GL_TEXTURE_2D;
658 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 708 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
709 glColor 1, 1, 1, 1;
659 710
660 $tex->draw_quad (0, 0, $w, $h); 711 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
661 712
662 glDisable GL_BLEND;
663 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
664} 714}
665 715
666############################################################################# 716#############################################################################
667 717
668package CFClient::UI::ViewPort; 718package CFClient::UI::ViewPort;
669 719
670our @ISA = CFClient::UI::Window::; 720our @ISA = CFClient::UI::Window::;
671 721
672sub new { die }
673
674sub size_request { 722sub size_request {
675 my ($self) = @_; 723 my ($self) = @_;
676 724
677 @$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)};
678 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 726 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
679 727
680 @$self{qw(child_w child_h)} 728 @$self{qw(child_w child_h)}
681} 729}
682 730
683sub _draw { 731sub size_allocate {
684 my ($self) = @_; 732 my ($self, $w, $h) = @_;
685 733
686 $self->{children}[1]->draw; 734 $self->update;
687} 735}
688 736
737sub set_offset {
738 my ($self, $x, $y) = @_;
689 739
690############################################################################# 740 $self->{view_x} = int $x;
741 $self->{view_y} = int $y;
691 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
692package CFClient::UI::Frame; 784package CFClient::UI::ScrolledWindow;
693 785
694our @ISA = CFClient::UI::Bin::; 786our @ISA = CFClient::UI::HBox::;
695
696use CFClient::OpenGL;
697 787
698sub new { 788sub new {
699 my $class = shift; 789 my $class = shift;
700 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
701 my $self = $class->SUPER::new ( 801 $self = $class->SUPER::new (
702 bg => [1, 1, 1, 1], 802 vp => (new CFClient::UI::ViewPort expand => 1),
703 border_bg => [1, 1, 1, 1], 803 slider => $slider,
704 border => 0.8,
705 @_ 804 @_,
706 ); 805 );
707 806
807 $self->{vp}->add ($self->{scrolled});
808 $self->add ($self->{vp});
809 $self->add ($self->{slider});
810
708 $self 811 $self
709} 812}
710 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
711sub _draw { 834sub _draw {
712 my ($self) = @_; 835 my ($self) = @_;
713 836
714 my ($w, $h) = ($self->{w}, $self->{h}); 837 if ($self->{bg}) {
838 my ($w, $h) = @$self{qw(w h)};
715 839
716 glEnable GL_BLEND; 840 glEnable GL_BLEND;
717 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 841 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
718 glEnable GL_TEXTURE_2D; 842 glColor @{ $self->{bg} };
719 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
720 843
721# glBegin GL_QUADS; 844 glBegin GL_QUADS;
722# glColor 0, 0, 0, 0;
723# glVertex 0 , 0; 845 glVertex 0 , 0;
724# glVertex 0 , $h; 846 glVertex 0 , $h;
725# glVertex $w, $h; 847 glVertex $w, $h;
726# glVertex $w, 0; 848 glVertex $w, 0;
727# glEnd; 849 glEnd;
728 850
729
730 $self->child->draw;
731 glDisable GL_BLEND; 851 glDisable GL_BLEND;
732 glDisable GL_TEXTURE_2D; 852 }
853
854 $self->SUPER::_draw;
733} 855}
734 856
735############################################################################# 857#############################################################################
736 858
737package CFClient::UI::FancyFrame; 859package CFClient::UI::FancyFrame;
750 # TODO: user_x, user_y, overwrite moveto? 872 # TODO: user_x, user_y, overwrite moveto?
751 873
752 my $self = $class->SUPER::new ( 874 my $self = $class->SUPER::new (
753 bg => [1, 1, 1, 1], 875 bg => [1, 1, 1, 1],
754 border_bg => [1, 1, 1, 1], 876 border_bg => [1, 1, 1, 1],
755 border => 0.8, 877 border => 0.6,
878 toplevel => 1,
879 can_events => 1,
756 @_ 880 @_
757 ); 881 );
758 882
759 $self->{title} &&= new CFClient::UI::Label 883 $self->{title} &&= new CFClient::UI::Label
760 align => 0, 884 align => 0,
761 valign => 1, 885 valign => 1,
762 text => $self->{title}, 886 text => $self->{title},
763 fontsize => 1; 887 fontsize => $self->{border};
764 888
765 $self 889 $self
766} 890}
767 891
768sub border { 892sub border {
784 my ($self, $w, $h) = @_; 908 my ($self, $w, $h) = @_;
785 909
786 $h -= List::Util::max 0, $self->border * 2; 910 $h -= List::Util::max 0, $self->border * 2;
787 $w -= List::Util::max 0, $self->border * 2; 911 $w -= List::Util::max 0, $self->border * 2;
788 912
789 $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)
790 if $self->{title}; 914 if $self->{title};
791 915
792 $self->child->configure ($self->border, $self->border, $w, $h); 916 $self->child->configure ($self->border, $self->border, $w, $h);
793} 917}
794 918
795sub button_down { 919sub button_down {
796 my ($self, $ev, $x, $y) = @_; 920 my ($self, $ev, $x, $y) = @_;
797 921
922 my ($w, $h) = @$self{qw(w h)};
798 my $border = $self->border; 923 my $border = $self->border;
799 924
800 if ($x < $self->{w} && $x >= $self->{w} - $border 925 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
801 && $y < $self->{h} && $y >= $self->{h} - $border) { 926 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
802 927
928 if ($lr & $td) {
929 my ($wx, $wy) = ($self->{x}, $self->{y});
803 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 930 my ($ox, $oy) = ($ev->{x}, $ev->{y});
804 my ($bw, $bh) = ($self->{w}, $self->{h}); 931 my ($bw, $bh) = ($self->{w}, $self->{h});
805 932
933 my $mx = $x < $border;
934 my $my = $y < $border;
935
806 $self->{motion} = sub { 936 $self->{motion} = sub {
807 my ($ev, $x, $y) = @_; 937 my ($ev, $x, $y) = @_;
808 938
809 ($x, $y) = ($ev->{x}, $ev->{y}); 939 my $dx = $ev->{x} - $ox;
940 my $dy = $ev->{y} - $oy;
810 941
811 $self->{user_w} = $bw + $x - $ox; 942 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
812 $self->{user_h} = $bh + $y - $oy; 943 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
944 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
813 $self->check_size; 945 $self->check_size;
814 }; 946 };
815 947
816 } elsif ($x >= 0 && $x < $self->{w} 948 } elsif ($lr ^ $td) {
817 && $y >= 0 && $y < $border) {
818
819 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 949 my ($ox, $oy) = ($ev->{x}, $ev->{y});
820 my ($bx, $by) = ($self->{x}, $self->{y}); 950 my ($bx, $by) = ($self->{x}, $self->{y});
821 951
822 $self->{motion} = sub { 952 $self->{motion} = sub {
823 my ($ev, $x, $y) = @_; 953 my ($ev, $x, $y) = @_;
846 my ($self) = @_; 976 my ($self) = @_;
847 977
848 my ($w, $h ) = ($self->{w}, $self->{h}); 978 my ($w, $h ) = ($self->{w}, $self->{h});
849 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 979 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
850 980
851 glEnable GL_BLEND;
852 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
853 glEnable GL_TEXTURE_2D; 981 glEnable GL_TEXTURE_2D;
854 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 982 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
855 983
856 my $border = $self->border; 984 my $border = $self->border;
857 985
858 glColor @{ $self->{border_bg} }; 986 glColor @{ $self->{border_bg} };
859 $tex[1]->draw_quad (0, 0, $w, $border); 987 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
860 $tex[3]->draw_quad (0, $border, $border, $ch); 988 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
861 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 989 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
862 $tex[4]->draw_quad (0, $h - $border, $w, $border); 990 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
863 991
992 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
864 my $bg = $tex[0]; 993 my $bg = $tex[0];
865 994
866 # TODO: repeat texture not scale 995 # TODO: repeat texture not scale
867 my $rep_x = $cw / $bg->{w}; 996 my $rep_x = $cw / $bg->{w};
868 my $rep_y = $ch / $bg->{h}; 997 my $rep_y = $ch / $bg->{h};
869 998
870 glColor @{ $self->{bg} }; 999 glColor @{ $self->{bg} };
871 1000
872 $bg->{s} = $rep_x; 1001 $bg->{s} = $rep_x;
873 $bg->{t} = $rep_y; 1002 $bg->{t} = $rep_y;
874 $bg->{wrap_mode} = 1; 1003 $bg->{wrap_mode} = 1;
875 $bg->draw_quad ($border, $border, $cw, $ch); 1004 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1005 }
876 1006
877 glDisable GL_TEXTURE_2D; 1007 glDisable GL_TEXTURE_2D;
878 glDisable GL_BLEND;
879 1008
880 $self->{title}->draw if $self->{title}; 1009 $self->{title}->draw if $self->{title};
1010
881 $self->child->draw; 1011 $self->child->draw;
882} 1012}
883 1013
884############################################################################# 1014#############################################################################
885 1015
907 $self->{children}[$y][$x] = $child; 1037 $self->{children}[$y][$x] = $child;
908 1038
909 $child->check_size; 1039 $child->check_size;
910} 1040}
911 1041
1042sub children {
1043 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1044}
1045
912# TODO: move to container class maybe? send childs a signal on removal? 1046# TODO: move to container class maybe? send childs a signal on removal?
913sub clear { 1047sub clear {
914 my ($self) = @_; 1048 my ($self) = @_;
915 1049
916 my $children = delete $self->{children}; 1050 my @children = $self->children;
1051 delete $self->{children};
917 1052
918 for (grep $_, map @$_, grep $_, @$children) { 1053 for (@children) {
919 delete $_->{parent}; 1054 delete $_->{parent};
920 $_->hide; 1055 $_->hide;
921 } 1056 }
922 1057
923 $self->update; 1058 $self->update;
1104} 1239}
1105 1240
1106sub size_allocate { 1241sub size_allocate {
1107 my ($self, $w, $h) = @_; 1242 my ($self, $w, $h) = @_;
1108 1243
1244 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1245
1109 my $children = $self->{children}; 1246 my $children = $self->{children};
1110 1247
1111 my @h = map $_->{req_h}, @$children; 1248 my @h = map $_->{req_h}, @$children;
1112 1249
1113 my $req_h = List::Util::sum @h; 1250 my $req_h = List::Util::sum @h;
1142 1279
1143############################################################################# 1280#############################################################################
1144 1281
1145package CFClient::UI::Label; 1282package CFClient::UI::Label;
1146 1283
1147our @ISA = CFClient::UI::Base::; 1284our @ISA = CFClient::UI::DrawBG::;
1148 1285
1149use CFClient::OpenGL; 1286use CFClient::OpenGL;
1150 1287
1151sub new { 1288sub new {
1152 my ($class, %arg) = @_; 1289 my ($class, %arg) = @_;
1153 1290
1154 my $self = $class->SUPER::new ( 1291 my $self = $class->SUPER::new (
1155 fg => [1, 1, 1], 1292 fg => [1, 1, 1],
1293 #bg => none
1294 #active_bg => none
1156 #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),
1157 fontsize => 1, 1301 fontsize => 1,
1158 text => "",
1159 align => -1, 1302 align => -1,
1160 valign => -1, 1303 valign => -1,
1161 padding => 2, 1304 padding => 2,
1162 layout => new CFClient::Layout,
1163 can_events => 0, 1305 can_events => 0,
1164 %arg 1306 %arg
1165 ); 1307 );
1166 1308
1167 if (exists $self->{template}) { 1309 if (exists $self->{template}) {
1168 my $layout = new CFClient::Layout; 1310 my $layout = new CFClient::Layout;
1169 $layout->set_text (delete $self->{template}); 1311 $layout->set_text (delete $self->{template});
1170 $self->{template} = $layout; 1312 $self->{template} = $layout;
1171 } 1313 }
1172 1314
1173 $self->set_text (delete $self->{text}) if exists $self->{text}; 1315 if (exists $self->{markup}) {
1174 $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 }
1175 1320
1176 $self 1321 $self
1177} 1322}
1178 1323
1179sub escape { 1324sub escape($) {
1180 local $_ = $_[1]; 1325 local $_ = $_[0];
1181 1326
1182 s/&/&amp;/g; 1327 s/&/&amp;/g;
1183 s/>/&gt;/g; 1328 s/>/&gt;/g;
1184 s/</&lt;/g; 1329 s/</&lt;/g;
1185 1330
1186 $_[1] 1331 $_
1332}
1333
1334sub update {
1335 my ($self) = @_;
1336
1337 delete $self->{texture};
1338 $self->SUPER::update;
1187} 1339}
1188 1340
1189sub set_text { 1341sub set_text {
1190 my ($self, $text) = @_; 1342 my ($self, $text) = @_;
1191 1343
1344 return if $self->{text} eq "T$text";
1345 $self->{text} = "T$text";
1346
1347 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1192 $self->{layout}->set_text ($text); 1348 $self->{layout}->set_text ($text);
1193 1349
1194 delete $self->{texture}; 1350 $self->update;
1195 $self->check_size; 1351 $self->check_size;
1196 $self->update;
1197} 1352}
1198 1353
1199sub set_markup { 1354sub set_markup {
1200 my ($self, $markup) = @_; 1355 my ($self, $markup) = @_;
1201 1356
1357 return if $self->{text} eq "M$markup";
1358 $self->{text} = "M$markup";
1359
1360 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1361
1362 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1202 $self->{layout}->set_markup ($markup); 1363 $self->{layout}->set_markup ($markup);
1203 1364
1204 delete $self->{texture}; 1365 $self->update;
1205 $self->check_size; 1366 $self->check_size;
1206 $self->update;
1207} 1367}
1208 1368
1209sub size_request { 1369sub size_request {
1210 my ($self) = @_; 1370 my ($self) = @_;
1211 1371
1212 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1372 $self->{layout}->set_font ($self->{font}) if $self->{font};
1213 $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});
1214 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1376 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1215 1377
1216 my ($w, $h) = $self->{layout}->size; 1378 my ($w, $h) = $self->{layout}->size;
1217 1379
1218 if (exists $self->{template}) { 1380 if (exists $self->{template}) {
1240sub set_fontsize { 1402sub set_fontsize {
1241 my ($self, $fontsize) = @_; 1403 my ($self, $fontsize) = @_;
1242 1404
1243 $self->{fontsize} = $fontsize; 1405 $self->{fontsize} = $fontsize;
1244 delete $self->{texture}; 1406 delete $self->{texture};
1407
1408 $self->update;
1245 $self->check_size; 1409 $self->check_size;
1246 $self->update;
1247} 1410}
1248 1411
1249sub _draw { 1412sub _draw {
1250 my ($self) = @_; 1413 my ($self) = @_;
1251 1414
1415 $self->SUPER::_draw; # draw background, if applicable
1416
1252 my $tex = $self->{texture} ||= do { 1417 my $tex = $self->{texture} ||= do {
1418 $self->{layout}->set_foreground (@{$self->{fg}});
1253 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1419 $self->{layout}->set_font ($self->{font}) if $self->{font};
1254 $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});
1255 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1423 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1424
1256 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
1257 }; 1436 };
1258 1437
1259 glEnable GL_BLEND;
1260 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1261 glEnable GL_TEXTURE_2D; 1438 glEnable GL_TEXTURE_2D;
1262 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1439 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1263 1440
1441 if ($tex->{format} == GL_ALPHA) {
1264 glColor @{$self->{fg}}; 1442 glColor @{$self->{fg}};
1265
1266 $self->{ox} = int (
1267 $self->{align} < 0 ? $self->{padding}
1268 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1269 : ($self->{w} - $tex->{w}) * 0.5
1270 );
1271
1272 $self->{oy} = int (
1273 $self->{valign} < 0 ? $self->{padding}
1274 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1275 : ($self->{h} - $tex->{h}) * 0.5
1276 );
1277
1278 $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 }
1279 1447
1280 glDisable GL_TEXTURE_2D; 1448 glDisable GL_TEXTURE_2D;
1281 glDisable GL_BLEND;
1282} 1449}
1283 1450
1284############################################################################# 1451#############################################################################
1285 1452
1286package CFClient::UI::EntryBase; 1453package CFClient::UI::EntryBase;
1321 $self->{layout}->set_text ("$text "); 1488 $self->{layout}->set_text ("$text ");
1322 1489
1323 $self->emit (changed => $self->{text}); 1490 $self->emit (changed => $self->{text});
1324} 1491}
1325 1492
1326sub get_text {
1327 $_[0]{text}
1328}
1329
1330sub size_request {
1331 my ($self) = @_;
1332
1333 my ($w, $h) = $self->SUPER::size_request;
1334
1335 ($w + 1, $h) # add 1 for cursor
1336}
1337
1338sub size_allocate {
1339 my ($self, $w, $h) = @_;
1340
1341 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1342}
1343
1344sub set_text { 1493sub set_text {
1345 my ($self, $text) = @_; 1494 my ($self, $text) = @_;
1346 1495
1347 $self->{cursor} = length $text; 1496 $self->{cursor} = length $text;
1348 $self->_set_text ($text); 1497 $self->_set_text ($text);
1349 $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
1350} 1518}
1351 1519
1352sub key_down { 1520sub key_down {
1353 my ($self, $ev) = @_; 1521 my ($self, $ev) = @_;
1354 1522
1356 my $sym = $ev->{sym}; 1524 my $sym = $ev->{sym};
1357 my $uni = $ev->{unicode}; 1525 my $uni = $ev->{unicode};
1358 1526
1359 my $text = $self->get_text; 1527 my $text = $self->get_text;
1360 1528
1361 if ($sym == 8) { 1529 if ($uni == 8) {
1362 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1530 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1363 } elsif ($sym == 127) { 1531 } elsif ($uni == 127) {
1364 substr $text, $self->{cursor}, 1, ""; 1532 substr $text, $self->{cursor}, 1, "";
1365 } elsif ($sym == CFClient::SDLK_LEFT) { 1533 } elsif ($sym == CFClient::SDLK_LEFT) {
1366 --$self->{cursor} if $self->{cursor}; 1534 --$self->{cursor} if $self->{cursor};
1367 } elsif ($sym == CFClient::SDLK_RIGHT) { 1535 } elsif ($sym == CFClient::SDLK_RIGHT) {
1368 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1536 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1369 } elsif ($sym == CFClient::SDLK_HOME) { 1537 } elsif ($sym == CFClient::SDLK_HOME) {
1370 $self->{cursor} = 0; 1538 $self->{cursor} = 0;
1371 } elsif ($sym == CFClient::SDLK_END) { 1539 } elsif ($sym == CFClient::SDLK_END) {
1372 $self->{cursor} = length $text; 1540 $self->{cursor} = length $text;
1373 } elsif ($sym == 27) { 1541 } elsif ($uni == 27) {
1374 $self->emit ('escape'); 1542 $self->emit ('escape');
1375 } elsif ($uni) { 1543 } elsif ($uni) {
1376 substr $text, $self->{cursor}++, 0, chr $uni; 1544 substr $text, $self->{cursor}++, 0, chr $uni;
1377 } 1545 }
1378 1546
1379 $self->_set_text ($text); 1547 $self->_set_text ($text);
1380 $self->update; 1548 $self->update;
1549 $self->check_size;
1381} 1550}
1382 1551
1383sub focus_in { 1552sub focus_in {
1384 my ($self) = @_; 1553 my ($self) = @_;
1385 1554
1461 my ($self, $ev) = @_; 1630 my ($self, $ev) = @_;
1462 1631
1463 my $sym = $ev->{sym}; 1632 my $sym = $ev->{sym};
1464 1633
1465 if ($sym == 13) { 1634 if ($sym == 13) {
1635 unshift @{$self->{history}},
1636 my $txt = $self->get_text;
1637 $self->{history_pointer} = -1;
1638 $self->{history_saveback} = '';
1466 $self->emit (activate => $self->get_text); 1639 $self->emit (activate => $txt);
1467 $self->update; 1640 $self->update;
1641
1642 } elsif ($sym == CFClient::SDLK_UP) {
1643 if ($self->{history_pointer} < 0) {
1644 $self->{history_saveback} = $self->get_text;
1645 }
1646 if (@{$self->{history} || []} > 0) {
1647 $self->{history_pointer}++;
1648 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1649 $self->{history_pointer} = @{$self->{history} || []} - 1;
1650 }
1651 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1652 }
1653
1654 } elsif ($sym == CFClient::SDLK_DOWN) {
1655 $self->{history_pointer}--;
1656 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1657
1658 if ($self->{history_pointer} >= 0) {
1659 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1660 } else {
1661 $self->set_text ($self->{history_saveback});
1662 }
1468 1663
1469 } else { 1664 } else {
1470 $self->SUPER::key_down ($ev); 1665 $self->SUPER::key_down ($ev);
1471 } 1666 }
1472 1667
1488 my $class = shift; 1683 my $class = shift;
1489 1684
1490 $class->SUPER::new ( 1685 $class->SUPER::new (
1491 padding => 4, 1686 padding => 4,
1492 fg => [1, 1, 1], 1687 fg => [1, 1, 1],
1493 bg => [1, 1, 1, 0.2],
1494 active_fg => [0, 0, 1], 1688 active_fg => [0, 0, 1],
1495 can_hover => 1, 1689 can_hover => 1,
1496 align => 0, 1690 align => 0,
1497 valign => 0, 1691 valign => 0,
1498 can_events => 1, 1692 can_events => 1,
1516 1710
1517 if ($GRAB == $self) { 1711 if ($GRAB == $self) {
1518 $self->{fg} = $self->{active_fg}; 1712 $self->{fg} = $self->{active_fg};
1519 } 1713 }
1520 1714
1521 glEnable GL_BLEND;
1522 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1523 glEnable GL_TEXTURE_2D; 1715 glEnable GL_TEXTURE_2D;
1524 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1525 glColor 0, 0, 0, 1; 1717 glColor 0, 0, 0, 1;
1526 1718
1527 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1719 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1528 1720
1529 glDisable GL_TEXTURE_2D; 1721 glDisable GL_TEXTURE_2D;
1530 glDisable GL_BLEND;
1531 1722
1532 $self->SUPER::_draw; 1723 $self->SUPER::_draw;
1533} 1724}
1534 1725
1535############################################################################# 1726#############################################################################
1549 1740
1550 $class->SUPER::new ( 1741 $class->SUPER::new (
1551 padding => 2, 1742 padding => 2,
1552 fg => [1, 1, 1], 1743 fg => [1, 1, 1],
1553 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],
1554 state => 0, 1747 state => 0,
1555 can_hover => 1, 1748 can_hover => 1,
1556 @_ 1749 @_
1557 ) 1750 )
1558} 1751}
1582 1775
1583 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;
1584 1777
1585 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1778 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1586 1779
1587 glEnable GL_BLEND; 1780 my $tex = $self->{state} ? $tex[1] : $tex[0];
1781
1588 glEnable GL_TEXTURE_2D; 1782 glEnable GL_TEXTURE_2D;
1589 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1590
1591 my $tex = $self->{state} ? $tex[1] : $tex[0];
1592
1593 $tex->draw_quad (0, 0, $s, $s); 1783 $tex->draw_quad_alpha (0, 0, $s, $s);
1594
1595 glDisable GL_TEXTURE_2D; 1784 glDisable GL_TEXTURE_2D;
1596 glDisable GL_BLEND;
1597} 1785}
1598 1786
1599############################################################################# 1787#############################################################################
1600 1788
1601package CFClient::UI::Image; 1789package CFClient::UI::Image;
1644 glTranslate 0, -$self->{w}, 0; 1832 glTranslate 0, -$self->{w}, 0;
1645 1833
1646 ($w, $h) = ($h, $w); 1834 ($w, $h) = ($h, $w);
1647 } 1835 }
1648 1836
1649 glEnable GL_BLEND;
1650 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1651 glEnable GL_TEXTURE_2D; 1837 glEnable GL_TEXTURE_2D;
1652 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1838 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1653 1839
1654 $tex->draw_quad (0, 0, $w, $h); 1840 $tex->draw_quad_alpha (0, 0, $w, $h);
1655 1841
1656 glDisable GL_BLEND;
1657 glDisable GL_TEXTURE_2D; 1842 glDisable GL_TEXTURE_2D;
1658} 1843}
1659 1844
1660############################################################################# 1845#############################################################################
1661 1846
1709} 1894}
1710 1895
1711sub set_max { 1896sub set_max {
1712 my ($self, $max) = @_; 1897 my ($self, $max) = @_;
1713 1898
1899 return if $self->{max_val} == $max;
1900
1714 $self->{max_val} = $max; 1901 $self->{max_val} = $max;
1902 $self->update;
1715} 1903}
1716 1904
1717sub set_value { 1905sub set_value {
1718 my ($self, $val, $max) = @_; 1906 my ($self, $val, $max) = @_;
1719 1907
1720 $self->set_max ($max) 1908 $self->set_max ($max)
1721 if defined $max; 1909 if defined $max;
1722 1910
1723 $max = $self->{max_val}; 1911 return if $self->{val} == $val;
1912
1724 $self->{val} = $val; 1913 $self->{val} = $val;
1725
1726 $self->update; 1914 $self->update;
1727} 1915}
1728 1916
1729sub _draw { 1917sub _draw {
1730 my ($self) = @_; 1918 my ($self) = @_;
1793 1981
1794sub new { 1982sub new {
1795 my ($class, %arg) = @_; 1983 my ($class, %arg) = @_;
1796 1984
1797 my $self = $class->SUPER::new ( 1985 my $self = $class->SUPER::new (
1798 tooltip => $arg{type}, 1986 tooltip => $arg{type},
1799 can_hover => 1, 1987 can_hover => 1,
1988 can_events => 1,
1800 %arg, 1989 %arg,
1801 ); 1990 );
1802 1991
1803 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1992 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1804 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 1993 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1812 2001
1813 $self->{value}->set_fontsize ($fsize); 2002 $self->{value}->set_fontsize ($fsize);
1814 $self->{max} ->set_fontsize ($fsize); 2003 $self->{max} ->set_fontsize ($fsize);
1815} 2004}
1816 2005
2006sub set_max {
2007 my ($self, $max) = @_;
2008
2009 $self->{gauge}->set_max ($max);
2010 $self->{max}->set_text ($max);
2011}
2012
1817sub set_value { 2013sub set_value {
1818 my ($self, $val, $max) = @_; 2014 my ($self, $val, $max) = @_;
1819 2015
1820 $self->set_max ($max) 2016 $self->set_max ($max)
1821 if defined $max; 2017 if defined $max;
1822 2018
1823 $self->{gauge}->set_value ($val, $max); 2019 $self->{gauge}->set_value ($val, $max);
1824 $self->{value}->set_text ($val); 2020 $self->{value}->set_text ($val);
1825}
1826
1827sub set_max {
1828 my ($self, $max) = @_;
1829
1830 $self->{gauge}->set_max ($max);
1831 $self->{max}->set_text ($max);
1832} 2021}
1833 2022
1834############################################################################# 2023#############################################################################
1835 2024
1836package CFClient::UI::Slider; 2025package CFClient::UI::Slider;
1846 qw(s1_slider.png s1_slider_bg.png); 2035 qw(s1_slider.png s1_slider_bg.png);
1847 2036
1848sub new { 2037sub new {
1849 my $class = shift; 2038 my $class = shift;
1850 2039
1851 # range [value, low, high, page] 2040 # range [value, low, high, page, unit]
1852 2041
1853 # TODO: 0-width page 2042 # TODO: 0-width page
1854 # TODO: req_w/h are wrong with vertical 2043 # TODO: req_w/h are wrong with vertical
1855 # TODO: calculations are off 2044 # TODO: calculations are off
1856 my $self = $class->SUPER::new ( 2045 my $self = $class->SUPER::new (
1857 fg => [1, 1, 1], 2046 fg => [1, 1, 1],
1858 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],
1859 range => [0, 0, 100, 10], 2050 range => [0, 0, 100, 10],
1860 req_w => 20, 2051 req_w => $::WIDTH / 80,
1861 req_h => 20, 2052 req_h => $::WIDTH / 80,
1862 vertical => 0, 2053 vertical => 0,
1863 can_hover => 1, 2054 can_hover => 1,
1864 inner_pad => 5, 2055 inner_pad => 0.02,
1865 @_ 2056 @_
1866 ); 2057 );
1867 2058
2059 $self->set_value ($self->{range}[0]);
2060 $self->update;
2061
1868 $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 }
1869} 2086}
1870 2087
1871sub size_request { 2088sub size_request {
1872 my ($self) = @_; 2089 my ($self) = @_;
1873 2090
1886 2103
1887sub mouse_motion { 2104sub mouse_motion {
1888 my ($self, $ev, $x, $y) = @_; 2105 my ($self, $ev, $x, $y) = @_;
1889 2106
1890 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
1891 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2124 my ($value, $lo, $hi, $page) = @{$self->{range}};
1892 2125
1893 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2126 my $inner_w = 1 - 2 * $self->{inner_pad};
1894 2127
1895 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2128 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
1896 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1897 2129
1898 $x -= $inner_pad_px; # substract the padding 2130 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
1899 $x = $x * ($hi - $lo) / $inner_w + $lo; 2131 $value = $self->{scale} * ($value - $lo);
1900 $x = $lo if $x < $lo;
1901 $x = $hi - $page if $x > $hi - $page;
1902 $self->{range}[0] = $x;
1903 2132
1904 $self->emit (changed => $x); 2133 $value = $self->{inner_pad} + ($value - $page * 0.5);
1905 $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;
1906 } 2140 });
1907}
1908 2141
1909# the inner_* stuff is for generating a padding for the slider handle, 2142 $self->SUPER::update;
1910# so that the handle doesn't leave the texture. This calculation isn't 100%
1911# correct propably, but it does the job for now
1912sub _calc_inner_pad_px {
1913 my ($self, $w) = @_;
1914 ($w / 100) * $self->{inner_pad} # % to pixels
1915} 2143}
1916 2144
1917sub _draw { 2145sub _draw {
1918 my ($self) = @_; 2146 my ($self) = @_;
1919 2147
1920 $self->SUPER::_draw (); 2148 $self->SUPER::_draw ();
1921 2149
1922 my ($w, $h) = @$self{qw(w h)}; 2150 glScale $self->{w}, $self->{h};
1923 2151
1924 if ($self->{vertical}) { 2152 if ($self->{vertical}) {
1925 # draw a vertical slider like a rotated horizontal slider 2153 # draw a vertical slider like a rotated horizontal slider
1926 2154
2155 glTranslate 1, 0, 0;
1927 glRotate 90, 0, 0, 1; 2156 glRotate 90, 0, 0, 1;
1928 glTranslate 0, -$self->{w}, 0;
1929
1930 ($w, $h) = ($h, $w);
1931 } 2157 }
1932 2158
1933 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2159 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1934 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2160 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1935 2161
1936 my ($value, $lo, $hi, $page) = @{$self->{range}};
1937
1938 $hi = $value + 1 if $lo == $hi;
1939
1940 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1941 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1942
1943 $page = int $page * $inner_w / ($hi - $lo);
1944 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1945
1946 $w -= $page;
1947 $page &= ~1;
1948 glTranslate $page * 0.5, 0, 0;
1949 $page ||= 2;
1950
1951 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1952 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1953
1954 glEnable GL_BLEND;
1955 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1956 glEnable GL_TEXTURE_2D; 2162 glEnable GL_TEXTURE_2D;
1957 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2163 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1958 2164
1959 # draw background 2165 # draw background
1960 $tex[1]->draw_quad (0, 0, $w, $h); 2166 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
1961 2167
1962 # draw handle 2168 # draw handle
1963 $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);
1964 2170
1965 glDisable GL_BLEND;
1966 glDisable GL_TEXTURE_2D; 2171 glDisable GL_TEXTURE_2D;
1967} 2172}
1968 2173
1969############################################################################# 2174#############################################################################
1970 2175
1981 fontsize => 1, 2186 fontsize => 1,
1982 can_events => 0, 2187 can_events => 0,
1983 #font => default_font 2188 #font => default_font
1984 @_, 2189 @_,
1985 2190
1986 layout => (new CFClient::Layout), 2191 layout => (new CFClient::Layout 1),
1987 par => [], 2192 par => [],
1988 height => 0, 2193 height => 0,
1989 children => [ 2194 children => [
1990 (new CFClient::UI::Empty expand => 1), 2195 (new CFClient::UI::Empty expand => 1),
1991 (new CFClient::UI::Slider vertical => 1), 2196 (new CFClient::UI::Slider vertical => 1),
1992 ], 2197 ],
1993 ); 2198 );
1994 2199
1995 $self->{children}[1]->connect (changed => sub { 2200 $self->{children}[1]->connect (changed => sub { $self->update });
1996 $self->update;
1997 });
1998 2201
1999 $self 2202 $self
2000} 2203}
2001 2204
2002sub set_fontsize { 2205sub set_fontsize {
2010 my ($self, $text) = @_; 2213 my ($self, $text) = @_;
2011 2214
2012 my $layout = $self->{layout}; 2215 my $layout = $self->{layout};
2013 2216
2014 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2217 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2015 $layout->set_width ($self->{w}); 2218 $layout->set_width ($self->{children}[0]{w});
2016 $layout->set_text ($text); 2219 $layout->set_markup ($text);
2017 2220
2018 ($layout->size)[1] 2221 ($layout->size)[1]
2019} 2222}
2020 2223
2021sub reflow { 2224sub reflow {
2059 2262
2060 return unless $self->{h} > 0; 2263 return unless $self->{h} > 0;
2061 2264
2062 delete $self->{texture}; 2265 delete $self->{texture};
2063 2266
2064 $ROOT->on_refresh ($self, sub { 2267 $ROOT->on_post_alloc ($self, sub {
2065 if (delete $self->{need_reflow}) { 2268 if (delete $self->{need_reflow}) {
2066 my $height = 0; 2269 my $height = 0;
2067 2270
2068 $height += $_->[0] = $self->text_height ($_->[2]) 2271 $height += $_->[0] = $self->text_height ($_->[2])
2069 for @{$self->{par}}; 2272 for @{$self->{par}};
2073 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2276 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2074 2277
2075 delete $self->{texture}; 2278 delete $self->{texture};
2076 } 2279 }
2077 2280
2078 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2281 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2079 glClearColor 0, 0, 0, 1; 2282 glClearColor 0.5, 0.5, 0.5, 0;
2080 glClear GL_COLOR_BUFFER_BIT; 2283 glClear GL_COLOR_BUFFER_BIT;
2081 2284
2082 glEnable GL_BLEND;
2083 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2084 glEnable GL_TEXTURE_2D; 2285 glEnable GL_TEXTURE_2D;
2085 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2286 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2086 2287
2087 my $top = int $self->{children}[1]{range}[0]; 2288 my $top = int $self->{children}[1]{range}[0];
2088 2289
2097 2298
2098 for my $par (@{$self->{par}}) { 2299 for my $par (@{$self->{par}}) {
2099 my $h = $par->[0]; 2300 my $h = $par->[0];
2100 2301
2101 if ($y0 < $y + $h && $y < $y1) { 2302 if ($y0 < $y + $h && $y < $y1) {
2303 $layout->set_foreground (@{ $par->[1] });
2102 $layout->set_text ($par->[2]); 2304 $layout->set_markup ($par->[2]);
2103 2305
2104 glColor @{ $par->[1] };
2105 my ($W, $H) = $layout->size; 2306 my ($W, $H) = $layout->size;
2106 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);
2107 } 2308 }
2108 2309
2109 $y += $h; 2310 $y += $h;
2110 } 2311 }
2111 2312
2112 glDisable GL_TEXTURE_2D; 2313 glDisable GL_TEXTURE_2D;
2113 glDisable GL_BLEND;
2114 }; 2314 };
2115 }); 2315 });
2116} 2316}
2117 2317
2118sub _draw { 2318sub _draw {
2119 my ($self) = @_; 2319 my ($self) = @_;
2120 2320
2121 if ($self->{texture}) {
2122 glEnable GL_TEXTURE_2D; 2321 glEnable GL_TEXTURE_2D;
2123 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2322 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2323 glColor 1, 1, 1, 1;
2124 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2324 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2125 glDisable GL_TEXTURE_2D; 2325 glDisable GL_TEXTURE_2D;
2126 }
2127 2326
2128 $self->{children}[1]->draw; 2327 $self->{children}[1]->draw;
2129 2328
2130} 2329}
2131 2330
2225 @_, 2424 @_,
2226 can_events => 0, 2425 can_events => 0,
2227 ) 2426 )
2228} 2427}
2229 2428
2230sub set_markup { 2429sub set_tooltip_from {
2231 my ($self, $text) = @_; 2430 my ($self, $widget) = @_;
2232 2431
2233 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2432 $self->add (new CFClient::UI::Label
2234 $self->{label}->set_markup ($text); 2433 markup => $widget->{tooltip},
2235 $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 );
2236} 2440}
2237 2441
2238sub size_request { 2442sub size_request {
2239 my ($self) = @_; 2443 my ($self) = @_;
2240
2241 $self->child->set_max_size ($::WIDTH * 0.3);
2242 2444
2243 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2445 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2244 2446
2245 ($w + 4, $h + 4) 2447 ($w + 4, $h + 4)
2246} 2448}
2252} 2454}
2253 2455
2254sub _draw { 2456sub _draw {
2255 my ($self) = @_; 2457 my ($self) = @_;
2256 2458
2257 glPushMatrix;
2258 glTranslate 0.375, 0.375; 2459 glTranslate 0.375, 0.375;
2259 2460
2260 my ($w, $h) = @$self{qw(w h)}; 2461 my ($w, $h) = @$self{qw(w h)};
2261 2462
2262 glColor 1, 0.8, 0.4; 2463 glColor 1, 0.8, 0.4;
2273 glVertex 0 , $h; 2474 glVertex 0 , $h;
2274 glVertex $w, $h; 2475 glVertex $w, $h;
2275 glVertex $w, 0; 2476 glVertex $w, 0;
2276 glEnd; 2477 glEnd;
2277 2478
2278 glPopMatrix; 2479 glTranslate 2 - 0.375, 2 - 0.375;
2279
2280 glTranslate 2, 2;
2281 $self->SUPER::_draw; 2480 $self->SUPER::_draw;
2282} 2481}
2283 2482
2284############################################################################# 2483#############################################################################
2285 2484
2290use CFClient::OpenGL; 2489use CFClient::OpenGL;
2291 2490
2292sub new { 2491sub new {
2293 my $class = shift; 2492 my $class = shift;
2294 2493
2295 $class->SUPER::new ( 2494 my $self = $class->SUPER::new (
2296 aspect => 1, 2495 aspect => 1,
2297 @_, 2496 @_,
2298 ) 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
2299} 2514}
2300 2515
2301sub size_request { 2516sub size_request {
2302 (32, 8) 2517 (32, 8)
2303} 2518}
2304 2519
2305sub draw { 2520sub _draw {
2306 my ($self) = @_; 2521 my ($self) = @_;
2307 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
2308 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2534 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2309 2535
2310 if ($tex) { 2536 if ($tex) {
2311 glEnable GL_BLEND;
2312 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2313 glEnable GL_TEXTURE_2D; 2537 glEnable GL_TEXTURE_2D;
2314 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2538 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2315 glColor 1, 1, 1, 1; 2539 glColor 1, 1, 1, 1;
2316 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2540 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2317 glDisable GL_TEXTURE_2D; 2541 glDisable GL_TEXTURE_2D;
2318 glDisable GL_BLEND; 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});
2707}
2708
2709#############################################################################
2710
2711package CFClient::UI::Menu;
2712
2713our @ISA = CFClient::UI::FancyFrame::;
2714
2715use CFClient::OpenGL;
2716
2717sub new {
2718 my $class = shift;
2719
2720 my $self = $class->SUPER::new (
2721 items => [],
2722 z => 100,
2723 @_,
2724 );
2725
2726 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2727
2728 for my $item (@{ $self->{items} }) {
2729 my ($widget, $cb) = @$item;
2730
2731 # handle various types of items, only text for now
2732 if (!ref $widget) {
2733 $widget = new CFClient::UI::Label
2734 can_hover => 1,
2735 can_events => 1,
2736 text => $widget;
2737 }
2738
2739 $self->{item}{$widget} = $item;
2740
2741 $self->{vbox}->add ($widget);
2742 }
2743
2744 $self
2745}
2746
2747# popup given the event (must be a mouse button down event currently)
2748sub popup {
2749 my ($self, $ev) = @_;
2750
2751 $self->emit ("popdown");
2752
2753 # maybe save $GRAB? must be careful about events...
2754 $GRAB = $self;
2755 $self->{button} = $ev->{button};
2756
2757 $self->show;
2758 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2759}
2760
2761sub mouse_motion {
2762 my ($self, $ev, $x, $y) = @_;
2763
2764 # TODO: should use vbox->find_widget or so
2765 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2766 $self->{hover} = $self->{item}{$HOVER};
2767}
2768
2769sub button_up {
2770 my ($self, $ev, $x, $y) = @_;
2771
2772 if ($ev->{button} == $self->{button}) {
2773 undef $GRAB;
2774 $self->hide;
2775
2776 $self->emit ("popdown");
2777 $self->{hover}[1]->() if $self->{hover};
2778 }
2779}
2780
2781#############################################################################
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 @_,
2319 } 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;
2320} 2887}
2321 2888
2322############################################################################# 2889#############################################################################
2323 2890
2324package CFClient::UI::Root; 2891package CFClient::UI::Root;
2325 2892
2326our @ISA = CFClient::UI::Container::; 2893our @ISA = CFClient::UI::Container::;
2327 2894
2328use CFClient::OpenGL; 2895use CFClient::OpenGL;
2329 2896
2330sub check_size { 2897sub new {
2331 my ($self) = @_; 2898 my $class = shift;
2332 2899
2333 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2900 $class->SUPER::new (
2334} 2901 @_,
2335 2902 )
2336sub size_request {
2337 ($::WIDTH, $::HEIGHT)
2338} 2903}
2339 2904
2340sub configure { 2905sub configure {
2341 my ($self, $x, $y, $w, $h) = @_; 2906 my ($self, $x, $y, $w, $h) = @_;
2342 2907
2343 $self->SUPER::configure ($x, $y, $w, $h); 2908 $self->{w} = $w;
2909 $self->{h} = $h;
2910}
2344 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
2345 for my $child (@{$self->{children}}) { 2934 for my $child ($self->children) {
2346 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)};
2347 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
2348 $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;
2349 $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
2350 $child->configure ($X, $Y, $W,$H); 2946 $child->configure ($X, $Y, $W, $H);
2351 } 2947 }
2352} 2948}
2353 2949
2354sub _topleft { 2950sub coord2local {
2355 my ($self, $x, $y) = @_; 2951 my ($self, $x, $y) = @_;
2356 2952
2357 ($x, $y) 2953 ($x, $y)
2358} 2954}
2359 2955
2956sub coord2global {
2957 my ($self, $x, $y) = @_;
2958
2959 ($x, $y)
2960}
2961
2360sub update { 2962sub update {
2361 my ($self) = @_; 2963 my ($self) = @_;
2362 2964
2363 $self->check_size; 2965 $self->check_size;
2364 ::refresh (); 2966 $::WANT_REFRESH++;
2365} 2967}
2366 2968
2367sub add { 2969sub add {
2368 my ($self, $child) = @_; 2970 my ($self, @children) = @_;
2369 2971
2972 for my $child (@children) {
2973 $child->{toplevel} = 1;
2974
2370 # integerize window positions 2975 # integerise window positions
2371 $child->{x} = int $child->{x}; 2976 $child->{x} = int $child->{x};
2372 $child->{y} = int $child->{y}; 2977 $child->{y} = int $child->{y};
2978 }
2373 2979
2374 $self->SUPER::add ($child); 2980 $self->SUPER::add (@children);
2375} 2981}
2376 2982
2377sub on_refresh { 2983sub on_refresh {
2378 my ($self, $id, $cb) = @_; 2984 my ($self, $id, $cb) = @_;
2379 2985
2380 $self->{refresh_hook}{$id} = $cb; 2986 $self->{refresh_hook}{$id} = $cb;
2381} 2987}
2382 2988
2989sub on_post_alloc {
2990 my ($self, $id, $cb) = @_;
2991
2992 $self->{post_alloc_hook}{$id} = $cb;
2993}
2994
2383sub draw { 2995sub draw {
2384 my ($self) = @_; 2996 my ($self) = @_;
2385 2997
2386 while (my $rcb = delete $self->{refresh_hook}) { 2998 while ($self->{refresh_hook}) {
2387 $_->() 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}};
2388 } 3051 }
2389 3052
2390 glViewport 0, 0, $::WIDTH, $::HEIGHT; 3053 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2391 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3054 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2392 glClear GL_COLOR_BUFFER_BIT; 3055 glClear GL_COLOR_BUFFER_BIT;
2393 3056
2394 glMatrixMode GL_PROJECTION; 3057 glMatrixMode GL_PROJECTION;
2395 glLoadIdentity; 3058 glLoadIdentity;
2396 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3059 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2397 glMatrixMode GL_MODELVIEW; 3060 glMatrixMode GL_MODELVIEW;
2398 glLoadIdentity; 3061 glLoadIdentity;
2399 3062
2400 $self->_draw; 3063 $self->_draw;
2401} 3064}
2403############################################################################# 3066#############################################################################
2404 3067
2405package CFClient::UI; 3068package CFClient::UI;
2406 3069
2407$ROOT = new CFClient::UI::Root; 3070$ROOT = new CFClient::UI::Root;
2408$TOOLTIP = new CFClient::UI::Tooltip; 3071$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2409 3072
24101 30731
2411 3074

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines