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.151 by root, Sun Apr 23 00:57:39 2006 UTC vs.
Revision 1.186 by root, Mon May 8 20:39:34 2006 UTC

14our $BUTTON_STATE; 14our $BUTTON_STATE;
15 15
16sub check_tooltip { 16sub check_tooltip {
17 if (!$GRAB) { 17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) { 19 if (length $widget->{tooltip}) {
20 20
21 if ($TOOLTIP->{owner} != $widget) { 21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget; 22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
23 $TOOLTIP->set_text ($widget->{tooltip}); 28 $TOOLTIP->set_markup ($widget->{tooltip});
24 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); 29
25 $TOOLTIP->show; 30 $TOOLTIP->show;
31
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33
34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 }
37
38 $TOOLTIP->move ($x, $y);
26 } 39 }
27 40
28 return; 41 return;
29 } 42 }
30 } 43 }
34 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
35} 48}
36 49
37# class methods for events 50# class methods for events
38sub feed_sdl_key_down_event { 51sub feed_sdl_key_down_event {
39 $FOCUS->key_down ($_[0]) if $FOCUS; 52 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
53 if $FOCUS;
40} 54}
41 55
42sub feed_sdl_key_up_event { 56sub feed_sdl_key_up_event {
43 $FOCUS->key_up ($_[0]) if $FOCUS; 57 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
58 if $FOCUS;
44} 59}
45 60
46sub feed_sdl_button_down_event { 61sub feed_sdl_button_down_event {
47 my ($ev) = @_; 62 my ($ev) = @_;
48 my ($x, $y) = ($ev->{x}, $ev->{y}); 63 my ($x, $y) = ($ev->{x}, $ev->{y});
56 check_tooltip; 71 check_tooltip;
57 } 72 }
58 73
59 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 74 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
60 75
61 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 76 if ($GRAB) {
77 ($x, $y) = $GRAB->coord2local ($x, $y);
78 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
79 }
62} 80}
63 81
64sub feed_sdl_button_up_event { 82sub feed_sdl_button_up_event {
65 my ($ev) = @_; 83 my ($ev) = @_;
66 my ($x, $y) = ($ev->{x}, $ev->{y}); 84 my ($x, $y) = ($ev->{x}, $ev->{y});
67 85
68 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 86 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
69 87
70 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 88 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
71 89
72 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 90 if ($GRAB) {
91 ($x, $y) = $GRAB->coord2local ($x, $y);
92 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
93 }
73 94
74 if (!$BUTTON_STATE) { 95 if (!$BUTTON_STATE) {
75 my $grab = $GRAB; undef $GRAB; 96 my $grab = $GRAB; undef $GRAB;
76 $grab->update if $grab; 97 $grab->update if $grab;
77 $GRAB->update if $GRAB; 98 $GRAB->update if $GRAB;
93 $HOVER->update if $HOVER && $HOVER->{can_hover}; 114 $HOVER->update if $HOVER && $HOVER->{can_hover};
94 115
95 check_tooltip; 116 check_tooltip;
96 } 117 }
97 118
98 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 119 if ($HOVER) {
120 ($x, $y) = $HOVER->coord2local ($x, $y);
121 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
122 }
99} 123}
100 124
101# convert position array to integers 125# convert position array to integers
102sub harmonize { 126sub harmonize {
103 my ($vals) = @_; 127 my ($vals) = @_;
121 145
122sub new { 146sub new {
123 my $class = shift; 147 my $class = shift;
124 148
125 my $self = bless { 149 my $self = bless {
126 x => 0, 150 x => 0,
127 y => 0, 151 y => 0,
128 z => 0, 152 z => 0,
129 can_events => 1, 153 can_events => 1,
130 @_ 154 @_
131 }, $class; 155 }, $class;
132 156
133 for (keys %$self) { 157 for (keys %$self) {
137 } 161 }
138 162
139 $self 163 $self
140} 164}
141 165
166sub destroy {
167 my ($self) = @_;
168
169 $self->hide;
170 %$self = ();
171}
172
142sub show { 173sub show {
143 my ($self) = @_; 174 my ($self) = @_;
144 175
145 return if $self->{parent}; 176 return if $self->{parent};
146 177
148} 179}
149 180
150sub hide { 181sub hide {
151 my ($self) = @_; 182 my ($self) = @_;
152 183
153 return unless $self->{parent}; 184 undef $GRAB if $GRAB == $self;
185 undef $HOVER if $HOVER == $self;
154 186
155 $self->{parent}->remove ($self); 187 $self->{parent}->remove ($self)
188 if $self->{parent};
156} 189}
157 190
158sub move { 191sub move {
159 my ($self, $x, $y, $z) = @_; 192 my ($self, $x, $y, $z) = @_;
160 193
163 $self->{z} = $z if defined $z; 196 $self->{z} = $z if defined $z;
164 197
165 $self->update; 198 $self->update;
166} 199}
167 200
168sub needs_redraw { 201sub set_size {
169 0 202 my ($self, $w, $h) = @_;
203
204 $self->{user_w} = $w;
205 $self->{user_h} = $h;
206
207 $self->check_size;
170} 208}
171 209
172sub size_request { 210sub size_request {
173 require Carp; 211 require Carp;
174 Carp::confess "size_request is abstract"; 212 Carp::confess "size_request is abstract";
199 $self->{w} = $w; 237 $self->{w} = $w;
200 $self->{h} = $h; 238 $self->{h} = $h;
201 239
202 $self->size_allocate ($w, $h); 240 $self->size_allocate ($w, $h);
203 $self->update; 241 $self->update;
242 $self->emit (size_allocate => $w, $h);
204 } 243 }
205} 244}
206 245
207sub size_allocate { 246sub size_allocate {
208 # nothing to be done 247 # nothing to be done
248}
249
250sub 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}
264
265sub set_max_size {
266 my ($self, $w, $h) = @_;
267
268 delete $self->{max_w}; $self->{max_w} = $w if $w;
269 delete $self->{max_h}; $self->{max_h} = $h if $h;
209} 270}
210 271
211# return top left coordinates 272# return top left coordinates
212sub _topleft { 273sub _topleft {
213 my ($self, $x, $y) = @_; 274 my ($self, $x, $y) = @_;
275
276 $self->{parent}
277 or Carp::confess "no parent widget in _topleft\n";#d#
214 278
215 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 279 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
216} 280}
217 281
218# translate global coordinates to local coordinate system 282# translate global coordinates to local coordinate system
296 glVertex $x + $self->{w}, $y + $self->{h}; 360 glVertex $x + $self->{w}, $y + $self->{h};
297 glVertex $x , $y + $self->{h}; 361 glVertex $x , $y + $self->{h};
298 glEnd; 362 glEnd;
299 glDisable GL_BLEND; 363 glDisable GL_BLEND;
300 } 364 }
365
366 if ($ENV{PCLIENT_DEBUG}) {
367 glPushMatrix;
368 glColor 1, 1, 0, 1;
369 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
370 glBegin GL_LINE_LOOP;
371 glVertex 0 , 0;
372 glVertex $self->{w}, 0;
373 glVertex $self->{w}, $self->{h};
374 glVertex 0 , $self->{h};
375 glEnd;
376 glPopMatrix;
377 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
378 }
301} 379}
302 380
303sub _draw { 381sub _draw {
304 my ($self) = @_; 382 my ($self) = @_;
305 383
325} 403}
326 404
327sub check_size { 405sub check_size {
328 my ($self) = @_; 406 my ($self) = @_;
329 407
330 return unless $self->{parent}; 408 $self->{parent}
409 or return 1;
331 410
332 my ($w, $h) = $self->size_request; 411 my ($w, $h) = $self->{user_w} && $self->{user_h}
412 ? @$self{qw(user_w user_h)}
413 : $self->size_request;
333 414
334 if ($w != $self->{req_w} || $h != $self->{req_h}) { 415 if ($w != $self->{req_w} || $h != $self->{req_h}) {
335 $self->{req_w} = $w; 416 $self->{req_w} = $w;
336 $self->{req_h} = $h; 417 $self->{req_h} = $h;
337 418
338 $self->{parent}->check_size; 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
339 } 428 }
340} 429}
341 430
342sub update { 431sub update {
343 my ($self) = @_; 432 my ($self) = @_;
353} 442}
354 443
355sub emit { 444sub emit {
356 my ($self, $signal, @args) = @_; 445 my ($self, $signal, @args) = @_;
357 446
358 for my $cb (@{$self->{signal_cb}{$signal} || []}) { 447 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
359 $cb->($self, @args);
360 }
361} 448}
362 449
363sub DESTROY { 450sub DESTROY {
364 my ($self) = @_; 451 my ($self) = @_;
365 452
432sub new { 519sub new {
433 my ($class, %arg) = @_; 520 my ($class, %arg) = @_;
434 521
435 my $children = delete $arg{children} || []; 522 my $children = delete $arg{children} || [];
436 523
437 my $self = $class->SUPER::new (children => [], can_events => 0, %arg); 524 my $self = $class->SUPER::new (
525 children => [],
526 can_events => 0,
527 %arg,
528 );
438 $self->add ($_) for @$children; 529 $self->add ($_) for @$children;
439 530
440 $self 531 $self
441} 532}
442 533
451 sort { $a->{z} <=> $b->{z} } 542 sort { $a->{z} <=> $b->{z} }
452 @{$self->{children}}, $child 543 @{$self->{children}}, $child
453 ]; 544 ];
454 545
455 $child->check_size; 546 $child->check_size;
547 $self->update;
548}
549
550sub children {
551 @{ $_[0]{children} }
456} 552}
457 553
458sub remove { 554sub remove {
459 my ($self, $child) = @_; 555 my ($self, $child) = @_;
460 556
461 delete $child->{parent}; 557 delete $child->{parent};
558 $child->hide;
462 559
463 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 560 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
561
562 $self->check_size;
563 $self->update;
564}
565
566sub clear {
567 my ($self) = @_;
568
569 my $children = delete $self->{children};
570 $self->{children} = [];
571
572 for (@$children) {
573 delete $_->{parent};
574 $_->hide;
575 }
464 576
465 $self->check_size; 577 $self->check_size;
466 $self->update; 578 $self->update;
467} 579}
468 580
546} 658}
547 659
548sub update { 660sub update {
549 my ($self) = @_; 661 my ($self) = @_;
550 662
551 # we want to do this delayed... 663 $ROOT->on_refresh ($self => sub { $self->render_child });
552 $self->render_chld;
553 $self->SUPER::update; 664 $self->SUPER::update;
554}
555
556sub render_chld {
557 my ($self) = @_;
558
559 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
560 glClearColor 0, 0, 0, 1;
561 glClear GL_COLOR_BUFFER_BIT;
562 $self->child->draw;
563 };
564} 665}
565 666
566sub size_allocate { 667sub size_allocate {
567 my ($self, $w, $h) = @_; 668 my ($self, $w, $h) = @_;
568 669
569 $self->child->configure (0, 0, $w, $h); 670 $self->SUPER::size_allocate ($w, $h);
671 $self->update;
672}
570 673
674sub _render {
675 $_[0]{children}[0]->draw;
676}
677
678sub render_child {
679 my ($self) = @_;
680
681 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
682 glClearColor 0, 0, 0, 0;
683 glClear GL_COLOR_BUFFER_BIT;
684
571 $self->render_chld; 685 $self->_render;
686# glColorMask 1, 1, 1, 0;
687# glEnable GL_BLEND;
688# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
689# glRasterPos 0, 0;
690# glCopyPixels 0, 0, $self->{w}, $self->{h};
691# glDisable GL_BLEND;
692# glColorMask 1, 1, 1, 1;
693 };
572} 694}
573 695
574sub _draw { 696sub _draw {
575 my ($self) = @_; 697 my ($self) = @_;
576 698
578 700
579 my $tex = $self->{texture} 701 my $tex = $self->{texture}
580 or return; 702 or return;
581 703
582 glEnable GL_BLEND; 704 glEnable GL_BLEND;
583 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 705 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
584 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
585 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
708 glColor 0, 0, 0, 1;
586 709
587 $tex->draw_quad (0, 0, $w, $h); 710 $tex->draw_quad (0, 0, $w, $h);
588 711
589 glDisable GL_BLEND; 712 glDisable GL_BLEND;
590 glDisable GL_TEXTURE_2D; 713 glDisable GL_TEXTURE_2D;
594 717
595package CFClient::UI::ViewPort; 718package CFClient::UI::ViewPort;
596 719
597our @ISA = CFClient::UI::Window::; 720our @ISA = CFClient::UI::Window::;
598 721
599sub new { die }
600
601sub size_request { 722sub size_request {
602 my ($self) = @_; 723 my ($self) = @_;
603 724
604 @$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)};
605 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 726 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
606 727
607 @$self{qw(child_w child_h)} 728 @$self{qw(child_w child_h)}
608} 729}
609 730
610sub _draw { 731sub size_allocate {
611 my ($self) = @_; 732 my ($self, $w, $h) = @_;
612 733
613 $self->{children}[1]->draw; 734 $self->update;
614} 735}
615 736
737sub set_offset {
738 my ($self, $x, $y) = @_;
739
740 $self->{view_x} = int $x;
741 $self->{view_y} = int $y;
742
743 $self->update;
744}
745
746# hmm, this does not work for topleft of $self... but we should not aks for that
747sub _topleft {
748 my ($self, $x, $y) = @_;
749
750 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y})
751}
752
753sub find_widget {
754 my ($self, $x, $y) = @_;
755
756 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
757 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
758 ) {
759 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
760 } else {
761 $self->CFClient::UI::Base::find_widget ($x, $y)
762 }
763}
764
765sub _render {
766 my ($self) = @_;
767
768 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
769
770 $self->SUPER::_render;
771}
772
773#############################################################################
774
775package CFClient::UI::ScrolledWindow;
776
777our @ISA = CFClient::UI::HBox::;
778
779sub new {
780 my $class = shift;
781
782 my $self;
783
784 my $slider = new CFClient::UI::Slider
785 vertical => 1,
786 range => [0, 0, 1, 0.01], # HACK fix
787 connect_changed => sub {
788 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
789 },
790 ;
791
792 $self = $class->SUPER::new (
793 vp => (new CFClient::UI::ViewPort),
794 slider => $slider,
795 @_,
796 );
797
798 $self->{vp}->add ($self->{scrolled});
799 $self->add ($self->{vp});
800 $self->add ($self->{slider});
801
802 $self
803}
804
805#TODO# update range on size_allocate depeneing on child
806# update viewport offset on scroll
616 807
617############################################################################# 808#############################################################################
618 809
619package CFClient::UI::Frame; 810package CFClient::UI::Frame;
620 811
631 border => 0.8, 822 border => 0.8,
632 @_ 823 @_
633 ); 824 );
634 825
635 $self 826 $self
636}
637
638sub set_size {
639 my ($self, $w, $h) = @_;
640 $self->{req_w} = $w;
641 $self->{req_h} = $h;
642 $self->check_size;
643}
644
645sub size_request {
646 my ($self) = @_;
647 ($self->{req_w}, $self->{req_h})
648}
649
650sub size_allocate {
651 my ($self, $w, $h) = @_;
652 $self->{w} = $w;
653 $self->{h} = $h;
654 $self->child->configure (0, 0, $w, $h);
655} 827}
656 828
657sub _draw { 829sub _draw {
658 my ($self) = @_; 830 my ($self) = @_;
659 831
694 my $class = shift; 866 my $class = shift;
695 867
696 # TODO: user_x, user_y, overwrite moveto? 868 # TODO: user_x, user_y, overwrite moveto?
697 869
698 my $self = $class->SUPER::new ( 870 my $self = $class->SUPER::new (
699 bg => [1, 1, 1, 1], 871 bg => [1, 1, 1, 1],
700 border_bg => [1, 1, 1, 1], 872 border_bg => [1, 1, 1, 1],
701 border => 0.8, 873 border => 0.6,
702 can_events => 1, 874 can_events => 1,
703 @_ 875 @_
704 ); 876 );
705 877
706 $self->{title} &&= new CFClient::UI::Label 878 $self->{title} &&= new CFClient::UI::Label
707 align => 0, 879 align => 0,
708 valign => 1, 880 valign => 1,
709 text => $self->{title}, 881 text => $self->{title},
710 fontsize => 1; 882 fontsize => $self->{border};
711 883
712 $self 884 $self
713} 885}
714 886
715sub border { 887sub border {
716 int $_[0]{border} * $::FONTSIZE 888 int $_[0]{border} * $::FONTSIZE
717} 889}
718 890
719sub size_request { 891sub size_request {
720 my ($self) = @_; 892 my ($self) = @_;
721
722 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
723 893
724 my ($w, $h) = $self->SUPER::size_request; 894 my ($w, $h) = $self->SUPER::size_request;
725 895
726 ( 896 (
727 $w + $self->border * 2, 897 $w + $self->border * 2,
742} 912}
743 913
744sub button_down { 914sub button_down {
745 my ($self, $ev, $x, $y) = @_; 915 my ($self, $ev, $x, $y) = @_;
746 916
917 my ($w, $h) = @$self{qw(w h)};
747 my $border = $self->border; 918 my $border = $self->border;
748 919
749 if ($x < $self->{w} && $x >= $self->{w} - $border 920 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
750 && $y < $self->{h} && $y >= $self->{h} - $border) { 921 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
751 922
923 if ($lr & $td) {
924 my ($wx, $wy) = ($self->{x}, $self->{y});
752 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 925 my ($ox, $oy) = ($ev->{x}, $ev->{y});
753 my ($bw, $bh) = ($self->{w}, $self->{h}); 926 my ($bw, $bh) = ($self->{w}, $self->{h});
754 927
928 my $mx = $x < $border;
929 my $my = $y < $border;
930
755 $self->{motion} = sub { 931 $self->{motion} = sub {
756 my ($ev, $x, $y) = @_; 932 my ($ev, $x, $y) = @_;
757 933
758 ($x, $y) = ($ev->{x}, $ev->{y}); 934 my $dx = $ev->{x} - $ox;
935 my $dy = $ev->{y} - $oy;
759 936
760 $self->{user_w} = $bw + $x - $ox; 937 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1);
761 $self->{user_h} = $bh + $y - $oy; 938 $self->{user_h} = $bh + $dy * ($my ? -1 : 1);
939 $self->move ($wx + $dx * $mx, $wy + $dy * $my);
762 $self->check_size; 940 $self->check_size;
763 }; 941 };
764 942
765 } elsif ($x >= 0 && $x < $self->{w} 943 } elsif ($lr ^ $td) {
766 && $y >= 0 && $y < $border) {
767
768 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 944 my ($ox, $oy) = ($ev->{x}, $ev->{y});
769 my ($bx, $by) = ($self->{x}, $self->{y}); 945 my ($bx, $by) = ($self->{x}, $self->{y});
770 946
771 $self->{motion} = sub { 947 $self->{motion} = sub {
772 my ($ev, $x, $y) = @_; 948 my ($ev, $x, $y) = @_;
808 $tex[1]->draw_quad (0, 0, $w, $border); 984 $tex[1]->draw_quad (0, 0, $w, $border);
809 $tex[3]->draw_quad (0, $border, $border, $ch); 985 $tex[3]->draw_quad (0, $border, $border, $ch);
810 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 986 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
811 $tex[4]->draw_quad (0, $h - $border, $w, $border); 987 $tex[4]->draw_quad (0, $h - $border, $w, $border);
812 988
989 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
813 my $bg = $tex[0]; 990 my $bg = $tex[0];
814 991
815 # TODO: repeat texture not scale 992 # TODO: repeat texture not scale
816 my $rep_x = $cw / $bg->{w}; 993 my $rep_x = $cw / $bg->{w};
817 my $rep_y = $ch / $bg->{h}; 994 my $rep_y = $ch / $bg->{h};
818 995
819 glColor @{ $self->{bg} }; 996 glColor @{ $self->{bg} };
820 997
821 $bg->{s} = $rep_x; 998 $bg->{s} = $rep_x;
822 $bg->{t} = $rep_y; 999 $bg->{t} = $rep_y;
823 $bg->{wrap_mode} = 1; 1000 $bg->{wrap_mode} = 1;
824 $bg->draw_quad ($border, $border, $cw, $ch); 1001 $bg->draw_quad ($border, $border, $cw, $ch);
825 1002
826 glDisable GL_TEXTURE_2D; 1003 glDisable GL_TEXTURE_2D;
827 glDisable GL_BLEND; 1004 glDisable GL_BLEND;
1005 }
828 1006
829 $self->{title}->draw if $self->{title}; 1007 $self->{title}->draw if $self->{title};
1008
830 $self->child->draw; 1009 $self->child->draw;
831} 1010}
832 1011
833############################################################################# 1012#############################################################################
834 1013
856 $self->{children}[$y][$x] = $child; 1035 $self->{children}[$y][$x] = $child;
857 1036
858 $child->check_size; 1037 $child->check_size;
859} 1038}
860 1039
1040sub children {
1041 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1042}
1043
861# TODO: move to container class maybe? send childs a signal on removal? 1044# TODO: move to container class maybe? send childs a signal on removal?
862sub clear { 1045sub clear {
863 my ($self) = @_; 1046 my ($self) = @_;
864 1047
1048 my @children = $self->children;
865 delete $self->{children}; 1049 delete $self->{children};
1050
1051 for (@children) {
1052 delete $_->{parent};
1053 $_->hide;
1054 }
1055
866 $self->update; 1056 $self->update;
867} 1057}
868 1058
869sub get_wh { 1059sub get_wh {
870 my ($self) = @_; 1060 my ($self) = @_;
1093 1283
1094sub new { 1284sub new {
1095 my ($class, %arg) = @_; 1285 my ($class, %arg) = @_;
1096 1286
1097 my $self = $class->SUPER::new ( 1287 my $self = $class->SUPER::new (
1098 fg => [1, 1, 1], 1288 fg => [1, 1, 1],
1289 #font => default_font
1099 fontsize => 1, 1290 fontsize => 1,
1100 text => "", 1291 text => "",
1101 align => -1, 1292 align => -1,
1102 valign => -1, 1293 valign => -1,
1103 padding => 2, 1294 padding => 2,
1104 layout => new CFClient::Layout, 1295 layout => new CFClient::Layout,
1105 can_events => 0, 1296 can_events => 0,
1106 %arg 1297 %arg
1107 ); 1298 );
1108 1299
1109 if (exists $self->{template}) { 1300 if (exists $self->{template}) {
1116 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1307 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1117 1308
1118 $self 1309 $self
1119} 1310}
1120 1311
1121sub escape_text { 1312sub escape {
1122 local $_ = $_[1]; 1313 local $_ = $_[1];
1123 1314
1124 s/&/&amp;/g; 1315 s/&/&amp;/g;
1125 s/>/&gt;/g; 1316 s/>/&gt;/g;
1126 s/</&lt;/g; 1317 s/</&lt;/g;
1127 1318
1128 $_[1] 1319 $_[1]
1129} 1320}
1130 1321
1322sub update {
1323 my ($self) = @_;
1324
1325 delete $self->{texture};
1326 $self->SUPER::update;
1327}
1328
1131sub set_text { 1329sub set_text {
1132 my ($self, $text) = @_; 1330 my ($self, $text) = @_;
1133 1331
1332 return if $self->{text} eq "T$text";
1333 $self->{text} = "T$text";
1334
1134 $self->{layout}->set_text ($text); 1335 $self->{layout}->set_text ($text);
1135 1336
1136 delete $self->{texture}; 1337 delete $self->{texture};
1338 $self->update;
1137 $self->check_size; 1339 $self->check_size;
1138 $self->update;
1139} 1340}
1140 1341
1141sub set_markup { 1342sub set_markup {
1142 my ($self, $markup) = @_; 1343 my ($self, $markup) = @_;
1143 1344
1345 return if $self->{text} eq "M$markup";
1346 $self->{text} = "M$markup";
1347
1144 $self->{layout}->set_markup ($markup); 1348 $self->{layout}->set_markup ($markup);
1145 1349
1146 delete $self->{texture}; 1350 delete $self->{texture};
1351 $self->update;
1147 $self->check_size; 1352 $self->check_size;
1148 $self->update;
1149} 1353}
1150 1354
1151sub size_request { 1355sub size_request {
1152 my ($self) = @_; 1356 my ($self) = @_;
1153 1357
1154 $self->{layout}->set_width; 1358 $self->{layout}->set_font ($self->{font}) if $self->{font};
1359 $self->{layout}->set_width ($self->{max_w} || -1);
1155 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1360 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1156 1361
1157 my ($w, $h) = $self->{layout}->size; 1362 my ($w, $h) = $self->{layout}->size;
1158 1363
1159 if (exists $self->{template}) { 1364 if (exists $self->{template}) {
1365 $self->{template}->set_font ($self->{font}) if $self->{font};
1160 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1366 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1161 1367
1162 my ($w2, $h2) = $self->{template}->size; 1368 my ($w2, $h2) = $self->{template}->size;
1163 1369
1164 $w = List::Util::max $w, $w2; 1370 $w = List::Util::max $w, $w2;
1179 1385
1180sub set_fontsize { 1386sub set_fontsize {
1181 my ($self, $fontsize) = @_; 1387 my ($self, $fontsize) = @_;
1182 1388
1183 $self->{fontsize} = $fontsize; 1389 $self->{fontsize} = $fontsize;
1390 delete $self->{texture};
1391
1392 $self->update;
1184 $self->check_size; 1393 $self->check_size;
1185} 1394}
1186 1395
1187sub _draw { 1396sub _draw {
1188 my ($self) = @_; 1397 my ($self) = @_;
1189 1398
1190 my $tex = $self->{texture} ||= do { 1399 my $tex = $self->{texture} ||= do {
1400 $self->{layout}->set_font ($self->{font}) if $self->{font};
1191 $self->{layout}->set_width ($self->{w}); 1401 $self->{layout}->set_width ($self->{w});
1192 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1402 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1193 new_from_layout CFClient::Texture $self->{layout} 1403 new_from_layout CFClient::Texture $self->{layout}
1194 }; 1404 };
1195 1405
1228 1438
1229sub new { 1439sub new {
1230 my $class = shift; 1440 my $class = shift;
1231 1441
1232 $class->SUPER::new ( 1442 $class->SUPER::new (
1233 fg => [1, 1, 1], 1443 fg => [1, 1, 1],
1234 bg => [0, 0, 0, 0.2], 1444 bg => [0, 0, 0, 0.2],
1235 active_bg => [1, 1, 1, 0.5], 1445 active_bg => [1, 1, 1, 0.5],
1236 active_fg => [0, 0, 0], 1446 active_fg => [0, 0, 0],
1237 can_hover => 1, 1447 can_hover => 1,
1238 can_focus => 1, 1448 can_focus => 1,
1239 valign => 0, 1449 valign => 0,
1240 can_events => 1, 1450 can_events => 1,
1241 @_ 1451 @_
1242 ) 1452 )
1243} 1453}
1244 1454
1398 my ($self, $ev) = @_; 1608 my ($self, $ev) = @_;
1399 1609
1400 my $sym = $ev->{sym}; 1610 my $sym = $ev->{sym};
1401 1611
1402 if ($sym == 13) { 1612 if ($sym == 13) {
1613 unshift @{$self->{history}},
1614 my $txt = $self->get_text;
1615 $self->{history_pointer} = -1;
1616 $self->{history_saveback} = '';
1403 $self->emit (activate => $self->get_text); 1617 $self->emit (activate => $txt);
1404 $self->update; 1618 $self->update;
1619
1620 } elsif ($sym == CFClient::SDLK_UP) {
1621 if ($self->{history_pointer} < 0) {
1622 $self->{history_saveback} = $self->get_text;
1623 }
1624 if (@{$self->{history} || []} > 0) {
1625 $self->{history_pointer}++;
1626 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1627 $self->{history_pointer} = @{$self->{history} || []} - 1;
1628 }
1629 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1630 }
1631
1632 } elsif ($sym == CFClient::SDLK_DOWN) {
1633 $self->{history_pointer}--;
1634 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1635
1636 if ($self->{history_pointer} >= 0) {
1637 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1638 } else {
1639 $self->set_text ($self->{history_saveback});
1640 }
1405 1641
1406 } else { 1642 } else {
1407 $self->SUPER::key_down ($ev); 1643 $self->SUPER::key_down ($ev);
1408 } 1644 }
1409 1645
1423 1659
1424sub new { 1660sub new {
1425 my $class = shift; 1661 my $class = shift;
1426 1662
1427 $class->SUPER::new ( 1663 $class->SUPER::new (
1428 padding => 4, 1664 padding => 4,
1429 fg => [1, 1, 1], 1665 fg => [1, 1, 1],
1430 bg => [1, 1, 1, 0.2], 1666 bg => [1, 1, 1, 0.2],
1431 active_fg => [0, 0, 1], 1667 active_fg => [0, 0, 1],
1432 can_hover => 1, 1668 can_hover => 1,
1433 align => 0, 1669 align => 0,
1434 valign => 0, 1670 valign => 0,
1435 can_events => 1, 1671 can_events => 1,
1436 @_ 1672 @_
1437 ) 1673 )
1438} 1674}
1439 1675
1598 1834
1599package CFClient::UI::VGauge; 1835package CFClient::UI::VGauge;
1600 1836
1601our @ISA = CFClient::UI::Base::; 1837our @ISA = CFClient::UI::Base::;
1602 1838
1839use List::Util qw(min max);
1840
1603use CFClient::OpenGL; 1841use CFClient::OpenGL;
1604 1842
1605my %tex = ( 1843my %tex = (
1606 food => [ 1844 food => [
1607 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1845 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1608 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1846 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1609 ], 1847 ],
1610 grace => [ 1848 grace => [
1611 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1849 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1612 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1850 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1613 ], 1851 ],
1614 hp => [ 1852 hp => [
1615 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1853 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1616 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1854 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1617 ], 1855 ],
1618 mana => [ 1856 mana => [
1619 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1857 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1620 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1858 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1621 ], 1859 ],
1622); 1860);
1623 1861
1624# eg. VGauge->new (gauge => 'food'), default gauge: food 1862# eg. VGauge->new (gauge => 'food'), default gauge: food
1625sub new { 1863sub new {
1644} 1882}
1645 1883
1646sub set_max { 1884sub set_max {
1647 my ($self, $max) = @_; 1885 my ($self, $max) = @_;
1648 1886
1887 return if $self->{max_val} == $max;
1888
1649 $self->{max_val} = $max; 1889 $self->{max_val} = $max;
1890 $self->update;
1650} 1891}
1651 1892
1652sub set_value { 1893sub set_value {
1653 my ($self, $val, $max) = @_; 1894 my ($self, $val, $max) = @_;
1654 1895
1655 $self->set_max ($max) 1896 $self->set_max ($max)
1656 if defined $max; 1897 if defined $max;
1657 1898
1658 $max = $self->{max_val}; 1899 return if $self->{val} == $val;
1900
1659 $self->{val} = $val; 1901 $self->{val} = $val;
1660
1661 $self->update; 1902 $self->update;
1662} 1903}
1663 1904
1664sub _draw { 1905sub _draw {
1665 my ($self) = @_; 1906 my ($self) = @_;
1666 1907
1667 my $tex = $tex{$self->{type}}; 1908 my $tex = $tex{$self->{type}};
1909 my ($t1, $t2, $t3) = @$tex;
1668 1910
1669 my ($w, $h) = ($self->{w}, $self->{h}); 1911 my ($w, $h) = ($self->{w}, $self->{h});
1670 1912
1671 if ($self->{vertical}) { 1913 if ($self->{vertical}) {
1672 glRotate 90, 0, 0, 1; 1914 glRotate 90, 0, 0, 1;
1674 1916
1675 ($w, $h) = ($h, $w); 1917 ($w, $h) = ($h, $w);
1676 } 1918 }
1677 1919
1678 my $ycut = $self->{val} / ($self->{max_val} || 1); 1920 my $ycut = $self->{val} / ($self->{max_val} || 1);
1679 $ycut = 1 if $self->{val} > $self->{max_val};
1680 1921
1681 my $t1 = $tex->[0]; 1922 my $ycut1 = max 0, min 1, $ycut;
1682 my $t2 = $tex->[1]; 1923 my $ycut2 = max 0, min 1, $ycut - 1;
1924
1925 my $h1 = $self->{h} * (1 - $ycut1);
1926 my $h2 = $self->{h} * (1 - $ycut2);
1683 1927
1684 glEnable GL_BLEND; 1928 glEnable GL_BLEND;
1685 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1929 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1686 glEnable GL_TEXTURE_2D; 1930 glEnable GL_TEXTURE_2D;
1687 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1931 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1688 1932
1689 my $h1 = $self->{h} - $ycut * $self->{h};
1690 my $h2 = $ycut * $self->{h};
1691
1692 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1933 glBindTexture GL_TEXTURE_2D, $t1->{name};
1693 glBegin GL_QUADS; 1934 glBegin GL_QUADS;
1694 glTexCoord 0 , 0; glVertex 0 , 0; 1935 glTexCoord 0 , 0; glVertex 0 , 0;
1695 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1936 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1696 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1937 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1697 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1938 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1698 glEnd; 1939 glEnd;
1699 1940
1941 my $ycut1 = List::Util::min 1, $ycut;
1700 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1942 glBindTexture GL_TEXTURE_2D, $t2->{name};
1701 glBegin GL_QUADS; 1943 glBegin GL_QUADS;
1702 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1944 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1703 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1945 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1704 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1946 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1705 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1947 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1706 glEnd; 1948 glEnd;
1949
1950 if ($t3) {
1951 glBindTexture GL_TEXTURE_2D, $t3->{name};
1952 glBegin GL_QUADS;
1953 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1954 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1955 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1956 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1957 glEnd;
1958 }
1707 1959
1708 glDisable GL_BLEND; 1960 glDisable GL_BLEND;
1709 glDisable GL_TEXTURE_2D; 1961 glDisable GL_TEXTURE_2D;
1710} 1962}
1711 1963
1717 1969
1718sub new { 1970sub new {
1719 my ($class, %arg) = @_; 1971 my ($class, %arg) = @_;
1720 1972
1721 my $self = $class->SUPER::new ( 1973 my $self = $class->SUPER::new (
1722 tooltip => $arg{type}, 1974 tooltip => $arg{type},
1975 can_hover => 1,
1976 can_events => 1,
1723 %arg, 1977 %arg,
1724 ); 1978 );
1725 1979
1726 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1980 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1727 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1981 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1728 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); 1982 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1729 1983
1730 $self 1984 $self
1731} 1985}
1732 1986
1735 1989
1736 $self->{value}->set_fontsize ($fsize); 1990 $self->{value}->set_fontsize ($fsize);
1737 $self->{max} ->set_fontsize ($fsize); 1991 $self->{max} ->set_fontsize ($fsize);
1738} 1992}
1739 1993
1994sub set_max {
1995 my ($self, $max) = @_;
1996
1997 $self->{gauge}->set_max ($max);
1998 $self->{max}->set_text ($max);
1999}
2000
1740sub set_value { 2001sub set_value {
1741 my ($self, $val, $max) = @_; 2002 my ($self, $val, $max) = @_;
1742 2003
1743 $self->set_max ($max) 2004 $self->set_max ($max)
1744 if defined $max; 2005 if defined $max;
1745 2006
1746 $self->{gauge}->set_value ($val, $max); 2007 $self->{gauge}->set_value ($val, $max);
1747 $self->{value}->set_text ($val); 2008 $self->{value}->set_text ($val);
1748}
1749
1750sub set_max {
1751 my ($self, $max) = @_;
1752
1753 $self->{gauge}->set_max ($max);
1754 $self->{max}->set_text ($max);
1755} 2009}
1756 2010
1757############################################################################# 2011#############################################################################
1758 2012
1759package CFClient::UI::Slider; 2013package CFClient::UI::Slider;
1778 # TODO: calculations are off 2032 # TODO: calculations are off
1779 my $self = $class->SUPER::new ( 2033 my $self = $class->SUPER::new (
1780 fg => [1, 1, 1], 2034 fg => [1, 1, 1],
1781 active_fg => [0, 0, 0], 2035 active_fg => [0, 0, 0],
1782 range => [0, 0, 100, 10], 2036 range => [0, 0, 100, 10],
1783 req_w => 20, 2037 req_w => $::WIDTH / 80,
1784 req_h => 20, 2038 req_h => $::WIDTH / 80,
1785 vertical => 0, 2039 vertical => 0,
1786 can_hover => 1, 2040 can_hover => 1,
1787 inner_pad => 5, 2041 inner_pad => 5,
1788 @_ 2042 @_
1789 ); 2043 );
1899 2153
1900sub new { 2154sub new {
1901 my $class = shift; 2155 my $class = shift;
1902 2156
1903 my $self = $class->SUPER::new ( 2157 my $self = $class->SUPER::new (
1904 fontsize => 1, 2158 fontsize => 1,
2159 can_events => 0,
2160 #font => default_font
1905 @_, 2161 @_,
1906 2162
1907 layout => (new CFClient::Layout), 2163 layout => (new CFClient::Layout),
1908 par => [], 2164 par => [],
1909 height => 0, 2165 height => 0,
1910 children => [ 2166 children => [
1911 (new CFClient::UI::Empty expand => 1), 2167 (new CFClient::UI::Empty expand => 1),
1912 (new CFClient::UI::Slider vertical => 1), 2168 (new CFClient::UI::Slider vertical => 1),
1913 ], 2169 ],
1914 ); 2170 );
1915 2171
1916 $self->{children}[1]->connect (changed => sub { 2172 $self->{children}[1]->connect (changed => sub { $self->update });
1917 $self->update;
1918 });
1919 2173
1920 $self 2174 $self
1921} 2175}
1922 2176
1923sub set_fontsize { 2177sub set_fontsize {
1931 my ($self, $text) = @_; 2185 my ($self, $text) = @_;
1932 2186
1933 my $layout = $self->{layout}; 2187 my $layout = $self->{layout};
1934 2188
1935 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2189 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1936 $layout->set_width ($self->{w}); 2190 $layout->set_width ($self->{children}[0]{w});
1937 $layout->set_text ($text); 2191 $layout->set_text ($text);
1938 2192
1939 ($layout->size)[1] 2193 ($layout->size)[1]
1940} 2194}
1941 2195
1949sub size_allocate { 2203sub size_allocate {
1950 my ($self, $w, $h) = @_; 2204 my ($self, $w, $h) = @_;
1951 2205
1952 $self->SUPER::size_allocate ($w, $h); 2206 $self->SUPER::size_allocate ($w, $h);
1953 2207
2208 $self->{layout}->set_font ($self->{font}) if $self->{font};
1954 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2209 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1955 $self->{layout}->set_width ($self->{children}[0]{w}); 2210 $self->{layout}->set_width ($self->{children}[0]{w});
1956 2211
1957 $self->reflow; 2212 $self->reflow;
1958} 2213}
1993 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2248 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1994 2249
1995 delete $self->{texture}; 2250 delete $self->{texture};
1996 } 2251 }
1997 2252
1998 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2253 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
1999 glClearColor 0, 0, 0, 1; 2254 glClearColor 0, 0, 0, 0;
2000 glClear GL_COLOR_BUFFER_BIT; 2255 glClear GL_COLOR_BUFFER_BIT;
2001 2256
2002 glEnable GL_BLEND; 2257 glEnable GL_BLEND;
2003 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2258 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2004 glEnable GL_TEXTURE_2D; 2259 glEnable GL_TEXTURE_2D;
2011 2266
2012 my $y = 0; 2267 my $y = 0;
2013 2268
2014 my $layout = $self->{layout}; 2269 my $layout = $self->{layout};
2015 2270
2271 $layout->set_font ($self->{font}) if $self->{font};
2272
2016 for my $par (@{$self->{par}}) { 2273 for my $par (@{$self->{par}}) {
2017 my $h = $par->[0]; 2274 my $h = $par->[0];
2018 2275
2019 if ($y0 < $y + $h && $y < $y1) { 2276 if ($y0 < $y + $h && $y < $y1) {
2020 $layout->set_text ($par->[2]); 2277 $layout->set_text ($par->[2]);
2034} 2291}
2035 2292
2036sub _draw { 2293sub _draw {
2037 my ($self) = @_; 2294 my ($self) = @_;
2038 2295
2039 if ($self->{texture}) { 2296 glEnable GL_BLEND;
2297 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2040 glEnable GL_TEXTURE_2D; 2298 glEnable GL_TEXTURE_2D;
2041 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2299 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2300 glColor 1, 1, 1, 1;
2042 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2301 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2043 glDisable GL_TEXTURE_2D; 2302 glDisable GL_TEXTURE_2D;
2044 } 2303 glDisable GL_BLEND;
2045 2304
2046 $self->{children}[1]->draw; 2305 $self->{children}[1]->draw;
2047 2306
2048} 2307}
2049 2308
2097 2356
2098sub new { 2357sub new {
2099 my $class = shift; 2358 my $class = shift;
2100 2359
2101 my $self = $class->SUPER::new ( 2360 my $self = $class->SUPER::new (
2102 state => 0, 2361 state => 0,
2103 connect_activate => \&toggle_flopper, 2362 connect_activate => \&toggle_flopper,
2104 can_events => 1,
2105 @_ 2363 @_
2106 ); 2364 );
2107 2365
2108 if ($self->{state}) { 2366 if ($self->{state}) {
2109 $self->{state} = 0; 2367 $self->{state} = 0;
2129 $self->emit (changed => $self->{state}); 2387 $self->emit (changed => $self->{state});
2130} 2388}
2131 2389
2132############################################################################# 2390#############################################################################
2133 2391
2392package CFClient::UI::Tooltip;
2393
2394our @ISA = CFClient::UI::Bin::;
2395
2396use CFClient::OpenGL;
2397
2398sub new {
2399 my $class = shift;
2400
2401 $class->SUPER::new (
2402 @_,
2403 can_events => 0,
2404 )
2405}
2406
2407sub set_markup {
2408 my ($self, $text) = @_;
2409
2410 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2411 $self->{label}->set_markup ($text);
2412 $self->add ($self->{label});
2413}
2414
2415sub size_request {
2416 my ($self) = @_;
2417
2418 $self->child->set_max_size ($::WIDTH * 0.3);
2419
2420 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2421
2422 ($w + 4, $h + 4)
2423}
2424
2425sub size_allocate {
2426 my ($self, $w, $h) = @_;
2427
2428 $self->SUPER::size_allocate ($w - 4, $h - 4);
2429}
2430
2431sub _draw {
2432 my ($self) = @_;
2433
2434 glPushMatrix;
2435 glTranslate 0.375, 0.375;
2436
2437 my ($w, $h) = @$self{qw(w h)};
2438
2439 glColor 1, 0.8, 0.4;
2440 glBegin GL_QUADS;
2441 glVertex 0 , 0;
2442 glVertex 0 , $h;
2443 glVertex $w, $h;
2444 glVertex $w, 0;
2445 glEnd;
2446
2447 glColor 0, 0, 0;
2448 glBegin GL_LINE_LOOP;
2449 glVertex 0 , 0;
2450 glVertex 0 , $h;
2451 glVertex $w, $h;
2452 glVertex $w, 0;
2453 glEnd;
2454
2455 glPopMatrix;
2456
2457 glTranslate 2, 2;
2458 $self->SUPER::_draw;
2459}
2460
2461#############################################################################
2462
2463package CFClient::UI::Face;
2464
2465our @ISA = CFClient::UI::Base::;
2466
2467use CFClient::OpenGL;
2468
2469sub new {
2470 my $class = shift;
2471
2472 $class->SUPER::new (
2473 aspect => 1,
2474 @_,
2475 )
2476}
2477
2478sub size_request {
2479 (32, 8)
2480}
2481
2482sub _draw {
2483 my ($self) = @_;
2484
2485 return unless $::CONN;#d# manage and cache textures differently
2486 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2487
2488 # TODO animation
2489 if ($tex) {
2490 glEnable GL_BLEND;
2491 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2492 glEnable GL_TEXTURE_2D;
2493 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2494 glColor 1, 1, 1, 1;
2495 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2496 glDisable GL_TEXTURE_2D;
2497 glDisable GL_BLEND;
2498 }
2499}
2500
2501#############################################################################
2502
2503package CFClient::UI::InventoryItem;
2504
2505our @ISA = CFClient::UI::HBox::;
2506
2507sub new {
2508 my $class = shift;
2509
2510 my %args = @_;
2511
2512 my $item = $args{item};
2513
2514 my $desc = $item->{nrof} < 2
2515 ? $item->{name}
2516 : "$item->{nrof} $item->{name_pl}";
2517
2518
2519 my $self = $class->SUPER::new (
2520 can_hover => 1,
2521 can_events => 1,
2522 tooltip => (CFClient::UI::Label->escape ($desc)
2523 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2524 connect_button_down => sub {
2525 my ($self, $ev, $x, $y) = @_;
2526
2527 # todo: maybe put examine on 1? but should just be a tooltip :(
2528 if ($ev->{button} == 1) {
2529 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2530 } elsif ($ev->{button} == 2) {
2531 $::CONN->send ("apply $item->{tag}");
2532 } elsif ($ev->{button} == 3) {
2533 CFClient::UI::Menu->new (
2534 items => [
2535 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2536 [
2537 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2538 sub { $::CONN->send ("lock $item->{tag}") },
2539 ],
2540 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2541 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2542 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2543 ],
2544 )->popup ($ev);
2545 }
2546
2547 1
2548 },
2549 %args
2550 );
2551 $self->add(new CFClient::UI::Face
2552 can_events => 0,
2553 face => $item->{face},
2554 anim => $item->{anim},
2555 animspeed => $item->{animspeed});
2556 $self->add(new CFClient::UI::Label
2557 can_events => 0,
2558 text => $desc);
2559
2560 $self
2561}
2562
2563#############################################################################
2564
2565package CFClient::UI::Inventory;
2566
2567our @ISA = CFClient::UI::ScrolledWindow::;
2568
2569sub new {
2570 my $class = shift;
2571
2572 my $self = $class->SUPER::new (
2573 scrolled => (new CFClient::UI::VBox),
2574 @_,
2575 );
2576
2577 $self
2578}
2579
2580sub set_items {
2581 my ($self, $items) = @_;
2582
2583 $self->{scrolled}->clear;
2584 return unless $items;
2585
2586 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2587
2588 $self->{real_items} = \@items;
2589
2590 for my $item (@items) {
2591 my $desc = $item->{nrof} < 2
2592 ? $item->{name}
2593 : "$item->{nrof} $item->{name_pl}";
2594
2595 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item);
2596 }
2597
2598# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2599}
2600
2601sub size_request {
2602 my ($self) = @_;
2603 ($self->{req_w}, $self->{req_h});
2604}
2605
2606#############################################################################
2607
2608package CFClient::UI::Menu;
2609
2610our @ISA = CFClient::UI::FancyFrame::;
2611
2612use CFClient::OpenGL;
2613
2614sub new {
2615 my $class = shift;
2616
2617 my $self = $class->SUPER::new (
2618 items => [],
2619 z => 100,
2620 @_,
2621 );
2622
2623 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2624
2625 for my $item (@{ $self->{items} }) {
2626 my ($widget, $cb) = @$item;
2627
2628 # handle various types of items, only text for now
2629 if (!ref $widget) {
2630 $widget = new CFClient::UI::Label
2631 can_hover => 1,
2632 can_events => 1,
2633 text => $widget;
2634 }
2635
2636 $self->{item}{$widget} = $item;
2637
2638 $self->{vbox}->add ($widget);
2639 }
2640
2641 $self
2642}
2643
2644# popup given the event (must be a mouse button down event currently)
2645sub popup {
2646 my ($self, $ev) = @_;
2647
2648 $self->emit ("popdown");
2649
2650 # maybe save $GRAB? must be careful about events...
2651 $GRAB = $self;
2652 $self->{button} = $ev->{button};
2653
2654 $self->show;
2655 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2656}
2657
2658sub mouse_motion {
2659 my ($self, $ev, $x, $y) = @_;
2660
2661 # TODO: should use vbox->find_widget or so
2662 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2663 $self->{hover} = $self->{item}{$HOVER};
2664}
2665
2666sub button_up {
2667 my ($self, $ev, $x, $y) = @_;
2668
2669 if ($ev->{button} == $self->{button}) {
2670 undef $GRAB;
2671 $self->hide;
2672
2673 $self->emit ("popdown");
2674 $self->{hover}[1]->() if $self->{hover};
2675 }
2676}
2677
2678#############################################################################
2679
2134package CFClient::UI::Root; 2680package CFClient::UI::Root;
2135 2681
2136our @ISA = CFClient::UI::Container::; 2682our @ISA = CFClient::UI::Container::;
2137 2683
2138use CFClient::OpenGL; 2684use CFClient::OpenGL;
2139 2685
2140sub check_size { 2686sub check_size {
2141 my ($self) = @_; 2687 my ($self) = @_;
2142 2688
2143 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2689 $self->configure (0, 0, $self->{w}, $self->{h});
2144} 2690}
2145 2691
2146sub size_request { 2692sub size_request {
2147 ($::WIDTH, $::HEIGHT) 2693 my ($self) = @_;
2694
2695 ($self->{w}, $self->{h})
2696}
2697
2698sub size_allocate {
2699 my ($self, $w, $h) = @_;
2700
2701 my $old_w = $self->{old_w};
2702 my $old_h = $self->{old_h};
2703
2704 if ($old_w && $old_h) {
2705 for my $child ($self->children) {
2706 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2707 $child->{w} = int 0.5 + $child->{req_w} * $w / $old_w;
2708 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2709 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2710 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2711 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2712 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2713 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2714 }
2715 }
2716
2717 $self->{old_w} = $w;
2718 $self->{old_h} = $h;
2148} 2719}
2149 2720
2150sub configure { 2721sub configure {
2151 my ($self, $x, $y, $w, $h) = @_; 2722 my ($self, $x, $y, $w, $h) = @_;
2152 2723
2153 $self->SUPER::configure ($x, $y, $w, $h); 2724 $self->SUPER::configure ($x, $y, $w, $h);
2154 2725
2155 for my $child (@{$self->{children}}) { 2726 for my $child ($self->children) {
2156 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2727 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2157 2728
2158 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2729 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2159 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2730 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2160 $child->configure ($X, $Y, $W,$H); 2731 $child->configure ($X, $Y, $W, $H);
2161 } 2732 }
2162} 2733}
2163 2734
2164sub _topleft { 2735sub _topleft {
2165 my ($self, $x, $y) = @_; 2736 my ($self, $x, $y) = @_;
2213############################################################################# 2784#############################################################################
2214 2785
2215package CFClient::UI; 2786package CFClient::UI;
2216 2787
2217$ROOT = new CFClient::UI::Root; 2788$ROOT = new CFClient::UI::Root;
2218$TOOLTIP = new CFClient::UI::Label fontsize => 0.8, can_events => 0; 2789$TOOLTIP = new CFClient::UI::Tooltip;
2219 2790
22201 27911
2221 2792

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines