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.436 by root, Wed Aug 22 21:40:57 2007 UTC vs.
Revision 1.473 by root, Sun Jan 11 03:19:47 2009 UTC

1package CFPlus::UI; 1package DC::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use DC;
10use CFPlus::Pod; 9use DC::Pod;
11use CFPlus::Texture; 10use DC::Texture;
12 11
13our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
14 13
15our $LAYOUT; 14our $LAYOUT;
16our $ROOT; 15our $ROOT;
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
42 } 43 }
43 44
44 $TOOLTIP->hide; 45 $TOOLTIP->hide;
45 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
46 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
47}); 48};
48 49
49sub get_layout { 50sub get_layout {
50 my $layout; 51 my $layout;
51 52
52 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
87 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
88 89
89 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
90 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
91 92
92 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
93 } 94 }
94} 95}
95 96
96sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
97 my ($ev) = @_; 98 my ($ev) = @_;
103 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
104 105
105 $GRAB = $widget; 106 $GRAB = $widget;
106 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
107 108
108 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
109 } 110 }
110 111
111 if ($GRAB) { 112 if ($GRAB) {
112 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
113 # mousewheel 114 # mousewheel
114 my $delta = $ev->{button} * 2 - 9; 115 my $delta = $ev->{button} * 2 - 9;
115 my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; 116 my $shift = $ev->{mod} & DC::KMOD_SHIFT;
116 117
117 $ev->{dx} = $shift ? $delta : 0; 118 $ev->{dx} = $shift ? $delta : 0;
118 $ev->{dy} = $shift ? 0 : $delta; 119 $ev->{dy} = $shift ? 0 : $delta;
119 120
120 $GRAB->emit (mouse_wheel => $ev); 121 $GRAB->emit (mouse_wheel => $ev);
138 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 140 $grab->update if $grab;
140 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
141 142
142 check_hover $widget; 143 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
144 } 145 }
145} 146}
146 147
147sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
148 my ($ev) = @_; 149 my ($ev) = @_;
188# call when resolution changes etc. 189# call when resolution changes etc.
189sub rescale_widgets { 190sub rescale_widgets {
190 my ($sx, $sy) = @_; 191 my ($sx, $sy) = @_;
191 192
192 for my $widget (values %WIDGET) { 193 for my $widget (values %WIDGET) {
193 if ($widget->{is_toplevel}) { 194 if ($widget->{is_toplevel} || $widget->{c_rescale}) {
194 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; 195 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
195 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; 196 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
196 197
197 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; 198 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
198 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 199 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
210 reconfigure_widgets; 211 reconfigure_widgets;
211} 212}
212 213
213############################################################################# 214#############################################################################
214 215
215package CFPlus::UI::Event; 216package DC::UI::Event;
216 217
217sub xy { 218sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y}) 219 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219} 220}
220 221
221############################################################################# 222#############################################################################
222 223
223package CFPlus::UI::Base; 224package DC::UI::Base;
224 225
225use strict; 226use strict;
226 227
227use CFPlus::OpenGL; 228use DC::OpenGL;
228 229
229sub new { 230sub new {
230 my $class = shift; 231 my $class = shift;
231 232
232 my $self = bless { 233 my $self = bless {
237 h => undef, 238 h => undef,
238 can_events => 1, 239 can_events => 1,
239 @_ 240 @_
240 }, $class; 241 }, $class;
241 242
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 243 DC::weaken ($DC::UI::WIDGET{$self+0} = $self);
243 244
244 for (keys %$self) { 245 for (keys %$self) {
245 if (/^on_(.*)$/) { 246 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_}); 247 $self->connect ($1 => delete $self->{$_});
247 } 248 }
248 } 249 }
249 250
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { 251 if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) {
251 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; 252 $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x};
252 $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; 253 $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y};
253 $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; 254 $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w};
254 $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; 255 $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h};
255 256
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; 257 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; 258 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258 259
259 $self->show if $layout->{show}; 260 $self->show if $layout->{show};
269 $self->emit ("destroy"); 270 $self->emit ("destroy");
270 %$self = (); 271 %$self = ();
271} 272}
272 273
273sub TO_JSON { 274sub TO_JSON {
274 { __w_ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
275} 276}
276 277
277sub show { 278sub show {
278 my ($self) = @_; 279 my ($self) = @_;
279 280
280 return if $self->{parent}; 281 return if $self->{parent};
281 282
282 $CFPlus::UI::ROOT->add ($self); 283 $DC::UI::ROOT->add ($self);
283} 284}
284 285
285sub set_visible { 286sub set_visible {
286 my ($self) = @_; 287 my ($self) = @_;
287 288
288 return if $self->{visible}; 289 return if $self->{visible};
290
291 $self->{parent} && $self->{parent}{root}#d#
292 or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1);
289 293
290 $self->{root} = $self->{parent}{root}; 294 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1; 295 $self->{visible} = $self->{parent}{visible} + 1;
292 296
293 $self->emit (visibility_change => 1); 297 $self->emit (visibility_change => 1);
294 298
295 $self->realloc if !exists $self->{req_w}; 299 $self->realloc if !exists $self->{req_w};
296 300
297 $_->set_visible for $self->children; 301 $_->set_visible for $self->visible_children;
298} 302}
299 303
300sub set_invisible { 304sub set_invisible {
301 my ($self) = @_; 305 my ($self) = @_;
302 306
308 delete $self->{root}; 312 delete $self->{root};
309 313
310 undef $GRAB if $GRAB == $self; 314 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 315 undef $HOVER if $HOVER == $self;
312 316
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 317 $DC::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 318 if $TOOLTIP->{owner} == $self;
315 319
316 $self->emit ("focus_out"); 320 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 321 $self->emit (visibility_change => 0);
318} 322}
337sub hide { 341sub hide {
338 my ($self) = @_; 342 my ($self) = @_;
339 343
340 $self->set_invisible; 344 $self->set_invisible;
341 345
346 # extra $parent copy for 5.8.8+ bug workaround
347 # (otherwise $_[0] in remove gets freed
348 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 349 $parent->remove ($self);
343 if $self->{parent}; 350 }
344} 351}
345 352
346sub move_abs { 353sub move_abs {
347 my ($self, $x, $y, $z) = @_; 354 my ($self, $x, $y, $z) = @_;
348 355
360 $self->{force_h} = $h; 367 $self->{force_h} = $h;
361 368
362 $self->realloc; 369 $self->realloc;
363} 370}
364 371
372# traverse the widget chain up to find the maximum "physical" size constraints
373sub get_max_wh {
374 my ($self) = @_;
375
376 my ($w, $h) = @$self{qw(max_w max_h)};
377
378 if ($w <= 0 || $h <= 0) {
379 my ($mw, $mh) = $self->{parent}
380 ? $self->{parent}->get_max_wh
381 : ($::WIDTH, $::HEIGHT);
382
383 $w = $mw if $w <= 0;
384 $h = $mh if $h <= 0;
385 }
386
387 ($w, $h)
388}
389
365sub size_request { 390sub size_request {
366 require Carp; 391 require Carp;
367 Carp::confess "size_request is abstract"; 392 Carp::confess "size_request is abstract";
368} 393}
369 394
375 my ($self, $x, $y, $w, $h) = @_; 400 my ($self, $x, $y, $w, $h) = @_;
376 401
377 if ($self->{aspect}) { 402 if ($self->{aspect}) {
378 my ($ow, $oh) = ($w, $h); 403 my ($ow, $oh) = ($w, $h);
379 404
380 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; 405 $w = List::Util::min $w, DC::ceil $h * $self->{aspect};
381 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; 406 $h = List::Util::min $h, DC::ceil $w / $self->{aspect};
382 407
383 # use alignment to adjust x, y 408 # use alignment to adjust x, y
384 409
385 $x += int 0.5 * ($ow - $w); 410 $x += int 0.5 * ($ow - $w);
386 $y += int 0.5 * ($oh - $h); 411 $y += int 0.5 * ($oh - $h);
427 452
428 return if $self->{tooltip} eq $tooltip; 453 return if $self->{tooltip} eq $tooltip;
429 454
430 $self->{tooltip} = $tooltip; 455 $self->{tooltip} = $tooltip;
431 456
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 457 if ($DC::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 458 delete $DC::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 459 $DC::UI::TOOLTIP_WATCHER->invoke;
435 } 460 }
436} 461}
437 462
438# translate global coordinates to local coordinate system 463# translate global coordinates to local coordinate system
439sub coord2local { 464sub coord2local {
440 my ($self, $x, $y) = @_; 465 my ($self, $x, $y) = @_;
441 466
442 Carp::confess unless $self->{parent};#d# 467 return (undef, undef) unless $self->{parent};
443 468
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 469 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 470}
446 471
447# translate local coordinates to global coordinate system 472# translate local coordinates to global coordinate system
448sub coord2global { 473sub coord2global {
449 my ($self, $x, $y) = @_; 474 my ($self, $x, $y) = @_;
450 475
451 Carp::confess unless $self->{parent};#d# 476 return (undef, undef) unless $self->{parent};
452 477
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 478 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 479}
455 480
456sub invoke_focus_in { 481sub invoke_focus_in {
505sub connect { 530sub connect {
506 my ($self, $signal, $cb) = @_; 531 my ($self, $signal, $cb) = @_;
507 532
508 push @{ $self->{signal_cb}{$signal} }, $cb; 533 push @{ $self->{signal_cb}{$signal} }, $cb;
509 534
510 defined wantarray and CFPlus::guard { 535 defined wantarray and DC::guard {
511 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, 536 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
512 @{ $self->{signal_cb}{$signal} }; 537 @{ $self->{signal_cb}{$signal} };
513 } 538 }
514} 539}
515 540
553# in .xs 578# in .xs
554 579
555sub set_parent { 580sub set_parent {
556 my ($self, $parent) = @_; 581 my ($self, $parent) = @_;
557 582
558 CFPlus::weaken ($self->{parent} = $parent); 583 DC::weaken ($self->{parent} = $parent);
559 $self->set_visible if $parent->{visible}; 584 $self->set_visible if $parent->{visible};
560} 585}
561 586
562sub realloc { 587sub realloc {
563 my ($self) = @_; 588 my ($self) = @_;
601} 626}
602 627
603sub DESTROY { 628sub DESTROY {
604 my ($self) = @_; 629 my ($self) = @_;
605 630
606 return if CFPlus::in_destruct; 631 return if DC::in_destruct;
607 632
608 local $@; 633 local $@;
609 eval { $self->destroy }; 634 eval { $self->destroy };
610 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 635 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
611 636
612 delete $WIDGET{$self+0}; 637 delete $WIDGET{$self+0};
613} 638}
614 639
615############################################################################# 640#############################################################################
616 641
617package CFPlus::UI::DrawBG; 642package DC::UI::DrawBG;
618 643
619our @ISA = CFPlus::UI::Base::; 644our @ISA = DC::UI::Base::;
620 645
621use strict; 646use strict;
622use CFPlus::OpenGL; 647use DC::OpenGL;
623 648
624sub new { 649sub new {
625 my $class = shift; 650 my $class = shift;
626 651
627 $class->SUPER::new ( 652 $class->SUPER::new (
649 } 674 }
650} 675}
651 676
652############################################################################# 677#############################################################################
653 678
654package CFPlus::UI::Empty; 679package DC::UI::Empty;
655 680
656our @ISA = CFPlus::UI::Base::; 681our @ISA = DC::UI::Base::;
657 682
658sub new { 683sub new {
659 my ($class, %arg) = @_; 684 my ($class, %arg) = @_;
660 $class->SUPER::new (can_events => 0, %arg); 685 $class->SUPER::new (can_events => 0, %arg);
661} 686}
668 693
669sub draw { } 694sub draw { }
670 695
671############################################################################# 696#############################################################################
672 697
673package CFPlus::UI::Container; 698package DC::UI::Container;
674 699
675our @ISA = CFPlus::UI::Base::; 700our @ISA = DC::UI::Base::;
676 701
677sub new { 702sub new {
678 my ($class, %arg) = @_; 703 my ($class, %arg) = @_;
679 704
680 my $children = delete $arg{children}; 705 my $children = delete $arg{children};
774 $_->draw for $self->visible_children; 799 $_->draw for $self->visible_children;
775} 800}
776 801
777############################################################################# 802#############################################################################
778 803
779package CFPlus::UI::Bin; 804package DC::UI::Bin;
780 805
781our @ISA = CFPlus::UI::Container::; 806our @ISA = DC::UI::Container::;
782 807
783sub new { 808sub new {
784 my ($class, %arg) = @_; 809 my ($class, %arg) = @_;
785 810
786 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; 811 my $child = (delete $arg{child}) || new DC::UI::Empty::;
787 812
788 $class->SUPER::new (children => [$child], %arg) 813 $class->SUPER::new (children => [$child], %arg)
789} 814}
790 815
791sub add { 816sub add {
798sub remove { 823sub remove {
799 my ($self, $widget) = @_; 824 my ($self, $widget) = @_;
800 825
801 $self->SUPER::remove ($widget); 826 $self->SUPER::remove ($widget);
802 827
803 $self->{children} = [new CFPlus::UI::Empty] 828 $self->{children} = [new DC::UI::Empty]
804 unless @{$self->{children}}; 829 unless @{$self->{children}};
805} 830}
806 831
807sub child { $_[0]->{children}[0] } 832sub child { $_[0]->{children}[0] }
808 833
817 842
818 1 843 1
819} 844}
820 845
821############################################################################# 846#############################################################################
822
823# back-buffered drawing area 847# back-buffered drawing area
824 848
825package CFPlus::UI::Window; 849package DC::UI::Window;
826 850
827our @ISA = CFPlus::UI::Bin::; 851our @ISA = DC::UI::Bin::;
828 852
829use CFPlus::OpenGL; 853use DC::OpenGL;
830 854
831sub new { 855sub new {
832 my ($class, %arg) = @_; 856 my ($class, %arg) = @_;
833 857
834 my $self = $class->SUPER::new (%arg); 858 my $self = $class->SUPER::new (%arg);
856} 880}
857 881
858sub render_child { 882sub render_child {
859 my ($self) = @_; 883 my ($self) = @_;
860 884
861 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { 885 $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub {
862 glClearColor 0, 0, 0, 0; 886 glClearColor 0, 0, 0, 0;
863 glClear GL_COLOR_BUFFER_BIT; 887 glClear GL_COLOR_BUFFER_BIT;
864 888
865 { 889 {
866 package CFPlus::UI::Base; 890 package DC::UI::Base;
867 891
868 local ($draw_x, $draw_y, $draw_w, $draw_h) = 892 local ($draw_x, $draw_y, $draw_w, $draw_h) =
869 (0, 0, $self->{w}, $self->{h}); 893 (0, 0, $self->{w}, $self->{h});
870 894
871 $self->_render; 895 $self->_render;
888 glDisable GL_TEXTURE_2D; 912 glDisable GL_TEXTURE_2D;
889} 913}
890 914
891############################################################################# 915#############################################################################
892 916
893package CFPlus::UI::ViewPort; 917package DC::UI::ViewPort;
894 918
895use List::Util qw(min max); 919use List::Util qw(min max);
896 920
897our @ISA = CFPlus::UI::Window::; 921our @ISA = DC::UI::Window::;
898 922
899sub new { 923sub new {
900 my $class = shift; 924 my $class = shift;
901 925
902 $class->SUPER::new ( 926 $class->SUPER::new (
986 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} 1010 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
987 && $y >= $self->{y} && $y < $self->{y} + $self->{h} 1011 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
988 ) { 1012 ) {
989 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) 1013 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
990 } else { 1014 } else {
991 $self->CFPlus::UI::Base::find_widget ($x, $y) 1015 $self->DC::UI::Base::find_widget ($x, $y)
992 } 1016 }
993} 1017}
994 1018
995sub _render { 1019sub _render {
996 my ($self) = @_; 1020 my ($self) = @_;
997 1021
998 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; 1022 local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x};
999 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; 1023 local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y};
1000 1024
1001 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; 1025 DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1002 1026
1003 $self->SUPER::_render; 1027 $self->SUPER::_render;
1004} 1028}
1005 1029
1006############################################################################# 1030#############################################################################
1007 1031
1008package CFPlus::UI::ScrolledWindow; 1032package DC::UI::ScrolledWindow;
1009 1033
1010our @ISA = CFPlus::UI::Table::; 1034our @ISA = DC::UI::Table::;
1011 1035
1012sub new { 1036sub new {
1013 my ($class, %arg) = @_; 1037 my ($class, %arg) = @_;
1014 1038
1015 my $child = delete $arg{child}; 1039 my $child = delete $arg{child};
1016 1040
1017 my $self; 1041 my $self;
1018 1042
1019 my $hslider = new CFPlus::UI::Slider 1043 my $hslider = new DC::UI::Slider
1020 c_col => 0, 1044 c_col => 0,
1021 c_row => 1, 1045 c_row => 1,
1022 vertical => 0, 1046 vertical => 0,
1023 range => [0, 0, 1, 0.01], # HACK fix 1047 range => [0, 0, 1, 0.01], # HACK fix
1024 on_changed => sub { 1048 on_changed => sub {
1025 $self->{hpos} = $_[1]; 1049 $self->{hpos} = $_[1];
1026 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); 1050 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1027 }, 1051 },
1028 ; 1052 ;
1029 1053
1030 my $vslider = new CFPlus::UI::Slider 1054 my $vslider = new DC::UI::Slider
1031 c_col => 1, 1055 c_col => 1,
1032 c_row => 0, 1056 c_row => 0,
1033 vertical => 1, 1057 vertical => 1,
1034 range => [0, 0, 1, 0.01], # HACK fix 1058 range => [0, 0, 1, 0.01], # HACK fix
1035 on_changed => sub { 1059 on_changed => sub {
1047 col_expand => [1, 0], 1071 col_expand => [1, 0],
1048 row_expand => [1, 0], 1072 row_expand => [1, 0],
1049 %arg, 1073 %arg,
1050 ); 1074 );
1051 1075
1052 $self->{vp} = new CFPlus::UI::ViewPort 1076 $self->{vp} = new DC::UI::ViewPort
1053 c_col => 0, 1077 c_col => 0,
1054 c_row => 0, 1078 c_row => 0,
1055 expand => 1, 1079 expand => 1,
1056 scroll_x => $self->{scroll_x}, 1080 scroll_x => $self->{scroll_x},
1057 scroll_y => $self->{scroll_y}, 1081 scroll_y => $self->{scroll_y},
1183 $self->SUPER::invoke_size_allocate ($w, $h) 1207 $self->SUPER::invoke_size_allocate ($w, $h)
1184} 1208}
1185 1209
1186############################################################################# 1210#############################################################################
1187 1211
1188package CFPlus::UI::Frame; 1212package DC::UI::Frame;
1189 1213
1190our @ISA = CFPlus::UI::Bin::; 1214our @ISA = DC::UI::Bin::;
1191 1215
1192use CFPlus::OpenGL; 1216use DC::OpenGL;
1193 1217
1194sub new { 1218sub new {
1195 my $class = shift; 1219 my $class = shift;
1196 1220
1197 $class->SUPER::new ( 1221 $class->SUPER::new (
1216 $self->SUPER::_draw; 1240 $self->SUPER::_draw;
1217} 1241}
1218 1242
1219############################################################################# 1243#############################################################################
1220 1244
1221package CFPlus::UI::FancyFrame; 1245package DC::UI::FancyFrame;
1222 1246
1223our @ISA = CFPlus::UI::Bin::; 1247our @ISA = DC::UI::Bin::;
1224 1248
1225use CFPlus::OpenGL; 1249use DC::OpenGL;
1226 1250
1227sub new { 1251sub new {
1228 my ($class, %arg) = @_; 1252 my ($class, %arg) = @_;
1229 1253
1230 if ((exists $arg{label}) && !ref $arg{label}) { 1254 if ((exists $arg{label}) && !ref $arg{label}) {
1231 $arg{label} = new CFPlus::UI::Label 1255 $arg{label} = new DC::UI::Label
1232 align => 1, 1256 align => 1,
1233 valign => 0, 1257 valign => 0.5,
1234 text => $arg{label}, 1258 text => $arg{label},
1235 fontsize => ($arg{border} || 0.8) * 0.75; 1259 fontsize => ($arg{border} || 0.8) * 0.75;
1236 } 1260 }
1237 1261
1238 my $self = $class->SUPER::new ( 1262 my $self = $class->SUPER::new (
1239 # label => "", 1263 # label => "",
1240 fg => [0.6, 0.3, 0.1], 1264 fg => undef,
1241 border => 0.8, 1265 border => 0.8,
1242 style => 'single', 1266 style => 'single',
1243 %arg, 1267 %arg,
1244 ); 1268 );
1245 1269
1248 1272
1249sub add { 1273sub add {
1250 my ($self, @widgets) = @_; 1274 my ($self, @widgets) = @_;
1251 1275
1252 $self->SUPER::add (@widgets); 1276 $self->SUPER::add (@widgets);
1253 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; 1277 $self->DC::UI::Container::add ($self->{label}) if $self->{label};
1254} 1278}
1255 1279
1256sub border { 1280sub border {
1257 int $_[0]{border} * $::FONTSIZE 1281 int $_[0]{border} * $::FONTSIZE
1258} 1282}
1298 my $border = $self->border; 1322 my $border = $self->border;
1299 my ($w, $h) = ($self->{w}, $self->{h}); 1323 my ($w, $h) = ($self->{w}, $self->{h});
1300 1324
1301 $child->draw; 1325 $child->draw;
1302 1326
1303 glColor @{$self->{fg}}; 1327 glColor @{$self->{fg} || $DC::THEME{fancyframe}};
1304 glBegin GL_LINE_STRIP; 1328 glBegin GL_LINE_STRIP;
1305 glVertex $border * 1.5 , $border * 0.5 + 0.5; 1329 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1306 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; 1330 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1307 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1331 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1308 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; 1332 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1316 } 1340 }
1317} 1341}
1318 1342
1319############################################################################# 1343#############################################################################
1320 1344
1321package CFPlus::UI::Toplevel; 1345package DC::UI::Toplevel;
1322 1346
1323our @ISA = CFPlus::UI::Bin::; 1347our @ISA = DC::UI::Bin::;
1324 1348
1325use CFPlus::OpenGL; 1349use DC::OpenGL;
1326 1350
1327my $bg = 1351my $bg =
1328 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", 1352 new_from_resource DC::Texture "d1_bg.png",
1329 mipmap => 1, wrap => 1; 1353 mipmap => 1, wrap => 1;
1330 1354
1331my @border = 1355my @border =
1332 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1356 map { new_from_resource DC::Texture $_, mipmap => 1 }
1333 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1357 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1334 1358
1335my @icon = 1359my @icon =
1336 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 1360 map { new_from_resource DC::Texture $_, mipmap => 1 }
1337 qw(x1_move.png x1_resize.png); 1361 qw(x1_move.png x1_resize.png);
1338 1362
1339sub new { 1363sub new {
1340 my ($class, %arg) = @_; 1364 my ($class, %arg) = @_;
1341 1365
1342 my $self = $class->SUPER::new ( 1366 my $self = $class->SUPER::new (
1343 bg => [1, 1, 1, 1], 1367 bg => [1, 1, 1, 1],
1344 border_bg => [1, 1, 1, 1], 1368 border_bg => [1, 1, 1, 1],
1345 border => 0.6, 1369 border => 0.8,
1346 can_events => 1, 1370 can_events => 1,
1347 min_w => 64, 1371 min_w => 64,
1348 min_h => 32, 1372 min_h => 32,
1349 %arg, 1373 %arg,
1350 ); 1374 );
1351 1375
1352 $self->{title_widget} = new CFPlus::UI::Label 1376 $self->{title_widget} = new DC::UI::Label
1353 align => 0, 1377 align => 0.5,
1354 valign => 1, 1378 valign => 1,
1355 text => $self->{title}, 1379 text => $self->{title},
1356 fontsize => $self->{border}, 1380 fontsize => $self->{border},
1357 if exists $self->{title}; 1381 if exists $self->{title};
1358 1382
1359 if ($self->{has_close_button}) { 1383 if ($self->{has_close_button}) {
1360 $self->{close_button} = 1384 $self->{close_button} =
1361 new CFPlus::UI::ImageButton 1385 new DC::UI::ImageButton
1362 path => 'x1_close.png', 1386 path => 'x1_close.png',
1363 on_activate => sub { $self->emit ("delete") }; 1387 on_activate => sub { $self->emit ("delete") };
1364 1388
1365 $self->CFPlus::UI::Container::add ($self->{close_button}); 1389 $self->DC::UI::Container::add ($self->{close_button});
1366 } 1390 }
1367 1391
1368 $self 1392 $self
1369} 1393}
1370 1394
1371sub add { 1395sub add {
1372 my ($self, @widgets) = @_; 1396 my ($self, @widgets) = @_;
1373 1397
1374 $self->SUPER::add (@widgets); 1398 $self->SUPER::add (@widgets);
1375 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; 1399 $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button};
1376 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1400 $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1377} 1401}
1378 1402
1379sub border { 1403sub border {
1380 int $_[0]{border} * $::FONTSIZE 1404 int $_[0]{border} * $::FONTSIZE
1405}
1406
1407sub get_max_wh {
1408 my ($self) = @_;
1409
1410 return ($self->{w}, $self->{h})
1411 if $self->{visible} && $self->{w};
1412
1413 $self->SUPER::get_max_wh
1381} 1414}
1382 1415
1383sub size_request { 1416sub size_request {
1384 my ($self) = @_; 1417 my ($self) = @_;
1385 1418
1510 glEnable GL_TEXTURE_2D; 1543 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1544 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1512 1545
1513 my $border = $self->border; 1546 my $border = $self->border;
1514 1547
1548 if ($border) {
1515 glColor @{ $self->{border_bg} }; 1549 glColor @{ $self->{border_bg} };
1516 $border[0]->draw_quad_alpha ( 0, 0, $w, $border); 1550 $border[0]->draw_quad_alpha ( 0, 0, $w, $border);
1517 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); 1551 $border[1]->draw_quad_alpha ( 0, $border, $border, $ch);
1518 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1552 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1519 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); 1553 $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border);
1520 1554
1521 # move 1555 # move
1522 my $w2 = ($w - $border) * .5; 1556 my $w2 = ($w - $border) * .5;
1523 my $h2 = ($h - $border) * .5; 1557 my $h2 = ($h - $border) * .5;
1524 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); 1558 $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border);
1525 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); 1559 $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border);
1526 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); 1560 $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border);
1527 1561
1528 # resize 1562 # resize
1529 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); 1563 $icon[1]->draw_quad_alpha ( 0, 0, $border, $border);
1530 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) 1564 $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border)
1531 unless $self->{has_close_button}; 1565 unless $self->{has_close_button};
1532 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); 1566 $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border);
1533 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); 1567 $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border);
1568 }
1534 1569
1535 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1570 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1536 glColor @{ $self->{bg} }; 1571 glColor @{ $self->{bg} };
1537 1572
1538 # TODO: repeat texture not scale 1573 # TODO: repeat texture not scale
1557 if $self->{close_button}; 1592 if $self->{close_button};
1558} 1593}
1559 1594
1560############################################################################# 1595#############################################################################
1561 1596
1562package CFPlus::UI::Table; 1597package DC::UI::Table;
1563 1598
1564our @ISA = CFPlus::UI::Container::; 1599our @ISA = DC::UI::Container::;
1565 1600
1566use List::Util qw(max sum); 1601use List::Util qw(max sum);
1567 1602
1568use CFPlus::OpenGL; 1603use DC::OpenGL;
1569 1604
1570sub new { 1605sub new {
1571 my $class = shift; 1606 my $class = shift;
1572 1607
1573 $class->SUPER::new ( 1608 $class->SUPER::new (
1617 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; 1652 my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)};
1618 1653
1619 my $sw = sum @w[$c .. $c + $cs - 1]; 1654 my $sw = sum @w[$c .. $c + $cs - 1];
1620 1655
1621 if ($w > $sw) { 1656 if ($w > $sw) {
1622 $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; 1657 $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1];
1623 } 1658 }
1624 } 1659 }
1625 1660
1626 # second pass, rows 1661 # second pass, rows
1627 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { 1662 for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) {
1628 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; 1663 my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)};
1629 1664
1630 my $sh = sum @h[$r .. $r + $rs - 1]; 1665 my $sh = sum @h[$r .. $r + $rs - 1];
1631 1666
1632 if ($h > $sh) { 1667 if ($h > $sh) {
1633 $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; 1668 $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1];
1634 } 1669 }
1635 } 1670 }
1636 1671
1637 (\@w, \@h) 1672 (\@w, \@h)
1638} 1673}
1661 @col_expand = (1) x @$ws unless @col_expand; 1696 @col_expand = (1) x @$ws unless @col_expand;
1662 my $col_expand = (sum @col_expand) || 1; 1697 my $col_expand = (sum @col_expand) || 1;
1663 1698
1664 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 1699 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1665 1700
1666 CFPlus::UI::harmonize $ws; 1701 DC::UI::harmonize $ws;
1667 1702
1668 my @row_expand = @{$self->{row_expand}}; 1703 my @row_expand = @{$self->{row_expand}};
1669 @row_expand = (1) x @$ws unless @row_expand; 1704 @row_expand = (1) x @$ws unless @row_expand;
1670 my $row_expand = (sum @row_expand) || 1; 1705 my $row_expand = (sum @row_expand) || 1;
1671 1706
1672 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; 1707 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1673 1708
1674 CFPlus::UI::harmonize $hs; 1709 DC::UI::harmonize $hs;
1675 1710
1676 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } 1711 my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] }
1677 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } 1712 my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] }
1678 1713
1679 for my $widget ($self->children) { 1714 for my $widget ($self->children) {
1688 1 1723 1
1689} 1724}
1690 1725
1691############################################################################# 1726#############################################################################
1692 1727
1693package CFPlus::UI::Fixed; 1728package DC::UI::Fixed;
1694 1729
1695use List::Util qw(min max); 1730use List::Util qw(min max);
1696 1731
1697our @ISA = CFPlus::UI::Container::; 1732our @ISA = DC::UI::Container::;
1698 1733
1699sub _scale($$$) { 1734sub _scale($$$) {
1700 my ($rel, $val, $max) = @_; 1735 my ($rel, $val, $max) = @_;
1701 1736
1702 $rel ? $val * $max : $val 1737 $rel ? $val * $max : $val
1754 1 1789 1
1755} 1790}
1756 1791
1757############################################################################# 1792#############################################################################
1758 1793
1759package CFPlus::UI::Box; 1794package DC::UI::Box;
1760 1795
1761our @ISA = CFPlus::UI::Container::; 1796our @ISA = DC::UI::Container::;
1762 1797
1763sub size_request { 1798sub size_request {
1764 my ($self) = @_; 1799 my ($self) = @_;
1765 1800
1766 my @children = $self->visible_children; 1801 my @children = $self->visible_children;
1801 $req[$_] += $space * $children[$_]{expand} 1836 $req[$_] += $space * $children[$_]{expand}
1802 for 0 .. $#children; 1837 for 0 .. $#children;
1803 } 1838 }
1804 } 1839 }
1805 1840
1806 CFPlus::UI::harmonize \@req; 1841 DC::UI::harmonize \@req;
1807 1842
1808 my $pos = 0; 1843 my $pos = 0;
1809 for (0 .. $#children) { 1844 for (0 .. $#children) {
1810 my $alloc = $req[$_]; 1845 my $alloc = $req[$_];
1811 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1846 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1816 1 1851 1
1817} 1852}
1818 1853
1819############################################################################# 1854#############################################################################
1820 1855
1821package CFPlus::UI::HBox; 1856package DC::UI::HBox;
1822 1857
1823our @ISA = CFPlus::UI::Box::; 1858our @ISA = DC::UI::Box::;
1824 1859
1825sub new { 1860sub new {
1826 my $class = shift; 1861 my $class = shift;
1827 1862
1828 $class->SUPER::new ( 1863 $class->SUPER::new (
1831 ) 1866 )
1832} 1867}
1833 1868
1834############################################################################# 1869#############################################################################
1835 1870
1836package CFPlus::UI::VBox; 1871package DC::UI::VBox;
1837 1872
1838our @ISA = CFPlus::UI::Box::; 1873our @ISA = DC::UI::Box::;
1839 1874
1840sub new { 1875sub new {
1841 my $class = shift; 1876 my $class = shift;
1842 1877
1843 $class->SUPER::new ( 1878 $class->SUPER::new (
1846 ) 1881 )
1847} 1882}
1848 1883
1849############################################################################# 1884#############################################################################
1850 1885
1851package CFPlus::UI::Label; 1886package DC::UI::Label;
1852 1887
1853our @ISA = CFPlus::UI::DrawBG::; 1888our @ISA = DC::UI::DrawBG::;
1854 1889
1855use CFPlus::OpenGL; 1890use DC::OpenGL;
1856 1891
1857sub new { 1892sub new {
1858 my ($class, %arg) = @_; 1893 my ($class, %arg) = @_;
1859 1894
1860 my $self = $class->SUPER::new ( 1895 my $self = $class->SUPER::new (
1865 #text => initial text 1900 #text => initial text
1866 #markup => initial narkup 1901 #markup => initial narkup
1867 #max_w => maximum pixel width 1902 #max_w => maximum pixel width
1868 #style => 0, # render flags 1903 #style => 0, # render flags
1869 ellipsise => 3, # end 1904 ellipsise => 3, # end
1870 layout => (new CFPlus::Layout), 1905 layout => (new DC::Layout),
1871 fontsize => 1, 1906 fontsize => 1,
1872 align => -1, 1907 align => 0.5,
1873 valign => -1, 1908 valign => 0.5,
1874 padding_x => 2, 1909 padding_x => 4,
1875 padding_y => 2, 1910 padding_y => 2,
1876 can_events => 0, 1911 can_events => 0,
1877 %arg 1912 %arg
1878 ); 1913 );
1879 1914
1880 if (exists $self->{template}) { 1915 if (exists $self->{template}) {
1881 my $layout = new CFPlus::Layout; 1916 my $layout = new DC::Layout;
1882 $layout->set_text (delete $self->{template}); 1917 $layout->set_text (delete $self->{template});
1883 $self->{template} = $layout; 1918 $self->{template} = $layout;
1884 } 1919 }
1885 1920
1886 if (exists $self->{markup}) { 1921 if (exists $self->{markup}) {
1942 1977
1943sub size_request { 1978sub size_request {
1944 my ($self) = @_; 1979 my ($self) = @_;
1945 1980
1946 $self->{size_req} ||= do { 1981 $self->{size_req} ||= do {
1982 my ($max_w, $max_h) = $self->get_max_wh;
1983
1947 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1984 $self->{layout}->set_font ($self->{font}) if $self->{font};
1948 $self->{layout}->set_width ($self->{max_w} || -1); 1985 $self->{layout}->set_width ($max_w);
1949 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1986 $self->{layout}->set_ellipsise ($self->{ellipsise});
1950 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1987 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1951 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1988 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1952 1989
1953 my ($w, $h) = $self->{layout}->size; 1990 my ($w, $h) = $self->{layout}->size;
1954 1991
1955 if (exists $self->{template}) { 1992 if (exists $self->{template}) {
1956 $self->{template}->set_font ($self->{font}) if $self->{font}; 1993 $self->{template}->set_font ($self->{font}) if $self->{font};
1957 $self->{template}->set_width ($self->{max_w} || -1); 1994 $self->{template}->set_width ($max_w);
1958 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1995 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1959 1996
1960 my ($w2, $h2) = $self->{template}->size; 1997 my ($w2, $h2) = $self->{template}->size;
1961 1998
1962 $w = List::Util::max $w, $w2; 1999 $w = List::Util::max $w, $w2;
2018 2055
2019 [$self->{layout}->size] 2056 [$self->{layout}->size]
2020 }; 2057 };
2021 2058
2022 unless (exists $self->{ox}) { 2059 unless (exists $self->{ox}) {
2023 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 2060 $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2);
2024 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} 2061 $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2);
2025 : ($self->{w} - $size->[0]) * 0.5);
2026
2027 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
2028 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
2029 : ($self->{h} - $size->[1]) * 0.5);
2030 2062
2031 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2063 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2032 }; 2064 };
2033 2065
2034# unless ($self->{list}) { 2066# unless ($self->{list}) {
2035# $self->{list} = CFPlus::OpenGL::glGenList; 2067# $self->{list} = DC::OpenGL::glGenList;
2036# CFPlus::OpenGL::glNewList $self->{list}; 2068# DC::OpenGL::glNewList $self->{list};
2037# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); 2069# $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
2038# CFPlus::OpenGL::glEndList; 2070# DC::OpenGL::glEndList;
2039# } 2071# }
2040# 2072#
2041# CFPlus::OpenGL::glCallList $self->{list}; 2073# DC::OpenGL::glCallList $self->{list};
2042 2074
2043 $self->{layout}->draw; 2075 $self->{layout}->draw;
2044} 2076}
2045 2077
2046#sub destroy { 2078#sub destroy {
2047# my ($self) = @_; 2079# my ($self) = @_;
2048# 2080#
2049# CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; 2081# DC::OpenGL::glDeleteList delete $self->{list} if $self->{list};
2050# 2082#
2051# $self->SUPER::destroy; 2083# $self->SUPER::destroy;
2052#} 2084#}
2053 2085
2054############################################################################# 2086#############################################################################
2055 2087
2056package CFPlus::UI::EntryBase; 2088package DC::UI::EntryBase;
2057 2089
2058our @ISA = CFPlus::UI::Label::; 2090our @ISA = DC::UI::Label::;
2059 2091
2060use CFPlus::OpenGL; 2092use DC::OpenGL;
2061 2093
2062sub new { 2094sub new {
2063 my $class = shift; 2095 my $class = shift;
2064 2096
2065 $class->SUPER::new ( 2097 $class->SUPER::new (
2066 fg => [1, 1, 1], 2098 fg => [1, 1, 1],
2067 bg => [0, 0, 0, 0.2], 2099 bg => [0, 0, 0, 0.2],
2068 outline => [0.6, 0.3, 0.1], 2100 outline => undef,
2069 active_bg => [0, 0, 1, .2], 2101 active_bg => [0, 0, 1, .2],
2070 active_fg => [1, 1, 1], 2102 active_fg => [1, 1, 1],
2071 active_outline => [1, 1, 0], 2103 active_outline => [1, 1, 0],
2072 can_hover => 1, 2104 can_hover => 1,
2073 can_focus => 1, 2105 can_focus => 1,
2106 align => 0,
2074 valign => 0, 2107 valign => 0.5,
2075 can_events => 1, 2108 can_events => 1,
2076 ellipsise => 0, 2109 ellipsise => 0,
2110 padding_x => 4,
2111 padding_y => 2,
2077 #text => ... 2112 #text => ...
2078 #hidden => "*", 2113 #hidden => "*",
2079 @_ 2114 @_
2080 ) 2115 )
2081} 2116}
2128 2163
2129 my $text = $self->get_text; 2164 my $text = $self->get_text;
2130 2165
2131 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; 2166 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2132 2167
2133 if ($uni == 8) { 2168 if ($sym == DC::SDLK_BACKSPACE) {
2134 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 2169 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2135 } elsif ($uni == 127) { 2170 } elsif ($sym == DC::SDLK_DELETE) {
2136 substr $text, $self->{cursor}, 1, ""; 2171 substr $text, $self->{cursor}, 1, "";
2137 } elsif ($sym == CFPlus::SDLK_LEFT) { 2172 } elsif ($sym == DC::SDLK_LEFT) {
2138 --$self->{cursor} if $self->{cursor}; 2173 --$self->{cursor} if $self->{cursor};
2139 } elsif ($sym == CFPlus::SDLK_RIGHT) { 2174 } elsif ($sym == DC::SDLK_RIGHT) {
2140 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 2175 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2141 } elsif ($sym == CFPlus::SDLK_HOME) { 2176 } elsif ($sym == DC::SDLK_HOME) {
2142 # what a hack 2177 # what a hack
2143 $self->{cursor} = 2178 $self->{cursor} =
2144 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ 2179 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2145 ? length $1 2180 ? length $1
2146 : 0; 2181 : 0;
2147 } elsif ($sym == CFPlus::SDLK_END) { 2182 } elsif ($sym == DC::SDLK_END) {
2148 # uh, again 2183 # uh, again
2149 $self->{cursor} = 2184 $self->{cursor} =
2150 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ 2185 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2151 ? $self->{cursor} + length $1 2186 ? $self->{cursor} + length $1
2152 : length $self->{text}; 2187 : length $self->{text};
2233 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 2268 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2234 } 2269 }
2235 2270
2236 glColor_premultiply @{$self->{active_fg}}; 2271 glColor_premultiply @{$self->{active_fg}};
2237 glBegin GL_LINES; 2272 glBegin GL_LINES;
2238 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2273 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2239 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2274 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2240 glEnd; 2275 glEnd;
2241 2276
2242 glLineWidth 3; 2277 glLineWidth 3;
2243 glColor @{$self->{active_outline}}; 2278 glColor @{$self->{active_outline}};
2244 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1; 2279 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2245 glLineWidth 1; 2280 glLineWidth 1;
2246 2281
2247 } else { 2282 } else {
2248 glColor @{$self->{outline}}; 2283 glColor @{$self->{outline} || $DC::THEME{entry_outline}};
2249 glTranslate .375, .375;
2250 glBegin GL_LINE_STRIP; 2284 glBegin GL_LINE_STRIP;
2251 glVertex 0, $self->{h} * .5; 2285 glVertex .5, $self->{h} * .5;
2252 glVertex 0, $self->{h} - 3; 2286 glVertex .5, $self->{h} - 2.5;
2253 glVertex $self->{w} - 1, $self->{h} - 3; 2287 glVertex $self->{w} - .5, $self->{h} - 2.5;
2254 glVertex $self->{w} - 1, $self->{h} * .5; 2288 glVertex $self->{w} - .5, $self->{h} * .5;
2255 glEnd; 2289 glEnd;
2256 } 2290 }
2257} 2291}
2258 2292
2259############################################################################# 2293#############################################################################
2260 2294
2261package CFPlus::UI::Entry; 2295package DC::UI::Entry;
2262 2296
2263our @ISA = CFPlus::UI::EntryBase::; 2297our @ISA = DC::UI::EntryBase::;
2264 2298
2265use CFPlus::OpenGL; 2299use DC::OpenGL;
2300
2301sub new {
2302 my $class = shift;
2303
2304 $class->SUPER::new (
2305 history_pointer => -1,
2306 @_
2307 )
2308}
2309
2266 2310
2267sub invoke_key_down { 2311sub invoke_key_down {
2268 my ($self, $ev) = @_; 2312 my ($self, $ev) = @_;
2269 2313
2270 my $sym = $ev->{sym}; 2314 my $sym = $ev->{sym};
2276 $self->{history_pointer} = -1; 2320 $self->{history_pointer} = -1;
2277 $self->{history_saveback} = ''; 2321 $self->{history_saveback} = '';
2278 $self->emit (activate => $txt); 2322 $self->emit (activate => $txt);
2279 $self->update; 2323 $self->update;
2280 2324
2281 } elsif ($sym == CFPlus::SDLK_UP) { 2325 } elsif ($sym == DC::SDLK_UP) {
2282 if ($self->{history_pointer} < 0) { 2326 if ($self->{history_pointer} < 0) {
2283 $self->{history_saveback} = $self->get_text; 2327 $self->{history_saveback} = $self->get_text;
2284 } 2328 }
2285 if (@{$self->{history} || []} > 0) { 2329 if (@{$self->{history} || []} > 0) {
2286 $self->{history_pointer}++; 2330 $self->{history_pointer}++;
2288 $self->{history_pointer} = @{$self->{history} || []} - 1; 2332 $self->{history_pointer} = @{$self->{history} || []} - 1;
2289 } 2333 }
2290 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2334 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2291 } 2335 }
2292 2336
2293 } elsif ($sym == CFPlus::SDLK_DOWN) { 2337 } elsif ($sym == DC::SDLK_DOWN) {
2294 $self->{history_pointer}--; 2338 $self->{history_pointer}--;
2295 $self->{history_pointer} = -1 if $self->{history_pointer} < 0; 2339 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2296 2340
2297 if ($self->{history_pointer} >= 0) { 2341 if ($self->{history_pointer} >= 0) {
2298 $self->set_text ($self->{history}->[$self->{history_pointer}]); 2342 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2299 } else { 2343 } else {
2344 if (defined $self->{history_saveback}) {
2300 $self->set_text ($self->{history_saveback}); 2345 $self->set_text ($self->{history_saveback});
2346 $self->{history_saveback} = undef;
2347 }
2301 } 2348 }
2302 2349
2303 } else { 2350 } else {
2304 return $self->SUPER::invoke_key_down ($ev) 2351 return $self->SUPER::invoke_key_down ($ev)
2305 } 2352 }
2307 1 2354 1
2308} 2355}
2309 2356
2310############################################################################# 2357#############################################################################
2311 2358
2312package CFPlus::UI::TextEdit; 2359package DC::UI::TextEdit;
2313 2360
2314our @ISA = CFPlus::UI::EntryBase::; 2361our @ISA = DC::UI::EntryBase::;
2315 2362
2316use CFPlus::OpenGL; 2363use DC::OpenGL;
2364
2365sub new {
2366 my $class = shift;
2367
2368 $class->SUPER::new (
2369 padding_y => 4,
2370
2371 @_
2372 )
2373}
2317 2374
2318sub move_cursor_ver { 2375sub move_cursor_ver {
2319 my ($self, $dy) = @_; 2376 my ($self, $dy) = @_;
2320 2377
2321 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); 2378 my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2322 2379
2323 $y += $dy; 2380 $line += $dy;
2324 2381
2325 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { 2382 if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) {
2326 $self->{cursor} = $index; 2383 $self->{cursor} = $index;
2327 delete $self->{cur_h}; 2384 delete $self->{cur_h};
2328 $self->update; 2385 $self->update;
2329 return; 2386 return;
2330 } 2387 }
2333sub invoke_key_down { 2390sub invoke_key_down {
2334 my ($self, $ev) = @_; 2391 my ($self, $ev) = @_;
2335 2392
2336 my $sym = $ev->{sym}; 2393 my $sym = $ev->{sym};
2337 2394
2338 if ($sym == CFPlus::SDLK_UP) { 2395 if ($sym == DC::SDLK_UP) {
2339 $self->move_cursor_ver (-1); 2396 $self->move_cursor_ver (-1);
2340 } elsif ($sym == CFPlus::SDLK_DOWN) { 2397 } elsif ($sym == DC::SDLK_DOWN) {
2341 $self->move_cursor_ver (+1); 2398 $self->move_cursor_ver (+1);
2342 } else { 2399 } else {
2343 return $self->SUPER::invoke_key_down ($ev) 2400 return $self->SUPER::invoke_key_down ($ev)
2344 } 2401 }
2345 2402
2346 1 2403 1
2347} 2404}
2348 2405
2349############################################################################# 2406#############################################################################
2350 2407
2351package CFPlus::UI::ButtonBin; 2408package DC::UI::ButtonBin;
2352 2409
2353our @ISA = CFPlus::UI::Bin::; 2410our @ISA = DC::UI::Bin::;
2354 2411
2355use CFPlus::OpenGL; 2412use DC::OpenGL;
2356 2413
2357my @tex = 2414my @tex =
2358 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2415 map { new_from_resource DC::Texture $_, mipmap => 1 }
2359 qw(b1_button_inactive.png b1_button_active.png); 2416 qw(b1_button_inactive.png b1_button_active.png);
2360 2417
2361sub new { 2418sub new {
2362 my $class = shift; 2419 my $class = shift;
2363 2420
2364 $class->SUPER::new ( 2421 $class->SUPER::new (
2365 can_hover => 1, 2422 can_hover => 1,
2366 align => 0, 2423 align => 0.5,
2367 valign => 0, 2424 valign => 0.5,
2368 can_events => 1, 2425 can_events => 1,
2369 @_ 2426 @_
2370 ) 2427 )
2371} 2428}
2372 2429
2395 $self->SUPER::_draw; 2452 $self->SUPER::_draw;
2396} 2453}
2397 2454
2398############################################################################# 2455#############################################################################
2399 2456
2400package CFPlus::UI::Button; 2457package DC::UI::Button;
2401 2458
2402our @ISA = CFPlus::UI::Label::; 2459our @ISA = DC::UI::Label::;
2403 2460
2404use CFPlus::OpenGL; 2461use DC::OpenGL;
2405 2462
2406my @tex = 2463my @tex =
2407 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2464 map { new_from_resource DC::Texture $_, mipmap => 1 }
2408 qw(b1_button_inactive.png b1_button_active.png); 2465 qw(b1_button_inactive.png b1_button_active.png);
2409 2466
2410sub new { 2467sub new {
2411 my $class = shift; 2468 my $class = shift;
2412 2469
2413 $class->SUPER::new ( 2470 $class->SUPER::new (
2414 padding_x => 4, 2471 padding_x => 8,
2415 padding_y => 4, 2472 padding_y => 4,
2416 fg => [1.0, 1.0, 1.0], 2473 fg => [1.0, 1.0, 1.0],
2417 active_fg => [0.8, 0.8, 0.8], 2474 active_fg => [0.8, 0.8, 0.8],
2418 can_hover => 1, 2475 can_hover => 1,
2419 align => 0, 2476 align => 0.5,
2420 valign => 0, 2477 valign => 0.5,
2421 can_events => 1, 2478 can_events => 1,
2422 @_ 2479 @_
2423 ) 2480 )
2424} 2481}
2425 2482
2450 $self->SUPER::_draw; 2507 $self->SUPER::_draw;
2451} 2508}
2452 2509
2453############################################################################# 2510#############################################################################
2454 2511
2455package CFPlus::UI::CheckBox; 2512package DC::UI::CheckBox;
2456 2513
2457our @ISA = CFPlus::UI::DrawBG::; 2514our @ISA = DC::UI::DrawBG::;
2458 2515
2459my @tex = 2516my @tex =
2460 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2517 map { new_from_resource DC::Texture $_, mipmap => 1 }
2461 qw(c1_checkbox_bg.png c1_checkbox_active.png); 2518 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2462 2519
2463use CFPlus::OpenGL; 2520use DC::OpenGL;
2464 2521
2465sub new { 2522sub new {
2466 my $class = shift; 2523 my $class = shift;
2467 2524
2468 $class->SUPER::new ( 2525 $class->SUPER::new (
2508sub _draw { 2565sub _draw {
2509 my ($self) = @_; 2566 my ($self) = @_;
2510 2567
2511 $self->SUPER::_draw; 2568 $self->SUPER::_draw;
2512 2569
2513 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2570 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2514 2571
2515 my ($w, $h) = @$self{qw(w h)}; 2572 my ($w, $h) = @$self{qw(w h)};
2516 2573
2517 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2574 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2518 2575
2525 glDisable GL_TEXTURE_2D; 2582 glDisable GL_TEXTURE_2D;
2526} 2583}
2527 2584
2528############################################################################# 2585#############################################################################
2529 2586
2530package CFPlus::UI::Image; 2587package DC::UI::Image;
2531 2588
2532our @ISA = CFPlus::UI::Base::; 2589our @ISA = DC::UI::Base::;
2533 2590
2534use CFPlus::OpenGL; 2591use DC::OpenGL;
2535 2592
2536our %texture_cache; 2593our %texture_cache;
2537 2594
2538sub new { 2595sub new {
2539 my $class = shift; 2596 my $class = shift;
2546 2603
2547 $self->{path} || $self->{tex} 2604 $self->{path} || $self->{tex}
2548 or Carp::croak "'path' or 'tex' attributes required"; 2605 or Carp::croak "'path' or 'tex' attributes required";
2549 2606
2550 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2607 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2551 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2608 new_from_resource DC::Texture $self->{path}, mipmap => 1;
2552 2609
2553 CFPlus::weaken $texture_cache{$self->{path}}; 2610 DC::weaken $texture_cache{$self->{path}};
2554 2611
2555 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2612 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2556 2613
2557 $self 2614 $self
2558} 2615}
2559 2616
2560sub STORABLE_freeze { 2617sub STORABLE_freeze {
2561 my ($self, $cloning) = @_; 2618 my ($self, $cloning) = @_;
2562 2619
2563 $self->{path} 2620 $self->{path}
2564 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; 2621 or die "cannot serialise DC::UI::Image on non-loadable images\n";
2565 2622
2566 $self->{path} 2623 $self->{path}
2567} 2624}
2568 2625
2569sub STORABLE_attach { 2626sub STORABLE_attach {
2600 glDisable GL_TEXTURE_2D; 2657 glDisable GL_TEXTURE_2D;
2601} 2658}
2602 2659
2603############################################################################# 2660#############################################################################
2604 2661
2605package CFPlus::UI::ImageButton; 2662package DC::UI::ImageButton;
2606 2663
2607our @ISA = CFPlus::UI::Image::; 2664our @ISA = DC::UI::Image::;
2608 2665
2609use CFPlus::OpenGL; 2666use DC::OpenGL;
2610 2667
2611my %textures; 2668my %textures;
2612 2669
2613sub new { 2670sub new {
2614 my $class = shift; 2671 my $class = shift;
2617 padding_x => 4, 2674 padding_x => 4,
2618 padding_y => 4, 2675 padding_y => 4,
2619 fg => [1, 1, 1], 2676 fg => [1, 1, 1],
2620 active_fg => [0, 0, 1], 2677 active_fg => [0, 0, 1],
2621 can_hover => 1, 2678 can_hover => 1,
2622 align => 0, 2679 align => 0.5,
2623 valign => 0, 2680 valign => 0.5,
2624 can_events => 1, 2681 can_events => 1,
2625 @_ 2682 @_
2626 ); 2683 );
2627} 2684}
2628 2685
2642 1 2699 1
2643} 2700}
2644 2701
2645############################################################################# 2702#############################################################################
2646 2703
2647package CFPlus::UI::VGauge; 2704package DC::UI::VGauge;
2648 2705
2649our @ISA = CFPlus::UI::Base::; 2706our @ISA = DC::UI::Base::;
2650 2707
2651use List::Util qw(min max); 2708use List::Util qw(min max);
2652 2709
2653use CFPlus::OpenGL; 2710use DC::OpenGL;
2654 2711
2655my %tex = ( 2712my %tex = (
2656 food => [ 2713 food => [
2657 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2714 map { new_from_resource DC::Texture $_, mipmap => 1 }
2658 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 2715 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2659 ], 2716 ],
2660 grace => [ 2717 grace => [
2661 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2718 map { new_from_resource DC::Texture $_, mipmap => 1 }
2662 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ 2719 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2663 ], 2720 ],
2664 hp => [ 2721 hp => [
2665 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2722 map { new_from_resource DC::Texture $_, mipmap => 1 }
2666 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 2723 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2667 ], 2724 ],
2668 mana => [ 2725 mana => [
2669 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } 2726 map { new_from_resource DC::Texture $_, mipmap => 1 }
2670 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ 2727 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2671 ], 2728 ],
2672); 2729);
2673 2730
2674# eg. VGauge->new (gauge => 'food'), default gauge: food 2731# eg. VGauge->new (gauge => 'food'), default gauge: food
2777 glDisable GL_TEXTURE_2D; 2834 glDisable GL_TEXTURE_2D;
2778} 2835}
2779 2836
2780############################################################################# 2837#############################################################################
2781 2838
2782package CFPlus::UI::Progress; 2839package DC::UI::Progress;
2783 2840
2784our @ISA = CFPlus::UI::Label::; 2841our @ISA = DC::UI::Label::;
2785 2842
2786use CFPlus::OpenGL; 2843use DC::OpenGL;
2787 2844
2788sub new { 2845sub new {
2789 my ($class, %arg) = @_; 2846 my ($class, %arg) = @_;
2790 2847
2791 my $self = $class->SUPER::new ( 2848 my $self = $class->SUPER::new (
2849 padding_x => 2,
2850 padding_y => 2,
2792 fg => [1, 1, 1], 2851 fg => [1, 1, 1],
2793 bg => [0, 0, 1, 0.2], 2852 bg => [0, 0, 1, 0.2],
2794 bar => [0.7, 0.5, 0.1, 0.8], 2853 bar => [0.7, 0.5, 0.1, 0.8],
2795 outline => [0.4, 0.3, 0], 2854 outline => [0.4, 0.3, 0],
2796 fontsize => 0.9, 2855 fontsize => 0.9,
2797 valign => 0, 2856 valign => 0.5,
2798 align => 0, 2857 align => 0.5,
2799 can_events => 1, 2858 can_events => 1,
2800 ellipsise => 1, 2859 ellipsise => 1,
2801 label => "%d%%", 2860 label => "%d%%",
2802 %arg, 2861 %arg,
2803 ); 2862 );
2811 my ($self, $label) = @_; 2870 my ($self, $label) = @_;
2812 2871
2813 return if $self->{label} eq $label; 2872 return if $self->{label} eq $label;
2814 $self->{label} = $label; 2873 $self->{label} = $label;
2815 2874
2816 $self->CFPlus::UI::Progress::set_value (0 + delete $self->{value}); 2875 $self->DC::UI::Progress::set_value (0 + delete $self->{value});
2817} 2876}
2818 2877
2819sub set_value { 2878sub set_value {
2820 my ($self, $value) = @_; 2879 my ($self, $value) = @_;
2821 2880
2836 my ($self) = @_; 2895 my ($self) = @_;
2837 2896
2838 glEnable GL_BLEND; 2897 glEnable GL_BLEND;
2839 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2898 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2840 2899
2900 my $px = $self->{padding_x};
2901 my $py = $self->{padding_y};
2902
2841 if ($self->{value} >= 0) { 2903 if ($self->{value} >= 0) {
2842 my $s = 3 + ($self->{w} - 6) * $self->{value}; 2904 my $s = int $px + ($self->{w} - $px * 2) * $self->{value};
2843 2905
2844 glColor_premultiply @{$self->{bar}}; 2906 glColor_premultiply @{$self->{bar}};
2845 glRect 2, 2, $s, $self->{h} - 2; 2907 glRect $px, $py, $s, $self->{h} - $py;
2846 glColor_premultiply @{$self->{bg}}; 2908 glColor_premultiply @{$self->{bg}};
2847 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2; 2909 glRect $s , $py, $self->{w} - $px, $self->{h} - $py;
2848 } 2910 }
2849 2911
2850 glColor_premultiply @{$self->{outline}}; 2912 glColor_premultiply @{$self->{outline}};
2913
2914 $px -= .5;
2915 $py -= .5;
2916
2851 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2; 2917 glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py;
2852 2918
2853 glDisable GL_BLEND; 2919 glDisable GL_BLEND;
2854 2920
2855 { 2921 {
2856 local $self->{bg}; # do not draw background 2922 local $self->{bg}; # do not draw background
2858 } 2924 }
2859} 2925}
2860 2926
2861############################################################################# 2927#############################################################################
2862 2928
2863package CFPlus::UI::ExperienceProgress; 2929package DC::UI::ExperienceProgress;
2864 2930
2865our @ISA = CFPlus::UI::Progress::; 2931our @ISA = DC::UI::Progress::;
2866 2932
2867sub new { 2933sub new {
2868 my ($class, %arg) = @_; 2934 my ($class, %arg) = @_;
2869 2935
2936 my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : "";
2937
2870 my $self = $class->SUPER::new ( 2938 my $self = $class->SUPER::new (
2939 %arg,
2871 tooltip => sub { 2940 tooltip => sub {
2872 my ($self) = @_; 2941 my ($self) = @_;
2873 2942
2874 sprintf "level %d\n%s points\n%s next level\n%s to go", 2943 sprintf "%slevel %d\n%s points\n%s next level\n%s to go",
2944 $tt,
2875 $self->{lvl}, 2945 $self->{lvl},
2876 ::formsep ($self->{exp}), 2946 ::formsep ($self->{exp}),
2877 ::formsep ($self->{nxt}), 2947 ::formsep ($self->{nxt}),
2878 ::formsep ($self->{nxt} - $self->{exp}), 2948 ::formsep ($self->{nxt} - $self->{exp}),
2879 }, 2949 },
2880 %arg
2881 ); 2950 );
2882 2951
2883 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } 2952 $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) }
2884 if $::CONN; 2953 if $::CONN;
2885 2954
2915 $self->SUPER::set_value ($v); 2984 $self->SUPER::set_value ($v);
2916} 2985}
2917 2986
2918############################################################################# 2987#############################################################################
2919 2988
2920package CFPlus::UI::Gauge; 2989package DC::UI::Gauge;
2921 2990
2922our @ISA = CFPlus::UI::VBox::; 2991our @ISA = DC::UI::VBox::;
2923 2992
2924sub new { 2993sub new {
2925 my ($class, %arg) = @_; 2994 my ($class, %arg) = @_;
2926 2995
2927 my $self = $class->SUPER::new ( 2996 my $self = $class->SUPER::new (
2929 can_hover => 1, 2998 can_hover => 1,
2930 can_events => 1, 2999 can_events => 1,
2931 %arg, 3000 %arg,
2932 ); 3001 );
2933 3002
2934 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); 3003 $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999");
2935 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 3004 $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2936 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); 3005 $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999");
2937 3006
2938 $self 3007 $self
2939} 3008}
2940 3009
2941sub set_fontsize { 3010sub set_fontsize {
2962 $self->{value}->set_text ($val); 3031 $self->{value}->set_text ($val);
2963} 3032}
2964 3033
2965############################################################################# 3034#############################################################################
2966 3035
2967package CFPlus::UI::Slider; 3036package DC::UI::Slider;
2968 3037
2969use strict; 3038use strict;
2970 3039
2971use CFPlus::OpenGL; 3040use DC::OpenGL;
2972 3041
2973our @ISA = CFPlus::UI::DrawBG::; 3042our @ISA = DC::UI::DrawBG::;
2974 3043
2975my @tex = 3044my @tex =
2976 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } 3045 map { new_from_resource DC::Texture $_ }
2977 qw(s1_slider.png s1_slider_bg.png); 3046 qw(s1_slider.png s1_slider_bg.png);
2978 3047
2979sub new { 3048sub new {
2980 my $class = shift; 3049 my $class = shift;
2981 3050
3018sub set_value { 3087sub set_value {
3019 my ($self, $value) = @_; 3088 my ($self, $value) = @_;
3020 3089
3021 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; 3090 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
3022 3091
3023 $hi = $lo + 1 if $hi <= $lo; 3092 $hi = $lo if $hi < $lo;
3024 3093
3025 $page = $hi - $lo if $page > $hi - $lo; 3094 $value = $hi - $page if $value > $hi - $page;
3026
3027 $value = $lo if $value < $lo; 3095 $value = $lo if $value < $lo;
3028 $value = $hi - $page if $value > $hi - $page;
3029 3096
3030 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit 3097 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
3031 if $unit; 3098 if $unit;
3032 3099
3033 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 3100 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
3077sub invoke_mouse_wheel { 3144sub invoke_mouse_wheel {
3078 my ($self, $ev) = @_; 3145 my ($self, $ev) = @_;
3079 3146
3080 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 3147 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
3081 3148
3082 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; 3149 my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2;
3083 3150
3084 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); 3151 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
3085 3152
3086 1 3153 1
3087} 3154}
3097 my ($self) = @_; 3164 my ($self) = @_;
3098 3165
3099 unless ($self->{knob_w}) { 3166 unless ($self->{knob_w}) {
3100 $self->set_value ($self->{range}[0]); 3167 $self->set_value ($self->{range}[0]);
3101 3168
3102 my ($value, $lo, $hi, $page) = @{$self->{range}}; 3169 my ($value, $lo, $hi, $page, $unit) = @{$self->{range}};
3103 my $range = ($hi - $page - $lo) || 1e-100; 3170 my $range = ($hi - $page - $lo) || 1e-10;
3104 3171
3105 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; 3172 my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w};
3106 3173
3107 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; 3174 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
3108 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; 3175 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
3109 3176
3110 $value = ($value - $lo) / $range; 3177 $value = ($value - $lo) / $range;
3140 glDisable GL_TEXTURE_2D; 3207 glDisable GL_TEXTURE_2D;
3141} 3208}
3142 3209
3143############################################################################# 3210#############################################################################
3144 3211
3145package CFPlus::UI::ValSlider; 3212package DC::UI::ValSlider;
3146 3213
3147our @ISA = CFPlus::UI::HBox::; 3214our @ISA = DC::UI::HBox::;
3148 3215
3149sub new { 3216sub new {
3150 my ($class, %arg) = @_; 3217 my ($class, %arg) = @_;
3151 3218
3152 my $range = delete $arg{range}; 3219 my $range = delete $arg{range};
3153 3220
3154 my $self = $class->SUPER::new ( 3221 my $self = $class->SUPER::new (
3155 slider => (new CFPlus::UI::Slider expand => 1, range => $range), 3222 slider => (new DC::UI::Slider expand => 1, range => $range),
3156 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), 3223 entry => (new DC::UI::Label text => "", template => delete $arg{template}),
3157 to_value => sub { shift }, 3224 to_value => sub { shift },
3158 from_value => sub { shift }, 3225 from_value => sub { shift },
3159 %arg, 3226 %arg,
3160 ); 3227 );
3161 3228
3181sub set_range { shift->{slider}->set_range (@_) } 3248sub set_range { shift->{slider}->set_range (@_) }
3182sub set_value { shift->{slider}->set_value (@_) } 3249sub set_value { shift->{slider}->set_value (@_) }
3183 3250
3184############################################################################# 3251#############################################################################
3185 3252
3186package CFPlus::UI::TextScroller; 3253package DC::UI::TextScroller;
3187 3254
3188our @ISA = CFPlus::UI::HBox::; 3255our @ISA = DC::UI::HBox::;
3189 3256
3190use CFPlus::OpenGL; 3257use DC::OpenGL;
3191 3258
3192sub new { 3259sub new {
3193 my $class = shift; 3260 my $class = shift;
3194 3261
3195 my $self = $class->SUPER::new ( 3262 my $self = $class->SUPER::new (
3197 can_events => 1, 3264 can_events => 1,
3198 indent => 0, 3265 indent => 0,
3199 #font => default_font 3266 #font => default_font
3200 @_, 3267 @_,
3201 3268
3202 layout => (new CFPlus::Layout), 3269 layout => (new DC::Layout),
3203 par => [], 3270 par => [],
3204 max_par => 0, 3271 max_par => 0,
3205 height => 0, 3272 height => 0,
3206 children => [ 3273 children => [
3207 (new CFPlus::UI::Empty expand => 1), 3274 (new DC::UI::Empty expand => 1),
3208 (new CFPlus::UI::Slider vertical => 1), 3275 (new DC::UI::Slider vertical => 1),
3209 ], 3276 ],
3210 ); 3277 );
3211 3278
3212 $self->{children}[1]->connect (changed => sub { $self->update }); 3279 $self->{children}[1]->connect (changed => sub { $self->update });
3213 3280
3269 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 3336 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
3270 $layout->set_markup ($para->{markup}); 3337 $layout->set_markup ($para->{markup});
3271 3338
3272 $layout->set_shapes ( 3339 $layout->set_shapes (
3273 map 3340 map
3274 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), 3341 +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
3275 @{$para->{widget}} 3342 @{$para->{widget}}
3276 ); 3343 );
3277 3344
3278 $layout 3345 $layout
3279} 3346}
3396 $ROOT->on_post_alloc ($self => sub { 3463 $ROOT->on_post_alloc ($self => sub {
3397 $self->force_uptodate; 3464 $self->force_uptodate;
3398 3465
3399 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 3466 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3400 3467
3401 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { 3468 $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub {
3402 glClearColor 0, 0, 0, 0; 3469 glClearColor 0, 0, 0, 0;
3403 glClear GL_COLOR_BUFFER_BIT; 3470 glClear GL_COLOR_BUFFER_BIT;
3404 3471
3405 package CFPlus::UI::Base; 3472 package DC::UI::Base;
3406 local ($draw_x, $draw_y, $draw_w, $draw_h) = 3473 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3407 (0, 0, $self->{w}, $self->{h}); 3474 (0, 0, $self->{w}, $self->{h});
3408 3475
3409 my $top = int $self->{children}[1]{range}[0]; 3476 my $top = int $self->{children}[1]{range}[0];
3410 3477
3469 $self->{children}[1]->draw; 3536 $self->{children}[1]->draw;
3470} 3537}
3471 3538
3472############################################################################# 3539#############################################################################
3473 3540
3474package CFPlus::UI::Animator; 3541package DC::UI::Animator;
3475 3542
3476use CFPlus::OpenGL; 3543use DC::OpenGL;
3477 3544
3478our @ISA = CFPlus::UI::Bin::; 3545our @ISA = DC::UI::Bin::;
3479 3546
3480sub moveto { 3547sub moveto {
3481 my ($self, $x, $y) = @_; 3548 my ($self, $x, $y) = @_;
3482 3549
3483 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 3550 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3511 glPopMatrix; 3578 glPopMatrix;
3512} 3579}
3513 3580
3514############################################################################# 3581#############################################################################
3515 3582
3516package CFPlus::UI::Flopper; 3583package DC::UI::Flopper;
3517 3584
3518our @ISA = CFPlus::UI::Button::; 3585our @ISA = DC::UI::Button::;
3519 3586
3520sub new { 3587sub new {
3521 my $class = shift; 3588 my $class = shift;
3522 3589
3523 my $self = $class->SUPER::new ( 3590 my $self = $class->SUPER::new (
3535 $self->{other}->toggle_visibility; 3602 $self->{other}->toggle_visibility;
3536} 3603}
3537 3604
3538############################################################################# 3605#############################################################################
3539 3606
3540package CFPlus::UI::Tooltip; 3607package DC::UI::Tooltip;
3541 3608
3542our @ISA = CFPlus::UI::Bin::; 3609our @ISA = DC::UI::Bin::;
3543 3610
3544use CFPlus::OpenGL; 3611use DC::OpenGL;
3545 3612
3546sub new { 3613sub new {
3547 my $class = shift; 3614 my $class = shift;
3548 3615
3549 $class->SUPER::new ( 3616 $class->SUPER::new (
3556 my ($self, $widget) = @_; 3623 my ($self, $widget) = @_;
3557 3624
3558 my $tip = $widget->{tooltip}; 3625 my $tip = $widget->{tooltip};
3559 $tip = $tip->($widget) if "CODE" eq ref $tip; 3626 $tip = $tip->($widget) if "CODE" eq ref $tip;
3560 3627
3561 $tip = CFPlus::Pod::section_label tooltip => $1 3628 $tip = DC::Pod::section_label tooltip => $1
3562 if $tip =~ /^#(.*)$/; 3629 if $tip =~ /^#(.*)$/;
3563 3630
3564 if ($ENV{CFPLUS_DEBUG} & 2) { 3631 if ($ENV{CFPLUS_DEBUG} & 2) {
3565 $tip .= "\n\n" . (ref $widget) . "\n" 3632 $tip .= "\n\n" . (ref $widget) . "\n"
3566 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" 3633 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3569 } 3636 }
3570 3637
3571 $tip =~ s/^\n+//; 3638 $tip =~ s/^\n+//;
3572 $tip =~ s/\n+$//; 3639 $tip =~ s/\n+$//;
3573 3640
3574 $self->add (new CFPlus::UI::Label 3641 $self->add (new DC::UI::Label
3642 fg => $DC::THEME{tooltip_fg},
3575 markup => $tip, 3643 markup => $tip,
3576 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, 3644 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3645 align => 0,
3577 fontsize => 0.8, 3646 fontsize => 0.8,
3578 style => 1, # FLAG_INVERSE 3647 style => $DC::THEME{tooltip_style}, # FLAG_INVERSE
3579 ellipsise => 0, 3648 ellipsise => 0,
3580 font => ($widget->{tooltip_font} || $::FONT_PROP), 3649 font => ($widget->{tooltip_font} || $::FONT_PROP),
3581 ); 3650 );
3582} 3651}
3583 3652
3618} 3687}
3619 3688
3620sub _draw { 3689sub _draw {
3621 my ($self) = @_; 3690 my ($self) = @_;
3622 3691
3623 glTranslate 0.375, 0.375;
3624
3625 my ($w, $h) = @$self{qw(w h)}; 3692 my ($w, $h) = @$self{qw(w h)};
3626 3693
3627 glColor 1, 0.8, 0.4; 3694 glColor @{ $DC::THEME{tooltip_bg} };
3628 glRect 0, 0, $w, $h; 3695 glRect 0, 0, $w, $h;
3629 3696
3630 glColor 0, 0, 0; 3697 glColor @{ $DC::THEME{tooltip_border} };
3631 glRect_lineloop 0, 0, $w, $h; 3698 glRect_lineloop .5, .5, $w + .5, $h + .5;
3632 3699
3633 glTranslate 2 - 0.375, 2 - 0.375; 3700 glTranslate 2, 2;
3634 3701
3635 $self->SUPER::_draw; 3702 $self->SUPER::_draw;
3636} 3703}
3637 3704
3638############################################################################# 3705#############################################################################
3639 3706
3640package CFPlus::UI::Face; 3707package DC::UI::Face;
3641 3708
3642our @ISA = CFPlus::UI::DrawBG::; 3709our @ISA = DC::UI::DrawBG::;
3643 3710
3644use CFPlus::OpenGL; 3711use DC::OpenGL;
3645 3712
3646sub new { 3713sub new {
3647 my $class = shift; 3714 my $class = shift;
3648 3715
3649 my $self = $class->SUPER::new ( 3716 my $self = $class->SUPER::new (
3653 can_events => 0, 3720 can_events => 0,
3654 @_, 3721 @_,
3655 ); 3722 );
3656 3723
3657 if ($self->{anim} && $self->{animspeed}) { 3724 if ($self->{anim} && $self->{animspeed}) {
3658 CFPlus::weaken (my $widget = $self); 3725 DC::weaken (my $widget = $self);
3659 3726
3660 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3727 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3661 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3728 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3662 $self->{timer} = Event->timer (
3663 parked => 1,
3664 cb => sub {
3665 return unless $::CONN; 3729 return unless $::CONN;
3666 3730
3667 my $w = $widget 3731 my $w = $widget
3668 or return; 3732 or return;
3669 3733
3670 ++$w->{frame}; 3734 ++$w->{frame};
3671 $w->update_face; 3735 $w->update_face;
3672 3736
3673 # somehow, $widget can go away 3737 # somehow, $widget can go away
3674 $w->update; 3738 $w->update;
3675 $w->update_timer; 3739 $w->update_timer;
3676 },
3677 ); 3740 };
3678 3741
3679 $self->update_face; 3742 $self->update_face;
3680 $self->update_timer; 3743 $self->update_timer;
3681 } 3744 }
3682 3745
3687 my ($self) = @_; 3750 my ($self) = @_;
3688 3751
3689 return unless $self->{timer}; 3752 return unless $self->{timer};
3690 3753
3691 if ($self->{visible}) { 3754 if ($self->{visible}) {
3692 $self->{timer}->at (
3693 $self->{anim_start}
3694 + $self->{animspeed}
3695 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3696 );
3697 $self->{timer}->start; 3755 $self->{timer}->start;
3698 } else { 3756 } else {
3699 $self->{timer}->stop; 3757 $self->{timer}->stop;
3700 } 3758 }
3701} 3759}
3778 $self->SUPER::destroy; 3836 $self->SUPER::destroy;
3779} 3837}
3780 3838
3781############################################################################# 3839#############################################################################
3782 3840
3783package CFPlus::UI::Buttonbar; 3841package DC::UI::Buttonbar;
3784 3842
3785our @ISA = CFPlus::UI::HBox::; 3843our @ISA = DC::UI::HBox::;
3786 3844
3787# TODO: should actually wrap buttons and other goodies. 3845# TODO: should actually wrap buttons and other goodies.
3788 3846
3789############################################################################# 3847#############################################################################
3790 3848
3791package CFPlus::UI::Menu; 3849package DC::UI::Menu;
3792 3850
3793our @ISA = CFPlus::UI::Toplevel::; 3851our @ISA = DC::UI::Toplevel::;
3794 3852
3795use CFPlus::OpenGL; 3853use DC::OpenGL;
3796 3854
3797sub new { 3855sub new {
3798 my $class = shift; 3856 my $class = shift;
3799 3857
3800 my $self = $class->SUPER::new ( 3858 my $self = $class->SUPER::new (
3801 items => [], 3859 items => [],
3802 z => 100, 3860 z => 100,
3803 @_, 3861 @_,
3804 ); 3862 );
3805 3863
3806 $self->add ($self->{vbox} = new CFPlus::UI::VBox); 3864 $self->add ($self->{vbox} = new DC::UI::VBox);
3807 3865
3808 for my $item (@{ $self->{items} }) { 3866 for my $item (@{ $self->{items} }) {
3809 my ($widget, $cb, $tooltip) = @$item; 3867 my ($widget, $cb, $tooltip) = @$item;
3810 3868
3811 # handle various types of items, only text for now 3869 # handle various types of items, only text for now
3812 if (!ref $widget) { 3870 if (!ref $widget) {
3813 if ($widget =~ /\t/) { 3871 if ($widget =~ /\t/) {
3814 my ($left, $right) = split /\t/, $widget, 2; 3872 my ($left, $right) = split /\t/, $widget, 2;
3815 3873
3816 $widget = new CFPlus::UI::HBox 3874 $widget = new DC::UI::HBox
3817 can_hover => 1, 3875 can_hover => 1,
3818 can_events => 1, 3876 can_events => 1,
3819 tooltip => $tooltip, 3877 tooltip => $tooltip,
3820 children => [ 3878 children => [
3821 (new CFPlus::UI::Label markup => $left, expand => 1), 3879 (new DC::UI::Label markup => $left , align => 0, expand => 1),
3822 (new CFPlus::UI::Label markup => $right, align => +1), 3880 (new DC::UI::Label markup => $right, align => 1),
3823 ], 3881 ],
3824 ; 3882 ;
3825 3883
3826 } else { 3884 } else {
3827 $widget = new CFPlus::UI::Label 3885 $widget = new DC::UI::Label
3828 can_hover => 1, 3886 can_hover => 1,
3829 can_events => 1, 3887 can_events => 1,
3888 align => 0,
3830 markup => $widget, 3889 markup => $widget,
3831 tooltip => $tooltip; 3890 tooltip => $tooltip;
3832 } 3891 }
3833 } 3892 }
3834 3893
3850 $GRAB = $self; 3909 $GRAB = $self;
3851 $self->{button} = $ev->{button}; 3910 $self->{button} = $ev->{button};
3852 3911
3853 $self->show; 3912 $self->show;
3854 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 3913 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
3914
3915 1 # so it can be used inside event handlers
3855} 3916}
3856 3917
3857sub invoke_mouse_motion { 3918sub invoke_mouse_motion {
3858 my ($self, $ev, $x, $y) = @_; 3919 my ($self, $ev, $x, $y) = @_;
3859 3920
3880 1 3941 1
3881} 3942}
3882 3943
3883############################################################################# 3944#############################################################################
3884 3945
3885package CFPlus::UI::Multiplexer; 3946package DC::UI::Multiplexer;
3886 3947
3887our @ISA = CFPlus::UI::Container::; 3948our @ISA = DC::UI::Container::;
3888 3949
3889sub new { 3950sub new {
3890 my $class = shift; 3951 my $class = shift;
3891 3952
3892 my $self = $class->SUPER::new ( 3953 my $self = $class->SUPER::new (
3893 @_, 3954 @_,
3894 ); 3955 );
3895 3956
3896 $self->{current} = $self->{children}[0] 3957 $self->set_current_page (0);
3897 if @{ $self->{children} };
3898 3958
3899 $self 3959 $self
3900} 3960}
3901 3961
3902sub add { 3962sub add {
3903 my ($self, @widgets) = @_; 3963 my ($self, @widgets) = @_;
3904 3964
3905 $self->SUPER::add (@widgets); 3965 $self->SUPER::add (@widgets);
3906 3966
3907 $self->{current} = $self->{children}[0] 3967 $self->set_current_page (0)
3908 if @{ $self->{children} }; 3968 if @widgets == @{ $self->{children} };
3909} 3969}
3910 3970
3911sub get_current_page { 3971sub get_current_page {
3912 my ($self) = @_; 3972 my ($self) = @_;
3913 3973
3919 3979
3920 my $widget = ref $page_or_widget 3980 my $widget = ref $page_or_widget
3921 ? $page_or_widget 3981 ? $page_or_widget
3922 : $self->{children}[$page_or_widget]; 3982 : $self->{children}[$page_or_widget];
3923 3983
3984 $self->{current}->set_invisible if $self->{current} && $self->{visible};
3985
3924 $self->{current} = $widget; 3986 if (($self->{current} = $widget)) {
3987 $self->{current}->set_visible if $self->{current} && $self->{visible};
3925 $self->{current}->configure (0, 0, $self->{w}, $self->{h}); 3988 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3926 3989
3927 $self->emit (page_changed => $self->{current}); 3990 $self->emit (page_changed => $self->{current});
3991 }
3928 3992
3929 $self->realloc; 3993 $self->realloc;
3930} 3994}
3931 3995
3932sub visible_children { 3996sub visible_children {
3933 $_[0]{current} 3997 $_[0]{current} || ()
3934} 3998}
3935 3999
3936sub size_request { 4000sub size_request {
3937 my ($self) = @_; 4001 my ($self) = @_;
3938 4002
4003 $self->{current}
3939 $self->{current}->size_request 4004 ? $self->{current}->size_request
4005 : (0, 0)
3940} 4006}
3941 4007
3942sub invoke_size_allocate { 4008sub invoke_size_allocate {
3943 my ($self, $w, $h) = @_; 4009 my ($self, $w, $h) = @_;
3944 4010
3945 $self->{current}->configure (0, 0, $w, $h); 4011 $self->{current}->configure (0, 0, $w, $h)
4012 if $self->{current};
3946 4013
3947 1 4014 1
3948} 4015}
3949 4016
3950sub _draw { 4017sub _draw {
3951 my ($self) = @_; 4018 my ($self) = @_;
3952 4019
3953 $self->{current}->draw; 4020 $self->{current}->draw
4021 if $self->{current};
3954} 4022}
3955 4023
3956############################################################################# 4024#############################################################################
3957 4025
3958package CFPlus::UI::Notebook; 4026package DC::UI::Notebook;
3959 4027
3960use CFPlus::OpenGL; 4028use DC::OpenGL;
3961 4029
3962our @ISA = CFPlus::UI::VBox::; 4030our @ISA = DC::UI::VBox::;
3963 4031
3964sub new { 4032sub new {
3965 my $class = shift; 4033 my $class = shift;
3966 4034
3967 my $self = $class->SUPER::new ( 4035 my $self = $class->SUPER::new (
3968 buttonbar => (new CFPlus::UI::Buttonbar), 4036 buttonbar => (new DC::UI::Buttonbar),
3969 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 4037 multiplexer => (new DC::UI::Multiplexer expand => 1),
3970 active_outline => [1, 1, 0], 4038 active_outline => [.7, .7, 0.2],
3971 # filter => # will be put between multiplexer and $self 4039 # filter => # will be put between multiplexer and $self
3972 @_, 4040 @_,
3973 ); 4041 );
3974 4042
3975 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 4043 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3983 4051
3984 for my $child (@$widgets) { 4052 for my $child (@$widgets) {
3985 Scalar::Util::weaken $child; 4053 Scalar::Util::weaken $child;
3986 $child->{c_tab_} ||= do { 4054 $child->{c_tab_} ||= do {
3987 my $tab = 4055 my $tab =
3988 (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") 4056 (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base")
3989 ? $child->{c_tab} 4057 ? $child->{c_tab}
3990 : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; 4058 : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1];
3991 4059
3992 $tab->connect (activate => sub { 4060 $tab->connect (activate => sub {
3993 $wself->set_current_page ($child); 4061 $wself->set_current_page ($child);
3994 }); 4062 });
3995 4063
4027sub pages { 4095sub pages {
4028 my ($self) = @_; 4096 my ($self) = @_;
4029 $self->{multiplexer}->children 4097 $self->{multiplexer}->children
4030} 4098}
4031 4099
4100sub page_index {
4101 my ($self, $widget) = @_;
4102
4103 my $i = 0;
4104 for ($self->pages) {
4105 if ($_ eq $widget) { return $i };
4106 $i++;
4107 }
4108
4109 undef
4110}
4111
4032sub add_tab { 4112sub add_tab {
4033 my ($self, $title, $widget, $tooltip) = @_; 4113 my ($self, $title, $widget, $tooltip) = @_;
4034 4114
4035 $title = [$title, $tooltip] unless ref $title; 4115 $title = [$title, $tooltip] unless ref $title;
4036 $widget->{c_tab} = $title; 4116 $widget->{c_tab} = $title;
4056 4136
4057 $self->SUPER::_draw (); 4137 $self->SUPER::_draw ();
4058 4138
4059 if (my $cur = $self->{multiplexer}{current}) { 4139 if (my $cur = $self->{multiplexer}{current}) {
4060 if ($cur = $cur->{c_tab_}) { 4140 if ($cur = $cur->{c_tab_}) {
4061 glTranslate $cur->{x}, $cur->{y}; 4141 glTranslate $self->{buttonbar}{x} + $cur->{x},
4142 $self->{buttonbar}{y} + $cur->{y};
4062 glLineWidth 3; 4143 glLineWidth 3;
4144 #glEnable GL_BLEND;
4145 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4063 glColor @{$self->{active_outline}}; 4146 glColor @{$self->{active_outline}};
4064 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2; 4147 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4065 glLineWidth 1; 4148 glLineWidth 1;
4149 #glDisable GL_BLEND;
4066 } 4150 }
4067 } 4151 }
4068} 4152}
4069 4153
4070############################################################################# 4154#############################################################################
4071 4155
4072package CFPlus::UI::Selector; 4156package DC::UI::Selector;
4073 4157
4074use utf8; 4158use utf8;
4075 4159
4076our @ISA = CFPlus::UI::Button::; 4160our @ISA = DC::UI::Button::;
4077 4161
4078sub new { 4162sub new {
4079 my $class = shift; 4163 my $class = shift;
4080 4164
4081 my $self = $class->SUPER::new ( 4165 my $self = $class->SUPER::new (
4098 my ($value, $title, $tooltip) = @$_; 4182 my ($value, $title, $tooltip) = @$_;
4099 4183
4100 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; 4184 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
4101 } 4185 }
4102 4186
4103 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); 4187 DC::UI::Menu->new (items => \@menu_items)->popup ($ev);
4104} 4188}
4105 4189
4106sub _set_value { 4190sub _set_value {
4107 my ($self, $value) = @_; 4191 my ($self, $value) = @_;
4108 4192
4109 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4193 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4194 $item ||= $self->{options}[0]
4110 or return; 4195 or return;
4111 4196
4112 $self->{value} = $item->[0]; 4197 $self->{value} = $item->[0];
4113 $self->set_markup ("$item->[1] ⇓"); 4198 $self->set_markup ("$item->[1] ⇓");
4114 $self->set_tooltip ($item->[2]); 4199# $self->set_tooltip ($item->[2]);
4115} 4200}
4116 4201
4117sub set_value { 4202sub set_value {
4118 my ($self, $value) = @_; 4203 my ($self, $value) = @_;
4119 4204
4121 4206
4122 $self->_set_value ($value); 4207 $self->_set_value ($value);
4123 $self->emit (changed => $value); 4208 $self->emit (changed => $value);
4124} 4209}
4125 4210
4211sub set_options {
4212 my ($self, $options) = @_;
4213
4214 $self->{options} = $options;
4215 $self->_set_value ($self->{value});
4216}
4217
4126############################################################################# 4218#############################################################################
4127 4219
4128package CFPlus::UI::Statusbox; 4220package DC::UI::Statusbox;
4129 4221
4130our @ISA = CFPlus::UI::VBox::; 4222our @ISA = DC::UI::VBox::;
4131 4223
4132sub new { 4224sub new {
4133 my $class = shift; 4225 my $class = shift;
4134 4226
4135 my $self = $class->SUPER::new ( 4227 my $self = $class->SUPER::new (
4136 fontsize => 0.8, 4228 fontsize => 0.8,
4137 @_, 4229 @_,
4138 ); 4230 );
4139 4231
4140 CFPlus::weaken (my $this = $self); 4232 DC::weaken (my $this = $self);
4141 4233
4142 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4234 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4143 4235
4144 $self 4236 $self
4145} 4237}
4146 4238
4147sub reorder { 4239sub reorder {
4148 my ($self) = @_; 4240 my ($self) = @_;
4149 my $NOW = Time::HiRes::time; 4241 my $NOW = EV::time;
4150 4242
4151 # freeze display when hovering over any label 4243 # freeze display when hovering over any label
4152 return if $CFPlus::UI::TOOLTIP->{owner} 4244 return if $DC::UI::TOOLTIP->{owner}
4153 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, 4245 && grep $DC::UI::TOOLTIP->{owner} == $_->{label},
4154 values %{ $self->{item} }; 4246 values %{ $self->{item} };
4155 4247
4156 while (my ($k, $v) = each %{ $self->{item} }) { 4248 while (my ($k, $v) = each %{ $self->{item} }) {
4157 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4249 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4158 } 4250 }
4251
4252 $self->{timer}->set (1, 1);
4159 4253
4160 my @widgets; 4254 my @widgets;
4161 4255
4162 my @items = sort { 4256 my @items = sort {
4163 $a->{pri} <=> $b->{pri} 4257 $a->{pri} <=> $b->{pri}
4164 or $b->{id} <=> $a->{id} 4258 or $b->{id} <=> $a->{id}
4165 } values %{ $self->{item} }; 4259 } values %{ $self->{item} };
4166
4167 $self->{timer}->interval (1);
4168 4260
4169 my $count = 10 + 1; 4261 my $count = 10 + 1;
4170 for my $item (@items) { 4262 for my $item (@items) {
4171 last unless --$count; 4263 last unless --$count;
4172 4264
4179 for ($short) { 4271 for ($short) {
4180 s/^\s+//; 4272 s/^\s+//;
4181 s/\s+/ /g; 4273 s/\s+/ /g;
4182 } 4274 }
4183 4275
4184 new CFPlus::UI::Label 4276 new DC::UI::Label
4185 markup => $short, 4277 markup => $short,
4186 tooltip => $item->{tooltip}, 4278 tooltip => $item->{tooltip},
4187 tooltip_font => $::FONT_PROP, 4279 tooltip_font => $::FONT_PROP,
4188 tooltip_width => 0.67, 4280 tooltip_width => 0.67,
4189 fontsize => $item->{fontsize} || $self->{fontsize}, 4281 fontsize => $item->{fontsize} || $self->{fontsize},
4190 max_w => $::WIDTH * 0.44, 4282 max_w => $::WIDTH * 0.44,
4283 align => 0,
4191 fg => [@{ $item->{fg} }], 4284 fg => [@{ $item->{fg} }],
4192 can_events => 1, 4285 can_events => 1,
4193 can_hover => 1 4286 can_hover => 1
4194 }; 4287 };
4195 4288
4196 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4289 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4197 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4290 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4198 $label->update; 4291 $label->update;
4199 $label->set_max_size (undef, $label->{req_h} * $diff) 4292 $label->set_max_size (undef, $label->{req_h} * $diff)
4200 if $diff < 1; 4293 if $diff < 1;
4201 $self->{timer}->interval (1/30); 4294 $self->{timer}->set (1/30, 1/30);
4202 } else { 4295 } else {
4203 $label->{fg}[3] = $item->{fg}[3] || 1; 4296 $label->{fg}[3] = $item->{fg}[3] || 1;
4204 } 4297 }
4205 4298
4206 push @widgets, $label; 4299 push @widgets, $label;
4207 } 4300 }
4301
4302 my $hash = join ",", @widgets;
4303 return if $hash eq $self->{last_widget_hash};
4304 $self->{last_widget_hash} = $hash;
4208 4305
4209 $self->clear; 4306 $self->clear;
4210 $self->SUPER::add (reverse @widgets); 4307 $self->SUPER::add (reverse @widgets);
4211} 4308}
4212 4309
4268 $self->SUPER::destroy; 4365 $self->SUPER::destroy;
4269} 4366}
4270 4367
4271############################################################################# 4368#############################################################################
4272 4369
4273package CFPlus::UI::Root; 4370package DC::UI::Root;
4274 4371
4275our @ISA = CFPlus::UI::Container::; 4372our @ISA = DC::UI::Container::;
4276 4373
4277use List::Util qw(min max); 4374use List::Util qw(min max);
4278 4375
4279use CFPlus::OpenGL; 4376use DC::OpenGL;
4280 4377
4281sub new { 4378sub new {
4282 my $class = shift; 4379 my $class = shift;
4283 4380
4284 my $self = $class->SUPER::new ( 4381 my $self = $class->SUPER::new (
4285 visible => 1, 4382 visible => 1,
4286 @_, 4383 @_,
4287 ); 4384 );
4288 4385
4289 CFPlus::weaken ($self->{root} = $self); 4386 DC::weaken ($self->{root} = $self);
4290 4387
4291 $self 4388 $self
4292} 4389}
4293 4390
4294sub size_request { 4391sub size_request {
4342} 4439}
4343 4440
4344sub update { 4441sub update {
4345 my ($self) = @_; 4442 my ($self) = @_;
4346 4443
4347 $::WANT_REFRESH->start; 4444 $::WANT_REFRESH = 1;
4348} 4445}
4349 4446
4350sub add { 4447sub add {
4351 my ($self, @children) = @_; 4448 my ($self, @children) = @_;
4352 4449
4419 4516
4420 delete $queue{$widget+0}; 4517 delete $queue{$widget+0};
4421 4518
4422 my ($w, $h) = $widget->size_request; 4519 my ($w, $h) = $widget->size_request;
4423 4520
4424 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; 4521 $w += $widget->{padding_x} * 2;
4425 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; 4522 $h += $widget->{padding_y} * 2;
4523
4524 $w = max $widget->{min_w}, $w;
4525 $h = max $widget->{min_h}, $h;
4426 4526
4427 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 4527 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4428 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 4528 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4429 4529
4430 $w = $widget->{force_w} if exists $widget->{force_w}; 4530 $w = $widget->{force_w} if exists $widget->{force_w};
4497 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; 4597 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4498 glMatrixMode GL_MODELVIEW; 4598 glMatrixMode GL_MODELVIEW;
4499 glLoadIdentity; 4599 glLoadIdentity;
4500 4600
4501 { 4601 {
4502 package CFPlus::UI::Base; 4602 package DC::UI::Base;
4503 4603
4504 local ($draw_x, $draw_y, $draw_w, $draw_h) = 4604 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4505 (0, 0, $self->{w}, $self->{h}); 4605 (0, 0, $self->{w}, $self->{h});
4506 4606
4507 $self->_draw; 4607 $self->_draw;
4508 } 4608 }
4509} 4609}
4510 4610
4511############################################################################# 4611#############################################################################
4512 4612
4513package CFPlus::UI; 4613package DC::UI;
4514 4614
4515$ROOT = new CFPlus::UI::Root; 4615$ROOT = new DC::UI::Root;
4516$TOOLTIP = new CFPlus::UI::Tooltip z => 900; 4616$TOOLTIP = new DC::UI::Tooltip z => 900;
4517 4617
45181 46181
4519

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines