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.225 by root, Tue May 23 21:14:41 2006 UTC vs.
Revision 1.247 by root, Sun May 28 01:16:04 2006 UTC

5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8 8
9use CFClient; 9use CFClient;
10use CFClient::Texture;
10 11
11our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
12 13
14our $LAYOUT;
13our $ROOT; 15our $ROOT;
14our $TOOLTIP; 16our $TOOLTIP;
15our $BUTTON_STATE; 17our $BUTTON_STATE;
16 18
17our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20
21sub get_layout {
22 for (grep { $_->{name} } values %WIDGET) {
23 $LAYOUT->{$_->{name}} = {
24 x => $_->{x} / $::WIDTH,
25 y => $_->{y} / $::HEIGHT,
26 w => $_->{w} / $::WIDTH,
27 h => $_->{h} / $::HEIGHT
28 };
29 }
30
31 return $LAYOUT;
32}
33
34sub set_layout {
35 my ($layout) = @_;
36 $LAYOUT = $layout;
37}
18 38
19sub check_tooltip { 39sub check_tooltip {
20 if (!$GRAB) { 40 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 42 if (length $widget->{tooltip}) {
50 delete $TOOLTIP->{owner}; 70 delete $TOOLTIP->{owner};
51} 71}
52 72
53# class methods for events 73# class methods for events
54sub feed_sdl_key_down_event { 74sub feed_sdl_key_down_event {
55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 75 $FOCUS->emit (key_down => $_[0])
56 if $FOCUS; 76 if $FOCUS;
57} 77}
58 78
59sub feed_sdl_key_up_event { 79sub feed_sdl_key_up_event {
60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 80 $FOCUS->emit (key_up => $_[0])
61 if $FOCUS; 81 if $FOCUS;
62} 82}
63 83
64sub feed_sdl_button_down_event { 84sub feed_sdl_button_down_event {
65 my ($ev) = @_; 85 my ($ev) = @_;
74 check_tooltip; 94 check_tooltip;
75 } 95 }
76 96
77 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 97 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
78 98
79 if ($GRAB) { 99 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
80 ($x, $y) = $GRAB->coord2local ($x, $y); 100 if $GRAB;
81 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
82 }
83} 101}
84 102
85sub feed_sdl_button_up_event { 103sub feed_sdl_button_up_event {
86 my ($ev) = @_; 104 my ($ev) = @_;
87 my ($x, $y) = ($ev->{x}, $ev->{y}); 105 my ($x, $y) = ($ev->{x}, $ev->{y});
88 106
89 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 107 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
90 108
91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 109 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
92 110
93 if ($GRAB) { 111 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
94 ($x, $y) = $GRAB->coord2local ($x, $y); 112 if $GRAB;
95 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
96 }
97 113
98 if (!$BUTTON_STATE) { 114 if (!$BUTTON_STATE) {
99 my $grab = $GRAB; undef $GRAB; 115 my $grab = $GRAB; undef $GRAB;
100 $grab->update if $grab; 116 $grab->update if $grab;
101 $GRAB->update if $GRAB; 117 $GRAB->update if $GRAB;
117 $HOVER->update if $HOVER && $HOVER->{can_hover}; 133 $HOVER->update if $HOVER && $HOVER->{can_hover};
118 134
119 check_tooltip; 135 check_tooltip;
120 } 136 }
121 137
122 if ($HOVER) {
123 ($x, $y) = $HOVER->coord2local ($x, $y);
124 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); 138 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
125 } 139 if $HOVER;
126} 140}
127 141
128# convert position array to integers 142# convert position array to integers
129sub harmonize { 143sub harmonize {
130 my ($vals) = @_; 144 my ($vals) = @_;
144 158
145 $_->update 159 $_->update
146 for @widgets; 160 for @widgets;
147} 161}
148 162
163sub reconfigure_widgets {
164 # make a copy, otherwise C<for> complains about freed values.
165 my @widgets = values %WIDGET;
166
167 $_->reconfigure
168 for @widgets;
169}
170
149# call when resolution changes etc. 171# call when resolution changes etc.
150sub rescale_widgets { 172sub rescale_widgets {
151 my ($sx, $sy) = @_; 173 my ($sx, $sy) = @_;
152 174
153 # make a copy, otherwise for complains about freed values.
154 my @widgets = values %WIDGET; 175 for my $widget (values %WIDGET) {
155
156 for my $widget (@widgets) {
157 if ($widget->{toplevel}) { 176 if ($widget->{is_toplevel}) {
158 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 177 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
159 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 178 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
160 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 179 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
161 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
162 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 180 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
163 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 181 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
164 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 182 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
165 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
166 } 183 }
167
168 $widget->reconfigure;
169 } 184 }
185
186 reconfigure_widgets;
170} 187}
171 188
172############################################################################# 189#############################################################################
173 190
174package CFClient::UI::Base; 191package CFClient::UI::Base;
187 can_events => 1, 204 can_events => 1,
188 @_ 205 @_
189 }, $class; 206 }, $class;
190 207
191 for (keys %$self) { 208 for (keys %$self) {
192 if (/^connect_(.*)$/) { 209 if (/^on_(.*)$/) {
193 $self->connect ($1 => delete $self->{$_}); 210 $self->connect ($1 => delete $self->{$_});
194 } 211 }
195 } 212 }
196 213
197 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 214 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
198 215
216 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
217 $self->{req_x} = $layout->{x} * $::WIDTH;
218 $self->{req_y} = $layout->{y} * $::HEIGHT;
219 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
220 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
221 }
222
199 $self 223 $self
224}
225
226sub toggle_visibility {
227 my ($self) = @_;
228
229 if ($self->{visible}) {
230 $self->hide;
231 } else {
232 $self->show;
233 }
200} 234}
201 235
202sub destroy { 236sub destroy {
203 my ($self) = @_; 237 my ($self) = @_;
204 238
211 return if $self->{parent}; 245 return if $self->{parent};
212 246
213 $CFClient::UI::ROOT->add ($self); 247 $CFClient::UI::ROOT->add ($self);
214} 248}
215 249
216sub show_centered { 250sub center {
217 my ($self) = @_; 251 my ($self) = @_;
218 return if $self->{parent};
219
220 $self->show;
221 252
222 $CFClient::UI::ROOT->on_post_alloc ( 253 $CFClient::UI::ROOT->on_post_alloc (
223 "centered $self" => sub { 254 "center_$self" => sub {
224 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 255 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
225 }, 256 },
226 ); 257 );
227}
228 258
229sub hide { 259 $self->update;
260}
261
262sub set_visible {
230 my ($self) = @_; 263 my ($self) = @_;
264
265 return if $self->{visible};
266
267 $self->{root} = $self->{parent}{root};
268 $self->{visible} = $self->{parent}{visible} + 1;
269
270 $self->emit (visibility_change => 1)
271}
272
273sub set_invisible {
274 my ($self) = @_;
275
276 return unless $self->{visible};
277
278 # broken show/hide model
279
280 delete $self->{root};
281 delete $self->{visible};
231 282
232 undef $GRAB if $GRAB == $self; 283 undef $GRAB if $GRAB == $self;
233 undef $HOVER if $HOVER == $self; 284 undef $HOVER if $HOVER == $self;
285
286 CFClient::UI::check_tooltip
287 if $CFClient::UI::TOOLTIP->{owner} == $self;
288
289 $self->focus_out;
290
291 $self->emit (visibility_change => 0);
292}
293
294sub hide {
295 my ($self) = @_;
296
297 $self->set_invisible;
234 298
235 $self->{parent}->remove ($self) 299 $self->{parent}->remove ($self)
236 if $self->{parent}; 300 if $self->{parent};
237} 301}
238 302
247} 311}
248 312
249sub set_size { 313sub set_size {
250 my ($self, $w, $h) = @_; 314 my ($self, $w, $h) = @_;
251 315
252 $self->{user_w} = $w; 316 $self->{def_w} = $w;
253 $self->{user_h} = $h; 317 $self->{def_h} = $h;
254 318
255 $self->check_size; 319 $self->check_size;
256} 320}
257 321
258sub size_request { 322sub size_request {
308} 372}
309 373
310sub set_tooltip { 374sub set_tooltip {
311 my ($self, $tooltip) = @_; 375 my ($self, $tooltip) = @_;
312 376
377 $tooltip =~ s/^\s+//;
378 $tooltip =~ s/\s+$//;
379
380 return if $self->{tooltip} eq $tooltip;
381
313 $self->{tooltip} = $tooltip; 382 $self->{tooltip} = $tooltip;
314 383
315 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 384 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
316 delete $CFClient::UI::TOOLTIP->{owner}; 385 delete $CFClient::UI::TOOLTIP->{owner};
317 CFClient::UI::check_tooltip; 386 CFClient::UI::check_tooltip;
338 return if $FOCUS == $self; 407 return if $FOCUS == $self;
339 return unless $self->{can_focus}; 408 return unless $self->{can_focus};
340 409
341 my $focus = $FOCUS; $FOCUS = $self; 410 my $focus = $FOCUS; $FOCUS = $self;
342 411
343 $self->emit (focus_in => $focus); 412 $self->_emit (focus_in => $focus);
344 413
345 $focus->update if $focus; 414 $focus->update if $focus;
346 $FOCUS->update; 415 $FOCUS->update;
347} 416}
348 417
351 420
352 return unless $FOCUS == $self; 421 return unless $FOCUS == $self;
353 422
354 my $focus = $FOCUS; undef $FOCUS; 423 my $focus = $FOCUS; undef $FOCUS;
355 424
356 $self->emit (focus_out => $focus); 425 $self->_emit (focus_out => $focus);
357 426
358 $focus->update if $focus; #? 427 $focus->update if $focus; #?
428
429 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
430 unless $FOCUS;
359} 431}
360 432
361sub mouse_motion { } 433sub mouse_motion { }
362sub button_up { } 434sub button_up { }
363sub key_down { } 435sub key_down { }
364sub key_up { } 436sub key_up { }
365 437
366sub button_down { 438sub button_down {
367 my ($self, $ev, $x, $y) = @_; 439 my ($self, $ev, $x, $y) = @_;
368 440
369 $self->focus_in; 441 $self->focus_in;
403 if ($ENV{PCLIENT_DEBUG}) { 475 if ($ENV{PCLIENT_DEBUG}) {
404 glPushMatrix; 476 glPushMatrix;
405 glColor 1, 1, 0, 1; 477 glColor 1, 1, 0, 1;
406 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 478 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
407 glBegin GL_LINE_LOOP; 479 glBegin GL_LINE_LOOP;
408 glVertex 0 , 0; 480 glVertex 0 , 0;
409 glVertex $self->{w}, 0; 481 glVertex $self->{w} - 1, 0;
410 glVertex $self->{w}, $self->{h}; 482 glVertex $self->{w} - 1, $self->{h} - 1;
411 glVertex 0 , $self->{h}; 483 glVertex 0 , $self->{h} - 1;
412 glEnd; 484 glEnd;
413 glPopMatrix; 485 glPopMatrix;
414 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 486 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
415 } 487 }
416} 488}
436sub set_parent { 508sub set_parent {
437 my ($self, $parent) = @_; 509 my ($self, $parent) = @_;
438 510
439 Scalar::Util::weaken ($self->{parent} = $parent); 511 Scalar::Util::weaken ($self->{parent} = $parent);
440 512
441 # TODO: req_w _does_change after ->reconfigure 513 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
514
442 $self->check_size 515 $self->check_size;
443 unless exists $self->{req_w};
444
445 $self->show;
446} 516}
447 517
448sub check_size { 518sub check_size {
449 my ($self, $forced) = @_; 519 my ($self, $forced) = @_;
450 520
463 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
464 534
465 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
466} 536}
467 537
538sub _emit {
539 my ($self, $signal, @args) = @_;
540
541 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
542}
543
468sub emit { 544sub emit {
469 my ($self, $signal, @args) = @_; 545 my ($self, $signal, @args) = @_;
470 546
471 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} 547 $self->_emit ($signal, @args)
548 || $self->$signal (@args);
549}
550
551sub visibility_change {
552 #my ($self, $visible) = @_;
472} 553}
473 554
474sub DESTROY { 555sub DESTROY {
475 my ($self) = @_; 556 my ($self) = @_;
476 557
589 delete $child->{parent}; 670 delete $child->{parent};
590 $child->hide; 671 $child->hide;
591 672
592 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 673 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
593 674
594 $self->check_size; 675 $self->check_size (1);
595 $self->update; 676 $self->update;
596} 677}
597 678
598sub clear { 679sub clear {
599 my ($self) = @_; 680 my ($self) = @_;
739 820
740package CFClient::UI::ViewPort; 821package CFClient::UI::ViewPort;
741 822
742our @ISA = CFClient::UI::Window::; 823our @ISA = CFClient::UI::Window::;
743 824
825sub new {
826 my $class = shift;
827
828 $class->SUPER::new (
829 scroll_x => 0,
830 scroll_y => 1,
831 @_,
832 )
833}
834
744sub size_request { 835sub size_request {
745 my ($self) = @_; 836 my ($self) = @_;
746 837
747 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 838 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
748 839
749 @$self{qw(child_w child_h)} 840 $w = 10 if $self->{scroll_x};
841 $h = 10 if $self->{scroll_y};
842
843 ($w, $h)
750} 844}
751 845
752sub size_allocate { 846sub size_allocate {
753 my ($self, $w, $h) = @_; 847 my ($self, $w, $h) = @_;
754 848
755 my ($cw, $ch) = @$self{qw(child_w child_h)}; 849 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
756# $w = $self->{w}; 850 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
851
757 $self->child->configure (0, 0, $cw, $ch); 852 $self->child->configure (0, 0, $w, $h);
758 $self->update; 853 $self->update;
759} 854}
760 855
761sub set_offset { 856sub set_offset {
762 my ($self, $x, $y) = @_; 857 my ($self, $x, $y) = @_;
813 my $class = shift; 908 my $class = shift;
814 909
815 my $self; 910 my $self;
816 911
817 my $slider = new CFClient::UI::Slider 912 my $slider = new CFClient::UI::Slider
818 vertical => 1, 913 vertical => 1,
819 range => [0, 0, 1, 0.01], # HACK fix 914 range => [0, 0, 1, 0.01], # HACK fix
820 connect_changed => sub { 915 on_changed => sub {
821 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 916 $self->{vp}->set_offset (0, $_[1]);
822 }, 917 },
823 ; 918 ;
824 919
825 $self = $class->SUPER::new ( 920 $self = $class->SUPER::new (
826 vp => (new CFClient::UI::ViewPort expand => 1), 921 vp => (new CFClient::UI::ViewPort expand => 1),
831 $self->{vp}->add ($self->{scrolled}); 926 $self->{vp}->add ($self->{scrolled});
832 $self->add ($self->{vp}); 927 $self->add ($self->{vp});
833 $self->add ($self->{slider}); 928 $self->add ($self->{slider});
834 929
835 $self 930 $self
931}
932
933sub update {
934 my ($self) = @_;
935
936 $self->SUPER::update;
937
938 # todo: overwrite size_allocate of child
939 my $child = $self->{vp}->child;
940 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
941}
942
943sub size_allocate {
944 my ($self, $w, $h) = @_;
945
946 $self->SUPER::size_allocate ($w, $h);
947
948 my $child = $self->{vp}->child;
949 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
836} 950}
837 951
838#TODO# update range on size_allocate depending on child 952#TODO# update range on size_allocate depending on child
839# update viewport offset on scroll 953# update viewport offset on scroll
840 954
891 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1005 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
892 1006
893sub new { 1007sub new {
894 my $class = shift; 1008 my $class = shift;
895 1009
896 # TODO: user_x, user_y, overwrite moveto?
897
898 my $self = $class->SUPER::new ( 1010 my $self = $class->SUPER::new (
899 bg => [1, 1, 1, 1], 1011 bg => [1, 1, 1, 1],
900 border_bg => [1, 1, 1, 1], 1012 border_bg => [1, 1, 1, 1],
901 border => 0.6, 1013 border => 0.6,
902 toplevel => 1, 1014 is_toplevel => 1,
903 can_events => 1, 1015 can_events => 1,
904 @_ 1016 @_
905 ); 1017 );
906 1018
907 $self->{title} &&= new CFClient::UI::Label 1019 $self->{title} &&= new CFClient::UI::Label
908 align => 0, 1020 align => 0,
961 my ($ev, $x, $y) = @_; 1073 my ($ev, $x, $y) = @_;
962 1074
963 my $dx = $ev->{x} - $ox; 1075 my $dx = $ev->{x} - $ox;
964 my $dy = $ev->{y} - $oy; 1076 my $dy = $ev->{y} - $oy;
965 1077
1078 $self->{user_x} = $wx + $dx * $mx;
1079 $self->{user_y} = $wy + $dy * $my;
966 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1080 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
967 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1081 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
968 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1082 $self->move ($self->{user_x}, $self->{user_y});
969 $self->check_size; 1083 $self->check_size;
970 }; 1084 };
971 1085
972 } elsif ($lr ^ $td) { 1086 } elsif ($lr ^ $td) {
973 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1087 my ($ox, $oy) = ($ev->{x}, $ev->{y});
976 $self->{motion} = sub { 1090 $self->{motion} = sub {
977 my ($ev, $x, $y) = @_; 1091 my ($ev, $x, $y) = @_;
978 1092
979 ($x, $y) = ($ev->{x}, $ev->{y}); 1093 ($x, $y) = ($ev->{x}, $ev->{y});
980 1094
981 $self->move ($bx + $x - $ox, $by + $y - $oy); 1095 $self->{user_x} = $bx + $x - $ox;
1096 $self->{user_y} = $by + $y - $oy;
1097
1098 $self->move ($self->{user_x}, $self->{user_y});
982 $self->update; 1099 $self->update;
983 }; 1100 };
984 } 1101 }
985} 1102}
986 1103
1048sub new { 1165sub new {
1049 my $class = shift; 1166 my $class = shift;
1050 1167
1051 $class->SUPER::new ( 1168 $class->SUPER::new (
1052 col_expand => [], 1169 col_expand => [],
1053 @_ 1170 @_,
1054 ) 1171 )
1172}
1173
1174sub children {
1175 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1055} 1176}
1056 1177
1057sub add { 1178sub add {
1058 my ($self, $x, $y, $child) = @_; 1179 my ($self, $x, $y, $child) = @_;
1059 1180
1060 $child->set_parent ($self); 1181 $child->set_parent ($self);
1061 $self->{children}[$y][$x] = $child; 1182 $self->{children}[$y][$x] = $child;
1062 1183
1063 $child->check_size; 1184 $self->check_size (1);
1064} 1185}
1065 1186
1066sub children {
1067 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1068}
1069
1070# TODO: move to container class maybe? send childs a signal on removal? 1187# TODO: move to container class maybe? send children a signal on removal?
1071sub clear { 1188sub clear {
1072 my ($self) = @_; 1189 my ($self) = @_;
1073 1190
1074 my @children = $self->children; 1191 my @children = $self->children;
1075 delete $self->{children}; 1192 delete $self->{children};
1077 for (@children) { 1194 for (@children) {
1078 delete $_->{parent}; 1195 delete $_->{parent};
1079 $_->hide; 1196 $_->hide;
1080 } 1197 }
1081 1198
1199 $self->check_size (1);
1082 $self->update; 1200 $self->update;
1083} 1201}
1084 1202
1085sub get_wh { 1203sub get_wh {
1086 my ($self) = @_; 1204 my ($self) = @_;
1118sub size_allocate { 1236sub size_allocate {
1119 my ($self, $w, $h) = @_; 1237 my ($self, $w, $h) = @_;
1120 1238
1121 my ($ws, $hs) = $self->get_wh; 1239 my ($ws, $hs) = $self->get_wh;
1122 1240
1123 my $req_w = sum @$ws; 1241 my $req_w = (sum @$ws) || 1;
1124 my $req_h = sum @$hs; 1242 my $req_h = (sum @$hs) || 1;
1125 1243
1126 # TODO: nicer code && do row_expand 1244 # TODO: nicer code && do row_expand
1127 my @col_expand = @{$self->{col_expand}}; 1245 my @col_expand = @{$self->{col_expand}};
1128 @col_expand = (1) x @$ws unless @col_expand; 1246 @col_expand = (1) x @$ws unless @col_expand;
1129 my $col_expand = (sum @col_expand) || 1; 1247 my $col_expand = (sum @col_expand) || 1;
1183 } 1301 }
1184} 1302}
1185 1303
1186############################################################################# 1304#############################################################################
1187 1305
1188package CFClient::UI::HBox; 1306package CFClient::UI::Box;
1189
1190# TODO: wrap into common Box base class
1191 1307
1192our @ISA = CFClient::UI::Container::; 1308our @ISA = CFClient::UI::Container::;
1193 1309
1194sub size_request { 1310sub size_request {
1195 my ($self) = @_; 1311 my ($self) = @_;
1196 1312
1197 my @alloc = map [$_->size_request], @{$self->{children}}; 1313 $self->{vertical}
1198 1314 ? (
1199 ( 1315 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1200 (List::Util::sum map $_->[0], @alloc), 1316 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1201 (List::Util::max map $_->[1], @alloc), 1317 )
1202 ) 1318 : (
1319 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1320 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1321 )
1203} 1322}
1204 1323
1205sub size_allocate { 1324sub size_allocate {
1206 my ($self, $w, $h) = @_; 1325 my ($self, $w, $h) = @_;
1207 1326
1208 ($h, $w) = ($w, $h); 1327 my $space = $self->{vertical} ? $h : $w;
1209
1210 my $children = $self->{children}; 1328 my $children = $self->{children};
1211 1329
1212 my @h = map $_->{req_w}, @$children; 1330 my @req;
1213 1331
1214 my $req_h = List::Util::sum @h; 1332 if ($self->{homogeneous}) {
1215 1333 @req = ($space / (@$children || 1)) x @$children;
1216 if ($req_h > $h) {
1217 # ah well, not enough space
1218 $_ *= $h / $req_h for @h;
1219 } else { 1334 } else {
1335 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1336 my $req = List::Util::sum @req;
1337
1338 if ($req > $space) {
1339 # ah well, not enough space
1340 $_ *= $space / $req for @req;
1341 } else {
1220 my $exp = List::Util::sum map $_->{expand}, @$children; 1342 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1221 $exp ||= 1;
1222 1343
1344 $space = ($space - $req) / $expand; # remaining space to give away
1345
1346 $req[$_] += $space * $children->[$_]{expand}
1223 for (0 .. $#$children) { 1347 for 0 .. $#$children;
1224 my $child = $children->[$_];
1225
1226 my $alloc_h = $h[$_];
1227 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1228 $h[$_] = $alloc_h;
1229 } 1348 }
1230 } 1349 }
1231 1350
1232 CFClient::UI::harmonize \@h; 1351 CFClient::UI::harmonize \@req;
1233 1352
1234 my $y = 0; 1353 my $pos = 0;
1235 for (0 .. $#$children) { 1354 for (0 .. $#$children) {
1236 my $child = $children->[$_];
1237 my $h = $h[$_]; 1355 my $alloc = $req[$_];
1238 $child->configure ($y, 0, $h, $w); 1356 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1239 1357
1240 $y += $h; 1358 $pos += $alloc;
1241 } 1359 }
1242 1360
1243 1 1361 1
1244} 1362}
1245 1363
1246############################################################################# 1364#############################################################################
1247 1365
1366package CFClient::UI::HBox;
1367
1368our @ISA = CFClient::UI::Box::;
1369
1370sub new {
1371 my $class = shift;
1372
1373 $class->SUPER::new (
1374 vertical => 0,
1375 @_,
1376 )
1377}
1378
1379#############################################################################
1380
1248package CFClient::UI::VBox; 1381package CFClient::UI::VBox;
1249 1382
1250# TODO: wrap into common Box base class
1251
1252our @ISA = CFClient::UI::Container::; 1383our @ISA = CFClient::UI::Box::;
1253 1384
1254sub size_request { 1385sub new {
1255 my ($self) = @_; 1386 my $class = shift;
1256 1387
1257 my @alloc = map [$_->size_request], @{$self->{children}}; 1388 $class->SUPER::new (
1258 1389 vertical => 1,
1259 ( 1390 @_,
1260 (List::Util::max map $_->[0], @alloc),
1261 (List::Util::sum map $_->[1], @alloc),
1262 ) 1391 )
1263}
1264
1265sub size_allocate {
1266 my ($self, $w, $h) = @_;
1267
1268 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1269
1270 my $children = $self->{children};
1271
1272 my @h = map $_->{req_h}, @$children;
1273
1274 my $req_h = List::Util::sum @h;
1275
1276 if ($req_h > $h) {
1277 # ah well, not enough space
1278 $_ *= $h / $req_h for @h;
1279 } else {
1280 my $exp = List::Util::sum map $_->{expand}, @$children;
1281 $exp ||= 1;
1282
1283 for (0 .. $#$children) {
1284 my $child = $children->[$_];
1285
1286 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1287 }
1288 }
1289
1290 CFClient::UI::harmonize \@h;
1291
1292 my $y = 0;
1293 for (0 .. $#$children) {
1294 my $child = $children->[$_];
1295 my $h = $h[$_];
1296 $child->configure (0, $y, $w, $h);
1297
1298 $y += $h;
1299 }
1300
1301 1
1302} 1392}
1303 1393
1304############################################################################# 1394#############################################################################
1305 1395
1306package CFClient::UI::Label; 1396package CFClient::UI::Label;
1510 $self->{text} = $text; 1600 $self->{text} = $text;
1511 1601
1512 $text =~ s/./*/g if $self->{hidden}; 1602 $text =~ s/./*/g if $self->{hidden};
1513 $self->{layout}->set_text ("$text "); 1603 $self->{layout}->set_text ("$text ");
1514 1604
1515 $self->emit (changed => $self->{text}); 1605 $self->_emit (changed => $self->{text});
1516} 1606}
1517 1607
1518sub set_text { 1608sub set_text {
1519 my ($self, $text) = @_; 1609 my ($self, $text) = @_;
1520 1610
1532 my ($self) = @_; 1622 my ($self) = @_;
1533 1623
1534 my ($w, $h) = $self->SUPER::size_request; 1624 my ($w, $h) = $self->SUPER::size_request;
1535 1625
1536 ($w + 1, $h) # add 1 for cursor 1626 ($w + 1, $h) # add 1 for cursor
1537}
1538
1539sub size_allocate {
1540 my ($self, $w, $h) = @_;
1541
1542 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1543} 1627}
1544 1628
1545sub key_down { 1629sub key_down {
1546 my ($self, $ev) = @_; 1630 my ($self, $ev) = @_;
1547 1631
1562 } elsif ($sym == CFClient::SDLK_HOME) { 1646 } elsif ($sym == CFClient::SDLK_HOME) {
1563 $self->{cursor} = 0; 1647 $self->{cursor} = 0;
1564 } elsif ($sym == CFClient::SDLK_END) { 1648 } elsif ($sym == CFClient::SDLK_END) {
1565 $self->{cursor} = length $text; 1649 $self->{cursor} = length $text;
1566 } elsif ($uni == 27) { 1650 } elsif ($uni == 27) {
1567 $self->emit ('escape'); 1651 $self->_emit ('escape');
1568 } elsif ($uni) { 1652 } elsif ($uni) {
1569 substr $text, $self->{cursor}++, 0, chr $uni; 1653 substr $text, $self->{cursor}++, 0, chr $uni;
1570 } 1654 }
1571 1655
1572 $self->_set_text ($text); 1656 $self->_set_text ($text);
1659 if ($sym == 13) { 1743 if ($sym == 13) {
1660 unshift @{$self->{history}}, 1744 unshift @{$self->{history}},
1661 my $txt = $self->get_text; 1745 my $txt = $self->get_text;
1662 $self->{history_pointer} = -1; 1746 $self->{history_pointer} = -1;
1663 $self->{history_saveback} = ''; 1747 $self->{history_saveback} = '';
1664 $self->emit (activate => $txt); 1748 $self->_emit (activate => $txt);
1665 $self->update; 1749 $self->update;
1666 1750
1667 } elsif ($sym == CFClient::SDLK_UP) { 1751 } elsif ($sym == CFClient::SDLK_UP) {
1668 if ($self->{history_pointer} < 0) { 1752 if ($self->{history_pointer} < 0) {
1669 $self->{history_saveback} = $self->get_text; 1753 $self->{history_saveback} = $self->get_text;
1717 can_events => 1, 1801 can_events => 1,
1718 @_ 1802 @_
1719 ) 1803 )
1720} 1804}
1721 1805
1806sub activate { }
1807
1722sub button_up { 1808sub button_up {
1723 my ($self, $ev, $x, $y) = @_; 1809 my ($self, $ev, $x, $y) = @_;
1724 1810
1811 $self->emit ("activate")
1725 if ($x >= 0 && $x < $self->{w} 1812 if $x >= 0 && $x < $self->{w}
1726 && $y >= 0 && $y < $self->{h}) { 1813 && $y >= 0 && $y < $self->{h};
1727 $self->emit ("activate");
1728 }
1729} 1814}
1730 1815
1731sub _draw { 1816sub _draw {
1732 my ($self) = @_; 1817 my ($self) = @_;
1733 1818
1785 my ($self, $ev, $x, $y) = @_; 1870 my ($self, $ev, $x, $y) = @_;
1786 1871
1787 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1872 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1788 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1873 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1789 $self->{state} = !$self->{state}; 1874 $self->{state} = !$self->{state};
1790 $self->emit (changed => $self->{state}); 1875 $self->_emit (changed => $self->{state});
1791 } 1876 }
1792} 1877}
1793 1878
1794sub _draw { 1879sub _draw {
1795 my ($self) = @_; 1880 my ($self) = @_;
2070 my $self = $class->SUPER::new ( 2155 my $self = $class->SUPER::new (
2071 fg => [1, 1, 1], 2156 fg => [1, 1, 1],
2072 active_fg => [0, 0, 0], 2157 active_fg => [0, 0, 0],
2073 bg => [0, 0, 0, 0.2], 2158 bg => [0, 0, 0, 0.2],
2074 active_bg => [1, 1, 1, 0.5], 2159 active_bg => [1, 1, 1, 0.5],
2075 range => [0, 0, 100, 10], 2160 range => [0, 0, 100, 10, 0],
2076 req_w => $::WIDTH / 80, 2161 req_w => $::WIDTH / 80,
2077 req_h => $::WIDTH / 80, 2162 req_h => $::WIDTH / 80,
2078 vertical => 0, 2163 vertical => 0,
2079 can_hover => 1, 2164 can_hover => 1,
2080 inner_pad => 0.02, 2165 inner_pad => 0.02,
2088} 2173}
2089 2174
2090sub set_range { 2175sub set_range {
2091 my ($self, $range) = @_; 2176 my ($self, $range) = @_;
2092 2177
2093 $self->{range} = $range; 2178 ($range, $self->{range}) = ($self->{range}, $range);
2094 2179
2095 $self->update; 2180 $self->update
2181 if "@$range" ne "@{$self->{range}}";
2096} 2182}
2097 2183
2098sub set_value { 2184sub set_value {
2099 my ($self, $value) = @_; 2185 my ($self, $value) = @_;
2100 2186
2101 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 2187 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2102 2188
2103 $hi = $lo + 1 if $hi <= $lo; 2189 $hi = $lo + 1 if $hi <= $lo;
2104 2190
2191 $page = $hi - $lo if $page > $hi - $lo;
2192
2105 $value = $lo if $value < $lo; 2193 $value = $lo if $value < $lo;
2106 $value = $hi if $value > $hi; 2194 $value = $hi - $page if $value > $hi - $page;
2107 2195
2108 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 2196 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2109 if $unit; 2197 if $unit;
2110 2198
2111 $page = $hi - $lo if $page > $hi - $lo;
2112
2113 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2199 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2114 2200
2115 if ($value != $old_value) { 2201 if ($value != $old_value) {
2116 $self->emit (changed => $value); 2202 $self->_emit (changed => $value);
2117 $self->update; 2203 $self->update;
2118 } 2204 }
2119} 2205}
2120 2206
2121sub size_request { 2207sub size_request {
2129 2215
2130sub button_down { 2216sub button_down {
2131 my ($self, $ev, $x, $y) = @_; 2217 my ($self, $ev, $x, $y) = @_;
2132 2218
2133 $self->SUPER::button_down ($ev, $x, $y); 2219 $self->SUPER::button_down ($ev, $x, $y);
2220
2221 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2222
2134 $self->mouse_motion ($ev, $x, $y); 2223 $self->mouse_motion ($ev, $x, $y);
2135} 2224}
2136 2225
2137sub mouse_motion { 2226sub mouse_motion {
2138 my ($self, $ev, $x, $y) = @_; 2227 my ($self, $ev, $x, $y) = @_;
2140 if ($GRAB == $self) { 2229 if ($GRAB == $self) {
2141 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2230 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2142 2231
2143 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2232 my (undef, $lo, $hi, $page) = @{$self->{range}};
2144 2233
2145 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; 2234 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2146 2235
2147 $self->set_value ($x * ($hi - $lo) + $lo); 2236 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2148 } 2237 }
2149} 2238}
2150 2239
2151sub update { 2240sub update {
2152 my ($self) = @_; 2241 my ($self) = @_;
2153 2242
2154 $CFClient::UI::ROOT->on_post_alloc ($self => sub { 2243 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2155 $self->set_value ($self->{range}[0]); 2244 $self->set_value ($self->{range}[0]);
2156 2245
2157 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2246 my ($value, $lo, $hi, $page) = @{$self->{range}};
2247 my $range = ($hi - $page - $lo) || 1e-100;
2158 2248
2159 my $inner_w = 1 - 2 * $self->{inner_pad}; 2249 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
2160 2250
2161 $self->{scale} = ($inner_w / ($hi - $lo)) || 1; 2251 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2252 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2162 2253
2163 $page = $self->{scale} * $page || 10 / ($self->{w} || 1); 2254 $value = ($value - $lo) / $range;
2164 $value = $self->{scale} * ($value - $lo); 2255 $value = $value * $self->{scale} + $self->{offset};
2165 2256
2166 $value = $self->{inner_pad} + ($value - $page * 0.5);
2167
2168 $value = 0 if $value < 0;
2169 $page = 1 - $value if $value + $page > 1;
2170
2171 $self->{knob_x} = $value; 2257 $self->{knob_x} = $value - $knob_w * 0.5;
2172 $self->{knob_w} = $page; 2258 $self->{knob_w} = $knob_w;
2173 }); 2259 });
2174 2260
2175 $self->SUPER::update; 2261 $self->SUPER::update;
2176} 2262}
2177 2263
2293 $self->{layout}->set_width ($self->{children}[0]{w}); 2379 $self->{layout}->set_width ($self->{children}[0]{w});
2294 2380
2295 $self->reflow; 2381 $self->reflow;
2296} 2382}
2297 2383
2298sub text_height { 2384sub text_size {
2299 my ($self, $text, $indent) = @_; 2385 my ($self, $text, $indent) = @_;
2300 2386
2301 my $layout = $self->{layout}; 2387 my $layout = $self->{layout};
2302 2388
2303 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2389 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2304 $layout->set_width ($self->{children}[0]{w} - $indent); 2390 $layout->set_width ($self->{children}[0]{w} - $indent);
2305 $layout->set_markup ($text); 2391 $layout->set_markup ($text);
2306 2392
2307 ($layout->size)[1] 2393 $layout->size
2308} 2394}
2309 2395
2310sub reflow { 2396sub reflow {
2311 my ($self) = @_; 2397 my ($self) = @_;
2312 2398
2313 $self->{need_reflow}++; 2399 $self->{need_reflow}++;
2314 $self->update; 2400 $self->update;
2315} 2401}
2316 2402
2403sub set_offset {
2404 my ($self, $offset) = @_;
2405
2406 # todo: base offset on lines or so, not on pixels
2407 $self->{children}[1]->set_value ($offset);
2408}
2409
2410sub clear {
2411 my ($self) = @_;
2412
2413 $self->{par} = [];
2414 $self->{height} = 0;
2415 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2416}
2417
2317sub add_paragraph { 2418sub add_paragraph {
2318 my ($self, $color, $text, $indent) = @_; 2419 my ($self, $color, $text, $indent) = @_;
2319 2420
2320 #TODO: intelligently "reformat" paragraph
2321
2322 for my $line (split /\n/, $text) { 2421 for my $line (split /\n/, $text) {
2323 my $height = $self->text_height ($line); 2422 my ($w, $h) = $self->text_size ($line);
2324 $self->{height} += $height; 2423 $self->{height} += $h;
2325 push @{$self->{par}}, [$height, $color, $indent, $line]; 2424 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line];
2326 } 2425 }
2327 2426
2328 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2427 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]);
2329 $self->{children}[1]->update;
2330} 2428}
2331 2429
2332sub update { 2430sub update {
2333 my ($self) = @_; 2431 my ($self) = @_;
2334 2432
2337 return unless $self->{h} > 0; 2435 return unless $self->{h} > 0;
2338 2436
2339 delete $self->{texture}; 2437 delete $self->{texture};
2340 2438
2341 $ROOT->on_post_alloc ($self, sub { 2439 $ROOT->on_post_alloc ($self, sub {
2440 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2441
2342 if (delete $self->{need_reflow}) { 2442 if (delete $self->{need_reflow}) {
2343 my $height = 0; 2443 my $height = 0;
2344 2444
2345 $height += $_->[0] = $self->text_height ($_->[3], $_->[2]) 2445 my $layout = $self->{layout};
2446
2447 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2448
2346 for @{$self->{par}}; 2449 for (@{$self->{par}}) {
2450 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2451 $layout->set_width ($W - $_->[3]);
2452 $layout->set_markup ($_->[4]);
2453 my ($w, $h) = $layout->size;
2454 $_->[0] = $w + $_->[3];
2455 $_->[1] = $h;
2456 }
2457
2458 $height += $_->[1];
2459 }
2347 2460
2348 $self->{height} = $height; 2461 $self->{height} = $height;
2349 2462
2350 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2463 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2351 $self->{children}[1]->update;
2352 2464
2353 delete $self->{texture}; 2465 delete $self->{texture};
2354 } 2466 }
2355 2467
2356 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2468 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2357 glClearColor 0.5, 0.5, 0.5, 0; 2469 glClearColor 0.5, 0.5, 0.5, 0;
2358 glClear GL_COLOR_BUFFER_BIT; 2470 glClear GL_COLOR_BUFFER_BIT;
2359 2471
2360 my $top = int $self->{children}[1]{range}[0]; 2472 my $top = int $self->{children}[1]{range}[0];
2361 2473
2362 my $y0 = $top; 2474 my $y0 = $top;
2363 my $y1 = $top + $self->{h}; 2475 my $y1 = $top + $H;
2364 2476
2365 my $y = 0; 2477 my $y = 0;
2366 2478
2367 my $layout = $self->{layout}; 2479 my $layout = $self->{layout};
2368 2480
2369 $layout->set_font ($self->{font}) if $self->{font}; 2481 $layout->set_font ($self->{font}) if $self->{font};
2370 2482
2371 glEnable GL_BLEND; 2483 glEnable GL_BLEND;
2484 #TODO# not correct in windows where rgba is forced off
2372 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2485 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2373 2486
2374 for my $par (@{$self->{par}}) { 2487 for my $par (@{$self->{par}}) {
2375 my $h = $par->[0]; 2488 my $h = $par->[1];
2376 2489
2377 if ($y0 < $y + $h && $y < $y1) { 2490 if ($y0 < $y + $h && $y < $y1) {
2378 $layout->set_foreground (@{ $par->[1] }); 2491 $layout->set_foreground (@{ $par->[2] });
2379 $layout->set_width ($self->{w} - $par->[2]); 2492 $layout->set_width ($W - $par->[3]);
2380 $layout->set_markup ($par->[3]); 2493 $layout->set_markup ($par->[4]);
2381 2494
2382 my ($w, $h, $data, $format, $internalformat) = $layout->render; 2495 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2383 2496
2384 glRasterPos $par->[2], $y - $y0; 2497 glRasterPos $par->[3], $y - $y0;
2385 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; 2498 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2386 } 2499 }
2387 2500
2388 $y += $h; 2501 $y += $h;
2389 } 2502 }
2456 2569
2457sub new { 2570sub new {
2458 my $class = shift; 2571 my $class = shift;
2459 2572
2460 my $self = $class->SUPER::new ( 2573 my $self = $class->SUPER::new (
2461 state => 0, 2574 state => 0,
2462 connect_activate => \&toggle_flopper, 2575 on_activate => \&toggle_flopper,
2463 @_ 2576 @_
2464 ); 2577 );
2465 2578
2466 if ($self->{state}) {
2467 $self->{state} = 0;
2468 $self->toggle_flopper;
2469 }
2470
2471 $self 2579 $self
2472} 2580}
2473 2581
2474sub toggle_flopper { 2582sub toggle_flopper {
2475 my ($self) = @_; 2583 my ($self) = @_;
2476 2584
2477 # TODO: use animation 2585 $self->{other}->toggle_visibility;
2478 if ($self->{state} = !$self->{state}) {
2479 $CFClient::UI::ROOT->add ($self->{other});
2480 $self->{other}->move ($self->coord2global (0, $self->{h}));
2481 $self->emit ("open");
2482 } else {
2483 $CFClient::UI::ROOT->remove ($self->{other});
2484 $self->emit ("close");
2485 }
2486
2487 $self->emit (changed => $self->{state});
2488} 2586}
2489 2587
2490############################################################################# 2588#############################################################################
2491 2589
2492package CFClient::UI::Tooltip; 2590package CFClient::UI::Tooltip;
2568 2666
2569sub new { 2667sub new {
2570 my $class = shift; 2668 my $class = shift;
2571 2669
2572 my $self = $class->SUPER::new ( 2670 my $self = $class->SUPER::new (
2573 aspect => 1, 2671 aspect => 1,
2672 can_events => 0,
2574 @_, 2673 @_,
2575 ); 2674 );
2576 2675
2577 if ($self->{anim} && $self->{animspeed}) { 2676 if ($self->{anim} && $self->{animspeed}) {
2578 Scalar::Util::weaken (my $widget = $self); 2677 Scalar::Util::weaken (my $widget = $self);
2604} 2703}
2605 2704
2606sub _draw { 2705sub _draw {
2607 my ($self) = @_; 2706 my ($self) = @_;
2608 2707
2609 return unless $::CONN;#d# manage and cache textures differently 2708 return unless $::CONN;
2610 2709
2611 my $face; 2710 my $face;
2612 2711
2613 if ($self->{frame}) { 2712 if ($self->{frame}) {
2614 my $anim = $::CONN->{anim}[$self->{anim}]; 2713 my $anim = $::CONN->{anim}[$self->{anim}];
2637 $self->SUPER::DESTROY; 2736 $self->SUPER::DESTROY;
2638} 2737}
2639 2738
2640############################################################################# 2739#############################################################################
2641 2740
2642package CFClient::UI::InventoryItem; 2741package CFClient::UI::Inventory;
2643 2742
2644our @ISA = CFClient::UI::HBox::; 2743our @ISA = CFClient::UI::ScrolledWindow::;
2645
2646sub _item_to_desc {
2647 my ($item) = @_;
2648
2649 my $desc =
2650 $item->{nrof} < 2
2651 ? $item->{name}
2652 : "$item->{nrof} × $item->{name_pl}";
2653
2654 $item->{flags} & Crossfire::Protocol::F_OPEN
2655 and $desc .= " (open)";
2656 $item->{flags} & Crossfire::Protocol::F_APPLIED
2657 and $desc .= " (applied)";
2658 $item->{flags} & Crossfire::Protocol::F_UNPAID
2659 and $desc .= " (unpaid)";
2660 $item->{flags} & Crossfire::Protocol::F_MAGIC
2661 and $desc .= " (magic)";
2662 $item->{flags} & Crossfire::Protocol::F_CURSED
2663 and $desc .= " (cursed)";
2664 $item->{flags} & Crossfire::Protocol::F_DAMNED
2665 and $desc .= " (damned)";
2666 $item->{flags} & Crossfire::Protocol::F_LOCKED
2667 and $desc .= " *";
2668
2669 $desc
2670}
2671 2744
2672sub new { 2745sub new {
2673 my $class = shift; 2746 my $class = shift;
2674 2747
2675 my %args = @_;
2676
2677 my $item = delete $args{item};
2678
2679 my $desc = _item_to_desc ($item);
2680
2681 my $self = $class->SUPER::new ( 2748 my $self = $class->SUPER::new (
2682 can_hover => 1,
2683 can_events => 1,
2684 tooltip => ((CFClient::UI::Label::escape $desc)
2685 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2686 connect_button_down => sub {
2687 my ($self, $ev, $x, $y) = @_;
2688
2689 # todo: maybe put examine on 1? but should just be a tooltip :(
2690 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2691 my $targ = $::CONN->{player}{tag};
2692
2693 if ($item->{container} == $::CONN->{player}{tag}) {
2694 $targ = $main::OPENCONT;
2695 }
2696
2697 $::CONN->send ("move $targ $item->{tag} 0");
2698 } elsif ($ev->{button} == 1) {
2699 $::CONN->send ("examine $item->{tag}");
2700 } elsif ($ev->{button} == 2) {
2701 $::CONN->send ("apply $item->{tag}");
2702 } elsif ($ev->{button} == 3) {
2703 my @menu_items = (
2704 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2705 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2706 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2707 (
2708 $item->{flags} & Crossfire::Protocol::F_LOCKED
2709 ? (
2710 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2711 )
2712 : (
2713 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2714 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2715 )
2716 ),
2717 );
2718
2719 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2720 }
2721
2722 1
2723 },
2724 %args
2725 );
2726
2727
2728 $self->add (new CFClient::UI::Face
2729 can_events => 0,
2730 face => $item->{face},
2731 anim => $item->{anim},
2732 animspeed => $item->{animspeed},
2733 );
2734
2735 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2736
2737 $self->{item} = $item;
2738
2739 $self->update_item;
2740
2741 $self
2742}
2743
2744sub update_item {
2745 my ($self) = @_;
2746
2747 my $desc = _item_to_desc ($self->{item});
2748
2749 $self->{name_lbl}->set_text ($desc);
2750}
2751
2752#############################################################################
2753
2754package CFClient::UI::Inventory;
2755
2756our @ISA = CFClient::UI::ScrolledWindow::;
2757
2758sub new {
2759 my $class = shift;
2760
2761 my $self = $class->SUPER::new (
2762 scrolled => (new CFClient::UI::Table), 2749 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2763 @_, 2750 @_,
2764 ); 2751 );
2765 2752
2766 $self 2753 $self
2767} 2754}
2777 or ($a->{name} cmp $b->{name}) 2764 or ($a->{name} cmp $b->{name})
2778 } @$items; 2765 } @$items;
2779 2766
2780 $self->{real_items} = \@items; 2767 $self->{real_items} = \@items;
2781 2768
2769 my $row = 0;
2782 for my $item (@items) { 2770 for my $item (@items) {
2783 $item->{item} = $item; 2771 CFClient::Item::update_widgets $item;
2784 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2785 $item->update_item ();
2786 }
2787 2772
2788 my $i = 0; 2773 $self->{scrolled}->add (0, $row, $item->{face_widget});
2789 for (@items) { 2774 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2790 $self->{scrolled}->add (0, $i, $_); 2775 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2791 my $nrof = $_->{item}->{nrof} || 1;
2792 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2793 }
2794 2776
2795# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2777 $row++;
2796} 2778 }
2797
2798sub size_request {
2799 my ($self) = @_;
2800 ($self->{req_w}, $self->{req_h});
2801} 2779}
2802 2780
2803############################################################################# 2781#############################################################################
2804 2782
2805package CFClient::UI::Menu; 2783package CFClient::UI::Menu;
2840 2818
2841# popup given the event (must be a mouse button down event currently) 2819# popup given the event (must be a mouse button down event currently)
2842sub popup { 2820sub popup {
2843 my ($self, $ev) = @_; 2821 my ($self, $ev) = @_;
2844 2822
2845 $self->emit ("popdown"); 2823 $self->_emit ("popdown");
2846 2824
2847 # maybe save $GRAB? must be careful about events... 2825 # maybe save $GRAB? must be careful about events...
2848 $GRAB = $self; 2826 $GRAB = $self;
2849 $self->{button} = $ev->{button}; 2827 $self->{button} = $ev->{button};
2850 2828
2865 2843
2866 if ($ev->{button} == $self->{button}) { 2844 if ($ev->{button} == $self->{button}) {
2867 undef $GRAB; 2845 undef $GRAB;
2868 $self->hide; 2846 $self->hide;
2869 2847
2870 $self->emit ("popdown"); 2848 $self->_emit ("popdown");
2871 $self->{hover}[1]->() if $self->{hover}; 2849 $self->{hover}[1]->() if $self->{hover};
2872 } 2850 }
2873} 2851}
2874 2852
2875############################################################################# 2853#############################################################################
2937sub add { 2915sub add {
2938 my ($self, $text, %arg) = @_; 2916 my ($self, $text, %arg) = @_;
2939 2917
2940 $text =~ s/^\s+//; 2918 $text =~ s/^\s+//;
2941 $text =~ s/\s+$//; 2919 $text =~ s/\s+$//;
2920
2921 return unless $text;
2942 2922
2943 my $timeout = time + ((delete $arg{timeout}) || 60); 2923 my $timeout = time + ((delete $arg{timeout}) || 60);
2944 2924
2945 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2925 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2946 2926
2990 2970
2991sub new { 2971sub new {
2992 my $class = shift; 2972 my $class = shift;
2993 2973
2994 $class->SUPER::new ( 2974 $class->SUPER::new (
2975 visible => 1,
2995 @_, 2976 @_,
2996 ) 2977 )
2997} 2978}
2998 2979
2999sub configure { 2980sub configure {
3017} 2998}
3018 2999
3019sub size_allocate { 3000sub size_allocate {
3020 my ($self, $w, $h) = @_; 3001 my ($self, $w, $h) = @_;
3021 3002
3022 my $old_w = $self->{old_w}; $self->{old_w} = $w;
3023 my $old_h = $self->{old_h}; $self->{old_h} = $h;
3024
3025 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
3026 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
3027
3028 for my $child ($self->children) { 3003 for my $child ($self->children) {
3029 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3004 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3030 3005
3031 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3006 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3032 if exists $child->{req_x}; 3007 if exists $child->{req_x};
3033 3008
3034 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3009 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3035 if exists $child->{req_y}; 3010 if exists $child->{req_y};
3036 3011
3012 delete @$child{qw(req_x req_y)};
3013
3037 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3014 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3038 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3015 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3039 3016
3040 $child->configure ($X, $Y, $W, $H); 3017 $child->configure ($X, $Y, $W, $H);
3041 } 3018 }
3062 3039
3063sub add { 3040sub add {
3064 my ($self, @children) = @_; 3041 my ($self, @children) = @_;
3065 3042
3066 for my $child (@children) { 3043 for my $child (@children) {
3067 $child->{toplevel} = 1; 3044 $child->{is_toplevel} = 1;
3068 3045
3069 # integerise window positions 3046 # integerise window positions
3070 $child->{x} = int $child->{x}; 3047 $child->{x} = int $child->{x};
3071 $child->{y} = int $child->{y}; 3048 $child->{y} = int $child->{y};
3072 } 3049 }
3073 3050
3074 $self->SUPER::add (@children); 3051 $self->SUPER::add (@children);
3075 3052
3053 for (my @widgets = @children; my $w = pop @widgets; ) {
3054 push @widgets, $w->children;
3055 $w->set_visible;
3056 }
3057
3058}
3059
3060sub remove {
3061 my ($self, @children) = @_;
3062
3063 $self->SUPER::remove (@children);
3064
3076 while (@children) { 3065 while (@children) {
3077 my $w = pop @children; 3066 my $w = pop @children;
3078 push @children, $w->children; 3067 push @children, $w->children;
3079 $w->{visible} = 1; 3068 $w->set_invisible;
3080 }
3081}
3082
3083sub remove {
3084 my ($self, @children) = @_;
3085
3086 $self->SUPER::remove (@children);
3087
3088 while (@children) {
3089 my $w = pop @children;
3090 push @children, $w->children;
3091 delete $w->{visible};
3092 } 3069 }
3093} 3070}
3094 3071
3095sub on_refresh { 3072sub on_refresh {
3096 my ($self, $id, $cb) = @_; 3073 my ($self, $id, $cb) = @_;
3111 $_->() 3088 $_->()
3112 for values %{delete $self->{refresh_hook}}; 3089 for values %{delete $self->{refresh_hook}};
3113 } 3090 }
3114 3091
3115 if ($self->{check_size}) { 3092 if ($self->{check_size}) {
3116 my @queue = ([], []); 3093 my @queue;
3117 3094
3118 for (;;) { 3095 for (;;) {
3119 if ($self->{check_size}) { 3096 if ($self->{check_size}) {
3120 # heuristic: check containers last 3097 #TODO use array-of-depth approach
3121 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3098
3099 @queue = sort { $a->{visible} <=> $b->{visible} }
3122 for values %{delete $self->{check_size}} 3100 @queue, values %{delete $self->{check_size}};
3123 } 3101 }
3124 3102
3125 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3103 my $widget = pop @queue || last;
3126 3104
3105 defined $widget->{visible} or last; # do not resize invisible widgets
3106
3127 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3107 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3128 ? @$widget{qw(user_w user_h)} 3108 ? @$widget{qw(def_w def_h)}
3129 : $widget->size_request; 3109 : $widget->size_request;
3130 3110
3131 if (delete $widget->{force_alloc} 3111 if (delete $widget->{force_alloc}
3132 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3112 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3133 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3113 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3150 $w = 0 if $w < 0; 3130 $w = 0 if $w < 0;
3151 $h = 0 if $h < 0; 3131 $h = 0 if $h < 0;
3152 3132
3153 $widget->{w} = $w; 3133 $widget->{w} = $w;
3154 $widget->{h} = $h; 3134 $widget->{h} = $h;
3155 $widget->size_allocate ($w, $h);
3156 $widget->emit (size_allocate => $w, $h); 3135 $widget->emit (size_allocate => $w, $h);
3157 } 3136 }
3158 } 3137 }
3159 3138
3160 while ($self->{post_alloc_hook}) { 3139 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines