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.426 by root, Sat Aug 11 12:07:54 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 { __w_ => $_[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 }
959}
960
961sub set_center {
962 my ($self, $x, $y) = @_;
963
964 $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5);
965}
966
967sub make_visible {
968 my ($self, $x, $y, $border) = @_;
969
970 if ( $x < $self->{view_x} + $self->{w} * $border
971 || $x > $self->{view_x} + $self->{w} * (1 - $border)
972 || $y < $self->{view_y} + $self->{h} * $border
973 || $y > $self->{view_y} + $self->{h} * (1 - $border)
974 ) {
975 $self->set_center ($x, $y);
976 }
958} 977}
959 978
960# hmm, this does not work for topleft of $self... but we should not ask for that 979# hmm, this does not work for topleft of $self... but we should not ask for that
961sub coord2local { 980sub coord2local {
962 my ($self, $x, $y) = @_; 981 my ($self, $x, $y) = @_;
977 my ($self, $x, $y) = @_; 996 my ($self, $x, $y) = @_;
978 997
979 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 998 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
980 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 999 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
981 ) { 1000 ) {
982 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1001 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
983 } else { 1002 } else {
984 $self->CFClient::UI::Base::find_widget ($x, $y) 1003 $self->CFPlus::UI::Base::find_widget ($x, $y)
985 } 1004 }
986} 1005}
987 1006
988sub _render { 1007sub _render {
989 my ($self) = @_; 1008 my ($self) = @_;
990 1009
991 local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x}; 1010 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}; 1011 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y};
993 1012
994 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1013 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
995 1014
996 $self->SUPER::_render; 1015 $self->SUPER::_render;
997} 1016}
998 1017
999############################################################################# 1018#############################################################################
1000 1019
1001package CFClient::UI::ScrolledWindow; 1020package CFPlus::UI::ScrolledWindow;
1002 1021
1003our @ISA = CFClient::UI::HBox::; 1022our @ISA = CFPlus::UI::Table::;
1004 1023
1005sub new { 1024sub new {
1006 my ($class, %arg) = @_; 1025 my ($class, %arg) = @_;
1007 1026
1008 my $child = delete $arg{child}; 1027 my $child = delete $arg{child};
1009 1028
1010 my $self; 1029 my $self;
1011 1030
1012 my $slider = new CFClient::UI::Slider 1031 my $hslider = new CFPlus::UI::Slider
1032 c_col => 0,
1033 c_row => 1,
1034 vertical => 0,
1035 range => [0, 0, 1, 0.01], # HACK fix
1036 on_changed => sub {
1037 $self->{hpos} = $_[1];
1038 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1039 },
1040 ;
1041
1042 my $vslider = new CFPlus::UI::Slider
1043 c_col => 1,
1044 c_row => 0,
1013 vertical => 1, 1045 vertical => 1,
1014 range => [0, 0, 1, 0.01], # HACK fix 1046 range => [0, 0, 1, 0.01], # HACK fix
1015 on_changed => sub { 1047 on_changed => sub {
1016 $self->{vp}->set_offset (0, $_[1]); 1048 $self->{vpos} = $_[1];
1049 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1017 }, 1050 },
1018 ; 1051 ;
1019 1052
1020 $self = $class->SUPER::new ( 1053 $self = $class->SUPER::new (
1021 vp => (new CFClient::UI::ViewPort expand => 1), 1054 scroll_x => 0,
1055 scroll_y => 1,
1022 can_events => 1, 1056 can_events => 1,
1023 slider => $slider, 1057 hslider => $hslider,
1058 vslider => $vslider,
1059 col_expand => [1, 0],
1060 row_expand => [1, 0],
1024 %arg, 1061 %arg,
1025 ); 1062 );
1026 1063
1064 $self->{vp} = new CFPlus::UI::ViewPort
1065 c_col => 0,
1066 c_row => 0,
1067 expand => 1,
1068 scroll_x => $self->{scroll_x},
1069 scroll_y => $self->{scroll_y},
1070 on_changed => sub {
1071 my ($vp, $x, $y) = @_;
1072
1073 $vp->{parent}{hslider}->set_value ($x);
1074 $vp->{parent}{vslider}->set_value ($y);
1075
1076 0
1077 },
1078 on_size_allocate => sub {
1079 my ($vp, $w, $h) = @_;
1080 $vp->{parent}->update_slider;
1081 0
1082 },
1083 ;
1084
1027 $self->SUPER::add ($self->{vp}, $self->{slider}); 1085 $self->SUPER::add ($self->{vp});
1086
1028 $self->add ($child) if $child; 1087 $self->add ($child) if $child;
1029 1088
1030 $self 1089 $self
1031} 1090}
1032
1033#TODO# update range on size_allocate depending on child
1034 1091
1035sub add { 1092sub add {
1036 my ($self, $widget) = @_; 1093 my ($self, $widget) = @_;
1037 1094
1038 $self->{vp}->add ($self->{child} = $widget); 1095 $self->{vp}->add ($self->{child} = $widget);
1039} 1096}
1040 1097
1098sub set_offset { shift->{vp}->set_offset (@_) }
1099sub set_center { shift->{vp}->set_center (@_) }
1100sub make_visible { shift->{vp}->make_visible (@_) }
1101
1102sub update_slider {
1103 my ($self) = @_;
1104
1105 my $child = ($self->{vp} or return)->child;
1106
1107 if ($self->{scroll_x}) {
1108 my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w});
1109 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1110
1111 my $visible = $w1 > $w2;
1112 if ($visible != $self->{hslider_visible}) {
1113 $self->{hslider_visible} = $visible;
1114 $visible ? $self->SUPER::add ($self->{hslider})
1115 : $self->SUPER::remove ($self->{hslider});
1116 }
1117 }
1118
1119 if ($self->{scroll_y}) {
1120 my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h});
1121 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1122
1123 my $visible = $h1 > $h2;
1124 if ($visible != $self->{vslider_visible}) {
1125 $self->{vslider_visible} = $visible;
1126 $visible ? $self->SUPER::add ($self->{vslider})
1127 : $self->SUPER::remove ($self->{vslider});
1128 }
1129 }
1130}
1131
1132sub start_dragging {
1133 my ($self, $ev) = @_;
1134
1135 $self->grab_focus;
1136
1137 my $ox = $self->{vp}{view_x};
1138 my $oy = $self->{vp}{view_y};
1139
1140 $self->{motion} = sub {
1141 my ($ev, $x, $y) = @_;
1142
1143 $ox -= $ev->{xrel};
1144 $oy -= $ev->{yrel};
1145
1146 $self->{vp}->set_offset ($ox, $oy);
1147 };
1148}
1149
1041sub invoke_mouse_wheel { 1150sub invoke_mouse_wheel {
1042 my ($self, $ev) = @_; 1151 my ($self, $ev) = @_;
1043 1152
1044 return 0 unless $ev->{dy}; # only vertical movements
1045
1046 $self->{slider}->emit (mouse_wheel => $ev); 1153 $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible};
1154 $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible};
1047 1155
1048 1 1156 1
1049} 1157}
1050 1158
1051sub update_slider { 1159sub invoke_button_down {
1052 my ($self) = @_; 1160 my ($self, $ev, $x, $y) = @_;
1053 1161
1054 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1162 if ($ev->{button} == 2) {
1055} 1163 $self->start_dragging ($ev);
1164 return 1;
1165 }
1056 1166
1057sub update { 1167 0
1058 my ($self) = @_; 1168}
1059 1169
1060 $self->SUPER::update; 1170sub invoke_button_up {
1171 my ($self, $ev, $x, $y) = @_;
1061 1172
1062 $self->update_slider; 1173 if (delete $self->{motion}) {
1174 return 1;
1175 }
1176
1177 0
1178}
1179
1180sub invoke_mouse_motion {
1181 my ($self, $ev, $x, $y) = @_;
1182
1183 if ($self->{motion}) {
1184 $self->{motion}->($ev, $x, $y);
1185 return 1;
1186 }
1187
1188 0
1063} 1189}
1064 1190
1065sub invoke_size_allocate { 1191sub invoke_size_allocate {
1066 my ($self, $w, $h) = @_; 1192 my ($self, $w, $h) = @_;
1067 1193
1068 $self->update_slider; 1194 $self->update_slider;
1069
1070 $self->SUPER::invoke_size_allocate ($w, $h) 1195 $self->SUPER::invoke_size_allocate ($w, $h)
1071} 1196}
1072 1197
1073############################################################################# 1198#############################################################################
1074 1199
1075package CFClient::UI::Frame; 1200package CFPlus::UI::Frame;
1076 1201
1077our @ISA = CFClient::UI::Bin::; 1202our @ISA = CFPlus::UI::Bin::;
1078 1203
1079use CFClient::OpenGL; 1204use CFPlus::OpenGL;
1080 1205
1081sub new { 1206sub new {
1082 my $class = shift; 1207 my $class = shift;
1083 1208
1084 $class->SUPER::new ( 1209 $class->SUPER::new (
1110 $self->SUPER::_draw; 1235 $self->SUPER::_draw;
1111} 1236}
1112 1237
1113############################################################################# 1238#############################################################################
1114 1239
1115package CFClient::UI::FancyFrame; 1240package CFPlus::UI::FancyFrame;
1116 1241
1117our @ISA = CFClient::UI::Bin::; 1242our @ISA = CFPlus::UI::Bin::;
1118 1243
1119use CFClient::OpenGL; 1244use CFPlus::OpenGL;
1245
1246sub new {
1247 my ($class, %arg) = @_;
1248
1249 if ((exists $arg{label}) && !ref $arg{label}) {
1250 $arg{label} = new CFPlus::UI::Label
1251 align => 1,
1252 valign => 0,
1253 text => $arg{label},
1254 fontsize => ($arg{border} || 0.8) * 0.75;
1255 }
1256
1257 my $self = $class->SUPER::new (
1258 # label => "",
1259 fg => [0.6, 0.3, 0.1],
1260 border => 0.8,
1261 style => 'single',
1262 %arg,
1263 );
1264
1265 $self
1266}
1267
1268sub add {
1269 my ($self, @widgets) = @_;
1270
1271 $self->SUPER::add (@widgets);
1272 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label};
1273}
1274
1275sub border {
1276 int $_[0]{border} * $::FONTSIZE
1277}
1278
1279sub size_request {
1280 my ($self) = @_;
1281
1282 ($self->{label_w}, undef) = $self->{label}->size_request
1283 if $self->{label};
1284
1285 my ($w, $h) = $self->SUPER::size_request;
1286
1287 (
1288 $w + $self->border * 2,
1289 $h + $self->border * 2,
1290 )
1291}
1292
1293sub invoke_size_allocate {
1294 my ($self, $w, $h) = @_;
1295
1296 my $border = $self->border;
1297
1298 $w -= List::Util::max 0, $border * 2;
1299 $h -= List::Util::max 0, $border * 2;
1300
1301 if (my $label = $self->{label}) {
1302 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1303 $label->{h} = List::Util::min $h, $border;
1304 $label->invoke_size_allocate ($label->{w}, $label->{h});
1305 }
1306
1307 $self->child->configure ($border, $border, $w, $h);
1308
1309 1
1310}
1311
1312sub _draw {
1313 my ($self) = @_;
1314
1315 my $child = $self->{children}[0];
1316
1317 my $border = $self->border;
1318 my ($w, $h) = ($self->{w}, $self->{h});
1319
1320 $child->draw;
1321
1322 glColor @{$self->{fg}};
1323 glBegin GL_LINE_STRIP;
1324 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1325 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1326 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1327 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1328 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1329 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1330 glEnd;
1331
1332 if ($self->{label}) {
1333 glTranslate $border * 2, 0;
1334 $self->{label}->_draw;
1335 }
1336}
1337
1338#############################################################################
1339
1340package CFPlus::UI::Toplevel;
1341
1342our @ISA = CFPlus::UI::Bin::;
1343
1344use CFPlus::OpenGL;
1120 1345
1121my $bg = 1346my $bg =
1122 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png", 1347 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png",
1123 mipmap => 1, wrap => 1; 1348 mipmap => 1, wrap => 1;
1124 1349
1125my @border = 1350my @border =
1126 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1351 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); 1352 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1353
1354my @icon =
1355 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1356 qw(x1_move.png x1_resize.png);
1128 1357
1129sub new { 1358sub new {
1130 my ($class, %arg) = @_; 1359 my ($class, %arg) = @_;
1131 1360
1132 my $self = $class->SUPER::new ( 1361 my $self = $class->SUPER::new (
1137 min_w => 64, 1366 min_w => 64,
1138 min_h => 32, 1367 min_h => 32,
1139 %arg, 1368 %arg,
1140 ); 1369 );
1141 1370
1142 $self->{title_widget} = new CFClient::UI::Label 1371 $self->{title_widget} = new CFPlus::UI::Label
1143 align => 0, 1372 align => 0,
1144 valign => 1, 1373 valign => 1,
1145 text => $self->{title}, 1374 text => $self->{title},
1146 fontsize => $self->{border}, 1375 fontsize => $self->{border},
1147 if exists $self->{title}; 1376 if exists $self->{title};
1148 1377
1149 if ($self->{has_close_button}) { 1378 if ($self->{has_close_button}) {
1150 $self->{close_button} = 1379 $self->{close_button} =
1151 new CFClient::UI::ImageButton 1380 new CFPlus::UI::ImageButton
1152 path => 'x1_close.png', 1381 path => 'x1_close.png',
1153 on_activate => sub { $self->emit ("delete") }; 1382 on_activate => sub { $self->emit ("delete") };
1154 1383
1155 $self->CFClient::UI::Container::add ($self->{close_button}); 1384 $self->CFPlus::UI::Container::add ($self->{close_button});
1156 } 1385 }
1157 1386
1158 $self 1387 $self
1159} 1388}
1160 1389
1161sub add { 1390sub add {
1162 my ($self, @widgets) = @_; 1391 my ($self, @widgets) = @_;
1163 1392
1164 $self->SUPER::add (@widgets); 1393 $self->SUPER::add (@widgets);
1165 $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1394 $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}; 1395 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1167} 1396}
1168 1397
1169sub border { 1398sub border {
1170 int $_[0]{border} * $::FONTSIZE 1399 int $_[0]{border} * $::FONTSIZE
1171} 1400}
1279 $self->{motion}->($ev, $x, $y) if $self->{motion}; 1508 $self->{motion}->($ev, $x, $y) if $self->{motion};
1280 1509
1281 ! ! $self->{motion} 1510 ! ! $self->{motion}
1282} 1511}
1283 1512
1513sub invoke_visibility_change {
1514 my ($self, $visible) = @_;
1515
1516 delete $self->{motion} unless $visible;
1517
1518 0
1519}
1520
1284sub _draw { 1521sub _draw {
1285 my ($self) = @_; 1522 my ($self) = @_;
1286 1523
1287 my $child = $self->{children}[0]; 1524 my $child = $self->{children}[0];
1288 1525
1293 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1530 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1294 1531
1295 my $border = $self->border; 1532 my $border = $self->border;
1296 1533
1297 glColor @{ $self->{border_bg} }; 1534 glColor @{ $self->{border_bg} };
1298 $border[0]->draw_quad_alpha (0, 0, $w, $border); 1535 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1299 $border[1]->draw_quad_alpha (0, $border, $border, $ch); 1536 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1300 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1537 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1301 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); 1538 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1539
1540 # move
1541 my $w2 = ($w - $border) * .5;
1542 my $h2 = ($h - $border) * .5;
1543 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1544 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1545 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1546
1547 # resize
1548 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1549 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1550 unless $self->{has_close_button};
1551 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1302 1553
1303 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1554 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1304 glColor @{ $self->{bg} }; 1555 glColor @{ $self->{bg} };
1305 1556
1306 # TODO: repeat texture not scale 1557 # TODO: repeat texture not scale
1325 if $self->{close_button}; 1576 if $self->{close_button};
1326} 1577}
1327 1578
1328############################################################################# 1579#############################################################################
1329 1580
1330package CFClient::UI::Table; 1581package CFPlus::UI::Table;
1331 1582
1332our @ISA = CFClient::UI::Base::; 1583our @ISA = CFPlus::UI::Container::;
1333 1584
1334use List::Util qw(max sum); 1585use List::Util qw(max sum);
1335 1586
1336use CFClient::OpenGL; 1587use CFPlus::OpenGL;
1337 1588
1338sub new { 1589sub new {
1339 my $class = shift; 1590 my $class = shift;
1340 1591
1341 $class->SUPER::new ( 1592 $class->SUPER::new (
1342 col_expand => [], 1593 col_expand => [],
1594 row_expand => [],
1343 @_, 1595 @_,
1344 ) 1596 )
1345} 1597}
1346 1598
1347sub children {
1348 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1349}
1350
1351sub add { 1599sub add {
1352 my ($self, $x, $y, $child) = @_; 1600 my ($self, @widgets) = @_;
1353 1601
1354 $child->set_parent ($self); 1602 for my $child (@widgets) {
1355 $self->{children}[$y][$x] = $child; 1603 $child->{c_rowspan} ||= 1;
1604 $child->{c_colspan} ||= 1;
1605 }
1356 1606
1357 $self->realloc; 1607 $self->SUPER::add (@widgets);
1358} 1608}
1359 1609
1360sub remove { 1610sub add_at {
1611 my $self = shift;
1612
1613 my @widgets;
1614
1615 while (@_) {
1616 my ($col, $row, $child) = splice @_, 0, 3, ();
1617
1618 $child->{c_row} = $row;
1619 $child->{c_col} = $col;
1620
1621 push @widgets, $child;
1622 }
1623
1624 $self->add (@widgets);
1625}
1626
1627sub get_wh {
1361 my ($self, $child) = @_; 1628 my ($self) = @_;
1362 1629
1363 # TODO: not yet implemented 1630 my (@w, @h);
1364}
1365
1366# TODO: move to container class maybe? send children a signal on removal?
1367sub clear {
1368 my ($self) = @_;
1369 1631
1370 my @children = $self->children; 1632 my @children = $self->children;
1371 delete $self->{children}; 1633
1634 # first pass, columns
1635 for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) {
1636 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1637
1638 my $sw = sum @w[$c .. $c + $cs - 1];
1639
1640 if ($w > $sw) {
1641 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1642 }
1372 1643 }
1373 for (@children) {
1374 delete $_->{parent};
1375 $_->hide;
1376 }
1377 1644
1378 $self->realloc; 1645 # second pass, rows
1379} 1646 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_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)}; 1647 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1394 1648
1395 $w[$x] = max $w[$x], $w; 1649 my $sh = sum @h[$r .. $r + $rs - 1];
1396 $h[$y] = max $h[$y], $h; 1650
1651 if ($h > $sh) {
1652 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1397 } 1653 }
1398 } 1654 }
1399 1655
1400 (\@w, \@h) 1656 (\@w, \@h)
1401} 1657}
1417 my ($ws, $hs) = $self->get_wh; 1673 my ($ws, $hs) = $self->get_wh;
1418 1674
1419 my $req_w = (sum @$ws) || 1; 1675 my $req_w = (sum @$ws) || 1;
1420 my $req_h = (sum @$hs) || 1; 1676 my $req_h = (sum @$hs) || 1;
1421 1677
1422 # TODO: nicer code && do row_expand 1678 # now linearly scale the rows/columns to the allocated size
1423 my @col_expand = @{$self->{col_expand}}; 1679 my @col_expand = @{$self->{col_expand}};
1424 @col_expand = (1) x @$ws unless @col_expand; 1680 @col_expand = (1) x @$ws unless @col_expand;
1425 my $col_expand = (sum @col_expand) || 1; 1681 my $col_expand = (sum @col_expand) || 1;
1426 1682
1427 # linearly scale sizes
1428 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1683 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1429 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1430 1684
1431 CFClient::UI::harmonize $ws; 1685 CFPlus::UI::harmonize $ws;
1686
1687 my @row_expand = @{$self->{row_expand}};
1688 @row_expand = (1) x @$ws unless @row_expand;
1689 my $row_expand = (sum @row_expand) || 1;
1690
1691 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1692
1432 CFClient::UI::harmonize $hs; 1693 CFPlus::UI::harmonize $hs;
1433 1694
1434 my $y; 1695 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1696 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1435 1697
1436 for my $r (0 .. $#{$self->{children}}) { 1698 for my $widget ($self->children) {
1437 my $row = $self->{children}[$r] 1699 my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)};
1438 or next;
1439 1700
1440 my $x = 0; 1701 $widget->configure (
1441 my $row_h = $hs->[$r]; 1702 $x[$c], $y[$r],
1703 $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r],
1442 1704 );
1443 for my $c (0 .. $#$row) { 1705 }
1444 my $col_w = $ws->[$c];
1445 1706
1446 if (my $widget = $row->[$c]) { 1707 1
1447 $widget->configure ($x, $y, $col_w, $row_h); 1708}
1448 }
1449 1709
1450 $x += $col_w; 1710#############################################################################
1711
1712package CFPlus::UI::Fixed;
1713
1714use List::Util qw(min max);
1715
1716our @ISA = CFPlus::UI::Container::;
1717
1718sub _scale($$$) {
1719 my ($rel, $val, $max) = @_;
1720
1721 $rel ? $val * $max : $val
1722}
1723
1724sub size_request {
1725 my ($self) = @_;
1726
1727 my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0);
1728
1729 # determine overall size by querying abs widgets
1730 for my $child ($self->visible_children) {
1731 unless ($child->{c_rel}) {
1732 my $x = $child->{c_x};
1733 my $y = $child->{c_y};
1734
1735 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1736 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1451 } 1737 }
1738 }
1452 1739
1453 $y += $row_h; 1740 my $W = $x2 - $x1;
1741 my $H = $y2 - $y1;
1742
1743 # now layout remaining widgets
1744 for my $child ($self->visible_children) {
1745 if ($child->{c_rel}) {
1746 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1747 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1748
1749 $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w};
1750 $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h};
1751 }
1752 }
1753
1754 my $W = $x2 - $x1;
1755 my $H = $y2 - $y1;
1756
1757 ($W, $H)
1758}
1759
1760sub invoke_size_allocate {
1761 my ($self, $W, $H) = @_;
1762
1763 for my $child ($self->visible_children) {
1764 my $x = _scale $child->{c_rel}, $child->{c_x}, $W;
1765 my $y = _scale $child->{c_rel}, $child->{c_y}, $H;
1766
1767 $x += $child->{c_halign} * $child->{req_w};
1768 $y += $child->{c_valign} * $child->{req_h};
1769
1770 $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h});
1454 } 1771 }
1455 1772
1456 1 1773 1
1457} 1774}
1458 1775
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############################################################################# 1776#############################################################################
1484 1777
1485package CFClient::UI::Box; 1778package CFPlus::UI::Box;
1486 1779
1487our @ISA = CFClient::UI::Container::; 1780our @ISA = CFPlus::UI::Container::;
1488 1781
1489sub size_request { 1782sub size_request {
1490 my ($self) = @_; 1783 my ($self) = @_;
1784
1785 my @children = $self->visible_children;
1491 1786
1492 $self->{vertical} 1787 $self->{vertical}
1493 ? ( 1788 ? (
1494 (List::Util::max map $_->{req_w}, @{$self->{children}}), 1789 (List::Util::max map $_->{req_w}, @children),
1495 (List::Util::sum map $_->{req_h}, @{$self->{children}}), 1790 (List::Util::sum map $_->{req_h}, @children),
1496 ) 1791 )
1497 : ( 1792 : (
1498 (List::Util::sum map $_->{req_w}, @{$self->{children}}), 1793 (List::Util::sum map $_->{req_w}, @children),
1499 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1794 (List::Util::max map $_->{req_h}, @children),
1500 ) 1795 )
1501} 1796}
1502 1797
1503sub invoke_size_allocate { 1798sub invoke_size_allocate {
1504 my ($self, $w, $h) = @_; 1799 my ($self, $w, $h) = @_;
1525 $req[$_] += $space * $children[$_]{expand} 1820 $req[$_] += $space * $children[$_]{expand}
1526 for 0 .. $#children; 1821 for 0 .. $#children;
1527 } 1822 }
1528 } 1823 }
1529 1824
1530 CFClient::UI::harmonize \@req; 1825 CFPlus::UI::harmonize \@req;
1531 1826
1532 my $pos = 0; 1827 my $pos = 0;
1533 for (0 .. $#children) { 1828 for (0 .. $#children) {
1534 my $alloc = $req[$_]; 1829 my $alloc = $req[$_];
1535 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1830 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1540 1 1835 1
1541} 1836}
1542 1837
1543############################################################################# 1838#############################################################################
1544 1839
1545package CFClient::UI::HBox; 1840package CFPlus::UI::HBox;
1546 1841
1547our @ISA = CFClient::UI::Box::; 1842our @ISA = CFPlus::UI::Box::;
1548 1843
1549sub new { 1844sub new {
1550 my $class = shift; 1845 my $class = shift;
1551 1846
1552 $class->SUPER::new ( 1847 $class->SUPER::new (
1555 ) 1850 )
1556} 1851}
1557 1852
1558############################################################################# 1853#############################################################################
1559 1854
1560package CFClient::UI::VBox; 1855package CFPlus::UI::VBox;
1561 1856
1562our @ISA = CFClient::UI::Box::; 1857our @ISA = CFPlus::UI::Box::;
1563 1858
1564sub new { 1859sub new {
1565 my $class = shift; 1860 my $class = shift;
1566 1861
1567 $class->SUPER::new ( 1862 $class->SUPER::new (
1570 ) 1865 )
1571} 1866}
1572 1867
1573############################################################################# 1868#############################################################################
1574 1869
1575package CFClient::UI::Label; 1870package CFPlus::UI::Label;
1576 1871
1577our @ISA = CFClient::UI::DrawBG::; 1872our @ISA = CFPlus::UI::DrawBG::;
1578 1873
1579use CFClient::OpenGL; 1874use CFPlus::OpenGL;
1580 1875
1581sub new { 1876sub new {
1582 my ($class, %arg) = @_; 1877 my ($class, %arg) = @_;
1583 1878
1584 my $self = $class->SUPER::new ( 1879 my $self = $class->SUPER::new (
1587 #active_bg => none 1882 #active_bg => none
1588 #font => default_font 1883 #font => default_font
1589 #text => initial text 1884 #text => initial text
1590 #markup => initial narkup 1885 #markup => initial narkup
1591 #max_w => maximum pixel width 1886 #max_w => maximum pixel width
1887 #style => 0, # render flags
1592 ellipsise => 3, # end 1888 ellipsise => 3, # end
1593 layout => (new CFClient::Layout), 1889 layout => (new CFPlus::Layout),
1594 fontsize => 1, 1890 fontsize => 1,
1595 align => -1, 1891 align => -1,
1596 valign => -1, 1892 valign => -1,
1597 padding_x => 2, 1893 padding_x => 2,
1598 padding_y => 2, 1894 padding_y => 2,
1599 can_events => 0, 1895 can_events => 0,
1600 %arg 1896 %arg
1601 ); 1897 );
1602 1898
1603 if (exists $self->{template}) { 1899 if (exists $self->{template}) {
1604 my $layout = new CFClient::Layout; 1900 my $layout = new CFPlus::Layout;
1605 $layout->set_text (delete $self->{template}); 1901 $layout->set_text (delete $self->{template});
1606 $self->{template} = $layout; 1902 $self->{template} = $layout;
1607 } 1903 }
1608 1904
1609 if (exists $self->{markup}) { 1905 if (exists $self->{markup}) {
1613 } 1909 }
1614 1910
1615 $self 1911 $self
1616} 1912}
1617 1913
1618sub escape($) {
1619 local $_ = $_[0];
1620
1621 s/&/&amp;/g;
1622 s/>/&gt;/g;
1623 s/</&lt;/g;
1624
1625 $_
1626}
1627
1628sub update { 1914sub update {
1629 my ($self) = @_; 1915 my ($self) = @_;
1630 1916
1631 delete $self->{texture}; 1917 delete $self->{texture};
1632 $self->SUPER::update; 1918 $self->SUPER::update;
1635sub realloc { 1921sub realloc {
1636 my ($self) = @_; 1922 my ($self) = @_;
1637 1923
1638 delete $self->{ox}; 1924 delete $self->{ox};
1639 $self->SUPER::realloc; 1925 $self->SUPER::realloc;
1926}
1927
1928sub clear {
1929 my ($self) = @_;
1930
1931 $self->set_text ("");
1640} 1932}
1641 1933
1642sub set_text { 1934sub set_text {
1643 my ($self, $text) = @_; 1935 my ($self, $text) = @_;
1644 1936
1752 : ($self->{w} - $size->[0]) * 0.5); 2044 : ($self->{w} - $size->[0]) * 0.5);
1753 2045
1754 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 2046 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1755 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} 2047 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1756 : ($self->{h} - $size->[1]) * 0.5); 2048 : ($self->{h} - $size->[1]) * 0.5);
2049
2050 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1757 }; 2051 };
1758 2052
1759 my $w = List::Util::min $self->{w} + 4, $size->[0]; 2053# unless ($self->{list}) {
1760 my $h = List::Util::min $self->{h} + 2, $size->[1]; 2054# $self->{list} = CFPlus::OpenGL::glGenList;
1761 2055# CFPlus::OpenGL::glNewList $self->{list};
1762 $self->{layout}->render ($self->{ox}, $self->{oy}); 2056# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2057# CFPlus::OpenGL::glEndList;
2058# }
2059#
2060# CFPlus::OpenGL::glCallList $self->{list};
2061
2062 $self->{layout}->draw;
1763} 2063}
2064
2065#sub destroy {
2066# my ($self) = @_;
2067#
2068# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2069#
2070# $self->SUPER::destroy;
2071#}
1764 2072
1765############################################################################# 2073#############################################################################
1766 2074
1767package CFClient::UI::EntryBase; 2075package CFPlus::UI::EntryBase;
1768 2076
1769our @ISA = CFClient::UI::Label::; 2077our @ISA = CFPlus::UI::Label::;
1770 2078
1771use CFClient::OpenGL; 2079use CFPlus::OpenGL;
1772 2080
1773sub new { 2081sub new {
1774 my $class = shift; 2082 my $class = shift;
1775 2083
1776 $class->SUPER::new ( 2084 $class->SUPER::new (
1777 fg => [1, 1, 1], 2085 fg => [1, 1, 1],
1778 bg => [0, 0, 0, 0.2], 2086 bg => [0, 0, 0, 0.2],
2087 outline => [0.6, 0.3, 0.1],
1779 active_bg => [1, 1, 1, 0.5], 2088 active_bg => [0, 0, 1, .2],
1780 active_fg => [0, 0, 0], 2089 active_fg => [1, 1, 1],
2090 active_outline => [1, 1, 0],
1781 can_hover => 1, 2091 can_hover => 1,
1782 can_focus => 1, 2092 can_focus => 1,
1783 valign => 0, 2093 valign => 0,
1784 can_events => 1, 2094 can_events => 1,
2095 ellipsise => 0,
1785 #text => ... 2096 #text => ...
1786 #hidden => "*", 2097 #hidden => "*",
1787 @_ 2098 @_
1788 ) 2099 )
1789} 2100}
1834 my $sym = $ev->{sym}; 2145 my $sym = $ev->{sym};
1835 my $uni = $ev->{unicode}; 2146 my $uni = $ev->{unicode};
1836 2147
1837 my $text = $self->get_text; 2148 my $text = $self->get_text;
1838 2149
2150 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2151
1839 if ($uni == 8) { 2152 if ($uni == 8) {
1840 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2153 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1841 } elsif ($uni == 127) { 2154 } elsif ($uni == 127) {
1842 substr $text, $self->{cursor}, 1, ""; 2155 substr $text, $self->{cursor}, 1, "";
1843 } elsif ($sym == CFClient::SDLK_LEFT) { 2156 } elsif ($sym == CFPlus::SDLK_LEFT) {
1844 --$self->{cursor} if $self->{cursor}; 2157 --$self->{cursor} if $self->{cursor};
1845 } elsif ($sym == CFClient::SDLK_RIGHT) { 2158 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1846 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2159 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1847 } elsif ($sym == CFClient::SDLK_HOME) { 2160 } elsif ($sym == CFPlus::SDLK_HOME) {
2161 # what a hack
2162 $self->{cursor} =
2163 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2164 ? length $1
2165 : 0;
2166 } elsif ($sym == CFPlus::SDLK_END) {
2167 # uh, again
2168 $self->{cursor} =
2169 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2170 ? $self->{cursor} + length $1
2171 : length $self->{text};
2172 } elsif ($uni == 21) { # ctrl-u
2173 $text = "";
1848 $self->{cursor} = 0; 2174 $self->{cursor} = 0;
1849 } elsif ($sym == CFClient::SDLK_END) {
1850 $self->{cursor} = length $text;
1851 } elsif ($uni == 27) { 2175 } elsif ($uni == 27) {
1852 $self->emit ('escape'); 2176 $self->emit ('escape');
1853 } elsif ($uni) { 2177 } elsif ($uni == 0x0d) {
2178 substr $text, $self->{cursor}++, 0, "\012";
2179 } elsif ($uni >= 0x20) {
1854 substr $text, $self->{cursor}++, 0, chr $uni; 2180 substr $text, $self->{cursor}++, 0, chr $uni;
1855 } else { 2181 } else {
1856 return 0; 2182 return 0;
1857 } 2183 }
1858 2184
1859 $self->_set_text ($text); 2185 $self->_set_text ($text);
1860 2186
1861 $self->realloc; 2187 $self->realloc;
2188 $self->update;
1862 2189
1863 1 2190 1
1864} 2191}
1865 2192
1866sub invoke_focus_in { 2193sub invoke_focus_in {
1925 2252
1926 unless (exists $self->{cur_h}) { 2253 unless (exists $self->{cur_h}) {
1927 my $text = substr $self->{text}, 0, $self->{cursor}; 2254 my $text = substr $self->{text}, 0, $self->{cursor};
1928 utf8::encode $text; 2255 utf8::encode $text;
1929 2256
1930 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 2257 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1931 } 2258 }
1932 2259
1933 glColor @{$self->{fg}}; 2260 glColor_premultiply @{$self->{active_fg}};
1934 glBegin GL_LINES; 2261 glBegin GL_LINES;
1935 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2262 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}; 2263 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1937 glEnd; 2264 glEnd;
1938 }
1939}
1940 2265
2266 glLineWidth 3;
2267 glColor @{$self->{active_outline}};
2268 glBegin GL_LINE_LOOP;
2269 glVertex 0, 0;
2270 glVertex $self->{w} - 1, 0;
2271 glVertex $self->{w} - 1, $self->{h} - 1;
2272 glVertex 0, $self->{h} - 1;
2273 glEnd;
2274 glLineWidth 1;
2275
2276 } else {
2277 glColor @{$self->{outline}};
2278 glTranslate .375, .375;
2279 glBegin GL_LINE_STRIP;
2280 glVertex 0, $self->{h} * .5;
2281 glVertex 0, $self->{h} - 3;
2282 glVertex $self->{w} - 1, $self->{h} - 3;
2283 glVertex $self->{w} - 1, $self->{h} * .5;
2284 glEnd;
2285 }
2286}
2287
2288#############################################################################
2289
1941package CFClient::UI::Entry; 2290package CFPlus::UI::Entry;
1942 2291
1943our @ISA = CFClient::UI::EntryBase::; 2292our @ISA = CFPlus::UI::EntryBase::;
1944 2293
1945use CFClient::OpenGL; 2294use CFPlus::OpenGL;
1946 2295
1947sub invoke_key_down { 2296sub invoke_key_down {
1948 my ($self, $ev) = @_; 2297 my ($self, $ev) = @_;
1949 2298
1950 my $sym = $ev->{sym}; 2299 my $sym = $ev->{sym};
1951 2300
1952 if ($sym == 13) { 2301 if ($ev->{uni} == 0x0d || $sym == 13) {
1953 unshift @{$self->{history}}, 2302 unshift @{$self->{history}},
1954 my $txt = $self->get_text; 2303 my $txt = $self->get_text;
1955 2304
1956 $self->{history_pointer} = -1; 2305 $self->{history_pointer} = -1;
1957 $self->{history_saveback} = ''; 2306 $self->{history_saveback} = '';
1958 $self->emit (activate => $txt); 2307 $self->emit (activate => $txt);
1959 $self->update; 2308 $self->update;
1960 2309
1961 } elsif ($sym == CFClient::SDLK_UP) { 2310 } elsif ($sym == CFPlus::SDLK_UP) {
1962 if ($self->{history_pointer} < 0) { 2311 if ($self->{history_pointer} < 0) {
1963 $self->{history_saveback} = $self->get_text; 2312 $self->{history_saveback} = $self->get_text;
1964 } 2313 }
1965 if (@{$self->{history} || []} > 0) { 2314 if (@{$self->{history} || []} > 0) {
1966 $self->{history_pointer}++; 2315 $self->{history_pointer}++;
1968 $self->{history_pointer} = @{$self->{history} || []} - 1; 2317 $self->{history_pointer} = @{$self->{history} || []} - 1;
1969 } 2318 }
1970 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2319 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1971 } 2320 }
1972 2321
1973 } elsif ($sym == CFClient::SDLK_DOWN) { 2322 } elsif ($sym == CFPlus::SDLK_DOWN) {
1974 $self->{history_pointer}--; 2323 $self->{history_pointer}--;
1975 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2324 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1976 2325
1977 if ($self->{history_pointer} >= 0) { 2326 if ($self->{history_pointer} >= 0) {
1978 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2327 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1987 1 2336 1
1988} 2337}
1989 2338
1990############################################################################# 2339#############################################################################
1991 2340
2341package CFPlus::UI::TextEdit;
2342
2343our @ISA = CFPlus::UI::EntryBase::;
2344
2345use CFPlus::OpenGL;
2346
2347sub move_cursor_ver {
2348 my ($self, $dy) = @_;
2349
2350 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2351
2352 $y += $dy;
2353
2354 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2355 $self->{cursor} = $index;
2356 delete $self->{cur_h};
2357 $self->update;
2358 return;
2359 }
2360}
2361
2362sub invoke_key_down {
2363 my ($self, $ev) = @_;
2364
2365 my $sym = $ev->{sym};
2366
2367 if ($sym == CFPlus::SDLK_UP) {
2368 $self->move_cursor_ver (-1);
2369 } elsif ($sym == CFPlus::SDLK_DOWN) {
2370 $self->move_cursor_ver (+1);
2371 } else {
2372 return $self->SUPER::invoke_key_down ($ev)
2373 }
2374
2375 1
2376}
2377
2378#############################################################################
2379
1992package CFClient::UI::Button; 2380package CFPlus::UI::ButtonBin;
1993 2381
1994our @ISA = CFClient::UI::Label::; 2382our @ISA = CFPlus::UI::Bin::;
1995 2383
1996use CFClient::OpenGL; 2384use CFPlus::OpenGL;
1997 2385
1998my @tex = 2386my @tex =
1999 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2387 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2000 qw(b1_button_active.png); 2388 qw(b1_button_inactive.png b1_button_active.png);
2001 2389
2002sub new { 2390sub new {
2003 my $class = shift; 2391 my $class = shift;
2004 2392
2005 $class->SUPER::new ( 2393 $class->SUPER::new (
2006 padding_x => 4,
2007 padding_y => 4,
2008 fg => [1, 1, 1],
2009 active_fg => [0, 0, 1],
2010 can_hover => 1, 2394 can_hover => 1,
2011 align => 0, 2395 align => 0,
2012 valign => 0, 2396 valign => 0,
2013 can_events => 1, 2397 can_events => 1,
2014 @_ 2398 @_
2026} 2410}
2027 2411
2028sub _draw { 2412sub _draw {
2029 my ($self) = @_; 2413 my ($self) = @_;
2030 2414
2031 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2032
2033 glEnable GL_TEXTURE_2D; 2415 glEnable GL_TEXTURE_2D;
2034 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2416 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2035 glColor 0, 0, 0, 1; 2417 glColor 0, 0, 0, 1;
2036 2418
2419 my $tex = $tex[$GRAB == $self];
2037 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 2420 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2038 2421
2039 glDisable GL_TEXTURE_2D; 2422 glDisable GL_TEXTURE_2D;
2040 2423
2041 $self->SUPER::_draw; 2424 $self->SUPER::_draw;
2042} 2425}
2043 2426
2044############################################################################# 2427#############################################################################
2045 2428
2429package CFPlus::UI::Button;
2430
2431our @ISA = CFPlus::UI::Label::;
2432
2433use CFPlus::OpenGL;
2434
2435my @tex =
2436 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2437 qw(b1_button_inactive.png b1_button_active.png);
2438
2439sub new {
2440 my $class = shift;
2441
2442 $class->SUPER::new (
2443 padding_x => 4,
2444 padding_y => 4,
2445 fg => [1.0, 1.0, 1.0],
2446 active_fg => [0.8, 0.8, 0.8],
2447 can_hover => 1,
2448 align => 0,
2449 valign => 0,
2450 can_events => 1,
2451 @_
2452 )
2453}
2454
2455sub invoke_button_up {
2456 my ($self, $ev, $x, $y) = @_;
2457
2458 $self->emit ("activate")
2459 if $x >= 0 && $x < $self->{w}
2460 && $y >= 0 && $y < $self->{h};
2461
2462 1
2463}
2464
2465sub _draw {
2466 my ($self) = @_;
2467
2468 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2469
2470 glEnable GL_TEXTURE_2D;
2471 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2472 glColor 0, 0, 0, 1;
2473
2474 my $tex = $tex[$GRAB == $self];
2475 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2476
2477 glDisable GL_TEXTURE_2D;
2478
2479 $self->SUPER::_draw;
2480}
2481
2482#############################################################################
2483
2484package CFPlus::UI::CheckBox;
2485
2486our @ISA = CFPlus::UI::DrawBG::;
2487
2488my @tex =
2489 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2490 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2491
2492use CFPlus::OpenGL;
2493
2494sub new {
2495 my $class = shift;
2496
2497 $class->SUPER::new (
2498 padding_x => 2,
2499 padding_y => 2,
2500 fg => [1, 1, 1],
2501 active_fg => [1, 1, 0],
2502 bg => [0, 0, 0, 0.2],
2503 active_bg => [1, 1, 1, 0.5],
2504 state => 0,
2505 can_hover => 1,
2506 @_
2507 )
2508}
2509
2510sub size_request {
2511 my ($self) = @_;
2512
2513 (6) x 2
2514}
2515
2516sub toggle {
2517 my ($self) = @_;
2518
2519 $self->{state} = !$self->{state};
2520 $self->emit (changed => $self->{state});
2521 $self->update;
2522}
2523
2524sub invoke_button_down {
2525 my ($self, $ev, $x, $y) = @_;
2526
2527 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2528 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2529 $self->toggle;
2530 } else {
2531 return 0
2532 }
2533
2534 1
2535}
2536
2537sub _draw {
2538 my ($self) = @_;
2539
2540 $self->SUPER::_draw;
2541
2542 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2543
2544 my ($w, $h) = @$self{qw(w h)};
2545
2546 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2547
2548 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2549
2550 my $tex = $self->{state} ? $tex[1] : $tex[0];
2551
2552 glEnable GL_TEXTURE_2D;
2553 $tex->draw_quad_alpha (0, 0, $s, $s);
2554 glDisable GL_TEXTURE_2D;
2555}
2556
2557#############################################################################
2558
2559package CFPlus::UI::Image;
2560
2561our @ISA = CFPlus::UI::Base::;
2562
2563use CFPlus::OpenGL;
2564
2565our %texture_cache;
2566
2567sub new {
2568 my $class = shift;
2569
2570 my $self = $class->SUPER::new (
2571 can_events => 0,
2572 scale => 1,
2573 @_,
2574 );
2575
2576 $self->{path} || $self->{tex}
2577 or Carp::croak "'path' or 'tex' attributes required";
2578
2579 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2580 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2581
2582 CFPlus::weaken $texture_cache{$self->{path}};
2583
2584 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2585
2586 $self
2587}
2588
2589sub STORABLE_freeze {
2590 my ($self, $cloning) = @_;
2591
2592 $self->{path}
2593 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n";
2594
2595 $self->{path}
2596}
2597
2598sub STORABLE_attach {
2599 my ($self, $cloning, $path) = @_;
2600
2601 $self->new (path => $path)
2602}
2603
2604sub size_request {
2605 my ($self) = @_;
2606
2607 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2608}
2609
2610sub _draw {
2611 my ($self) = @_;
2612
2613 my $tex = $self->{tex};
2614
2615 my ($w, $h) = ($self->{w}, $self->{h});
2616
2617 if ($self->{rot90}) {
2618 glRotate 90, 0, 0, 1;
2619 glTranslate 0, -$self->{w}, 0;
2620
2621 ($w, $h) = ($h, $w);
2622 }
2623
2624 glEnable GL_TEXTURE_2D;
2625 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2626
2627 $tex->draw_quad_alpha (0, 0, $w, $h);
2628
2629 glDisable GL_TEXTURE_2D;
2630}
2631
2632#############################################################################
2633
2046package CFClient::UI::ImageButton; 2634package CFPlus::UI::ImageButton;
2047 2635
2048our @ISA = CFClient::UI::Image::; 2636our @ISA = CFPlus::UI::Image::;
2049 2637
2050use CFClient::OpenGL; 2638use CFPlus::OpenGL;
2051 2639
2052my %textures; 2640my %textures;
2053 2641
2054sub new { 2642sub new {
2055 my $class = shift; 2643 my $class = shift;
2077 1 2665 1
2078} 2666}
2079 2667
2080############################################################################# 2668#############################################################################
2081 2669
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; 2670package CFPlus::UI::VGauge;
2158 2671
2159our @ISA = CFClient::UI::Base::; 2672our @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 2673
2220use List::Util qw(min max); 2674use List::Util qw(min max);
2221 2675
2222use CFClient::OpenGL; 2676use CFPlus::OpenGL;
2223 2677
2224my %tex = ( 2678my %tex = (
2225 food => [ 2679 food => [
2226 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2680 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2227 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2228 ], 2682 ],
2229 grace => [ 2683 grace => [
2230 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2684 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/ 2685 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2232 ], 2686 ],
2233 hp => [ 2687 hp => [
2234 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2688 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2235 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2236 ], 2690 ],
2237 mana => [ 2691 mana => [
2238 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 2692 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/ 2693 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2240 ], 2694 ],
2241); 2695);
2242 2696
2243# eg. VGauge->new (gauge => 'food'), default gauge: food 2697# eg. VGauge->new (gauge => 'food'), default gauge: food
2346 glDisable GL_TEXTURE_2D; 2800 glDisable GL_TEXTURE_2D;
2347} 2801}
2348 2802
2349############################################################################# 2803#############################################################################
2350 2804
2351package CFClient::UI::Gauge; 2805package CFPlus::UI::Gauge;
2352 2806
2353our @ISA = CFClient::UI::VBox::; 2807our @ISA = CFPlus::UI::VBox::;
2354 2808
2355sub new { 2809sub new {
2356 my ($class, %arg) = @_; 2810 my ($class, %arg) = @_;
2357 2811
2358 my $self = $class->SUPER::new ( 2812 my $self = $class->SUPER::new (
2360 can_hover => 1, 2814 can_hover => 1,
2361 can_events => 1, 2815 can_events => 1,
2362 %arg, 2816 %arg,
2363 ); 2817 );
2364 2818
2365 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 2819 $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); 2820 $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"); 2821 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999");
2368 2822
2369 $self 2823 $self
2370} 2824}
2371 2825
2372sub set_fontsize { 2826sub set_fontsize {
2393 $self->{value}->set_text ($val); 2847 $self->{value}->set_text ($val);
2394} 2848}
2395 2849
2396############################################################################# 2850#############################################################################
2397 2851
2398package CFClient::UI::Slider; 2852package CFPlus::UI::Slider;
2399 2853
2400use strict; 2854use strict;
2401 2855
2402use CFClient::OpenGL; 2856use CFPlus::OpenGL;
2403 2857
2404our @ISA = CFClient::UI::DrawBG::; 2858our @ISA = CFPlus::UI::DrawBG::;
2405 2859
2406my @tex = 2860my @tex =
2407 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 2861 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ }
2408 qw(s1_slider.png s1_slider_bg.png); 2862 qw(s1_slider.png s1_slider_bg.png);
2409 2863
2410sub new { 2864sub new {
2411 my $class = shift; 2865 my $class = shift;
2412 2866
2480 2934
2481 $self->SUPER::invoke_button_down ($ev, $x, $y); 2935 $self->SUPER::invoke_button_down ($ev, $x, $y);
2482 2936
2483 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 2937 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2484 2938
2485 $self->invoke_mouse_motion ($ev, $x, $y) 2939 $self->invoke_mouse_motion ($ev, $x, $y);
2940
2941 1
2486} 2942}
2487 2943
2488sub invoke_mouse_motion { 2944sub invoke_mouse_motion {
2489 my ($self, $ev, $x, $y) = @_; 2945 my ($self, $ev, $x, $y) = @_;
2490 2946
2506sub invoke_mouse_wheel { 2962sub invoke_mouse_wheel {
2507 my ($self, $ev) = @_; 2963 my ($self, $ev) = @_;
2508 2964
2509 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2965 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2510 2966
2967 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2968
2511 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.1); 2969 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2512 2970
2513 ! ! $delta 2971 1
2514} 2972}
2515 2973
2516sub update { 2974sub update {
2517 my ($self) = @_; 2975 my ($self) = @_;
2518 2976
2567 glDisable GL_TEXTURE_2D; 3025 glDisable GL_TEXTURE_2D;
2568} 3026}
2569 3027
2570############################################################################# 3028#############################################################################
2571 3029
2572package CFClient::UI::ValSlider; 3030package CFPlus::UI::ValSlider;
2573 3031
2574our @ISA = CFClient::UI::HBox::; 3032our @ISA = CFPlus::UI::HBox::;
2575 3033
2576sub new { 3034sub new {
2577 my ($class, %arg) = @_; 3035 my ($class, %arg) = @_;
2578 3036
2579 my $range = delete $arg{range}; 3037 my $range = delete $arg{range};
2580 3038
2581 my $self = $class->SUPER::new ( 3039 my $self = $class->SUPER::new (
2582 slider => (new CFClient::UI::Slider expand => 1, range => $range), 3040 slider => (new CFPlus::UI::Slider expand => 1, range => $range),
2583 entry => (new CFClient::UI::Label text => "", template => delete $arg{template}), 3041 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}),
2584 to_value => sub { shift }, 3042 to_value => sub { shift },
2585 from_value => sub { shift }, 3043 from_value => sub { shift },
2586 %arg, 3044 %arg,
2587 ); 3045 );
2588 3046
2608sub set_range { shift->{slider}->set_range (@_) } 3066sub set_range { shift->{slider}->set_range (@_) }
2609sub set_value { shift->{slider}->set_value (@_) } 3067sub set_value { shift->{slider}->set_value (@_) }
2610 3068
2611############################################################################# 3069#############################################################################
2612 3070
2613package CFClient::UI::TextScroller; 3071package CFPlus::UI::TextScroller;
2614 3072
2615our @ISA = CFClient::UI::HBox::; 3073our @ISA = CFPlus::UI::HBox::;
2616 3074
2617use CFClient::OpenGL; 3075use CFPlus::OpenGL;
2618 3076
2619sub new { 3077sub new {
2620 my $class = shift; 3078 my $class = shift;
2621 3079
2622 my $self = $class->SUPER::new ( 3080 my $self = $class->SUPER::new (
2624 can_events => 1, 3082 can_events => 1,
2625 indent => 0, 3083 indent => 0,
2626 #font => default_font 3084 #font => default_font
2627 @_, 3085 @_,
2628 3086
2629 layout => (new CFClient::Layout), 3087 layout => (new CFPlus::Layout),
2630 par => [], 3088 par => [],
3089 max_par => 0,
2631 height => 0, 3090 height => 0,
2632 children => [ 3091 children => [
2633 (new CFClient::UI::Empty expand => 1), 3092 (new CFPlus::UI::Empty expand => 1),
2634 (new CFClient::UI::Slider vertical => 1), 3093 (new CFPlus::UI::Slider vertical => 1),
2635 ], 3094 ],
2636 ); 3095 );
2637 3096
2638 $self->{children}[1]->connect (changed => sub { $self->update }); 3097 $self->{children}[1]->connect (changed => sub { $self->update });
2639 3098
2648} 3107}
2649 3108
2650sub size_request { 3109sub size_request {
2651 my ($self) = @_; 3110 my ($self) = @_;
2652 3111
2653 my ($empty, $slider) = @{ $self->{children} }; 3112 my ($empty, $slider) = $self->visible_children;
2654 3113
2655 local $self->{children} = [$empty, $slider]; 3114 local $self->{children} = [$empty, $slider];
2656 $self->SUPER::size_request 3115 $self->SUPER::size_request
2657} 3116}
2658 3117
2716 3175
2717 # todo: base offset on lines or so, not on pixels 3176 # todo: base offset on lines or so, not on pixels
2718 $self->{children}[1]->set_value ($offset); 3177 $self->{children}[1]->set_value ($offset);
2719} 3178}
2720 3179
3180sub current_paragraph {
3181 my ($self) = @_;
3182
3183 $self->{top_paragraph} - 1
3184}
3185
3186sub scroll_to {
3187 my ($self, $para) = @_;
3188
3189 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
3190
3191 $self->{scroll_to} = $para;
3192 $self->update;
3193}
3194
2721sub clear { 3195sub clear {
2722 my ($self) = @_; 3196 my ($self) = @_;
2723 3197
2724 my (undef, undef, @other) = @{ $self->{children} }; 3198 my (undef, undef, @other) = @{ $self->{children} };
2725 $self->remove ($_) for @other; 3199 $self->remove ($_) for @other;
2728 $self->{height} = 0; 3202 $self->{height} = 0;
2729 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 3203 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2730} 3204}
2731 3205
2732sub add_paragraph { 3206sub add_paragraph {
2733 my ($self, $color, $para, $indent) = @_; 3207 my $self = shift;
2734 3208
2735 my ($text, @w) = ref $para ? @$para : $para; 3209 for my $para (@_) {
2736
2737 $para = { 3210 $para = {
3211 fg => [1, 1, 1, 1],
3212 indent => 0,
3213 markup => "",
3214 widget => [],
3215 ref $para ? %$para : (markup => $para),
2738 w => 1e10, 3216 w => 1e10,
2739 wrapped => 1, 3217 wrapped => 1,
2740 fg => $color,
2741 indent => $indent,
2742 markup => $text,
2743 widget => \@w,
2744 }; 3218 };
2745 3219
2746 $self->add (@w) if @w; 3220 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2747 push @{$self->{par}}, $para; 3221 push @{$self->{par}}, $para;
3222 }
3223
3224 if (my $max = $self->{max_par}) {
3225 shift @{$self->{par}} while @{$self->{par}} > $max;
3226 }
2748 3227
2749 $self->{need_reflow}++; 3228 $self->{need_reflow}++;
2750 $self->update; 3229 $self->update;
2751} 3230}
2752 3231
2753sub scroll_to_bottom { 3232sub scroll_to_bottom {
2754 my ($self) = @_; 3233 my ($self) = @_;
2755 3234
2756 $self->{scroll_to_bottom} = 1; 3235 $self->{scroll_to} = $#{$self->{par}};
2757 $self->update; 3236 $self->update;
2758} 3237}
2759 3238
3239sub force_uptodate {
3240 my ($self) = @_;
3241
3242 if (delete $self->{need_reflow}) {
3243 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3244
3245 my $height = 0;
3246
3247 for my $para (@{$self->{par}}) {
3248 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
3249 my $layout = $self->get_layout ($para);
3250 my ($w, $h) = $layout->size;
3251
3252 $para->{w} = $w + $para->{indent};
3253 $para->{h} = $h;
3254 $para->{wrapped} = $layout->has_wrapped;
3255 }
3256
3257 $para->{y} = $height;
3258 $height += $para->{h};
3259 }
3260
3261 $self->{height} = $height;
3262 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
3263
3264 delete $self->{texture};
3265 }
3266
3267 if (my $paridx = delete $self->{scroll_to}) {
3268 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
3269 }
3270}
3271
2760sub update { 3272sub update {
2761 my ($self) = @_; 3273 my ($self) = @_;
2762 3274
2763 $self->SUPER::update; 3275 $self->SUPER::update;
2764 3276
2765 return unless $self->{h} > 0; 3277 return unless $self->{h} > 0;
2766 3278
2767 delete $self->{texture}; 3279 delete $self->{texture};
2768 3280
2769 $ROOT->on_post_alloc ($self => sub { 3281 $ROOT->on_post_alloc ($self => sub {
3282 $self->force_uptodate;
3283
2770 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3284 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2771 3285
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 { 3286 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
2800 glClearColor 0, 0, 0, 0; 3287 glClearColor 0, 0, 0, 0;
2801 glClear GL_COLOR_BUFFER_BIT; 3288 glClear GL_COLOR_BUFFER_BIT;
2802 3289
3290 package CFPlus::UI::Base;
3291 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3292 (0, 0, $self->{w}, $self->{h});
3293
3294 my $top = int $self->{children}[1]{range}[0];
3295
3296 my $paridx = 0;
3297 my $top_paragraph;
2803 my $top = int $self->{children}[1]{range}[0]; 3298 my $top = int $self->{children}[1]{range}[0];
2804 3299
2805 my $y0 = $top; 3300 my $y0 = $top;
2806 my $y1 = $top + $H; 3301 my $y1 = $top + $H;
2807 3302
2808 my $y = 0;
2809
2810 for my $para (@{$self->{par}}) { 3303 for my $para (@{$self->{par}}) {
2811 my $h = $para->{h}; 3304 my $h = $para->{h};
3305 my $y = $para->{y};
2812 3306
2813 if ($y0 < $y + $h && $y < $y1) { 3307 if ($y0 < $y + $h && $y < $y1) {
2814
2815 my $layout = $self->get_layout ($para); 3308 my $layout = $self->get_layout ($para);
2816 3309
2817 $layout->render ($para->{indent}, $y - $y0); 3310 $layout->render ($para->{indent}, $y - $y0);
3311 $layout->draw;
2818 3312
2819 if (my @w = @{ $para->{widget} }) { 3313 if (my @w = @{ $para->{widget} }) {
2820 my @s = $layout->get_shapes; 3314 my @s = $layout->get_shapes;
2821 3315
2822 for (@w) { 3316 for (@w) {
2828 $_->draw; 3322 $_->draw;
2829 } 3323 }
2830 } 3324 }
2831 } 3325 }
2832 3326
2833 $y += $h; 3327 $paridx++;
3328 $top_paragraph ||= $paridx if $y >= $top;
2834 } 3329 }
3330
3331 $self->{top_paragraph} = $top_paragraph;
2835 }; 3332 };
2836 }); 3333 });
2837} 3334}
2838 3335
2839sub reconfigure { 3336sub reconfigure {
2857 $self->{children}[1]->draw; 3354 $self->{children}[1]->draw;
2858} 3355}
2859 3356
2860############################################################################# 3357#############################################################################
2861 3358
2862package CFClient::UI::Animator; 3359package CFPlus::UI::Animator;
2863 3360
2864use CFClient::OpenGL; 3361use CFPlus::OpenGL;
2865 3362
2866our @ISA = CFClient::UI::Bin::; 3363our @ISA = CFPlus::UI::Bin::;
2867 3364
2868sub moveto { 3365sub moveto {
2869 my ($self, $x, $y) = @_; 3366 my ($self, $x, $y) = @_;
2870 3367
2871 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3368 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
2899 glPopMatrix; 3396 glPopMatrix;
2900} 3397}
2901 3398
2902############################################################################# 3399#############################################################################
2903 3400
2904package CFClient::UI::Flopper; 3401package CFPlus::UI::Flopper;
2905 3402
2906our @ISA = CFClient::UI::Button::; 3403our @ISA = CFPlus::UI::Button::;
2907 3404
2908sub new { 3405sub new {
2909 my $class = shift; 3406 my $class = shift;
2910 3407
2911 my $self = $class->SUPER::new ( 3408 my $self = $class->SUPER::new (
2923 $self->{other}->toggle_visibility; 3420 $self->{other}->toggle_visibility;
2924} 3421}
2925 3422
2926############################################################################# 3423#############################################################################
2927 3424
2928package CFClient::UI::Tooltip; 3425package CFPlus::UI::Tooltip;
2929 3426
2930our @ISA = CFClient::UI::Bin::; 3427our @ISA = CFPlus::UI::Bin::;
2931 3428
2932use CFClient::OpenGL; 3429use CFPlus::OpenGL;
2933 3430
2934sub new { 3431sub new {
2935 my $class = shift; 3432 my $class = shift;
2936 3433
2937 $class->SUPER::new ( 3434 $class->SUPER::new (
2940 ) 3437 )
2941} 3438}
2942 3439
2943sub set_tooltip_from { 3440sub set_tooltip_from {
2944 my ($self, $widget) = @_; 3441 my ($self, $widget) = @_;
3442
3443 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3444 if $widget->{tooltip} =~ /^#(.*)$/;
2945 3445
2946 my $tooltip = $widget->{tooltip}; 3446 my $tooltip = $widget->{tooltip};
2947 3447
2948 if ($ENV{CFPLUS_DEBUG} & 2) { 3448 if ($ENV{CFPLUS_DEBUG} & 2) {
2949 $tooltip .= "\n\n" . (ref $widget) . "\n" 3449 $tooltip .= "\n\n" . (ref $widget) . "\n"
2953 } 3453 }
2954 3454
2955 $tooltip =~ s/^\n+//; 3455 $tooltip =~ s/^\n+//;
2956 $tooltip =~ s/\n+$//; 3456 $tooltip =~ s/\n+$//;
2957 3457
2958 $self->add (new CFClient::UI::Label 3458 $self->add (new CFPlus::UI::Label
2959 markup => $tooltip, 3459 markup => $tooltip,
2960 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3460 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2961 fontsize => 0.8, 3461 fontsize => 0.8,
2962 fg => [0, 0, 0, 1], 3462 style => 1, # FLAG_INVERSE
2963 ellipsise => 0, 3463 ellipsise => 0,
2964 font => ($widget->{tooltip_font} || $::FONT_PROP), 3464 font => ($widget->{tooltip_font} || $::FONT_PROP),
2965 ); 3465 );
2966} 3466}
2967 3467
2986 3486
2987 $self->{root}->on_post_alloc ("move_$self" => sub { 3487 $self->{root}->on_post_alloc ("move_$self" => sub {
2988 my $widget = $self->{owner} 3488 my $widget = $self->{owner}
2989 or return; 3489 or return;
2990 3490
3491 if ($widget->{visible}) {
2991 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 3492 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2992 3493
2993 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 3494 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2994 if $x + $self->{w} > $self->{root}{w}; 3495 if $x + $self->{w} > $self->{root}{w};
2995 3496
2996 $self->move_abs ($x, $y); 3497 $self->move_abs ($x, $y);
3498 } else {
3499 $self->hide;
3500 }
2997 }); 3501 });
2998} 3502}
2999 3503
3000sub _draw { 3504sub _draw {
3001 my ($self) = @_; 3505 my ($self) = @_;
3003 glTranslate 0.375, 0.375; 3507 glTranslate 0.375, 0.375;
3004 3508
3005 my ($w, $h) = @$self{qw(w h)}; 3509 my ($w, $h) = @$self{qw(w h)};
3006 3510
3007 glColor 1, 0.8, 0.4; 3511 glColor 1, 0.8, 0.4;
3008 glBegin GL_QUADS; 3512 glRect 0, 0, $w, $h;
3009 glVertex 0 , 0;
3010 glVertex 0 , $h;
3011 glVertex $w, $h;
3012 glVertex $w, 0;
3013 glEnd;
3014 3513
3015 glColor 0, 0, 0; 3514 glColor 0, 0, 0;
3016 glBegin GL_LINE_LOOP; 3515 glBegin GL_LINE_LOOP;
3017 glVertex 0 , 0; 3516 glVertex 0 , 0;
3018 glVertex 0 , $h; 3517 glVertex 0 , $h;
3025 $self->SUPER::_draw; 3524 $self->SUPER::_draw;
3026} 3525}
3027 3526
3028############################################################################# 3527#############################################################################
3029 3528
3030package CFClient::UI::Face; 3529package CFPlus::UI::Face;
3031 3530
3032our @ISA = CFClient::UI::Base::; 3531our @ISA = CFPlus::UI::DrawBG::;
3033 3532
3034use CFClient::OpenGL; 3533use CFPlus::OpenGL;
3035 3534
3036sub new { 3535sub new {
3037 my $class = shift; 3536 my $class = shift;
3038 3537
3039 my $self = $class->SUPER::new ( 3538 my $self = $class->SUPER::new (
3539 size_w => 32,
3540 size_h => 8,
3040 aspect => 1, 3541 aspect => 1,
3041 can_events => 0, 3542 can_events => 0,
3042 @_, 3543 @_,
3043 ); 3544 );
3044 3545
3045 if ($self->{anim} && $self->{animspeed}) { 3546 if ($self->{anim} && $self->{animspeed}) {
3046 Scalar::Util::weaken (my $widget = $self); 3547 CFPlus::weaken (my $widget = $self);
3047 3548
3549 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3550 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed};
3048 $self->{timer} = Event->timer ( 3551 $self->{timer} = Event->timer (
3049 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3050 hard => 1, 3552 parked => 1,
3051 interval => $self->{animspeed},
3052 cb => sub { 3553 cb => sub {
3554 return unless $::CONN && $widget;
3555
3053 ++$widget->{frame}; 3556 ++$widget->{frame};
3557 $widget->update_face;
3054 $widget->update; 3558 $widget->update;
3559
3560 $widget->update_timer;
3055 }, 3561 },
3056 ); 3562 );
3563
3564 $self->update_face;
3565 $self->update_timer;
3057 } 3566 }
3058 3567
3059 $self 3568 $self
3060} 3569}
3061 3570
3571sub update_timer {
3572 my ($self) = @_;
3573
3574 return unless $self->{timer};
3575
3576 if ($self->{visible}) {
3577 $self->{timer}->at (
3578 $self->{anim_start}
3579 + $self->{animspeed}
3580 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3581 );
3582 $self->{timer}->start;
3583 } else {
3584 $self->{timer}->stop;
3585 }
3586}
3587
3588sub update_face {
3589 my ($self) = @_;
3590
3591 return unless $::CONN;
3592
3593 if (my $anim = $::CONN->{anim}[$self->{anim}]) {
3594 if ($anim && @$anim) {
3595 delete $self->{wait_face};
3596 $self->{face} = $anim->[ $self->{frame} % @$anim ];
3597 if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]) {
3598 unless ($tex->{name} || $tex->{loading}) {
3599 $tex->upload (sub { $self->reconfigure });
3600 }
3601 }
3602 }
3603 }
3604}
3605
3062sub size_request { 3606sub size_request {
3063 (32, 8) 3607 my ($self) = @_;
3608
3609 if ($::CONN) {
3610 if (my $faceid = $::CONN->{faceid}[$self->{face}]) {
3611 if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) {
3612 if ($tex->{name}) {
3613 return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h});
3614 } elsif (!$tex->{loading}) {
3615 $tex->upload (sub { $self->reconfigure });
3616 }
3617 } else {
3618 $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub {
3619 $self->reconfigure;
3620 });
3621 }
3622 }
3623 }
3624
3625 ($self->{size_w} || 8, $self->{size_h} || 8)
3064} 3626}
3065 3627
3066sub update { 3628sub update {
3067 my ($self) = @_; 3629 my ($self) = @_;
3068 3630
3069 return unless $self->{visible}; 3631 return unless $self->{visible};
3070 3632
3071 $self->SUPER::update; 3633 $self->SUPER::update;
3072} 3634}
3073 3635
3636sub invoke_visibility_change {
3637 my ($self) = @_;
3638
3639 $self->update_timer;
3640
3641 0
3642}
3643
3074sub _draw { 3644sub _draw {
3075 my ($self) = @_; 3645 my ($self) = @_;
3076 3646
3077 return unless $::CONN; 3647 $self->SUPER::_draw;
3078 3648
3079 my $face; 3649 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; 3650 glEnable GL_TEXTURE_2D;
3092 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 3651 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3093 glColor 0, 0, 0, 1; 3652 glColor 0, 0, 0, 1;
3094 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); 3653 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3095 glDisable GL_TEXTURE_2D; 3654 glDisable GL_TEXTURE_2D;
3097} 3656}
3098 3657
3099sub destroy { 3658sub destroy {
3100 my ($self) = @_; 3659 my ($self) = @_;
3101 3660
3102 $self->{timer}->cancel 3661 (delete $self->{timer})->cancel
3103 if $self->{timer}; 3662 if $self->{timer};
3104 3663
3105 $self->SUPER::destroy; 3664 $self->SUPER::destroy;
3106} 3665}
3107 3666
3108############################################################################# 3667#############################################################################
3109 3668
3110package CFClient::UI::Buttonbar; 3669package CFPlus::UI::Buttonbar;
3111 3670
3112our @ISA = CFClient::UI::HBox::; 3671our @ISA = CFPlus::UI::HBox::;
3113 3672
3114# TODO: should actualyl wrap buttons and other goodies. 3673# TODO: should actually wrap buttons and other goodies.
3115 3674
3116############################################################################# 3675#############################################################################
3117 3676
3118package CFClient::UI::Menu; 3677package CFPlus::UI::Menu;
3119 3678
3120our @ISA = CFClient::UI::FancyFrame::; 3679our @ISA = CFPlus::UI::Toplevel::;
3121 3680
3122use CFClient::OpenGL; 3681use CFPlus::OpenGL;
3123 3682
3124sub new { 3683sub new {
3125 my $class = shift; 3684 my $class = shift;
3126 3685
3127 my $self = $class->SUPER::new ( 3686 my $self = $class->SUPER::new (
3128 items => [], 3687 items => [],
3129 z => 100, 3688 z => 100,
3130 @_, 3689 @_,
3131 ); 3690 );
3132 3691
3133 $self->add ($self->{vbox} = new CFClient::UI::VBox); 3692 $self->add ($self->{vbox} = new CFPlus::UI::VBox);
3134 3693
3135 for my $item (@{ $self->{items} }) { 3694 for my $item (@{ $self->{items} }) {
3136 my ($widget, $cb, $tooltip) = @$item; 3695 my ($widget, $cb, $tooltip) = @$item;
3137 3696
3138 # handle various types of items, only text for now 3697 # handle various types of items, only text for now
3139 if (!ref $widget) { 3698 if (!ref $widget) {
3140 if ($widget =~ /\t/) { 3699 if ($widget =~ /\t/) {
3141 my ($left, $right) = split /\t/, $widget, 2; 3700 my ($left, $right) = split /\t/, $widget, 2;
3142 3701
3143 $widget = new CFClient::UI::HBox 3702 $widget = new CFPlus::UI::HBox
3144 can_hover => 1, 3703 can_hover => 1,
3145 can_events => 1, 3704 can_events => 1,
3146 tooltip => $tooltip, 3705 tooltip => $tooltip,
3147 children => [ 3706 children => [
3148 (new CFClient::UI::Label markup => $left, expand => 1), 3707 (new CFPlus::UI::Label markup => $left, expand => 1),
3149 (new CFClient::UI::Label markup => $right, align => +1), 3708 (new CFPlus::UI::Label markup => $right, align => +1),
3150 ], 3709 ],
3151 ; 3710 ;
3152 3711
3153 } else { 3712 } else {
3154 $widget = new CFClient::UI::Label 3713 $widget = new CFPlus::UI::Label
3155 can_hover => 1, 3714 can_hover => 1,
3156 can_events => 1, 3715 can_events => 1,
3157 markup => $widget, 3716 markup => $widget,
3158 tooltip => $tooltip; 3717 tooltip => $tooltip;
3159 } 3718 }
3207 1 3766 1
3208} 3767}
3209 3768
3210############################################################################# 3769#############################################################################
3211 3770
3212package CFClient::UI::Multiplexer; 3771package CFPlus::UI::Multiplexer;
3213 3772
3214our @ISA = CFClient::UI::Container::; 3773our @ISA = CFPlus::UI::Container::;
3215 3774
3216sub new { 3775sub new {
3217 my $class = shift; 3776 my $class = shift;
3218 3777
3219 my $self = $class->SUPER::new ( 3778 my $self = $class->SUPER::new (
3280 $self->{current}->draw; 3839 $self->{current}->draw;
3281} 3840}
3282 3841
3283############################################################################# 3842#############################################################################
3284 3843
3285package CFClient::UI::Notebook; 3844package CFPlus::UI::Notebook;
3286 3845
3287our @ISA = CFClient::UI::VBox::; 3846our @ISA = CFPlus::UI::VBox::;
3288 3847
3289sub new { 3848sub new {
3290 my $class = shift; 3849 my $class = shift;
3291 3850
3292 my $self = $class->SUPER::new ( 3851 my $self = $class->SUPER::new (
3293 buttonbar => (new CFClient::UI::Buttonbar), 3852 buttonbar => (new CFPlus::UI::Buttonbar),
3294 multiplexer => (new CFClient::UI::Multiplexer expand => 1), 3853 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3295 # filter => # will be put between multiplexer and $self 3854 # filter => # will be put between multiplexer and $self
3296 @_, 3855 @_,
3297 ); 3856 );
3298 3857
3299 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3858 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3300 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); 3859 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3301 3860
3861 {
3862 Scalar::Util::weaken (my $wself = $self);
3863
3864 $self->{multiplexer}->connect (c_add => sub {
3865 my ($mplex, $widgets) = @_;
3866
3867 for my $child (@$widgets) {
3868 Scalar::Util::weaken $child;
3869 $child->{c_tab_} ||= do {
3870 my $tab =
3871 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base")
3872 ? $child->{c_tab}
3873 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3874
3875 $tab->connect (activate => sub {
3876 $wself->set_current_page ($child);
3877 });
3878
3879 $tab
3880 };
3881
3882 $self->{buttonbar}->add ($child->{c_tab_});
3883 }
3884 });
3885
3886 $self->{multiplexer}->connect (c_remove => sub {
3887 my ($mplex, $widgets) = @_;
3888
3889 for my $child (@$widgets) {
3890 $wself->{buttonbar}->remove ($child->{c_tab_});
3891 }
3892 });
3893 }
3894
3302 $self 3895 $self
3303} 3896}
3304 3897
3305sub add { 3898sub add {
3899 my ($self, @widgets) = @_;
3900
3901 $self->{multiplexer}->add (@widgets)
3902}
3903
3904sub remove {
3905 my ($self, @widgets) = @_;
3906
3907 $self->{multiplexer}->remove (@widgets)
3908}
3909
3910sub pages {
3911 my ($self) = @_;
3912 $self->{multiplexer}->children
3913}
3914
3915sub add_tab {
3306 my ($self, $title, $widget, $tooltip) = @_; 3916 my ($self, $title, $widget, $tooltip) = @_;
3307 3917
3308 Scalar::Util::weaken $self; 3918 $title = [$title, $tooltip] unless ref $title;
3919 $widget->{c_tab} = $title;
3309 3920
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); 3921 $self->add ($widget);
3317} 3922}
3318 3923
3319sub get_current_page { 3924sub get_current_page {
3320 my ($self) = @_; 3925 my ($self) = @_;
3321 3926
3329 $self->emit (page_changed => $self->{multiplexer}{current}); 3934 $self->emit (page_changed => $self->{multiplexer}{current});
3330} 3935}
3331 3936
3332############################################################################# 3937#############################################################################
3333 3938
3334package CFClient::UI::Combobox; 3939package CFPlus::UI::Selector;
3335 3940
3336use utf8; 3941use utf8;
3337 3942
3338our @ISA = CFClient::UI::Button::; 3943our @ISA = CFPlus::UI::Button::;
3339 3944
3340sub new { 3945sub new {
3341 my $class = shift; 3946 my $class = shift;
3342 3947
3343 my $self = $class->SUPER::new ( 3948 my $self = $class->SUPER::new (
3360 my ($value, $title, $tooltip) = @$_; 3965 my ($value, $title, $tooltip) = @$_;
3361 3966
3362 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 3967 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3363 } 3968 }
3364 3969
3365 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); 3970 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);
3366} 3971}
3367 3972
3368sub _set_value { 3973sub _set_value {
3369 my ($self, $value) = @_; 3974 my ($self, $value) = @_;
3370 3975
3385 $self->emit (changed => $value); 3990 $self->emit (changed => $value);
3386} 3991}
3387 3992
3388############################################################################# 3993#############################################################################
3389 3994
3390package CFClient::UI::Statusbox; 3995package CFPlus::UI::Statusbox;
3391 3996
3392our @ISA = CFClient::UI::VBox::; 3997our @ISA = CFPlus::UI::VBox::;
3393 3998
3394sub new { 3999sub new {
3395 my $class = shift; 4000 my $class = shift;
3396 4001
3397 my $self = $class->SUPER::new ( 4002 my $self = $class->SUPER::new (
3398 fontsize => 0.8, 4003 fontsize => 0.8,
3399 @_, 4004 @_,
3400 ); 4005 );
3401 4006
3402 Scalar::Util::weaken (my $this = $self); 4007 CFPlus::weaken (my $this = $self);
3403 4008
3404 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4009 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3405 4010
3406 $self 4011 $self
3407} 4012}
3409sub reorder { 4014sub reorder {
3410 my ($self) = @_; 4015 my ($self) = @_;
3411 my $NOW = Time::HiRes::time; 4016 my $NOW = Time::HiRes::time;
3412 4017
3413 # freeze display when hovering over any label 4018 # freeze display when hovering over any label
3414 return if $CFClient::UI::TOOLTIP->{owner} 4019 return if $CFPlus::UI::TOOLTIP->{owner}
3415 && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label}, 4020 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label},
3416 values %{ $self->{item} }; 4021 values %{ $self->{item} };
3417 4022
3418 while (my ($k, $v) = each %{ $self->{item} }) { 4023 while (my ($k, $v) = each %{ $self->{item} }) {
3419 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4024 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3420 } 4025 }
3441 for ($short) { 4046 for ($short) {
3442 s/^\s+//; 4047 s/^\s+//;
3443 s/\s+/ /g; 4048 s/\s+/ /g;
3444 } 4049 }
3445 4050
3446 new CFClient::UI::Label 4051 new CFPlus::UI::Label
3447 markup => $short, 4052 markup => $short,
3448 tooltip => $item->{tooltip}, 4053 tooltip => $item->{tooltip},
3449 tooltip_font => $::FONT_PROP, 4054 tooltip_font => $::FONT_PROP,
3450 tooltip_width => 0.67, 4055 tooltip_width => 0.67,
3451 fontsize => $item->{fontsize} || $self->{fontsize}, 4056 fontsize => $item->{fontsize} || $self->{fontsize},
3530 $self->SUPER::destroy; 4135 $self->SUPER::destroy;
3531} 4136}
3532 4137
3533############################################################################# 4138#############################################################################
3534 4139
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; 4140package CFPlus::UI::Root;
3700 4141
3701our @ISA = CFClient::UI::Container::; 4142our @ISA = CFPlus::UI::Container::;
3702 4143
3703use List::Util qw(min max); 4144use List::Util qw(min max);
3704 4145
3705use CFClient::OpenGL; 4146use CFPlus::OpenGL;
3706 4147
3707sub new { 4148sub new {
3708 my $class = shift; 4149 my $class = shift;
3709 4150
3710 my $self = $class->SUPER::new ( 4151 my $self = $class->SUPER::new (
3711 visible => 1, 4152 visible => 1,
3712 @_, 4153 @_,
3713 ); 4154 );
3714 4155
3715 Scalar::Util::weaken ($self->{root} = $self); 4156 CFPlus::weaken ($self->{root} = $self);
3716 4157
3717 $self 4158 $self
3718} 4159}
3719 4160
3720sub size_request { 4161sub size_request {
3768} 4209}
3769 4210
3770sub update { 4211sub update {
3771 my ($self) = @_; 4212 my ($self) = @_;
3772 4213
3773 $::WANT_REFRESH++; 4214 $::WANT_REFRESH->start;
3774} 4215}
3775 4216
3776sub add { 4217sub add {
3777 my ($self, @children) = @_; 4218 my ($self, @children) = @_;
3778 4219
3815 while ($self->{refresh_hook}) { 4256 while ($self->{refresh_hook}) {
3816 $_->() 4257 $_->()
3817 for values %{delete $self->{refresh_hook}}; 4258 for values %{delete $self->{refresh_hook}};
3818 } 4259 }
3819 4260
3820 if ($self->{realloc}) { 4261 while ($self->{realloc}) {
3821 my %queue; 4262 my %queue;
3822 my @queue; 4263 my @queue;
3823 my $widget; 4264 my $widget;
3824 4265
3825 outer: 4266 outer:
3872 } 4313 }
3873 } 4314 }
3874 4315
3875 delete $self->{realloc}{$widget+0}; 4316 delete $self->{realloc}{$widget+0};
3876 } 4317 }
3877 }
3878 4318
3879 while (my $size_alloc = delete $self->{size_alloc}) { 4319 while (my $size_alloc = delete $self->{size_alloc}) {
3880 my @queue = sort { $b->{visible} <=> $a->{visible} } 4320 my @queue = sort { $a->{visible} <=> $b->{visible} }
3881 values %$size_alloc; 4321 values %$size_alloc;
3882 4322
3883 while () { 4323 while () {
3884 my $widget = pop @queue || last; 4324 my $widget = pop @queue || last;
3885 4325
3886 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 4326 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3887 4327
3888 $w = 0 if $w < 0;
3889 $h = 0 if $h < 0;
3890
3891 $w = max $widget->{min_w}, $w; 4328 $w = max $widget->{min_w}, $w;
3892 $h = max $widget->{min_h}, $h; 4329 $h = max $widget->{min_h}, $h;
3893 4330
4331# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4332# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4333
3894 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4334 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3895 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4335 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3896 4336
3897 $w = int $w + 0.5; 4337 $w = int $w + 0.5;
3898 $h = int $h + 0.5; 4338 $h = int $h + 0.5;
3899 4339
3900 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 4340 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3901 $widget->{old_w} = $widget->{w}; 4341 $widget->{old_w} = $widget->{w};
3902 $widget->{old_h} = $widget->{h}; 4342 $widget->{old_h} = $widget->{h};
3903 4343
3904 $widget->{w} = $w; 4344 $widget->{w} = $w;
3905 $widget->{h} = $h; 4345 $widget->{h} = $h;
3906 4346
3907 $widget->emit (size_allocate => $w, $h); 4347 $widget->emit (size_allocate => $w, $h);
4348 }
3908 } 4349 }
3909 } 4350 }
3910 } 4351 }
3911 4352
3912 while ($self->{post_alloc_hook}) { 4353 while ($self->{post_alloc_hook}) {
3913 $_->() 4354 $_->()
3914 for values %{delete $self->{post_alloc_hook}}; 4355 for values %{delete $self->{post_alloc_hook}};
3915 } 4356 }
3916
3917 4357
3918 glViewport 0, 0, $::WIDTH, $::HEIGHT; 4358 glViewport 0, 0, $::WIDTH, $::HEIGHT;
3919 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 4359 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3920 glClear GL_COLOR_BUFFER_BIT; 4360 glClear GL_COLOR_BUFFER_BIT;
3921 4361
3924 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4364 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3925 glMatrixMode GL_MODELVIEW; 4365 glMatrixMode GL_MODELVIEW;
3926 glLoadIdentity; 4366 glLoadIdentity;
3927 4367
3928 { 4368 {
3929 package CFClient::UI::Base; 4369 package CFPlus::UI::Base;
3930 4370
3931 ($draw_x, $draw_y, $draw_w, $draw_h) = 4371 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3932 (0, 0, $self->{w}, $self->{h}); 4372 (0, 0, $self->{w}, $self->{h});
3933 }
3934 4373
3935 $self->_draw; 4374 $self->_draw;
4375 }
3936} 4376}
3937 4377
3938############################################################################# 4378#############################################################################
3939 4379
3940package CFClient::UI; 4380package CFPlus::UI;
3941 4381
3942$ROOT = new CFClient::UI::Root; 4382$ROOT = new CFPlus::UI::Root;
3943$TOOLTIP = new CFClient::UI::Tooltip z => 900; 4383$TOOLTIP = new CFPlus::UI::Tooltip z => 900;
3944 4384
39451 43851
3946 4386

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines