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.198 by root, Fri May 12 02:08:52 2006 UTC vs.
Revision 1.251 by root, Sun May 28 22:24:24 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;
18
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}
16 38
17sub check_tooltip { 39sub check_tooltip {
18 if (!$GRAB) { 40 if (!$GRAB) {
19 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
20 if (length $widget->{tooltip}) { 42 if (length $widget->{tooltip}) {
29 $TOOLTIP->set_tooltip_from ($widget); 51 $TOOLTIP->set_tooltip_from ($widget);
30 $TOOLTIP->show; 52 $TOOLTIP->show;
31 53
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 54 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 55
34 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 56 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
36 } 57 if $x + $TOOLTIP->{w} > $::WIDTH;
37 58
38 $TOOLTIP->move ($x, $y); 59 $TOOLTIP->move ($x, $y);
39 $TOOLTIP->check_size;
40 $TOOLTIP->update; 60 $TOOLTIP->realloc;
41 } 61 }
42 62
43 return; 63 return;
44 } 64 }
45 } 65 }
49 delete $TOOLTIP->{owner}; 69 delete $TOOLTIP->{owner};
50} 70}
51 71
52# class methods for events 72# class methods for events
53sub feed_sdl_key_down_event { 73sub feed_sdl_key_down_event {
54 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 74 $FOCUS->emit (key_down => $_[0])
55 if $FOCUS; 75 if $FOCUS;
56} 76}
57 77
58sub feed_sdl_key_up_event { 78sub feed_sdl_key_up_event {
59 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 79 $FOCUS->emit (key_up => $_[0])
60 if $FOCUS; 80 if $FOCUS;
61} 81}
62 82
63sub feed_sdl_button_down_event { 83sub feed_sdl_button_down_event {
64 my ($ev) = @_; 84 my ($ev) = @_;
73 check_tooltip; 93 check_tooltip;
74 } 94 }
75 95
76 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 96 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
77 97
78 if ($GRAB) { 98 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
79 ($x, $y) = $GRAB->coord2local ($x, $y); 99 if $GRAB;
80 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
81 }
82} 100}
83 101
84sub feed_sdl_button_up_event { 102sub feed_sdl_button_up_event {
85 my ($ev) = @_; 103 my ($ev) = @_;
86 my ($x, $y) = ($ev->{x}, $ev->{y}); 104 my ($x, $y) = ($ev->{x}, $ev->{y});
87 105
88 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 106 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
89 107
90 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 108 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
91 109
92 if ($GRAB) { 110 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
93 ($x, $y) = $GRAB->coord2local ($x, $y); 111 if $GRAB;
94 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
95 }
96 112
97 if (!$BUTTON_STATE) { 113 if (!$BUTTON_STATE) {
98 my $grab = $GRAB; undef $GRAB; 114 my $grab = $GRAB; undef $GRAB;
99 $grab->update if $grab; 115 $grab->update if $grab;
100 $GRAB->update if $GRAB; 116 $GRAB->update if $GRAB;
116 $HOVER->update if $HOVER && $HOVER->{can_hover}; 132 $HOVER->update if $HOVER && $HOVER->{can_hover};
117 133
118 check_tooltip; 134 check_tooltip;
119 } 135 }
120 136
121 if ($HOVER) {
122 ($x, $y) = $HOVER->coord2local ($x, $y);
123 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); 137 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
124 } 138 if $HOVER;
125} 139}
126 140
127# convert position array to integers 141# convert position array to integers
128sub harmonize { 142sub harmonize {
129 my ($vals) = @_; 143 my ($vals) = @_;
133 for (@$vals) { 147 for (@$vals) {
134 my $i = int $_ + $rem; 148 my $i = int $_ + $rem;
135 $rem += $_ - $i; 149 $rem += $_ - $i;
136 $_ = $i; 150 $_ = $i;
137 } 151 }
152}
153
154sub full_refresh {
155 # make a copy, otherwise for complains about freed values.
156 my @widgets = values %WIDGET;
157
158 $_->update
159 for @widgets;
160}
161
162sub reconfigure_widgets {
163 # make a copy, otherwise C<for> complains about freed values.
164 my @widgets = values %WIDGET;
165
166 $_->reconfigure
167 for @widgets;
168}
169
170# call when resolution changes etc.
171sub rescale_widgets {
172 my ($sx, $sy) = @_;
173
174 for my $widget (values %WIDGET) {
175 if ($widget->{is_toplevel}) {
176 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
177 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
178 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
179 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
180 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
181 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
182 }
183 }
184
185 reconfigure_widgets;
138} 186}
139 187
140############################################################################# 188#############################################################################
141 189
142package CFClient::UI::Base; 190package CFClient::UI::Base;
155 can_events => 1, 203 can_events => 1,
156 @_ 204 @_
157 }, $class; 205 }, $class;
158 206
159 for (keys %$self) { 207 for (keys %$self) {
160 if (/^connect_(.*)$/) { 208 if (/^on_(.*)$/) {
161 $self->connect ($1 => delete $self->{$_}); 209 $self->connect ($1 => delete $self->{$_});
162 } 210 }
163 } 211 }
164 212
213 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
214
215 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
216 $self->{req_x} = $layout->{x} * $::WIDTH;
217 $self->{req_y} = $layout->{y} * $::HEIGHT;
218 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
219 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
220 }
221
165 $self 222 $self
166} 223}
167 224
168sub destroy { 225sub destroy {
169 my ($self) = @_; 226 my ($self) = @_;
178 return if $self->{parent}; 235 return if $self->{parent};
179 236
180 $CFClient::UI::ROOT->add ($self); 237 $CFClient::UI::ROOT->add ($self);
181} 238}
182 239
183sub hide { 240sub center {
184 my ($self) = @_; 241 my ($self) = @_;
242
243 $CFClient::UI::ROOT->on_post_alloc (
244 "center_$self" => sub {
245 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
246 },
247 );
248
249 $self->update;
250}
251
252sub set_visible {
253 my ($self) = @_;
254
255 return if $self->{visible};
256
257 $self->{root} = $self->{parent}{root};
258 $self->{visible} = $self->{parent}{visible} + 1;
259
260 $self->emit (visibility_change => 1);
261
262 $self->realloc unless exists $self->{req_w};
263
264 $_->set_visible for $self->children;
265}
266
267sub set_invisible {
268 my ($self) = @_;
269
270 return unless $self->{visible};
271
272 $_->set_invisible for $self->children;
273
274 delete $self->{root};
275 delete $self->{visible};
185 276
186 undef $GRAB if $GRAB == $self; 277 undef $GRAB if $GRAB == $self;
187 undef $HOVER if $HOVER == $self; 278 undef $HOVER if $HOVER == $self;
279
280 CFClient::UI::check_tooltip
281 if $TOOLTIP->{owner} == $self;
282
283 $self->focus_out;
284
285 $self->emit (visibility_change => 0);
286}
287
288sub set_visibility {
289 my ($self, $visible) = @_;
290
291 return if $self->{visible} == $visible;
292
293 $visible ? $self->hide
294 : $self->show;
295}
296
297sub toggle_visibility {
298 my ($self) = @_;
299
300 $self->{visible}
301 ? $self->hide
302 : $self->show;
303}
304
305sub hide {
306 my ($self) = @_;
307
308 $self->set_invisible;
188 309
189 $self->{parent}->remove ($self) 310 $self->{parent}->remove ($self)
190 if $self->{parent}; 311 if $self->{parent};
191} 312}
192 313
201} 322}
202 323
203sub set_size { 324sub set_size {
204 my ($self, $w, $h) = @_; 325 my ($self, $w, $h) = @_;
205 326
206 $self->{user_w} = $w; 327 $self->{def_w} = $w;
207 $self->{user_h} = $h; 328 $self->{def_h} = $h;
208 329
209 $self->check_size; 330 $self->realloc;
210} 331}
211 332
212sub size_request { 333sub size_request {
213 require Carp; 334 require Carp;
214 Carp::confess "size_request is abstract"; 335 Carp::confess "size_request is abstract";
234 $self->{y} = $y; 355 $self->{y} = $y;
235 $self->update; 356 $self->update;
236 } 357 }
237 358
238 if ($self->{w} != $w || $self->{h} != $h) { 359 if ($self->{w} != $w || $self->{h} != $h) {
360 return unless $self->{visible};
361
239 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 362 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
240 } 363 }
241} 364}
242 365
243sub size_allocate { 366sub size_allocate {
244 # nothing to be done 367 # nothing to be done
245} 368}
246 369
247sub children { 370sub children {
248} 371}
249 372
250# call when resolution changes etc.
251sub reconfigure {
252 my ($self) = @_;
253
254 $_->reconfigure
255 for $self->children;
256
257 $self->check_size (1);
258 $self->update;
259}
260
261sub set_max_size { 373sub set_max_size {
262 my ($self, $w, $h) = @_; 374 my ($self, $w, $h) = @_;
263 375
264 delete $self->{max_w}; $self->{max_w} = $w if $w; 376 delete $self->{max_w}; $self->{max_w} = $w if $w;
265 delete $self->{max_h}; $self->{max_h} = $h if $h; 377 delete $self->{max_h}; $self->{max_h} = $h if $h;
378}
379
380sub set_tooltip {
381 my ($self, $tooltip) = @_;
382
383 $tooltip =~ s/^\s+//;
384 $tooltip =~ s/\s+$//;
385
386 return if $self->{tooltip} eq $tooltip;
387
388 $self->{tooltip} = $tooltip;
389
390 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
391 delete $CFClient::UI::TOOLTIP->{owner};
392 CFClient::UI::check_tooltip;
393 }
266} 394}
267 395
268# translate global coordinates to local coordinate system 396# translate global coordinates to local coordinate system
269sub coord2local { 397sub coord2local {
270 my ($self, $x, $y) = @_; 398 my ($self, $x, $y) = @_;
285 return if $FOCUS == $self; 413 return if $FOCUS == $self;
286 return unless $self->{can_focus}; 414 return unless $self->{can_focus};
287 415
288 my $focus = $FOCUS; $FOCUS = $self; 416 my $focus = $FOCUS; $FOCUS = $self;
289 417
290 $self->emit (focus_in => $focus); 418 $self->_emit (focus_in => $focus);
291 419
292 $focus->update if $focus; 420 $focus->update if $focus;
293 $FOCUS->update; 421 $FOCUS->update;
294} 422}
295 423
298 426
299 return unless $FOCUS == $self; 427 return unless $FOCUS == $self;
300 428
301 my $focus = $FOCUS; undef $FOCUS; 429 my $focus = $FOCUS; undef $FOCUS;
302 430
303 $self->emit (focus_out => $focus); 431 $self->_emit (focus_out => $focus);
304 432
305 $focus->update if $focus; #? 433 $focus->update if $focus; #?
434
435 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
436 unless $FOCUS;
306} 437}
307 438
308sub mouse_motion { } 439sub mouse_motion { }
309sub button_up { } 440sub button_up { }
310sub key_down { } 441sub key_down { }
311sub key_up { } 442sub key_up { }
312 443
313sub button_down { 444sub button_down {
314 my ($self, $ev, $x, $y) = @_; 445 my ($self, $ev, $x, $y) = @_;
315 446
316 $self->focus_in; 447 $self->focus_in;
319sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 450sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
320sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 451sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
321sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 452sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
322sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 453sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
323sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 454sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
455
456sub find_widget {
457 my ($self, $x, $y) = @_;
458
459 return () unless $self->{can_events};
460
461 return $self
462 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
463 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
464
465 ()
466}
467
468sub set_parent {
469 my ($self, $parent) = @_;
470
471 Scalar::Util::weaken ($self->{parent} = $parent);
472
473 $self->set_visible if $parent->{visible};
474
475 $self->realloc;
476}
477
478sub connect {
479 my ($self, $signal, $cb) = @_;
480
481 push @{ $self->{signal_cb}{$signal} }, $cb;
482}
483
484sub _emit {
485 my ($self, $signal, @args) = @_;
486
487 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
488}
489
490sub emit {
491 my ($self, $signal, @args) = @_;
492
493 $self->_emit ($signal, @args)
494 || $self->$signal (@args);
495}
496
497sub visibility_change {
498 #my ($self, $visible) = @_;
499}
500
501sub realloc {
502 my ($self) = @_;
503
504 return unless $self->{visible};
505
506 return if $self->{root}{realloc}{$self};
507
508 $self->{root}{realloc}{$self} = $self;
509 $self->{root}->update;
510}
511
512sub update {
513 my ($self) = @_;
514
515 $self->{parent}->update
516 if $self->{parent};
517}
324 518
325sub draw { 519sub draw {
326 my ($self) = @_; 520 my ($self) = @_;
327 521
328 return unless $self->{h} && $self->{w}; 522 return unless $self->{h} && $self->{w};
350 if ($ENV{PCLIENT_DEBUG}) { 544 if ($ENV{PCLIENT_DEBUG}) {
351 glPushMatrix; 545 glPushMatrix;
352 glColor 1, 1, 0, 1; 546 glColor 1, 1, 0, 1;
353 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 547 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
354 glBegin GL_LINE_LOOP; 548 glBegin GL_LINE_LOOP;
355 glVertex 0 , 0; 549 glVertex 0 , 0;
356 glVertex $self->{w}, 0; 550 glVertex $self->{w} - 1, 0;
357 glVertex $self->{w}, $self->{h}; 551 glVertex $self->{w} - 1, $self->{h} - 1;
358 glVertex 0 , $self->{h}; 552 glVertex 0 , $self->{h} - 1;
359 glEnd; 553 glEnd;
360 glPopMatrix; 554 glPopMatrix;
361 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 555 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
362 } 556 }
363} 557}
364 558
365sub _draw { 559sub _draw {
366 my ($self) = @_; 560 my ($self) = @_;
367 561
368 warn "no draw defined for $self\n"; 562 warn "no draw defined for $self\n";
369} 563}
370 564
371sub find_widget {
372 my ($self, $x, $y) = @_;
373
374 return () unless $self->{can_events};
375
376 return $self
377 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
378 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
379
380 ()
381}
382
383sub set_parent {
384 my ($self, $parent) = @_;
385
386 Scalar::Util::weaken ($self->{parent} = $parent);
387
388
389 # TODO: req_w _does_change after ->reconfigure
390 $self->check_size
391 unless exists $self->{req_w};
392}
393
394sub check_size {
395 my ($self, $forced) = @_;
396
397 $self->{force_alloc} = 1 if $forced;
398 $CFClient::UI::ROOT->{check_size}{$self} = $self;
399}
400
401sub update {
402 my ($self) = @_;
403
404 $self->{parent}->update
405 if $self->{parent};
406}
407
408sub connect {
409 my ($self, $signal, $cb) = @_;
410
411 push @{ $self->{signal_cb}{$signal} }, $cb;
412}
413
414sub emit {
415 my ($self, $signal, @args) = @_;
416
417 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
418}
419
420sub DESTROY { 565sub DESTROY {
421 my ($self) = @_; 566 my ($self) = @_;
422 567
568 delete $WIDGET{$self+0};
423 #$self->deactivate; 569 #$self->deactivate;
424} 570}
425 571
426############################################################################# 572#############################################################################
427 573
436 my $class = shift; 582 my $class = shift;
437 583
438 # range [value, low, high, page] 584 # range [value, low, high, page]
439 585
440 $class->SUPER::new ( 586 $class->SUPER::new (
441 bg => [0, 0, 0, 0.2], 587 #bg => [0, 0, 0, 0.2],
442 active_bg => [1, 1, 1, 0.5], 588 #active_bg => [1, 1, 1, 0.5],
443 @_ 589 @_
444 ) 590 )
445} 591}
446 592
447sub _draw { 593sub _draw {
448 my ($self) = @_; 594 my ($self) = @_;
449 595
596 my $color = $FOCUS == $self && $self->{active_bg}
597 ? $self->{active_bg}
598 : $self->{bg};
599
600 if ($color && (@$color < 4 || $color->[3])) {
450 my ($w, $h) = @$self{qw(w h)}; 601 my ($w, $h) = @$self{qw(w h)};
451 602
452 glEnable GL_BLEND; 603 glEnable GL_BLEND;
453 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 604 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
454 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 605 glColor @$color;
455 606
456 glBegin GL_QUADS; 607 glBegin GL_QUADS;
457 glVertex 0 , 0; 608 glVertex 0 , 0;
458 glVertex 0 , $h; 609 glVertex 0 , $h;
459 glVertex $w, $h; 610 glVertex $w, $h;
460 glVertex $w, 0; 611 glVertex $w, 0;
461 glEnd; 612 glEnd;
462 613
463 glDisable GL_BLEND; 614 glDisable GL_BLEND;
615 }
464} 616}
465 617
466############################################################################# 618#############################################################################
467 619
468package CFClient::UI::Empty; 620package CFClient::UI::Empty;
512 $self->{children} = [ 664 $self->{children} = [
513 sort { $a->{z} <=> $b->{z} } 665 sort { $a->{z} <=> $b->{z} }
514 @{$self->{children}}, @widgets 666 @{$self->{children}}, @widgets
515 ]; 667 ];
516 668
517 $self->check_size (1); 669 $self->realloc;
518 $self->update;
519} 670}
520 671
521sub children { 672sub children {
522 @{ $_[0]{children} } 673 @{ $_[0]{children} }
523} 674}
528 delete $child->{parent}; 679 delete $child->{parent};
529 $child->hide; 680 $child->hide;
530 681
531 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 682 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
532 683
533 $self->check_size; 684 $self->realloc;
534 $self->update;
535} 685}
536 686
537sub clear { 687sub clear {
538 my ($self) = @_; 688 my ($self) = @_;
539 689
543 for (@$children) { 693 for (@$children) {
544 delete $_->{parent}; 694 delete $_->{parent};
545 $_->hide; 695 $_->hide;
546 } 696 }
547 697
548 $self->check_size; 698 $self->realloc;
549 $self->update;
550} 699}
551 700
552sub find_widget { 701sub find_widget {
553 my ($self, $x, $y) = @_; 702 my ($self, $x, $y) = @_;
554 703
607sub size_request { 756sub size_request {
608 $_[0]{children}[0]->size_request 757 $_[0]{children}[0]->size_request
609} 758}
610 759
611sub size_allocate { 760sub size_allocate {
612 my ($self, $w, $h) = @_; 761 my ($self, $w, $h, $changed) = @_;
613 762
614 $self->{children}[0]->configure (0, 0, $w, $h); 763 $self->{children}[0]->configure (0, 0, $w, $h);
615} 764}
616 765
617############################################################################# 766#############################################################################
634 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 783 $ROOT->on_post_alloc ($self => sub { $self->render_child });
635 $self->SUPER::update; 784 $self->SUPER::update;
636} 785}
637 786
638sub size_allocate { 787sub size_allocate {
639 my ($self, $w, $h) = @_; 788 my ($self, $w, $h, $changed) = @_;
640 789
641 $self->SUPER::size_allocate ($w, $h); 790 $self->SUPER::size_allocate ($w, $h, $changed);
642 $self->update; 791 $self->update
792 if $changed;
643} 793}
644 794
645sub _render { 795sub _render {
646 $_[0]{children}[0]->draw; 796 $_[0]{children}[0]->draw;
647} 797}
652 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 802 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
653 glClearColor 0, 0, 0, 0; 803 glClearColor 0, 0, 0, 0;
654 glClear GL_COLOR_BUFFER_BIT; 804 glClear GL_COLOR_BUFFER_BIT;
655 805
656 $self->_render; 806 $self->_render;
657# glColorMask 1, 1, 1, 0;
658# glEnable GL_BLEND;
659# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
660# glRasterPos 0, 0;
661# glCopyPixels 0, 0, $self->{w}, $self->{h};
662# glDisable GL_BLEND;
663# glColorMask 1, 1, 1, 1;
664 }; 807 };
665} 808}
666 809
667sub _draw { 810sub _draw {
668 my ($self) = @_; 811 my ($self) = @_;
672 my $tex = $self->{texture} 815 my $tex = $self->{texture}
673 or return; 816 or return;
674 817
675 glEnable GL_TEXTURE_2D; 818 glEnable GL_TEXTURE_2D;
676 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 819 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
677 glColor 0, 0, 0, 1; 820 glColor 1, 1, 1, 1;
678 821
679 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 822 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
680 823
681 glDisable GL_TEXTURE_2D; 824 glDisable GL_TEXTURE_2D;
682} 825}
685 828
686package CFClient::UI::ViewPort; 829package CFClient::UI::ViewPort;
687 830
688our @ISA = CFClient::UI::Window::; 831our @ISA = CFClient::UI::Window::;
689 832
833sub new {
834 my $class = shift;
835
836 $class->SUPER::new (
837 scroll_x => 0,
838 scroll_y => 1,
839 @_,
840 )
841}
842
690sub size_request { 843sub size_request {
691 my ($self) = @_; 844 my ($self) = @_;
692 845
693 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 846 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
694 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
695 847
696 @$self{qw(child_w child_h)} 848 $w = 10 if $self->{scroll_x};
849 $h = 10 if $self->{scroll_y};
850
851 ($w, $h)
697} 852}
698 853
699sub size_allocate { 854sub size_allocate {
700 my ($self, $w, $h) = @_; 855 my ($self, $w, $h, $changed) = @_;
701 856
857 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
858 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
859
860 $self->child->configure (0, 0, $w, $h);
702 $self->update; 861 $self->update;
703} 862}
704 863
705sub set_offset { 864sub set_offset {
706 my ($self, $x, $y) = @_; 865 my ($self, $x, $y) = @_;
757 my $class = shift; 916 my $class = shift;
758 917
759 my $self; 918 my $self;
760 919
761 my $slider = new CFClient::UI::Slider 920 my $slider = new CFClient::UI::Slider
762 vertical => 1, 921 vertical => 1,
763 range => [0, 0, 1, 0.01], # HACK fix 922 range => [0, 0, 1, 0.01], # HACK fix
764 connect_changed => sub { 923 on_changed => sub {
765 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); 924 $self->{vp}->set_offset (0, $_[1]);
766 }, 925 },
767 ; 926 ;
768 927
769 $self = $class->SUPER::new ( 928 $self = $class->SUPER::new (
770 vp => (new CFClient::UI::ViewPort), 929 vp => (new CFClient::UI::ViewPort expand => 1),
771 slider => $slider, 930 slider => $slider,
772 @_, 931 @_,
773 ); 932 );
774 933
775 $self->{vp}->add ($self->{scrolled}); 934 $self->{vp}->add ($self->{scrolled});
777 $self->add ($self->{slider}); 936 $self->add ($self->{slider});
778 937
779 $self 938 $self
780} 939}
781 940
941sub update {
942 my ($self) = @_;
943
944 $self->SUPER::update;
945
946 # todo: overwrite size_allocate of child
947 my $child = $self->{vp}->child;
948 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
949}
950
951sub size_allocate {
952 my ($self, $w, $h, $changed) = @_;
953
954 $self->SUPER::size_allocate ($w, $h, $changed);
955
956 my $child = $self->{vp}->child;
957 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
958}
959
782#TODO# update range on size_allocate depeneing on child 960#TODO# update range on size_allocate depending on child
783# update viewport offset on scroll 961# update viewport offset on scroll
784 962
785############################################################################# 963#############################################################################
786 964
787package CFClient::UI::Frame; 965package CFClient::UI::Frame;
788 966
789our @ISA = CFClient::UI::Bin::; 967our @ISA = CFClient::UI::Bin::;
790 968
791use CFClient::OpenGL; 969use CFClient::OpenGL;
970
971sub new {
972 my $class = shift;
973
974 $class->SUPER::new (
975 bg => undef,
976 @_,
977 )
978}
979
980sub _draw {
981 my ($self) = @_;
982
983 if ($self->{bg}) {
984 my ($w, $h) = @$self{qw(w h)};
985
986 glEnable GL_BLEND;
987 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
988 glColor @{ $self->{bg} };
989
990 glBegin GL_QUADS;
991 glVertex 0 , 0;
992 glVertex 0 , $h;
993 glVertex $w, $h;
994 glVertex $w, 0;
995 glEnd;
996
997 glDisable GL_BLEND;
998 }
999
1000 $self->SUPER::_draw;
1001}
792 1002
793############################################################################# 1003#############################################################################
794 1004
795package CFClient::UI::FancyFrame; 1005package CFClient::UI::FancyFrame;
796 1006
803 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1013 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
804 1014
805sub new { 1015sub new {
806 my $class = shift; 1016 my $class = shift;
807 1017
808 # TODO: user_x, user_y, overwrite moveto?
809
810 my $self = $class->SUPER::new ( 1018 my $self = $class->SUPER::new (
811 bg => [1, 1, 1, 1], 1019 bg => [1, 1, 1, 1],
812 border_bg => [1, 1, 1, 1], 1020 border_bg => [1, 1, 1, 1],
813 border => 0.6, 1021 border => 0.6,
1022 is_toplevel => 1,
814 can_events => 1, 1023 can_events => 1,
815 @_ 1024 @_
816 ); 1025 );
817 1026
818 $self->{title} &&= new CFClient::UI::Label 1027 $self->{title} &&= new CFClient::UI::Label
819 align => 0, 1028 align => 0,
838 $h + $self->border * 2, 1047 $h + $self->border * 2,
839 ) 1048 )
840} 1049}
841 1050
842sub size_allocate { 1051sub size_allocate {
843 my ($self, $w, $h) = @_; 1052 my ($self, $w, $h, $changed) = @_;
1053
1054 return unless $changed;
844 1055
845 $h -= List::Util::max 0, $self->border * 2; 1056 $h -= List::Util::max 0, $self->border * 2;
846 $w -= List::Util::max 0, $self->border * 2; 1057 $w -= List::Util::max 0, $self->border * 2;
847 1058
848 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 1059 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
849 if $self->{title}; 1060 if $self->{title};
850 1061
851 $self->child->configure ($self->border, $self->border, $w, $h); 1062 $self->child->configure ($self->border, $self->border, $w, $h);
852} 1063}
853 1064
872 my ($ev, $x, $y) = @_; 1083 my ($ev, $x, $y) = @_;
873 1084
874 my $dx = $ev->{x} - $ox; 1085 my $dx = $ev->{x} - $ox;
875 my $dy = $ev->{y} - $oy; 1086 my $dy = $ev->{y} - $oy;
876 1087
1088 $self->{user_x} = $wx + $dx * $mx;
1089 $self->{user_y} = $wy + $dy * $my;
877 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1090 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
878 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1091 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
879 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1092 $self->move ($self->{user_x}, $self->{user_y});
880 $self->check_size; 1093 $self->realloc;
881 }; 1094 };
882 1095
883 } elsif ($lr ^ $td) { 1096 } elsif ($lr ^ $td) {
884 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1097 my ($ox, $oy) = ($ev->{x}, $ev->{y});
885 my ($bx, $by) = ($self->{x}, $self->{y}); 1098 my ($bx, $by) = ($self->{x}, $self->{y});
887 $self->{motion} = sub { 1100 $self->{motion} = sub {
888 my ($ev, $x, $y) = @_; 1101 my ($ev, $x, $y) = @_;
889 1102
890 ($x, $y) = ($ev->{x}, $ev->{y}); 1103 ($x, $y) = ($ev->{x}, $ev->{y});
891 1104
892 $self->move ($bx + $x - $ox, $by + $y - $oy); 1105 $self->{user_x} = $bx + $x - $ox;
1106 $self->{user_y} = $by + $y - $oy;
1107
1108 $self->move ($self->{user_x}, $self->{user_y});
893 $self->update; 1109 $self->realloc;
894 }; 1110 };
895 } 1111 }
896} 1112}
897 1113
898sub button_up { 1114sub button_up {
959sub new { 1175sub new {
960 my $class = shift; 1176 my $class = shift;
961 1177
962 $class->SUPER::new ( 1178 $class->SUPER::new (
963 col_expand => [], 1179 col_expand => [],
964 @_ 1180 @_,
965 ) 1181 )
1182}
1183
1184sub children {
1185 grep $_, map @$_, grep $_, @{ $_[0]{children} }
966} 1186}
967 1187
968sub add { 1188sub add {
969 my ($self, $x, $y, $child) = @_; 1189 my ($self, $x, $y, $child) = @_;
970 1190
971 $child->set_parent ($self); 1191 $child->set_parent ($self);
972 $self->{children}[$y][$x] = $child; 1192 $self->{children}[$y][$x] = $child;
973 1193
974 $child->check_size; 1194 $self->realloc;
975} 1195}
976 1196
977sub children {
978 grep $_, map @$_, grep $_, @{ $_[0]{children} }
979}
980
981# TODO: move to container class maybe? send childs a signal on removal? 1197# TODO: move to container class maybe? send children a signal on removal?
982sub clear { 1198sub clear {
983 my ($self) = @_; 1199 my ($self) = @_;
984 1200
985 my @children = $self->children; 1201 my @children = $self->children;
986 delete $self->{children}; 1202 delete $self->{children};
988 for (@children) { 1204 for (@children) {
989 delete $_->{parent}; 1205 delete $_->{parent};
990 $_->hide; 1206 $_->hide;
991 } 1207 }
992 1208
993 $self->update; 1209 $self->realloc;
994} 1210}
995 1211
996sub get_wh { 1212sub get_wh {
997 my ($self) = @_; 1213 my ($self) = @_;
998 1214
1025 (sum @$hs), 1241 (sum @$hs),
1026 ) 1242 )
1027} 1243}
1028 1244
1029sub size_allocate { 1245sub size_allocate {
1030 my ($self, $w, $h) = @_; 1246 my ($self, $w, $h, $changed) = @_;
1031 1247
1032 my ($ws, $hs) = $self->get_wh; 1248 my ($ws, $hs) = $self->get_wh;
1033 1249
1034 my $req_w = sum @$ws; 1250 my $req_w = (sum @$ws) || 1;
1035 my $req_h = sum @$hs; 1251 my $req_h = (sum @$hs) || 1;
1036 1252
1037 # TODO: nicer code && do row_expand 1253 # TODO: nicer code && do row_expand
1038 my @col_expand = @{$self->{col_expand}}; 1254 my @col_expand = @{$self->{col_expand}};
1039 @col_expand = (1) x @$ws unless @col_expand; 1255 @col_expand = (1) x @$ws unless @col_expand;
1040 my $col_expand = (sum @col_expand) || 1; 1256 my $col_expand = (sum @col_expand) || 1;
1094 } 1310 }
1095} 1311}
1096 1312
1097############################################################################# 1313#############################################################################
1098 1314
1315package CFClient::UI::Box;
1316
1317our @ISA = CFClient::UI::Container::;
1318
1319sub size_request {
1320 my ($self) = @_;
1321
1322 $self->{vertical}
1323 ? (
1324 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1325 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1326 )
1327 : (
1328 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1329 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1330 )
1331}
1332
1333sub size_allocate {
1334 my ($self, $w, $h, $changed) = @_;
1335
1336 my $space = $self->{vertical} ? $h : $w;
1337 my $children = $self->{children};
1338
1339 my @req;
1340
1341 if ($self->{homogeneous}) {
1342 @req = ($space / (@$children || 1)) x @$children;
1343 } else {
1344 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1345 my $req = List::Util::sum @req;
1346
1347 if ($req > $space) {
1348 # ah well, not enough space
1349 $_ *= $space / $req for @req;
1350 } else {
1351 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1352
1353 $space = ($space - $req) / $expand; # remaining space to give away
1354
1355 $req[$_] += $space * $children->[$_]{expand}
1356 for 0 .. $#$children;
1357 }
1358 }
1359
1360 CFClient::UI::harmonize \@req;
1361
1362 my $pos = 0;
1363 for (0 .. $#$children) {
1364 my $alloc = $req[$_];
1365 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1366
1367 $pos += $alloc;
1368 }
1369
1370 1
1371}
1372
1373#############################################################################
1374
1099package CFClient::UI::HBox; 1375package CFClient::UI::HBox;
1100 1376
1101# TODO: wrap into common Box base class
1102
1103our @ISA = CFClient::UI::Container::; 1377our @ISA = CFClient::UI::Box::;
1104 1378
1105sub size_request { 1379sub new {
1106 my ($self) = @_; 1380 my $class = shift;
1107 1381
1108 my @alloc = map [$_->size_request], @{$self->{children}}; 1382 $class->SUPER::new (
1109 1383 vertical => 0,
1110 ( 1384 @_,
1111 (List::Util::sum map $_->[0], @alloc),
1112 (List::Util::max map $_->[1], @alloc),
1113 ) 1385 )
1114} 1386}
1115 1387
1116sub size_allocate {
1117 my ($self, $w, $h) = @_;
1118
1119 ($h, $w) = ($w, $h);
1120
1121 my $children = $self->{children};
1122
1123 my @h = map $_->{req_w}, @$children;
1124
1125 my $req_h = List::Util::sum @h;
1126
1127 if ($req_h > $h) {
1128 # ah well, not enough space
1129 $_ *= $h / $req_h for @h;
1130 } else {
1131 my $exp = List::Util::sum map $_->{expand}, @$children;
1132 $exp ||= 1;
1133
1134 for (0 .. $#$children) {
1135 my $child = $children->[$_];
1136
1137 my $alloc_h = $h[$_];
1138 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1139 $h[$_] = $alloc_h;
1140 }
1141 }
1142
1143 CFClient::UI::harmonize \@h;
1144
1145 my $y = 0;
1146 for (0 .. $#$children) {
1147 my $child = $children->[$_];
1148 my $h = $h[$_];
1149 $child->configure ($y, 0, $h, $w);
1150
1151 $y += $h;
1152 }
1153
1154 1
1155}
1156
1157############################################################################# 1388#############################################################################
1158 1389
1159package CFClient::UI::VBox; 1390package CFClient::UI::VBox;
1160 1391
1161# TODO: wrap into common Box base class
1162
1163our @ISA = CFClient::UI::Container::; 1392our @ISA = CFClient::UI::Box::;
1164 1393
1165sub size_request { 1394sub new {
1166 my ($self) = @_; 1395 my $class = shift;
1167 1396
1168 my @alloc = map [$_->size_request], @{$self->{children}}; 1397 $class->SUPER::new (
1169 1398 vertical => 1,
1170 ( 1399 @_,
1171 (List::Util::max map $_->[0], @alloc),
1172 (List::Util::sum map $_->[1], @alloc),
1173 ) 1400 )
1174} 1401}
1175 1402
1176sub size_allocate {
1177 my ($self, $w, $h) = @_;
1178
1179 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1180
1181 my $children = $self->{children};
1182
1183 my @h = map $_->{req_h}, @$children;
1184
1185 my $req_h = List::Util::sum @h;
1186
1187 if ($req_h > $h) {
1188 # ah well, not enough space
1189 $_ *= $h / $req_h for @h;
1190 } else {
1191 my $exp = List::Util::sum map $_->{expand}, @$children;
1192 $exp ||= 1;
1193
1194 for (0 .. $#$children) {
1195 my $child = $children->[$_];
1196
1197 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1198 }
1199 }
1200
1201 CFClient::UI::harmonize \@h;
1202
1203 my $y = 0;
1204 for (0 .. $#$children) {
1205 my $child = $children->[$_];
1206 my $h = $h[$_];
1207 $child->configure (0, $y, $w, $h);
1208
1209 $y += $h;
1210 }
1211
1212 1
1213}
1214
1215############################################################################# 1403#############################################################################
1216 1404
1217package CFClient::UI::Label; 1405package CFClient::UI::Label;
1218 1406
1219our @ISA = CFClient::UI::Base::; 1407our @ISA = CFClient::UI::DrawBG::;
1220 1408
1221use CFClient::OpenGL; 1409use CFClient::OpenGL;
1222 1410
1223sub new { 1411sub new {
1224 my ($class, %arg) = @_; 1412 my ($class, %arg) = @_;
1225 1413
1226 my $self = $class->SUPER::new ( 1414 my $self = $class->SUPER::new (
1227 fg => [1, 1, 1], 1415 fg => [1, 1, 1],
1416 #bg => none
1417 #active_bg => none
1228 #font => default_font 1418 #font => default_font
1229 #text => initial text 1419 #text => initial text
1230 #markup => initial narkup 1420 #markup => initial narkup
1421 #max_w => maximum pixel width
1422 ellipsise => 3, # end
1231 layout => (new CFClient::Layout), 1423 layout => (new CFClient::Layout),
1232 fontsize => 1, 1424 fontsize => 1,
1233 align => -1, 1425 align => -1,
1234 valign => -1, 1426 valign => -1,
1235 padding => 2, 1427 padding => 2,
1250 } 1442 }
1251 1443
1252 $self 1444 $self
1253} 1445}
1254 1446
1255sub escape { 1447sub escape($) {
1256 local $_ = $_[1]; 1448 local $_ = $_[0];
1257 1449
1258 s/&/&amp;/g; 1450 s/&/&amp;/g;
1259 s/>/&gt;/g; 1451 s/>/&gt;/g;
1260 s/</&lt;/g; 1452 s/</&lt;/g;
1261 1453
1262 $_[1] 1454 $_
1263} 1455}
1264 1456
1265sub update { 1457sub update {
1266 my ($self) = @_; 1458 my ($self) = @_;
1267 1459
1276 $self->{text} = "T$text"; 1468 $self->{text} = "T$text";
1277 1469
1278 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1470 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1279 $self->{layout}->set_text ($text); 1471 $self->{layout}->set_text ($text);
1280 1472
1281 $self->update; 1473 $self->realloc;
1282 $self->check_size;
1283} 1474}
1284 1475
1285sub set_markup { 1476sub set_markup {
1286 my ($self, $markup) = @_; 1477 my ($self, $markup) = @_;
1287 1478
1291 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1482 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1292 1483
1293 $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;
1294 $self->{layout}->set_markup ($markup); 1485 $self->{layout}->set_markup ($markup);
1295 1486
1296 $self->update; 1487 $self->realloc;
1297 $self->check_size;
1298} 1488}
1299 1489
1300sub size_request { 1490sub size_request {
1301 my ($self) = @_; 1491 my ($self) = @_;
1302 1492
1303 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1493 $self->{layout}->set_font ($self->{font}) if $self->{font};
1304 $self->{layout}->set_width ($self->{max_w} || -1); 1494 $self->{layout}->set_width ($self->{max_w} || -1);
1495 $self->{layout}->set_ellipsise ($self->{ellipsise});
1496 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1305 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1497 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1306 1498
1307 my ($w, $h) = $self->{layout}->size; 1499 my ($w, $h) = $self->{layout}->size;
1308 1500
1309 if (exists $self->{template}) { 1501 if (exists $self->{template}) {
1321 $h + $self->{padding} * 2, 1513 $h + $self->{padding} * 2,
1322 ) 1514 )
1323} 1515}
1324 1516
1325sub size_allocate { 1517sub size_allocate {
1326 my ($self, $w, $h) = @_; 1518 my ($self, $w, $h, $changed) = @_;
1327 1519
1328 delete $self->{texture}; 1520 delete $self->{texture}
1521 if $changed;
1329} 1522}
1330 1523
1331sub set_fontsize { 1524sub set_fontsize {
1332 my ($self, $fontsize) = @_; 1525 my ($self, $fontsize) = @_;
1333 1526
1334 $self->{fontsize} = $fontsize; 1527 $self->{fontsize} = $fontsize;
1335 delete $self->{texture}; 1528 delete $self->{texture};
1336 1529
1337 $self->update; 1530 $self->realloc;
1338 $self->check_size;
1339} 1531}
1340 1532
1341sub _draw { 1533sub _draw {
1342 my ($self) = @_; 1534 my ($self) = @_;
1535
1536 $self->SUPER::_draw; # draw background, if applicable
1343 1537
1344 my $tex = $self->{texture} ||= do { 1538 my $tex = $self->{texture} ||= do {
1345 $self->{layout}->set_foreground (@{$self->{fg}}); 1539 $self->{layout}->set_foreground (@{$self->{fg}});
1346 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1540 $self->{layout}->set_font ($self->{font}) if $self->{font};
1347 $self->{layout}->set_width ($self->{w}); 1541 $self->{layout}->set_width ($self->{w});
1542 $self->{layout}->set_ellipsise ($self->{ellipsise});
1543 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1348 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1544 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1349 1545
1350 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1546 my $tex = new_from_layout CFClient::Texture $self->{layout};
1351 1547
1352 $self->{ox} = int $self->{align} < 0 ? $self->{padding} 1548 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1353 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1549 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1354 : ($self->{w} - $tex->{w}) * 0.5; 1550 : ($self->{w} - $tex->{w}) * 0.5);
1355 1551
1356 $self->{oy} = int $self->{valign} < 0 ? $self->{padding} 1552 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1357 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1553 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1358 : ($self->{h} - $tex->{h}) * 0.5; 1554 : ($self->{h} - $tex->{h}) * 0.5);
1359 1555
1360 $tex 1556 $tex
1361 }; 1557 };
1362 1558
1363 glEnable GL_TEXTURE_2D; 1559 glEnable GL_TEXTURE_2D;
1391 active_fg => [0, 0, 0], 1587 active_fg => [0, 0, 0],
1392 can_hover => 1, 1588 can_hover => 1,
1393 can_focus => 1, 1589 can_focus => 1,
1394 valign => 0, 1590 valign => 0,
1395 can_events => 1, 1591 can_events => 1,
1592 #text => ...
1396 @_ 1593 @_
1397 ) 1594 )
1398} 1595}
1399 1596
1400sub _set_text { 1597sub _set_text {
1410 $self->{text} = $text; 1607 $self->{text} = $text;
1411 1608
1412 $text =~ s/./*/g if $self->{hidden}; 1609 $text =~ s/./*/g if $self->{hidden};
1413 $self->{layout}->set_text ("$text "); 1610 $self->{layout}->set_text ("$text ");
1414 1611
1415 $self->emit (changed => $self->{text}); 1612 $self->_emit (changed => $self->{text});
1416} 1613}
1417 1614
1418sub set_text { 1615sub set_text {
1419 my ($self, $text) = @_; 1616 my ($self, $text) = @_;
1420 1617
1421 $self->{cursor} = length $text; 1618 $self->{cursor} = length $text;
1422 $self->_set_text ($text); 1619 $self->_set_text ($text);
1423 $self->check_size; 1620
1424 $self->update; 1621 $self->realloc;
1425} 1622}
1426 1623
1427sub get_text { 1624sub get_text {
1428 $_[0]{text} 1625 $_[0]{text}
1429} 1626}
1432 my ($self) = @_; 1629 my ($self) = @_;
1433 1630
1434 my ($w, $h) = $self->SUPER::size_request; 1631 my ($w, $h) = $self->SUPER::size_request;
1435 1632
1436 ($w + 1, $h) # add 1 for cursor 1633 ($w + 1, $h) # add 1 for cursor
1437}
1438
1439sub size_allocate {
1440 my ($self, $w, $h) = @_;
1441
1442 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1443} 1634}
1444 1635
1445sub key_down { 1636sub key_down {
1446 my ($self, $ev) = @_; 1637 my ($self, $ev) = @_;
1447 1638
1449 my $sym = $ev->{sym}; 1640 my $sym = $ev->{sym};
1450 my $uni = $ev->{unicode}; 1641 my $uni = $ev->{unicode};
1451 1642
1452 my $text = $self->get_text; 1643 my $text = $self->get_text;
1453 1644
1454 if ($sym == 8) { 1645 if ($uni == 8) {
1455 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1646 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1456 } elsif ($sym == 127) { 1647 } elsif ($uni == 127) {
1457 substr $text, $self->{cursor}, 1, ""; 1648 substr $text, $self->{cursor}, 1, "";
1458 } elsif ($sym == CFClient::SDLK_LEFT) { 1649 } elsif ($sym == CFClient::SDLK_LEFT) {
1459 --$self->{cursor} if $self->{cursor}; 1650 --$self->{cursor} if $self->{cursor};
1460 } elsif ($sym == CFClient::SDLK_RIGHT) { 1651 } elsif ($sym == CFClient::SDLK_RIGHT) {
1461 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1652 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1462 } elsif ($sym == CFClient::SDLK_HOME) { 1653 } elsif ($sym == CFClient::SDLK_HOME) {
1463 $self->{cursor} = 0; 1654 $self->{cursor} = 0;
1464 } elsif ($sym == CFClient::SDLK_END) { 1655 } elsif ($sym == CFClient::SDLK_END) {
1465 $self->{cursor} = length $text; 1656 $self->{cursor} = length $text;
1466 } elsif ($sym == 27) { 1657 } elsif ($uni == 27) {
1467 $self->emit ('escape'); 1658 $self->_emit ('escape');
1468 } elsif ($uni) { 1659 } elsif ($uni) {
1469 substr $text, $self->{cursor}++, 0, chr $uni; 1660 substr $text, $self->{cursor}++, 0, chr $uni;
1470 } 1661 }
1471 1662
1472 $self->_set_text ($text); 1663 $self->_set_text ($text);
1473 $self->update; 1664
1665 $self->realloc;
1474} 1666}
1475 1667
1476sub focus_in { 1668sub focus_in {
1477 my ($self) = @_; 1669 my ($self) = @_;
1478 1670
1558 if ($sym == 13) { 1750 if ($sym == 13) {
1559 unshift @{$self->{history}}, 1751 unshift @{$self->{history}},
1560 my $txt = $self->get_text; 1752 my $txt = $self->get_text;
1561 $self->{history_pointer} = -1; 1753 $self->{history_pointer} = -1;
1562 $self->{history_saveback} = ''; 1754 $self->{history_saveback} = '';
1563 $self->emit (activate => $txt); 1755 $self->_emit (activate => $txt);
1564 $self->update; 1756 $self->update;
1565 1757
1566 } elsif ($sym == CFClient::SDLK_UP) { 1758 } elsif ($sym == CFClient::SDLK_UP) {
1567 if ($self->{history_pointer} < 0) { 1759 if ($self->{history_pointer} < 0) {
1568 $self->{history_saveback} = $self->get_text; 1760 $self->{history_saveback} = $self->get_text;
1607 my $class = shift; 1799 my $class = shift;
1608 1800
1609 $class->SUPER::new ( 1801 $class->SUPER::new (
1610 padding => 4, 1802 padding => 4,
1611 fg => [1, 1, 1], 1803 fg => [1, 1, 1],
1612 bg => [1, 1, 1, 0.2],
1613 active_fg => [0, 0, 1], 1804 active_fg => [0, 0, 1],
1614 can_hover => 1, 1805 can_hover => 1,
1615 align => 0, 1806 align => 0,
1616 valign => 0, 1807 valign => 0,
1617 can_events => 1, 1808 can_events => 1,
1618 @_ 1809 @_
1619 ) 1810 )
1620} 1811}
1621 1812
1813sub activate { }
1814
1622sub button_up { 1815sub button_up {
1623 my ($self, $ev, $x, $y) = @_; 1816 my ($self, $ev, $x, $y) = @_;
1624 1817
1818 $self->emit ("activate")
1625 if ($x >= 0 && $x < $self->{w} 1819 if $x >= 0 && $x < $self->{w}
1626 && $y >= 0 && $y < $self->{h}) { 1820 && $y >= 0 && $y < $self->{h};
1627 $self->emit ("activate");
1628 }
1629} 1821}
1630 1822
1631sub _draw { 1823sub _draw {
1632 my ($self) = @_; 1824 my ($self) = @_;
1633 1825
1665 1857
1666 $class->SUPER::new ( 1858 $class->SUPER::new (
1667 padding => 2, 1859 padding => 2,
1668 fg => [1, 1, 1], 1860 fg => [1, 1, 1],
1669 active_fg => [1, 1, 0], 1861 active_fg => [1, 1, 0],
1862 bg => [0, 0, 0, 0.2],
1863 active_bg => [1, 1, 1, 0.5],
1670 state => 0, 1864 state => 0,
1671 can_hover => 1, 1865 can_hover => 1,
1672 @_ 1866 @_
1673 ) 1867 )
1674} 1868}
1683 my ($self, $ev, $x, $y) = @_; 1877 my ($self, $ev, $x, $y) = @_;
1684 1878
1685 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1879 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1686 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1880 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1687 $self->{state} = !$self->{state}; 1881 $self->{state} = !$self->{state};
1688 $self->emit (changed => $self->{state}); 1882 $self->_emit (changed => $self->{state});
1689 } 1883 }
1690} 1884}
1691 1885
1692sub _draw { 1886sub _draw {
1693 my ($self) = @_; 1887 my ($self) = @_;
1958 qw(s1_slider.png s1_slider_bg.png); 2152 qw(s1_slider.png s1_slider_bg.png);
1959 2153
1960sub new { 2154sub new {
1961 my $class = shift; 2155 my $class = shift;
1962 2156
1963 # range [value, low, high, page] 2157 # range [value, low, high, page, unit]
1964 2158
1965 # TODO: 0-width page 2159 # TODO: 0-width page
1966 # TODO: req_w/h are wrong with vertical 2160 # TODO: req_w/h are wrong with vertical
1967 # TODO: calculations are off 2161 # TODO: calculations are off
1968 my $self = $class->SUPER::new ( 2162 my $self = $class->SUPER::new (
1969 fg => [1, 1, 1], 2163 fg => [1, 1, 1],
1970 active_fg => [0, 0, 0], 2164 active_fg => [0, 0, 0],
2165 bg => [0, 0, 0, 0.2],
2166 active_bg => [1, 1, 1, 0.5],
1971 range => [0, 0, 100, 10], 2167 range => [0, 0, 100, 10, 0],
1972 req_w => $::WIDTH / 80, 2168 req_w => $::WIDTH / 80,
1973 req_h => $::WIDTH / 80, 2169 req_h => $::WIDTH / 80,
1974 vertical => 0, 2170 vertical => 0,
1975 can_hover => 1, 2171 can_hover => 1,
1976 inner_pad => 5, 2172 inner_pad => 0.02,
1977 @_ 2173 @_
1978 ); 2174 );
1979 2175
2176 $self->set_value ($self->{range}[0]);
2177 $self->update;
2178
1980 $self 2179 $self
2180}
2181
2182sub changed { }
2183
2184sub set_range {
2185 my ($self, $range) = @_;
2186
2187 ($range, $self->{range}) = ($self->{range}, $range);
2188
2189 $self->update
2190 if "@$range" ne "@{$self->{range}}";
2191}
2192
2193sub set_value {
2194 my ($self, $value) = @_;
2195
2196 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2197
2198 $hi = $lo + 1 if $hi <= $lo;
2199
2200 $page = $hi - $lo if $page > $hi - $lo;
2201
2202 $value = $lo if $value < $lo;
2203 $value = $hi - $page if $value > $hi - $page;
2204
2205 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2206 if $unit;
2207
2208 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2209
2210 if ($value != $old_value) {
2211 $self->_emit (changed => $value);
2212 $self->update;
2213 }
1981} 2214}
1982 2215
1983sub size_request { 2216sub size_request {
1984 my ($self) = @_; 2217 my ($self) = @_;
1985 2218
1991 2224
1992sub button_down { 2225sub button_down {
1993 my ($self, $ev, $x, $y) = @_; 2226 my ($self, $ev, $x, $y) = @_;
1994 2227
1995 $self->SUPER::button_down ($ev, $x, $y); 2228 $self->SUPER::button_down ($ev, $x, $y);
2229
2230 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2231
1996 $self->mouse_motion ($ev, $x, $y); 2232 $self->mouse_motion ($ev, $x, $y);
1997} 2233}
1998 2234
1999sub mouse_motion { 2235sub mouse_motion {
2000 my ($self, $ev, $x, $y) = @_; 2236 my ($self, $ev, $x, $y) = @_;
2001 2237
2002 if ($GRAB == $self) { 2238 if ($GRAB == $self) {
2239 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2240
2241 my (undef, $lo, $hi, $page) = @{$self->{range}};
2242
2243 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2244
2245 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2246 }
2247}
2248
2249sub update {
2250 my ($self) = @_;
2251
2252 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2253 $self->set_value ($self->{range}[0]);
2254
2003 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2255 my ($value, $lo, $hi, $page) = @{$self->{range}};
2256 my $range = ($hi - $page - $lo) || 1e-100;
2004 2257
2005 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2258 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
2006 2259
2007 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2260 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2008 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 2261 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2009 2262
2010 $x -= $inner_pad_px; # substract the padding 2263 $value = ($value - $lo) / $range;
2011 $x = $x * ($hi - $lo) / $inner_w + $lo; 2264 $value = $value * $self->{scale} + $self->{offset};
2012 $x = $lo if $x < $lo;
2013 $x = $hi - $page if $x > $hi - $page;
2014 $self->{range}[0] = $x;
2015 2265
2016 $self->emit (changed => $x); 2266 $self->{knob_x} = $value - $knob_w * 0.5;
2017 $self->update; 2267 $self->{knob_w} = $knob_w;
2018 } 2268 });
2019}
2020 2269
2021# the inner_* stuff is for generating a padding for the slider handle, 2270 $self->SUPER::update;
2022# so that the handle doesn't leave the texture. This calculation isn't 100%
2023# correct propably, but it does the job for now
2024sub _calc_inner_pad_px {
2025 my ($self, $w) = @_;
2026 ($w / 100) * $self->{inner_pad} # % to pixels
2027} 2271}
2028 2272
2029sub _draw { 2273sub _draw {
2030 my ($self) = @_; 2274 my ($self) = @_;
2031 2275
2032 $self->SUPER::_draw (); 2276 $self->SUPER::_draw ();
2033 2277
2034 my ($w, $h) = @$self{qw(w h)}; 2278 glScale $self->{w}, $self->{h};
2035 2279
2036 if ($self->{vertical}) { 2280 if ($self->{vertical}) {
2037 # draw a vertical slider like a rotated horizontal slider 2281 # draw a vertical slider like a rotated horizontal slider
2038 2282
2283 glTranslate 1, 0, 0;
2039 glRotate 90, 0, 0, 1; 2284 glRotate 90, 0, 0, 1;
2040 glTranslate 0, -$self->{w}, 0;
2041
2042 ($w, $h) = ($h, $w);
2043 } 2285 }
2044 2286
2045 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2287 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2046 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2288 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2047 2289
2048 my ($value, $lo, $hi, $page) = @{$self->{range}};
2049
2050 $hi = $value + 1 if $lo == $hi;
2051
2052 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2053 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2054
2055 $page = int $page * $inner_w / ($hi - $lo);
2056 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2057
2058 $w -= $page;
2059 $page &= ~1;
2060 glTranslate $page * 0.5, 0, 0;
2061 $page ||= 2;
2062
2063 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2064 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2065
2066 glEnable GL_TEXTURE_2D; 2290 glEnable GL_TEXTURE_2D;
2067 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2291 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2068 2292
2069 # draw background 2293 # draw background
2070 $tex[1]->draw_quad_alpha (0, 0, $w, $h); 2294 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2071 2295
2072 # draw handle 2296 # draw handle
2073 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); 2297 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2074 2298
2075 glDisable GL_TEXTURE_2D; 2299 glDisable GL_TEXTURE_2D;
2076} 2300}
2301
2302#############################################################################
2303
2304package CFClient::UI::ValSlider;
2305
2306our @ISA = CFClient::UI::HBox::;
2307
2308sub new {
2309 my ($class, %arg) = @_;
2310
2311 my $range = delete $arg{range};
2312
2313 my $self = $class->SUPER::new (
2314 slider => (new CFClient::UI::Slider expand => 1, range => $range),
2315 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}),
2316 to_value => sub { shift },
2317 from_value => sub { shift },
2318 %arg,
2319 );
2320
2321 $self->{slider}->connect (changed => sub {
2322 my ($self, $value) = @_;
2323 $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value));
2324 $self->{parent}->emit (changed => $value);
2325 });
2326
2327# $self->{entry}->connect (changed => sub {
2328# my ($self, $value) = @_;
2329# $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value));
2330# $self->{parent}->emit (changed => $value);
2331# });
2332
2333 $self->add ($self->{slider}, $self->{entry});
2334
2335 $self->{slider}->emit (changed => $self->{slider}{range}[0]);
2336
2337 $self
2338}
2339
2340sub set_range { shift->{slider}->set_range (@_) }
2341sub set_value { shift->{slider}->set_value (@_) }
2077 2342
2078############################################################################# 2343#############################################################################
2079 2344
2080package CFClient::UI::TextView; 2345package CFClient::UI::TextView;
2081 2346
2111 2376
2112 $self->{fontsize} = $fontsize; 2377 $self->{fontsize} = $fontsize;
2113 $self->reflow; 2378 $self->reflow;
2114} 2379}
2115 2380
2116sub text_height {
2117 my ($self, $text) = @_;
2118
2119 my $layout = $self->{layout};
2120
2121 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2122 $layout->set_width ($self->{children}[0]{w});
2123 $layout->set_markup ($text);
2124
2125 ($layout->size)[1]
2126}
2127
2128sub reflow {
2129 my ($self) = @_;
2130
2131 $self->{need_reflow}++;
2132 $self->update;
2133}
2134
2135sub size_allocate { 2381sub size_allocate {
2136 my ($self, $w, $h) = @_; 2382 my ($self, $w, $h, $changed) = @_;
2137 2383
2138 $self->SUPER::size_allocate ($w, $h); 2384 $self->SUPER::size_allocate ($w, $h, $changed);
2385
2386 return unless $changed;
2139 2387
2140 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2388 $self->{layout}->set_font ($self->{font}) if $self->{font};
2141 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2389 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2142 $self->{layout}->set_width ($self->{children}[0]{w}); 2390 $self->{layout}->set_width ($self->{children}[0]{w});
2143 2391
2144 $self->reflow; 2392 $self->reflow;
2145} 2393}
2146 2394
2395sub text_size {
2396 my ($self, $text, $indent) = @_;
2397
2398 my $layout = $self->{layout};
2399
2400 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2401 $layout->set_width ($self->{children}[0]{w} - $indent);
2402 $layout->set_markup ($text);
2403
2404 $layout->size
2405}
2406
2407sub reflow {
2408 my ($self) = @_;
2409
2410 $self->{need_reflow}++;
2411 $self->update;
2412}
2413
2414sub set_offset {
2415 my ($self, $offset) = @_;
2416
2417 # todo: base offset on lines or so, not on pixels
2418 $self->{children}[1]->set_value ($offset);
2419}
2420
2421sub clear {
2422 my ($self) = @_;
2423
2424 $self->{par} = [];
2425 $self->{height} = 0;
2426 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2427}
2428
2147sub add_paragraph { 2429sub add_paragraph {
2148 my ($self, $color, $text) = @_; 2430 my ($self, $color, $text, $indent) = @_;
2149 2431
2150 #TODO: intelligently "reformat" paragraph 2432 for my $line (split /\n/, $text) {
2151 2433 my ($w, $h) = $self->text_size ($line);
2152 my $height = $self->text_height ($text);
2153
2154 $self->{height} += $height; 2434 $self->{height} += $h;
2435 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line];
2436 }
2155 2437
2156 push @{$self->{par}}, [$height, $color, $text];
2157
2158 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; 2438 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]);
2159 $self->{children}[1]->update;
2160} 2439}
2161 2440
2162sub update { 2441sub update {
2163 my ($self) = @_; 2442 my ($self) = @_;
2164 2443
2167 return unless $self->{h} > 0; 2446 return unless $self->{h} > 0;
2168 2447
2169 delete $self->{texture}; 2448 delete $self->{texture};
2170 2449
2171 $ROOT->on_post_alloc ($self, sub { 2450 $ROOT->on_post_alloc ($self, sub {
2451 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2452
2172 if (delete $self->{need_reflow}) { 2453 if (delete $self->{need_reflow}) {
2173 my $height = 0; 2454 my $height = 0;
2174 2455
2175 $height += $_->[0] = $self->text_height ($_->[2]) 2456 my $layout = $self->{layout};
2457
2458 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2459
2176 for @{$self->{par}}; 2460 for (@{$self->{par}}) {
2461 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2462 $layout->set_width ($W - $_->[3]);
2463 $layout->set_markup ($_->[4]);
2464 my ($w, $h) = $layout->size;
2465 $_->[0] = $w + $_->[3];
2466 $_->[1] = $h;
2467 }
2468
2469 $height += $_->[1];
2470 }
2177 2471
2178 $self->{height} = $height; 2472 $self->{height} = $height;
2179 2473
2180 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; 2474 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2181 2475
2182 delete $self->{texture}; 2476 delete $self->{texture};
2183 } 2477 }
2184 2478
2185 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2479 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2186 glClearColor 0, 0, 0, 0; 2480 glClearColor 0.5, 0.5, 0.5, 0;
2187 glClear GL_COLOR_BUFFER_BIT; 2481 glClear GL_COLOR_BUFFER_BIT;
2188 2482
2189 glEnable GL_TEXTURE_2D;
2190 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2191
2192 my $top = int $self->{children}[1]{range}[0]; 2483 my $top = int $self->{children}[1]{range}[0];
2193 2484
2194 my $y0 = $top; 2485 my $y0 = $top;
2195 my $y1 = $top + $self->{h}; 2486 my $y1 = $top + $H;
2196 2487
2197 my $y = 0; 2488 my $y = 0;
2198 2489
2199 my $layout = $self->{layout}; 2490 my $layout = $self->{layout};
2200 2491
2201 $layout->set_font ($self->{font}) if $self->{font}; 2492 $layout->set_font ($self->{font}) if $self->{font};
2202 2493
2494 glEnable GL_BLEND;
2495 #TODO# not correct in windows where rgba is forced off
2496 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2497
2203 for my $par (@{$self->{par}}) { 2498 for my $par (@{$self->{par}}) {
2204 my $h = $par->[0]; 2499 my $h = $par->[1];
2205 2500
2206 if ($y0 < $y + $h && $y < $y1) { 2501 if ($y0 < $y + $h && $y < $y1) {
2207 $layout->set_foreground (@{ $par->[1] }); 2502 $layout->set_foreground (@{ $par->[2] });
2503 $layout->set_width ($W - $par->[3]);
2208 $layout->set_markup ($par->[2]); 2504 $layout->set_markup ($par->[4]);
2209 2505
2210 my ($W, $H) = $layout->size; 2506 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2211 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); 2507
2508 glRasterPos $par->[3], $y - $y0;
2509 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2212 } 2510 }
2213 2511
2214 $y += $h; 2512 $y += $h;
2215 } 2513 }
2216 2514
2217 glDisable GL_TEXTURE_2D; 2515 glDisable GL_BLEND;
2218 }; 2516 };
2219 }); 2517 });
2220} 2518}
2221 2519
2222sub _draw { 2520sub _draw {
2223 my ($self) = @_; 2521 my ($self) = @_;
2224 2522
2225 glEnable GL_TEXTURE_2D; 2523 glEnable GL_TEXTURE_2D;
2226 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2524 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2227 glColor 1, 1, 1, 1; 2525 glColor 1, 1, 1, 1;
2228 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2526 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2229 glDisable GL_TEXTURE_2D; 2527 glDisable GL_TEXTURE_2D;
2230 2528
2231 $self->{children}[1]->draw; 2529 $self->{children}[1]->draw;
2232 2530
2233} 2531}
2282 2580
2283sub new { 2581sub new {
2284 my $class = shift; 2582 my $class = shift;
2285 2583
2286 my $self = $class->SUPER::new ( 2584 my $self = $class->SUPER::new (
2287 state => 0, 2585 state => 0,
2288 connect_activate => \&toggle_flopper, 2586 on_activate => \&toggle_flopper,
2289 @_ 2587 @_
2290 ); 2588 );
2291 2589
2292 if ($self->{state}) {
2293 $self->{state} = 0;
2294 $self->toggle_flopper;
2295 }
2296
2297 $self 2590 $self
2298} 2591}
2299 2592
2300sub toggle_flopper { 2593sub toggle_flopper {
2301 my ($self) = @_; 2594 my ($self) = @_;
2302 2595
2303 # TODO: use animation 2596 $self->{other}->toggle_visibility;
2304 if ($self->{state} = !$self->{state}) {
2305 $CFClient::UI::ROOT->add ($self->{other});
2306 $self->{other}->move ($self->coord2global (0, $self->{h}));
2307 $self->emit ("open");
2308 } else {
2309 $CFClient::UI::ROOT->remove ($self->{other});
2310 $self->emit ("close");
2311 }
2312
2313 $self->emit (changed => $self->{state});
2314} 2597}
2315 2598
2316############################################################################# 2599#############################################################################
2317 2600
2318package CFClient::UI::Tooltip; 2601package CFClient::UI::Tooltip;
2332 2615
2333sub set_tooltip_from { 2616sub set_tooltip_from {
2334 my ($self, $widget) = @_; 2617 my ($self, $widget) = @_;
2335 2618
2336 $self->add (new CFClient::UI::Label 2619 $self->add (new CFClient::UI::Label
2337 markup => $widget->{tooltip}, 2620 markup => $widget->{tooltip},
2338 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2621 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2339 fontsize => 0.8, 2622 fontsize => 0.8,
2340 fg => [0, 0, 0, 1], 2623 fg => [0, 0, 0, 1],
2624 ellipsise => 0,
2341 font => ($widget->{tooltip_font} || $::FONT_PROP), 2625 font => ($widget->{tooltip_font} || $::FONT_PROP),
2342 ); 2626 );
2343} 2627}
2344 2628
2345sub size_request { 2629sub size_request {
2346 my ($self) = @_; 2630 my ($self) = @_;
2349 2633
2350 ($w + 4, $h + 4) 2634 ($w + 4, $h + 4)
2351} 2635}
2352 2636
2353sub size_allocate { 2637sub size_allocate {
2354 my ($self, $w, $h) = @_; 2638 my ($self, $w, $h, $changed) = @_;
2355 2639
2640 return unless $changed;
2641
2356 $self->SUPER::size_allocate ($w - 4, $h - 4); 2642 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2357} 2643}
2358 2644
2359sub _draw { 2645sub _draw {
2360 my ($self) = @_; 2646 my ($self) = @_;
2361 2647
2392use CFClient::OpenGL; 2678use CFClient::OpenGL;
2393 2679
2394sub new { 2680sub new {
2395 my $class = shift; 2681 my $class = shift;
2396 2682
2397 $class->SUPER::new ( 2683 my $self = $class->SUPER::new (
2398 aspect => 1, 2684 aspect => 1,
2685 can_events => 0,
2399 @_, 2686 @_,
2400 ) 2687 );
2688
2689 if ($self->{anim} && $self->{animspeed}) {
2690 Scalar::Util::weaken (my $widget = $self);
2691
2692 $self->{timer} = Event->timer (
2693 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2694 hard => 1,
2695 interval => $self->{animspeed},
2696 cb => sub {
2697 ++$widget->{frame};
2698 $widget->update;
2699 },
2700 );
2701 }
2702
2703 $self
2401} 2704}
2402 2705
2403sub size_request { 2706sub size_request {
2404 (32, 8) 2707 (32, 8)
2405} 2708}
2406 2709
2710sub update {
2711 my ($self) = @_;
2712
2713 return unless $self->{visible};
2714
2715 $self->SUPER::update;
2716}
2717
2407sub _draw { 2718sub _draw {
2408 my ($self) = @_; 2719 my ($self) = @_;
2409 2720
2410 return unless $::CONN;#d# manage and cache textures differently 2721 return unless $::CONN;
2722
2723 my $face;
2724
2725 if ($self->{frame}) {
2726 my $anim = $::CONN->{anim}[$self->{anim}];
2727
2728 $face = $anim->[ $self->{frame} % @$anim ]
2729 if $anim && @$anim;
2730 }
2731
2411 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2732 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2412 2733
2413 # TODO animation
2414 if ($tex) { 2734 if ($tex) {
2415 glEnable GL_TEXTURE_2D; 2735 glEnable GL_TEXTURE_2D;
2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2736 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2417 glColor 1, 1, 1, 1; 2737 glColor 1, 1, 1, 1;
2418 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2738 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2419 glDisable GL_TEXTURE_2D; 2739 glDisable GL_TEXTURE_2D;
2420 } 2740 }
2421} 2741}
2422 2742
2423############################################################################# 2743sub DESTROY {
2744 my ($self) = @_;
2424 2745
2746 $self->{timer}->cancel
2747 if $self->{timer};
2748
2749 $self->SUPER::DESTROY;
2750}
2751
2752#############################################################################
2753
2425package CFClient::UI::InventoryItem; 2754package CFClient::UI::Inventory;
2426 2755
2427our @ISA = CFClient::UI::HBox::; 2756our @ISA = CFClient::UI::ScrolledWindow::;
2428 2757
2429sub new { 2758sub new {
2430 my $class = shift; 2759 my $class = shift;
2431 2760
2432 my %args = @_;
2433
2434 my $item = delete $args{item};
2435
2436 my $desc = $item->{nrof} < 2
2437 ? $item->{name}
2438 : "$item->{nrof} $item->{name_pl}";
2439
2440
2441 my $self = $class->SUPER::new ( 2761 my $self = $class->SUPER::new (
2442 can_hover => 1, 2762 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2443 can_events => 1,
2444 tooltip => (CFClient::UI::Label->escape ($desc)
2445 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2446 connect_button_down => sub {
2447 my ($self, $ev, $x, $y) = @_;
2448
2449 # todo: maybe put examine on 1? but should just be a tooltip :(
2450 if ($ev->{button} == 1) {
2451 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2452 } elsif ($ev->{button} == 2) {
2453 $::CONN->send ("apply $item->{tag}");
2454 } elsif ($ev->{button} == 3) {
2455 CFClient::UI::Menu->new (
2456 items => [
2457 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2458 [
2459 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2460 sub { $::CONN->send ("lock $item->{tag}") },
2461 ],
2462 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2463 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2464 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2465 ],
2466 )->popup ($ev);
2467 }
2468
2469 1
2470 },
2471 %args
2472 );
2473
2474 $self->add (new CFClient::UI::Face
2475 can_events => 0,
2476 face => $item->{face},
2477 anim => $item->{anim},
2478 animspeed => $item->{animspeed},
2479 );
2480
2481 $self->add (new CFClient::UI::Label
2482 can_events => 0,
2483 text => $desc,
2484 );
2485
2486 $self
2487}
2488
2489#############################################################################
2490
2491package CFClient::UI::Inventory;
2492
2493our @ISA = CFClient::UI::ScrolledWindow::;
2494
2495sub new {
2496 my $class = shift;
2497
2498 my $self = $class->SUPER::new (
2499 scrolled => (new CFClient::UI::VBox),
2500 @_, 2763 @_,
2501 ); 2764 );
2502 2765
2503 $self 2766 $self
2504} 2767}
2507 my ($self, $items) = @_; 2770 my ($self, $items) = @_;
2508 2771
2509 $self->{scrolled}->clear; 2772 $self->{scrolled}->clear;
2510 return unless $items; 2773 return unless $items;
2511 2774
2512 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2775 my @items = sort {
2776 ($a->{type} <=> $b->{type})
2777 or ($a->{name} cmp $b->{name})
2778 } @$items;
2513 2779
2514 $self->{real_items} = \@items; 2780 $self->{real_items} = \@items;
2515 2781
2782 my $row = 0;
2516 for my $item (@items) { 2783 for my $item (@items) {
2517 my $desc = $item->{nrof} < 2 2784 CFClient::Item::update_widgets $item;
2518 ? $item->{name}
2519 : "$item->{nrof} $item->{name_pl}";
2520 2785
2521 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); 2786 $self->{scrolled}->add (0, $row, $item->{face_widget});
2522 } 2787 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2788 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2523 2789
2524# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2790 $row++;
2525} 2791 }
2526
2527sub size_request {
2528 my ($self) = @_;
2529 ($self->{req_w}, $self->{req_h});
2530} 2792}
2531 2793
2532############################################################################# 2794#############################################################################
2533 2795
2534package CFClient::UI::Menu; 2796package CFClient::UI::Menu;
2569 2831
2570# popup given the event (must be a mouse button down event currently) 2832# popup given the event (must be a mouse button down event currently)
2571sub popup { 2833sub popup {
2572 my ($self, $ev) = @_; 2834 my ($self, $ev) = @_;
2573 2835
2574 $self->emit ("popdown"); 2836 $self->_emit ("popdown");
2575 2837
2576 # maybe save $GRAB? must be careful about events... 2838 # maybe save $GRAB? must be careful about events...
2577 $GRAB = $self; 2839 $GRAB = $self;
2578 $self->{button} = $ev->{button}; 2840 $self->{button} = $ev->{button};
2579 2841
2594 2856
2595 if ($ev->{button} == $self->{button}) { 2857 if ($ev->{button} == $self->{button}) {
2596 undef $GRAB; 2858 undef $GRAB;
2597 $self->hide; 2859 $self->hide;
2598 2860
2599 $self->emit ("popdown"); 2861 $self->_emit ("popdown");
2600 $self->{hover}[1]->() if $self->{hover}; 2862 $self->{hover}[1]->() if $self->{hover};
2601 } 2863 }
2602} 2864}
2603 2865
2604############################################################################# 2866#############################################################################
2605 2867
2606package CFClient::UI::Statusbox; 2868package CFClient::UI::Statusbox;
2607 2869
2608our @ISA = CFClient::UI::VBox::; 2870our @ISA = CFClient::UI::VBox::;
2871
2872sub new {
2873 my $class = shift;
2874
2875 $class->SUPER::new (
2876 fontsize => 0.8,
2877 @_,
2878 )
2879}
2609 2880
2610sub reorder { 2881sub reorder {
2611 my ($self) = @_; 2882 my ($self) = @_;
2612 my $NOW = time; 2883 my $NOW = time;
2613 2884
2632 ? "<b>$item->{count} ×</b> $item->{text}" 2903 ? "<b>$item->{count} ×</b> $item->{text}"
2633 : $item->{text}; 2904 : $item->{text};
2634 2905
2635 for ($short) { 2906 for ($short) {
2636 s/^\s+//; 2907 s/^\s+//;
2637 s/\012.*/…/s; 2908 s/\s+/ /g;
2638 my $len = int 40 / $item->{fontsize};
2639 substr $_, $len, length, "…" if $len < length;
2640 } 2909 }
2641 2910
2642 new CFClient::UI::Label 2911 new CFClient::UI::Label
2643 markup => $short, 2912 markup => $short,
2644 tooltip => $item->{tooltip}, 2913 tooltip => $item->{tooltip},
2645 tooltip_font => $::FONT_PROP, 2914 tooltip_font => $::FONT_PROP,
2646 tooltip_width => 0.67, 2915 tooltip_width => 0.67,
2647 fontsize => $item->{fontsize}, 2916 fontsize => $item->{fontsize} || $self->{fontsize},
2917 max_w => $::WIDTH * 0.44,
2648 color => $item->{color}, 2918 fg => $item->{fg},
2649 can_events => 1, 2919 can_events => 1,
2650 can_hover => 1 2920 can_hover => 1
2651 }; 2921 };
2652 } 2922 }
2653 2923
2658sub add { 2928sub add {
2659 my ($self, $text, %arg) = @_; 2929 my ($self, $text, %arg) = @_;
2660 2930
2661 $text =~ s/^\s+//; 2931 $text =~ s/^\s+//;
2662 $text =~ s/\s+$//; 2932 $text =~ s/\s+$//;
2933
2934 return unless $text;
2663 2935
2664 my $timeout = time + ((delete $arg{timeout}) || 60); 2936 my $timeout = time + ((delete $arg{timeout}) || 60);
2665 2937
2666 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2938 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2667 2939
2679 $self->{item}{$group} = { 2951 $self->{item}{$group} = {
2680 id => ++$self->{id}, 2952 id => ++$self->{id},
2681 text => $text, 2953 text => $text,
2682 timeout => $timeout, 2954 timeout => $timeout,
2683 tooltip => $text, 2955 tooltip => $text,
2684 fontsize => 0.8,
2685 color => [0.8, 0.8, 0.8, 0.8], 2956 fg => [0.8, 0.8, 0.8, 0.8],
2686 pri => 0, 2957 pri => 0,
2687 count => 1, 2958 count => 1,
2688 %arg, 2959 %arg,
2689 }; 2960 };
2690 } 2961 }
2691 2962
2692 $self->reorder; 2963 $self->reorder;
2693} 2964}
2694 2965
2966sub reconfigure {
2967 my ($self) = @_;
2968
2969 delete $_->{label}
2970 for values %{ $self->{item} || {} };
2971
2972 $self->reorder;
2973 $self->SUPER::reconfigure;
2974}
2975
2695############################################################################# 2976#############################################################################
2696 2977
2697package CFClient::UI::Root; 2978package CFClient::UI::Root;
2698 2979
2699our @ISA = CFClient::UI::Container::; 2980our @ISA = CFClient::UI::Container::;
2701use CFClient::OpenGL; 2982use CFClient::OpenGL;
2702 2983
2703sub new { 2984sub new {
2704 my $class = shift; 2985 my $class = shift;
2705 2986
2706 $class->SUPER::new ( 2987 my $self = $class->SUPER::new (
2988 visible => 1,
2707 @_, 2989 @_,
2708 ) 2990 );
2991
2992 Scalar::Util::weaken ($self->{root} = $self);
2993
2994 $self
2709} 2995}
2710 2996
2711sub configure { 2997sub configure {
2712 my ($self, $x, $y, $w, $h) = @_; 2998 my ($self, $x, $y, $w, $h) = @_;
2713 2999
2714 $self->{w} = $w; 3000 $self->{w} = $w;
2715 $self->{h} = $h; 3001 $self->{h} = $h;
2716} 3002}
2717 3003
2718sub check_size { 3004sub reconfigure {
2719 my ($self) = @_; 3005 my ($self) = @_;
2720 3006
3007 $self->SUPER::reconfigure;
3008
2721 $self->size_allocate ($self->{w}, $self->{h}) 3009 $self->size_allocate ($self->{w}, $self->{h}, 1)
2722 if $self->{w}; 3010 if $self->{w};
2723} 3011}
2724 3012
2725sub size_request { 3013sub size_request {
2726 my ($self) = @_; 3014 my ($self) = @_;
2727 3015
2728 ($self->{w}, $self->{h}) 3016 ($self->{w}, $self->{h})
2729} 3017}
2730 3018
2731sub size_allocate { 3019sub size_allocate {
2732 my ($self, $w, $h) = @_; 3020 my ($self, $w, $h, $changed) = @_;
2733
2734 my $old_w = $self->{old_w};
2735 my $old_h = $self->{old_h};
2736
2737 if ($old_w && $old_h) {
2738 for my $child ($self->children) {
2739 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2740 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2741 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2742 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2743 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2744 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2745 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2746 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2747 }
2748 }
2749 3021
2750 for my $child ($self->children) { 3022 for my $child ($self->children) {
2751 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3023 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2752 3024
3025 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3026 if exists $child->{req_x};
3027
3028 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3029 if exists $child->{req_y};
3030
3031 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3032
2753 $X = List::Util::max 0, List::Util::min $w - $W, $X; 3033 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2754 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 3034 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3035
2755 $child->configure ($X, $Y, $W, $H); 3036 $child->configure ($X, $Y, $W, $H);
2756 } 3037 }
2757
2758 $self->{old_w} = $w;
2759 $self->{old_h} = $h;
2760} 3038}
2761 3039
2762sub coord2local { 3040sub coord2local {
2763 my ($self, $x, $y) = @_; 3041 my ($self, $x, $y) = @_;
2764 3042
2772} 3050}
2773 3051
2774sub update { 3052sub update {
2775 my ($self) = @_; 3053 my ($self) = @_;
2776 3054
2777 $self->check_size;
2778 $::WANT_REFRESH++; 3055 $::WANT_REFRESH++;
2779} 3056}
2780 3057
2781sub add { 3058sub add {
2782 my ($self, $child) = @_; 3059 my ($self, @children) = @_;
2783 3060
3061 for my $child (@children) {
3062 $child->{is_toplevel} = 1;
3063
2784 # integerise window positions 3064 # integerise window positions
2785 $child->{x} = int $child->{x}; 3065 $child->{x} = int $child->{x};
2786 $child->{y} = int $child->{y}; 3066 $child->{y} = int $child->{y};
3067 }
2787 3068
2788 $self->SUPER::add ($child); 3069 $self->SUPER::add (@children);
3070}
3071
3072sub remove {
3073 my ($self, @children) = @_;
3074
3075 $self->SUPER::remove (@children);
3076
3077 while (@children) {
3078 my $w = pop @children;
3079 push @children, $w->children;
3080 $w->set_invisible;
3081 }
2789} 3082}
2790 3083
2791sub on_refresh { 3084sub on_refresh {
2792 my ($self, $id, $cb) = @_; 3085 my ($self, $id, $cb) = @_;
2793 3086
2806 while ($self->{refresh_hook}) { 3099 while ($self->{refresh_hook}) {
2807 $_->() 3100 $_->()
2808 for values %{delete $self->{refresh_hook}}; 3101 for values %{delete $self->{refresh_hook}};
2809 } 3102 }
2810 3103
2811 if ($self->{check_size}) { 3104 if ($self->{realloc}) {
2812 my @queue = ([], []); 3105 my @queue;
2813 3106
2814 for (;;) { 3107 while () {
2815 if ($self->{check_size}) { 3108 if ($self->{realloc}) {
2816 # heuristic: check containers last 3109 #TODO use array-of-depth approach
2817 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3110
2818 for values %{delete $self->{check_size}} 3111 @queue = sort { $a->{visible} <=> $b->{visible} }
3112 @queue, values %{delete $self->{realloc}};
2819 } 3113 }
2820 3114
2821 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3115 my $widget = pop @queue || last;
2822 3116
3117 $widget->{visible} or last; # do not resize invisible widgets
3118
2823 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3119 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
2824 ? @$widget{qw(user_w user_h)} 3120 ? @$widget{qw(def_w def_h)}
2825 : $widget->size_request; 3121 : $widget->size_request;
2826 3122
2827 if (delete $widget->{force_alloc}
2828 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2829 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3123 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2830 3124
2831 $widget->{req_w} = $w; 3125 $widget->{req_w} = $w;
2832 $widget->{req_h} = $h; 3126 $widget->{req_h} = $h;
2833 3127
2834 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3128 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}];
2835 3129
2836 $widget->{parent}->check_size 3130 push @queue, $widget->{parent}
2837 if $widget->{parent}; 3131 if $widget->{parent};
2838 }
2839 } 3132 }
2840 } 3133 }
2841 3134
2842 while ($self->{size_alloc}) { 3135 while (my $size_alloc = delete $self->{size_alloc}) {
2843 for (values %{delete $self->{size_alloc}}) { 3136 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
2844 my ($widget, $w, $h) = @$_; 3137 values %$size_alloc;
3138
3139 while () {
3140 my ($widget, $w, $h) = @{ pop @queue or last };
2845 3141
2846 $w = 0 if $w < 0; 3142 $w = 0 if $w < 0;
2847 $h = 0 if $h < 0; 3143 $h = 0 if $h < 0;
2848 3144
2849 $widget->{w} = $w; 3145 $widget->{w} = $w;
2850 $widget->{h} = $h; 3146 $widget->{h} = $h;
2851 $widget->size_allocate ($w, $h);
2852 $widget->emit (size_allocate => $w, $h); 3147 $widget->emit (size_allocate => $w, $h, 1);
2853 } 3148 }
2854 } 3149 }
2855 3150
2856 while ($self->{post_alloc_hook}) { 3151 while ($self->{post_alloc_hook}) {
2857 $_->() 3152 $_->()
2862 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 3157 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2863 glClear GL_COLOR_BUFFER_BIT; 3158 glClear GL_COLOR_BUFFER_BIT;
2864 3159
2865 glMatrixMode GL_PROJECTION; 3160 glMatrixMode GL_PROJECTION;
2866 glLoadIdentity; 3161 glLoadIdentity;
2867 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; 3162 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
2868 glMatrixMode GL_MODELVIEW; 3163 glMatrixMode GL_MODELVIEW;
2869 glLoadIdentity; 3164 glLoadIdentity;
2870 3165
2871 $self->_draw; 3166 $self->_draw;
2872} 3167}
2874############################################################################# 3169#############################################################################
2875 3170
2876package CFClient::UI; 3171package CFClient::UI;
2877 3172
2878$ROOT = new CFClient::UI::Root; 3173$ROOT = new CFClient::UI::Root;
2879$TOOLTIP = new CFClient::UI::Tooltip; 3174$TOOLTIP = new CFClient::UI::Tooltip z => 900;
2880 3175
28811 31761
2882 3177

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines