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.228 by root, Wed May 24 03:52:54 2006 UTC vs.
Revision 1.257 by root, Tue May 30 02:22:02 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 my $layout;
23
24 for (grep { $_->{name} } values %WIDGET) {
25 my $win = $layout->{$_->{name}} = { };
26
27 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
28 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
29 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
30 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
31 }
32
33 $layout
34}
35
36sub set_layout {
37 my ($layout) = @_;
38 $LAYOUT = $layout;
39}
18 40
19sub check_tooltip { 41sub check_tooltip {
20 if (!$GRAB) { 42 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 43 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 44 if (length $widget->{tooltip}) {
23 45
24 if ($TOOLTIP->{owner} != $widget) { 46 if ($TOOLTIP->{owner} != $widget) {
47 $TOOLTIP->hide;
48
25 $TOOLTIP->{owner} = $widget; 49 $TOOLTIP->{owner} = $widget;
26 50
27 my $tip = $widget->{tooltip}; 51 my $tip = $widget->{tooltip};
28 52
29 $tip = $tip->($widget) if CODE:: eq ref $tip; 53 $tip = $tip->($widget) if CODE:: eq ref $tip;
30 54
31 $TOOLTIP->set_tooltip_from ($widget); 55 $TOOLTIP->set_tooltip_from ($widget);
32 $TOOLTIP->show; 56 $TOOLTIP->show;
33
34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
35
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 if $x + $TOOLTIP->{w} > $::WIDTH;
38
39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
42 } 57 }
43 58
44 return; 59 return;
45 } 60 }
46 } 61 }
50 delete $TOOLTIP->{owner}; 65 delete $TOOLTIP->{owner};
51} 66}
52 67
53# class methods for events 68# class methods for events
54sub feed_sdl_key_down_event { 69sub feed_sdl_key_down_event {
55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 70 $FOCUS->emit (key_down => $_[0])
56 if $FOCUS; 71 if $FOCUS;
57} 72}
58 73
59sub feed_sdl_key_up_event { 74sub feed_sdl_key_up_event {
60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 75 $FOCUS->emit (key_up => $_[0])
61 if $FOCUS; 76 if $FOCUS;
62} 77}
63 78
64sub feed_sdl_button_down_event { 79sub feed_sdl_button_down_event {
65 my ($ev) = @_; 80 my ($ev) = @_;
74 check_tooltip; 89 check_tooltip;
75 } 90 }
76 91
77 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 92 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
78 93
79 if ($GRAB) { 94 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
80 ($x, $y) = $GRAB->coord2local ($x, $y); 95 if $GRAB;
81 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
82 }
83} 96}
84 97
85sub feed_sdl_button_up_event { 98sub feed_sdl_button_up_event {
86 my ($ev) = @_; 99 my ($ev) = @_;
87 my ($x, $y) = ($ev->{x}, $ev->{y}); 100 my ($x, $y) = ($ev->{x}, $ev->{y});
88 101
89 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 102 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
90 103
91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 104 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
92 105
93 if ($GRAB) { 106 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
94 ($x, $y) = $GRAB->coord2local ($x, $y); 107 if $GRAB;
95 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
96 }
97 108
98 if (!$BUTTON_STATE) { 109 if (!$BUTTON_STATE) {
99 my $grab = $GRAB; undef $GRAB; 110 my $grab = $GRAB; undef $GRAB;
100 $grab->update if $grab; 111 $grab->update if $grab;
101 $GRAB->update if $GRAB; 112 $GRAB->update if $GRAB;
117 $HOVER->update if $HOVER && $HOVER->{can_hover}; 128 $HOVER->update if $HOVER && $HOVER->{can_hover};
118 129
119 check_tooltip; 130 check_tooltip;
120 } 131 }
121 132
122 if ($HOVER) {
123 ($x, $y) = $HOVER->coord2local ($x, $y);
124 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); 133 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
125 } 134 if $HOVER;
126} 135}
127 136
128# convert position array to integers 137# convert position array to integers
129sub harmonize { 138sub harmonize {
130 my ($vals) = @_; 139 my ($vals) = @_;
144 153
145 $_->update 154 $_->update
146 for @widgets; 155 for @widgets;
147} 156}
148 157
158sub reconfigure_widgets {
159 # make a copy, otherwise C<for> complains about freed values.
160 my @widgets = values %WIDGET;
161
162 $_->reconfigure
163 for @widgets;
164}
165
149# call when resolution changes etc. 166# call when resolution changes etc.
150sub rescale_widgets { 167sub rescale_widgets {
151 my ($sx, $sy) = @_; 168 my ($sx, $sy) = @_;
152 169
153 # make a copy, otherwise for complains about freed values.
154 my @widgets = values %WIDGET; 170 for my $widget (values %WIDGET) {
155
156 for my $widget (@widgets) {
157 if ($widget->{toplevel}) { 171 if ($widget->{is_toplevel}) {
172 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
173 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
174
158 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 175 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
159 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 176 $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}; 177 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_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}; 178 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
163 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 179 $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}; 180 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
165 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; 181
182 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
183 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
184
166 } 185 }
167
168 $widget->reconfigure;
169 } 186 }
187
188 reconfigure_widgets;
170} 189}
171 190
172############################################################################# 191#############################################################################
173 192
174package CFClient::UI::Base; 193package CFClient::UI::Base;
179 198
180sub new { 199sub new {
181 my $class = shift; 200 my $class = shift;
182 201
183 my $self = bless { 202 my $self = bless {
184 x => 0, 203 x => "center",
185 y => 0, 204 y => "center",
186 z => 0, 205 z => 0,
206 w => undef,
207 h => undef,
187 can_events => 1, 208 can_events => 1,
188 @_ 209 @_
189 }, $class; 210 }, $class;
190 211
212 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
213 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x};
214 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y};
215 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w};
216 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h};
217
218 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
219 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
220 }
221
222 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
223
191 for (keys %$self) { 224 for (keys %$self) {
192 if (/^connect_(.*)$/) { 225 if (/^on_(.*)$/) {
193 $self->connect ($1 => delete $self->{$_}); 226 $self->connect ($1 => delete $self->{$_});
194 } 227 }
195 } 228 }
196 229
197 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
198
199 $self 230 $self
200} 231}
201 232
202sub destroy { 233sub destroy {
203 my ($self) = @_; 234 my ($self) = @_;
206 %$self = (); 237 %$self = ();
207} 238}
208 239
209sub show { 240sub show {
210 my ($self) = @_; 241 my ($self) = @_;
242
211 return if $self->{parent}; 243 return if $self->{parent};
212 244
213 $CFClient::UI::ROOT->add ($self); 245 $CFClient::UI::ROOT->add ($self);
214} 246}
215 247
216sub show_centered { 248sub set_visible {
217 my ($self) = @_; 249 my ($self) = @_;
250
218 return if $self->{parent}; 251 return if $self->{visible};
219 252
220 $self->show; 253 $self->{root} = $self->{parent}{root};
254 $self->{visible} = $self->{parent}{visible} + 1;
221 255
222 $CFClient::UI::ROOT->on_post_alloc ( 256 $self->emit (visibility_change => 1);
223 "centered $self" => sub {
224 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
225 },
226 );
227}
228 257
229sub hide { 258 $self->realloc if !exists $self->{req_w};
259
260 $_->set_visible for $self->children;
261}
262
263sub set_invisible {
230 my ($self) = @_; 264 my ($self) = @_;
265
266 return unless $self->{visible};
267
268 $_->set_invisible for $self->children;
269
270 delete $self->{root};
271 delete $self->{visible};
231 272
232 undef $GRAB if $GRAB == $self; 273 undef $GRAB if $GRAB == $self;
233 undef $HOVER if $HOVER == $self; 274 undef $HOVER if $HOVER == $self;
234 275
276 CFClient::UI::check_tooltip
277 if $TOOLTIP->{owner} == $self;
278
279 $self->focus_out;
280
281 $self->emit (visibility_change => 0);
282}
283
284sub set_visibility {
285 my ($self, $visible) = @_;
286
287 return if $self->{visible} == $visible;
288
289 $visible ? $self->hide
290 : $self->show;
291}
292
293sub toggle_visibility {
294 my ($self) = @_;
295
296 $self->{visible}
297 ? $self->hide
298 : $self->show;
299}
300
301sub hide {
302 my ($self) = @_;
303
304 $self->set_invisible;
305
235 $self->{parent}->remove ($self) 306 $self->{parent}->remove ($self)
236 if $self->{parent}; 307 if $self->{parent};
237} 308}
238 309
239sub move { 310sub move_abs {
240 my ($self, $x, $y, $z) = @_; 311 my ($self, $x, $y, $z) = @_;
241 312
242 $self->{x} = int $x; 313 $self->{x} = List::Util::max 0, int $x;
243 $self->{y} = int $y; 314 $self->{y} = List::Util::max 0, int $y;
244 $self->{z} = $z if defined $z; 315 $self->{z} = $z if defined $z;
245 316
246 $self->update; 317 $self->update;
247} 318}
248 319
249sub set_size { 320sub set_size {
250 my ($self, $w, $h) = @_; 321 my ($self, $w, $h) = @_;
251 322
252 $self->{user_w} = $w; 323 $self->{force_w} = $w;
253 $self->{user_h} = $h; 324 $self->{force_h} = $h;
254 325
255 $self->check_size; 326 $self->realloc;
256} 327}
257 328
258sub size_request { 329sub size_request {
259 require Carp; 330 require Carp;
260 Carp::confess "size_request is abstract"; 331 Carp::confess "size_request is abstract";
262 333
263sub configure { 334sub configure {
264 my ($self, $x, $y, $w, $h) = @_; 335 my ($self, $x, $y, $w, $h) = @_;
265 336
266 if ($self->{aspect}) { 337 if ($self->{aspect}) {
338 my ($ow, $oh) = ($w, $h);
339
267 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 340 $w = List::Util::min $w, int $h * $self->{aspect};
268 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 341 $h = List::Util::min $h, int $w / $self->{aspect};
269 342
270 # use alignment to adjust x, y 343 # use alignment to adjust x, y
271 344
272 $x += int +($w - $w2) * 0.5; 345 $x += int 0.5 * ($ow - $w);
273 $y += int +($h - $h2) * 0.5; 346 $y += int 0.5 * ($oh - $h);
274
275 ($w, $h) = ($w2, $h2);
276 } 347 }
277 348
278 if ($self->{x} != $x || $self->{y} != $y) { 349 if ($self->{x} ne $x || $self->{y} ne $y) {
279 $self->{x} = $x; 350 $self->{x} = $x;
280 $self->{y} = $y; 351 $self->{y} = $y;
281 $self->update; 352 $self->update;
282 } 353 }
283 354
284 if ($self->{w} != $w || $self->{h} != $h) { 355 if ($self->{w} != $w || $self->{h} != $h) {
356 return unless $self->{visible};
357
285 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 358 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
286 } 359 }
287} 360}
288 361
289sub size_allocate { 362sub size_allocate {
290 # nothing to be done 363 # nothing to be done
291} 364}
292 365
293sub reconfigure {
294 my ($self) = @_;
295
296 $self->check_size (1);
297 $self->update;
298}
299
300sub children { 366sub children {
301} 367}
302 368
303sub set_max_size { 369sub set_max_size {
304 my ($self, $w, $h) = @_; 370 my ($self, $w, $h) = @_;
307 delete $self->{max_h}; $self->{max_h} = $h if $h; 373 delete $self->{max_h}; $self->{max_h} = $h if $h;
308} 374}
309 375
310sub set_tooltip { 376sub set_tooltip {
311 my ($self, $tooltip) = @_; 377 my ($self, $tooltip) = @_;
378
379 $tooltip =~ s/^\s+//;
380 $tooltip =~ s/\s+$//;
381
382 return if $self->{tooltip} eq $tooltip;
312 383
313 $self->{tooltip} = $tooltip; 384 $self->{tooltip} = $tooltip;
314 385
315 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 386 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
316 delete $CFClient::UI::TOOLTIP->{owner}; 387 delete $CFClient::UI::TOOLTIP->{owner};
338 return if $FOCUS == $self; 409 return if $FOCUS == $self;
339 return unless $self->{can_focus}; 410 return unless $self->{can_focus};
340 411
341 my $focus = $FOCUS; $FOCUS = $self; 412 my $focus = $FOCUS; $FOCUS = $self;
342 413
343 $self->emit (focus_in => $focus); 414 $self->_emit (focus_in => $focus);
344 415
345 $focus->update if $focus; 416 $focus->update if $focus;
346 $FOCUS->update; 417 $FOCUS->update;
347} 418}
348 419
351 422
352 return unless $FOCUS == $self; 423 return unless $FOCUS == $self;
353 424
354 my $focus = $FOCUS; undef $FOCUS; 425 my $focus = $FOCUS; undef $FOCUS;
355 426
356 $self->emit (focus_out => $focus); 427 $self->_emit (focus_out => $focus);
357 428
358 $focus->update if $focus; #? 429 $focus->update if $focus; #?
430
431 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
432 unless $FOCUS;
359} 433}
360 434
361sub mouse_motion { } 435sub mouse_motion { }
362sub button_up { } 436sub button_up { }
363sub key_down { } 437sub key_down { }
364sub key_up { } 438sub key_up { }
365 439
366sub button_down { 440sub button_down {
367 my ($self, $ev, $x, $y) = @_; 441 my ($self, $ev, $x, $y) = @_;
368 442
369 $self->focus_in; 443 $self->focus_in;
372sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 446sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
373sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 447sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
374sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 448sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
375sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 449sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
376sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 450sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
451
452sub find_widget {
453 my ($self, $x, $y) = @_;
454
455 return () unless $self->{can_events};
456
457 return $self
458 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
459 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
460
461 ()
462}
463
464sub set_parent {
465 my ($self, $parent) = @_;
466
467 Scalar::Util::weaken ($self->{parent} = $parent);
468
469 $self->set_visible if $parent->{visible};
470}
471
472sub connect {
473 my ($self, $signal, $cb) = @_;
474
475 push @{ $self->{signal_cb}{$signal} }, $cb;
476}
477
478sub _emit {
479 my ($self, $signal, @args) = @_;
480
481 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
482}
483
484sub emit {
485 my ($self, $signal, @args) = @_;
486
487 $self->_emit ($signal, @args)
488 || $self->$signal (@args);
489}
490
491sub visibility_change {
492 #my ($self, $visible) = @_;
493}
494
495sub realloc {
496 my ($self) = @_;
497
498 if ($self->{visible}) {
499 return if $self->{root}{realloc}{$self};
500
501 $self->{root}{realloc}{$self} = $self;
502 $self->{root}->update;
503 } else {
504 delete $self->{req_w};
505 }
506}
507
508sub update {
509 my ($self) = @_;
510
511 $self->{parent}->update
512 if $self->{parent};
513}
514
515sub reconfigure {
516 my ($self) = @_;
517
518 $self->realloc;
519 $self->update;
520}
377 521
378sub draw { 522sub draw {
379 my ($self) = @_; 523 my ($self) = @_;
380 524
381 return unless $self->{h} && $self->{w}; 525 return unless $self->{h} && $self->{w};
398 glVertex $x , $y + $self->{h}; 542 glVertex $x , $y + $self->{h};
399 glEnd; 543 glEnd;
400 glDisable GL_BLEND; 544 glDisable GL_BLEND;
401 } 545 }
402 546
403 if ($ENV{PCLIENT_DEBUG}) { 547 if ($ENV{CFPLUS_DEBUG}) {
404 glPushMatrix; 548 glPushMatrix;
405 glColor 1, 1, 0, 1; 549 glColor 1, 1, 0, 1;
406 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 550 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
407 glBegin GL_LINE_LOOP; 551 glBegin GL_LINE_LOOP;
408 glVertex 0 , 0; 552 glVertex 0 , 0;
409 glVertex $self->{w}, 0; 553 glVertex $self->{w} - 1, 0;
410 glVertex $self->{w}, $self->{h}; 554 glVertex $self->{w} - 1, $self->{h} - 1;
411 glVertex 0 , $self->{h}; 555 glVertex 0 , $self->{h} - 1;
412 glEnd; 556 glEnd;
413 glPopMatrix; 557 glPopMatrix;
414 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 558 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
415 } 559 }
416} 560}
417 561
418sub _draw { 562sub _draw {
419 my ($self) = @_; 563 my ($self) = @_;
420 564
421 warn "no draw defined for $self\n"; 565 warn "no draw defined for $self\n";
422}
423
424sub find_widget {
425 my ($self, $x, $y) = @_;
426
427 return () unless $self->{can_events};
428
429 return $self
430 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
431 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
432
433 ()
434}
435
436sub set_parent {
437 my ($self, $parent) = @_;
438
439 Scalar::Util::weaken ($self->{parent} = $parent);
440
441 # TODO: req_w _does_change after ->reconfigure
442 $self->check_size
443 unless exists $self->{req_w};
444
445 $self->show;
446}
447
448sub check_size {
449 my ($self, $forced) = @_;
450
451 $self->{force_alloc} = 1 if $forced;
452 $CFClient::UI::ROOT->{check_size}{$self} = $self;
453}
454
455sub update {
456 my ($self) = @_;
457
458 $self->{parent}->update
459 if $self->{parent};
460}
461
462sub connect {
463 my ($self, $signal, $cb) = @_;
464
465 push @{ $self->{signal_cb}{$signal} }, $cb;
466}
467
468sub emit {
469 my ($self, $signal, @args) = @_;
470
471 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
472} 566}
473 567
474sub DESTROY { 568sub DESTROY {
475 my ($self) = @_; 569 my ($self) = @_;
476 570
534 my ($class, %arg) = @_; 628 my ($class, %arg) = @_;
535 $class->SUPER::new (can_events => 0, %arg); 629 $class->SUPER::new (can_events => 0, %arg);
536} 630}
537 631
538sub size_request { 632sub size_request {
539 (0, 0) 633 my ($self) = @_;
634
635 ($self->{w} + 0, $self->{h} + 0)
540} 636}
541 637
542sub draw { } 638sub draw { }
543 639
544############################################################################# 640#############################################################################
573 $self->{children} = [ 669 $self->{children} = [
574 sort { $a->{z} <=> $b->{z} } 670 sort { $a->{z} <=> $b->{z} }
575 @{$self->{children}}, @widgets 671 @{$self->{children}}, @widgets
576 ]; 672 ];
577 673
578 $self->check_size (1); 674 $self->realloc;
579 $self->update;
580} 675}
581 676
582sub children { 677sub children {
583 @{ $_[0]{children} } 678 @{ $_[0]{children} }
584} 679}
589 delete $child->{parent}; 684 delete $child->{parent};
590 $child->hide; 685 $child->hide;
591 686
592 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 687 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
593 688
594 $self->check_size; 689 $self->realloc;
595 $self->update;
596} 690}
597 691
598sub clear { 692sub clear {
599 my ($self) = @_; 693 my ($self) = @_;
600 694
604 for (@$children) { 698 for (@$children) {
605 delete $_->{parent}; 699 delete $_->{parent};
606 $_->hide; 700 $_->hide;
607 } 701 }
608 702
609 $self->check_size; 703 $self->realloc;
610 $self->update;
611} 704}
612 705
613sub find_widget { 706sub find_widget {
614 my ($self, $x, $y) = @_; 707 my ($self, $x, $y) = @_;
615 708
668sub size_request { 761sub size_request {
669 $_[0]{children}[0]->size_request 762 $_[0]{children}[0]->size_request
670} 763}
671 764
672sub size_allocate { 765sub size_allocate {
673 my ($self, $w, $h) = @_; 766 my ($self, $w, $h, $changed) = @_;
674 767
675 $self->{children}[0]->configure (0, 0, $w, $h); 768 $self->{children}[0]->configure (0, 0, $w, $h);
676} 769}
677 770
678############################################################################# 771#############################################################################
695 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 788 $ROOT->on_post_alloc ($self => sub { $self->render_child });
696 $self->SUPER::update; 789 $self->SUPER::update;
697} 790}
698 791
699sub size_allocate { 792sub size_allocate {
700 my ($self, $w, $h) = @_; 793 my ($self, $w, $h, $changed) = @_;
701 794
702 $self->SUPER::size_allocate ($w, $h); 795 $self->SUPER::size_allocate ($w, $h, $changed);
703 $self->update; 796 $self->update
797 if $changed;
704} 798}
705 799
706sub _render { 800sub _render {
707 $_[0]{children}[0]->draw; 801 $_[0]{children}[0]->draw;
708} 802}
739 833
740package CFClient::UI::ViewPort; 834package CFClient::UI::ViewPort;
741 835
742our @ISA = CFClient::UI::Window::; 836our @ISA = CFClient::UI::Window::;
743 837
838sub new {
839 my $class = shift;
840
841 $class->SUPER::new (
842 scroll_x => 0,
843 scroll_y => 1,
844 @_,
845 )
846}
847
744sub size_request { 848sub size_request {
745 my ($self) = @_; 849 my ($self) = @_;
746 850
747 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 851 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
748 852
749 @$self{qw(child_w child_h)} 853 $w = 10 if $self->{scroll_x};
854 $h = 10 if $self->{scroll_y};
855
856 ($w, $h)
750} 857}
751 858
752sub size_allocate { 859sub size_allocate {
753 my ($self, $w, $h) = @_; 860 my ($self, $w, $h, $changed) = @_;
754 861
755 my ($cw, $ch) = @$self{qw(child_w child_h)}; 862 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
756# $w = $self->{w}; 863 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
864
757 $self->child->configure (0, 0, $cw, $ch); 865 $self->child->configure (0, 0, $w, $h);
758 $self->update; 866 $self->update;
759} 867}
760 868
761sub set_offset { 869sub set_offset {
762 my ($self, $x, $y) = @_; 870 my ($self, $x, $y) = @_;
813 my $class = shift; 921 my $class = shift;
814 922
815 my $self; 923 my $self;
816 924
817 my $slider = new CFClient::UI::Slider 925 my $slider = new CFClient::UI::Slider
818 vertical => 1, 926 vertical => 1,
819 range => [0, 0, 1, 0.01], # HACK fix 927 range => [0, 0, 1, 0.01], # HACK fix
820 connect_changed => sub { 928 on_changed => sub {
821 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 929 $self->{vp}->set_offset (0, $_[1]);
822 }, 930 },
823 ; 931 ;
824 932
825 $self = $class->SUPER::new ( 933 $self = $class->SUPER::new (
826 vp => (new CFClient::UI::ViewPort expand => 1), 934 vp => (new CFClient::UI::ViewPort expand => 1),
831 $self->{vp}->add ($self->{scrolled}); 939 $self->{vp}->add ($self->{scrolled});
832 $self->add ($self->{vp}); 940 $self->add ($self->{vp});
833 $self->add ($self->{slider}); 941 $self->add ($self->{slider});
834 942
835 $self 943 $self
944}
945
946sub update {
947 my ($self) = @_;
948
949 $self->SUPER::update;
950
951 # todo: overwrite size_allocate of child
952 my $child = $self->{vp}->child;
953 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
954}
955
956sub size_allocate {
957 my ($self, $w, $h, $changed) = @_;
958
959 $self->SUPER::size_allocate ($w, $h, $changed);
960
961 my $child = $self->{vp}->child;
962 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
836} 963}
837 964
838#TODO# update range on size_allocate depending on child 965#TODO# update range on size_allocate depending on child
839# update viewport offset on scroll 966# update viewport offset on scroll
840 967
884 1011
885our @ISA = CFClient::UI::Bin::; 1012our @ISA = CFClient::UI::Bin::;
886 1013
887use CFClient::OpenGL; 1014use CFClient::OpenGL;
888 1015
889my @tex = 1016my $bg =
1017 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1018 mipmap => 1, wrap => 1;
1019
1020my @border =
890 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1021 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
891 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1022 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
892 1023
893sub new { 1024sub new {
894 my $class = shift; 1025 my $class = shift;
895 1026
896 # TODO: user_x, user_y, overwrite moveto?
897
898 my $self = $class->SUPER::new ( 1027 my $self = $class->SUPER::new (
899 bg => [1, 1, 1, 1], 1028 bg => [1, 1, 1, 1],
900 border_bg => [1, 1, 1, 1], 1029 border_bg => [1, 1, 1, 1],
901 border => 0.6, 1030 border => 0.6,
902 toplevel => 1,
903 can_events => 1, 1031 can_events => 1,
904 @_ 1032 @_
905 ); 1033 );
906 1034
907 $self->{title} &&= new CFClient::UI::Label 1035 $self->{title} &&= new CFClient::UI::Label
908 align => 0, 1036 align => 0,
927 $h + $self->border * 2, 1055 $h + $self->border * 2,
928 ) 1056 )
929} 1057}
930 1058
931sub size_allocate { 1059sub size_allocate {
932 my ($self, $w, $h) = @_; 1060 my ($self, $w, $h, $changed) = @_;
1061
1062 return unless $changed;
933 1063
934 $h -= List::Util::max 0, $self->border * 2; 1064 $h -= List::Util::max 0, $self->border * 2;
935 $w -= List::Util::max 0, $self->border * 2; 1065 $w -= List::Util::max 0, $self->border * 2;
936 1066
937 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1067 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
961 my ($ev, $x, $y) = @_; 1091 my ($ev, $x, $y) = @_;
962 1092
963 my $dx = $ev->{x} - $ox; 1093 my $dx = $ev->{x} - $ox;
964 my $dy = $ev->{y} - $oy; 1094 my $dy = $ev->{y} - $oy;
965 1095
966 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1096 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
967 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1097 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1098
1099 $self->realloc;
968 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1100 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
969 $self->check_size;
970 }; 1101 };
971 1102
972 } elsif ($lr ^ $td) { 1103 } elsif ($lr ^ $td) {
973 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1104 my ($ox, $oy) = ($ev->{x}, $ev->{y});
974 my ($bx, $by) = ($self->{x}, $self->{y}); 1105 my ($bx, $by) = ($self->{x}, $self->{y});
976 $self->{motion} = sub { 1107 $self->{motion} = sub {
977 my ($ev, $x, $y) = @_; 1108 my ($ev, $x, $y) = @_;
978 1109
979 ($x, $y) = ($ev->{x}, $ev->{y}); 1110 ($x, $y) = ($ev->{x}, $ev->{y});
980 1111
981 $self->move ($bx + $x - $ox, $by + $y - $oy); 1112 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
982 $self->update;
983 }; 1113 };
984 } 1114 }
985} 1115}
986 1116
987sub button_up { 1117sub button_up {
1006 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1136 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1007 1137
1008 my $border = $self->border; 1138 my $border = $self->border;
1009 1139
1010 glColor @{ $self->{border_bg} }; 1140 glColor @{ $self->{border_bg} };
1011 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1141 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1012 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1142 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1013 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1143 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1014 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1144 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1015 1145
1016 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1146 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1017 my $bg = $tex[0]; 1147 glColor @{ $self->{bg} };
1018 1148
1019 # TODO: repeat texture not scale 1149 # TODO: repeat texture not scale
1150 # solve this better(?)
1020 my $rep_x = $cw / $bg->{w}; 1151 $bg->{s} = $cw / $bg->{w};
1021 my $rep_y = $ch / $bg->{h}; 1152 $bg->{t} = $ch / $bg->{h};
1022
1023 glColor @{ $self->{bg} };
1024
1025 $bg->{s} = $rep_x;
1026 $bg->{t} = $rep_y;
1027 $bg->{wrap_mode} = 1;
1028 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1153 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1029 } 1154 }
1030 1155
1031 glDisable GL_TEXTURE_2D; 1156 glDisable GL_TEXTURE_2D;
1032 1157
1048sub new { 1173sub new {
1049 my $class = shift; 1174 my $class = shift;
1050 1175
1051 $class->SUPER::new ( 1176 $class->SUPER::new (
1052 col_expand => [], 1177 col_expand => [],
1053 @_ 1178 @_,
1054 ) 1179 )
1180}
1181
1182sub children {
1183 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1055} 1184}
1056 1185
1057sub add { 1186sub add {
1058 my ($self, $x, $y, $child) = @_; 1187 my ($self, $x, $y, $child) = @_;
1059 1188
1060 $child->set_parent ($self); 1189 $child->set_parent ($self);
1061 $self->{children}[$y][$x] = $child; 1190 $self->{children}[$y][$x] = $child;
1062 1191
1063 $child->check_size; 1192 $self->realloc;
1064} 1193}
1065 1194
1066sub children {
1067 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1068}
1069
1070# TODO: move to container class maybe? send childs a signal on removal? 1195# TODO: move to container class maybe? send children a signal on removal?
1071sub clear { 1196sub clear {
1072 my ($self) = @_; 1197 my ($self) = @_;
1073 1198
1074 my @children = $self->children; 1199 my @children = $self->children;
1075 delete $self->{children}; 1200 delete $self->{children};
1077 for (@children) { 1202 for (@children) {
1078 delete $_->{parent}; 1203 delete $_->{parent};
1079 $_->hide; 1204 $_->hide;
1080 } 1205 }
1081 1206
1082 $self->update; 1207 $self->realloc;
1083} 1208}
1084 1209
1085sub get_wh { 1210sub get_wh {
1086 my ($self) = @_; 1211 my ($self) = @_;
1087 1212
1114 (sum @$hs), 1239 (sum @$hs),
1115 ) 1240 )
1116} 1241}
1117 1242
1118sub size_allocate { 1243sub size_allocate {
1119 my ($self, $w, $h) = @_; 1244 my ($self, $w, $h, $changed) = @_;
1120 1245
1121 my ($ws, $hs) = $self->get_wh; 1246 my ($ws, $hs) = $self->get_wh;
1122 1247
1123 my $req_w = sum @$ws; 1248 my $req_w = (sum @$ws) || 1;
1124 my $req_h = sum @$hs; 1249 my $req_h = (sum @$hs) || 1;
1125 1250
1126 # TODO: nicer code && do row_expand 1251 # TODO: nicer code && do row_expand
1127 my @col_expand = @{$self->{col_expand}}; 1252 my @col_expand = @{$self->{col_expand}};
1128 @col_expand = (1) x @$ws unless @col_expand; 1253 @col_expand = (1) x @$ws unless @col_expand;
1129 my $col_expand = (sum @col_expand) || 1; 1254 my $col_expand = (sum @col_expand) || 1;
1183 } 1308 }
1184} 1309}
1185 1310
1186############################################################################# 1311#############################################################################
1187 1312
1313package CFClient::UI::Box;
1314
1315our @ISA = CFClient::UI::Container::;
1316
1317sub size_request {
1318 my ($self) = @_;
1319
1320 $self->{vertical}
1321 ? (
1322 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1323 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1324 )
1325 : (
1326 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1327 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1328 )
1329}
1330
1331sub size_allocate {
1332 my ($self, $w, $h, $changed) = @_;
1333
1334 my $space = $self->{vertical} ? $h : $w;
1335 my $children = $self->{children};
1336
1337 my @req;
1338
1339 if ($self->{homogeneous}) {
1340 @req = ($space / (@$children || 1)) x @$children;
1341 } else {
1342 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1343 my $req = List::Util::sum @req;
1344
1345 if ($req > $space) {
1346 # ah well, not enough space
1347 $_ *= $space / $req for @req;
1348 } else {
1349 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1350
1351 $space = ($space - $req) / $expand; # remaining space to give away
1352
1353 $req[$_] += $space * $children->[$_]{expand}
1354 for 0 .. $#$children;
1355 }
1356 }
1357
1358 CFClient::UI::harmonize \@req;
1359
1360 my $pos = 0;
1361 for (0 .. $#$children) {
1362 my $alloc = $req[$_];
1363 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1364
1365 $pos += $alloc;
1366 }
1367
1368 1
1369}
1370
1371#############################################################################
1372
1188package CFClient::UI::HBox; 1373package CFClient::UI::HBox;
1189 1374
1190# TODO: wrap into common Box base class
1191
1192our @ISA = CFClient::UI::Container::; 1375our @ISA = CFClient::UI::Box::;
1193 1376
1194sub size_request { 1377sub new {
1195 my ($self) = @_; 1378 my $class = shift;
1196 1379
1197 my @alloc = map [$_->size_request], @{$self->{children}}; 1380 $class->SUPER::new (
1198 1381 vertical => 0,
1199 ( 1382 @_,
1200 (List::Util::sum map $_->[0], @alloc),
1201 (List::Util::max map $_->[1], @alloc),
1202 ) 1383 )
1203} 1384}
1204 1385
1205sub size_allocate {
1206 my ($self, $w, $h) = @_;
1207
1208 ($h, $w) = ($w, $h);
1209
1210 my $children = $self->{children};
1211
1212 my @h = map $_->{req_w}, @$children;
1213
1214 my $req_h = List::Util::sum @h;
1215
1216 if ($req_h > $h) {
1217 # ah well, not enough space
1218 $_ *= $h / $req_h for @h;
1219 } else {
1220 my $exp = List::Util::sum map $_->{expand}, @$children;
1221 $exp ||= 1;
1222
1223 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 }
1230 }
1231
1232 CFClient::UI::harmonize \@h;
1233
1234 my $y = 0;
1235 for (0 .. $#$children) {
1236 my $child = $children->[$_];
1237 my $h = $h[$_];
1238 $child->configure ($y, 0, $h, $w);
1239
1240 $y += $h;
1241 }
1242
1243 1
1244}
1245
1246############################################################################# 1386#############################################################################
1247 1387
1248package CFClient::UI::VBox; 1388package CFClient::UI::VBox;
1249 1389
1250# TODO: wrap into common Box base class
1251
1252our @ISA = CFClient::UI::Container::; 1390our @ISA = CFClient::UI::Box::;
1253 1391
1254sub size_request { 1392sub new {
1255 my ($self) = @_; 1393 my $class = shift;
1256 1394
1257 my @alloc = map [$_->size_request], @{$self->{children}}; 1395 $class->SUPER::new (
1258 1396 vertical => 1,
1259 ( 1397 @_,
1260 (List::Util::max map $_->[0], @alloc),
1261 (List::Util::sum map $_->[1], @alloc),
1262 ) 1398 )
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} 1399}
1303 1400
1304############################################################################# 1401#############################################################################
1305 1402
1306package CFClient::UI::Label; 1403package CFClient::UI::Label;
1369 $self->{text} = "T$text"; 1466 $self->{text} = "T$text";
1370 1467
1371 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1468 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1372 $self->{layout}->set_text ($text); 1469 $self->{layout}->set_text ($text);
1373 1470
1471 $self->realloc;
1374 $self->update; 1472 $self->update;
1375 $self->check_size;
1376} 1473}
1377 1474
1378sub set_markup { 1475sub set_markup {
1379 my ($self, $markup) = @_; 1476 my ($self, $markup) = @_;
1380 1477
1384 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1481 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1385 1482
1386 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1483 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1387 $self->{layout}->set_markup ($markup); 1484 $self->{layout}->set_markup ($markup);
1388 1485
1486 $self->realloc;
1389 $self->update; 1487 $self->update;
1390 $self->check_size;
1391} 1488}
1392 1489
1393sub size_request { 1490sub size_request {
1394 my ($self) = @_; 1491 my ($self) = @_;
1395 1492
1416 $h + $self->{padding} * 2, 1513 $h + $self->{padding} * 2,
1417 ) 1514 )
1418} 1515}
1419 1516
1420sub size_allocate { 1517sub size_allocate {
1421 my ($self, $w, $h) = @_; 1518 my ($self, $w, $h, $changed) = @_;
1422 1519
1423 delete $self->{texture}; 1520 delete $self->{texture}
1521 if $changed;
1424} 1522}
1425 1523
1426sub set_fontsize { 1524sub set_fontsize {
1427 my ($self, $fontsize) = @_; 1525 my ($self, $fontsize) = @_;
1428 1526
1429 $self->{fontsize} = $fontsize; 1527 $self->{fontsize} = $fontsize;
1430 delete $self->{texture}; 1528 delete $self->{texture};
1431 1529
1432 $self->update; 1530 $self->realloc;
1433 $self->check_size;
1434} 1531}
1435 1532
1436sub _draw { 1533sub _draw {
1437 my ($self) = @_; 1534 my ($self) = @_;
1438 1535
1510 $self->{text} = $text; 1607 $self->{text} = $text;
1511 1608
1512 $text =~ s/./*/g if $self->{hidden}; 1609 $text =~ s/./*/g if $self->{hidden};
1513 $self->{layout}->set_text ("$text "); 1610 $self->{layout}->set_text ("$text ");
1514 1611
1515 $self->emit (changed => $self->{text}); 1612 $self->_emit (changed => $self->{text});
1516} 1613}
1517 1614
1518sub set_text { 1615sub set_text {
1519 my ($self, $text) = @_; 1616 my ($self, $text) = @_;
1520 1617
1521 $self->{cursor} = length $text; 1618 $self->{cursor} = length $text;
1522 $self->_set_text ($text); 1619 $self->_set_text ($text);
1523 $self->update; 1620
1524 $self->check_size; 1621 $self->realloc;
1525} 1622}
1526 1623
1527sub get_text { 1624sub get_text {
1528 $_[0]{text} 1625 $_[0]{text}
1529} 1626}
1532 my ($self) = @_; 1629 my ($self) = @_;
1533 1630
1534 my ($w, $h) = $self->SUPER::size_request; 1631 my ($w, $h) = $self->SUPER::size_request;
1535 1632
1536 ($w + 1, $h) # add 1 for cursor 1633 ($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} 1634}
1544 1635
1545sub key_down { 1636sub key_down {
1546 my ($self, $ev) = @_; 1637 my ($self, $ev) = @_;
1547 1638
1562 } elsif ($sym == CFClient::SDLK_HOME) { 1653 } elsif ($sym == CFClient::SDLK_HOME) {
1563 $self->{cursor} = 0; 1654 $self->{cursor} = 0;
1564 } elsif ($sym == CFClient::SDLK_END) { 1655 } elsif ($sym == CFClient::SDLK_END) {
1565 $self->{cursor} = length $text; 1656 $self->{cursor} = length $text;
1566 } elsif ($uni == 27) { 1657 } elsif ($uni == 27) {
1567 $self->emit ('escape'); 1658 $self->_emit ('escape');
1568 } elsif ($uni) { 1659 } elsif ($uni) {
1569 substr $text, $self->{cursor}++, 0, chr $uni; 1660 substr $text, $self->{cursor}++, 0, chr $uni;
1570 } 1661 }
1571 1662
1572 $self->_set_text ($text); 1663 $self->_set_text ($text);
1573 $self->update; 1664
1574 $self->check_size; 1665 $self->realloc;
1575} 1666}
1576 1667
1577sub focus_in { 1668sub focus_in {
1578 my ($self) = @_; 1669 my ($self) = @_;
1579 1670
1659 if ($sym == 13) { 1750 if ($sym == 13) {
1660 unshift @{$self->{history}}, 1751 unshift @{$self->{history}},
1661 my $txt = $self->get_text; 1752 my $txt = $self->get_text;
1662 $self->{history_pointer} = -1; 1753 $self->{history_pointer} = -1;
1663 $self->{history_saveback} = ''; 1754 $self->{history_saveback} = '';
1664 $self->emit (activate => $txt); 1755 $self->_emit (activate => $txt);
1665 $self->update; 1756 $self->update;
1666 1757
1667 } elsif ($sym == CFClient::SDLK_UP) { 1758 } elsif ($sym == CFClient::SDLK_UP) {
1668 if ($self->{history_pointer} < 0) { 1759 if ($self->{history_pointer} < 0) {
1669 $self->{history_saveback} = $self->get_text; 1760 $self->{history_saveback} = $self->get_text;
1717 can_events => 1, 1808 can_events => 1,
1718 @_ 1809 @_
1719 ) 1810 )
1720} 1811}
1721 1812
1813sub activate { }
1814
1722sub button_up { 1815sub button_up {
1723 my ($self, $ev, $x, $y) = @_; 1816 my ($self, $ev, $x, $y) = @_;
1724 1817
1818 $self->emit ("activate")
1725 if ($x >= 0 && $x < $self->{w} 1819 if $x >= 0 && $x < $self->{w}
1726 && $y >= 0 && $y < $self->{h}) { 1820 && $y >= 0 && $y < $self->{h};
1727 $self->emit ("activate");
1728 }
1729} 1821}
1730 1822
1731sub _draw { 1823sub _draw {
1732 my ($self) = @_; 1824 my ($self) = @_;
1733 1825
1785 my ($self, $ev, $x, $y) = @_; 1877 my ($self, $ev, $x, $y) = @_;
1786 1878
1787 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1879 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1788 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1880 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1789 $self->{state} = !$self->{state}; 1881 $self->{state} = !$self->{state};
1790 $self->emit (changed => $self->{state}); 1882 $self->_emit (changed => $self->{state});
1791 } 1883 }
1792} 1884}
1793 1885
1794sub _draw { 1886sub _draw {
1795 my ($self) = @_; 1887 my ($self) = @_;
2071 fg => [1, 1, 1], 2163 fg => [1, 1, 1],
2072 active_fg => [0, 0, 0], 2164 active_fg => [0, 0, 0],
2073 bg => [0, 0, 0, 0.2], 2165 bg => [0, 0, 0, 0.2],
2074 active_bg => [1, 1, 1, 0.5], 2166 active_bg => [1, 1, 1, 0.5],
2075 range => [0, 0, 100, 10, 0], 2167 range => [0, 0, 100, 10, 0],
2076 req_w => $::WIDTH / 80, 2168 min_w => $::WIDTH / 80,
2077 req_h => $::WIDTH / 80, 2169 min_h => $::WIDTH / 80,
2078 vertical => 0, 2170 vertical => 0,
2079 can_hover => 1, 2171 can_hover => 1,
2080 inner_pad => 0.02, 2172 inner_pad => 0.02,
2081 @_ 2173 @_
2082 ); 2174 );
2085 $self->update; 2177 $self->update;
2086 2178
2087 $self 2179 $self
2088} 2180}
2089 2181
2182sub changed { }
2183
2090sub set_range { 2184sub set_range {
2091 my ($self, $range) = @_; 2185 my ($self, $range) = @_;
2092 2186
2093 $self->{range} = $range; 2187 ($range, $self->{range}) = ($self->{range}, $range);
2094 2188
2095 $self->update; 2189 $self->update
2190 if "@$range" ne "@{$self->{range}}";
2096} 2191}
2097 2192
2098sub set_value { 2193sub set_value {
2099 my ($self, $value) = @_; 2194 my ($self, $value) = @_;
2100 2195
2111 if $unit; 2206 if $unit;
2112 2207
2113 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2208 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2114 2209
2115 if ($value != $old_value) { 2210 if ($value != $old_value) {
2116 $self->emit (changed => $value); 2211 $self->_emit (changed => $value);
2117 $self->update; 2212 $self->update;
2118 } 2213 }
2119} 2214}
2120 2215
2121sub size_request { 2216sub size_request {
2122 my ($self) = @_; 2217 my ($self) = @_;
2123 2218
2124 my $w = $self->{req_w}; 2219 ($self->{req_w}, $self->{req_h})
2125 my $h = $self->{req_h};
2126
2127 $self->{vertical} ? ($h, $w) : ($w, $h)
2128} 2220}
2129 2221
2130sub button_down { 2222sub button_down {
2131 my ($self, $ev, $x, $y) = @_; 2223 my ($self, $ev, $x, $y) = @_;
2132 2224
2282 $self->{fontsize} = $fontsize; 2374 $self->{fontsize} = $fontsize;
2283 $self->reflow; 2375 $self->reflow;
2284} 2376}
2285 2377
2286sub size_allocate { 2378sub size_allocate {
2287 my ($self, $w, $h) = @_; 2379 my ($self, $w, $h, $changed) = @_;
2288 2380
2289 $self->SUPER::size_allocate ($w, $h); 2381 $self->SUPER::size_allocate ($w, $h, $changed);
2382
2383 return unless $changed;
2290 2384
2291 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2385 $self->{layout}->set_font ($self->{font}) if $self->{font};
2292 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2386 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2293 $self->{layout}->set_width ($self->{children}[0]{w}); 2387 $self->{layout}->set_width ($self->{children}[0]{w});
2294 2388
2483 2577
2484sub new { 2578sub new {
2485 my $class = shift; 2579 my $class = shift;
2486 2580
2487 my $self = $class->SUPER::new ( 2581 my $self = $class->SUPER::new (
2488 state => 0, 2582 state => 0,
2489 connect_activate => \&toggle_flopper, 2583 on_activate => \&toggle_flopper,
2490 @_ 2584 @_
2491 ); 2585 );
2492 2586
2493 if ($self->{state}) {
2494 $self->{state} = 0;
2495 $self->toggle_flopper;
2496 }
2497
2498 $self 2587 $self
2499} 2588}
2500 2589
2501sub toggle_flopper { 2590sub toggle_flopper {
2502 my ($self) = @_; 2591 my ($self) = @_;
2503 2592
2504 # TODO: use animation 2593 $self->{other}->toggle_visibility;
2505 if ($self->{state} = !$self->{state}) {
2506 $CFClient::UI::ROOT->add ($self->{other});
2507 $self->{other}->move ($self->coord2global (0, $self->{h}));
2508 $self->emit ("open");
2509 } else {
2510 $CFClient::UI::ROOT->remove ($self->{other});
2511 $self->emit ("close");
2512 }
2513
2514 $self->emit (changed => $self->{state});
2515} 2594}
2516 2595
2517############################################################################# 2596#############################################################################
2518 2597
2519package CFClient::UI::Tooltip; 2598package CFClient::UI::Tooltip;
2551 2630
2552 ($w + 4, $h + 4) 2631 ($w + 4, $h + 4)
2553} 2632}
2554 2633
2555sub size_allocate { 2634sub size_allocate {
2556 my ($self, $w, $h) = @_; 2635 my ($self, $w, $h, $changed) = @_;
2557 2636
2637 return unless $changed;
2638
2558 $self->SUPER::size_allocate ($w - 4, $h - 4); 2639 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2640}
2641
2642sub visibility_change {
2643 my ($self, $visible) = @_;
2644
2645 return unless $visible;
2646
2647 $self->{root}->on_post_alloc ("move_$self" => sub {
2648 my $widget = $self->{owner}
2649 or return;
2650
2651 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2652
2653 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2654 if $x + $self->{w} > $::WIDTH;
2655
2656 $self->move_abs ($x, $y);
2657 });
2559} 2658}
2560 2659
2561sub _draw { 2660sub _draw {
2562 my ($self) = @_; 2661 my ($self) = @_;
2563 2662
2580 glVertex $w, $h; 2679 glVertex $w, $h;
2581 glVertex $w, 0; 2680 glVertex $w, 0;
2582 glEnd; 2681 glEnd;
2583 2682
2584 glTranslate 2 - 0.375, 2 - 0.375; 2683 glTranslate 2 - 0.375, 2 - 0.375;
2684
2585 $self->SUPER::_draw; 2685 $self->SUPER::_draw;
2586} 2686}
2587 2687
2588############################################################################# 2688#############################################################################
2589 2689
2595 2695
2596sub new { 2696sub new {
2597 my $class = shift; 2697 my $class = shift;
2598 2698
2599 my $self = $class->SUPER::new ( 2699 my $self = $class->SUPER::new (
2600 aspect => 1, 2700 aspect => 1,
2701 can_events => 0,
2601 @_, 2702 @_,
2602 ); 2703 );
2603 2704
2604 if ($self->{anim} && $self->{animspeed}) { 2705 if ($self->{anim} && $self->{animspeed}) {
2605 Scalar::Util::weaken (my $widget = $self); 2706 Scalar::Util::weaken (my $widget = $self);
2664 $self->SUPER::DESTROY; 2765 $self->SUPER::DESTROY;
2665} 2766}
2666 2767
2667############################################################################# 2768#############################################################################
2668 2769
2669package CFClient::UI::InventoryItem; 2770package CFClient::UI::Inventory;
2670 2771
2671our @ISA = CFClient::UI::HBox::; 2772our @ISA = CFClient::UI::ScrolledWindow::;
2672
2673sub _item_to_desc {
2674 my ($item) = @_;
2675
2676 my $desc =
2677 $item->{nrof} < 2
2678 ? $item->{name}
2679 : "$item->{nrof} × $item->{name_pl}";
2680
2681 $item->{flags} & Crossfire::Protocol::F_OPEN
2682 and $desc .= " (open)";
2683 $item->{flags} & Crossfire::Protocol::F_APPLIED
2684 and $desc .= " (applied)";
2685 $item->{flags} & Crossfire::Protocol::F_UNPAID
2686 and $desc .= " (unpaid)";
2687 $item->{flags} & Crossfire::Protocol::F_MAGIC
2688 and $desc .= " (magic)";
2689 $item->{flags} & Crossfire::Protocol::F_CURSED
2690 and $desc .= " (cursed)";
2691 $item->{flags} & Crossfire::Protocol::F_DAMNED
2692 and $desc .= " (damned)";
2693 $item->{flags} & Crossfire::Protocol::F_LOCKED
2694 and $desc .= " *";
2695
2696 $desc
2697}
2698 2773
2699sub new { 2774sub new {
2700 my $class = shift; 2775 my $class = shift;
2701 2776
2702 my %args = @_;
2703
2704 my $item = delete $args{item};
2705
2706 my $desc = _item_to_desc ($item);
2707
2708 my $self = $class->SUPER::new ( 2777 my $self = $class->SUPER::new (
2709 can_hover => 1,
2710 can_events => 1,
2711 tooltip => ((CFClient::UI::Label::escape $desc)
2712 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2713 connect_button_down => sub {
2714 my ($self, $ev, $x, $y) = @_;
2715
2716 # todo: maybe put examine on 1? but should just be a tooltip :(
2717 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2718 my $targ = $::CONN->{player}{tag};
2719
2720 if ($item->{container} == $::CONN->{player}{tag}) {
2721 $targ = $main::OPENCONT;
2722 }
2723
2724 $::CONN->send ("move $targ $item->{tag} 0");
2725 } elsif ($ev->{button} == 1) {
2726 $::CONN->send ("examine $item->{tag}");
2727 } elsif ($ev->{button} == 2) {
2728 $::CONN->send ("apply $item->{tag}");
2729 } elsif ($ev->{button} == 3) {
2730 my @menu_items = (
2731 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2732 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2733 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2734 (
2735 $item->{flags} & Crossfire::Protocol::F_LOCKED
2736 ? (
2737 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2738 )
2739 : (
2740 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2741 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2742 )
2743 ),
2744 );
2745
2746 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2747 }
2748
2749 1
2750 },
2751 %args
2752 );
2753
2754
2755 $self->add (new CFClient::UI::Face
2756 can_events => 0,
2757 face => $item->{face},
2758 anim => $item->{anim},
2759 animspeed => $item->{animspeed},
2760 );
2761
2762 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2763
2764 $self->{item} = $item;
2765
2766 $self->update_item;
2767
2768 $self
2769}
2770
2771sub update_item {
2772 my ($self) = @_;
2773
2774 my $desc = _item_to_desc ($self->{item});
2775
2776 $self->{name_lbl}->set_text ($desc);
2777}
2778
2779#############################################################################
2780
2781package CFClient::UI::Inventory;
2782
2783our @ISA = CFClient::UI::ScrolledWindow::;
2784
2785sub new {
2786 my $class = shift;
2787
2788 my $self = $class->SUPER::new (
2789 scrolled => (new CFClient::UI::Table), 2778 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2790 @_, 2779 @_,
2791 ); 2780 );
2792 2781
2793 $self 2782 $self
2794} 2783}
2804 or ($a->{name} cmp $b->{name}) 2793 or ($a->{name} cmp $b->{name})
2805 } @$items; 2794 } @$items;
2806 2795
2807 $self->{real_items} = \@items; 2796 $self->{real_items} = \@items;
2808 2797
2798 my $row = 0;
2809 for my $item (@items) { 2799 for my $item (@items) {
2810 $item->{item} = $item; 2800 CFClient::Item::update_widgets $item;
2811 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2812 $item->update_item ();
2813 }
2814 2801
2815 my $i = 0; 2802 $self->{scrolled}->add (0, $row, $item->{face_widget});
2816 for (@items) { 2803 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2817 $self->{scrolled}->add (0, $i, $_); 2804 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2818 my $nrof = $_->{item}->{nrof} || 1;
2819 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2820 }
2821 2805
2822# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2806 $row++;
2823} 2807 }
2824
2825sub size_request {
2826 my ($self) = @_;
2827 ($self->{req_w}, $self->{req_h});
2828} 2808}
2829 2809
2830############################################################################# 2810#############################################################################
2831 2811
2832package CFClient::UI::Menu; 2812package CFClient::UI::Menu;
2867 2847
2868# popup given the event (must be a mouse button down event currently) 2848# popup given the event (must be a mouse button down event currently)
2869sub popup { 2849sub popup {
2870 my ($self, $ev) = @_; 2850 my ($self, $ev) = @_;
2871 2851
2872 $self->emit ("popdown"); 2852 $self->_emit ("popdown");
2873 2853
2874 # maybe save $GRAB? must be careful about events... 2854 # maybe save $GRAB? must be careful about events...
2875 $GRAB = $self; 2855 $GRAB = $self;
2876 $self->{button} = $ev->{button}; 2856 $self->{button} = $ev->{button};
2877 2857
2892 2872
2893 if ($ev->{button} == $self->{button}) { 2873 if ($ev->{button} == $self->{button}) {
2894 undef $GRAB; 2874 undef $GRAB;
2895 $self->hide; 2875 $self->hide;
2896 2876
2897 $self->emit ("popdown"); 2877 $self->_emit ("popdown");
2898 $self->{hover}[1]->() if $self->{hover}; 2878 $self->{hover}[1]->() if $self->{hover};
2899 } 2879 }
2900} 2880}
2901 2881
2902############################################################################# 2882#############################################################################
2964sub add { 2944sub add {
2965 my ($self, $text, %arg) = @_; 2945 my ($self, $text, %arg) = @_;
2966 2946
2967 $text =~ s/^\s+//; 2947 $text =~ s/^\s+//;
2968 $text =~ s/\s+$//; 2948 $text =~ s/\s+$//;
2949
2950 return unless $text;
2969 2951
2970 my $timeout = time + ((delete $arg{timeout}) || 60); 2952 my $timeout = time + ((delete $arg{timeout}) || 60);
2971 2953
2972 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2954 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2973 2955
3016use CFClient::OpenGL; 2998use CFClient::OpenGL;
3017 2999
3018sub new { 3000sub new {
3019 my $class = shift; 3001 my $class = shift;
3020 3002
3021 $class->SUPER::new ( 3003 my $self = $class->SUPER::new (
3004 visible => 1,
3022 @_, 3005 @_,
3023 ) 3006 );
3007
3008 Scalar::Util::weaken ($self->{root} = $self);
3009
3010 $self
3024} 3011}
3025 3012
3026sub configure { 3013sub configure {
3027 my ($self, $x, $y, $w, $h) = @_; 3014 my ($self, $x, $y, $w, $h) = @_;
3028 3015
3029 $self->{w} = $w; 3016 $self->{w} = $w;
3030 $self->{h} = $h; 3017 $self->{h} = $h;
3031} 3018}
3032 3019
3033sub check_size { 3020sub reconfigure {
3034 my ($self) = @_; 3021 my ($self) = @_;
3035 3022
3023 $self->SUPER::reconfigure;
3024
3036 $self->size_allocate ($self->{w}, $self->{h}) 3025 $self->size_allocate ($self->{w}, $self->{h}, 1)
3037 if $self->{w}; 3026 if $self->{w};
3038} 3027}
3039 3028
3040sub size_request { 3029sub size_request {
3041 my ($self) = @_; 3030 my ($self) = @_;
3042 3031
3043 ($self->{w}, $self->{h}) 3032 ($self->{w}, $self->{h})
3044} 3033}
3045 3034
3035sub _to_pixel {
3036 my ($coord, $size, $max) = @_;
3037
3038 $coord =
3039 $coord eq "center" ? ($max - $size) * 0.5
3040 : $coord eq "max" ? $max
3041 : $coord;
3042
3043 $coord = 0 if $coord < 0;
3044 $coord = $max - $size if $coord > $max - $size;
3045
3046 int $coord + 0.5
3047}
3048
3046sub size_allocate { 3049sub size_allocate {
3047 my ($self, $w, $h) = @_; 3050 my ($self, $w, $h, $changed) = @_;
3048
3049 my $old_w = $self->{old_w}; $self->{old_w} = $w;
3050 my $old_h = $self->{old_h}; $self->{old_h} = $h;
3051
3052 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
3053 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
3054 3051
3055 for my $child ($self->children) { 3052 for my $child ($self->children) {
3056 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3053 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3057 3054
3058 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3055 $X = $child->{force_x} if exists $child->{force_x};
3059 if exists $child->{req_x}; 3056 $Y = $child->{force_y} if exists $child->{force_y};
3060 3057
3061 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3058 $X = _to_pixel $X, $W, $self->{w};
3062 if exists $child->{req_y}; 3059 $Y = _to_pixel $Y, $H, $self->{h};
3063
3064 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3065 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3066 3060
3067 $child->configure ($X, $Y, $W, $H); 3061 $child->configure ($X, $Y, $W, $H);
3068 } 3062 }
3069} 3063}
3070 3064
3081} 3075}
3082 3076
3083sub update { 3077sub update {
3084 my ($self) = @_; 3078 my ($self) = @_;
3085 3079
3086 $self->check_size;
3087 $::WANT_REFRESH++; 3080 $::WANT_REFRESH++;
3088} 3081}
3089 3082
3090sub add { 3083sub add {
3091 my ($self, @children) = @_; 3084 my ($self, @children) = @_;
3092 3085
3093 for my $child (@children) {
3094 $child->{toplevel} = 1; 3086 $_->{is_toplevel} = 1
3095 3087 for @children;
3096 # integerise window positions
3097 $child->{x} = int $child->{x};
3098 $child->{y} = int $child->{y};
3099 }
3100 3088
3101 $self->SUPER::add (@children); 3089 $self->SUPER::add (@children);
3090}
3091
3092sub remove {
3093 my ($self, @children) = @_;
3094
3095 $self->SUPER::remove (@children);
3096
3097 delete $self->{is_toplevel}
3098 for @children;
3102 3099
3103 while (@children) { 3100 while (@children) {
3104 my $w = pop @children; 3101 my $w = pop @children;
3105 push @children, $w->children; 3102 push @children, $w->children;
3106 $w->{visible} = 1; 3103 $w->set_invisible;
3107 }
3108}
3109
3110sub remove {
3111 my ($self, @children) = @_;
3112
3113 $self->SUPER::remove (@children);
3114
3115 while (@children) {
3116 my $w = pop @children;
3117 push @children, $w->children;
3118 delete $w->{visible};
3119 } 3104 }
3120} 3105}
3121 3106
3122sub on_refresh { 3107sub on_refresh {
3123 my ($self, $id, $cb) = @_; 3108 my ($self, $id, $cb) = @_;
3137 while ($self->{refresh_hook}) { 3122 while ($self->{refresh_hook}) {
3138 $_->() 3123 $_->()
3139 for values %{delete $self->{refresh_hook}}; 3124 for values %{delete $self->{refresh_hook}};
3140 } 3125 }
3141 3126
3142 if ($self->{check_size}) { 3127 if ($self->{realloc}) {
3143 my @queue = ([], []); 3128 my @queue;
3144 3129
3145 for (;;) { 3130 while () {
3146 if ($self->{check_size}) { 3131 if ($self->{realloc}) {
3147 # heuristic: check containers last 3132 #TODO use array-of-depth approach
3148 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3133
3149 for values %{delete $self->{check_size}} 3134 @queue = sort { $a->{visible} <=> $b->{visible} }
3135 @queue, values %{delete $self->{realloc}};
3150 } 3136 }
3151 3137
3152 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3138 my $widget = pop @queue || last;
3153 3139
3154 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3140 $widget->{visible} or last; # do not resize invisible widgets
3155 ? @$widget{qw(user_w user_h)} 3141
3156 : $widget->size_request; 3142 my ($w, $h) = $widget->size_request;
3157 3143
3158 if (delete $widget->{force_alloc} 3144 my $min_size = $widget->{is_toplevel} ? 16 : 0;
3159 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3145
3160 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3146 $w = List::Util::max $widget->{min_w} || $widget->{is_toplevel} * 16, $w;
3161 3147 $h = List::Util::max $widget->{min_h} || $widget->{is_toplevel} * 16, $h;
3148
3149 $w = $widget->{force_w} if exists $widget->{force_w};
3150 $h = $widget->{force_h} if exists $widget->{force_h};
3151
3162 $widget->{req_w} = $w; 3152 $widget->{req_w} = $w;
3163 $widget->{req_h} = $h; 3153 $widget->{req_h} = $h;
3164 3154
3165 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3155 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3166 3156
3167 $widget->{parent}->check_size 3157 push @queue, $widget->{parent}
3168 if $widget->{parent}; 3158 if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent};
3169 }
3170 } 3159 }
3171 } 3160 }
3172 3161
3173 while ($self->{size_alloc}) { 3162 while (my $size_alloc = delete $self->{size_alloc}) {
3174 for (values %{delete $self->{size_alloc}}) { 3163 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3175 my ($widget, $w, $h) = @$_; 3164 values %$size_alloc;
3165
3166 while () {
3167 my ($widget, $w, $h) = @{ pop @queue or last };
3168
3169 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3170 $h = $widget->{h} || $widget->{req_h} unless defined $h;
3176 3171
3177 $w = 0 if $w < 0; 3172 $w = 0 if $w < 0;
3178 $h = 0 if $h < 0; 3173 $h = 0 if $h < 0;
3179 3174
3175 $w = int $w + 0.5;
3176 $h = int $h + 0.5;
3177
3178 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3179
3180 $widget->{w} = $w; 3180 $widget->{w} = $w;
3181 $widget->{h} = $h; 3181 $widget->{h} = $h;
3182 $widget->size_allocate ($w, $h); 3182
3183 $widget->emit (size_allocate => $w, $h); 3183 $widget->emit (size_allocate => $w, $h, $changed);
3184 } 3184 }
3185 } 3185 }
3186 3186
3187 while ($self->{post_alloc_hook}) { 3187 while ($self->{post_alloc_hook}) {
3188 $_->() 3188 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines