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.254 by root, Mon May 29 01:53:23 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}) {
23 43
24 if ($TOOLTIP->{owner} != $widget) { 44 if ($TOOLTIP->{owner} != $widget) {
45 $TOOLTIP->hide;
46
25 $TOOLTIP->{owner} = $widget; 47 $TOOLTIP->{owner} = $widget;
26 48
27 my $tip = $widget->{tooltip}; 49 my $tip = $widget->{tooltip};
28 50
29 $tip = $tip->($widget) if CODE:: eq ref $tip; 51 $tip = $tip->($widget) if CODE:: eq ref $tip;
30 52
31 $TOOLTIP->set_tooltip_from ($widget); 53 $TOOLTIP->set_tooltip_from ($widget);
32 $TOOLTIP->show; 54 $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 } 55 }
43 56
44 return; 57 return;
45 } 58 }
46 } 59 }
50 delete $TOOLTIP->{owner}; 63 delete $TOOLTIP->{owner};
51} 64}
52 65
53# class methods for events 66# class methods for events
54sub feed_sdl_key_down_event { 67sub feed_sdl_key_down_event {
55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 68 $FOCUS->emit (key_down => $_[0])
56 if $FOCUS; 69 if $FOCUS;
57} 70}
58 71
59sub feed_sdl_key_up_event { 72sub feed_sdl_key_up_event {
60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 73 $FOCUS->emit (key_up => $_[0])
61 if $FOCUS; 74 if $FOCUS;
62} 75}
63 76
64sub feed_sdl_button_down_event { 77sub feed_sdl_button_down_event {
65 my ($ev) = @_; 78 my ($ev) = @_;
74 check_tooltip; 87 check_tooltip;
75 } 88 }
76 89
77 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 90 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
78 91
79 if ($GRAB) { 92 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
80 ($x, $y) = $GRAB->coord2local ($x, $y); 93 if $GRAB;
81 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
82 }
83} 94}
84 95
85sub feed_sdl_button_up_event { 96sub feed_sdl_button_up_event {
86 my ($ev) = @_; 97 my ($ev) = @_;
87 my ($x, $y) = ($ev->{x}, $ev->{y}); 98 my ($x, $y) = ($ev->{x}, $ev->{y});
88 99
89 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 100 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
90 101
91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 102 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
92 103
93 if ($GRAB) { 104 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
94 ($x, $y) = $GRAB->coord2local ($x, $y); 105 if $GRAB;
95 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
96 }
97 106
98 if (!$BUTTON_STATE) { 107 if (!$BUTTON_STATE) {
99 my $grab = $GRAB; undef $GRAB; 108 my $grab = $GRAB; undef $GRAB;
100 $grab->update if $grab; 109 $grab->update if $grab;
101 $GRAB->update if $GRAB; 110 $GRAB->update if $GRAB;
117 $HOVER->update if $HOVER && $HOVER->{can_hover}; 126 $HOVER->update if $HOVER && $HOVER->{can_hover};
118 127
119 check_tooltip; 128 check_tooltip;
120 } 129 }
121 130
122 if ($HOVER) {
123 ($x, $y) = $HOVER->coord2local ($x, $y);
124 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); 131 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
125 } 132 if $HOVER;
126} 133}
127 134
128# convert position array to integers 135# convert position array to integers
129sub harmonize { 136sub harmonize {
130 my ($vals) = @_; 137 my ($vals) = @_;
161 for my $widget (values %WIDGET) { 168 for my $widget (values %WIDGET) {
162 if ($widget->{is_toplevel}) { 169 if ($widget->{is_toplevel}) {
163 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 170 $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}; 171 $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}; 172 $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}; 173 $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}; 174 $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}; 175 $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 } 176 }
172 } 177 }
173 178
174 reconfigure_widgets; 179 reconfigure_widgets;
175} 180}
192 can_events => 1, 197 can_events => 1,
193 @_ 198 @_
194 }, $class; 199 }, $class;
195 200
196 for (keys %$self) { 201 for (keys %$self) {
197 if (/^connect_(.*)$/) { 202 if (/^on_(.*)$/) {
198 $self->connect ($1 => delete $self->{$_}); 203 $self->connect ($1 => delete $self->{$_});
199 } 204 }
200 } 205 }
201 206
202 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 207 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
203 208
209 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
210 $self->{req_x} = $layout->{x} * $::WIDTH;
211 $self->{req_y} = $layout->{y} * $::HEIGHT;
212 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
213 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
214 }
215
204 $self 216 $self
205} 217}
206 218
207sub destroy { 219sub destroy {
208 my ($self) = @_; 220 my ($self) = @_;
211 %$self = (); 223 %$self = ();
212} 224}
213 225
214sub show { 226sub show {
215 my ($self) = @_; 227 my ($self) = @_;
228
216 return if $self->{parent}; 229 return if $self->{parent};
217 230
218 $CFClient::UI::ROOT->add ($self); 231 $CFClient::UI::ROOT->add ($self);
219} 232}
220 233
221sub show_centered { 234sub center {
222 my ($self) = @_; 235 my ($self) = @_;
223 return if $self->{parent};
224
225 $self->show;
226 236
227 $CFClient::UI::ROOT->on_post_alloc ( 237 $CFClient::UI::ROOT->on_post_alloc (
228 "centered $self" => sub { 238 "center_$self" => sub {
229 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 239 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
230 }, 240 },
231 ); 241 );
232}
233 242
234sub hide { 243 $self->update;
244}
245
246sub set_visible {
235 my ($self) = @_; 247 my ($self) = @_;
248
249 return if $self->{visible};
250
251 $self->{root} = $self->{parent}{root};
252 $self->{visible} = $self->{parent}{visible} + 1;
253
254 $self->emit (visibility_change => 1);
255
256 $self->realloc if !exists $self->{req_w};
257
258 $_->set_visible for $self->children;
259}
260
261sub set_invisible {
262 my ($self) = @_;
263
264 return unless $self->{visible};
265
266 $_->set_invisible for $self->children;
267
268 delete $self->{root};
269 delete $self->{visible};
236 270
237 undef $GRAB if $GRAB == $self; 271 undef $GRAB if $GRAB == $self;
238 undef $HOVER if $HOVER == $self; 272 undef $HOVER if $HOVER == $self;
273
274 CFClient::UI::check_tooltip
275 if $TOOLTIP->{owner} == $self;
276
277 $self->focus_out;
278
279 $self->emit (visibility_change => 0);
280}
281
282sub set_visibility {
283 my ($self, $visible) = @_;
284
285 return if $self->{visible} == $visible;
286
287 $visible ? $self->hide
288 : $self->show;
289}
290
291sub toggle_visibility {
292 my ($self) = @_;
293
294 $self->{visible}
295 ? $self->hide
296 : $self->show;
297}
298
299sub hide {
300 my ($self) = @_;
301
302 $self->set_invisible;
239 303
240 $self->{parent}->remove ($self) 304 $self->{parent}->remove ($self)
241 if $self->{parent}; 305 if $self->{parent};
242} 306}
243 307
252} 316}
253 317
254sub set_size { 318sub set_size {
255 my ($self, $w, $h) = @_; 319 my ($self, $w, $h) = @_;
256 320
257 $self->{user_w} = $w; 321 $self->{def_w} = $w;
258 $self->{user_h} = $h; 322 $self->{def_h} = $h;
259 323
260 $self->check_size; 324 $self->realloc;
261} 325}
262 326
263sub size_request { 327sub size_request {
264 require Carp; 328 require Carp;
265 Carp::confess "size_request is abstract"; 329 Carp::confess "size_request is abstract";
285 $self->{y} = $y; 349 $self->{y} = $y;
286 $self->update; 350 $self->update;
287 } 351 }
288 352
289 if ($self->{w} != $w || $self->{h} != $h) { 353 if ($self->{w} != $w || $self->{h} != $h) {
354 return unless $self->{visible};
355
290 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 356 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
291 } 357 }
292} 358}
293 359
294sub size_allocate { 360sub size_allocate {
295 # nothing to be done 361 # nothing to be done
296} 362}
297 363
298sub reconfigure {
299 my ($self) = @_;
300
301 $self->check_size (1);
302 $self->update;
303}
304
305sub children { 364sub children {
306} 365}
307 366
308sub set_max_size { 367sub set_max_size {
309 my ($self, $w, $h) = @_; 368 my ($self, $w, $h) = @_;
312 delete $self->{max_h}; $self->{max_h} = $h if $h; 371 delete $self->{max_h}; $self->{max_h} = $h if $h;
313} 372}
314 373
315sub set_tooltip { 374sub set_tooltip {
316 my ($self, $tooltip) = @_; 375 my ($self, $tooltip) = @_;
376
377 $tooltip =~ s/^\s+//;
378 $tooltip =~ s/\s+$//;
379
380 return if $self->{tooltip} eq $tooltip;
317 381
318 $self->{tooltip} = $tooltip; 382 $self->{tooltip} = $tooltip;
319 383
320 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 384 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
321 delete $CFClient::UI::TOOLTIP->{owner}; 385 delete $CFClient::UI::TOOLTIP->{owner};
343 return if $FOCUS == $self; 407 return if $FOCUS == $self;
344 return unless $self->{can_focus}; 408 return unless $self->{can_focus};
345 409
346 my $focus = $FOCUS; $FOCUS = $self; 410 my $focus = $FOCUS; $FOCUS = $self;
347 411
348 $self->emit (focus_in => $focus); 412 $self->_emit (focus_in => $focus);
349 413
350 $focus->update if $focus; 414 $focus->update if $focus;
351 $FOCUS->update; 415 $FOCUS->update;
352} 416}
353 417
356 420
357 return unless $FOCUS == $self; 421 return unless $FOCUS == $self;
358 422
359 my $focus = $FOCUS; undef $FOCUS; 423 my $focus = $FOCUS; undef $FOCUS;
360 424
361 $self->emit (focus_out => $focus); 425 $self->_emit (focus_out => $focus);
362 426
363 $focus->update if $focus; #? 427 $focus->update if $focus; #?
428
429 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
430 unless $FOCUS;
364} 431}
365 432
366sub mouse_motion { } 433sub mouse_motion { }
367sub button_up { } 434sub button_up { }
368sub key_down { } 435sub key_down { }
369sub key_up { } 436sub key_up { }
370 437
371sub button_down { 438sub button_down {
372 my ($self, $ev, $x, $y) = @_; 439 my ($self, $ev, $x, $y) = @_;
373 440
374 $self->focus_in; 441 $self->focus_in;
377sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 444sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
378sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 445sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
379sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 446sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
380sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 447sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
381sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 448sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
449
450sub find_widget {
451 my ($self, $x, $y) = @_;
452
453 return () unless $self->{can_events};
454
455 return $self
456 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
457 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
458
459 ()
460}
461
462sub set_parent {
463 my ($self, $parent) = @_;
464
465 Scalar::Util::weaken ($self->{parent} = $parent);
466
467 $self->set_visible if $parent->{visible};
468}
469
470sub connect {
471 my ($self, $signal, $cb) = @_;
472
473 push @{ $self->{signal_cb}{$signal} }, $cb;
474}
475
476sub _emit {
477 my ($self, $signal, @args) = @_;
478
479 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
480}
481
482sub emit {
483 my ($self, $signal, @args) = @_;
484
485 $self->_emit ($signal, @args)
486 || $self->$signal (@args);
487}
488
489sub visibility_change {
490 #my ($self, $visible) = @_;
491}
492
493sub realloc {
494 my ($self) = @_;
495
496 if ($self->{visible}) {
497 return if $self->{root}{realloc}{$self};
498
499 $self->{root}{realloc}{$self} = $self;
500 $self->{root}->update;
501 } else {
502 delete $self->{req_w};
503 }
504}
505
506sub update {
507 my ($self) = @_;
508
509 $self->{parent}->update
510 if $self->{parent};
511}
382 512
383sub draw { 513sub draw {
384 my ($self) = @_; 514 my ($self) = @_;
385 515
386 return unless $self->{h} && $self->{w}; 516 return unless $self->{h} && $self->{w};
408 if ($ENV{PCLIENT_DEBUG}) { 538 if ($ENV{PCLIENT_DEBUG}) {
409 glPushMatrix; 539 glPushMatrix;
410 glColor 1, 1, 0, 1; 540 glColor 1, 1, 0, 1;
411 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 541 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
412 glBegin GL_LINE_LOOP; 542 glBegin GL_LINE_LOOP;
413 glVertex 0 , 0; 543 glVertex 0 , 0;
414 glVertex $self->{w}, 0; 544 glVertex $self->{w} - 1, 0;
415 glVertex $self->{w}, $self->{h}; 545 glVertex $self->{w} - 1, $self->{h} - 1;
416 glVertex 0 , $self->{h}; 546 glVertex 0 , $self->{h} - 1;
417 glEnd; 547 glEnd;
418 glPopMatrix; 548 glPopMatrix;
419 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 549 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
420 } 550 }
421} 551}
422 552
423sub _draw { 553sub _draw {
424 my ($self) = @_; 554 my ($self) = @_;
425 555
426 warn "no draw defined for $self\n"; 556 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} 557}
478 558
479sub DESTROY { 559sub DESTROY {
480 my ($self) = @_; 560 my ($self) = @_;
481 561
578 $self->{children} = [ 658 $self->{children} = [
579 sort { $a->{z} <=> $b->{z} } 659 sort { $a->{z} <=> $b->{z} }
580 @{$self->{children}}, @widgets 660 @{$self->{children}}, @widgets
581 ]; 661 ];
582 662
583 $self->check_size (1); 663 $self->realloc;
584 $self->update;
585} 664}
586 665
587sub children { 666sub children {
588 @{ $_[0]{children} } 667 @{ $_[0]{children} }
589} 668}
594 delete $child->{parent}; 673 delete $child->{parent};
595 $child->hide; 674 $child->hide;
596 675
597 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 676 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
598 677
599 $self->check_size; 678 $self->realloc;
600 $self->update;
601} 679}
602 680
603sub clear { 681sub clear {
604 my ($self) = @_; 682 my ($self) = @_;
605 683
609 for (@$children) { 687 for (@$children) {
610 delete $_->{parent}; 688 delete $_->{parent};
611 $_->hide; 689 $_->hide;
612 } 690 }
613 691
614 $self->check_size; 692 $self->realloc;
615 $self->update;
616} 693}
617 694
618sub find_widget { 695sub find_widget {
619 my ($self, $x, $y) = @_; 696 my ($self, $x, $y) = @_;
620 697
673sub size_request { 750sub size_request {
674 $_[0]{children}[0]->size_request 751 $_[0]{children}[0]->size_request
675} 752}
676 753
677sub size_allocate { 754sub size_allocate {
678 my ($self, $w, $h) = @_; 755 my ($self, $w, $h, $changed) = @_;
679 756
680 $self->{children}[0]->configure (0, 0, $w, $h); 757 $self->{children}[0]->configure (0, 0, $w, $h);
681} 758}
682 759
683############################################################################# 760#############################################################################
700 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 777 $ROOT->on_post_alloc ($self => sub { $self->render_child });
701 $self->SUPER::update; 778 $self->SUPER::update;
702} 779}
703 780
704sub size_allocate { 781sub size_allocate {
705 my ($self, $w, $h) = @_; 782 my ($self, $w, $h, $changed) = @_;
706 783
707 $self->SUPER::size_allocate ($w, $h); 784 $self->SUPER::size_allocate ($w, $h, $changed);
708 $self->update; 785 $self->update
786 if $changed;
709} 787}
710 788
711sub _render { 789sub _render {
712 $_[0]{children}[0]->draw; 790 $_[0]{children}[0]->draw;
713} 791}
744 822
745package CFClient::UI::ViewPort; 823package CFClient::UI::ViewPort;
746 824
747our @ISA = CFClient::UI::Window::; 825our @ISA = CFClient::UI::Window::;
748 826
827sub new {
828 my $class = shift;
829
830 $class->SUPER::new (
831 scroll_x => 0,
832 scroll_y => 1,
833 @_,
834 )
835}
836
749sub size_request { 837sub size_request {
750 my ($self) = @_; 838 my ($self) = @_;
751 839
752 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 840 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
753 841
754 @$self{qw(child_w child_h)} 842 $w = 10 if $self->{scroll_x};
843 $h = 10 if $self->{scroll_y};
844
845 ($w, $h)
755} 846}
756 847
757sub size_allocate { 848sub size_allocate {
758 my ($self, $w, $h) = @_; 849 my ($self, $w, $h, $changed) = @_;
759 850
760 my ($cw, $ch) = @$self{qw(child_w child_h)}; 851 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
761# $w = $self->{w}; 852 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
853
762 $self->child->configure (0, 0, $cw, $ch); 854 $self->child->configure (0, 0, $w, $h);
763 $self->update; 855 $self->update;
764} 856}
765 857
766sub set_offset { 858sub set_offset {
767 my ($self, $x, $y) = @_; 859 my ($self, $x, $y) = @_;
818 my $class = shift; 910 my $class = shift;
819 911
820 my $self; 912 my $self;
821 913
822 my $slider = new CFClient::UI::Slider 914 my $slider = new CFClient::UI::Slider
823 vertical => 1, 915 vertical => 1,
824 range => [0, 0, 1, 0.01], # HACK fix 916 range => [0, 0, 1, 0.01], # HACK fix
825 connect_changed => sub { 917 on_changed => sub {
826 $self->{vp}->set_offset (0, $_[1]); 918 $self->{vp}->set_offset (0, $_[1]);
827 }, 919 },
828 ; 920 ;
829 921
830 $self = $class->SUPER::new ( 922 $self = $class->SUPER::new (
838 $self->add ($self->{slider}); 930 $self->add ($self->{slider});
839 931
840 $self 932 $self
841} 933}
842 934
935sub update {
936 my ($self) = @_;
937
938 $self->SUPER::update;
939
940 # todo: overwrite size_allocate of child
941 my $child = $self->{vp}->child;
942 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
943}
944
843sub size_allocate { 945sub size_allocate {
844 my ($self, $w, $h) = @_; 946 my ($self, $w, $h, $changed) = @_;
845 947
846 $self->SUPER::size_allocate ($w, $h); 948 $self->SUPER::size_allocate ($w, $h, $changed);
847 949
848 my $child = $self->{vp}->child; 950 my $child = $self->{vp}->child;
849 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 951 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
850} 952}
851 953
904 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1006 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); 1007 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
906 1008
907sub new { 1009sub new {
908 my $class = shift; 1010 my $class = shift;
909
910 # TODO: user_x, user_y, overwrite moveto?
911 1011
912 my $self = $class->SUPER::new ( 1012 my $self = $class->SUPER::new (
913 bg => [1, 1, 1, 1], 1013 bg => [1, 1, 1, 1],
914 border_bg => [1, 1, 1, 1], 1014 border_bg => [1, 1, 1, 1],
915 border => 0.6, 1015 border => 0.6,
941 $h + $self->border * 2, 1041 $h + $self->border * 2,
942 ) 1042 )
943} 1043}
944 1044
945sub size_allocate { 1045sub size_allocate {
946 my ($self, $w, $h) = @_; 1046 my ($self, $w, $h, $changed) = @_;
1047
1048 return unless $changed;
947 1049
948 $h -= List::Util::max 0, $self->border * 2; 1050 $h -= List::Util::max 0, $self->border * 2;
949 $w -= List::Util::max 0, $self->border * 2; 1051 $w -= List::Util::max 0, $self->border * 2;
950 1052
951 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1053 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
975 my ($ev, $x, $y) = @_; 1077 my ($ev, $x, $y) = @_;
976 1078
977 my $dx = $ev->{x} - $ox; 1079 my $dx = $ev->{x} - $ox;
978 my $dy = $ev->{y} - $oy; 1080 my $dy = $ev->{y} - $oy;
979 1081
1082 $self->{user_x} = $wx + $dx * $mx;
1083 $self->{user_y} = $wy + $dy * $my;
980 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1084 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
981 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1085 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
982 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1086 $self->move ($self->{user_x}, $self->{user_y});
983 $self->check_size; 1087 $self->realloc;
984 }; 1088 };
985 1089
986 } elsif ($lr ^ $td) { 1090 } elsif ($lr ^ $td) {
987 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1091 my ($ox, $oy) = ($ev->{x}, $ev->{y});
988 my ($bx, $by) = ($self->{x}, $self->{y}); 1092 my ($bx, $by) = ($self->{x}, $self->{y});
990 $self->{motion} = sub { 1094 $self->{motion} = sub {
991 my ($ev, $x, $y) = @_; 1095 my ($ev, $x, $y) = @_;
992 1096
993 ($x, $y) = ($ev->{x}, $ev->{y}); 1097 ($x, $y) = ($ev->{x}, $ev->{y});
994 1098
995 $self->move ($bx + $x - $ox, $by + $y - $oy); 1099 $self->{user_x} = $bx + $x - $ox;
1100 $self->{user_y} = $by + $y - $oy;
1101
1102 $self->move ($self->{user_x}, $self->{user_y});
996 $self->update; 1103 $self->realloc;
997 }; 1104 };
998 } 1105 }
999} 1106}
1000 1107
1001sub button_up { 1108sub button_up {
1062sub new { 1169sub new {
1063 my $class = shift; 1170 my $class = shift;
1064 1171
1065 $class->SUPER::new ( 1172 $class->SUPER::new (
1066 col_expand => [], 1173 col_expand => [],
1067 @_ 1174 @_,
1068 ) 1175 )
1176}
1177
1178sub children {
1179 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1069} 1180}
1070 1181
1071sub add { 1182sub add {
1072 my ($self, $x, $y, $child) = @_; 1183 my ($self, $x, $y, $child) = @_;
1073 1184
1074 $child->set_parent ($self); 1185 $child->set_parent ($self);
1075 $self->{children}[$y][$x] = $child; 1186 $self->{children}[$y][$x] = $child;
1076 1187
1077 $child->check_size; 1188 $self->realloc;
1078} 1189}
1079 1190
1080sub children {
1081 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1082}
1083
1084# TODO: move to container class maybe? send childs a signal on removal? 1191# TODO: move to container class maybe? send children a signal on removal?
1085sub clear { 1192sub clear {
1086 my ($self) = @_; 1193 my ($self) = @_;
1087 1194
1088 my @children = $self->children; 1195 my @children = $self->children;
1089 delete $self->{children}; 1196 delete $self->{children};
1091 for (@children) { 1198 for (@children) {
1092 delete $_->{parent}; 1199 delete $_->{parent};
1093 $_->hide; 1200 $_->hide;
1094 } 1201 }
1095 1202
1096 $self->update; 1203 $self->realloc;
1097} 1204}
1098 1205
1099sub get_wh { 1206sub get_wh {
1100 my ($self) = @_; 1207 my ($self) = @_;
1101 1208
1128 (sum @$hs), 1235 (sum @$hs),
1129 ) 1236 )
1130} 1237}
1131 1238
1132sub size_allocate { 1239sub size_allocate {
1133 my ($self, $w, $h) = @_; 1240 my ($self, $w, $h, $changed) = @_;
1134 1241
1135 my ($ws, $hs) = $self->get_wh; 1242 my ($ws, $hs) = $self->get_wh;
1136 1243
1137 my $req_w = sum @$ws; 1244 my $req_w = (sum @$ws) || 1;
1138 my $req_h = sum @$hs; 1245 my $req_h = (sum @$hs) || 1;
1139 1246
1140 # TODO: nicer code && do row_expand 1247 # TODO: nicer code && do row_expand
1141 my @col_expand = @{$self->{col_expand}}; 1248 my @col_expand = @{$self->{col_expand}};
1142 @col_expand = (1) x @$ws unless @col_expand; 1249 @col_expand = (1) x @$ws unless @col_expand;
1143 my $col_expand = (sum @col_expand) || 1; 1250 my $col_expand = (sum @col_expand) || 1;
1197 } 1304 }
1198} 1305}
1199 1306
1200############################################################################# 1307#############################################################################
1201 1308
1309package CFClient::UI::Box;
1310
1311our @ISA = CFClient::UI::Container::;
1312
1313sub size_request {
1314 my ($self) = @_;
1315
1316 $self->{vertical}
1317 ? (
1318 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1319 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1320 )
1321 : (
1322 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1323 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1324 )
1325}
1326
1327sub size_allocate {
1328 my ($self, $w, $h, $changed) = @_;
1329
1330 my $space = $self->{vertical} ? $h : $w;
1331 my $children = $self->{children};
1332
1333 my @req;
1334
1335 if ($self->{homogeneous}) {
1336 @req = ($space / (@$children || 1)) x @$children;
1337 } else {
1338 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1339 my $req = List::Util::sum @req;
1340
1341 if ($req > $space) {
1342 # ah well, not enough space
1343 $_ *= $space / $req for @req;
1344 } else {
1345 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1346
1347 $space = ($space - $req) / $expand; # remaining space to give away
1348
1349 $req[$_] += $space * $children->[$_]{expand}
1350 for 0 .. $#$children;
1351 }
1352 }
1353
1354 CFClient::UI::harmonize \@req;
1355
1356 my $pos = 0;
1357 for (0 .. $#$children) {
1358 my $alloc = $req[$_];
1359 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1360
1361 $pos += $alloc;
1362 }
1363
1364 1
1365}
1366
1367#############################################################################
1368
1202package CFClient::UI::HBox; 1369package CFClient::UI::HBox;
1203 1370
1204# TODO: wrap into common Box base class
1205
1206our @ISA = CFClient::UI::Container::; 1371our @ISA = CFClient::UI::Box::;
1207 1372
1208sub size_request { 1373sub new {
1209 my ($self) = @_; 1374 my $class = shift;
1210 1375
1211 my @alloc = map [$_->size_request], @{$self->{children}}; 1376 $class->SUPER::new (
1212 1377 vertical => 0,
1213 ( 1378 @_,
1214 (List::Util::sum map $_->[0], @alloc),
1215 (List::Util::max map $_->[1], @alloc),
1216 ) 1379 )
1217} 1380}
1218 1381
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############################################################################# 1382#############################################################################
1261 1383
1262package CFClient::UI::VBox; 1384package CFClient::UI::VBox;
1263 1385
1264# TODO: wrap into common Box base class
1265
1266our @ISA = CFClient::UI::Container::; 1386our @ISA = CFClient::UI::Box::;
1267 1387
1268sub size_request { 1388sub new {
1269 my ($self) = @_; 1389 my $class = shift;
1270 1390
1271 my @alloc = map [$_->size_request], @{$self->{children}}; 1391 $class->SUPER::new (
1272 1392 vertical => 1,
1273 ( 1393 @_,
1274 (List::Util::max map $_->[0], @alloc),
1275 (List::Util::sum map $_->[1], @alloc),
1276 ) 1394 )
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} 1395}
1317 1396
1318############################################################################# 1397#############################################################################
1319 1398
1320package CFClient::UI::Label; 1399package CFClient::UI::Label;
1383 $self->{text} = "T$text"; 1462 $self->{text} = "T$text";
1384 1463
1385 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1464 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1386 $self->{layout}->set_text ($text); 1465 $self->{layout}->set_text ($text);
1387 1466
1467 $self->realloc;
1388 $self->update; 1468 $self->update;
1389 $self->check_size;
1390} 1469}
1391 1470
1392sub set_markup { 1471sub set_markup {
1393 my ($self, $markup) = @_; 1472 my ($self, $markup) = @_;
1394 1473
1398 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1477 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1399 1478
1400 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1479 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1401 $self->{layout}->set_markup ($markup); 1480 $self->{layout}->set_markup ($markup);
1402 1481
1482 $self->realloc;
1403 $self->update; 1483 $self->update;
1404 $self->check_size;
1405} 1484}
1406 1485
1407sub size_request { 1486sub size_request {
1408 my ($self) = @_; 1487 my ($self) = @_;
1409 1488
1430 $h + $self->{padding} * 2, 1509 $h + $self->{padding} * 2,
1431 ) 1510 )
1432} 1511}
1433 1512
1434sub size_allocate { 1513sub size_allocate {
1435 my ($self, $w, $h) = @_; 1514 my ($self, $w, $h, $changed) = @_;
1436 1515
1437 delete $self->{texture}; 1516 delete $self->{texture}
1517 if $changed;
1438} 1518}
1439 1519
1440sub set_fontsize { 1520sub set_fontsize {
1441 my ($self, $fontsize) = @_; 1521 my ($self, $fontsize) = @_;
1442 1522
1443 $self->{fontsize} = $fontsize; 1523 $self->{fontsize} = $fontsize;
1444 delete $self->{texture}; 1524 delete $self->{texture};
1445 1525
1446 $self->update; 1526 $self->realloc;
1447 $self->check_size;
1448} 1527}
1449 1528
1450sub _draw { 1529sub _draw {
1451 my ($self) = @_; 1530 my ($self) = @_;
1452 1531
1524 $self->{text} = $text; 1603 $self->{text} = $text;
1525 1604
1526 $text =~ s/./*/g if $self->{hidden}; 1605 $text =~ s/./*/g if $self->{hidden};
1527 $self->{layout}->set_text ("$text "); 1606 $self->{layout}->set_text ("$text ");
1528 1607
1529 $self->emit (changed => $self->{text}); 1608 $self->_emit (changed => $self->{text});
1530} 1609}
1531 1610
1532sub set_text { 1611sub set_text {
1533 my ($self, $text) = @_; 1612 my ($self, $text) = @_;
1534 1613
1535 $self->{cursor} = length $text; 1614 $self->{cursor} = length $text;
1536 $self->_set_text ($text); 1615 $self->_set_text ($text);
1537 $self->update; 1616
1538 $self->check_size; 1617 $self->realloc;
1539} 1618}
1540 1619
1541sub get_text { 1620sub get_text {
1542 $_[0]{text} 1621 $_[0]{text}
1543} 1622}
1546 my ($self) = @_; 1625 my ($self) = @_;
1547 1626
1548 my ($w, $h) = $self->SUPER::size_request; 1627 my ($w, $h) = $self->SUPER::size_request;
1549 1628
1550 ($w + 1, $h) # add 1 for cursor 1629 ($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} 1630}
1558 1631
1559sub key_down { 1632sub key_down {
1560 my ($self, $ev) = @_; 1633 my ($self, $ev) = @_;
1561 1634
1576 } elsif ($sym == CFClient::SDLK_HOME) { 1649 } elsif ($sym == CFClient::SDLK_HOME) {
1577 $self->{cursor} = 0; 1650 $self->{cursor} = 0;
1578 } elsif ($sym == CFClient::SDLK_END) { 1651 } elsif ($sym == CFClient::SDLK_END) {
1579 $self->{cursor} = length $text; 1652 $self->{cursor} = length $text;
1580 } elsif ($uni == 27) { 1653 } elsif ($uni == 27) {
1581 $self->emit ('escape'); 1654 $self->_emit ('escape');
1582 } elsif ($uni) { 1655 } elsif ($uni) {
1583 substr $text, $self->{cursor}++, 0, chr $uni; 1656 substr $text, $self->{cursor}++, 0, chr $uni;
1584 } 1657 }
1585 1658
1586 $self->_set_text ($text); 1659 $self->_set_text ($text);
1587 $self->update; 1660
1588 $self->check_size; 1661 $self->realloc;
1589} 1662}
1590 1663
1591sub focus_in { 1664sub focus_in {
1592 my ($self) = @_; 1665 my ($self) = @_;
1593 1666
1673 if ($sym == 13) { 1746 if ($sym == 13) {
1674 unshift @{$self->{history}}, 1747 unshift @{$self->{history}},
1675 my $txt = $self->get_text; 1748 my $txt = $self->get_text;
1676 $self->{history_pointer} = -1; 1749 $self->{history_pointer} = -1;
1677 $self->{history_saveback} = ''; 1750 $self->{history_saveback} = '';
1678 $self->emit (activate => $txt); 1751 $self->_emit (activate => $txt);
1679 $self->update; 1752 $self->update;
1680 1753
1681 } elsif ($sym == CFClient::SDLK_UP) { 1754 } elsif ($sym == CFClient::SDLK_UP) {
1682 if ($self->{history_pointer} < 0) { 1755 if ($self->{history_pointer} < 0) {
1683 $self->{history_saveback} = $self->get_text; 1756 $self->{history_saveback} = $self->get_text;
1731 can_events => 1, 1804 can_events => 1,
1732 @_ 1805 @_
1733 ) 1806 )
1734} 1807}
1735 1808
1809sub activate { }
1810
1736sub button_up { 1811sub button_up {
1737 my ($self, $ev, $x, $y) = @_; 1812 my ($self, $ev, $x, $y) = @_;
1738 1813
1814 $self->emit ("activate")
1739 if ($x >= 0 && $x < $self->{w} 1815 if $x >= 0 && $x < $self->{w}
1740 && $y >= 0 && $y < $self->{h}) { 1816 && $y >= 0 && $y < $self->{h};
1741 $self->emit ("activate");
1742 }
1743} 1817}
1744 1818
1745sub _draw { 1819sub _draw {
1746 my ($self) = @_; 1820 my ($self) = @_;
1747 1821
1799 my ($self, $ev, $x, $y) = @_; 1873 my ($self, $ev, $x, $y) = @_;
1800 1874
1801 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1875 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1802 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1876 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1803 $self->{state} = !$self->{state}; 1877 $self->{state} = !$self->{state};
1804 $self->emit (changed => $self->{state}); 1878 $self->_emit (changed => $self->{state});
1805 } 1879 }
1806} 1880}
1807 1881
1808sub _draw { 1882sub _draw {
1809 my ($self) = @_; 1883 my ($self) = @_;
2099 $self->update; 2173 $self->update;
2100 2174
2101 $self 2175 $self
2102} 2176}
2103 2177
2178sub changed { }
2179
2104sub set_range { 2180sub set_range {
2105 my ($self, $range) = @_; 2181 my ($self, $range) = @_;
2106 2182
2107 $self->{range} = $range; 2183 ($range, $self->{range}) = ($self->{range}, $range);
2108 2184
2109 $self->update; 2185 $self->update
2186 if "@$range" ne "@{$self->{range}}";
2110} 2187}
2111 2188
2112sub set_value { 2189sub set_value {
2113 my ($self, $value) = @_; 2190 my ($self, $value) = @_;
2114 2191
2125 if $unit; 2202 if $unit;
2126 2203
2127 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2204 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2128 2205
2129 if ($value != $old_value) { 2206 if ($value != $old_value) {
2130 $self->emit (changed => $value); 2207 $self->_emit (changed => $value);
2131 $self->update; 2208 $self->update;
2132 } 2209 }
2133} 2210}
2134 2211
2135sub size_request { 2212sub size_request {
2296 $self->{fontsize} = $fontsize; 2373 $self->{fontsize} = $fontsize;
2297 $self->reflow; 2374 $self->reflow;
2298} 2375}
2299 2376
2300sub size_allocate { 2377sub size_allocate {
2301 my ($self, $w, $h) = @_; 2378 my ($self, $w, $h, $changed) = @_;
2302 2379
2303 $self->SUPER::size_allocate ($w, $h); 2380 $self->SUPER::size_allocate ($w, $h, $changed);
2381
2382 return unless $changed;
2304 2383
2305 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2384 $self->{layout}->set_font ($self->{font}) if $self->{font};
2306 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2385 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2307 $self->{layout}->set_width ($self->{children}[0]{w}); 2386 $self->{layout}->set_width ($self->{children}[0]{w});
2308 2387
2497 2576
2498sub new { 2577sub new {
2499 my $class = shift; 2578 my $class = shift;
2500 2579
2501 my $self = $class->SUPER::new ( 2580 my $self = $class->SUPER::new (
2502 state => 0, 2581 state => 0,
2503 connect_activate => \&toggle_flopper, 2582 on_activate => \&toggle_flopper,
2504 @_ 2583 @_
2505 ); 2584 );
2506 2585
2507 if ($self->{state}) {
2508 $self->{state} = 0;
2509 $self->toggle_flopper;
2510 }
2511
2512 $self 2586 $self
2513} 2587}
2514 2588
2515sub toggle_flopper { 2589sub toggle_flopper {
2516 my ($self) = @_; 2590 my ($self) = @_;
2517 2591
2518 # TODO: use animation 2592 $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} 2593}
2530 2594
2531############################################################################# 2595#############################################################################
2532 2596
2533package CFClient::UI::Tooltip; 2597package CFClient::UI::Tooltip;
2565 2629
2566 ($w + 4, $h + 4) 2630 ($w + 4, $h + 4)
2567} 2631}
2568 2632
2569sub size_allocate { 2633sub size_allocate {
2570 my ($self, $w, $h) = @_; 2634 my ($self, $w, $h, $changed) = @_;
2571 2635
2636 return unless $changed;
2637
2572 $self->SUPER::size_allocate ($w - 4, $h - 4); 2638 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2639}
2640
2641sub visibility_change {
2642 my ($self, $visible) = @_;
2643
2644 return unless $visible;
2645
2646 $self->{root}->on_post_alloc ("move_$self" => sub {
2647 my $widget = $self->{owner}
2648 or return;
2649
2650 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2651
2652 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2653 if $x + $self->{w} > $::WIDTH;
2654
2655 $self->move ($x, $y);
2656 });
2573} 2657}
2574 2658
2575sub _draw { 2659sub _draw {
2576 my ($self) = @_; 2660 my ($self) = @_;
2577 2661
2594 glVertex $w, $h; 2678 glVertex $w, $h;
2595 glVertex $w, 0; 2679 glVertex $w, 0;
2596 glEnd; 2680 glEnd;
2597 2681
2598 glTranslate 2 - 0.375, 2 - 0.375; 2682 glTranslate 2 - 0.375, 2 - 0.375;
2683
2599 $self->SUPER::_draw; 2684 $self->SUPER::_draw;
2600} 2685}
2601 2686
2602############################################################################# 2687#############################################################################
2603 2688
2609 2694
2610sub new { 2695sub new {
2611 my $class = shift; 2696 my $class = shift;
2612 2697
2613 my $self = $class->SUPER::new ( 2698 my $self = $class->SUPER::new (
2614 aspect => 1, 2699 aspect => 1,
2700 can_events => 0,
2615 @_, 2701 @_,
2616 ); 2702 );
2617 2703
2618 if ($self->{anim} && $self->{animspeed}) { 2704 if ($self->{anim} && $self->{animspeed}) {
2619 Scalar::Util::weaken (my $widget = $self); 2705 Scalar::Util::weaken (my $widget = $self);
2678 $self->SUPER::DESTROY; 2764 $self->SUPER::DESTROY;
2679} 2765}
2680 2766
2681############################################################################# 2767#############################################################################
2682 2768
2683package CFClient::UI::InventoryItem; 2769package CFClient::UI::Inventory;
2684 2770
2685our @ISA = CFClient::UI::HBox::; 2771our @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 2772
2713sub new { 2773sub new {
2714 my $class = shift; 2774 my $class = shift;
2715 2775
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 ( 2776 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), 2777 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2804 @_, 2778 @_,
2805 ); 2779 );
2806 2780
2807 $self 2781 $self
2808} 2782}
2818 or ($a->{name} cmp $b->{name}) 2792 or ($a->{name} cmp $b->{name})
2819 } @$items; 2793 } @$items;
2820 2794
2821 $self->{real_items} = \@items; 2795 $self->{real_items} = \@items;
2822 2796
2797 my $row = 0;
2823 for my $item (@items) { 2798 for my $item (@items) {
2824 $item->{item} = $item; 2799 CFClient::Item::update_widgets $item;
2825 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2826 $item->update_item ();
2827 }
2828 2800
2829 my $i = 0; 2801 $self->{scrolled}->add (0, $row, $item->{face_widget});
2830 for (@items) { 2802 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2831 $self->{scrolled}->add (0, $i, $_); 2803 $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 2804
2836# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2805 $row++;
2837} 2806 }
2838
2839sub size_request {
2840 my ($self) = @_;
2841 ($self->{req_w}, $self->{req_h});
2842} 2807}
2843 2808
2844############################################################################# 2809#############################################################################
2845 2810
2846package CFClient::UI::Menu; 2811package CFClient::UI::Menu;
2881 2846
2882# popup given the event (must be a mouse button down event currently) 2847# popup given the event (must be a mouse button down event currently)
2883sub popup { 2848sub popup {
2884 my ($self, $ev) = @_; 2849 my ($self, $ev) = @_;
2885 2850
2886 $self->emit ("popdown"); 2851 $self->_emit ("popdown");
2887 2852
2888 # maybe save $GRAB? must be careful about events... 2853 # maybe save $GRAB? must be careful about events...
2889 $GRAB = $self; 2854 $GRAB = $self;
2890 $self->{button} = $ev->{button}; 2855 $self->{button} = $ev->{button};
2891 2856
2906 2871
2907 if ($ev->{button} == $self->{button}) { 2872 if ($ev->{button} == $self->{button}) {
2908 undef $GRAB; 2873 undef $GRAB;
2909 $self->hide; 2874 $self->hide;
2910 2875
2911 $self->emit ("popdown"); 2876 $self->_emit ("popdown");
2912 $self->{hover}[1]->() if $self->{hover}; 2877 $self->{hover}[1]->() if $self->{hover};
2913 } 2878 }
2914} 2879}
2915 2880
2916############################################################################# 2881#############################################################################
2978sub add { 2943sub add {
2979 my ($self, $text, %arg) = @_; 2944 my ($self, $text, %arg) = @_;
2980 2945
2981 $text =~ s/^\s+//; 2946 $text =~ s/^\s+//;
2982 $text =~ s/\s+$//; 2947 $text =~ s/\s+$//;
2948
2949 return unless $text;
2983 2950
2984 my $timeout = time + ((delete $arg{timeout}) || 60); 2951 my $timeout = time + ((delete $arg{timeout}) || 60);
2985 2952
2986 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2953 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2987 2954
3030use CFClient::OpenGL; 2997use CFClient::OpenGL;
3031 2998
3032sub new { 2999sub new {
3033 my $class = shift; 3000 my $class = shift;
3034 3001
3035 $class->SUPER::new ( 3002 my $self = $class->SUPER::new (
3003 visible => 1,
3036 @_, 3004 @_,
3037 ) 3005 );
3006
3007 Scalar::Util::weaken ($self->{root} = $self);
3008
3009 $self
3038} 3010}
3039 3011
3040sub configure { 3012sub configure {
3041 my ($self, $x, $y, $w, $h) = @_; 3013 my ($self, $x, $y, $w, $h) = @_;
3042 3014
3043 $self->{w} = $w; 3015 $self->{w} = $w;
3044 $self->{h} = $h; 3016 $self->{h} = $h;
3045} 3017}
3046 3018
3047sub check_size { 3019sub reconfigure {
3048 my ($self) = @_; 3020 my ($self) = @_;
3049 3021
3022 $self->SUPER::reconfigure;
3023
3050 $self->size_allocate ($self->{w}, $self->{h}) 3024 $self->size_allocate ($self->{w}, $self->{h}, 1)
3051 if $self->{w}; 3025 if $self->{w};
3052} 3026}
3053 3027
3054sub size_request { 3028sub size_request {
3055 my ($self) = @_; 3029 my ($self) = @_;
3056 3030
3057 ($self->{w}, $self->{h}) 3031 ($self->{w}, $self->{h})
3058} 3032}
3059 3033
3060sub size_allocate { 3034sub size_allocate {
3061 my ($self, $w, $h) = @_; 3035 my ($self, $w, $h, $changed) = @_;
3062 3036
3063 for my $child ($self->children) { 3037 for my $child ($self->children) {
3064 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3038 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3065 3039
3066 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3040 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3067 if exists $child->{req_x}; 3041 if exists $child->{req_x};
3068 3042
3069 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3043 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3070 if exists $child->{req_y}; 3044 if exists $child->{req_y};
3071 3045
3046 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3047
3072 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3048 $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; 3049 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3074 3050
3075 $child->configure ($X, $Y, $W, $H); 3051 $child->configure ($X, $Y, $W, $H);
3076 } 3052 }
3089} 3065}
3090 3066
3091sub update { 3067sub update {
3092 my ($self) = @_; 3068 my ($self) = @_;
3093 3069
3094 $self->check_size;
3095 $::WANT_REFRESH++; 3070 $::WANT_REFRESH++;
3096} 3071}
3097 3072
3098sub add { 3073sub add {
3099 my ($self, @children) = @_; 3074 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 3075
3107 for my $child (@children) { 3076 for my $child (@children) {
3108 $child->{is_toplevel} = 1; 3077 $child->{is_toplevel} = 1;
3109 3078
3110 # integerise window positions 3079 # integerise window positions
3121 $self->SUPER::remove (@children); 3090 $self->SUPER::remove (@children);
3122 3091
3123 while (@children) { 3092 while (@children) {
3124 my $w = pop @children; 3093 my $w = pop @children;
3125 push @children, $w->children; 3094 push @children, $w->children;
3126 delete $w->{visible}; 3095 $w->set_invisible;
3127 } 3096 }
3128} 3097}
3129 3098
3130sub on_refresh { 3099sub on_refresh {
3131 my ($self, $id, $cb) = @_; 3100 my ($self, $id, $cb) = @_;
3145 while ($self->{refresh_hook}) { 3114 while ($self->{refresh_hook}) {
3146 $_->() 3115 $_->()
3147 for values %{delete $self->{refresh_hook}}; 3116 for values %{delete $self->{refresh_hook}};
3148 } 3117 }
3149 3118
3150 if ($self->{check_size}) { 3119 if ($self->{realloc}) {
3151 my @queue = ([], []); 3120 my @queue;
3152 3121
3153 for (;;) { 3122 while () {
3154 if ($self->{check_size}) { 3123 if ($self->{realloc}) {
3155 # heuristic: check containers last 3124 #TODO use array-of-depth approach
3156 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3125
3157 for values %{delete $self->{check_size}} 3126 @queue = sort { $a->{visible} <=> $b->{visible} }
3127 @queue, values %{delete $self->{realloc}};
3158 } 3128 }
3159 3129
3160 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3130 my $widget = pop @queue || last;
3161 3131
3132 $widget->{visible} or last; # do not resize invisible widgets
3133
3162 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3134 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3163 ? @$widget{qw(user_w user_h)} 3135 ? @$widget{qw(def_w def_h)}
3164 : $widget->size_request; 3136 : $widget->size_request;
3165 3137
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# 3138 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3169 3139
3170 $widget->{req_w} = $w; 3140 $widget->{req_w} = $w;
3171 $widget->{req_h} = $h; 3141 $widget->{req_h} = $h;
3172 3142
3173 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3143 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3174 3144
3175 $widget->{parent}->check_size 3145 push @queue, $widget->{parent}
3176 if $widget->{parent}; 3146 if $widget->{parent};
3177 }
3178 } 3147 }
3179 } 3148 }
3180 3149
3181 while ($self->{size_alloc}) { 3150 while (my $size_alloc = delete $self->{size_alloc}) {
3182 for (values %{delete $self->{size_alloc}}) { 3151 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3183 my ($widget, $w, $h) = @$_; 3152 values %$size_alloc;
3153
3154 while () {
3155 my ($widget, $w, $h) = @{ pop @queue or last };
3184 3156
3185 $w = 0 if $w < 0; 3157 $w = 0 if $w < 0;
3186 $h = 0 if $h < 0; 3158 $h = 0 if $h < 0;
3187 3159
3160 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3161
3188 $widget->{w} = $w; 3162 $widget->{w} = $w;
3189 $widget->{h} = $h; 3163 $widget->{h} = $h;
3190 $widget->size_allocate ($w, $h); 3164
3191 $widget->emit (size_allocate => $w, $h); 3165 $widget->emit (size_allocate => $w, $h, $changed);
3192 } 3166 }
3193 } 3167 }
3194 3168
3195 while ($self->{post_alloc_hook}) { 3169 while ($self->{post_alloc_hook}) {
3196 $_->() 3170 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines