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.453 by root, Wed Dec 26 20:46:39 2007 UTC vs.
Revision 1.489 by root, Sun Nov 25 14:37:18 2012 UTC

1package dc::UI; 1package DC::UI;
2 2
3use utf8; 3use common::sense;
4use strict;
5 4
6use List::Util (); 5use List::Util ();
7 6
8use dc; 7use AnyEvent ();
8use Guard ();
9
10use DC;
9use dc::Pod; 11use DC::Pod;
10use dc::Texture; 12use DC::Texture;
11 13
12our ($FOCUS, $HOVER, $GRAB); # various widgets 14our ($FOCUS, $HOVER, $GRAB); # various widgets
13 15
14our $LAYOUT; 16our $LAYOUT;
15our $ROOT; 17our $ROOT;
50sub get_layout { 52sub get_layout {
51 my $layout; 53 my $layout;
52 54
53 for (grep { $_->{name} } values %WIDGET) { 55 for (grep { $_->{name} } values %WIDGET) {
54 my $win = $layout->{$_->{name}} = { }; 56 my $win = $layout->{$_->{name}} = { };
55 57
56 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/; 58 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
57 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/; 59 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
58 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w}; 60 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
59 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h}; 61 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
60 62
69 71
70 $LAYOUT = $layout; 72 $LAYOUT = $layout;
71} 73}
72 74
73# class methods for events 75# class methods for events
74sub feed_sdl_key_down_event { 76sub feed_sdl_key_down_event {
75 $FOCUS->emit (key_down => $_[0]) 77 $FOCUS->emit (key_down => $_[0])
76 if $FOCUS; 78 if $FOCUS;
77} 79}
78 80
79sub feed_sdl_key_up_event { 81sub feed_sdl_key_up_event {
111 113
112 if ($GRAB) { 114 if ($GRAB) {
113 if ($ev->{button} == 4 || $ev->{button} == 5) { 115 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel 116 # mousewheel
115 my $delta = $ev->{button} * 2 - 9; 117 my $delta = $ev->{button} * 2 - 9;
116 my $shift = $ev->{mod} & dc::KMOD_SHIFT; 118 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
117 119
118 $ev->{dx} = $shift ? $delta : 0; 120 $ev->{dx} = $shift ? $delta : 0;
119 $ev->{dy} = $shift ? 0 : $delta; 121 $ev->{dy} = $shift ? 0 : $delta;
120 122
121 $GRAB->emit (mouse_wheel => $ev); 123 $GRAB->emit (mouse_wheel => $ev);
189# call when resolution changes etc. 191# call when resolution changes etc.
190sub rescale_widgets { 192sub rescale_widgets {
191 my ($sx, $sy) = @_; 193 my ($sx, $sy) = @_;
192 194
193 for my $widget (values %WIDGET) { 195 for my $widget (values %WIDGET) {
194 if ($widget->{is_toplevel}) { 196 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 197 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
196 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 198 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
197 199
198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 200 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
199 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 201 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
211 reconfigure_widgets; 213 reconfigure_widgets;
212} 214}
213 215
214############################################################################# 216#############################################################################
215 217
216package dc::UI::Event; 218package DC::UI::Event;
217 219
218sub xy { 220sub xy {
219 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 221 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
220} 222}
221 223
222############################################################################# 224#############################################################################
223 225
224package dc::UI::Base; 226package DC::UI::Base;
225 227
226use strict; 228use common::sense;
227 229
228use dc::OpenGL; 230use DC::OpenGL;
229 231
230sub new { 232sub new {
231 my $class = shift; 233 my $class = shift;
232 234
233 my $self = bless { 235 my $self = bless {
238 h => undef, 240 h => undef,
239 can_events => 1, 241 can_events => 1,
240 @_ 242 @_
241 }, $class; 243 }, $class;
242 244
243 dc::weaken ($dc::UI::WIDGET{$self+0} = $self); 245 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
244 246
245 for (keys %$self) { 247 for (keys %$self) {
246 if (/^on_(.*)$/) { 248 if (/^on_(.*)$/) {
247 $self->connect ($1 => delete $self->{$_}); 249 $self->connect ($1 => delete $self->{$_});
248 } 250 }
249 } 251 }
250 252
251 if (my $layout = $dc::UI::LAYOUT->{$self->{name}}) { 253 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
252 $self->{x} = $layout->{x} * $dc::UI::ROOT->{alloc_w} if exists $layout->{x}; 254 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
253 $self->{y} = $layout->{y} * $dc::UI::ROOT->{alloc_h} if exists $layout->{y}; 255 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
254 $self->{force_w} = $layout->{w} * $dc::UI::ROOT->{alloc_w} if exists $layout->{w}; 256 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
255 $self->{force_h} = $layout->{h} * $dc::UI::ROOT->{alloc_h} if exists $layout->{h}; 257 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
256 258
257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 259 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 260 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
259 261
260 $self->show if $layout->{show}; 262 $self->show if $layout->{show};
278sub show { 280sub show {
279 my ($self) = @_; 281 my ($self) = @_;
280 282
281 return if $self->{parent}; 283 return if $self->{parent};
282 284
283 $dc::UI::ROOT->add ($self); 285 $DC::UI::ROOT->add ($self);
284} 286}
285 287
286sub set_visible { 288sub set_visible {
287 my ($self) = @_; 289 my ($self) = @_;
288 290
289 return if $self->{visible}; 291 return if $self->{visible};
292
293 $self->{parent} && $self->{parent}{root}#d#
294 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
290 295
291 $self->{root} = $self->{parent}{root}; 296 $self->{root} = $self->{parent}{root};
292 $self->{visible} = $self->{parent}{visible} + 1; 297 $self->{visible} = $self->{parent}{visible} + 1;
293 298
294 $self->emit (visibility_change => 1); 299 $self->emit (visibility_change => 1);
295 300
296 $self->realloc if !exists $self->{req_w}; 301 $self->realloc if !exists $self->{req_w};
297 302
298 $_->set_visible for $self->children; 303 $_->set_visible for $self->visible_children;
299} 304}
300 305
301sub set_invisible { 306sub set_invisible {
302 my ($self) = @_; 307 my ($self) = @_;
303 308
309 delete $self->{root}; 314 delete $self->{root};
310 315
311 undef $GRAB if $GRAB == $self; 316 undef $GRAB if $GRAB == $self;
312 undef $HOVER if $HOVER == $self; 317 undef $HOVER if $HOVER == $self;
313 318
314 $dc::UI::TOOLTIP_WATCHER->invoke 319 $DC::UI::TOOLTIP_WATCHER->invoke
315 if $TOOLTIP->{owner} == $self; 320 if $TOOLTIP->{owner} == $self;
316 321
317 $self->emit ("focus_out"); 322 $self->emit ("focus_out");
318 $self->emit (visibility_change => 0); 323 $self->emit (visibility_change => 0);
319} 324}
358} 363}
359 364
360sub set_size { 365sub set_size {
361 my ($self, $w, $h) = @_; 366 my ($self, $w, $h) = @_;
362 367
363 $self->{force_w} = $w; 368 $self->{force_w} = List::Util::min $w, ($self->{max_w} || $::WIDTH );
364 $self->{force_h} = $h; 369 $self->{force_h} = List::Util::min $h, ($self->{max_h} || $::HEIGHT);
365 370
366 $self->realloc; 371 $self->realloc;
367} 372}
368 373
369# traverse the widget chain up to find the maximum "physical" size constraints 374## traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh { 375#sub get_max_wh {
371 my ($self) = @_; 376# my ($self) = @_;
372 377#
373 return $self->{parent}->get_max_wh 378# my ($w, $h) = @$self{qw(max_w max_h)};
374 if $self->{parent}; 379#
375 380# if ($w <= 0 || $h <= 0) {
381# my ($mw, $mh) = $self->{parent}
382# ? $self->{parent}->get_max_wh
376 ($::WIDTH, $::HEIGHT) 383# : ($::WIDTH, $::HEIGHT);
377} 384#
385# $w = $mw if $w <= 0;
386# $h = $mh if $h <= 0;
387# }
388#
389# ($w, $h)
390#}
378 391
379sub size_request { 392sub size_request {
380 require Carp; 393 require Carp;
381 Carp::confess "size_request is abstract"; 394 Carp::confess "size_request is abstract";
382} 395}
389 my ($self, $x, $y, $w, $h) = @_; 402 my ($self, $x, $y, $w, $h) = @_;
390 403
391 if ($self->{aspect}) { 404 if ($self->{aspect}) {
392 my ($ow, $oh) = ($w, $h); 405 my ($ow, $oh) = ($w, $h);
393 406
394 $w = List::Util::min $w, dc::ceil $h * $self->{aspect}; 407 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
395 $h = List::Util::min $h, dc::ceil $w / $self->{aspect}; 408 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
396 409
397 # use alignment to adjust x, y 410 # use alignment to adjust x, y
398 411
399 $x += int 0.5 * ($ow - $w); 412 $x += int 0.5 * ($ow - $w);
400 $y += int 0.5 * ($oh - $h); 413 $y += int 0.5 * ($oh - $h);
441 454
442 return if $self->{tooltip} eq $tooltip; 455 return if $self->{tooltip} eq $tooltip;
443 456
444 $self->{tooltip} = $tooltip; 457 $self->{tooltip} = $tooltip;
445 458
446 if ($dc::UI::TOOLTIP->{owner} == $self) { 459 if ($DC::UI::TOOLTIP->{owner} == $self) {
447 delete $dc::UI::TOOLTIP->{owner}; 460 delete $DC::UI::TOOLTIP->{owner};
448 $dc::UI::TOOLTIP_WATCHER->invoke; 461 $DC::UI::TOOLTIP_WATCHER->invoke;
449 } 462 }
450} 463}
451 464
452# translate global coordinates to local coordinate system 465# translate global coordinates to local coordinate system
453sub coord2local { 466sub coord2local {
519sub connect { 532sub connect {
520 my ($self, $signal, $cb) = @_; 533 my ($self, $signal, $cb) = @_;
521 534
522 push @{ $self->{signal_cb}{$signal} }, $cb; 535 push @{ $self->{signal_cb}{$signal} }, $cb;
523 536
524 defined wantarray and dc::guard { 537 defined wantarray and Guard::guard {
525 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 538 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
526 @{ $self->{signal_cb}{$signal} }; 539 @{ $self->{signal_cb}{$signal} };
527 } 540 }
528} 541}
529 542
567# in .xs 580# in .xs
568 581
569sub set_parent { 582sub set_parent {
570 my ($self, $parent) = @_; 583 my ($self, $parent) = @_;
571 584
572 dc::weaken ($self->{parent} = $parent); 585 DC::weaken ($self->{parent} = $parent);
573 $self->set_visible if $parent->{visible}; 586 $self->set_visible if $parent->{visible};
574} 587}
575 588
576sub realloc { 589sub realloc {
577 my ($self) = @_; 590 my ($self) = @_;
615} 628}
616 629
617sub DESTROY { 630sub DESTROY {
618 my ($self) = @_; 631 my ($self) = @_;
619 632
620 return if dc::in_destruct; 633 return if DC::in_destruct;
621 634
622 local $@; 635 local $@;
623 eval { $self->destroy }; 636 eval { $self->destroy };
624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 637 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
625 638
626 delete $WIDGET{$self+0}; 639 delete $WIDGET{$self+0};
627} 640}
628 641
629############################################################################# 642#############################################################################
630 643
631package dc::UI::DrawBG; 644package DC::UI::DrawBG;
632 645
633our @ISA = dc::UI::Base::; 646our @ISA = DC::UI::Base::;
634 647
635use strict; 648use common::sense;
649
636use dc::OpenGL; 650use DC::OpenGL;
637 651
638sub new { 652sub new {
639 my $class = shift; 653 my $class = shift;
640 654
641 $class->SUPER::new ( 655 $class->SUPER::new (
643 #active_bg => [1, 1, 1, 0.5], 657 #active_bg => [1, 1, 1, 0.5],
644 @_ 658 @_
645 ) 659 )
646} 660}
647 661
662sub set_bg {
663 my ($self, $bg) = @_;
664
665 $self->{bg} = $bg;
666 $self->update;
667}
668
648sub _draw { 669sub _draw {
649 my ($self) = @_; 670 my ($self) = @_;
650 671
651 my $color = $FOCUS == $self && $self->{active_bg} 672 my $color = $FOCUS == $self
652 ? $self->{active_bg} 673 ? $self->{active_bg} || $self->{bg}
653 : $self->{bg}; 674 : $self->{bg};
654 675
655 if ($color && (@$color < 4 || $color->[3])) { 676 if ($color && (@$color < 4 || $color->[3])) {
656 my ($w, $h) = @$self{qw(w h)}; 677 my ($w, $h) = @$self{qw(w h)};
657 678
663 } 684 }
664} 685}
665 686
666############################################################################# 687#############################################################################
667 688
668package dc::UI::Empty; 689package DC::UI::Empty;
669 690
670our @ISA = dc::UI::Base::; 691our @ISA = DC::UI::Base::;
671 692
672sub new { 693sub new {
673 my ($class, %arg) = @_; 694 my ($class, %arg) = @_;
674 $class->SUPER::new (can_events => 0, %arg); 695 $class->SUPER::new (can_events => 0, %arg);
675} 696}
682 703
683sub draw { } 704sub draw { }
684 705
685############################################################################# 706#############################################################################
686 707
687package dc::UI::Container; 708package DC::UI::Container;
688 709
689our @ISA = dc::UI::Base::; 710our @ISA = DC::UI::Base::;
690 711
691sub new { 712sub new {
692 my ($class, %arg) = @_; 713 my ($class, %arg) = @_;
693 714
694 my $children = delete $arg{children}; 715 my $children = delete $arg{children};
788 $_->draw for $self->visible_children; 809 $_->draw for $self->visible_children;
789} 810}
790 811
791############################################################################# 812#############################################################################
792 813
793package dc::UI::Bin; 814package DC::UI::Bin;
794 815
795our @ISA = dc::UI::Container::; 816our @ISA = DC::UI::Container::;
796 817
797sub new { 818sub new {
798 my ($class, %arg) = @_; 819 my ($class, %arg) = @_;
799 820
800 my $child = (delete $arg{child}) || new dc::UI::Empty::; 821 my $child = (delete $arg{child}) || new DC::UI::Empty::;
801 822
802 $class->SUPER::new (children => [$child], %arg) 823 $class->SUPER::new (children => [$child], %arg)
803} 824}
804 825
805sub add { 826sub add {
812sub remove { 833sub remove {
813 my ($self, $widget) = @_; 834 my ($self, $widget) = @_;
814 835
815 $self->SUPER::remove ($widget); 836 $self->SUPER::remove ($widget);
816 837
817 $self->{children} = [new dc::UI::Empty] 838 $self->{children} = [new DC::UI::Empty]
818 unless @{$self->{children}}; 839 unless @{$self->{children}};
819} 840}
820 841
821sub child { $_[0]->{children}[0] } 842sub child { $_[0]->{children}[0] }
822 843
833} 854}
834 855
835############################################################################# 856#############################################################################
836# back-buffered drawing area 857# back-buffered drawing area
837 858
838package dc::UI::Window; 859package DC::UI::Window;
839 860
840our @ISA = dc::UI::Bin::; 861our @ISA = DC::UI::Bin::;
841 862
842use dc::OpenGL; 863use DC::OpenGL;
843 864
844sub new { 865sub new {
845 my ($class, %arg) = @_; 866 my ($class, %arg) = @_;
846 867
847 my $self = $class->SUPER::new (%arg); 868 my $self = $class->SUPER::new (%arg);
869} 890}
870 891
871sub render_child { 892sub render_child {
872 my ($self) = @_; 893 my ($self) = @_;
873 894
874 $self->{texture} = new_from_opengl dc::Texture $self->{w}, $self->{h}, sub { 895 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
875 glClearColor 0, 0, 0, 0; 896 glClearColor 0, 0, 0, 0;
876 glClear GL_COLOR_BUFFER_BIT; 897 glClear GL_COLOR_BUFFER_BIT;
877 898
878 { 899 {
879 package dc::UI::Base; 900 package DC::UI::Base;
880 901
881 local ($draw_x, $draw_y, $draw_w, $draw_h) = 902 local ($draw_x, $draw_y, $draw_w, $draw_h) =
882 (0, 0, $self->{w}, $self->{h}); 903 (0, 0, $self->{w}, $self->{h});
883 904
884 $self->_render; 905 $self->_render;
901 glDisable GL_TEXTURE_2D; 922 glDisable GL_TEXTURE_2D;
902} 923}
903 924
904############################################################################# 925#############################################################################
905 926
906package dc::UI::ViewPort; 927package DC::UI::ViewPort;
907 928
908use List::Util qw(min max); 929use List::Util qw(min max);
909 930
910our @ISA = dc::UI::Window::; 931our @ISA = DC::UI::Window::;
911 932
912sub new { 933sub new {
913 my $class = shift; 934 my $class = shift;
914 935
915 $class->SUPER::new ( 936 $class->SUPER::new (
999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1020 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1021 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1001 ) { 1022 ) {
1002 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1023 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1003 } else { 1024 } else {
1004 $self->dc::UI::Base::find_widget ($x, $y) 1025 $self->DC::UI::Base::find_widget ($x, $y)
1005 } 1026 }
1006} 1027}
1007 1028
1008sub _render { 1029sub _render {
1009 my ($self) = @_; 1030 my ($self) = @_;
1010 1031
1011 local $dc::UI::Base::draw_x = $dc::UI::Base::draw_x - $self->{view_x}; 1032 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
1012 local $dc::UI::Base::draw_y = $dc::UI::Base::draw_y - $self->{view_y}; 1033 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1013 1034
1014 dc::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1035 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1015 1036
1016 $self->SUPER::_render; 1037 $self->SUPER::_render;
1017} 1038}
1018 1039
1019############################################################################# 1040#############################################################################
1020 1041
1021package dc::UI::ScrolledWindow; 1042package DC::UI::ScrolledWindow;
1022 1043
1023our @ISA = dc::UI::Table::; 1044our @ISA = DC::UI::Table::;
1024 1045
1025sub new { 1046sub new {
1026 my ($class, %arg) = @_; 1047 my ($class, %arg) = @_;
1027 1048
1028 my $child = delete $arg{child}; 1049 my $child = delete $arg{child};
1029 1050
1030 my $self; 1051 my $self;
1031 1052
1032 my $hslider = new dc::UI::Slider 1053 my $hslider = new DC::UI::Slider
1033 c_col => 0, 1054 c_col => 0,
1034 c_row => 1, 1055 c_row => 1,
1035 vertical => 0, 1056 vertical => 0,
1036 range => [0, 0, 1, 0.01], # HACK fix 1057 range => [0, 0, 1, 0.01], # HACK fix
1037 on_changed => sub { 1058 on_changed => sub {
1038 $self->{hpos} = $_[1]; 1059 $self->{hpos} = $_[1];
1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1060 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1040 }, 1061 },
1041 ; 1062 ;
1042 1063
1043 my $vslider = new dc::UI::Slider 1064 my $vslider = new DC::UI::Slider
1044 c_col => 1, 1065 c_col => 1,
1045 c_row => 0, 1066 c_row => 0,
1046 vertical => 1, 1067 vertical => 1,
1047 range => [0, 0, 1, 0.01], # HACK fix 1068 range => [0, 0, 1, 0.01], # HACK fix
1048 on_changed => sub { 1069 on_changed => sub {
1060 col_expand => [1, 0], 1081 col_expand => [1, 0],
1061 row_expand => [1, 0], 1082 row_expand => [1, 0],
1062 %arg, 1083 %arg,
1063 ); 1084 );
1064 1085
1065 $self->{vp} = new dc::UI::ViewPort 1086 $self->{vp} = new DC::UI::ViewPort
1066 c_col => 0, 1087 c_col => 0,
1067 c_row => 0, 1088 c_row => 0,
1068 expand => 1, 1089 expand => 1,
1069 scroll_x => $self->{scroll_x}, 1090 scroll_x => $self->{scroll_x},
1070 scroll_y => $self->{scroll_y}, 1091 scroll_y => $self->{scroll_y},
1135 1156
1136 $self->grab_focus; 1157 $self->grab_focus;
1137 1158
1138 my $ox = $self->{vp}{view_x}; 1159 my $ox = $self->{vp}{view_x};
1139 my $oy = $self->{vp}{view_y}; 1160 my $oy = $self->{vp}{view_y};
1140 1161
1141 $self->{motion} = sub { 1162 $self->{motion} = sub {
1142 my ($ev, $x, $y) = @_; 1163 my ($ev, $x, $y) = @_;
1143 1164
1144 $ox -= $ev->{xrel}; 1165 $ox -= $ev->{xrel};
1145 $oy -= $ev->{yrel}; 1166 $oy -= $ev->{yrel};
1196 $self->SUPER::invoke_size_allocate ($w, $h) 1217 $self->SUPER::invoke_size_allocate ($w, $h)
1197} 1218}
1198 1219
1199############################################################################# 1220#############################################################################
1200 1221
1201package dc::UI::Frame; 1222package DC::UI::Frame;
1202 1223
1203our @ISA = dc::UI::Bin::; 1224our @ISA = DC::UI::Bin::;
1204 1225
1205use dc::OpenGL; 1226use DC::OpenGL;
1206 1227
1207sub new { 1228sub new {
1208 my $class = shift; 1229 my $class = shift;
1209 1230
1210 $class->SUPER::new ( 1231 $class->SUPER::new (
1229 $self->SUPER::_draw; 1250 $self->SUPER::_draw;
1230} 1251}
1231 1252
1232############################################################################# 1253#############################################################################
1233 1254
1234package dc::UI::FancyFrame; 1255package DC::UI::FancyFrame;
1235 1256
1236our @ISA = dc::UI::Bin::; 1257our @ISA = DC::UI::Bin::;
1237 1258
1238use dc::OpenGL; 1259use DC::OpenGL;
1239 1260
1240sub new { 1261sub new {
1241 my ($class, %arg) = @_; 1262 my ($class, %arg) = @_;
1242 1263
1243 if ((exists $arg{label}) && !ref $arg{label}) { 1264 if ((exists $arg{label}) && !ref $arg{label}) {
1244 $arg{label} = new dc::UI::Label 1265 $arg{label} = new DC::UI::Label
1245 align => 1, 1266 align => 1,
1246 valign => 0, 1267 valign => 0.5,
1247 text => $arg{label}, 1268 text => $arg{label},
1248 fontsize => ($arg{border} || 0.8) * 0.75; 1269 fontsize => ($arg{border} || 0.8) * 0.75;
1249 } 1270 }
1250 1271
1251 my $self = $class->SUPER::new ( 1272 my $self = $class->SUPER::new (
1252 # label => "", 1273 # label => "",
1253 fg => [0.6, 0.3, 0.1], 1274 fg => undef,
1254 border => 0.8, 1275 border => 0.8,
1255 style => 'single', 1276 style => 'single',
1256 %arg, 1277 %arg,
1257 ); 1278 );
1258 1279
1261 1282
1262sub add { 1283sub add {
1263 my ($self, @widgets) = @_; 1284 my ($self, @widgets) = @_;
1264 1285
1265 $self->SUPER::add (@widgets); 1286 $self->SUPER::add (@widgets);
1266 $self->dc::UI::Container::add ($self->{label}) if $self->{label}; 1287 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1267} 1288}
1268 1289
1269sub border { 1290sub border {
1270 int $_[0]{border} * $::FONTSIZE 1291 int $_[0]{border} * $::FONTSIZE
1271} 1292}
1311 my $border = $self->border; 1332 my $border = $self->border;
1312 my ($w, $h) = ($self->{w}, $self->{h}); 1333 my ($w, $h) = ($self->{w}, $self->{h});
1313 1334
1314 $child->draw; 1335 $child->draw;
1315 1336
1316 glColor @{$self->{fg}}; 1337 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1317 glBegin GL_LINE_STRIP; 1338 glBegin GL_LINE_STRIP;
1318 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1339 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1319 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1340 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1320 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1341 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1321 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1342 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1329 } 1350 }
1330} 1351}
1331 1352
1332############################################################################# 1353#############################################################################
1333 1354
1334package dc::UI::Toplevel; 1355package DC::UI::Toplevel;
1335 1356
1336our @ISA = dc::UI::Bin::; 1357our @ISA = DC::UI::Bin::;
1337 1358
1338use dc::OpenGL; 1359use DC::OpenGL;
1339 1360
1340my $bg = 1361my $bg =
1341 new_from_file dc::Texture dc::find_rcfile "d1_bg.png", 1362 new_from_resource DC::Texture "d1_bg.png",
1342 mipmap => 1, wrap => 1; 1363 mipmap => 1, wrap => 1;
1343 1364
1344my @border = 1365my @border =
1345 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 1366 map { new_from_resource DC::Texture $_, mipmap => 1 }
1346 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1367 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1347 1368
1348my @icon = 1369my @icon =
1349 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 1370 map { new_from_resource DC::Texture $_, mipmap => 1 }
1350 qw(x1_move.png x1_resize.png); 1371 qw(x1_move.png x1_resize.png);
1351 1372
1352sub new { 1373sub new {
1353 my ($class, %arg) = @_; 1374 my ($class, %arg) = @_;
1354 1375
1355 my $self = $class->SUPER::new ( 1376 my $self = $class->SUPER::new (
1356 bg => [1, 1, 1, 1], 1377 bg => [1, 1, 1, 1],
1357 border_bg => [1, 1, 1, 1], 1378 border_bg => [1, 1, 1, 1],
1358 border => 0.6, 1379 border => 0.8,
1359 can_events => 1, 1380 can_events => 1,
1360 min_w => 64, 1381 min_w => 64,
1361 min_h => 32, 1382 min_h => 32,
1362 %arg, 1383 %arg,
1363 ); 1384 );
1364 1385
1365 $self->{title_widget} = new dc::UI::Label 1386 $self->{title_widget} = new DC::UI::Label
1366 align => 0, 1387 align => 0.5,
1367 valign => 1, 1388 valign => 1,
1368 text => $self->{title}, 1389 text => $self->{title},
1369 fontsize => $self->{border}, 1390 fontsize => $self->{border},
1370 if exists $self->{title}; 1391 if exists $self->{title};
1371 1392
1372 if ($self->{has_close_button}) { 1393 if ($self->{has_close_button}) {
1373 $self->{close_button} = 1394 $self->{close_button} =
1374 new dc::UI::ImageButton 1395 new DC::UI::ImageButton
1375 path => 'x1_close.png', 1396 path => 'x1_close.png',
1376 on_activate => sub { $self->emit ("delete") }; 1397 on_activate => sub { $self->emit ("delete") };
1377 1398
1378 $self->dc::UI::Container::add ($self->{close_button}); 1399 $self->DC::UI::Container::add ($self->{close_button});
1379 } 1400 }
1380 1401
1381 $self 1402 $self
1382} 1403}
1383 1404
1384sub add { 1405sub add {
1385 my ($self, @widgets) = @_; 1406 my ($self, @widgets) = @_;
1386 1407
1387 $self->SUPER::add (@widgets); 1408 $self->SUPER::add (@widgets);
1388 $self->dc::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1409 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1389 $self->dc::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1410 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1390} 1411}
1391 1412
1392sub border { 1413sub border {
1393 int $_[0]{border} * $::FONTSIZE 1414 int $_[0]{border} * $::FONTSIZE
1394} 1415}
1395 1416
1396sub get_max_wh { 1417#sub get_max_wh {
1397 my ($self) = @_; 1418# my ($self) = @_;
1398 1419#
1399 return ($self->{w}, $self->{h}) 1420# return ($self->{w}, $self->{h})
1400 if $self->{visible} && $self->{w}; 1421# if $self->{visible} && $self->{w};
1401 1422#
1402 $self->SUPER::get_max_wh 1423# $self->SUPER::get_max_wh
1403} 1424#}
1404 1425
1405sub size_request { 1426sub size_request {
1406 my ($self) = @_; 1427 my ($self) = @_;
1407 1428
1408 $self->{title_widget}->size_request 1429 $self->{title_widget}->size_request
1443 1464
1444sub invoke_delete { 1465sub invoke_delete {
1445 my ($self) = @_; 1466 my ($self) = @_;
1446 1467
1447 $self->hide; 1468 $self->hide;
1448 1469
1449 1 1470 1
1450} 1471}
1451 1472
1452sub invoke_button_down { 1473sub invoke_button_down {
1453 my ($self, $ev, $x, $y) = @_; 1474 my ($self, $ev, $x, $y) = @_;
1454 1475
1455 my ($w, $h) = @$self{qw(w h)}; 1476 my ($w, $h) = @$self{qw(w h)};
1456 my $border = $self->border; 1477 my $border = $self->border;
1457 1478
1458 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); 1479 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); # left-right
1459 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); 1480 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); # top-down
1460 1481
1461 if ($lr & $td) { 1482 if ($lr & $td) { # corners
1462 my ($wx, $wy) = ($self->{x}, $self->{y}); 1483 my ($wx, $wy) = ($self->{x}, $self->{y});
1463 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1484 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1464 my ($bw, $bh) = ($self->{w}, $self->{h}); 1485 my ($bw, $bh) = ($self->{w}, $self->{h});
1465 1486
1466 my $mx = $x < $border; 1487 my $mx = $x < $border;
1470 my ($ev, $x, $y) = @_; 1491 my ($ev, $x, $y) = @_;
1471 1492
1472 my $dx = $ev->{x} - $ox; 1493 my $dx = $ev->{x} - $ox;
1473 my $dy = $ev->{y} - $oy; 1494 my $dy = $ev->{y} - $oy;
1474 1495
1496 $self->set_size (
1475 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); 1497 $bw + $dx * ($mx ? -1 : 1),
1476 $self->{force_h} = $bh + $dy * ($my ? -1 : 1); 1498 $bh + $dy * ($my ? -1 : 1),
1499 );
1477 1500
1478 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); 1501 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1479 $self->realloc; 1502 $self->realloc;
1480 }; 1503 };
1481 1504
1482 } elsif ($lr ^ $td) { 1505 } elsif ($lr ^ $td) { # edges
1483 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1506 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1484 my ($bx, $by) = ($self->{x}, $self->{y}); 1507 my ($bx, $by) = ($self->{x}, $self->{y});
1485 1508
1486 $self->{motion} = sub { 1509 $self->{motion} = sub {
1487 my ($ev, $x, $y) = @_; 1510 my ($ev, $x, $y) = @_;
1532 glEnable GL_TEXTURE_2D; 1555 glEnable GL_TEXTURE_2D;
1533 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1556 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1534 1557
1535 my $border = $self->border; 1558 my $border = $self->border;
1536 1559
1560 if ($border) {
1537 glColor @{ $self->{border_bg} }; 1561 glColor @{ $self->{border_bg} };
1538 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1562 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1539 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1563 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1540 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1564 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1541 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1565 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1542 1566
1543 # move 1567 # move
1544 my $w2 = ($w - $border) * .5; 1568 my $w2 = ($w - $border) * .5;
1545 my $h2 = ($h - $border) * .5; 1569 my $h2 = ($h - $border) * .5;
1546 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1570 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1547 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1571 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1548 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1572 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1549 1573
1550 # resize 1574 # resize
1551 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1575 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1576 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1553 unless $self->{has_close_button}; 1577 unless $self->{has_close_button};
1554 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1578 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1555 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1579 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1580 }
1556 1581
1557 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1582 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1558 glColor @{ $self->{bg} }; 1583 glColor @{ $self->{bg} };
1559 1584
1560 # TODO: repeat texture not scale 1585 # TODO: repeat texture not scale
1579 if $self->{close_button}; 1604 if $self->{close_button};
1580} 1605}
1581 1606
1582############################################################################# 1607#############################################################################
1583 1608
1584package dc::UI::Table; 1609package DC::UI::Table;
1585 1610
1586our @ISA = dc::UI::Container::; 1611our @ISA = DC::UI::Container::;
1587 1612
1588use List::Util qw(max sum); 1613use List::Util qw(max sum);
1589 1614
1590use dc::OpenGL; 1615use DC::OpenGL;
1591 1616
1592sub new { 1617sub new {
1593 my $class = shift; 1618 my $class = shift;
1594 1619
1595 $class->SUPER::new ( 1620 $class->SUPER::new (
1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1664 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1640 1665
1641 my $sw = sum @w[$c .. $c + $cs - 1]; 1666 my $sw = sum @w[$c .. $c + $cs - 1];
1642 1667
1643 if ($w > $sw) { 1668 if ($w > $sw) {
1644 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1669 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1645 } 1670 }
1646 } 1671 }
1647 1672
1648 # second pass, rows 1673 # second pass, rows
1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1674 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1675 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1651 1676
1652 my $sh = sum @h[$r .. $r + $rs - 1]; 1677 my $sh = sum @h[$r .. $r + $rs - 1];
1653 1678
1654 if ($h > $sh) { 1679 if ($h > $sh) {
1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1680 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1656 } 1681 }
1657 } 1682 }
1658 1683
1659 (\@w, \@h) 1684 (\@w, \@h)
1660} 1685}
1683 @col_expand = (1) x @$ws unless @col_expand; 1708 @col_expand = (1) x @$ws unless @col_expand;
1684 my $col_expand = (sum @col_expand) || 1; 1709 my $col_expand = (sum @col_expand) || 1;
1685 1710
1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1711 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1687 1712
1688 dc::UI::harmonize $ws; 1713 DC::UI::harmonize $ws;
1689 1714
1690 my @row_expand = @{$self->{row_expand}}; 1715 my @row_expand = @{$self->{row_expand}};
1691 @row_expand = (1) x @$ws unless @row_expand; 1716 @row_expand = (1) x @$ws unless @row_expand;
1692 my $row_expand = (sum @row_expand) || 1; 1717 my $row_expand = (sum @row_expand) || 1;
1693 1718
1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1719 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1695 1720
1696 dc::UI::harmonize $hs; 1721 DC::UI::harmonize $hs;
1697 1722
1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1723 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1724 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1700 1725
1701 for my $widget ($self->children) { 1726 for my $widget ($self->children) {
1710 1 1735 1
1711} 1736}
1712 1737
1713############################################################################# 1738#############################################################################
1714 1739
1715package dc::UI::Fixed; 1740package DC::UI::Fixed;
1716 1741
1717use List::Util qw(min max); 1742use List::Util qw(min max);
1718 1743
1719our @ISA = dc::UI::Container::; 1744our @ISA = DC::UI::Container::;
1720 1745
1721sub _scale($$$) { 1746sub _scale($$$) {
1722 my ($rel, $val, $max) = @_; 1747 my ($rel, $val, $max) = @_;
1723 1748
1724 $rel ? $val * $max : $val 1749 $rel ? $val * $max : $val
1776 1 1801 1
1777} 1802}
1778 1803
1779############################################################################# 1804#############################################################################
1780 1805
1781package dc::UI::Box; 1806package DC::UI::Box;
1782 1807
1783our @ISA = dc::UI::Container::; 1808our @ISA = DC::UI::Container::;
1784 1809
1785sub size_request { 1810sub size_request {
1786 my ($self) = @_; 1811 my ($self) = @_;
1787 1812
1788 my @children = $self->visible_children; 1813 my @children = $self->visible_children;
1815 if ($req > $space) { 1840 if ($req > $space) {
1816 # ah well, not enough space 1841 # ah well, not enough space
1817 $_ *= $space / $req for @req; 1842 $_ *= $space / $req for @req;
1818 } else { 1843 } else {
1819 my $expand = (List::Util::sum map $_->{expand}, @children) || 1; 1844 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1820 1845
1821 $space = ($space - $req) / $expand; # remaining space to give away 1846 $space = ($space - $req) / $expand; # remaining space to give away
1822 1847
1823 $req[$_] += $space * $children[$_]{expand} 1848 $req[$_] += $space * $children[$_]{expand}
1824 for 0 .. $#children; 1849 for 0 .. $#children;
1825 } 1850 }
1826 } 1851 }
1827 1852
1828 dc::UI::harmonize \@req; 1853 DC::UI::harmonize \@req;
1829 1854
1830 my $pos = 0; 1855 my $pos = 0;
1831 for (0 .. $#children) { 1856 for (0 .. $#children) {
1832 my $alloc = $req[$_]; 1857 my $alloc = $req[$_];
1833 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1858 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1838 1 1863 1
1839} 1864}
1840 1865
1841############################################################################# 1866#############################################################################
1842 1867
1843package dc::UI::HBox; 1868package DC::UI::HBox;
1844 1869
1845our @ISA = dc::UI::Box::; 1870our @ISA = DC::UI::Box::;
1846 1871
1847sub new { 1872sub new {
1848 my $class = shift; 1873 my $class = shift;
1849 1874
1850 $class->SUPER::new ( 1875 $class->SUPER::new (
1853 ) 1878 )
1854} 1879}
1855 1880
1856############################################################################# 1881#############################################################################
1857 1882
1858package dc::UI::VBox; 1883package DC::UI::VBox;
1859 1884
1860our @ISA = dc::UI::Box::; 1885our @ISA = DC::UI::Box::;
1861 1886
1862sub new { 1887sub new {
1863 my $class = shift; 1888 my $class = shift;
1864 1889
1865 $class->SUPER::new ( 1890 $class->SUPER::new (
1868 ) 1893 )
1869} 1894}
1870 1895
1871############################################################################# 1896#############################################################################
1872 1897
1873package dc::UI::Label; 1898package DC::UI::Label;
1874 1899
1875our @ISA = dc::UI::DrawBG::; 1900our @ISA = DC::UI::DrawBG::;
1876 1901
1877use dc::OpenGL; 1902use DC::OpenGL;
1878 1903
1879sub new { 1904sub new {
1880 my ($class, %arg) = @_; 1905 my ($class, %arg) = @_;
1881 1906
1882 my $self = $class->SUPER::new ( 1907 my $self = $class->SUPER::new (
1887 #text => initial text 1912 #text => initial text
1888 #markup => initial narkup 1913 #markup => initial narkup
1889 #max_w => maximum pixel width 1914 #max_w => maximum pixel width
1890 #style => 0, # render flags 1915 #style => 0, # render flags
1891 ellipsise => 3, # end 1916 ellipsise => 3, # end
1892 layout => (new dc::Layout), 1917 layout => (new DC::Layout),
1893 fontsize => 1, 1918 fontsize => 1,
1894 align => -1, 1919 align => 0.5,
1895 valign => -1, 1920 valign => 0.5,
1896 padding_x => 2, 1921 padding_x => 4,
1897 padding_y => 2, 1922 padding_y => 2,
1898 can_events => 0, 1923 can_events => 0,
1899 %arg 1924 %arg
1900 ); 1925 );
1901 1926
1902 if (exists $self->{template}) { 1927 if (exists $self->{template}) {
1903 my $layout = new dc::Layout; 1928 my $layout = new DC::Layout;
1904 $layout->set_text (delete $self->{template}); 1929 $layout->set_text (delete $self->{template});
1905 $self->{template} = $layout; 1930 $self->{template} = $layout;
1906 } 1931 }
1907 1932
1908 if (exists $self->{markup}) { 1933 if (exists $self->{markup}) {
1964 1989
1965sub size_request { 1990sub size_request {
1966 my ($self) = @_; 1991 my ($self) = @_;
1967 1992
1968 $self->{size_req} ||= do { 1993 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh; 1994 my $max_w = $self->{w} || $self->{max_w} || $::WIDTH || 0x0fffffff; # actually 2**31-1 but allow for overflow inside pango
1970 1995
1971 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1996 $self->{layout}->set_font ($self->{font}) if $self->{font};
1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1997 $self->{layout}->set_width ($max_w);
1973 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1998 $self->{layout}->set_ellipsise ($self->{ellipsise});
1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1999 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2000 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1976 2001
1977 my ($w, $h) = $self->{layout}->size; 2002 my ($w, $h) = $self->{layout}->size;
1978 2003
1979 if (exists $self->{template}) { 2004 if (exists $self->{template}) {
1980 $self->{template}->set_font ($self->{font}) if $self->{font}; 2005 $self->{template}->set_font ($self->{font}) if $self->{font};
1981 $self->{template}->set_width ($self->{max_w} || -1); 2006 $self->{template}->set_width ($max_w);
1982 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2007 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1983 2008
1984 my ($w2, $h2) = $self->{template}->size; 2009 my ($w2, $h2) = $self->{template}->size;
1985 2010
1986 $w = List::Util::max $w, $w2; 2011 $w = List::Util::max $w, $w2;
2042 2067
2043 [$self->{layout}->size] 2068 [$self->{layout}->size]
2044 }; 2069 };
2045 2070
2046 unless (exists $self->{ox}) { 2071 unless (exists $self->{ox}) {
2047 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2072 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2048 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2073 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2049 : ($self->{w} - $size->[0]) * 0.5);
2050
2051 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2052 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2053 : ($self->{h} - $size->[1]) * 0.5);
2054 2074
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2075 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2056 }; 2076 };
2057 2077
2058# unless ($self->{list}) { 2078# unless ($self->{list}) {
2059# $self->{list} = dc::OpenGL::glGenList; 2079# $self->{list} = DC::OpenGL::glGenList;
2060# dc::OpenGL::glNewList $self->{list}; 2080# DC::OpenGL::glNewList $self->{list};
2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2081# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2062# dc::OpenGL::glEndList; 2082# DC::OpenGL::glEndList;
2063# } 2083# }
2064# 2084#
2065# dc::OpenGL::glCallList $self->{list}; 2085# DC::OpenGL::glCallList $self->{list};
2066 2086
2067 $self->{layout}->draw; 2087 $self->{layout}->draw;
2068} 2088}
2069 2089
2070#sub destroy { 2090#sub destroy {
2071# my ($self) = @_; 2091# my ($self) = @_;
2072# 2092#
2073# dc::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2093# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2074# 2094#
2075# $self->SUPER::destroy; 2095# $self->SUPER::destroy;
2076#} 2096#}
2077 2097
2078############################################################################# 2098#############################################################################
2079 2099
2080package dc::UI::EntryBase; 2100package DC::UI::EntryBase;
2081 2101
2082our @ISA = dc::UI::Label::; 2102our @ISA = DC::UI::Label::;
2083 2103
2084use dc::OpenGL; 2104use DC::OpenGL;
2085 2105
2086sub new { 2106sub new {
2087 my $class = shift; 2107 my $class = shift;
2088 2108
2089 $class->SUPER::new ( 2109 $class->SUPER::new (
2090 fg => [1, 1, 1], 2110 fg => [1, 1, 1],
2091 bg => [0, 0, 0, 0.2], 2111 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1], 2112 outline => undef,
2093 active_bg => [0, 0, 1, .2], 2113 active_bg => [0, 0, 1, .2],
2094 active_fg => [1, 1, 1], 2114 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0], 2115 active_outline => [1, 1, 0],
2096 can_hover => 1, 2116 can_hover => 1,
2097 can_focus => 1, 2117 can_focus => 1,
2118 align => 0,
2098 valign => 0, 2119 valign => 0.5,
2099 can_events => 1, 2120 can_events => 1,
2100 ellipsise => 0, 2121 ellipsise => 0,
2122 padding_x => 4,
2123 padding_y => 2,
2101 #text => ... 2124 #text => ...
2102 #hidden => "*", 2125 #hidden => "*",
2103 @_ 2126 @_
2104 ) 2127 )
2105} 2128}
2152 2175
2153 my $text = $self->get_text; 2176 my $text = $self->get_text;
2154 2177
2155 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2178 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2156 2179
2157 if ($uni == 8) { 2180 if ($sym == DC::SDLK_BACKSPACE) {
2158 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2181 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2159 } elsif ($uni == 127) { 2182 } elsif ($sym == DC::SDLK_DELETE) {
2160 substr $text, $self->{cursor}, 1, ""; 2183 substr $text, $self->{cursor}, 1, "";
2161 } elsif ($sym == dc::SDLK_LEFT) { 2184 } elsif ($sym == DC::SDLK_LEFT) {
2162 --$self->{cursor} if $self->{cursor}; 2185 --$self->{cursor} if $self->{cursor};
2163 } elsif ($sym == dc::SDLK_RIGHT) { 2186 } elsif ($sym == DC::SDLK_RIGHT) {
2164 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2187 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2165 } elsif ($sym == dc::SDLK_HOME) { 2188 } elsif ($sym == DC::SDLK_HOME) {
2166 # what a hack 2189 # what a hack
2167 $self->{cursor} = 2190 $self->{cursor} =
2168 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2191 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2169 ? length $1 2192 ? length $1
2170 : 0; 2193 : 0;
2171 } elsif ($sym == dc::SDLK_END) { 2194 } elsif ($sym == DC::SDLK_END) {
2172 # uh, again 2195 # uh, again
2173 $self->{cursor} = 2196 $self->{cursor} =
2174 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2197 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2175 ? $self->{cursor} + length $1 2198 ? $self->{cursor} + length $1
2176 : length $self->{text}; 2199 : length $self->{text};
2215 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; 2238 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
2216 $self->{cursor} = length $text; 2239 $self->{cursor} = length $text;
2217 2240
2218 $self->_set_text ($self->{text}); 2241 $self->_set_text ($self->{text});
2219 $self->update; 2242 $self->update;
2220 2243
2221 1 2244 1
2222} 2245}
2223 2246
2224sub invoke_mouse_motion { 2247sub invoke_mouse_motion {
2225 my ($self, $ev, $x, $y) = @_; 2248 my ($self, $ev, $x, $y) = @_;
2267 glColor @{$self->{active_outline}}; 2290 glColor @{$self->{active_outline}};
2268 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2291 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2269 glLineWidth 1; 2292 glLineWidth 1;
2270 2293
2271 } else { 2294 } else {
2272 glColor @{$self->{outline}}; 2295 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2273 glBegin GL_LINE_STRIP; 2296 glBegin GL_LINE_STRIP;
2274 glVertex .5, $self->{h} * .5; 2297 glVertex .5, $self->{h} * .5;
2275 glVertex .5, $self->{h} - 2.5; 2298 glVertex .5, $self->{h} - 2.5;
2276 glVertex $self->{w} - .5, $self->{h} - 2.5; 2299 glVertex $self->{w} - .5, $self->{h} - 2.5;
2277 glVertex $self->{w} - .5, $self->{h} * .5; 2300 glVertex $self->{w} - .5, $self->{h} * .5;
2279 } 2302 }
2280} 2303}
2281 2304
2282############################################################################# 2305#############################################################################
2283 2306
2284package dc::UI::Entry; 2307package DC::UI::Entry;
2285 2308
2286our @ISA = dc::UI::EntryBase::; 2309our @ISA = DC::UI::EntryBase::;
2287 2310
2288use dc::OpenGL; 2311use DC::OpenGL;
2312
2313sub new {
2314 my $class = shift;
2315
2316 $class->SUPER::new (
2317 history_pointer => -1,
2318 @_
2319 )
2320}
2321
2289 2322
2290sub invoke_key_down { 2323sub invoke_key_down {
2291 my ($self, $ev) = @_; 2324 my ($self, $ev) = @_;
2292 2325
2293 my $sym = $ev->{sym}; 2326 my $sym = $ev->{sym};
2299 $self->{history_pointer} = -1; 2332 $self->{history_pointer} = -1;
2300 $self->{history_saveback} = ''; 2333 $self->{history_saveback} = '';
2301 $self->emit (activate => $txt); 2334 $self->emit (activate => $txt);
2302 $self->update; 2335 $self->update;
2303 2336
2304 } elsif ($sym == dc::SDLK_UP) { 2337 } elsif ($sym == DC::SDLK_UP) {
2305 if ($self->{history_pointer} < 0) { 2338 if ($self->{history_pointer} < 0) {
2306 $self->{history_saveback} = $self->get_text; 2339 $self->{history_saveback} = $self->get_text;
2307 } 2340 }
2308 if (@{$self->{history} || []} > 0) { 2341 if (@{$self->{history} || []} > 0) {
2309 $self->{history_pointer}++; 2342 $self->{history_pointer}++;
2311 $self->{history_pointer} = @{$self->{history} || []} - 1; 2344 $self->{history_pointer} = @{$self->{history} || []} - 1;
2312 } 2345 }
2313 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2346 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2314 } 2347 }
2315 2348
2316 } elsif ($sym == dc::SDLK_DOWN) { 2349 } elsif ($sym == DC::SDLK_DOWN) {
2317 $self->{history_pointer}--; 2350 $self->{history_pointer}--;
2318 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2351 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2319 2352
2320 if ($self->{history_pointer} >= 0) { 2353 if ($self->{history_pointer} >= 0) {
2321 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2354 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2322 } else { 2355 } else {
2356 if (defined $self->{history_saveback}) {
2323 $self->set_text ($self->{history_saveback}); 2357 $self->set_text ($self->{history_saveback});
2358 $self->{history_saveback} = undef;
2359 }
2324 } 2360 }
2325 2361
2326 } else { 2362 } else {
2327 return $self->SUPER::invoke_key_down ($ev) 2363 return $self->SUPER::invoke_key_down ($ev)
2328 } 2364 }
2330 1 2366 1
2331} 2367}
2332 2368
2333############################################################################# 2369#############################################################################
2334 2370
2335package dc::UI::TextEdit; 2371package DC::UI::TextEdit;
2336 2372
2337our @ISA = dc::UI::EntryBase::; 2373our @ISA = DC::UI::EntryBase::;
2338 2374
2339use dc::OpenGL; 2375use DC::OpenGL;
2376
2377sub new {
2378 my $class = shift;
2379
2380 $class->SUPER::new (
2381 padding_y => 4,
2382
2383 @_
2384 )
2385}
2340 2386
2341sub move_cursor_ver { 2387sub move_cursor_ver {
2342 my ($self, $dy) = @_; 2388 my ($self, $dy) = @_;
2343 2389
2344 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2390 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2345 2391
2346 $y += $dy; 2392 $line += $dy;
2347 2393
2348 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2394 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2349 $self->{cursor} = $index; 2395 $self->{cursor} = $index;
2350 delete $self->{cur_h}; 2396 delete $self->{cur_h};
2351 $self->update; 2397 $self->update;
2352 return; 2398 return;
2353 } 2399 }
2356sub invoke_key_down { 2402sub invoke_key_down {
2357 my ($self, $ev) = @_; 2403 my ($self, $ev) = @_;
2358 2404
2359 my $sym = $ev->{sym}; 2405 my $sym = $ev->{sym};
2360 2406
2361 if ($sym == dc::SDLK_UP) { 2407 if ($sym == DC::SDLK_UP) {
2362 $self->move_cursor_ver (-1); 2408 $self->move_cursor_ver (-1);
2363 } elsif ($sym == dc::SDLK_DOWN) { 2409 } elsif ($sym == DC::SDLK_DOWN) {
2364 $self->move_cursor_ver (+1); 2410 $self->move_cursor_ver (+1);
2365 } else { 2411 } else {
2366 return $self->SUPER::invoke_key_down ($ev) 2412 return $self->SUPER::invoke_key_down ($ev)
2367 } 2413 }
2368 2414
2369 1 2415 1
2370} 2416}
2371 2417
2372############################################################################# 2418#############################################################################
2373 2419
2374package dc::UI::ButtonBin; 2420package DC::UI::ButtonBin;
2375 2421
2376our @ISA = dc::UI::Bin::; 2422our @ISA = DC::UI::Bin::;
2377 2423
2378use dc::OpenGL; 2424use DC::OpenGL;
2379 2425
2380my @tex = 2426my @tex =
2381 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2427 map { new_from_resource DC::Texture $_, mipmap => 1 }
2382 qw(b1_button_inactive.png b1_button_active.png); 2428 qw(b1_button_inactive.png b1_button_active.png);
2383 2429
2384sub new { 2430sub new {
2385 my $class = shift; 2431 my $class = shift;
2386 2432
2387 $class->SUPER::new ( 2433 $class->SUPER::new (
2388 can_hover => 1, 2434 can_hover => 1,
2389 align => 0, 2435 align => 0.5,
2390 valign => 0, 2436 valign => 0.5,
2391 can_events => 1, 2437 can_events => 1,
2392 @_ 2438 @_
2393 ) 2439 )
2394} 2440}
2395 2441
2418 $self->SUPER::_draw; 2464 $self->SUPER::_draw;
2419} 2465}
2420 2466
2421############################################################################# 2467#############################################################################
2422 2468
2423package dc::UI::Button; 2469package DC::UI::Button;
2424 2470
2425our @ISA = dc::UI::Label::; 2471our @ISA = DC::UI::Label::;
2426 2472
2427use dc::OpenGL; 2473use DC::OpenGL;
2428 2474
2429my @tex = 2475my @tex =
2430 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2476 map { new_from_resource DC::Texture $_, mipmap => 1 }
2431 qw(b1_button_inactive.png b1_button_active.png); 2477 qw(b1_button_inactive.png b1_button_active.png);
2432 2478
2433sub new { 2479sub new {
2434 my $class = shift; 2480 my $class = shift;
2435 2481
2436 $class->SUPER::new ( 2482 $class->SUPER::new (
2437 padding_x => 4, 2483 padding_x => 8,
2438 padding_y => 4, 2484 padding_y => 4,
2439 fg => [1.0, 1.0, 1.0], 2485 fg => [1.0, 1.0, 1.0],
2440 active_fg => [0.8, 0.8, 0.8], 2486 active_fg => [0.8, 0.8, 0.8],
2441 can_hover => 1, 2487 can_hover => 1,
2442 align => 0, 2488 align => 0.5,
2443 valign => 0, 2489 valign => 0.5,
2444 can_events => 1, 2490 can_events => 1,
2445 @_ 2491 @_
2446 ) 2492 )
2447} 2493}
2448 2494
2473 $self->SUPER::_draw; 2519 $self->SUPER::_draw;
2474} 2520}
2475 2521
2476############################################################################# 2522#############################################################################
2477 2523
2478package dc::UI::CheckBox; 2524package DC::UI::CheckBox;
2479 2525
2480our @ISA = dc::UI::DrawBG::; 2526our @ISA = DC::UI::DrawBG::;
2481 2527
2482my @tex = 2528my @tex =
2483 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2529 map { new_from_resource DC::Texture $_, mipmap => 1 }
2484 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2530 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2485 2531
2486use dc::OpenGL; 2532use DC::OpenGL;
2487 2533
2488sub new { 2534sub new {
2489 my $class = shift; 2535 my $class = shift;
2490 2536
2491 $class->SUPER::new ( 2537 $class->SUPER::new (
2538 fontsize => 1,
2492 padding_x => 2, 2539 padding_x => 2,
2493 padding_y => 2, 2540 padding_y => 2,
2494 fg => [1, 1, 1], 2541 fg => [1, 1, 1],
2495 active_fg => [1, 1, 0], 2542 active_fg => [1, 1, 0],
2496 bg => [0, 0, 0, 0.2], 2543 bg => [0, 0, 0, 0.2],
2502} 2549}
2503 2550
2504sub size_request { 2551sub size_request {
2505 my ($self) = @_; 2552 my ($self) = @_;
2506 2553
2507 (6) x 2 2554 ($self->{fontsize} * $::FONTSIZE) x 2
2508} 2555}
2509 2556
2510sub toggle { 2557sub toggle {
2511 my ($self) = @_; 2558 my ($self) = @_;
2512 2559
2548 glDisable GL_TEXTURE_2D; 2595 glDisable GL_TEXTURE_2D;
2549} 2596}
2550 2597
2551############################################################################# 2598#############################################################################
2552 2599
2553package dc::UI::Image; 2600package DC::UI::Image;
2554 2601
2555our @ISA = dc::UI::Base::; 2602our @ISA = DC::UI::DrawBG::;
2556 2603
2557use dc::OpenGL; 2604use DC::OpenGL;
2558 2605
2559our %texture_cache; 2606our %texture_cache;
2560 2607
2561sub new { 2608sub new {
2562 my $class = shift; 2609 my $class = shift;
2569 2616
2570 $self->{path} || $self->{tex} 2617 $self->{path} || $self->{tex}
2571 or Carp::croak "'path' or 'tex' attributes required"; 2618 or Carp::croak "'path' or 'tex' attributes required";
2572 2619
2573 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2620 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2574 new_from_file dc::Texture dc::find_rcfile $self->{path}, mipmap => 1; 2621 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2575 2622
2576 dc::weaken $texture_cache{$self->{path}}; 2623 DC::weaken $texture_cache{$self->{path}};
2577 2624
2578 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2625 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2579 2626
2580 $self 2627 $self
2581} 2628}
2582 2629
2583sub STORABLE_freeze { 2630sub STORABLE_freeze {
2584 my ($self, $cloning) = @_; 2631 my ($self, $cloning) = @_;
2585 2632
2586 $self->{path} 2633 $self->{path}
2587 or die "cannot serialise dc::UI::Image on non-loadable images\n"; 2634 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2588 2635
2589 $self->{path} 2636 $self->{path}
2590} 2637}
2591 2638
2592sub STORABLE_attach { 2639sub STORABLE_attach {
2593 my ($self, $cloning, $path) = @_; 2640 my ($self, $cloning, $path) = @_;
2594 2641
2595 $self->new (path => $path) 2642 $self->new (path => $path)
2596} 2643}
2597 2644
2645sub set_texture {
2646 my ($self, $tex) = @_;
2647
2648 $self->{tex} = $tex;
2649 $self->update;
2650}
2651
2598sub size_request { 2652sub size_request {
2599 my ($self) = @_; 2653 my ($self) = @_;
2600 2654
2601 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2655 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2602} 2656}
2603 2657
2604sub _draw { 2658sub _draw {
2605 my ($self) = @_; 2659 my ($self) = @_;
2660
2661 $self->SUPER::_draw;
2606 2662
2607 my $tex = $self->{tex}; 2663 my $tex = $self->{tex};
2608 2664
2609 my ($w, $h) = ($self->{w}, $self->{h}); 2665 my ($w, $h) = ($self->{w}, $self->{h});
2610 2666
2623 glDisable GL_TEXTURE_2D; 2679 glDisable GL_TEXTURE_2D;
2624} 2680}
2625 2681
2626############################################################################# 2682#############################################################################
2627 2683
2628package dc::UI::ImageButton; 2684package DC::UI::ImageButton;
2629 2685
2630our @ISA = dc::UI::Image::; 2686our @ISA = DC::UI::Image::;
2631 2687
2632use dc::OpenGL; 2688use DC::OpenGL;
2633
2634my %textures;
2635 2689
2636sub new { 2690sub new {
2637 my $class = shift; 2691 my $class = shift;
2638 2692
2639 my $self = $class->SUPER::new ( 2693 my $self = $class->SUPER::new (
2640 padding_x => 4, 2694 padding_x => 4,
2641 padding_y => 4, 2695 padding_y => 4,
2642 fg => [1, 1, 1], 2696 fg => [1, 1, 1],
2643 active_fg => [0, 0, 1], 2697 active_fg => [0, 0, 1],
2644 can_hover => 1, 2698 can_hover => 1,
2645 align => 0, 2699 align => 0.5,
2646 valign => 0, 2700 valign => 0.5,
2647 can_events => 1, 2701 can_events => 1,
2648 @_ 2702 @_
2649 ); 2703 );
2650} 2704}
2651 2705
2665 1 2719 1
2666} 2720}
2667 2721
2668############################################################################# 2722#############################################################################
2669 2723
2670package dc::UI::VGauge; 2724package DC::UI::VGauge;
2671 2725
2672our @ISA = dc::UI::Base::; 2726our @ISA = DC::UI::Base::;
2673 2727
2674use List::Util qw(min max); 2728use List::Util qw(min max);
2675 2729
2676use dc::OpenGL; 2730use DC::OpenGL;
2677 2731
2678my %tex = ( 2732my %tex = (
2679 food => [ 2733 food => [
2680 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2734 map { new_from_resource DC::Texture $_, mipmap => 1 }
2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2735 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2682 ], 2736 ],
2683 grace => [ 2737 grace => [
2684 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2685 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2739 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2686 ], 2740 ],
2687 hp => [ 2741 hp => [
2688 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2742 map { new_from_resource DC::Texture $_, mipmap => 1 }
2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2743 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2690 ], 2744 ],
2691 mana => [ 2745 mana => [
2692 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2746 map { new_from_resource DC::Texture $_, mipmap => 1 }
2693 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2747 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2694 ], 2748 ],
2695); 2749);
2696 2750
2697# eg. VGauge->new (gauge => 'food'), default gauge: food 2751# eg. VGauge->new (gauge => 'food'), default gauge: food
2800 glDisable GL_TEXTURE_2D; 2854 glDisable GL_TEXTURE_2D;
2801} 2855}
2802 2856
2803############################################################################# 2857#############################################################################
2804 2858
2805package dc::UI::Progress; 2859package DC::UI::Progress;
2806 2860
2807our @ISA = dc::UI::Label::; 2861our @ISA = DC::UI::Label::;
2808 2862
2809use dc::OpenGL; 2863use DC::OpenGL;
2810 2864
2811sub new { 2865sub new {
2812 my ($class, %arg) = @_; 2866 my ($class, %arg) = @_;
2813 2867
2814 my $self = $class->SUPER::new ( 2868 my $self = $class->SUPER::new (
2869 padding_x => 2,
2870 padding_y => 2,
2815 fg => [1, 1, 1], 2871 fg => [1, 1, 1],
2816 bg => [0, 0, 1, 0.2], 2872 bg => [0, 0, 1, 0.2],
2817 bar => [0.7, 0.5, 0.1, 0.8], 2873 bar => [0.7, 0.5, 0.1, 0.8],
2818 outline => [0.4, 0.3, 0], 2874 outline => [0.4, 0.3, 0],
2819 fontsize => 0.9, 2875 fontsize => 0.9,
2820 valign => 0, 2876 valign => 0.5,
2821 align => 0, 2877 align => 0.5,
2822 can_events => 1, 2878 can_events => 1,
2823 ellipsise => 1, 2879 ellipsise => 1,
2824 label => "%d%%", 2880 label => "%d%%",
2825 %arg, 2881 %arg,
2826 ); 2882 );
2834 my ($self, $label) = @_; 2890 my ($self, $label) = @_;
2835 2891
2836 return if $self->{label} eq $label; 2892 return if $self->{label} eq $label;
2837 $self->{label} = $label; 2893 $self->{label} = $label;
2838 2894
2839 $self->dc::UI::Progress::set_value (0 + delete $self->{value}); 2895 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2840} 2896}
2841 2897
2842sub set_value { 2898sub set_value {
2843 my ($self, $value) = @_; 2899 my ($self, $value) = @_;
2844 2900
2859 my ($self) = @_; 2915 my ($self) = @_;
2860 2916
2861 glEnable GL_BLEND; 2917 glEnable GL_BLEND;
2862 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2918 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2863 2919
2920 my $px = $self->{padding_x};
2921 my $py = $self->{padding_y};
2922
2864 if ($self->{value} >= 0) { 2923 if ($self->{value} >= 0) {
2865 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2924 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2866 2925
2867 glColor_premultiply @{$self->{bar}}; 2926 glColor_premultiply @{$self->{bar}};
2868 glRect 2, 2, $s, $self->{h} - 2; 2927 glRect $px, $py, $s, $self->{h} - $py;
2869 glColor_premultiply @{$self->{bg}}; 2928 glColor_premultiply @{$self->{bg}};
2870 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2929 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2871 } 2930 }
2872 2931
2873 glColor_premultiply @{$self->{outline}}; 2932 glColor_premultiply @{$self->{outline}};
2933
2934 $px -= .5;
2935 $py -= .5;
2936
2874 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2937 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2875 2938
2876 glDisable GL_BLEND; 2939 glDisable GL_BLEND;
2877 2940
2878 { 2941 {
2879 local $self->{bg}; # do not draw background 2942 local $self->{bg}; # do not draw background
2881 } 2944 }
2882} 2945}
2883 2946
2884############################################################################# 2947#############################################################################
2885 2948
2886package dc::UI::ExperienceProgress; 2949package DC::UI::ExperienceProgress;
2887 2950
2888our @ISA = dc::UI::Progress::; 2951our @ISA = DC::UI::Progress::;
2889 2952
2890sub new { 2953sub new {
2891 my ($class, %arg) = @_; 2954 my ($class, %arg) = @_;
2892 2955
2956 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2957
2893 my $self = $class->SUPER::new ( 2958 my $self = $class->SUPER::new (
2959 %arg,
2894 tooltip => sub { 2960 tooltip => sub {
2895 my ($self) = @_; 2961 my ($self) = @_;
2896 2962
2897 sprintf "level %d\n%s points\n%s next level\n%s to go", 2963 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2964 $tt,
2898 $self->{lvl}, 2965 $self->{lvl},
2899 ::formsep ($self->{exp}), 2966 ::formsep ($self->{exp}),
2900 ::formsep ($self->{nxt}), 2967 ::formsep ($self->{nxt}),
2901 ::formsep ($self->{nxt} - $self->{exp}), 2968 ::formsep ($self->{nxt} - $self->{exp}),
2969 $self->_percent * 100,
2902 }, 2970 },
2903 %arg
2904 ); 2971 );
2905 2972
2906 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2973 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2907 if $::CONN; 2974 if $::CONN;
2908 2975
2916 if $::CONN; 2983 if $::CONN;
2917 2984
2918 $self->SUPER::DESTROY; 2985 $self->SUPER::DESTROY;
2919} 2986}
2920 2987
2988sub _percent {
2989 my ($self) = @_;
2990
2991 my $table = $::CONN && $::CONN->{exp_table}
2992 or return -1;
2993
2994 my $l0 = $table->[$self->{lvl} - 1];
2995 my $l1 = $table->[$self->{lvl}];
2996
2997 $self->{nxt} = $l1;
2998
2999 ($self->{exp} - $l0) / ($l1 - $l0)
3000}
3001
2921sub set_value { 3002sub set_value {
2922 my ($self, $lvl, $exp) = @_; 3003 my ($self, $lvl, $exp) = @_;
2923 3004
2924 $self->{lvl} = $lvl; 3005 $self->{lvl} = $lvl;
2925 $self->{exp} = $exp; 3006 $self->{exp} = $exp;
2926 3007
2927 my $v = -1;
2928
2929 if ($::CONN && (my $table = $::CONN->{exp_table})) {
2930 my $l0 = $table->[$lvl - 1];
2931 my $l1 = $table->[$lvl];
2932
2933 $self->{nxt} = $l1;
2934
2935 $v = ($exp - $l0) / ($l1 - $l0);
2936 }
2937
2938 $self->SUPER::set_value ($v); 3008 $self->SUPER::set_value ($self->_percent);
2939} 3009}
2940 3010
2941############################################################################# 3011#############################################################################
2942 3012
2943package dc::UI::Gauge; 3013package DC::UI::Gauge;
2944 3014
2945our @ISA = dc::UI::VBox::; 3015our @ISA = DC::UI::VBox::;
2946 3016
2947sub new { 3017sub new {
2948 my ($class, %arg) = @_; 3018 my ($class, %arg) = @_;
2949 3019
2950 my $self = $class->SUPER::new ( 3020 my $self = $class->SUPER::new (
2952 can_hover => 1, 3022 can_hover => 1,
2953 can_events => 1, 3023 can_events => 1,
2954 %arg, 3024 %arg,
2955 ); 3025 );
2956 3026
2957 $self->add ($self->{value} = new dc::UI::Label valign => +1, align => 0, template => "999"); 3027 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2958 $self->add ($self->{gauge} = new dc::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3028 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2959 $self->add ($self->{max} = new dc::UI::Label valign => -1, align => 0, template => "999"); 3029 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2960 3030
2961 $self 3031 $self
2962} 3032}
2963 3033
2964sub set_fontsize { 3034sub set_fontsize {
2985 $self->{value}->set_text ($val); 3055 $self->{value}->set_text ($val);
2986} 3056}
2987 3057
2988############################################################################# 3058#############################################################################
2989 3059
2990package dc::UI::Slider; 3060package DC::UI::Slider;
2991 3061
2992use strict; 3062use common::sense;
2993 3063
2994use dc::OpenGL; 3064use DC::OpenGL;
2995 3065
2996our @ISA = dc::UI::DrawBG::; 3066our @ISA = DC::UI::DrawBG::;
2997 3067
2998my @tex = 3068my @tex =
2999 map { new_from_file dc::Texture dc::find_rcfile $_ } 3069 map { new_from_resource DC::Texture $_ }
3000 qw(s1_slider.png s1_slider_bg.png); 3070 qw(s1_slider.png s1_slider_bg.png);
3001 3071
3002sub new { 3072sub new {
3003 my $class = shift; 3073 my $class = shift;
3004 3074
3041sub set_value { 3111sub set_value {
3042 my ($self, $value) = @_; 3112 my ($self, $value) = @_;
3043 3113
3044 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3114 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3045 3115
3046 $hi = $lo + 1 if $hi <= $lo; 3116 $hi = $lo if $hi < $lo;
3047 3117
3048 $page = $hi - $lo if $page > $hi - $lo; 3118 $value = $hi - $page if $value > $hi - $page;
3049
3050 $value = $lo if $value < $lo; 3119 $value = $lo if $value < $lo;
3051 $value = $hi - $page if $value > $hi - $page;
3052 3120
3053 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3121 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3054 if $unit; 3122 if $unit;
3055 3123
3056 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3124 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3071 my ($self, $ev, $x, $y) = @_; 3139 my ($self, $ev, $x, $y) = @_;
3072 3140
3073 $self->SUPER::invoke_button_down ($ev, $x, $y); 3141 $self->SUPER::invoke_button_down ($ev, $x, $y);
3074 3142
3075 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; 3143 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
3076 3144
3077 $self->invoke_mouse_motion ($ev, $x, $y); 3145 $self->invoke_mouse_motion ($ev, $x, $y);
3078 3146
3079 1 3147 1
3080} 3148}
3081 3149
3085 if ($GRAB == $self) { 3153 if ($GRAB == $self) {
3086 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 3154 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
3087 3155
3088 my (undef, $lo, $hi, $page) = @{$self->{range}}; 3156 my (undef, $lo, $hi, $page) = @{$self->{range}};
3089 3157
3090 $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); 3158 $x = ($x - $self->{click}[1]) / ($w * $self->{scale} || 1e999);
3091 3159
3092 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); 3160 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
3093 } else { 3161 } else {
3094 return 0; 3162 return 0;
3095 } 3163 }
3100sub invoke_mouse_wheel { 3168sub invoke_mouse_wheel {
3101 my ($self, $ev) = @_; 3169 my ($self, $ev) = @_;
3102 3170
3103 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3171 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3104 3172
3105 my $pagepart = $ev->{mod} & dc::KMOD_SHIFT ? 1 : 0.2; 3173 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3106 3174
3107 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3175 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3108 3176
3109 1 3177 1
3110} 3178}
3120 my ($self) = @_; 3188 my ($self) = @_;
3121 3189
3122 unless ($self->{knob_w}) { 3190 unless ($self->{knob_w}) {
3123 $self->set_value ($self->{range}[0]); 3191 $self->set_value ($self->{range}[0]);
3124 3192
3125 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3193 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3126 my $range = ($hi - $page - $lo) || 1e-100; 3194 my $range = ($hi - $page - $lo) || 1e-10;
3127 3195
3128 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3196 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3129 3197
3130 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3198 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3131 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3199 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3132 3200
3133 $value = ($value - $lo) / $range; 3201 $value = ($value - $lo) / $range;
3141 3209
3142 glScale $self->{w}, $self->{h}; 3210 glScale $self->{w}, $self->{h};
3143 3211
3144 if ($self->{vertical}) { 3212 if ($self->{vertical}) {
3145 # draw a vertical slider like a rotated horizontal slider 3213 # draw a vertical slider like a rotated horizontal slider
3146 3214
3147 glTranslate 1, 0, 0; 3215 glTranslate 1, 0, 0;
3148 glRotate 90, 0, 0, 1; 3216 glRotate 90, 0, 0, 1;
3149 } 3217 }
3150 3218
3151 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 3219 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
3163 glDisable GL_TEXTURE_2D; 3231 glDisable GL_TEXTURE_2D;
3164} 3232}
3165 3233
3166############################################################################# 3234#############################################################################
3167 3235
3168package dc::UI::ValSlider; 3236package DC::UI::ValSlider;
3169 3237
3170our @ISA = dc::UI::HBox::; 3238our @ISA = DC::UI::HBox::;
3171 3239
3172sub new { 3240sub new {
3173 my ($class, %arg) = @_; 3241 my ($class, %arg) = @_;
3174 3242
3175 my $range = delete $arg{range}; 3243 my $range = delete $arg{range};
3176 3244
3177 my $self = $class->SUPER::new ( 3245 my $self = $class->SUPER::new (
3178 slider => (new dc::UI::Slider expand => 1, range => $range), 3246 slider => (new DC::UI::Slider expand => 1, range => $range),
3179 entry => (new dc::UI::Label text => "", template => delete $arg{template}), 3247 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3180 to_value => sub { shift }, 3248 to_value => sub { shift },
3181 from_value => sub { shift }, 3249 from_value => sub { shift },
3182 %arg, 3250 %arg,
3183 ); 3251 );
3184 3252
3204sub set_range { shift->{slider}->set_range (@_) } 3272sub set_range { shift->{slider}->set_range (@_) }
3205sub set_value { shift->{slider}->set_value (@_) } 3273sub set_value { shift->{slider}->set_value (@_) }
3206 3274
3207############################################################################# 3275#############################################################################
3208 3276
3209package dc::UI::TextScroller; 3277package DC::UI::TextScroller;
3210 3278
3211our @ISA = dc::UI::HBox::; 3279our @ISA = DC::UI::HBox::;
3212 3280
3213use dc::OpenGL; 3281use DC::OpenGL;
3214 3282
3215sub new { 3283sub new {
3216 my $class = shift; 3284 my $class = shift;
3217 3285
3218 my $self = $class->SUPER::new ( 3286 my $self = $class->SUPER::new (
3219 fontsize => 1, 3287 fontsize => 1,
3220 can_events => 1, 3288 can_events => 1,
3221 indent => 0, 3289 indent => 0,
3222 #font => default_font 3290 #font => default_font
3223 @_, 3291 @_,
3224 3292
3225 layout => (new dc::Layout), 3293 layout => (new DC::Layout),
3226 par => [],
3227 max_par => 0, 3294 max_par => 0,
3228 height => 0, 3295 height => 0,
3229 children => [ 3296 children => [
3230 (new dc::UI::Empty expand => 1), 3297 (new DC::UI::Empty expand => 1),
3231 (new dc::UI::Slider vertical => 1), 3298 (new DC::UI::Slider vertical => 1),
3232 ], 3299 ],
3233 ); 3300 );
3234 3301
3235 $self->{children}[1]->connect (changed => sub { $self->update }); 3302 $self->{children}[1]->connect (changed => sub { $self->update });
3303
3304 $self->add_paragraph (@{ delete $self->{par} }) if @{ $self->{par} };
3236 3305
3237 $self 3306 $self
3238} 3307}
3239 3308
3240sub set_fontsize { 3309sub set_fontsize {
3292 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3361 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3293 $layout->set_markup ($para->{markup}); 3362 $layout->set_markup ($para->{markup});
3294 3363
3295 $layout->set_shapes ( 3364 $layout->set_shapes (
3296 map 3365 map
3297 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3366 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3298 @{$para->{widget}} 3367 @{$para->{widget}}
3299 ); 3368 );
3300 3369
3301 $layout 3370 $layout
3302} 3371}
3419 $ROOT->on_post_alloc ($self => sub { 3488 $ROOT->on_post_alloc ($self => sub {
3420 $self->force_uptodate; 3489 $self->force_uptodate;
3421 3490
3422 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3491 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3423 3492
3424 $self->{texture} ||= new_from_opengl dc::Texture $W, $H, sub { 3493 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3425 glClearColor 0, 0, 0, 0; 3494 glClearColor 0, 0, 0, 0;
3426 glClear GL_COLOR_BUFFER_BIT; 3495 glClear GL_COLOR_BUFFER_BIT;
3427 3496
3428 package dc::UI::Base; 3497 package DC::UI::Base;
3429 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3498 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3430 (0, 0, $self->{w}, $self->{h}); 3499 (0, 0, $self->{w}, $self->{h});
3431 3500
3432 my $top = int $self->{children}[1]{range}[0]; 3501 my $top = int $self->{children}[1]{range}[0];
3433 3502
3492 $self->{children}[1]->draw; 3561 $self->{children}[1]->draw;
3493} 3562}
3494 3563
3495############################################################################# 3564#############################################################################
3496 3565
3497package dc::UI::Animator; 3566package DC::UI::Animator;
3498 3567
3499use dc::OpenGL; 3568use DC::OpenGL;
3500 3569
3501our @ISA = dc::UI::Bin::; 3570our @ISA = DC::UI::Bin::;
3502 3571
3503sub moveto { 3572sub moveto {
3504 my ($self, $x, $y) = @_; 3573 my ($self, $x, $y) = @_;
3505 3574
3506 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3575 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3507 $self->{speed} = 0.001; 3576 $self->{speed} = 0.001;
3508 $self->{time} = 1; 3577 $self->{time} = 1;
3509 3578
3510 ::animation_start $self; 3579 ::animation_start $self;
3511} 3580}
3512 3581
3513sub animate { 3582sub animate {
3514 my ($self, $interval) = @_; 3583 my ($self, $interval) = @_;
3518 $self->{time} = 0; 3587 $self->{time} = 0;
3519 ::animation_stop $self; 3588 ::animation_stop $self;
3520 } 3589 }
3521 3590
3522 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; 3591 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
3523 3592
3524 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); 3593 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
3525 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); 3594 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
3526} 3595}
3527 3596
3528sub _draw { 3597sub _draw {
3534 glPopMatrix; 3603 glPopMatrix;
3535} 3604}
3536 3605
3537############################################################################# 3606#############################################################################
3538 3607
3539package dc::UI::Flopper; 3608package DC::UI::Flopper;
3540 3609
3541our @ISA = dc::UI::Button::; 3610our @ISA = DC::UI::Button::;
3542 3611
3543sub new { 3612sub new {
3544 my $class = shift; 3613 my $class = shift;
3545 3614
3546 my $self = $class->SUPER::new ( 3615 my $self = $class->SUPER::new (
3558 $self->{other}->toggle_visibility; 3627 $self->{other}->toggle_visibility;
3559} 3628}
3560 3629
3561############################################################################# 3630#############################################################################
3562 3631
3563package dc::UI::Tooltip; 3632package DC::UI::Tooltip;
3564 3633
3565our @ISA = dc::UI::Bin::; 3634our @ISA = DC::UI::Bin::;
3566 3635
3567use dc::OpenGL; 3636use DC::OpenGL;
3568 3637
3569sub new { 3638sub new {
3570 my $class = shift; 3639 my $class = shift;
3571 3640
3572 $class->SUPER::new ( 3641 $class->SUPER::new (
3573 @_, 3642 @_,
3574 can_events => 0, 3643 can_events => 0,
3575 ) 3644 )
3576} 3645}
3577 3646
3647# expand, as good as possible
3648sub _expand_doclets {
3649 my ($tip) = @_;
3650
3651 $tip =~ s{#\(([^)]+)\)}{
3652 if ($::CONN) {
3653 exists $::CONN->{doclet}{$1}
3654 ? $::CONN->{doclet}{$1}
3655 : "(waiting for server to show full text)"
3656 } else {
3657 "(unable to show full text without server connection)"
3658 }
3659 }ge;
3660
3661 $tip =~ s/^\n+//;
3662 $tip =~ s/\n+$//;
3663
3664 $tip
3665}
3666
3667# expands a tooltip, potentially multiple times remotely
3668# and returns a guard. clals the clalback each time the text changes.
3669sub expand_tooltip {
3670 my ($tip, $cb) = @_;
3671
3672 # first expand #name tooltips from local pod
3673 $tip = DC::Pod::section_label tooltip => $1
3674 if $tip =~ /^#([^(].*)$/;
3675
3676 my $active; # true if any remote requests outstanding
3677
3678 if ($::CONN && $::CONN->{addme_success}) {
3679 # now find all doclet references
3680 for my $doclet ($tip =~ /#\(([^)]+)\)/g) {
3681 unless (exists $::CONN->{doclet}{$doclet}) {
3682 # need to ask the server
3683 # we don't try to avoid duplicate requests
3684
3685 $active = 1;
3686 $::CONN->send_exti_req (doclet => (split /\//, $doclet, 2), sub {
3687 $::CONN->{doclet}{$doclet} = DC::sanitise_cfxml $_[0];
3688 $cb->(_expand_doclets $tip) if $active;
3689 });
3690 }
3691 }
3692 }
3693
3694 $cb->(_expand_doclets $tip);
3695
3696 $active and Guard::guard { undef $active }
3697}
3698
3578sub set_tooltip_from { 3699sub set_tooltip_from {
3579 my ($self, $widget) = @_; 3700 my ($self, $widget) = @_;
3580 3701
3581 my $tip = $widget->{tooltip}; 3702 my $tip = $widget->{tooltip};
3582 $tip = $tip->($widget) if "CODE" eq ref $tip; 3703 $tip = $tip->($widget) if "CODE" eq ref $tip;
3583
3584 $tip = dc::Pod::section_label tooltip => $1
3585 if $tip =~ /^#(.*)$/;
3586 3704
3587 if ($ENV{CFPLUS_DEBUG} & 2) {
3588 $tip .= "\n\n" . (ref $widget) . "\n"
3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3590 . "req $widget->{req_w} $widget->{req_h}\n"
3591 . "visible $widget->{visible}";
3592 }
3593
3594 $tip =~ s/^\n+//;
3595 $tip =~ s/\n+$//;
3596
3597 $self->add (new dc::UI::Label 3705 $self->add (new DC::UI::Label
3598 markup => $tip, 3706 fg => $DC::THEME{tooltip_fg},
3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3707 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3708 align => 0,
3600 fontsize => 0.8, 3709 fontsize => 0.8,
3601 style => 1, # FLAG_INVERSE 3710 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3602 ellipsise => 0, 3711 ellipsise => 0,
3603 font => ($widget->{tooltip_font} || $::FONT_PROP), 3712 font => ($widget->{tooltip_font} || $::FONT_PROP),
3604 ); 3713 );
3714
3715 $self->{tooltip_expand} = expand_tooltip $tip, sub {
3716 my ($tip) = @_;
3717
3718 if ($ENV{CFPLUS_DEBUG} & 2) {
3719 $tip .= "\n\n" . (ref $widget) . "\n"
3720 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3721 . "req $widget->{req_w} $widget->{req_h}\n"
3722 . "visible $widget->{visible}";
3723 }
3724
3725 $self->{children}[0]->set_markup ($tip);
3726 };
3605} 3727}
3606 3728
3607sub size_request { 3729sub size_request {
3608 my ($self) = @_; 3730 my ($self) = @_;
3609 3731
3643sub _draw { 3765sub _draw {
3644 my ($self) = @_; 3766 my ($self) = @_;
3645 3767
3646 my ($w, $h) = @$self{qw(w h)}; 3768 my ($w, $h) = @$self{qw(w h)};
3647 3769
3648 glColor 1, 0.8, 0.4; 3770 glColor @{ $DC::THEME{tooltip_bg} };
3649 glRect 0, 0, $w, $h; 3771 glRect 0, 0, $w, $h;
3650 3772
3651 glColor 0, 0, 0; 3773 glColor @{ $DC::THEME{tooltip_border} };
3652 glRect_lineloop .5, .5, $w + .5, $h + .5; 3774 glRect_lineloop .5, .5, $w + .5, $h + .5;
3653 3775
3654 glTranslate 2, 2; 3776 glTranslate 2, 2;
3655 3777
3656 $self->SUPER::_draw; 3778 $self->SUPER::_draw;
3657} 3779}
3658 3780
3659############################################################################# 3781#############################################################################
3660 3782
3661package dc::UI::Face; 3783package DC::UI::Face;
3662 3784
3663our @ISA = dc::UI::DrawBG::; 3785our @ISA = DC::UI::DrawBG::;
3664 3786
3665use dc::OpenGL; 3787use DC::OpenGL;
3666 3788
3667sub new { 3789sub new {
3668 my $class = shift; 3790 my $class = shift;
3669 3791
3670 my $self = $class->SUPER::new ( 3792 my $self = $class->SUPER::new (
3673 aspect => 1, 3795 aspect => 1,
3674 can_events => 0, 3796 can_events => 0,
3675 @_, 3797 @_,
3676 ); 3798 );
3677 3799
3678 if ($self->{anim} && $self->{animspeed}) {
3679 dc::weaken (my $widget = $self);
3680
3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3683 return unless $::CONN;
3684
3685 my $w = $widget
3686 or return;
3687
3688 ++$w->{frame};
3689 $w->update_face;
3690
3691 # somehow, $widget can go away
3692 $w->update;
3693 $w->update_timer;
3694 };
3695
3696 $self->update_face;
3697 $self->update_timer; 3800 $self->update_anim;
3698 } 3801
3699
3700 $self 3802 $self
3701} 3803}
3702 3804
3703sub update_timer { 3805sub update_timer {
3704 my ($self) = @_; 3806 my ($self) = @_;
3726 $tex->upload (sub { $self->reconfigure }); 3828 $tex->upload (sub { $self->reconfigure });
3727 } 3829 }
3728 } 3830 }
3729 } 3831 }
3730 } 3832 }
3833 }
3834}
3835
3836sub update_anim {
3837 my ($self) = @_;
3838
3839 if ($self->{anim} && $self->{animspeed}) {
3840 DC::weaken (my $widget = $self);
3841
3842 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3843 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3844 return unless $::CONN;
3845
3846 my $w = $widget
3847 or return;
3848
3849 ++$w->{frame};
3850 $w->update_face;
3851
3852 # somehow, $widget can go away
3853 $w->update;
3854 $w->update_timer;
3855 };
3856
3857 $self->update_face;
3858 $self->update_timer;
3859 } else {
3860 delete $self->{timer};
3731 } 3861 }
3732} 3862}
3733 3863
3734sub size_request { 3864sub size_request {
3735 my ($self) = @_; 3865 my ($self) = @_;
3757 return unless $self->{visible}; 3887 return unless $self->{visible};
3758 3888
3759 $self->SUPER::update; 3889 $self->SUPER::update;
3760} 3890}
3761 3891
3892sub set_face {
3893 my ($self, $face) = @_;
3894
3895 $self->{face} = $face;
3896 $self->reconfigure;
3897}
3898
3899sub set_anim {
3900 my ($self, $anim) = @_;
3901
3902 $self->{anim} = $anim;
3903 $self->update_anim;
3904}
3905
3906sub set_animspeed {
3907 my ($self, $animspeed) = @_;
3908
3909 $self->{animspeed} = $animspeed;
3910 $self->update_anim;
3911}
3912
3762sub invoke_visibility_change { 3913sub invoke_visibility_change {
3763 my ($self) = @_; 3914 my ($self) = @_;
3764 3915
3765 $self->update_timer; 3916 $self->update_timer;
3766 3917
3790 $self->SUPER::destroy; 3941 $self->SUPER::destroy;
3791} 3942}
3792 3943
3793############################################################################# 3944#############################################################################
3794 3945
3795package dc::UI::Buttonbar; 3946package DC::UI::Buttonbar;
3796 3947
3797our @ISA = dc::UI::HBox::; 3948our @ISA = DC::UI::HBox::;
3798 3949
3799# TODO: should actually wrap buttons and other goodies. 3950# TODO: should actually wrap buttons and other goodies.
3800 3951
3801############################################################################# 3952#############################################################################
3802 3953
3803package dc::UI::Menu; 3954package DC::UI::Menu;
3804 3955
3805our @ISA = dc::UI::Toplevel::; 3956our @ISA = DC::UI::Toplevel::;
3806 3957
3807use dc::OpenGL; 3958use DC::OpenGL;
3808 3959
3809sub new { 3960sub new {
3810 my $class = shift; 3961 my $class = shift;
3811 3962
3812 my $self = $class->SUPER::new ( 3963 my $self = $class->SUPER::new (
3813 items => [], 3964 items => [],
3814 z => 100, 3965 z => 100,
3815 @_, 3966 @_,
3816 ); 3967 );
3817 3968
3818 $self->add ($self->{vbox} = new dc::UI::VBox); 3969 $self->add ($self->{vbox} = new DC::UI::VBox);
3819 3970
3820 for my $item (@{ $self->{items} }) { 3971 for my $item (@{ $self->{items} }) {
3821 my ($widget, $cb, $tooltip) = @$item; 3972 my ($widget, $cb, $tooltip) = @$item;
3822 3973
3823 # handle various types of items, only text for now 3974 # handle various types of items, only text for now
3824 if (!ref $widget) { 3975 if (!ref $widget) {
3825 if ($widget =~ /\t/) { 3976 if ($widget =~ /\t/) {
3826 my ($left, $right) = split /\t/, $widget, 2; 3977 my ($left, $right) = split /\t/, $widget, 2;
3827 3978
3828 $widget = new dc::UI::HBox 3979 $widget = new DC::UI::HBox
3829 can_hover => 1, 3980 can_hover => 1,
3830 can_events => 1, 3981 can_events => 1,
3831 tooltip => $tooltip, 3982 tooltip => $tooltip,
3832 children => [ 3983 children => [
3833 (new dc::UI::Label markup => $left, expand => 1), 3984 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3834 (new dc::UI::Label markup => $right, align => +1), 3985 (new DC::UI::Label markup => $right, align => 1),
3835 ], 3986 ],
3836 ; 3987 ;
3837 3988
3838 } else { 3989 } else {
3839 $widget = new dc::UI::Label 3990 $widget = new DC::UI::Label
3840 can_hover => 1, 3991 can_hover => 1,
3841 can_events => 1, 3992 can_events => 1,
3993 align => 0,
3842 markup => $widget, 3994 markup => $widget,
3843 tooltip => $tooltip; 3995 tooltip => $tooltip;
3844 } 3996 }
3845 } 3997 }
3846 3998
3861 # maybe save $GRAB? must be careful about events... 4013 # maybe save $GRAB? must be careful about events...
3862 $GRAB = $self; 4014 $GRAB = $self;
3863 $self->{button} = $ev->{button}; 4015 $self->{button} = $ev->{button};
3864 4016
3865 $self->show; 4017 $self->show;
3866 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 4018
4019 my $x = $ev->{x};
4020 my $y = $ev->{y};
4021
4022 $self->{root}->on_post_alloc ($self => sub {
4023 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
4024 });
4025
4026 1 # so it can be used inside event handlers
3867} 4027}
3868 4028
3869sub invoke_mouse_motion { 4029sub invoke_mouse_motion {
3870 my ($self, $ev, $x, $y) = @_; 4030 my ($self, $ev, $x, $y) = @_;
3871 4031
3892 1 4052 1
3893} 4053}
3894 4054
3895############################################################################# 4055#############################################################################
3896 4056
3897package dc::UI::Multiplexer; 4057package DC::UI::Multiplexer;
3898 4058
3899our @ISA = dc::UI::Container::; 4059our @ISA = DC::UI::Container::;
3900 4060
3901sub new { 4061sub new {
3902 my $class = shift; 4062 my $class = shift;
3903 4063
3904 my $self = $class->SUPER::new ( 4064 my $self = $class->SUPER::new (
3905 @_, 4065 @_,
3906 ); 4066 );
3907 4067
3908 $self->{current} = $self->{children}[0] 4068 $self->set_current_page (0);
3909 if @{ $self->{children} };
3910 4069
3911 $self 4070 $self
3912} 4071}
3913 4072
3914sub add { 4073sub add {
3915 my ($self, @widgets) = @_; 4074 my ($self, @widgets) = @_;
3916 4075
3917 $self->SUPER::add (@widgets); 4076 $self->SUPER::add (@widgets);
3918 4077
3919 $self->{current} = $self->{children}[0] 4078 $self->set_current_page (0)
3920 if @{ $self->{children} }; 4079 if @widgets == @{ $self->{children} };
3921} 4080}
3922 4081
3923sub get_current_page { 4082sub get_current_page {
3924 my ($self) = @_; 4083 my ($self) = @_;
3925 4084
3931 4090
3932 my $widget = ref $page_or_widget 4091 my $widget = ref $page_or_widget
3933 ? $page_or_widget 4092 ? $page_or_widget
3934 : $self->{children}[$page_or_widget]; 4093 : $self->{children}[$page_or_widget];
3935 4094
4095 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4096
3936 $self->{current} = $widget; 4097 if (($self->{current} = $widget)) {
4098 $self->{current}->set_visible if $self->{current} && $self->{visible};
3937 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4099 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3938 4100
3939 $self->emit (page_changed => $self->{current}); 4101 $self->emit (page_changed => $self->{current});
4102 }
3940 4103
3941 $self->realloc; 4104 $self->realloc;
3942} 4105}
3943 4106
3944sub visible_children { 4107sub visible_children {
3945 $_[0]{current} 4108 $_[0]{current} || ()
3946} 4109}
3947 4110
3948sub size_request { 4111sub size_request {
3949 my ($self) = @_; 4112 my ($self) = @_;
3950 4113
4114 $self->{current}
3951 $self->{current}->size_request 4115 ? $self->{current}->size_request
4116 : (0, 0)
3952} 4117}
3953 4118
3954sub invoke_size_allocate { 4119sub invoke_size_allocate {
3955 my ($self, $w, $h) = @_; 4120 my ($self, $w, $h) = @_;
3956 4121
3957 $self->{current}->configure (0, 0, $w, $h); 4122 $self->{current}->configure (0, 0, $w, $h)
4123 if $self->{current};
3958 4124
3959 1 4125 1
3960} 4126}
3961 4127
3962sub _draw { 4128sub _draw {
3963 my ($self) = @_; 4129 my ($self) = @_;
3964 4130
3965 $self->{current}->draw; 4131 $self->{current}->draw
4132 if $self->{current};
3966} 4133}
3967 4134
3968############################################################################# 4135#############################################################################
3969 4136
3970package dc::UI::Notebook; 4137package DC::UI::Notebook;
3971 4138
3972use dc::OpenGL; 4139use DC::OpenGL;
3973 4140
3974our @ISA = dc::UI::VBox::; 4141our @ISA = DC::UI::VBox::;
3975 4142
3976sub new { 4143sub new {
3977 my $class = shift; 4144 my $class = shift;
3978 4145
3979 my $self = $class->SUPER::new ( 4146 my $self = $class->SUPER::new (
3980 buttonbar => (new dc::UI::Buttonbar), 4147 buttonbar => (new DC::UI::Buttonbar),
3981 multiplexer => (new dc::UI::Multiplexer expand => 1), 4148 multiplexer => (new DC::UI::Multiplexer expand => 1),
3982 active_outline => [.7, .7, 0.2], 4149 active_outline => [.7, .7, 0.2],
3983 # filter => # will be put between multiplexer and $self 4150 # filter => # will be put between multiplexer and $self
3984 @_, 4151 @_,
3985 ); 4152 );
3986 4153
3995 4162
3996 for my $child (@$widgets) { 4163 for my $child (@$widgets) {
3997 Scalar::Util::weaken $child; 4164 Scalar::Util::weaken $child;
3998 $child->{c_tab_} ||= do { 4165 $child->{c_tab_} ||= do {
3999 my $tab = 4166 my $tab =
4000 (UNIVERSAL::isa $child->{c_tab}, "dc::UI::Base") 4167 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4001 ? $child->{c_tab} 4168 ? $child->{c_tab}
4002 : new dc::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4169 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4003 4170
4004 $tab->connect (activate => sub { 4171 $tab->connect (activate => sub {
4005 $wself->set_current_page ($child); 4172 $wself->set_current_page ($child);
4006 }); 4173 });
4007 4174
4037} 4204}
4038 4205
4039sub pages { 4206sub pages {
4040 my ($self) = @_; 4207 my ($self) = @_;
4041 $self->{multiplexer}->children 4208 $self->{multiplexer}->children
4209}
4210
4211sub page_index {
4212 my ($self, $widget) = @_;
4213
4214 my $i = 0;
4215 for ($self->pages) {
4216 if ($_ eq $widget) { return $i };
4217 $i++;
4218 }
4219
4220 undef
4042} 4221}
4043 4222
4044sub add_tab { 4223sub add_tab {
4045 my ($self, $title, $widget, $tooltip) = @_; 4224 my ($self, $title, $widget, $tooltip) = @_;
4046 4225
4083 } 4262 }
4084} 4263}
4085 4264
4086############################################################################# 4265#############################################################################
4087 4266
4088package dc::UI::Selector; 4267package DC::UI::Selector;
4089 4268
4090use utf8; 4269use utf8;
4091 4270
4092our @ISA = dc::UI::Button::; 4271our @ISA = DC::UI::Button::;
4093 4272
4094sub new { 4273sub new {
4095 my $class = shift; 4274 my $class = shift;
4096 4275
4097 my $self = $class->SUPER::new ( 4276 my $self = $class->SUPER::new (
4114 my ($value, $title, $tooltip) = @$_; 4293 my ($value, $title, $tooltip) = @$_;
4115 4294
4116 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4295 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4117 } 4296 }
4118 4297
4119 dc::UI::Menu->new (items => \@menu_items)->popup ($ev); 4298 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4120} 4299}
4121 4300
4122sub _set_value { 4301sub _set_value {
4123 my ($self, $value) = @_; 4302 my ($self, $value) = @_;
4124 4303
4147 $self->_set_value ($self->{value}); 4326 $self->_set_value ($self->{value});
4148} 4327}
4149 4328
4150############################################################################# 4329#############################################################################
4151 4330
4152package dc::UI::Statusbox; 4331package DC::UI::Statusbox;
4153 4332
4154our @ISA = dc::UI::VBox::; 4333our @ISA = DC::UI::VBox::;
4155 4334
4156sub new { 4335sub new {
4157 my $class = shift; 4336 my $class = shift;
4158 4337
4159 my $self = $class->SUPER::new ( 4338 my $self = $class->SUPER::new (
4160 fontsize => 0.8, 4339 fontsize => 0.8,
4161 @_, 4340 @_,
4162 ); 4341 );
4163 4342
4164 dc::weaken (my $this = $self); 4343 DC::weaken (my $this = $self);
4165 4344
4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; 4345 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4167 4346
4168 $self 4347 $self
4169} 4348}
4170 4349
4171sub reorder { 4350sub reorder {
4172 my ($self) = @_; 4351 my ($self) = @_;
4173 my $NOW = Time::HiRes::time; 4352 my $NOW = AE::time;
4174 4353
4175 # freeze display when hovering over any label 4354 # freeze display when hovering over any label
4176 return if $dc::UI::TOOLTIP->{owner} 4355 return if $DC::UI::TOOLTIP->{owner}
4177 && grep $dc::UI::TOOLTIP->{owner} == $_->{label}, 4356 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4178 values %{ $self->{item} }; 4357 values %{ $self->{item} };
4179 4358
4180 while (my ($k, $v) = each %{ $self->{item} }) { 4359 while (my ($k, $v) = each %{ $self->{item} }) {
4181 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4360 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4182 } 4361 }
4203 for ($short) { 4382 for ($short) {
4204 s/^\s+//; 4383 s/^\s+//;
4205 s/\s+/ /g; 4384 s/\s+/ /g;
4206 } 4385 }
4207 4386
4208 new dc::UI::Label 4387 new DC::UI::Label
4209 markup => $short, 4388 markup => $short,
4210 tooltip => $item->{tooltip}, 4389 tooltip => $item->{tooltip},
4211 tooltip_font => $::FONT_PROP, 4390 tooltip_font => $::FONT_PROP,
4212 tooltip_width => 0.67, 4391 tooltip_width => 0.67,
4213 fontsize => $item->{fontsize} || $self->{fontsize}, 4392 fontsize => $item->{fontsize} || $self->{fontsize},
4214 max_w => $::WIDTH * 0.44, 4393 max_w => $::WIDTH * 0.44,
4394 align => 0,
4215 fg => [@{ $item->{fg} }], 4395 fg => [@{ $item->{fg} }],
4216 can_events => 1, 4396 can_events => 1,
4217 can_hover => 1 4397 can_hover => 1
4218 }; 4398 };
4219 4399
4227 $label->{fg}[3] = $item->{fg}[3] || 1; 4407 $label->{fg}[3] = $item->{fg}[3] || 1;
4228 } 4408 }
4229 4409
4230 push @widgets, $label; 4410 push @widgets, $label;
4231 } 4411 }
4412
4413 my $hash = join ",", @widgets;
4414 return if $hash eq $self->{last_widget_hash};
4415 $self->{last_widget_hash} = $hash;
4232 4416
4233 $self->clear; 4417 $self->clear;
4234 $self->SUPER::add (reverse @widgets); 4418 $self->SUPER::add (reverse @widgets);
4235} 4419}
4236 4420
4272 $ROOT->on_refresh (reorder => sub { 4456 $ROOT->on_refresh (reorder => sub {
4273 $self->reorder; 4457 $self->reorder;
4274 }); 4458 });
4275} 4459}
4276 4460
4461sub clr_group {
4462 my ($self, $group) = @_;
4463
4464 if (delete $self->{item}{$group}) {
4465 $ROOT->on_refresh (reorder => sub {
4466 $self->reorder;
4467 });
4468 }
4469}
4470
4277sub reconfigure { 4471sub reconfigure {
4278 my ($self) = @_; 4472 my ($self) = @_;
4279 4473
4280 delete $_->{label} 4474 delete $_->{label}
4281 for values %{ $self->{item} || {} }; 4475 for values %{ $self->{item} || {} };
4292 $self->SUPER::destroy; 4486 $self->SUPER::destroy;
4293} 4487}
4294 4488
4295############################################################################# 4489#############################################################################
4296 4490
4297package dc::UI::Root; 4491package DC::UI::Root;
4298 4492
4299our @ISA = dc::UI::Container::; 4493our @ISA = DC::UI::Container::;
4300 4494
4301use List::Util qw(min max); 4495use List::Util qw(min max);
4302 4496
4303use dc::OpenGL; 4497use DC::OpenGL;
4304 4498
4305sub new { 4499sub new {
4306 my $class = shift; 4500 my $class = shift;
4307 4501
4308 my $self = $class->SUPER::new ( 4502 my $self = $class->SUPER::new (
4309 visible => 1, 4503 visible => 1,
4310 @_, 4504 @_,
4311 ); 4505 );
4312 4506
4313 dc::weaken ($self->{root} = $self); 4507 DC::weaken ($self->{root} = $self);
4314 4508
4315 $self 4509 $self
4316} 4510}
4317 4511
4318sub size_request { 4512sub size_request {
4435 while () { 4629 while () {
4436 @queue or last outer; 4630 @queue or last outer;
4437 4631
4438 $widget = pop @{ $queue[-1] || [] } 4632 $widget = pop @{ $queue[-1] || [] }
4439 and last; 4633 and last;
4440 4634
4441 pop @queue; 4635 pop @queue;
4442 } 4636 }
4443 4637
4444 delete $queue{$widget+0}; 4638 delete $queue{$widget+0};
4445 4639
4446 my ($w, $h) = $widget->size_request; 4640 my ($w, $h) = $widget->size_request;
4447 4641
4448 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4642 $w += $widget->{padding_x} * 2;
4449 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4643 $h += $widget->{padding_y} * 2;
4644
4645 $w = max $widget->{min_w}, $w;
4646 $h = max $widget->{min_h}, $h;
4450 4647
4451 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4648 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4452 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4649 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4453 4650
4454 $w = $widget->{force_w} if exists $widget->{force_w}; 4651 $w = $widget->{force_w} if exists $widget->{force_w};
4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4718 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4522 glMatrixMode GL_MODELVIEW; 4719 glMatrixMode GL_MODELVIEW;
4523 glLoadIdentity; 4720 glLoadIdentity;
4524 4721
4525 { 4722 {
4526 package dc::UI::Base; 4723 package DC::UI::Base;
4527 4724
4528 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4725 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4529 (0, 0, $self->{w}, $self->{h}); 4726 (0, 0, $self->{w}, $self->{h});
4530 4727
4531 $self->_draw; 4728 $self->_draw;
4532 } 4729 }
4533} 4730}
4534 4731
4535############################################################################# 4732#############################################################################
4536 4733
4537package dc::UI; 4734package DC::UI;
4538 4735
4539$ROOT = new dc::UI::Root; 4736$ROOT = new DC::UI::Root;
4540$TOOLTIP = new dc::UI::Tooltip z => 900; 4737$TOOLTIP = new DC::UI::Tooltip z => 900;
4541 4738
45421 47391
4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines