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.479 by root, Thu Feb 4 20:01:09 2010 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 Guard ();
8
9use DC;
9use dc::Pod; 10use DC::Pod;
10use dc::Texture; 11use DC::Texture;
11 12
12our ($FOCUS, $HOVER, $GRAB); # various widgets 13our ($FOCUS, $HOVER, $GRAB); # various widgets
13 14
14our $LAYOUT; 15our $LAYOUT;
15our $ROOT; 16our $ROOT;
111 112
112 if ($GRAB) { 113 if ($GRAB) {
113 if ($ev->{button} == 4 || $ev->{button} == 5) { 114 if ($ev->{button} == 4 || $ev->{button} == 5) {
114 # mousewheel 115 # mousewheel
115 my $delta = $ev->{button} * 2 - 9; 116 my $delta = $ev->{button} * 2 - 9;
116 my $shift = $ev->{mod} & dc::KMOD_SHIFT; 117 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
117 118
118 $ev->{dx} = $shift ? $delta : 0; 119 $ev->{dx} = $shift ? $delta : 0;
119 $ev->{dy} = $shift ? 0 : $delta; 120 $ev->{dy} = $shift ? 0 : $delta;
120 121
121 $GRAB->emit (mouse_wheel => $ev); 122 $GRAB->emit (mouse_wheel => $ev);
189# call when resolution changes etc. 190# call when resolution changes etc.
190sub rescale_widgets { 191sub rescale_widgets {
191 my ($sx, $sy) = @_; 192 my ($sx, $sy) = @_;
192 193
193 for my $widget (values %WIDGET) { 194 for my $widget (values %WIDGET) {
194 if ($widget->{is_toplevel}) { 195 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 196 $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.]+$/; 197 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
197 198
198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 199 $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}; 200 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
211 reconfigure_widgets; 212 reconfigure_widgets;
212} 213}
213 214
214############################################################################# 215#############################################################################
215 216
216package dc::UI::Event; 217package DC::UI::Event;
217 218
218sub xy { 219sub xy {
219 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 220 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
220} 221}
221 222
222############################################################################# 223#############################################################################
223 224
224package dc::UI::Base; 225package DC::UI::Base;
225 226
226use strict; 227use strict;
227 228
228use dc::OpenGL; 229use DC::OpenGL;
229 230
230sub new { 231sub new {
231 my $class = shift; 232 my $class = shift;
232 233
233 my $self = bless { 234 my $self = bless {
238 h => undef, 239 h => undef,
239 can_events => 1, 240 can_events => 1,
240 @_ 241 @_
241 }, $class; 242 }, $class;
242 243
243 dc::weaken ($dc::UI::WIDGET{$self+0} = $self); 244 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
244 245
245 for (keys %$self) { 246 for (keys %$self) {
246 if (/^on_(.*)$/) { 247 if (/^on_(.*)$/) {
247 $self->connect ($1 => delete $self->{$_}); 248 $self->connect ($1 => delete $self->{$_});
248 } 249 }
249 } 250 }
250 251
251 if (my $layout = $dc::UI::LAYOUT->{$self->{name}}) { 252 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
252 $self->{x} = $layout->{x} * $dc::UI::ROOT->{alloc_w} if exists $layout->{x}; 253 $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}; 254 $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}; 255 $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}; 256 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
256 257
257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 258 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 259 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
259 260
260 $self->show if $layout->{show}; 261 $self->show if $layout->{show};
278sub show { 279sub show {
279 my ($self) = @_; 280 my ($self) = @_;
280 281
281 return if $self->{parent}; 282 return if $self->{parent};
282 283
283 $dc::UI::ROOT->add ($self); 284 $DC::UI::ROOT->add ($self);
284} 285}
285 286
286sub set_visible { 287sub set_visible {
287 my ($self) = @_; 288 my ($self) = @_;
288 289
289 return if $self->{visible}; 290 return if $self->{visible};
291
292 $self->{parent} && $self->{parent}{root}#d#
293 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
290 294
291 $self->{root} = $self->{parent}{root}; 295 $self->{root} = $self->{parent}{root};
292 $self->{visible} = $self->{parent}{visible} + 1; 296 $self->{visible} = $self->{parent}{visible} + 1;
293 297
294 $self->emit (visibility_change => 1); 298 $self->emit (visibility_change => 1);
295 299
296 $self->realloc if !exists $self->{req_w}; 300 $self->realloc if !exists $self->{req_w};
297 301
298 $_->set_visible for $self->children; 302 $_->set_visible for $self->visible_children;
299} 303}
300 304
301sub set_invisible { 305sub set_invisible {
302 my ($self) = @_; 306 my ($self) = @_;
303 307
309 delete $self->{root}; 313 delete $self->{root};
310 314
311 undef $GRAB if $GRAB == $self; 315 undef $GRAB if $GRAB == $self;
312 undef $HOVER if $HOVER == $self; 316 undef $HOVER if $HOVER == $self;
313 317
314 $dc::UI::TOOLTIP_WATCHER->invoke 318 $DC::UI::TOOLTIP_WATCHER->invoke
315 if $TOOLTIP->{owner} == $self; 319 if $TOOLTIP->{owner} == $self;
316 320
317 $self->emit ("focus_out"); 321 $self->emit ("focus_out");
318 $self->emit (visibility_change => 0); 322 $self->emit (visibility_change => 0);
319} 323}
368 372
369# traverse the widget chain up to find the maximum "physical" size constraints 373# traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh { 374sub get_max_wh {
371 my ($self) = @_; 375 my ($self) = @_;
372 376
377 my ($w, $h) = @$self{qw(max_w max_h)};
378
379 if ($w <= 0 || $h <= 0) {
380 my ($mw, $mh) = $self->{parent}
373 return $self->{parent}->get_max_wh 381 ? $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT) 382 : ($::WIDTH, $::HEIGHT);
383
384 $w = $mw if $w <= 0;
385 $h = $mh if $h <= 0;
386 }
387
388 ($w, $h)
377} 389}
378 390
379sub size_request { 391sub size_request {
380 require Carp; 392 require Carp;
381 Carp::confess "size_request is abstract"; 393 Carp::confess "size_request is abstract";
389 my ($self, $x, $y, $w, $h) = @_; 401 my ($self, $x, $y, $w, $h) = @_;
390 402
391 if ($self->{aspect}) { 403 if ($self->{aspect}) {
392 my ($ow, $oh) = ($w, $h); 404 my ($ow, $oh) = ($w, $h);
393 405
394 $w = List::Util::min $w, dc::ceil $h * $self->{aspect}; 406 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
395 $h = List::Util::min $h, dc::ceil $w / $self->{aspect}; 407 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
396 408
397 # use alignment to adjust x, y 409 # use alignment to adjust x, y
398 410
399 $x += int 0.5 * ($ow - $w); 411 $x += int 0.5 * ($ow - $w);
400 $y += int 0.5 * ($oh - $h); 412 $y += int 0.5 * ($oh - $h);
441 453
442 return if $self->{tooltip} eq $tooltip; 454 return if $self->{tooltip} eq $tooltip;
443 455
444 $self->{tooltip} = $tooltip; 456 $self->{tooltip} = $tooltip;
445 457
446 if ($dc::UI::TOOLTIP->{owner} == $self) { 458 if ($DC::UI::TOOLTIP->{owner} == $self) {
447 delete $dc::UI::TOOLTIP->{owner}; 459 delete $DC::UI::TOOLTIP->{owner};
448 $dc::UI::TOOLTIP_WATCHER->invoke; 460 $DC::UI::TOOLTIP_WATCHER->invoke;
449 } 461 }
450} 462}
451 463
452# translate global coordinates to local coordinate system 464# translate global coordinates to local coordinate system
453sub coord2local { 465sub coord2local {
519sub connect { 531sub connect {
520 my ($self, $signal, $cb) = @_; 532 my ($self, $signal, $cb) = @_;
521 533
522 push @{ $self->{signal_cb}{$signal} }, $cb; 534 push @{ $self->{signal_cb}{$signal} }, $cb;
523 535
524 defined wantarray and dc::guard { 536 defined wantarray and Guard::guard {
525 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 537 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
526 @{ $self->{signal_cb}{$signal} }; 538 @{ $self->{signal_cb}{$signal} };
527 } 539 }
528} 540}
529 541
567# in .xs 579# in .xs
568 580
569sub set_parent { 581sub set_parent {
570 my ($self, $parent) = @_; 582 my ($self, $parent) = @_;
571 583
572 dc::weaken ($self->{parent} = $parent); 584 DC::weaken ($self->{parent} = $parent);
573 $self->set_visible if $parent->{visible}; 585 $self->set_visible if $parent->{visible};
574} 586}
575 587
576sub realloc { 588sub realloc {
577 my ($self) = @_; 589 my ($self) = @_;
615} 627}
616 628
617sub DESTROY { 629sub DESTROY {
618 my ($self) = @_; 630 my ($self) = @_;
619 631
620 return if dc::in_destruct; 632 return if DC::in_destruct;
621 633
622 local $@; 634 local $@;
623 eval { $self->destroy }; 635 eval { $self->destroy };
624 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 636 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
625 637
626 delete $WIDGET{$self+0}; 638 delete $WIDGET{$self+0};
627} 639}
628 640
629############################################################################# 641#############################################################################
630 642
631package dc::UI::DrawBG; 643package DC::UI::DrawBG;
632 644
633our @ISA = dc::UI::Base::; 645our @ISA = DC::UI::Base::;
634 646
635use strict; 647use strict;
636use dc::OpenGL; 648use DC::OpenGL;
637 649
638sub new { 650sub new {
639 my $class = shift; 651 my $class = shift;
640 652
641 $class->SUPER::new ( 653 $class->SUPER::new (
643 #active_bg => [1, 1, 1, 0.5], 655 #active_bg => [1, 1, 1, 0.5],
644 @_ 656 @_
645 ) 657 )
646} 658}
647 659
660sub set_bg {
661 my ($self, $bg) = @_;
662
663 $self->{bg} = $bg;
664 $self->update;
665}
666
648sub _draw { 667sub _draw {
649 my ($self) = @_; 668 my ($self) = @_;
650 669
651 my $color = $FOCUS == $self && $self->{active_bg} 670 my $color = $FOCUS == $self
652 ? $self->{active_bg} 671 ? $self->{active_bg} || $self->{bg}
653 : $self->{bg}; 672 : $self->{bg};
654 673
655 if ($color && (@$color < 4 || $color->[3])) { 674 if ($color && (@$color < 4 || $color->[3])) {
656 my ($w, $h) = @$self{qw(w h)}; 675 my ($w, $h) = @$self{qw(w h)};
657 676
663 } 682 }
664} 683}
665 684
666############################################################################# 685#############################################################################
667 686
668package dc::UI::Empty; 687package DC::UI::Empty;
669 688
670our @ISA = dc::UI::Base::; 689our @ISA = DC::UI::Base::;
671 690
672sub new { 691sub new {
673 my ($class, %arg) = @_; 692 my ($class, %arg) = @_;
674 $class->SUPER::new (can_events => 0, %arg); 693 $class->SUPER::new (can_events => 0, %arg);
675} 694}
682 701
683sub draw { } 702sub draw { }
684 703
685############################################################################# 704#############################################################################
686 705
687package dc::UI::Container; 706package DC::UI::Container;
688 707
689our @ISA = dc::UI::Base::; 708our @ISA = DC::UI::Base::;
690 709
691sub new { 710sub new {
692 my ($class, %arg) = @_; 711 my ($class, %arg) = @_;
693 712
694 my $children = delete $arg{children}; 713 my $children = delete $arg{children};
788 $_->draw for $self->visible_children; 807 $_->draw for $self->visible_children;
789} 808}
790 809
791############################################################################# 810#############################################################################
792 811
793package dc::UI::Bin; 812package DC::UI::Bin;
794 813
795our @ISA = dc::UI::Container::; 814our @ISA = DC::UI::Container::;
796 815
797sub new { 816sub new {
798 my ($class, %arg) = @_; 817 my ($class, %arg) = @_;
799 818
800 my $child = (delete $arg{child}) || new dc::UI::Empty::; 819 my $child = (delete $arg{child}) || new DC::UI::Empty::;
801 820
802 $class->SUPER::new (children => [$child], %arg) 821 $class->SUPER::new (children => [$child], %arg)
803} 822}
804 823
805sub add { 824sub add {
812sub remove { 831sub remove {
813 my ($self, $widget) = @_; 832 my ($self, $widget) = @_;
814 833
815 $self->SUPER::remove ($widget); 834 $self->SUPER::remove ($widget);
816 835
817 $self->{children} = [new dc::UI::Empty] 836 $self->{children} = [new DC::UI::Empty]
818 unless @{$self->{children}}; 837 unless @{$self->{children}};
819} 838}
820 839
821sub child { $_[0]->{children}[0] } 840sub child { $_[0]->{children}[0] }
822 841
833} 852}
834 853
835############################################################################# 854#############################################################################
836# back-buffered drawing area 855# back-buffered drawing area
837 856
838package dc::UI::Window; 857package DC::UI::Window;
839 858
840our @ISA = dc::UI::Bin::; 859our @ISA = DC::UI::Bin::;
841 860
842use dc::OpenGL; 861use DC::OpenGL;
843 862
844sub new { 863sub new {
845 my ($class, %arg) = @_; 864 my ($class, %arg) = @_;
846 865
847 my $self = $class->SUPER::new (%arg); 866 my $self = $class->SUPER::new (%arg);
869} 888}
870 889
871sub render_child { 890sub render_child {
872 my ($self) = @_; 891 my ($self) = @_;
873 892
874 $self->{texture} = new_from_opengl dc::Texture $self->{w}, $self->{h}, sub { 893 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
875 glClearColor 0, 0, 0, 0; 894 glClearColor 0, 0, 0, 0;
876 glClear GL_COLOR_BUFFER_BIT; 895 glClear GL_COLOR_BUFFER_BIT;
877 896
878 { 897 {
879 package dc::UI::Base; 898 package DC::UI::Base;
880 899
881 local ($draw_x, $draw_y, $draw_w, $draw_h) = 900 local ($draw_x, $draw_y, $draw_w, $draw_h) =
882 (0, 0, $self->{w}, $self->{h}); 901 (0, 0, $self->{w}, $self->{h});
883 902
884 $self->_render; 903 $self->_render;
901 glDisable GL_TEXTURE_2D; 920 glDisable GL_TEXTURE_2D;
902} 921}
903 922
904############################################################################# 923#############################################################################
905 924
906package dc::UI::ViewPort; 925package DC::UI::ViewPort;
907 926
908use List::Util qw(min max); 927use List::Util qw(min max);
909 928
910our @ISA = dc::UI::Window::; 929our @ISA = DC::UI::Window::;
911 930
912sub new { 931sub new {
913 my $class = shift; 932 my $class = shift;
914 933
915 $class->SUPER::new ( 934 $class->SUPER::new (
999 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1018 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1000 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1019 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1001 ) { 1020 ) {
1002 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1021 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1003 } else { 1022 } else {
1004 $self->dc::UI::Base::find_widget ($x, $y) 1023 $self->DC::UI::Base::find_widget ($x, $y)
1005 } 1024 }
1006} 1025}
1007 1026
1008sub _render { 1027sub _render {
1009 my ($self) = @_; 1028 my ($self) = @_;
1010 1029
1011 local $dc::UI::Base::draw_x = $dc::UI::Base::draw_x - $self->{view_x}; 1030 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}; 1031 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1013 1032
1014 dc::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1033 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1015 1034
1016 $self->SUPER::_render; 1035 $self->SUPER::_render;
1017} 1036}
1018 1037
1019############################################################################# 1038#############################################################################
1020 1039
1021package dc::UI::ScrolledWindow; 1040package DC::UI::ScrolledWindow;
1022 1041
1023our @ISA = dc::UI::Table::; 1042our @ISA = DC::UI::Table::;
1024 1043
1025sub new { 1044sub new {
1026 my ($class, %arg) = @_; 1045 my ($class, %arg) = @_;
1027 1046
1028 my $child = delete $arg{child}; 1047 my $child = delete $arg{child};
1029 1048
1030 my $self; 1049 my $self;
1031 1050
1032 my $hslider = new dc::UI::Slider 1051 my $hslider = new DC::UI::Slider
1033 c_col => 0, 1052 c_col => 0,
1034 c_row => 1, 1053 c_row => 1,
1035 vertical => 0, 1054 vertical => 0,
1036 range => [0, 0, 1, 0.01], # HACK fix 1055 range => [0, 0, 1, 0.01], # HACK fix
1037 on_changed => sub { 1056 on_changed => sub {
1038 $self->{hpos} = $_[1]; 1057 $self->{hpos} = $_[1];
1039 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1058 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1040 }, 1059 },
1041 ; 1060 ;
1042 1061
1043 my $vslider = new dc::UI::Slider 1062 my $vslider = new DC::UI::Slider
1044 c_col => 1, 1063 c_col => 1,
1045 c_row => 0, 1064 c_row => 0,
1046 vertical => 1, 1065 vertical => 1,
1047 range => [0, 0, 1, 0.01], # HACK fix 1066 range => [0, 0, 1, 0.01], # HACK fix
1048 on_changed => sub { 1067 on_changed => sub {
1060 col_expand => [1, 0], 1079 col_expand => [1, 0],
1061 row_expand => [1, 0], 1080 row_expand => [1, 0],
1062 %arg, 1081 %arg,
1063 ); 1082 );
1064 1083
1065 $self->{vp} = new dc::UI::ViewPort 1084 $self->{vp} = new DC::UI::ViewPort
1066 c_col => 0, 1085 c_col => 0,
1067 c_row => 0, 1086 c_row => 0,
1068 expand => 1, 1087 expand => 1,
1069 scroll_x => $self->{scroll_x}, 1088 scroll_x => $self->{scroll_x},
1070 scroll_y => $self->{scroll_y}, 1089 scroll_y => $self->{scroll_y},
1196 $self->SUPER::invoke_size_allocate ($w, $h) 1215 $self->SUPER::invoke_size_allocate ($w, $h)
1197} 1216}
1198 1217
1199############################################################################# 1218#############################################################################
1200 1219
1201package dc::UI::Frame; 1220package DC::UI::Frame;
1202 1221
1203our @ISA = dc::UI::Bin::; 1222our @ISA = DC::UI::Bin::;
1204 1223
1205use dc::OpenGL; 1224use DC::OpenGL;
1206 1225
1207sub new { 1226sub new {
1208 my $class = shift; 1227 my $class = shift;
1209 1228
1210 $class->SUPER::new ( 1229 $class->SUPER::new (
1229 $self->SUPER::_draw; 1248 $self->SUPER::_draw;
1230} 1249}
1231 1250
1232############################################################################# 1251#############################################################################
1233 1252
1234package dc::UI::FancyFrame; 1253package DC::UI::FancyFrame;
1235 1254
1236our @ISA = dc::UI::Bin::; 1255our @ISA = DC::UI::Bin::;
1237 1256
1238use dc::OpenGL; 1257use DC::OpenGL;
1239 1258
1240sub new { 1259sub new {
1241 my ($class, %arg) = @_; 1260 my ($class, %arg) = @_;
1242 1261
1243 if ((exists $arg{label}) && !ref $arg{label}) { 1262 if ((exists $arg{label}) && !ref $arg{label}) {
1244 $arg{label} = new dc::UI::Label 1263 $arg{label} = new DC::UI::Label
1245 align => 1, 1264 align => 1,
1246 valign => 0, 1265 valign => 0.5,
1247 text => $arg{label}, 1266 text => $arg{label},
1248 fontsize => ($arg{border} || 0.8) * 0.75; 1267 fontsize => ($arg{border} || 0.8) * 0.75;
1249 } 1268 }
1250 1269
1251 my $self = $class->SUPER::new ( 1270 my $self = $class->SUPER::new (
1252 # label => "", 1271 # label => "",
1253 fg => [0.6, 0.3, 0.1], 1272 fg => undef,
1254 border => 0.8, 1273 border => 0.8,
1255 style => 'single', 1274 style => 'single',
1256 %arg, 1275 %arg,
1257 ); 1276 );
1258 1277
1261 1280
1262sub add { 1281sub add {
1263 my ($self, @widgets) = @_; 1282 my ($self, @widgets) = @_;
1264 1283
1265 $self->SUPER::add (@widgets); 1284 $self->SUPER::add (@widgets);
1266 $self->dc::UI::Container::add ($self->{label}) if $self->{label}; 1285 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1267} 1286}
1268 1287
1269sub border { 1288sub border {
1270 int $_[0]{border} * $::FONTSIZE 1289 int $_[0]{border} * $::FONTSIZE
1271} 1290}
1311 my $border = $self->border; 1330 my $border = $self->border;
1312 my ($w, $h) = ($self->{w}, $self->{h}); 1331 my ($w, $h) = ($self->{w}, $self->{h});
1313 1332
1314 $child->draw; 1333 $child->draw;
1315 1334
1316 glColor @{$self->{fg}}; 1335 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1317 glBegin GL_LINE_STRIP; 1336 glBegin GL_LINE_STRIP;
1318 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1337 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1319 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1338 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1320 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1339 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; 1340 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1329 } 1348 }
1330} 1349}
1331 1350
1332############################################################################# 1351#############################################################################
1333 1352
1334package dc::UI::Toplevel; 1353package DC::UI::Toplevel;
1335 1354
1336our @ISA = dc::UI::Bin::; 1355our @ISA = DC::UI::Bin::;
1337 1356
1338use dc::OpenGL; 1357use DC::OpenGL;
1339 1358
1340my $bg = 1359my $bg =
1341 new_from_file dc::Texture dc::find_rcfile "d1_bg.png", 1360 new_from_resource DC::Texture "d1_bg.png",
1342 mipmap => 1, wrap => 1; 1361 mipmap => 1, wrap => 1;
1343 1362
1344my @border = 1363my @border =
1345 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 1364 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); 1365 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1347 1366
1348my @icon = 1367my @icon =
1349 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 1368 map { new_from_resource DC::Texture $_, mipmap => 1 }
1350 qw(x1_move.png x1_resize.png); 1369 qw(x1_move.png x1_resize.png);
1351 1370
1352sub new { 1371sub new {
1353 my ($class, %arg) = @_; 1372 my ($class, %arg) = @_;
1354 1373
1355 my $self = $class->SUPER::new ( 1374 my $self = $class->SUPER::new (
1356 bg => [1, 1, 1, 1], 1375 bg => [1, 1, 1, 1],
1357 border_bg => [1, 1, 1, 1], 1376 border_bg => [1, 1, 1, 1],
1358 border => 0.6, 1377 border => 0.8,
1359 can_events => 1, 1378 can_events => 1,
1360 min_w => 64, 1379 min_w => 64,
1361 min_h => 32, 1380 min_h => 32,
1362 %arg, 1381 %arg,
1363 ); 1382 );
1364 1383
1365 $self->{title_widget} = new dc::UI::Label 1384 $self->{title_widget} = new DC::UI::Label
1366 align => 0, 1385 align => 0.5,
1367 valign => 1, 1386 valign => 1,
1368 text => $self->{title}, 1387 text => $self->{title},
1369 fontsize => $self->{border}, 1388 fontsize => $self->{border},
1370 if exists $self->{title}; 1389 if exists $self->{title};
1371 1390
1372 if ($self->{has_close_button}) { 1391 if ($self->{has_close_button}) {
1373 $self->{close_button} = 1392 $self->{close_button} =
1374 new dc::UI::ImageButton 1393 new DC::UI::ImageButton
1375 path => 'x1_close.png', 1394 path => 'x1_close.png',
1376 on_activate => sub { $self->emit ("delete") }; 1395 on_activate => sub { $self->emit ("delete") };
1377 1396
1378 $self->dc::UI::Container::add ($self->{close_button}); 1397 $self->DC::UI::Container::add ($self->{close_button});
1379 } 1398 }
1380 1399
1381 $self 1400 $self
1382} 1401}
1383 1402
1384sub add { 1403sub add {
1385 my ($self, @widgets) = @_; 1404 my ($self, @widgets) = @_;
1386 1405
1387 $self->SUPER::add (@widgets); 1406 $self->SUPER::add (@widgets);
1388 $self->dc::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1407 $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}; 1408 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1390} 1409}
1391 1410
1392sub border { 1411sub border {
1393 int $_[0]{border} * $::FONTSIZE 1412 int $_[0]{border} * $::FONTSIZE
1394} 1413}
1532 glEnable GL_TEXTURE_2D; 1551 glEnable GL_TEXTURE_2D;
1533 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1552 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1534 1553
1535 my $border = $self->border; 1554 my $border = $self->border;
1536 1555
1556 if ($border) {
1537 glColor @{ $self->{border_bg} }; 1557 glColor @{ $self->{border_bg} };
1538 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1558 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1539 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1559 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1540 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1560 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1541 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1561 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1542 1562
1543 # move 1563 # move
1544 my $w2 = ($w - $border) * .5; 1564 my $w2 = ($w - $border) * .5;
1545 my $h2 = ($h - $border) * .5; 1565 my $h2 = ($h - $border) * .5;
1546 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1566 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1547 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1567 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1548 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1568 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1549 1569
1550 # resize 1570 # resize
1551 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1571 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1552 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1572 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1553 unless $self->{has_close_button}; 1573 unless $self->{has_close_button};
1554 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1574 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1555 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1575 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1576 }
1556 1577
1557 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1578 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1558 glColor @{ $self->{bg} }; 1579 glColor @{ $self->{bg} };
1559 1580
1560 # TODO: repeat texture not scale 1581 # TODO: repeat texture not scale
1579 if $self->{close_button}; 1600 if $self->{close_button};
1580} 1601}
1581 1602
1582############################################################################# 1603#############################################################################
1583 1604
1584package dc::UI::Table; 1605package DC::UI::Table;
1585 1606
1586our @ISA = dc::UI::Container::; 1607our @ISA = DC::UI::Container::;
1587 1608
1588use List::Util qw(max sum); 1609use List::Util qw(max sum);
1589 1610
1590use dc::OpenGL; 1611use DC::OpenGL;
1591 1612
1592sub new { 1613sub new {
1593 my $class = shift; 1614 my $class = shift;
1594 1615
1595 $class->SUPER::new ( 1616 $class->SUPER::new (
1639 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1660 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1640 1661
1641 my $sw = sum @w[$c .. $c + $cs - 1]; 1662 my $sw = sum @w[$c .. $c + $cs - 1];
1642 1663
1643 if ($w > $sw) { 1664 if ($w > $sw) {
1644 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1665 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1645 } 1666 }
1646 } 1667 }
1647 1668
1648 # second pass, rows 1669 # second pass, rows
1649 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1670 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1650 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1671 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1651 1672
1652 my $sh = sum @h[$r .. $r + $rs - 1]; 1673 my $sh = sum @h[$r .. $r + $rs - 1];
1653 1674
1654 if ($h > $sh) { 1675 if ($h > $sh) {
1655 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1676 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1656 } 1677 }
1657 } 1678 }
1658 1679
1659 (\@w, \@h) 1680 (\@w, \@h)
1660} 1681}
1683 @col_expand = (1) x @$ws unless @col_expand; 1704 @col_expand = (1) x @$ws unless @col_expand;
1684 my $col_expand = (sum @col_expand) || 1; 1705 my $col_expand = (sum @col_expand) || 1;
1685 1706
1686 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1707 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1687 1708
1688 dc::UI::harmonize $ws; 1709 DC::UI::harmonize $ws;
1689 1710
1690 my @row_expand = @{$self->{row_expand}}; 1711 my @row_expand = @{$self->{row_expand}};
1691 @row_expand = (1) x @$ws unless @row_expand; 1712 @row_expand = (1) x @$ws unless @row_expand;
1692 my $row_expand = (sum @row_expand) || 1; 1713 my $row_expand = (sum @row_expand) || 1;
1693 1714
1694 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1715 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1695 1716
1696 dc::UI::harmonize $hs; 1717 DC::UI::harmonize $hs;
1697 1718
1698 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1719 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1699 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1720 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1700 1721
1701 for my $widget ($self->children) { 1722 for my $widget ($self->children) {
1710 1 1731 1
1711} 1732}
1712 1733
1713############################################################################# 1734#############################################################################
1714 1735
1715package dc::UI::Fixed; 1736package DC::UI::Fixed;
1716 1737
1717use List::Util qw(min max); 1738use List::Util qw(min max);
1718 1739
1719our @ISA = dc::UI::Container::; 1740our @ISA = DC::UI::Container::;
1720 1741
1721sub _scale($$$) { 1742sub _scale($$$) {
1722 my ($rel, $val, $max) = @_; 1743 my ($rel, $val, $max) = @_;
1723 1744
1724 $rel ? $val * $max : $val 1745 $rel ? $val * $max : $val
1776 1 1797 1
1777} 1798}
1778 1799
1779############################################################################# 1800#############################################################################
1780 1801
1781package dc::UI::Box; 1802package DC::UI::Box;
1782 1803
1783our @ISA = dc::UI::Container::; 1804our @ISA = DC::UI::Container::;
1784 1805
1785sub size_request { 1806sub size_request {
1786 my ($self) = @_; 1807 my ($self) = @_;
1787 1808
1788 my @children = $self->visible_children; 1809 my @children = $self->visible_children;
1823 $req[$_] += $space * $children[$_]{expand} 1844 $req[$_] += $space * $children[$_]{expand}
1824 for 0 .. $#children; 1845 for 0 .. $#children;
1825 } 1846 }
1826 } 1847 }
1827 1848
1828 dc::UI::harmonize \@req; 1849 DC::UI::harmonize \@req;
1829 1850
1830 my $pos = 0; 1851 my $pos = 0;
1831 for (0 .. $#children) { 1852 for (0 .. $#children) {
1832 my $alloc = $req[$_]; 1853 my $alloc = $req[$_];
1833 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1854 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1838 1 1859 1
1839} 1860}
1840 1861
1841############################################################################# 1862#############################################################################
1842 1863
1843package dc::UI::HBox; 1864package DC::UI::HBox;
1844 1865
1845our @ISA = dc::UI::Box::; 1866our @ISA = DC::UI::Box::;
1846 1867
1847sub new { 1868sub new {
1848 my $class = shift; 1869 my $class = shift;
1849 1870
1850 $class->SUPER::new ( 1871 $class->SUPER::new (
1853 ) 1874 )
1854} 1875}
1855 1876
1856############################################################################# 1877#############################################################################
1857 1878
1858package dc::UI::VBox; 1879package DC::UI::VBox;
1859 1880
1860our @ISA = dc::UI::Box::; 1881our @ISA = DC::UI::Box::;
1861 1882
1862sub new { 1883sub new {
1863 my $class = shift; 1884 my $class = shift;
1864 1885
1865 $class->SUPER::new ( 1886 $class->SUPER::new (
1868 ) 1889 )
1869} 1890}
1870 1891
1871############################################################################# 1892#############################################################################
1872 1893
1873package dc::UI::Label; 1894package DC::UI::Label;
1874 1895
1875our @ISA = dc::UI::DrawBG::; 1896our @ISA = DC::UI::DrawBG::;
1876 1897
1877use dc::OpenGL; 1898use DC::OpenGL;
1878 1899
1879sub new { 1900sub new {
1880 my ($class, %arg) = @_; 1901 my ($class, %arg) = @_;
1881 1902
1882 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1887 #text => initial text 1908 #text => initial text
1888 #markup => initial narkup 1909 #markup => initial narkup
1889 #max_w => maximum pixel width 1910 #max_w => maximum pixel width
1890 #style => 0, # render flags 1911 #style => 0, # render flags
1891 ellipsise => 3, # end 1912 ellipsise => 3, # end
1892 layout => (new dc::Layout), 1913 layout => (new DC::Layout),
1893 fontsize => 1, 1914 fontsize => 1,
1894 align => -1, 1915 align => 0.5,
1895 valign => -1, 1916 valign => 0.5,
1896 padding_x => 2, 1917 padding_x => 4,
1897 padding_y => 2, 1918 padding_y => 2,
1898 can_events => 0, 1919 can_events => 0,
1899 %arg 1920 %arg
1900 ); 1921 );
1901 1922
1902 if (exists $self->{template}) { 1923 if (exists $self->{template}) {
1903 my $layout = new dc::Layout; 1924 my $layout = new DC::Layout;
1904 $layout->set_text (delete $self->{template}); 1925 $layout->set_text (delete $self->{template});
1905 $self->{template} = $layout; 1926 $self->{template} = $layout;
1906 } 1927 }
1907 1928
1908 if (exists $self->{markup}) { 1929 if (exists $self->{markup}) {
1967 1988
1968 $self->{size_req} ||= do { 1989 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh; 1990 my ($max_w, $max_h) = $self->get_max_wh;
1970 1991
1971 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1992 $self->{layout}->set_font ($self->{font}) if $self->{font};
1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1); 1993 $self->{layout}->set_width ($max_w);
1973 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1994 $self->{layout}->set_ellipsise ($self->{ellipsise});
1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1995 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1996 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1976 1997
1977 my ($w, $h) = $self->{layout}->size; 1998 my ($w, $h) = $self->{layout}->size;
1978 1999
1979 if (exists $self->{template}) { 2000 if (exists $self->{template}) {
1980 $self->{template}->set_font ($self->{font}) if $self->{font}; 2001 $self->{template}->set_font ($self->{font}) if $self->{font};
1981 $self->{template}->set_width ($self->{max_w} || -1); 2002 $self->{template}->set_width ($max_w);
1982 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 2003 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1983 2004
1984 my ($w2, $h2) = $self->{template}->size; 2005 my ($w2, $h2) = $self->{template}->size;
1985 2006
1986 $w = List::Util::max $w, $w2; 2007 $w = List::Util::max $w, $w2;
2042 2063
2043 [$self->{layout}->size] 2064 [$self->{layout}->size]
2044 }; 2065 };
2045 2066
2046 unless (exists $self->{ox}) { 2067 unless (exists $self->{ox}) {
2047 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2068 $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} 2069 $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 2070
2055 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2071 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2056 }; 2072 };
2057 2073
2058# unless ($self->{list}) { 2074# unless ($self->{list}) {
2059# $self->{list} = dc::OpenGL::glGenList; 2075# $self->{list} = DC::OpenGL::glGenList;
2060# dc::OpenGL::glNewList $self->{list}; 2076# DC::OpenGL::glNewList $self->{list};
2061# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2077# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2062# dc::OpenGL::glEndList; 2078# DC::OpenGL::glEndList;
2063# } 2079# }
2064# 2080#
2065# dc::OpenGL::glCallList $self->{list}; 2081# DC::OpenGL::glCallList $self->{list};
2066 2082
2067 $self->{layout}->draw; 2083 $self->{layout}->draw;
2068} 2084}
2069 2085
2070#sub destroy { 2086#sub destroy {
2071# my ($self) = @_; 2087# my ($self) = @_;
2072# 2088#
2073# dc::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2089# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2074# 2090#
2075# $self->SUPER::destroy; 2091# $self->SUPER::destroy;
2076#} 2092#}
2077 2093
2078############################################################################# 2094#############################################################################
2079 2095
2080package dc::UI::EntryBase; 2096package DC::UI::EntryBase;
2081 2097
2082our @ISA = dc::UI::Label::; 2098our @ISA = DC::UI::Label::;
2083 2099
2084use dc::OpenGL; 2100use DC::OpenGL;
2085 2101
2086sub new { 2102sub new {
2087 my $class = shift; 2103 my $class = shift;
2088 2104
2089 $class->SUPER::new ( 2105 $class->SUPER::new (
2090 fg => [1, 1, 1], 2106 fg => [1, 1, 1],
2091 bg => [0, 0, 0, 0.2], 2107 bg => [0, 0, 0, 0.2],
2092 outline => [0.6, 0.3, 0.1], 2108 outline => undef,
2093 active_bg => [0, 0, 1, .2], 2109 active_bg => [0, 0, 1, .2],
2094 active_fg => [1, 1, 1], 2110 active_fg => [1, 1, 1],
2095 active_outline => [1, 1, 0], 2111 active_outline => [1, 1, 0],
2096 can_hover => 1, 2112 can_hover => 1,
2097 can_focus => 1, 2113 can_focus => 1,
2114 align => 0,
2098 valign => 0, 2115 valign => 0.5,
2099 can_events => 1, 2116 can_events => 1,
2100 ellipsise => 0, 2117 ellipsise => 0,
2118 padding_x => 4,
2119 padding_y => 2,
2101 #text => ... 2120 #text => ...
2102 #hidden => "*", 2121 #hidden => "*",
2103 @_ 2122 @_
2104 ) 2123 )
2105} 2124}
2152 2171
2153 my $text = $self->get_text; 2172 my $text = $self->get_text;
2154 2173
2155 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2174 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2156 2175
2157 if ($uni == 8) { 2176 if ($sym == DC::SDLK_BACKSPACE) {
2158 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2177 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2159 } elsif ($uni == 127) { 2178 } elsif ($sym == DC::SDLK_DELETE) {
2160 substr $text, $self->{cursor}, 1, ""; 2179 substr $text, $self->{cursor}, 1, "";
2161 } elsif ($sym == dc::SDLK_LEFT) { 2180 } elsif ($sym == DC::SDLK_LEFT) {
2162 --$self->{cursor} if $self->{cursor}; 2181 --$self->{cursor} if $self->{cursor};
2163 } elsif ($sym == dc::SDLK_RIGHT) { 2182 } elsif ($sym == DC::SDLK_RIGHT) {
2164 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2183 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2165 } elsif ($sym == dc::SDLK_HOME) { 2184 } elsif ($sym == DC::SDLK_HOME) {
2166 # what a hack 2185 # what a hack
2167 $self->{cursor} = 2186 $self->{cursor} =
2168 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2187 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2169 ? length $1 2188 ? length $1
2170 : 0; 2189 : 0;
2171 } elsif ($sym == dc::SDLK_END) { 2190 } elsif ($sym == DC::SDLK_END) {
2172 # uh, again 2191 # uh, again
2173 $self->{cursor} = 2192 $self->{cursor} =
2174 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2193 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2175 ? $self->{cursor} + length $1 2194 ? $self->{cursor} + length $1
2176 : length $self->{text}; 2195 : length $self->{text};
2267 glColor @{$self->{active_outline}}; 2286 glColor @{$self->{active_outline}};
2268 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2287 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2269 glLineWidth 1; 2288 glLineWidth 1;
2270 2289
2271 } else { 2290 } else {
2272 glColor @{$self->{outline}}; 2291 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2273 glBegin GL_LINE_STRIP; 2292 glBegin GL_LINE_STRIP;
2274 glVertex .5, $self->{h} * .5; 2293 glVertex .5, $self->{h} * .5;
2275 glVertex .5, $self->{h} - 2.5; 2294 glVertex .5, $self->{h} - 2.5;
2276 glVertex $self->{w} - .5, $self->{h} - 2.5; 2295 glVertex $self->{w} - .5, $self->{h} - 2.5;
2277 glVertex $self->{w} - .5, $self->{h} * .5; 2296 glVertex $self->{w} - .5, $self->{h} * .5;
2279 } 2298 }
2280} 2299}
2281 2300
2282############################################################################# 2301#############################################################################
2283 2302
2284package dc::UI::Entry; 2303package DC::UI::Entry;
2285 2304
2286our @ISA = dc::UI::EntryBase::; 2305our @ISA = DC::UI::EntryBase::;
2287 2306
2288use dc::OpenGL; 2307use DC::OpenGL;
2308
2309sub new {
2310 my $class = shift;
2311
2312 $class->SUPER::new (
2313 history_pointer => -1,
2314 @_
2315 )
2316}
2317
2289 2318
2290sub invoke_key_down { 2319sub invoke_key_down {
2291 my ($self, $ev) = @_; 2320 my ($self, $ev) = @_;
2292 2321
2293 my $sym = $ev->{sym}; 2322 my $sym = $ev->{sym};
2299 $self->{history_pointer} = -1; 2328 $self->{history_pointer} = -1;
2300 $self->{history_saveback} = ''; 2329 $self->{history_saveback} = '';
2301 $self->emit (activate => $txt); 2330 $self->emit (activate => $txt);
2302 $self->update; 2331 $self->update;
2303 2332
2304 } elsif ($sym == dc::SDLK_UP) { 2333 } elsif ($sym == DC::SDLK_UP) {
2305 if ($self->{history_pointer} < 0) { 2334 if ($self->{history_pointer} < 0) {
2306 $self->{history_saveback} = $self->get_text; 2335 $self->{history_saveback} = $self->get_text;
2307 } 2336 }
2308 if (@{$self->{history} || []} > 0) { 2337 if (@{$self->{history} || []} > 0) {
2309 $self->{history_pointer}++; 2338 $self->{history_pointer}++;
2311 $self->{history_pointer} = @{$self->{history} || []} - 1; 2340 $self->{history_pointer} = @{$self->{history} || []} - 1;
2312 } 2341 }
2313 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2314 } 2343 }
2315 2344
2316 } elsif ($sym == dc::SDLK_DOWN) { 2345 } elsif ($sym == DC::SDLK_DOWN) {
2317 $self->{history_pointer}--; 2346 $self->{history_pointer}--;
2318 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2347 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2319 2348
2320 if ($self->{history_pointer} >= 0) { 2349 if ($self->{history_pointer} >= 0) {
2321 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2350 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2322 } else { 2351 } else {
2352 if (defined $self->{history_saveback}) {
2323 $self->set_text ($self->{history_saveback}); 2353 $self->set_text ($self->{history_saveback});
2354 $self->{history_saveback} = undef;
2355 }
2324 } 2356 }
2325 2357
2326 } else { 2358 } else {
2327 return $self->SUPER::invoke_key_down ($ev) 2359 return $self->SUPER::invoke_key_down ($ev)
2328 } 2360 }
2330 1 2362 1
2331} 2363}
2332 2364
2333############################################################################# 2365#############################################################################
2334 2366
2335package dc::UI::TextEdit; 2367package DC::UI::TextEdit;
2336 2368
2337our @ISA = dc::UI::EntryBase::; 2369our @ISA = DC::UI::EntryBase::;
2338 2370
2339use dc::OpenGL; 2371use DC::OpenGL;
2372
2373sub new {
2374 my $class = shift;
2375
2376 $class->SUPER::new (
2377 padding_y => 4,
2378
2379 @_
2380 )
2381}
2340 2382
2341sub move_cursor_ver { 2383sub move_cursor_ver {
2342 my ($self, $dy) = @_; 2384 my ($self, $dy) = @_;
2343 2385
2344 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2386 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2345 2387
2346 $y += $dy; 2388 $line += $dy;
2347 2389
2348 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2390 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2349 $self->{cursor} = $index; 2391 $self->{cursor} = $index;
2350 delete $self->{cur_h}; 2392 delete $self->{cur_h};
2351 $self->update; 2393 $self->update;
2352 return; 2394 return;
2353 } 2395 }
2356sub invoke_key_down { 2398sub invoke_key_down {
2357 my ($self, $ev) = @_; 2399 my ($self, $ev) = @_;
2358 2400
2359 my $sym = $ev->{sym}; 2401 my $sym = $ev->{sym};
2360 2402
2361 if ($sym == dc::SDLK_UP) { 2403 if ($sym == DC::SDLK_UP) {
2362 $self->move_cursor_ver (-1); 2404 $self->move_cursor_ver (-1);
2363 } elsif ($sym == dc::SDLK_DOWN) { 2405 } elsif ($sym == DC::SDLK_DOWN) {
2364 $self->move_cursor_ver (+1); 2406 $self->move_cursor_ver (+1);
2365 } else { 2407 } else {
2366 return $self->SUPER::invoke_key_down ($ev) 2408 return $self->SUPER::invoke_key_down ($ev)
2367 } 2409 }
2368 2410
2369 1 2411 1
2370} 2412}
2371 2413
2372############################################################################# 2414#############################################################################
2373 2415
2374package dc::UI::ButtonBin; 2416package DC::UI::ButtonBin;
2375 2417
2376our @ISA = dc::UI::Bin::; 2418our @ISA = DC::UI::Bin::;
2377 2419
2378use dc::OpenGL; 2420use DC::OpenGL;
2379 2421
2380my @tex = 2422my @tex =
2381 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2423 map { new_from_resource DC::Texture $_, mipmap => 1 }
2382 qw(b1_button_inactive.png b1_button_active.png); 2424 qw(b1_button_inactive.png b1_button_active.png);
2383 2425
2384sub new { 2426sub new {
2385 my $class = shift; 2427 my $class = shift;
2386 2428
2387 $class->SUPER::new ( 2429 $class->SUPER::new (
2388 can_hover => 1, 2430 can_hover => 1,
2389 align => 0, 2431 align => 0.5,
2390 valign => 0, 2432 valign => 0.5,
2391 can_events => 1, 2433 can_events => 1,
2392 @_ 2434 @_
2393 ) 2435 )
2394} 2436}
2395 2437
2418 $self->SUPER::_draw; 2460 $self->SUPER::_draw;
2419} 2461}
2420 2462
2421############################################################################# 2463#############################################################################
2422 2464
2423package dc::UI::Button; 2465package DC::UI::Button;
2424 2466
2425our @ISA = dc::UI::Label::; 2467our @ISA = DC::UI::Label::;
2426 2468
2427use dc::OpenGL; 2469use DC::OpenGL;
2428 2470
2429my @tex = 2471my @tex =
2430 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2472 map { new_from_resource DC::Texture $_, mipmap => 1 }
2431 qw(b1_button_inactive.png b1_button_active.png); 2473 qw(b1_button_inactive.png b1_button_active.png);
2432 2474
2433sub new { 2475sub new {
2434 my $class = shift; 2476 my $class = shift;
2435 2477
2436 $class->SUPER::new ( 2478 $class->SUPER::new (
2437 padding_x => 4, 2479 padding_x => 8,
2438 padding_y => 4, 2480 padding_y => 4,
2439 fg => [1.0, 1.0, 1.0], 2481 fg => [1.0, 1.0, 1.0],
2440 active_fg => [0.8, 0.8, 0.8], 2482 active_fg => [0.8, 0.8, 0.8],
2441 can_hover => 1, 2483 can_hover => 1,
2442 align => 0, 2484 align => 0.5,
2443 valign => 0, 2485 valign => 0.5,
2444 can_events => 1, 2486 can_events => 1,
2445 @_ 2487 @_
2446 ) 2488 )
2447} 2489}
2448 2490
2473 $self->SUPER::_draw; 2515 $self->SUPER::_draw;
2474} 2516}
2475 2517
2476############################################################################# 2518#############################################################################
2477 2519
2478package dc::UI::CheckBox; 2520package DC::UI::CheckBox;
2479 2521
2480our @ISA = dc::UI::DrawBG::; 2522our @ISA = DC::UI::DrawBG::;
2481 2523
2482my @tex = 2524my @tex =
2483 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2525 map { new_from_resource DC::Texture $_, mipmap => 1 }
2484 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2526 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2485 2527
2486use dc::OpenGL; 2528use DC::OpenGL;
2487 2529
2488sub new { 2530sub new {
2489 my $class = shift; 2531 my $class = shift;
2490 2532
2491 $class->SUPER::new ( 2533 $class->SUPER::new (
2534 fontsize => 1,
2492 padding_x => 2, 2535 padding_x => 2,
2493 padding_y => 2, 2536 padding_y => 2,
2494 fg => [1, 1, 1], 2537 fg => [1, 1, 1],
2495 active_fg => [1, 1, 0], 2538 active_fg => [1, 1, 0],
2496 bg => [0, 0, 0, 0.2], 2539 bg => [0, 0, 0, 0.2],
2502} 2545}
2503 2546
2504sub size_request { 2547sub size_request {
2505 my ($self) = @_; 2548 my ($self) = @_;
2506 2549
2507 (6) x 2 2550 ($self->{fontsize} * $::FONTSIZE) x 2
2508} 2551}
2509 2552
2510sub toggle { 2553sub toggle {
2511 my ($self) = @_; 2554 my ($self) = @_;
2512 2555
2548 glDisable GL_TEXTURE_2D; 2591 glDisable GL_TEXTURE_2D;
2549} 2592}
2550 2593
2551############################################################################# 2594#############################################################################
2552 2595
2553package dc::UI::Image; 2596package DC::UI::Image;
2554 2597
2555our @ISA = dc::UI::Base::; 2598our @ISA = DC::UI::DrawBG::;
2556 2599
2557use dc::OpenGL; 2600use DC::OpenGL;
2558 2601
2559our %texture_cache; 2602our %texture_cache;
2560 2603
2561sub new { 2604sub new {
2562 my $class = shift; 2605 my $class = shift;
2569 2612
2570 $self->{path} || $self->{tex} 2613 $self->{path} || $self->{tex}
2571 or Carp::croak "'path' or 'tex' attributes required"; 2614 or Carp::croak "'path' or 'tex' attributes required";
2572 2615
2573 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2616 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2574 new_from_file dc::Texture dc::find_rcfile $self->{path}, mipmap => 1; 2617 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2575 2618
2576 dc::weaken $texture_cache{$self->{path}}; 2619 DC::weaken $texture_cache{$self->{path}};
2577 2620
2578 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2621 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2579 2622
2580 $self 2623 $self
2581} 2624}
2582 2625
2583sub STORABLE_freeze { 2626sub STORABLE_freeze {
2584 my ($self, $cloning) = @_; 2627 my ($self, $cloning) = @_;
2585 2628
2586 $self->{path} 2629 $self->{path}
2587 or die "cannot serialise dc::UI::Image on non-loadable images\n"; 2630 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2588 2631
2589 $self->{path} 2632 $self->{path}
2590} 2633}
2591 2634
2592sub STORABLE_attach { 2635sub STORABLE_attach {
2593 my ($self, $cloning, $path) = @_; 2636 my ($self, $cloning, $path) = @_;
2594 2637
2595 $self->new (path => $path) 2638 $self->new (path => $path)
2596} 2639}
2597 2640
2641sub set_texture {
2642 my ($self, $tex) = @_;
2643
2644 $self->{tex} = $tex;
2645 $self->update;
2646}
2647
2598sub size_request { 2648sub size_request {
2599 my ($self) = @_; 2649 my ($self) = @_;
2600 2650
2601 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) 2651 (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale})
2602} 2652}
2603 2653
2604sub _draw { 2654sub _draw {
2605 my ($self) = @_; 2655 my ($self) = @_;
2656
2657 $self->SUPER::_draw;
2606 2658
2607 my $tex = $self->{tex}; 2659 my $tex = $self->{tex};
2608 2660
2609 my ($w, $h) = ($self->{w}, $self->{h}); 2661 my ($w, $h) = ($self->{w}, $self->{h});
2610 2662
2623 glDisable GL_TEXTURE_2D; 2675 glDisable GL_TEXTURE_2D;
2624} 2676}
2625 2677
2626############################################################################# 2678#############################################################################
2627 2679
2628package dc::UI::ImageButton; 2680package DC::UI::ImageButton;
2629 2681
2630our @ISA = dc::UI::Image::; 2682our @ISA = DC::UI::Image::;
2631 2683
2632use dc::OpenGL; 2684use DC::OpenGL;
2633
2634my %textures;
2635 2685
2636sub new { 2686sub new {
2637 my $class = shift; 2687 my $class = shift;
2638 2688
2639 my $self = $class->SUPER::new ( 2689 my $self = $class->SUPER::new (
2640 padding_x => 4, 2690 padding_x => 4,
2641 padding_y => 4, 2691 padding_y => 4,
2642 fg => [1, 1, 1], 2692 fg => [1, 1, 1],
2643 active_fg => [0, 0, 1], 2693 active_fg => [0, 0, 1],
2644 can_hover => 1, 2694 can_hover => 1,
2645 align => 0, 2695 align => 0.5,
2646 valign => 0, 2696 valign => 0.5,
2647 can_events => 1, 2697 can_events => 1,
2648 @_ 2698 @_
2649 ); 2699 );
2650} 2700}
2651 2701
2665 1 2715 1
2666} 2716}
2667 2717
2668############################################################################# 2718#############################################################################
2669 2719
2670package dc::UI::VGauge; 2720package DC::UI::VGauge;
2671 2721
2672our @ISA = dc::UI::Base::; 2722our @ISA = DC::UI::Base::;
2673 2723
2674use List::Util qw(min max); 2724use List::Util qw(min max);
2675 2725
2676use dc::OpenGL; 2726use DC::OpenGL;
2677 2727
2678my %tex = ( 2728my %tex = (
2679 food => [ 2729 food => [
2680 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2730 map { new_from_resource DC::Texture $_, mipmap => 1 }
2681 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2731 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2682 ], 2732 ],
2683 grace => [ 2733 grace => [
2684 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2734 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/ 2735 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2686 ], 2736 ],
2687 hp => [ 2737 hp => [
2688 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2738 map { new_from_resource DC::Texture $_, mipmap => 1 }
2689 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2739 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2690 ], 2740 ],
2691 mana => [ 2741 mana => [
2692 map { new_from_file dc::Texture dc::find_rcfile $_, mipmap => 1 } 2742 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/ 2743 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2694 ], 2744 ],
2695); 2745);
2696 2746
2697# eg. VGauge->new (gauge => 'food'), default gauge: food 2747# eg. VGauge->new (gauge => 'food'), default gauge: food
2800 glDisable GL_TEXTURE_2D; 2850 glDisable GL_TEXTURE_2D;
2801} 2851}
2802 2852
2803############################################################################# 2853#############################################################################
2804 2854
2805package dc::UI::Progress; 2855package DC::UI::Progress;
2806 2856
2807our @ISA = dc::UI::Label::; 2857our @ISA = DC::UI::Label::;
2808 2858
2809use dc::OpenGL; 2859use DC::OpenGL;
2810 2860
2811sub new { 2861sub new {
2812 my ($class, %arg) = @_; 2862 my ($class, %arg) = @_;
2813 2863
2814 my $self = $class->SUPER::new ( 2864 my $self = $class->SUPER::new (
2865 padding_x => 2,
2866 padding_y => 2,
2815 fg => [1, 1, 1], 2867 fg => [1, 1, 1],
2816 bg => [0, 0, 1, 0.2], 2868 bg => [0, 0, 1, 0.2],
2817 bar => [0.7, 0.5, 0.1, 0.8], 2869 bar => [0.7, 0.5, 0.1, 0.8],
2818 outline => [0.4, 0.3, 0], 2870 outline => [0.4, 0.3, 0],
2819 fontsize => 0.9, 2871 fontsize => 0.9,
2820 valign => 0, 2872 valign => 0.5,
2821 align => 0, 2873 align => 0.5,
2822 can_events => 1, 2874 can_events => 1,
2823 ellipsise => 1, 2875 ellipsise => 1,
2824 label => "%d%%", 2876 label => "%d%%",
2825 %arg, 2877 %arg,
2826 ); 2878 );
2834 my ($self, $label) = @_; 2886 my ($self, $label) = @_;
2835 2887
2836 return if $self->{label} eq $label; 2888 return if $self->{label} eq $label;
2837 $self->{label} = $label; 2889 $self->{label} = $label;
2838 2890
2839 $self->dc::UI::Progress::set_value (0 + delete $self->{value}); 2891 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2840} 2892}
2841 2893
2842sub set_value { 2894sub set_value {
2843 my ($self, $value) = @_; 2895 my ($self, $value) = @_;
2844 2896
2859 my ($self) = @_; 2911 my ($self) = @_;
2860 2912
2861 glEnable GL_BLEND; 2913 glEnable GL_BLEND;
2862 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2914 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2863 2915
2916 my $px = $self->{padding_x};
2917 my $py = $self->{padding_y};
2918
2864 if ($self->{value} >= 0) { 2919 if ($self->{value} >= 0) {
2865 my $s = int 2 + ($self->{w} - 4) * $self->{value}; 2920 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2866 2921
2867 glColor_premultiply @{$self->{bar}}; 2922 glColor_premultiply @{$self->{bar}};
2868 glRect 2, 2, $s, $self->{h} - 2; 2923 glRect $px, $py, $s, $self->{h} - $py;
2869 glColor_premultiply @{$self->{bg}}; 2924 glColor_premultiply @{$self->{bg}};
2870 glRect $s, 2, $self->{w} - 2, $self->{h} - 2; 2925 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2871 } 2926 }
2872 2927
2873 glColor_premultiply @{$self->{outline}}; 2928 glColor_premultiply @{$self->{outline}};
2929
2930 $px -= .5;
2931 $py -= .5;
2932
2874 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; 2933 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2875 2934
2876 glDisable GL_BLEND; 2935 glDisable GL_BLEND;
2877 2936
2878 { 2937 {
2879 local $self->{bg}; # do not draw background 2938 local $self->{bg}; # do not draw background
2881 } 2940 }
2882} 2941}
2883 2942
2884############################################################################# 2943#############################################################################
2885 2944
2886package dc::UI::ExperienceProgress; 2945package DC::UI::ExperienceProgress;
2887 2946
2888our @ISA = dc::UI::Progress::; 2947our @ISA = DC::UI::Progress::;
2889 2948
2890sub new { 2949sub new {
2891 my ($class, %arg) = @_; 2950 my ($class, %arg) = @_;
2892 2951
2952 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2953
2893 my $self = $class->SUPER::new ( 2954 my $self = $class->SUPER::new (
2955 %arg,
2894 tooltip => sub { 2956 tooltip => sub {
2895 my ($self) = @_; 2957 my ($self) = @_;
2896 2958
2897 sprintf "level %d\n%s points\n%s next level\n%s to go", 2959 sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done",
2960 $tt,
2898 $self->{lvl}, 2961 $self->{lvl},
2899 ::formsep ($self->{exp}), 2962 ::formsep ($self->{exp}),
2900 ::formsep ($self->{nxt}), 2963 ::formsep ($self->{nxt}),
2901 ::formsep ($self->{nxt} - $self->{exp}), 2964 ::formsep ($self->{nxt} - $self->{exp}),
2965 $self->_percent * 100,
2902 }, 2966 },
2903 %arg
2904 ); 2967 );
2905 2968
2906 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2969 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2907 if $::CONN; 2970 if $::CONN;
2908 2971
2916 if $::CONN; 2979 if $::CONN;
2917 2980
2918 $self->SUPER::DESTROY; 2981 $self->SUPER::DESTROY;
2919} 2982}
2920 2983
2984sub _percent {
2985 my ($self) = @_;
2986
2987 my $table = $::CONN && $::CONN->{exp_table}
2988 or return -1;
2989
2990 my $l0 = $table->[$self->{lvl} - 1];
2991 my $l1 = $table->[$self->{lvl}];
2992
2993 $self->{nxt} = $l1;
2994
2995 ($self->{exp} - $l0) / ($l1 - $l0)
2996}
2997
2921sub set_value { 2998sub set_value {
2922 my ($self, $lvl, $exp) = @_; 2999 my ($self, $lvl, $exp) = @_;
2923 3000
2924 $self->{lvl} = $lvl; 3001 $self->{lvl} = $lvl;
2925 $self->{exp} = $exp; 3002 $self->{exp} = $exp;
2926 3003
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); 3004 $self->SUPER::set_value ($self->_percent);
2939} 3005}
2940 3006
2941############################################################################# 3007#############################################################################
2942 3008
2943package dc::UI::Gauge; 3009package DC::UI::Gauge;
2944 3010
2945our @ISA = dc::UI::VBox::; 3011our @ISA = DC::UI::VBox::;
2946 3012
2947sub new { 3013sub new {
2948 my ($class, %arg) = @_; 3014 my ($class, %arg) = @_;
2949 3015
2950 my $self = $class->SUPER::new ( 3016 my $self = $class->SUPER::new (
2952 can_hover => 1, 3018 can_hover => 1,
2953 can_events => 1, 3019 can_events => 1,
2954 %arg, 3020 %arg,
2955 ); 3021 );
2956 3022
2957 $self->add ($self->{value} = new dc::UI::Label valign => +1, align => 0, template => "999"); 3023 $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); 3024 $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"); 3025 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2960 3026
2961 $self 3027 $self
2962} 3028}
2963 3029
2964sub set_fontsize { 3030sub set_fontsize {
2985 $self->{value}->set_text ($val); 3051 $self->{value}->set_text ($val);
2986} 3052}
2987 3053
2988############################################################################# 3054#############################################################################
2989 3055
2990package dc::UI::Slider; 3056package DC::UI::Slider;
2991 3057
2992use strict; 3058use strict;
2993 3059
2994use dc::OpenGL; 3060use DC::OpenGL;
2995 3061
2996our @ISA = dc::UI::DrawBG::; 3062our @ISA = DC::UI::DrawBG::;
2997 3063
2998my @tex = 3064my @tex =
2999 map { new_from_file dc::Texture dc::find_rcfile $_ } 3065 map { new_from_resource DC::Texture $_ }
3000 qw(s1_slider.png s1_slider_bg.png); 3066 qw(s1_slider.png s1_slider_bg.png);
3001 3067
3002sub new { 3068sub new {
3003 my $class = shift; 3069 my $class = shift;
3004 3070
3041sub set_value { 3107sub set_value {
3042 my ($self, $value) = @_; 3108 my ($self, $value) = @_;
3043 3109
3044 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3110 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3045 3111
3046 $hi = $lo + 1 if $hi <= $lo; 3112 $hi = $lo if $hi < $lo;
3047 3113
3048 $page = $hi - $lo if $page > $hi - $lo; 3114 $value = $hi - $page if $value > $hi - $page;
3049
3050 $value = $lo if $value < $lo; 3115 $value = $lo if $value < $lo;
3051 $value = $hi - $page if $value > $hi - $page;
3052 3116
3053 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3117 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3054 if $unit; 3118 if $unit;
3055 3119
3056 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3120 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3100sub invoke_mouse_wheel { 3164sub invoke_mouse_wheel {
3101 my ($self, $ev) = @_; 3165 my ($self, $ev) = @_;
3102 3166
3103 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3167 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3104 3168
3105 my $pagepart = $ev->{mod} & dc::KMOD_SHIFT ? 1 : 0.2; 3169 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3106 3170
3107 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3171 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3108 3172
3109 1 3173 1
3110} 3174}
3120 my ($self) = @_; 3184 my ($self) = @_;
3121 3185
3122 unless ($self->{knob_w}) { 3186 unless ($self->{knob_w}) {
3123 $self->set_value ($self->{range}[0]); 3187 $self->set_value ($self->{range}[0]);
3124 3188
3125 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3189 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3126 my $range = ($hi - $page - $lo) || 1e-100; 3190 my $range = ($hi - $page - $lo) || 1e-10;
3127 3191
3128 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3192 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3129 3193
3130 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3194 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3131 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3195 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3132 3196
3133 $value = ($value - $lo) / $range; 3197 $value = ($value - $lo) / $range;
3163 glDisable GL_TEXTURE_2D; 3227 glDisable GL_TEXTURE_2D;
3164} 3228}
3165 3229
3166############################################################################# 3230#############################################################################
3167 3231
3168package dc::UI::ValSlider; 3232package DC::UI::ValSlider;
3169 3233
3170our @ISA = dc::UI::HBox::; 3234our @ISA = DC::UI::HBox::;
3171 3235
3172sub new { 3236sub new {
3173 my ($class, %arg) = @_; 3237 my ($class, %arg) = @_;
3174 3238
3175 my $range = delete $arg{range}; 3239 my $range = delete $arg{range};
3176 3240
3177 my $self = $class->SUPER::new ( 3241 my $self = $class->SUPER::new (
3178 slider => (new dc::UI::Slider expand => 1, range => $range), 3242 slider => (new DC::UI::Slider expand => 1, range => $range),
3179 entry => (new dc::UI::Label text => "", template => delete $arg{template}), 3243 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3180 to_value => sub { shift }, 3244 to_value => sub { shift },
3181 from_value => sub { shift }, 3245 from_value => sub { shift },
3182 %arg, 3246 %arg,
3183 ); 3247 );
3184 3248
3204sub set_range { shift->{slider}->set_range (@_) } 3268sub set_range { shift->{slider}->set_range (@_) }
3205sub set_value { shift->{slider}->set_value (@_) } 3269sub set_value { shift->{slider}->set_value (@_) }
3206 3270
3207############################################################################# 3271#############################################################################
3208 3272
3209package dc::UI::TextScroller; 3273package DC::UI::TextScroller;
3210 3274
3211our @ISA = dc::UI::HBox::; 3275our @ISA = DC::UI::HBox::;
3212 3276
3213use dc::OpenGL; 3277use DC::OpenGL;
3214 3278
3215sub new { 3279sub new {
3216 my $class = shift; 3280 my $class = shift;
3217 3281
3218 my $self = $class->SUPER::new ( 3282 my $self = $class->SUPER::new (
3220 can_events => 1, 3284 can_events => 1,
3221 indent => 0, 3285 indent => 0,
3222 #font => default_font 3286 #font => default_font
3223 @_, 3287 @_,
3224 3288
3225 layout => (new dc::Layout), 3289 layout => (new DC::Layout),
3226 par => [], 3290 par => [],
3227 max_par => 0, 3291 max_par => 0,
3228 height => 0, 3292 height => 0,
3229 children => [ 3293 children => [
3230 (new dc::UI::Empty expand => 1), 3294 (new DC::UI::Empty expand => 1),
3231 (new dc::UI::Slider vertical => 1), 3295 (new DC::UI::Slider vertical => 1),
3232 ], 3296 ],
3233 ); 3297 );
3234 3298
3235 $self->{children}[1]->connect (changed => sub { $self->update }); 3299 $self->{children}[1]->connect (changed => sub { $self->update });
3236 3300
3292 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3356 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3293 $layout->set_markup ($para->{markup}); 3357 $layout->set_markup ($para->{markup});
3294 3358
3295 $layout->set_shapes ( 3359 $layout->set_shapes (
3296 map 3360 map
3297 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3361 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3298 @{$para->{widget}} 3362 @{$para->{widget}}
3299 ); 3363 );
3300 3364
3301 $layout 3365 $layout
3302} 3366}
3419 $ROOT->on_post_alloc ($self => sub { 3483 $ROOT->on_post_alloc ($self => sub {
3420 $self->force_uptodate; 3484 $self->force_uptodate;
3421 3485
3422 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3486 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3423 3487
3424 $self->{texture} ||= new_from_opengl dc::Texture $W, $H, sub { 3488 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3425 glClearColor 0, 0, 0, 0; 3489 glClearColor 0, 0, 0, 0;
3426 glClear GL_COLOR_BUFFER_BIT; 3490 glClear GL_COLOR_BUFFER_BIT;
3427 3491
3428 package dc::UI::Base; 3492 package DC::UI::Base;
3429 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3493 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3430 (0, 0, $self->{w}, $self->{h}); 3494 (0, 0, $self->{w}, $self->{h});
3431 3495
3432 my $top = int $self->{children}[1]{range}[0]; 3496 my $top = int $self->{children}[1]{range}[0];
3433 3497
3492 $self->{children}[1]->draw; 3556 $self->{children}[1]->draw;
3493} 3557}
3494 3558
3495############################################################################# 3559#############################################################################
3496 3560
3497package dc::UI::Animator; 3561package DC::UI::Animator;
3498 3562
3499use dc::OpenGL; 3563use DC::OpenGL;
3500 3564
3501our @ISA = dc::UI::Bin::; 3565our @ISA = DC::UI::Bin::;
3502 3566
3503sub moveto { 3567sub moveto {
3504 my ($self, $x, $y) = @_; 3568 my ($self, $x, $y) = @_;
3505 3569
3506 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3570 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3534 glPopMatrix; 3598 glPopMatrix;
3535} 3599}
3536 3600
3537############################################################################# 3601#############################################################################
3538 3602
3539package dc::UI::Flopper; 3603package DC::UI::Flopper;
3540 3604
3541our @ISA = dc::UI::Button::; 3605our @ISA = DC::UI::Button::;
3542 3606
3543sub new { 3607sub new {
3544 my $class = shift; 3608 my $class = shift;
3545 3609
3546 my $self = $class->SUPER::new ( 3610 my $self = $class->SUPER::new (
3558 $self->{other}->toggle_visibility; 3622 $self->{other}->toggle_visibility;
3559} 3623}
3560 3624
3561############################################################################# 3625#############################################################################
3562 3626
3563package dc::UI::Tooltip; 3627package DC::UI::Tooltip;
3564 3628
3565our @ISA = dc::UI::Bin::; 3629our @ISA = DC::UI::Bin::;
3566 3630
3567use dc::OpenGL; 3631use DC::OpenGL;
3568 3632
3569sub new { 3633sub new {
3570 my $class = shift; 3634 my $class = shift;
3571 3635
3572 $class->SUPER::new ( 3636 $class->SUPER::new (
3579 my ($self, $widget) = @_; 3643 my ($self, $widget) = @_;
3580 3644
3581 my $tip = $widget->{tooltip}; 3645 my $tip = $widget->{tooltip};
3582 $tip = $tip->($widget) if "CODE" eq ref $tip; 3646 $tip = $tip->($widget) if "CODE" eq ref $tip;
3583 3647
3584 $tip = dc::Pod::section_label tooltip => $1 3648 $tip = DC::Pod::section_label tooltip => $1
3585 if $tip =~ /^#(.*)$/; 3649 if $tip =~ /^#(.*)$/;
3586 3650
3587 if ($ENV{CFPLUS_DEBUG} & 2) { 3651 if ($ENV{CFPLUS_DEBUG} & 2) {
3588 $tip .= "\n\n" . (ref $widget) . "\n" 3652 $tip .= "\n\n" . (ref $widget) . "\n"
3589 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3653 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3592 } 3656 }
3593 3657
3594 $tip =~ s/^\n+//; 3658 $tip =~ s/^\n+//;
3595 $tip =~ s/\n+$//; 3659 $tip =~ s/\n+$//;
3596 3660
3597 $self->add (new dc::UI::Label 3661 $self->add (new DC::UI::Label
3662 fg => $DC::THEME{tooltip_fg},
3598 markup => $tip, 3663 markup => $tip,
3599 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3664 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3665 align => 0,
3600 fontsize => 0.8, 3666 fontsize => 0.8,
3601 style => 1, # FLAG_INVERSE 3667 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3602 ellipsise => 0, 3668 ellipsise => 0,
3603 font => ($widget->{tooltip_font} || $::FONT_PROP), 3669 font => ($widget->{tooltip_font} || $::FONT_PROP),
3604 ); 3670 );
3605} 3671}
3606 3672
3643sub _draw { 3709sub _draw {
3644 my ($self) = @_; 3710 my ($self) = @_;
3645 3711
3646 my ($w, $h) = @$self{qw(w h)}; 3712 my ($w, $h) = @$self{qw(w h)};
3647 3713
3648 glColor 1, 0.8, 0.4; 3714 glColor @{ $DC::THEME{tooltip_bg} };
3649 glRect 0, 0, $w, $h; 3715 glRect 0, 0, $w, $h;
3650 3716
3651 glColor 0, 0, 0; 3717 glColor @{ $DC::THEME{tooltip_border} };
3652 glRect_lineloop .5, .5, $w + .5, $h + .5; 3718 glRect_lineloop .5, .5, $w + .5, $h + .5;
3653 3719
3654 glTranslate 2, 2; 3720 glTranslate 2, 2;
3655 3721
3656 $self->SUPER::_draw; 3722 $self->SUPER::_draw;
3657} 3723}
3658 3724
3659############################################################################# 3725#############################################################################
3660 3726
3661package dc::UI::Face; 3727package DC::UI::Face;
3662 3728
3663our @ISA = dc::UI::DrawBG::; 3729our @ISA = DC::UI::DrawBG::;
3664 3730
3665use dc::OpenGL; 3731use DC::OpenGL;
3666 3732
3667sub new { 3733sub new {
3668 my $class = shift; 3734 my $class = shift;
3669 3735
3670 my $self = $class->SUPER::new ( 3736 my $self = $class->SUPER::new (
3673 aspect => 1, 3739 aspect => 1,
3674 can_events => 0, 3740 can_events => 0,
3675 @_, 3741 @_,
3676 ); 3742 );
3677 3743
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; 3744 $self->update_anim;
3698 }
3699 3745
3700 $self 3746 $self
3701} 3747}
3702 3748
3703sub update_timer { 3749sub update_timer {
3726 $tex->upload (sub { $self->reconfigure }); 3772 $tex->upload (sub { $self->reconfigure });
3727 } 3773 }
3728 } 3774 }
3729 } 3775 }
3730 } 3776 }
3777 }
3778}
3779
3780sub update_anim {
3781 my ($self) = @_;
3782
3783 if ($self->{anim} && $self->{animspeed}) {
3784 DC::weaken (my $widget = $self);
3785
3786 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3787 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3788 return unless $::CONN;
3789
3790 my $w = $widget
3791 or return;
3792
3793 ++$w->{frame};
3794 $w->update_face;
3795
3796 # somehow, $widget can go away
3797 $w->update;
3798 $w->update_timer;
3799 };
3800
3801 $self->update_face;
3802 $self->update_timer;
3803 } else {
3804 delete $self->{timer};
3731 } 3805 }
3732} 3806}
3733 3807
3734sub size_request { 3808sub size_request {
3735 my ($self) = @_; 3809 my ($self) = @_;
3757 return unless $self->{visible}; 3831 return unless $self->{visible};
3758 3832
3759 $self->SUPER::update; 3833 $self->SUPER::update;
3760} 3834}
3761 3835
3836sub set_face {
3837 my ($self, $face) = @_;
3838
3839 $self->{face} = $face;
3840 $self->reconfigure;
3841}
3842
3843sub set_anim {
3844 my ($self, $anim) = @_;
3845
3846 $self->{anim} = $anim;
3847 $self->update_anim;
3848}
3849
3850sub set_animspeed {
3851 my ($self, $animspeed) = @_;
3852
3853 $self->{animspeed} = $animspeed;
3854 $self->update_anim;
3855}
3856
3762sub invoke_visibility_change { 3857sub invoke_visibility_change {
3763 my ($self) = @_; 3858 my ($self) = @_;
3764 3859
3765 $self->update_timer; 3860 $self->update_timer;
3766 3861
3790 $self->SUPER::destroy; 3885 $self->SUPER::destroy;
3791} 3886}
3792 3887
3793############################################################################# 3888#############################################################################
3794 3889
3795package dc::UI::Buttonbar; 3890package DC::UI::Buttonbar;
3796 3891
3797our @ISA = dc::UI::HBox::; 3892our @ISA = DC::UI::HBox::;
3798 3893
3799# TODO: should actually wrap buttons and other goodies. 3894# TODO: should actually wrap buttons and other goodies.
3800 3895
3801############################################################################# 3896#############################################################################
3802 3897
3803package dc::UI::Menu; 3898package DC::UI::Menu;
3804 3899
3805our @ISA = dc::UI::Toplevel::; 3900our @ISA = DC::UI::Toplevel::;
3806 3901
3807use dc::OpenGL; 3902use DC::OpenGL;
3808 3903
3809sub new { 3904sub new {
3810 my $class = shift; 3905 my $class = shift;
3811 3906
3812 my $self = $class->SUPER::new ( 3907 my $self = $class->SUPER::new (
3813 items => [], 3908 items => [],
3814 z => 100, 3909 z => 100,
3815 @_, 3910 @_,
3816 ); 3911 );
3817 3912
3818 $self->add ($self->{vbox} = new dc::UI::VBox); 3913 $self->add ($self->{vbox} = new DC::UI::VBox);
3819 3914
3820 for my $item (@{ $self->{items} }) { 3915 for my $item (@{ $self->{items} }) {
3821 my ($widget, $cb, $tooltip) = @$item; 3916 my ($widget, $cb, $tooltip) = @$item;
3822 3917
3823 # handle various types of items, only text for now 3918 # handle various types of items, only text for now
3824 if (!ref $widget) { 3919 if (!ref $widget) {
3825 if ($widget =~ /\t/) { 3920 if ($widget =~ /\t/) {
3826 my ($left, $right) = split /\t/, $widget, 2; 3921 my ($left, $right) = split /\t/, $widget, 2;
3827 3922
3828 $widget = new dc::UI::HBox 3923 $widget = new DC::UI::HBox
3829 can_hover => 1, 3924 can_hover => 1,
3830 can_events => 1, 3925 can_events => 1,
3831 tooltip => $tooltip, 3926 tooltip => $tooltip,
3832 children => [ 3927 children => [
3833 (new dc::UI::Label markup => $left, expand => 1), 3928 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3834 (new dc::UI::Label markup => $right, align => +1), 3929 (new DC::UI::Label markup => $right, align => 1),
3835 ], 3930 ],
3836 ; 3931 ;
3837 3932
3838 } else { 3933 } else {
3839 $widget = new dc::UI::Label 3934 $widget = new DC::UI::Label
3840 can_hover => 1, 3935 can_hover => 1,
3841 can_events => 1, 3936 can_events => 1,
3937 align => 0,
3842 markup => $widget, 3938 markup => $widget,
3843 tooltip => $tooltip; 3939 tooltip => $tooltip;
3844 } 3940 }
3845 } 3941 }
3846 3942
3861 # maybe save $GRAB? must be careful about events... 3957 # maybe save $GRAB? must be careful about events...
3862 $GRAB = $self; 3958 $GRAB = $self;
3863 $self->{button} = $ev->{button}; 3959 $self->{button} = $ev->{button};
3864 3960
3865 $self->show; 3961 $self->show;
3866 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3962
3963 my $x = $ev->{x};
3964 my $y = $ev->{y};
3965
3966 $self->{root}->on_post_alloc ($self => sub {
3967 $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5);
3968 });
3969
3970 1 # so it can be used inside event handlers
3867} 3971}
3868 3972
3869sub invoke_mouse_motion { 3973sub invoke_mouse_motion {
3870 my ($self, $ev, $x, $y) = @_; 3974 my ($self, $ev, $x, $y) = @_;
3871 3975
3892 1 3996 1
3893} 3997}
3894 3998
3895############################################################################# 3999#############################################################################
3896 4000
3897package dc::UI::Multiplexer; 4001package DC::UI::Multiplexer;
3898 4002
3899our @ISA = dc::UI::Container::; 4003our @ISA = DC::UI::Container::;
3900 4004
3901sub new { 4005sub new {
3902 my $class = shift; 4006 my $class = shift;
3903 4007
3904 my $self = $class->SUPER::new ( 4008 my $self = $class->SUPER::new (
3905 @_, 4009 @_,
3906 ); 4010 );
3907 4011
3908 $self->{current} = $self->{children}[0] 4012 $self->set_current_page (0);
3909 if @{ $self->{children} };
3910 4013
3911 $self 4014 $self
3912} 4015}
3913 4016
3914sub add { 4017sub add {
3915 my ($self, @widgets) = @_; 4018 my ($self, @widgets) = @_;
3916 4019
3917 $self->SUPER::add (@widgets); 4020 $self->SUPER::add (@widgets);
3918 4021
3919 $self->{current} = $self->{children}[0] 4022 $self->set_current_page (0)
3920 if @{ $self->{children} }; 4023 if @widgets == @{ $self->{children} };
3921} 4024}
3922 4025
3923sub get_current_page { 4026sub get_current_page {
3924 my ($self) = @_; 4027 my ($self) = @_;
3925 4028
3931 4034
3932 my $widget = ref $page_or_widget 4035 my $widget = ref $page_or_widget
3933 ? $page_or_widget 4036 ? $page_or_widget
3934 : $self->{children}[$page_or_widget]; 4037 : $self->{children}[$page_or_widget];
3935 4038
4039 $self->{current}->set_invisible if $self->{current} && $self->{visible};
4040
3936 $self->{current} = $widget; 4041 if (($self->{current} = $widget)) {
4042 $self->{current}->set_visible if $self->{current} && $self->{visible};
3937 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 4043 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3938 4044
3939 $self->emit (page_changed => $self->{current}); 4045 $self->emit (page_changed => $self->{current});
4046 }
3940 4047
3941 $self->realloc; 4048 $self->realloc;
3942} 4049}
3943 4050
3944sub visible_children { 4051sub visible_children {
3945 $_[0]{current} 4052 $_[0]{current} || ()
3946} 4053}
3947 4054
3948sub size_request { 4055sub size_request {
3949 my ($self) = @_; 4056 my ($self) = @_;
3950 4057
4058 $self->{current}
3951 $self->{current}->size_request 4059 ? $self->{current}->size_request
4060 : (0, 0)
3952} 4061}
3953 4062
3954sub invoke_size_allocate { 4063sub invoke_size_allocate {
3955 my ($self, $w, $h) = @_; 4064 my ($self, $w, $h) = @_;
3956 4065
3957 $self->{current}->configure (0, 0, $w, $h); 4066 $self->{current}->configure (0, 0, $w, $h)
4067 if $self->{current};
3958 4068
3959 1 4069 1
3960} 4070}
3961 4071
3962sub _draw { 4072sub _draw {
3963 my ($self) = @_; 4073 my ($self) = @_;
3964 4074
3965 $self->{current}->draw; 4075 $self->{current}->draw
4076 if $self->{current};
3966} 4077}
3967 4078
3968############################################################################# 4079#############################################################################
3969 4080
3970package dc::UI::Notebook; 4081package DC::UI::Notebook;
3971 4082
3972use dc::OpenGL; 4083use DC::OpenGL;
3973 4084
3974our @ISA = dc::UI::VBox::; 4085our @ISA = DC::UI::VBox::;
3975 4086
3976sub new { 4087sub new {
3977 my $class = shift; 4088 my $class = shift;
3978 4089
3979 my $self = $class->SUPER::new ( 4090 my $self = $class->SUPER::new (
3980 buttonbar => (new dc::UI::Buttonbar), 4091 buttonbar => (new DC::UI::Buttonbar),
3981 multiplexer => (new dc::UI::Multiplexer expand => 1), 4092 multiplexer => (new DC::UI::Multiplexer expand => 1),
3982 active_outline => [.7, .7, 0.2], 4093 active_outline => [.7, .7, 0.2],
3983 # filter => # will be put between multiplexer and $self 4094 # filter => # will be put between multiplexer and $self
3984 @_, 4095 @_,
3985 ); 4096 );
3986 4097
3995 4106
3996 for my $child (@$widgets) { 4107 for my $child (@$widgets) {
3997 Scalar::Util::weaken $child; 4108 Scalar::Util::weaken $child;
3998 $child->{c_tab_} ||= do { 4109 $child->{c_tab_} ||= do {
3999 my $tab = 4110 my $tab =
4000 (UNIVERSAL::isa $child->{c_tab}, "dc::UI::Base") 4111 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
4001 ? $child->{c_tab} 4112 ? $child->{c_tab}
4002 : new dc::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4113 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
4003 4114
4004 $tab->connect (activate => sub { 4115 $tab->connect (activate => sub {
4005 $wself->set_current_page ($child); 4116 $wself->set_current_page ($child);
4006 }); 4117 });
4007 4118
4037} 4148}
4038 4149
4039sub pages { 4150sub pages {
4040 my ($self) = @_; 4151 my ($self) = @_;
4041 $self->{multiplexer}->children 4152 $self->{multiplexer}->children
4153}
4154
4155sub page_index {
4156 my ($self, $widget) = @_;
4157
4158 my $i = 0;
4159 for ($self->pages) {
4160 if ($_ eq $widget) { return $i };
4161 $i++;
4162 }
4163
4164 undef
4042} 4165}
4043 4166
4044sub add_tab { 4167sub add_tab {
4045 my ($self, $title, $widget, $tooltip) = @_; 4168 my ($self, $title, $widget, $tooltip) = @_;
4046 4169
4083 } 4206 }
4084} 4207}
4085 4208
4086############################################################################# 4209#############################################################################
4087 4210
4088package dc::UI::Selector; 4211package DC::UI::Selector;
4089 4212
4090use utf8; 4213use utf8;
4091 4214
4092our @ISA = dc::UI::Button::; 4215our @ISA = DC::UI::Button::;
4093 4216
4094sub new { 4217sub new {
4095 my $class = shift; 4218 my $class = shift;
4096 4219
4097 my $self = $class->SUPER::new ( 4220 my $self = $class->SUPER::new (
4114 my ($value, $title, $tooltip) = @$_; 4237 my ($value, $title, $tooltip) = @$_;
4115 4238
4116 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4239 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4117 } 4240 }
4118 4241
4119 dc::UI::Menu->new (items => \@menu_items)->popup ($ev); 4242 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4120} 4243}
4121 4244
4122sub _set_value { 4245sub _set_value {
4123 my ($self, $value) = @_; 4246 my ($self, $value) = @_;
4124 4247
4147 $self->_set_value ($self->{value}); 4270 $self->_set_value ($self->{value});
4148} 4271}
4149 4272
4150############################################################################# 4273#############################################################################
4151 4274
4152package dc::UI::Statusbox; 4275package DC::UI::Statusbox;
4153 4276
4154our @ISA = dc::UI::VBox::; 4277our @ISA = DC::UI::VBox::;
4155 4278
4156sub new { 4279sub new {
4157 my $class = shift; 4280 my $class = shift;
4158 4281
4159 my $self = $class->SUPER::new ( 4282 my $self = $class->SUPER::new (
4160 fontsize => 0.8, 4283 fontsize => 0.8,
4161 @_, 4284 @_,
4162 ); 4285 );
4163 4286
4164 dc::weaken (my $this = $self); 4287 DC::weaken (my $this = $self);
4165 4288
4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; 4289 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4167 4290
4168 $self 4291 $self
4169} 4292}
4170 4293
4171sub reorder { 4294sub reorder {
4172 my ($self) = @_; 4295 my ($self) = @_;
4173 my $NOW = Time::HiRes::time; 4296 my $NOW = EV::time;
4174 4297
4175 # freeze display when hovering over any label 4298 # freeze display when hovering over any label
4176 return if $dc::UI::TOOLTIP->{owner} 4299 return if $DC::UI::TOOLTIP->{owner}
4177 && grep $dc::UI::TOOLTIP->{owner} == $_->{label}, 4300 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4178 values %{ $self->{item} }; 4301 values %{ $self->{item} };
4179 4302
4180 while (my ($k, $v) = each %{ $self->{item} }) { 4303 while (my ($k, $v) = each %{ $self->{item} }) {
4181 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4304 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4182 } 4305 }
4203 for ($short) { 4326 for ($short) {
4204 s/^\s+//; 4327 s/^\s+//;
4205 s/\s+/ /g; 4328 s/\s+/ /g;
4206 } 4329 }
4207 4330
4208 new dc::UI::Label 4331 new DC::UI::Label
4209 markup => $short, 4332 markup => $short,
4210 tooltip => $item->{tooltip}, 4333 tooltip => $item->{tooltip},
4211 tooltip_font => $::FONT_PROP, 4334 tooltip_font => $::FONT_PROP,
4212 tooltip_width => 0.67, 4335 tooltip_width => 0.67,
4213 fontsize => $item->{fontsize} || $self->{fontsize}, 4336 fontsize => $item->{fontsize} || $self->{fontsize},
4214 max_w => $::WIDTH * 0.44, 4337 max_w => $::WIDTH * 0.44,
4338 align => 0,
4215 fg => [@{ $item->{fg} }], 4339 fg => [@{ $item->{fg} }],
4216 can_events => 1, 4340 can_events => 1,
4217 can_hover => 1 4341 can_hover => 1
4218 }; 4342 };
4219 4343
4227 $label->{fg}[3] = $item->{fg}[3] || 1; 4351 $label->{fg}[3] = $item->{fg}[3] || 1;
4228 } 4352 }
4229 4353
4230 push @widgets, $label; 4354 push @widgets, $label;
4231 } 4355 }
4356
4357 my $hash = join ",", @widgets;
4358 return if $hash eq $self->{last_widget_hash};
4359 $self->{last_widget_hash} = $hash;
4232 4360
4233 $self->clear; 4361 $self->clear;
4234 $self->SUPER::add (reverse @widgets); 4362 $self->SUPER::add (reverse @widgets);
4235} 4363}
4236 4364
4292 $self->SUPER::destroy; 4420 $self->SUPER::destroy;
4293} 4421}
4294 4422
4295############################################################################# 4423#############################################################################
4296 4424
4297package dc::UI::Root; 4425package DC::UI::Root;
4298 4426
4299our @ISA = dc::UI::Container::; 4427our @ISA = DC::UI::Container::;
4300 4428
4301use List::Util qw(min max); 4429use List::Util qw(min max);
4302 4430
4303use dc::OpenGL; 4431use DC::OpenGL;
4304 4432
4305sub new { 4433sub new {
4306 my $class = shift; 4434 my $class = shift;
4307 4435
4308 my $self = $class->SUPER::new ( 4436 my $self = $class->SUPER::new (
4309 visible => 1, 4437 visible => 1,
4310 @_, 4438 @_,
4311 ); 4439 );
4312 4440
4313 dc::weaken ($self->{root} = $self); 4441 DC::weaken ($self->{root} = $self);
4314 4442
4315 $self 4443 $self
4316} 4444}
4317 4445
4318sub size_request { 4446sub size_request {
4443 4571
4444 delete $queue{$widget+0}; 4572 delete $queue{$widget+0};
4445 4573
4446 my ($w, $h) = $widget->size_request; 4574 my ($w, $h) = $widget->size_request;
4447 4575
4448 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4576 $w += $widget->{padding_x} * 2;
4449 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4577 $h += $widget->{padding_y} * 2;
4578
4579 $w = max $widget->{min_w}, $w;
4580 $h = max $widget->{min_h}, $h;
4450 4581
4451 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4582 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4452 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4583 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4453 4584
4454 $w = $widget->{force_w} if exists $widget->{force_w}; 4585 $w = $widget->{force_w} if exists $widget->{force_w};
4521 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4652 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4522 glMatrixMode GL_MODELVIEW; 4653 glMatrixMode GL_MODELVIEW;
4523 glLoadIdentity; 4654 glLoadIdentity;
4524 4655
4525 { 4656 {
4526 package dc::UI::Base; 4657 package DC::UI::Base;
4527 4658
4528 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4659 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4529 (0, 0, $self->{w}, $self->{h}); 4660 (0, 0, $self->{w}, $self->{h});
4530 4661
4531 $self->_draw; 4662 $self->_draw;
4532 } 4663 }
4533} 4664}
4534 4665
4535############################################################################# 4666#############################################################################
4536 4667
4537package dc::UI; 4668package DC::UI;
4538 4669
4539$ROOT = new dc::UI::Root; 4670$ROOT = new DC::UI::Root;
4540$TOOLTIP = new dc::UI::Tooltip z => 900; 4671$TOOLTIP = new DC::UI::Tooltip z => 900;
4541 4672
45421 46731
4543

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines