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.263 by elmex, Wed May 31 14:04:52 2006 UTC vs.
Revision 1.310 by root, Fri Jun 23 20:28:20 2006 UTC

3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8use Event;
8 9
9use CFClient; 10use CFClient;
10use CFClient::Texture; 11use CFClient::Texture;
11 12
12our ($FOCUS, $HOVER, $GRAB); # various widgets 13our ($FOCUS, $HOVER, $GRAB); # various widgets
15our $ROOT; 16our $ROOT;
16our $TOOLTIP; 17our $TOOLTIP;
17our $BUTTON_STATE; 18our $BUTTON_STATE;
18 19
19our %WIDGET; # all widgets, weak-referenced 20our %WIDGET; # all widgets, weak-referenced
21
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub {
23 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->hide;
28
29 $TOOLTIP->{owner} = $widget;
30
31 return if $ENV{CFPLUS_DEBUG} & 8;
32
33 my $tip = $widget->{tooltip};
34
35 $tip = $tip->($widget) if CODE:: eq ref $tip;
36
37 $TOOLTIP->set_tooltip_from ($widget);
38 $TOOLTIP->show;
39 }
40
41 return;
42 }
43 }
44 }
45
46 $TOOLTIP->hide;
47 delete $TOOLTIP->{owner};
48});
20 49
21sub get_layout { 50sub get_layout {
22 my $layout; 51 my $layout;
23 52
24 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
39 my ($layout) = @_; 68 my ($layout) = @_;
40 69
41 $LAYOUT = $layout; 70 $LAYOUT = $layout;
42} 71}
43 72
44sub check_tooltip {
45 if (!$GRAB) {
46 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
47 if (length $widget->{tooltip}) {
48
49 if ($TOOLTIP->{owner} != $widget) {
50 $TOOLTIP->hide;
51
52 $TOOLTIP->{owner} = $widget;
53
54 my $tip = $widget->{tooltip};
55
56 $tip = $tip->($widget) if CODE:: eq ref $tip;
57
58 $TOOLTIP->set_tooltip_from ($widget);
59 $TOOLTIP->show;
60 }
61
62 return;
63 }
64 }
65 }
66
67 $TOOLTIP->hide;
68 delete $TOOLTIP->{owner};
69}
70
71# class methods for events 73# class methods for events
72sub feed_sdl_key_down_event { 74sub feed_sdl_key_down_event {
73 $FOCUS->emit (key_down => $_[0]) 75 $FOCUS->emit (key_down => $_[0])
74 if $FOCUS; 76 if $FOCUS;
75} 77}
87 my $widget = $ROOT->find_widget ($x, $y); 89 my $widget = $ROOT->find_widget ($x, $y);
88 90
89 $GRAB = $widget; 91 $GRAB = $widget;
90 $GRAB->update if $GRAB; 92 $GRAB->update if $GRAB;
91 93
92 check_tooltip; 94 $TOOLTIP_WATCHER->cb->();
93 } 95 }
94 96
95 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 97 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
96 98
97 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) 99 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
112 if (!$BUTTON_STATE) { 114 if (!$BUTTON_STATE) {
113 my $grab = $GRAB; undef $GRAB; 115 my $grab = $GRAB; undef $GRAB;
114 $grab->update if $grab; 116 $grab->update if $grab;
115 $GRAB->update if $GRAB; 117 $GRAB->update if $GRAB;
116 118
117 check_tooltip; 119 $TOOLTIP_WATCHER->cb->();
118 } 120 }
119} 121}
120 122
121sub feed_sdl_motion_event { 123sub feed_sdl_motion_event {
122 my ($ev) = @_; 124 my ($ev) = @_;
128 my $hover = $HOVER; $HOVER = $widget; 130 my $hover = $HOVER; $HOVER = $widget;
129 131
130 $hover->update if $hover && $hover->{can_hover}; 132 $hover->update if $hover && $hover->{can_hover};
131 $HOVER->update if $HOVER && $HOVER->{can_hover}; 133 $HOVER->update if $HOVER && $HOVER->{can_hover};
132 134
133 check_tooltip; 135 $TOOLTIP_WATCHER->start;
134 } 136 }
135 137
136 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) 138 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
137 if $HOVER; 139 if $HOVER;
138} 140}
170sub rescale_widgets { 172sub rescale_widgets {
171 my ($sx, $sy) = @_; 173 my ($sx, $sy) = @_;
172 174
173 for my $widget (values %WIDGET) { 175 for my $widget (values %WIDGET) {
174 if ($widget->{is_toplevel}) { 176 if ($widget->{is_toplevel}) {
175 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 177 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
176 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 178 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
177 179
178 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 180 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
179 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 181 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
180 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w}; 182 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
181 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/; 183 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
182 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 184 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
183 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h}; 185 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
184 186
185 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 187 $widget->{x} -= int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
186 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 188 $widget->{y} -= int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
187 189
188 } 190 }
189 } 191 }
190 192
191 reconfigure_widgets; 193 reconfigure_widgets;
270 272
271 return unless $self->{visible}; 273 return unless $self->{visible};
272 274
273 $_->set_invisible for $self->children; 275 $_->set_invisible for $self->children;
274 276
277 delete $self->{visible};
275 delete $self->{root}; 278 delete $self->{root};
276 delete $self->{visible};
277 279
278 undef $GRAB if $GRAB == $self; 280 undef $GRAB if $GRAB == $self;
279 undef $HOVER if $HOVER == $self; 281 undef $HOVER if $HOVER == $self;
280 282
281 CFClient::UI::check_tooltip 283 $CFClient::UI::TOOLTIP_WATCHER->cb->()
282 if $TOOLTIP->{owner} == $self; 284 if $TOOLTIP->{owner} == $self;
283 285
284 $self->focus_out; 286 $self->emit ("focus_out");
285
286 $self->emit (visibility_change => 0); 287 $self->emit (visibility_change => 0);
287} 288}
288 289
289sub set_visibility { 290sub set_visibility {
290 my ($self, $visible) = @_; 291 my ($self, $visible) = @_;
313} 314}
314 315
315sub move_abs { 316sub move_abs {
316 my ($self, $x, $y, $z) = @_; 317 my ($self, $x, $y, $z) = @_;
317 318
318 $self->{x} = List::Util::max 0, int $x; 319 $self->{x} = List::Util::max 0, List::Util::min $self->{root}{w} - $self->{w}, int $x;
319 $self->{y} = List::Util::max 0, int $y; 320 $self->{y} = List::Util::max 0, List::Util::min $self->{root}{h} - $self->{h}, int $y;
320 $self->{z} = $z if defined $z; 321 $self->{z} = $z if defined $z;
321 322
322 $self->update; 323 $self->update;
323} 324}
324 325
365 366
366 $self->{root}{size_alloc}{$self+0} = $self; 367 $self->{root}{size_alloc}{$self+0} = $self;
367 } 368 }
368} 369}
369 370
370sub size_allocate {
371 # nothing to be done
372}
373
374sub children { 371sub children {
372 # nop
373}
374
375sub visible_children {
376 $_[0]->children
375} 377}
376 378
377sub set_max_size { 379sub set_max_size {
378 my ($self, $w, $h) = @_; 380 my ($self, $w, $h) = @_;
379 381
380 delete $self->{max_w}; $self->{max_w} = $w if $w; 382 $self->{max_w} = int $w if defined $w;
381 delete $self->{max_h}; $self->{max_h} = $h if $h; 383 $self->{max_h} = int $h if defined $h;
384
385 $self->realloc;
382} 386}
383 387
384sub set_tooltip { 388sub set_tooltip {
385 my ($self, $tooltip) = @_; 389 my ($self, $tooltip) = @_;
386 390
391 395
392 $self->{tooltip} = $tooltip; 396 $self->{tooltip} = $tooltip;
393 397
394 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 398 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
395 delete $CFClient::UI::TOOLTIP->{owner}; 399 delete $CFClient::UI::TOOLTIP->{owner};
396 CFClient::UI::check_tooltip; 400 $CFClient::UI::TOOLTIP_WATCHER->cb->();
397 } 401 }
398} 402}
399 403
400# translate global coordinates to local coordinate system 404# translate global coordinates to local coordinate system
401sub coord2local { 405sub coord2local {
409 my ($self, $x, $y) = @_; 413 my ($self, $x, $y) = @_;
410 414
411 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 415 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
412} 416}
413 417
414sub focus_in { 418sub invoke_focus_in {
415 my ($self) = @_; 419 my ($self) = @_;
416 420
417 return if $FOCUS == $self; 421 return if $FOCUS == $self;
418 return unless $self->{can_focus}; 422 return unless $self->{can_focus};
419 423
420 my $focus = $FOCUS; $FOCUS = $self; 424 my $focus = $FOCUS; $FOCUS = $self;
421 425
422 $self->_emit (focus_in => $focus);
423
424 $focus->update if $focus; 426 $focus->update if $focus;
425 $FOCUS->update; 427 $FOCUS->update;
426}
427 428
429 0
430}
431
428sub focus_out { 432sub invoke_focus_out {
429 my ($self) = @_; 433 my ($self) = @_;
430 434
431 return unless $FOCUS == $self; 435 return unless $FOCUS == $self;
432 436
433 my $focus = $FOCUS; undef $FOCUS; 437 my $focus = $FOCUS; undef $FOCUS;
434 438
435 $self->_emit (focus_out => $focus);
436
437 $focus->update if $focus; #? 439 $focus->update if $focus; #?
438 440
439 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus 441 $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus
440 unless $FOCUS; 442 unless $FOCUS;
441}
442 443
444 0
445}
446
447sub grab_focus {
448 my ($self) = @_;
449
450 $self->emit ("focus_in");
451}
452
443sub mouse_motion { } 453sub invoke_mouse_motion { 1 }
444sub button_up { } 454sub invoke_button_up { 1 }
445sub key_down { } 455sub invoke_key_down { 1 }
446sub key_up { } 456sub invoke_key_up { 1 }
447 457
448sub button_down { 458sub invoke_button_down {
449 my ($self, $ev, $x, $y) = @_; 459 my ($self, $ev, $x, $y) = @_;
450 460
451 $self->focus_in; 461 $self->grab_focus;
452}
453 462
454sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 463 1
455sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 464}
456sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 465
457sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 466sub connect {
458sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 467 my ($self, $signal, $cb) = @_;
468
469 push @{ $self->{signal_cb}{$signal} }, $cb;
470}
471
472sub emit {
473 my ($self, $signal, @args) = @_;
474
475 #d##TODO# stop propagating at first true, do not use sum
476 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before
477 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure
478 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
479}
459 480
460sub find_widget { 481sub find_widget {
461 my ($self, $x, $y) = @_; 482 my ($self, $x, $y) = @_;
462 483
463 return () unless $self->{can_events}; 484 return () unless $self->{can_events};
472sub set_parent { 493sub set_parent {
473 my ($self, $parent) = @_; 494 my ($self, $parent) = @_;
474 495
475 Scalar::Util::weaken ($self->{parent} = $parent); 496 Scalar::Util::weaken ($self->{parent} = $parent);
476 $self->set_visible if $parent->{visible}; 497 $self->set_visible if $parent->{visible};
477}
478
479sub connect {
480 my ($self, $signal, $cb) = @_;
481
482 push @{ $self->{signal_cb}{$signal} }, $cb;
483}
484
485sub _emit {
486 my ($self, $signal, @args) = @_;
487
488 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
489}
490
491sub emit {
492 my ($self, $signal, @args) = @_;
493
494 $self->_emit ($signal, @args)
495 || $self->$signal (@args);
496}
497
498sub visibility_change {
499 #my ($self, $visible) = @_;
500} 498}
501 499
502sub realloc { 500sub realloc {
503 my ($self) = @_; 501 my ($self) = @_;
504 502
525 523
526 $self->realloc; 524 $self->realloc;
527 $self->update; 525 $self->update;
528} 526}
529 527
528# using global variables seems a bit hacky, but passing through all drawing
529# functions seems pointless.
530our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
531
530sub draw { 532sub draw {
531 my ($self) = @_; 533 my ($self) = @_;
532 534
533 return unless $self->{h} && $self->{w}; 535 return unless $self->{h} && $self->{w};
536
537 # update screen rectangle
538 local $draw_x = $draw_x + $self->{x};
539 local $draw_y = $draw_y + $self->{y};
540 local $draw_w = $draw_x + $self->{w};
541 local $draw_h = $draw_y + $self->{h};
542
543 # skip widgets that are entirely outside the drawing area
544 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
545 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
534 546
535 glPushMatrix; 547 glPushMatrix;
536 glTranslate $self->{x}, $self->{y}, 0; 548 glTranslate $self->{x}, $self->{y}, 0;
537 $self->_draw;
538 glPopMatrix;
539 549
540 if ($self == $HOVER && $self->{can_hover}) { 550 if ($self == $HOVER && $self->{can_hover}) {
541 my ($x, $y) = @$self{qw(x y)};
542
543 glColor 1, 0.8, 0.5, 0.2; 551 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
544 glEnable GL_BLEND; 552 glEnable GL_BLEND;
545 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 553 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
546 glBegin GL_QUADS; 554 glBegin GL_QUADS;
547 glVertex $x , $y; 555 glVertex 0 , 0;
548 glVertex $x + $self->{w}, $y; 556 glVertex $self->{w}, 0;
549 glVertex $x + $self->{w}, $y + $self->{h}; 557 glVertex $self->{w}, $self->{h};
550 glVertex $x , $y + $self->{h}; 558 glVertex 0 , $self->{h};
551 glEnd; 559 glEnd;
552 glDisable GL_BLEND; 560 glDisable GL_BLEND;
553 } 561 }
554 562
555 if ($ENV{CFPLUS_DEBUG} & 1) { 563 if ($ENV{CFPLUS_DEBUG} & 1) {
556 glPushMatrix; 564 glPushMatrix;
557 glColor 1, 1, 0, 1; 565 glColor 1, 1, 0, 1;
558 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 566 glTranslate 0.375, 0.375;
559 glBegin GL_LINE_LOOP; 567 glBegin GL_LINE_LOOP;
560 glVertex 0 , 0; 568 glVertex 0 , 0;
561 glVertex $self->{w} - 1, 0; 569 glVertex $self->{w} - 1, 0;
562 glVertex $self->{w} - 1, $self->{h} - 1; 570 glVertex $self->{w} - 1, $self->{h} - 1;
563 glVertex 0 , $self->{h} - 1; 571 glVertex 0 , $self->{h} - 1;
564 glEnd; 572 glEnd;
565 glPopMatrix; 573 glPopMatrix;
566 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 574 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
567 } 575 }
576
577 $self->_draw;
578 glPopMatrix;
568} 579}
569 580
570sub _draw { 581sub _draw {
571 my ($self) = @_; 582 my ($self) = @_;
572 583
575 586
576sub DESTROY { 587sub DESTROY {
577 my ($self) = @_; 588 my ($self) = @_;
578 589
579 delete $WIDGET{$self+0}; 590 delete $WIDGET{$self+0};
580 #$self->deactivate; 591
592 eval { $self->destroy };
593 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
581} 594}
582 595
583############################################################################# 596#############################################################################
584 597
585package CFClient::UI::DrawBG; 598package CFClient::UI::DrawBG;
610 623
611 if ($color && (@$color < 4 || $color->[3])) { 624 if ($color && (@$color < 4 || $color->[3])) {
612 my ($w, $h) = @$self{qw(w h)}; 625 my ($w, $h) = @$self{qw(w h)};
613 626
614 glEnable GL_BLEND; 627 glEnable GL_BLEND;
615 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 628 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
616 glColor @$color; 629 glColor_premultiply @$color;
617 630
618 glBegin GL_QUADS; 631 glBegin GL_QUADS;
619 glVertex 0 , 0; 632 glVertex 0 , 0;
620 glVertex 0 , $h; 633 glVertex 0 , $h;
621 glVertex $w, $h; 634 glVertex $w, $h;
652our @ISA = CFClient::UI::Base::; 665our @ISA = CFClient::UI::Base::;
653 666
654sub new { 667sub new {
655 my ($class, %arg) = @_; 668 my ($class, %arg) = @_;
656 669
657 my $children = delete $arg{children} || []; 670 my $children = delete $arg{children};
658 671
659 my $self = $class->SUPER::new ( 672 my $self = $class->SUPER::new (
660 children => [], 673 children => [],
661 can_events => 0, 674 can_events => 0,
662 %arg, 675 %arg,
663 ); 676 );
677
664 $self->add ($_) for @$children; 678 $self->add (@$children)
679 if $children;
665 680
666 $self 681 $self
667} 682}
668 683
669sub add { 684sub add {
717 $x -= $self->{x}; 732 $x -= $self->{x};
718 $y -= $self->{y}; 733 $y -= $self->{y};
719 734
720 my $res; 735 my $res;
721 736
722 for (reverse @{ $self->{children} }) { 737 for (reverse $self->visible_children) {
723 $res = $_->find_widget ($x, $y) 738 $res = $_->find_widget ($x, $y)
724 and return $res; 739 and return $res;
725 } 740 }
726 741
727 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) 742 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
748} 763}
749 764
750sub add { 765sub add {
751 my ($self, $child) = @_; 766 my ($self, $child) = @_;
752 767
753 $self->{children} = []; 768 $self->SUPER::remove ($_) for @{ $self->{children} };
754
755 $self->SUPER::add ($child); 769 $self->SUPER::add ($child);
756} 770}
757 771
758sub remove { 772sub remove {
759 my ($self, $widget) = @_; 773 my ($self, $widget) = @_;
768 782
769sub size_request { 783sub size_request {
770 $_[0]{children}[0]->size_request 784 $_[0]{children}[0]->size_request
771} 785}
772 786
773sub size_allocate { 787sub invoke_size_allocate {
774 my ($self, $w, $h) = @_; 788 my ($self, $w, $h) = @_;
775 789
776 $self->{children}[0]->configure (0, 0, $w, $h); 790 $self->{children}[0]->configure (0, 0, $w, $h);
791
792 1
777} 793}
778 794
779############################################################################# 795#############################################################################
796
797# back-buffered drawing area
780 798
781package CFClient::UI::Window; 799package CFClient::UI::Window;
782 800
783our @ISA = CFClient::UI::Bin::; 801our @ISA = CFClient::UI::Bin::;
784 802
795 813
796 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 814 $ROOT->on_post_alloc ($self => sub { $self->render_child });
797 $self->SUPER::update; 815 $self->SUPER::update;
798} 816}
799 817
800sub size_allocate { 818sub invoke_size_allocate {
801 my ($self, $w, $h) = @_; 819 my ($self, $w, $h) = @_;
802 820
803 $self->SUPER::size_allocate ($w, $h);
804 $self->update; 821 $self->update;
822
823 $self->SUPER::invoke_size_allocate ($w, $h)
805} 824}
806 825
807sub _render { 826sub _render {
827 my ($self) = @_;
828
808 $_[0]{children}[0]->draw; 829 $self->{children}[0]->draw;
809} 830}
810 831
811sub render_child { 832sub render_child {
812 my ($self) = @_; 833 my ($self) = @_;
813 834
814 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 835 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
815 glClearColor 0, 0, 0, 0; 836 glClearColor 0, 0, 0, 0;
816 glClear GL_COLOR_BUFFER_BIT; 837 glClear GL_COLOR_BUFFER_BIT;
817 838
839 {
840 package CFClient::UI::Base;
841
842 ($draw_x, $draw_y, $draw_w, $draw_h) =
843 (0, 0, $self->{w}, $self->{h});
844 }
845
818 $self->_render; 846 $self->_render;
819 }; 847 };
820} 848}
821 849
822sub _draw { 850sub _draw {
823 my ($self) = @_; 851 my ($self) = @_;
824 852
825 my ($w, $h) = ($self->w, $self->h); 853 my ($w, $h) = @$self{qw(w h)};
826 854
827 my $tex = $self->{texture} 855 my $tex = $self->{texture}
828 or return; 856 or return;
829 857
830 glEnable GL_TEXTURE_2D; 858 glEnable GL_TEXTURE_2D;
831 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 859 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
832 glColor 1, 1, 1, 1; 860 glColor 0, 0, 0, 1;
833 861
834 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 862 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
835 863
836 glDisable GL_TEXTURE_2D; 864 glDisable GL_TEXTURE_2D;
837} 865}
861 $h = 10 if $self->{scroll_y}; 889 $h = 10 if $self->{scroll_y};
862 890
863 ($w, $h) 891 ($w, $h)
864} 892}
865 893
866sub size_allocate { 894sub invoke_size_allocate {
867 my ($self, $w, $h) = @_; 895 my ($self, $w, $h) = @_;
868 896
869 my $child = $self->child; 897 my $child = $self->child;
870 898
871 $w = $child->{req_w} if $self->{scroll_x} && $child->{req_w}; 899 $w = $child->{req_w} if $self->{scroll_x} && $child->{req_w};
872 $h = $child->{req_h} if $self->{scroll_y} && $child->{req_h}; 900 $h = $child->{req_h} if $self->{scroll_y} && $child->{req_h};
873 901
874 $self->child->configure (0, 0, $w, $h); 902 $self->child->configure (0, 0, $w, $h);
875 $self->update; 903 $self->update;
904
905 1
876} 906}
877 907
878sub set_offset { 908sub set_offset {
879 my ($self, $x, $y) = @_; 909 my ($self, $x, $y) = @_;
880 910
913} 943}
914 944
915sub _render { 945sub _render {
916 my ($self) = @_; 946 my ($self) = @_;
917 947
948 local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x};
949 local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y};
950
918 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 951 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
919 952
920 $self->SUPER::_render; 953 $self->SUPER::_render;
921} 954}
922 955
925package CFClient::UI::ScrolledWindow; 958package CFClient::UI::ScrolledWindow;
926 959
927our @ISA = CFClient::UI::HBox::; 960our @ISA = CFClient::UI::HBox::;
928 961
929sub new { 962sub new {
930 my $class = shift; 963 my ($class, %arg) = @_;
964
965 my $child = delete $arg{child};
931 966
932 my $self; 967 my $self;
933 968
934 my $slider = new CFClient::UI::Slider 969 my $slider = new CFClient::UI::Slider
935 vertical => 1, 970 vertical => 1,
940 ; 975 ;
941 976
942 $self = $class->SUPER::new ( 977 $self = $class->SUPER::new (
943 vp => (new CFClient::UI::ViewPort expand => 1), 978 vp => (new CFClient::UI::ViewPort expand => 1),
944 slider => $slider, 979 slider => $slider,
945 @_, 980 %arg,
946 ); 981 );
947 982
948 $self->{vp}->add ($self->{scrolled});
949 $self->add ($self->{vp});
950 $self->add ($self->{slider}); 983 $self->SUPER::add ($self->{vp}, $self->{slider});
984 $self->add ($child) if $child;
951 985
952 $self 986 $self
987}
988
989sub add {
990 my ($self, $widget) = @_;
991
992 $self->{vp}->add ($self->{child} = $widget);
953} 993}
954 994
955sub update { 995sub update {
956 my ($self) = @_; 996 my ($self) = @_;
957 997
960 # todo: overwrite size_allocate of child 1000 # todo: overwrite size_allocate of child
961 my $child = $self->{vp}->child; 1001 my $child = $self->{vp}->child;
962 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 1002 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
963} 1003}
964 1004
965sub size_allocate { 1005sub invoke_size_allocate {
966 my ($self, $w, $h) = @_; 1006 my ($self, $w, $h) = @_;
967
968 $self->SUPER::size_allocate ($w, $h);
969 1007
970 my $child = $self->{vp}->child; 1008 my $child = $self->{vp}->child;
971 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 1009 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
1010
1011 $self->SUPER::invoke_size_allocate ($w, $h)
972} 1012}
973 1013
974#TODO# update range on size_allocate depending on child 1014#TODO# update range on size_allocate depending on child
975# update viewport offset on scroll 1015# update viewport offset on scroll
976 1016
996 1036
997 if ($self->{bg}) { 1037 if ($self->{bg}) {
998 my ($w, $h) = @$self{qw(w h)}; 1038 my ($w, $h) = @$self{qw(w h)};
999 1039
1000 glEnable GL_BLEND; 1040 glEnable GL_BLEND;
1001 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1041 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1002 glColor @{ $self->{bg} }; 1042 glColor_premultiply @{ $self->{bg} };
1003 1043
1004 glBegin GL_QUADS; 1044 glBegin GL_QUADS;
1005 glVertex 0 , 0; 1045 glVertex 0 , 0;
1006 glVertex 0 , $h; 1046 glVertex 0 , $h;
1007 glVertex $w, $h; 1047 glVertex $w, $h;
1029my @border = 1069my @border =
1030 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1070 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1031 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1071 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1032 1072
1033sub new { 1073sub new {
1034 my $class = shift; 1074 my ($class, %arg) = @_;
1035 1075
1036 my $self = $class->SUPER::new ( 1076 my $self = $class->SUPER::new (
1037 bg => [1, 1, 1, 1], 1077 bg => [1, 1, 1, 1],
1038 border_bg => [1, 1, 1, 1], 1078 border_bg => [1, 1, 1, 1],
1039 border => 0.6, 1079 border => 0.6,
1040 can_events => 1, 1080 can_events => 1,
1041 min_w => 16, 1081 min_w => 16,
1042 min_h => 16, 1082 min_h => 16,
1043 @_ 1083 %arg,
1044 ); 1084 );
1045 1085
1046 $self->{title} &&= new CFClient::UI::Label 1086 $self->{title_widget} = new CFClient::UI::Label
1047 align => 0, 1087 align => 0,
1048 valign => 1, 1088 valign => 1,
1049 text => $self->{title}, 1089 text => $self->{title},
1050 fontsize => $self->{border}; 1090 fontsize => $self->{border},
1091 if exists $self->{title};
1092
1093 if ($self->{has_close_button}) {
1094 $self->{close_button} =
1095 new CFClient::UI::ImageButton
1096 path => 'x1_close.png',
1097 on_activate => sub { $self->hide };
1098
1099 $self->CFClient::UI::Container::add ($self->{close_button});
1100 }
1051 1101
1052 $self 1102 $self
1103}
1104
1105sub add {
1106 my ($self, @widgets) = @_;
1107
1108 $self->SUPER::add (@widgets);
1109 $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button};
1110 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1053} 1111}
1054 1112
1055sub border { 1113sub border {
1056 int $_[0]{border} * $::FONTSIZE 1114 int $_[0]{border} * $::FONTSIZE
1057} 1115}
1058 1116
1059sub size_request { 1117sub size_request {
1060 my ($self) = @_; 1118 my ($self) = @_;
1119
1120 $self->{title_widget}->size_request
1121 if $self->{title_widget};
1122
1123 $self->{close_button}->size_request
1124 if $self->{close_button};
1061 1125
1062 my ($w, $h) = $self->SUPER::size_request; 1126 my ($w, $h) = $self->SUPER::size_request;
1063 1127
1064 ( 1128 (
1065 $w + $self->border * 2, 1129 $w + $self->border * 2,
1066 $h + $self->border * 2, 1130 $h + $self->border * 2,
1067 ) 1131 )
1068} 1132}
1069 1133
1070sub size_allocate { 1134sub invoke_size_allocate {
1071 my ($self, $w, $h) = @_; 1135 my ($self, $w, $h) = @_;
1072 1136
1137 if ($self->{title_widget}) {
1138 $self->{title_widget}{w} = $w;
1139 $self->{title_widget}{h} = $h;
1140 $self->{title_widget}->invoke_size_allocate ($w, $h);
1141 }
1142
1143 my $border = $self->border;
1144
1073 $h -= List::Util::max 0, $self->border * 2; 1145 $h -= List::Util::max 0, $border * 2;
1074 $w -= List::Util::max 0, $self->border * 2; 1146 $w -= List::Util::max 0, $border * 2;
1075 1147
1076 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1077 if $self->{title};
1078
1079 $self->child->configure ($self->border, $self->border, $w, $h); 1148 $self->child->configure ($border, $border, $w, $h);
1080}
1081 1149
1150 $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border)
1151 if $self->{close_button};
1152
1153 1
1154}
1155
1082sub button_down { 1156sub invoke_button_down {
1083 my ($self, $ev, $x, $y) = @_; 1157 my ($self, $ev, $x, $y) = @_;
1084 1158
1085 my ($w, $h) = @$self{qw(w h)}; 1159 my ($w, $h) = @$self{qw(w h)};
1086 my $border = $self->border; 1160 my $border = $self->border;
1087 1161
1103 my $dy = $ev->{y} - $oy; 1177 my $dy = $ev->{y} - $oy;
1104 1178
1105 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1179 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1106 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1180 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1107 1181
1182 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1108 $self->realloc; 1183 $self->realloc;
1109 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1110 }; 1184 };
1111 1185
1112 } elsif ($lr ^ $td) { 1186 } elsif ($lr ^ $td) {
1113 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1187 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1114 my ($bx, $by) = ($self->{x}, $self->{y}); 1188 my ($bx, $by) = ($self->{x}, $self->{y});
1117 my ($ev, $x, $y) = @_; 1191 my ($ev, $x, $y) = @_;
1118 1192
1119 ($x, $y) = ($ev->{x}, $ev->{y}); 1193 ($x, $y) = ($ev->{x}, $ev->{y});
1120 1194
1121 $self->move_abs ($bx + $x - $ox, $by + $y - $oy); 1195 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1196 # HACK: the next line is required to enforce placement
1197 $self->{parent}->invoke_size_allocate ($self->{parent}{w}, $self->{parent}{h});
1122 }; 1198 };
1199 } else {
1200 return 0;
1201 }
1202
1123 } 1203 1
1124} 1204}
1125 1205
1126sub button_up { 1206sub invoke_button_up {
1127 my ($self, $ev, $x, $y) = @_; 1207 my ($self, $ev, $x, $y) = @_;
1128 1208
1129 delete $self->{motion}; 1209 ! ! delete $self->{motion}
1130} 1210}
1131 1211
1132sub mouse_motion { 1212sub invoke_mouse_motion {
1133 my ($self, $ev, $x, $y) = @_; 1213 my ($self, $ev, $x, $y) = @_;
1134 1214
1135 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1215 $self->{motion}->($ev, $x, $y) if $self->{motion};
1216
1217 ! ! $self->{motion}
1136} 1218}
1137 1219
1138sub _draw { 1220sub _draw {
1139 my ($self) = @_; 1221 my ($self) = @_;
1140 1222
1223 my $child = $self->{children}[0];
1224
1141 my ($w, $h ) = ($self->{w}, $self->{h}); 1225 my ($w, $h ) = ($self->{w}, $self->{h});
1142 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 1226 my ($cw, $ch) = ($child->{w}, $child->{h});
1143 1227
1144 glEnable GL_TEXTURE_2D; 1228 glEnable GL_TEXTURE_2D;
1145 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1229 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1146 1230
1147 my $border = $self->border; 1231 my $border = $self->border;
1162 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1246 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1163 } 1247 }
1164 1248
1165 glDisable GL_TEXTURE_2D; 1249 glDisable GL_TEXTURE_2D;
1166 1250
1167 $self->{title}->draw if $self->{title};
1168
1169 $self->child->draw; 1251 $child->draw;
1252
1253 if ($self->{title_widget}) {
1254 glTranslate 0, $border - $self->{h};
1255 $self->{title_widget}->_draw;
1256
1257 glTranslate 0, - ($border - $self->{h});
1258 }
1259
1260 $self->{close_button}->draw
1261 if $self->{close_button};
1170} 1262}
1171 1263
1172############################################################################# 1264#############################################################################
1173 1265
1174package CFClient::UI::Table; 1266package CFClient::UI::Table;
1197 1289
1198 $child->set_parent ($self); 1290 $child->set_parent ($self);
1199 $self->{children}[$y][$x] = $child; 1291 $self->{children}[$y][$x] = $child;
1200 1292
1201 $self->realloc; 1293 $self->realloc;
1294}
1295
1296sub remove {
1297 my ($self, $child) = @_;
1298
1299 # TODO: not yet implemented
1202} 1300}
1203 1301
1204# TODO: move to container class maybe? send children a signal on removal? 1302# TODO: move to container class maybe? send children a signal on removal?
1205sub clear { 1303sub clear {
1206 my ($self) = @_; 1304 my ($self) = @_;
1247 (sum @$ws), 1345 (sum @$ws),
1248 (sum @$hs), 1346 (sum @$hs),
1249 ) 1347 )
1250} 1348}
1251 1349
1252sub size_allocate { 1350sub invoke_size_allocate {
1253 my ($self, $w, $h) = @_; 1351 my ($self, $w, $h) = @_;
1254 1352
1255 my ($ws, $hs) = $self->get_wh; 1353 my ($ws, $hs) = $self->get_wh;
1256 1354
1257 my $req_w = (sum @$ws) || 1; 1355 my $req_w = (sum @$ws) || 1;
1289 } 1387 }
1290 1388
1291 $y += $row_h; 1389 $y += $row_h;
1292 } 1390 }
1293 1391
1392 1
1294} 1393}
1295 1394
1296sub find_widget { 1395sub find_widget {
1297 my ($self, $x, $y) = @_; 1396 my ($self, $x, $y) = @_;
1298 1397
1335 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1434 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1336 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1435 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1337 ) 1436 )
1338} 1437}
1339 1438
1340sub size_allocate { 1439sub invoke_size_allocate {
1341 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1342 1441
1343 my $space = $self->{vertical} ? $h : $w; 1442 my $space = $self->{vertical} ? $h : $w;
1344 my $children = $self->{children}; 1443 my @children = $self->visible_children;
1345 1444
1346 my @req; 1445 my @req;
1347 1446
1348 if ($self->{homogeneous}) { 1447 if ($self->{homogeneous}) {
1349 @req = ($space / (@$children || 1)) x @$children; 1448 @req = ($space / (@children || 1)) x @children;
1350 } else { 1449 } else {
1351 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children; 1450 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @children;
1352 my $req = List::Util::sum @req; 1451 my $req = List::Util::sum @req;
1353 1452
1354 if ($req > $space) { 1453 if ($req > $space) {
1355 # ah well, not enough space 1454 # ah well, not enough space
1356 $_ *= $space / $req for @req; 1455 $_ *= $space / $req for @req;
1357 } else { 1456 } else {
1358 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1; 1457 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1359 1458
1360 $space = ($space - $req) / $expand; # remaining space to give away 1459 $space = ($space - $req) / $expand; # remaining space to give away
1361 1460
1362 $req[$_] += $space * $children->[$_]{expand} 1461 $req[$_] += $space * $children[$_]{expand}
1363 for 0 .. $#$children; 1462 for 0 .. $#children;
1364 } 1463 }
1365 } 1464 }
1366 1465
1367 CFClient::UI::harmonize \@req; 1466 CFClient::UI::harmonize \@req;
1368 1467
1369 my $pos = 0; 1468 my $pos = 0;
1370 for (0 .. $#$children) { 1469 for (0 .. $#children) {
1371 my $alloc = $req[$_]; 1470 my $alloc = $req[$_];
1372 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1471 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1373 1472
1374 $pos += $alloc; 1473 $pos += $alloc;
1375 } 1474 }
1376 1475
1377 1 1476 1
1467 1566
1468 delete $self->{texture}; 1567 delete $self->{texture};
1469 $self->SUPER::update; 1568 $self->SUPER::update;
1470} 1569}
1471 1570
1571sub realloc {
1572 my ($self) = @_;
1573
1574 delete $self->{ox};
1575 $self->SUPER::realloc;
1576}
1577
1472sub set_text { 1578sub set_text {
1473 my ($self, $text) = @_; 1579 my ($self, $text) = @_;
1474 1580
1475 return if $self->{text} eq "T$text"; 1581 return if $self->{text} eq "T$text";
1476 $self->{text} = "T$text"; 1582 $self->{text} = "T$text";
1477 1583
1478 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1584 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1479 $self->{layout}->set_text ($text); 1585 $self->{layout}->set_text ($text);
1480 1586
1587 delete $self->{size_req};
1481 $self->realloc; 1588 $self->realloc;
1482 $self->update; 1589 $self->update;
1483} 1590}
1484 1591
1485sub set_markup { 1592sub set_markup {
1491 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1598 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1492 1599
1493 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1600 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1494 $self->{layout}->set_markup ($markup); 1601 $self->{layout}->set_markup ($markup);
1495 1602
1603 delete $self->{size_req};
1496 $self->realloc; 1604 $self->realloc;
1497 $self->update; 1605 $self->update;
1498} 1606}
1499 1607
1500sub size_request { 1608sub size_request {
1501 my ($self) = @_; 1609 my ($self) = @_;
1502 1610
1611 $self->{size_req} ||= do {
1503 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1612 $self->{layout}->set_font ($self->{font}) if $self->{font};
1504 $self->{layout}->set_width ($self->{max_w} || -1); 1613 $self->{layout}->set_width ($self->{max_w} || -1);
1505 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1614 $self->{layout}->set_ellipsise ($self->{ellipsise});
1506 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1615 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1507 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1616 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1508 1617
1509 my ($w, $h) = $self->{layout}->size; 1618 my ($w, $h) = $self->{layout}->size;
1510 1619
1511 if (exists $self->{template}) { 1620 if (exists $self->{template}) {
1512 $self->{template}->set_font ($self->{font}) if $self->{font}; 1621 $self->{template}->set_font ($self->{font}) if $self->{font};
1513 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1622 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1514 1623
1515 my ($w2, $h2) = $self->{template}->size; 1624 my ($w2, $h2) = $self->{template}->size;
1516 1625
1517 $w = List::Util::max $w, $w2; 1626 $w = List::Util::max $w, $w2;
1518 $h = List::Util::max $h, $h2; 1627 $h = List::Util::max $h, $h2;
1628 }
1629
1630 [$w, $h]
1519 } 1631 };
1520 1632
1521 ($w, $h) 1633 @{ $self->{size_req} }
1522} 1634}
1523 1635
1524sub size_allocate { 1636sub invoke_size_allocate {
1525 my ($self, $w, $h) = @_; 1637 my ($self, $w, $h) = @_;
1526 1638
1639 delete $self->{ox};
1640
1527 delete $self->{texture}; 1641 delete $self->{texture}
1642 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1643
1644 1
1528} 1645}
1529 1646
1530sub set_fontsize { 1647sub set_fontsize {
1531 my ($self, $fontsize) = @_; 1648 my ($self, $fontsize) = @_;
1532 1649
1533 $self->{fontsize} = $fontsize; 1650 $self->{fontsize} = $fontsize;
1534 delete $self->{texture}; 1651 delete $self->{texture};
1535 1652
1536 $self->realloc; 1653 $self->realloc;
1654}
1655
1656sub reconfigure {
1657 my ($self) = @_;
1658
1659 delete $self->{size_req};
1660
1661 $self->SUPER::reconfigure;
1537} 1662}
1538 1663
1539sub _draw { 1664sub _draw {
1540 my ($self) = @_; 1665 my ($self) = @_;
1541 1666
1547 $self->{layout}->set_width ($self->{w}); 1672 $self->{layout}->set_width ($self->{w});
1548 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1673 $self->{layout}->set_ellipsise ($self->{ellipsise});
1549 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1674 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1550 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1675 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 1676
1552 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1677 new_from_layout CFClient::Texture $self->{layout}
1678 };
1553 1679
1680 unless (exists $self->{ox}) {
1554 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 1681 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1555 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} 1682 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1556 : ($self->{w} - $tex->{w}) * 0.5); 1683 : ($self->{w} - $tex->{w}) * 0.5);
1557 1684
1558 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 1685 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1559 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} 1686 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1560 : ($self->{h} - $tex->{h}) * 0.5); 1687 : ($self->{h} - $tex->{h}) * 0.5);
1561
1562 $tex
1563 }; 1688 };
1564 1689
1565 glEnable GL_TEXTURE_2D; 1690 glEnable GL_TEXTURE_2D;
1566 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1691
1692 my $w = List::Util::min $self->{w} + 4, $tex->{w};
1693 my $h = List::Util::min $self->{h} + 2, $tex->{h};
1567 1694
1568 if ($tex->{format} == GL_ALPHA) { 1695 if ($tex->{format} == GL_ALPHA) {
1696 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1569 glColor @{$self->{fg}}; 1697 glColor @{$self->{fg}};
1570 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); 1698 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}, $w, $h);
1571 } else { 1699 } else {
1700 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1572 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); 1701 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}, $w, $h);
1573 } 1702 }
1574 1703
1575 glDisable GL_TEXTURE_2D; 1704 glDisable GL_TEXTURE_2D;
1576} 1705}
1577 1706
1594 can_hover => 1, 1723 can_hover => 1,
1595 can_focus => 1, 1724 can_focus => 1,
1596 valign => 0, 1725 valign => 0,
1597 can_events => 1, 1726 can_events => 1,
1598 #text => ... 1727 #text => ...
1728 #hidden => "*",
1599 @_ 1729 @_
1600 ) 1730 )
1601} 1731}
1602 1732
1603sub _set_text { 1733sub _set_text {
1605 1735
1606 delete $self->{cur_h}; 1736 delete $self->{cur_h};
1607 1737
1608 return if $self->{text} eq $text; 1738 return if $self->{text} eq $text;
1609 1739
1610 delete $self->{texture};
1611
1612 $self->{last_activity} = $::NOW; 1740 $self->{last_activity} = $::NOW;
1613 $self->{text} = $text; 1741 $self->{text} = $text;
1614 1742
1615 $text =~ s/./*/g if $self->{hidden}; 1743 $text =~ s/./*/g if $self->{hidden};
1616 $self->{layout}->set_text ("$text "); 1744 $self->{layout}->set_text ("$text ");
1745 delete $self->{size_req};
1617 1746
1618 $self->_emit (changed => $self->{text}); 1747 $self->emit (changed => $self->{text});
1748
1749 $self->realloc;
1750 $self->update;
1619} 1751}
1620 1752
1621sub set_text { 1753sub set_text {
1622 my ($self, $text) = @_; 1754 my ($self, $text) = @_;
1623 1755
1624 $self->{cursor} = length $text; 1756 $self->{cursor} = length $text;
1625 $self->_set_text ($text); 1757 $self->_set_text ($text);
1626
1627 $self->realloc;
1628} 1758}
1629 1759
1630sub get_text { 1760sub get_text {
1631 $_[0]{text} 1761 $_[0]{text}
1632} 1762}
1637 my ($w, $h) = $self->SUPER::size_request; 1767 my ($w, $h) = $self->SUPER::size_request;
1638 1768
1639 ($w + 1, $h) # add 1 for cursor 1769 ($w + 1, $h) # add 1 for cursor
1640} 1770}
1641 1771
1642sub key_down { 1772sub invoke_key_down {
1643 my ($self, $ev) = @_; 1773 my ($self, $ev) = @_;
1644 1774
1645 my $mod = $ev->{mod}; 1775 my $mod = $ev->{mod};
1646 my $sym = $ev->{sym}; 1776 my $sym = $ev->{sym};
1647 my $uni = $ev->{unicode}; 1777 my $uni = $ev->{unicode};
1659 } elsif ($sym == CFClient::SDLK_HOME) { 1789 } elsif ($sym == CFClient::SDLK_HOME) {
1660 $self->{cursor} = 0; 1790 $self->{cursor} = 0;
1661 } elsif ($sym == CFClient::SDLK_END) { 1791 } elsif ($sym == CFClient::SDLK_END) {
1662 $self->{cursor} = length $text; 1792 $self->{cursor} = length $text;
1663 } elsif ($uni == 27) { 1793 } elsif ($uni == 27) {
1664 $self->_emit ('escape'); 1794 $self->emit ('escape');
1665 } elsif ($uni) { 1795 } elsif ($uni) {
1666 substr $text, $self->{cursor}++, 0, chr $uni; 1796 substr $text, $self->{cursor}++, 0, chr $uni;
1797 } else {
1798 return 0;
1667 } 1799 }
1668 1800
1669 $self->_set_text ($text); 1801 $self->_set_text ($text);
1670 1802
1671 $self->realloc; 1803 $self->realloc;
1672}
1673 1804
1805 1
1806}
1807
1674sub focus_in { 1808sub invoke_focus_in {
1675 my ($self) = @_; 1809 my ($self) = @_;
1676 1810
1677 $self->{last_activity} = $::NOW; 1811 $self->{last_activity} = $::NOW;
1678 1812
1679 $self->SUPER::focus_in; 1813 $self->SUPER::invoke_focus_in
1680} 1814}
1681 1815
1682sub button_down { 1816sub invoke_button_down {
1683 my ($self, $ev, $x, $y) = @_; 1817 my ($self, $ev, $x, $y) = @_;
1684 1818
1685 $self->SUPER::button_down ($ev, $x, $y); 1819 $self->SUPER::invoke_button_down ($ev, $x, $y);
1686 1820
1687 my $idx = $self->{layout}->xy_to_index ($x, $y); 1821 my $idx = $self->{layout}->xy_to_index ($x, $y);
1688 1822
1689 # byte-index to char-index 1823 # byte-index to char-index
1690 my $text = $self->{text}; 1824 my $text = $self->{text};
1691 utf8::encode $text; 1825 utf8::encode $text;
1692 $self->{cursor} = length substr $text, 0, $idx; 1826 $self->{cursor} = length substr $text, 0, $idx;
1693 1827
1694 $self->_set_text ($self->{text}); 1828 $self->_set_text ($self->{text});
1695 $self->update; 1829 $self->update;
1830
1831 1
1696} 1832}
1697 1833
1698sub mouse_motion { 1834sub invoke_mouse_motion {
1699 my ($self, $ev, $x, $y) = @_; 1835 my ($self, $ev, $x, $y) = @_;
1700# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 1836# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1837
1838 1
1701} 1839}
1702 1840
1703sub _draw { 1841sub _draw {
1704 my ($self) = @_; 1842 my ($self) = @_;
1705 1843
1706 local $self->{fg} = $self->{fg}; 1844 local $self->{fg} = $self->{fg};
1707 1845
1708 if ($FOCUS == $self) { 1846 if ($FOCUS == $self) {
1709 glColor @{$self->{active_bg}}; 1847 glColor_premultiply @{$self->{active_bg}};
1710 $self->{fg} = $self->{active_fg}; 1848 $self->{fg} = $self->{active_fg};
1711 } else { 1849 } else {
1712 glColor @{$self->{bg}}; 1850 glColor_premultiply @{$self->{bg}};
1713 } 1851 }
1714 1852
1715 glEnable GL_BLEND; 1853 glEnable GL_BLEND;
1716 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1854 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1717 glBegin GL_QUADS; 1855 glBegin GL_QUADS;
1718 glVertex 0 , 0; 1856 glVertex 0 , 0;
1719 glVertex 0 , $self->{h}; 1857 glVertex 0 , $self->{h};
1720 glVertex $self->{w}, $self->{h}; 1858 glVertex $self->{w}, $self->{h};
1721 glVertex $self->{w}, 0; 1859 glVertex $self->{w}, 0;
1746 1884
1747our @ISA = CFClient::UI::EntryBase::; 1885our @ISA = CFClient::UI::EntryBase::;
1748 1886
1749use CFClient::OpenGL; 1887use CFClient::OpenGL;
1750 1888
1751sub key_down { 1889sub invoke_key_down {
1752 my ($self, $ev) = @_; 1890 my ($self, $ev) = @_;
1753 1891
1754 my $sym = $ev->{sym}; 1892 my $sym = $ev->{sym};
1755 1893
1756 if ($sym == 13) { 1894 if ($sym == 13) {
1757 unshift @{$self->{history}}, 1895 unshift @{$self->{history}},
1758 my $txt = $self->get_text; 1896 my $txt = $self->get_text;
1897
1759 $self->{history_pointer} = -1; 1898 $self->{history_pointer} = -1;
1760 $self->{history_saveback} = ''; 1899 $self->{history_saveback} = '';
1761 $self->_emit (activate => $txt); 1900 $self->emit (activate => $txt);
1762 $self->update; 1901 $self->update;
1763 1902
1764 } elsif ($sym == CFClient::SDLK_UP) { 1903 } elsif ($sym == CFClient::SDLK_UP) {
1765 if ($self->{history_pointer} < 0) { 1904 if ($self->{history_pointer} < 0) {
1766 $self->{history_saveback} = $self->get_text; 1905 $self->{history_saveback} = $self->get_text;
1782 } else { 1921 } else {
1783 $self->set_text ($self->{history_saveback}); 1922 $self->set_text ($self->{history_saveback});
1784 } 1923 }
1785 1924
1786 } else { 1925 } else {
1787 $self->SUPER::key_down ($ev); 1926 return $self->SUPER::invoke_key_down ($ev)
1927 }
1928
1788 } 1929 1
1789
1790} 1930}
1791 1931
1792############################################################################# 1932#############################################################################
1793 1933
1794package CFClient::UI::Button; 1934package CFClient::UI::Button;
1815 can_events => 1, 1955 can_events => 1,
1816 @_ 1956 @_
1817 ) 1957 )
1818} 1958}
1819 1959
1820sub activate { }
1821
1822sub button_up { 1960sub invoke_button_up {
1823 my ($self, $ev, $x, $y) = @_; 1961 my ($self, $ev, $x, $y) = @_;
1824 1962
1825 $self->emit ("activate") 1963 $self->emit ("activate")
1826 if $x >= 0 && $x < $self->{w} 1964 if $x >= 0 && $x < $self->{w}
1827 && $y >= 0 && $y < $self->{h}; 1965 && $y >= 0 && $y < $self->{h};
1966
1967 1
1828} 1968}
1829 1969
1830sub _draw { 1970sub _draw {
1831 my ($self) = @_; 1971 my ($self) = @_;
1832 1972
1833 local $self->{fg} = $self->{fg}; 1973 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
1834
1835 if ($GRAB == $self) {
1836 $self->{fg} = $self->{active_fg};
1837 }
1838 1974
1839 glEnable GL_TEXTURE_2D; 1975 glEnable GL_TEXTURE_2D;
1840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1976 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1841 glColor 0, 0, 0, 1; 1977 glColor 0, 0, 0, 1;
1842 1978
1843 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 1979 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1844 1980
1845 glDisable GL_TEXTURE_2D; 1981 glDisable GL_TEXTURE_2D;
1846 1982
1847 $self->SUPER::_draw; 1983 $self->SUPER::_draw;
1984}
1985
1986#############################################################################
1987
1988package CFClient::UI::ImageButton;
1989
1990our @ISA = CFClient::UI::Image::;
1991
1992use CFClient::OpenGL;
1993
1994my %textures;
1995
1996sub new {
1997 my $class = shift;
1998
1999 my $self = $class->SUPER::new (
2000 padding_x => 4,
2001 padding_y => 4,
2002 fg => [1, 1, 1],
2003 active_fg => [0, 0, 1],
2004 can_hover => 1,
2005 align => 0,
2006 valign => 0,
2007 can_events => 1,
2008 @_
2009 );
2010}
2011
2012sub invoke_button_up {
2013 my ($self, $ev, $x, $y) = @_;
2014
2015 $self->emit ("activate")
2016 if $x >= 0 && $x < $self->{w}
2017 && $y >= 0 && $y < $self->{h};
2018
2019 1
1848} 2020}
1849 2021
1850############################################################################# 2022#############################################################################
1851 2023
1852package CFClient::UI::CheckBox; 2024package CFClient::UI::CheckBox;
1879 my ($self) = @_; 2051 my ($self) = @_;
1880 2052
1881 (6) x 2 2053 (6) x 2
1882} 2054}
1883 2055
1884sub button_down { 2056sub invoke_button_down {
1885 my ($self, $ev, $x, $y) = @_; 2057 my ($self, $ev, $x, $y) = @_;
1886 2058
1887 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} 2059 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1888 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { 2060 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1889 $self->{state} = !$self->{state}; 2061 $self->{state} = !$self->{state};
1890 $self->_emit (changed => $self->{state}); 2062 $self->emit (changed => $self->{state});
2063 } else {
2064 return 0
2065 }
2066
1891 } 2067 1
1892} 2068}
1893 2069
1894sub _draw { 2070sub _draw {
1895 my ($self) = @_; 2071 my ($self) = @_;
1896 2072
1916package CFClient::UI::Image; 2092package CFClient::UI::Image;
1917 2093
1918our @ISA = CFClient::UI::Base::; 2094our @ISA = CFClient::UI::Base::;
1919 2095
1920use CFClient::OpenGL; 2096use CFClient::OpenGL;
1921use Carp qw/confess/;
1922 2097
1923our %loaded_images; 2098our %texture_cache;
1924 2099
1925sub new { 2100sub new {
1926 my $class = shift; 2101 my $class = shift;
1927 2102
1928 my $self = $class->SUPER::new (can_events => 0, @_); 2103 my $self = $class->SUPER::new (
2104 can_events => 0,
2105 @_,
2106 );
1929 2107
1930 $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; 2108 $self->{path}
2109 or Carp::croak "required attribute 'path' not set";
1931 2110
1932 $loaded_images{$self->{image}} ||= 2111 $self->{tex} = $texture_cache{$self->{path}} ||=
1933 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; 2112 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
1934 2113
1935 my $tex = $self->{tex} = $loaded_images{$self->{image}}; 2114 Scalar::Util::weaken $texture_cache{$self->{path}};
1936 2115
1937 Scalar::Util::weaken $loaded_images{$self->{image}}; 2116 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
1938
1939 $self->{aspect} = $tex->{w} / $tex->{h};
1940 2117
1941 $self 2118 $self
1942} 2119}
1943 2120
1944sub size_request { 2121sub size_request {
1945 my ($self) = @_; 2122 my ($self) = @_;
1946 2123
1947 ($self->{tex}->{w}, $self->{tex}->{h}) 2124 ($self->{tex}{w}, $self->{tex}{h})
1948} 2125}
1949 2126
1950sub _draw { 2127sub _draw {
1951 my ($self) = @_; 2128 my ($self) = @_;
1952 2129
2063 2240
2064 my $h1 = $self->{h} * (1 - $ycut1); 2241 my $h1 = $self->{h} * (1 - $ycut1);
2065 my $h2 = $self->{h} * (1 - $ycut2); 2242 my $h2 = $self->{h} * (1 - $ycut2);
2066 2243
2067 glEnable GL_BLEND; 2244 glEnable GL_BLEND;
2068 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2245 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
2246 GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2069 glEnable GL_TEXTURE_2D; 2247 glEnable GL_TEXTURE_2D;
2070 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2248 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2071 2249
2072 glBindTexture GL_TEXTURE_2D, $t1->{name}; 2250 glBindTexture GL_TEXTURE_2D, $t1->{name};
2073 glBegin GL_QUADS; 2251 glBegin GL_QUADS;
2187 $self->update; 2365 $self->update;
2188 2366
2189 $self 2367 $self
2190} 2368}
2191 2369
2192sub changed { }
2193
2194sub set_range { 2370sub set_range {
2195 my ($self, $range) = @_; 2371 my ($self, $range) = @_;
2196 2372
2197 ($range, $self->{range}) = ($self->{range}, $range); 2373 ($range, $self->{range}) = ($self->{range}, $range);
2198 2374
2199 $self->update
2200 if "@$range" ne "@{$self->{range}}"; 2375 if ("@$range" ne "@{$self->{range}}") {
2376 $self->update;
2377 $self->set_value ($self->{range}[0]);
2378 }
2201} 2379}
2202 2380
2203sub set_value { 2381sub set_value {
2204 my ($self, $value) = @_; 2382 my ($self, $value) = @_;
2205 2383
2216 if $unit; 2394 if $unit;
2217 2395
2218 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2396 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2219 2397
2220 if ($value != $old_value) { 2398 if ($value != $old_value) {
2221 $self->_emit (changed => $value); 2399 $self->emit (changed => $value);
2222 $self->update; 2400 $self->update;
2223 } 2401 }
2224} 2402}
2225 2403
2226sub size_request { 2404sub size_request {
2227 my ($self) = @_; 2405 my ($self) = @_;
2228 2406
2229 ($self->{req_w}, $self->{req_h}) 2407 ($self->{req_w}, $self->{req_h})
2230} 2408}
2231 2409
2232sub button_down { 2410sub invoke_button_down {
2233 my ($self, $ev, $x, $y) = @_; 2411 my ($self, $ev, $x, $y) = @_;
2234 2412
2235 $self->SUPER::button_down ($ev, $x, $y); 2413 $self->SUPER::invoke_button_down ($ev, $x, $y);
2236 2414
2237 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2415 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2238 2416
2239 $self->mouse_motion ($ev, $x, $y); 2417 $self->invoke_mouse_motion ($ev, $x, $y)
2240} 2418}
2241 2419
2242sub mouse_motion { 2420sub invoke_mouse_motion {
2243 my ($self, $ev, $x, $y) = @_; 2421 my ($self, $ev, $x, $y) = @_;
2244 2422
2245 if ($GRAB == $self) { 2423 if ($GRAB == $self) {
2246 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2424 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2247 2425
2248 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2426 my (undef, $lo, $hi, $page) = @{$self->{range}};
2249 2427
2250 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 2428 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2251 2429
2252 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 2430 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2431 } else {
2432 return 0;
2433 }
2434
2253 } 2435 1
2254} 2436}
2255 2437
2256sub update { 2438sub update {
2257 my ($self) = @_; 2439 my ($self) = @_;
2258 2440
2259 $CFClient::UI::ROOT->on_post_alloc ($self => sub { 2441 delete $self->{knob_w};
2442 $self->SUPER::update;
2443}
2444
2445sub _draw {
2446 my ($self) = @_;
2447
2448 unless ($self->{knob_w}) {
2260 $self->set_value ($self->{range}[0]); 2449 $self->set_value ($self->{range}[0]);
2261 2450
2262 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2451 my ($value, $lo, $hi, $page) = @{$self->{range}};
2263 my $range = ($hi - $page - $lo) || 1e-100; 2452 my $range = ($hi - $page - $lo) || 1e-100;
2264 2453
2270 $value = ($value - $lo) / $range; 2459 $value = ($value - $lo) / $range;
2271 $value = $value * $self->{scale} + $self->{offset}; 2460 $value = $value * $self->{scale} + $self->{offset};
2272 2461
2273 $self->{knob_x} = $value - $knob_w * 0.5; 2462 $self->{knob_x} = $value - $knob_w * 0.5;
2274 $self->{knob_w} = $knob_w; 2463 $self->{knob_w} = $knob_w;
2275 }); 2464 }
2276
2277 $self->SUPER::update;
2278}
2279
2280sub _draw {
2281 my ($self) = @_;
2282 2465
2283 $self->SUPER::_draw (); 2466 $self->SUPER::_draw ();
2284 2467
2285 glScale $self->{w}, $self->{h}; 2468 glScale $self->{w}, $self->{h};
2286 2469
2347sub set_range { shift->{slider}->set_range (@_) } 2530sub set_range { shift->{slider}->set_range (@_) }
2348sub set_value { shift->{slider}->set_value (@_) } 2531sub set_value { shift->{slider}->set_value (@_) }
2349 2532
2350############################################################################# 2533#############################################################################
2351 2534
2352package CFClient::UI::TextView; 2535package CFClient::UI::TextScroller;
2353 2536
2354our @ISA = CFClient::UI::HBox::; 2537our @ISA = CFClient::UI::HBox::;
2355 2538
2356use CFClient::OpenGL; 2539use CFClient::OpenGL;
2357 2540
2359 my $class = shift; 2542 my $class = shift;
2360 2543
2361 my $self = $class->SUPER::new ( 2544 my $self = $class->SUPER::new (
2362 fontsize => 1, 2545 fontsize => 1,
2363 can_events => 0, 2546 can_events => 0,
2547 indent => 0,
2364 #font => default_font 2548 #font => default_font
2365 @_, 2549 @_,
2366 2550
2367 layout => (new CFClient::Layout 1), 2551 layout => (new CFClient::Layout 1),
2368 par => [], 2552 par => [],
2383 2567
2384 $self->{fontsize} = $fontsize; 2568 $self->{fontsize} = $fontsize;
2385 $self->reflow; 2569 $self->reflow;
2386} 2570}
2387 2571
2572sub visible_children {
2573 my ($self) = @_;
2574
2575 @{$self->{children}}[0,1]
2576}
2577
2388sub size_allocate { 2578sub invoke_size_allocate {
2389 my ($self, $w, $h) = @_; 2579 my ($self, $w, $h) = @_;
2390 2580
2391 $self->SUPER::size_allocate ($w, $h); 2581 my (undef, undef, @other) = @{ $self->{children} };
2582 $_->configure (@$_{qw(x y req_w req_h)}) for @other;
2392 2583
2393 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2584 $self->{layout}->set_font ($self->{font}) if $self->{font};
2394 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2585 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2395 $self->{layout}->set_width ($self->{children}[0]{w}); 2586 $self->{layout}->set_width ($self->{children}[0]{w});
2587 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2396 2588
2397 $self->reflow; 2589 $self->reflow;
2398}
2399 2590
2400sub text_size { 2591 $self->SUPER::invoke_size_allocate ($w, $h)
2401 my ($self, $text, $indent) = @_; 2592}
2593
2594sub get_layout {
2595 my ($self, $para) = @_;
2402 2596
2403 my $layout = $self->{layout}; 2597 my $layout = $self->{layout};
2404 2598
2599 $layout->set_font ($self->{font}) if $self->{font};
2405 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2600 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2406 $layout->set_width ($self->{children}[0]{w} - $indent); 2601 $layout->set_width ($self->{children}[0]{w} - $para->{indent});
2602 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2407 $layout->set_markup ($text); 2603 $layout->set_markup ($para->{markup});
2408 2604 $layout->set_shapes (map +($_->{req_w}, $_->{req_h}), @{$para->{widget}});
2605
2409 $layout->size 2606 $layout
2410} 2607}
2411 2608
2412sub reflow { 2609sub reflow {
2413 my ($self) = @_; 2610 my ($self) = @_;
2414 2611
2423 $self->{children}[1]->set_value ($offset); 2620 $self->{children}[1]->set_value ($offset);
2424} 2621}
2425 2622
2426sub clear { 2623sub clear {
2427 my ($self) = @_; 2624 my ($self) = @_;
2625
2626 my (undef, undef, @other) = @{ $self->{children} };
2627 $self->remove ($_) for @other;
2428 2628
2429 $self->{par} = []; 2629 $self->{par} = [];
2430 $self->{height} = 0; 2630 $self->{height} = 0;
2431 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 2631 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2432} 2632}
2433 2633
2434sub add_paragraph { 2634sub add_paragraph {
2435 my ($self, $color, $text, $indent) = @_; 2635 my ($self, $color, $para, $indent) = @_;
2436 2636
2437 for my $line (split /\n/, $text) { 2637 my ($text, @w) = ref $para ? @$para : $para;
2438 my ($w, $h) = $self->text_size ($line); 2638
2439 $self->{height} += $h; 2639 $para = {
2440 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line]; 2640 w => 1e10,
2641 wrapped => 1,
2642 fg => $color,
2643 indent => $indent,
2644 markup => $text,
2645 widget => \@w,
2441 } 2646 };
2442 2647
2443 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); 2648 $self->add (@w) if @w;
2649 push @{$self->{par}}, $para;
2650
2651 $self->{need_reflow}++;
2652 $self->update;
2653}
2654
2655sub scroll_to_bottom {
2656 my ($self) = @_;
2657
2658 $self->{scroll_to_bottom} = 1;
2659 $self->update;
2444} 2660}
2445 2661
2446sub update { 2662sub update {
2447 my ($self) = @_; 2663 my ($self) = @_;
2448 2664
2450 2666
2451 return unless $self->{h} > 0; 2667 return unless $self->{h} > 0;
2452 2668
2453 delete $self->{texture}; 2669 delete $self->{texture};
2454 2670
2455 $ROOT->on_post_alloc ($self, sub { 2671 $ROOT->on_post_alloc ($self => sub {
2456 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2672 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2457 2673
2458 if (delete $self->{need_reflow}) { 2674 if (delete $self->{need_reflow}) {
2459 my $height = 0; 2675 my $height = 0;
2460 2676
2461 my $layout = $self->{layout};
2462
2463 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2464
2465 for (@{$self->{par}}) { 2677 for my $para (@{$self->{par}}) {
2466 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support 2678 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2467 $layout->set_width ($W - $_->[3]); 2679 my $layout = $self->get_layout ($para);
2468 $layout->set_markup ($_->[4]);
2469 my ($w, $h) = $layout->size; 2680 my ($w, $h) = $layout->size;
2470 $_->[0] = $w + $_->[3]; 2681
2682 $para->{w} = $w + $para->{indent};
2471 $_->[1] = $h; 2683 $para->{h} = $h;
2684 $para->{wrapped} = $layout->has_wrapped;
2472 } 2685 }
2473 2686
2474 $height += $_->[1]; 2687 $height += $para->{h};
2475 } 2688 }
2476 2689
2477 $self->{height} = $height; 2690 $self->{height} = $height;
2478 2691
2479 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); 2692 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2480 2693
2481 delete $self->{texture}; 2694 delete $self->{texture};
2482 } 2695 }
2483 2696
2697 if (delete $self->{scroll_to_bottom}) {
2698 $self->{children}[1]->set_value (1e10);
2699 }
2700
2484 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 2701 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2485 glClearColor 0.5, 0.5, 0.5, 0; 2702 glClearColor 0, 0, 0, 0;
2486 glClear GL_COLOR_BUFFER_BIT; 2703 glClear GL_COLOR_BUFFER_BIT;
2487 2704
2488 my $top = int $self->{children}[1]{range}[0]; 2705 my $top = int $self->{children}[1]{range}[0];
2489 2706
2490 my $y0 = $top; 2707 my $y0 = $top;
2491 my $y1 = $top + $H; 2708 my $y1 = $top + $H;
2492 2709
2493 my $y = 0; 2710 my $y = 0;
2494
2495 my $layout = $self->{layout};
2496
2497 $layout->set_font ($self->{font}) if $self->{font};
2498 2711
2499 glEnable GL_BLEND; 2712 glEnable GL_BLEND;
2500 #TODO# not correct in windows where rgba is forced off 2713 #TODO# not correct in windows where rgba is forced off
2501 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2714 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2502 2715
2503 for my $par (@{$self->{par}}) { 2716 for my $para (@{$self->{par}}) {
2504 my $h = $par->[1]; 2717 my $h = $para->{h};
2505 2718
2506 if ($y0 < $y + $h && $y < $y1) { 2719 if ($y0 < $y + $h && $y < $y1) {
2507 $layout->set_foreground (@{ $par->[2] }); 2720
2508 $layout->set_width ($W - $par->[3]); 2721 my $layout = $self->get_layout ($para);
2509 $layout->set_markup ($par->[4]);
2510 2722
2511 my ($w, $h, $data, $format, $internalformat) = $layout->render; 2723 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2512 2724
2513 glRasterPos $par->[3], $y - $y0; 2725 glRasterPos $para->{indent}, $y - $y0;
2514 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; 2726 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2727
2728 if (my @w = @{ $para->{widget} }) {
2729 my @s = $layout->get_shapes;
2730
2731 for (@w) {
2732 my ($dx, $dy) = splice @s, 0, 2, ();
2733
2734 $_->{x} = $dx + $para->{indent};
2735 $_->{y} = $dy + $y - $y0;
2736
2737 $_->draw;
2738 }
2739 }
2515 } 2740 }
2516 2741
2517 $y += $h; 2742 $y += $h;
2518 } 2743 }
2519 2744
2520 glDisable GL_BLEND; 2745 glDisable GL_BLEND;
2521 }; 2746 };
2522 }); 2747 });
2523} 2748}
2524 2749
2750sub reconfigure {
2751 my ($self) = @_;
2752
2753 $self->SUPER::reconfigure;
2754
2755 $_->{w} = 1e10 for @{ $self->{par} };
2756 $self->reflow;
2757}
2758
2525sub _draw { 2759sub _draw {
2526 my ($self) = @_; 2760 my ($self) = @_;
2527 2761
2528 glEnable GL_TEXTURE_2D; 2762 glEnable GL_TEXTURE_2D;
2529 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2763 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2530 glColor 1, 1, 1, 1; 2764 glColor 0, 0, 0, 1;
2531 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2765 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2532 glDisable GL_TEXTURE_2D; 2766 glDisable GL_TEXTURE_2D;
2533 2767
2534 $self->{children}[1]->draw; 2768 $self->{children}[1]->draw;
2535
2536} 2769}
2537 2770
2538############################################################################# 2771#############################################################################
2539 2772
2540package CFClient::UI::Animator; 2773package CFClient::UI::Animator;
2627 $tooltip .= "\n\n" . (ref $widget) . "\n" 2860 $tooltip .= "\n\n" . (ref $widget) . "\n"
2628 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 2861 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
2629 . "req $widget->{req_w} $widget->{req_h}\n" 2862 . "req $widget->{req_w} $widget->{req_h}\n"
2630 . "visible $widget->{visible}"; 2863 . "visible $widget->{visible}";
2631 } 2864 }
2865
2866 $tooltip =~ s/^\n+//;
2867 $tooltip =~ s/\n+$//;
2632 2868
2633 $self->add (new CFClient::UI::Label 2869 $self->add (new CFClient::UI::Label
2634 markup => $tooltip, 2870 markup => $tooltip,
2635 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2871 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2636 fontsize => 0.8, 2872 fontsize => 0.8,
2646 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2882 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2647 2883
2648 ($w + 4, $h + 4) 2884 ($w + 4, $h + 4)
2649} 2885}
2650 2886
2651sub size_allocate { 2887sub invoke_size_allocate {
2652 my ($self, $w, $h) = @_; 2888 my ($self, $w, $h) = @_;
2653 2889
2654 $self->SUPER::size_allocate ($w - 4, $h - 4); 2890 $self->SUPER::invoke_size_allocate ($w - 4, $h - 4)
2655} 2891}
2656 2892
2657sub visibility_change { 2893sub invoke_visibility_change {
2658 my ($self, $visible) = @_; 2894 my ($self, $visible) = @_;
2659 2895
2660 return unless $visible; 2896 return unless $visible;
2661 2897
2662 $self->{root}->on_post_alloc ("move_$self" => sub { 2898 $self->{root}->on_post_alloc ("move_$self" => sub {
2664 or return; 2900 or return;
2665 2901
2666 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 2902 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2667 2903
2668 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 2904 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2669 if $x + $self->{w} > $::WIDTH; 2905 if $x + $self->{w} > $self->{root}{w};
2670 2906
2671 $self->move_abs ($x, $y); 2907 $self->move_abs ($x, $y);
2672 }); 2908 });
2673} 2909}
2674 2910
2763 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; 2999 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2764 3000
2765 if ($tex) { 3001 if ($tex) {
2766 glEnable GL_TEXTURE_2D; 3002 glEnable GL_TEXTURE_2D;
2767 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3003 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2768 glColor 1, 1, 1, 1; 3004 glColor 0, 0, 0, 1;
2769 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3005 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2770 glDisable GL_TEXTURE_2D; 3006 glDisable GL_TEXTURE_2D;
2771 } 3007 }
2772} 3008}
2773 3009
2774sub DESTROY { 3010sub destroy {
2775 my ($self) = @_; 3011 my ($self) = @_;
2776 3012
2777 $self->{timer}->cancel 3013 $self->{timer}->cancel
2778 if $self->{timer}; 3014 if $self->{timer};
2779 3015
2780 $self->SUPER::DESTROY; 3016 $self->SUPER::destroy;
2781} 3017}
2782 3018
2783############################################################################# 3019#############################################################################
2784 3020
2785package CFClient::UI::Inventory; 3021package CFClient::UI::Buttonbar;
2786 3022
2787our @ISA = CFClient::UI::ScrolledWindow::; 3023our @ISA = CFClient::UI::HBox::;
2788 3024
2789sub new { 3025# TODO: should actualyl wrap buttons and other goodies.
2790 my $class = shift;
2791
2792 my $self = $class->SUPER::new (
2793 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2794 @_,
2795 );
2796
2797 $self
2798}
2799
2800sub set_items {
2801 my ($self, $items) = @_;
2802
2803 $self->{scrolled}->clear;
2804 return unless $items;
2805
2806 my @items = sort {
2807 ($a->{type} <=> $b->{type})
2808 or ($a->{name} cmp $b->{name})
2809 } @$items;
2810
2811 $self->{real_items} = \@items;
2812
2813 my $row = 0;
2814 for my $item (@items) {
2815 CFClient::Item::update_widgets $item;
2816
2817 $self->{scrolled}->add (0, $row, $item->{face_widget});
2818 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2819 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2820
2821 $row++;
2822 }
2823}
2824 3026
2825############################################################################# 3027#############################################################################
2826 3028
2827package CFClient::UI::Menu; 3029package CFClient::UI::Menu;
2828 3030
2840 ); 3042 );
2841 3043
2842 $self->add ($self->{vbox} = new CFClient::UI::VBox); 3044 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2843 3045
2844 for my $item (@{ $self->{items} }) { 3046 for my $item (@{ $self->{items} }) {
2845 my ($widget, $cb) = @$item; 3047 my ($widget, $cb, $tooltip) = @$item;
2846 3048
2847 # handle various types of items, only text for now 3049 # handle various types of items, only text for now
2848 if (!ref $widget) { 3050 if (!ref $widget) {
2849 $widget = new CFClient::UI::Label 3051 $widget = new CFClient::UI::Label
2850 can_hover => 1, 3052 can_hover => 1,
2851 can_events => 1, 3053 can_events => 1,
2852 text => $widget; 3054 markup => $widget,
3055 tooltip => $tooltip
2853 } 3056 }
2854 3057
2855 $self->{item}{$widget} = $item; 3058 $self->{item}{$widget} = $item;
2856 3059
2857 $self->{vbox}->add ($widget); 3060 $self->{vbox}->add ($widget);
2862 3065
2863# popup given the event (must be a mouse button down event currently) 3066# popup given the event (must be a mouse button down event currently)
2864sub popup { 3067sub popup {
2865 my ($self, $ev) = @_; 3068 my ($self, $ev) = @_;
2866 3069
2867 $self->_emit ("popdown"); 3070 $self->emit ("popdown");
2868 3071
2869 # maybe save $GRAB? must be careful about events... 3072 # maybe save $GRAB? must be careful about events...
2870 $GRAB = $self; 3073 $GRAB = $self;
2871 $self->{button} = $ev->{button}; 3074 $self->{button} = $ev->{button};
2872 3075
2873 $self->show; 3076 $self->show;
2874 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3077 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2875} 3078}
2876 3079
2877sub mouse_motion { 3080sub invoke_mouse_motion {
2878 my ($self, $ev, $x, $y) = @_; 3081 my ($self, $ev, $x, $y) = @_;
2879 3082
2880 # TODO: should use vbox->find_widget or so 3083 # TODO: should use vbox->find_widget or so
2881 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 3084 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2882 $self->{hover} = $self->{item}{$HOVER}; 3085 $self->{hover} = $self->{item}{$HOVER};
2883}
2884 3086
3087 0
3088}
3089
2885sub button_up { 3090sub invoke_button_up {
2886 my ($self, $ev, $x, $y) = @_; 3091 my ($self, $ev, $x, $y) = @_;
2887 3092
2888 if ($ev->{button} == $self->{button}) { 3093 if ($ev->{button} == $self->{button}) {
2889 undef $GRAB; 3094 undef $GRAB;
2890 $self->hide; 3095 $self->hide;
2891 3096
2892 $self->_emit ("popdown"); 3097 $self->emit ("popdown");
2893 $self->{hover}[1]->() if $self->{hover}; 3098 $self->{hover}[1]->() if $self->{hover};
3099 } else {
3100 return 0
3101 }
3102
2894 } 3103 1
2895} 3104}
2896 3105
2897############################################################################# 3106#############################################################################
2898 3107
2899package CFClient::UI::Statusbox; 3108package CFClient::UI::Multiplexer;
2900 3109
2901our @ISA = CFClient::UI::VBox::; 3110our @ISA = CFClient::UI::Container::;
2902 3111
2903sub new { 3112sub new {
2904 my $class = shift; 3113 my $class = shift;
2905 3114
2906 $class->SUPER::new ( 3115 my $self = $class->SUPER::new (
3116 @_,
3117 );
3118
3119 $self->{current} = $self->{children}[0]
3120 if @{ $self->{children} };
3121
3122 $self
3123}
3124
3125sub add {
3126 my ($self, @widgets) = @_;
3127
3128 $self->SUPER::add (@widgets);
3129
3130 $self->{current} = $self->{children}[0]
3131 if @{ $self->{children} };
3132}
3133
3134sub set_current_page {
3135 my ($self, $page_or_widget) = @_;
3136
3137 my $widget = ref $page_or_widget
3138 ? $page_or_widget
3139 : $self->{children}[$page_or_widget];
3140
3141 $self->{current} = $widget;
3142 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3143
3144 $self->emit (page_changed => $self->{current});
3145
3146 $self->realloc;
3147}
3148
3149sub visible_children {
3150 $_[0]{current}
3151}
3152
3153sub size_request {
3154 my ($self) = @_;
3155
3156 $self->{current}->size_request
3157}
3158
3159sub invoke_size_allocate {
3160 my ($self, $w, $h) = @_;
3161
3162 $self->{current}->configure (0, 0, $w, $h);
3163
3164 1
3165}
3166
3167sub _draw {
3168 my ($self) = @_;
3169
3170 $self->{current}->draw;
3171}
3172
3173#############################################################################
3174
3175package CFClient::UI::Notebook;
3176
3177our @ISA = CFClient::UI::VBox::;
3178
3179sub new {
3180 my $class = shift;
3181
3182 my $self = $class->SUPER::new (
3183 buttonbar => (new CFClient::UI::Buttonbar),
3184 multiplexer => (new CFClient::UI::Multiplexer expand => 1),
3185 # filter => # will be put between multiplexer and $self
3186 @_,
3187 );
3188
3189 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3190 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3191
3192 $self
3193}
3194
3195sub add {
3196 my ($self, $title, $widget, $tooltip) = @_;
3197
3198 Scalar::Util::weaken $self;
3199
3200 $self->{buttonbar}->add (new CFClient::UI::Button
3201 markup => $title,
3202 tooltip => $tooltip,
3203 on_activate => sub { $self->set_current_page ($widget) },
3204 );
3205
3206 $self->{multiplexer}->add ($widget);
3207}
3208
3209sub set_current_page {
3210 my ($self, $page) = @_;
3211
3212 $self->{multiplexer}->set_current_page ($page);
3213 $self->emit (page_changed => $self->{multiplexer}{current});
3214}
3215
3216#############################################################################
3217
3218package CFClient::UI::Combobox;
3219
3220use utf8;
3221
3222our @ISA = CFClient::UI::Button::;
3223
3224sub new {
3225 my $class = shift;
3226
3227 my $self = $class->SUPER::new (
3228 options => [], # [value, title, longdesc], ...
3229 value => undef,
3230 @_,
3231 );
3232
3233 $self->_set_value ($self->{value});
3234
3235 $self
3236}
3237
3238sub invoke_button_down {
3239 my ($self, $ev) = @_;
3240
3241 my @menu_items;
3242
3243 for (@{ $self->{options} }) {
3244 my ($value, $title, $tooltip) = @$_;
3245
3246 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3247 }
3248
3249 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
3250}
3251
3252sub _set_value {
3253 my ($self, $value) = @_;
3254
3255 my ($item) = grep $_->[0] eq $value, @{ $self->{options} }
3256 or return;
3257
3258 $self->{value} = $item->[0];
3259 $self->set_markup ("$item->[1] ⇓");
3260 $self->set_tooltip ($item->[2]);
3261}
3262
3263sub set_value {
3264 my ($self, $value) = @_;
3265
3266 return unless $self->{value} ne $value;
3267
3268 $self->_set_value ($value);
3269 $self->emit (changed => $value);
3270}
3271
3272#############################################################################
3273
3274package CFClient::UI::Statusbox;
3275
3276our @ISA = CFClient::UI::VBox::;
3277
3278sub new {
3279 my $class = shift;
3280
3281 my $self = $class->SUPER::new (
2907 fontsize => 0.8, 3282 fontsize => 0.8,
2908 @_, 3283 @_,
2909 ) 3284 );
3285
3286 Scalar::Util::weaken (my $this = $self);
3287
3288 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3289
3290 $self
2910} 3291}
2911 3292
2912sub reorder { 3293sub reorder {
2913 my ($self) = @_; 3294 my ($self) = @_;
2914 my $NOW = time; 3295 my $NOW = Time::HiRes::time;
3296
3297 # freeze display when hovering over any label
3298 return if $CFClient::UI::TOOLTIP->{owner}
3299 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label},
3300 values %{ $self->{item} };
2915 3301
2916 while (my ($k, $v) = each %{ $self->{item} }) { 3302 while (my ($k, $v) = each %{ $self->{item} }) {
2917 delete $self->{item}{$k} if $v->{timeout} < $NOW; 3303 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2918 } 3304 }
2919 3305
2922 my @items = sort { 3308 my @items = sort {
2923 $a->{pri} <=> $b->{pri} 3309 $a->{pri} <=> $b->{pri}
2924 or $b->{id} <=> $a->{id} 3310 or $b->{id} <=> $a->{id}
2925 } values %{ $self->{item} }; 3311 } values %{ $self->{item} };
2926 3312
3313 $self->{timer}->interval (1);
3314
2927 my $count = 10 + 1; 3315 my $count = 10 + 1;
2928 for my $item (@items) { 3316 for my $item (@items) {
2929 last unless --$count; 3317 last unless --$count;
2930 3318
2931 push @widgets, $item->{label} ||= do { 3319 my $label = $item->{label} ||= do {
2932 # TODO: doesn't handle markup well (read as: at all) 3320 # TODO: doesn't handle markup well (read as: at all)
2933 my $short = $item->{count} > 1 3321 my $short = $item->{count} > 1
2934 ? "<b>$item->{count} ×</b> $item->{text}" 3322 ? "<b>$item->{count} ×</b> $item->{text}"
2935 : $item->{text}; 3323 : $item->{text};
2936 3324
2944 tooltip => $item->{tooltip}, 3332 tooltip => $item->{tooltip},
2945 tooltip_font => $::FONT_PROP, 3333 tooltip_font => $::FONT_PROP,
2946 tooltip_width => 0.67, 3334 tooltip_width => 0.67,
2947 fontsize => $item->{fontsize} || $self->{fontsize}, 3335 fontsize => $item->{fontsize} || $self->{fontsize},
2948 max_w => $::WIDTH * 0.44, 3336 max_w => $::WIDTH * 0.44,
2949 fg => $item->{fg}, 3337 fg => [@{ $item->{fg} }],
2950 can_events => 1, 3338 can_events => 1,
2951 can_hover => 1 3339 can_hover => 1
2952 }; 3340 };
3341
3342 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3343 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3344 $label->update;
3345 $label->set_max_size (undef, $label->{req_h} * $diff)
3346 if $diff < 1;
3347 $self->{timer}->interval (1/30);
3348 } else {
3349 $label->{fg}[3] = $item->{fg}[3] || 1;
3350 }
3351
3352 push @widgets, $label;
2953 } 3353 }
2954 3354
2955 $self->clear; 3355 $self->clear;
2956 $self->SUPER::add (reverse @widgets); 3356 $self->SUPER::add (reverse @widgets);
2957} 3357}
2962 $text =~ s/^\s+//; 3362 $text =~ s/^\s+//;
2963 $text =~ s/\s+$//; 3363 $text =~ s/\s+$//;
2964 3364
2965 return unless $text; 3365 return unless $text;
2966 3366
2967 my $timeout = time + ((delete $arg{timeout}) || 60); 3367 my $timeout = (int time) + ((delete $arg{timeout}) || 60);
2968 3368
2969 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 3369 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2970 3370
2971 if (my $item = $self->{item}{$group}) { 3371 if (my $item = $self->{item}{$group}) {
2972 if ($item->{text} eq $text) { 3372 if ($item->{text} eq $text) {
2973 $item->{count}++; 3373 $item->{count}++;
2974 } else { 3374 } else {
2975 $item->{count} = 1; 3375 $item->{count} = 1;
2976 $item->{text} = $item->{tooltip} = $text; 3376 $item->{text} = $item->{tooltip} = $text;
2977 } 3377 }
2978 $item->{id} = ++$self->{id}; 3378 $item->{id} += 0.2;#d#
2979 $item->{timeout} = $timeout; 3379 $item->{timeout} = $timeout;
2980 delete $item->{label}; 3380 delete $item->{label};
2981 } else { 3381 } else {
2982 $self->{item}{$group} = { 3382 $self->{item}{$group} = {
2983 id => ++$self->{id}, 3383 id => ++$self->{id},
3002 3402
3003 $self->reorder; 3403 $self->reorder;
3004 $self->SUPER::reconfigure; 3404 $self->SUPER::reconfigure;
3005} 3405}
3006 3406
3407sub destroy {
3408 my ($self) = @_;
3409
3410 $self->{timer}->cancel;
3411
3412 $self->SUPER::destroy;
3413}
3414
3007############################################################################# 3415#############################################################################
3008 3416
3009package CFClient::UI::Root; 3417package CFClient::UI::Inventory;
3010 3418
3011our @ISA = CFClient::UI::Container::; 3419our @ISA = CFClient::UI::ScrolledWindow::;
3012
3013use CFClient::OpenGL;
3014 3420
3015sub new { 3421sub new {
3016 my $class = shift; 3422 my $class = shift;
3017 3423
3018 my $self = $class->SUPER::new ( 3424 my $self = $class->SUPER::new (
3019 visible => 1, 3425 child => (new CFClient::UI::Table col_expand => [0, 1, 0]),
3020 @_, 3426 @_,
3021 ); 3427 );
3022 3428
3023 Scalar::Util::weaken ($self->{root} = $self);
3024
3025 $self 3429 $self
3026} 3430}
3027 3431
3028sub size_request { 3432sub set_items {
3029 my ($self) = @_; 3433 my ($self, $items) = @_;
3030 3434
3031 ($self->{w}, $self->{h}) 3435 $self->{child}->clear;
3032} 3436 return unless $items;
3033 3437
3034sub _to_pixel { 3438 my @items = sort {
3035 my ($coord, $size, $max) = @_; 3439 ($a->{type} <=> $b->{type})
3440 or ($a->{name} cmp $b->{name})
3441 } @$items;
3036 3442
3037 $coord = 3443 $self->{real_items} = \@items;
3038 $coord eq "center" ? ($max - $size) * 0.5
3039 : $coord eq "max" ? $max
3040 : $coord;
3041 3444
3042 $coord = 0 if $coord < 0; 3445 my $row = 0;
3043 $coord = $max - $size if $coord > $max - $size; 3446 for my $item (@items) {
3447 CFClient::Item::update_widgets $item;
3044 3448
3045 int $coord + 0.5 3449 $self->{child}->add (0, $row, $item->{face_widget});
3046} 3450 $self->{child}->add (1, $row, $item->{desc_widget});
3451 $self->{child}->add (2, $row, $item->{weight_widget});
3047 3452
3048sub size_allocate { 3453 $row++;
3049 my ($self, $w, $h) = @_;
3050
3051 for my $child ($self->children) {
3052 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3053
3054 $X = $child->{force_x} if exists $child->{force_x};
3055 $Y = $child->{force_y} if exists $child->{force_y};
3056
3057 $X = _to_pixel $X, $W, $self->{w};
3058 $Y = _to_pixel $Y, $H, $self->{h};
3059
3060 $child->configure ($X, $Y, $W, $H);
3061 } 3454 }
3062}
3063
3064sub coord2local {
3065 my ($self, $x, $y) = @_;
3066
3067 ($x, $y)
3068}
3069
3070sub coord2global {
3071 my ($self, $x, $y) = @_;
3072
3073 ($x, $y)
3074}
3075
3076sub update {
3077 my ($self) = @_;
3078
3079 $::WANT_REFRESH++;
3080}
3081
3082sub add {
3083 my ($self, @children) = @_;
3084
3085 $_->{is_toplevel} = 1
3086 for @children;
3087
3088 $self->SUPER::add (@children);
3089}
3090
3091sub remove {
3092 my ($self, @children) = @_;
3093
3094 $self->SUPER::remove (@children);
3095
3096 delete $self->{is_toplevel}
3097 for @children;
3098
3099 while (@children) {
3100 my $w = pop @children;
3101 push @children, $w->children;
3102 $w->set_invisible;
3103 }
3104}
3105
3106sub on_refresh {
3107 my ($self, $id, $cb) = @_;
3108
3109 $self->{refresh_hook}{$id} = $cb;
3110}
3111
3112sub on_post_alloc {
3113 my ($self, $id, $cb) = @_;
3114
3115 $self->{post_alloc_hook}{$id} = $cb;
3116}
3117
3118sub draw {
3119 my ($self) = @_;
3120
3121 while ($self->{refresh_hook}) {
3122 $_->()
3123 for values %{delete $self->{refresh_hook}};
3124 }
3125
3126 if ($self->{realloc}) {
3127 my @queue;
3128
3129 while () {
3130 if ($self->{realloc}) {
3131 #TODO use array-of-depth approach
3132
3133 use sort 'stable';
3134
3135 @queue = sort { $a->{visible} <=> $b->{visible} }
3136 @queue, values %{delete $self->{realloc}};
3137 }
3138
3139 my $widget = pop @queue || last;
3140
3141 $widget->{visible} or last; # do not resize invisible widgets
3142
3143 my ($w, $h) = $widget->size_request;
3144
3145 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3146 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3147
3148 $w = $widget->{force_w} if exists $widget->{force_w};
3149 $h = $widget->{force_h} if exists $widget->{force_h};
3150
3151 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3152 || delete $widget->{force_realloc}) {
3153 $widget->{req_w} = $w;
3154 $widget->{req_h} = $h;
3155
3156 $self->{size_alloc}{$widget+0} = $widget;
3157
3158 if (my $parent = $widget->{parent}) {
3159 $self->{realloc}{$parent+0} = $parent;
3160 #unshift @queue, $parent;
3161 $parent->{force_size_alloc} = 1;
3162 $self->{size_alloc}{$parent+0} = $parent;
3163 }
3164 }
3165
3166 delete $self->{realloc}{$widget+0};
3167 }
3168 }
3169
3170 while (my $size_alloc = delete $self->{size_alloc}) {
3171 my @queue = sort { $b->{visible} <=> $a->{visible} }
3172 values %$size_alloc;
3173
3174 while () {
3175 my $widget = pop @queue || last;
3176
3177 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3178
3179 $w = 0 if $w < 0;
3180 $h = 0 if $h < 0;
3181
3182 $w = int $w + 0.5;
3183 $h = int $h + 0.5;
3184
3185 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3186 $widget->{w} = $w;
3187 $widget->{h} = $h;
3188
3189 $widget->emit (size_allocate => $w, $h);
3190 }
3191 }
3192 }
3193
3194 while ($self->{post_alloc_hook}) {
3195 $_->()
3196 for values %{delete $self->{post_alloc_hook}};
3197 }
3198
3199
3200 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3201 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3202 glClear GL_COLOR_BUFFER_BIT;
3203
3204 glMatrixMode GL_PROJECTION;
3205 glLoadIdentity;
3206 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3207 glMatrixMode GL_MODELVIEW;
3208 glLoadIdentity;
3209
3210 $self->_draw;
3211} 3455}
3212 3456
3213############################################################################# 3457#############################################################################
3214 3458
3215package CFClient::UI::BindEditor; 3459package CFClient::UI::BindEditor;
3274 $self->update_binding_widgets; 3518 $self->update_binding_widgets;
3275 3519
3276 $self 3520 $self
3277} 3521}
3278 3522
3523sub cfg_bind {
3524 my ($self, $mod, $sym, $cmds) = @_;
3525 $::CFG->{profile}{default}{bindings}{$mod}{$sym} = $cmds;
3526 ::update_bindings ();
3527}
3528
3529sub cfg_unbind {
3530 my ($self, $mod, $sym, $cmds) = @_;
3531 delete $::CFG->{profile}{default}{bindings}{$mod}{$sym};
3532 ::update_bindings ();
3533}
3534
3279sub commit { 3535sub commit {
3280 my ($self) = @_; 3536 my ($self) = @_;
3281 my ($mod, $sym, $cmds) = $self->get_binding; 3537 my ($mod, $sym, $cmds) = $self->get_binding;
3282 if ($sym != 0 && @$cmds > 0) { 3538 if ($sym != 0 && @$cmds > 0) {
3283 $::STATUSBOX->add ("Bound actions to '".CFClient::Binder::keycombo_to_name ($mod, $sym) 3539 $::STATUSBOX->add ("Bound actions to '".CFClient::Binder::keycombo_to_name ($mod, $sym)
3310 $rec = $::CONN->stop_record if $::CONN; 3566 $rec = $::CONN->stop_record if $::CONN;
3311 return unless ref $rec eq 'ARRAY'; 3567 return unless ref $rec eq 'ARRAY';
3312 $self->set_command_list ($rec); 3568 $self->set_command_list ($rec);
3313} 3569}
3314 3570
3315# if $commit is true, the binding will be set after the user entered a key combo 3571
3572sub ask_for_bind_and_commit {
3573 my ($self) = @_;
3574 $self->ask_for_bind (1);
3575}
3576
3316sub ask_for_bind { 3577sub ask_for_bind {
3317 my ($self, $commit) = @_; 3578 my ($self, $commit, $end_cb) = @_;
3318 3579
3319 CFClient::Binder::open_binding_dialog (sub { 3580 CFClient::Binder::open_binding_dialog (sub {
3320 my ($mod, $sym) = @_; 3581 my ($mod, $sym) = @_;
3321 $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak? 3582 $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak?
3322 $self->update_binding_widgets; 3583 $self->update_binding_widgets;
3323 $self->commit if $commit; 3584 $self->commit if $commit;
3585 $end_cb->() if $end_cb;
3324 }); 3586 });
3325} 3587}
3326 3588
3327# $mod and $sym are the modifiers and key symbol 3589# $mod and $sym are the modifiers and key symbol
3328# $cmds is a array ref of strings (the commands) 3590# $cmds is a array ref of strings (the commands)
3346} 3608}
3347 3609
3348# this is a shortcut method that asks for a binding 3610# this is a shortcut method that asks for a binding
3349# and then just binds it. 3611# and then just binds it.
3350sub do_quick_binding { 3612sub do_quick_binding {
3351 my ($self, $cmds) = @_; 3613 my ($self, $cmds, $end_cb) = @_;
3352 $self->set_binding (undef, undef, $cmds, sub { 3614 $self->set_binding (undef, undef, $cmds, sub { $self->cfg_bind (@_) });
3353 $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2];
3354 });
3355 $self->ask_for_bind (1); 3615 $self->ask_for_bind (1, $end_cb);
3356} 3616}
3357 3617
3358sub update_binding_widgets { 3618sub update_binding_widgets {
3359 my ($self) = @_; 3619 my ($self) = @_;
3360 my ($mod, $sym, $cmds) = $self->get_binding; 3620 my ($mod, $sym, $cmds) = $self->get_binding;
3400 3660
3401 $idx++ 3661 $idx++
3402 } 3662 }
3403} 3663}
3404 3664
3405
3406############################################################################# 3665#############################################################################
3407 3666
3408package CFClient::UI::SpellList; 3667package CFClient::UI::SpellList;
3409 3668
3410our @ISA = CFClient::UI::FancyFrame::; 3669our @ISA = CFClient::UI::Table::;
3411 3670
3412sub new { 3671sub new {
3413 my $class = shift; 3672 my $class = shift;
3414 3673
3415 my $self = $class->SUPER::new (binding => [], commands => [], @_); 3674 my $self = $class->SUPER::new (
3416 3675 binding => [],
3417 $self->add (new CFClient::UI::ScrolledWindow 3676 commands => [],
3418 scrolled => $self->{spellbox} = new CFClient::UI::Table); 3677 @_,
3419 3678 )
3420 $self;
3421} 3679}
3422 3680
3423# XXX: Do sorting? Argl... 3681my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3682
3683my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3684 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3685my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3686 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3687my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3688 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3689my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3690 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3691my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3692 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3693
3694sub rebuild_spell_list {
3695 my ($self) = @_;
3696
3697 $CFClient::UI::ROOT->on_refresh ($self => sub {
3698 $self->clear;
3699
3700 return unless $::CONN;
3701
3702 $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3703 $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL);
3704 $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3705 $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3706 $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3707
3708 my $row = 0;
3709
3710 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3711 my $spell = $self->{spell}{$_};
3712
3713 $row++;
3714
3715 my $spell_cb = sub {
3716 my ($widget, $ev) = @_;
3717
3718 if ($ev->{button} == 1) {
3719 $::CONN->user_send ("cast $spell->{name}");
3720 } elsif ($ev->{button} == 2) {
3721 $::CONN->user_send ("invoke $spell->{name}");
3722 } elsif ($ev->{button} == 3) {
3723 (new CFClient::UI::Menu
3724 items => [
3725 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3726 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3727 ],
3728 )->popup ($ev);
3729 } else {
3730 return 0;
3731 }
3732
3733 1
3734 };
3735
3736 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3737
3738 #TODO: add path info to tooltip
3739 #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path});
3740
3741 $self->add (0, $row, new CFClient::UI::Face
3742 face => $spell->{face},
3743 can_hover => 1,
3744 can_events => 1,
3745 tooltip => $tooltip,
3746 on_button_down => $spell_cb,
3747 );
3748
3749 $self->add (1, $row, new CFClient::UI::Label
3750 expand => 1,
3751 text => $spell->{name},
3752 can_hover => 1,
3753 can_events => 1,
3754 tooltip => $tooltip,
3755 on_button_down => $spell_cb,
3756 );
3757
3758 $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3759 $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3760 $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3761 $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3762 }
3763 });
3764}
3765
3424sub add_spell { 3766sub add_spell {
3425 my ($self, $spell) = @_; 3767 my ($self, $spell) = @_;
3768
3426 $self->{spells}->{$spell->{name}} = $spell; 3769 $self->{spell}->{$spell->{name}} = $spell;
3427 3770 $self->rebuild_spell_list;
3428 $self->{spellbox}->add (0, $self->{tbl_idx}, new CFClient::UI::Face
3429 face => $spell->{face},
3430 can_hover => 1,
3431 can_events => 1,
3432 tooltip => $spell->{message});
3433
3434 $self->{spellbox}->add (1, $self->{tbl_idx}, new CFClient::UI::Label
3435 text => $spell->{name},
3436 can_hover => 1,
3437 can_events => 1,
3438 tooltip => $spell->{message},
3439 expand => 1);
3440
3441 $self->{spellbox}->add (2, $self->{tbl_idx}, new CFClient::UI::Label
3442 text => (sprintf "lvl: %2d sp: %2d dmg: %2d",
3443 $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}),
3444 expand => 1);
3445
3446 $self->{spellbox}->add (3, $self->{tbl_idx}++, new CFClient::UI::Button
3447 text => "bind to key",
3448 on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) });
3449}
3450
3451sub rebuild_spell_list {
3452 my ($self) = @_;
3453 $self->{tbl_idx} = 0;
3454 $self->add_spell ($_) for values %{$self->{spells}};
3455} 3771}
3456 3772
3457sub remove_spell { 3773sub remove_spell {
3458 my ($self, $spell) = @_; 3774 my ($self, $spell) = @_;
3775
3459 delete $self->{spells}->{$spell->{name}}; 3776 delete $self->{spell}->{$spell->{name}};
3460 $self->rebuild_spell_list; 3777 $self->rebuild_spell_list;
3778}
3779
3780sub clear_spells {
3781 my ($self) = @_;
3782
3783 $self->{spell} = {};
3784 $self->rebuild_spell_list;
3785}
3786
3787#############################################################################
3788
3789package CFClient::UI::Root;
3790
3791our @ISA = CFClient::UI::Container::;
3792
3793use List::Util qw(min max);
3794
3795use CFClient::OpenGL;
3796
3797sub new {
3798 my $class = shift;
3799
3800 my $self = $class->SUPER::new (
3801 visible => 1,
3802 @_,
3803 );
3804
3805 Scalar::Util::weaken ($self->{root} = $self);
3806
3807 $self
3808}
3809
3810sub size_request {
3811 my ($self) = @_;
3812
3813 ($self->{w}, $self->{h})
3814}
3815
3816sub _to_pixel {
3817 my ($coord, $size, $max) = @_;
3818
3819 $coord =
3820 $coord eq "center" ? ($max - $size) * 0.5
3821 : $coord eq "max" ? $max
3822 : $coord;
3823
3824 $coord = 0 if $coord < 0;
3825 $coord = $max - $size if $coord > $max - $size;
3826
3827 int $coord + 0.5
3828}
3829
3830sub invoke_size_allocate {
3831 my ($self, $w, $h) = @_;
3832
3833 for my $child ($self->children) {
3834 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3835
3836 $X = $child->{force_x} if exists $child->{force_x};
3837 $Y = $child->{force_y} if exists $child->{force_y};
3838
3839 $X = _to_pixel $X, $W, $self->{w};
3840 $Y = _to_pixel $Y, $H, $self->{h};
3841
3842 $child->configure ($X, $Y, $W, $H);
3843 }
3844
3845 1
3846}
3847
3848sub coord2local {
3849 my ($self, $x, $y) = @_;
3850
3851 ($x, $y)
3852}
3853
3854sub coord2global {
3855 my ($self, $x, $y) = @_;
3856
3857 ($x, $y)
3858}
3859
3860sub update {
3861 my ($self) = @_;
3862
3863 $::WANT_REFRESH++;
3864}
3865
3866sub add {
3867 my ($self, @children) = @_;
3868
3869 $_->{is_toplevel} = 1
3870 for @children;
3871
3872 $self->SUPER::add (@children);
3873}
3874
3875sub remove {
3876 my ($self, @children) = @_;
3877
3878 $self->SUPER::remove (@children);
3879
3880 delete $self->{is_toplevel}
3881 for @children;
3882
3883 while (@children) {
3884 my $w = pop @children;
3885 push @children, $w->children;
3886 $w->set_invisible;
3887 }
3888}
3889
3890sub on_refresh {
3891 my ($self, $id, $cb) = @_;
3892
3893 $self->{refresh_hook}{$id} = $cb;
3894}
3895
3896sub on_post_alloc {
3897 my ($self, $id, $cb) = @_;
3898
3899 $self->{post_alloc_hook}{$id} = $cb;
3900}
3901
3902sub draw {
3903 my ($self) = @_;
3904
3905 while ($self->{refresh_hook}) {
3906 $_->()
3907 for values %{delete $self->{refresh_hook}};
3908 }
3909
3910 if ($self->{realloc}) {
3911 my %queue;
3912 my @queue;
3913 my $widget;
3914
3915 outer:
3916 while () {
3917 if (my $realloc = delete $self->{realloc}) {
3918 for $widget (values %$realloc) {
3919 $widget->{visible} or next; # do not resize invisible widgets
3920
3921 $queue{$widget+0}++ and next; # duplicates are common
3922
3923 push @{ $queue[$widget->{visible}] }, $widget;
3924 }
3925 }
3926
3927 while () {
3928 @queue or last outer;
3929
3930 $widget = pop @{ $queue[-1] || [] }
3931 and last;
3932
3933 pop @queue;
3934 }
3935
3936 delete $queue{$widget+0};
3937
3938 my ($w, $h) = $widget->size_request;
3939
3940 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3941 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3942
3943 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3944 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3945
3946 $w = $widget->{force_w} if exists $widget->{force_w};
3947 $h = $widget->{force_h} if exists $widget->{force_h};
3948
3949 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3950 || delete $widget->{force_realloc}) {
3951 $widget->{req_w} = $w;
3952 $widget->{req_h} = $h;
3953
3954 $self->{size_alloc}{$widget+0} = $widget;
3955
3956 if (my $parent = $widget->{parent}) {
3957 $self->{realloc}{$parent+0} = $parent
3958 unless $queue{$parent+0};
3959
3960 $parent->{force_size_alloc} = 1;
3961 $self->{size_alloc}{$parent+0} = $parent;
3962 }
3963 }
3964
3965 delete $self->{realloc}{$widget+0};
3966 }
3967 }
3968
3969 while (my $size_alloc = delete $self->{size_alloc}) {
3970 my @queue = sort { $b->{visible} <=> $a->{visible} }
3971 values %$size_alloc;
3972
3973 while () {
3974 my $widget = pop @queue || last;
3975
3976 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3977
3978 $w = 0 if $w < 0;
3979 $h = 0 if $h < 0;
3980
3981 $w = int $w + 0.5;
3982 $h = int $h + 0.5;
3983
3984 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3985 $widget->{old_w} = $widget->{w};
3986 $widget->{old_h} = $widget->{h};
3987
3988 $widget->{w} = $w;
3989 $widget->{h} = $h;
3990
3991 $widget->emit (size_allocate => $w, $h);
3992 }
3993 }
3994 }
3995
3996 while ($self->{post_alloc_hook}) {
3997 $_->()
3998 for values %{delete $self->{post_alloc_hook}};
3999 }
4000
4001
4002 glViewport 0, 0, $::WIDTH, $::HEIGHT;
4003 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
4004 glClear GL_COLOR_BUFFER_BIT;
4005
4006 glMatrixMode GL_PROJECTION;
4007 glLoadIdentity;
4008 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4009 glMatrixMode GL_MODELVIEW;
4010 glLoadIdentity;
4011
4012 {
4013 package CFClient::UI::Base;
4014
4015 ($draw_x, $draw_y, $draw_w, $draw_h) =
4016 (0, 0, $self->{w}, $self->{h});
4017 }
4018
4019 $self->_draw;
3461} 4020}
3462 4021
3463############################################################################# 4022#############################################################################
3464 4023
3465package CFClient::UI; 4024package CFClient::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines