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.331 by root, Sun Jul 23 04:46:02 2006 UTC vs.
Revision 1.413 by root, Mon Jul 23 15:30:45 2007 UTC

1package CFClient::UI; 1package CFPlus::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util ();
7use List::Util (); 6use List::Util ();
8use Event; 7use Event;
9 8
10use CFClient; 9use CFPlus;
10use CFPlus::Pod;
11use CFClient::Texture; 11use CFPlus::Texture;
12 12
13our ($FOCUS, $HOVER, $GRAB); # various widgets 13our ($FOCUS, $HOVER, $GRAB); # various widgets
14 14
15our $LAYOUT; 15our $LAYOUT;
16our $ROOT; 16our $ROOT;
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub {
23 if (!$GRAB) { 23 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 25 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 26 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
27 $TOOLTIP->hide; 28 $TOOLTIP->hide;
28 29
29 $TOOLTIP->{owner} = $widget; 30 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
30 32
31 return if $ENV{CFPLUS_DEBUG} & 8; 33 return if $ENV{CFPLUS_DEBUG} & 8;
32 34
33 my $tip = $widget->{tooltip}; 35 my $tip = $widget->{tooltip};
34 36
42 } 44 }
43 } 45 }
44 } 46 }
45 47
46 $TOOLTIP->hide; 48 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
47 delete $TOOLTIP->{owner}; 50 delete $TOOLTIP->{owner};
48}); 51});
49 52
50sub get_layout { 53sub get_layout {
51 my $layout; 54 my $layout;
96 99
97sub feed_sdl_button_down_event { 100sub feed_sdl_button_down_event {
98 my ($ev) = @_; 101 my ($ev) = @_;
99 my ($x, $y) = ($ev->{x}, $ev->{y}); 102 my ($x, $y) = ($ev->{x}, $ev->{y});
100 103
101 unless ($BUTTON_STATE) { 104 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
105
106 unless ($GRAB) {
102 my $widget = $ROOT->find_widget ($x, $y); 107 my $widget = $ROOT->find_widget ($x, $y);
103 108
104 $GRAB = $widget; 109 $GRAB = $widget;
105 $GRAB->update if $GRAB; 110 $GRAB->update if $GRAB;
106 111
107 $TOOLTIP_WATCHER->cb->(); 112 $TOOLTIP_WATCHER->cb->();
108 } 113 }
109
110 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
111 114
112 if ($GRAB) { 115 if ($GRAB) {
113 if ($ev->{button} == 4 || $ev->{button} == 5) { 116 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel 117 # mousewheel
115 $ev->{dx} = 0;
116 $ev->{dy} = $ev->{button} * 2 - 9; 118 my $delta = $ev->{button} * 2 - 9;
119 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT;
120
121 $ev->{dx} = $shift ? $delta : 0;
122 $ev->{dy} = $shift ? 0 : $delta;
123
117 $GRAB->emit (mouse_wheel => $ev); 124 $GRAB->emit (mouse_wheel => $ev);
118 } else { 125 } else {
119 $GRAB->emit (button_down => $ev) 126 $GRAB->emit (button_down => $ev)
120 } 127 }
121 } 128 }
207 reconfigure_widgets; 214 reconfigure_widgets;
208} 215}
209 216
210############################################################################# 217#############################################################################
211 218
212package CFClient::UI::Event; 219package CFPlus::UI::Event;
213 220
214sub xy { 221sub xy {
215 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 222 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
216} 223}
217 224
218############################################################################# 225#############################################################################
219 226
220package CFClient::UI::Base; 227package CFPlus::UI::Base;
221 228
222use strict; 229use strict;
223 230
224use CFClient::OpenGL; 231use CFPlus::OpenGL;
225 232
226sub new { 233sub new {
227 my $class = shift; 234 my $class = shift;
228 235
229 my $self = bless { 236 my $self = bless {
234 h => undef, 241 h => undef,
235 can_events => 1, 242 can_events => 1,
236 @_ 243 @_
237 }, $class; 244 }, $class;
238 245
239 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 246 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self);
240 247
241 for (keys %$self) { 248 for (keys %$self) {
242 if (/^on_(.*)$/) { 249 if (/^on_(.*)$/) {
243 $self->connect ($1 => delete $self->{$_}); 250 $self->connect ($1 => delete $self->{$_});
244 } 251 }
245 } 252 }
246 253
247 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { 254 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) {
248 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{x}; 255 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x};
249 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{y}; 256 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y};
250 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{w}; 257 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w};
251 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{h}; 258 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h};
252 259
253 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 260 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
254 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 261 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
255 262
256 $self->show if $layout->{show}; 263 $self->show if $layout->{show};
261 268
262sub destroy { 269sub destroy {
263 my ($self) = @_; 270 my ($self) = @_;
264 271
265 $self->hide; 272 $self->hide;
273 $self->emit ("destroy");
266 %$self = (); 274 %$self = ();
267} 275}
268 276
277sub TO_JSON {
278 { __widget_ref__ => $_[0]{s_id} }
279}
280
269sub show { 281sub show {
270 my ($self) = @_; 282 my ($self) = @_;
271 283
272 return if $self->{parent}; 284 return if $self->{parent};
273 285
274 $CFClient::UI::ROOT->add ($self); 286 $CFPlus::UI::ROOT->add ($self);
275} 287}
276 288
277sub set_visible { 289sub set_visible {
278 my ($self) = @_; 290 my ($self) = @_;
279 291
300 delete $self->{root}; 312 delete $self->{root};
301 313
302 undef $GRAB if $GRAB == $self; 314 undef $GRAB if $GRAB == $self;
303 undef $HOVER if $HOVER == $self; 315 undef $HOVER if $HOVER == $self;
304 316
305 $CFClient::UI::TOOLTIP_WATCHER->cb->() 317 $CFPlus::UI::TOOLTIP_WATCHER->cb->()
306 if $TOOLTIP->{owner} == $self; 318 if $TOOLTIP->{owner} == $self;
307 319
308 $self->emit ("focus_out"); 320 $self->emit ("focus_out");
309 $self->emit (visibility_change => 0); 321 $self->emit (visibility_change => 0);
310} 322}
312sub set_visibility { 324sub set_visibility {
313 my ($self, $visible) = @_; 325 my ($self, $visible) = @_;
314 326
315 return if $self->{visible} == $visible; 327 return if $self->{visible} == $visible;
316 328
317 $visible ? $self->hide 329 $visible ? $self->show
318 : $self->show; 330 : $self->hide;
319} 331}
320 332
321sub toggle_visibility { 333sub toggle_visibility {
322 my ($self) = @_; 334 my ($self) = @_;
323 335
367 my ($self, $x, $y, $w, $h) = @_; 379 my ($self, $x, $y, $w, $h) = @_;
368 380
369 if ($self->{aspect}) { 381 if ($self->{aspect}) {
370 my ($ow, $oh) = ($w, $h); 382 my ($ow, $oh) = ($w, $h);
371 383
372 $w = List::Util::min $w, int $h * $self->{aspect}; 384 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect};
373 $h = List::Util::min $h, int $w / $self->{aspect}; 385 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect};
374 386
375 # use alignment to adjust x, y 387 # use alignment to adjust x, y
376 388
377 $x += int 0.5 * ($ow - $w); 389 $x += int 0.5 * ($ow - $w);
378 $y += int 0.5 * ($oh - $h); 390 $y += int 0.5 * ($oh - $h);
419 431
420 return if $self->{tooltip} eq $tooltip; 432 return if $self->{tooltip} eq $tooltip;
421 433
422 $self->{tooltip} = $tooltip; 434 $self->{tooltip} = $tooltip;
423 435
424 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 436 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
425 delete $CFClient::UI::TOOLTIP->{owner}; 437 delete $CFPlus::UI::TOOLTIP->{owner};
426 $CFClient::UI::TOOLTIP_WATCHER->cb->(); 438 $CFPlus::UI::TOOLTIP_WATCHER->cb->();
427 } 439 }
428} 440}
429 441
430# translate global coordinates to local coordinate system 442# translate global coordinates to local coordinate system
431sub coord2local { 443sub coord2local {
437} 449}
438 450
439# translate local coordinates to global coordinate system 451# translate local coordinates to global coordinate system
440sub coord2global { 452sub coord2global {
441 my ($self, $x, $y) = @_; 453 my ($self, $x, $y) = @_;
454
455 Carp::confess unless $self->{parent};#d#
442 456
443 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 457 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
444} 458}
445 459
446sub invoke_focus_in { 460sub invoke_focus_in {
494 508
495sub connect { 509sub connect {
496 my ($self, $signal, $cb) = @_; 510 my ($self, $signal, $cb) = @_;
497 511
498 push @{ $self->{signal_cb}{$signal} }, $cb; 512 push @{ $self->{signal_cb}{$signal} }, $cb;
513
514 defined wantarray and CFPlus::guard {
515 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
516 @{ $self->{signal_cb}{$signal} };
517 }
518}
519
520sub disconnect_all {
521 my ($self, $signal) = @_;
522
523 delete $self->{signal_cb}{$signal};
499} 524}
500 525
501my %has_coords = ( 526my %has_coords = (
502 button_down => 1, 527 button_down => 1,
503 button_up => 1, 528 button_up => 1,
506); 531);
507 532
508sub emit { 533sub emit {
509 my ($self, $signal, @args) = @_; 534 my ($self, $signal, @args) = @_;
510 535
536 # I do not really like this solution, but I do not like duplication
537 # and needlessly verbose code, either.
511 my @append 538 my @append
512 = $has_coords{$signal} 539 = $has_coords{$signal}
513 ? $args[0]->xy ($self) 540 ? $args[0]->xy ($self)
514 : (); 541 : ();
515 542
516 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# 543 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
517 544
518 #d##TODO# stop propagating at first true, do not use sum 545 for my $cb (
519 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before 546 @{$self->{signal_cb}{$signal} || []}, # before
520 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure 547 ($self->can ("invoke_$signal") || sub { 1 }), # closure
548 ) {
549 return $cb->($self, @args, @append) || next;
550 }
551
552 # parent
521 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 553 $self->{parent} && $self->{parent}->emit ($signal, @args)
522} 554}
523 555
524sub find_widget { 556#sub find_widget {
525 my ($self, $x, $y) = @_; 557# in .xs
526
527 return () unless $self->{can_events};
528
529 return $self
530 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
531 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
532
533 ()
534}
535 558
536sub set_parent { 559sub set_parent {
537 my ($self, $parent) = @_; 560 my ($self, $parent) = @_;
538 561
539 Scalar::Util::weaken ($self->{parent} = $parent); 562 CFPlus::weaken ($self->{parent} = $parent);
540 $self->set_visible if $parent->{visible}; 563 $self->set_visible if $parent->{visible};
541} 564}
542 565
543sub realloc { 566sub realloc {
544 my ($self) = @_; 567 my ($self) = @_;
570 593
571# using global variables seems a bit hacky, but passing through all drawing 594# using global variables seems a bit hacky, but passing through all drawing
572# functions seems pointless. 595# functions seems pointless.
573our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn 596our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
574 597
575sub draw { 598#sub draw {
576 my ($self) = @_; 599#CFPlus.xs
577
578 return unless $self->{h} && $self->{w};
579
580 # update screen rectangle
581 local $draw_x = $draw_x + $self->{x};
582 local $draw_y = $draw_y + $self->{y};
583
584 # skip widgets that are entirely outside the drawing area
585 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
586 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
587
588 glPushMatrix;
589 glTranslate $self->{x}, $self->{y}, 0;
590
591 if ($self == $HOVER && $self->{can_hover}) {
592 glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2;
593 glEnable GL_BLEND;
594 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
595 glBegin GL_QUADS;
596 glVertex 0 , 0;
597 glVertex $self->{w}, 0;
598 glVertex $self->{w}, $self->{h};
599 glVertex 0 , $self->{h};
600 glEnd;
601 glDisable GL_BLEND;
602 }
603
604 if ($ENV{CFPLUS_DEBUG} & 1) {
605 glPushMatrix;
606 glColor 1, 1, 0, 1;
607 glTranslate 0.375, 0.375;
608 glBegin GL_LINE_LOOP;
609 glVertex 0 , 0;
610 glVertex $self->{w} - 1, 0;
611 glVertex $self->{w} - 1, $self->{h} - 1;
612 glVertex 0 , $self->{h} - 1;
613 glEnd;
614 glPopMatrix;
615 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
616 }
617
618 $self->_draw;
619 glPopMatrix;
620}
621 600
622sub _draw { 601sub _draw {
623 my ($self) = @_; 602 my ($self) = @_;
624 603
625 warn "no draw defined for $self\n"; 604 warn "no draw defined for $self\n";
626} 605}
627 606
628sub DESTROY { 607sub DESTROY {
629 my ($self) = @_; 608 my ($self) = @_;
630 609
631 return if CFClient::in_destruct; 610 return if CFPlus::in_destruct;
632
633 delete $WIDGET{$self+0};
634 611
635 eval { $self->destroy }; 612 eval { $self->destroy };
636 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 613 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
614
615 delete $WIDGET{$self+0};
637} 616}
638 617
639############################################################################# 618#############################################################################
640 619
641package CFClient::UI::DrawBG; 620package CFPlus::UI::DrawBG;
642 621
643our @ISA = CFClient::UI::Base::; 622our @ISA = CFPlus::UI::Base::;
644 623
645use strict; 624use strict;
646use CFClient::OpenGL; 625use CFPlus::OpenGL;
647 626
648sub new { 627sub new {
649 my $class = shift; 628 my $class = shift;
650 629
651 # range [value, low, high, page] 630 # range [value, low, high, page]
682 } 661 }
683} 662}
684 663
685############################################################################# 664#############################################################################
686 665
687package CFClient::UI::Empty; 666package CFPlus::UI::Empty;
688 667
689our @ISA = CFClient::UI::Base::; 668our @ISA = CFPlus::UI::Base::;
690 669
691sub new { 670sub new {
692 my ($class, %arg) = @_; 671 my ($class, %arg) = @_;
693 $class->SUPER::new (can_events => 0, %arg); 672 $class->SUPER::new (can_events => 0, %arg);
694} 673}
701 680
702sub draw { } 681sub draw { }
703 682
704############################################################################# 683#############################################################################
705 684
706package CFClient::UI::Container; 685package CFPlus::UI::Container;
707 686
708our @ISA = CFClient::UI::Base::; 687our @ISA = CFPlus::UI::Base::;
709 688
710sub new { 689sub new {
711 my ($class, %arg) = @_; 690 my ($class, %arg) = @_;
712 691
713 my $children = delete $arg{children}; 692 my $children = delete $arg{children};
717 can_events => 0, 696 can_events => 0,
718 %arg, 697 %arg,
719 ); 698 );
720 699
721 $self->add (@$children) 700 $self->add (@$children)
722 if $children; 701 if $children && @$children;
723 702
724 $self 703 $self
704}
705
706sub realloc {
707 my ($self) = @_;
708
709 $self->{force_realloc} = 1;
710 $self->{force_size_alloc} = 1;
711 $self->SUPER::realloc;
725} 712}
726 713
727sub add { 714sub add {
728 my ($self, @widgets) = @_; 715 my ($self, @widgets) = @_;
729 716
730 $_->set_parent ($self) 717 $_->set_parent ($self)
731 for @widgets; 718 for @widgets;
732 719
720 # TODO: only do this in widgets that need it, e.g. root, fixed
733 use sort 'stable'; 721 use sort 'stable';
734 722
735 $self->{children} = [ 723 $self->{children} = [
736 sort { $a->{z} <=> $b->{z} } 724 sort { $a->{z} <=> $b->{z} }
737 @{$self->{children}}, @widgets 725 @{$self->{children}}, @widgets
738 ]; 726 ];
739 727
740 $self->realloc; 728 $self->realloc;
729
730 $self->emit (c_add => \@widgets);
731
732 map $_+0, @widgets
741} 733}
742 734
743sub children { 735sub children {
744 @{ $_[0]{children} } 736 @{ $_[0]{children} }
745} 737}
746 738
747sub remove { 739sub remove {
748 my ($self, $child) = @_; 740 my ($self, @widgets) = @_;
749 741
742 $self->emit (c_remove => \@widgets);
743
744 for my $child (@widgets) {
750 delete $child->{parent}; 745 delete $child->{parent};
751 $child->hide; 746 $child->hide;
752
753 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 747 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
748 }
754 749
755 $self->realloc; 750 $self->realloc;
756} 751}
757 752
758sub clear { 753sub clear {
759 my ($self) = @_; 754 my ($self) = @_;
760 755
761 my $children = delete $self->{children}; 756 my $children = $self->{children};
762 $self->{children} = []; 757 $self->{children} = [];
763 758
764 for (@$children) { 759 for (@$children) {
765 delete $_->{parent}; 760 delete $_->{parent};
766 $_->hide; 761 $_->hide;
786} 781}
787 782
788sub _draw { 783sub _draw {
789 my ($self) = @_; 784 my ($self) = @_;
790 785
791 $_->draw for @{$self->{children}}; 786 $_->draw for $self->visible_children;
792} 787}
793 788
794############################################################################# 789#############################################################################
795 790
796package CFClient::UI::Bin; 791package CFPlus::UI::Bin;
797 792
798our @ISA = CFClient::UI::Container::; 793our @ISA = CFPlus::UI::Container::;
799 794
800sub new { 795sub new {
801 my ($class, %arg) = @_; 796 my ($class, %arg) = @_;
802 797
803 my $child = (delete $arg{child}) || new CFClient::UI::Empty::; 798 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::;
804 799
805 $class->SUPER::new (children => [$child], %arg) 800 $class->SUPER::new (children => [$child], %arg)
806} 801}
807 802
808sub add { 803sub add {
809 my ($self, $child) = @_; 804 my ($self, $child) = @_;
810 805
811 $self->SUPER::remove ($_) for @{ $self->{children} }; 806 $self->clear;
812 $self->SUPER::add ($child); 807 $self->SUPER::add ($child);
813} 808}
814 809
815sub remove { 810sub remove {
816 my ($self, $widget) = @_; 811 my ($self, $widget) = @_;
817 812
818 $self->SUPER::remove ($widget); 813 $self->SUPER::remove ($widget);
819 814
820 $self->{children} = [new CFClient::UI::Empty] 815 $self->{children} = [new CFPlus::UI::Empty]
821 unless @{$self->{children}}; 816 unless @{$self->{children}};
822} 817}
823 818
824sub child { $_[0]->{children}[0] } 819sub child { $_[0]->{children}[0] }
825 820
837 832
838############################################################################# 833#############################################################################
839 834
840# back-buffered drawing area 835# back-buffered drawing area
841 836
842package CFClient::UI::Window; 837package CFPlus::UI::Window;
843 838
844our @ISA = CFClient::UI::Bin::; 839our @ISA = CFPlus::UI::Bin::;
845 840
846use CFClient::OpenGL; 841use CFPlus::OpenGL;
847 842
848sub new { 843sub new {
849 my ($class, %arg) = @_; 844 my ($class, %arg) = @_;
850 845
851 my $self = $class->SUPER::new (%arg); 846 my $self = $class->SUPER::new (%arg);
873} 868}
874 869
875sub render_child { 870sub render_child {
876 my ($self) = @_; 871 my ($self) = @_;
877 872
878 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 873 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub {
879 glClearColor 0, 0, 0, 0; 874 glClearColor 0, 0, 0, 0;
880 glClear GL_COLOR_BUFFER_BIT; 875 glClear GL_COLOR_BUFFER_BIT;
881 876
882 { 877 {
883 package CFClient::UI::Base; 878 package CFPlus::UI::Base;
884 879
885 ($draw_x, $draw_y, $draw_w, $draw_h) = 880 local ($draw_x, $draw_y, $draw_w, $draw_h) =
886 (0, 0, $self->{w}, $self->{h}); 881 (0, 0, $self->{w}, $self->{h});
882
883 $self->_render;
887 } 884 }
888
889 $self->_render;
890 }; 885 };
891} 886}
892 887
893sub _draw { 888sub _draw {
894 my ($self) = @_; 889 my ($self) = @_;
895
896 my ($w, $h) = @$self{qw(w h)};
897 890
898 my $tex = $self->{texture} 891 my $tex = $self->{texture}
899 or return; 892 or return;
900 893
901 glEnable GL_TEXTURE_2D; 894 glEnable GL_TEXTURE_2D;
902 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 895 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
903 glColor 0, 0, 0, 1; 896 glColor 0, 0, 0, 1;
904 897
905 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); 898 $tex->draw_quad_alpha_premultiplied (0, 0);
906 899
907 glDisable GL_TEXTURE_2D; 900 glDisable GL_TEXTURE_2D;
908} 901}
909 902
910############################################################################# 903#############################################################################
911 904
912package CFClient::UI::ViewPort; 905package CFPlus::UI::ViewPort;
913 906
907use List::Util qw(min max);
908
914our @ISA = CFClient::UI::Window::; 909our @ISA = CFPlus::UI::Window::;
915 910
916sub new { 911sub new {
917 my $class = shift; 912 my $class = shift;
918 913
919 $class->SUPER::new ( 914 $class->SUPER::new (
926sub size_request { 921sub size_request {
927 my ($self) = @_; 922 my ($self) = @_;
928 923
929 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 924 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
930 925
931 $w = 10 if $self->{scroll_x}; 926 $w = 1 if $self->{scroll_x};
932 $h = 10 if $self->{scroll_y}; 927 $h = 1 if $self->{scroll_y};
933 928
934 ($w, $h) 929 ($w, $h)
935} 930}
936 931
937sub invoke_size_allocate { 932sub invoke_size_allocate {
949} 944}
950 945
951sub set_offset { 946sub set_offset {
952 my ($self, $x, $y) = @_; 947 my ($self, $x, $y) = @_;
953 948
949 my $x = max 0, min $self->child->{w} - $self->{w}, int $x;
950 my $y = max 0, min $self->child->{h} - $self->{h}, int $y;
951
952 if ($x != $self->{view_x} or $y != $self->{view_y}) {
954 $self->{view_x} = int $x; 953 $self->{view_x} = $x;
955 $self->{view_y} = int $y; 954 $self->{view_y} = $y;
956 955
956 $self->emit (changed => $x, $y);
957 $self->update; 957 $self->update;
958 }
958} 959}
959 960
960# hmm, this does not work for topleft of $self... but we should not ask for that 961# hmm, this does not work for topleft of $self... but we should not ask for that
961sub coord2local { 962sub coord2local {
962 my ($self, $x, $y) = @_; 963 my ($self, $x, $y) = @_;
977 my ($self, $x, $y) = @_; 978 my ($self, $x, $y) = @_;
978 979
979 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 980 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
980 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 981 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
981 ) { 982 ) {
982 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 983 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
983 } else { 984 } else {
984 $self->CFClient::UI::Base::find_widget ($x, $y) 985 $self->CFPlus::UI::Base::find_widget ($x, $y)
985 } 986 }
986} 987}
987 988
988sub _render { 989sub _render {
989 my ($self) = @_; 990 my ($self) = @_;
990 991
991 local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x}; 992 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x};
992 local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y}; 993 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y};
993 994
994 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 995 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
995 996
996 $self->SUPER::_render; 997 $self->SUPER::_render;
997} 998}
998 999
999############################################################################# 1000#############################################################################
1000 1001
1001package CFClient::UI::ScrolledWindow; 1002package CFPlus::UI::ScrolledWindow;
1002 1003
1003our @ISA = CFClient::UI::HBox::; 1004our @ISA = CFPlus::UI::Table::;
1004 1005
1005sub new { 1006sub new {
1006 my ($class, %arg) = @_; 1007 my ($class, %arg) = @_;
1007 1008
1008 my $child = delete $arg{child}; 1009 my $child = delete $arg{child};
1009 1010
1010 my $self; 1011 my $self;
1011 1012
1012 my $slider = new CFClient::UI::Slider 1013 my $hslider = new CFPlus::UI::Slider
1014 col => 0,
1015 row => 1,
1016 vertical => 0,
1017 range => [0, 0, 1, 0.01], # HACK fix
1018 on_changed => sub {
1019 $self->{hpos} = $_[1];
1020 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1021 },
1022 ;
1023
1024 my $vslider = new CFPlus::UI::Slider
1025 col => 1,
1026 row => 0,
1013 vertical => 1, 1027 vertical => 1,
1014 range => [0, 0, 1, 0.01], # HACK fix 1028 range => [0, 0, 1, 0.01], # HACK fix
1015 on_changed => sub { 1029 on_changed => sub {
1016 $self->{vp}->set_offset (0, $_[1]); 1030 $self->{vpos} = $_[1];
1031 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1017 }, 1032 },
1018 ; 1033 ;
1019 1034
1020 $self = $class->SUPER::new ( 1035 $self = $class->SUPER::new (
1021 vp => (new CFClient::UI::ViewPort expand => 1), 1036 scroll_x => 0,
1037 scroll_y => 1,
1022 can_events => 1, 1038 can_events => 1,
1023 slider => $slider, 1039 hslider => $hslider,
1040 vslider => $vslider,
1041 col_expand => [1, 0],
1042 row_expand => [1, 0],
1024 %arg, 1043 %arg,
1025 ); 1044 );
1026 1045
1046 $self->{vp} = new CFPlus::UI::ViewPort
1047 col => 0,
1048 row => 0,
1049 expand => 1,
1050 scroll_x => $self->{scroll_x},
1051 scroll_y => $self->{scroll_y},
1052 on_changed => sub {
1053 my ($vp, $x, $y) = @_;
1054
1055 $vp->{parent}{hslider}->set_value ($x);
1056 $vp->{parent}{vslider}->set_value ($y);
1057
1058 0
1059 },
1060 on_size_allocate => sub {
1061 my ($vp, $w, $h) = @_;
1062 $vp->{parent}->update_slider;
1063 0
1064 },
1065 ;
1066
1027 $self->SUPER::add ($self->{vp}, $self->{slider}); 1067 $self->SUPER::add ($self->{vp});
1068
1028 $self->add ($child) if $child; 1069 $self->add ($child) if $child;
1029 1070
1030 $self 1071 $self
1031} 1072}
1032
1033#TODO# update range on size_allocate depending on child
1034 1073
1035sub add { 1074sub add {
1036 my ($self, $widget) = @_; 1075 my ($self, $widget) = @_;
1037 1076
1038 $self->{vp}->add ($self->{child} = $widget); 1077 $self->{vp}->add ($self->{child} = $widget);
1039} 1078}
1040 1079
1080sub update_slider {
1081 my ($self) = @_;
1082
1083 my $child = ($self->{vp} or return)->child;
1084
1085 if ($self->{scroll_x}) {
1086 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1087 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1088
1089 my $visible = $w1 > $w2;
1090 if ($visible != $self->{hslider_visible}) {
1091 $self->{hslider_visible} = $visible;
1092 $visible ? $self->SUPER::add ($self->{hslider})
1093 : $self->SUPER::remove ($self->{hslider});
1094 }
1095 }
1096
1097 if ($self->{scroll_y}) {
1098 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1099 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1100
1101 my $visible = $h1 > $h2;
1102 if ($visible != $self->{vslider_visible}) {
1103 $self->{vslider_visible} = $visible;
1104 $visible ? $self->SUPER::add ($self->{vslider})
1105 : $self->SUPER::remove ($self->{vslider});
1106 }
1107 }
1108}
1109
1110sub start_dragging {
1111 my ($self, $ev) = @_;
1112
1113 $self->grab_focus;
1114
1115 my $ox = $self->{vp}{view_x};
1116 my $oy = $self->{vp}{view_y};
1117
1118 $self->{motion} = sub {
1119 my ($ev, $x, $y) = @_;
1120
1121 $ox -= $ev->{xrel};
1122 $oy -= $ev->{yrel};
1123
1124 $self->{vp}->set_offset ($ox, $oy);
1125 };
1126}
1127
1041sub invoke_mouse_wheel { 1128sub invoke_mouse_wheel {
1042 my ($self, $ev) = @_; 1129 my ($self, $ev) = @_;
1043 1130
1044 return 0 unless $ev->{dy}; # only vertical movements
1045
1046 $self->{slider}->emit (mouse_wheel => $ev); 1131 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1132 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1047 1133
1048 1 1134 1
1049} 1135}
1050 1136
1051sub update_slider { 1137sub invoke_button_down {
1052 my ($self) = @_; 1138 my ($self, $ev, $x, $y) = @_;
1053 1139
1054 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1140 if ($ev->{button} == 2) {
1055} 1141 $self->start_dragging ($ev);
1142 return 1;
1143 }
1056 1144
1057sub update { 1145 0
1058 my ($self) = @_; 1146}
1059 1147
1060 $self->SUPER::update; 1148sub invoke_button_up {
1149 my ($self, $ev, $x, $y) = @_;
1061 1150
1062 $self->update_slider; 1151 if (delete $self->{motion}) {
1152 return 1;
1153 }
1154
1155 0
1156}
1157
1158sub invoke_mouse_motion {
1159 my ($self, $ev, $x, $y) = @_;
1160
1161 if ($self->{motion}) {
1162 $self->{motion}->($ev, $x, $y);
1163 return 1;
1164 }
1165
1166 0
1063} 1167}
1064 1168
1065sub invoke_size_allocate { 1169sub invoke_size_allocate {
1066 my ($self, $w, $h) = @_; 1170 my ($self, $w, $h) = @_;
1067 1171
1068 $self->update_slider; 1172 $self->update_slider;
1069
1070 $self->SUPER::invoke_size_allocate ($w, $h) 1173 $self->SUPER::invoke_size_allocate ($w, $h)
1071} 1174}
1072 1175
1073############################################################################# 1176#############################################################################
1074 1177
1075package CFClient::UI::Frame; 1178package CFPlus::UI::Frame;
1076 1179
1077our @ISA = CFClient::UI::Bin::; 1180our @ISA = CFPlus::UI::Bin::;
1078 1181
1079use CFClient::OpenGL; 1182use CFPlus::OpenGL;
1080 1183
1081sub new { 1184sub new {
1082 my $class = shift; 1185 my $class = shift;
1083 1186
1084 $class->SUPER::new ( 1187 $class->SUPER::new (
1110 $self->SUPER::_draw; 1213 $self->SUPER::_draw;
1111} 1214}
1112 1215
1113############################################################################# 1216#############################################################################
1114 1217
1115package CFClient::UI::FancyFrame; 1218package CFPlus::UI::FancyFrame;
1116 1219
1117our @ISA = CFClient::UI::Bin::; 1220our @ISA = CFPlus::UI::Bin::;
1118 1221
1119use CFClient::OpenGL; 1222use CFPlus::OpenGL;
1223
1224sub new {
1225 my ($class, %arg) = @_;
1226
1227 if ((exists $arg{label}) && !ref $arg{label}) {
1228 $arg{label} = new CFPlus::UI::Label
1229 align => 1,
1230 valign => 0,
1231 text => $arg{label},
1232 fontsize => ($arg{border} || 0.8) * 0.75;
1233 }
1234
1235 my $self = $class->SUPER::new (
1236 # label => "",
1237 fg => [0.6, 0.3, 0.1],
1238 border => 0.8,
1239 style => 'single',
1240 %arg,
1241 );
1242
1243 $self
1244}
1245
1246sub add {
1247 my ($self, @widgets) = @_;
1248
1249 $self->SUPER::add (@widgets);
1250 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label};
1251}
1252
1253sub border {
1254 int $_[0]{border} * $::FONTSIZE
1255}
1256
1257sub size_request {
1258 my ($self) = @_;
1259
1260 ($self->{label_w}, undef) = $self->{label}->size_request
1261 if $self->{label};
1262
1263 my ($w, $h) = $self->SUPER::size_request;
1264
1265 (
1266 $w + $self->border * 2,
1267 $h + $self->border * 2,
1268 )
1269}
1270
1271sub invoke_size_allocate {
1272 my ($self, $w, $h) = @_;
1273
1274 my $border = $self->border;
1275
1276 $w -= List::Util::max 0, $border * 2;
1277 $h -= List::Util::max 0, $border * 2;
1278
1279 if (my $label = $self->{label}) {
1280 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1281 $label->{h} = List::Util::min $h, $border;
1282 $label->invoke_size_allocate ($label->{w}, $label->{h});
1283 }
1284
1285 $self->child->configure ($border, $border, $w, $h);
1286
1287 1
1288}
1289
1290sub _draw {
1291 my ($self) = @_;
1292
1293 my $child = $self->{children}[0];
1294
1295 my $border = $self->border;
1296 my ($w, $h) = ($self->{w}, $self->{h});
1297
1298 $child->draw;
1299
1300 glColor @{$self->{fg}};
1301 glBegin GL_LINE_STRIP;
1302 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1303 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1304 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1305 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1306 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1307 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1308 glEnd;
1309
1310 if ($self->{label}) {
1311 glTranslate $border * 2, 0;
1312 $self->{label}->_draw;
1313 }
1314}
1315
1316#############################################################################
1317
1318package CFPlus::UI::Toplevel;
1319
1320our @ISA = CFPlus::UI::Bin::;
1321
1322use CFPlus::OpenGL;
1120 1323
1121my $bg = 1324my $bg =
1122 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png", 1325 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png",
1123 mipmap => 1, wrap => 1; 1326 mipmap => 1, wrap => 1;
1124 1327
1125my @border = 1328my @border =
1126 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1329 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1127 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1330 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1331
1332my @icon =
1333 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1334 qw(x1_move.png x1_resize.png);
1128 1335
1129sub new { 1336sub new {
1130 my ($class, %arg) = @_; 1337 my ($class, %arg) = @_;
1131 1338
1132 my $self = $class->SUPER::new ( 1339 my $self = $class->SUPER::new (
1137 min_w => 64, 1344 min_w => 64,
1138 min_h => 32, 1345 min_h => 32,
1139 %arg, 1346 %arg,
1140 ); 1347 );
1141 1348
1142 $self->{title_widget} = new CFClient::UI::Label 1349 $self->{title_widget} = new CFPlus::UI::Label
1143 align => 0, 1350 align => 0,
1144 valign => 1, 1351 valign => 1,
1145 text => $self->{title}, 1352 text => $self->{title},
1146 fontsize => $self->{border}, 1353 fontsize => $self->{border},
1147 if exists $self->{title}; 1354 if exists $self->{title};
1148 1355
1149 if ($self->{has_close_button}) { 1356 if ($self->{has_close_button}) {
1150 $self->{close_button} = 1357 $self->{close_button} =
1151 new CFClient::UI::ImageButton 1358 new CFPlus::UI::ImageButton
1152 path => 'x1_close.png', 1359 path => 'x1_close.png',
1153 on_activate => sub { $self->emit ("delete") }; 1360 on_activate => sub { $self->emit ("delete") };
1154 1361
1155 $self->CFClient::UI::Container::add ($self->{close_button}); 1362 $self->CFPlus::UI::Container::add ($self->{close_button});
1156 } 1363 }
1157 1364
1158 $self 1365 $self
1159} 1366}
1160 1367
1161sub add { 1368sub add {
1162 my ($self, @widgets) = @_; 1369 my ($self, @widgets) = @_;
1163 1370
1164 $self->SUPER::add (@widgets); 1371 $self->SUPER::add (@widgets);
1165 $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1372 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button};
1166 $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1373 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1167} 1374}
1168 1375
1169sub border { 1376sub border {
1170 int $_[0]{border} * $::FONTSIZE 1377 int $_[0]{border} * $::FONTSIZE
1171} 1378}
1279 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1486 $self->{motion}->($ev, $x, $y) if $self->{motion};
1280 1487
1281 ! ! $self->{motion} 1488 ! ! $self->{motion}
1282} 1489}
1283 1490
1491sub invoke_visibility_change {
1492 my ($self, $visible) = @_;
1493
1494 delete $self->{motion} unless $visible;
1495
1496 0
1497}
1498
1284sub _draw { 1499sub _draw {
1285 my ($self) = @_; 1500 my ($self) = @_;
1286 1501
1287 my $child = $self->{children}[0]; 1502 my $child = $self->{children}[0];
1288 1503
1293 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1508 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1294 1509
1295 my $border = $self->border; 1510 my $border = $self->border;
1296 1511
1297 glColor @{ $self->{border_bg} }; 1512 glColor @{ $self->{border_bg} };
1298 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1513 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1299 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1514 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1300 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1515 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1301 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1516 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1517
1518 # move
1519 my $w2 = ($w - $border) * .5;
1520 my $h2 = ($h - $border) * .5;
1521 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1522 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1523 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1524
1525 # resize
1526 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1527 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1528 unless $self->{has_close_button};
1529 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1530 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1302 1531
1303 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1532 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1304 glColor @{ $self->{bg} }; 1533 glColor @{ $self->{bg} };
1305 1534
1306 # TODO: repeat texture not scale 1535 # TODO: repeat texture not scale
1325 if $self->{close_button}; 1554 if $self->{close_button};
1326} 1555}
1327 1556
1328############################################################################# 1557#############################################################################
1329 1558
1330package CFClient::UI::Table; 1559package CFPlus::UI::Table;
1331 1560
1332our @ISA = CFClient::UI::Base::; 1561our @ISA = CFPlus::UI::Container::;
1333 1562
1334use List::Util qw(max sum); 1563use List::Util qw(max sum);
1335 1564
1336use CFClient::OpenGL; 1565use CFPlus::OpenGL;
1337 1566
1338sub new { 1567sub new {
1339 my $class = shift; 1568 my $class = shift;
1340 1569
1341 $class->SUPER::new ( 1570 $class->SUPER::new (
1342 col_expand => [], 1571 col_expand => [],
1572 row_expand => [],
1343 @_, 1573 @_,
1344 ) 1574 )
1345} 1575}
1346 1576
1347sub children {
1348 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1349}
1350
1351sub add { 1577sub add {
1352 my ($self, $x, $y, $child) = @_; 1578 my ($self, @widgets) = @_;
1353 1579
1354 $child->set_parent ($self); 1580 for my $child (@widgets) {
1355 $self->{children}[$y][$x] = $child; 1581 $child->{rowspan} ||= 1;
1582 $child->{colspan} ||= 1;
1583 }
1356 1584
1357 $self->realloc; 1585 $self->SUPER::add (@widgets);
1358} 1586}
1359 1587
1360sub remove { 1588sub add_at {
1589 my $self = shift;
1590
1591 my @widgets;
1592
1593 while (@_) {
1594 my ($col, $row, $child) = splice @_, 0, 3, ();
1595
1596 $child->{row} = $row;
1597 $child->{col} = $col;
1598
1599 push @widgets, $child;
1600 }
1601
1602 $self->add (@widgets);
1603}
1604
1605sub get_wh {
1361 my ($self, $child) = @_; 1606 my ($self) = @_;
1362 1607
1363 # TODO: not yet implemented 1608 my (@w, @h);
1364}
1365
1366# TODO: move to container class maybe? send children a signal on removal?
1367sub clear {
1368 my ($self) = @_;
1369 1609
1370 my @children = $self->children; 1610 my @children = $self->children;
1371 delete $self->{children}; 1611
1612 # first pass, columns
1613 for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) {
1614 my ($c, $w, $cs) = @$widget{qw(col req_w colspan)};
1615
1616 my $sw = sum @w[$c .. $c + $cs - 1];
1617
1618 if ($w > $sw) {
1619 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1620 }
1372 1621 }
1373 for (@children) {
1374 delete $_->{parent};
1375 $_->hide;
1376 }
1377 1622
1378 $self->realloc; 1623 # second pass, rows
1379} 1624 for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) {
1380
1381sub get_wh {
1382 my ($self) = @_;
1383
1384 my (@w, @h);
1385
1386 for my $y (0 .. $#{$self->{children}}) {
1387 my $row = $self->{children}[$y]
1388 or next;
1389
1390 for my $x (0 .. $#$row) {
1391 my $widget = $row->[$x]
1392 or next;
1393 my ($w, $h) = @$widget{qw(req_w req_h)}; 1625 my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)};
1394 1626
1395 $w[$x] = max $w[$x], $w; 1627 my $sh = sum @h[$r .. $r + $rs - 1];
1396 $h[$y] = max $h[$y], $h; 1628
1629 if ($h > $sh) {
1630 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1397 } 1631 }
1398 } 1632 }
1399 1633
1400 (\@w, \@h) 1634 (\@w, \@h)
1401} 1635}
1417 my ($ws, $hs) = $self->get_wh; 1651 my ($ws, $hs) = $self->get_wh;
1418 1652
1419 my $req_w = (sum @$ws) || 1; 1653 my $req_w = (sum @$ws) || 1;
1420 my $req_h = (sum @$hs) || 1; 1654 my $req_h = (sum @$hs) || 1;
1421 1655
1422 # TODO: nicer code && do row_expand 1656 # now linearly scale the rows/columns to the allocated size
1423 my @col_expand = @{$self->{col_expand}}; 1657 my @col_expand = @{$self->{col_expand}};
1424 @col_expand = (1) x @$ws unless @col_expand; 1658 @col_expand = (1) x @$ws unless @col_expand;
1425 my $col_expand = (sum @col_expand) || 1; 1659 my $col_expand = (sum @col_expand) || 1;
1426 1660
1427 # linearly scale sizes
1428 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1661 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1429 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1430 1662
1431 CFClient::UI::harmonize $ws; 1663 CFPlus::UI::harmonize $ws;
1664
1665 my @row_expand = @{$self->{row_expand}};
1666 @row_expand = (1) x @$ws unless @row_expand;
1667 my $row_expand = (sum @row_expand) || 1;
1668
1669 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1670
1432 CFClient::UI::harmonize $hs; 1671 CFPlus::UI::harmonize $hs;
1433 1672
1434 my $y; 1673 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1674 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1435 1675
1436 for my $r (0 .. $#{$self->{children}}) { 1676 for my $widget ($self->children) {
1437 my $row = $self->{children}[$r] 1677 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)};
1438 or next;
1439 1678
1440 my $x = 0; 1679 $widget->configure (
1441 my $row_h = $hs->[$r]; 1680 $x[$c], $y[$r],
1681 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1442 1682 );
1443 for my $c (0 .. $#$row) { 1683 }
1444 my $col_w = $ws->[$c];
1445 1684
1446 if (my $widget = $row->[$c]) { 1685 1
1447 $widget->configure ($x, $y, $col_w, $row_h); 1686}
1448 }
1449 1687
1450 $x += $col_w; 1688#############################################################################
1689
1690package CFPlus::UI::Fixed;
1691
1692use List::Util qw(min max);
1693
1694our @ISA = CFPlus::UI::Container::;
1695
1696sub _scale($$$) {
1697 my ($rel, $val, $max) = @_;
1698
1699 $rel ? $val * $max : $val
1700}
1701
1702sub size_request {
1703 my ($self) = @_;
1704
1705 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1706
1707 # determine overall size by querying abs widgets
1708 for my $child ($self->visible_children) {
1709 unless ($child->{c_rel}) {
1710 my $x = $child->{c_x};
1711 my $y = $child->{c_y};
1712
1713 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1714 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1451 } 1715 }
1716 }
1452 1717
1453 $y += $row_h; 1718 my $W = $x2 - $x1;
1719 my $H = $y2 - $y1;
1720
1721 # now layout remaining widgets
1722 for my $child ($self->visible_children) {
1723 if ($child->{c_rel}) {
1724 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1725 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1726
1727 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1728 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1729 }
1730 }
1731
1732 my $W = $x2 - $x1;
1733 my $H = $y2 - $y1;
1734
1735 ($W, $H)
1736}
1737
1738sub invoke_size_allocate {
1739 my ($self, $W, $H) = @_;
1740
1741 for my $child ($self->visible_children) {
1742 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1743 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1744
1745 $x += $child->{c_halign} * $child->{req_w};
1746 $y += $child->{c_valign} * $child->{req_h};
1747
1748 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1454 } 1749 }
1455 1750
1456 1 1751 1
1457} 1752}
1458 1753
1459sub find_widget {
1460 my ($self, $x, $y) = @_;
1461
1462 $x -= $self->{x};
1463 $y -= $self->{y};
1464
1465 my $res;
1466
1467 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1468 $res = $_->find_widget ($x, $y)
1469 and return $res;
1470 }
1471
1472 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1473}
1474
1475sub _draw {
1476 my ($self) = @_;
1477
1478 for (grep $_, @{$self->{children}}) {
1479 $_->draw for grep $_, @$_;
1480 }
1481}
1482
1483############################################################################# 1754#############################################################################
1484 1755
1485package CFClient::UI::Box; 1756package CFPlus::UI::Box;
1486 1757
1487our @ISA = CFClient::UI::Container::; 1758our @ISA = CFPlus::UI::Container::;
1488 1759
1489sub size_request { 1760sub size_request {
1490 my ($self) = @_; 1761 my ($self) = @_;
1762
1763 my @children = $self->visible_children;
1491 1764
1492 $self->{vertical} 1765 $self->{vertical}
1493 ? ( 1766 ? (
1494 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1767 (List::Util::max map $_->{req_w}, @children),
1495 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1768 (List::Util::sum map $_->{req_h}, @children),
1496 ) 1769 )
1497 : ( 1770 : (
1498 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1771 (List::Util::sum map $_->{req_w}, @children),
1499 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1772 (List::Util::max map $_->{req_h}, @children),
1500 ) 1773 )
1501} 1774}
1502 1775
1503sub invoke_size_allocate { 1776sub invoke_size_allocate {
1504 my ($self, $w, $h) = @_; 1777 my ($self, $w, $h) = @_;
1525 $req[$_] += $space * $children[$_]{expand} 1798 $req[$_] += $space * $children[$_]{expand}
1526 for 0 .. $#children; 1799 for 0 .. $#children;
1527 } 1800 }
1528 } 1801 }
1529 1802
1530 CFClient::UI::harmonize \@req; 1803 CFPlus::UI::harmonize \@req;
1531 1804
1532 my $pos = 0; 1805 my $pos = 0;
1533 for (0 .. $#children) { 1806 for (0 .. $#children) {
1534 my $alloc = $req[$_]; 1807 my $alloc = $req[$_];
1535 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1808 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1540 1 1813 1
1541} 1814}
1542 1815
1543############################################################################# 1816#############################################################################
1544 1817
1545package CFClient::UI::HBox; 1818package CFPlus::UI::HBox;
1546 1819
1547our @ISA = CFClient::UI::Box::; 1820our @ISA = CFPlus::UI::Box::;
1548 1821
1549sub new { 1822sub new {
1550 my $class = shift; 1823 my $class = shift;
1551 1824
1552 $class->SUPER::new ( 1825 $class->SUPER::new (
1555 ) 1828 )
1556} 1829}
1557 1830
1558############################################################################# 1831#############################################################################
1559 1832
1560package CFClient::UI::VBox; 1833package CFPlus::UI::VBox;
1561 1834
1562our @ISA = CFClient::UI::Box::; 1835our @ISA = CFPlus::UI::Box::;
1563 1836
1564sub new { 1837sub new {
1565 my $class = shift; 1838 my $class = shift;
1566 1839
1567 $class->SUPER::new ( 1840 $class->SUPER::new (
1570 ) 1843 )
1571} 1844}
1572 1845
1573############################################################################# 1846#############################################################################
1574 1847
1575package CFClient::UI::Label; 1848package CFPlus::UI::Label;
1576 1849
1577our @ISA = CFClient::UI::DrawBG::; 1850our @ISA = CFPlus::UI::DrawBG::;
1578 1851
1579use CFClient::OpenGL; 1852use CFPlus::OpenGL;
1580 1853
1581sub new { 1854sub new {
1582 my ($class, %arg) = @_; 1855 my ($class, %arg) = @_;
1583 1856
1584 my $self = $class->SUPER::new ( 1857 my $self = $class->SUPER::new (
1587 #active_bg => none 1860 #active_bg => none
1588 #font => default_font 1861 #font => default_font
1589 #text => initial text 1862 #text => initial text
1590 #markup => initial narkup 1863 #markup => initial narkup
1591 #max_w => maximum pixel width 1864 #max_w => maximum pixel width
1865 #style => 0, # render flags
1592 ellipsise => 3, # end 1866 ellipsise => 3, # end
1593 layout => (new CFClient::Layout), 1867 layout => (new CFPlus::Layout),
1594 fontsize => 1, 1868 fontsize => 1,
1595 align => -1, 1869 align => -1,
1596 valign => -1, 1870 valign => -1,
1597 padding_x => 2, 1871 padding_x => 2,
1598 padding_y => 2, 1872 padding_y => 2,
1599 can_events => 0, 1873 can_events => 0,
1600 %arg 1874 %arg
1601 ); 1875 );
1602 1876
1603 if (exists $self->{template}) { 1877 if (exists $self->{template}) {
1604 my $layout = new CFClient::Layout; 1878 my $layout = new CFPlus::Layout;
1605 $layout->set_text (delete $self->{template}); 1879 $layout->set_text (delete $self->{template});
1606 $self->{template} = $layout; 1880 $self->{template} = $layout;
1607 } 1881 }
1608 1882
1609 if (exists $self->{markup}) { 1883 if (exists $self->{markup}) {
1611 } else { 1885 } else {
1612 $self->set_text (delete $self->{text}); 1886 $self->set_text (delete $self->{text});
1613 } 1887 }
1614 1888
1615 $self 1889 $self
1616}
1617
1618sub escape($) {
1619 local $_ = $_[0];
1620
1621 s/&/&amp;/g;
1622 s/>/&gt;/g;
1623 s/</&lt;/g;
1624
1625 $_
1626} 1890}
1627 1891
1628sub update { 1892sub update {
1629 my ($self) = @_; 1893 my ($self) = @_;
1630 1894
1754 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2018 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1755 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2019 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1756 : ($self->{h} - $size->[1]) * 0.5); 2020 : ($self->{h} - $size->[1]) * 0.5);
1757 }; 2021 };
1758 2022
1759 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2023# unless ($self->{list}) {
1760 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2024# $self->{list} = CFPlus::OpenGL::glGenList;
2025# CFPlus::OpenGL::glNewList $self->{list};
2026# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2027# CFPlus::OpenGL::glEndList;
2028# }
2029#
2030# CFPlus::OpenGL::glCallList $self->{list};
1761 2031
1762 $self->{layout}->render ($self->{ox}, $self->{oy}); 2032 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1763} 2033}
2034
2035#sub destroy {
2036# my ($self) = @_;
2037#
2038# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2039#
2040# $self->SUPER::destroy;
2041#}
1764 2042
1765############################################################################# 2043#############################################################################
1766 2044
1767package CFClient::UI::EntryBase; 2045package CFPlus::UI::EntryBase;
1768 2046
1769our @ISA = CFClient::UI::Label::; 2047our @ISA = CFPlus::UI::Label::;
1770 2048
1771use CFClient::OpenGL; 2049use CFPlus::OpenGL;
1772 2050
1773sub new { 2051sub new {
1774 my $class = shift; 2052 my $class = shift;
1775 2053
1776 $class->SUPER::new ( 2054 $class->SUPER::new (
1780 active_fg => [0, 0, 0], 2058 active_fg => [0, 0, 0],
1781 can_hover => 1, 2059 can_hover => 1,
1782 can_focus => 1, 2060 can_focus => 1,
1783 valign => 0, 2061 valign => 0,
1784 can_events => 1, 2062 can_events => 1,
2063 ellipsise => 0,
1785 #text => ... 2064 #text => ...
1786 #hidden => "*", 2065 #hidden => "*",
1787 @_ 2066 @_
1788 ) 2067 )
1789} 2068}
1834 my $sym = $ev->{sym}; 2113 my $sym = $ev->{sym};
1835 my $uni = $ev->{unicode}; 2114 my $uni = $ev->{unicode};
1836 2115
1837 my $text = $self->get_text; 2116 my $text = $self->get_text;
1838 2117
2118 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2119
1839 if ($uni == 8) { 2120 if ($uni == 8) {
1840 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2121 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1841 } elsif ($uni == 127) { 2122 } elsif ($uni == 127) {
1842 substr $text, $self->{cursor}, 1, ""; 2123 substr $text, $self->{cursor}, 1, "";
1843 } elsif ($sym == CFClient::SDLK_LEFT) { 2124 } elsif ($sym == CFPlus::SDLK_LEFT) {
1844 --$self->{cursor} if $self->{cursor}; 2125 --$self->{cursor} if $self->{cursor};
1845 } elsif ($sym == CFClient::SDLK_RIGHT) { 2126 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1846 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2127 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1847 } elsif ($sym == CFClient::SDLK_HOME) { 2128 } elsif ($sym == CFPlus::SDLK_HOME) {
2129 # what a hack
2130 $self->{cursor} =
2131 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2132 ? length $1
2133 : 0;
2134 } elsif ($sym == CFPlus::SDLK_END) {
2135 # uh, again
2136 $self->{cursor} =
2137 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2138 ? $self->{cursor} + length $1
2139 : length $self->{text};
2140 } elsif ($uni == 21) { # ctrl-u
2141 $text = "";
1848 $self->{cursor} = 0; 2142 $self->{cursor} = 0;
1849 } elsif ($sym == CFClient::SDLK_END) {
1850 $self->{cursor} = length $text;
1851 } elsif ($uni == 27) { 2143 } elsif ($uni == 27) {
1852 $self->emit ('escape'); 2144 $self->emit ('escape');
1853 } elsif ($uni) { 2145 } elsif ($uni == 0x0d) {
2146 substr $text, $self->{cursor}++, 0, "\012";
2147 } elsif ($uni >= 0x20) {
1854 substr $text, $self->{cursor}++, 0, chr $uni; 2148 substr $text, $self->{cursor}++, 0, chr $uni;
1855 } else { 2149 } else {
1856 return 0; 2150 return 0;
1857 } 2151 }
1858 2152
1859 $self->_set_text ($text); 2153 $self->_set_text ($text);
1860 2154
1861 $self->realloc; 2155 $self->realloc;
2156 $self->update;
1862 2157
1863 1 2158 1
1864} 2159}
1865 2160
1866sub invoke_focus_in { 2161sub invoke_focus_in {
1928 utf8::encode $text; 2223 utf8::encode $text;
1929 2224
1930 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2225 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1931 } 2226 }
1932 2227
1933 glColor @{$self->{fg}};
1934 glBegin GL_LINES; 2228 glBegin GL_LINES;
1935 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2229 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1936 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2230 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1937 glEnd; 2231 glEnd;
1938 } 2232 }
1939} 2233}
1940 2234
2235#############################################################################
2236
1941package CFClient::UI::Entry; 2237package CFPlus::UI::Entry;
1942 2238
1943our @ISA = CFClient::UI::EntryBase::; 2239our @ISA = CFPlus::UI::EntryBase::;
1944 2240
1945use CFClient::OpenGL; 2241use CFPlus::OpenGL;
1946 2242
1947sub invoke_key_down { 2243sub invoke_key_down {
1948 my ($self, $ev) = @_; 2244 my ($self, $ev) = @_;
1949 2245
1950 my $sym = $ev->{sym}; 2246 my $sym = $ev->{sym};
1951 2247
1952 if ($sym == 13) { 2248 if ($ev->{uni} == 0x0d || $sym == 13) {
1953 unshift @{$self->{history}}, 2249 unshift @{$self->{history}},
1954 my $txt = $self->get_text; 2250 my $txt = $self->get_text;
1955 2251
1956 $self->{history_pointer} = -1; 2252 $self->{history_pointer} = -1;
1957 $self->{history_saveback} = ''; 2253 $self->{history_saveback} = '';
1958 $self->emit (activate => $txt); 2254 $self->emit (activate => $txt);
1959 $self->update; 2255 $self->update;
1960 2256
1961 } elsif ($sym == CFClient::SDLK_UP) { 2257 } elsif ($sym == CFPlus::SDLK_UP) {
1962 if ($self->{history_pointer} < 0) { 2258 if ($self->{history_pointer} < 0) {
1963 $self->{history_saveback} = $self->get_text; 2259 $self->{history_saveback} = $self->get_text;
1964 } 2260 }
1965 if (@{$self->{history} || []} > 0) { 2261 if (@{$self->{history} || []} > 0) {
1966 $self->{history_pointer}++; 2262 $self->{history_pointer}++;
1968 $self->{history_pointer} = @{$self->{history} || []} - 1; 2264 $self->{history_pointer} = @{$self->{history} || []} - 1;
1969 } 2265 }
1970 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2266 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1971 } 2267 }
1972 2268
1973 } elsif ($sym == CFClient::SDLK_DOWN) { 2269 } elsif ($sym == CFPlus::SDLK_DOWN) {
1974 $self->{history_pointer}--; 2270 $self->{history_pointer}--;
1975 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2271 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1976 2272
1977 if ($self->{history_pointer} >= 0) { 2273 if ($self->{history_pointer} >= 0) {
1978 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2274 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1987 1 2283 1
1988} 2284}
1989 2285
1990############################################################################# 2286#############################################################################
1991 2287
2288package CFPlus::UI::TextEdit;
2289
2290our @ISA = CFPlus::UI::EntryBase::;
2291
2292use CFPlus::OpenGL;
2293
2294sub move_cursor_ver {
2295 my ($self, $dy) = @_;
2296
2297 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2298
2299 $y += $dy;
2300
2301 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2302 $self->{cursor} = $index;
2303 delete $self->{cur_h};
2304 $self->update;
2305 return;
2306 }
2307}
2308
2309sub invoke_key_down {
2310 my ($self, $ev) = @_;
2311
2312 my $sym = $ev->{sym};
2313
2314 if ($sym == CFPlus::SDLK_UP) {
2315 $self->move_cursor_ver (-1);
2316 } elsif ($sym == CFPlus::SDLK_DOWN) {
2317 $self->move_cursor_ver (+1);
2318 } else {
2319 return $self->SUPER::invoke_key_down ($ev)
2320 }
2321
2322 1
2323}
2324
2325#############################################################################
2326
1992package CFClient::UI::Button; 2327package CFPlus::UI::Button;
1993 2328
1994our @ISA = CFClient::UI::Label::; 2329our @ISA = CFPlus::UI::Label::;
1995 2330
1996use CFClient::OpenGL; 2331use CFPlus::OpenGL;
1997 2332
1998my @tex = 2333my @tex =
1999 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2334 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2000 qw(b1_button_active.png); 2335 qw(b1_button_inactive.png b1_button_active.png);
2001 2336
2002sub new { 2337sub new {
2003 my $class = shift; 2338 my $class = shift;
2004 2339
2005 $class->SUPER::new ( 2340 $class->SUPER::new (
2006 padding_x => 4, 2341 padding_x => 4,
2007 padding_y => 4, 2342 padding_y => 4,
2008 fg => [1, 1, 1], 2343 fg => [1.0, 1.0, 1.0],
2009 active_fg => [0, 0, 1], 2344 active_fg => [0.8, 0.8, 0.8],
2010 can_hover => 1, 2345 can_hover => 1,
2011 align => 0, 2346 align => 0,
2012 valign => 0, 2347 valign => 0,
2013 can_events => 1, 2348 can_events => 1,
2014 @_ 2349 @_
2032 2367
2033 glEnable GL_TEXTURE_2D; 2368 glEnable GL_TEXTURE_2D;
2034 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2369 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2035 glColor 0, 0, 0, 1; 2370 glColor 0, 0, 0, 1;
2036 2371
2372 my $tex = $tex[$GRAB == $self];
2037 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2373 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2038 2374
2039 glDisable GL_TEXTURE_2D; 2375 glDisable GL_TEXTURE_2D;
2040 2376
2041 $self->SUPER::_draw; 2377 $self->SUPER::_draw;
2042} 2378}
2043 2379
2044############################################################################# 2380#############################################################################
2045 2381
2382package CFPlus::UI::CheckBox;
2383
2384our @ISA = CFPlus::UI::DrawBG::;
2385
2386my @tex =
2387 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2388 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2389
2390use CFPlus::OpenGL;
2391
2392sub new {
2393 my $class = shift;
2394
2395 $class->SUPER::new (
2396 padding_x => 2,
2397 padding_y => 2,
2398 fg => [1, 1, 1],
2399 active_fg => [1, 1, 0],
2400 bg => [0, 0, 0, 0.2],
2401 active_bg => [1, 1, 1, 0.5],
2402 state => 0,
2403 can_hover => 1,
2404 @_
2405 )
2406}
2407
2408sub size_request {
2409 my ($self) = @_;
2410
2411 (6) x 2
2412}
2413
2414sub toggle {
2415 my ($self) = @_;
2416
2417 $self->{state} = !$self->{state};
2418 $self->emit (changed => $self->{state});
2419 $self->update;
2420}
2421
2422sub invoke_button_down {
2423 my ($self, $ev, $x, $y) = @_;
2424
2425 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2426 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2427 $self->toggle;
2428 } else {
2429 return 0
2430 }
2431
2432 1
2433}
2434
2435sub _draw {
2436 my ($self) = @_;
2437
2438 $self->SUPER::_draw;
2439
2440 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2441
2442 my ($w, $h) = @$self{qw(w h)};
2443
2444 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2445
2446 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2447
2448 my $tex = $self->{state} ? $tex[1] : $tex[0];
2449
2450 glEnable GL_TEXTURE_2D;
2451 $tex->draw_quad_alpha (0, 0, $s, $s);
2452 glDisable GL_TEXTURE_2D;
2453}
2454
2455#############################################################################
2456
2457package CFPlus::UI::Image;
2458
2459our @ISA = CFPlus::UI::Base::;
2460
2461use CFPlus::OpenGL;
2462
2463our %texture_cache;
2464
2465sub new {
2466 my $class = shift;
2467
2468 my $self = $class->SUPER::new (
2469 can_events => 0,
2470 @_,
2471 );
2472
2473 $self->{path} || $self->{tex}
2474 or Carp::croak "'path' or 'tex' attributes required";
2475
2476 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2477 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2478
2479 CFPlus::weaken $texture_cache{$self->{path}};
2480
2481 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2482
2483 $self
2484}
2485
2486sub STORABLE_freeze {
2487 my ($self, $cloning) = @_;
2488
2489 $self->{path}
2490 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n";
2491
2492 $self->{path}
2493}
2494
2495sub STORABLE_attach {
2496 my ($self, $cloning, $path) = @_;
2497
2498 $self->new (path => $path)
2499}
2500
2501sub size_request {
2502 my ($self) = @_;
2503
2504 ($self->{tex}{w}, $self->{tex}{h})
2505}
2506
2507sub _draw {
2508 my ($self) = @_;
2509
2510 my $tex = $self->{tex};
2511
2512 my ($w, $h) = ($self->{w}, $self->{h});
2513
2514 if ($self->{rot90}) {
2515 glRotate 90, 0, 0, 1;
2516 glTranslate 0, -$self->{w}, 0;
2517
2518 ($w, $h) = ($h, $w);
2519 }
2520
2521 glEnable GL_TEXTURE_2D;
2522 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2523
2524 $tex->draw_quad (0, 0, $w, $h);
2525
2526 glDisable GL_TEXTURE_2D;
2527}
2528
2529#############################################################################
2530
2046package CFClient::UI::ImageButton; 2531package CFPlus::UI::ImageButton;
2047 2532
2048our @ISA = CFClient::UI::Image::; 2533our @ISA = CFPlus::UI::Image::;
2049 2534
2050use CFClient::OpenGL; 2535use CFPlus::OpenGL;
2051 2536
2052my %textures; 2537my %textures;
2053 2538
2054sub new { 2539sub new {
2055 my $class = shift; 2540 my $class = shift;
2077 1 2562 1
2078} 2563}
2079 2564
2080############################################################################# 2565#############################################################################
2081 2566
2082package CFClient::UI::CheckBox;
2083
2084our @ISA = CFClient::UI::DrawBG::;
2085
2086my @tex =
2087 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
2088 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2089
2090use CFClient::OpenGL;
2091
2092sub new {
2093 my $class = shift;
2094
2095 $class->SUPER::new (
2096 padding_x => 2,
2097 padding_y => 2,
2098 fg => [1, 1, 1],
2099 active_fg => [1, 1, 0],
2100 bg => [0, 0, 0, 0.2],
2101 active_bg => [1, 1, 1, 0.5],
2102 state => 0,
2103 can_hover => 1,
2104 @_
2105 )
2106}
2107
2108sub size_request {
2109 my ($self) = @_;
2110
2111 (6) x 2
2112}
2113
2114sub toggle {
2115 my ($self) = @_;
2116
2117 $self->{state} = !$self->{state};
2118 $self->emit (changed => $self->{state});
2119 $self->update;
2120}
2121
2122sub invoke_button_down {
2123 my ($self, $ev, $x, $y) = @_;
2124
2125 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2126 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2127 $self->toggle;
2128 } else {
2129 return 0
2130 }
2131
2132 1
2133}
2134
2135sub _draw {
2136 my ($self) = @_;
2137
2138 $self->SUPER::_draw;
2139
2140 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2141
2142 my ($w, $h) = @$self{qw(w h)};
2143
2144 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2145
2146 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2147
2148 my $tex = $self->{state} ? $tex[1] : $tex[0];
2149
2150 glEnable GL_TEXTURE_2D;
2151 $tex->draw_quad_alpha (0, 0, $s, $s);
2152 glDisable GL_TEXTURE_2D;
2153}
2154
2155#############################################################################
2156
2157package CFClient::UI::Image; 2567package CFPlus::UI::VGauge;
2158 2568
2159our @ISA = CFClient::UI::Base::; 2569our @ISA = CFPlus::UI::Base::;
2160
2161use CFClient::OpenGL;
2162
2163our %texture_cache;
2164
2165sub new {
2166 my $class = shift;
2167
2168 my $self = $class->SUPER::new (
2169 can_events => 0,
2170 @_,
2171 );
2172
2173 $self->{path} || $self->{tex}
2174 or Carp::croak "'path' or 'tex' attributes required";
2175
2176 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2177 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2178
2179 Scalar::Util::weaken $texture_cache{$self->{path}};
2180
2181 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2182
2183 $self
2184}
2185
2186sub size_request {
2187 my ($self) = @_;
2188
2189 ($self->{tex}{w}, $self->{tex}{h})
2190}
2191
2192sub _draw {
2193 my ($self) = @_;
2194
2195 my $tex = $self->{tex};
2196
2197 my ($w, $h) = ($self->{w}, $self->{h});
2198
2199 if ($self->{rot90}) {
2200 glRotate 90, 0, 0, 1;
2201 glTranslate 0, -$self->{w}, 0;
2202
2203 ($w, $h) = ($h, $w);
2204 }
2205
2206 glEnable GL_TEXTURE_2D;
2207 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2208
2209 $tex->draw_quad_alpha (0, 0, $w, $h);
2210
2211 glDisable GL_TEXTURE_2D;
2212}
2213
2214#############################################################################
2215
2216package CFClient::UI::VGauge;
2217
2218our @ISA = CFClient::UI::Base::;
2219 2570
2220use List::Util qw(min max); 2571use List::Util qw(min max);
2221 2572
2222use CFClient::OpenGL; 2573use CFPlus::OpenGL;
2223 2574
2224my %tex = ( 2575my %tex = (
2225 food => [ 2576 food => [
2226 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2577 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2227 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2578 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2228 ], 2579 ],
2229 grace => [ 2580 grace => [
2230 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2581 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2231 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2582 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2232 ], 2583 ],
2233 hp => [ 2584 hp => [
2234 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2585 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2235 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2586 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2236 ], 2587 ],
2237 mana => [ 2588 mana => [
2238 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2589 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2239 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2590 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2240 ], 2591 ],
2241); 2592);
2242 2593
2243# eg. VGauge->new (gauge => 'food'), default gauge: food 2594# eg. VGauge->new (gauge => 'food'), default gauge: food
2346 glDisable GL_TEXTURE_2D; 2697 glDisable GL_TEXTURE_2D;
2347} 2698}
2348 2699
2349############################################################################# 2700#############################################################################
2350 2701
2351package CFClient::UI::Gauge; 2702package CFPlus::UI::Gauge;
2352 2703
2353our @ISA = CFClient::UI::VBox::; 2704our @ISA = CFPlus::UI::VBox::;
2354 2705
2355sub new { 2706sub new {
2356 my ($class, %arg) = @_; 2707 my ($class, %arg) = @_;
2357 2708
2358 my $self = $class->SUPER::new ( 2709 my $self = $class->SUPER::new (
2360 can_hover => 1, 2711 can_hover => 1,
2361 can_events => 1, 2712 can_events => 1,
2362 %arg, 2713 %arg,
2363 ); 2714 );
2364 2715
2365 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 2716 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999");
2366 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 2717 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2367 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); 2718 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999");
2368 2719
2369 $self 2720 $self
2370} 2721}
2371 2722
2372sub set_fontsize { 2723sub set_fontsize {
2393 $self->{value}->set_text ($val); 2744 $self->{value}->set_text ($val);
2394} 2745}
2395 2746
2396############################################################################# 2747#############################################################################
2397 2748
2398package CFClient::UI::Slider; 2749package CFPlus::UI::Slider;
2399 2750
2400use strict; 2751use strict;
2401 2752
2402use CFClient::OpenGL; 2753use CFPlus::OpenGL;
2403 2754
2404our @ISA = CFClient::UI::DrawBG::; 2755our @ISA = CFPlus::UI::DrawBG::;
2405 2756
2406my @tex = 2757my @tex =
2407 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 2758 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ }
2408 qw(s1_slider.png s1_slider_bg.png); 2759 qw(s1_slider.png s1_slider_bg.png);
2409 2760
2410sub new { 2761sub new {
2411 my $class = shift; 2762 my $class = shift;
2412 2763
2480 2831
2481 $self->SUPER::invoke_button_down ($ev, $x, $y); 2832 $self->SUPER::invoke_button_down ($ev, $x, $y);
2482 2833
2483 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2834 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2484 2835
2485 $self->invoke_mouse_motion ($ev, $x, $y) 2836 $self->invoke_mouse_motion ($ev, $x, $y);
2837
2838 1
2486} 2839}
2487 2840
2488sub invoke_mouse_motion { 2841sub invoke_mouse_motion {
2489 my ($self, $ev, $x, $y) = @_; 2842 my ($self, $ev, $x, $y) = @_;
2490 2843
2506sub invoke_mouse_wheel { 2859sub invoke_mouse_wheel {
2507 my ($self, $ev) = @_; 2860 my ($self, $ev) = @_;
2508 2861
2509 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2862 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2510 2863
2864 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2865
2511 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.1); 2866 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2512 2867
2513 ! ! $delta 2868 1
2514} 2869}
2515 2870
2516sub update { 2871sub update {
2517 my ($self) = @_; 2872 my ($self) = @_;
2518 2873
2567 glDisable GL_TEXTURE_2D; 2922 glDisable GL_TEXTURE_2D;
2568} 2923}
2569 2924
2570############################################################################# 2925#############################################################################
2571 2926
2572package CFClient::UI::ValSlider; 2927package CFPlus::UI::ValSlider;
2573 2928
2574our @ISA = CFClient::UI::HBox::; 2929our @ISA = CFPlus::UI::HBox::;
2575 2930
2576sub new { 2931sub new {
2577 my ($class, %arg) = @_; 2932 my ($class, %arg) = @_;
2578 2933
2579 my $range = delete $arg{range}; 2934 my $range = delete $arg{range};
2580 2935
2581 my $self = $class->SUPER::new ( 2936 my $self = $class->SUPER::new (
2582 slider => (new CFClient::UI::Slider expand => 1, range => $range), 2937 slider => (new CFPlus::UI::Slider expand => 1, range => $range),
2583 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}), 2938 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}),
2584 to_value => sub { shift }, 2939 to_value => sub { shift },
2585 from_value => sub { shift }, 2940 from_value => sub { shift },
2586 %arg, 2941 %arg,
2587 ); 2942 );
2588 2943
2608sub set_range { shift->{slider}->set_range (@_) } 2963sub set_range { shift->{slider}->set_range (@_) }
2609sub set_value { shift->{slider}->set_value (@_) } 2964sub set_value { shift->{slider}->set_value (@_) }
2610 2965
2611############################################################################# 2966#############################################################################
2612 2967
2613package CFClient::UI::TextScroller; 2968package CFPlus::UI::TextScroller;
2614 2969
2615our @ISA = CFClient::UI::HBox::; 2970our @ISA = CFPlus::UI::HBox::;
2616 2971
2617use CFClient::OpenGL; 2972use CFPlus::OpenGL;
2618 2973
2619sub new { 2974sub new {
2620 my $class = shift; 2975 my $class = shift;
2621 2976
2622 my $self = $class->SUPER::new ( 2977 my $self = $class->SUPER::new (
2624 can_events => 1, 2979 can_events => 1,
2625 indent => 0, 2980 indent => 0,
2626 #font => default_font 2981 #font => default_font
2627 @_, 2982 @_,
2628 2983
2629 layout => (new CFClient::Layout), 2984 layout => (new CFPlus::Layout),
2630 par => [], 2985 par => [],
2986 max_par => 0,
2631 height => 0, 2987 height => 0,
2632 children => [ 2988 children => [
2633 (new CFClient::UI::Empty expand => 1), 2989 (new CFPlus::UI::Empty expand => 1),
2634 (new CFClient::UI::Slider vertical => 1), 2990 (new CFPlus::UI::Slider vertical => 1),
2635 ], 2991 ],
2636 ); 2992 );
2637 2993
2638 $self->{children}[1]->connect (changed => sub { $self->update }); 2994 $self->{children}[1]->connect (changed => sub { $self->update });
2639 2995
2648} 3004}
2649 3005
2650sub size_request { 3006sub size_request {
2651 my ($self) = @_; 3007 my ($self) = @_;
2652 3008
2653 my ($empty, $slider) = @{ $self->{children} }; 3009 my ($empty, $slider) = $self->visible_children;
2654 3010
2655 local $self->{children} = [$empty, $slider]; 3011 local $self->{children} = [$empty, $slider];
2656 $self->SUPER::size_request 3012 $self->SUPER::size_request
2657} 3013}
2658 3014
2716 3072
2717 # todo: base offset on lines or so, not on pixels 3073 # todo: base offset on lines or so, not on pixels
2718 $self->{children}[1]->set_value ($offset); 3074 $self->{children}[1]->set_value ($offset);
2719} 3075}
2720 3076
3077sub current_paragraph {
3078 my ($self) = @_;
3079
3080 $self->{top_paragraph} - 1
3081}
3082
3083sub scroll_to {
3084 my ($self, $para) = @_;
3085
3086 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
3087
3088 $self->{scroll_to} = $para;
3089 $self->update;
3090}
3091
2721sub clear { 3092sub clear {
2722 my ($self) = @_; 3093 my ($self) = @_;
2723 3094
2724 my (undef, undef, @other) = @{ $self->{children} }; 3095 my (undef, undef, @other) = @{ $self->{children} };
2725 $self->remove ($_) for @other; 3096 $self->remove ($_) for @other;
2728 $self->{height} = 0; 3099 $self->{height} = 0;
2729 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 3100 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2730} 3101}
2731 3102
2732sub add_paragraph { 3103sub add_paragraph {
2733 my ($self, $color, $para, $indent) = @_; 3104 my $self = shift;
2734 3105
2735 my ($text, @w) = ref $para ? @$para : $para; 3106 for my $para (@_) {
2736
2737 $para = { 3107 $para = {
3108 fg => [1, 1, 1, 1],
3109 indent => 0,
3110 markup => "",
3111 widget => [],
3112 ref $para ? %$para : (markup => $para),
2738 w => 1e10, 3113 w => 1e10,
2739 wrapped => 1, 3114 wrapped => 1,
2740 fg => $color,
2741 indent => $indent,
2742 markup => $text,
2743 widget => \@w,
2744 }; 3115 };
2745 3116
2746 $self->add (@w) if @w; 3117 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2747 push @{$self->{par}}, $para; 3118 push @{$self->{par}}, $para;
3119 }
3120
3121 if (my $max = $self->{max_par}) {
3122 shift @{$self->{par}} while @{$self->{par}} > $max;
3123 }
2748 3124
2749 $self->{need_reflow}++; 3125 $self->{need_reflow}++;
2750 $self->update; 3126 $self->update;
2751} 3127}
2752 3128
2753sub scroll_to_bottom { 3129sub scroll_to_bottom {
2754 my ($self) = @_; 3130 my ($self) = @_;
2755 3131
2756 $self->{scroll_to_bottom} = 1; 3132 $self->{scroll_to} = $#{$self->{par}};
2757 $self->update; 3133 $self->update;
2758} 3134}
2759 3135
3136sub force_uptodate {
3137 my ($self) = @_;
3138
3139 if (delete $self->{need_reflow}) {
3140 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3141
3142 my $height = 0;
3143
3144 for my $para (@{$self->{par}}) {
3145 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
3146 my $layout = $self->get_layout ($para);
3147 my ($w, $h) = $layout->size;
3148
3149 $para->{w} = $w + $para->{indent};
3150 $para->{h} = $h;
3151 $para->{wrapped} = $layout->has_wrapped;
3152 }
3153
3154 $para->{y} = $height;
3155 $height += $para->{h};
3156 }
3157
3158 $self->{height} = $height;
3159 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
3160
3161 delete $self->{texture};
3162 }
3163
3164 if (my $paridx = delete $self->{scroll_to}) {
3165 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
3166 }
3167}
3168
2760sub update { 3169sub update {
2761 my ($self) = @_; 3170 my ($self) = @_;
2762 3171
2763 $self->SUPER::update; 3172 $self->SUPER::update;
2764 3173
2765 return unless $self->{h} > 0; 3174 return unless $self->{h} > 0;
2766 3175
2767 delete $self->{texture}; 3176 delete $self->{texture};
2768 3177
2769 $ROOT->on_post_alloc ($self => sub { 3178 $ROOT->on_post_alloc ($self => sub {
3179 $self->force_uptodate;
3180
2770 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3181 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2771 3182
2772 if (delete $self->{need_reflow}) {
2773 my $height = 0;
2774
2775 for my $para (@{$self->{par}}) {
2776 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2777 my $layout = $self->get_layout ($para);
2778 my ($w, $h) = $layout->size;
2779
2780 $para->{w} = $w + $para->{indent};
2781 $para->{h} = $h;
2782 $para->{wrapped} = $layout->has_wrapped;
2783 }
2784
2785 $height += $para->{h};
2786 }
2787
2788 $self->{height} = $height;
2789
2790 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2791
2792 delete $self->{texture};
2793 }
2794
2795 if (delete $self->{scroll_to_bottom}) {
2796 $self->{children}[1]->set_value (1e10);
2797 }
2798
2799 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 3183 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2800 glClearColor 0, 0, 0, 0; 3184 glClearColor 0, 0, 0, 0;
2801 glClear GL_COLOR_BUFFER_BIT; 3185 glClear GL_COLOR_BUFFER_BIT;
2802 3186
3187 package CFPlus::UI::Base;
3188 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3189 (0, 0, $self->{w}, $self->{h});
3190
3191 my $top = int $self->{children}[1]{range}[0];
3192
3193 my $paridx = 0;
3194 my $top_paragraph;
2803 my $top = int $self->{children}[1]{range}[0]; 3195 my $top = int $self->{children}[1]{range}[0];
2804 3196
2805 my $y0 = $top; 3197 my $y0 = $top;
2806 my $y1 = $top + $H; 3198 my $y1 = $top + $H;
2807 3199
2808 my $y = 0;
2809
2810 for my $para (@{$self->{par}}) { 3200 for my $para (@{$self->{par}}) {
2811 my $h = $para->{h}; 3201 my $h = $para->{h};
3202 my $y = $para->{y};
2812 3203
2813 if ($y0 < $y + $h && $y < $y1) { 3204 if ($y0 < $y + $h && $y < $y1) {
2814
2815 my $layout = $self->get_layout ($para); 3205 my $layout = $self->get_layout ($para);
2816 3206
2817 $layout->render ($para->{indent}, $y - $y0); 3207 $layout->render ($para->{indent}, $y - $y0);
2818 3208
2819 if (my @w = @{ $para->{widget} }) { 3209 if (my @w = @{ $para->{widget} }) {
2828 $_->draw; 3218 $_->draw;
2829 } 3219 }
2830 } 3220 }
2831 } 3221 }
2832 3222
2833 $y += $h; 3223 $paridx++;
3224 $top_paragraph ||= $paridx if $y >= $top;
2834 } 3225 }
3226
3227 $self->{top_paragraph} = $top_paragraph;
2835 }; 3228 };
2836 }); 3229 });
2837} 3230}
2838 3231
2839sub reconfigure { 3232sub reconfigure {
2857 $self->{children}[1]->draw; 3250 $self->{children}[1]->draw;
2858} 3251}
2859 3252
2860############################################################################# 3253#############################################################################
2861 3254
2862package CFClient::UI::Animator; 3255package CFPlus::UI::Animator;
2863 3256
2864use CFClient::OpenGL; 3257use CFPlus::OpenGL;
2865 3258
2866our @ISA = CFClient::UI::Bin::; 3259our @ISA = CFPlus::UI::Bin::;
2867 3260
2868sub moveto { 3261sub moveto {
2869 my ($self, $x, $y) = @_; 3262 my ($self, $x, $y) = @_;
2870 3263
2871 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3264 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
2899 glPopMatrix; 3292 glPopMatrix;
2900} 3293}
2901 3294
2902############################################################################# 3295#############################################################################
2903 3296
2904package CFClient::UI::Flopper; 3297package CFPlus::UI::Flopper;
2905 3298
2906our @ISA = CFClient::UI::Button::; 3299our @ISA = CFPlus::UI::Button::;
2907 3300
2908sub new { 3301sub new {
2909 my $class = shift; 3302 my $class = shift;
2910 3303
2911 my $self = $class->SUPER::new ( 3304 my $self = $class->SUPER::new (
2923 $self->{other}->toggle_visibility; 3316 $self->{other}->toggle_visibility;
2924} 3317}
2925 3318
2926############################################################################# 3319#############################################################################
2927 3320
2928package CFClient::UI::Tooltip; 3321package CFPlus::UI::Tooltip;
2929 3322
2930our @ISA = CFClient::UI::Bin::; 3323our @ISA = CFPlus::UI::Bin::;
2931 3324
2932use CFClient::OpenGL; 3325use CFPlus::OpenGL;
2933 3326
2934sub new { 3327sub new {
2935 my $class = shift; 3328 my $class = shift;
2936 3329
2937 $class->SUPER::new ( 3330 $class->SUPER::new (
2940 ) 3333 )
2941} 3334}
2942 3335
2943sub set_tooltip_from { 3336sub set_tooltip_from {
2944 my ($self, $widget) = @_; 3337 my ($self, $widget) = @_;
3338
3339 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3340 if $widget->{tooltip} =~ /^#(.*)$/;
2945 3341
2946 my $tooltip = $widget->{tooltip}; 3342 my $tooltip = $widget->{tooltip};
2947 3343
2948 if ($ENV{CFPLUS_DEBUG} & 2) { 3344 if ($ENV{CFPLUS_DEBUG} & 2) {
2949 $tooltip .= "\n\n" . (ref $widget) . "\n" 3345 $tooltip .= "\n\n" . (ref $widget) . "\n"
2953 } 3349 }
2954 3350
2955 $tooltip =~ s/^\n+//; 3351 $tooltip =~ s/^\n+//;
2956 $tooltip =~ s/\n+$//; 3352 $tooltip =~ s/\n+$//;
2957 3353
2958 $self->add (new CFClient::UI::Label 3354 $self->add (new CFPlus::UI::Label
2959 markup => $tooltip, 3355 markup => $tooltip,
2960 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3356 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2961 fontsize => 0.8, 3357 fontsize => 0.8,
2962 fg => [0, 0, 0, 1], 3358 style => 1, # FLAG_INVERSE
2963 ellipsise => 0, 3359 ellipsise => 0,
2964 font => ($widget->{tooltip_font} || $::FONT_PROP), 3360 font => ($widget->{tooltip_font} || $::FONT_PROP),
2965 ); 3361 );
2966} 3362}
2967 3363
2986 3382
2987 $self->{root}->on_post_alloc ("move_$self" => sub { 3383 $self->{root}->on_post_alloc ("move_$self" => sub {
2988 my $widget = $self->{owner} 3384 my $widget = $self->{owner}
2989 or return; 3385 or return;
2990 3386
3387 if ($widget->{visible}) {
2991 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 3388 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2992 3389
2993 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 3390 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2994 if $x + $self->{w} > $self->{root}{w}; 3391 if $x + $self->{w} > $self->{root}{w};
2995 3392
2996 $self->move_abs ($x, $y); 3393 $self->move_abs ($x, $y);
3394 } else {
3395 $self->hide;
3396 }
2997 }); 3397 });
2998} 3398}
2999 3399
3000sub _draw { 3400sub _draw {
3001 my ($self) = @_; 3401 my ($self) = @_;
3025 $self->SUPER::_draw; 3425 $self->SUPER::_draw;
3026} 3426}
3027 3427
3028############################################################################# 3428#############################################################################
3029 3429
3030package CFClient::UI::Face; 3430package CFPlus::UI::Face;
3031 3431
3032our @ISA = CFClient::UI::Base::; 3432our @ISA = CFPlus::UI::DrawBG::;
3033 3433
3034use CFClient::OpenGL; 3434use CFPlus::OpenGL;
3035 3435
3036sub new { 3436sub new {
3037 my $class = shift; 3437 my $class = shift;
3038 3438
3039 my $self = $class->SUPER::new ( 3439 my $self = $class->SUPER::new (
3440 size_w => 32,
3441 size_h => 8,
3040 aspect => 1, 3442 aspect => 1,
3041 can_events => 0, 3443 can_events => 0,
3042 @_, 3444 @_,
3043 ); 3445 );
3044 3446
3045 if ($self->{anim} && $self->{animspeed}) { 3447 if ($self->{anim} && $self->{animspeed}) {
3046 Scalar::Util::weaken (my $widget = $self); 3448 CFPlus::weaken (my $widget = $self);
3047 3449
3450 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3451 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3048 $self->{timer} = Event->timer ( 3452 $self->{timer} = Event->timer (
3049 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3050 hard => 1, 3453 parked => 1,
3051 interval => $self->{animspeed},
3052 cb => sub { 3454 cb => sub {
3455 return unless $::CONN && $widget;
3456
3053 ++$widget->{frame}; 3457 ++$widget->{frame};
3458 $widget->update_face;
3054 $widget->update; 3459 $widget->update;
3460
3461 $widget->update_timer;
3055 }, 3462 },
3056 ); 3463 );
3464
3465 $self->update_face;
3466 $self->update_timer;
3057 } 3467 }
3058 3468
3059 $self 3469 $self
3060} 3470}
3061 3471
3472sub update_timer {
3473 my ($self) = @_;
3474
3475 return unless $self->{timer};
3476
3477 if ($self->{visible}) {
3478 $self->{timer}->at (
3479 $self->{anim_start}
3480 + $self->{animspeed}
3481 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3482 );
3483 $self->{timer}->start;
3484 } else {
3485 $self->{timer}->stop;
3486 }
3487}
3488
3489sub update_face {
3490 my ($self) = @_;
3491
3492 return unless $::CONN;
3493
3494 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3495 if ($anim && @$anim) {
3496 delete $self->{wait_face};
3497 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3498 $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ];
3499 }
3500 }
3501}
3502
3062sub size_request { 3503sub size_request {
3063 (32, 8) 3504 my ($self) = @_;
3505
3506 if ($::CONN) {
3507 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3508 if (my $tex = $::CONN->{texture}[$faceid]) {
3509 $self->{tex} = $tex;
3510 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3511 } else {
3512 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3513 $self->realloc;
3514 });
3515 }
3516 }
3517 }
3518
3519 ($self->{size_w} || 8, $self->{size_h} || 8)
3064} 3520}
3065 3521
3066sub update { 3522sub update {
3067 my ($self) = @_; 3523 my ($self) = @_;
3068 3524
3069 return unless $self->{visible}; 3525 return unless $self->{visible};
3070 3526
3071 $self->SUPER::update; 3527 $self->SUPER::update;
3072} 3528}
3073 3529
3530sub invoke_visibility_change {
3531 my ($self) = @_;
3532
3533 $self->update_timer;
3534
3535 0
3536}
3537
3074sub _draw { 3538sub _draw {
3075 my ($self) = @_; 3539 my ($self) = @_;
3076 3540
3077 return unless $::CONN; 3541 $self->SUPER::_draw;
3078 3542
3079 my $face; 3543 if (my $tex = $self->{tex}) {
3080
3081 if ($self->{frame}) {
3082 my $anim = $::CONN->{anim}[$self->{anim}];
3083
3084 $face = $anim->[ $self->{frame} % @$anim ]
3085 if $anim && @$anim;
3086 }
3087
3088 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
3089
3090 if ($tex) {
3091 glEnable GL_TEXTURE_2D; 3544 glEnable GL_TEXTURE_2D;
3092 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3545 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3093 glColor 0, 0, 0, 1; 3546 glColor 0, 0, 0, 1;
3094 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3547 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3095 glDisable GL_TEXTURE_2D; 3548 glDisable GL_TEXTURE_2D;
3097} 3550}
3098 3551
3099sub destroy { 3552sub destroy {
3100 my ($self) = @_; 3553 my ($self) = @_;
3101 3554
3102 $self->{timer}->cancel 3555 (delete $self->{timer})->cancel
3103 if $self->{timer}; 3556 if $self->{timer};
3104 3557
3105 $self->SUPER::destroy; 3558 $self->SUPER::destroy;
3106} 3559}
3107 3560
3108############################################################################# 3561#############################################################################
3109 3562
3110package CFClient::UI::Buttonbar; 3563package CFPlus::UI::Buttonbar;
3111 3564
3112our @ISA = CFClient::UI::HBox::; 3565our @ISA = CFPlus::UI::HBox::;
3113 3566
3114# TODO: should actualyl wrap buttons and other goodies. 3567# TODO: should actually wrap buttons and other goodies.
3115 3568
3116############################################################################# 3569#############################################################################
3117 3570
3118package CFClient::UI::Menu; 3571package CFPlus::UI::Menu;
3119 3572
3120our @ISA = CFClient::UI::FancyFrame::; 3573our @ISA = CFPlus::UI::Toplevel::;
3121 3574
3122use CFClient::OpenGL; 3575use CFPlus::OpenGL;
3123 3576
3124sub new { 3577sub new {
3125 my $class = shift; 3578 my $class = shift;
3126 3579
3127 my $self = $class->SUPER::new ( 3580 my $self = $class->SUPER::new (
3128 items => [], 3581 items => [],
3129 z => 100, 3582 z => 100,
3130 @_, 3583 @_,
3131 ); 3584 );
3132 3585
3133 $self->add ($self->{vbox} = new CFClient::UI::VBox); 3586 $self->add ($self->{vbox} = new CFPlus::UI::VBox);
3134 3587
3135 for my $item (@{ $self->{items} }) { 3588 for my $item (@{ $self->{items} }) {
3136 my ($widget, $cb, $tooltip) = @$item; 3589 my ($widget, $cb, $tooltip) = @$item;
3137 3590
3138 # handle various types of items, only text for now 3591 # handle various types of items, only text for now
3139 if (!ref $widget) { 3592 if (!ref $widget) {
3140 if ($widget =~ /\t/) { 3593 if ($widget =~ /\t/) {
3141 my ($left, $right) = split /\t/, $widget, 2; 3594 my ($left, $right) = split /\t/, $widget, 2;
3142 3595
3143 $widget = new CFClient::UI::HBox 3596 $widget = new CFPlus::UI::HBox
3144 can_hover => 1, 3597 can_hover => 1,
3145 can_events => 1, 3598 can_events => 1,
3146 tooltip => $tooltip, 3599 tooltip => $tooltip,
3147 children => [ 3600 children => [
3148 (new CFClient::UI::Label markup => $left, expand => 1), 3601 (new CFPlus::UI::Label markup => $left, expand => 1),
3149 (new CFClient::UI::Label markup => $right, align => +1), 3602 (new CFPlus::UI::Label markup => $right, align => +1),
3150 ], 3603 ],
3151 ; 3604 ;
3152 3605
3153 } else { 3606 } else {
3154 $widget = new CFClient::UI::Label 3607 $widget = new CFPlus::UI::Label
3155 can_hover => 1, 3608 can_hover => 1,
3156 can_events => 1, 3609 can_events => 1,
3157 markup => $widget, 3610 markup => $widget,
3158 tooltip => $tooltip; 3611 tooltip => $tooltip;
3159 } 3612 }
3207 1 3660 1
3208} 3661}
3209 3662
3210############################################################################# 3663#############################################################################
3211 3664
3212package CFClient::UI::Multiplexer; 3665package CFPlus::UI::Multiplexer;
3213 3666
3214our @ISA = CFClient::UI::Container::; 3667our @ISA = CFPlus::UI::Container::;
3215 3668
3216sub new { 3669sub new {
3217 my $class = shift; 3670 my $class = shift;
3218 3671
3219 my $self = $class->SUPER::new ( 3672 my $self = $class->SUPER::new (
3280 $self->{current}->draw; 3733 $self->{current}->draw;
3281} 3734}
3282 3735
3283############################################################################# 3736#############################################################################
3284 3737
3285package CFClient::UI::Notebook; 3738package CFPlus::UI::Notebook;
3286 3739
3287our @ISA = CFClient::UI::VBox::; 3740our @ISA = CFPlus::UI::VBox::;
3288 3741
3289sub new { 3742sub new {
3290 my $class = shift; 3743 my $class = shift;
3291 3744
3292 my $self = $class->SUPER::new ( 3745 my $self = $class->SUPER::new (
3293 buttonbar => (new CFClient::UI::Buttonbar), 3746 buttonbar => (new CFPlus::UI::Buttonbar),
3294 multiplexer => (new CFClient::UI::Multiplexer expand => 1), 3747 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3295 # filter => # will be put between multiplexer and $self 3748 # filter => # will be put between multiplexer and $self
3296 @_, 3749 @_,
3297 ); 3750 );
3298 3751
3299 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3752 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3300 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3753 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3301 3754
3755 {
3756 Scalar::Util::weaken (my $wself = $self);
3757
3758 $self->{multiplexer}->connect (c_add => sub {
3759 my ($mplex, $widgets) = @_;
3760
3761 for my $child (@$widgets) {
3762 Scalar::Util::weaken $child;
3763 $child->{c_tab_} ||= do {
3764 my $tab =
3765 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
3766 ? $child->{c_tab}
3767 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3768
3769 $tab->connect (activate => sub {
3770 $wself->set_current_page ($child);
3771 });
3772
3773 $tab
3774 };
3775
3776 $self->{buttonbar}->add ($child->{c_tab_});
3777 }
3778 });
3779
3780 $self->{multiplexer}->connect (c_remove => sub {
3781 my ($mplex, $widgets) = @_;
3782
3783 for my $child (@$widgets) {
3784 $wself->{buttonbar}->remove ($child->{c_tab_});
3785 }
3786 });
3787 }
3788
3302 $self 3789 $self
3303} 3790}
3304 3791
3305sub add { 3792sub add {
3793 my ($self, @widgets) = @_;
3794
3795 $self->{multiplexer}->add (@widgets)
3796}
3797
3798sub remove {
3799 my ($self, @widgets) = @_;
3800
3801 $self->{multiplexer}->remove (@widgets)
3802}
3803
3804sub add_tab {
3306 my ($self, $title, $widget, $tooltip) = @_; 3805 my ($self, $title, $widget, $tooltip) = @_;
3307 3806
3308 Scalar::Util::weaken $self; 3807 $title = [$title, $tooltip] unless ref $title;
3808 $widget->{c_tab} = $title;
3309 3809
3310 $self->{buttonbar}->add (new CFClient::UI::Button
3311 markup => $title,
3312 tooltip => $tooltip,
3313 on_activate => sub { $self->set_current_page ($widget) },
3314 );
3315
3316 $self->{multiplexer}->add ($widget); 3810 $self->add ($widget);
3317} 3811}
3318 3812
3319sub get_current_page { 3813sub get_current_page {
3320 my ($self) = @_; 3814 my ($self) = @_;
3321 3815
3329 $self->emit (page_changed => $self->{multiplexer}{current}); 3823 $self->emit (page_changed => $self->{multiplexer}{current});
3330} 3824}
3331 3825
3332############################################################################# 3826#############################################################################
3333 3827
3334package CFClient::UI::Combobox; 3828package CFPlus::UI::Selector;
3335 3829
3336use utf8; 3830use utf8;
3337 3831
3338our @ISA = CFClient::UI::Button::; 3832our @ISA = CFPlus::UI::Button::;
3339 3833
3340sub new { 3834sub new {
3341 my $class = shift; 3835 my $class = shift;
3342 3836
3343 my $self = $class->SUPER::new ( 3837 my $self = $class->SUPER::new (
3360 my ($value, $title, $tooltip) = @$_; 3854 my ($value, $title, $tooltip) = @$_;
3361 3855
3362 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 3856 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3363 } 3857 }
3364 3858
3365 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 3859 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);
3366} 3860}
3367 3861
3368sub _set_value { 3862sub _set_value {
3369 my ($self, $value) = @_; 3863 my ($self, $value) = @_;
3370 3864
3385 $self->emit (changed => $value); 3879 $self->emit (changed => $value);
3386} 3880}
3387 3881
3388############################################################################# 3882#############################################################################
3389 3883
3390package CFClient::UI::Statusbox; 3884package CFPlus::UI::Statusbox;
3391 3885
3392our @ISA = CFClient::UI::VBox::; 3886our @ISA = CFPlus::UI::VBox::;
3393 3887
3394sub new { 3888sub new {
3395 my $class = shift; 3889 my $class = shift;
3396 3890
3397 my $self = $class->SUPER::new ( 3891 my $self = $class->SUPER::new (
3398 fontsize => 0.8, 3892 fontsize => 0.8,
3399 @_, 3893 @_,
3400 ); 3894 );
3401 3895
3402 Scalar::Util::weaken (my $this = $self); 3896 CFPlus::weaken (my $this = $self);
3403 3897
3404 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 3898 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3405 3899
3406 $self 3900 $self
3407} 3901}
3409sub reorder { 3903sub reorder {
3410 my ($self) = @_; 3904 my ($self) = @_;
3411 my $NOW = Time::HiRes::time; 3905 my $NOW = Time::HiRes::time;
3412 3906
3413 # freeze display when hovering over any label 3907 # freeze display when hovering over any label
3414 return if $CFClient::UI::TOOLTIP->{owner} 3908 return if $CFPlus::UI::TOOLTIP->{owner}
3415 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label}, 3909 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label},
3416 values %{ $self->{item} }; 3910 values %{ $self->{item} };
3417 3911
3418 while (my ($k, $v) = each %{ $self->{item} }) { 3912 while (my ($k, $v) = each %{ $self->{item} }) {
3419 delete $self->{item}{$k} if $v->{timeout} < $NOW; 3913 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3420 } 3914 }
3441 for ($short) { 3935 for ($short) {
3442 s/^\s+//; 3936 s/^\s+//;
3443 s/\s+/ /g; 3937 s/\s+/ /g;
3444 } 3938 }
3445 3939
3446 new CFClient::UI::Label 3940 new CFPlus::UI::Label
3447 markup => $short, 3941 markup => $short,
3448 tooltip => $item->{tooltip}, 3942 tooltip => $item->{tooltip},
3449 tooltip_font => $::FONT_PROP, 3943 tooltip_font => $::FONT_PROP,
3450 tooltip_width => 0.67, 3944 tooltip_width => 0.67,
3451 fontsize => $item->{fontsize} || $self->{fontsize}, 3945 fontsize => $item->{fontsize} || $self->{fontsize},
3530 $self->SUPER::destroy; 4024 $self->SUPER::destroy;
3531} 4025}
3532 4026
3533############################################################################# 4027#############################################################################
3534 4028
3535package CFClient::UI::Inventory;
3536
3537our @ISA = CFClient::UI::Table::;
3538
3539sub new {
3540 my $class = shift;
3541
3542 my $self = $class->SUPER::new (
3543 col_expand => [0, 1, 0],
3544 @_,
3545 );
3546
3547 $self
3548}
3549
3550sub set_items {
3551 my ($self, $items) = @_;
3552
3553 $self->clear;
3554 return unless $items;
3555
3556 my @items = sort {
3557 ($a->{type} <=> $b->{type})
3558 or ($a->{name} cmp $b->{name})
3559 } values %$items;
3560
3561 $self->{real_items} = \@items;
3562
3563 my $row = 0;
3564 for my $item (@items) {
3565 CFClient::Item::update_widgets $item;
3566
3567 $self->add (0, $row, $item->{face_widget});
3568 $self->add (1, $row, $item->{desc_widget});
3569 $self->add (2, $row, $item->{weight_widget});
3570
3571 $row++;
3572 }
3573}
3574
3575#############################################################################
3576
3577package CFClient::UI::SpellList;
3578
3579our @ISA = CFClient::UI::Table::;
3580
3581sub new {
3582 my $class = shift;
3583
3584 my $self = $class->SUPER::new (
3585 binding => [],
3586 commands => [],
3587 @_,
3588 )
3589}
3590
3591my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>";
3592
3593my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3594 "<b>Name</b>. The name of the spell.$TOOLTIP_ALL");
3595my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip =>
3596 "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL");
3597my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3598 "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL");
3599my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3600 "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL");
3601my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip =>
3602 "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL");
3603
3604sub rebuild_spell_list {
3605 my ($self) = @_;
3606
3607 $CFClient::UI::ROOT->on_refresh ($self => sub {
3608 $self->clear;
3609
3610 return unless $::CONN;
3611
3612 $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME);
3613 $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL);
3614 $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL);
3615 $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP);
3616 $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG);
3617
3618 my $row = 0;
3619
3620 for (sort { $a cmp $b } keys %{ $self->{spell} }) {
3621 my $spell = $self->{spell}{$_};
3622
3623 $row++;
3624
3625 my $spell_cb = sub {
3626 my ($widget, $ev) = @_;
3627
3628 if ($ev->{button} == 1) {
3629 $::CONN->user_send ("cast $spell->{name}");
3630 } elsif ($ev->{button} == 2) {
3631 $::CONN->user_send ("invoke $spell->{name}");
3632 } elsif ($ev->{button} == 3) {
3633 (new CFClient::UI::Menu
3634 items => [
3635 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3636 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3637 ],
3638 )->popup ($ev);
3639 } else {
3640 return 0;
3641 }
3642
3643 1
3644 };
3645
3646 my $tooltip = "$spell->{message}$TOOLTIP_ALL";
3647
3648 #TODO: add path info to tooltip
3649 #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path});
3650
3651 $self->add (0, $row, new CFClient::UI::Face
3652 face => $spell->{face},
3653 can_hover => 1,
3654 can_events => 1,
3655 tooltip => $tooltip,
3656 on_button_down => $spell_cb,
3657 );
3658
3659 $self->add (1, $row, new CFClient::UI::Label
3660 expand => 1,
3661 text => $spell->{name},
3662 can_hover => 1,
3663 can_events => 1,
3664 tooltip => $tooltip,
3665 on_button_down => $spell_cb,
3666 );
3667
3668 $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL);
3669 $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL);
3670 $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP);
3671 $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG);
3672 }
3673 });
3674}
3675
3676sub add_spell {
3677 my ($self, $spell) = @_;
3678
3679 $self->{spell}->{$spell->{name}} = $spell;
3680 $self->rebuild_spell_list;
3681}
3682
3683sub remove_spell {
3684 my ($self, $spell) = @_;
3685
3686 delete $self->{spell}->{$spell->{name}};
3687 $self->rebuild_spell_list;
3688}
3689
3690sub clear_spells {
3691 my ($self) = @_;
3692
3693 $self->{spell} = {};
3694 $self->rebuild_spell_list;
3695}
3696
3697#############################################################################
3698
3699package CFClient::UI::Root; 4029package CFPlus::UI::Root;
3700 4030
3701our @ISA = CFClient::UI::Container::; 4031our @ISA = CFPlus::UI::Container::;
3702 4032
3703use List::Util qw(min max); 4033use List::Util qw(min max);
3704 4034
3705use CFClient::OpenGL; 4035use CFPlus::OpenGL;
3706 4036
3707sub new { 4037sub new {
3708 my $class = shift; 4038 my $class = shift;
3709 4039
3710 my $self = $class->SUPER::new ( 4040 my $self = $class->SUPER::new (
3711 visible => 1, 4041 visible => 1,
3712 @_, 4042 @_,
3713 ); 4043 );
3714 4044
3715 Scalar::Util::weaken ($self->{root} = $self); 4045 CFPlus::weaken ($self->{root} = $self);
3716 4046
3717 $self 4047 $self
3718} 4048}
3719 4049
3720sub size_request { 4050sub size_request {
3815 while ($self->{refresh_hook}) { 4145 while ($self->{refresh_hook}) {
3816 $_->() 4146 $_->()
3817 for values %{delete $self->{refresh_hook}}; 4147 for values %{delete $self->{refresh_hook}};
3818 } 4148 }
3819 4149
3820 if ($self->{realloc}) { 4150 while ($self->{realloc}) {
3821 my %queue; 4151 my %queue;
3822 my @queue; 4152 my @queue;
3823 my $widget; 4153 my $widget;
3824 4154
3825 outer: 4155 outer:
3872 } 4202 }
3873 } 4203 }
3874 4204
3875 delete $self->{realloc}{$widget+0}; 4205 delete $self->{realloc}{$widget+0};
3876 } 4206 }
3877 }
3878 4207
3879 while (my $size_alloc = delete $self->{size_alloc}) { 4208 while (my $size_alloc = delete $self->{size_alloc}) {
3880 my @queue = sort { $b->{visible} <=> $a->{visible} } 4209 my @queue = sort { $a->{visible} <=> $b->{visible} }
3881 values %$size_alloc; 4210 values %$size_alloc;
3882 4211
3883 while () { 4212 while () {
3884 my $widget = pop @queue || last; 4213 my $widget = pop @queue || last;
3885 4214
3886 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4215 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3887 4216
3888 $w = 0 if $w < 0;
3889 $h = 0 if $h < 0;
3890
3891 $w = max $widget->{min_w}, $w; 4217 $w = max $widget->{min_w}, $w;
3892 $h = max $widget->{min_h}, $h; 4218 $h = max $widget->{min_h}, $h;
3893 4219
4220# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4221# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4222
3894 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4223 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3895 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4224 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3896 4225
3897 $w = int $w + 0.5; 4226 $w = int $w + 0.5;
3898 $h = int $h + 0.5; 4227 $h = int $h + 0.5;
3899 4228
3900 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4229 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3901 $widget->{old_w} = $widget->{w}; 4230 $widget->{old_w} = $widget->{w};
3902 $widget->{old_h} = $widget->{h}; 4231 $widget->{old_h} = $widget->{h};
3903 4232
3904 $widget->{w} = $w; 4233 $widget->{w} = $w;
3905 $widget->{h} = $h; 4234 $widget->{h} = $h;
3906 4235
3907 $widget->emit (size_allocate => $w, $h); 4236 $widget->emit (size_allocate => $w, $h);
4237 }
3908 } 4238 }
3909 } 4239 }
3910 } 4240 }
3911 4241
3912 while ($self->{post_alloc_hook}) { 4242 while ($self->{post_alloc_hook}) {
3913 $_->() 4243 $_->()
3914 for values %{delete $self->{post_alloc_hook}}; 4244 for values %{delete $self->{post_alloc_hook}};
3915 } 4245 }
3916
3917 4246
3918 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4247 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3919 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4248 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3920 glClear GL_COLOR_BUFFER_BIT; 4249 glClear GL_COLOR_BUFFER_BIT;
3921 4250
3924 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4253 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3925 glMatrixMode GL_MODELVIEW; 4254 glMatrixMode GL_MODELVIEW;
3926 glLoadIdentity; 4255 glLoadIdentity;
3927 4256
3928 { 4257 {
3929 package CFClient::UI::Base; 4258 package CFPlus::UI::Base;
3930 4259
3931 ($draw_x, $draw_y, $draw_w, $draw_h) = 4260 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3932 (0, 0, $self->{w}, $self->{h}); 4261 (0, 0, $self->{w}, $self->{h});
3933 }
3934 4262
3935 $self->_draw; 4263 $self->_draw;
4264 }
3936} 4265}
3937 4266
3938############################################################################# 4267#############################################################################
3939 4268
3940package CFClient::UI; 4269package CFPlus::UI;
3941 4270
3942$ROOT = new CFClient::UI::Root; 4271$ROOT = new CFPlus::UI::Root;
3943$TOOLTIP = new CFClient::UI::Tooltip z => 900; 4272$TOOLTIP = new CFPlus::UI::Tooltip z => 900;
3944 4273
39451 42741
3946 4275

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines