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