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.308 by root, Mon Jun 19 05:18:14 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 image => '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->{children};
1345 1444
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
2063 2239
2064 my $h1 = $self->{h} * (1 - $ycut1); 2240 my $h1 = $self->{h} * (1 - $ycut1);
2065 my $h2 = $self->{h} * (1 - $ycut2); 2241 my $h2 = $self->{h} * (1 - $ycut2);
2066 2242
2067 glEnable GL_BLEND; 2243 glEnable GL_BLEND;
2068 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2244 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
2245 GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2069 glEnable GL_TEXTURE_2D; 2246 glEnable GL_TEXTURE_2D;
2070 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2247 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2071 2248
2072 glBindTexture GL_TEXTURE_2D, $t1->{name}; 2249 glBindTexture GL_TEXTURE_2D, $t1->{name};
2073 glBegin GL_QUADS; 2250 glBegin GL_QUADS;
2187 $self->update; 2364 $self->update;
2188 2365
2189 $self 2366 $self
2190} 2367}
2191 2368
2192sub changed { }
2193
2194sub set_range { 2369sub set_range {
2195 my ($self, $range) = @_; 2370 my ($self, $range) = @_;
2196 2371
2197 ($range, $self->{range}) = ($self->{range}, $range); 2372 ($range, $self->{range}) = ($self->{range}, $range);
2198 2373
2199 $self->update
2200 if "@$range" ne "@{$self->{range}}"; 2374 if ("@$range" ne "@{$self->{range}}") {
2375 $self->update;
2376 $self->set_value ($self->{range}[0]);
2377 }
2201} 2378}
2202 2379
2203sub set_value { 2380sub set_value {
2204 my ($self, $value) = @_; 2381 my ($self, $value) = @_;
2205 2382
2216 if $unit; 2393 if $unit;
2217 2394
2218 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2395 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2219 2396
2220 if ($value != $old_value) { 2397 if ($value != $old_value) {
2221 $self->_emit (changed => $value); 2398 $self->emit (changed => $value);
2222 $self->update; 2399 $self->update;
2223 } 2400 }
2224} 2401}
2225 2402
2226sub size_request { 2403sub size_request {
2227 my ($self) = @_; 2404 my ($self) = @_;
2228 2405
2229 ($self->{req_w}, $self->{req_h}) 2406 ($self->{req_w}, $self->{req_h})
2230} 2407}
2231 2408
2232sub button_down { 2409sub invoke_button_down {
2233 my ($self, $ev, $x, $y) = @_; 2410 my ($self, $ev, $x, $y) = @_;
2234 2411
2235 $self->SUPER::button_down ($ev, $x, $y); 2412 $self->SUPER::invoke_button_down ($ev, $x, $y);
2236 2413
2237 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2414 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2238 2415
2239 $self->mouse_motion ($ev, $x, $y); 2416 $self->invoke_mouse_motion ($ev, $x, $y)
2240} 2417}
2241 2418
2242sub mouse_motion { 2419sub invoke_mouse_motion {
2243 my ($self, $ev, $x, $y) = @_; 2420 my ($self, $ev, $x, $y) = @_;
2244 2421
2245 if ($GRAB == $self) { 2422 if ($GRAB == $self) {
2246 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2423 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2247 2424
2248 my (undef, $lo, $hi, $page) = @{$self->{range}}; 2425 my (undef, $lo, $hi, $page) = @{$self->{range}};
2249 2426
2250 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 2427 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2251 2428
2252 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 2429 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2430 } else {
2431 return 0;
2432 }
2433
2253 } 2434 1
2254} 2435}
2255 2436
2256sub update { 2437sub update {
2257 my ($self) = @_; 2438 my ($self) = @_;
2258 2439
2259 $CFClient::UI::ROOT->on_post_alloc ($self => sub { 2440 delete $self->{knob_w};
2441 $self->SUPER::update;
2442}
2443
2444sub _draw {
2445 my ($self) = @_;
2446
2447 unless ($self->{knob_w}) {
2260 $self->set_value ($self->{range}[0]); 2448 $self->set_value ($self->{range}[0]);
2261 2449
2262 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2450 my ($value, $lo, $hi, $page) = @{$self->{range}};
2263 my $range = ($hi - $page - $lo) || 1e-100; 2451 my $range = ($hi - $page - $lo) || 1e-100;
2264 2452
2270 $value = ($value - $lo) / $range; 2458 $value = ($value - $lo) / $range;
2271 $value = $value * $self->{scale} + $self->{offset}; 2459 $value = $value * $self->{scale} + $self->{offset};
2272 2460
2273 $self->{knob_x} = $value - $knob_w * 0.5; 2461 $self->{knob_x} = $value - $knob_w * 0.5;
2274 $self->{knob_w} = $knob_w; 2462 $self->{knob_w} = $knob_w;
2275 }); 2463 }
2276
2277 $self->SUPER::update;
2278}
2279
2280sub _draw {
2281 my ($self) = @_;
2282 2464
2283 $self->SUPER::_draw (); 2465 $self->SUPER::_draw ();
2284 2466
2285 glScale $self->{w}, $self->{h}; 2467 glScale $self->{w}, $self->{h};
2286 2468
2347sub set_range { shift->{slider}->set_range (@_) } 2529sub set_range { shift->{slider}->set_range (@_) }
2348sub set_value { shift->{slider}->set_value (@_) } 2530sub set_value { shift->{slider}->set_value (@_) }
2349 2531
2350############################################################################# 2532#############################################################################
2351 2533
2352package CFClient::UI::TextView; 2534package CFClient::UI::TextScroller;
2353 2535
2354our @ISA = CFClient::UI::HBox::; 2536our @ISA = CFClient::UI::HBox::;
2355 2537
2356use CFClient::OpenGL; 2538use CFClient::OpenGL;
2357 2539
2359 my $class = shift; 2541 my $class = shift;
2360 2542
2361 my $self = $class->SUPER::new ( 2543 my $self = $class->SUPER::new (
2362 fontsize => 1, 2544 fontsize => 1,
2363 can_events => 0, 2545 can_events => 0,
2546 indent => 0,
2364 #font => default_font 2547 #font => default_font
2365 @_, 2548 @_,
2366 2549
2367 layout => (new CFClient::Layout 1), 2550 layout => (new CFClient::Layout 1),
2368 par => [], 2551 par => [],
2383 2566
2384 $self->{fontsize} = $fontsize; 2567 $self->{fontsize} = $fontsize;
2385 $self->reflow; 2568 $self->reflow;
2386} 2569}
2387 2570
2388sub size_allocate { 2571sub invoke_size_allocate {
2389 my ($self, $w, $h) = @_; 2572 my ($self, $w, $h) = @_;
2390
2391 $self->SUPER::size_allocate ($w, $h);
2392 2573
2393 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2574 $self->{layout}->set_font ($self->{font}) if $self->{font};
2394 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2575 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2395 $self->{layout}->set_width ($self->{children}[0]{w}); 2576 $self->{layout}->set_width ($self->{children}[0]{w});
2577 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2396 2578
2397 $self->reflow; 2579 $self->reflow;
2580
2581 $self->SUPER::invoke_size_allocate ($w, $h)
2398} 2582}
2399 2583
2400sub text_size { 2584sub text_size {
2401 my ($self, $text, $indent) = @_; 2585 my ($self, $text, $indent) = @_;
2402 2586
2403 my $layout = $self->{layout}; 2587 my $layout = $self->{layout};
2404 2588
2405 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2589 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2406 $layout->set_width ($self->{children}[0]{w} - $indent); 2590 $layout->set_width ($self->{children}[0]{w} - $indent);
2591 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2407 $layout->set_markup ($text); 2592 $layout->set_markup ($text);
2408 2593
2409 $layout->size 2594 $layout->size
2410} 2595}
2411 2596
2450 2635
2451 return unless $self->{h} > 0; 2636 return unless $self->{h} > 0;
2452 2637
2453 delete $self->{texture}; 2638 delete $self->{texture};
2454 2639
2455 $ROOT->on_post_alloc ($self, sub { 2640 $ROOT->on_post_alloc ($self => sub {
2456 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2641 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2457 2642
2458 if (delete $self->{need_reflow}) { 2643 if (delete $self->{need_reflow}) {
2459 my $height = 0; 2644 my $height = 0;
2460 2645
2463 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2648 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2464 2649
2465 for (@{$self->{par}}) { 2650 for (@{$self->{par}}) {
2466 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support 2651 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2467 $layout->set_width ($W - $_->[3]); 2652 $layout->set_width ($W - $_->[3]);
2653 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2468 $layout->set_markup ($_->[4]); 2654 $layout->set_markup ($_->[4]);
2469 my ($w, $h) = $layout->size; 2655 my ($w, $h) = $layout->size;
2470 $_->[0] = $w + $_->[3]; 2656 $_->[0] = $w + $_->[3];
2471 $_->[1] = $h; 2657 $_->[1] = $h;
2472 } 2658 }
2475 } 2661 }
2476 2662
2477 $self->{height} = $height; 2663 $self->{height} = $height;
2478 2664
2479 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); 2665 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2480 2666
2481 delete $self->{texture}; 2667 delete $self->{texture};
2482 } 2668 }
2483 2669
2484 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 2670 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2485 glClearColor 0.5, 0.5, 0.5, 0; 2671 glClearColor 0, 0, 0, 0;
2486 glClear GL_COLOR_BUFFER_BIT; 2672 glClear GL_COLOR_BUFFER_BIT;
2487 2673
2488 my $top = int $self->{children}[1]{range}[0]; 2674 my $top = int $self->{children}[1]{range}[0];
2489 2675
2490 my $y0 = $top; 2676 my $y0 = $top;
2504 my $h = $par->[1]; 2690 my $h = $par->[1];
2505 2691
2506 if ($y0 < $y + $h && $y < $y1) { 2692 if ($y0 < $y + $h && $y < $y1) {
2507 $layout->set_foreground (@{ $par->[2] }); 2693 $layout->set_foreground (@{ $par->[2] });
2508 $layout->set_width ($W - $par->[3]); 2694 $layout->set_width ($W - $par->[3]);
2695 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2509 $layout->set_markup ($par->[4]); 2696 $layout->set_markup ($par->[4]);
2510 2697
2511 my ($w, $h, $data, $format, $internalformat) = $layout->render; 2698 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2512 2699
2513 glRasterPos $par->[3], $y - $y0; 2700 glRasterPos $par->[3], $y - $y0;
2525sub _draw { 2712sub _draw {
2526 my ($self) = @_; 2713 my ($self) = @_;
2527 2714
2528 glEnable GL_TEXTURE_2D; 2715 glEnable GL_TEXTURE_2D;
2529 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2530 glColor 1, 1, 1, 1; 2717 glColor 0, 0, 0, 1;
2531 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2718 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2532 glDisable GL_TEXTURE_2D; 2719 glDisable GL_TEXTURE_2D;
2533 2720
2534 $self->{children}[1]->draw; 2721 $self->{children}[1]->draw;
2535 2722
2536} 2723}
2627 $tooltip .= "\n\n" . (ref $widget) . "\n" 2814 $tooltip .= "\n\n" . (ref $widget) . "\n"
2628 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 2815 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
2629 . "req $widget->{req_w} $widget->{req_h}\n" 2816 . "req $widget->{req_w} $widget->{req_h}\n"
2630 . "visible $widget->{visible}"; 2817 . "visible $widget->{visible}";
2631 } 2818 }
2819
2820 $tooltip =~ s/^\n+//;
2821 $tooltip =~ s/\n+$//;
2632 2822
2633 $self->add (new CFClient::UI::Label 2823 $self->add (new CFClient::UI::Label
2634 markup => $tooltip, 2824 markup => $tooltip,
2635 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 2825 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2636 fontsize => 0.8, 2826 fontsize => 0.8,
2646 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2836 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2647 2837
2648 ($w + 4, $h + 4) 2838 ($w + 4, $h + 4)
2649} 2839}
2650 2840
2651sub size_allocate { 2841sub invoke_size_allocate {
2652 my ($self, $w, $h) = @_; 2842 my ($self, $w, $h) = @_;
2653 2843
2654 $self->SUPER::size_allocate ($w - 4, $h - 4); 2844 $self->SUPER::invoke_size_allocate ($w - 4, $h - 4)
2655} 2845}
2656 2846
2657sub visibility_change { 2847sub invoke_visibility_change {
2658 my ($self, $visible) = @_; 2848 my ($self, $visible) = @_;
2659 2849
2660 return unless $visible; 2850 return unless $visible;
2661 2851
2662 $self->{root}->on_post_alloc ("move_$self" => sub { 2852 $self->{root}->on_post_alloc ("move_$self" => sub {
2664 or return; 2854 or return;
2665 2855
2666 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 2856 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2667 2857
2668 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 2858 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2669 if $x + $self->{w} > $::WIDTH; 2859 if $x + $self->{w} > $self->{root}{w};
2670 2860
2671 $self->move_abs ($x, $y); 2861 $self->move_abs ($x, $y);
2672 }); 2862 });
2673} 2863}
2674 2864
2763 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; 2953 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2764 2954
2765 if ($tex) { 2955 if ($tex) {
2766 glEnable GL_TEXTURE_2D; 2956 glEnable GL_TEXTURE_2D;
2767 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2957 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2768 glColor 1, 1, 1, 1; 2958 glColor 0, 0, 0, 1;
2769 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2959 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2770 glDisable GL_TEXTURE_2D; 2960 glDisable GL_TEXTURE_2D;
2771 } 2961 }
2772} 2962}
2773 2963
2774sub DESTROY { 2964sub destroy {
2775 my ($self) = @_; 2965 my ($self) = @_;
2776 2966
2777 $self->{timer}->cancel 2967 $self->{timer}->cancel
2778 if $self->{timer}; 2968 if $self->{timer};
2779 2969
2780 $self->SUPER::DESTROY; 2970 $self->SUPER::destroy;
2781} 2971}
2782 2972
2783############################################################################# 2973#############################################################################
2784 2974
2785package CFClient::UI::Inventory; 2975package CFClient::UI::Buttonbar;
2786 2976
2787our @ISA = CFClient::UI::ScrolledWindow::; 2977our @ISA = CFClient::UI::HBox::;
2788 2978
2789sub new { 2979# 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 2980
2825############################################################################# 2981#############################################################################
2826 2982
2827package CFClient::UI::Menu; 2983package CFClient::UI::Menu;
2828 2984
2840 ); 2996 );
2841 2997
2842 $self->add ($self->{vbox} = new CFClient::UI::VBox); 2998 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2843 2999
2844 for my $item (@{ $self->{items} }) { 3000 for my $item (@{ $self->{items} }) {
2845 my ($widget, $cb) = @$item; 3001 my ($widget, $cb, $tooltip) = @$item;
2846 3002
2847 # handle various types of items, only text for now 3003 # handle various types of items, only text for now
2848 if (!ref $widget) { 3004 if (!ref $widget) {
2849 $widget = new CFClient::UI::Label 3005 $widget = new CFClient::UI::Label
2850 can_hover => 1, 3006 can_hover => 1,
2851 can_events => 1, 3007 can_events => 1,
2852 text => $widget; 3008 markup => $widget,
3009 tooltip => $tooltip
2853 } 3010 }
2854 3011
2855 $self->{item}{$widget} = $item; 3012 $self->{item}{$widget} = $item;
2856 3013
2857 $self->{vbox}->add ($widget); 3014 $self->{vbox}->add ($widget);
2862 3019
2863# popup given the event (must be a mouse button down event currently) 3020# popup given the event (must be a mouse button down event currently)
2864sub popup { 3021sub popup {
2865 my ($self, $ev) = @_; 3022 my ($self, $ev) = @_;
2866 3023
2867 $self->_emit ("popdown"); 3024 $self->emit ("popdown");
2868 3025
2869 # maybe save $GRAB? must be careful about events... 3026 # maybe save $GRAB? must be careful about events...
2870 $GRAB = $self; 3027 $GRAB = $self;
2871 $self->{button} = $ev->{button}; 3028 $self->{button} = $ev->{button};
2872 3029
2873 $self->show; 3030 $self->show;
2874 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3031 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2875} 3032}
2876 3033
2877sub mouse_motion { 3034sub invoke_mouse_motion {
2878 my ($self, $ev, $x, $y) = @_; 3035 my ($self, $ev, $x, $y) = @_;
2879 3036
2880 # TODO: should use vbox->find_widget or so 3037 # TODO: should use vbox->find_widget or so
2881 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 3038 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2882 $self->{hover} = $self->{item}{$HOVER}; 3039 $self->{hover} = $self->{item}{$HOVER};
2883}
2884 3040
3041 0
3042}
3043
2885sub button_up { 3044sub invoke_button_up {
2886 my ($self, $ev, $x, $y) = @_; 3045 my ($self, $ev, $x, $y) = @_;
2887 3046
2888 if ($ev->{button} == $self->{button}) { 3047 if ($ev->{button} == $self->{button}) {
2889 undef $GRAB; 3048 undef $GRAB;
2890 $self->hide; 3049 $self->hide;
2891 3050
2892 $self->_emit ("popdown"); 3051 $self->emit ("popdown");
2893 $self->{hover}[1]->() if $self->{hover}; 3052 $self->{hover}[1]->() if $self->{hover};
3053 } else {
3054 return 0
3055 }
3056
2894 } 3057 1
2895} 3058}
2896 3059
2897############################################################################# 3060#############################################################################
2898 3061
2899package CFClient::UI::Statusbox; 3062package CFClient::UI::Multiplexer;
2900 3063
2901our @ISA = CFClient::UI::VBox::; 3064our @ISA = CFClient::UI::Container::;
2902 3065
2903sub new { 3066sub new {
2904 my $class = shift; 3067 my $class = shift;
2905 3068
2906 $class->SUPER::new ( 3069 my $self = $class->SUPER::new (
3070 @_,
3071 );
3072
3073 $self->{current} = $self->{children}[0]
3074 if @{ $self->{children} };
3075
3076 $self
3077}
3078
3079sub add {
3080 my ($self, @widgets) = @_;
3081
3082 $self->SUPER::add (@widgets);
3083
3084 $self->{current} = $self->{children}[0]
3085 if @{ $self->{children} };
3086}
3087
3088sub set_current_page {
3089 my ($self, $page_or_widget) = @_;
3090
3091 my $widget = ref $page_or_widget
3092 ? $page_or_widget
3093 : $self->{children}[$page_or_widget];
3094
3095 $self->{current} = $widget;
3096 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3097
3098 $self->emit (page_changed => $self->{current});
3099
3100 $self->realloc;
3101}
3102
3103sub visible_children {
3104 $_[0]{current}
3105}
3106
3107sub size_request {
3108 my ($self) = @_;
3109
3110 $self->{current}->size_request
3111}
3112
3113sub invoke_size_allocate {
3114 my ($self, $w, $h) = @_;
3115
3116 $self->{current}->configure (0, 0, $w, $h);
3117
3118 1
3119}
3120
3121sub _draw {
3122 my ($self) = @_;
3123
3124 $self->{current}->draw;
3125}
3126
3127#############################################################################
3128
3129package CFClient::UI::Notebook;
3130
3131our @ISA = CFClient::UI::VBox::;
3132
3133sub new {
3134 my $class = shift;
3135
3136 my $self = $class->SUPER::new (
3137 buttonbar => (new CFClient::UI::Buttonbar),
3138 multiplexer => (new CFClient::UI::Multiplexer expand => 1),
3139 # filter => # will be put between multiplexer and $self
3140 @_,
3141 );
3142
3143 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3144 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3145
3146 $self
3147}
3148
3149sub add {
3150 my ($self, $title, $widget, $tooltip) = @_;
3151
3152 Scalar::Util::weaken $self;
3153
3154 $self->{buttonbar}->add (new CFClient::UI::Button
3155 markup => $title,
3156 tooltip => $tooltip,
3157 on_activate => sub { $self->set_current_page ($widget) },
3158 );
3159
3160 $self->{multiplexer}->add ($widget);
3161}
3162
3163sub set_current_page {
3164 my ($self, $page) = @_;
3165
3166 $self->{multiplexer}->set_current_page ($page);
3167 $self->emit (page_changed => $self->{multiplexer}{current});
3168}
3169
3170#############################################################################
3171
3172package CFClient::UI::Combobox;
3173
3174use utf8;
3175
3176our @ISA = CFClient::UI::Button::;
3177
3178sub new {
3179 my $class = shift;
3180
3181 my $self = $class->SUPER::new (
3182 options => [], # [value, title, longdesc], ...
3183 value => undef,
3184 @_,
3185 );
3186
3187 $self->_set_value ($self->{value});
3188
3189 $self
3190}
3191
3192sub invoke_button_down {
3193 my ($self, $ev) = @_;
3194
3195 my @menu_items;
3196
3197 for (@{ $self->{options} }) {
3198 my ($value, $title, $tooltip) = @$_;
3199
3200 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3201 }
3202
3203 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
3204}
3205
3206sub _set_value {
3207 my ($self, $value) = @_;
3208
3209 my ($item) = grep $_->[0] eq $value, @{ $self->{options} }
3210 or return;
3211
3212 $self->{value} = $item->[0];
3213 $self->set_markup ("$item->[1] ⇓");
3214 $self->set_tooltip ($item->[2]);
3215}
3216
3217sub set_value {
3218 my ($self, $value) = @_;
3219
3220 return unless $self->{value} ne $value;
3221
3222 $self->_set_value ($value);
3223 $self->emit (changed => $value);
3224}
3225
3226#############################################################################
3227
3228package CFClient::UI::Statusbox;
3229
3230our @ISA = CFClient::UI::VBox::;
3231
3232sub new {
3233 my $class = shift;
3234
3235 my $self = $class->SUPER::new (
2907 fontsize => 0.8, 3236 fontsize => 0.8,
2908 @_, 3237 @_,
2909 ) 3238 );
3239
3240 Scalar::Util::weaken (my $this = $self);
3241
3242 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3243
3244 $self
2910} 3245}
2911 3246
2912sub reorder { 3247sub reorder {
2913 my ($self) = @_; 3248 my ($self) = @_;
2914 my $NOW = time; 3249 my $NOW = Time::HiRes::time;
3250
3251 # freeze display when hovering over any label
3252 return if $CFClient::UI::TOOLTIP->{owner}
3253 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label},
3254 values %{ $self->{item} };
2915 3255
2916 while (my ($k, $v) = each %{ $self->{item} }) { 3256 while (my ($k, $v) = each %{ $self->{item} }) {
2917 delete $self->{item}{$k} if $v->{timeout} < $NOW; 3257 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2918 } 3258 }
2919 3259
2922 my @items = sort { 3262 my @items = sort {
2923 $a->{pri} <=> $b->{pri} 3263 $a->{pri} <=> $b->{pri}
2924 or $b->{id} <=> $a->{id} 3264 or $b->{id} <=> $a->{id}
2925 } values %{ $self->{item} }; 3265 } values %{ $self->{item} };
2926 3266
3267 $self->{timer}->interval (1);
3268
2927 my $count = 10 + 1; 3269 my $count = 10 + 1;
2928 for my $item (@items) { 3270 for my $item (@items) {
2929 last unless --$count; 3271 last unless --$count;
2930 3272
2931 push @widgets, $item->{label} ||= do { 3273 my $label = $item->{label} ||= do {
2932 # TODO: doesn't handle markup well (read as: at all) 3274 # TODO: doesn't handle markup well (read as: at all)
2933 my $short = $item->{count} > 1 3275 my $short = $item->{count} > 1
2934 ? "<b>$item->{count} ×</b> $item->{text}" 3276 ? "<b>$item->{count} ×</b> $item->{text}"
2935 : $item->{text}; 3277 : $item->{text};
2936 3278
2944 tooltip => $item->{tooltip}, 3286 tooltip => $item->{tooltip},
2945 tooltip_font => $::FONT_PROP, 3287 tooltip_font => $::FONT_PROP,
2946 tooltip_width => 0.67, 3288 tooltip_width => 0.67,
2947 fontsize => $item->{fontsize} || $self->{fontsize}, 3289 fontsize => $item->{fontsize} || $self->{fontsize},
2948 max_w => $::WIDTH * 0.44, 3290 max_w => $::WIDTH * 0.44,
2949 fg => $item->{fg}, 3291 fg => [@{ $item->{fg} }],
2950 can_events => 1, 3292 can_events => 1,
2951 can_hover => 1 3293 can_hover => 1
2952 }; 3294 };
3295
3296 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3297 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3298 $label->update;
3299 $label->set_max_size (undef, $label->{req_h} * $diff)
3300 if $diff < 1;
3301 $self->{timer}->interval (1/30);
3302 } else {
3303 $label->{fg}[3] = $item->{fg}[3] || 1;
3304 }
3305
3306 push @widgets, $label;
2953 } 3307 }
2954 3308
2955 $self->clear; 3309 $self->clear;
2956 $self->SUPER::add (reverse @widgets); 3310 $self->SUPER::add (reverse @widgets);
2957} 3311}
2962 $text =~ s/^\s+//; 3316 $text =~ s/^\s+//;
2963 $text =~ s/\s+$//; 3317 $text =~ s/\s+$//;
2964 3318
2965 return unless $text; 3319 return unless $text;
2966 3320
2967 my $timeout = time + ((delete $arg{timeout}) || 60); 3321 my $timeout = (int time) + ((delete $arg{timeout}) || 60);
2968 3322
2969 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 3323 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2970 3324
2971 if (my $item = $self->{item}{$group}) { 3325 if (my $item = $self->{item}{$group}) {
2972 if ($item->{text} eq $text) { 3326 if ($item->{text} eq $text) {
2973 $item->{count}++; 3327 $item->{count}++;
2974 } else { 3328 } else {
2975 $item->{count} = 1; 3329 $item->{count} = 1;
2976 $item->{text} = $item->{tooltip} = $text; 3330 $item->{text} = $item->{tooltip} = $text;
2977 } 3331 }
2978 $item->{id} = ++$self->{id}; 3332 $item->{id} += 0.2;#d#
2979 $item->{timeout} = $timeout; 3333 $item->{timeout} = $timeout;
2980 delete $item->{label}; 3334 delete $item->{label};
2981 } else { 3335 } else {
2982 $self->{item}{$group} = { 3336 $self->{item}{$group} = {
2983 id => ++$self->{id}, 3337 id => ++$self->{id},
3002 3356
3003 $self->reorder; 3357 $self->reorder;
3004 $self->SUPER::reconfigure; 3358 $self->SUPER::reconfigure;
3005} 3359}
3006 3360
3361sub destroy {
3362 my ($self) = @_;
3363
3364 $self->{timer}->cancel;
3365
3366 $self->SUPER::destroy;
3367}
3368
3007############################################################################# 3369#############################################################################
3008 3370
3009package CFClient::UI::Root; 3371package CFClient::UI::Inventory;
3010 3372
3011our @ISA = CFClient::UI::Container::; 3373our @ISA = CFClient::UI::ScrolledWindow::;
3012
3013use CFClient::OpenGL;
3014 3374
3015sub new { 3375sub new {
3016 my $class = shift; 3376 my $class = shift;
3017 3377
3018 my $self = $class->SUPER::new ( 3378 my $self = $class->SUPER::new (
3019 visible => 1, 3379 child => (new CFClient::UI::Table col_expand => [0, 1, 0]),
3020 @_, 3380 @_,
3021 ); 3381 );
3022 3382
3023 Scalar::Util::weaken ($self->{root} = $self);
3024
3025 $self 3383 $self
3026} 3384}
3027 3385
3028sub size_request { 3386sub set_items {
3029 my ($self) = @_; 3387 my ($self, $items) = @_;
3030 3388
3031 ($self->{w}, $self->{h}) 3389 $self->{child}->clear;
3032} 3390 return unless $items;
3033 3391
3034sub _to_pixel { 3392 my @items = sort {
3035 my ($coord, $size, $max) = @_; 3393 ($a->{type} <=> $b->{type})
3394 or ($a->{name} cmp $b->{name})
3395 } @$items;
3036 3396
3037 $coord = 3397 $self->{real_items} = \@items;
3038 $coord eq "center" ? ($max - $size) * 0.5
3039 : $coord eq "max" ? $max
3040 : $coord;
3041 3398
3042 $coord = 0 if $coord < 0; 3399 my $row = 0;
3043 $coord = $max - $size if $coord > $max - $size; 3400 for my $item (@items) {
3401 CFClient::Item::update_widgets $item;
3044 3402
3045 int $coord + 0.5 3403 $self->{child}->add (0, $row, $item->{face_widget});
3046} 3404 $self->{child}->add (1, $row, $item->{desc_widget});
3405 $self->{child}->add (2, $row, $item->{weight_widget});
3047 3406
3048sub size_allocate { 3407 $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 } 3408 }
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} 3409}
3212 3410
3213############################################################################# 3411#############################################################################
3214 3412
3215package CFClient::UI::BindEditor; 3413package CFClient::UI::BindEditor;
3310 $rec = $::CONN->stop_record if $::CONN; 3508 $rec = $::CONN->stop_record if $::CONN;
3311 return unless ref $rec eq 'ARRAY'; 3509 return unless ref $rec eq 'ARRAY';
3312 $self->set_command_list ($rec); 3510 $self->set_command_list ($rec);
3313} 3511}
3314 3512
3315# if $commit is true, the binding will be set after the user entered a key combo 3513
3514sub ask_for_bind_and_commit {
3515 my ($self) = @_;
3516 $self->ask_for_bind (1);
3517}
3518
3316sub ask_for_bind { 3519sub ask_for_bind {
3317 my ($self, $commit) = @_; 3520 my ($self, $commit, $end_cb) = @_;
3318 3521
3319 CFClient::Binder::open_binding_dialog (sub { 3522 CFClient::Binder::open_binding_dialog (sub {
3320 my ($mod, $sym) = @_; 3523 my ($mod, $sym) = @_;
3321 $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak? 3524 $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak?
3322 $self->update_binding_widgets; 3525 $self->update_binding_widgets;
3323 $self->commit if $commit; 3526 $self->commit if $commit;
3527 $end_cb->() if $end_cb;
3324 }); 3528 });
3325} 3529}
3326 3530
3327# $mod and $sym are the modifiers and key symbol 3531# $mod and $sym are the modifiers and key symbol
3328# $cmds is a array ref of strings (the commands) 3532# $cmds is a array ref of strings (the commands)
3346} 3550}
3347 3551
3348# this is a shortcut method that asks for a binding 3552# this is a shortcut method that asks for a binding
3349# and then just binds it. 3553# and then just binds it.
3350sub do_quick_binding { 3554sub do_quick_binding {
3351 my ($self, $cmds) = @_; 3555 my ($self, $cmds, $end_cb) = @_;
3352 $self->set_binding (undef, undef, $cmds, sub { 3556 $self->set_binding (undef, undef, $cmds, sub {
3353 $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2]; 3557 $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2];
3354 }); 3558 });
3355 $self->ask_for_bind (1); 3559 $self->ask_for_bind (1, $end_cb);
3356} 3560}
3357 3561
3358sub update_binding_widgets { 3562sub update_binding_widgets {
3359 my ($self) = @_; 3563 my ($self) = @_;
3360 my ($mod, $sym, $cmds) = $self->get_binding; 3564 my ($mod, $sym, $cmds) = $self->get_binding;
3400 3604
3401 $idx++ 3605 $idx++
3402 } 3606 }
3403} 3607}
3404 3608
3405
3406############################################################################# 3609#############################################################################
3407 3610
3408package CFClient::UI::SpellList; 3611package CFClient::UI::SpellList;
3409 3612
3410our @ISA = CFClient::UI::FancyFrame::; 3613our @ISA = CFClient::UI::Table::;
3411 3614
3412sub new { 3615sub new {
3413 my $class = shift; 3616 my $class = shift;
3414 3617
3415 my $self = $class->SUPER::new (binding => [], commands => [], @_); 3618 my $self = $class->SUPER::new (
3416 3619 binding => [],
3417 $self->add (new CFClient::UI::ScrolledWindow 3620 commands => [],
3418 scrolled => $self->{spellbox} = new CFClient::UI::Table); 3621 @_,
3419 3622 )
3420 $self;
3421} 3623}
3422 3624
3423# XXX: Do sorting? Argl... 3625my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3626
3627my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3628 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3629my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3630 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3631my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3632 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3633my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3634 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3635my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3636 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3637
3638sub rebuild_spell_list {
3639 my ($self) = @_;
3640
3641 $CFClient::UI::ROOT->on_refresh ($self => sub {
3642 $self->clear;
3643
3644 return unless $::CONN;
3645
3646 $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3647 $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL);
3648 $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3649 $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3650 $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3651
3652 my $row = 0;
3653
3654 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3655 my $spell = $self->{spell}{$_};
3656
3657 $row++;
3658
3659 my $spell_cb = sub {
3660 my ($widget, $ev) = @_;
3661
3662 if ($ev->{button} == 1) {
3663 $::CONN->user_send ("cast $spell->{name}");
3664 } elsif ($ev->{button} == 2) {
3665 $::CONN->user_send ("invoke $spell->{name}");
3666 } elsif ($ev->{button} == 3) {
3667 (new CFClient::UI::Menu
3668 items => [
3669 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3670 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3671 ],
3672 )->popup ($ev);
3673 } else {
3674 return 0;
3675 }
3676
3677 1
3678 };
3679
3680 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3681
3682 #TODO: add path info to tooltip
3683 #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path});
3684
3685 $self->add (0, $row, new CFClient::UI::Face
3686 face => $spell->{face},
3687 can_hover => 1,
3688 can_events => 1,
3689 tooltip => $tooltip,
3690 on_button_down => $spell_cb,
3691 );
3692
3693 $self->add (1, $row, new CFClient::UI::Label
3694 expand => 1,
3695 text => $spell->{name},
3696 can_hover => 1,
3697 can_events => 1,
3698 tooltip => $tooltip,
3699 on_button_down => $spell_cb,
3700 );
3701
3702 $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3703 $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3704 $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3705 $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3706 }
3707 });
3708}
3709
3424sub add_spell { 3710sub add_spell {
3425 my ($self, $spell) = @_; 3711 my ($self, $spell) = @_;
3712
3426 $self->{spells}->{$spell->{name}} = $spell; 3713 $self->{spell}->{$spell->{name}} = $spell;
3427 3714 $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} 3715}
3456 3716
3457sub remove_spell { 3717sub remove_spell {
3458 my ($self, $spell) = @_; 3718 my ($self, $spell) = @_;
3719
3459 delete $self->{spells}->{$spell->{name}}; 3720 delete $self->{spell}->{$spell->{name}};
3460 $self->rebuild_spell_list; 3721 $self->rebuild_spell_list;
3722}
3723
3724sub clear_spells {
3725 my ($self) = @_;
3726
3727 $self->{spell} = {};
3728 $self->rebuild_spell_list;
3729}
3730
3731#############################################################################
3732
3733package CFClient::UI::Root;
3734
3735our @ISA = CFClient::UI::Container::;
3736
3737use List::Util qw(min max);
3738
3739use CFClient::OpenGL;
3740
3741sub new {
3742 my $class = shift;
3743
3744 my $self = $class->SUPER::new (
3745 visible => 1,
3746 @_,
3747 );
3748
3749 Scalar::Util::weaken ($self->{root} = $self);
3750
3751 $self
3752}
3753
3754sub size_request {
3755 my ($self) = @_;
3756
3757 ($self->{w}, $self->{h})
3758}
3759
3760sub _to_pixel {
3761 my ($coord, $size, $max) = @_;
3762
3763 $coord =
3764 $coord eq "center" ? ($max - $size) * 0.5
3765 : $coord eq "max" ? $max
3766 : $coord;
3767
3768 $coord = 0 if $coord < 0;
3769 $coord = $max - $size if $coord > $max - $size;
3770
3771 int $coord + 0.5
3772}
3773
3774sub invoke_size_allocate {
3775 my ($self, $w, $h) = @_;
3776
3777 for my $child ($self->children) {
3778 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3779
3780 $X = $child->{force_x} if exists $child->{force_x};
3781 $Y = $child->{force_y} if exists $child->{force_y};
3782
3783 $X = _to_pixel $X, $W, $self->{w};
3784 $Y = _to_pixel $Y, $H, $self->{h};
3785
3786 $child->configure ($X, $Y, $W, $H);
3787 }
3788
3789 1
3790}
3791
3792sub coord2local {
3793 my ($self, $x, $y) = @_;
3794
3795 ($x, $y)
3796}
3797
3798sub coord2global {
3799 my ($self, $x, $y) = @_;
3800
3801 ($x, $y)
3802}
3803
3804sub update {
3805 my ($self) = @_;
3806
3807 $::WANT_REFRESH++;
3808}
3809
3810sub add {
3811 my ($self, @children) = @_;
3812
3813 $_->{is_toplevel} = 1
3814 for @children;
3815
3816 $self->SUPER::add (@children);
3817}
3818
3819sub remove {
3820 my ($self, @children) = @_;
3821
3822 $self->SUPER::remove (@children);
3823
3824 delete $self->{is_toplevel}
3825 for @children;
3826
3827 while (@children) {
3828 my $w = pop @children;
3829 push @children, $w->children;
3830 $w->set_invisible;
3831 }
3832}
3833
3834sub on_refresh {
3835 my ($self, $id, $cb) = @_;
3836
3837 $self->{refresh_hook}{$id} = $cb;
3838}
3839
3840sub on_post_alloc {
3841 my ($self, $id, $cb) = @_;
3842
3843 $self->{post_alloc_hook}{$id} = $cb;
3844}
3845
3846sub draw {
3847 my ($self) = @_;
3848
3849 while ($self->{refresh_hook}) {
3850 $_->()
3851 for values %{delete $self->{refresh_hook}};
3852 }
3853
3854 if ($self->{realloc}) {
3855 my %queue;
3856 my @queue;
3857 my $widget;
3858
3859 outer:
3860 while () {
3861 if (my $realloc = delete $self->{realloc}) {
3862 for $widget (values %$realloc) {
3863 $widget->{visible} or next; # do not resize invisible widgets
3864
3865 $queue{$widget+0}++ and next; # duplicates are common
3866
3867 push @{ $queue[$widget->{visible}] }, $widget;
3868 }
3869 }
3870
3871 while () {
3872 @queue or last outer;
3873
3874 $widget = pop @{ $queue[-1] || [] }
3875 and last;
3876
3877 pop @queue;
3878 }
3879
3880 delete $queue{$widget+0};
3881
3882 my ($w, $h) = $widget->size_request;
3883
3884 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3885 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3886
3887 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3888 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3889
3890 $w = $widget->{force_w} if exists $widget->{force_w};
3891 $h = $widget->{force_h} if exists $widget->{force_h};
3892
3893 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3894 || delete $widget->{force_realloc}) {
3895 $widget->{req_w} = $w;
3896 $widget->{req_h} = $h;
3897
3898 $self->{size_alloc}{$widget+0} = $widget;
3899
3900 if (my $parent = $widget->{parent}) {
3901 $self->{realloc}{$parent+0} = $parent
3902 unless $queue{$parent+0};
3903
3904 $parent->{force_size_alloc} = 1;
3905 $self->{size_alloc}{$parent+0} = $parent;
3906 }
3907 }
3908
3909 delete $self->{realloc}{$widget+0};
3910 }
3911 }
3912
3913 while (my $size_alloc = delete $self->{size_alloc}) {
3914 my @queue = sort { $b->{visible} <=> $a->{visible} }
3915 values %$size_alloc;
3916
3917 while () {
3918 my $widget = pop @queue || last;
3919
3920 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3921
3922 $w = 0 if $w < 0;
3923 $h = 0 if $h < 0;
3924
3925 $w = int $w + 0.5;
3926 $h = int $h + 0.5;
3927
3928 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3929 $widget->{old_w} = $widget->{w};
3930 $widget->{old_h} = $widget->{h};
3931
3932 $widget->{w} = $w;
3933 $widget->{h} = $h;
3934
3935 $widget->emit (size_allocate => $w, $h);
3936 }
3937 }
3938 }
3939
3940 while ($self->{post_alloc_hook}) {
3941 $_->()
3942 for values %{delete $self->{post_alloc_hook}};
3943 }
3944
3945
3946 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3947 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3948 glClear GL_COLOR_BUFFER_BIT;
3949
3950 glMatrixMode GL_PROJECTION;
3951 glLoadIdentity;
3952 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3953 glMatrixMode GL_MODELVIEW;
3954 glLoadIdentity;
3955
3956 {
3957 package CFClient::UI::Base;
3958
3959 ($draw_x, $draw_y, $draw_w, $draw_h) =
3960 (0, 0, $self->{w}, $self->{h});
3961 }
3962
3963 $self->_draw;
3461} 3964}
3462 3965
3463############################################################################# 3966#############################################################################
3464 3967
3465package CFClient::UI; 3968package CFClient::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines