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 | use Event; |
|
|
8 | |
6 | |
9 | use CFPlus; |
7 | use Guard (); |
|
|
8 | |
|
|
9 | use DC; |
10 | use CFPlus::Pod; |
10 | use DC::Pod; |
11 | use CFPlus::Texture; |
11 | use DC::Texture; |
12 | |
12 | |
13 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
14 | |
14 | |
15 | our $LAYOUT; |
15 | our $LAYOUT; |
16 | our $ROOT; |
16 | our $ROOT; |
17 | our $TOOLTIP; |
17 | our $TOOLTIP; |
18 | our $BUTTON_STATE; |
18 | our $BUTTON_STATE; |
19 | |
19 | |
20 | our %WIDGET; # all widgets, weak-referenced |
20 | our %WIDGET; # all widgets, weak-referenced |
21 | |
21 | |
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
22 | our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub { |
|
|
23 | $_[0]->stop; |
|
|
24 | |
23 | if (!$GRAB) { |
25 | if (!$GRAB) { |
24 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
26 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
25 | if (length $widget->{tooltip}) { |
27 | if (length $widget->{tooltip}) { |
26 | if ($TOOLTIP->{owner} != $widget) { |
28 | if ($TOOLTIP->{owner} != $widget) { |
27 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
29 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
… | |
… | |
30 | $TOOLTIP->{owner} = $widget; |
32 | $TOOLTIP->{owner} = $widget; |
31 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
33 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
32 | |
34 | |
33 | return if $ENV{CFPLUS_DEBUG} & 8; |
35 | return if $ENV{CFPLUS_DEBUG} & 8; |
34 | |
36 | |
35 | my $tip = $widget->{tooltip}; |
|
|
36 | |
|
|
37 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
38 | |
|
|
39 | $TOOLTIP->set_tooltip_from ($widget); |
37 | $TOOLTIP->set_tooltip_from ($widget); |
40 | $TOOLTIP->show; |
38 | $TOOLTIP->show; |
41 | } |
39 | } |
42 | |
40 | |
43 | return; |
41 | return; |
… | |
… | |
46 | } |
44 | } |
47 | |
45 | |
48 | $TOOLTIP->hide; |
46 | $TOOLTIP->hide; |
49 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
47 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
50 | delete $TOOLTIP->{owner}; |
48 | delete $TOOLTIP->{owner}; |
51 | }); |
49 | }; |
52 | |
50 | |
53 | sub get_layout { |
51 | sub get_layout { |
54 | my $layout; |
52 | my $layout; |
55 | |
53 | |
56 | for (grep { $_->{name} } values %WIDGET) { |
54 | for (grep { $_->{name} } values %WIDGET) { |
… | |
… | |
91 | my $hover = $HOVER; $HOVER = $widget; |
89 | my $hover = $HOVER; $HOVER = $widget; |
92 | |
90 | |
93 | $hover->update if $hover && $hover->{can_hover}; |
91 | $hover->update if $hover && $hover->{can_hover}; |
94 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
92 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
95 | |
93 | |
96 | $TOOLTIP_WATCHER->start; |
94 | $TOOLTIP_WATCHER->again; |
97 | } |
95 | } |
98 | } |
96 | } |
99 | |
97 | |
100 | sub feed_sdl_button_down_event { |
98 | sub feed_sdl_button_down_event { |
101 | my ($ev) = @_; |
99 | my ($ev) = @_; |
… | |
… | |
107 | my $widget = $ROOT->find_widget ($x, $y); |
105 | my $widget = $ROOT->find_widget ($x, $y); |
108 | |
106 | |
109 | $GRAB = $widget; |
107 | $GRAB = $widget; |
110 | $GRAB->update if $GRAB; |
108 | $GRAB->update if $GRAB; |
111 | |
109 | |
112 | $TOOLTIP_WATCHER->cb->(); |
110 | $TOOLTIP_WATCHER->invoke; |
113 | } |
111 | } |
114 | |
112 | |
115 | if ($GRAB) { |
113 | if ($GRAB) { |
116 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
114 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
117 | # mousewheel |
115 | # mousewheel |
118 | my $delta = $ev->{button} * 2 - 9; |
116 | my $delta = $ev->{button} * 2 - 9; |
119 | my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; |
117 | my $shift = $ev->{mod} & DC::KMOD_SHIFT; |
120 | |
118 | |
121 | $ev->{dx} = $shift ? $delta : 0; |
119 | $ev->{dx} = $shift ? $delta : 0; |
122 | $ev->{dy} = $shift ? 0 : $delta; |
120 | $ev->{dy} = $shift ? 0 : $delta; |
123 | |
121 | |
124 | $GRAB->emit (mouse_wheel => $ev); |
122 | $GRAB->emit (mouse_wheel => $ev); |
… | |
… | |
142 | my $grab = $GRAB; undef $GRAB; |
140 | my $grab = $GRAB; undef $GRAB; |
143 | $grab->update if $grab; |
141 | $grab->update if $grab; |
144 | $GRAB->update if $GRAB; |
142 | $GRAB->update if $GRAB; |
145 | |
143 | |
146 | check_hover $widget; |
144 | check_hover $widget; |
147 | $TOOLTIP_WATCHER->cb->(); |
145 | $TOOLTIP_WATCHER->invoke; |
148 | } |
146 | } |
149 | } |
147 | } |
150 | |
148 | |
151 | sub feed_sdl_motion_event { |
149 | sub feed_sdl_motion_event { |
152 | my ($ev) = @_; |
150 | my ($ev) = @_; |
… | |
… | |
192 | # call when resolution changes etc. |
190 | # call when resolution changes etc. |
193 | sub rescale_widgets { |
191 | sub rescale_widgets { |
194 | my ($sx, $sy) = @_; |
192 | my ($sx, $sy) = @_; |
195 | |
193 | |
196 | for my $widget (values %WIDGET) { |
194 | for my $widget (values %WIDGET) { |
197 | if ($widget->{is_toplevel}) { |
195 | if ($widget->{is_toplevel} || $widget->{c_rescale}) { |
198 | $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
196 | $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
199 | $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
197 | $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
200 | |
198 | |
201 | $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; |
199 | $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; |
202 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
200 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
… | |
… | |
214 | reconfigure_widgets; |
212 | reconfigure_widgets; |
215 | } |
213 | } |
216 | |
214 | |
217 | ############################################################################# |
215 | ############################################################################# |
218 | |
216 | |
219 | package CFPlus::UI::Event; |
217 | package DC::UI::Event; |
220 | |
218 | |
221 | sub xy { |
219 | sub xy { |
222 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
220 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
223 | } |
221 | } |
224 | |
222 | |
225 | ############################################################################# |
223 | ############################################################################# |
226 | |
224 | |
227 | package CFPlus::UI::Base; |
225 | package DC::UI::Base; |
228 | |
226 | |
229 | use strict; |
227 | use strict; |
230 | |
228 | |
231 | use CFPlus::OpenGL; |
229 | use DC::OpenGL; |
232 | |
230 | |
233 | sub new { |
231 | sub new { |
234 | my $class = shift; |
232 | my $class = shift; |
235 | |
233 | |
236 | my $self = bless { |
234 | my $self = bless { |
… | |
… | |
241 | h => undef, |
239 | h => undef, |
242 | can_events => 1, |
240 | can_events => 1, |
243 | @_ |
241 | @_ |
244 | }, $class; |
242 | }, $class; |
245 | |
243 | |
246 | CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
244 | DC::weaken ($DC::UI::WIDGET{$self+0} = $self); |
247 | |
245 | |
248 | for (keys %$self) { |
246 | for (keys %$self) { |
249 | if (/^on_(.*)$/) { |
247 | if (/^on_(.*)$/) { |
250 | $self->connect ($1 => delete $self->{$_}); |
248 | $self->connect ($1 => delete $self->{$_}); |
251 | } |
249 | } |
252 | } |
250 | } |
253 | |
251 | |
254 | if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { |
252 | if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) { |
255 | $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; |
253 | $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x}; |
256 | $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; |
254 | $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y}; |
257 | $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; |
255 | $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w}; |
258 | $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; |
256 | $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h}; |
259 | |
257 | |
260 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
258 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
261 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
259 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
262 | |
260 | |
263 | $self->show if $layout->{show}; |
261 | $self->show if $layout->{show}; |
… | |
… | |
273 | $self->emit ("destroy"); |
271 | $self->emit ("destroy"); |
274 | %$self = (); |
272 | %$self = (); |
275 | } |
273 | } |
276 | |
274 | |
277 | sub TO_JSON { |
275 | sub TO_JSON { |
278 | { __w_ => $_[0]{s_id} } |
276 | { "\fw" => $_[0]{s_id} } |
279 | } |
277 | } |
280 | |
278 | |
281 | sub show { |
279 | sub show { |
282 | my ($self) = @_; |
280 | my ($self) = @_; |
283 | |
281 | |
284 | return if $self->{parent}; |
282 | return if $self->{parent}; |
285 | |
283 | |
286 | $CFPlus::UI::ROOT->add ($self); |
284 | $DC::UI::ROOT->add ($self); |
287 | } |
285 | } |
288 | |
286 | |
289 | sub set_visible { |
287 | sub set_visible { |
290 | my ($self) = @_; |
288 | my ($self) = @_; |
291 | |
289 | |
292 | return if $self->{visible}; |
290 | return if $self->{visible}; |
|
|
291 | |
|
|
292 | $self->{parent} && $self->{parent}{root}#d# |
|
|
293 | or return ::clienterror ("set_visible called without parent ($self->{parent}) or root\n" => 1); |
293 | |
294 | |
294 | $self->{root} = $self->{parent}{root}; |
295 | $self->{root} = $self->{parent}{root}; |
295 | $self->{visible} = $self->{parent}{visible} + 1; |
296 | $self->{visible} = $self->{parent}{visible} + 1; |
296 | |
297 | |
297 | $self->emit (visibility_change => 1); |
298 | $self->emit (visibility_change => 1); |
298 | |
299 | |
299 | $self->realloc if !exists $self->{req_w}; |
300 | $self->realloc if !exists $self->{req_w}; |
300 | |
301 | |
301 | $_->set_visible for $self->children; |
302 | $_->set_visible for $self->visible_children; |
302 | } |
303 | } |
303 | |
304 | |
304 | sub set_invisible { |
305 | sub set_invisible { |
305 | my ($self) = @_; |
306 | my ($self) = @_; |
306 | |
307 | |
… | |
… | |
312 | delete $self->{root}; |
313 | delete $self->{root}; |
313 | |
314 | |
314 | undef $GRAB if $GRAB == $self; |
315 | undef $GRAB if $GRAB == $self; |
315 | undef $HOVER if $HOVER == $self; |
316 | undef $HOVER if $HOVER == $self; |
316 | |
317 | |
317 | $CFPlus::UI::TOOLTIP_WATCHER->cb->() |
318 | $DC::UI::TOOLTIP_WATCHER->invoke |
318 | if $TOOLTIP->{owner} == $self; |
319 | if $TOOLTIP->{owner} == $self; |
319 | |
320 | |
320 | $self->emit ("focus_out"); |
321 | $self->emit ("focus_out"); |
321 | $self->emit (visibility_change => 0); |
322 | $self->emit (visibility_change => 0); |
322 | } |
323 | } |
… | |
… | |
341 | sub hide { |
342 | sub hide { |
342 | my ($self) = @_; |
343 | my ($self) = @_; |
343 | |
344 | |
344 | $self->set_invisible; |
345 | $self->set_invisible; |
345 | |
346 | |
|
|
347 | # extra $parent copy for 5.8.8+ bug workaround |
|
|
348 | # (otherwise $_[0] in remove gets freed |
|
|
349 | if (my $parent = $self->{parent}) { |
346 | $self->{parent}->remove ($self) |
350 | $parent->remove ($self); |
347 | if $self->{parent}; |
351 | } |
348 | } |
352 | } |
349 | |
353 | |
350 | sub move_abs { |
354 | sub move_abs { |
351 | my ($self, $x, $y, $z) = @_; |
355 | my ($self, $x, $y, $z) = @_; |
352 | |
356 | |
… | |
… | |
364 | $self->{force_h} = $h; |
368 | $self->{force_h} = $h; |
365 | |
369 | |
366 | $self->realloc; |
370 | $self->realloc; |
367 | } |
371 | } |
368 | |
372 | |
|
|
373 | # traverse the widget chain up to find the maximum "physical" size constraints |
|
|
374 | sub get_max_wh { |
|
|
375 | my ($self) = @_; |
|
|
376 | |
|
|
377 | my ($w, $h) = @$self{qw(max_w max_h)}; |
|
|
378 | |
|
|
379 | if ($w <= 0 || $h <= 0) { |
|
|
380 | my ($mw, $mh) = $self->{parent} |
|
|
381 | ? $self->{parent}->get_max_wh |
|
|
382 | : ($::WIDTH, $::HEIGHT); |
|
|
383 | |
|
|
384 | $w = $mw if $w <= 0; |
|
|
385 | $h = $mh if $h <= 0; |
|
|
386 | } |
|
|
387 | |
|
|
388 | ($w, $h) |
|
|
389 | } |
|
|
390 | |
369 | sub size_request { |
391 | sub size_request { |
370 | require Carp; |
392 | require Carp; |
371 | Carp::confess "size_request is abstract"; |
393 | Carp::confess "size_request is abstract"; |
372 | } |
394 | } |
373 | |
395 | |
… | |
… | |
379 | my ($self, $x, $y, $w, $h) = @_; |
401 | my ($self, $x, $y, $w, $h) = @_; |
380 | |
402 | |
381 | if ($self->{aspect}) { |
403 | if ($self->{aspect}) { |
382 | my ($ow, $oh) = ($w, $h); |
404 | my ($ow, $oh) = ($w, $h); |
383 | |
405 | |
384 | $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; |
406 | $w = List::Util::min $w, DC::ceil $h * $self->{aspect}; |
385 | $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; |
407 | $h = List::Util::min $h, DC::ceil $w / $self->{aspect}; |
386 | |
408 | |
387 | # use alignment to adjust x, y |
409 | # use alignment to adjust x, y |
388 | |
410 | |
389 | $x += int 0.5 * ($ow - $w); |
411 | $x += int 0.5 * ($ow - $w); |
390 | $y += int 0.5 * ($oh - $h); |
412 | $y += int 0.5 * ($oh - $h); |
… | |
… | |
431 | |
453 | |
432 | return if $self->{tooltip} eq $tooltip; |
454 | return if $self->{tooltip} eq $tooltip; |
433 | |
455 | |
434 | $self->{tooltip} = $tooltip; |
456 | $self->{tooltip} = $tooltip; |
435 | |
457 | |
436 | if ($CFPlus::UI::TOOLTIP->{owner} == $self) { |
458 | if ($DC::UI::TOOLTIP->{owner} == $self) { |
437 | delete $CFPlus::UI::TOOLTIP->{owner}; |
459 | delete $DC::UI::TOOLTIP->{owner}; |
438 | $CFPlus::UI::TOOLTIP_WATCHER->cb->(); |
460 | $DC::UI::TOOLTIP_WATCHER->invoke; |
439 | } |
461 | } |
440 | } |
462 | } |
441 | |
463 | |
442 | # translate global coordinates to local coordinate system |
464 | # translate global coordinates to local coordinate system |
443 | sub coord2local { |
465 | sub coord2local { |
444 | my ($self, $x, $y) = @_; |
466 | my ($self, $x, $y) = @_; |
445 | |
467 | |
446 | Carp::confess unless $self->{parent};#d# |
468 | return (undef, undef) unless $self->{parent}; |
447 | |
469 | |
448 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
470 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
449 | } |
471 | } |
450 | |
472 | |
451 | # translate local coordinates to global coordinate system |
473 | # translate local coordinates to global coordinate system |
452 | sub coord2global { |
474 | sub coord2global { |
453 | my ($self, $x, $y) = @_; |
475 | my ($self, $x, $y) = @_; |
454 | |
476 | |
455 | Carp::confess unless $self->{parent};#d# |
477 | return (undef, undef) unless $self->{parent}; |
456 | |
478 | |
457 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
479 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
458 | } |
480 | } |
459 | |
481 | |
460 | sub invoke_focus_in { |
482 | sub invoke_focus_in { |
… | |
… | |
509 | sub connect { |
531 | sub connect { |
510 | my ($self, $signal, $cb) = @_; |
532 | my ($self, $signal, $cb) = @_; |
511 | |
533 | |
512 | push @{ $self->{signal_cb}{$signal} }, $cb; |
534 | push @{ $self->{signal_cb}{$signal} }, $cb; |
513 | |
535 | |
514 | defined wantarray and CFPlus::guard { |
536 | defined wantarray and Guard::guard { |
515 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
537 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
516 | @{ $self->{signal_cb}{$signal} }; |
538 | @{ $self->{signal_cb}{$signal} }; |
517 | } |
539 | } |
518 | } |
540 | } |
519 | |
541 | |
… | |
… | |
557 | # in .xs |
579 | # in .xs |
558 | |
580 | |
559 | sub set_parent { |
581 | sub set_parent { |
560 | my ($self, $parent) = @_; |
582 | my ($self, $parent) = @_; |
561 | |
583 | |
562 | CFPlus::weaken ($self->{parent} = $parent); |
584 | DC::weaken ($self->{parent} = $parent); |
563 | $self->set_visible if $parent->{visible}; |
585 | $self->set_visible if $parent->{visible}; |
564 | } |
586 | } |
565 | |
587 | |
566 | sub realloc { |
588 | sub realloc { |
567 | my ($self) = @_; |
589 | my ($self) = @_; |
… | |
… | |
605 | } |
627 | } |
606 | |
628 | |
607 | sub DESTROY { |
629 | sub DESTROY { |
608 | my ($self) = @_; |
630 | my ($self) = @_; |
609 | |
631 | |
610 | return if CFPlus::in_destruct; |
632 | return if DC::in_destruct; |
611 | |
633 | |
|
|
634 | local $@; |
612 | eval { $self->destroy }; |
635 | eval { $self->destroy }; |
613 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
636 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
614 | |
637 | |
615 | delete $WIDGET{$self+0}; |
638 | delete $WIDGET{$self+0}; |
616 | } |
639 | } |
617 | |
640 | |
618 | ############################################################################# |
641 | ############################################################################# |
619 | |
642 | |
620 | package CFPlus::UI::DrawBG; |
643 | package DC::UI::DrawBG; |
621 | |
644 | |
622 | our @ISA = CFPlus::UI::Base::; |
645 | our @ISA = DC::UI::Base::; |
623 | |
646 | |
624 | use strict; |
647 | use strict; |
625 | use CFPlus::OpenGL; |
648 | use DC::OpenGL; |
626 | |
649 | |
627 | sub new { |
650 | sub new { |
628 | my $class = shift; |
651 | my $class = shift; |
629 | |
|
|
630 | # range [value, low, high, page] |
|
|
631 | |
652 | |
632 | $class->SUPER::new ( |
653 | $class->SUPER::new ( |
633 | #bg => [0, 0, 0, 0.2], |
654 | #bg => [0, 0, 0, 0.2], |
634 | #active_bg => [1, 1, 1, 0.5], |
655 | #active_bg => [1, 1, 1, 0.5], |
635 | @_ |
656 | @_ |
636 | ) |
657 | ) |
637 | } |
658 | } |
638 | |
659 | |
|
|
660 | sub set_bg { |
|
|
661 | my ($self, $bg) = @_; |
|
|
662 | |
|
|
663 | $self->{bg} = $bg; |
|
|
664 | $self->update; |
|
|
665 | } |
|
|
666 | |
639 | sub _draw { |
667 | sub _draw { |
640 | my ($self) = @_; |
668 | my ($self) = @_; |
641 | |
669 | |
642 | my $color = $FOCUS == $self && $self->{active_bg} |
670 | my $color = $FOCUS == $self |
643 | ? $self->{active_bg} |
671 | ? $self->{active_bg} || $self->{bg} |
644 | : $self->{bg}; |
672 | : $self->{bg}; |
645 | |
673 | |
646 | if ($color && (@$color < 4 || $color->[3])) { |
674 | if ($color && (@$color < 4 || $color->[3])) { |
647 | my ($w, $h) = @$self{qw(w h)}; |
675 | my ($w, $h) = @$self{qw(w h)}; |
648 | |
676 | |
649 | glEnable GL_BLEND; |
677 | glEnable GL_BLEND; |
650 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
678 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
651 | glColor_premultiply @$color; |
679 | glColor_premultiply @$color; |
652 | |
|
|
653 | glBegin GL_QUADS; |
|
|
654 | glVertex 0 , 0; |
|
|
655 | glVertex 0 , $h; |
|
|
656 | glVertex $w, $h; |
680 | glRect 0, 0, $w, $h; |
657 | glVertex $w, 0; |
|
|
658 | glEnd; |
|
|
659 | |
|
|
660 | glDisable GL_BLEND; |
681 | glDisable GL_BLEND; |
661 | } |
682 | } |
662 | } |
683 | } |
663 | |
684 | |
664 | ############################################################################# |
685 | ############################################################################# |
665 | |
686 | |
666 | package CFPlus::UI::Empty; |
687 | package DC::UI::Empty; |
667 | |
688 | |
668 | our @ISA = CFPlus::UI::Base::; |
689 | our @ISA = DC::UI::Base::; |
669 | |
690 | |
670 | sub new { |
691 | sub new { |
671 | my ($class, %arg) = @_; |
692 | my ($class, %arg) = @_; |
672 | $class->SUPER::new (can_events => 0, %arg); |
693 | $class->SUPER::new (can_events => 0, %arg); |
673 | } |
694 | } |
… | |
… | |
680 | |
701 | |
681 | sub draw { } |
702 | sub draw { } |
682 | |
703 | |
683 | ############################################################################# |
704 | ############################################################################# |
684 | |
705 | |
685 | package CFPlus::UI::Container; |
706 | package DC::UI::Container; |
686 | |
707 | |
687 | our @ISA = CFPlus::UI::Base::; |
708 | our @ISA = DC::UI::Base::; |
688 | |
709 | |
689 | sub new { |
710 | sub new { |
690 | my ($class, %arg) = @_; |
711 | my ($class, %arg) = @_; |
691 | |
712 | |
692 | my $children = delete $arg{children}; |
713 | my $children = delete $arg{children}; |
… | |
… | |
786 | $_->draw for $self->visible_children; |
807 | $_->draw for $self->visible_children; |
787 | } |
808 | } |
788 | |
809 | |
789 | ############################################################################# |
810 | ############################################################################# |
790 | |
811 | |
791 | package CFPlus::UI::Bin; |
812 | package DC::UI::Bin; |
792 | |
813 | |
793 | our @ISA = CFPlus::UI::Container::; |
814 | our @ISA = DC::UI::Container::; |
794 | |
815 | |
795 | sub new { |
816 | sub new { |
796 | my ($class, %arg) = @_; |
817 | my ($class, %arg) = @_; |
797 | |
818 | |
798 | my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; |
819 | my $child = (delete $arg{child}) || new DC::UI::Empty::; |
799 | |
820 | |
800 | $class->SUPER::new (children => [$child], %arg) |
821 | $class->SUPER::new (children => [$child], %arg) |
801 | } |
822 | } |
802 | |
823 | |
803 | sub add { |
824 | sub add { |
… | |
… | |
810 | sub remove { |
831 | sub remove { |
811 | my ($self, $widget) = @_; |
832 | my ($self, $widget) = @_; |
812 | |
833 | |
813 | $self->SUPER::remove ($widget); |
834 | $self->SUPER::remove ($widget); |
814 | |
835 | |
815 | $self->{children} = [new CFPlus::UI::Empty] |
836 | $self->{children} = [new DC::UI::Empty] |
816 | unless @{$self->{children}}; |
837 | unless @{$self->{children}}; |
817 | } |
838 | } |
818 | |
839 | |
819 | sub child { $_[0]->{children}[0] } |
840 | sub child { $_[0]->{children}[0] } |
820 | |
841 | |
… | |
… | |
829 | |
850 | |
830 | 1 |
851 | 1 |
831 | } |
852 | } |
832 | |
853 | |
833 | ############################################################################# |
854 | ############################################################################# |
834 | |
|
|
835 | # back-buffered drawing area |
855 | # back-buffered drawing area |
836 | |
856 | |
837 | package CFPlus::UI::Window; |
857 | package DC::UI::Window; |
838 | |
858 | |
839 | our @ISA = CFPlus::UI::Bin::; |
859 | our @ISA = DC::UI::Bin::; |
840 | |
860 | |
841 | use CFPlus::OpenGL; |
861 | use DC::OpenGL; |
842 | |
862 | |
843 | sub new { |
863 | sub new { |
844 | my ($class, %arg) = @_; |
864 | my ($class, %arg) = @_; |
845 | |
865 | |
846 | my $self = $class->SUPER::new (%arg); |
866 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
868 | } |
888 | } |
869 | |
889 | |
870 | sub render_child { |
890 | sub render_child { |
871 | my ($self) = @_; |
891 | my ($self) = @_; |
872 | |
892 | |
873 | $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { |
893 | $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub { |
874 | glClearColor 0, 0, 0, 0; |
894 | glClearColor 0, 0, 0, 0; |
875 | glClear GL_COLOR_BUFFER_BIT; |
895 | glClear GL_COLOR_BUFFER_BIT; |
876 | |
896 | |
877 | { |
897 | { |
878 | package CFPlus::UI::Base; |
898 | package DC::UI::Base; |
879 | |
899 | |
880 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
900 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
881 | (0, 0, $self->{w}, $self->{h}); |
901 | (0, 0, $self->{w}, $self->{h}); |
882 | |
902 | |
883 | $self->_render; |
903 | $self->_render; |
… | |
… | |
900 | glDisable GL_TEXTURE_2D; |
920 | glDisable GL_TEXTURE_2D; |
901 | } |
921 | } |
902 | |
922 | |
903 | ############################################################################# |
923 | ############################################################################# |
904 | |
924 | |
905 | package CFPlus::UI::ViewPort; |
925 | package DC::UI::ViewPort; |
906 | |
926 | |
907 | use List::Util qw(min max); |
927 | use List::Util qw(min max); |
908 | |
928 | |
909 | our @ISA = CFPlus::UI::Window::; |
929 | our @ISA = DC::UI::Window::; |
910 | |
930 | |
911 | sub new { |
931 | sub new { |
912 | my $class = shift; |
932 | my $class = shift; |
913 | |
933 | |
914 | $class->SUPER::new ( |
934 | $class->SUPER::new ( |
… | |
… | |
998 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
1018 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
999 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
1019 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
1000 | ) { |
1020 | ) { |
1001 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
1021 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
1002 | } else { |
1022 | } else { |
1003 | $self->CFPlus::UI::Base::find_widget ($x, $y) |
1023 | $self->DC::UI::Base::find_widget ($x, $y) |
1004 | } |
1024 | } |
1005 | } |
1025 | } |
1006 | |
1026 | |
1007 | sub _render { |
1027 | sub _render { |
1008 | my ($self) = @_; |
1028 | my ($self) = @_; |
1009 | |
1029 | |
1010 | local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; |
1030 | local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x}; |
1011 | local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; |
1031 | local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y}; |
1012 | |
1032 | |
1013 | CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1033 | DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1014 | |
1034 | |
1015 | $self->SUPER::_render; |
1035 | $self->SUPER::_render; |
1016 | } |
1036 | } |
1017 | |
1037 | |
1018 | ############################################################################# |
1038 | ############################################################################# |
1019 | |
1039 | |
1020 | package CFPlus::UI::ScrolledWindow; |
1040 | package DC::UI::ScrolledWindow; |
1021 | |
1041 | |
1022 | our @ISA = CFPlus::UI::Table::; |
1042 | our @ISA = DC::UI::Table::; |
1023 | |
1043 | |
1024 | sub new { |
1044 | sub new { |
1025 | my ($class, %arg) = @_; |
1045 | my ($class, %arg) = @_; |
1026 | |
1046 | |
1027 | my $child = delete $arg{child}; |
1047 | my $child = delete $arg{child}; |
1028 | |
1048 | |
1029 | my $self; |
1049 | my $self; |
1030 | |
1050 | |
1031 | my $hslider = new CFPlus::UI::Slider |
1051 | my $hslider = new DC::UI::Slider |
1032 | col => 0, |
1052 | c_col => 0, |
1033 | row => 1, |
1053 | c_row => 1, |
1034 | vertical => 0, |
1054 | vertical => 0, |
1035 | range => [0, 0, 1, 0.01], # HACK fix |
1055 | range => [0, 0, 1, 0.01], # HACK fix |
1036 | on_changed => sub { |
1056 | on_changed => sub { |
1037 | $self->{hpos} = $_[1]; |
1057 | $self->{hpos} = $_[1]; |
1038 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1058 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1039 | }, |
1059 | }, |
1040 | ; |
1060 | ; |
1041 | |
1061 | |
1042 | my $vslider = new CFPlus::UI::Slider |
1062 | my $vslider = new DC::UI::Slider |
1043 | col => 1, |
1063 | c_col => 1, |
1044 | row => 0, |
1064 | c_row => 0, |
1045 | vertical => 1, |
1065 | vertical => 1, |
1046 | range => [0, 0, 1, 0.01], # HACK fix |
1066 | range => [0, 0, 1, 0.01], # HACK fix |
1047 | on_changed => sub { |
1067 | on_changed => sub { |
1048 | $self->{vpos} = $_[1]; |
1068 | $self->{vpos} = $_[1]; |
1049 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1069 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
… | |
… | |
1059 | col_expand => [1, 0], |
1079 | col_expand => [1, 0], |
1060 | row_expand => [1, 0], |
1080 | row_expand => [1, 0], |
1061 | %arg, |
1081 | %arg, |
1062 | ); |
1082 | ); |
1063 | |
1083 | |
1064 | $self->{vp} = new CFPlus::UI::ViewPort |
1084 | $self->{vp} = new DC::UI::ViewPort |
1065 | col => 0, |
1085 | c_col => 0, |
1066 | row => 0, |
1086 | c_row => 0, |
1067 | expand => 1, |
1087 | expand => 1, |
1068 | scroll_x => $self->{scroll_x}, |
1088 | scroll_x => $self->{scroll_x}, |
1069 | scroll_y => $self->{scroll_y}, |
1089 | scroll_y => $self->{scroll_y}, |
1070 | on_changed => sub { |
1090 | on_changed => sub { |
1071 | my ($vp, $x, $y) = @_; |
1091 | my ($vp, $x, $y) = @_; |
… | |
… | |
1195 | $self->SUPER::invoke_size_allocate ($w, $h) |
1215 | $self->SUPER::invoke_size_allocate ($w, $h) |
1196 | } |
1216 | } |
1197 | |
1217 | |
1198 | ############################################################################# |
1218 | ############################################################################# |
1199 | |
1219 | |
1200 | package CFPlus::UI::Frame; |
1220 | package DC::UI::Frame; |
1201 | |
1221 | |
1202 | our @ISA = CFPlus::UI::Bin::; |
1222 | our @ISA = DC::UI::Bin::; |
1203 | |
1223 | |
1204 | use CFPlus::OpenGL; |
1224 | use DC::OpenGL; |
1205 | |
1225 | |
1206 | sub new { |
1226 | sub new { |
1207 | my $class = shift; |
1227 | my $class = shift; |
1208 | |
1228 | |
1209 | $class->SUPER::new ( |
1229 | $class->SUPER::new ( |
… | |
… | |
1219 | my ($w, $h) = @$self{qw(w h)}; |
1239 | my ($w, $h) = @$self{qw(w h)}; |
1220 | |
1240 | |
1221 | glEnable GL_BLEND; |
1241 | glEnable GL_BLEND; |
1222 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1242 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1223 | glColor_premultiply @{ $self->{bg} }; |
1243 | glColor_premultiply @{ $self->{bg} }; |
1224 | |
|
|
1225 | glBegin GL_QUADS; |
|
|
1226 | glVertex 0 , 0; |
|
|
1227 | glVertex 0 , $h; |
|
|
1228 | glVertex $w, $h; |
1244 | glRect 0, 0, $w, $h; |
1229 | glVertex $w, 0; |
|
|
1230 | glEnd; |
|
|
1231 | |
|
|
1232 | glDisable GL_BLEND; |
1245 | glDisable GL_BLEND; |
1233 | } |
1246 | } |
1234 | |
1247 | |
1235 | $self->SUPER::_draw; |
1248 | $self->SUPER::_draw; |
1236 | } |
1249 | } |
1237 | |
1250 | |
1238 | ############################################################################# |
1251 | ############################################################################# |
1239 | |
1252 | |
1240 | package CFPlus::UI::FancyFrame; |
1253 | package DC::UI::FancyFrame; |
1241 | |
1254 | |
1242 | our @ISA = CFPlus::UI::Bin::; |
1255 | our @ISA = DC::UI::Bin::; |
1243 | |
1256 | |
1244 | use CFPlus::OpenGL; |
1257 | use DC::OpenGL; |
1245 | |
1258 | |
1246 | sub new { |
1259 | sub new { |
1247 | my ($class, %arg) = @_; |
1260 | my ($class, %arg) = @_; |
1248 | |
1261 | |
1249 | if ((exists $arg{label}) && !ref $arg{label}) { |
1262 | if ((exists $arg{label}) && !ref $arg{label}) { |
1250 | $arg{label} = new CFPlus::UI::Label |
1263 | $arg{label} = new DC::UI::Label |
1251 | align => 1, |
1264 | align => 1, |
1252 | valign => 0, |
1265 | valign => 0.5, |
1253 | text => $arg{label}, |
1266 | text => $arg{label}, |
1254 | fontsize => ($arg{border} || 0.8) * 0.75; |
1267 | fontsize => ($arg{border} || 0.8) * 0.75; |
1255 | } |
1268 | } |
1256 | |
1269 | |
1257 | my $self = $class->SUPER::new ( |
1270 | my $self = $class->SUPER::new ( |
1258 | # label => "", |
1271 | # label => "", |
1259 | fg => [0.6, 0.3, 0.1], |
1272 | fg => undef, |
1260 | border => 0.8, |
1273 | border => 0.8, |
1261 | style => 'single', |
1274 | style => 'single', |
1262 | %arg, |
1275 | %arg, |
1263 | ); |
1276 | ); |
1264 | |
1277 | |
… | |
… | |
1267 | |
1280 | |
1268 | sub add { |
1281 | sub add { |
1269 | my ($self, @widgets) = @_; |
1282 | my ($self, @widgets) = @_; |
1270 | |
1283 | |
1271 | $self->SUPER::add (@widgets); |
1284 | $self->SUPER::add (@widgets); |
1272 | $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; |
1285 | $self->DC::UI::Container::add ($self->{label}) if $self->{label}; |
1273 | } |
1286 | } |
1274 | |
1287 | |
1275 | sub border { |
1288 | sub border { |
1276 | int $_[0]{border} * $::FONTSIZE |
1289 | int $_[0]{border} * $::FONTSIZE |
1277 | } |
1290 | } |
… | |
… | |
1317 | my $border = $self->border; |
1330 | my $border = $self->border; |
1318 | my ($w, $h) = ($self->{w}, $self->{h}); |
1331 | my ($w, $h) = ($self->{w}, $self->{h}); |
1319 | |
1332 | |
1320 | $child->draw; |
1333 | $child->draw; |
1321 | |
1334 | |
1322 | glColor @{$self->{fg}}; |
1335 | glColor @{$self->{fg} || $DC::THEME{fancyframe}}; |
1323 | glBegin GL_LINE_STRIP; |
1336 | glBegin GL_LINE_STRIP; |
1324 | glVertex $border * 1.5 , $border * 0.5 + 0.5; |
1337 | glVertex $border * 1.5 , $border * 0.5 + 0.5; |
1325 | glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; |
1338 | glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; |
1326 | glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
1339 | glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
1327 | glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
1340 | glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
… | |
… | |
1335 | } |
1348 | } |
1336 | } |
1349 | } |
1337 | |
1350 | |
1338 | ############################################################################# |
1351 | ############################################################################# |
1339 | |
1352 | |
1340 | package CFPlus::UI::Toplevel; |
1353 | package DC::UI::Toplevel; |
1341 | |
1354 | |
1342 | our @ISA = CFPlus::UI::Bin::; |
1355 | our @ISA = DC::UI::Bin::; |
1343 | |
1356 | |
1344 | use CFPlus::OpenGL; |
1357 | use DC::OpenGL; |
1345 | |
1358 | |
1346 | my $bg = |
1359 | my $bg = |
1347 | new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", |
1360 | new_from_resource DC::Texture "d1_bg.png", |
1348 | mipmap => 1, wrap => 1; |
1361 | mipmap => 1, wrap => 1; |
1349 | |
1362 | |
1350 | my @border = |
1363 | my @border = |
1351 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1364 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
1352 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1365 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1353 | |
1366 | |
1354 | my @icon = |
1367 | my @icon = |
1355 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1368 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
1356 | qw(x1_move.png x1_resize.png); |
1369 | qw(x1_move.png x1_resize.png); |
1357 | |
1370 | |
1358 | sub new { |
1371 | sub new { |
1359 | my ($class, %arg) = @_; |
1372 | my ($class, %arg) = @_; |
1360 | |
1373 | |
1361 | my $self = $class->SUPER::new ( |
1374 | my $self = $class->SUPER::new ( |
1362 | bg => [1, 1, 1, 1], |
1375 | bg => [1, 1, 1, 1], |
1363 | border_bg => [1, 1, 1, 1], |
1376 | border_bg => [1, 1, 1, 1], |
1364 | border => 0.6, |
1377 | border => 0.8, |
1365 | can_events => 1, |
1378 | can_events => 1, |
1366 | min_w => 64, |
1379 | min_w => 64, |
1367 | min_h => 32, |
1380 | min_h => 32, |
1368 | %arg, |
1381 | %arg, |
1369 | ); |
1382 | ); |
1370 | |
1383 | |
1371 | $self->{title_widget} = new CFPlus::UI::Label |
1384 | $self->{title_widget} = new DC::UI::Label |
1372 | align => 0, |
1385 | align => 0.5, |
1373 | valign => 1, |
1386 | valign => 1, |
1374 | text => $self->{title}, |
1387 | text => $self->{title}, |
1375 | fontsize => $self->{border}, |
1388 | fontsize => $self->{border}, |
1376 | if exists $self->{title}; |
1389 | if exists $self->{title}; |
1377 | |
1390 | |
1378 | if ($self->{has_close_button}) { |
1391 | if ($self->{has_close_button}) { |
1379 | $self->{close_button} = |
1392 | $self->{close_button} = |
1380 | new CFPlus::UI::ImageButton |
1393 | new DC::UI::ImageButton |
1381 | path => 'x1_close.png', |
1394 | path => 'x1_close.png', |
1382 | on_activate => sub { $self->emit ("delete") }; |
1395 | on_activate => sub { $self->emit ("delete") }; |
1383 | |
1396 | |
1384 | $self->CFPlus::UI::Container::add ($self->{close_button}); |
1397 | $self->DC::UI::Container::add ($self->{close_button}); |
1385 | } |
1398 | } |
1386 | |
1399 | |
1387 | $self |
1400 | $self |
1388 | } |
1401 | } |
1389 | |
1402 | |
1390 | sub add { |
1403 | sub add { |
1391 | my ($self, @widgets) = @_; |
1404 | my ($self, @widgets) = @_; |
1392 | |
1405 | |
1393 | $self->SUPER::add (@widgets); |
1406 | $self->SUPER::add (@widgets); |
1394 | $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1407 | $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1395 | $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1408 | $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1396 | } |
1409 | } |
1397 | |
1410 | |
1398 | sub border { |
1411 | sub border { |
1399 | int $_[0]{border} * $::FONTSIZE |
1412 | int $_[0]{border} * $::FONTSIZE |
|
|
1413 | } |
|
|
1414 | |
|
|
1415 | sub get_max_wh { |
|
|
1416 | my ($self) = @_; |
|
|
1417 | |
|
|
1418 | return ($self->{w}, $self->{h}) |
|
|
1419 | if $self->{visible} && $self->{w}; |
|
|
1420 | |
|
|
1421 | $self->SUPER::get_max_wh |
1400 | } |
1422 | } |
1401 | |
1423 | |
1402 | sub size_request { |
1424 | sub size_request { |
1403 | my ($self) = @_; |
1425 | my ($self) = @_; |
1404 | |
1426 | |
… | |
… | |
1529 | glEnable GL_TEXTURE_2D; |
1551 | glEnable GL_TEXTURE_2D; |
1530 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1552 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1531 | |
1553 | |
1532 | my $border = $self->border; |
1554 | my $border = $self->border; |
1533 | |
1555 | |
|
|
1556 | if ($border) { |
1534 | glColor @{ $self->{border_bg} }; |
1557 | glColor @{ $self->{border_bg} }; |
1535 | $border[0]->draw_quad_alpha ( 0, 0, $w, $border); |
1558 | $border[0]->draw_quad_alpha ( 0, 0, $w, $border); |
1536 | $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); |
1559 | $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); |
1537 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1560 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1538 | $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); |
1561 | $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); |
1539 | |
1562 | |
1540 | # move |
1563 | # move |
1541 | my $w2 = ($w - $border) * .5; |
1564 | my $w2 = ($w - $border) * .5; |
1542 | my $h2 = ($h - $border) * .5; |
1565 | my $h2 = ($h - $border) * .5; |
1543 | $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); |
1566 | $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); |
1544 | $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); |
1567 | $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); |
1545 | $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); |
1568 | $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); |
1546 | |
1569 | |
1547 | # resize |
1570 | # resize |
1548 | $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); |
1571 | $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); |
1549 | $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) |
1572 | $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) |
1550 | unless $self->{has_close_button}; |
1573 | unless $self->{has_close_button}; |
1551 | $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); |
1574 | $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); |
1552 | $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); |
1575 | $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); |
|
|
1576 | } |
1553 | |
1577 | |
1554 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1578 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1555 | glColor @{ $self->{bg} }; |
1579 | glColor @{ $self->{bg} }; |
1556 | |
1580 | |
1557 | # TODO: repeat texture not scale |
1581 | # TODO: repeat texture not scale |
… | |
… | |
1576 | if $self->{close_button}; |
1600 | if $self->{close_button}; |
1577 | } |
1601 | } |
1578 | |
1602 | |
1579 | ############################################################################# |
1603 | ############################################################################# |
1580 | |
1604 | |
1581 | package CFPlus::UI::Table; |
1605 | package DC::UI::Table; |
1582 | |
1606 | |
1583 | our @ISA = CFPlus::UI::Container::; |
1607 | our @ISA = DC::UI::Container::; |
1584 | |
1608 | |
1585 | use List::Util qw(max sum); |
1609 | use List::Util qw(max sum); |
1586 | |
1610 | |
1587 | use CFPlus::OpenGL; |
1611 | use DC::OpenGL; |
1588 | |
1612 | |
1589 | sub new { |
1613 | sub new { |
1590 | my $class = shift; |
1614 | my $class = shift; |
1591 | |
1615 | |
1592 | $class->SUPER::new ( |
1616 | $class->SUPER::new ( |
… | |
… | |
1598 | |
1622 | |
1599 | sub add { |
1623 | sub add { |
1600 | my ($self, @widgets) = @_; |
1624 | my ($self, @widgets) = @_; |
1601 | |
1625 | |
1602 | for my $child (@widgets) { |
1626 | for my $child (@widgets) { |
1603 | $child->{rowspan} ||= 1; |
1627 | $child->{c_rowspan} ||= 1; |
1604 | $child->{colspan} ||= 1; |
1628 | $child->{c_colspan} ||= 1; |
1605 | } |
1629 | } |
1606 | |
1630 | |
1607 | $self->SUPER::add (@widgets); |
1631 | $self->SUPER::add (@widgets); |
1608 | } |
1632 | } |
1609 | |
1633 | |
… | |
… | |
1613 | my @widgets; |
1637 | my @widgets; |
1614 | |
1638 | |
1615 | while (@_) { |
1639 | while (@_) { |
1616 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1640 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1617 | |
1641 | |
1618 | $child->{row} = $row; |
1642 | $child->{c_row} = $row; |
1619 | $child->{col} = $col; |
1643 | $child->{c_col} = $col; |
1620 | |
1644 | |
1621 | push @widgets, $child; |
1645 | push @widgets, $child; |
1622 | } |
1646 | } |
1623 | |
1647 | |
1624 | $self->add (@widgets); |
1648 | $self->add (@widgets); |
… | |
… | |
1630 | my (@w, @h); |
1654 | my (@w, @h); |
1631 | |
1655 | |
1632 | my @children = $self->children; |
1656 | my @children = $self->children; |
1633 | |
1657 | |
1634 | # first pass, columns |
1658 | # first pass, columns |
1635 | for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { |
1659 | for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) { |
1636 | my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; |
1660 | my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; |
1637 | |
1661 | |
1638 | my $sw = sum @w[$c .. $c + $cs - 1]; |
1662 | my $sw = sum @w[$c .. $c + $cs - 1]; |
1639 | |
1663 | |
1640 | if ($w > $sw) { |
1664 | if ($w > $sw) { |
1641 | $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1665 | $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1642 | } |
1666 | } |
1643 | } |
1667 | } |
1644 | |
1668 | |
1645 | # second pass, rows |
1669 | # second pass, rows |
1646 | for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { |
1670 | for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { |
1647 | my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; |
1671 | my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; |
1648 | |
1672 | |
1649 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1673 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1650 | |
1674 | |
1651 | if ($h > $sh) { |
1675 | if ($h > $sh) { |
1652 | $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1676 | $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1653 | } |
1677 | } |
1654 | } |
1678 | } |
1655 | |
1679 | |
1656 | (\@w, \@h) |
1680 | (\@w, \@h) |
1657 | } |
1681 | } |
… | |
… | |
1680 | @col_expand = (1) x @$ws unless @col_expand; |
1704 | @col_expand = (1) x @$ws unless @col_expand; |
1681 | my $col_expand = (sum @col_expand) || 1; |
1705 | my $col_expand = (sum @col_expand) || 1; |
1682 | |
1706 | |
1683 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1707 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1684 | |
1708 | |
1685 | CFPlus::UI::harmonize $ws; |
1709 | DC::UI::harmonize $ws; |
1686 | |
1710 | |
1687 | my @row_expand = @{$self->{row_expand}}; |
1711 | my @row_expand = @{$self->{row_expand}}; |
1688 | @row_expand = (1) x @$ws unless @row_expand; |
1712 | @row_expand = (1) x @$ws unless @row_expand; |
1689 | my $row_expand = (sum @row_expand) || 1; |
1713 | my $row_expand = (sum @row_expand) || 1; |
1690 | |
1714 | |
1691 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1715 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1692 | |
1716 | |
1693 | CFPlus::UI::harmonize $hs; |
1717 | DC::UI::harmonize $hs; |
1694 | |
1718 | |
1695 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
1719 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
1696 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1720 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1697 | |
1721 | |
1698 | for my $widget ($self->children) { |
1722 | for my $widget ($self->children) { |
1699 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; |
1723 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)}; |
1700 | |
1724 | |
1701 | $widget->configure ( |
1725 | $widget->configure ( |
1702 | $x[$c], $y[$r], |
1726 | $x[$c], $y[$r], |
1703 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1727 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1704 | ); |
1728 | ); |
… | |
… | |
1707 | 1 |
1731 | 1 |
1708 | } |
1732 | } |
1709 | |
1733 | |
1710 | ############################################################################# |
1734 | ############################################################################# |
1711 | |
1735 | |
1712 | package CFPlus::UI::Fixed; |
1736 | package DC::UI::Fixed; |
1713 | |
1737 | |
1714 | use List::Util qw(min max); |
1738 | use List::Util qw(min max); |
1715 | |
1739 | |
1716 | our @ISA = CFPlus::UI::Container::; |
1740 | our @ISA = DC::UI::Container::; |
1717 | |
1741 | |
1718 | sub _scale($$$) { |
1742 | sub _scale($$$) { |
1719 | my ($rel, $val, $max) = @_; |
1743 | my ($rel, $val, $max) = @_; |
1720 | |
1744 | |
1721 | $rel ? $val * $max : $val |
1745 | $rel ? $val * $max : $val |
… | |
… | |
1773 | 1 |
1797 | 1 |
1774 | } |
1798 | } |
1775 | |
1799 | |
1776 | ############################################################################# |
1800 | ############################################################################# |
1777 | |
1801 | |
1778 | package CFPlus::UI::Box; |
1802 | package DC::UI::Box; |
1779 | |
1803 | |
1780 | our @ISA = CFPlus::UI::Container::; |
1804 | our @ISA = DC::UI::Container::; |
1781 | |
1805 | |
1782 | sub size_request { |
1806 | sub size_request { |
1783 | my ($self) = @_; |
1807 | my ($self) = @_; |
1784 | |
1808 | |
1785 | my @children = $self->visible_children; |
1809 | my @children = $self->visible_children; |
… | |
… | |
1820 | $req[$_] += $space * $children[$_]{expand} |
1844 | $req[$_] += $space * $children[$_]{expand} |
1821 | for 0 .. $#children; |
1845 | for 0 .. $#children; |
1822 | } |
1846 | } |
1823 | } |
1847 | } |
1824 | |
1848 | |
1825 | CFPlus::UI::harmonize \@req; |
1849 | DC::UI::harmonize \@req; |
1826 | |
1850 | |
1827 | my $pos = 0; |
1851 | my $pos = 0; |
1828 | for (0 .. $#children) { |
1852 | for (0 .. $#children) { |
1829 | my $alloc = $req[$_]; |
1853 | my $alloc = $req[$_]; |
1830 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
1854 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
… | |
… | |
1835 | 1 |
1859 | 1 |
1836 | } |
1860 | } |
1837 | |
1861 | |
1838 | ############################################################################# |
1862 | ############################################################################# |
1839 | |
1863 | |
1840 | package CFPlus::UI::HBox; |
1864 | package DC::UI::HBox; |
1841 | |
1865 | |
1842 | our @ISA = CFPlus::UI::Box::; |
1866 | our @ISA = DC::UI::Box::; |
1843 | |
1867 | |
1844 | sub new { |
1868 | sub new { |
1845 | my $class = shift; |
1869 | my $class = shift; |
1846 | |
1870 | |
1847 | $class->SUPER::new ( |
1871 | $class->SUPER::new ( |
… | |
… | |
1850 | ) |
1874 | ) |
1851 | } |
1875 | } |
1852 | |
1876 | |
1853 | ############################################################################# |
1877 | ############################################################################# |
1854 | |
1878 | |
1855 | package CFPlus::UI::VBox; |
1879 | package DC::UI::VBox; |
1856 | |
1880 | |
1857 | our @ISA = CFPlus::UI::Box::; |
1881 | our @ISA = DC::UI::Box::; |
1858 | |
1882 | |
1859 | sub new { |
1883 | sub new { |
1860 | my $class = shift; |
1884 | my $class = shift; |
1861 | |
1885 | |
1862 | $class->SUPER::new ( |
1886 | $class->SUPER::new ( |
… | |
… | |
1865 | ) |
1889 | ) |
1866 | } |
1890 | } |
1867 | |
1891 | |
1868 | ############################################################################# |
1892 | ############################################################################# |
1869 | |
1893 | |
1870 | package CFPlus::UI::Label; |
1894 | package DC::UI::Label; |
1871 | |
1895 | |
1872 | our @ISA = CFPlus::UI::DrawBG::; |
1896 | our @ISA = DC::UI::DrawBG::; |
1873 | |
1897 | |
1874 | use CFPlus::OpenGL; |
1898 | use DC::OpenGL; |
1875 | |
1899 | |
1876 | sub new { |
1900 | sub new { |
1877 | my ($class, %arg) = @_; |
1901 | my ($class, %arg) = @_; |
1878 | |
1902 | |
1879 | my $self = $class->SUPER::new ( |
1903 | my $self = $class->SUPER::new ( |
… | |
… | |
1884 | #text => initial text |
1908 | #text => initial text |
1885 | #markup => initial narkup |
1909 | #markup => initial narkup |
1886 | #max_w => maximum pixel width |
1910 | #max_w => maximum pixel width |
1887 | #style => 0, # render flags |
1911 | #style => 0, # render flags |
1888 | ellipsise => 3, # end |
1912 | ellipsise => 3, # end |
1889 | layout => (new CFPlus::Layout), |
1913 | layout => (new DC::Layout), |
1890 | fontsize => 1, |
1914 | fontsize => 1, |
1891 | align => -1, |
1915 | align => 0.5, |
1892 | valign => -1, |
1916 | valign => 0.5, |
1893 | padding_x => 2, |
1917 | padding_x => 4, |
1894 | padding_y => 2, |
1918 | padding_y => 2, |
1895 | can_events => 0, |
1919 | can_events => 0, |
1896 | %arg |
1920 | %arg |
1897 | ); |
1921 | ); |
1898 | |
1922 | |
1899 | if (exists $self->{template}) { |
1923 | if (exists $self->{template}) { |
1900 | my $layout = new CFPlus::Layout; |
1924 | my $layout = new DC::Layout; |
1901 | $layout->set_text (delete $self->{template}); |
1925 | $layout->set_text (delete $self->{template}); |
1902 | $self->{template} = $layout; |
1926 | $self->{template} = $layout; |
1903 | } |
1927 | } |
1904 | |
1928 | |
1905 | if (exists $self->{markup}) { |
1929 | if (exists $self->{markup}) { |
… | |
… | |
1923 | |
1947 | |
1924 | delete $self->{ox}; |
1948 | delete $self->{ox}; |
1925 | $self->SUPER::realloc; |
1949 | $self->SUPER::realloc; |
1926 | } |
1950 | } |
1927 | |
1951 | |
|
|
1952 | sub clear { |
|
|
1953 | my ($self) = @_; |
|
|
1954 | |
|
|
1955 | $self->set_text (""); |
|
|
1956 | } |
|
|
1957 | |
1928 | sub set_text { |
1958 | sub set_text { |
1929 | my ($self, $text) = @_; |
1959 | my ($self, $text) = @_; |
1930 | |
1960 | |
1931 | return if $self->{text} eq "T$text"; |
1961 | return if $self->{text} eq "T$text"; |
1932 | $self->{text} = "T$text"; |
1962 | $self->{text} = "T$text"; |
… | |
… | |
1955 | |
1985 | |
1956 | sub size_request { |
1986 | sub size_request { |
1957 | my ($self) = @_; |
1987 | my ($self) = @_; |
1958 | |
1988 | |
1959 | $self->{size_req} ||= do { |
1989 | $self->{size_req} ||= do { |
|
|
1990 | my ($max_w, $max_h) = $self->get_max_wh; |
|
|
1991 | |
1960 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1992 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1961 | $self->{layout}->set_width ($self->{max_w} || -1); |
1993 | $self->{layout}->set_width ($max_w); |
1962 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1994 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1963 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1995 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1964 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1996 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1965 | |
1997 | |
1966 | my ($w, $h) = $self->{layout}->size; |
1998 | my ($w, $h) = $self->{layout}->size; |
1967 | |
1999 | |
1968 | if (exists $self->{template}) { |
2000 | if (exists $self->{template}) { |
1969 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
2001 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1970 | $self->{template}->set_width ($self->{max_w} || -1); |
2002 | $self->{template}->set_width ($max_w); |
1971 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
2003 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1972 | |
2004 | |
1973 | my ($w2, $h2) = $self->{template}->size; |
2005 | my ($w2, $h2) = $self->{template}->size; |
1974 | |
2006 | |
1975 | $w = List::Util::max $w, $w2; |
2007 | $w = List::Util::max $w, $w2; |
… | |
… | |
2031 | |
2063 | |
2032 | [$self->{layout}->size] |
2064 | [$self->{layout}->size] |
2033 | }; |
2065 | }; |
2034 | |
2066 | |
2035 | unless (exists $self->{ox}) { |
2067 | unless (exists $self->{ox}) { |
2036 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
2068 | $self->{ox} = $self->{padding_x} + int $self->{align} * ($self->{w} - $size->[0] - $self->{padding_x} * 2); |
2037 | : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} |
2069 | $self->{oy} = $self->{padding_y} + int $self->{valign} * ($self->{h} - $size->[1] - $self->{padding_y} * 2); |
2038 | : ($self->{w} - $size->[0]) * 0.5); |
|
|
2039 | |
2070 | |
2040 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
2071 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2041 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
|
|
2042 | : ($self->{h} - $size->[1]) * 0.5); |
|
|
2043 | }; |
2072 | }; |
2044 | |
2073 | |
2045 | # unless ($self->{list}) { |
2074 | # unless ($self->{list}) { |
2046 | # $self->{list} = CFPlus::OpenGL::glGenList; |
2075 | # $self->{list} = DC::OpenGL::glGenList; |
2047 | # CFPlus::OpenGL::glNewList $self->{list}; |
2076 | # DC::OpenGL::glNewList $self->{list}; |
2048 | # $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2077 | # $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2049 | # CFPlus::OpenGL::glEndList; |
2078 | # DC::OpenGL::glEndList; |
2050 | # } |
2079 | # } |
2051 | # |
2080 | # |
2052 | # CFPlus::OpenGL::glCallList $self->{list}; |
2081 | # DC::OpenGL::glCallList $self->{list}; |
2053 | |
2082 | |
2054 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2083 | $self->{layout}->draw; |
2055 | } |
2084 | } |
2056 | |
2085 | |
2057 | #sub destroy { |
2086 | #sub destroy { |
2058 | # my ($self) = @_; |
2087 | # my ($self) = @_; |
2059 | # |
2088 | # |
2060 | # CFPlus::OpenGL::glDeleteList delete $self->{list} if $self->{list}; |
2089 | # DC::OpenGL::glDeleteList delete $self->{list} if $self->{list}; |
2061 | # |
2090 | # |
2062 | # $self->SUPER::destroy; |
2091 | # $self->SUPER::destroy; |
2063 | #} |
2092 | #} |
2064 | |
2093 | |
2065 | ############################################################################# |
2094 | ############################################################################# |
2066 | |
2095 | |
2067 | package CFPlus::UI::EntryBase; |
2096 | package DC::UI::EntryBase; |
2068 | |
2097 | |
2069 | our @ISA = CFPlus::UI::Label::; |
2098 | our @ISA = DC::UI::Label::; |
2070 | |
2099 | |
2071 | use CFPlus::OpenGL; |
2100 | use DC::OpenGL; |
2072 | |
2101 | |
2073 | sub new { |
2102 | sub new { |
2074 | my $class = shift; |
2103 | my $class = shift; |
2075 | |
2104 | |
2076 | $class->SUPER::new ( |
2105 | $class->SUPER::new ( |
2077 | fg => [1, 1, 1], |
2106 | fg => [1, 1, 1], |
2078 | bg => [0, 0, 0, 0.2], |
2107 | bg => [0, 0, 0, 0.2], |
|
|
2108 | outline => undef, |
2079 | active_bg => [0, 0, 1, .2], |
2109 | active_bg => [0, 0, 1, .2], |
2080 | active_fg => [1, 1, 1], |
2110 | active_fg => [1, 1, 1], |
2081 | active_outline => [1, 1, 0], |
2111 | active_outline => [1, 1, 0], |
2082 | can_hover => 1, |
2112 | can_hover => 1, |
2083 | can_focus => 1, |
2113 | can_focus => 1, |
|
|
2114 | align => 0, |
2084 | valign => 0, |
2115 | valign => 0.5, |
2085 | can_events => 1, |
2116 | can_events => 1, |
2086 | ellipsise => 0, |
2117 | ellipsise => 0, |
|
|
2118 | padding_x => 4, |
|
|
2119 | padding_y => 2, |
2087 | #text => ... |
2120 | #text => ... |
2088 | #hidden => "*", |
2121 | #hidden => "*", |
2089 | @_ |
2122 | @_ |
2090 | ) |
2123 | ) |
2091 | } |
2124 | } |
… | |
… | |
2138 | |
2171 | |
2139 | my $text = $self->get_text; |
2172 | my $text = $self->get_text; |
2140 | |
2173 | |
2141 | $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; |
2174 | $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; |
2142 | |
2175 | |
2143 | if ($uni == 8) { |
2176 | if ($sym == DC::SDLK_BACKSPACE) { |
2144 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2177 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2145 | } elsif ($uni == 127) { |
2178 | } elsif ($sym == DC::SDLK_DELETE) { |
2146 | substr $text, $self->{cursor}, 1, ""; |
2179 | substr $text, $self->{cursor}, 1, ""; |
2147 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
2180 | } elsif ($sym == DC::SDLK_LEFT) { |
2148 | --$self->{cursor} if $self->{cursor}; |
2181 | --$self->{cursor} if $self->{cursor}; |
2149 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
2182 | } elsif ($sym == DC::SDLK_RIGHT) { |
2150 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2183 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2151 | } elsif ($sym == CFPlus::SDLK_HOME) { |
2184 | } elsif ($sym == DC::SDLK_HOME) { |
2152 | # what a hack |
2185 | # what a hack |
2153 | $self->{cursor} = |
2186 | $self->{cursor} = |
2154 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
2187 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
2155 | ? length $1 |
2188 | ? length $1 |
2156 | : 0; |
2189 | : 0; |
2157 | } elsif ($sym == CFPlus::SDLK_END) { |
2190 | } elsif ($sym == DC::SDLK_END) { |
2158 | # uh, again |
2191 | # uh, again |
2159 | $self->{cursor} = |
2192 | $self->{cursor} = |
2160 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
2193 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
2161 | ? $self->{cursor} + length $1 |
2194 | ? $self->{cursor} + length $1 |
2162 | : length $self->{text}; |
2195 | : length $self->{text}; |
… | |
… | |
2226 | glColor_premultiply @{$self->{bg}}; |
2259 | glColor_premultiply @{$self->{bg}}; |
2227 | } |
2260 | } |
2228 | |
2261 | |
2229 | glEnable GL_BLEND; |
2262 | glEnable GL_BLEND; |
2230 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2263 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2231 | glBegin GL_QUADS; |
|
|
2232 | glVertex 0 , 0; |
|
|
2233 | glVertex 0 , $self->{h}; |
|
|
2234 | glVertex $self->{w}, $self->{h}; |
2264 | glRect 0, 0, $self->{w}, $self->{h}; |
2235 | glVertex $self->{w}, 0; |
|
|
2236 | glEnd; |
|
|
2237 | glDisable GL_BLEND; |
2265 | glDisable GL_BLEND; |
2238 | |
2266 | |
2239 | $self->SUPER::_draw; |
2267 | $self->SUPER::_draw; |
2240 | |
2268 | |
2241 | #TODO: force update every cursor change :( |
2269 | #TODO: force update every cursor change :( |
… | |
… | |
2248 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
2276 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
2249 | } |
2277 | } |
2250 | |
2278 | |
2251 | glColor_premultiply @{$self->{active_fg}}; |
2279 | glColor_premultiply @{$self->{active_fg}}; |
2252 | glBegin GL_LINES; |
2280 | glBegin GL_LINES; |
2253 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
2281 | glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy}; |
2254 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2282 | glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2255 | glEnd; |
2283 | glEnd; |
2256 | |
2284 | |
2257 | glLineWidth 3; |
2285 | glLineWidth 3; |
2258 | glColor @{$self->{active_outline}}; |
2286 | glColor @{$self->{active_outline}}; |
2259 | glTranslate -.375, -.375; |
2287 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
|
|
2288 | glLineWidth 1; |
|
|
2289 | |
|
|
2290 | } else { |
|
|
2291 | glColor @{$self->{outline} || $DC::THEME{entry_outline}}; |
2260 | glBegin GL_LINE_LOOP; |
2292 | glBegin GL_LINE_STRIP; |
2261 | glVertex 0, 0; |
2293 | glVertex .5, $self->{h} * .5; |
2262 | glVertex $self->{w}, 0; |
2294 | glVertex .5, $self->{h} - 2.5; |
2263 | glVertex $self->{w}, $self->{h}; |
2295 | glVertex $self->{w} - .5, $self->{h} - 2.5; |
2264 | glVertex 0, $self->{h}; |
2296 | glVertex $self->{w} - .5, $self->{h} * .5; |
2265 | glEnd; |
2297 | glEnd; |
2266 | glLineWidth 1; |
|
|
2267 | } |
2298 | } |
2268 | } |
2299 | } |
2269 | |
2300 | |
2270 | ############################################################################# |
2301 | ############################################################################# |
2271 | |
2302 | |
2272 | package CFPlus::UI::Entry; |
2303 | package DC::UI::Entry; |
2273 | |
2304 | |
2274 | our @ISA = CFPlus::UI::EntryBase::; |
2305 | our @ISA = DC::UI::EntryBase::; |
2275 | |
2306 | |
2276 | use CFPlus::OpenGL; |
2307 | use DC::OpenGL; |
|
|
2308 | |
|
|
2309 | sub new { |
|
|
2310 | my $class = shift; |
|
|
2311 | |
|
|
2312 | $class->SUPER::new ( |
|
|
2313 | history_pointer => -1, |
|
|
2314 | @_ |
|
|
2315 | ) |
|
|
2316 | } |
|
|
2317 | |
2277 | |
2318 | |
2278 | sub invoke_key_down { |
2319 | sub invoke_key_down { |
2279 | my ($self, $ev) = @_; |
2320 | my ($self, $ev) = @_; |
2280 | |
2321 | |
2281 | my $sym = $ev->{sym}; |
2322 | my $sym = $ev->{sym}; |
… | |
… | |
2287 | $self->{history_pointer} = -1; |
2328 | $self->{history_pointer} = -1; |
2288 | $self->{history_saveback} = ''; |
2329 | $self->{history_saveback} = ''; |
2289 | $self->emit (activate => $txt); |
2330 | $self->emit (activate => $txt); |
2290 | $self->update; |
2331 | $self->update; |
2291 | |
2332 | |
2292 | } elsif ($sym == CFPlus::SDLK_UP) { |
2333 | } elsif ($sym == DC::SDLK_UP) { |
2293 | if ($self->{history_pointer} < 0) { |
2334 | if ($self->{history_pointer} < 0) { |
2294 | $self->{history_saveback} = $self->get_text; |
2335 | $self->{history_saveback} = $self->get_text; |
2295 | } |
2336 | } |
2296 | if (@{$self->{history} || []} > 0) { |
2337 | if (@{$self->{history} || []} > 0) { |
2297 | $self->{history_pointer}++; |
2338 | $self->{history_pointer}++; |
… | |
… | |
2299 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2340 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2300 | } |
2341 | } |
2301 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2342 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2302 | } |
2343 | } |
2303 | |
2344 | |
2304 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2345 | } elsif ($sym == DC::SDLK_DOWN) { |
2305 | $self->{history_pointer}--; |
2346 | $self->{history_pointer}--; |
2306 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2347 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2307 | |
2348 | |
2308 | if ($self->{history_pointer} >= 0) { |
2349 | if ($self->{history_pointer} >= 0) { |
2309 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2350 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2310 | } else { |
2351 | } else { |
|
|
2352 | if (defined $self->{history_saveback}) { |
2311 | $self->set_text ($self->{history_saveback}); |
2353 | $self->set_text ($self->{history_saveback}); |
|
|
2354 | $self->{history_saveback} = undef; |
|
|
2355 | } |
2312 | } |
2356 | } |
2313 | |
2357 | |
2314 | } else { |
2358 | } else { |
2315 | return $self->SUPER::invoke_key_down ($ev) |
2359 | return $self->SUPER::invoke_key_down ($ev) |
2316 | } |
2360 | } |
… | |
… | |
2318 | 1 |
2362 | 1 |
2319 | } |
2363 | } |
2320 | |
2364 | |
2321 | ############################################################################# |
2365 | ############################################################################# |
2322 | |
2366 | |
2323 | package CFPlus::UI::TextEdit; |
2367 | package DC::UI::TextEdit; |
2324 | |
2368 | |
2325 | our @ISA = CFPlus::UI::EntryBase::; |
2369 | our @ISA = DC::UI::EntryBase::; |
2326 | |
2370 | |
2327 | use CFPlus::OpenGL; |
2371 | use DC::OpenGL; |
|
|
2372 | |
|
|
2373 | sub new { |
|
|
2374 | my $class = shift; |
|
|
2375 | |
|
|
2376 | $class->SUPER::new ( |
|
|
2377 | padding_y => 4, |
|
|
2378 | |
|
|
2379 | @_ |
|
|
2380 | ) |
|
|
2381 | } |
2328 | |
2382 | |
2329 | sub move_cursor_ver { |
2383 | sub move_cursor_ver { |
2330 | my ($self, $dy) = @_; |
2384 | my ($self, $dy) = @_; |
2331 | |
2385 | |
2332 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
2386 | my ($line, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
2333 | |
2387 | |
2334 | $y += $dy; |
2388 | $line += $dy; |
2335 | |
2389 | |
2336 | if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { |
2390 | if (defined (my $index = $self->{layout}->line_x_to_index ($line, $x))) { |
2337 | $self->{cursor} = $index; |
2391 | $self->{cursor} = $index; |
2338 | delete $self->{cur_h}; |
2392 | delete $self->{cur_h}; |
2339 | $self->update; |
2393 | $self->update; |
2340 | return; |
2394 | return; |
2341 | } |
2395 | } |
… | |
… | |
2344 | sub invoke_key_down { |
2398 | sub invoke_key_down { |
2345 | my ($self, $ev) = @_; |
2399 | my ($self, $ev) = @_; |
2346 | |
2400 | |
2347 | my $sym = $ev->{sym}; |
2401 | my $sym = $ev->{sym}; |
2348 | |
2402 | |
2349 | if ($sym == CFPlus::SDLK_UP) { |
2403 | if ($sym == DC::SDLK_UP) { |
2350 | $self->move_cursor_ver (-1); |
2404 | $self->move_cursor_ver (-1); |
2351 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2405 | } elsif ($sym == DC::SDLK_DOWN) { |
2352 | $self->move_cursor_ver (+1); |
2406 | $self->move_cursor_ver (+1); |
2353 | } else { |
2407 | } else { |
2354 | return $self->SUPER::invoke_key_down ($ev) |
2408 | return $self->SUPER::invoke_key_down ($ev) |
2355 | } |
2409 | } |
2356 | |
2410 | |
2357 | 1 |
2411 | 1 |
2358 | } |
2412 | } |
2359 | |
2413 | |
2360 | ############################################################################# |
2414 | ############################################################################# |
2361 | |
2415 | |
2362 | package CFPlus::UI::ButtonBin; |
2416 | package DC::UI::ButtonBin; |
2363 | |
2417 | |
2364 | our @ISA = CFPlus::UI::Bin::; |
2418 | our @ISA = DC::UI::Bin::; |
2365 | |
2419 | |
2366 | use CFPlus::OpenGL; |
2420 | use DC::OpenGL; |
2367 | |
2421 | |
2368 | my @tex = |
2422 | my @tex = |
2369 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2423 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2370 | qw(b1_button_inactive.png b1_button_active.png); |
2424 | qw(b1_button_inactive.png b1_button_active.png); |
2371 | |
2425 | |
2372 | sub new { |
2426 | sub new { |
2373 | my $class = shift; |
2427 | my $class = shift; |
2374 | |
2428 | |
2375 | $class->SUPER::new ( |
2429 | $class->SUPER::new ( |
2376 | can_hover => 1, |
2430 | can_hover => 1, |
2377 | align => 0, |
2431 | align => 0.5, |
2378 | valign => 0, |
2432 | valign => 0.5, |
2379 | can_events => 1, |
2433 | can_events => 1, |
2380 | @_ |
2434 | @_ |
2381 | ) |
2435 | ) |
2382 | } |
2436 | } |
2383 | |
2437 | |
… | |
… | |
2406 | $self->SUPER::_draw; |
2460 | $self->SUPER::_draw; |
2407 | } |
2461 | } |
2408 | |
2462 | |
2409 | ############################################################################# |
2463 | ############################################################################# |
2410 | |
2464 | |
2411 | package CFPlus::UI::Button; |
2465 | package DC::UI::Button; |
2412 | |
2466 | |
2413 | our @ISA = CFPlus::UI::Label::; |
2467 | our @ISA = DC::UI::Label::; |
2414 | |
2468 | |
2415 | use CFPlus::OpenGL; |
2469 | use DC::OpenGL; |
2416 | |
2470 | |
2417 | my @tex = |
2471 | my @tex = |
2418 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2472 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2419 | qw(b1_button_inactive.png b1_button_active.png); |
2473 | qw(b1_button_inactive.png b1_button_active.png); |
2420 | |
2474 | |
2421 | sub new { |
2475 | sub new { |
2422 | my $class = shift; |
2476 | my $class = shift; |
2423 | |
2477 | |
2424 | $class->SUPER::new ( |
2478 | $class->SUPER::new ( |
2425 | padding_x => 4, |
2479 | padding_x => 8, |
2426 | padding_y => 4, |
2480 | padding_y => 4, |
2427 | fg => [1.0, 1.0, 1.0], |
2481 | fg => [1.0, 1.0, 1.0], |
2428 | active_fg => [0.8, 0.8, 0.8], |
2482 | active_fg => [0.8, 0.8, 0.8], |
2429 | can_hover => 1, |
2483 | can_hover => 1, |
2430 | align => 0, |
2484 | align => 0.5, |
2431 | valign => 0, |
2485 | valign => 0.5, |
2432 | can_events => 1, |
2486 | can_events => 1, |
2433 | @_ |
2487 | @_ |
2434 | ) |
2488 | ) |
2435 | } |
2489 | } |
2436 | |
2490 | |
… | |
… | |
2461 | $self->SUPER::_draw; |
2515 | $self->SUPER::_draw; |
2462 | } |
2516 | } |
2463 | |
2517 | |
2464 | ############################################################################# |
2518 | ############################################################################# |
2465 | |
2519 | |
2466 | package CFPlus::UI::CheckBox; |
2520 | package DC::UI::CheckBox; |
2467 | |
2521 | |
2468 | our @ISA = CFPlus::UI::DrawBG::; |
2522 | our @ISA = DC::UI::DrawBG::; |
2469 | |
2523 | |
2470 | my @tex = |
2524 | my @tex = |
2471 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2525 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2472 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2526 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2473 | |
2527 | |
2474 | use CFPlus::OpenGL; |
2528 | use DC::OpenGL; |
2475 | |
2529 | |
2476 | sub new { |
2530 | sub new { |
2477 | my $class = shift; |
2531 | my $class = shift; |
2478 | |
2532 | |
2479 | $class->SUPER::new ( |
2533 | $class->SUPER::new ( |
|
|
2534 | fontsize => 1, |
2480 | padding_x => 2, |
2535 | padding_x => 2, |
2481 | padding_y => 2, |
2536 | padding_y => 2, |
2482 | fg => [1, 1, 1], |
2537 | fg => [1, 1, 1], |
2483 | active_fg => [1, 1, 0], |
2538 | active_fg => [1, 1, 0], |
2484 | bg => [0, 0, 0, 0.2], |
2539 | bg => [0, 0, 0, 0.2], |
… | |
… | |
2490 | } |
2545 | } |
2491 | |
2546 | |
2492 | sub size_request { |
2547 | sub size_request { |
2493 | my ($self) = @_; |
2548 | my ($self) = @_; |
2494 | |
2549 | |
2495 | (6) x 2 |
2550 | ($self->{fontsize} * $::FONTSIZE) x 2 |
2496 | } |
2551 | } |
2497 | |
2552 | |
2498 | sub toggle { |
2553 | sub toggle { |
2499 | my ($self) = @_; |
2554 | my ($self) = @_; |
2500 | |
2555 | |
… | |
… | |
2519 | sub _draw { |
2574 | sub _draw { |
2520 | my ($self) = @_; |
2575 | my ($self) = @_; |
2521 | |
2576 | |
2522 | $self->SUPER::_draw; |
2577 | $self->SUPER::_draw; |
2523 | |
2578 | |
2524 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
2579 | glTranslate $self->{padding_x}, $self->{padding_y}, 0; |
2525 | |
2580 | |
2526 | my ($w, $h) = @$self{qw(w h)}; |
2581 | my ($w, $h) = @$self{qw(w h)}; |
2527 | |
2582 | |
2528 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2583 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2529 | |
2584 | |
… | |
… | |
2536 | glDisable GL_TEXTURE_2D; |
2591 | glDisable GL_TEXTURE_2D; |
2537 | } |
2592 | } |
2538 | |
2593 | |
2539 | ############################################################################# |
2594 | ############################################################################# |
2540 | |
2595 | |
2541 | package CFPlus::UI::Image; |
2596 | package DC::UI::Image; |
2542 | |
2597 | |
2543 | our @ISA = CFPlus::UI::Base::; |
2598 | our @ISA = DC::UI::DrawBG::; |
2544 | |
2599 | |
2545 | use CFPlus::OpenGL; |
2600 | use DC::OpenGL; |
2546 | |
2601 | |
2547 | our %texture_cache; |
2602 | our %texture_cache; |
2548 | |
2603 | |
2549 | sub new { |
2604 | sub new { |
2550 | my $class = shift; |
2605 | my $class = shift; |
… | |
… | |
2557 | |
2612 | |
2558 | $self->{path} || $self->{tex} |
2613 | $self->{path} || $self->{tex} |
2559 | or Carp::croak "'path' or 'tex' attributes required"; |
2614 | or Carp::croak "'path' or 'tex' attributes required"; |
2560 | |
2615 | |
2561 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2616 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2562 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2617 | new_from_resource DC::Texture $self->{path}, mipmap => 1; |
2563 | |
2618 | |
2564 | CFPlus::weaken $texture_cache{$self->{path}}; |
2619 | DC::weaken $texture_cache{$self->{path}}; |
2565 | |
2620 | |
2566 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2621 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2567 | |
2622 | |
2568 | $self |
2623 | $self |
2569 | } |
2624 | } |
2570 | |
2625 | |
2571 | sub STORABLE_freeze { |
2626 | sub STORABLE_freeze { |
2572 | my ($self, $cloning) = @_; |
2627 | my ($self, $cloning) = @_; |
2573 | |
2628 | |
2574 | $self->{path} |
2629 | $self->{path} |
2575 | or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; |
2630 | or die "cannot serialise DC::UI::Image on non-loadable images\n"; |
2576 | |
2631 | |
2577 | $self->{path} |
2632 | $self->{path} |
2578 | } |
2633 | } |
2579 | |
2634 | |
2580 | sub STORABLE_attach { |
2635 | sub STORABLE_attach { |
2581 | my ($self, $cloning, $path) = @_; |
2636 | my ($self, $cloning, $path) = @_; |
2582 | |
2637 | |
2583 | $self->new (path => $path) |
2638 | $self->new (path => $path) |
2584 | } |
2639 | } |
2585 | |
2640 | |
|
|
2641 | sub set_texture { |
|
|
2642 | my ($self, $tex) = @_; |
|
|
2643 | |
|
|
2644 | $self->{tex} = $tex; |
|
|
2645 | $self->update; |
|
|
2646 | } |
|
|
2647 | |
2586 | sub size_request { |
2648 | sub size_request { |
2587 | my ($self) = @_; |
2649 | my ($self) = @_; |
2588 | |
2650 | |
2589 | (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) |
2651 | (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) |
2590 | } |
2652 | } |
2591 | |
2653 | |
2592 | sub _draw { |
2654 | sub _draw { |
2593 | my ($self) = @_; |
2655 | my ($self) = @_; |
|
|
2656 | |
|
|
2657 | $self->SUPER::_draw; |
2594 | |
2658 | |
2595 | my $tex = $self->{tex}; |
2659 | my $tex = $self->{tex}; |
2596 | |
2660 | |
2597 | my ($w, $h) = ($self->{w}, $self->{h}); |
2661 | my ($w, $h) = ($self->{w}, $self->{h}); |
2598 | |
2662 | |
… | |
… | |
2611 | glDisable GL_TEXTURE_2D; |
2675 | glDisable GL_TEXTURE_2D; |
2612 | } |
2676 | } |
2613 | |
2677 | |
2614 | ############################################################################# |
2678 | ############################################################################# |
2615 | |
2679 | |
2616 | package CFPlus::UI::ImageButton; |
2680 | package DC::UI::ImageButton; |
2617 | |
2681 | |
2618 | our @ISA = CFPlus::UI::Image::; |
2682 | our @ISA = DC::UI::Image::; |
2619 | |
2683 | |
2620 | use CFPlus::OpenGL; |
2684 | use DC::OpenGL; |
2621 | |
|
|
2622 | my %textures; |
|
|
2623 | |
2685 | |
2624 | sub new { |
2686 | sub new { |
2625 | my $class = shift; |
2687 | my $class = shift; |
2626 | |
2688 | |
2627 | my $self = $class->SUPER::new ( |
2689 | my $self = $class->SUPER::new ( |
2628 | padding_x => 4, |
2690 | padding_x => 4, |
2629 | padding_y => 4, |
2691 | padding_y => 4, |
2630 | fg => [1, 1, 1], |
2692 | fg => [1, 1, 1], |
2631 | active_fg => [0, 0, 1], |
2693 | active_fg => [0, 0, 1], |
2632 | can_hover => 1, |
2694 | can_hover => 1, |
2633 | align => 0, |
2695 | align => 0.5, |
2634 | valign => 0, |
2696 | valign => 0.5, |
2635 | can_events => 1, |
2697 | can_events => 1, |
2636 | @_ |
2698 | @_ |
2637 | ); |
2699 | ); |
2638 | } |
2700 | } |
2639 | |
2701 | |
|
|
2702 | sub invoke_button_down { |
|
|
2703 | my ($self, $ev, $x, $y) = @_; |
|
|
2704 | |
|
|
2705 | 1 |
|
|
2706 | } |
|
|
2707 | |
2640 | sub invoke_button_up { |
2708 | sub invoke_button_up { |
2641 | my ($self, $ev, $x, $y) = @_; |
2709 | my ($self, $ev, $x, $y) = @_; |
2642 | |
2710 | |
2643 | $self->emit ("activate") |
2711 | $self->emit ("activate") |
2644 | if $x >= 0 && $x < $self->{w} |
2712 | if $x >= 0 && $x < $self->{w} |
… | |
… | |
2647 | 1 |
2715 | 1 |
2648 | } |
2716 | } |
2649 | |
2717 | |
2650 | ############################################################################# |
2718 | ############################################################################# |
2651 | |
2719 | |
2652 | package CFPlus::UI::VGauge; |
2720 | package DC::UI::VGauge; |
2653 | |
2721 | |
2654 | our @ISA = CFPlus::UI::Base::; |
2722 | our @ISA = DC::UI::Base::; |
2655 | |
2723 | |
2656 | use List::Util qw(min max); |
2724 | use List::Util qw(min max); |
2657 | |
2725 | |
2658 | use CFPlus::OpenGL; |
2726 | use DC::OpenGL; |
2659 | |
2727 | |
2660 | my %tex = ( |
2728 | my %tex = ( |
2661 | food => [ |
2729 | food => [ |
2662 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2730 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2663 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2731 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2664 | ], |
2732 | ], |
2665 | grace => [ |
2733 | grace => [ |
2666 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2734 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2667 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2735 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2668 | ], |
2736 | ], |
2669 | hp => [ |
2737 | hp => [ |
2670 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2738 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2671 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2739 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2672 | ], |
2740 | ], |
2673 | mana => [ |
2741 | mana => [ |
2674 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2742 | map { new_from_resource DC::Texture $_, mipmap => 1 } |
2675 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2743 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2676 | ], |
2744 | ], |
2677 | ); |
2745 | ); |
2678 | |
2746 | |
2679 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
2747 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
… | |
… | |
2782 | glDisable GL_TEXTURE_2D; |
2850 | glDisable GL_TEXTURE_2D; |
2783 | } |
2851 | } |
2784 | |
2852 | |
2785 | ############################################################################# |
2853 | ############################################################################# |
2786 | |
2854 | |
|
|
2855 | package DC::UI::Progress; |
|
|
2856 | |
|
|
2857 | our @ISA = DC::UI::Label::; |
|
|
2858 | |
|
|
2859 | use DC::OpenGL; |
|
|
2860 | |
|
|
2861 | sub new { |
|
|
2862 | my ($class, %arg) = @_; |
|
|
2863 | |
|
|
2864 | my $self = $class->SUPER::new ( |
|
|
2865 | padding_x => 2, |
|
|
2866 | padding_y => 2, |
|
|
2867 | fg => [1, 1, 1], |
|
|
2868 | bg => [0, 0, 1, 0.2], |
|
|
2869 | bar => [0.7, 0.5, 0.1, 0.8], |
|
|
2870 | outline => [0.4, 0.3, 0], |
|
|
2871 | fontsize => 0.9, |
|
|
2872 | valign => 0.5, |
|
|
2873 | align => 0.5, |
|
|
2874 | can_events => 1, |
|
|
2875 | ellipsise => 1, |
|
|
2876 | label => "%d%%", |
|
|
2877 | %arg, |
|
|
2878 | ); |
|
|
2879 | |
|
|
2880 | $self->set_value ($arg{value} || -1); |
|
|
2881 | |
|
|
2882 | $self |
|
|
2883 | } |
|
|
2884 | |
|
|
2885 | sub set_label { |
|
|
2886 | my ($self, $label) = @_; |
|
|
2887 | |
|
|
2888 | return if $self->{label} eq $label; |
|
|
2889 | $self->{label} = $label; |
|
|
2890 | |
|
|
2891 | $self->DC::UI::Progress::set_value (0 + delete $self->{value}); |
|
|
2892 | } |
|
|
2893 | |
|
|
2894 | sub set_value { |
|
|
2895 | my ($self, $value) = @_; |
|
|
2896 | |
|
|
2897 | if ($self->{value} ne $value) { |
|
|
2898 | $self->{value} = $value; |
|
|
2899 | |
|
|
2900 | if ($value < 0) { |
|
|
2901 | $self->set_text ("-"); |
|
|
2902 | } else { |
|
|
2903 | $self->set_text (sprintf $self->{label}, $value * 100); |
|
|
2904 | } |
|
|
2905 | |
|
|
2906 | $self->update; |
|
|
2907 | } |
|
|
2908 | } |
|
|
2909 | |
|
|
2910 | sub _draw { |
|
|
2911 | my ($self) = @_; |
|
|
2912 | |
|
|
2913 | glEnable GL_BLEND; |
|
|
2914 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2915 | |
|
|
2916 | my $px = $self->{padding_x}; |
|
|
2917 | my $py = $self->{padding_y}; |
|
|
2918 | |
|
|
2919 | if ($self->{value} >= 0) { |
|
|
2920 | my $s = int $px + ($self->{w} - $px * 2) * $self->{value}; |
|
|
2921 | |
|
|
2922 | glColor_premultiply @{$self->{bar}}; |
|
|
2923 | glRect $px, $py, $s, $self->{h} - $py; |
|
|
2924 | glColor_premultiply @{$self->{bg}}; |
|
|
2925 | glRect $s , $py, $self->{w} - $px, $self->{h} - $py; |
|
|
2926 | } |
|
|
2927 | |
|
|
2928 | glColor_premultiply @{$self->{outline}}; |
|
|
2929 | |
|
|
2930 | $px -= .5; |
|
|
2931 | $py -= .5; |
|
|
2932 | |
|
|
2933 | glRect_lineloop $px, $py, $self->{w} - $px, $self->{h} - $py; |
|
|
2934 | |
|
|
2935 | glDisable GL_BLEND; |
|
|
2936 | |
|
|
2937 | { |
|
|
2938 | local $self->{bg}; # do not draw background |
|
|
2939 | $self->SUPER::_draw; |
|
|
2940 | } |
|
|
2941 | } |
|
|
2942 | |
|
|
2943 | ############################################################################# |
|
|
2944 | |
|
|
2945 | package DC::UI::ExperienceProgress; |
|
|
2946 | |
|
|
2947 | our @ISA = DC::UI::Progress::; |
|
|
2948 | |
|
|
2949 | sub new { |
|
|
2950 | my ($class, %arg) = @_; |
|
|
2951 | |
|
|
2952 | my $tt = exists $arg{tooltip} ? "$arg{tooltip}\n\n" : ""; |
|
|
2953 | |
|
|
2954 | my $self = $class->SUPER::new ( |
|
|
2955 | %arg, |
|
|
2956 | tooltip => sub { |
|
|
2957 | my ($self) = @_; |
|
|
2958 | |
|
|
2959 | sprintf "%slevel %d\n%s points\n%s next level\n%s to go, %d%% done", |
|
|
2960 | $tt, |
|
|
2961 | $self->{lvl}, |
|
|
2962 | ::formsep ($self->{exp}), |
|
|
2963 | ::formsep ($self->{nxt}), |
|
|
2964 | ::formsep ($self->{nxt} - $self->{exp}), |
|
|
2965 | $self->_percent * 100, |
|
|
2966 | }, |
|
|
2967 | ); |
|
|
2968 | |
|
|
2969 | $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } |
|
|
2970 | if $::CONN; |
|
|
2971 | |
|
|
2972 | $self |
|
|
2973 | } |
|
|
2974 | |
|
|
2975 | sub DESTROY { |
|
|
2976 | my ($self) = @_; |
|
|
2977 | |
|
|
2978 | delete $::CONN->{on_exp_update}{$self+0} |
|
|
2979 | if $::CONN; |
|
|
2980 | |
|
|
2981 | $self->SUPER::DESTROY; |
|
|
2982 | } |
|
|
2983 | |
|
|
2984 | sub _percent { |
|
|
2985 | my ($self) = @_; |
|
|
2986 | |
|
|
2987 | my $table = $::CONN && $::CONN->{exp_table} |
|
|
2988 | or return -1; |
|
|
2989 | |
|
|
2990 | my $l0 = $table->[$self->{lvl} - 1]; |
|
|
2991 | my $l1 = $table->[$self->{lvl}]; |
|
|
2992 | |
|
|
2993 | $self->{nxt} = $l1; |
|
|
2994 | |
|
|
2995 | ($self->{exp} - $l0) / ($l1 - $l0) |
|
|
2996 | } |
|
|
2997 | |
|
|
2998 | sub set_value { |
|
|
2999 | my ($self, $lvl, $exp) = @_; |
|
|
3000 | |
|
|
3001 | $self->{lvl} = $lvl; |
|
|
3002 | $self->{exp} = $exp; |
|
|
3003 | |
|
|
3004 | $self->SUPER::set_value ($self->_percent); |
|
|
3005 | } |
|
|
3006 | |
|
|
3007 | ############################################################################# |
|
|
3008 | |
2787 | package CFPlus::UI::Gauge; |
3009 | package DC::UI::Gauge; |
2788 | |
3010 | |
2789 | our @ISA = CFPlus::UI::VBox::; |
3011 | our @ISA = DC::UI::VBox::; |
2790 | |
3012 | |
2791 | sub new { |
3013 | sub new { |
2792 | my ($class, %arg) = @_; |
3014 | my ($class, %arg) = @_; |
2793 | |
3015 | |
2794 | my $self = $class->SUPER::new ( |
3016 | my $self = $class->SUPER::new ( |
… | |
… | |
2796 | can_hover => 1, |
3018 | can_hover => 1, |
2797 | can_events => 1, |
3019 | can_events => 1, |
2798 | %arg, |
3020 | %arg, |
2799 | ); |
3021 | ); |
2800 | |
3022 | |
2801 | $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); |
3023 | $self->add ($self->{value} = new DC::UI::Label valign => 1, align => 0.5, template => "999"); |
2802 | $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
3024 | $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2803 | $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); |
3025 | $self->add ($self->{max} = new DC::UI::Label valign => 0, align => 0.5, template => "999"); |
2804 | |
3026 | |
2805 | $self |
3027 | $self |
2806 | } |
3028 | } |
2807 | |
3029 | |
2808 | sub set_fontsize { |
3030 | sub set_fontsize { |
… | |
… | |
2829 | $self->{value}->set_text ($val); |
3051 | $self->{value}->set_text ($val); |
2830 | } |
3052 | } |
2831 | |
3053 | |
2832 | ############################################################################# |
3054 | ############################################################################# |
2833 | |
3055 | |
2834 | package CFPlus::UI::Slider; |
3056 | package DC::UI::Slider; |
2835 | |
3057 | |
2836 | use strict; |
3058 | use strict; |
2837 | |
3059 | |
2838 | use CFPlus::OpenGL; |
3060 | use DC::OpenGL; |
2839 | |
3061 | |
2840 | our @ISA = CFPlus::UI::DrawBG::; |
3062 | our @ISA = DC::UI::DrawBG::; |
2841 | |
3063 | |
2842 | my @tex = |
3064 | my @tex = |
2843 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } |
3065 | map { new_from_resource DC::Texture $_ } |
2844 | qw(s1_slider.png s1_slider_bg.png); |
3066 | qw(s1_slider.png s1_slider_bg.png); |
2845 | |
3067 | |
2846 | sub new { |
3068 | sub new { |
2847 | my $class = shift; |
3069 | my $class = shift; |
2848 | |
3070 | |
… | |
… | |
2885 | sub set_value { |
3107 | sub set_value { |
2886 | my ($self, $value) = @_; |
3108 | my ($self, $value) = @_; |
2887 | |
3109 | |
2888 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
3110 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
2889 | |
3111 | |
2890 | $hi = $lo + 1 if $hi <= $lo; |
3112 | $hi = $lo if $hi < $lo; |
2891 | |
3113 | |
2892 | $page = $hi - $lo if $page > $hi - $lo; |
3114 | $value = $hi - $page if $value > $hi - $page; |
2893 | |
|
|
2894 | $value = $lo if $value < $lo; |
3115 | $value = $lo if $value < $lo; |
2895 | $value = $hi - $page if $value > $hi - $page; |
|
|
2896 | |
3116 | |
2897 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
3117 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
2898 | if $unit; |
3118 | if $unit; |
2899 | |
3119 | |
2900 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
3120 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
… | |
… | |
2944 | sub invoke_mouse_wheel { |
3164 | sub invoke_mouse_wheel { |
2945 | my ($self, $ev) = @_; |
3165 | my ($self, $ev) = @_; |
2946 | |
3166 | |
2947 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
3167 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2948 | |
3168 | |
2949 | my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; |
3169 | my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2; |
2950 | |
3170 | |
2951 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
3171 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
2952 | |
3172 | |
2953 | 1 |
3173 | 1 |
2954 | } |
3174 | } |
… | |
… | |
2964 | my ($self) = @_; |
3184 | my ($self) = @_; |
2965 | |
3185 | |
2966 | unless ($self->{knob_w}) { |
3186 | unless ($self->{knob_w}) { |
2967 | $self->set_value ($self->{range}[0]); |
3187 | $self->set_value ($self->{range}[0]); |
2968 | |
3188 | |
2969 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
3189 | my ($value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
2970 | my $range = ($hi - $page - $lo) || 1e-100; |
3190 | my $range = ($hi - $page - $lo) || 1e-10; |
2971 | |
3191 | |
2972 | my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; |
3192 | my $knob_w = List::Util::min 1, $page / (($hi - $lo) || 1e-10) || 24 / $self->{w}; |
2973 | |
3193 | |
2974 | $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; |
3194 | $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; |
2975 | $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; |
3195 | $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; |
2976 | |
3196 | |
2977 | $value = ($value - $lo) / $range; |
3197 | $value = ($value - $lo) / $range; |
… | |
… | |
3007 | glDisable GL_TEXTURE_2D; |
3227 | glDisable GL_TEXTURE_2D; |
3008 | } |
3228 | } |
3009 | |
3229 | |
3010 | ############################################################################# |
3230 | ############################################################################# |
3011 | |
3231 | |
3012 | package CFPlus::UI::ValSlider; |
3232 | package DC::UI::ValSlider; |
3013 | |
3233 | |
3014 | our @ISA = CFPlus::UI::HBox::; |
3234 | our @ISA = DC::UI::HBox::; |
3015 | |
3235 | |
3016 | sub new { |
3236 | sub new { |
3017 | my ($class, %arg) = @_; |
3237 | my ($class, %arg) = @_; |
3018 | |
3238 | |
3019 | my $range = delete $arg{range}; |
3239 | my $range = delete $arg{range}; |
3020 | |
3240 | |
3021 | my $self = $class->SUPER::new ( |
3241 | my $self = $class->SUPER::new ( |
3022 | slider => (new CFPlus::UI::Slider expand => 1, range => $range), |
3242 | slider => (new DC::UI::Slider expand => 1, range => $range), |
3023 | entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), |
3243 | entry => (new DC::UI::Label text => "", template => delete $arg{template}), |
3024 | to_value => sub { shift }, |
3244 | to_value => sub { shift }, |
3025 | from_value => sub { shift }, |
3245 | from_value => sub { shift }, |
3026 | %arg, |
3246 | %arg, |
3027 | ); |
3247 | ); |
3028 | |
3248 | |
… | |
… | |
3048 | sub set_range { shift->{slider}->set_range (@_) } |
3268 | sub set_range { shift->{slider}->set_range (@_) } |
3049 | sub set_value { shift->{slider}->set_value (@_) } |
3269 | sub set_value { shift->{slider}->set_value (@_) } |
3050 | |
3270 | |
3051 | ############################################################################# |
3271 | ############################################################################# |
3052 | |
3272 | |
3053 | package CFPlus::UI::TextScroller; |
3273 | package DC::UI::TextScroller; |
3054 | |
3274 | |
3055 | our @ISA = CFPlus::UI::HBox::; |
3275 | our @ISA = DC::UI::HBox::; |
3056 | |
3276 | |
3057 | use CFPlus::OpenGL; |
3277 | use DC::OpenGL; |
3058 | |
3278 | |
3059 | sub new { |
3279 | sub new { |
3060 | my $class = shift; |
3280 | my $class = shift; |
3061 | |
3281 | |
3062 | my $self = $class->SUPER::new ( |
3282 | my $self = $class->SUPER::new ( |
… | |
… | |
3064 | can_events => 1, |
3284 | can_events => 1, |
3065 | indent => 0, |
3285 | indent => 0, |
3066 | #font => default_font |
3286 | #font => default_font |
3067 | @_, |
3287 | @_, |
3068 | |
3288 | |
3069 | layout => (new CFPlus::Layout), |
3289 | layout => (new DC::Layout), |
3070 | par => [], |
3290 | par => [], |
3071 | max_par => 0, |
3291 | max_par => 0, |
3072 | height => 0, |
3292 | height => 0, |
3073 | children => [ |
3293 | children => [ |
3074 | (new CFPlus::UI::Empty expand => 1), |
3294 | (new DC::UI::Empty expand => 1), |
3075 | (new CFPlus::UI::Slider vertical => 1), |
3295 | (new DC::UI::Slider vertical => 1), |
3076 | ], |
3296 | ], |
3077 | ); |
3297 | ); |
3078 | |
3298 | |
3079 | $self->{children}[1]->connect (changed => sub { $self->update }); |
3299 | $self->{children}[1]->connect (changed => sub { $self->update }); |
3080 | |
3300 | |
… | |
… | |
3136 | $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
3356 | $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); |
3137 | $layout->set_markup ($para->{markup}); |
3357 | $layout->set_markup ($para->{markup}); |
3138 | |
3358 | |
3139 | $layout->set_shapes ( |
3359 | $layout->set_shapes ( |
3140 | map |
3360 | map |
3141 | +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), |
3361 | +(0, $_->baseline_shift + $_->{padding_y} - $_->{h}, $_->{w}, $_->{h}), |
3142 | @{$para->{widget}} |
3362 | @{$para->{widget}} |
3143 | ); |
3363 | ); |
3144 | |
3364 | |
3145 | $layout |
3365 | $layout |
3146 | } |
3366 | } |
… | |
… | |
3263 | $ROOT->on_post_alloc ($self => sub { |
3483 | $ROOT->on_post_alloc ($self => sub { |
3264 | $self->force_uptodate; |
3484 | $self->force_uptodate; |
3265 | |
3485 | |
3266 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3486 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3267 | |
3487 | |
3268 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
3488 | $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub { |
3269 | glClearColor 0, 0, 0, 0; |
3489 | glClearColor 0, 0, 0, 0; |
3270 | glClear GL_COLOR_BUFFER_BIT; |
3490 | glClear GL_COLOR_BUFFER_BIT; |
3271 | |
3491 | |
3272 | package CFPlus::UI::Base; |
3492 | package DC::UI::Base; |
3273 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3493 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3274 | (0, 0, $self->{w}, $self->{h}); |
3494 | (0, 0, $self->{w}, $self->{h}); |
3275 | |
3495 | |
3276 | my $top = int $self->{children}[1]{range}[0]; |
3496 | my $top = int $self->{children}[1]{range}[0]; |
3277 | |
3497 | |
… | |
… | |
3288 | |
3508 | |
3289 | if ($y0 < $y + $h && $y < $y1) { |
3509 | if ($y0 < $y + $h && $y < $y1) { |
3290 | my $layout = $self->get_layout ($para); |
3510 | my $layout = $self->get_layout ($para); |
3291 | |
3511 | |
3292 | $layout->render ($para->{indent}, $y - $y0); |
3512 | $layout->render ($para->{indent}, $y - $y0); |
|
|
3513 | $layout->draw; |
3293 | |
3514 | |
3294 | if (my @w = @{ $para->{widget} }) { |
3515 | if (my @w = @{ $para->{widget} }) { |
3295 | my @s = $layout->get_shapes; |
3516 | my @s = $layout->get_shapes; |
3296 | |
3517 | |
3297 | for (@w) { |
3518 | for (@w) { |
… | |
… | |
3335 | $self->{children}[1]->draw; |
3556 | $self->{children}[1]->draw; |
3336 | } |
3557 | } |
3337 | |
3558 | |
3338 | ############################################################################# |
3559 | ############################################################################# |
3339 | |
3560 | |
3340 | package CFPlus::UI::Animator; |
3561 | package DC::UI::Animator; |
3341 | |
3562 | |
3342 | use CFPlus::OpenGL; |
3563 | use DC::OpenGL; |
3343 | |
3564 | |
3344 | our @ISA = CFPlus::UI::Bin::; |
3565 | our @ISA = DC::UI::Bin::; |
3345 | |
3566 | |
3346 | sub moveto { |
3567 | sub moveto { |
3347 | my ($self, $x, $y) = @_; |
3568 | my ($self, $x, $y) = @_; |
3348 | |
3569 | |
3349 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
3570 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
3377 | glPopMatrix; |
3598 | glPopMatrix; |
3378 | } |
3599 | } |
3379 | |
3600 | |
3380 | ############################################################################# |
3601 | ############################################################################# |
3381 | |
3602 | |
3382 | package CFPlus::UI::Flopper; |
3603 | package DC::UI::Flopper; |
3383 | |
3604 | |
3384 | our @ISA = CFPlus::UI::Button::; |
3605 | our @ISA = DC::UI::Button::; |
3385 | |
3606 | |
3386 | sub new { |
3607 | sub new { |
3387 | my $class = shift; |
3608 | my $class = shift; |
3388 | |
3609 | |
3389 | my $self = $class->SUPER::new ( |
3610 | my $self = $class->SUPER::new ( |
… | |
… | |
3401 | $self->{other}->toggle_visibility; |
3622 | $self->{other}->toggle_visibility; |
3402 | } |
3623 | } |
3403 | |
3624 | |
3404 | ############################################################################# |
3625 | ############################################################################# |
3405 | |
3626 | |
3406 | package CFPlus::UI::Tooltip; |
3627 | package DC::UI::Tooltip; |
3407 | |
3628 | |
3408 | our @ISA = CFPlus::UI::Bin::; |
3629 | our @ISA = DC::UI::Bin::; |
3409 | |
3630 | |
3410 | use CFPlus::OpenGL; |
3631 | use DC::OpenGL; |
3411 | |
3632 | |
3412 | sub new { |
3633 | sub new { |
3413 | my $class = shift; |
3634 | my $class = shift; |
3414 | |
3635 | |
3415 | $class->SUPER::new ( |
3636 | $class->SUPER::new ( |
… | |
… | |
3419 | } |
3640 | } |
3420 | |
3641 | |
3421 | sub set_tooltip_from { |
3642 | sub set_tooltip_from { |
3422 | my ($self, $widget) = @_; |
3643 | my ($self, $widget) = @_; |
3423 | |
3644 | |
3424 | $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1 |
|
|
3425 | if $widget->{tooltip} =~ /^#(.*)$/; |
|
|
3426 | |
|
|
3427 | my $tooltip = $widget->{tooltip}; |
3645 | my $tip = $widget->{tooltip}; |
|
|
3646 | $tip = $tip->($widget) if "CODE" eq ref $tip; |
|
|
3647 | |
|
|
3648 | $tip = DC::Pod::section_label tooltip => $1 |
|
|
3649 | if $tip =~ /^#(.*)$/; |
3428 | |
3650 | |
3429 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3651 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3430 | $tooltip .= "\n\n" . (ref $widget) . "\n" |
3652 | $tip .= "\n\n" . (ref $widget) . "\n" |
3431 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3653 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3432 | . "req $widget->{req_w} $widget->{req_h}\n" |
3654 | . "req $widget->{req_w} $widget->{req_h}\n" |
3433 | . "visible $widget->{visible}"; |
3655 | . "visible $widget->{visible}"; |
3434 | } |
3656 | } |
3435 | |
3657 | |
3436 | $tooltip =~ s/^\n+//; |
3658 | $tip =~ s/^\n+//; |
3437 | $tooltip =~ s/\n+$//; |
3659 | $tip =~ s/\n+$//; |
3438 | |
3660 | |
3439 | $self->add (new CFPlus::UI::Label |
3661 | $self->add (new DC::UI::Label |
|
|
3662 | fg => $DC::THEME{tooltip_fg}, |
3440 | markup => $tooltip, |
3663 | markup => $tip, |
3441 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3664 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
|
|
3665 | align => 0, |
3442 | fontsize => 0.8, |
3666 | fontsize => 0.8, |
3443 | style => 1, # FLAG_INVERSE |
3667 | style => $DC::THEME{tooltip_style}, # FLAG_INVERSE |
3444 | ellipsise => 0, |
3668 | ellipsise => 0, |
3445 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3669 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3446 | ); |
3670 | ); |
3447 | } |
3671 | } |
3448 | |
3672 | |
… | |
… | |
3483 | } |
3707 | } |
3484 | |
3708 | |
3485 | sub _draw { |
3709 | sub _draw { |
3486 | my ($self) = @_; |
3710 | my ($self) = @_; |
3487 | |
3711 | |
3488 | glTranslate 0.375, 0.375; |
|
|
3489 | |
|
|
3490 | my ($w, $h) = @$self{qw(w h)}; |
3712 | my ($w, $h) = @$self{qw(w h)}; |
3491 | |
3713 | |
3492 | glColor 1, 0.8, 0.4; |
3714 | glColor @{ $DC::THEME{tooltip_bg} }; |
3493 | glBegin GL_QUADS; |
3715 | glRect 0, 0, $w, $h; |
3494 | glVertex 0 , 0; |
|
|
3495 | glVertex 0 , $h; |
|
|
3496 | glVertex $w, $h; |
|
|
3497 | glVertex $w, 0; |
|
|
3498 | glEnd; |
|
|
3499 | |
3716 | |
3500 | glColor 0, 0, 0; |
3717 | glColor @{ $DC::THEME{tooltip_border} }; |
3501 | glBegin GL_LINE_LOOP; |
3718 | glRect_lineloop .5, .5, $w + .5, $h + .5; |
3502 | glVertex 0 , 0; |
|
|
3503 | glVertex 0 , $h; |
|
|
3504 | glVertex $w, $h; |
|
|
3505 | glVertex $w, 0; |
|
|
3506 | glEnd; |
|
|
3507 | |
3719 | |
3508 | glTranslate 2 - 0.375, 2 - 0.375; |
3720 | glTranslate 2, 2; |
3509 | |
3721 | |
3510 | $self->SUPER::_draw; |
3722 | $self->SUPER::_draw; |
3511 | } |
3723 | } |
3512 | |
3724 | |
3513 | ############################################################################# |
3725 | ############################################################################# |
3514 | |
3726 | |
3515 | package CFPlus::UI::Face; |
3727 | package DC::UI::Face; |
3516 | |
3728 | |
3517 | our @ISA = CFPlus::UI::DrawBG::; |
3729 | our @ISA = DC::UI::DrawBG::; |
3518 | |
3730 | |
3519 | use CFPlus::OpenGL; |
3731 | use DC::OpenGL; |
3520 | |
3732 | |
3521 | sub new { |
3733 | sub new { |
3522 | my $class = shift; |
3734 | my $class = shift; |
3523 | |
3735 | |
3524 | my $self = $class->SUPER::new ( |
3736 | my $self = $class->SUPER::new ( |
… | |
… | |
3527 | aspect => 1, |
3739 | aspect => 1, |
3528 | can_events => 0, |
3740 | can_events => 0, |
3529 | @_, |
3741 | @_, |
3530 | ); |
3742 | ); |
3531 | |
3743 | |
3532 | if ($self->{anim} && $self->{animspeed}) { |
|
|
3533 | CFPlus::weaken (my $widget = $self); |
|
|
3534 | |
|
|
3535 | $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; |
|
|
3536 | $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; |
|
|
3537 | $self->{timer} = Event->timer ( |
|
|
3538 | parked => 1, |
|
|
3539 | cb => sub { |
|
|
3540 | return unless $::CONN && $widget; |
|
|
3541 | |
|
|
3542 | ++$widget->{frame}; |
|
|
3543 | $widget->update_face; |
|
|
3544 | $widget->update; |
|
|
3545 | |
|
|
3546 | $widget->update_timer; |
|
|
3547 | }, |
|
|
3548 | ); |
|
|
3549 | |
|
|
3550 | $self->update_face; |
|
|
3551 | $self->update_timer; |
3744 | $self->update_anim; |
3552 | } |
|
|
3553 | |
3745 | |
3554 | $self |
3746 | $self |
3555 | } |
3747 | } |
3556 | |
3748 | |
3557 | sub update_timer { |
3749 | sub update_timer { |
3558 | my ($self) = @_; |
3750 | my ($self) = @_; |
3559 | |
3751 | |
3560 | return unless $self->{timer}; |
3752 | return unless $self->{timer}; |
3561 | |
3753 | |
3562 | if ($self->{visible}) { |
3754 | if ($self->{visible}) { |
3563 | $self->{timer}->at ( |
|
|
3564 | $self->{anim_start} |
|
|
3565 | + $self->{animspeed} |
|
|
3566 | * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} |
|
|
3567 | ); |
|
|
3568 | $self->{timer}->start; |
3755 | $self->{timer}->start; |
3569 | } else { |
3756 | } else { |
3570 | $self->{timer}->stop; |
3757 | $self->{timer}->stop; |
3571 | } |
3758 | } |
3572 | } |
3759 | } |
3573 | |
3760 | |
3574 | sub update_face { |
3761 | sub update_face { |
3575 | my ($self) = @_; |
3762 | my ($self) = @_; |
3576 | |
3763 | |
3577 | return unless $::CONN; |
|
|
3578 | |
|
|
3579 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
|
|
3580 | if ($anim && @$anim) { |
|
|
3581 | delete $self->{wait_face}; |
|
|
3582 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
|
|
3583 | $self->{tex} = $::CONN->{texture}[ $::CONN->{faceid}[$self->{face}] ]; |
|
|
3584 | } |
|
|
3585 | } |
|
|
3586 | } |
|
|
3587 | |
|
|
3588 | sub size_request { |
|
|
3589 | my ($self) = @_; |
|
|
3590 | |
|
|
3591 | if ($::CONN) { |
3764 | if ($::CONN) { |
3592 | if (my $faceid = $::CONN->{faceid}[$self->{face}]) { |
3765 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
3593 | if (my $tex = $::CONN->{texture}[$faceid]) { |
3766 | if ($anim && @$anim) { |
3594 | $self->{tex} = $tex; |
3767 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
3595 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
3768 | delete $self->{face_change_cb}; |
3596 | } else { |
3769 | |
3597 | $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { |
3770 | if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) { |
3598 | $self->realloc; |
3771 | unless ($tex->{name} || $tex->{loading}) { |
|
|
3772 | $tex->upload (sub { $self->reconfigure }); |
|
|
3773 | } |
3599 | }); |
3774 | } |
3600 | } |
3775 | } |
3601 | } |
3776 | } |
3602 | } |
3777 | } |
|
|
3778 | } |
|
|
3779 | |
|
|
3780 | sub update_anim { |
|
|
3781 | my ($self) = @_; |
|
|
3782 | |
|
|
3783 | if ($self->{anim} && $self->{animspeed}) { |
|
|
3784 | DC::weaken (my $widget = $self); |
|
|
3785 | |
|
|
3786 | $self->{animspeed} = List::Util::max 0.05, $self->{animspeed}; |
|
|
3787 | $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub { |
|
|
3788 | return unless $::CONN; |
|
|
3789 | |
|
|
3790 | my $w = $widget |
|
|
3791 | or return; |
|
|
3792 | |
|
|
3793 | ++$w->{frame}; |
|
|
3794 | $w->update_face; |
|
|
3795 | |
|
|
3796 | # somehow, $widget can go away |
|
|
3797 | $w->update; |
|
|
3798 | $w->update_timer; |
|
|
3799 | }; |
|
|
3800 | |
|
|
3801 | $self->update_face; |
|
|
3802 | $self->update_timer; |
|
|
3803 | } else { |
|
|
3804 | delete $self->{timer}; |
|
|
3805 | } |
|
|
3806 | } |
|
|
3807 | |
|
|
3808 | sub size_request { |
|
|
3809 | my ($self) = @_; |
|
|
3810 | |
|
|
3811 | if ($::CONN) { |
|
|
3812 | if (my $faceid = $::CONN->{face}[$self->{face}]{id}) { |
|
|
3813 | if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) { |
|
|
3814 | if ($tex->{name}) { |
|
|
3815 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
|
|
3816 | } elsif (!$tex->{loading}) { |
|
|
3817 | $tex->upload (sub { $self->reconfigure }); |
|
|
3818 | } |
|
|
3819 | } |
|
|
3820 | |
|
|
3821 | $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure }); |
|
|
3822 | } |
|
|
3823 | } |
3603 | |
3824 | |
3604 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3825 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3605 | } |
3826 | } |
3606 | |
3827 | |
3607 | sub update { |
3828 | sub update { |
3608 | my ($self) = @_; |
3829 | my ($self) = @_; |
3609 | |
3830 | |
3610 | return unless $self->{visible}; |
3831 | return unless $self->{visible}; |
3611 | |
3832 | |
3612 | $self->SUPER::update; |
3833 | $self->SUPER::update; |
|
|
3834 | } |
|
|
3835 | |
|
|
3836 | sub set_face { |
|
|
3837 | my ($self, $face) = @_; |
|
|
3838 | |
|
|
3839 | $self->{face} = $face; |
|
|
3840 | $self->reconfigure; |
|
|
3841 | } |
|
|
3842 | |
|
|
3843 | sub set_anim { |
|
|
3844 | my ($self, $anim) = @_; |
|
|
3845 | |
|
|
3846 | $self->{anim} = $anim; |
|
|
3847 | $self->update_anim; |
|
|
3848 | } |
|
|
3849 | |
|
|
3850 | sub set_animspeed { |
|
|
3851 | my ($self, $animspeed) = @_; |
|
|
3852 | |
|
|
3853 | $self->{animspeed} = $animspeed; |
|
|
3854 | $self->update_anim; |
3613 | } |
3855 | } |
3614 | |
3856 | |
3615 | sub invoke_visibility_change { |
3857 | sub invoke_visibility_change { |
3616 | my ($self) = @_; |
3858 | my ($self) = @_; |
3617 | |
3859 | |
… | |
… | |
3643 | $self->SUPER::destroy; |
3885 | $self->SUPER::destroy; |
3644 | } |
3886 | } |
3645 | |
3887 | |
3646 | ############################################################################# |
3888 | ############################################################################# |
3647 | |
3889 | |
3648 | package CFPlus::UI::Buttonbar; |
3890 | package DC::UI::Buttonbar; |
3649 | |
3891 | |
3650 | our @ISA = CFPlus::UI::HBox::; |
3892 | our @ISA = DC::UI::HBox::; |
3651 | |
3893 | |
3652 | # TODO: should actually wrap buttons and other goodies. |
3894 | # TODO: should actually wrap buttons and other goodies. |
3653 | |
3895 | |
3654 | ############################################################################# |
3896 | ############################################################################# |
3655 | |
3897 | |
3656 | package CFPlus::UI::Menu; |
3898 | package DC::UI::Menu; |
3657 | |
3899 | |
3658 | our @ISA = CFPlus::UI::Toplevel::; |
3900 | our @ISA = DC::UI::Toplevel::; |
3659 | |
3901 | |
3660 | use CFPlus::OpenGL; |
3902 | use DC::OpenGL; |
3661 | |
3903 | |
3662 | sub new { |
3904 | sub new { |
3663 | my $class = shift; |
3905 | my $class = shift; |
3664 | |
3906 | |
3665 | my $self = $class->SUPER::new ( |
3907 | my $self = $class->SUPER::new ( |
3666 | items => [], |
3908 | items => [], |
3667 | z => 100, |
3909 | z => 100, |
3668 | @_, |
3910 | @_, |
3669 | ); |
3911 | ); |
3670 | |
3912 | |
3671 | $self->add ($self->{vbox} = new CFPlus::UI::VBox); |
3913 | $self->add ($self->{vbox} = new DC::UI::VBox); |
3672 | |
3914 | |
3673 | for my $item (@{ $self->{items} }) { |
3915 | for my $item (@{ $self->{items} }) { |
3674 | my ($widget, $cb, $tooltip) = @$item; |
3916 | my ($widget, $cb, $tooltip) = @$item; |
3675 | |
3917 | |
3676 | # handle various types of items, only text for now |
3918 | # handle various types of items, only text for now |
3677 | if (!ref $widget) { |
3919 | if (!ref $widget) { |
3678 | if ($widget =~ /\t/) { |
3920 | if ($widget =~ /\t/) { |
3679 | my ($left, $right) = split /\t/, $widget, 2; |
3921 | my ($left, $right) = split /\t/, $widget, 2; |
3680 | |
3922 | |
3681 | $widget = new CFPlus::UI::HBox |
3923 | $widget = new DC::UI::HBox |
3682 | can_hover => 1, |
3924 | can_hover => 1, |
3683 | can_events => 1, |
3925 | can_events => 1, |
3684 | tooltip => $tooltip, |
3926 | tooltip => $tooltip, |
3685 | children => [ |
3927 | children => [ |
3686 | (new CFPlus::UI::Label markup => $left, expand => 1), |
3928 | (new DC::UI::Label markup => $left , align => 0, expand => 1), |
3687 | (new CFPlus::UI::Label markup => $right, align => +1), |
3929 | (new DC::UI::Label markup => $right, align => 1), |
3688 | ], |
3930 | ], |
3689 | ; |
3931 | ; |
3690 | |
3932 | |
3691 | } else { |
3933 | } else { |
3692 | $widget = new CFPlus::UI::Label |
3934 | $widget = new DC::UI::Label |
3693 | can_hover => 1, |
3935 | can_hover => 1, |
3694 | can_events => 1, |
3936 | can_events => 1, |
|
|
3937 | align => 0, |
3695 | markup => $widget, |
3938 | markup => $widget, |
3696 | tooltip => $tooltip; |
3939 | tooltip => $tooltip; |
3697 | } |
3940 | } |
3698 | } |
3941 | } |
3699 | |
3942 | |
… | |
… | |
3714 | # maybe save $GRAB? must be careful about events... |
3957 | # maybe save $GRAB? must be careful about events... |
3715 | $GRAB = $self; |
3958 | $GRAB = $self; |
3716 | $self->{button} = $ev->{button}; |
3959 | $self->{button} = $ev->{button}; |
3717 | |
3960 | |
3718 | $self->show; |
3961 | $self->show; |
3719 | $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
3962 | |
|
|
3963 | my $x = $ev->{x}; |
|
|
3964 | my $y = $ev->{y}; |
|
|
3965 | |
|
|
3966 | $self->{root}->on_post_alloc ($self => sub { |
|
|
3967 | $self->move_abs ($x - $self->{w} * 0.25, $y - $self->{border} * $::FONTSIZE * .5); |
|
|
3968 | }); |
|
|
3969 | |
|
|
3970 | 1 # so it can be used inside event handlers |
3720 | } |
3971 | } |
3721 | |
3972 | |
3722 | sub invoke_mouse_motion { |
3973 | sub invoke_mouse_motion { |
3723 | my ($self, $ev, $x, $y) = @_; |
3974 | my ($self, $ev, $x, $y) = @_; |
3724 | |
3975 | |
… | |
… | |
3745 | 1 |
3996 | 1 |
3746 | } |
3997 | } |
3747 | |
3998 | |
3748 | ############################################################################# |
3999 | ############################################################################# |
3749 | |
4000 | |
3750 | package CFPlus::UI::Multiplexer; |
4001 | package DC::UI::Multiplexer; |
3751 | |
4002 | |
3752 | our @ISA = CFPlus::UI::Container::; |
4003 | our @ISA = DC::UI::Container::; |
3753 | |
4004 | |
3754 | sub new { |
4005 | sub new { |
3755 | my $class = shift; |
4006 | my $class = shift; |
3756 | |
4007 | |
3757 | my $self = $class->SUPER::new ( |
4008 | my $self = $class->SUPER::new ( |
3758 | @_, |
4009 | @_, |
3759 | ); |
4010 | ); |
3760 | |
4011 | |
3761 | $self->{current} = $self->{children}[0] |
4012 | $self->set_current_page (0); |
3762 | if @{ $self->{children} }; |
|
|
3763 | |
4013 | |
3764 | $self |
4014 | $self |
3765 | } |
4015 | } |
3766 | |
4016 | |
3767 | sub add { |
4017 | sub add { |
3768 | my ($self, @widgets) = @_; |
4018 | my ($self, @widgets) = @_; |
3769 | |
4019 | |
3770 | $self->SUPER::add (@widgets); |
4020 | $self->SUPER::add (@widgets); |
3771 | |
4021 | |
3772 | $self->{current} = $self->{children}[0] |
4022 | $self->set_current_page (0) |
3773 | if @{ $self->{children} }; |
4023 | if @widgets == @{ $self->{children} }; |
3774 | } |
4024 | } |
3775 | |
4025 | |
3776 | sub get_current_page { |
4026 | sub get_current_page { |
3777 | my ($self) = @_; |
4027 | my ($self) = @_; |
3778 | |
4028 | |
… | |
… | |
3784 | |
4034 | |
3785 | my $widget = ref $page_or_widget |
4035 | my $widget = ref $page_or_widget |
3786 | ? $page_or_widget |
4036 | ? $page_or_widget |
3787 | : $self->{children}[$page_or_widget]; |
4037 | : $self->{children}[$page_or_widget]; |
3788 | |
4038 | |
|
|
4039 | $self->{current}->set_invisible if $self->{current} && $self->{visible}; |
|
|
4040 | |
3789 | $self->{current} = $widget; |
4041 | if (($self->{current} = $widget)) { |
|
|
4042 | $self->{current}->set_visible if $self->{current} && $self->{visible}; |
3790 | $self->{current}->configure (0, 0, $self->{w}, $self->{h}); |
4043 | $self->{current}->configure (0, 0, $self->{w}, $self->{h}); |
3791 | |
4044 | |
3792 | $self->emit (page_changed => $self->{current}); |
4045 | $self->emit (page_changed => $self->{current}); |
|
|
4046 | } |
3793 | |
4047 | |
3794 | $self->realloc; |
4048 | $self->realloc; |
3795 | } |
4049 | } |
3796 | |
4050 | |
3797 | sub visible_children { |
4051 | sub visible_children { |
3798 | $_[0]{current} |
4052 | $_[0]{current} || () |
3799 | } |
4053 | } |
3800 | |
4054 | |
3801 | sub size_request { |
4055 | sub size_request { |
3802 | my ($self) = @_; |
4056 | my ($self) = @_; |
3803 | |
4057 | |
|
|
4058 | $self->{current} |
3804 | $self->{current}->size_request |
4059 | ? $self->{current}->size_request |
|
|
4060 | : (0, 0) |
3805 | } |
4061 | } |
3806 | |
4062 | |
3807 | sub invoke_size_allocate { |
4063 | sub invoke_size_allocate { |
3808 | my ($self, $w, $h) = @_; |
4064 | my ($self, $w, $h) = @_; |
3809 | |
4065 | |
3810 | $self->{current}->configure (0, 0, $w, $h); |
4066 | $self->{current}->configure (0, 0, $w, $h) |
|
|
4067 | if $self->{current}; |
3811 | |
4068 | |
3812 | 1 |
4069 | 1 |
3813 | } |
4070 | } |
3814 | |
4071 | |
3815 | sub _draw { |
4072 | sub _draw { |
3816 | my ($self) = @_; |
4073 | my ($self) = @_; |
3817 | |
4074 | |
3818 | $self->{current}->draw; |
4075 | $self->{current}->draw |
|
|
4076 | if $self->{current}; |
3819 | } |
4077 | } |
3820 | |
4078 | |
3821 | ############################################################################# |
4079 | ############################################################################# |
3822 | |
4080 | |
3823 | package CFPlus::UI::Notebook; |
4081 | package DC::UI::Notebook; |
3824 | |
4082 | |
|
|
4083 | use DC::OpenGL; |
|
|
4084 | |
3825 | our @ISA = CFPlus::UI::VBox::; |
4085 | our @ISA = DC::UI::VBox::; |
3826 | |
4086 | |
3827 | sub new { |
4087 | sub new { |
3828 | my $class = shift; |
4088 | my $class = shift; |
3829 | |
4089 | |
3830 | my $self = $class->SUPER::new ( |
4090 | my $self = $class->SUPER::new ( |
3831 | buttonbar => (new CFPlus::UI::Buttonbar), |
4091 | buttonbar => (new DC::UI::Buttonbar), |
3832 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
4092 | multiplexer => (new DC::UI::Multiplexer expand => 1), |
|
|
4093 | active_outline => [.7, .7, 0.2], |
3833 | # filter => # will be put between multiplexer and $self |
4094 | # filter => # will be put between multiplexer and $self |
3834 | @_, |
4095 | @_, |
3835 | ); |
4096 | ); |
3836 | |
4097 | |
3837 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
4098 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
… | |
… | |
3845 | |
4106 | |
3846 | for my $child (@$widgets) { |
4107 | for my $child (@$widgets) { |
3847 | Scalar::Util::weaken $child; |
4108 | Scalar::Util::weaken $child; |
3848 | $child->{c_tab_} ||= do { |
4109 | $child->{c_tab_} ||= do { |
3849 | my $tab = |
4110 | my $tab = |
3850 | (UNIVERSAL::isa $child->{c_tab}, "CFPlus::UI::Base") |
4111 | (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base") |
3851 | ? $child->{c_tab} |
4112 | ? $child->{c_tab} |
3852 | : new CFPlus::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; |
4113 | : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; |
3853 | |
4114 | |
3854 | $tab->connect (activate => sub { |
4115 | $tab->connect (activate => sub { |
3855 | $wself->set_current_page ($child); |
4116 | $wself->set_current_page ($child); |
3856 | }); |
4117 | }); |
3857 | |
4118 | |
… | |
… | |
3889 | sub pages { |
4150 | sub pages { |
3890 | my ($self) = @_; |
4151 | my ($self) = @_; |
3891 | $self->{multiplexer}->children |
4152 | $self->{multiplexer}->children |
3892 | } |
4153 | } |
3893 | |
4154 | |
|
|
4155 | sub page_index { |
|
|
4156 | my ($self, $widget) = @_; |
|
|
4157 | |
|
|
4158 | my $i = 0; |
|
|
4159 | for ($self->pages) { |
|
|
4160 | if ($_ eq $widget) { return $i }; |
|
|
4161 | $i++; |
|
|
4162 | } |
|
|
4163 | |
|
|
4164 | undef |
|
|
4165 | } |
|
|
4166 | |
3894 | sub add_tab { |
4167 | sub add_tab { |
3895 | my ($self, $title, $widget, $tooltip) = @_; |
4168 | my ($self, $title, $widget, $tooltip) = @_; |
3896 | |
4169 | |
3897 | $title = [$title, $tooltip] unless ref $title; |
4170 | $title = [$title, $tooltip] unless ref $title; |
3898 | $widget->{c_tab} = $title; |
4171 | $widget->{c_tab} = $title; |
… | |
… | |
3911 | |
4184 | |
3912 | $self->{multiplexer}->set_current_page ($page); |
4185 | $self->{multiplexer}->set_current_page ($page); |
3913 | $self->emit (page_changed => $self->{multiplexer}{current}); |
4186 | $self->emit (page_changed => $self->{multiplexer}{current}); |
3914 | } |
4187 | } |
3915 | |
4188 | |
|
|
4189 | sub _draw { |
|
|
4190 | my ($self) = @_; |
|
|
4191 | |
|
|
4192 | $self->SUPER::_draw (); |
|
|
4193 | |
|
|
4194 | if (my $cur = $self->{multiplexer}{current}) { |
|
|
4195 | if ($cur = $cur->{c_tab_}) { |
|
|
4196 | glTranslate $self->{buttonbar}{x} + $cur->{x}, |
|
|
4197 | $self->{buttonbar}{y} + $cur->{y}; |
|
|
4198 | glLineWidth 3; |
|
|
4199 | #glEnable GL_BLEND; |
|
|
4200 | #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
4201 | glColor @{$self->{active_outline}}; |
|
|
4202 | glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5; |
|
|
4203 | glLineWidth 1; |
|
|
4204 | #glDisable GL_BLEND; |
|
|
4205 | } |
|
|
4206 | } |
|
|
4207 | } |
|
|
4208 | |
3916 | ############################################################################# |
4209 | ############################################################################# |
3917 | |
4210 | |
3918 | package CFPlus::UI::Selector; |
4211 | package DC::UI::Selector; |
3919 | |
4212 | |
3920 | use utf8; |
4213 | use utf8; |
3921 | |
4214 | |
3922 | our @ISA = CFPlus::UI::Button::; |
4215 | our @ISA = DC::UI::Button::; |
3923 | |
4216 | |
3924 | sub new { |
4217 | sub new { |
3925 | my $class = shift; |
4218 | my $class = shift; |
3926 | |
4219 | |
3927 | my $self = $class->SUPER::new ( |
4220 | my $self = $class->SUPER::new ( |
… | |
… | |
3944 | my ($value, $title, $tooltip) = @$_; |
4237 | my ($value, $title, $tooltip) = @$_; |
3945 | |
4238 | |
3946 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
4239 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
3947 | } |
4240 | } |
3948 | |
4241 | |
3949 | CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); |
4242 | DC::UI::Menu->new (items => \@menu_items)->popup ($ev); |
3950 | } |
4243 | } |
3951 | |
4244 | |
3952 | sub _set_value { |
4245 | sub _set_value { |
3953 | my ($self, $value) = @_; |
4246 | my ($self, $value) = @_; |
3954 | |
4247 | |
3955 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} } |
4248 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} }; |
|
|
4249 | $item ||= $self->{options}[0] |
3956 | or return; |
4250 | or return; |
3957 | |
4251 | |
3958 | $self->{value} = $item->[0]; |
4252 | $self->{value} = $item->[0]; |
3959 | $self->set_markup ("$item->[1] ⇓"); |
4253 | $self->set_markup ("$item->[1] ⇓"); |
3960 | $self->set_tooltip ($item->[2]); |
4254 | # $self->set_tooltip ($item->[2]); |
3961 | } |
4255 | } |
3962 | |
4256 | |
3963 | sub set_value { |
4257 | sub set_value { |
3964 | my ($self, $value) = @_; |
4258 | my ($self, $value) = @_; |
3965 | |
4259 | |
… | |
… | |
3967 | |
4261 | |
3968 | $self->_set_value ($value); |
4262 | $self->_set_value ($value); |
3969 | $self->emit (changed => $value); |
4263 | $self->emit (changed => $value); |
3970 | } |
4264 | } |
3971 | |
4265 | |
|
|
4266 | sub set_options { |
|
|
4267 | my ($self, $options) = @_; |
|
|
4268 | |
|
|
4269 | $self->{options} = $options; |
|
|
4270 | $self->_set_value ($self->{value}); |
|
|
4271 | } |
|
|
4272 | |
3972 | ############################################################################# |
4273 | ############################################################################# |
3973 | |
4274 | |
3974 | package CFPlus::UI::Statusbox; |
4275 | package DC::UI::Statusbox; |
3975 | |
4276 | |
3976 | our @ISA = CFPlus::UI::VBox::; |
4277 | our @ISA = DC::UI::VBox::; |
3977 | |
4278 | |
3978 | sub new { |
4279 | sub new { |
3979 | my $class = shift; |
4280 | my $class = shift; |
3980 | |
4281 | |
3981 | my $self = $class->SUPER::new ( |
4282 | my $self = $class->SUPER::new ( |
3982 | fontsize => 0.8, |
4283 | fontsize => 0.8, |
3983 | @_, |
4284 | @_, |
3984 | ); |
4285 | ); |
3985 | |
4286 | |
3986 | CFPlus::weaken (my $this = $self); |
4287 | DC::weaken (my $this = $self); |
3987 | |
4288 | |
3988 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
4289 | $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; |
3989 | |
4290 | |
3990 | $self |
4291 | $self |
3991 | } |
4292 | } |
3992 | |
4293 | |
3993 | sub reorder { |
4294 | sub reorder { |
3994 | my ($self) = @_; |
4295 | my ($self) = @_; |
3995 | my $NOW = Time::HiRes::time; |
4296 | my $NOW = EV::time; |
3996 | |
4297 | |
3997 | # freeze display when hovering over any label |
4298 | # freeze display when hovering over any label |
3998 | return if $CFPlus::UI::TOOLTIP->{owner} |
4299 | return if $DC::UI::TOOLTIP->{owner} |
3999 | && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, |
4300 | && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, |
4000 | values %{ $self->{item} }; |
4301 | values %{ $self->{item} }; |
4001 | |
4302 | |
4002 | while (my ($k, $v) = each %{ $self->{item} }) { |
4303 | while (my ($k, $v) = each %{ $self->{item} }) { |
4003 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4304 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4004 | } |
4305 | } |
|
|
4306 | |
|
|
4307 | $self->{timer}->set (1, 1); |
4005 | |
4308 | |
4006 | my @widgets; |
4309 | my @widgets; |
4007 | |
4310 | |
4008 | my @items = sort { |
4311 | my @items = sort { |
4009 | $a->{pri} <=> $b->{pri} |
4312 | $a->{pri} <=> $b->{pri} |
4010 | or $b->{id} <=> $a->{id} |
4313 | or $b->{id} <=> $a->{id} |
4011 | } values %{ $self->{item} }; |
4314 | } values %{ $self->{item} }; |
4012 | |
|
|
4013 | $self->{timer}->interval (1); |
|
|
4014 | |
4315 | |
4015 | my $count = 10 + 1; |
4316 | my $count = 10 + 1; |
4016 | for my $item (@items) { |
4317 | for my $item (@items) { |
4017 | last unless --$count; |
4318 | last unless --$count; |
4018 | |
4319 | |
… | |
… | |
4025 | for ($short) { |
4326 | for ($short) { |
4026 | s/^\s+//; |
4327 | s/^\s+//; |
4027 | s/\s+/ /g; |
4328 | s/\s+/ /g; |
4028 | } |
4329 | } |
4029 | |
4330 | |
4030 | new CFPlus::UI::Label |
4331 | new DC::UI::Label |
4031 | markup => $short, |
4332 | markup => $short, |
4032 | tooltip => $item->{tooltip}, |
4333 | tooltip => $item->{tooltip}, |
4033 | tooltip_font => $::FONT_PROP, |
4334 | tooltip_font => $::FONT_PROP, |
4034 | tooltip_width => 0.67, |
4335 | tooltip_width => 0.67, |
4035 | fontsize => $item->{fontsize} || $self->{fontsize}, |
4336 | fontsize => $item->{fontsize} || $self->{fontsize}, |
4036 | max_w => $::WIDTH * 0.44, |
4337 | max_w => $::WIDTH * 0.44, |
|
|
4338 | align => 0, |
4037 | fg => [@{ $item->{fg} }], |
4339 | fg => [@{ $item->{fg} }], |
4038 | can_events => 1, |
4340 | can_events => 1, |
4039 | can_hover => 1 |
4341 | can_hover => 1 |
4040 | }; |
4342 | }; |
4041 | |
4343 | |
4042 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
4344 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
4043 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
4345 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
4044 | $label->update; |
4346 | $label->update; |
4045 | $label->set_max_size (undef, $label->{req_h} * $diff) |
4347 | $label->set_max_size (undef, $label->{req_h} * $diff) |
4046 | if $diff < 1; |
4348 | if $diff < 1; |
4047 | $self->{timer}->interval (1/30); |
4349 | $self->{timer}->set (1/30, 1/30); |
4048 | } else { |
4350 | } else { |
4049 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4351 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4050 | } |
4352 | } |
4051 | |
4353 | |
4052 | push @widgets, $label; |
4354 | push @widgets, $label; |
4053 | } |
4355 | } |
|
|
4356 | |
|
|
4357 | my $hash = join ",", @widgets; |
|
|
4358 | return if $hash eq $self->{last_widget_hash}; |
|
|
4359 | $self->{last_widget_hash} = $hash; |
4054 | |
4360 | |
4055 | $self->clear; |
4361 | $self->clear; |
4056 | $self->SUPER::add (reverse @widgets); |
4362 | $self->SUPER::add (reverse @widgets); |
4057 | } |
4363 | } |
4058 | |
4364 | |
… | |
… | |
4114 | $self->SUPER::destroy; |
4420 | $self->SUPER::destroy; |
4115 | } |
4421 | } |
4116 | |
4422 | |
4117 | ############################################################################# |
4423 | ############################################################################# |
4118 | |
4424 | |
4119 | package CFPlus::UI::Root; |
4425 | package DC::UI::Root; |
4120 | |
4426 | |
4121 | our @ISA = CFPlus::UI::Container::; |
4427 | our @ISA = DC::UI::Container::; |
4122 | |
4428 | |
4123 | use List::Util qw(min max); |
4429 | use List::Util qw(min max); |
4124 | |
4430 | |
4125 | use CFPlus::OpenGL; |
4431 | use DC::OpenGL; |
4126 | |
4432 | |
4127 | sub new { |
4433 | sub new { |
4128 | my $class = shift; |
4434 | my $class = shift; |
4129 | |
4435 | |
4130 | my $self = $class->SUPER::new ( |
4436 | my $self = $class->SUPER::new ( |
4131 | visible => 1, |
4437 | visible => 1, |
4132 | @_, |
4438 | @_, |
4133 | ); |
4439 | ); |
4134 | |
4440 | |
4135 | CFPlus::weaken ($self->{root} = $self); |
4441 | DC::weaken ($self->{root} = $self); |
4136 | |
4442 | |
4137 | $self |
4443 | $self |
4138 | } |
4444 | } |
4139 | |
4445 | |
4140 | sub size_request { |
4446 | sub size_request { |
… | |
… | |
4188 | } |
4494 | } |
4189 | |
4495 | |
4190 | sub update { |
4496 | sub update { |
4191 | my ($self) = @_; |
4497 | my ($self) = @_; |
4192 | |
4498 | |
4193 | $::WANT_REFRESH++; |
4499 | $::WANT_REFRESH = 1; |
4194 | } |
4500 | } |
4195 | |
4501 | |
4196 | sub add { |
4502 | sub add { |
4197 | my ($self, @children) = @_; |
4503 | my ($self, @children) = @_; |
4198 | |
4504 | |
… | |
… | |
4265 | |
4571 | |
4266 | delete $queue{$widget+0}; |
4572 | delete $queue{$widget+0}; |
4267 | |
4573 | |
4268 | my ($w, $h) = $widget->size_request; |
4574 | my ($w, $h) = $widget->size_request; |
4269 | |
4575 | |
4270 | $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2; |
4576 | $w += $widget->{padding_x} * 2; |
4271 | $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2; |
4577 | $h += $widget->{padding_y} * 2; |
|
|
4578 | |
|
|
4579 | $w = max $widget->{min_w}, $w; |
|
|
4580 | $h = max $widget->{min_h}, $h; |
4272 | |
4581 | |
4273 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
4582 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
4274 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
4583 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
4275 | |
4584 | |
4276 | $w = $widget->{force_w} if exists $widget->{force_w}; |
4585 | $w = $widget->{force_w} if exists $widget->{force_w}; |
… | |
… | |
4343 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4652 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4344 | glMatrixMode GL_MODELVIEW; |
4653 | glMatrixMode GL_MODELVIEW; |
4345 | glLoadIdentity; |
4654 | glLoadIdentity; |
4346 | |
4655 | |
4347 | { |
4656 | { |
4348 | package CFPlus::UI::Base; |
4657 | package DC::UI::Base; |
4349 | |
4658 | |
4350 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4659 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4351 | (0, 0, $self->{w}, $self->{h}); |
4660 | (0, 0, $self->{w}, $self->{h}); |
4352 | |
4661 | |
4353 | $self->_draw; |
4662 | $self->_draw; |
4354 | } |
4663 | } |
4355 | } |
4664 | } |
4356 | |
4665 | |
4357 | ############################################################################# |
4666 | ############################################################################# |
4358 | |
4667 | |
4359 | package CFPlus::UI; |
4668 | package DC::UI; |
4360 | |
4669 | |
4361 | $ROOT = new CFPlus::UI::Root; |
4670 | $ROOT = new DC::UI::Root; |
4362 | $TOOLTIP = new CFPlus::UI::Tooltip z => 900; |
4671 | $TOOLTIP = new DC::UI::Tooltip z => 900; |
4363 | |
4672 | |
4364 | 1 |
4673 | 1 |
4365 | |
|
|