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.230 by root, Wed May 24 21:59:54 2006 UTC vs.
Revision 1.252 by root, Sun May 28 23:21:28 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}) {
27 my $tip = $widget->{tooltip}; 47 my $tip = $widget->{tooltip};
28 48
29 $tip = $tip->($widget) if CODE:: eq ref $tip; 49 $tip = $tip->($widget) if CODE:: eq ref $tip;
30 50
31 $TOOLTIP->set_tooltip_from ($widget); 51 $TOOLTIP->set_tooltip_from ($widget);
32 $TOOLTIP->show;
33 52
34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 53 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
35 54
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0) 55 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 if $x + $TOOLTIP->{w} > $::WIDTH; 56 if $x + $TOOLTIP->{w} > $::WIDTH;
38 57
39 $TOOLTIP->move ($x, $y); 58 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update; 59 $TOOLTIP->show;
42 } 60 }
43 61
44 return; 62 return;
45 } 63 }
46 } 64 }
50 delete $TOOLTIP->{owner}; 68 delete $TOOLTIP->{owner};
51} 69}
52 70
53# class methods for events 71# class methods for events
54sub feed_sdl_key_down_event { 72sub feed_sdl_key_down_event {
55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 73 $FOCUS->emit (key_down => $_[0])
56 if $FOCUS; 74 if $FOCUS;
57} 75}
58 76
59sub feed_sdl_key_up_event { 77sub feed_sdl_key_up_event {
60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 78 $FOCUS->emit (key_up => $_[0])
61 if $FOCUS; 79 if $FOCUS;
62} 80}
63 81
64sub feed_sdl_button_down_event { 82sub feed_sdl_button_down_event {
65 my ($ev) = @_; 83 my ($ev) = @_;
74 check_tooltip; 92 check_tooltip;
75 } 93 }
76 94
77 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 95 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
78 96
79 if ($GRAB) { 97 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
80 ($x, $y) = $GRAB->coord2local ($x, $y); 98 if $GRAB;
81 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
82 }
83} 99}
84 100
85sub feed_sdl_button_up_event { 101sub feed_sdl_button_up_event {
86 my ($ev) = @_; 102 my ($ev) = @_;
87 my ($x, $y) = ($ev->{x}, $ev->{y}); 103 my ($x, $y) = ($ev->{x}, $ev->{y});
88 104
89 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 105 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
90 106
91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 107 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
92 108
93 if ($GRAB) { 109 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
94 ($x, $y) = $GRAB->coord2local ($x, $y); 110 if $GRAB;
95 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
96 }
97 111
98 if (!$BUTTON_STATE) { 112 if (!$BUTTON_STATE) {
99 my $grab = $GRAB; undef $GRAB; 113 my $grab = $GRAB; undef $GRAB;
100 $grab->update if $grab; 114 $grab->update if $grab;
101 $GRAB->update if $GRAB; 115 $GRAB->update if $GRAB;
117 $HOVER->update if $HOVER && $HOVER->{can_hover}; 131 $HOVER->update if $HOVER && $HOVER->{can_hover};
118 132
119 check_tooltip; 133 check_tooltip;
120 } 134 }
121 135
122 if ($HOVER) {
123 ($x, $y) = $HOVER->coord2local ($x, $y);
124 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); 136 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
125 } 137 if $HOVER;
126} 138}
127 139
128# convert position array to integers 140# convert position array to integers
129sub harmonize { 141sub harmonize {
130 my ($vals) = @_; 142 my ($vals) = @_;
161 for my $widget (values %WIDGET) { 173 for my $widget (values %WIDGET) {
162 if ($widget->{is_toplevel}) { 174 if ($widget->{is_toplevel}) {
163 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 175 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
164 $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};
165 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 177 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
166 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
167 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 178 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
168 $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};
169 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 180 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
170 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
171 } 181 }
172 } 182 }
173 183
174 reconfigure_widgets; 184 reconfigure_widgets;
175} 185}
192 can_events => 1, 202 can_events => 1,
193 @_ 203 @_
194 }, $class; 204 }, $class;
195 205
196 for (keys %$self) { 206 for (keys %$self) {
197 if (/^connect_(.*)$/) { 207 if (/^on_(.*)$/) {
198 $self->connect ($1 => delete $self->{$_}); 208 $self->connect ($1 => delete $self->{$_});
199 } 209 }
200 } 210 }
201 211
202 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 212 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
203 213
214 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
215 $self->{req_x} = $layout->{x} * $::WIDTH;
216 $self->{req_y} = $layout->{y} * $::HEIGHT;
217 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
218 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
219 }
220
204 $self 221 $self
205} 222}
206 223
207sub destroy { 224sub destroy {
208 my ($self) = @_; 225 my ($self) = @_;
211 %$self = (); 228 %$self = ();
212} 229}
213 230
214sub show { 231sub show {
215 my ($self) = @_; 232 my ($self) = @_;
233
216 return if $self->{parent}; 234 return if $self->{parent};
217 235
218 $CFClient::UI::ROOT->add ($self); 236 $CFClient::UI::ROOT->add ($self);
219} 237}
220 238
221sub show_centered { 239sub center {
222 my ($self) = @_; 240 my ($self) = @_;
223 return if $self->{parent};
224
225 $self->show;
226 241
227 $CFClient::UI::ROOT->on_post_alloc ( 242 $CFClient::UI::ROOT->on_post_alloc (
228 "centered $self" => sub { 243 "center_$self" => sub {
229 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 244 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
230 }, 245 },
231 ); 246 );
232}
233 247
234sub hide { 248 $self->update;
249}
250
251sub set_visible {
235 my ($self) = @_; 252 my ($self) = @_;
253
254 return if $self->{visible};
255
256 $self->{root} = $self->{parent}{root};
257 $self->{visible} = $self->{parent}{visible} + 1;
258
259 $self->emit (visibility_change => 1);
260
261 $self->realloc if !exists $self->{req_w};
262
263 $_->set_visible for $self->children;
264}
265
266sub set_invisible {
267 my ($self) = @_;
268
269 return unless $self->{visible};
270
271 $_->set_invisible for $self->children;
272
273 delete $self->{root};
274 delete $self->{visible};
236 275
237 undef $GRAB if $GRAB == $self; 276 undef $GRAB if $GRAB == $self;
238 undef $HOVER if $HOVER == $self; 277 undef $HOVER if $HOVER == $self;
278
279 CFClient::UI::check_tooltip
280 if $TOOLTIP->{owner} == $self;
281
282 $self->focus_out;
283
284 $self->emit (visibility_change => 0);
285}
286
287sub set_visibility {
288 my ($self, $visible) = @_;
289
290 return if $self->{visible} == $visible;
291
292 $visible ? $self->hide
293 : $self->show;
294}
295
296sub toggle_visibility {
297 my ($self) = @_;
298
299 $self->{visible}
300 ? $self->hide
301 : $self->show;
302}
303
304sub hide {
305 my ($self) = @_;
306
307 $self->set_invisible;
239 308
240 $self->{parent}->remove ($self) 309 $self->{parent}->remove ($self)
241 if $self->{parent}; 310 if $self->{parent};
242} 311}
243 312
252} 321}
253 322
254sub set_size { 323sub set_size {
255 my ($self, $w, $h) = @_; 324 my ($self, $w, $h) = @_;
256 325
257 $self->{user_w} = $w; 326 $self->{def_w} = $w;
258 $self->{user_h} = $h; 327 $self->{def_h} = $h;
259 328
260 $self->check_size; 329 $self->realloc;
261} 330}
262 331
263sub size_request { 332sub size_request {
264 require Carp; 333 require Carp;
265 Carp::confess "size_request is abstract"; 334 Carp::confess "size_request is abstract";
285 $self->{y} = $y; 354 $self->{y} = $y;
286 $self->update; 355 $self->update;
287 } 356 }
288 357
289 if ($self->{w} != $w || $self->{h} != $h) { 358 if ($self->{w} != $w || $self->{h} != $h) {
359 return unless $self->{visible};
360
290 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 361 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
291 } 362 }
292} 363}
293 364
294sub size_allocate { 365sub size_allocate {
295 # nothing to be done 366 # nothing to be done
296} 367}
297 368
298sub reconfigure {
299 my ($self) = @_;
300
301 $self->check_size (1);
302 $self->update;
303}
304
305sub children { 369sub children {
306} 370}
307 371
308sub set_max_size { 372sub set_max_size {
309 my ($self, $w, $h) = @_; 373 my ($self, $w, $h) = @_;
312 delete $self->{max_h}; $self->{max_h} = $h if $h; 376 delete $self->{max_h}; $self->{max_h} = $h if $h;
313} 377}
314 378
315sub set_tooltip { 379sub set_tooltip {
316 my ($self, $tooltip) = @_; 380 my ($self, $tooltip) = @_;
381
382 $tooltip =~ s/^\s+//;
383 $tooltip =~ s/\s+$//;
384
385 return if $self->{tooltip} eq $tooltip;
317 386
318 $self->{tooltip} = $tooltip; 387 $self->{tooltip} = $tooltip;
319 388
320 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 389 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
321 delete $CFClient::UI::TOOLTIP->{owner}; 390 delete $CFClient::UI::TOOLTIP->{owner};
343 return if $FOCUS == $self; 412 return if $FOCUS == $self;
344 return unless $self->{can_focus}; 413 return unless $self->{can_focus};
345 414
346 my $focus = $FOCUS; $FOCUS = $self; 415 my $focus = $FOCUS; $FOCUS = $self;
347 416
348 $self->emit (focus_in => $focus); 417 $self->_emit (focus_in => $focus);
349 418
350 $focus->update if $focus; 419 $focus->update if $focus;
351 $FOCUS->update; 420 $FOCUS->update;
352} 421}
353 422
356 425
357 return unless $FOCUS == $self; 426 return unless $FOCUS == $self;
358 427
359 my $focus = $FOCUS; undef $FOCUS; 428 my $focus = $FOCUS; undef $FOCUS;
360 429
361 $self->emit (focus_out => $focus); 430 $self->_emit (focus_out => $focus);
362 431
363 $focus->update if $focus; #? 432 $focus->update if $focus; #?
433
434 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
435 unless $FOCUS;
364} 436}
365 437
366sub mouse_motion { } 438sub mouse_motion { }
367sub button_up { } 439sub button_up { }
368sub key_down { } 440sub key_down { }
369sub key_up { } 441sub key_up { }
370 442
371sub button_down { 443sub button_down {
372 my ($self, $ev, $x, $y) = @_; 444 my ($self, $ev, $x, $y) = @_;
373 445
374 $self->focus_in; 446 $self->focus_in;
377sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 449sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
378sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 450sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
379sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 451sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
380sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 452sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
381sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 453sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
454
455sub find_widget {
456 my ($self, $x, $y) = @_;
457
458 return () unless $self->{can_events};
459
460 return $self
461 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
462 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
463
464 ()
465}
466
467sub set_parent {
468 my ($self, $parent) = @_;
469
470 Scalar::Util::weaken ($self->{parent} = $parent);
471
472 $self->set_visible if $parent->{visible};
473}
474
475sub connect {
476 my ($self, $signal, $cb) = @_;
477
478 push @{ $self->{signal_cb}{$signal} }, $cb;
479}
480
481sub _emit {
482 my ($self, $signal, @args) = @_;
483
484 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
485}
486
487sub emit {
488 my ($self, $signal, @args) = @_;
489
490 $self->_emit ($signal, @args)
491 || $self->$signal (@args);
492}
493
494sub visibility_change {
495 #my ($self, $visible) = @_;
496}
497
498sub realloc {
499 my ($self) = @_;
500
501 if ($self->{visible}) {
502 return if $self->{root}{realloc}{$self};
503
504 $self->{root}{realloc}{$self} = $self;
505 $self->{root}->update;
506 } else {
507 delete $self->{req_w};
508 }
509}
510
511sub update {
512 my ($self) = @_;
513
514 $self->{parent}->update
515 if $self->{parent};
516}
382 517
383sub draw { 518sub draw {
384 my ($self) = @_; 519 my ($self) = @_;
385 520
386 return unless $self->{h} && $self->{w}; 521 return unless $self->{h} && $self->{w};
408 if ($ENV{PCLIENT_DEBUG}) { 543 if ($ENV{PCLIENT_DEBUG}) {
409 glPushMatrix; 544 glPushMatrix;
410 glColor 1, 1, 0, 1; 545 glColor 1, 1, 0, 1;
411 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 546 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
412 glBegin GL_LINE_LOOP; 547 glBegin GL_LINE_LOOP;
413 glVertex 0 , 0; 548 glVertex 0 , 0;
414 glVertex $self->{w}, 0; 549 glVertex $self->{w} - 1, 0;
415 glVertex $self->{w}, $self->{h}; 550 glVertex $self->{w} - 1, $self->{h} - 1;
416 glVertex 0 , $self->{h}; 551 glVertex 0 , $self->{h} - 1;
417 glEnd; 552 glEnd;
418 glPopMatrix; 553 glPopMatrix;
419 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 554 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
420 } 555 }
421} 556}
422 557
423sub _draw { 558sub _draw {
424 my ($self) = @_; 559 my ($self) = @_;
425 560
426 warn "no draw defined for $self\n"; 561 warn "no draw defined for $self\n";
427}
428
429sub find_widget {
430 my ($self, $x, $y) = @_;
431
432 return () unless $self->{can_events};
433
434 return $self
435 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
436 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
437
438 ()
439}
440
441sub set_parent {
442 my ($self, $parent) = @_;
443
444 Scalar::Util::weaken ($self->{parent} = $parent);
445
446 # TODO: req_w _does_change after ->reconfigure
447 $self->check_size
448 unless exists $self->{req_w};
449
450 $self->show;
451}
452
453sub check_size {
454 my ($self, $forced) = @_;
455
456 $self->{force_alloc} = 1 if $forced;
457 $CFClient::UI::ROOT->{check_size}{$self} = $self;
458}
459
460sub update {
461 my ($self) = @_;
462
463 $self->{parent}->update
464 if $self->{parent};
465}
466
467sub connect {
468 my ($self, $signal, $cb) = @_;
469
470 push @{ $self->{signal_cb}{$signal} }, $cb;
471}
472
473sub emit {
474 my ($self, $signal, @args) = @_;
475
476 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
477} 562}
478 563
479sub DESTROY { 564sub DESTROY {
480 my ($self) = @_; 565 my ($self) = @_;
481 566
578 $self->{children} = [ 663 $self->{children} = [
579 sort { $a->{z} <=> $b->{z} } 664 sort { $a->{z} <=> $b->{z} }
580 @{$self->{children}}, @widgets 665 @{$self->{children}}, @widgets
581 ]; 666 ];
582 667
583 $self->check_size (1); 668 $self->realloc;
584 $self->update;
585} 669}
586 670
587sub children { 671sub children {
588 @{ $_[0]{children} } 672 @{ $_[0]{children} }
589} 673}
594 delete $child->{parent}; 678 delete $child->{parent};
595 $child->hide; 679 $child->hide;
596 680
597 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 681 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
598 682
599 $self->check_size; 683 $self->realloc;
600 $self->update;
601} 684}
602 685
603sub clear { 686sub clear {
604 my ($self) = @_; 687 my ($self) = @_;
605 688
609 for (@$children) { 692 for (@$children) {
610 delete $_->{parent}; 693 delete $_->{parent};
611 $_->hide; 694 $_->hide;
612 } 695 }
613 696
614 $self->check_size; 697 $self->realloc;
615 $self->update;
616} 698}
617 699
618sub find_widget { 700sub find_widget {
619 my ($self, $x, $y) = @_; 701 my ($self, $x, $y) = @_;
620 702
673sub size_request { 755sub size_request {
674 $_[0]{children}[0]->size_request 756 $_[0]{children}[0]->size_request
675} 757}
676 758
677sub size_allocate { 759sub size_allocate {
678 my ($self, $w, $h) = @_; 760 my ($self, $w, $h, $changed) = @_;
679 761
680 $self->{children}[0]->configure (0, 0, $w, $h); 762 $self->{children}[0]->configure (0, 0, $w, $h);
681} 763}
682 764
683############################################################################# 765#############################################################################
700 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 782 $ROOT->on_post_alloc ($self => sub { $self->render_child });
701 $self->SUPER::update; 783 $self->SUPER::update;
702} 784}
703 785
704sub size_allocate { 786sub size_allocate {
705 my ($self, $w, $h) = @_; 787 my ($self, $w, $h, $changed) = @_;
706 788
707 $self->SUPER::size_allocate ($w, $h); 789 $self->SUPER::size_allocate ($w, $h, $changed);
708 $self->update; 790 $self->update
791 if $changed;
709} 792}
710 793
711sub _render { 794sub _render {
712 $_[0]{children}[0]->draw; 795 $_[0]{children}[0]->draw;
713} 796}
744 827
745package CFClient::UI::ViewPort; 828package CFClient::UI::ViewPort;
746 829
747our @ISA = CFClient::UI::Window::; 830our @ISA = CFClient::UI::Window::;
748 831
832sub new {
833 my $class = shift;
834
835 $class->SUPER::new (
836 scroll_x => 0,
837 scroll_y => 1,
838 @_,
839 )
840}
841
749sub size_request { 842sub size_request {
750 my ($self) = @_; 843 my ($self) = @_;
751 844
752 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 845 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
753 846
754 @$self{qw(child_w child_h)} 847 $w = 10 if $self->{scroll_x};
848 $h = 10 if $self->{scroll_y};
849
850 ($w, $h)
755} 851}
756 852
757sub size_allocate { 853sub size_allocate {
758 my ($self, $w, $h) = @_; 854 my ($self, $w, $h, $changed) = @_;
759 855
760 my ($cw, $ch) = @$self{qw(child_w child_h)}; 856 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
761# $w = $self->{w}; 857 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
858
762 $self->child->configure (0, 0, $cw, $ch); 859 $self->child->configure (0, 0, $w, $h);
763 $self->update; 860 $self->update;
764} 861}
765 862
766sub set_offset { 863sub set_offset {
767 my ($self, $x, $y) = @_; 864 my ($self, $x, $y) = @_;
818 my $class = shift; 915 my $class = shift;
819 916
820 my $self; 917 my $self;
821 918
822 my $slider = new CFClient::UI::Slider 919 my $slider = new CFClient::UI::Slider
823 vertical => 1, 920 vertical => 1,
824 range => [0, 0, 1, 0.01], # HACK fix 921 range => [0, 0, 1, 0.01], # HACK fix
825 connect_changed => sub { 922 on_changed => sub {
826 $self->{vp}->set_offset (0, $_[1]); 923 $self->{vp}->set_offset (0, $_[1]);
827 }, 924 },
828 ; 925 ;
829 926
830 $self = $class->SUPER::new ( 927 $self = $class->SUPER::new (
838 $self->add ($self->{slider}); 935 $self->add ($self->{slider});
839 936
840 $self 937 $self
841} 938}
842 939
940sub update {
941 my ($self) = @_;
942
943 $self->SUPER::update;
944
945 # todo: overwrite size_allocate of child
946 my $child = $self->{vp}->child;
947 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
948}
949
843sub size_allocate { 950sub size_allocate {
844 my ($self, $w, $h) = @_; 951 my ($self, $w, $h, $changed) = @_;
845 952
846 $self->SUPER::size_allocate ($w, $h); 953 $self->SUPER::size_allocate ($w, $h, $changed);
847 954
848 my $child = $self->{vp}->child; 955 my $child = $self->{vp}->child;
849 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 956 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
850} 957}
851 958
904 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1011 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
905 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1012 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
906 1013
907sub new { 1014sub new {
908 my $class = shift; 1015 my $class = shift;
909
910 # TODO: user_x, user_y, overwrite moveto?
911 1016
912 my $self = $class->SUPER::new ( 1017 my $self = $class->SUPER::new (
913 bg => [1, 1, 1, 1], 1018 bg => [1, 1, 1, 1],
914 border_bg => [1, 1, 1, 1], 1019 border_bg => [1, 1, 1, 1],
915 border => 0.6, 1020 border => 0.6,
941 $h + $self->border * 2, 1046 $h + $self->border * 2,
942 ) 1047 )
943} 1048}
944 1049
945sub size_allocate { 1050sub size_allocate {
946 my ($self, $w, $h) = @_; 1051 my ($self, $w, $h, $changed) = @_;
1052
1053 return unless $changed;
947 1054
948 $h -= List::Util::max 0, $self->border * 2; 1055 $h -= List::Util::max 0, $self->border * 2;
949 $w -= List::Util::max 0, $self->border * 2; 1056 $w -= List::Util::max 0, $self->border * 2;
950 1057
951 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1058 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
975 my ($ev, $x, $y) = @_; 1082 my ($ev, $x, $y) = @_;
976 1083
977 my $dx = $ev->{x} - $ox; 1084 my $dx = $ev->{x} - $ox;
978 my $dy = $ev->{y} - $oy; 1085 my $dy = $ev->{y} - $oy;
979 1086
1087 $self->{user_x} = $wx + $dx * $mx;
1088 $self->{user_y} = $wy + $dy * $my;
980 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1089 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
981 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1090 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
982 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1091 $self->move ($self->{user_x}, $self->{user_y});
983 $self->check_size; 1092 $self->realloc;
984 }; 1093 };
985 1094
986 } elsif ($lr ^ $td) { 1095 } elsif ($lr ^ $td) {
987 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1096 my ($ox, $oy) = ($ev->{x}, $ev->{y});
988 my ($bx, $by) = ($self->{x}, $self->{y}); 1097 my ($bx, $by) = ($self->{x}, $self->{y});
990 $self->{motion} = sub { 1099 $self->{motion} = sub {
991 my ($ev, $x, $y) = @_; 1100 my ($ev, $x, $y) = @_;
992 1101
993 ($x, $y) = ($ev->{x}, $ev->{y}); 1102 ($x, $y) = ($ev->{x}, $ev->{y});
994 1103
995 $self->move ($bx + $x - $ox, $by + $y - $oy); 1104 $self->{user_x} = $bx + $x - $ox;
1105 $self->{user_y} = $by + $y - $oy;
1106
1107 $self->move ($self->{user_x}, $self->{user_y});
996 $self->update; 1108 $self->realloc;
997 }; 1109 };
998 } 1110 }
999} 1111}
1000 1112
1001sub button_up { 1113sub button_up {
1062sub new { 1174sub new {
1063 my $class = shift; 1175 my $class = shift;
1064 1176
1065 $class->SUPER::new ( 1177 $class->SUPER::new (
1066 col_expand => [], 1178 col_expand => [],
1067 @_ 1179 @_,
1068 ) 1180 )
1181}
1182
1183sub children {
1184 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1069} 1185}
1070 1186
1071sub add { 1187sub add {
1072 my ($self, $x, $y, $child) = @_; 1188 my ($self, $x, $y, $child) = @_;
1073 1189
1074 $child->set_parent ($self); 1190 $child->set_parent ($self);
1075 $self->{children}[$y][$x] = $child; 1191 $self->{children}[$y][$x] = $child;
1076 1192
1077 $child->check_size; 1193 $self->realloc;
1078} 1194}
1079 1195
1080sub children {
1081 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1082}
1083
1084# TODO: move to container class maybe? send childs a signal on removal? 1196# TODO: move to container class maybe? send children a signal on removal?
1085sub clear { 1197sub clear {
1086 my ($self) = @_; 1198 my ($self) = @_;
1087 1199
1088 my @children = $self->children; 1200 my @children = $self->children;
1089 delete $self->{children}; 1201 delete $self->{children};
1091 for (@children) { 1203 for (@children) {
1092 delete $_->{parent}; 1204 delete $_->{parent};
1093 $_->hide; 1205 $_->hide;
1094 } 1206 }
1095 1207
1096 $self->update; 1208 $self->realloc;
1097} 1209}
1098 1210
1099sub get_wh { 1211sub get_wh {
1100 my ($self) = @_; 1212 my ($self) = @_;
1101 1213
1128 (sum @$hs), 1240 (sum @$hs),
1129 ) 1241 )
1130} 1242}
1131 1243
1132sub size_allocate { 1244sub size_allocate {
1133 my ($self, $w, $h) = @_; 1245 my ($self, $w, $h, $changed) = @_;
1134 1246
1135 my ($ws, $hs) = $self->get_wh; 1247 my ($ws, $hs) = $self->get_wh;
1136 1248
1137 my $req_w = sum @$ws; 1249 my $req_w = (sum @$ws) || 1;
1138 my $req_h = sum @$hs; 1250 my $req_h = (sum @$hs) || 1;
1139 1251
1140 # TODO: nicer code && do row_expand 1252 # TODO: nicer code && do row_expand
1141 my @col_expand = @{$self->{col_expand}}; 1253 my @col_expand = @{$self->{col_expand}};
1142 @col_expand = (1) x @$ws unless @col_expand; 1254 @col_expand = (1) x @$ws unless @col_expand;
1143 my $col_expand = (sum @col_expand) || 1; 1255 my $col_expand = (sum @col_expand) || 1;
1197 } 1309 }
1198} 1310}
1199 1311
1200############################################################################# 1312#############################################################################
1201 1313
1314package CFClient::UI::Box;
1315
1316our @ISA = CFClient::UI::Container::;
1317
1318sub size_request {
1319 my ($self) = @_;
1320
1321 $self->{vertical}
1322 ? (
1323 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1324 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1325 )
1326 : (
1327 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1328 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1329 )
1330}
1331
1332sub size_allocate {
1333 my ($self, $w, $h, $changed) = @_;
1334
1335 my $space = $self->{vertical} ? $h : $w;
1336 my $children = $self->{children};
1337
1338 my @req;
1339
1340 if ($self->{homogeneous}) {
1341 @req = ($space / (@$children || 1)) x @$children;
1342 } else {
1343 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1344 my $req = List::Util::sum @req;
1345
1346 if ($req > $space) {
1347 # ah well, not enough space
1348 $_ *= $space / $req for @req;
1349 } else {
1350 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1351
1352 $space = ($space - $req) / $expand; # remaining space to give away
1353
1354 $req[$_] += $space * $children->[$_]{expand}
1355 for 0 .. $#$children;
1356 }
1357 }
1358
1359 CFClient::UI::harmonize \@req;
1360
1361 my $pos = 0;
1362 for (0 .. $#$children) {
1363 my $alloc = $req[$_];
1364 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1365
1366 $pos += $alloc;
1367 }
1368
1369 1
1370}
1371
1372#############################################################################
1373
1202package CFClient::UI::HBox; 1374package CFClient::UI::HBox;
1203 1375
1204# TODO: wrap into common Box base class
1205
1206our @ISA = CFClient::UI::Container::; 1376our @ISA = CFClient::UI::Box::;
1207 1377
1208sub size_request { 1378sub new {
1209 my ($self) = @_; 1379 my $class = shift;
1210 1380
1211 my @alloc = map [$_->size_request], @{$self->{children}}; 1381 $class->SUPER::new (
1212 1382 vertical => 0,
1213 ( 1383 @_,
1214 (List::Util::sum map $_->[0], @alloc),
1215 (List::Util::max map $_->[1], @alloc),
1216 ) 1384 )
1217} 1385}
1218 1386
1219sub size_allocate {
1220 my ($self, $w, $h) = @_;
1221
1222 ($h, $w) = ($w, $h);
1223
1224 my $children = $self->{children};
1225
1226 my @h = map $_->{req_w}, @$children;
1227
1228 my $req_h = List::Util::sum @h;
1229
1230 if ($req_h > $h) {
1231 # ah well, not enough space
1232 $_ *= $h / $req_h for @h;
1233 } else {
1234 my $exp = List::Util::sum map $_->{expand}, @$children;
1235 $exp ||= 1;
1236
1237 for (0 .. $#$children) {
1238 my $child = $children->[$_];
1239
1240 my $alloc_h = $h[$_];
1241 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1242 $h[$_] = $alloc_h;
1243 }
1244 }
1245
1246 CFClient::UI::harmonize \@h;
1247
1248 my $y = 0;
1249 for (0 .. $#$children) {
1250 my $child = $children->[$_];
1251 my $h = $h[$_];
1252 $child->configure ($y, 0, $h, $w);
1253
1254 $y += $h;
1255 }
1256
1257 1
1258}
1259
1260############################################################################# 1387#############################################################################
1261 1388
1262package CFClient::UI::VBox; 1389package CFClient::UI::VBox;
1263 1390
1264# TODO: wrap into common Box base class
1265
1266our @ISA = CFClient::UI::Container::; 1391our @ISA = CFClient::UI::Box::;
1267 1392
1268sub size_request { 1393sub new {
1269 my ($self) = @_; 1394 my $class = shift;
1270 1395
1271 my @alloc = map [$_->size_request], @{$self->{children}}; 1396 $class->SUPER::new (
1272 1397 vertical => 1,
1273 ( 1398 @_,
1274 (List::Util::max map $_->[0], @alloc),
1275 (List::Util::sum map $_->[1], @alloc),
1276 ) 1399 )
1277}
1278
1279sub size_allocate {
1280 my ($self, $w, $h) = @_;
1281
1282 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1283
1284 my $children = $self->{children};
1285
1286 my @h = map $_->{req_h}, @$children;
1287
1288 my $req_h = List::Util::sum @h;
1289
1290 if ($req_h > $h) {
1291 # ah well, not enough space
1292 $_ *= $h / $req_h for @h;
1293 } else {
1294 my $exp = List::Util::sum map $_->{expand}, @$children;
1295 $exp ||= 1;
1296
1297 for (0 .. $#$children) {
1298 my $child = $children->[$_];
1299
1300 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1301 }
1302 }
1303
1304 CFClient::UI::harmonize \@h;
1305
1306 my $y = 0;
1307 for (0 .. $#$children) {
1308 my $child = $children->[$_];
1309 my $h = $h[$_];
1310 $child->configure (0, $y, $w, $h);
1311
1312 $y += $h;
1313 }
1314
1315 1
1316} 1400}
1317 1401
1318############################################################################# 1402#############################################################################
1319 1403
1320package CFClient::UI::Label; 1404package CFClient::UI::Label;
1383 $self->{text} = "T$text"; 1467 $self->{text} = "T$text";
1384 1468
1385 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1469 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1386 $self->{layout}->set_text ($text); 1470 $self->{layout}->set_text ($text);
1387 1471
1472 $self->realloc;
1388 $self->update; 1473 $self->update;
1389 $self->check_size;
1390} 1474}
1391 1475
1392sub set_markup { 1476sub set_markup {
1393 my ($self, $markup) = @_; 1477 my ($self, $markup) = @_;
1394 1478
1398 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1482 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1399 1483
1400 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1484 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1401 $self->{layout}->set_markup ($markup); 1485 $self->{layout}->set_markup ($markup);
1402 1486
1487 $self->realloc;
1403 $self->update; 1488 $self->update;
1404 $self->check_size;
1405} 1489}
1406 1490
1407sub size_request { 1491sub size_request {
1408 my ($self) = @_; 1492 my ($self) = @_;
1409 1493
1430 $h + $self->{padding} * 2, 1514 $h + $self->{padding} * 2,
1431 ) 1515 )
1432} 1516}
1433 1517
1434sub size_allocate { 1518sub size_allocate {
1435 my ($self, $w, $h) = @_; 1519 my ($self, $w, $h, $changed) = @_;
1436 1520
1437 delete $self->{texture}; 1521 delete $self->{texture}
1522 if $changed;
1438} 1523}
1439 1524
1440sub set_fontsize { 1525sub set_fontsize {
1441 my ($self, $fontsize) = @_; 1526 my ($self, $fontsize) = @_;
1442 1527
1443 $self->{fontsize} = $fontsize; 1528 $self->{fontsize} = $fontsize;
1444 delete $self->{texture}; 1529 delete $self->{texture};
1445 1530
1446 $self->update; 1531 $self->realloc;
1447 $self->check_size;
1448} 1532}
1449 1533
1450sub _draw { 1534sub _draw {
1451 my ($self) = @_; 1535 my ($self) = @_;
1452 1536
1524 $self->{text} = $text; 1608 $self->{text} = $text;
1525 1609
1526 $text =~ s/./*/g if $self->{hidden}; 1610 $text =~ s/./*/g if $self->{hidden};
1527 $self->{layout}->set_text ("$text "); 1611 $self->{layout}->set_text ("$text ");
1528 1612
1529 $self->emit (changed => $self->{text}); 1613 $self->_emit (changed => $self->{text});
1530} 1614}
1531 1615
1532sub set_text { 1616sub set_text {
1533 my ($self, $text) = @_; 1617 my ($self, $text) = @_;
1534 1618
1535 $self->{cursor} = length $text; 1619 $self->{cursor} = length $text;
1536 $self->_set_text ($text); 1620 $self->_set_text ($text);
1537 $self->update; 1621
1538 $self->check_size; 1622 $self->realloc;
1539} 1623}
1540 1624
1541sub get_text { 1625sub get_text {
1542 $_[0]{text} 1626 $_[0]{text}
1543} 1627}
1546 my ($self) = @_; 1630 my ($self) = @_;
1547 1631
1548 my ($w, $h) = $self->SUPER::size_request; 1632 my ($w, $h) = $self->SUPER::size_request;
1549 1633
1550 ($w + 1, $h) # add 1 for cursor 1634 ($w + 1, $h) # add 1 for cursor
1551}
1552
1553sub size_allocate {
1554 my ($self, $w, $h) = @_;
1555
1556 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1557} 1635}
1558 1636
1559sub key_down { 1637sub key_down {
1560 my ($self, $ev) = @_; 1638 my ($self, $ev) = @_;
1561 1639
1576 } elsif ($sym == CFClient::SDLK_HOME) { 1654 } elsif ($sym == CFClient::SDLK_HOME) {
1577 $self->{cursor} = 0; 1655 $self->{cursor} = 0;
1578 } elsif ($sym == CFClient::SDLK_END) { 1656 } elsif ($sym == CFClient::SDLK_END) {
1579 $self->{cursor} = length $text; 1657 $self->{cursor} = length $text;
1580 } elsif ($uni == 27) { 1658 } elsif ($uni == 27) {
1581 $self->emit ('escape'); 1659 $self->_emit ('escape');
1582 } elsif ($uni) { 1660 } elsif ($uni) {
1583 substr $text, $self->{cursor}++, 0, chr $uni; 1661 substr $text, $self->{cursor}++, 0, chr $uni;
1584 } 1662 }
1585 1663
1586 $self->_set_text ($text); 1664 $self->_set_text ($text);
1587 $self->update; 1665
1588 $self->check_size; 1666 $self->realloc;
1589} 1667}
1590 1668
1591sub focus_in { 1669sub focus_in {
1592 my ($self) = @_; 1670 my ($self) = @_;
1593 1671
1673 if ($sym == 13) { 1751 if ($sym == 13) {
1674 unshift @{$self->{history}}, 1752 unshift @{$self->{history}},
1675 my $txt = $self->get_text; 1753 my $txt = $self->get_text;
1676 $self->{history_pointer} = -1; 1754 $self->{history_pointer} = -1;
1677 $self->{history_saveback} = ''; 1755 $self->{history_saveback} = '';
1678 $self->emit (activate => $txt); 1756 $self->_emit (activate => $txt);
1679 $self->update; 1757 $self->update;
1680 1758
1681 } elsif ($sym == CFClient::SDLK_UP) { 1759 } elsif ($sym == CFClient::SDLK_UP) {
1682 if ($self->{history_pointer} < 0) { 1760 if ($self->{history_pointer} < 0) {
1683 $self->{history_saveback} = $self->get_text; 1761 $self->{history_saveback} = $self->get_text;
1731 can_events => 1, 1809 can_events => 1,
1732 @_ 1810 @_
1733 ) 1811 )
1734} 1812}
1735 1813
1814sub activate { }
1815
1736sub button_up { 1816sub button_up {
1737 my ($self, $ev, $x, $y) = @_; 1817 my ($self, $ev, $x, $y) = @_;
1738 1818
1819 $self->emit ("activate")
1739 if ($x >= 0 && $x < $self->{w} 1820 if $x >= 0 && $x < $self->{w}
1740 && $y >= 0 && $y < $self->{h}) { 1821 && $y >= 0 && $y < $self->{h};
1741 $self->emit ("activate");
1742 }
1743} 1822}
1744 1823
1745sub _draw { 1824sub _draw {
1746 my ($self) = @_; 1825 my ($self) = @_;
1747 1826
1799 my ($self, $ev, $x, $y) = @_; 1878 my ($self, $ev, $x, $y) = @_;
1800 1879
1801 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1880 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1802 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1881 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1803 $self->{state} = !$self->{state}; 1882 $self->{state} = !$self->{state};
1804 $self->emit (changed => $self->{state}); 1883 $self->_emit (changed => $self->{state});
1805 } 1884 }
1806} 1885}
1807 1886
1808sub _draw { 1887sub _draw {
1809 my ($self) = @_; 1888 my ($self) = @_;
2099 $self->update; 2178 $self->update;
2100 2179
2101 $self 2180 $self
2102} 2181}
2103 2182
2183sub changed { }
2184
2104sub set_range { 2185sub set_range {
2105 my ($self, $range) = @_; 2186 my ($self, $range) = @_;
2106 2187
2107 $self->{range} = $range; 2188 ($range, $self->{range}) = ($self->{range}, $range);
2108 2189
2109 $self->update; 2190 $self->update
2191 if "@$range" ne "@{$self->{range}}";
2110} 2192}
2111 2193
2112sub set_value { 2194sub set_value {
2113 my ($self, $value) = @_; 2195 my ($self, $value) = @_;
2114 2196
2125 if $unit; 2207 if $unit;
2126 2208
2127 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2209 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2128 2210
2129 if ($value != $old_value) { 2211 if ($value != $old_value) {
2130 $self->emit (changed => $value); 2212 $self->_emit (changed => $value);
2131 $self->update; 2213 $self->update;
2132 } 2214 }
2133} 2215}
2134 2216
2135sub size_request { 2217sub size_request {
2296 $self->{fontsize} = $fontsize; 2378 $self->{fontsize} = $fontsize;
2297 $self->reflow; 2379 $self->reflow;
2298} 2380}
2299 2381
2300sub size_allocate { 2382sub size_allocate {
2301 my ($self, $w, $h) = @_; 2383 my ($self, $w, $h, $changed) = @_;
2302 2384
2303 $self->SUPER::size_allocate ($w, $h); 2385 $self->SUPER::size_allocate ($w, $h, $changed);
2386
2387 return unless $changed;
2304 2388
2305 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2389 $self->{layout}->set_font ($self->{font}) if $self->{font};
2306 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2390 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2307 $self->{layout}->set_width ($self->{children}[0]{w}); 2391 $self->{layout}->set_width ($self->{children}[0]{w});
2308 2392
2497 2581
2498sub new { 2582sub new {
2499 my $class = shift; 2583 my $class = shift;
2500 2584
2501 my $self = $class->SUPER::new ( 2585 my $self = $class->SUPER::new (
2502 state => 0, 2586 state => 0,
2503 connect_activate => \&toggle_flopper, 2587 on_activate => \&toggle_flopper,
2504 @_ 2588 @_
2505 ); 2589 );
2506 2590
2507 if ($self->{state}) {
2508 $self->{state} = 0;
2509 $self->toggle_flopper;
2510 }
2511
2512 $self 2591 $self
2513} 2592}
2514 2593
2515sub toggle_flopper { 2594sub toggle_flopper {
2516 my ($self) = @_; 2595 my ($self) = @_;
2517 2596
2518 # TODO: use animation 2597 $self->{other}->toggle_visibility;
2519 if ($self->{state} = !$self->{state}) {
2520 $CFClient::UI::ROOT->add ($self->{other});
2521 $self->{other}->move ($self->coord2global (0, $self->{h}));
2522 $self->emit ("open");
2523 } else {
2524 $CFClient::UI::ROOT->remove ($self->{other});
2525 $self->emit ("close");
2526 }
2527
2528 $self->emit (changed => $self->{state});
2529} 2598}
2530 2599
2531############################################################################# 2600#############################################################################
2532 2601
2533package CFClient::UI::Tooltip; 2602package CFClient::UI::Tooltip;
2565 2634
2566 ($w + 4, $h + 4) 2635 ($w + 4, $h + 4)
2567} 2636}
2568 2637
2569sub size_allocate { 2638sub size_allocate {
2570 my ($self, $w, $h) = @_; 2639 my ($self, $w, $h, $changed) = @_;
2571 2640
2641 return unless $changed;
2642
2572 $self->SUPER::size_allocate ($w - 4, $h - 4); 2643 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2573} 2644}
2574 2645
2575sub _draw { 2646sub _draw {
2576 my ($self) = @_; 2647 my ($self) = @_;
2577 2648
2594 glVertex $w, $h; 2665 glVertex $w, $h;
2595 glVertex $w, 0; 2666 glVertex $w, 0;
2596 glEnd; 2667 glEnd;
2597 2668
2598 glTranslate 2 - 0.375, 2 - 0.375; 2669 glTranslate 2 - 0.375, 2 - 0.375;
2670
2599 $self->SUPER::_draw; 2671 $self->SUPER::_draw;
2600} 2672}
2601 2673
2602############################################################################# 2674#############################################################################
2603 2675
2609 2681
2610sub new { 2682sub new {
2611 my $class = shift; 2683 my $class = shift;
2612 2684
2613 my $self = $class->SUPER::new ( 2685 my $self = $class->SUPER::new (
2614 aspect => 1, 2686 aspect => 1,
2687 can_events => 0,
2615 @_, 2688 @_,
2616 ); 2689 );
2617 2690
2618 if ($self->{anim} && $self->{animspeed}) { 2691 if ($self->{anim} && $self->{animspeed}) {
2619 Scalar::Util::weaken (my $widget = $self); 2692 Scalar::Util::weaken (my $widget = $self);
2678 $self->SUPER::DESTROY; 2751 $self->SUPER::DESTROY;
2679} 2752}
2680 2753
2681############################################################################# 2754#############################################################################
2682 2755
2683package CFClient::UI::InventoryItem; 2756package CFClient::UI::Inventory;
2684 2757
2685our @ISA = CFClient::UI::HBox::; 2758our @ISA = CFClient::UI::ScrolledWindow::;
2686
2687sub _item_to_desc {
2688 my ($item) = @_;
2689
2690 my $desc =
2691 $item->{nrof} < 2
2692 ? $item->{name}
2693 : "$item->{nrof} × $item->{name_pl}";
2694
2695 $item->{flags} & Crossfire::Protocol::F_OPEN
2696 and $desc .= " (open)";
2697 $item->{flags} & Crossfire::Protocol::F_APPLIED
2698 and $desc .= " (applied)";
2699 $item->{flags} & Crossfire::Protocol::F_UNPAID
2700 and $desc .= " (unpaid)";
2701 $item->{flags} & Crossfire::Protocol::F_MAGIC
2702 and $desc .= " (magic)";
2703 $item->{flags} & Crossfire::Protocol::F_CURSED
2704 and $desc .= " (cursed)";
2705 $item->{flags} & Crossfire::Protocol::F_DAMNED
2706 and $desc .= " (damned)";
2707 $item->{flags} & Crossfire::Protocol::F_LOCKED
2708 and $desc .= " *";
2709
2710 $desc
2711}
2712 2759
2713sub new { 2760sub new {
2714 my $class = shift; 2761 my $class = shift;
2715 2762
2716 my %args = @_;
2717
2718 my $item = delete $args{item};
2719
2720 my $desc = _item_to_desc ($item);
2721
2722 my $self = $class->SUPER::new ( 2763 my $self = $class->SUPER::new (
2723 can_hover => 1,
2724 can_events => 1,
2725 tooltip => ((CFClient::UI::Label::escape $desc)
2726 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2727 connect_button_down => sub {
2728 my ($self, $ev, $x, $y) = @_;
2729
2730 # todo: maybe put examine on 1? but should just be a tooltip :(
2731 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2732 my $targ = $::CONN->{player}{tag};
2733
2734 if ($item->{container} == $::CONN->{player}{tag}) {
2735 $targ = $main::OPENCONT;
2736 }
2737
2738 $::CONN->send ("move $targ $item->{tag} 0");
2739 } elsif ($ev->{button} == 1) {
2740 $::CONN->send ("examine $item->{tag}");
2741 } elsif ($ev->{button} == 2) {
2742 $::CONN->send ("apply $item->{tag}");
2743 } elsif ($ev->{button} == 3) {
2744 my @menu_items = (
2745 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2746 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2747 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2748 (
2749 $item->{flags} & Crossfire::Protocol::F_LOCKED
2750 ? (
2751 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2752 )
2753 : (
2754 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2755 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2756 )
2757 ),
2758 );
2759
2760 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2761 }
2762
2763 1
2764 },
2765 %args
2766 );
2767
2768
2769 $self->add (new CFClient::UI::Face
2770 can_events => 0,
2771 face => $item->{face},
2772 anim => $item->{anim},
2773 animspeed => $item->{animspeed},
2774 );
2775
2776 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2777
2778 $self->{item} = $item;
2779
2780 $self->update_item;
2781
2782 $self
2783}
2784
2785sub update_item {
2786 my ($self) = @_;
2787
2788 my $desc = _item_to_desc ($self->{item});
2789
2790 $self->{name_lbl}->set_text ($desc);
2791}
2792
2793#############################################################################
2794
2795package CFClient::UI::Inventory;
2796
2797our @ISA = CFClient::UI::ScrolledWindow::;
2798
2799sub new {
2800 my $class = shift;
2801
2802 my $self = $class->SUPER::new (
2803 scrolled => (new CFClient::UI::Table), 2764 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2804 @_, 2765 @_,
2805 ); 2766 );
2806 2767
2807 $self 2768 $self
2808} 2769}
2818 or ($a->{name} cmp $b->{name}) 2779 or ($a->{name} cmp $b->{name})
2819 } @$items; 2780 } @$items;
2820 2781
2821 $self->{real_items} = \@items; 2782 $self->{real_items} = \@items;
2822 2783
2784 my $row = 0;
2823 for my $item (@items) { 2785 for my $item (@items) {
2824 $item->{item} = $item; 2786 CFClient::Item::update_widgets $item;
2825 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2826 $item->update_item ();
2827 }
2828 2787
2829 my $i = 0; 2788 $self->{scrolled}->add (0, $row, $item->{face_widget});
2830 for (@items) { 2789 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2831 $self->{scrolled}->add (0, $i, $_); 2790 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2832 my $nrof = $_->{item}->{nrof} || 1;
2833 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2834 }
2835 2791
2836# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2792 $row++;
2837} 2793 }
2838
2839sub size_request {
2840 my ($self) = @_;
2841 ($self->{req_w}, $self->{req_h});
2842} 2794}
2843 2795
2844############################################################################# 2796#############################################################################
2845 2797
2846package CFClient::UI::Menu; 2798package CFClient::UI::Menu;
2881 2833
2882# popup given the event (must be a mouse button down event currently) 2834# popup given the event (must be a mouse button down event currently)
2883sub popup { 2835sub popup {
2884 my ($self, $ev) = @_; 2836 my ($self, $ev) = @_;
2885 2837
2886 $self->emit ("popdown"); 2838 $self->_emit ("popdown");
2887 2839
2888 # maybe save $GRAB? must be careful about events... 2840 # maybe save $GRAB? must be careful about events...
2889 $GRAB = $self; 2841 $GRAB = $self;
2890 $self->{button} = $ev->{button}; 2842 $self->{button} = $ev->{button};
2891 2843
2906 2858
2907 if ($ev->{button} == $self->{button}) { 2859 if ($ev->{button} == $self->{button}) {
2908 undef $GRAB; 2860 undef $GRAB;
2909 $self->hide; 2861 $self->hide;
2910 2862
2911 $self->emit ("popdown"); 2863 $self->_emit ("popdown");
2912 $self->{hover}[1]->() if $self->{hover}; 2864 $self->{hover}[1]->() if $self->{hover};
2913 } 2865 }
2914} 2866}
2915 2867
2916############################################################################# 2868#############################################################################
2978sub add { 2930sub add {
2979 my ($self, $text, %arg) = @_; 2931 my ($self, $text, %arg) = @_;
2980 2932
2981 $text =~ s/^\s+//; 2933 $text =~ s/^\s+//;
2982 $text =~ s/\s+$//; 2934 $text =~ s/\s+$//;
2935
2936 return unless $text;
2983 2937
2984 my $timeout = time + ((delete $arg{timeout}) || 60); 2938 my $timeout = time + ((delete $arg{timeout}) || 60);
2985 2939
2986 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2940 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2987 2941
3030use CFClient::OpenGL; 2984use CFClient::OpenGL;
3031 2985
3032sub new { 2986sub new {
3033 my $class = shift; 2987 my $class = shift;
3034 2988
3035 $class->SUPER::new ( 2989 my $self = $class->SUPER::new (
2990 visible => 1,
3036 @_, 2991 @_,
3037 ) 2992 );
2993
2994 Scalar::Util::weaken ($self->{root} = $self);
2995
2996 $self
3038} 2997}
3039 2998
3040sub configure { 2999sub configure {
3041 my ($self, $x, $y, $w, $h) = @_; 3000 my ($self, $x, $y, $w, $h) = @_;
3042 3001
3043 $self->{w} = $w; 3002 $self->{w} = $w;
3044 $self->{h} = $h; 3003 $self->{h} = $h;
3045} 3004}
3046 3005
3047sub check_size { 3006sub reconfigure {
3048 my ($self) = @_; 3007 my ($self) = @_;
3049 3008
3009 $self->SUPER::reconfigure;
3010
3050 $self->size_allocate ($self->{w}, $self->{h}) 3011 $self->size_allocate ($self->{w}, $self->{h}, 1)
3051 if $self->{w}; 3012 if $self->{w};
3052} 3013}
3053 3014
3054sub size_request { 3015sub size_request {
3055 my ($self) = @_; 3016 my ($self) = @_;
3056 3017
3057 ($self->{w}, $self->{h}) 3018 ($self->{w}, $self->{h})
3058} 3019}
3059 3020
3060sub size_allocate { 3021sub size_allocate {
3061 my ($self, $w, $h) = @_; 3022 my ($self, $w, $h, $changed) = @_;
3062 3023
3063 for my $child ($self->children) { 3024 for my $child ($self->children) {
3064 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3025 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3065 3026
3066 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3027 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3067 if exists $child->{req_x}; 3028 if exists $child->{req_x};
3068 3029
3069 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3030 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3070 if exists $child->{req_y}; 3031 if exists $child->{req_y};
3071 3032
3033 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3034
3072 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3035 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3073 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3036 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3074 3037
3075 $child->configure ($X, $Y, $W, $H); 3038 $child->configure ($X, $Y, $W, $H);
3076 } 3039 }
3089} 3052}
3090 3053
3091sub update { 3054sub update {
3092 my ($self) = @_; 3055 my ($self) = @_;
3093 3056
3094 $self->check_size;
3095 $::WANT_REFRESH++; 3057 $::WANT_REFRESH++;
3096} 3058}
3097 3059
3098sub add { 3060sub add {
3099 my ($self, @children) = @_; 3061 my ($self, @children) = @_;
3100
3101 for (my @widgets = @children; my $w = pop @widgets; ) {
3102 push @widgets, $w->children;
3103 $w->{root} = $self;
3104 $w->{visible} = 1;
3105 }
3106 3062
3107 for my $child (@children) { 3063 for my $child (@children) {
3108 $child->{is_toplevel} = 1; 3064 $child->{is_toplevel} = 1;
3109 3065
3110 # integerise window positions 3066 # integerise window positions
3121 $self->SUPER::remove (@children); 3077 $self->SUPER::remove (@children);
3122 3078
3123 while (@children) { 3079 while (@children) {
3124 my $w = pop @children; 3080 my $w = pop @children;
3125 push @children, $w->children; 3081 push @children, $w->children;
3126 delete $w->{visible}; 3082 $w->set_invisible;
3127 } 3083 }
3128} 3084}
3129 3085
3130sub on_refresh { 3086sub on_refresh {
3131 my ($self, $id, $cb) = @_; 3087 my ($self, $id, $cb) = @_;
3145 while ($self->{refresh_hook}) { 3101 while ($self->{refresh_hook}) {
3146 $_->() 3102 $_->()
3147 for values %{delete $self->{refresh_hook}}; 3103 for values %{delete $self->{refresh_hook}};
3148 } 3104 }
3149 3105
3150 if ($self->{check_size}) { 3106 if ($self->{realloc}) {
3151 my @queue = ([], []); 3107 my @queue;
3152 3108
3153 for (;;) { 3109 while () {
3154 if ($self->{check_size}) { 3110 if ($self->{realloc}) {
3155 # heuristic: check containers last 3111 #TODO use array-of-depth approach
3156 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3112
3157 for values %{delete $self->{check_size}} 3113 @queue = sort { $a->{visible} <=> $b->{visible} }
3114 @queue, values %{delete $self->{realloc}};
3158 } 3115 }
3159 3116
3160 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3117 my $widget = pop @queue || last;
3161 3118
3119 $widget->{visible} or last; # do not resize invisible widgets
3120
3162 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3121 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3163 ? @$widget{qw(user_w user_h)} 3122 ? @$widget{qw(def_w def_h)}
3164 : $widget->size_request; 3123 : $widget->size_request;
3165 3124
3166 if (delete $widget->{force_alloc}
3167 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3168 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3125 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3169 3126
3170 $widget->{req_w} = $w; 3127 $widget->{req_w} = $w;
3171 $widget->{req_h} = $h; 3128 $widget->{req_h} = $h;
3172 3129
3173 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3130 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3174 3131
3175 $widget->{parent}->check_size 3132 push @queue, $widget->{parent}
3176 if $widget->{parent}; 3133 if $widget->{parent};
3177 }
3178 } 3134 }
3179 } 3135 }
3180 3136
3181 while ($self->{size_alloc}) { 3137 while (my $size_alloc = delete $self->{size_alloc}) {
3182 for (values %{delete $self->{size_alloc}}) { 3138 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3183 my ($widget, $w, $h) = @$_; 3139 values %$size_alloc;
3140
3141 while () {
3142 my ($widget, $w, $h) = @{ pop @queue or last };
3184 3143
3185 $w = 0 if $w < 0; 3144 $w = 0 if $w < 0;
3186 $h = 0 if $h < 0; 3145 $h = 0 if $h < 0;
3187 3146
3147 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3148
3188 $widget->{w} = $w; 3149 $widget->{w} = $w;
3189 $widget->{h} = $h; 3150 $widget->{h} = $h;
3190 $widget->size_allocate ($w, $h); 3151
3191 $widget->emit (size_allocate => $w, $h); 3152 $widget->emit (size_allocate => $w, $h, $changed);
3192 } 3153 }
3193 } 3154 }
3194 3155
3195 while ($self->{post_alloc_hook}) { 3156 while ($self->{post_alloc_hook}) {
3196 $_->() 3157 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines