1 | package CFPlus::UI; |
1 | package DC::UI; |
2 | |
2 | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use List::Util (); |
6 | use List::Util (); |
7 | use Event; |
|
|
8 | |
7 | |
9 | use CFPlus; |
8 | use DC; |
10 | use CFPlus::Pod; |
9 | use DC::Pod; |
11 | use CFPlus::Texture; |
10 | use DC::Texture; |
12 | |
11 | |
13 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
14 | |
13 | |
15 | our $LAYOUT; |
14 | our $LAYOUT; |
16 | our $ROOT; |
15 | our $ROOT; |
17 | our $TOOLTIP; |
16 | our $TOOLTIP; |
18 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
19 | |
18 | |
20 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
21 | |
20 | |
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
21 | our $TOOLTIP_WATCHER = EV::timer_ns 0, 0.03, sub { |
|
|
22 | $_[0]->stop; |
|
|
23 | |
23 | if (!$GRAB) { |
24 | if (!$GRAB) { |
24 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
25 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
25 | if (length $widget->{tooltip}) { |
26 | if (length $widget->{tooltip}) { |
26 | if ($TOOLTIP->{owner} != $widget) { |
27 | if ($TOOLTIP->{owner} != $widget) { |
27 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
28 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
… | |
… | |
30 | $TOOLTIP->{owner} = $widget; |
31 | $TOOLTIP->{owner} = $widget; |
31 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
32 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
32 | |
33 | |
33 | return if $ENV{CFPLUS_DEBUG} & 8; |
34 | return if $ENV{CFPLUS_DEBUG} & 8; |
34 | |
35 | |
35 | my $tip = $widget->{tooltip}; |
|
|
36 | |
|
|
37 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
38 | |
|
|
39 | $TOOLTIP->set_tooltip_from ($widget); |
36 | $TOOLTIP->set_tooltip_from ($widget); |
40 | $TOOLTIP->show; |
37 | $TOOLTIP->show; |
41 | } |
38 | } |
42 | |
39 | |
43 | return; |
40 | return; |
… | |
… | |
46 | } |
43 | } |
47 | |
44 | |
48 | $TOOLTIP->hide; |
45 | $TOOLTIP->hide; |
49 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
46 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
50 | delete $TOOLTIP->{owner}; |
47 | delete $TOOLTIP->{owner}; |
51 | }); |
48 | }; |
52 | |
49 | |
53 | sub get_layout { |
50 | sub get_layout { |
54 | my $layout; |
51 | my $layout; |
55 | |
52 | |
56 | for (grep { $_->{name} } values %WIDGET) { |
53 | for (grep { $_->{name} } values %WIDGET) { |
… | |
… | |
91 | my $hover = $HOVER; $HOVER = $widget; |
88 | my $hover = $HOVER; $HOVER = $widget; |
92 | |
89 | |
93 | $hover->update if $hover && $hover->{can_hover}; |
90 | $hover->update if $hover && $hover->{can_hover}; |
94 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
91 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
95 | |
92 | |
96 | $TOOLTIP_WATCHER->start; |
93 | $TOOLTIP_WATCHER->again; |
97 | } |
94 | } |
98 | } |
95 | } |
99 | |
96 | |
100 | sub feed_sdl_button_down_event { |
97 | sub feed_sdl_button_down_event { |
101 | my ($ev) = @_; |
98 | my ($ev) = @_; |
… | |
… | |
107 | my $widget = $ROOT->find_widget ($x, $y); |
104 | my $widget = $ROOT->find_widget ($x, $y); |
108 | |
105 | |
109 | $GRAB = $widget; |
106 | $GRAB = $widget; |
110 | $GRAB->update if $GRAB; |
107 | $GRAB->update if $GRAB; |
111 | |
108 | |
112 | $TOOLTIP_WATCHER->cb->(); |
109 | $TOOLTIP_WATCHER->invoke; |
113 | } |
110 | } |
114 | |
111 | |
115 | if ($GRAB) { |
112 | if ($GRAB) { |
116 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
113 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
117 | # mousewheel |
114 | # mousewheel |
118 | $ev->{dx} = 0; |
|
|
119 | $ev->{dy} = $ev->{button} * 2 - 9; |
115 | my $delta = $ev->{button} * 2 - 9; |
|
|
116 | my $shift = $ev->{mod} & DC::KMOD_SHIFT; |
|
|
117 | |
|
|
118 | $ev->{dx} = $shift ? $delta : 0; |
|
|
119 | $ev->{dy} = $shift ? 0 : $delta; |
|
|
120 | |
120 | $GRAB->emit (mouse_wheel => $ev); |
121 | $GRAB->emit (mouse_wheel => $ev); |
121 | } else { |
122 | } else { |
122 | $GRAB->emit (button_down => $ev) |
123 | $GRAB->emit (button_down => $ev) |
123 | } |
124 | } |
124 | } |
125 | } |
… | |
… | |
138 | my $grab = $GRAB; undef $GRAB; |
139 | my $grab = $GRAB; undef $GRAB; |
139 | $grab->update if $grab; |
140 | $grab->update if $grab; |
140 | $GRAB->update if $GRAB; |
141 | $GRAB->update if $GRAB; |
141 | |
142 | |
142 | check_hover $widget; |
143 | check_hover $widget; |
143 | $TOOLTIP_WATCHER->cb->(); |
144 | $TOOLTIP_WATCHER->invoke; |
144 | } |
145 | } |
145 | } |
146 | } |
146 | |
147 | |
147 | sub feed_sdl_motion_event { |
148 | sub feed_sdl_motion_event { |
148 | my ($ev) = @_; |
149 | my ($ev) = @_; |
… | |
… | |
210 | reconfigure_widgets; |
211 | reconfigure_widgets; |
211 | } |
212 | } |
212 | |
213 | |
213 | ############################################################################# |
214 | ############################################################################# |
214 | |
215 | |
215 | package CFPlus::UI::Event; |
216 | package DC::UI::Event; |
216 | |
217 | |
217 | sub xy { |
218 | sub xy { |
218 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
219 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
219 | } |
220 | } |
220 | |
221 | |
221 | ############################################################################# |
222 | ############################################################################# |
222 | |
223 | |
223 | package CFPlus::UI::Base; |
224 | package DC::UI::Base; |
224 | |
225 | |
225 | use strict; |
226 | use strict; |
226 | |
227 | |
227 | use CFPlus::OpenGL; |
228 | use DC::OpenGL; |
228 | |
229 | |
229 | sub new { |
230 | sub new { |
230 | my $class = shift; |
231 | my $class = shift; |
231 | |
232 | |
232 | my $self = bless { |
233 | my $self = bless { |
… | |
… | |
237 | h => undef, |
238 | h => undef, |
238 | can_events => 1, |
239 | can_events => 1, |
239 | @_ |
240 | @_ |
240 | }, $class; |
241 | }, $class; |
241 | |
242 | |
242 | CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
243 | DC::weaken ($DC::UI::WIDGET{$self+0} = $self); |
243 | |
244 | |
244 | for (keys %$self) { |
245 | for (keys %$self) { |
245 | if (/^on_(.*)$/) { |
246 | if (/^on_(.*)$/) { |
246 | $self->connect ($1 => delete $self->{$_}); |
247 | $self->connect ($1 => delete $self->{$_}); |
247 | } |
248 | } |
248 | } |
249 | } |
249 | |
250 | |
250 | if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { |
251 | if (my $layout = $DC::UI::LAYOUT->{$self->{name}}) { |
251 | $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; |
252 | $self->{x} = $layout->{x} * $DC::UI::ROOT->{alloc_w} if exists $layout->{x}; |
252 | $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; |
253 | $self->{y} = $layout->{y} * $DC::UI::ROOT->{alloc_h} if exists $layout->{y}; |
253 | $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; |
254 | $self->{force_w} = $layout->{w} * $DC::UI::ROOT->{alloc_w} if exists $layout->{w}; |
254 | $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; |
255 | $self->{force_h} = $layout->{h} * $DC::UI::ROOT->{alloc_h} if exists $layout->{h}; |
255 | |
256 | |
256 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
257 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
257 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
258 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
258 | |
259 | |
259 | $self->show if $layout->{show}; |
260 | $self->show if $layout->{show}; |
… | |
… | |
269 | $self->emit ("destroy"); |
270 | $self->emit ("destroy"); |
270 | %$self = (); |
271 | %$self = (); |
271 | } |
272 | } |
272 | |
273 | |
273 | sub TO_JSON { |
274 | sub TO_JSON { |
274 | { __widget_ref__ => $_[0]{s_id} } |
275 | { "\fw" => $_[0]{s_id} } |
275 | } |
276 | } |
276 | |
277 | |
277 | sub show { |
278 | sub show { |
278 | my ($self) = @_; |
279 | my ($self) = @_; |
279 | |
280 | |
280 | return if $self->{parent}; |
281 | return if $self->{parent}; |
281 | |
282 | |
282 | $CFPlus::UI::ROOT->add ($self); |
283 | $DC::UI::ROOT->add ($self); |
283 | } |
284 | } |
284 | |
285 | |
285 | sub set_visible { |
286 | sub set_visible { |
286 | my ($self) = @_; |
287 | my ($self) = @_; |
287 | |
288 | |
… | |
… | |
308 | delete $self->{root}; |
309 | delete $self->{root}; |
309 | |
310 | |
310 | undef $GRAB if $GRAB == $self; |
311 | undef $GRAB if $GRAB == $self; |
311 | undef $HOVER if $HOVER == $self; |
312 | undef $HOVER if $HOVER == $self; |
312 | |
313 | |
313 | $CFPlus::UI::TOOLTIP_WATCHER->cb->() |
314 | $DC::UI::TOOLTIP_WATCHER->invoke |
314 | if $TOOLTIP->{owner} == $self; |
315 | if $TOOLTIP->{owner} == $self; |
315 | |
316 | |
316 | $self->emit ("focus_out"); |
317 | $self->emit ("focus_out"); |
317 | $self->emit (visibility_change => 0); |
318 | $self->emit (visibility_change => 0); |
318 | } |
319 | } |
… | |
… | |
337 | sub hide { |
338 | sub hide { |
338 | my ($self) = @_; |
339 | my ($self) = @_; |
339 | |
340 | |
340 | $self->set_invisible; |
341 | $self->set_invisible; |
341 | |
342 | |
|
|
343 | # extra $parent copy for 5.8.8+ bug workaround |
|
|
344 | # (otherwise $_[0] in remove gets freed |
|
|
345 | if (my $parent = $self->{parent}) { |
342 | $self->{parent}->remove ($self) |
346 | $parent->remove ($self); |
343 | if $self->{parent}; |
347 | } |
344 | } |
348 | } |
345 | |
349 | |
346 | sub move_abs { |
350 | sub move_abs { |
347 | my ($self, $x, $y, $z) = @_; |
351 | my ($self, $x, $y, $z) = @_; |
348 | |
352 | |
… | |
… | |
360 | $self->{force_h} = $h; |
364 | $self->{force_h} = $h; |
361 | |
365 | |
362 | $self->realloc; |
366 | $self->realloc; |
363 | } |
367 | } |
364 | |
368 | |
|
|
369 | # traverse the widget chain up to find the maximum "physical" size constraints |
|
|
370 | sub get_max_wh { |
|
|
371 | my ($self) = @_; |
|
|
372 | |
|
|
373 | return $self->{parent}->get_max_wh |
|
|
374 | if $self->{parent}; |
|
|
375 | |
|
|
376 | ($::WIDTH, $::HEIGHT) |
|
|
377 | } |
|
|
378 | |
365 | sub size_request { |
379 | sub size_request { |
366 | require Carp; |
380 | require Carp; |
367 | Carp::confess "size_request is abstract"; |
381 | Carp::confess "size_request is abstract"; |
368 | } |
382 | } |
369 | |
383 | |
… | |
… | |
375 | my ($self, $x, $y, $w, $h) = @_; |
389 | my ($self, $x, $y, $w, $h) = @_; |
376 | |
390 | |
377 | if ($self->{aspect}) { |
391 | if ($self->{aspect}) { |
378 | my ($ow, $oh) = ($w, $h); |
392 | my ($ow, $oh) = ($w, $h); |
379 | |
393 | |
380 | $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; |
394 | $w = List::Util::min $w, DC::ceil $h * $self->{aspect}; |
381 | $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; |
395 | $h = List::Util::min $h, DC::ceil $w / $self->{aspect}; |
382 | |
396 | |
383 | # use alignment to adjust x, y |
397 | # use alignment to adjust x, y |
384 | |
398 | |
385 | $x += int 0.5 * ($ow - $w); |
399 | $x += int 0.5 * ($ow - $w); |
386 | $y += int 0.5 * ($oh - $h); |
400 | $y += int 0.5 * ($oh - $h); |
… | |
… | |
427 | |
441 | |
428 | return if $self->{tooltip} eq $tooltip; |
442 | return if $self->{tooltip} eq $tooltip; |
429 | |
443 | |
430 | $self->{tooltip} = $tooltip; |
444 | $self->{tooltip} = $tooltip; |
431 | |
445 | |
432 | if ($CFPlus::UI::TOOLTIP->{owner} == $self) { |
446 | if ($DC::UI::TOOLTIP->{owner} == $self) { |
433 | delete $CFPlus::UI::TOOLTIP->{owner}; |
447 | delete $DC::UI::TOOLTIP->{owner}; |
434 | $CFPlus::UI::TOOLTIP_WATCHER->cb->(); |
448 | $DC::UI::TOOLTIP_WATCHER->invoke; |
435 | } |
449 | } |
436 | } |
450 | } |
437 | |
451 | |
438 | # translate global coordinates to local coordinate system |
452 | # translate global coordinates to local coordinate system |
439 | sub coord2local { |
453 | sub coord2local { |
440 | my ($self, $x, $y) = @_; |
454 | my ($self, $x, $y) = @_; |
441 | |
455 | |
442 | Carp::confess unless $self->{parent};#d# |
456 | return (undef, undef) unless $self->{parent}; |
443 | |
457 | |
444 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
458 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
445 | } |
459 | } |
446 | |
460 | |
447 | # translate local coordinates to global coordinate system |
461 | # translate local coordinates to global coordinate system |
448 | sub coord2global { |
462 | sub coord2global { |
449 | my ($self, $x, $y) = @_; |
463 | my ($self, $x, $y) = @_; |
450 | |
464 | |
451 | Carp::confess unless $self->{parent};#d# |
465 | return (undef, undef) unless $self->{parent}; |
452 | |
466 | |
453 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
467 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
454 | } |
468 | } |
455 | |
469 | |
456 | sub invoke_focus_in { |
470 | sub invoke_focus_in { |
… | |
… | |
505 | sub connect { |
519 | sub connect { |
506 | my ($self, $signal, $cb) = @_; |
520 | my ($self, $signal, $cb) = @_; |
507 | |
521 | |
508 | push @{ $self->{signal_cb}{$signal} }, $cb; |
522 | push @{ $self->{signal_cb}{$signal} }, $cb; |
509 | |
523 | |
510 | defined wantarray and CFPlus::guard { |
524 | defined wantarray and DC::guard { |
511 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
525 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
512 | @{ $self->{signal_cb}{$signal} }; |
526 | @{ $self->{signal_cb}{$signal} }; |
513 | } |
527 | } |
514 | } |
528 | } |
515 | |
529 | |
… | |
… | |
547 | |
561 | |
548 | # parent |
562 | # parent |
549 | $self->{parent} && $self->{parent}->emit ($signal, @args) |
563 | $self->{parent} && $self->{parent}->emit ($signal, @args) |
550 | } |
564 | } |
551 | |
565 | |
552 | sub find_widget { |
566 | #sub find_widget { |
553 | my ($self, $x, $y) = @_; |
567 | # in .xs |
554 | |
|
|
555 | return () unless $self->{can_events}; |
|
|
556 | |
|
|
557 | return $self |
|
|
558 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
559 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
|
|
560 | |
|
|
561 | () |
|
|
562 | } |
|
|
563 | |
568 | |
564 | sub set_parent { |
569 | sub set_parent { |
565 | my ($self, $parent) = @_; |
570 | my ($self, $parent) = @_; |
566 | |
571 | |
567 | CFPlus::weaken ($self->{parent} = $parent); |
572 | DC::weaken ($self->{parent} = $parent); |
568 | $self->set_visible if $parent->{visible}; |
573 | $self->set_visible if $parent->{visible}; |
569 | } |
574 | } |
570 | |
575 | |
571 | sub realloc { |
576 | sub realloc { |
572 | my ($self) = @_; |
577 | my ($self) = @_; |
… | |
… | |
598 | |
603 | |
599 | # using global variables seems a bit hacky, but passing through all drawing |
604 | # using global variables seems a bit hacky, but passing through all drawing |
600 | # functions seems pointless. |
605 | # functions seems pointless. |
601 | our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn |
606 | our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn |
602 | |
607 | |
603 | sub draw { |
608 | #sub draw { |
604 | my ($self) = @_; |
609 | #CFPlus.xs |
605 | |
|
|
606 | return unless $self->{h} && $self->{w}; |
|
|
607 | |
|
|
608 | # update screen rectangle |
|
|
609 | local $draw_x = $draw_x + $self->{x}; |
|
|
610 | local $draw_y = $draw_y + $self->{y}; |
|
|
611 | |
|
|
612 | # skip widgets that are entirely outside the drawing area |
|
|
613 | return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) |
|
|
614 | || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); |
|
|
615 | |
|
|
616 | glPushMatrix; |
|
|
617 | glTranslate $self->{x}, $self->{y}, 0; |
|
|
618 | |
|
|
619 | if ($self == $HOVER && $self->{can_hover}) { |
|
|
620 | glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2; |
|
|
621 | glEnable GL_BLEND; |
|
|
622 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
623 | glBegin GL_QUADS; |
|
|
624 | glVertex 0 , 0; |
|
|
625 | glVertex $self->{w}, 0; |
|
|
626 | glVertex $self->{w}, $self->{h}; |
|
|
627 | glVertex 0 , $self->{h}; |
|
|
628 | glEnd; |
|
|
629 | glDisable GL_BLEND; |
|
|
630 | } |
|
|
631 | |
|
|
632 | if ($ENV{CFPLUS_DEBUG} & 1) { |
|
|
633 | glPushMatrix; |
|
|
634 | glColor 1, 1, 0, 1; |
|
|
635 | glTranslate 0.375, 0.375; |
|
|
636 | glBegin GL_LINE_LOOP; |
|
|
637 | glVertex 0 , 0; |
|
|
638 | glVertex $self->{w} - 1, 0; |
|
|
639 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
640 | glVertex 0 , $self->{h} - 1; |
|
|
641 | glEnd; |
|
|
642 | glPopMatrix; |
|
|
643 | #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
|
|
644 | } |
|
|
645 | |
|
|
646 | $self->_draw; |
|
|
647 | glPopMatrix; |
|
|
648 | } |
|
|
649 | |
610 | |
650 | sub _draw { |
611 | sub _draw { |
651 | my ($self) = @_; |
612 | my ($self) = @_; |
652 | |
613 | |
653 | warn "no draw defined for $self\n"; |
614 | warn "no draw defined for $self\n"; |
654 | } |
615 | } |
655 | |
616 | |
656 | my $cntx;#d# |
|
|
657 | sub DESTROY { |
617 | sub DESTROY { |
658 | my ($self) = @_; |
618 | my ($self) = @_; |
659 | |
619 | |
660 | return if CFPlus::in_destruct; |
620 | return if DC::in_destruct; |
661 | |
621 | |
|
|
622 | local $@; |
662 | eval { $self->destroy }; |
623 | eval { $self->destroy }; |
663 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
624 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
664 | |
625 | |
665 | delete $WIDGET{$self+0}; |
626 | delete $WIDGET{$self+0}; |
666 | } |
627 | } |
667 | |
628 | |
668 | ############################################################################# |
629 | ############################################################################# |
669 | |
630 | |
670 | package CFPlus::UI::DrawBG; |
631 | package DC::UI::DrawBG; |
671 | |
632 | |
672 | our @ISA = CFPlus::UI::Base::; |
633 | our @ISA = DC::UI::Base::; |
673 | |
634 | |
674 | use strict; |
635 | use strict; |
675 | use CFPlus::OpenGL; |
636 | use DC::OpenGL; |
676 | |
637 | |
677 | sub new { |
638 | sub new { |
678 | my $class = shift; |
639 | my $class = shift; |
679 | |
|
|
680 | # range [value, low, high, page] |
|
|
681 | |
640 | |
682 | $class->SUPER::new ( |
641 | $class->SUPER::new ( |
683 | #bg => [0, 0, 0, 0.2], |
642 | #bg => [0, 0, 0, 0.2], |
684 | #active_bg => [1, 1, 1, 0.5], |
643 | #active_bg => [1, 1, 1, 0.5], |
685 | @_ |
644 | @_ |
… | |
… | |
697 | my ($w, $h) = @$self{qw(w h)}; |
656 | my ($w, $h) = @$self{qw(w h)}; |
698 | |
657 | |
699 | glEnable GL_BLEND; |
658 | glEnable GL_BLEND; |
700 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
659 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
701 | glColor_premultiply @$color; |
660 | glColor_premultiply @$color; |
702 | |
|
|
703 | glBegin GL_QUADS; |
|
|
704 | glVertex 0 , 0; |
|
|
705 | glVertex 0 , $h; |
|
|
706 | glVertex $w, $h; |
661 | glRect 0, 0, $w, $h; |
707 | glVertex $w, 0; |
|
|
708 | glEnd; |
|
|
709 | |
|
|
710 | glDisable GL_BLEND; |
662 | glDisable GL_BLEND; |
711 | } |
663 | } |
712 | } |
664 | } |
713 | |
665 | |
714 | ############################################################################# |
666 | ############################################################################# |
715 | |
667 | |
716 | package CFPlus::UI::Empty; |
668 | package DC::UI::Empty; |
717 | |
669 | |
718 | our @ISA = CFPlus::UI::Base::; |
670 | our @ISA = DC::UI::Base::; |
719 | |
671 | |
720 | sub new { |
672 | sub new { |
721 | my ($class, %arg) = @_; |
673 | my ($class, %arg) = @_; |
722 | $class->SUPER::new (can_events => 0, %arg); |
674 | $class->SUPER::new (can_events => 0, %arg); |
723 | } |
675 | } |
… | |
… | |
730 | |
682 | |
731 | sub draw { } |
683 | sub draw { } |
732 | |
684 | |
733 | ############################################################################# |
685 | ############################################################################# |
734 | |
686 | |
735 | package CFPlus::UI::Container; |
687 | package DC::UI::Container; |
736 | |
688 | |
737 | our @ISA = CFPlus::UI::Base::; |
689 | our @ISA = DC::UI::Base::; |
738 | |
690 | |
739 | sub new { |
691 | sub new { |
740 | my ($class, %arg) = @_; |
692 | my ($class, %arg) = @_; |
741 | |
693 | |
742 | my $children = delete $arg{children}; |
694 | my $children = delete $arg{children}; |
… | |
… | |
746 | can_events => 0, |
698 | can_events => 0, |
747 | %arg, |
699 | %arg, |
748 | ); |
700 | ); |
749 | |
701 | |
750 | $self->add (@$children) |
702 | $self->add (@$children) |
751 | if $children; |
703 | if $children && @$children; |
752 | |
704 | |
753 | $self |
705 | $self |
754 | } |
706 | } |
755 | |
707 | |
756 | sub realloc { |
708 | sub realloc { |
… | |
… | |
765 | my ($self, @widgets) = @_; |
717 | my ($self, @widgets) = @_; |
766 | |
718 | |
767 | $_->set_parent ($self) |
719 | $_->set_parent ($self) |
768 | for @widgets; |
720 | for @widgets; |
769 | |
721 | |
|
|
722 | # TODO: only do this in widgets that need it, e.g. root, fixed |
770 | use sort 'stable'; |
723 | use sort 'stable'; |
771 | |
724 | |
772 | $self->{children} = [ |
725 | $self->{children} = [ |
773 | sort { $a->{z} <=> $b->{z} } |
726 | sort { $a->{z} <=> $b->{z} } |
774 | @{$self->{children}}, @widgets |
727 | @{$self->{children}}, @widgets |
775 | ]; |
728 | ]; |
776 | |
729 | |
777 | $self->realloc; |
730 | $self->realloc; |
|
|
731 | |
|
|
732 | $self->emit (c_add => \@widgets); |
|
|
733 | |
|
|
734 | map $_+0, @widgets |
778 | } |
735 | } |
779 | |
736 | |
780 | sub children { |
737 | sub children { |
781 | @{ $_[0]{children} } |
738 | @{ $_[0]{children} } |
782 | } |
739 | } |
783 | |
740 | |
784 | sub remove { |
741 | sub remove { |
785 | my ($self, $child) = @_; |
742 | my ($self, @widgets) = @_; |
786 | |
743 | |
|
|
744 | $self->emit (c_remove => \@widgets); |
|
|
745 | |
|
|
746 | for my $child (@widgets) { |
787 | delete $child->{parent}; |
747 | delete $child->{parent}; |
788 | $child->hide; |
748 | $child->hide; |
789 | |
|
|
790 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
749 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
|
|
750 | } |
791 | |
751 | |
792 | $self->realloc; |
752 | $self->realloc; |
793 | } |
753 | } |
794 | |
754 | |
795 | sub clear { |
755 | sub clear { |
796 | my ($self) = @_; |
756 | my ($self) = @_; |
797 | |
757 | |
798 | my $children = delete $self->{children}; |
758 | my $children = $self->{children}; |
799 | $self->{children} = []; |
759 | $self->{children} = []; |
800 | |
760 | |
801 | for (@$children) { |
761 | for (@$children) { |
802 | delete $_->{parent}; |
762 | delete $_->{parent}; |
803 | $_->hide; |
763 | $_->hide; |
… | |
… | |
823 | } |
783 | } |
824 | |
784 | |
825 | sub _draw { |
785 | sub _draw { |
826 | my ($self) = @_; |
786 | my ($self) = @_; |
827 | |
787 | |
828 | $_->draw for @{$self->{children}}; |
788 | $_->draw for $self->visible_children; |
829 | } |
789 | } |
830 | |
790 | |
831 | ############################################################################# |
791 | ############################################################################# |
832 | |
792 | |
833 | package CFPlus::UI::Bin; |
793 | package DC::UI::Bin; |
834 | |
794 | |
835 | our @ISA = CFPlus::UI::Container::; |
795 | our @ISA = DC::UI::Container::; |
836 | |
796 | |
837 | sub new { |
797 | sub new { |
838 | my ($class, %arg) = @_; |
798 | my ($class, %arg) = @_; |
839 | |
799 | |
840 | my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; |
800 | my $child = (delete $arg{child}) || new DC::UI::Empty::; |
841 | |
801 | |
842 | $class->SUPER::new (children => [$child], %arg) |
802 | $class->SUPER::new (children => [$child], %arg) |
843 | } |
803 | } |
844 | |
804 | |
845 | sub add { |
805 | sub add { |
846 | my ($self, $child) = @_; |
806 | my ($self, $child) = @_; |
847 | |
807 | |
848 | $self->SUPER::remove ($_) for @{ $self->{children} }; |
808 | $self->clear; |
849 | $self->SUPER::add ($child); |
809 | $self->SUPER::add ($child); |
850 | } |
810 | } |
851 | |
811 | |
852 | sub remove { |
812 | sub remove { |
853 | my ($self, $widget) = @_; |
813 | my ($self, $widget) = @_; |
854 | |
814 | |
855 | $self->SUPER::remove ($widget); |
815 | $self->SUPER::remove ($widget); |
856 | |
816 | |
857 | $self->{children} = [new CFPlus::UI::Empty] |
817 | $self->{children} = [new DC::UI::Empty] |
858 | unless @{$self->{children}}; |
818 | unless @{$self->{children}}; |
859 | } |
819 | } |
860 | |
820 | |
861 | sub child { $_[0]->{children}[0] } |
821 | sub child { $_[0]->{children}[0] } |
862 | |
822 | |
… | |
… | |
871 | |
831 | |
872 | 1 |
832 | 1 |
873 | } |
833 | } |
874 | |
834 | |
875 | ############################################################################# |
835 | ############################################################################# |
876 | |
|
|
877 | # back-buffered drawing area |
836 | # back-buffered drawing area |
878 | |
837 | |
879 | package CFPlus::UI::Window; |
838 | package DC::UI::Window; |
880 | |
839 | |
881 | our @ISA = CFPlus::UI::Bin::; |
840 | our @ISA = DC::UI::Bin::; |
882 | |
841 | |
883 | use CFPlus::OpenGL; |
842 | use DC::OpenGL; |
884 | |
843 | |
885 | sub new { |
844 | sub new { |
886 | my ($class, %arg) = @_; |
845 | my ($class, %arg) = @_; |
887 | |
846 | |
888 | my $self = $class->SUPER::new (%arg); |
847 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
910 | } |
869 | } |
911 | |
870 | |
912 | sub render_child { |
871 | sub render_child { |
913 | my ($self) = @_; |
872 | my ($self) = @_; |
914 | |
873 | |
915 | $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { |
874 | $self->{texture} = new_from_opengl DC::Texture $self->{w}, $self->{h}, sub { |
916 | glClearColor 0, 0, 0, 0; |
875 | glClearColor 0, 0, 0, 0; |
917 | glClear GL_COLOR_BUFFER_BIT; |
876 | glClear GL_COLOR_BUFFER_BIT; |
918 | |
877 | |
919 | { |
878 | { |
920 | package CFPlus::UI::Base; |
879 | package DC::UI::Base; |
921 | |
880 | |
922 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
881 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
923 | (0, 0, $self->{w}, $self->{h}); |
882 | (0, 0, $self->{w}, $self->{h}); |
924 | |
883 | |
925 | $self->_render; |
884 | $self->_render; |
… | |
… | |
942 | glDisable GL_TEXTURE_2D; |
901 | glDisable GL_TEXTURE_2D; |
943 | } |
902 | } |
944 | |
903 | |
945 | ############################################################################# |
904 | ############################################################################# |
946 | |
905 | |
947 | package CFPlus::UI::ViewPort; |
906 | package DC::UI::ViewPort; |
948 | |
907 | |
949 | use List::Util qw(min max); |
908 | use List::Util qw(min max); |
950 | |
909 | |
951 | our @ISA = CFPlus::UI::Window::; |
910 | our @ISA = DC::UI::Window::; |
952 | |
911 | |
953 | sub new { |
912 | sub new { |
954 | my $class = shift; |
913 | my $class = shift; |
955 | |
914 | |
956 | $class->SUPER::new ( |
915 | $class->SUPER::new ( |
… | |
… | |
963 | sub size_request { |
922 | sub size_request { |
964 | my ($self) = @_; |
923 | my ($self) = @_; |
965 | |
924 | |
966 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
925 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
967 | |
926 | |
968 | $w = 10 if $self->{scroll_x}; |
927 | $w = 1 if $self->{scroll_x}; |
969 | $h = 10 if $self->{scroll_y}; |
928 | $h = 1 if $self->{scroll_y}; |
970 | |
929 | |
971 | ($w, $h) |
930 | ($w, $h) |
972 | } |
931 | } |
973 | |
932 | |
974 | sub invoke_size_allocate { |
933 | sub invoke_size_allocate { |
… | |
… | |
998 | $self->emit (changed => $x, $y); |
957 | $self->emit (changed => $x, $y); |
999 | $self->update; |
958 | $self->update; |
1000 | } |
959 | } |
1001 | } |
960 | } |
1002 | |
961 | |
|
|
962 | sub set_center { |
|
|
963 | my ($self, $x, $y) = @_; |
|
|
964 | |
|
|
965 | $self->set_offset ($x - $self->{w} * .5, $y - $self->{h} * .5); |
|
|
966 | } |
|
|
967 | |
|
|
968 | sub make_visible { |
|
|
969 | my ($self, $x, $y, $border) = @_; |
|
|
970 | |
|
|
971 | if ( $x < $self->{view_x} + $self->{w} * $border |
|
|
972 | || $x > $self->{view_x} + $self->{w} * (1 - $border) |
|
|
973 | || $y < $self->{view_y} + $self->{h} * $border |
|
|
974 | || $y > $self->{view_y} + $self->{h} * (1 - $border) |
|
|
975 | ) { |
|
|
976 | $self->set_center ($x, $y); |
|
|
977 | } |
|
|
978 | } |
|
|
979 | |
1003 | # hmm, this does not work for topleft of $self... but we should not ask for that |
980 | # hmm, this does not work for topleft of $self... but we should not ask for that |
1004 | sub coord2local { |
981 | sub coord2local { |
1005 | my ($self, $x, $y) = @_; |
982 | my ($self, $x, $y) = @_; |
1006 | |
983 | |
1007 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
984 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
… | |
… | |
1020 | my ($self, $x, $y) = @_; |
997 | my ($self, $x, $y) = @_; |
1021 | |
998 | |
1022 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
999 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
1023 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
1000 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
1024 | ) { |
1001 | ) { |
1025 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
1002 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
1026 | } else { |
1003 | } else { |
1027 | $self->CFPlus::UI::Base::find_widget ($x, $y) |
1004 | $self->DC::UI::Base::find_widget ($x, $y) |
1028 | } |
1005 | } |
1029 | } |
1006 | } |
1030 | |
1007 | |
1031 | sub _render { |
1008 | sub _render { |
1032 | my ($self) = @_; |
1009 | my ($self) = @_; |
1033 | |
1010 | |
1034 | local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; |
1011 | local $DC::UI::Base::draw_x = $DC::UI::Base::draw_x - $self->{view_x}; |
1035 | local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; |
1012 | local $DC::UI::Base::draw_y = $DC::UI::Base::draw_y - $self->{view_y}; |
1036 | |
1013 | |
1037 | CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1014 | DC::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1038 | |
1015 | |
1039 | $self->SUPER::_render; |
1016 | $self->SUPER::_render; |
1040 | } |
1017 | } |
1041 | |
1018 | |
1042 | ############################################################################# |
1019 | ############################################################################# |
1043 | |
1020 | |
1044 | package CFPlus::UI::ScrolledWindow; |
1021 | package DC::UI::ScrolledWindow; |
1045 | |
1022 | |
1046 | our @ISA = CFPlus::UI::Table::; |
1023 | our @ISA = DC::UI::Table::; |
1047 | |
1024 | |
1048 | sub new { |
1025 | sub new { |
1049 | my ($class, %arg) = @_; |
1026 | my ($class, %arg) = @_; |
1050 | |
1027 | |
1051 | my $child = delete $arg{child}; |
1028 | my $child = delete $arg{child}; |
1052 | |
1029 | |
1053 | my $self; |
1030 | my $self; |
1054 | |
1031 | |
1055 | my $hslider = new CFPlus::UI::Slider |
1032 | my $hslider = new DC::UI::Slider |
|
|
1033 | c_col => 0, |
|
|
1034 | c_row => 1, |
1056 | vertical => 0, |
1035 | vertical => 0, |
1057 | range => [0, 0, 1, 0.01], # HACK fix |
1036 | range => [0, 0, 1, 0.01], # HACK fix |
1058 | on_changed => sub { |
1037 | on_changed => sub { |
1059 | $self->{hpos} = $_[1]; |
1038 | $self->{hpos} = $_[1]; |
1060 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1039 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1061 | }, |
1040 | }, |
1062 | ; |
1041 | ; |
1063 | |
1042 | |
1064 | my $vslider = new CFPlus::UI::Slider |
1043 | my $vslider = new DC::UI::Slider |
|
|
1044 | c_col => 1, |
|
|
1045 | c_row => 0, |
1065 | vertical => 1, |
1046 | vertical => 1, |
1066 | range => [0, 0, 1, 0.01], # HACK fix |
1047 | range => [0, 0, 1, 0.01], # HACK fix |
1067 | on_changed => sub { |
1048 | on_changed => sub { |
1068 | $self->{vpos} = $_[1]; |
1049 | $self->{vpos} = $_[1]; |
1069 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1050 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
… | |
… | |
1079 | col_expand => [1, 0], |
1060 | col_expand => [1, 0], |
1080 | row_expand => [1, 0], |
1061 | row_expand => [1, 0], |
1081 | %arg, |
1062 | %arg, |
1082 | ); |
1063 | ); |
1083 | |
1064 | |
1084 | $self->{vp} = new CFPlus::UI::ViewPort |
1065 | $self->{vp} = new DC::UI::ViewPort |
|
|
1066 | c_col => 0, |
|
|
1067 | c_row => 0, |
1085 | expand => 1, |
1068 | expand => 1, |
1086 | scroll_x => $self->{scroll_x}, |
1069 | scroll_x => $self->{scroll_x}, |
1087 | scroll_y => $self->{scroll_y}, |
1070 | scroll_y => $self->{scroll_y}, |
1088 | on_changed => sub { |
1071 | on_changed => sub { |
1089 | my ($vp, $x, $y) = @_; |
1072 | my ($vp, $x, $y) = @_; |
… | |
… | |
1091 | $vp->{parent}{hslider}->set_value ($x); |
1074 | $vp->{parent}{hslider}->set_value ($x); |
1092 | $vp->{parent}{vslider}->set_value ($y); |
1075 | $vp->{parent}{vslider}->set_value ($y); |
1093 | |
1076 | |
1094 | 0 |
1077 | 0 |
1095 | }, |
1078 | }, |
|
|
1079 | on_size_allocate => sub { |
|
|
1080 | my ($vp, $w, $h) = @_; |
|
|
1081 | $vp->{parent}->update_slider; |
|
|
1082 | 0 |
|
|
1083 | }, |
1096 | ; |
1084 | ; |
1097 | |
1085 | |
1098 | $self->SUPER::add (0, 0, $self->{vp}); |
1086 | $self->SUPER::add ($self->{vp}); |
1099 | |
1087 | |
1100 | $self->add ($child) if $child; |
1088 | $self->add ($child) if $child; |
1101 | |
1089 | |
1102 | $self |
1090 | $self |
1103 | } |
1091 | } |
1104 | |
|
|
1105 | #TODO# update range on size_allocate depending on child |
|
|
1106 | |
1092 | |
1107 | sub add { |
1093 | sub add { |
1108 | my ($self, $widget) = @_; |
1094 | my ($self, $widget) = @_; |
1109 | |
1095 | |
1110 | $self->{vp}->add ($self->{child} = $widget); |
1096 | $self->{vp}->add ($self->{child} = $widget); |
1111 | } |
1097 | } |
1112 | |
1098 | |
|
|
1099 | sub set_offset { shift->{vp}->set_offset (@_) } |
|
|
1100 | sub set_center { shift->{vp}->set_center (@_) } |
|
|
1101 | sub make_visible { shift->{vp}->make_visible (@_) } |
|
|
1102 | |
1113 | sub update_slider { |
1103 | sub update_slider { |
1114 | my ($self) = @_; |
1104 | my ($self) = @_; |
1115 | |
1105 | |
1116 | my $child = ($self->{vp} or return)->child; |
1106 | my $child = ($self->{vp} or return)->child; |
1117 | |
1107 | |
|
|
1108 | if ($self->{scroll_x}) { |
1118 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1109 | my ($w1, $w2) = ($child->{req_w}, $self->{vp}{w}); |
1119 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
1110 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
1120 | |
1111 | |
1121 | my $visible = $w1 > $w2; |
1112 | my $visible = $w1 > $w2; |
1122 | if ($visible != $self->{hslider}{visible}) { |
1113 | if ($visible != $self->{hslider_visible}) { |
|
|
1114 | $self->{hslider_visible} = $visible; |
1123 | $visible ? $self->SUPER::add (0, 1, $self->{hslider}) |
1115 | $visible ? $self->SUPER::add ($self->{hslider}) |
1124 | : $self->{hslider}->hide; |
1116 | : $self->SUPER::remove ($self->{hslider}); |
|
|
1117 | } |
1125 | } |
1118 | } |
1126 | |
1119 | |
|
|
1120 | if ($self->{scroll_y}) { |
1127 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
1121 | my ($h1, $h2) = ($child->{req_h}, $self->{vp}{h}); |
1128 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
1122 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
1129 | |
1123 | |
1130 | my $visible = $h1 > $h2; |
1124 | my $visible = $h1 > $h2; |
1131 | if ($visible != $self->{vslider}{visible}) { |
1125 | if ($visible != $self->{vslider_visible}) { |
|
|
1126 | $self->{vslider_visible} = $visible; |
1132 | $visible ? $self->SUPER::add (1, 0, $self->{vslider}) |
1127 | $visible ? $self->SUPER::add ($self->{vslider}) |
1133 | : $self->{vslider}->hide; |
1128 | : $self->SUPER::remove ($self->{vslider}); |
|
|
1129 | } |
|
|
1130 | } |
|
|
1131 | } |
|
|
1132 | |
|
|
1133 | sub start_dragging { |
|
|
1134 | my ($self, $ev) = @_; |
|
|
1135 | |
|
|
1136 | $self->grab_focus; |
|
|
1137 | |
|
|
1138 | my $ox = $self->{vp}{view_x}; |
|
|
1139 | my $oy = $self->{vp}{view_y}; |
|
|
1140 | |
|
|
1141 | $self->{motion} = sub { |
|
|
1142 | my ($ev, $x, $y) = @_; |
|
|
1143 | |
|
|
1144 | $ox -= $ev->{xrel}; |
|
|
1145 | $oy -= $ev->{yrel}; |
|
|
1146 | |
|
|
1147 | $self->{vp}->set_offset ($ox, $oy); |
1134 | } |
1148 | }; |
1135 | } |
|
|
1136 | |
|
|
1137 | sub update { |
|
|
1138 | my ($self) = @_; |
|
|
1139 | |
|
|
1140 | $self->SUPER::update; |
|
|
1141 | $self->update_slider; |
|
|
1142 | } |
1149 | } |
1143 | |
1150 | |
1144 | sub invoke_mouse_wheel { |
1151 | sub invoke_mouse_wheel { |
1145 | my ($self, $ev) = @_; |
1152 | my ($self, $ev) = @_; |
1146 | |
1153 | |
1147 | return 0 unless $ev->{dy}; # only vertical movements for now |
|
|
1148 | |
|
|
1149 | $self->{vslider}->emit (mouse_wheel => $ev); |
1154 | $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible}; |
|
|
1155 | $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible}; |
1150 | |
1156 | |
1151 | 1 |
1157 | 1 |
1152 | } |
1158 | } |
1153 | |
1159 | |
1154 | sub invoke_button_down { |
1160 | sub invoke_button_down { |
1155 | my ($self, $ev, $x, $y) = @_; |
1161 | my ($self, $ev, $x, $y) = @_; |
1156 | |
1162 | |
1157 | if ($ev->{button} == 2) { |
1163 | if ($ev->{button} == 2) { |
1158 | $self->grab_focus; |
1164 | $self->start_dragging ($ev); |
1159 | |
|
|
1160 | my $ox = $self->{vp}{view_x} + $ev->{x}; |
|
|
1161 | my $oy = $self->{vp}{view_y} + $ev->{y}; |
|
|
1162 | |
|
|
1163 | $self->{motion} = sub { |
|
|
1164 | my ($ev, $x, $y) = @_; |
|
|
1165 | |
|
|
1166 | $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y}); |
|
|
1167 | $self->update; |
|
|
1168 | }; |
|
|
1169 | |
|
|
1170 | return 1; |
1165 | return 1; |
1171 | } |
1166 | } |
1172 | |
1167 | |
1173 | 0 |
1168 | 0 |
1174 | } |
1169 | } |
… | |
… | |
1201 | $self->SUPER::invoke_size_allocate ($w, $h) |
1196 | $self->SUPER::invoke_size_allocate ($w, $h) |
1202 | } |
1197 | } |
1203 | |
1198 | |
1204 | ############################################################################# |
1199 | ############################################################################# |
1205 | |
1200 | |
1206 | package CFPlus::UI::Frame; |
1201 | package DC::UI::Frame; |
1207 | |
1202 | |
1208 | our @ISA = CFPlus::UI::Bin::; |
1203 | our @ISA = DC::UI::Bin::; |
1209 | |
1204 | |
1210 | use CFPlus::OpenGL; |
1205 | use DC::OpenGL; |
1211 | |
1206 | |
1212 | sub new { |
1207 | sub new { |
1213 | my $class = shift; |
1208 | my $class = shift; |
1214 | |
1209 | |
1215 | $class->SUPER::new ( |
1210 | $class->SUPER::new ( |
… | |
… | |
1225 | my ($w, $h) = @$self{qw(w h)}; |
1220 | my ($w, $h) = @$self{qw(w h)}; |
1226 | |
1221 | |
1227 | glEnable GL_BLEND; |
1222 | glEnable GL_BLEND; |
1228 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1223 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1229 | glColor_premultiply @{ $self->{bg} }; |
1224 | glColor_premultiply @{ $self->{bg} }; |
1230 | |
|
|
1231 | glBegin GL_QUADS; |
|
|
1232 | glVertex 0 , 0; |
|
|
1233 | glVertex 0 , $h; |
|
|
1234 | glVertex $w, $h; |
1225 | glRect 0, 0, $w, $h; |
1235 | glVertex $w, 0; |
|
|
1236 | glEnd; |
|
|
1237 | |
|
|
1238 | glDisable GL_BLEND; |
1226 | glDisable GL_BLEND; |
1239 | } |
1227 | } |
1240 | |
1228 | |
1241 | $self->SUPER::_draw; |
1229 | $self->SUPER::_draw; |
1242 | } |
1230 | } |
1243 | |
1231 | |
1244 | ############################################################################# |
1232 | ############################################################################# |
1245 | |
1233 | |
1246 | package CFPlus::UI::FancyFrame; |
1234 | package DC::UI::FancyFrame; |
1247 | |
1235 | |
1248 | our @ISA = CFPlus::UI::Bin::; |
1236 | our @ISA = DC::UI::Bin::; |
1249 | |
1237 | |
1250 | use CFPlus::OpenGL; |
1238 | use DC::OpenGL; |
1251 | |
1239 | |
1252 | sub new { |
1240 | sub new { |
1253 | my ($class, %arg) = @_; |
1241 | my ($class, %arg) = @_; |
1254 | |
1242 | |
1255 | if ((exists $arg{label}) && !ref $arg{label}) { |
1243 | if ((exists $arg{label}) && !ref $arg{label}) { |
1256 | $arg{label} = new CFPlus::UI::Label |
1244 | $arg{label} = new DC::UI::Label |
1257 | align => 1, |
1245 | align => 1, |
1258 | valign => 0, |
1246 | valign => 0, |
1259 | text => $arg{label}, |
1247 | text => $arg{label}, |
1260 | fontsize => ($arg{border} || 0.8) * 0.75; |
1248 | fontsize => ($arg{border} || 0.8) * 0.75; |
1261 | } |
1249 | } |
… | |
… | |
1273 | |
1261 | |
1274 | sub add { |
1262 | sub add { |
1275 | my ($self, @widgets) = @_; |
1263 | my ($self, @widgets) = @_; |
1276 | |
1264 | |
1277 | $self->SUPER::add (@widgets); |
1265 | $self->SUPER::add (@widgets); |
1278 | $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; |
1266 | $self->DC::UI::Container::add ($self->{label}) if $self->{label}; |
1279 | } |
1267 | } |
1280 | |
1268 | |
1281 | sub border { |
1269 | sub border { |
1282 | int $_[0]{border} * $::FONTSIZE |
1270 | int $_[0]{border} * $::FONTSIZE |
1283 | } |
1271 | } |
… | |
… | |
1341 | } |
1329 | } |
1342 | } |
1330 | } |
1343 | |
1331 | |
1344 | ############################################################################# |
1332 | ############################################################################# |
1345 | |
1333 | |
1346 | package CFPlus::UI::Toplevel; |
1334 | package DC::UI::Toplevel; |
1347 | |
1335 | |
1348 | our @ISA = CFPlus::UI::Bin::; |
1336 | our @ISA = DC::UI::Bin::; |
1349 | |
1337 | |
1350 | use CFPlus::OpenGL; |
1338 | use DC::OpenGL; |
1351 | |
1339 | |
1352 | my $bg = |
1340 | my $bg = |
1353 | new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", |
1341 | new_from_file DC::Texture DC::find_rcfile "d1_bg.png", |
1354 | mipmap => 1, wrap => 1; |
1342 | mipmap => 1, wrap => 1; |
1355 | |
1343 | |
1356 | my @border = |
1344 | my @border = |
1357 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1345 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
1358 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1346 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
1347 | |
|
|
1348 | my @icon = |
|
|
1349 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
|
|
1350 | qw(x1_move.png x1_resize.png); |
1359 | |
1351 | |
1360 | sub new { |
1352 | sub new { |
1361 | my ($class, %arg) = @_; |
1353 | my ($class, %arg) = @_; |
1362 | |
1354 | |
1363 | my $self = $class->SUPER::new ( |
1355 | my $self = $class->SUPER::new ( |
… | |
… | |
1368 | min_w => 64, |
1360 | min_w => 64, |
1369 | min_h => 32, |
1361 | min_h => 32, |
1370 | %arg, |
1362 | %arg, |
1371 | ); |
1363 | ); |
1372 | |
1364 | |
1373 | $self->{title_widget} = new CFPlus::UI::Label |
1365 | $self->{title_widget} = new DC::UI::Label |
1374 | align => 0, |
1366 | align => 0, |
1375 | valign => 1, |
1367 | valign => 1, |
1376 | text => $self->{title}, |
1368 | text => $self->{title}, |
1377 | fontsize => $self->{border}, |
1369 | fontsize => $self->{border}, |
1378 | if exists $self->{title}; |
1370 | if exists $self->{title}; |
1379 | |
1371 | |
1380 | if ($self->{has_close_button}) { |
1372 | if ($self->{has_close_button}) { |
1381 | $self->{close_button} = |
1373 | $self->{close_button} = |
1382 | new CFPlus::UI::ImageButton |
1374 | new DC::UI::ImageButton |
1383 | path => 'x1_close.png', |
1375 | path => 'x1_close.png', |
1384 | on_activate => sub { $self->emit ("delete") }; |
1376 | on_activate => sub { $self->emit ("delete") }; |
1385 | |
1377 | |
1386 | $self->CFPlus::UI::Container::add ($self->{close_button}); |
1378 | $self->DC::UI::Container::add ($self->{close_button}); |
1387 | } |
1379 | } |
1388 | |
1380 | |
1389 | $self |
1381 | $self |
1390 | } |
1382 | } |
1391 | |
1383 | |
1392 | sub add { |
1384 | sub add { |
1393 | my ($self, @widgets) = @_; |
1385 | my ($self, @widgets) = @_; |
1394 | |
1386 | |
1395 | $self->SUPER::add (@widgets); |
1387 | $self->SUPER::add (@widgets); |
1396 | $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1388 | $self->DC::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1397 | $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1389 | $self->DC::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1398 | } |
1390 | } |
1399 | |
1391 | |
1400 | sub border { |
1392 | sub border { |
1401 | int $_[0]{border} * $::FONTSIZE |
1393 | int $_[0]{border} * $::FONTSIZE |
|
|
1394 | } |
|
|
1395 | |
|
|
1396 | sub get_max_wh { |
|
|
1397 | my ($self) = @_; |
|
|
1398 | |
|
|
1399 | return ($self->{w}, $self->{h}) |
|
|
1400 | if $self->{visible} && $self->{w}; |
|
|
1401 | |
|
|
1402 | $self->SUPER::get_max_wh |
1402 | } |
1403 | } |
1403 | |
1404 | |
1404 | sub size_request { |
1405 | sub size_request { |
1405 | my ($self) = @_; |
1406 | my ($self) = @_; |
1406 | |
1407 | |
… | |
… | |
1532 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1533 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1533 | |
1534 | |
1534 | my $border = $self->border; |
1535 | my $border = $self->border; |
1535 | |
1536 | |
1536 | glColor @{ $self->{border_bg} }; |
1537 | glColor @{ $self->{border_bg} }; |
1537 | $border[0]->draw_quad_alpha (0, 0, $w, $border); |
1538 | $border[0]->draw_quad_alpha ( 0, 0, $w, $border); |
1538 | $border[1]->draw_quad_alpha (0, $border, $border, $ch); |
1539 | $border[1]->draw_quad_alpha ( 0, $border, $border, $ch); |
1539 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1540 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1540 | $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); |
1541 | $border[3]->draw_quad_alpha ( 0, $h - $border, $w, $border); |
|
|
1542 | |
|
|
1543 | # move |
|
|
1544 | my $w2 = ($w - $border) * .5; |
|
|
1545 | my $h2 = ($h - $border) * .5; |
|
|
1546 | $icon[0]->draw_quad_alpha ( 0, $h2, $border, $border); |
|
|
1547 | $icon[0]->draw_quad_alpha ($w - $border, $h2, $border, $border); |
|
|
1548 | $icon[0]->draw_quad_alpha ($w2 , $h - $border, $border, $border); |
|
|
1549 | |
|
|
1550 | # resize |
|
|
1551 | $icon[1]->draw_quad_alpha ( 0, 0, $border, $border); |
|
|
1552 | $icon[1]->draw_quad_alpha ($w - $border, 0, $border, $border) |
|
|
1553 | unless $self->{has_close_button}; |
|
|
1554 | $icon[1]->draw_quad_alpha ( 0, $h - $border, $border, $border); |
|
|
1555 | $icon[1]->draw_quad_alpha ($w - $border, $h - $border, $border, $border); |
1541 | |
1556 | |
1542 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1557 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1543 | glColor @{ $self->{bg} }; |
1558 | glColor @{ $self->{bg} }; |
1544 | |
1559 | |
1545 | # TODO: repeat texture not scale |
1560 | # TODO: repeat texture not scale |
… | |
… | |
1564 | if $self->{close_button}; |
1579 | if $self->{close_button}; |
1565 | } |
1580 | } |
1566 | |
1581 | |
1567 | ############################################################################# |
1582 | ############################################################################# |
1568 | |
1583 | |
1569 | package CFPlus::UI::Table; |
1584 | package DC::UI::Table; |
1570 | |
1585 | |
1571 | our @ISA = CFPlus::UI::Base::; |
1586 | our @ISA = DC::UI::Container::; |
1572 | |
1587 | |
1573 | use List::Util qw(max sum); |
1588 | use List::Util qw(max sum); |
1574 | |
1589 | |
1575 | use CFPlus::OpenGL; |
1590 | use DC::OpenGL; |
1576 | |
1591 | |
1577 | sub new { |
1592 | sub new { |
1578 | my $class = shift; |
1593 | my $class = shift; |
1579 | |
1594 | |
1580 | $class->SUPER::new ( |
1595 | $class->SUPER::new ( |
1581 | children => [], |
|
|
1582 | col_expand => [], |
1596 | col_expand => [], |
1583 | row_expand => [], |
1597 | row_expand => [], |
1584 | @_, |
1598 | @_, |
1585 | ) |
1599 | ) |
1586 | } |
1600 | } |
1587 | |
1601 | |
1588 | sub children { |
|
|
1589 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1590 | } |
|
|
1591 | |
|
|
1592 | # TODO: store row/col info in child widget and use standard add/del |
|
|
1593 | sub add { |
1602 | sub add { |
|
|
1603 | my ($self, @widgets) = @_; |
|
|
1604 | |
|
|
1605 | for my $child (@widgets) { |
|
|
1606 | $child->{c_rowspan} ||= 1; |
|
|
1607 | $child->{c_colspan} ||= 1; |
|
|
1608 | } |
|
|
1609 | |
|
|
1610 | $self->SUPER::add (@widgets); |
|
|
1611 | } |
|
|
1612 | |
|
|
1613 | sub add_at { |
1594 | my ($self) = shift; |
1614 | my $self = shift; |
|
|
1615 | |
|
|
1616 | my @widgets; |
1595 | |
1617 | |
1596 | while (@_) { |
1618 | while (@_) { |
1597 | my ($x, $y, $child) = splice @_, 0, 3, (); |
1619 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1598 | $child->set_parent ($self); |
|
|
1599 | $self->{children}[$y][$x] = $child; |
|
|
1600 | } |
|
|
1601 | |
1620 | |
1602 | $self->{force_realloc} = 1; |
1621 | $child->{c_row} = $row; |
1603 | $self->{force_size_alloc} = 1; |
1622 | $child->{c_col} = $col; |
1604 | $self->realloc; |
|
|
1605 | } |
|
|
1606 | |
1623 | |
1607 | sub remove { |
1624 | push @widgets, $child; |
|
|
1625 | } |
|
|
1626 | |
|
|
1627 | $self->add (@widgets); |
|
|
1628 | } |
|
|
1629 | |
|
|
1630 | sub get_wh { |
1608 | my ($self, $child) = @_; |
1631 | my ($self) = @_; |
1609 | |
1632 | |
1610 | for (@{ $self->{children} }) { |
1633 | my (@w, @h); |
1611 | for (@{ $_ || [] }) { |
1634 | |
1612 | $_ = undef if $_ == $child; |
1635 | my @children = $self->children; |
|
|
1636 | |
|
|
1637 | # first pass, columns |
|
|
1638 | for my $widget (sort { $a->{c_colspan} <=> $b->{c_colspan} } @children) { |
|
|
1639 | my ($c, $w, $cs) = @$widget{qw(c_col req_w c_colspan)}; |
|
|
1640 | |
|
|
1641 | my $sw = sum @w[$c .. $c + $cs - 1]; |
|
|
1642 | |
|
|
1643 | if ($w > $sw) { |
|
|
1644 | $_ += ($w - $sw) / ($_ ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1613 | } |
1645 | } |
1614 | } |
1646 | } |
1615 | } |
|
|
1616 | |
1647 | |
1617 | # TODO: move to container class maybe? send children a signal on removal? |
1648 | # second pass, rows |
1618 | sub clear { |
1649 | for my $widget (sort { $a->{c_rowspan} <=> $b->{c_rowspan} } @children) { |
1619 | my ($self) = @_; |
|
|
1620 | |
|
|
1621 | my @children = $self->children; |
|
|
1622 | delete $self->{children}; |
|
|
1623 | |
|
|
1624 | for (@children) { |
|
|
1625 | delete $_->{parent}; |
|
|
1626 | $_->hide; |
|
|
1627 | } |
|
|
1628 | |
|
|
1629 | $self->realloc; |
|
|
1630 | } |
|
|
1631 | |
|
|
1632 | sub get_wh { |
|
|
1633 | my ($self) = @_; |
|
|
1634 | |
|
|
1635 | my (@w, @h); |
|
|
1636 | |
|
|
1637 | for my $y (0 .. $#{$self->{children}}) { |
|
|
1638 | my $row = $self->{children}[$y] |
|
|
1639 | or next; |
|
|
1640 | |
|
|
1641 | for my $x (0 .. $#$row) { |
|
|
1642 | my $widget = $row->[$x] |
|
|
1643 | or next; |
|
|
1644 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
1650 | my ($r, $h, $rs) = @$widget{qw(c_row req_h c_rowspan)}; |
1645 | |
1651 | |
1646 | $w[$x] = max $w[$x], $w; |
1652 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1647 | $h[$y] = max $h[$y], $h; |
1653 | |
|
|
1654 | if ($h > $sh) { |
|
|
1655 | $_ += ($h - $sh) / ($_ ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1648 | } |
1656 | } |
1649 | } |
1657 | } |
1650 | |
1658 | |
1651 | (\@w, \@h) |
1659 | (\@w, \@h) |
1652 | } |
1660 | } |
… | |
… | |
1668 | my ($ws, $hs) = $self->get_wh; |
1676 | my ($ws, $hs) = $self->get_wh; |
1669 | |
1677 | |
1670 | my $req_w = (sum @$ws) || 1; |
1678 | my $req_w = (sum @$ws) || 1; |
1671 | my $req_h = (sum @$hs) || 1; |
1679 | my $req_h = (sum @$hs) || 1; |
1672 | |
1680 | |
1673 | # TODO: nicer code |
1681 | # now linearly scale the rows/columns to the allocated size |
1674 | my @col_expand = @{$self->{col_expand}}; |
1682 | my @col_expand = @{$self->{col_expand}}; |
1675 | @col_expand = (1) x @$ws unless @col_expand; |
1683 | @col_expand = (1) x @$ws unless @col_expand; |
1676 | my $col_expand = (sum @col_expand) || 1; |
1684 | my $col_expand = (sum @col_expand) || 1; |
1677 | |
1685 | |
1678 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1686 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1679 | |
1687 | |
1680 | CFPlus::UI::harmonize $ws; |
1688 | DC::UI::harmonize $ws; |
1681 | |
1689 | |
1682 | my @row_expand = @{$self->{row_expand}}; |
1690 | my @row_expand = @{$self->{row_expand}}; |
1683 | @row_expand = (1) x @$ws unless @row_expand; |
1691 | @row_expand = (1) x @$ws unless @row_expand; |
1684 | my $row_expand = (sum @row_expand) || 1; |
1692 | my $row_expand = (sum @row_expand) || 1; |
1685 | |
1693 | |
1686 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1694 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1687 | |
1695 | |
1688 | CFPlus::UI::harmonize $hs; |
1696 | DC::UI::harmonize $hs; |
1689 | |
1697 | |
1690 | my $y; |
1698 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
|
|
1699 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1691 | |
1700 | |
1692 | for my $r (0 .. $#{$self->{children}}) { |
1701 | for my $widget ($self->children) { |
1693 | my $row = $self->{children}[$r] |
1702 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(c_row c_col req_w req_h c_rowspan c_colspan)}; |
1694 | or next; |
|
|
1695 | |
1703 | |
1696 | my $x = 0; |
1704 | $widget->configure ( |
1697 | my $row_h = $hs->[$r]; |
1705 | $x[$c], $y[$r], |
|
|
1706 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1698 | |
1707 | ); |
1699 | for my $c (0 .. $#$row) { |
|
|
1700 | my $col_w = $ws->[$c]; |
|
|
1701 | |
|
|
1702 | if (my $widget = $row->[$c]) { |
|
|
1703 | $widget->configure ($x, $y, $col_w, $row_h); |
|
|
1704 | } |
|
|
1705 | |
|
|
1706 | $x += $col_w; |
|
|
1707 | } |
|
|
1708 | |
|
|
1709 | $y += $row_h; |
|
|
1710 | } |
1708 | } |
1711 | |
1709 | |
1712 | 1 |
1710 | 1 |
1713 | } |
1711 | } |
1714 | |
1712 | |
1715 | sub find_widget { |
|
|
1716 | my ($self, $x, $y) = @_; |
|
|
1717 | |
|
|
1718 | $x -= $self->{x}; |
|
|
1719 | $y -= $self->{y}; |
|
|
1720 | |
|
|
1721 | my $res; |
|
|
1722 | |
|
|
1723 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
1724 | $res = $_->find_widget ($x, $y) |
|
|
1725 | and return $res; |
|
|
1726 | } |
|
|
1727 | |
|
|
1728 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
1729 | } |
|
|
1730 | |
|
|
1731 | sub _draw { |
|
|
1732 | my ($self) = @_; |
|
|
1733 | |
|
|
1734 | for (grep $_, @{$self->{children}}) { |
|
|
1735 | $_->draw for grep $_, @$_; |
|
|
1736 | } |
|
|
1737 | } |
|
|
1738 | |
|
|
1739 | ############################################################################# |
1713 | ############################################################################# |
1740 | |
1714 | |
1741 | package CFPlus::UI::Fixed; |
1715 | package DC::UI::Fixed; |
1742 | |
1716 | |
1743 | use List::Util qw(min max); |
1717 | use List::Util qw(min max); |
1744 | |
1718 | |
1745 | our @ISA = CFPlus::UI::Container::; |
1719 | our @ISA = DC::UI::Container::; |
1746 | |
|
|
1747 | sub add { |
|
|
1748 | my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_; |
|
|
1749 | |
|
|
1750 | $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h]; |
|
|
1751 | $self->SUPER::add ($child); |
|
|
1752 | } |
|
|
1753 | |
1720 | |
1754 | sub _scale($$$) { |
1721 | sub _scale($$$) { |
1755 | my ($mode, $val, $max) = @_; |
1722 | my ($rel, $val, $max) = @_; |
1756 | |
1723 | |
1757 | $mode eq "abs" ? $val |
1724 | $rel ? $val * $max : $val |
1758 | : $mode eq "rel" ? $val * $max |
|
|
1759 | : 0 |
|
|
1760 | } |
1725 | } |
1761 | |
1726 | |
1762 | sub size_request { |
1727 | sub size_request { |
1763 | my ($self) = @_; |
1728 | my ($self) = @_; |
1764 | |
1729 | |
1765 | my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); |
1730 | my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); |
1766 | |
1731 | |
1767 | # determine overall size by querying abs widgets |
1732 | # determine overall size by querying abs widgets |
1768 | for my $child ($self->visible_children) { |
1733 | for my $child ($self->visible_children) { |
1769 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
1734 | unless ($child->{c_rel}) { |
|
|
1735 | my $x = $child->{c_x}; |
|
|
1736 | my $y = $child->{c_y}; |
1770 | |
1737 | |
1771 | if ($pos eq "abs") { |
|
|
1772 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1773 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1774 | |
|
|
1775 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
1738 | $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w}; |
1776 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
1739 | $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h}; |
1777 | } |
1740 | } |
1778 | } |
1741 | } |
1779 | |
1742 | |
1780 | my $W = $x2 - $x1; |
1743 | my $W = $x2 - $x1; |
1781 | my $H = $y2 - $y1; |
1744 | my $H = $y2 - $y1; |
1782 | |
1745 | |
1783 | # now layout remaining widgets |
1746 | # now layout remaining widgets |
1784 | for my $child ($self->visible_children) { |
1747 | for my $child ($self->visible_children) { |
1785 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
1748 | if ($child->{c_rel}) { |
|
|
1749 | my $x = _scale $child->{c_rel}, $child->{c_x}, $W; |
|
|
1750 | my $y = _scale $child->{c_rel}, $child->{c_y}, $H; |
1786 | |
1751 | |
1787 | if ($pos ne "abs") { |
|
|
1788 | $x = _scale $pos, $x, $W; |
|
|
1789 | $y = _scale $pos, $x, $H; |
|
|
1790 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1791 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1792 | |
|
|
1793 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
1752 | $x1 = min $x1, $x; $x2 = max $x2, $x + $child->{req_w}; |
1794 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
1753 | $y1 = min $y1, $y; $y2 = max $y2, $y + $child->{req_h}; |
1795 | } |
1754 | } |
1796 | } |
1755 | } |
1797 | |
1756 | |
1798 | my $W = $x2 - $x1; |
1757 | my $W = $x2 - $x1; |
1799 | my $H = $y2 - $y1; |
1758 | my $H = $y2 - $y1; |
… | |
… | |
1803 | |
1762 | |
1804 | sub invoke_size_allocate { |
1763 | sub invoke_size_allocate { |
1805 | my ($self, $W, $H) = @_; |
1764 | my ($self, $W, $H) = @_; |
1806 | |
1765 | |
1807 | for my $child ($self->visible_children) { |
1766 | for my $child ($self->visible_children) { |
1808 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
1767 | my $x = _scale $child->{c_rel}, $child->{c_x}, $W; |
|
|
1768 | my $y = _scale $child->{c_rel}, $child->{c_y}, $H; |
1809 | |
1769 | |
1810 | $x = _scale $pos, $x, $W; |
1770 | $x += $child->{c_halign} * $child->{req_w}; |
1811 | $y = _scale $pos, $x, $H; |
1771 | $y += $child->{c_valign} * $child->{req_h}; |
1812 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1813 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1814 | |
1772 | |
1815 | $child->configure ($x, $y, $w, $h); |
1773 | $child->configure (int $x, int $y, $child->{req_w}, $child->{req_h}); |
1816 | } |
1774 | } |
1817 | |
1775 | |
1818 | 1 |
1776 | 1 |
1819 | } |
1777 | } |
1820 | |
1778 | |
1821 | ############################################################################# |
1779 | ############################################################################# |
1822 | |
1780 | |
1823 | package CFPlus::UI::Box; |
1781 | package DC::UI::Box; |
1824 | |
1782 | |
1825 | our @ISA = CFPlus::UI::Container::; |
1783 | our @ISA = DC::UI::Container::; |
1826 | |
1784 | |
1827 | sub size_request { |
1785 | sub size_request { |
1828 | my ($self) = @_; |
1786 | my ($self) = @_; |
|
|
1787 | |
|
|
1788 | my @children = $self->visible_children; |
1829 | |
1789 | |
1830 | $self->{vertical} |
1790 | $self->{vertical} |
1831 | ? ( |
1791 | ? ( |
1832 | (List::Util::max map $_->{req_w}, @{$self->{children}}), |
1792 | (List::Util::max map $_->{req_w}, @children), |
1833 | (List::Util::sum map $_->{req_h}, @{$self->{children}}), |
1793 | (List::Util::sum map $_->{req_h}, @children), |
1834 | ) |
1794 | ) |
1835 | : ( |
1795 | : ( |
1836 | (List::Util::sum map $_->{req_w}, @{$self->{children}}), |
1796 | (List::Util::sum map $_->{req_w}, @children), |
1837 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1797 | (List::Util::max map $_->{req_h}, @children), |
1838 | ) |
1798 | ) |
1839 | } |
1799 | } |
1840 | |
1800 | |
1841 | sub invoke_size_allocate { |
1801 | sub invoke_size_allocate { |
1842 | my ($self, $w, $h) = @_; |
1802 | my ($self, $w, $h) = @_; |
… | |
… | |
1863 | $req[$_] += $space * $children[$_]{expand} |
1823 | $req[$_] += $space * $children[$_]{expand} |
1864 | for 0 .. $#children; |
1824 | for 0 .. $#children; |
1865 | } |
1825 | } |
1866 | } |
1826 | } |
1867 | |
1827 | |
1868 | CFPlus::UI::harmonize \@req; |
1828 | DC::UI::harmonize \@req; |
1869 | |
1829 | |
1870 | my $pos = 0; |
1830 | my $pos = 0; |
1871 | for (0 .. $#children) { |
1831 | for (0 .. $#children) { |
1872 | my $alloc = $req[$_]; |
1832 | my $alloc = $req[$_]; |
1873 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
1833 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
… | |
… | |
1878 | 1 |
1838 | 1 |
1879 | } |
1839 | } |
1880 | |
1840 | |
1881 | ############################################################################# |
1841 | ############################################################################# |
1882 | |
1842 | |
1883 | package CFPlus::UI::HBox; |
1843 | package DC::UI::HBox; |
1884 | |
1844 | |
1885 | our @ISA = CFPlus::UI::Box::; |
1845 | our @ISA = DC::UI::Box::; |
1886 | |
1846 | |
1887 | sub new { |
1847 | sub new { |
1888 | my $class = shift; |
1848 | my $class = shift; |
1889 | |
1849 | |
1890 | $class->SUPER::new ( |
1850 | $class->SUPER::new ( |
… | |
… | |
1893 | ) |
1853 | ) |
1894 | } |
1854 | } |
1895 | |
1855 | |
1896 | ############################################################################# |
1856 | ############################################################################# |
1897 | |
1857 | |
1898 | package CFPlus::UI::VBox; |
1858 | package DC::UI::VBox; |
1899 | |
1859 | |
1900 | our @ISA = CFPlus::UI::Box::; |
1860 | our @ISA = DC::UI::Box::; |
1901 | |
1861 | |
1902 | sub new { |
1862 | sub new { |
1903 | my $class = shift; |
1863 | my $class = shift; |
1904 | |
1864 | |
1905 | $class->SUPER::new ( |
1865 | $class->SUPER::new ( |
… | |
… | |
1908 | ) |
1868 | ) |
1909 | } |
1869 | } |
1910 | |
1870 | |
1911 | ############################################################################# |
1871 | ############################################################################# |
1912 | |
1872 | |
1913 | package CFPlus::UI::Label; |
1873 | package DC::UI::Label; |
1914 | |
1874 | |
1915 | our @ISA = CFPlus::UI::DrawBG::; |
1875 | our @ISA = DC::UI::DrawBG::; |
1916 | |
1876 | |
1917 | use CFPlus::OpenGL; |
1877 | use DC::OpenGL; |
1918 | |
1878 | |
1919 | sub new { |
1879 | sub new { |
1920 | my ($class, %arg) = @_; |
1880 | my ($class, %arg) = @_; |
1921 | |
1881 | |
1922 | my $self = $class->SUPER::new ( |
1882 | my $self = $class->SUPER::new ( |
… | |
… | |
1927 | #text => initial text |
1887 | #text => initial text |
1928 | #markup => initial narkup |
1888 | #markup => initial narkup |
1929 | #max_w => maximum pixel width |
1889 | #max_w => maximum pixel width |
1930 | #style => 0, # render flags |
1890 | #style => 0, # render flags |
1931 | ellipsise => 3, # end |
1891 | ellipsise => 3, # end |
1932 | layout => (new CFPlus::Layout), |
1892 | layout => (new DC::Layout), |
1933 | fontsize => 1, |
1893 | fontsize => 1, |
1934 | align => -1, |
1894 | align => -1, |
1935 | valign => -1, |
1895 | valign => -1, |
1936 | padding_x => 2, |
1896 | padding_x => 2, |
1937 | padding_y => 2, |
1897 | padding_y => 2, |
1938 | can_events => 0, |
1898 | can_events => 0, |
1939 | %arg |
1899 | %arg |
1940 | ); |
1900 | ); |
1941 | |
1901 | |
1942 | if (exists $self->{template}) { |
1902 | if (exists $self->{template}) { |
1943 | my $layout = new CFPlus::Layout; |
1903 | my $layout = new DC::Layout; |
1944 | $layout->set_text (delete $self->{template}); |
1904 | $layout->set_text (delete $self->{template}); |
1945 | $self->{template} = $layout; |
1905 | $self->{template} = $layout; |
1946 | } |
1906 | } |
1947 | |
1907 | |
1948 | if (exists $self->{markup}) { |
1908 | if (exists $self->{markup}) { |
… | |
… | |
1966 | |
1926 | |
1967 | delete $self->{ox}; |
1927 | delete $self->{ox}; |
1968 | $self->SUPER::realloc; |
1928 | $self->SUPER::realloc; |
1969 | } |
1929 | } |
1970 | |
1930 | |
|
|
1931 | sub clear { |
|
|
1932 | my ($self) = @_; |
|
|
1933 | |
|
|
1934 | $self->set_text (""); |
|
|
1935 | } |
|
|
1936 | |
1971 | sub set_text { |
1937 | sub set_text { |
1972 | my ($self, $text) = @_; |
1938 | my ($self, $text) = @_; |
1973 | |
1939 | |
1974 | return if $self->{text} eq "T$text"; |
1940 | return if $self->{text} eq "T$text"; |
1975 | $self->{text} = "T$text"; |
1941 | $self->{text} = "T$text"; |
… | |
… | |
1998 | |
1964 | |
1999 | sub size_request { |
1965 | sub size_request { |
2000 | my ($self) = @_; |
1966 | my ($self) = @_; |
2001 | |
1967 | |
2002 | $self->{size_req} ||= do { |
1968 | $self->{size_req} ||= do { |
|
|
1969 | my ($max_w, $max_h) = $self->get_max_wh; |
|
|
1970 | |
2003 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1971 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
2004 | $self->{layout}->set_width ($self->{max_w} || -1); |
1972 | $self->{layout}->set_width ($self->{max_w} || $max_w || -1); |
2005 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1973 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
2006 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1974 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
2007 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1975 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2008 | |
1976 | |
2009 | my ($w, $h) = $self->{layout}->size; |
1977 | my ($w, $h) = $self->{layout}->size; |
… | |
… | |
2081 | : ($self->{w} - $size->[0]) * 0.5); |
2049 | : ($self->{w} - $size->[0]) * 0.5); |
2082 | |
2050 | |
2083 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
2051 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
2084 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
2052 | : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y} |
2085 | : ($self->{h} - $size->[1]) * 0.5); |
2053 | : ($self->{h} - $size->[1]) * 0.5); |
|
|
2054 | |
|
|
2055 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2086 | }; |
2056 | }; |
2087 | |
2057 | |
2088 | my $w = List::Util::min $self->{w} + 4, $size->[0]; |
2058 | # unless ($self->{list}) { |
2089 | my $h = List::Util::min $self->{h} + 2, $size->[1]; |
2059 | # $self->{list} = DC::OpenGL::glGenList; |
2090 | |
2060 | # DC::OpenGL::glNewList $self->{list}; |
2091 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
2061 | # $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
|
|
2062 | # DC::OpenGL::glEndList; |
|
|
2063 | # } |
|
|
2064 | # |
|
|
2065 | # DC::OpenGL::glCallList $self->{list}; |
|
|
2066 | |
|
|
2067 | $self->{layout}->draw; |
2092 | } |
2068 | } |
|
|
2069 | |
|
|
2070 | #sub destroy { |
|
|
2071 | # my ($self) = @_; |
|
|
2072 | # |
|
|
2073 | # DC::OpenGL::glDeleteList delete $self->{list} if $self->{list}; |
|
|
2074 | # |
|
|
2075 | # $self->SUPER::destroy; |
|
|
2076 | #} |
2093 | |
2077 | |
2094 | ############################################################################# |
2078 | ############################################################################# |
2095 | |
2079 | |
2096 | package CFPlus::UI::EntryBase; |
2080 | package DC::UI::EntryBase; |
2097 | |
2081 | |
2098 | our @ISA = CFPlus::UI::Label::; |
2082 | our @ISA = DC::UI::Label::; |
2099 | |
2083 | |
2100 | use CFPlus::OpenGL; |
2084 | use DC::OpenGL; |
2101 | |
2085 | |
2102 | sub new { |
2086 | sub new { |
2103 | my $class = shift; |
2087 | my $class = shift; |
2104 | |
2088 | |
2105 | $class->SUPER::new ( |
2089 | $class->SUPER::new ( |
2106 | fg => [1, 1, 1], |
2090 | fg => [1, 1, 1], |
2107 | bg => [0, 0, 0, 0.2], |
2091 | bg => [0, 0, 0, 0.2], |
|
|
2092 | outline => [0.6, 0.3, 0.1], |
2108 | active_bg => [1, 1, 1, 0.5], |
2093 | active_bg => [0, 0, 1, .2], |
2109 | active_fg => [0, 0, 0], |
2094 | active_fg => [1, 1, 1], |
|
|
2095 | active_outline => [1, 1, 0], |
2110 | can_hover => 1, |
2096 | can_hover => 1, |
2111 | can_focus => 1, |
2097 | can_focus => 1, |
2112 | valign => 0, |
2098 | valign => 0, |
2113 | can_events => 1, |
2099 | can_events => 1, |
2114 | ellipsise => 0, |
2100 | ellipsise => 0, |
|
|
2101 | padding_x => 4, |
|
|
2102 | padding_y => 2, |
2115 | #text => ... |
2103 | #text => ... |
2116 | #hidden => "*", |
2104 | #hidden => "*", |
2117 | @_ |
2105 | @_ |
2118 | ) |
2106 | ) |
2119 | } |
2107 | } |
… | |
… | |
2170 | |
2158 | |
2171 | if ($uni == 8) { |
2159 | if ($uni == 8) { |
2172 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2160 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2173 | } elsif ($uni == 127) { |
2161 | } elsif ($uni == 127) { |
2174 | substr $text, $self->{cursor}, 1, ""; |
2162 | substr $text, $self->{cursor}, 1, ""; |
2175 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
2163 | } elsif ($sym == DC::SDLK_LEFT) { |
2176 | --$self->{cursor} if $self->{cursor}; |
2164 | --$self->{cursor} if $self->{cursor}; |
2177 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
2165 | } elsif ($sym == DC::SDLK_RIGHT) { |
2178 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2166 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2179 | } elsif ($sym == CFPlus::SDLK_HOME) { |
2167 | } elsif ($sym == DC::SDLK_HOME) { |
2180 | # what a hack |
2168 | # what a hack |
2181 | $self->{cursor} = |
2169 | $self->{cursor} = |
2182 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
2170 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
2183 | ? length $1 |
2171 | ? length $1 |
2184 | : 0; |
2172 | : 0; |
2185 | } elsif ($sym == CFPlus::SDLK_END) { |
2173 | } elsif ($sym == DC::SDLK_END) { |
2186 | # uh, again |
2174 | # uh, again |
2187 | $self->{cursor} = |
2175 | $self->{cursor} = |
2188 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
2176 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
2189 | ? $self->{cursor} + length $1 |
2177 | ? $self->{cursor} + length $1 |
2190 | : length $self->{text}; |
2178 | : length $self->{text}; |
… | |
… | |
2254 | glColor_premultiply @{$self->{bg}}; |
2242 | glColor_premultiply @{$self->{bg}}; |
2255 | } |
2243 | } |
2256 | |
2244 | |
2257 | glEnable GL_BLEND; |
2245 | glEnable GL_BLEND; |
2258 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2246 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2259 | glBegin GL_QUADS; |
|
|
2260 | glVertex 0 , 0; |
|
|
2261 | glVertex 0 , $self->{h}; |
|
|
2262 | glVertex $self->{w}, $self->{h}; |
2247 | glRect 0, 0, $self->{w}, $self->{h}; |
2263 | glVertex $self->{w}, 0; |
|
|
2264 | glEnd; |
|
|
2265 | glDisable GL_BLEND; |
2248 | glDisable GL_BLEND; |
2266 | |
2249 | |
2267 | $self->SUPER::_draw; |
2250 | $self->SUPER::_draw; |
2268 | |
2251 | |
2269 | #TODO: force update every cursor change :( |
2252 | #TODO: force update every cursor change :( |
… | |
… | |
2271 | |
2254 | |
2272 | unless (exists $self->{cur_h}) { |
2255 | unless (exists $self->{cur_h}) { |
2273 | my $text = substr $self->{text}, 0, $self->{cursor}; |
2256 | my $text = substr $self->{text}, 0, $self->{cursor}; |
2274 | utf8::encode $text; |
2257 | utf8::encode $text; |
2275 | |
2258 | |
2276 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
2259 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
2277 | } |
2260 | } |
2278 | |
2261 | |
|
|
2262 | glColor_premultiply @{$self->{active_fg}}; |
2279 | glBegin GL_LINES; |
2263 | glBegin GL_LINES; |
2280 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
2264 | glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy}; |
2281 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2265 | glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2282 | glEnd; |
2266 | glEnd; |
|
|
2267 | |
|
|
2268 | glLineWidth 3; |
|
|
2269 | glColor @{$self->{active_outline}}; |
|
|
2270 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
|
|
2271 | glLineWidth 1; |
|
|
2272 | |
|
|
2273 | } else { |
|
|
2274 | glColor @{$self->{outline}}; |
|
|
2275 | glBegin GL_LINE_STRIP; |
|
|
2276 | glVertex .5, $self->{h} * .5; |
|
|
2277 | glVertex .5, $self->{h} - 2.5; |
|
|
2278 | glVertex $self->{w} - .5, $self->{h} - 2.5; |
|
|
2279 | glVertex $self->{w} - .5, $self->{h} * .5; |
|
|
2280 | glEnd; |
2283 | } |
2281 | } |
2284 | } |
2282 | } |
2285 | |
2283 | |
2286 | ############################################################################# |
2284 | ############################################################################# |
2287 | |
2285 | |
2288 | package CFPlus::UI::Entry; |
2286 | package DC::UI::Entry; |
2289 | |
2287 | |
2290 | our @ISA = CFPlus::UI::EntryBase::; |
2288 | our @ISA = DC::UI::EntryBase::; |
2291 | |
2289 | |
2292 | use CFPlus::OpenGL; |
2290 | use DC::OpenGL; |
2293 | |
2291 | |
2294 | sub invoke_key_down { |
2292 | sub invoke_key_down { |
2295 | my ($self, $ev) = @_; |
2293 | my ($self, $ev) = @_; |
2296 | |
2294 | |
2297 | my $sym = $ev->{sym}; |
2295 | my $sym = $ev->{sym}; |
… | |
… | |
2303 | $self->{history_pointer} = -1; |
2301 | $self->{history_pointer} = -1; |
2304 | $self->{history_saveback} = ''; |
2302 | $self->{history_saveback} = ''; |
2305 | $self->emit (activate => $txt); |
2303 | $self->emit (activate => $txt); |
2306 | $self->update; |
2304 | $self->update; |
2307 | |
2305 | |
2308 | } elsif ($sym == CFPlus::SDLK_UP) { |
2306 | } elsif ($sym == DC::SDLK_UP) { |
2309 | if ($self->{history_pointer} < 0) { |
2307 | if ($self->{history_pointer} < 0) { |
2310 | $self->{history_saveback} = $self->get_text; |
2308 | $self->{history_saveback} = $self->get_text; |
2311 | } |
2309 | } |
2312 | if (@{$self->{history} || []} > 0) { |
2310 | if (@{$self->{history} || []} > 0) { |
2313 | $self->{history_pointer}++; |
2311 | $self->{history_pointer}++; |
… | |
… | |
2315 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2313 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2316 | } |
2314 | } |
2317 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2315 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2318 | } |
2316 | } |
2319 | |
2317 | |
2320 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2318 | } elsif ($sym == DC::SDLK_DOWN) { |
2321 | $self->{history_pointer}--; |
2319 | $self->{history_pointer}--; |
2322 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2320 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2323 | |
2321 | |
2324 | if ($self->{history_pointer} >= 0) { |
2322 | if ($self->{history_pointer} >= 0) { |
2325 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2323 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
… | |
… | |
2334 | 1 |
2332 | 1 |
2335 | } |
2333 | } |
2336 | |
2334 | |
2337 | ############################################################################# |
2335 | ############################################################################# |
2338 | |
2336 | |
2339 | package CFPlus::UI::TextEdit; |
2337 | package DC::UI::TextEdit; |
2340 | |
2338 | |
2341 | our @ISA = CFPlus::UI::EntryBase::; |
2339 | our @ISA = DC::UI::EntryBase::; |
2342 | |
2340 | |
2343 | use CFPlus::OpenGL; |
2341 | use DC::OpenGL; |
|
|
2342 | |
|
|
2343 | sub new { |
|
|
2344 | my $class = shift; |
|
|
2345 | |
|
|
2346 | $class->SUPER::new ( |
|
|
2347 | padding_y => 4, |
|
|
2348 | |
|
|
2349 | @_ |
|
|
2350 | ) |
|
|
2351 | } |
2344 | |
2352 | |
2345 | sub move_cursor_ver { |
2353 | sub move_cursor_ver { |
2346 | my ($self, $dy) = @_; |
2354 | my ($self, $dy) = @_; |
2347 | |
2355 | |
2348 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
2356 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
… | |
… | |
2360 | sub invoke_key_down { |
2368 | sub invoke_key_down { |
2361 | my ($self, $ev) = @_; |
2369 | my ($self, $ev) = @_; |
2362 | |
2370 | |
2363 | my $sym = $ev->{sym}; |
2371 | my $sym = $ev->{sym}; |
2364 | |
2372 | |
2365 | if ($sym == CFPlus::SDLK_UP) { |
2373 | if ($sym == DC::SDLK_UP) { |
2366 | $self->move_cursor_ver (-1); |
2374 | $self->move_cursor_ver (-1); |
2367 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
2375 | } elsif ($sym == DC::SDLK_DOWN) { |
2368 | $self->move_cursor_ver (+1); |
2376 | $self->move_cursor_ver (+1); |
2369 | } else { |
2377 | } else { |
2370 | return $self->SUPER::invoke_key_down ($ev) |
2378 | return $self->SUPER::invoke_key_down ($ev) |
2371 | } |
2379 | } |
2372 | |
2380 | |
2373 | 1 |
2381 | 1 |
2374 | } |
2382 | } |
2375 | |
2383 | |
2376 | ############################################################################# |
2384 | ############################################################################# |
2377 | |
2385 | |
2378 | package CFPlus::UI::Button; |
2386 | package DC::UI::ButtonBin; |
2379 | |
2387 | |
2380 | our @ISA = CFPlus::UI::Label::; |
2388 | our @ISA = DC::UI::Bin::; |
2381 | |
2389 | |
2382 | use CFPlus::OpenGL; |
2390 | use DC::OpenGL; |
2383 | |
2391 | |
2384 | my @tex = |
2392 | my @tex = |
2385 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2393 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2386 | qw(b1_button_active.png); |
2394 | qw(b1_button_inactive.png b1_button_active.png); |
2387 | |
2395 | |
2388 | sub new { |
2396 | sub new { |
2389 | my $class = shift; |
2397 | my $class = shift; |
2390 | |
2398 | |
2391 | $class->SUPER::new ( |
2399 | $class->SUPER::new ( |
2392 | padding_x => 4, |
|
|
2393 | padding_y => 4, |
|
|
2394 | fg => [1, 1, 1], |
|
|
2395 | active_fg => [0, 0, 1], |
|
|
2396 | can_hover => 1, |
2400 | can_hover => 1, |
2397 | align => 0, |
2401 | align => 0, |
2398 | valign => 0, |
2402 | valign => 0, |
2399 | can_events => 1, |
2403 | can_events => 1, |
2400 | @_ |
2404 | @_ |
… | |
… | |
2412 | } |
2416 | } |
2413 | |
2417 | |
2414 | sub _draw { |
2418 | sub _draw { |
2415 | my ($self) = @_; |
2419 | my ($self) = @_; |
2416 | |
2420 | |
2417 | local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg}; |
|
|
2418 | |
|
|
2419 | glEnable GL_TEXTURE_2D; |
2421 | glEnable GL_TEXTURE_2D; |
2420 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2422 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2421 | glColor 0, 0, 0, 1; |
2423 | glColor 0, 0, 0, 1; |
2422 | |
2424 | |
|
|
2425 | my $tex = $tex[$GRAB == $self]; |
2423 | $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2426 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2424 | |
2427 | |
2425 | glDisable GL_TEXTURE_2D; |
2428 | glDisable GL_TEXTURE_2D; |
2426 | |
2429 | |
2427 | $self->SUPER::_draw; |
2430 | $self->SUPER::_draw; |
2428 | } |
2431 | } |
2429 | |
2432 | |
2430 | ############################################################################# |
2433 | ############################################################################# |
2431 | |
2434 | |
2432 | package CFPlus::UI::CheckBox; |
2435 | package DC::UI::Button; |
2433 | |
2436 | |
2434 | our @ISA = CFPlus::UI::DrawBG::; |
2437 | our @ISA = DC::UI::Label::; |
|
|
2438 | |
|
|
2439 | use DC::OpenGL; |
2435 | |
2440 | |
2436 | my @tex = |
2441 | my @tex = |
2437 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2442 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
|
|
2443 | qw(b1_button_inactive.png b1_button_active.png); |
|
|
2444 | |
|
|
2445 | sub new { |
|
|
2446 | my $class = shift; |
|
|
2447 | |
|
|
2448 | $class->SUPER::new ( |
|
|
2449 | padding_x => 8, |
|
|
2450 | padding_y => 4, |
|
|
2451 | fg => [1.0, 1.0, 1.0], |
|
|
2452 | active_fg => [0.8, 0.8, 0.8], |
|
|
2453 | can_hover => 1, |
|
|
2454 | align => 0, |
|
|
2455 | valign => 0, |
|
|
2456 | can_events => 1, |
|
|
2457 | @_ |
|
|
2458 | ) |
|
|
2459 | } |
|
|
2460 | |
|
|
2461 | sub invoke_button_up { |
|
|
2462 | my ($self, $ev, $x, $y) = @_; |
|
|
2463 | |
|
|
2464 | $self->emit ("activate") |
|
|
2465 | if $x >= 0 && $x < $self->{w} |
|
|
2466 | && $y >= 0 && $y < $self->{h}; |
|
|
2467 | |
|
|
2468 | 1 |
|
|
2469 | } |
|
|
2470 | |
|
|
2471 | sub _draw { |
|
|
2472 | my ($self) = @_; |
|
|
2473 | |
|
|
2474 | local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg}; |
|
|
2475 | |
|
|
2476 | glEnable GL_TEXTURE_2D; |
|
|
2477 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2478 | glColor 0, 0, 0, 1; |
|
|
2479 | |
|
|
2480 | my $tex = $tex[$GRAB == $self]; |
|
|
2481 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
|
|
2482 | |
|
|
2483 | glDisable GL_TEXTURE_2D; |
|
|
2484 | |
|
|
2485 | $self->SUPER::_draw; |
|
|
2486 | } |
|
|
2487 | |
|
|
2488 | ############################################################################# |
|
|
2489 | |
|
|
2490 | package DC::UI::CheckBox; |
|
|
2491 | |
|
|
2492 | our @ISA = DC::UI::DrawBG::; |
|
|
2493 | |
|
|
2494 | my @tex = |
|
|
2495 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2438 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2496 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
2439 | |
2497 | |
2440 | use CFPlus::OpenGL; |
2498 | use DC::OpenGL; |
2441 | |
2499 | |
2442 | sub new { |
2500 | sub new { |
2443 | my $class = shift; |
2501 | my $class = shift; |
2444 | |
2502 | |
2445 | $class->SUPER::new ( |
2503 | $class->SUPER::new ( |
… | |
… | |
2485 | sub _draw { |
2543 | sub _draw { |
2486 | my ($self) = @_; |
2544 | my ($self) = @_; |
2487 | |
2545 | |
2488 | $self->SUPER::_draw; |
2546 | $self->SUPER::_draw; |
2489 | |
2547 | |
2490 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
2548 | glTranslate $self->{padding_x}, $self->{padding_y}, 0; |
2491 | |
2549 | |
2492 | my ($w, $h) = @$self{qw(w h)}; |
2550 | my ($w, $h) = @$self{qw(w h)}; |
2493 | |
2551 | |
2494 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2552 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
2495 | |
2553 | |
… | |
… | |
2502 | glDisable GL_TEXTURE_2D; |
2560 | glDisable GL_TEXTURE_2D; |
2503 | } |
2561 | } |
2504 | |
2562 | |
2505 | ############################################################################# |
2563 | ############################################################################# |
2506 | |
2564 | |
2507 | package CFPlus::UI::Image; |
2565 | package DC::UI::Image; |
2508 | |
2566 | |
2509 | our @ISA = CFPlus::UI::Base::; |
2567 | our @ISA = DC::UI::Base::; |
2510 | |
2568 | |
2511 | use CFPlus::OpenGL; |
2569 | use DC::OpenGL; |
2512 | |
2570 | |
2513 | our %texture_cache; |
2571 | our %texture_cache; |
2514 | |
2572 | |
2515 | sub new { |
2573 | sub new { |
2516 | my $class = shift; |
2574 | my $class = shift; |
2517 | |
2575 | |
2518 | my $self = $class->SUPER::new ( |
2576 | my $self = $class->SUPER::new ( |
2519 | can_events => 0, |
2577 | can_events => 0, |
|
|
2578 | scale => 1, |
2520 | @_, |
2579 | @_, |
2521 | ); |
2580 | ); |
2522 | |
2581 | |
2523 | $self->{path} || $self->{tex} |
2582 | $self->{path} || $self->{tex} |
2524 | or Carp::croak "'path' or 'tex' attributes required"; |
2583 | or Carp::croak "'path' or 'tex' attributes required"; |
2525 | |
2584 | |
2526 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2585 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2527 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2586 | new_from_file DC::Texture DC::find_rcfile $self->{path}, mipmap => 1; |
2528 | |
2587 | |
2529 | CFPlus::weaken $texture_cache{$self->{path}}; |
2588 | DC::weaken $texture_cache{$self->{path}}; |
2530 | |
2589 | |
2531 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2590 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2532 | |
2591 | |
2533 | $self |
2592 | $self |
2534 | } |
2593 | } |
2535 | |
2594 | |
2536 | sub STORABLE_freeze { |
2595 | sub STORABLE_freeze { |
2537 | my ($self, $cloning) = @_; |
2596 | my ($self, $cloning) = @_; |
2538 | |
2597 | |
2539 | $self->{path} |
2598 | $self->{path} |
2540 | or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; |
2599 | or die "cannot serialise DC::UI::Image on non-loadable images\n"; |
2541 | |
2600 | |
2542 | $self->{path} |
2601 | $self->{path} |
2543 | } |
2602 | } |
2544 | |
2603 | |
2545 | sub STORABLE_attach { |
2604 | sub STORABLE_attach { |
… | |
… | |
2549 | } |
2608 | } |
2550 | |
2609 | |
2551 | sub size_request { |
2610 | sub size_request { |
2552 | my ($self) = @_; |
2611 | my ($self) = @_; |
2553 | |
2612 | |
2554 | ($self->{tex}{w}, $self->{tex}{h}) |
2613 | (int $self->{tex}{w} * $self->{scale}, int $self->{tex}{h} * $self->{scale}) |
2555 | } |
2614 | } |
2556 | |
2615 | |
2557 | sub _draw { |
2616 | sub _draw { |
2558 | my ($self) = @_; |
2617 | my ($self) = @_; |
2559 | |
2618 | |
… | |
… | |
2569 | } |
2628 | } |
2570 | |
2629 | |
2571 | glEnable GL_TEXTURE_2D; |
2630 | glEnable GL_TEXTURE_2D; |
2572 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2631 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2573 | |
2632 | |
2574 | $tex->draw_quad (0, 0, $w, $h); |
2633 | $tex->draw_quad_alpha (0, 0, $w, $h); |
2575 | |
2634 | |
2576 | glDisable GL_TEXTURE_2D; |
2635 | glDisable GL_TEXTURE_2D; |
2577 | } |
2636 | } |
2578 | |
2637 | |
2579 | ############################################################################# |
2638 | ############################################################################# |
2580 | |
2639 | |
2581 | package CFPlus::UI::ImageButton; |
2640 | package DC::UI::ImageButton; |
2582 | |
2641 | |
2583 | our @ISA = CFPlus::UI::Image::; |
2642 | our @ISA = DC::UI::Image::; |
2584 | |
2643 | |
2585 | use CFPlus::OpenGL; |
2644 | use DC::OpenGL; |
2586 | |
2645 | |
2587 | my %textures; |
2646 | my %textures; |
2588 | |
2647 | |
2589 | sub new { |
2648 | sub new { |
2590 | my $class = shift; |
2649 | my $class = shift; |
… | |
… | |
2600 | can_events => 1, |
2659 | can_events => 1, |
2601 | @_ |
2660 | @_ |
2602 | ); |
2661 | ); |
2603 | } |
2662 | } |
2604 | |
2663 | |
|
|
2664 | sub invoke_button_down { |
|
|
2665 | my ($self, $ev, $x, $y) = @_; |
|
|
2666 | |
|
|
2667 | 1 |
|
|
2668 | } |
|
|
2669 | |
2605 | sub invoke_button_up { |
2670 | sub invoke_button_up { |
2606 | my ($self, $ev, $x, $y) = @_; |
2671 | my ($self, $ev, $x, $y) = @_; |
2607 | |
2672 | |
2608 | $self->emit ("activate") |
2673 | $self->emit ("activate") |
2609 | if $x >= 0 && $x < $self->{w} |
2674 | if $x >= 0 && $x < $self->{w} |
… | |
… | |
2612 | 1 |
2677 | 1 |
2613 | } |
2678 | } |
2614 | |
2679 | |
2615 | ############################################################################# |
2680 | ############################################################################# |
2616 | |
2681 | |
2617 | package CFPlus::UI::VGauge; |
2682 | package DC::UI::VGauge; |
2618 | |
2683 | |
2619 | our @ISA = CFPlus::UI::Base::; |
2684 | our @ISA = DC::UI::Base::; |
2620 | |
2685 | |
2621 | use List::Util qw(min max); |
2686 | use List::Util qw(min max); |
2622 | |
2687 | |
2623 | use CFPlus::OpenGL; |
2688 | use DC::OpenGL; |
2624 | |
2689 | |
2625 | my %tex = ( |
2690 | my %tex = ( |
2626 | food => [ |
2691 | food => [ |
2627 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2692 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2628 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2693 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2629 | ], |
2694 | ], |
2630 | grace => [ |
2695 | grace => [ |
2631 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2696 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2632 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2697 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2633 | ], |
2698 | ], |
2634 | hp => [ |
2699 | hp => [ |
2635 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2700 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2636 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2701 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2637 | ], |
2702 | ], |
2638 | mana => [ |
2703 | mana => [ |
2639 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2704 | map { new_from_file DC::Texture DC::find_rcfile $_, mipmap => 1 } |
2640 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2705 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2641 | ], |
2706 | ], |
2642 | ); |
2707 | ); |
2643 | |
2708 | |
2644 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
2709 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
… | |
… | |
2747 | glDisable GL_TEXTURE_2D; |
2812 | glDisable GL_TEXTURE_2D; |
2748 | } |
2813 | } |
2749 | |
2814 | |
2750 | ############################################################################# |
2815 | ############################################################################# |
2751 | |
2816 | |
|
|
2817 | package DC::UI::Progress; |
|
|
2818 | |
|
|
2819 | our @ISA = DC::UI::Label::; |
|
|
2820 | |
|
|
2821 | use DC::OpenGL; |
|
|
2822 | |
|
|
2823 | sub new { |
|
|
2824 | my ($class, %arg) = @_; |
|
|
2825 | |
|
|
2826 | my $self = $class->SUPER::new ( |
|
|
2827 | fg => [1, 1, 1], |
|
|
2828 | bg => [0, 0, 1, 0.2], |
|
|
2829 | bar => [0.7, 0.5, 0.1, 0.8], |
|
|
2830 | outline => [0.4, 0.3, 0], |
|
|
2831 | fontsize => 0.9, |
|
|
2832 | valign => 0, |
|
|
2833 | align => 0, |
|
|
2834 | can_events => 1, |
|
|
2835 | ellipsise => 1, |
|
|
2836 | label => "%d%%", |
|
|
2837 | %arg, |
|
|
2838 | ); |
|
|
2839 | |
|
|
2840 | $self->set_value ($arg{value} || -1); |
|
|
2841 | |
|
|
2842 | $self |
|
|
2843 | } |
|
|
2844 | |
|
|
2845 | sub set_label { |
|
|
2846 | my ($self, $label) = @_; |
|
|
2847 | |
|
|
2848 | return if $self->{label} eq $label; |
|
|
2849 | $self->{label} = $label; |
|
|
2850 | |
|
|
2851 | $self->DC::UI::Progress::set_value (0 + delete $self->{value}); |
|
|
2852 | } |
|
|
2853 | |
|
|
2854 | sub set_value { |
|
|
2855 | my ($self, $value) = @_; |
|
|
2856 | |
|
|
2857 | if ($self->{value} ne $value) { |
|
|
2858 | $self->{value} = $value; |
|
|
2859 | |
|
|
2860 | if ($value < 0) { |
|
|
2861 | $self->set_text ("-"); |
|
|
2862 | } else { |
|
|
2863 | $self->set_text (sprintf $self->{label}, $value * 100); |
|
|
2864 | } |
|
|
2865 | |
|
|
2866 | $self->update; |
|
|
2867 | } |
|
|
2868 | } |
|
|
2869 | |
|
|
2870 | sub _draw { |
|
|
2871 | my ($self) = @_; |
|
|
2872 | |
|
|
2873 | glEnable GL_BLEND; |
|
|
2874 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2875 | |
|
|
2876 | if ($self->{value} >= 0) { |
|
|
2877 | my $s = int 2 + ($self->{w} - 4) * $self->{value}; |
|
|
2878 | |
|
|
2879 | glColor_premultiply @{$self->{bar}}; |
|
|
2880 | glRect 2, 2, $s, $self->{h} - 2; |
|
|
2881 | glColor_premultiply @{$self->{bg}}; |
|
|
2882 | glRect $s, 2, $self->{w} - 2, $self->{h} - 2; |
|
|
2883 | } |
|
|
2884 | |
|
|
2885 | glColor_premultiply @{$self->{outline}}; |
|
|
2886 | glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5; |
|
|
2887 | |
|
|
2888 | glDisable GL_BLEND; |
|
|
2889 | |
|
|
2890 | { |
|
|
2891 | local $self->{bg}; # do not draw background |
|
|
2892 | $self->SUPER::_draw; |
|
|
2893 | } |
|
|
2894 | } |
|
|
2895 | |
|
|
2896 | ############################################################################# |
|
|
2897 | |
|
|
2898 | package DC::UI::ExperienceProgress; |
|
|
2899 | |
|
|
2900 | our @ISA = DC::UI::Progress::; |
|
|
2901 | |
|
|
2902 | sub new { |
|
|
2903 | my ($class, %arg) = @_; |
|
|
2904 | |
|
|
2905 | my $self = $class->SUPER::new ( |
|
|
2906 | tooltip => sub { |
|
|
2907 | my ($self) = @_; |
|
|
2908 | |
|
|
2909 | sprintf "level %d\n%s points\n%s next level\n%s to go", |
|
|
2910 | $self->{lvl}, |
|
|
2911 | ::formsep ($self->{exp}), |
|
|
2912 | ::formsep ($self->{nxt}), |
|
|
2913 | ::formsep ($self->{nxt} - $self->{exp}), |
|
|
2914 | }, |
|
|
2915 | %arg |
|
|
2916 | ); |
|
|
2917 | |
|
|
2918 | $::CONN->{on_exp_update}{$self+0} = sub { $self->set_value ($self->{value}) } |
|
|
2919 | if $::CONN; |
|
|
2920 | |
|
|
2921 | $self |
|
|
2922 | } |
|
|
2923 | |
|
|
2924 | sub DESTROY { |
|
|
2925 | my ($self) = @_; |
|
|
2926 | |
|
|
2927 | delete $::CONN->{on_exp_update}{$self+0} |
|
|
2928 | if $::CONN; |
|
|
2929 | |
|
|
2930 | $self->SUPER::DESTROY; |
|
|
2931 | } |
|
|
2932 | |
|
|
2933 | sub set_value { |
|
|
2934 | my ($self, $lvl, $exp) = @_; |
|
|
2935 | |
|
|
2936 | $self->{lvl} = $lvl; |
|
|
2937 | $self->{exp} = $exp; |
|
|
2938 | |
|
|
2939 | my $v = -1; |
|
|
2940 | |
|
|
2941 | if ($::CONN && (my $table = $::CONN->{exp_table})) { |
|
|
2942 | my $l0 = $table->[$lvl - 1]; |
|
|
2943 | my $l1 = $table->[$lvl]; |
|
|
2944 | |
|
|
2945 | $self->{nxt} = $l1; |
|
|
2946 | |
|
|
2947 | $v = ($exp - $l0) / ($l1 - $l0); |
|
|
2948 | } |
|
|
2949 | |
|
|
2950 | $self->SUPER::set_value ($v); |
|
|
2951 | } |
|
|
2952 | |
|
|
2953 | ############################################################################# |
|
|
2954 | |
2752 | package CFPlus::UI::Gauge; |
2955 | package DC::UI::Gauge; |
2753 | |
2956 | |
2754 | our @ISA = CFPlus::UI::VBox::; |
2957 | our @ISA = DC::UI::VBox::; |
2755 | |
2958 | |
2756 | sub new { |
2959 | sub new { |
2757 | my ($class, %arg) = @_; |
2960 | my ($class, %arg) = @_; |
2758 | |
2961 | |
2759 | my $self = $class->SUPER::new ( |
2962 | my $self = $class->SUPER::new ( |
… | |
… | |
2761 | can_hover => 1, |
2964 | can_hover => 1, |
2762 | can_events => 1, |
2965 | can_events => 1, |
2763 | %arg, |
2966 | %arg, |
2764 | ); |
2967 | ); |
2765 | |
2968 | |
2766 | $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); |
2969 | $self->add ($self->{value} = new DC::UI::Label valign => +1, align => 0, template => "999"); |
2767 | $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2970 | $self->add ($self->{gauge} = new DC::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2768 | $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); |
2971 | $self->add ($self->{max} = new DC::UI::Label valign => -1, align => 0, template => "999"); |
2769 | |
2972 | |
2770 | $self |
2973 | $self |
2771 | } |
2974 | } |
2772 | |
2975 | |
2773 | sub set_fontsize { |
2976 | sub set_fontsize { |
… | |
… | |
2794 | $self->{value}->set_text ($val); |
2997 | $self->{value}->set_text ($val); |
2795 | } |
2998 | } |
2796 | |
2999 | |
2797 | ############################################################################# |
3000 | ############################################################################# |
2798 | |
3001 | |
2799 | package CFPlus::UI::Slider; |
3002 | package DC::UI::Slider; |
2800 | |
3003 | |
2801 | use strict; |
3004 | use strict; |
2802 | |
3005 | |
2803 | use CFPlus::OpenGL; |
3006 | use DC::OpenGL; |
2804 | |
3007 | |
2805 | our @ISA = CFPlus::UI::DrawBG::; |
3008 | our @ISA = DC::UI::DrawBG::; |
2806 | |
3009 | |
2807 | my @tex = |
3010 | my @tex = |
2808 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } |
3011 | map { new_from_file DC::Texture DC::find_rcfile $_ } |
2809 | qw(s1_slider.png s1_slider_bg.png); |
3012 | qw(s1_slider.png s1_slider_bg.png); |
2810 | |
3013 | |
2811 | sub new { |
3014 | sub new { |
2812 | my $class = shift; |
3015 | my $class = shift; |
2813 | |
3016 | |
… | |
… | |
2881 | |
3084 | |
2882 | $self->SUPER::invoke_button_down ($ev, $x, $y); |
3085 | $self->SUPER::invoke_button_down ($ev, $x, $y); |
2883 | |
3086 | |
2884 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
3087 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
2885 | |
3088 | |
2886 | $self->invoke_mouse_motion ($ev, $x, $y) |
3089 | $self->invoke_mouse_motion ($ev, $x, $y); |
|
|
3090 | |
|
|
3091 | 1 |
2887 | } |
3092 | } |
2888 | |
3093 | |
2889 | sub invoke_mouse_motion { |
3094 | sub invoke_mouse_motion { |
2890 | my ($self, $ev, $x, $y) = @_; |
3095 | my ($self, $ev, $x, $y) = @_; |
2891 | |
3096 | |
… | |
… | |
2907 | sub invoke_mouse_wheel { |
3112 | sub invoke_mouse_wheel { |
2908 | my ($self, $ev) = @_; |
3113 | my ($self, $ev) = @_; |
2909 | |
3114 | |
2910 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
3115 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2911 | |
3116 | |
2912 | my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; |
3117 | my $pagepart = $ev->{mod} & DC::KMOD_SHIFT ? 1 : 0.2; |
2913 | |
3118 | |
2914 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
3119 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
2915 | |
3120 | |
2916 | ! ! $delta |
3121 | 1 |
2917 | } |
3122 | } |
2918 | |
3123 | |
2919 | sub update { |
3124 | sub update { |
2920 | my ($self) = @_; |
3125 | my ($self) = @_; |
2921 | |
3126 | |
… | |
… | |
2970 | glDisable GL_TEXTURE_2D; |
3175 | glDisable GL_TEXTURE_2D; |
2971 | } |
3176 | } |
2972 | |
3177 | |
2973 | ############################################################################# |
3178 | ############################################################################# |
2974 | |
3179 | |
2975 | package CFPlus::UI::ValSlider; |
3180 | package DC::UI::ValSlider; |
2976 | |
3181 | |
2977 | our @ISA = CFPlus::UI::HBox::; |
3182 | our @ISA = DC::UI::HBox::; |
2978 | |
3183 | |
2979 | sub new { |
3184 | sub new { |
2980 | my ($class, %arg) = @_; |
3185 | my ($class, %arg) = @_; |
2981 | |
3186 | |
2982 | my $range = delete $arg{range}; |
3187 | my $range = delete $arg{range}; |
2983 | |
3188 | |
2984 | my $self = $class->SUPER::new ( |
3189 | my $self = $class->SUPER::new ( |
2985 | slider => (new CFPlus::UI::Slider expand => 1, range => $range), |
3190 | slider => (new DC::UI::Slider expand => 1, range => $range), |
2986 | entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), |
3191 | entry => (new DC::UI::Label text => "", template => delete $arg{template}), |
2987 | to_value => sub { shift }, |
3192 | to_value => sub { shift }, |
2988 | from_value => sub { shift }, |
3193 | from_value => sub { shift }, |
2989 | %arg, |
3194 | %arg, |
2990 | ); |
3195 | ); |
2991 | |
3196 | |
… | |
… | |
3011 | sub set_range { shift->{slider}->set_range (@_) } |
3216 | sub set_range { shift->{slider}->set_range (@_) } |
3012 | sub set_value { shift->{slider}->set_value (@_) } |
3217 | sub set_value { shift->{slider}->set_value (@_) } |
3013 | |
3218 | |
3014 | ############################################################################# |
3219 | ############################################################################# |
3015 | |
3220 | |
3016 | package CFPlus::UI::TextScroller; |
3221 | package DC::UI::TextScroller; |
3017 | |
3222 | |
3018 | our @ISA = CFPlus::UI::HBox::; |
3223 | our @ISA = DC::UI::HBox::; |
3019 | |
3224 | |
3020 | use CFPlus::OpenGL; |
3225 | use DC::OpenGL; |
3021 | |
3226 | |
3022 | sub new { |
3227 | sub new { |
3023 | my $class = shift; |
3228 | my $class = shift; |
3024 | |
3229 | |
3025 | my $self = $class->SUPER::new ( |
3230 | my $self = $class->SUPER::new ( |
… | |
… | |
3027 | can_events => 1, |
3232 | can_events => 1, |
3028 | indent => 0, |
3233 | indent => 0, |
3029 | #font => default_font |
3234 | #font => default_font |
3030 | @_, |
3235 | @_, |
3031 | |
3236 | |
3032 | layout => (new CFPlus::Layout), |
3237 | layout => (new DC::Layout), |
3033 | par => [], |
3238 | par => [], |
3034 | max_par => 0, |
3239 | max_par => 0, |
3035 | height => 0, |
3240 | height => 0, |
3036 | children => [ |
3241 | children => [ |
3037 | (new CFPlus::UI::Empty expand => 1), |
3242 | (new DC::UI::Empty expand => 1), |
3038 | (new CFPlus::UI::Slider vertical => 1), |
3243 | (new DC::UI::Slider vertical => 1), |
3039 | ], |
3244 | ], |
3040 | ); |
3245 | ); |
3041 | |
3246 | |
3042 | $self->{children}[1]->connect (changed => sub { $self->update }); |
3247 | $self->{children}[1]->connect (changed => sub { $self->update }); |
3043 | |
3248 | |
… | |
… | |
3052 | } |
3257 | } |
3053 | |
3258 | |
3054 | sub size_request { |
3259 | sub size_request { |
3055 | my ($self) = @_; |
3260 | my ($self) = @_; |
3056 | |
3261 | |
3057 | my ($empty, $slider) = @{ $self->{children} }; |
3262 | my ($empty, $slider) = $self->visible_children; |
3058 | |
3263 | |
3059 | local $self->{children} = [$empty, $slider]; |
3264 | local $self->{children} = [$empty, $slider]; |
3060 | $self->SUPER::size_request |
3265 | $self->SUPER::size_request |
3061 | } |
3266 | } |
3062 | |
3267 | |
… | |
… | |
3226 | $ROOT->on_post_alloc ($self => sub { |
3431 | $ROOT->on_post_alloc ($self => sub { |
3227 | $self->force_uptodate; |
3432 | $self->force_uptodate; |
3228 | |
3433 | |
3229 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3434 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3230 | |
3435 | |
3231 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
3436 | $self->{texture} ||= new_from_opengl DC::Texture $W, $H, sub { |
3232 | glClearColor 0, 0, 0, 0; |
3437 | glClearColor 0, 0, 0, 0; |
3233 | glClear GL_COLOR_BUFFER_BIT; |
3438 | glClear GL_COLOR_BUFFER_BIT; |
3234 | |
3439 | |
3235 | package CFPlus::UI::Base; |
3440 | package DC::UI::Base; |
3236 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3441 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3237 | (0, 0, $self->{w}, $self->{h}); |
3442 | (0, 0, $self->{w}, $self->{h}); |
3238 | |
3443 | |
3239 | my $top = int $self->{children}[1]{range}[0]; |
3444 | my $top = int $self->{children}[1]{range}[0]; |
3240 | |
3445 | |
… | |
… | |
3251 | |
3456 | |
3252 | if ($y0 < $y + $h && $y < $y1) { |
3457 | if ($y0 < $y + $h && $y < $y1) { |
3253 | my $layout = $self->get_layout ($para); |
3458 | my $layout = $self->get_layout ($para); |
3254 | |
3459 | |
3255 | $layout->render ($para->{indent}, $y - $y0); |
3460 | $layout->render ($para->{indent}, $y - $y0); |
|
|
3461 | $layout->draw; |
3256 | |
3462 | |
3257 | if (my @w = @{ $para->{widget} }) { |
3463 | if (my @w = @{ $para->{widget} }) { |
3258 | my @s = $layout->get_shapes; |
3464 | my @s = $layout->get_shapes; |
3259 | |
3465 | |
3260 | for (@w) { |
3466 | for (@w) { |
… | |
… | |
3298 | $self->{children}[1]->draw; |
3504 | $self->{children}[1]->draw; |
3299 | } |
3505 | } |
3300 | |
3506 | |
3301 | ############################################################################# |
3507 | ############################################################################# |
3302 | |
3508 | |
3303 | package CFPlus::UI::Animator; |
3509 | package DC::UI::Animator; |
3304 | |
3510 | |
3305 | use CFPlus::OpenGL; |
3511 | use DC::OpenGL; |
3306 | |
3512 | |
3307 | our @ISA = CFPlus::UI::Bin::; |
3513 | our @ISA = DC::UI::Bin::; |
3308 | |
3514 | |
3309 | sub moveto { |
3515 | sub moveto { |
3310 | my ($self, $x, $y) = @_; |
3516 | my ($self, $x, $y) = @_; |
3311 | |
3517 | |
3312 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
3518 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
3340 | glPopMatrix; |
3546 | glPopMatrix; |
3341 | } |
3547 | } |
3342 | |
3548 | |
3343 | ############################################################################# |
3549 | ############################################################################# |
3344 | |
3550 | |
3345 | package CFPlus::UI::Flopper; |
3551 | package DC::UI::Flopper; |
3346 | |
3552 | |
3347 | our @ISA = CFPlus::UI::Button::; |
3553 | our @ISA = DC::UI::Button::; |
3348 | |
3554 | |
3349 | sub new { |
3555 | sub new { |
3350 | my $class = shift; |
3556 | my $class = shift; |
3351 | |
3557 | |
3352 | my $self = $class->SUPER::new ( |
3558 | my $self = $class->SUPER::new ( |
… | |
… | |
3364 | $self->{other}->toggle_visibility; |
3570 | $self->{other}->toggle_visibility; |
3365 | } |
3571 | } |
3366 | |
3572 | |
3367 | ############################################################################# |
3573 | ############################################################################# |
3368 | |
3574 | |
3369 | package CFPlus::UI::Tooltip; |
3575 | package DC::UI::Tooltip; |
3370 | |
3576 | |
3371 | our @ISA = CFPlus::UI::Bin::; |
3577 | our @ISA = DC::UI::Bin::; |
3372 | |
3578 | |
3373 | use CFPlus::OpenGL; |
3579 | use DC::OpenGL; |
3374 | |
3580 | |
3375 | sub new { |
3581 | sub new { |
3376 | my $class = shift; |
3582 | my $class = shift; |
3377 | |
3583 | |
3378 | $class->SUPER::new ( |
3584 | $class->SUPER::new ( |
… | |
… | |
3382 | } |
3588 | } |
3383 | |
3589 | |
3384 | sub set_tooltip_from { |
3590 | sub set_tooltip_from { |
3385 | my ($self, $widget) = @_; |
3591 | my ($self, $widget) = @_; |
3386 | |
3592 | |
3387 | $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1 |
|
|
3388 | if $widget->{tooltip} =~ /^#(.*)$/; |
|
|
3389 | |
|
|
3390 | my $tooltip = $widget->{tooltip}; |
3593 | my $tip = $widget->{tooltip}; |
|
|
3594 | $tip = $tip->($widget) if "CODE" eq ref $tip; |
|
|
3595 | |
|
|
3596 | $tip = DC::Pod::section_label tooltip => $1 |
|
|
3597 | if $tip =~ /^#(.*)$/; |
3391 | |
3598 | |
3392 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3599 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3393 | $tooltip .= "\n\n" . (ref $widget) . "\n" |
3600 | $tip .= "\n\n" . (ref $widget) . "\n" |
3394 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3601 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
3395 | . "req $widget->{req_w} $widget->{req_h}\n" |
3602 | . "req $widget->{req_w} $widget->{req_h}\n" |
3396 | . "visible $widget->{visible}"; |
3603 | . "visible $widget->{visible}"; |
3397 | } |
3604 | } |
3398 | |
3605 | |
3399 | $tooltip =~ s/^\n+//; |
3606 | $tip =~ s/^\n+//; |
3400 | $tooltip =~ s/\n+$//; |
3607 | $tip =~ s/\n+$//; |
3401 | |
3608 | |
3402 | $self->add (new CFPlus::UI::Label |
3609 | $self->add (new DC::UI::Label |
3403 | markup => $tooltip, |
3610 | markup => $tip, |
3404 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3611 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3405 | fontsize => 0.8, |
3612 | fontsize => 0.8, |
3406 | style => 1, # FLAG_INVERSE |
3613 | style => 1, # FLAG_INVERSE |
3407 | ellipsise => 0, |
3614 | ellipsise => 0, |
3408 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3615 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
… | |
… | |
3446 | } |
3653 | } |
3447 | |
3654 | |
3448 | sub _draw { |
3655 | sub _draw { |
3449 | my ($self) = @_; |
3656 | my ($self) = @_; |
3450 | |
3657 | |
3451 | glTranslate 0.375, 0.375; |
|
|
3452 | |
|
|
3453 | my ($w, $h) = @$self{qw(w h)}; |
3658 | my ($w, $h) = @$self{qw(w h)}; |
3454 | |
3659 | |
3455 | glColor 1, 0.8, 0.4; |
3660 | glColor 1, 0.8, 0.4; |
3456 | glBegin GL_QUADS; |
3661 | glRect 0, 0, $w, $h; |
3457 | glVertex 0 , 0; |
|
|
3458 | glVertex 0 , $h; |
|
|
3459 | glVertex $w, $h; |
|
|
3460 | glVertex $w, 0; |
|
|
3461 | glEnd; |
|
|
3462 | |
3662 | |
3463 | glColor 0, 0, 0; |
3663 | glColor 0, 0, 0; |
3464 | glBegin GL_LINE_LOOP; |
3664 | glRect_lineloop .5, .5, $w + .5, $h + .5; |
3465 | glVertex 0 , 0; |
|
|
3466 | glVertex 0 , $h; |
|
|
3467 | glVertex $w, $h; |
|
|
3468 | glVertex $w, 0; |
|
|
3469 | glEnd; |
|
|
3470 | |
3665 | |
3471 | glTranslate 2 - 0.375, 2 - 0.375; |
3666 | glTranslate 2, 2; |
3472 | |
3667 | |
3473 | $self->SUPER::_draw; |
3668 | $self->SUPER::_draw; |
3474 | } |
3669 | } |
3475 | |
3670 | |
3476 | ############################################################################# |
3671 | ############################################################################# |
3477 | |
3672 | |
3478 | package CFPlus::UI::Face; |
3673 | package DC::UI::Face; |
3479 | |
3674 | |
3480 | our @ISA = CFPlus::UI::DrawBG::; |
3675 | our @ISA = DC::UI::DrawBG::; |
3481 | |
3676 | |
3482 | use CFPlus::OpenGL; |
3677 | use DC::OpenGL; |
3483 | |
3678 | |
3484 | sub new { |
3679 | sub new { |
3485 | my $class = shift; |
3680 | my $class = shift; |
3486 | |
3681 | |
3487 | my $self = $class->SUPER::new ( |
3682 | my $self = $class->SUPER::new ( |
… | |
… | |
3491 | can_events => 0, |
3686 | can_events => 0, |
3492 | @_, |
3687 | @_, |
3493 | ); |
3688 | ); |
3494 | |
3689 | |
3495 | if ($self->{anim} && $self->{animspeed}) { |
3690 | if ($self->{anim} && $self->{animspeed}) { |
3496 | CFPlus::weaken (my $widget = $self); |
3691 | DC::weaken (my $widget = $self); |
3497 | |
3692 | |
3498 | $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; |
3693 | $self->{animspeed} = List::Util::max 0.05, $self->{animspeed}; |
3499 | $widget->{anim_start} = $self->{animspeed} * Event::time / $self->{animspeed}; |
3694 | $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub { |
3500 | $self->{timer} = Event->timer ( |
|
|
3501 | parked => 1, |
|
|
3502 | cb => sub { |
|
|
3503 | return unless $::CONN; |
3695 | return unless $::CONN; |
3504 | |
3696 | |
3505 | if (!$widget) {#d# |
3697 | my $w = $widget |
3506 | warn "animation widget timer active but widgetd estroyed\n";#d# |
3698 | or return; |
3507 | warn $_[0]->w->active;#d# |
|
|
3508 | }#d# |
|
|
3509 | |
3699 | |
3510 | ++$widget->{frame}; |
3700 | ++$w->{frame}; |
3511 | $widget->update_face; |
3701 | $w->update_face; |
|
|
3702 | |
|
|
3703 | # somehow, $widget can go away |
3512 | $widget->update; |
3704 | $w->update; |
3513 | |
|
|
3514 | $widget->update_timer; |
3705 | $w->update_timer; |
3515 | }, |
|
|
3516 | ); |
3706 | }; |
3517 | |
3707 | |
3518 | $self->update_face; |
3708 | $self->update_face; |
3519 | $self->update_timer; |
3709 | $self->update_timer; |
3520 | } |
3710 | } |
3521 | |
3711 | |
… | |
… | |
3526 | my ($self) = @_; |
3716 | my ($self) = @_; |
3527 | |
3717 | |
3528 | return unless $self->{timer}; |
3718 | return unless $self->{timer}; |
3529 | |
3719 | |
3530 | if ($self->{visible}) { |
3720 | if ($self->{visible}) { |
3531 | $self->{timer}->at ( |
|
|
3532 | $self->{anim_start} |
|
|
3533 | + $self->{animspeed} |
|
|
3534 | * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} |
|
|
3535 | ); |
|
|
3536 | $self->{timer}->start; |
3721 | $self->{timer}->start; |
3537 | } else { |
3722 | } else { |
3538 | $self->{timer}->stop; |
3723 | $self->{timer}->stop; |
3539 | } |
3724 | } |
3540 | } |
3725 | } |
3541 | |
3726 | |
3542 | sub update_face { |
3727 | sub update_face { |
3543 | my ($self) = @_; |
3728 | my ($self) = @_; |
3544 | |
3729 | |
3545 | return unless $::CONN; |
|
|
3546 | |
|
|
3547 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
|
|
3548 | if ($anim && @$anim) { |
|
|
3549 | delete $self->{wait_face}; |
|
|
3550 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
|
|
3551 | } |
|
|
3552 | } |
|
|
3553 | } |
|
|
3554 | |
|
|
3555 | sub size_request { |
|
|
3556 | my ($self) = @_; |
|
|
3557 | |
|
|
3558 | if ($::CONN) { |
3730 | if ($::CONN) { |
3559 | if (my $faceid = $::CONN->{faceid}[$self->{face}]) { |
3731 | if (my $anim = $::CONN->{anim}[$self->{anim}]) { |
3560 | if (my $tex = $::CONN->{texture}[$faceid]) { |
3732 | if ($anim && @$anim) { |
3561 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
3733 | $self->{face} = $anim->[ $self->{frame} % @$anim ]; |
3562 | } else { |
3734 | delete $self->{face_change_cb}; |
3563 | $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { |
3735 | |
3564 | $self->realloc; |
3736 | if (my $tex = $self->{tex} = $::CONN->{texture}[ $::CONN->{face}[$self->{face}]{id} ]) { |
|
|
3737 | unless ($tex->{name} || $tex->{loading}) { |
|
|
3738 | $tex->upload (sub { $self->reconfigure }); |
|
|
3739 | } |
3565 | }); |
3740 | } |
3566 | } |
3741 | } |
3567 | } |
3742 | } |
3568 | } |
3743 | } |
|
|
3744 | } |
|
|
3745 | |
|
|
3746 | sub size_request { |
|
|
3747 | my ($self) = @_; |
|
|
3748 | |
|
|
3749 | if ($::CONN) { |
|
|
3750 | if (my $faceid = $::CONN->{face}[$self->{face}]{id}) { |
|
|
3751 | if (my $tex = $self->{tex} = $::CONN->{texture}[$faceid]) { |
|
|
3752 | if ($tex->{name}) { |
|
|
3753 | return ($self->{size_w} || $tex->{w}, $self->{size_h} || $tex->{h}); |
|
|
3754 | } elsif (!$tex->{loading}) { |
|
|
3755 | $tex->upload (sub { $self->reconfigure }); |
|
|
3756 | } |
|
|
3757 | } |
|
|
3758 | |
|
|
3759 | $self->{face_change_cb} ||= $::CONN->on_face_change ($self->{face}, sub { $self->reconfigure }); |
|
|
3760 | } |
|
|
3761 | } |
3569 | |
3762 | |
3570 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3763 | ($self->{size_w} || 8, $self->{size_h} || 8) |
3571 | } |
3764 | } |
3572 | |
3765 | |
3573 | sub update { |
3766 | sub update { |
… | |
… | |
3587 | } |
3780 | } |
3588 | |
3781 | |
3589 | sub _draw { |
3782 | sub _draw { |
3590 | my ($self) = @_; |
3783 | my ($self) = @_; |
3591 | |
3784 | |
3592 | return unless $::CONN; |
|
|
3593 | |
|
|
3594 | $self->SUPER::_draw; |
3785 | $self->SUPER::_draw; |
3595 | |
3786 | |
3596 | my $faceid = $::CONN->{faceid}[$self->{face}] |
3787 | if (my $tex = $self->{tex}) { |
3597 | or return; |
|
|
3598 | |
|
|
3599 | my $tex = $::CONN->{texture}[$faceid]; |
|
|
3600 | |
|
|
3601 | if ($tex) { |
|
|
3602 | glEnable GL_TEXTURE_2D; |
3788 | glEnable GL_TEXTURE_2D; |
3603 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3789 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3604 | glColor 0, 0, 0, 1; |
3790 | glColor 0, 0, 0, 1; |
3605 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3791 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3606 | glDisable GL_TEXTURE_2D; |
3792 | glDisable GL_TEXTURE_2D; |
… | |
… | |
3616 | $self->SUPER::destroy; |
3802 | $self->SUPER::destroy; |
3617 | } |
3803 | } |
3618 | |
3804 | |
3619 | ############################################################################# |
3805 | ############################################################################# |
3620 | |
3806 | |
3621 | package CFPlus::UI::Buttonbar; |
3807 | package DC::UI::Buttonbar; |
3622 | |
3808 | |
3623 | our @ISA = CFPlus::UI::HBox::; |
3809 | our @ISA = DC::UI::HBox::; |
3624 | |
3810 | |
3625 | # TODO: should actually wrap buttons and other goodies. |
3811 | # TODO: should actually wrap buttons and other goodies. |
3626 | |
3812 | |
3627 | ############################################################################# |
3813 | ############################################################################# |
3628 | |
3814 | |
3629 | package CFPlus::UI::Menu; |
3815 | package DC::UI::Menu; |
3630 | |
3816 | |
3631 | our @ISA = CFPlus::UI::Toplevel::; |
3817 | our @ISA = DC::UI::Toplevel::; |
3632 | |
3818 | |
3633 | use CFPlus::OpenGL; |
3819 | use DC::OpenGL; |
3634 | |
3820 | |
3635 | sub new { |
3821 | sub new { |
3636 | my $class = shift; |
3822 | my $class = shift; |
3637 | |
3823 | |
3638 | my $self = $class->SUPER::new ( |
3824 | my $self = $class->SUPER::new ( |
3639 | items => [], |
3825 | items => [], |
3640 | z => 100, |
3826 | z => 100, |
3641 | @_, |
3827 | @_, |
3642 | ); |
3828 | ); |
3643 | |
3829 | |
3644 | $self->add ($self->{vbox} = new CFPlus::UI::VBox); |
3830 | $self->add ($self->{vbox} = new DC::UI::VBox); |
3645 | |
3831 | |
3646 | for my $item (@{ $self->{items} }) { |
3832 | for my $item (@{ $self->{items} }) { |
3647 | my ($widget, $cb, $tooltip) = @$item; |
3833 | my ($widget, $cb, $tooltip) = @$item; |
3648 | |
3834 | |
3649 | # handle various types of items, only text for now |
3835 | # handle various types of items, only text for now |
3650 | if (!ref $widget) { |
3836 | if (!ref $widget) { |
3651 | if ($widget =~ /\t/) { |
3837 | if ($widget =~ /\t/) { |
3652 | my ($left, $right) = split /\t/, $widget, 2; |
3838 | my ($left, $right) = split /\t/, $widget, 2; |
3653 | |
3839 | |
3654 | $widget = new CFPlus::UI::HBox |
3840 | $widget = new DC::UI::HBox |
3655 | can_hover => 1, |
3841 | can_hover => 1, |
3656 | can_events => 1, |
3842 | can_events => 1, |
3657 | tooltip => $tooltip, |
3843 | tooltip => $tooltip, |
3658 | children => [ |
3844 | children => [ |
3659 | (new CFPlus::UI::Label markup => $left, expand => 1), |
3845 | (new DC::UI::Label markup => $left, expand => 1), |
3660 | (new CFPlus::UI::Label markup => $right, align => +1), |
3846 | (new DC::UI::Label markup => $right, align => +1), |
3661 | ], |
3847 | ], |
3662 | ; |
3848 | ; |
3663 | |
3849 | |
3664 | } else { |
3850 | } else { |
3665 | $widget = new CFPlus::UI::Label |
3851 | $widget = new DC::UI::Label |
3666 | can_hover => 1, |
3852 | can_hover => 1, |
3667 | can_events => 1, |
3853 | can_events => 1, |
3668 | markup => $widget, |
3854 | markup => $widget, |
3669 | tooltip => $tooltip; |
3855 | tooltip => $tooltip; |
3670 | } |
3856 | } |
… | |
… | |
3718 | 1 |
3904 | 1 |
3719 | } |
3905 | } |
3720 | |
3906 | |
3721 | ############################################################################# |
3907 | ############################################################################# |
3722 | |
3908 | |
3723 | package CFPlus::UI::Multiplexer; |
3909 | package DC::UI::Multiplexer; |
3724 | |
3910 | |
3725 | our @ISA = CFPlus::UI::Container::; |
3911 | our @ISA = DC::UI::Container::; |
3726 | |
3912 | |
3727 | sub new { |
3913 | sub new { |
3728 | my $class = shift; |
3914 | my $class = shift; |
3729 | |
3915 | |
3730 | my $self = $class->SUPER::new ( |
3916 | my $self = $class->SUPER::new ( |
… | |
… | |
3791 | $self->{current}->draw; |
3977 | $self->{current}->draw; |
3792 | } |
3978 | } |
3793 | |
3979 | |
3794 | ############################################################################# |
3980 | ############################################################################# |
3795 | |
3981 | |
3796 | package CFPlus::UI::Notebook; |
3982 | package DC::UI::Notebook; |
3797 | |
3983 | |
|
|
3984 | use DC::OpenGL; |
|
|
3985 | |
3798 | our @ISA = CFPlus::UI::VBox::; |
3986 | our @ISA = DC::UI::VBox::; |
3799 | |
3987 | |
3800 | sub new { |
3988 | sub new { |
3801 | my $class = shift; |
3989 | my $class = shift; |
3802 | |
3990 | |
3803 | my $self = $class->SUPER::new ( |
3991 | my $self = $class->SUPER::new ( |
3804 | buttonbar => (new CFPlus::UI::Buttonbar), |
3992 | buttonbar => (new DC::UI::Buttonbar), |
3805 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
3993 | multiplexer => (new DC::UI::Multiplexer expand => 1), |
|
|
3994 | active_outline => [.7, .7, 0.2], |
3806 | # filter => # will be put between multiplexer and $self |
3995 | # filter => # will be put between multiplexer and $self |
3807 | @_, |
3996 | @_, |
3808 | ); |
3997 | ); |
3809 | |
3998 | |
3810 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
3999 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
3811 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
4000 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
3812 | |
4001 | |
|
|
4002 | { |
|
|
4003 | Scalar::Util::weaken (my $wself = $self); |
|
|
4004 | |
|
|
4005 | $self->{multiplexer}->connect (c_add => sub { |
|
|
4006 | my ($mplex, $widgets) = @_; |
|
|
4007 | |
|
|
4008 | for my $child (@$widgets) { |
|
|
4009 | Scalar::Util::weaken $child; |
|
|
4010 | $child->{c_tab_} ||= do { |
|
|
4011 | my $tab = |
|
|
4012 | (UNIVERSAL::isa $child->{c_tab}, "DC::UI::Base") |
|
|
4013 | ? $child->{c_tab} |
|
|
4014 | : new DC::UI::Button markup => $child->{c_tab}[0], tooltip => $child->{c_tab}[1]; |
|
|
4015 | |
|
|
4016 | $tab->connect (activate => sub { |
|
|
4017 | $wself->set_current_page ($child); |
|
|
4018 | }); |
|
|
4019 | |
|
|
4020 | $tab |
|
|
4021 | }; |
|
|
4022 | |
|
|
4023 | $self->{buttonbar}->add ($child->{c_tab_}); |
|
|
4024 | } |
|
|
4025 | }); |
|
|
4026 | |
|
|
4027 | $self->{multiplexer}->connect (c_remove => sub { |
|
|
4028 | my ($mplex, $widgets) = @_; |
|
|
4029 | |
|
|
4030 | for my $child (@$widgets) { |
|
|
4031 | $wself->{buttonbar}->remove ($child->{c_tab_}); |
|
|
4032 | } |
|
|
4033 | }); |
|
|
4034 | } |
|
|
4035 | |
3813 | $self |
4036 | $self |
3814 | } |
4037 | } |
3815 | |
4038 | |
3816 | sub add { |
4039 | sub add { |
|
|
4040 | my ($self, @widgets) = @_; |
|
|
4041 | |
|
|
4042 | $self->{multiplexer}->add (@widgets) |
|
|
4043 | } |
|
|
4044 | |
|
|
4045 | sub remove { |
|
|
4046 | my ($self, @widgets) = @_; |
|
|
4047 | |
|
|
4048 | $self->{multiplexer}->remove (@widgets) |
|
|
4049 | } |
|
|
4050 | |
|
|
4051 | sub pages { |
|
|
4052 | my ($self) = @_; |
|
|
4053 | $self->{multiplexer}->children |
|
|
4054 | } |
|
|
4055 | |
|
|
4056 | sub add_tab { |
3817 | my ($self, $title, $widget, $tooltip) = @_; |
4057 | my ($self, $title, $widget, $tooltip) = @_; |
3818 | |
4058 | |
3819 | CFPlus::weaken $self; |
4059 | $title = [$title, $tooltip] unless ref $title; |
|
|
4060 | $widget->{c_tab} = $title; |
3820 | |
4061 | |
3821 | $self->{buttonbar}->add (new CFPlus::UI::Button |
|
|
3822 | markup => $title, |
|
|
3823 | tooltip => $tooltip, |
|
|
3824 | on_activate => sub { $self->set_current_page ($widget) }, |
|
|
3825 | ); |
|
|
3826 | |
|
|
3827 | $self->{multiplexer}->add ($widget); |
4062 | $self->add ($widget); |
3828 | } |
4063 | } |
3829 | |
4064 | |
3830 | sub get_current_page { |
4065 | sub get_current_page { |
3831 | my ($self) = @_; |
4066 | my ($self) = @_; |
3832 | |
4067 | |
… | |
… | |
3838 | |
4073 | |
3839 | $self->{multiplexer}->set_current_page ($page); |
4074 | $self->{multiplexer}->set_current_page ($page); |
3840 | $self->emit (page_changed => $self->{multiplexer}{current}); |
4075 | $self->emit (page_changed => $self->{multiplexer}{current}); |
3841 | } |
4076 | } |
3842 | |
4077 | |
|
|
4078 | sub _draw { |
|
|
4079 | my ($self) = @_; |
|
|
4080 | |
|
|
4081 | $self->SUPER::_draw (); |
|
|
4082 | |
|
|
4083 | if (my $cur = $self->{multiplexer}{current}) { |
|
|
4084 | if ($cur = $cur->{c_tab_}) { |
|
|
4085 | glTranslate $self->{buttonbar}{x} + $cur->{x}, |
|
|
4086 | $self->{buttonbar}{y} + $cur->{y}; |
|
|
4087 | glLineWidth 3; |
|
|
4088 | #glEnable GL_BLEND; |
|
|
4089 | #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
4090 | glColor @{$self->{active_outline}}; |
|
|
4091 | glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5; |
|
|
4092 | glLineWidth 1; |
|
|
4093 | #glDisable GL_BLEND; |
|
|
4094 | } |
|
|
4095 | } |
|
|
4096 | } |
|
|
4097 | |
3843 | ############################################################################# |
4098 | ############################################################################# |
3844 | |
4099 | |
3845 | package CFPlus::UI::Selector; |
4100 | package DC::UI::Selector; |
3846 | |
4101 | |
3847 | use utf8; |
4102 | use utf8; |
3848 | |
4103 | |
3849 | our @ISA = CFPlus::UI::Button::; |
4104 | our @ISA = DC::UI::Button::; |
3850 | |
4105 | |
3851 | sub new { |
4106 | sub new { |
3852 | my $class = shift; |
4107 | my $class = shift; |
3853 | |
4108 | |
3854 | my $self = $class->SUPER::new ( |
4109 | my $self = $class->SUPER::new ( |
… | |
… | |
3871 | my ($value, $title, $tooltip) = @$_; |
4126 | my ($value, $title, $tooltip) = @$_; |
3872 | |
4127 | |
3873 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
4128 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
3874 | } |
4129 | } |
3875 | |
4130 | |
3876 | CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); |
4131 | DC::UI::Menu->new (items => \@menu_items)->popup ($ev); |
3877 | } |
4132 | } |
3878 | |
4133 | |
3879 | sub _set_value { |
4134 | sub _set_value { |
3880 | my ($self, $value) = @_; |
4135 | my ($self, $value) = @_; |
3881 | |
4136 | |
3882 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} } |
4137 | my ($item) = grep $_->[0] eq $value, @{ $self->{options} }; |
|
|
4138 | $item ||= $self->{options}[0] |
3883 | or return; |
4139 | or return; |
3884 | |
4140 | |
3885 | $self->{value} = $item->[0]; |
4141 | $self->{value} = $item->[0]; |
3886 | $self->set_markup ("$item->[1] ⇓"); |
4142 | $self->set_markup ("$item->[1] ⇓"); |
3887 | $self->set_tooltip ($item->[2]); |
4143 | # $self->set_tooltip ($item->[2]); |
3888 | } |
4144 | } |
3889 | |
4145 | |
3890 | sub set_value { |
4146 | sub set_value { |
3891 | my ($self, $value) = @_; |
4147 | my ($self, $value) = @_; |
3892 | |
4148 | |
… | |
… | |
3894 | |
4150 | |
3895 | $self->_set_value ($value); |
4151 | $self->_set_value ($value); |
3896 | $self->emit (changed => $value); |
4152 | $self->emit (changed => $value); |
3897 | } |
4153 | } |
3898 | |
4154 | |
|
|
4155 | sub set_options { |
|
|
4156 | my ($self, $options) = @_; |
|
|
4157 | |
|
|
4158 | $self->{options} = $options; |
|
|
4159 | $self->_set_value ($self->{value}); |
|
|
4160 | } |
|
|
4161 | |
3899 | ############################################################################# |
4162 | ############################################################################# |
3900 | |
4163 | |
3901 | package CFPlus::UI::Statusbox; |
4164 | package DC::UI::Statusbox; |
3902 | |
4165 | |
3903 | our @ISA = CFPlus::UI::VBox::; |
4166 | our @ISA = DC::UI::VBox::; |
3904 | |
4167 | |
3905 | sub new { |
4168 | sub new { |
3906 | my $class = shift; |
4169 | my $class = shift; |
3907 | |
4170 | |
3908 | my $self = $class->SUPER::new ( |
4171 | my $self = $class->SUPER::new ( |
3909 | fontsize => 0.8, |
4172 | fontsize => 0.8, |
3910 | @_, |
4173 | @_, |
3911 | ); |
4174 | ); |
3912 | |
4175 | |
3913 | CFPlus::weaken (my $this = $self); |
4176 | DC::weaken (my $this = $self); |
3914 | |
4177 | |
3915 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
4178 | $self->{timer} = EV::timer 1, 1, sub { $this->reorder }; |
3916 | |
4179 | |
3917 | $self |
4180 | $self |
3918 | } |
4181 | } |
3919 | |
4182 | |
3920 | sub reorder { |
4183 | sub reorder { |
3921 | my ($self) = @_; |
4184 | my ($self) = @_; |
3922 | my $NOW = Time::HiRes::time; |
4185 | my $NOW = Time::HiRes::time; |
3923 | |
4186 | |
3924 | # freeze display when hovering over any label |
4187 | # freeze display when hovering over any label |
3925 | return if $CFPlus::UI::TOOLTIP->{owner} |
4188 | return if $DC::UI::TOOLTIP->{owner} |
3926 | && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, |
4189 | && grep $DC::UI::TOOLTIP->{owner} == $_->{label}, |
3927 | values %{ $self->{item} }; |
4190 | values %{ $self->{item} }; |
3928 | |
4191 | |
3929 | while (my ($k, $v) = each %{ $self->{item} }) { |
4192 | while (my ($k, $v) = each %{ $self->{item} }) { |
3930 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
4193 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
3931 | } |
4194 | } |
|
|
4195 | |
|
|
4196 | $self->{timer}->set (1, 1); |
3932 | |
4197 | |
3933 | my @widgets; |
4198 | my @widgets; |
3934 | |
4199 | |
3935 | my @items = sort { |
4200 | my @items = sort { |
3936 | $a->{pri} <=> $b->{pri} |
4201 | $a->{pri} <=> $b->{pri} |
3937 | or $b->{id} <=> $a->{id} |
4202 | or $b->{id} <=> $a->{id} |
3938 | } values %{ $self->{item} }; |
4203 | } values %{ $self->{item} }; |
3939 | |
|
|
3940 | $self->{timer}->interval (1); |
|
|
3941 | |
4204 | |
3942 | my $count = 10 + 1; |
4205 | my $count = 10 + 1; |
3943 | for my $item (@items) { |
4206 | for my $item (@items) { |
3944 | last unless --$count; |
4207 | last unless --$count; |
3945 | |
4208 | |
… | |
… | |
3952 | for ($short) { |
4215 | for ($short) { |
3953 | s/^\s+//; |
4216 | s/^\s+//; |
3954 | s/\s+/ /g; |
4217 | s/\s+/ /g; |
3955 | } |
4218 | } |
3956 | |
4219 | |
3957 | new CFPlus::UI::Label |
4220 | new DC::UI::Label |
3958 | markup => $short, |
4221 | markup => $short, |
3959 | tooltip => $item->{tooltip}, |
4222 | tooltip => $item->{tooltip}, |
3960 | tooltip_font => $::FONT_PROP, |
4223 | tooltip_font => $::FONT_PROP, |
3961 | tooltip_width => 0.67, |
4224 | tooltip_width => 0.67, |
3962 | fontsize => $item->{fontsize} || $self->{fontsize}, |
4225 | fontsize => $item->{fontsize} || $self->{fontsize}, |
… | |
… | |
3969 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
4232 | if ((my $diff = $item->{timeout} - $NOW) < 2) { |
3970 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
4233 | $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; |
3971 | $label->update; |
4234 | $label->update; |
3972 | $label->set_max_size (undef, $label->{req_h} * $diff) |
4235 | $label->set_max_size (undef, $label->{req_h} * $diff) |
3973 | if $diff < 1; |
4236 | if $diff < 1; |
3974 | $self->{timer}->interval (1/30); |
4237 | $self->{timer}->set (1/30, 1/30); |
3975 | } else { |
4238 | } else { |
3976 | $label->{fg}[3] = $item->{fg}[3] || 1; |
4239 | $label->{fg}[3] = $item->{fg}[3] || 1; |
3977 | } |
4240 | } |
3978 | |
4241 | |
3979 | push @widgets, $label; |
4242 | push @widgets, $label; |
… | |
… | |
4041 | $self->SUPER::destroy; |
4304 | $self->SUPER::destroy; |
4042 | } |
4305 | } |
4043 | |
4306 | |
4044 | ############################################################################# |
4307 | ############################################################################# |
4045 | |
4308 | |
4046 | package CFPlus::UI::Root; |
4309 | package DC::UI::Root; |
4047 | |
4310 | |
4048 | our @ISA = CFPlus::UI::Container::; |
4311 | our @ISA = DC::UI::Container::; |
4049 | |
4312 | |
4050 | use List::Util qw(min max); |
4313 | use List::Util qw(min max); |
4051 | |
4314 | |
4052 | use CFPlus::OpenGL; |
4315 | use DC::OpenGL; |
4053 | |
4316 | |
4054 | sub new { |
4317 | sub new { |
4055 | my $class = shift; |
4318 | my $class = shift; |
4056 | |
4319 | |
4057 | my $self = $class->SUPER::new ( |
4320 | my $self = $class->SUPER::new ( |
4058 | visible => 1, |
4321 | visible => 1, |
4059 | @_, |
4322 | @_, |
4060 | ); |
4323 | ); |
4061 | |
4324 | |
4062 | CFPlus::weaken ($self->{root} = $self); |
4325 | DC::weaken ($self->{root} = $self); |
4063 | |
4326 | |
4064 | $self |
4327 | $self |
4065 | } |
4328 | } |
4066 | |
4329 | |
4067 | sub size_request { |
4330 | sub size_request { |
… | |
… | |
4115 | } |
4378 | } |
4116 | |
4379 | |
4117 | sub update { |
4380 | sub update { |
4118 | my ($self) = @_; |
4381 | my ($self) = @_; |
4119 | |
4382 | |
4120 | $::WANT_REFRESH++; |
4383 | $::WANT_REFRESH = 1; |
4121 | } |
4384 | } |
4122 | |
4385 | |
4123 | sub add { |
4386 | sub add { |
4124 | my ($self, @children) = @_; |
4387 | my ($self, @children) = @_; |
4125 | |
4388 | |
… | |
… | |
4162 | while ($self->{refresh_hook}) { |
4425 | while ($self->{refresh_hook}) { |
4163 | $_->() |
4426 | $_->() |
4164 | for values %{delete $self->{refresh_hook}}; |
4427 | for values %{delete $self->{refresh_hook}}; |
4165 | } |
4428 | } |
4166 | |
4429 | |
4167 | if ($self->{realloc}) { |
4430 | while ($self->{realloc}) { |
4168 | my %queue; |
4431 | my %queue; |
4169 | my @queue; |
4432 | my @queue; |
4170 | my $widget; |
4433 | my $widget; |
4171 | |
4434 | |
4172 | outer: |
4435 | outer: |
… | |
… | |
4219 | } |
4482 | } |
4220 | } |
4483 | } |
4221 | |
4484 | |
4222 | delete $self->{realloc}{$widget+0}; |
4485 | delete $self->{realloc}{$widget+0}; |
4223 | } |
4486 | } |
4224 | } |
|
|
4225 | |
4487 | |
4226 | while (my $size_alloc = delete $self->{size_alloc}) { |
4488 | while (my $size_alloc = delete $self->{size_alloc}) { |
4227 | my @queue = sort { $b->{visible} <=> $a->{visible} } |
4489 | my @queue = sort { $a->{visible} <=> $b->{visible} } |
4228 | values %$size_alloc; |
4490 | values %$size_alloc; |
4229 | |
4491 | |
4230 | while () { |
4492 | while () { |
4231 | my $widget = pop @queue || last; |
4493 | my $widget = pop @queue || last; |
4232 | |
4494 | |
4233 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
4495 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
4234 | |
4496 | |
4235 | $w = 0 if $w < 0; |
|
|
4236 | $h = 0 if $h < 0; |
|
|
4237 | |
|
|
4238 | $w = max $widget->{min_w}, $w; |
4497 | $w = max $widget->{min_w}, $w; |
4239 | $h = max $widget->{min_h}, $h; |
4498 | $h = max $widget->{min_h}, $h; |
4240 | |
4499 | |
4241 | # $w = min $self->{w} - $widget->{x}, $w if $self->{w}; |
4500 | # $w = min $self->{w} - $widget->{x}, $w if $self->{w}; |
4242 | # $h = min $self->{h} - $widget->{y}, $h if $self->{h}; |
4501 | # $h = min $self->{h} - $widget->{y}, $h if $self->{h}; |
4243 | |
4502 | |
4244 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
4503 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
4245 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
4504 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
4246 | |
4505 | |
4247 | $w = int $w + 0.5; |
4506 | $w = int $w + 0.5; |
4248 | $h = int $h + 0.5; |
4507 | $h = int $h + 0.5; |
4249 | |
4508 | |
4250 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
4509 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
4251 | $widget->{old_w} = $widget->{w}; |
4510 | $widget->{old_w} = $widget->{w}; |
4252 | $widget->{old_h} = $widget->{h}; |
4511 | $widget->{old_h} = $widget->{h}; |
4253 | |
4512 | |
4254 | $widget->{w} = $w; |
4513 | $widget->{w} = $w; |
4255 | $widget->{h} = $h; |
4514 | $widget->{h} = $h; |
4256 | |
4515 | |
4257 | $widget->emit (size_allocate => $w, $h); |
4516 | $widget->emit (size_allocate => $w, $h); |
|
|
4517 | } |
4258 | } |
4518 | } |
4259 | } |
4519 | } |
4260 | } |
4520 | } |
4261 | |
4521 | |
4262 | while ($self->{post_alloc_hook}) { |
4522 | while ($self->{post_alloc_hook}) { |
4263 | $_->() |
4523 | $_->() |
4264 | for values %{delete $self->{post_alloc_hook}}; |
4524 | for values %{delete $self->{post_alloc_hook}}; |
4265 | } |
4525 | } |
4266 | |
|
|
4267 | |
4526 | |
4268 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
4527 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
4269 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
4528 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
4270 | glClear GL_COLOR_BUFFER_BIT; |
4529 | glClear GL_COLOR_BUFFER_BIT; |
4271 | |
4530 | |
… | |
… | |
4274 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4533 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
4275 | glMatrixMode GL_MODELVIEW; |
4534 | glMatrixMode GL_MODELVIEW; |
4276 | glLoadIdentity; |
4535 | glLoadIdentity; |
4277 | |
4536 | |
4278 | { |
4537 | { |
4279 | package CFPlus::UI::Base; |
4538 | package DC::UI::Base; |
4280 | |
4539 | |
4281 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4540 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
4282 | (0, 0, $self->{w}, $self->{h}); |
4541 | (0, 0, $self->{w}, $self->{h}); |
4283 | |
4542 | |
4284 | $self->_draw; |
4543 | $self->_draw; |
4285 | } |
4544 | } |
4286 | } |
4545 | } |
4287 | |
4546 | |
4288 | ############################################################################# |
4547 | ############################################################################# |
4289 | |
4548 | |
4290 | package CFPlus::UI; |
4549 | package DC::UI; |
4291 | |
4550 | |
4292 | $ROOT = new CFPlus::UI::Root; |
4551 | $ROOT = new DC::UI::Root; |
4293 | $TOOLTIP = new CFPlus::UI::Tooltip z => 900; |
4552 | $TOOLTIP = new DC::UI::Tooltip z => 900; |
4294 | |
4553 | |
4295 | 1 |
4554 | 1 |
4296 | |
4555 | |