ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.370
Committed: Sun Jul 15 21:02:20 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.369: +0 -2 lines
Log Message:
implement very simplistic mouse motion event compression,
fixing a longstanding mysterious problem.

File Contents

# Content
1 package CFPlus::UI;
2
3 use utf8;
4 use strict;
5
6 use List::Util ();
7 use Event;
8
9 use CFPlus;
10 use CFPlus::Pod;
11 use CFPlus::Texture;
12
13 our ($FOCUS, $HOVER, $GRAB); # various widgets
14
15 our $LAYOUT;
16 our $ROOT;
17 our $TOOLTIP;
18 our $BUTTON_STATE;
19
20 our %WIDGET; # all widgets, weak-referenced
21
22 our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub {
23 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
28 $TOOLTIP->hide;
29
30 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
32
33 return if $ENV{CFPLUS_DEBUG} & 8;
34
35 my $tip = $widget->{tooltip};
36
37 $tip = $tip->($widget) if CODE:: eq ref $tip;
38
39 $TOOLTIP->set_tooltip_from ($widget);
40 $TOOLTIP->show;
41 }
42
43 return;
44 }
45 }
46 }
47
48 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
50 delete $TOOLTIP->{owner};
51 });
52
53 sub get_layout {
54 my $layout;
55
56 for (grep { $_->{name} } values %WIDGET) {
57 my $win = $layout->{$_->{name}} = { };
58
59 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
60 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
61 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
62 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
63
64 $win->{show} = $_->{visible} && $_->{is_toplevel};
65 }
66
67 $layout
68 }
69
70 sub set_layout {
71 my ($layout) = @_;
72
73 $LAYOUT = $layout;
74 }
75
76 # class methods for events
77 sub feed_sdl_key_down_event {
78 $FOCUS->emit (key_down => $_[0])
79 if $FOCUS;
80 }
81
82 sub feed_sdl_key_up_event {
83 $FOCUS->emit (key_up => $_[0])
84 if $FOCUS;
85 }
86
87 sub check_hover {
88 my ($widget) = @_;
89
90 if ($widget != $HOVER) {
91 my $hover = $HOVER; $HOVER = $widget;
92
93 $hover->update if $hover && $hover->{can_hover};
94 $HOVER->update if $HOVER && $HOVER->{can_hover};
95
96 $TOOLTIP_WATCHER->start;
97 }
98 }
99
100 sub feed_sdl_button_down_event {
101 my ($ev) = @_;
102 my ($x, $y) = ($ev->{x}, $ev->{y});
103
104 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
105
106 unless ($GRAB) {
107 my $widget = $ROOT->find_widget ($x, $y);
108
109 $GRAB = $widget;
110 $GRAB->update if $GRAB;
111
112 $TOOLTIP_WATCHER->cb->();
113 }
114
115 if ($GRAB) {
116 if ($ev->{button} == 4 || $ev->{button} == 5) {
117 # mousewheel
118 $ev->{dx} = 0;
119 $ev->{dy} = $ev->{button} * 2 - 9;
120 $GRAB->emit (mouse_wheel => $ev);
121 } else {
122 $GRAB->emit (button_down => $ev)
123 }
124 }
125 }
126
127 sub feed_sdl_button_up_event {
128 my ($ev) = @_;
129
130 my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y});
131
132 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
133
134 $GRAB->emit (button_up => $ev)
135 if $GRAB && $ev->{button} != 4 && $ev->{button} != 5;
136
137 unless ($BUTTON_STATE) {
138 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab;
140 $GRAB->update if $GRAB;
141
142 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->();
144 }
145 }
146
147 sub feed_sdl_motion_event {
148 my ($ev) = @_;
149 my ($x, $y) = ($ev->{x}, $ev->{y});
150
151 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
152
153 check_hover $widget;
154
155 $HOVER->emit (mouse_motion => $ev)
156 if $HOVER;
157 }
158
159 # convert position array to integers
160 sub harmonize {
161 my ($vals) = @_;
162
163 my $rem = 0;
164
165 for (@$vals) {
166 my $i = int $_ + $rem;
167 $rem += $_ - $i;
168 $_ = $i;
169 }
170 }
171
172 sub full_refresh {
173 # make a copy, otherwise for complains about freed values.
174 my @widgets = values %WIDGET;
175
176 $_->update
177 for @widgets;
178 }
179
180 sub reconfigure_widgets {
181 # make a copy, otherwise C<for> complains about freed values.
182 my @widgets = values %WIDGET;
183
184 $_->reconfigure
185 for @widgets;
186 }
187
188 # call when resolution changes etc.
189 sub rescale_widgets {
190 my ($sx, $sy) = @_;
191
192 for my $widget (values %WIDGET) {
193 if ($widget->{is_toplevel}) {
194 $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
195 $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
196
197 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
198 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
199 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
200 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
201 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
202 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
203
204 $widget->{x} -= int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
205 $widget->{y} -= int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
206
207 }
208 }
209
210 reconfigure_widgets;
211 }
212
213 #############################################################################
214
215 package CFPlus::UI::Event;
216
217 sub xy {
218 $_[1]->coord2local ($_[0]{x}, $_[0]{y})
219 }
220
221 #############################################################################
222
223 package CFPlus::UI::Base;
224
225 use strict;
226
227 use CFPlus::OpenGL;
228
229 sub new {
230 my $class = shift;
231
232 my $self = bless {
233 x => "center",
234 y => "center",
235 z => 0,
236 w => undef,
237 h => undef,
238 can_events => 1,
239 @_
240 }, $class;
241
242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self);
243
244 for (keys %$self) {
245 if (/^on_(.*)$/) {
246 $self->connect ($1 => delete $self->{$_});
247 }
248 }
249
250 if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) {
251 $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x};
252 $self->{y} = $layout->{y} * $CFPlus::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_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h};
255
256 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
257 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
258
259 $self->show if $layout->{show};
260 }
261
262 $self
263 }
264
265 sub destroy {
266 my ($self) = @_;
267
268 $self->hide;
269 $self->emit ("destroy");
270 %$self = ();
271 }
272
273 sub TO_JSON {
274 { __widget_ref__ => $_[0]{s_id} }
275 }
276
277 sub show {
278 my ($self) = @_;
279
280 return if $self->{parent};
281
282 $CFPlus::UI::ROOT->add ($self);
283 }
284
285 sub set_visible {
286 my ($self) = @_;
287
288 return if $self->{visible};
289
290 $self->{root} = $self->{parent}{root};
291 $self->{visible} = $self->{parent}{visible} + 1;
292
293 $self->emit (visibility_change => 1);
294
295 $self->realloc if !exists $self->{req_w};
296
297 $_->set_visible for $self->children;
298 }
299
300 sub set_invisible {
301 my ($self) = @_;
302
303 return unless $self->{visible};
304
305 $_->set_invisible for $self->children;
306
307 delete $self->{visible};
308 delete $self->{root};
309
310 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self;
312
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->()
314 if $TOOLTIP->{owner} == $self;
315
316 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0);
318 }
319
320 sub set_visibility {
321 my ($self, $visible) = @_;
322
323 return if $self->{visible} == $visible;
324
325 $visible ? $self->show
326 : $self->hide;
327 }
328
329 sub toggle_visibility {
330 my ($self) = @_;
331
332 $self->{visible}
333 ? $self->hide
334 : $self->show;
335 }
336
337 sub hide {
338 my ($self) = @_;
339
340 $self->set_invisible;
341
342 $self->{parent}->remove ($self)
343 if $self->{parent};
344 }
345
346 sub move_abs {
347 my ($self, $x, $y, $z) = @_;
348
349 $self->{x} = List::Util::max 0, List::Util::min $self->{root}{w} - $self->{w}, int $x;
350 $self->{y} = List::Util::max 0, List::Util::min $self->{root}{h} - $self->{h}, int $y;
351 $self->{z} = $z if defined $z;
352
353 $self->update;
354 }
355
356 sub set_size {
357 my ($self, $w, $h) = @_;
358
359 $self->{force_w} = $w;
360 $self->{force_h} = $h;
361
362 $self->realloc;
363 }
364
365 sub size_request {
366 require Carp;
367 Carp::confess "size_request is abstract";
368 }
369
370 sub baseline_shift {
371 0
372 }
373
374 sub configure {
375 my ($self, $x, $y, $w, $h) = @_;
376
377 if ($self->{aspect}) {
378 my ($ow, $oh) = ($w, $h);
379
380 $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect};
381 $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect};
382
383 # use alignment to adjust x, y
384
385 $x += int 0.5 * ($ow - $w);
386 $y += int 0.5 * ($oh - $h);
387 }
388
389 if ($self->{x} ne $x || $self->{y} ne $y) {
390 $self->{x} = $x;
391 $self->{y} = $y;
392 $self->update;
393 }
394
395 if ($self->{alloc_w} != $w || $self->{alloc_h} != $h) {
396 return unless $self->{visible};
397
398 $self->{alloc_w} = $w;
399 $self->{alloc_h} = $h;
400
401 $self->{root}{size_alloc}{$self+0} = $self;
402 }
403 }
404
405 sub children {
406 # nop
407 }
408
409 sub visible_children {
410 $_[0]->children
411 }
412
413 sub set_max_size {
414 my ($self, $w, $h) = @_;
415
416 $self->{max_w} = int $w if defined $w;
417 $self->{max_h} = int $h if defined $h;
418
419 $self->realloc;
420 }
421
422 sub set_tooltip {
423 my ($self, $tooltip) = @_;
424
425 $tooltip =~ s/^\s+//;
426 $tooltip =~ s/\s+$//;
427
428 return if $self->{tooltip} eq $tooltip;
429
430 $self->{tooltip} = $tooltip;
431
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->();
435 }
436 }
437
438 # translate global coordinates to local coordinate system
439 sub coord2local {
440 my ($self, $x, $y) = @_;
441
442 Carp::confess unless $self->{parent};#d#
443
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445 }
446
447 # translate local coordinates to global coordinate system
448 sub coord2global {
449 my ($self, $x, $y) = @_;
450
451 Carp::confess unless $self->{parent};#d#
452
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454 }
455
456 sub invoke_focus_in {
457 my ($self) = @_;
458
459 return if $FOCUS == $self;
460 return unless $self->{can_focus};
461
462 $FOCUS = $self;
463
464 $self->update;
465
466 0
467 }
468
469 sub invoke_focus_out {
470 my ($self) = @_;
471
472 return unless $FOCUS == $self;
473
474 undef $FOCUS;
475
476 $self->update;
477
478 $::MAPWIDGET->grab_focus #d# focus mapwidget if no other widget has focus
479 unless $FOCUS;
480
481 0
482 }
483
484 sub grab_focus {
485 my ($self) = @_;
486
487 $FOCUS->emit ("focus_out") if $FOCUS;
488 $self->emit ("focus_in");
489 }
490
491 sub invoke_mouse_motion { 0 }
492 sub invoke_button_up { 0 }
493 sub invoke_key_down { 0 }
494 sub invoke_key_up { 0 }
495 sub invoke_mouse_wheel { 0 }
496
497 sub invoke_button_down {
498 my ($self, $ev, $x, $y) = @_;
499
500 $self->grab_focus;
501
502 0
503 }
504
505 sub connect {
506 my ($self, $signal, $cb) = @_;
507
508 push @{ $self->{signal_cb}{$signal} }, $cb;
509
510 defined wantarray and CFPlus::guard {
511 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
512 @{ $self->{signal_cb}{$signal} };
513 }
514 }
515
516 sub disconnect_all {
517 my ($self, $signal) = @_;
518
519 delete $self->{signal_cb}{$signal};
520 }
521
522 my %has_coords = (
523 button_down => 1,
524 button_up => 1,
525 mouse_motion => 1,
526 mouse_wheel => 1,
527 );
528
529 sub emit {
530 my ($self, $signal, @args) = @_;
531
532 # I do not really like this solution, but I do not like duplication
533 # and needlessly verbose code, either.
534 my @append
535 = $has_coords{$signal}
536 ? $args[0]->xy ($self)
537 : ();
538
539 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
540
541 for my $cb (
542 @{$self->{signal_cb}{$signal} || []}, # before
543 ($self->can ("invoke_$signal") || sub { 1 }), # closure
544 ) {
545 return $cb->($self, @args, @append) || next;
546 }
547
548 # parent
549 $self->{parent} && $self->{parent}->emit ($signal, @args)
550 }
551
552 sub find_widget {
553 my ($self, $x, $y) = @_;
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
564 sub set_parent {
565 my ($self, $parent) = @_;
566
567 CFPlus::weaken ($self->{parent} = $parent);
568 $self->set_visible if $parent->{visible};
569 }
570
571 sub realloc {
572 my ($self) = @_;
573
574 if ($self->{visible}) {
575 return if $self->{root}{realloc}{$self+0};
576
577 $self->{root}{realloc}{$self+0} = $self;
578 $self->{root}->update;
579 } else {
580 delete $self->{req_w};
581 delete $self->{req_h};
582 }
583 }
584
585 sub update {
586 my ($self) = @_;
587
588 $self->{parent}->update
589 if $self->{parent};
590 }
591
592 sub reconfigure {
593 my ($self) = @_;
594
595 $self->realloc;
596 $self->update;
597 }
598
599 # using global variables seems a bit hacky, but passing through all drawing
600 # functions seems pointless.
601 our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn
602
603 sub draw {
604 my ($self) = @_;
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
650 sub _draw {
651 my ($self) = @_;
652
653 warn "no draw defined for $self\n";
654 }
655
656 my $cntx;#d#
657 sub DESTROY {
658 my ($self) = @_;
659
660 return if CFPlus::in_destruct;
661
662 eval { $self->destroy };
663 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
664
665 delete $WIDGET{$self+0};
666 }
667
668 #############################################################################
669
670 package CFPlus::UI::DrawBG;
671
672 our @ISA = CFPlus::UI::Base::;
673
674 use strict;
675 use CFPlus::OpenGL;
676
677 sub new {
678 my $class = shift;
679
680 # range [value, low, high, page]
681
682 $class->SUPER::new (
683 #bg => [0, 0, 0, 0.2],
684 #active_bg => [1, 1, 1, 0.5],
685 @_
686 )
687 }
688
689 sub _draw {
690 my ($self) = @_;
691
692 my $color = $FOCUS == $self && $self->{active_bg}
693 ? $self->{active_bg}
694 : $self->{bg};
695
696 if ($color && (@$color < 4 || $color->[3])) {
697 my ($w, $h) = @$self{qw(w h)};
698
699 glEnable GL_BLEND;
700 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
701 glColor_premultiply @$color;
702
703 glBegin GL_QUADS;
704 glVertex 0 , 0;
705 glVertex 0 , $h;
706 glVertex $w, $h;
707 glVertex $w, 0;
708 glEnd;
709
710 glDisable GL_BLEND;
711 }
712 }
713
714 #############################################################################
715
716 package CFPlus::UI::Empty;
717
718 our @ISA = CFPlus::UI::Base::;
719
720 sub new {
721 my ($class, %arg) = @_;
722 $class->SUPER::new (can_events => 0, %arg);
723 }
724
725 sub size_request {
726 my ($self) = @_;
727
728 ($self->{w} + 0, $self->{h} + 0)
729 }
730
731 sub draw { }
732
733 #############################################################################
734
735 package CFPlus::UI::Container;
736
737 our @ISA = CFPlus::UI::Base::;
738
739 sub new {
740 my ($class, %arg) = @_;
741
742 my $children = delete $arg{children};
743
744 my $self = $class->SUPER::new (
745 children => [],
746 can_events => 0,
747 %arg,
748 );
749
750 $self->add (@$children)
751 if $children;
752
753 $self
754 }
755
756 sub realloc {
757 my ($self) = @_;
758
759 $self->{force_realloc} = 1;
760 $self->{force_size_alloc} = 1;
761 $self->SUPER::realloc;
762 }
763
764 sub add {
765 my ($self, @widgets) = @_;
766
767 $_->set_parent ($self)
768 for @widgets;
769
770 use sort 'stable';
771
772 $self->{children} = [
773 sort { $a->{z} <=> $b->{z} }
774 @{$self->{children}}, @widgets
775 ];
776
777 $self->realloc;
778 }
779
780 sub children {
781 @{ $_[0]{children} }
782 }
783
784 sub remove {
785 my ($self, $child) = @_;
786
787 delete $child->{parent};
788 $child->hide;
789
790 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
791
792 $self->realloc;
793 }
794
795 sub clear {
796 my ($self) = @_;
797
798 my $children = delete $self->{children};
799 $self->{children} = [];
800
801 for (@$children) {
802 delete $_->{parent};
803 $_->hide;
804 }
805
806 $self->realloc;
807 }
808
809 sub find_widget {
810 my ($self, $x, $y) = @_;
811
812 $x -= $self->{x};
813 $y -= $self->{y};
814
815 my $res;
816
817 for (reverse $self->visible_children) {
818 $res = $_->find_widget ($x, $y)
819 and return $res;
820 }
821
822 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
823 }
824
825 sub _draw {
826 my ($self) = @_;
827
828 $_->draw for @{$self->{children}};
829 }
830
831 #############################################################################
832
833 package CFPlus::UI::Bin;
834
835 our @ISA = CFPlus::UI::Container::;
836
837 sub new {
838 my ($class, %arg) = @_;
839
840 my $child = (delete $arg{child}) || new CFPlus::UI::Empty::;
841
842 $class->SUPER::new (children => [$child], %arg)
843 }
844
845 sub add {
846 my ($self, $child) = @_;
847
848 $self->SUPER::remove ($_) for @{ $self->{children} };
849 $self->SUPER::add ($child);
850 }
851
852 sub remove {
853 my ($self, $widget) = @_;
854
855 $self->SUPER::remove ($widget);
856
857 $self->{children} = [new CFPlus::UI::Empty]
858 unless @{$self->{children}};
859 }
860
861 sub child { $_[0]->{children}[0] }
862
863 sub size_request {
864 $_[0]{children}[0]->size_request
865 }
866
867 sub invoke_size_allocate {
868 my ($self, $w, $h) = @_;
869
870 $self->{children}[0]->configure (0, 0, $w, $h);
871
872 1
873 }
874
875 #############################################################################
876
877 # back-buffered drawing area
878
879 package CFPlus::UI::Window;
880
881 our @ISA = CFPlus::UI::Bin::;
882
883 use CFPlus::OpenGL;
884
885 sub new {
886 my ($class, %arg) = @_;
887
888 my $self = $class->SUPER::new (%arg);
889 }
890
891 sub update {
892 my ($self) = @_;
893
894 $ROOT->on_post_alloc ($self => sub { $self->render_child });
895 $self->SUPER::update;
896 }
897
898 sub invoke_size_allocate {
899 my ($self, $w, $h) = @_;
900
901 $self->update;
902
903 $self->SUPER::invoke_size_allocate ($w, $h)
904 }
905
906 sub _render {
907 my ($self) = @_;
908
909 $self->{children}[0]->draw;
910 }
911
912 sub render_child {
913 my ($self) = @_;
914
915 $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub {
916 glClearColor 0, 0, 0, 0;
917 glClear GL_COLOR_BUFFER_BIT;
918
919 {
920 package CFPlus::UI::Base;
921
922 local ($draw_x, $draw_y, $draw_w, $draw_h) =
923 (0, 0, $self->{w}, $self->{h});
924
925 $self->_render;
926 }
927 };
928 }
929
930 sub _draw {
931 my ($self) = @_;
932
933 my $tex = $self->{texture}
934 or return;
935
936 glEnable GL_TEXTURE_2D;
937 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
938 glColor 0, 0, 0, 1;
939
940 $tex->draw_quad_alpha_premultiplied (0, 0);
941
942 glDisable GL_TEXTURE_2D;
943 }
944
945 #############################################################################
946
947 package CFPlus::UI::ViewPort;
948
949 our @ISA = CFPlus::UI::Window::;
950
951 sub new {
952 my $class = shift;
953
954 $class->SUPER::new (
955 scroll_x => 0,
956 scroll_y => 1,
957 @_,
958 )
959 }
960
961 sub size_request {
962 my ($self) = @_;
963
964 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
965
966 $w = 10 if $self->{scroll_x};
967 $h = 10 if $self->{scroll_y};
968
969 ($w, $h)
970 }
971
972 sub invoke_size_allocate {
973 my ($self, $w, $h) = @_;
974
975 my $child = $self->child;
976
977 $w = $child->{req_w} if $self->{scroll_x} && $child->{req_w};
978 $h = $child->{req_h} if $self->{scroll_y} && $child->{req_h};
979
980 $self->child->configure (0, 0, $w, $h);
981 $self->update;
982
983 1
984 }
985
986 sub set_offset {
987 my ($self, $x, $y) = @_;
988
989 $self->{view_x} = int $x;
990 $self->{view_y} = int $y;
991
992 $self->update;
993 }
994
995 # hmm, this does not work for topleft of $self... but we should not ask for that
996 sub coord2local {
997 my ($self, $x, $y) = @_;
998
999 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
1000 }
1001
1002 sub coord2global {
1003 my ($self, $x, $y) = @_;
1004
1005 $x = List::Util::min $self->{w}, $x - $self->{view_x};
1006 $y = List::Util::min $self->{h}, $y - $self->{view_y};
1007
1008 $self->SUPER::coord2global ($x, $y)
1009 }
1010
1011 sub find_widget {
1012 my ($self, $x, $y) = @_;
1013
1014 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
1015 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
1016 ) {
1017 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
1018 } else {
1019 $self->CFPlus::UI::Base::find_widget ($x, $y)
1020 }
1021 }
1022
1023 sub _render {
1024 my ($self) = @_;
1025
1026 local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x};
1027 local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y};
1028
1029 CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
1030
1031 $self->SUPER::_render;
1032 }
1033
1034 #############################################################################
1035
1036 package CFPlus::UI::ScrolledWindow;
1037
1038 our @ISA = CFPlus::UI::Table::;
1039
1040 sub new {
1041 my ($class, %arg) = @_;
1042
1043 my $child = delete $arg{child};
1044
1045 my $self;
1046
1047 my $hslider = new CFPlus::UI::Slider
1048 vertical => 0,
1049 range => [0, 0, 1, 0.01], # HACK fix
1050 on_changed => sub {
1051 $self->{hpos} = $_[1];
1052 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1053 },
1054 ;
1055
1056 my $vslider = new CFPlus::UI::Slider
1057 vertical => 1,
1058 range => [0, 0, 1, 0.01], # HACK fix
1059 on_changed => sub {
1060 $self->{vpos} = $_[1];
1061 $self->{vp}->set_offset ($self->{hpos}, $self->{vpos});
1062 },
1063 ;
1064
1065 $self = $class->SUPER::new (
1066 vp => (new CFPlus::UI::ViewPort expand => 1),
1067 can_events => 1,
1068 hslider => $hslider,
1069 vslider => $vslider,
1070 col_expand => [1, 0],
1071 row_expand => [1, 0],
1072 %arg,
1073 );
1074
1075 $self->SUPER::add (0, 0, $self->{vp});
1076 $self->SUPER::add (1, 0, $self->{vslider});
1077 $self->SUPER::add (0, 1, $self->{hslider});
1078
1079 $self->add ($child) if $child;
1080
1081 $self
1082 }
1083
1084 #TODO# update range on size_allocate depending on child
1085
1086 sub add {
1087 my ($self, $widget) = @_;
1088
1089 $self->{vp}->add ($self->{child} = $widget);
1090 }
1091
1092 sub invoke_mouse_wheel {
1093 my ($self, $ev) = @_;
1094
1095 return 0 unless $ev->{dy}; # only vertical movements
1096
1097 $self->{vslider}->emit (mouse_wheel => $ev);
1098
1099 1
1100 }
1101
1102 sub update_slider {
1103 my ($self) = @_;
1104
1105 my ($w1, $w2) = ($self->{vp}->child->{w}, $self->{vp}{w});
1106 $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]);
1107 #$self->{hslider}->set_visibility ($w1 != $w2);
1108
1109 my ($h1, $h2) = ($self->{vp}->child->{h}, $self->{vp}{h});
1110 $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]);
1111 #$self->{vslider}->set_visibility ($h1 != $h2);
1112 }
1113
1114 sub update {
1115 my ($self) = @_;
1116
1117 $self->SUPER::update;
1118 $self->update_slider;
1119 }
1120
1121 sub invoke_size_allocate {
1122 my ($self, $w, $h) = @_;
1123
1124 $self->update_slider;
1125 $self->SUPER::invoke_size_allocate ($w, $h)
1126 }
1127
1128 #############################################################################
1129
1130 package CFPlus::UI::Frame;
1131
1132 our @ISA = CFPlus::UI::Bin::;
1133
1134 use CFPlus::OpenGL;
1135
1136 sub new {
1137 my $class = shift;
1138
1139 $class->SUPER::new (
1140 bg => undef,
1141 @_,
1142 )
1143 }
1144
1145 sub _draw {
1146 my ($self) = @_;
1147
1148 if ($self->{bg}) {
1149 my ($w, $h) = @$self{qw(w h)};
1150
1151 glEnable GL_BLEND;
1152 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
1153 glColor_premultiply @{ $self->{bg} };
1154
1155 glBegin GL_QUADS;
1156 glVertex 0 , 0;
1157 glVertex 0 , $h;
1158 glVertex $w, $h;
1159 glVertex $w, 0;
1160 glEnd;
1161
1162 glDisable GL_BLEND;
1163 }
1164
1165 $self->SUPER::_draw;
1166 }
1167
1168 #############################################################################
1169
1170 package CFPlus::UI::FancyFrame;
1171
1172 our @ISA = CFPlus::UI::Bin::;
1173
1174 use CFPlus::OpenGL;
1175
1176 sub new {
1177 my ($class, %arg) = @_;
1178
1179 if ((exists $arg{label}) && !ref $arg{label}) {
1180 $arg{label} = new CFPlus::UI::Label
1181 align => 1,
1182 valign => 0,
1183 text => $arg{label},
1184 fontsize => ($arg{border} || 0.8) * 0.75;
1185 }
1186
1187 my $self = $class->SUPER::new (
1188 # label => "",
1189 fg => [0.6, 0.3, 0.1],
1190 border => 0.8,
1191 style => 'single',
1192 %arg,
1193 );
1194
1195 $self
1196 }
1197
1198 sub add {
1199 my ($self, @widgets) = @_;
1200
1201 $self->SUPER::add (@widgets);
1202 $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label};
1203 }
1204
1205 sub border {
1206 int $_[0]{border} * $::FONTSIZE
1207 }
1208
1209 sub size_request {
1210 my ($self) = @_;
1211
1212 ($self->{label_w}, undef) = $self->{label}->size_request
1213 if $self->{label};
1214
1215 my ($w, $h) = $self->SUPER::size_request;
1216
1217 (
1218 $w + $self->border * 2,
1219 $h + $self->border * 2,
1220 )
1221 }
1222
1223 sub invoke_size_allocate {
1224 my ($self, $w, $h) = @_;
1225
1226 my $border = $self->border;
1227
1228 $w -= List::Util::max 0, $border * 2;
1229 $h -= List::Util::max 0, $border * 2;
1230
1231 if (my $label = $self->{label}) {
1232 $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2;
1233 $label->{h} = List::Util::min $h, $border;
1234 $label->invoke_size_allocate ($label->{w}, $label->{h});
1235 }
1236
1237 $self->child->configure ($border, $border, $w, $h);
1238
1239 1
1240 }
1241
1242 sub _draw {
1243 my ($self) = @_;
1244
1245 my $child = $self->{children}[0];
1246
1247 my $border = $self->border;
1248 my ($w, $h) = ($self->{w}, $self->{h});
1249
1250 $child->draw;
1251
1252 glColor @{$self->{fg}};
1253 glBegin GL_LINE_STRIP;
1254 glVertex $border * 1.5 , $border * 0.5 + 0.5;
1255 glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1256 glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1257 glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5;
1258 glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5;
1259 glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5;
1260 glEnd;
1261
1262 if ($self->{label}) {
1263 glTranslate $border * 2, 0;
1264 $self->{label}->_draw;
1265 }
1266 }
1267
1268 #############################################################################
1269
1270 package CFPlus::UI::Toplevel;
1271
1272 our @ISA = CFPlus::UI::Bin::;
1273
1274 use CFPlus::OpenGL;
1275
1276 my $bg =
1277 new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png",
1278 mipmap => 1, wrap => 1;
1279
1280 my @border =
1281 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
1282 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1283
1284 sub new {
1285 my ($class, %arg) = @_;
1286
1287 my $self = $class->SUPER::new (
1288 bg => [1, 1, 1, 1],
1289 border_bg => [1, 1, 1, 1],
1290 border => 0.6,
1291 can_events => 1,
1292 min_w => 64,
1293 min_h => 32,
1294 %arg,
1295 );
1296
1297 $self->{title_widget} = new CFPlus::UI::Label
1298 align => 0,
1299 valign => 1,
1300 text => $self->{title},
1301 fontsize => $self->{border},
1302 if exists $self->{title};
1303
1304 if ($self->{has_close_button}) {
1305 $self->{close_button} =
1306 new CFPlus::UI::ImageButton
1307 path => 'x1_close.png',
1308 on_activate => sub { $self->emit ("delete") };
1309
1310 $self->CFPlus::UI::Container::add ($self->{close_button});
1311 }
1312
1313 $self
1314 }
1315
1316 sub add {
1317 my ($self, @widgets) = @_;
1318
1319 $self->SUPER::add (@widgets);
1320 $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button};
1321 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1322 }
1323
1324 sub border {
1325 int $_[0]{border} * $::FONTSIZE
1326 }
1327
1328 sub size_request {
1329 my ($self) = @_;
1330
1331 $self->{title_widget}->size_request
1332 if $self->{title_widget};
1333
1334 $self->{close_button}->size_request
1335 if $self->{close_button};
1336
1337 my ($w, $h) = $self->SUPER::size_request;
1338
1339 (
1340 $w + $self->border * 2,
1341 $h + $self->border * 2,
1342 )
1343 }
1344
1345 sub invoke_size_allocate {
1346 my ($self, $w, $h) = @_;
1347
1348 if ($self->{title_widget}) {
1349 $self->{title_widget}{w} = $w;
1350 $self->{title_widget}{h} = $h;
1351 $self->{title_widget}->invoke_size_allocate ($w, $h);
1352 }
1353
1354 my $border = $self->border;
1355
1356 $h -= List::Util::max 0, $border * 2;
1357 $w -= List::Util::max 0, $border * 2;
1358
1359 $self->child->configure ($border, $border, $w, $h);
1360
1361 $self->{close_button}->configure ($self->{w} - $border, 0, $border, $border)
1362 if $self->{close_button};
1363
1364 1
1365 }
1366
1367 sub invoke_delete {
1368 my ($self) = @_;
1369
1370 $self->hide;
1371
1372 1
1373 }
1374
1375 sub invoke_button_down {
1376 my ($self, $ev, $x, $y) = @_;
1377
1378 my ($w, $h) = @$self{qw(w h)};
1379 my $border = $self->border;
1380
1381 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
1382 my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
1383
1384 if ($lr & $td) {
1385 my ($wx, $wy) = ($self->{x}, $self->{y});
1386 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1387 my ($bw, $bh) = ($self->{w}, $self->{h});
1388
1389 my $mx = $x < $border;
1390 my $my = $y < $border;
1391
1392 $self->{motion} = sub {
1393 my ($ev, $x, $y) = @_;
1394
1395 my $dx = $ev->{x} - $ox;
1396 my $dy = $ev->{y} - $oy;
1397
1398 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1399 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1400
1401 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1402 $self->realloc;
1403 };
1404
1405 } elsif ($lr ^ $td) {
1406 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1407 my ($bx, $by) = ($self->{x}, $self->{y});
1408
1409 $self->{motion} = sub {
1410 my ($ev, $x, $y) = @_;
1411
1412 ($x, $y) = ($ev->{x}, $ev->{y});
1413
1414 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1415 # HACK: the next line is required to enforce placement
1416 $self->{parent}->invoke_size_allocate ($self->{parent}{w}, $self->{parent}{h});
1417 };
1418 } else {
1419 return 0;
1420 }
1421
1422 1
1423 }
1424
1425 sub invoke_button_up {
1426 my ($self, $ev, $x, $y) = @_;
1427
1428 ! ! delete $self->{motion}
1429 }
1430
1431 sub invoke_mouse_motion {
1432 my ($self, $ev, $x, $y) = @_;
1433
1434 $self->{motion}->($ev, $x, $y) if $self->{motion};
1435
1436 ! ! $self->{motion}
1437 }
1438
1439 sub invoke_visibility_change {
1440 my ($self, $visible) = @_;
1441
1442 delete $self->{motion} unless $visible;
1443
1444 0
1445 }
1446
1447 sub _draw {
1448 my ($self) = @_;
1449
1450 my $child = $self->{children}[0];
1451
1452 my ($w, $h ) = ($self->{w}, $self->{h});
1453 my ($cw, $ch) = ($child->{w}, $child->{h});
1454
1455 glEnable GL_TEXTURE_2D;
1456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1457
1458 my $border = $self->border;
1459
1460 glColor @{ $self->{border_bg} };
1461 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1462 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1463 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1464 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1465
1466 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1467 glColor @{ $self->{bg} };
1468
1469 # TODO: repeat texture not scale
1470 # solve this better(?)
1471 $bg->{s} = $cw / $bg->{w};
1472 $bg->{t} = $ch / $bg->{h};
1473 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1474 }
1475
1476 glDisable GL_TEXTURE_2D;
1477
1478 $child->draw;
1479
1480 if ($self->{title_widget}) {
1481 glTranslate 0, $border - $self->{h};
1482 $self->{title_widget}->_draw;
1483
1484 glTranslate 0, - ($border - $self->{h});
1485 }
1486
1487 $self->{close_button}->draw
1488 if $self->{close_button};
1489 }
1490
1491 #############################################################################
1492
1493 package CFPlus::UI::Table;
1494
1495 our @ISA = CFPlus::UI::Base::;
1496
1497 use List::Util qw(max sum);
1498
1499 use CFPlus::OpenGL;
1500
1501 sub new {
1502 my $class = shift;
1503
1504 $class->SUPER::new (
1505 col_expand => [],
1506 row_expand => [],
1507 @_,
1508 )
1509 }
1510
1511 sub children {
1512 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1513 }
1514
1515 sub add {
1516 my ($self) = shift;
1517
1518 while (@_) {
1519 my ($x, $y, $child) = splice @_, 0, 3, ();
1520 $child->set_parent ($self);
1521 $self->{children}[$y][$x] = $child;
1522 }
1523
1524 $self->{force_realloc} = 1;
1525 $self->{force_size_alloc} = 1;
1526 $self->realloc;
1527 }
1528
1529 sub remove {
1530 my ($self, $child) = @_;
1531
1532 # TODO: not yet implemented
1533 }
1534
1535 # TODO: move to container class maybe? send children a signal on removal?
1536 sub clear {
1537 my ($self) = @_;
1538
1539 my @children = $self->children;
1540 delete $self->{children};
1541
1542 for (@children) {
1543 delete $_->{parent};
1544 $_->hide;
1545 }
1546
1547 $self->realloc;
1548 }
1549
1550 sub get_wh {
1551 my ($self) = @_;
1552
1553 my (@w, @h);
1554
1555 for my $y (0 .. $#{$self->{children}}) {
1556 my $row = $self->{children}[$y]
1557 or next;
1558
1559 for my $x (0 .. $#$row) {
1560 my $widget = $row->[$x]
1561 or next;
1562 my ($w, $h) = @$widget{qw(req_w req_h)};
1563
1564 $w[$x] = max $w[$x], $w;
1565 $h[$y] = max $h[$y], $h;
1566 }
1567 }
1568
1569 (\@w, \@h)
1570 }
1571
1572 sub size_request {
1573 my ($self) = @_;
1574
1575 my ($ws, $hs) = $self->get_wh;
1576
1577 (
1578 (sum @$ws),
1579 (sum @$hs),
1580 )
1581 }
1582
1583 sub invoke_size_allocate {
1584 my ($self, $w, $h) = @_;
1585
1586 my ($ws, $hs) = $self->get_wh;
1587
1588 my $req_w = (sum @$ws) || 1;
1589 my $req_h = (sum @$hs) || 1;
1590
1591 # TODO: nicer code
1592 my @col_expand = @{$self->{col_expand}};
1593 @col_expand = (1) x @$ws unless @col_expand;
1594 my $col_expand = (sum @col_expand) || 1;
1595
1596 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1597
1598 CFPlus::UI::harmonize $ws;
1599
1600 my @row_expand = @{$self->{row_expand}};
1601 @row_expand = (1) x @$ws unless @row_expand;
1602 my $row_expand = (sum @row_expand) || 1;
1603
1604 $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs;
1605
1606 CFPlus::UI::harmonize $hs;
1607
1608 my $y;
1609
1610 for my $r (0 .. $#{$self->{children}}) {
1611 my $row = $self->{children}[$r]
1612 or next;
1613
1614 my $x = 0;
1615 my $row_h = $hs->[$r];
1616
1617 for my $c (0 .. $#$row) {
1618 my $col_w = $ws->[$c];
1619
1620 if (my $widget = $row->[$c]) {
1621 $widget->configure ($x, $y, $col_w, $row_h);
1622 }
1623
1624 $x += $col_w;
1625 }
1626
1627 $y += $row_h;
1628 }
1629
1630 1
1631 }
1632
1633 sub find_widget {
1634 my ($self, $x, $y) = @_;
1635
1636 $x -= $self->{x};
1637 $y -= $self->{y};
1638
1639 my $res;
1640
1641 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1642 $res = $_->find_widget ($x, $y)
1643 and return $res;
1644 }
1645
1646 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1647 }
1648
1649 sub _draw {
1650 my ($self) = @_;
1651
1652 for (grep $_, @{$self->{children}}) {
1653 $_->draw for grep $_, @$_;
1654 }
1655 }
1656
1657 #############################################################################
1658
1659 package CFPlus::UI::Box;
1660
1661 our @ISA = CFPlus::UI::Container::;
1662
1663 sub size_request {
1664 my ($self) = @_;
1665
1666 $self->{vertical}
1667 ? (
1668 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1669 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1670 )
1671 : (
1672 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1673 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1674 )
1675 }
1676
1677 sub invoke_size_allocate {
1678 my ($self, $w, $h) = @_;
1679
1680 my $space = $self->{vertical} ? $h : $w;
1681 my @children = $self->visible_children;
1682
1683 my @req;
1684
1685 if ($self->{homogeneous}) {
1686 @req = ($space / (@children || 1)) x @children;
1687 } else {
1688 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @children;
1689 my $req = List::Util::sum @req;
1690
1691 if ($req > $space) {
1692 # ah well, not enough space
1693 $_ *= $space / $req for @req;
1694 } else {
1695 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1696
1697 $space = ($space - $req) / $expand; # remaining space to give away
1698
1699 $req[$_] += $space * $children[$_]{expand}
1700 for 0 .. $#children;
1701 }
1702 }
1703
1704 CFPlus::UI::harmonize \@req;
1705
1706 my $pos = 0;
1707 for (0 .. $#children) {
1708 my $alloc = $req[$_];
1709 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1710
1711 $pos += $alloc;
1712 }
1713
1714 1
1715 }
1716
1717 #############################################################################
1718
1719 package CFPlus::UI::HBox;
1720
1721 our @ISA = CFPlus::UI::Box::;
1722
1723 sub new {
1724 my $class = shift;
1725
1726 $class->SUPER::new (
1727 vertical => 0,
1728 @_,
1729 )
1730 }
1731
1732 #############################################################################
1733
1734 package CFPlus::UI::VBox;
1735
1736 our @ISA = CFPlus::UI::Box::;
1737
1738 sub new {
1739 my $class = shift;
1740
1741 $class->SUPER::new (
1742 vertical => 1,
1743 @_,
1744 )
1745 }
1746
1747 #############################################################################
1748
1749 package CFPlus::UI::Label;
1750
1751 our @ISA = CFPlus::UI::DrawBG::;
1752
1753 use CFPlus::OpenGL;
1754
1755 sub new {
1756 my ($class, %arg) = @_;
1757
1758 my $self = $class->SUPER::new (
1759 fg => [1, 1, 1],
1760 #bg => none
1761 #active_bg => none
1762 #font => default_font
1763 #text => initial text
1764 #markup => initial narkup
1765 #max_w => maximum pixel width
1766 #style => 0, # render flags
1767 ellipsise => 3, # end
1768 layout => (new CFPlus::Layout),
1769 fontsize => 1,
1770 align => -1,
1771 valign => -1,
1772 padding_x => 2,
1773 padding_y => 2,
1774 can_events => 0,
1775 %arg
1776 );
1777
1778 if (exists $self->{template}) {
1779 my $layout = new CFPlus::Layout;
1780 $layout->set_text (delete $self->{template});
1781 $self->{template} = $layout;
1782 }
1783
1784 if (exists $self->{markup}) {
1785 $self->set_markup (delete $self->{markup});
1786 } else {
1787 $self->set_text (delete $self->{text});
1788 }
1789
1790 $self
1791 }
1792
1793 sub update {
1794 my ($self) = @_;
1795
1796 delete $self->{texture};
1797 $self->SUPER::update;
1798 }
1799
1800 sub realloc {
1801 my ($self) = @_;
1802
1803 delete $self->{ox};
1804 $self->SUPER::realloc;
1805 }
1806
1807 sub set_text {
1808 my ($self, $text) = @_;
1809
1810 return if $self->{text} eq "T$text";
1811 $self->{text} = "T$text";
1812
1813 $self->{layout}->set_text ($text);
1814
1815 delete $self->{size_req};
1816 $self->realloc;
1817 $self->update;
1818 }
1819
1820 sub set_markup {
1821 my ($self, $markup) = @_;
1822
1823 return if $self->{text} eq "M$markup";
1824 $self->{text} = "M$markup";
1825
1826 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1827
1828 $self->{layout}->set_markup ($markup);
1829
1830 delete $self->{size_req};
1831 $self->realloc;
1832 $self->update;
1833 }
1834
1835 sub size_request {
1836 my ($self) = @_;
1837
1838 $self->{size_req} ||= do {
1839 $self->{layout}->set_font ($self->{font}) if $self->{font};
1840 $self->{layout}->set_width ($self->{max_w} || -1);
1841 $self->{layout}->set_ellipsise ($self->{ellipsise});
1842 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1843 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1844
1845 my ($w, $h) = $self->{layout}->size;
1846
1847 if (exists $self->{template}) {
1848 $self->{template}->set_font ($self->{font}) if $self->{font};
1849 $self->{template}->set_width ($self->{max_w} || -1);
1850 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1851
1852 my ($w2, $h2) = $self->{template}->size;
1853
1854 $w = List::Util::max $w, $w2;
1855 $h = List::Util::max $h, $h2;
1856 }
1857
1858 [$w, $h]
1859 };
1860
1861 @{ $self->{size_req} }
1862 }
1863
1864 sub baseline_shift {
1865 $_[0]{layout}->descent
1866 }
1867
1868 sub invoke_size_allocate {
1869 my ($self, $w, $h) = @_;
1870
1871 delete $self->{ox};
1872
1873 delete $self->{texture}
1874 unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w};
1875
1876 1
1877 }
1878
1879 sub set_fontsize {
1880 my ($self, $fontsize) = @_;
1881
1882 $self->{fontsize} = $fontsize;
1883 delete $self->{size_req};
1884 delete $self->{texture};
1885
1886 $self->realloc;
1887 }
1888
1889 sub reconfigure {
1890 my ($self) = @_;
1891
1892 delete $self->{size_req};
1893 delete $self->{texture};
1894
1895 $self->SUPER::reconfigure;
1896 }
1897
1898 sub _draw {
1899 my ($self) = @_;
1900
1901 $self->SUPER::_draw; # draw background, if applicable
1902
1903 my $size = $self->{texture} ||= do {
1904 $self->{layout}->set_foreground (@{$self->{fg}});
1905 $self->{layout}->set_font ($self->{font}) if $self->{font};
1906 $self->{layout}->set_width ($self->{w});
1907 $self->{layout}->set_ellipsise ($self->{ellipsise});
1908 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1909 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1910
1911 [$self->{layout}->size]
1912 };
1913
1914 unless (exists $self->{ox}) {
1915 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1916 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x}
1917 : ($self->{w} - $size->[0]) * 0.5);
1918
1919 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1920 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1921 : ($self->{h} - $size->[1]) * 0.5);
1922 };
1923
1924 my $w = List::Util::min $self->{w} + 4, $size->[0];
1925 my $h = List::Util::min $self->{h} + 2, $size->[1];
1926
1927 $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style});
1928 }
1929
1930 #############################################################################
1931
1932 package CFPlus::UI::EntryBase;
1933
1934 our @ISA = CFPlus::UI::Label::;
1935
1936 use CFPlus::OpenGL;
1937
1938 sub new {
1939 my $class = shift;
1940
1941 $class->SUPER::new (
1942 fg => [1, 1, 1],
1943 bg => [0, 0, 0, 0.2],
1944 active_bg => [1, 1, 1, 0.5],
1945 active_fg => [0, 0, 0],
1946 can_hover => 1,
1947 can_focus => 1,
1948 valign => 0,
1949 can_events => 1,
1950 ellipsise => 0,
1951 #text => ...
1952 #hidden => "*",
1953 @_
1954 )
1955 }
1956
1957 sub _set_text {
1958 my ($self, $text) = @_;
1959
1960 delete $self->{cur_h};
1961
1962 return if $self->{text} eq $text;
1963
1964 $self->{last_activity} = $::NOW;
1965 $self->{text} = $text;
1966
1967 $text =~ s/./*/g if $self->{hidden};
1968 $self->{layout}->set_text ("$text ");
1969 delete $self->{size_req};
1970
1971 $self->emit (changed => $self->{text});
1972
1973 $self->realloc;
1974 $self->update;
1975 }
1976
1977 sub set_text {
1978 my ($self, $text) = @_;
1979
1980 $self->{cursor} = length $text;
1981 $self->_set_text ($text);
1982 }
1983
1984 sub get_text {
1985 $_[0]{text}
1986 }
1987
1988 sub size_request {
1989 my ($self) = @_;
1990
1991 my ($w, $h) = $self->SUPER::size_request;
1992
1993 ($w + 1, $h) # add 1 for cursor
1994 }
1995
1996 sub invoke_key_down {
1997 my ($self, $ev) = @_;
1998
1999 my $mod = $ev->{mod};
2000 my $sym = $ev->{sym};
2001 my $uni = $ev->{unicode};
2002
2003 my $text = $self->get_text;
2004
2005 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
2006
2007 if ($uni == 8) {
2008 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
2009 } elsif ($uni == 127) {
2010 substr $text, $self->{cursor}, 1, "";
2011 } elsif ($sym == CFPlus::SDLK_LEFT) {
2012 --$self->{cursor} if $self->{cursor};
2013 } elsif ($sym == CFPlus::SDLK_RIGHT) {
2014 ++$self->{cursor} if $self->{cursor} < length $self->{text};
2015 } elsif ($sym == CFPlus::SDLK_HOME) {
2016 # what a hack
2017 $self->{cursor} =
2018 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
2019 ? length $1
2020 : 0;
2021 } elsif ($sym == CFPlus::SDLK_END) {
2022 # uh, again
2023 $self->{cursor} =
2024 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
2025 ? $self->{cursor} + length $1
2026 : length $self->{text};
2027 } elsif ($uni == 21) { # ctrl-u
2028 $text = "";
2029 $self->{cursor} = 0;
2030 } elsif ($uni == 27) {
2031 $self->emit ('escape');
2032 } elsif ($uni == 0x0d) {
2033 substr $text, $self->{cursor}++, 0, "\012";
2034 } elsif ($uni >= 0x20) {
2035 substr $text, $self->{cursor}++, 0, chr $uni;
2036 } else {
2037 return 0;
2038 }
2039
2040 $self->_set_text ($text);
2041
2042 $self->realloc;
2043 $self->update;
2044
2045 1
2046 }
2047
2048 sub invoke_focus_in {
2049 my ($self) = @_;
2050
2051 $self->{last_activity} = $::NOW;
2052
2053 $self->SUPER::invoke_focus_in
2054 }
2055
2056 sub invoke_button_down {
2057 my ($self, $ev, $x, $y) = @_;
2058
2059 $self->SUPER::invoke_button_down ($ev, $x, $y);
2060
2061 my $idx = $self->{layout}->xy_to_index ($x, $y);
2062
2063 # byte-index to char-index
2064 my $text = $self->{text};
2065 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
2066 $self->{cursor} = length $text;
2067
2068 $self->_set_text ($self->{text});
2069 $self->update;
2070
2071 1
2072 }
2073
2074 sub invoke_mouse_motion {
2075 my ($self, $ev, $x, $y) = @_;
2076 # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
2077
2078 1
2079 }
2080
2081 sub _draw {
2082 my ($self) = @_;
2083
2084 local $self->{fg} = $self->{fg};
2085
2086 if ($FOCUS == $self) {
2087 glColor_premultiply @{$self->{active_bg}};
2088 $self->{fg} = $self->{active_fg};
2089 } else {
2090 glColor_premultiply @{$self->{bg}};
2091 }
2092
2093 glEnable GL_BLEND;
2094 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2095 glBegin GL_QUADS;
2096 glVertex 0 , 0;
2097 glVertex 0 , $self->{h};
2098 glVertex $self->{w}, $self->{h};
2099 glVertex $self->{w}, 0;
2100 glEnd;
2101 glDisable GL_BLEND;
2102
2103 $self->SUPER::_draw;
2104
2105 #TODO: force update every cursor change :(
2106 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
2107
2108 unless (exists $self->{cur_h}) {
2109 my $text = substr $self->{text}, 0, $self->{cursor};
2110 utf8::encode $text;
2111
2112 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
2113 }
2114
2115 glBegin GL_LINES;
2116 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
2117 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2118 glEnd;
2119 }
2120 }
2121
2122 #############################################################################
2123
2124 package CFPlus::UI::Entry;
2125
2126 our @ISA = CFPlus::UI::EntryBase::;
2127
2128 use CFPlus::OpenGL;
2129
2130 sub invoke_key_down {
2131 my ($self, $ev) = @_;
2132
2133 my $sym = $ev->{sym};
2134
2135 if ($ev->{uni} == 0x0d || $sym == 13) {
2136 unshift @{$self->{history}},
2137 my $txt = $self->get_text;
2138
2139 $self->{history_pointer} = -1;
2140 $self->{history_saveback} = '';
2141 $self->emit (activate => $txt);
2142 $self->update;
2143
2144 } elsif ($sym == CFPlus::SDLK_UP) {
2145 if ($self->{history_pointer} < 0) {
2146 $self->{history_saveback} = $self->get_text;
2147 }
2148 if (@{$self->{history} || []} > 0) {
2149 $self->{history_pointer}++;
2150 if ($self->{history_pointer} >= @{$self->{history} || []}) {
2151 $self->{history_pointer} = @{$self->{history} || []} - 1;
2152 }
2153 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2154 }
2155
2156 } elsif ($sym == CFPlus::SDLK_DOWN) {
2157 $self->{history_pointer}--;
2158 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
2159
2160 if ($self->{history_pointer} >= 0) {
2161 $self->set_text ($self->{history}->[$self->{history_pointer}]);
2162 } else {
2163 $self->set_text ($self->{history_saveback});
2164 }
2165
2166 } else {
2167 return $self->SUPER::invoke_key_down ($ev)
2168 }
2169
2170 1
2171 }
2172
2173 #############################################################################
2174
2175 package CFPlus::UI::TextEdit;
2176
2177 our @ISA = CFPlus::UI::EntryBase::;
2178
2179 use CFPlus::OpenGL;
2180
2181 sub move_cursor_ver {
2182 my ($self, $dy) = @_;
2183
2184 my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor});
2185
2186 $y += $dy;
2187
2188 if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) {
2189 $self->{cursor} = $index;
2190 delete $self->{cur_h};
2191 $self->update;
2192 return;
2193 }
2194 }
2195
2196 sub invoke_key_down {
2197 my ($self, $ev) = @_;
2198
2199 my $sym = $ev->{sym};
2200
2201 if ($sym == CFPlus::SDLK_UP) {
2202 $self->move_cursor_ver (-1);
2203 } elsif ($sym == CFPlus::SDLK_DOWN) {
2204 $self->move_cursor_ver (+1);
2205 } else {
2206 return $self->SUPER::invoke_key_down ($ev)
2207 }
2208
2209 1
2210 }
2211
2212 #############################################################################
2213
2214 package CFPlus::UI::Button;
2215
2216 our @ISA = CFPlus::UI::Label::;
2217
2218 use CFPlus::OpenGL;
2219
2220 my @tex =
2221 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2222 qw(b1_button_active.png);
2223
2224 sub new {
2225 my $class = shift;
2226
2227 $class->SUPER::new (
2228 padding_x => 4,
2229 padding_y => 4,
2230 fg => [1, 1, 1],
2231 active_fg => [0, 0, 1],
2232 can_hover => 1,
2233 align => 0,
2234 valign => 0,
2235 can_events => 1,
2236 @_
2237 )
2238 }
2239
2240 sub invoke_button_up {
2241 my ($self, $ev, $x, $y) = @_;
2242
2243 $self->emit ("activate")
2244 if $x >= 0 && $x < $self->{w}
2245 && $y >= 0 && $y < $self->{h};
2246
2247 1
2248 }
2249
2250 sub _draw {
2251 my ($self) = @_;
2252
2253 local $self->{fg} = $GRAB == $self ? $self->{active_fg} : $self->{fg};
2254
2255 glEnable GL_TEXTURE_2D;
2256 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2257 glColor 0, 0, 0, 1;
2258
2259 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2260
2261 glDisable GL_TEXTURE_2D;
2262
2263 $self->SUPER::_draw;
2264 }
2265
2266 #############################################################################
2267
2268 package CFPlus::UI::CheckBox;
2269
2270 our @ISA = CFPlus::UI::DrawBG::;
2271
2272 my @tex =
2273 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2274 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2275
2276 use CFPlus::OpenGL;
2277
2278 sub new {
2279 my $class = shift;
2280
2281 $class->SUPER::new (
2282 padding_x => 2,
2283 padding_y => 2,
2284 fg => [1, 1, 1],
2285 active_fg => [1, 1, 0],
2286 bg => [0, 0, 0, 0.2],
2287 active_bg => [1, 1, 1, 0.5],
2288 state => 0,
2289 can_hover => 1,
2290 @_
2291 )
2292 }
2293
2294 sub size_request {
2295 my ($self) = @_;
2296
2297 (6) x 2
2298 }
2299
2300 sub toggle {
2301 my ($self) = @_;
2302
2303 $self->{state} = !$self->{state};
2304 $self->emit (changed => $self->{state});
2305 $self->update;
2306 }
2307
2308 sub invoke_button_down {
2309 my ($self, $ev, $x, $y) = @_;
2310
2311 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2312 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2313 $self->toggle;
2314 } else {
2315 return 0
2316 }
2317
2318 1
2319 }
2320
2321 sub _draw {
2322 my ($self) = @_;
2323
2324 $self->SUPER::_draw;
2325
2326 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2327
2328 my ($w, $h) = @$self{qw(w h)};
2329
2330 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2331
2332 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2333
2334 my $tex = $self->{state} ? $tex[1] : $tex[0];
2335
2336 glEnable GL_TEXTURE_2D;
2337 $tex->draw_quad_alpha (0, 0, $s, $s);
2338 glDisable GL_TEXTURE_2D;
2339 }
2340
2341 #############################################################################
2342
2343 package CFPlus::UI::Image;
2344
2345 our @ISA = CFPlus::UI::Base::;
2346
2347 use CFPlus::OpenGL;
2348
2349 our %texture_cache;
2350
2351 sub new {
2352 my $class = shift;
2353
2354 my $self = $class->SUPER::new (
2355 can_events => 0,
2356 @_,
2357 );
2358
2359 $self->{path} || $self->{tex}
2360 or Carp::croak "'path' or 'tex' attributes required";
2361
2362 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2363 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2364
2365 CFPlus::weaken $texture_cache{$self->{path}};
2366
2367 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2368
2369 $self
2370 }
2371
2372 sub STORABLE_freeze {
2373 my ($self, $cloning) = @_;
2374
2375 $self->{path}
2376 or die "cannot serialise CFPlus::UI::Image on non-loadable images\n";
2377
2378 $self->{path}
2379 }
2380
2381 sub STORABLE_attach {
2382 my ($self, $cloning, $path) = @_;
2383
2384 $self->new (path => $path)
2385 }
2386
2387 sub size_request {
2388 my ($self) = @_;
2389
2390 ($self->{tex}{w}, $self->{tex}{h})
2391 }
2392
2393 sub _draw {
2394 my ($self) = @_;
2395
2396 my $tex = $self->{tex};
2397
2398 my ($w, $h) = ($self->{w}, $self->{h});
2399
2400 if ($self->{rot90}) {
2401 glRotate 90, 0, 0, 1;
2402 glTranslate 0, -$self->{w}, 0;
2403
2404 ($w, $h) = ($h, $w);
2405 }
2406
2407 glEnable GL_TEXTURE_2D;
2408 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2409
2410 $tex->draw_quad (0, 0, $w, $h);
2411
2412 glDisable GL_TEXTURE_2D;
2413 }
2414
2415 #############################################################################
2416
2417 package CFPlus::UI::ImageButton;
2418
2419 our @ISA = CFPlus::UI::Image::;
2420
2421 use CFPlus::OpenGL;
2422
2423 my %textures;
2424
2425 sub new {
2426 my $class = shift;
2427
2428 my $self = $class->SUPER::new (
2429 padding_x => 4,
2430 padding_y => 4,
2431 fg => [1, 1, 1],
2432 active_fg => [0, 0, 1],
2433 can_hover => 1,
2434 align => 0,
2435 valign => 0,
2436 can_events => 1,
2437 @_
2438 );
2439 }
2440
2441 sub invoke_button_up {
2442 my ($self, $ev, $x, $y) = @_;
2443
2444 $self->emit ("activate")
2445 if $x >= 0 && $x < $self->{w}
2446 && $y >= 0 && $y < $self->{h};
2447
2448 1
2449 }
2450
2451 #############################################################################
2452
2453 package CFPlus::UI::VGauge;
2454
2455 our @ISA = CFPlus::UI::Base::;
2456
2457 use List::Util qw(min max);
2458
2459 use CFPlus::OpenGL;
2460
2461 my %tex = (
2462 food => [
2463 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2464 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
2465 ],
2466 grace => [
2467 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2468 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
2469 ],
2470 hp => [
2471 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2472 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
2473 ],
2474 mana => [
2475 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 }
2476 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
2477 ],
2478 );
2479
2480 # eg. VGauge->new (gauge => 'food'), default gauge: food
2481 sub new {
2482 my $class = shift;
2483
2484 my $self = $class->SUPER::new (
2485 type => 'food',
2486 @_
2487 );
2488
2489 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
2490
2491 $self
2492 }
2493
2494 sub size_request {
2495 my ($self) = @_;
2496
2497 #my $tex = $tex{$self->{type}}[0];
2498 #@$tex{qw(w h)}
2499 (0, 0)
2500 }
2501
2502 sub set_max {
2503 my ($self, $max) = @_;
2504
2505 return if $self->{max_val} == $max;
2506
2507 $self->{max_val} = $max;
2508 $self->update;
2509 }
2510
2511 sub set_value {
2512 my ($self, $val, $max) = @_;
2513
2514 $self->set_max ($max)
2515 if defined $max;
2516
2517 return if $self->{val} == $val;
2518
2519 $self->{val} = $val;
2520 $self->update;
2521 }
2522
2523 sub _draw {
2524 my ($self) = @_;
2525
2526 my $tex = $tex{$self->{type}};
2527 my ($t1, $t2, $t3) = @$tex;
2528
2529 my ($w, $h) = ($self->{w}, $self->{h});
2530
2531 if ($self->{vertical}) {
2532 glRotate 90, 0, 0, 1;
2533 glTranslate 0, -$self->{w}, 0;
2534
2535 ($w, $h) = ($h, $w);
2536 }
2537
2538 my $ycut = $self->{val} / ($self->{max_val} || 1);
2539
2540 my $ycut1 = max 0, min 1, $ycut;
2541 my $ycut2 = max 0, min 1, $ycut - 1;
2542
2543 my $h1 = $self->{h} * (1 - $ycut1);
2544 my $h2 = $self->{h} * (1 - $ycut2);
2545 my $h3 = $self->{h};
2546
2547 $_ = $_ * (284-4)/288 + 4/288 for ($h1, $h2, $h3);
2548
2549 glEnable GL_BLEND;
2550 glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
2551 GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2552 glEnable GL_TEXTURE_2D;
2553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2554
2555 glBindTexture GL_TEXTURE_2D, $t1->{name};
2556 glBegin GL_QUADS;
2557 glTexCoord 0 , 0; glVertex 0 , 0;
2558 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
2559 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
2560 glTexCoord $t1->{s}, 0; glVertex $w, 0;
2561 glEnd;
2562
2563 my $ycut1 = List::Util::min 1, $ycut;
2564 glBindTexture GL_TEXTURE_2D, $t2->{name};
2565 glBegin GL_QUADS;
2566 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
2567 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
2568 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
2569 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
2570 glEnd;
2571
2572 if ($t3) {
2573 glBindTexture GL_TEXTURE_2D, $t3->{name};
2574 glBegin GL_QUADS;
2575 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
2576 glTexCoord 0 , $t3->{t}; glVertex 0 , $h3;
2577 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $h3;
2578 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
2579 glEnd;
2580 }
2581
2582 glDisable GL_BLEND;
2583 glDisable GL_TEXTURE_2D;
2584 }
2585
2586 #############################################################################
2587
2588 package CFPlus::UI::Gauge;
2589
2590 our @ISA = CFPlus::UI::VBox::;
2591
2592 sub new {
2593 my ($class, %arg) = @_;
2594
2595 my $self = $class->SUPER::new (
2596 tooltip => $arg{type},
2597 can_hover => 1,
2598 can_events => 1,
2599 %arg,
2600 );
2601
2602 $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999");
2603 $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2604 $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999");
2605
2606 $self
2607 }
2608
2609 sub set_fontsize {
2610 my ($self, $fsize) = @_;
2611
2612 $self->{value}->set_fontsize ($fsize);
2613 $self->{max} ->set_fontsize ($fsize);
2614 }
2615
2616 sub set_max {
2617 my ($self, $max) = @_;
2618
2619 $self->{gauge}->set_max ($max);
2620 $self->{max}->set_text ($max);
2621 }
2622
2623 sub set_value {
2624 my ($self, $val, $max) = @_;
2625
2626 $self->set_max ($max)
2627 if defined $max;
2628
2629 $self->{gauge}->set_value ($val, $max);
2630 $self->{value}->set_text ($val);
2631 }
2632
2633 #############################################################################
2634
2635 package CFPlus::UI::Slider;
2636
2637 use strict;
2638
2639 use CFPlus::OpenGL;
2640
2641 our @ISA = CFPlus::UI::DrawBG::;
2642
2643 my @tex =
2644 map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ }
2645 qw(s1_slider.png s1_slider_bg.png);
2646
2647 sub new {
2648 my $class = shift;
2649
2650 # range [value, low, high, page, unit]
2651
2652 # TODO: 0-width page
2653 # TODO: req_w/h are wrong with vertical
2654 # TODO: calculations are off
2655 my $self = $class->SUPER::new (
2656 fg => [1, 1, 1],
2657 active_fg => [0, 0, 0],
2658 bg => [0, 0, 0, 0.2],
2659 active_bg => [1, 1, 1, 0.5],
2660 range => [0, 0, 100, 10, 0],
2661 min_w => $::WIDTH / 80,
2662 min_h => $::WIDTH / 80,
2663 vertical => 0,
2664 can_hover => 1,
2665 inner_pad => 0.02,
2666 @_
2667 );
2668
2669 $self->set_value ($self->{range}[0]);
2670 $self->update;
2671
2672 $self
2673 }
2674
2675 sub set_range {
2676 my ($self, $range) = @_;
2677
2678 ($range, $self->{range}) = ($self->{range}, $range);
2679
2680 if ("@$range" ne "@{$self->{range}}") {
2681 $self->update;
2682 $self->set_value ($self->{range}[0]);
2683 }
2684 }
2685
2686 sub set_value {
2687 my ($self, $value) = @_;
2688
2689 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2690
2691 $hi = $lo + 1 if $hi <= $lo;
2692
2693 $page = $hi - $lo if $page > $hi - $lo;
2694
2695 $value = $lo if $value < $lo;
2696 $value = $hi - $page if $value > $hi - $page;
2697
2698 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2699 if $unit;
2700
2701 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2702
2703 if ($value != $old_value) {
2704 $self->emit (changed => $value);
2705 $self->update;
2706 }
2707 }
2708
2709 sub size_request {
2710 my ($self) = @_;
2711
2712 ($self->{req_w}, $self->{req_h})
2713 }
2714
2715 sub invoke_button_down {
2716 my ($self, $ev, $x, $y) = @_;
2717
2718 $self->SUPER::invoke_button_down ($ev, $x, $y);
2719
2720 $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2721
2722 $self->invoke_mouse_motion ($ev, $x, $y)
2723 }
2724
2725 sub invoke_mouse_motion {
2726 my ($self, $ev, $x, $y) = @_;
2727
2728 if ($GRAB == $self) {
2729 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2730
2731 my (undef, $lo, $hi, $page) = @{$self->{range}};
2732
2733 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2734
2735 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2736 } else {
2737 return 0;
2738 }
2739
2740 1
2741 }
2742
2743 sub invoke_mouse_wheel {
2744 my ($self, $ev) = @_;
2745
2746 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2747
2748 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2749
2750 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2751
2752 ! ! $delta
2753 }
2754
2755 sub update {
2756 my ($self) = @_;
2757
2758 delete $self->{knob_w};
2759 $self->SUPER::update;
2760 }
2761
2762 sub _draw {
2763 my ($self) = @_;
2764
2765 unless ($self->{knob_w}) {
2766 $self->set_value ($self->{range}[0]);
2767
2768 my ($value, $lo, $hi, $page) = @{$self->{range}};
2769 my $range = ($hi - $page - $lo) || 1e-100;
2770
2771 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
2772
2773 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2774 $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2775
2776 $value = ($value - $lo) / $range;
2777 $value = $value * $self->{scale} + $self->{offset};
2778
2779 $self->{knob_x} = $value - $knob_w * 0.5;
2780 $self->{knob_w} = $knob_w;
2781 }
2782
2783 $self->SUPER::_draw ();
2784
2785 glScale $self->{w}, $self->{h};
2786
2787 if ($self->{vertical}) {
2788 # draw a vertical slider like a rotated horizontal slider
2789
2790 glTranslate 1, 0, 0;
2791 glRotate 90, 0, 0, 1;
2792 }
2793
2794 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2795 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2796
2797 glEnable GL_TEXTURE_2D;
2798 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2799
2800 # draw background
2801 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2802
2803 # draw handle
2804 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2805
2806 glDisable GL_TEXTURE_2D;
2807 }
2808
2809 #############################################################################
2810
2811 package CFPlus::UI::ValSlider;
2812
2813 our @ISA = CFPlus::UI::HBox::;
2814
2815 sub new {
2816 my ($class, %arg) = @_;
2817
2818 my $range = delete $arg{range};
2819
2820 my $self = $class->SUPER::new (
2821 slider => (new CFPlus::UI::Slider expand => 1, range => $range),
2822 entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}),
2823 to_value => sub { shift },
2824 from_value => sub { shift },
2825 %arg,
2826 );
2827
2828 $self->{slider}->connect (changed => sub {
2829 my ($self, $value) = @_;
2830 $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value));
2831 $self->{parent}->emit (changed => $value);
2832 });
2833
2834 # $self->{entry}->connect (changed => sub {
2835 # my ($self, $value) = @_;
2836 # $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value));
2837 # $self->{parent}->emit (changed => $value);
2838 # });
2839
2840 $self->add ($self->{slider}, $self->{entry});
2841
2842 $self->{slider}->emit (changed => $self->{slider}{range}[0]);
2843
2844 $self
2845 }
2846
2847 sub set_range { shift->{slider}->set_range (@_) }
2848 sub set_value { shift->{slider}->set_value (@_) }
2849
2850 #############################################################################
2851
2852 package CFPlus::UI::TextScroller;
2853
2854 our @ISA = CFPlus::UI::HBox::;
2855
2856 use CFPlus::OpenGL;
2857
2858 sub new {
2859 my $class = shift;
2860
2861 my $self = $class->SUPER::new (
2862 fontsize => 1,
2863 can_events => 1,
2864 indent => 0,
2865 #font => default_font
2866 @_,
2867
2868 layout => (new CFPlus::Layout),
2869 par => [],
2870 max_par => 0,
2871 height => 0,
2872 children => [
2873 (new CFPlus::UI::Empty expand => 1),
2874 (new CFPlus::UI::Slider vertical => 1),
2875 ],
2876 );
2877
2878 $self->{children}[1]->connect (changed => sub { $self->update });
2879
2880 $self
2881 }
2882
2883 sub set_fontsize {
2884 my ($self, $fontsize) = @_;
2885
2886 $self->{fontsize} = $fontsize;
2887 $self->reflow;
2888 }
2889
2890 sub size_request {
2891 my ($self) = @_;
2892
2893 my ($empty, $slider) = @{ $self->{children} };
2894
2895 local $self->{children} = [$empty, $slider];
2896 $self->SUPER::size_request
2897 }
2898
2899 sub invoke_size_allocate {
2900 my ($self, $w, $h) = @_;
2901
2902 my ($empty, $slider, @other) = @{ $self->{children} };
2903 $_->configure (@$_{qw(x y req_w req_h)}) for @other;
2904
2905 $self->{layout}->set_font ($self->{font}) if $self->{font};
2906 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2907 $self->{layout}->set_width ($empty->{w});
2908 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2909
2910 $self->reflow;
2911
2912 local $self->{children} = [$empty, $slider];
2913 $self->SUPER::invoke_size_allocate ($w, $h)
2914 }
2915
2916 sub invoke_mouse_wheel {
2917 my ($self, $ev) = @_;
2918
2919 return 0 unless $ev->{dy}; # only vertical movements
2920
2921 $self->{children}[1]->emit (mouse_wheel => $ev);
2922
2923 1
2924 }
2925
2926 sub get_layout {
2927 my ($self, $para) = @_;
2928
2929 my $layout = $self->{layout};
2930
2931 $layout->set_font ($self->{font}) if $self->{font};
2932 $layout->set_foreground (@{$para->{fg}});
2933 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2934 $layout->set_width ($self->{children}[0]{w} - $para->{indent});
2935 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2936 $layout->set_markup ($para->{markup});
2937
2938 $layout->set_shapes (
2939 map
2940 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
2941 @{$para->{widget}}
2942 );
2943
2944 $layout
2945 }
2946
2947 sub reflow {
2948 my ($self) = @_;
2949
2950 $self->{need_reflow}++;
2951 $self->update;
2952 }
2953
2954 sub set_offset {
2955 my ($self, $offset) = @_;
2956
2957 # todo: base offset on lines or so, not on pixels
2958 $self->{children}[1]->set_value ($offset);
2959 }
2960
2961 sub current_paragraph {
2962 my ($self) = @_;
2963
2964 $self->{top_paragraph} - 1
2965 }
2966
2967 sub scroll_to {
2968 my ($self, $para) = @_;
2969
2970 $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para;
2971
2972 $self->{scroll_to} = $para;
2973 $self->update;
2974 }
2975
2976 sub clear {
2977 my ($self) = @_;
2978
2979 my (undef, undef, @other) = @{ $self->{children} };
2980 $self->remove ($_) for @other;
2981
2982 $self->{par} = [];
2983 $self->{height} = 0;
2984 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2985 }
2986
2987 sub add_paragraph {
2988 my $self = shift;
2989
2990 for my $para (@_) {
2991 $para = {
2992 fg => [1, 1, 1, 1],
2993 indent => 0,
2994 markup => "",
2995 widget => [],
2996 ref $para ? %$para : (markup => $para),
2997 w => 1e10,
2998 wrapped => 1,
2999 };
3000
3001 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
3002 push @{$self->{par}}, $para;
3003 }
3004
3005 if (my $max = $self->{max_par}) {
3006 shift @{$self->{par}} while @{$self->{par}} > $max;
3007 }
3008
3009 $self->{need_reflow}++;
3010 $self->update;
3011 }
3012
3013 sub scroll_to_bottom {
3014 my ($self) = @_;
3015
3016 $self->{scroll_to} = $#{$self->{par}};
3017 $self->update;
3018 }
3019
3020 sub force_uptodate {
3021 my ($self) = @_;
3022
3023 if (delete $self->{need_reflow}) {
3024 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3025
3026 my $height = 0;
3027
3028 for my $para (@{$self->{par}}) {
3029 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
3030 my $layout = $self->get_layout ($para);
3031 my ($w, $h) = $layout->size;
3032
3033 $para->{w} = $w + $para->{indent};
3034 $para->{h} = $h;
3035 $para->{wrapped} = $layout->has_wrapped;
3036 }
3037
3038 $para->{y} = $height;
3039 $height += $para->{h};
3040 }
3041
3042 $self->{height} = $height;
3043 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
3044
3045 delete $self->{texture};
3046 }
3047
3048 if (my $paridx = delete $self->{scroll_to}) {
3049 $self->{children}[1]->set_value ($self->{par}[$paridx]{y});
3050 }
3051 }
3052
3053 sub update {
3054 my ($self) = @_;
3055
3056 $self->SUPER::update;
3057
3058 return unless $self->{h} > 0;
3059
3060 delete $self->{texture};
3061
3062 $ROOT->on_post_alloc ($self => sub {
3063 $self->force_uptodate;
3064
3065 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
3066
3067 $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub {
3068 glClearColor 0, 0, 0, 0;
3069 glClear GL_COLOR_BUFFER_BIT;
3070
3071 package CFPlus::UI::Base;
3072 local ($draw_x, $draw_y, $draw_w, $draw_h) =
3073 (0, 0, $self->{w}, $self->{h});
3074
3075 my $top = int $self->{children}[1]{range}[0];
3076
3077 my $paridx = 0;
3078 my $top_paragraph;
3079 my $top = int $self->{children}[1]{range}[0];
3080
3081 my $y0 = $top;
3082 my $y1 = $top + $H;
3083
3084 for my $para (@{$self->{par}}) {
3085 my $h = $para->{h};
3086 my $y = $para->{y};
3087
3088 if ($y0 < $y + $h && $y < $y1) {
3089 my $layout = $self->get_layout ($para);
3090
3091 $layout->render ($para->{indent}, $y - $y0);
3092
3093 if (my @w = @{ $para->{widget} }) {
3094 my @s = $layout->get_shapes;
3095
3096 for (@w) {
3097 my ($dx, $dy) = splice @s, 0, 2, ();
3098
3099 $_->{x} = $dx + $para->{indent};
3100 $_->{y} = $dy + $y - $y0;
3101
3102 $_->draw;
3103 }
3104 }
3105 }
3106
3107 $paridx++;
3108 $top_paragraph ||= $paridx if $y >= $top;
3109 }
3110
3111 $self->{top_paragraph} = $top_paragraph;
3112 };
3113 });
3114 }
3115
3116 sub reconfigure {
3117 my ($self) = @_;
3118
3119 $self->SUPER::reconfigure;
3120
3121 $_->{w} = 1e10 for @{ $self->{par} };
3122 $self->reflow;
3123 }
3124
3125 sub _draw {
3126 my ($self) = @_;
3127
3128 glEnable GL_TEXTURE_2D;
3129 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3130 glColor 0, 0, 0, 1;
3131 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
3132 glDisable GL_TEXTURE_2D;
3133
3134 $self->{children}[1]->draw;
3135 }
3136
3137 #############################################################################
3138
3139 package CFPlus::UI::Animator;
3140
3141 use CFPlus::OpenGL;
3142
3143 our @ISA = CFPlus::UI::Bin::;
3144
3145 sub moveto {
3146 my ($self, $x, $y) = @_;
3147
3148 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
3149 $self->{speed} = 0.001;
3150 $self->{time} = 1;
3151
3152 ::animation_start $self;
3153 }
3154
3155 sub animate {
3156 my ($self, $interval) = @_;
3157
3158 $self->{time} -= $interval * $self->{speed};
3159 if ($self->{time} <= 0) {
3160 $self->{time} = 0;
3161 ::animation_stop $self;
3162 }
3163
3164 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
3165
3166 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
3167 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
3168 }
3169
3170 sub _draw {
3171 my ($self) = @_;
3172
3173 glPushMatrix;
3174 glRotate $self->{time} * 1000, 0, 1, 0;
3175 $self->{children}[0]->draw;
3176 glPopMatrix;
3177 }
3178
3179 #############################################################################
3180
3181 package CFPlus::UI::Flopper;
3182
3183 our @ISA = CFPlus::UI::Button::;
3184
3185 sub new {
3186 my $class = shift;
3187
3188 my $self = $class->SUPER::new (
3189 state => 0,
3190 on_activate => \&toggle_flopper,
3191 @_
3192 );
3193
3194 $self
3195 }
3196
3197 sub toggle_flopper {
3198 my ($self) = @_;
3199
3200 $self->{other}->toggle_visibility;
3201 }
3202
3203 #############################################################################
3204
3205 package CFPlus::UI::Tooltip;
3206
3207 our @ISA = CFPlus::UI::Bin::;
3208
3209 use CFPlus::OpenGL;
3210
3211 sub new {
3212 my $class = shift;
3213
3214 $class->SUPER::new (
3215 @_,
3216 can_events => 0,
3217 )
3218 }
3219
3220 sub set_tooltip_from {
3221 my ($self, $widget) = @_;
3222
3223 $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1
3224 if $widget->{tooltip} =~ /^#(.*)$/;
3225
3226 my $tooltip = $widget->{tooltip};
3227
3228 if ($ENV{CFPLUS_DEBUG} & 2) {
3229 $tooltip .= "\n\n" . (ref $widget) . "\n"
3230 . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n"
3231 . "req $widget->{req_w} $widget->{req_h}\n"
3232 . "visible $widget->{visible}";
3233 }
3234
3235 $tooltip =~ s/^\n+//;
3236 $tooltip =~ s/\n+$//;
3237
3238 $self->add (new CFPlus::UI::Label
3239 markup => $tooltip,
3240 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
3241 fontsize => 0.8,
3242 style => 1, # FLAG_INVERSE
3243 ellipsise => 0,
3244 font => ($widget->{tooltip_font} || $::FONT_PROP),
3245 );
3246 }
3247
3248 sub size_request {
3249 my ($self) = @_;
3250
3251 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
3252
3253 ($w + 4, $h + 4)
3254 }
3255
3256 sub invoke_size_allocate {
3257 my ($self, $w, $h) = @_;
3258
3259 $self->SUPER::invoke_size_allocate ($w - 4, $h - 4)
3260 }
3261
3262 sub invoke_visibility_change {
3263 my ($self, $visible) = @_;
3264
3265 return unless $visible;
3266
3267 $self->{root}->on_post_alloc ("move_$self" => sub {
3268 my $widget = $self->{owner}
3269 or return;
3270
3271 if ($widget->{visible}) {
3272 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
3273
3274 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
3275 if $x + $self->{w} > $self->{root}{w};
3276
3277 $self->move_abs ($x, $y);
3278 } else {
3279 $self->hide;
3280 }
3281 });
3282 }
3283
3284 sub _draw {
3285 my ($self) = @_;
3286
3287 glTranslate 0.375, 0.375;
3288
3289 my ($w, $h) = @$self{qw(w h)};
3290
3291 glColor 1, 0.8, 0.4;
3292 glBegin GL_QUADS;
3293 glVertex 0 , 0;
3294 glVertex 0 , $h;
3295 glVertex $w, $h;
3296 glVertex $w, 0;
3297 glEnd;
3298
3299 glColor 0, 0, 0;
3300 glBegin GL_LINE_LOOP;
3301 glVertex 0 , 0;
3302 glVertex 0 , $h;
3303 glVertex $w, $h;
3304 glVertex $w, 0;
3305 glEnd;
3306
3307 glTranslate 2 - 0.375, 2 - 0.375;
3308
3309 $self->SUPER::_draw;
3310 }
3311
3312 #############################################################################
3313
3314 package CFPlus::UI::Face;
3315
3316 our @ISA = CFPlus::UI::DrawBG::;
3317
3318 use CFPlus::OpenGL;
3319
3320 sub new {
3321 my $class = shift;
3322
3323 my $self = $class->SUPER::new (
3324 aspect => 1,
3325 can_events => 0,
3326 @_,
3327 );
3328
3329 if ($self->{anim} && $self->{animspeed}) {
3330 CFPlus::weaken (my $widget = $self);
3331
3332 $self->{timer} = Event->timer (
3333 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3334 hard => 1,
3335 interval => $self->{animspeed},
3336 cb => sub {
3337 ++$widget->{frame};
3338 $widget->update;
3339 },
3340 );
3341 }
3342
3343 $self
3344 }
3345
3346 sub size_request {
3347 (32, 8)
3348 }
3349
3350 sub update {
3351 my ($self) = @_;
3352
3353 return unless $self->{visible};
3354
3355 $self->SUPER::update;
3356 }
3357
3358 sub _draw {
3359 my ($self) = @_;
3360
3361 return unless $::CONN;
3362
3363 $self->SUPER::_draw;
3364
3365 my $face;
3366
3367 if ($self->{frame}) {
3368 my $anim = $::CONN->{anim}[$self->{anim}];
3369
3370 $face = $anim->[ $self->{frame} % @$anim ]
3371 if $anim && @$anim;
3372 }
3373
3374 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
3375
3376 if ($tex) {
3377 glEnable GL_TEXTURE_2D;
3378 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
3379 glColor 0, 0, 0, 1;
3380 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
3381 glDisable GL_TEXTURE_2D;
3382 }
3383 }
3384
3385 sub destroy {
3386 my ($self) = @_;
3387
3388 $self->{timer}->cancel
3389 if $self->{timer};
3390
3391 $self->SUPER::destroy;
3392 }
3393
3394 #############################################################################
3395
3396 package CFPlus::UI::Buttonbar;
3397
3398 our @ISA = CFPlus::UI::HBox::;
3399
3400 # TODO: should actually wrap buttons and other goodies.
3401
3402 #############################################################################
3403
3404 package CFPlus::UI::Menu;
3405
3406 our @ISA = CFPlus::UI::Toplevel::;
3407
3408 use CFPlus::OpenGL;
3409
3410 sub new {
3411 my $class = shift;
3412
3413 my $self = $class->SUPER::new (
3414 items => [],
3415 z => 100,
3416 @_,
3417 );
3418
3419 $self->add ($self->{vbox} = new CFPlus::UI::VBox);
3420
3421 for my $item (@{ $self->{items} }) {
3422 my ($widget, $cb, $tooltip) = @$item;
3423
3424 # handle various types of items, only text for now
3425 if (!ref $widget) {
3426 if ($widget =~ /\t/) {
3427 my ($left, $right) = split /\t/, $widget, 2;
3428
3429 $widget = new CFPlus::UI::HBox
3430 can_hover => 1,
3431 can_events => 1,
3432 tooltip => $tooltip,
3433 children => [
3434 (new CFPlus::UI::Label markup => $left, expand => 1),
3435 (new CFPlus::UI::Label markup => $right, align => +1),
3436 ],
3437 ;
3438
3439 } else {
3440 $widget = new CFPlus::UI::Label
3441 can_hover => 1,
3442 can_events => 1,
3443 markup => $widget,
3444 tooltip => $tooltip;
3445 }
3446 }
3447
3448 $self->{item}{$widget} = $item;
3449
3450 $self->{vbox}->add ($widget);
3451 }
3452
3453 $self
3454 }
3455
3456 # popup given the event (must be a mouse button down event currently)
3457 sub popup {
3458 my ($self, $ev) = @_;
3459
3460 $self->emit ("popdown");
3461
3462 # maybe save $GRAB? must be careful about events...
3463 $GRAB = $self;
3464 $self->{button} = $ev->{button};
3465
3466 $self->show;
3467 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
3468 }
3469
3470 sub invoke_mouse_motion {
3471 my ($self, $ev, $x, $y) = @_;
3472
3473 # TODO: should use vbox->find_widget or so
3474 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
3475 $self->{hover} = $self->{item}{$HOVER};
3476
3477 0
3478 }
3479
3480 sub invoke_button_up {
3481 my ($self, $ev, $x, $y) = @_;
3482
3483 if ($ev->{button} == $self->{button}) {
3484 undef $GRAB;
3485 $self->hide;
3486
3487 $self->emit ("popdown");
3488 $self->{hover}[1]->() if $self->{hover};
3489 } else {
3490 return 0
3491 }
3492
3493 1
3494 }
3495
3496 #############################################################################
3497
3498 package CFPlus::UI::Multiplexer;
3499
3500 our @ISA = CFPlus::UI::Container::;
3501
3502 sub new {
3503 my $class = shift;
3504
3505 my $self = $class->SUPER::new (
3506 @_,
3507 );
3508
3509 $self->{current} = $self->{children}[0]
3510 if @{ $self->{children} };
3511
3512 $self
3513 }
3514
3515 sub add {
3516 my ($self, @widgets) = @_;
3517
3518 $self->SUPER::add (@widgets);
3519
3520 $self->{current} = $self->{children}[0]
3521 if @{ $self->{children} };
3522 }
3523
3524 sub get_current_page {
3525 my ($self) = @_;
3526
3527 $self->{current}
3528 }
3529
3530 sub set_current_page {
3531 my ($self, $page_or_widget) = @_;
3532
3533 my $widget = ref $page_or_widget
3534 ? $page_or_widget
3535 : $self->{children}[$page_or_widget];
3536
3537 $self->{current} = $widget;
3538 $self->{current}->configure (0, 0, $self->{w}, $self->{h});
3539
3540 $self->emit (page_changed => $self->{current});
3541
3542 $self->realloc;
3543 }
3544
3545 sub visible_children {
3546 $_[0]{current}
3547 }
3548
3549 sub size_request {
3550 my ($self) = @_;
3551
3552 $self->{current}->size_request
3553 }
3554
3555 sub invoke_size_allocate {
3556 my ($self, $w, $h) = @_;
3557
3558 $self->{current}->configure (0, 0, $w, $h);
3559
3560 1
3561 }
3562
3563 sub _draw {
3564 my ($self) = @_;
3565
3566 $self->{current}->draw;
3567 }
3568
3569 #############################################################################
3570
3571 package CFPlus::UI::Notebook;
3572
3573 our @ISA = CFPlus::UI::VBox::;
3574
3575 sub new {
3576 my $class = shift;
3577
3578 my $self = $class->SUPER::new (
3579 buttonbar => (new CFPlus::UI::Buttonbar),
3580 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3581 # filter => # will be put between multiplexer and $self
3582 @_,
3583 );
3584
3585 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
3586 $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer});
3587
3588 $self
3589 }
3590
3591 sub add {
3592 my ($self, $title, $widget, $tooltip) = @_;
3593
3594 CFPlus::weaken $self;
3595
3596 $self->{buttonbar}->add (new CFPlus::UI::Button
3597 markup => $title,
3598 tooltip => $tooltip,
3599 on_activate => sub { $self->set_current_page ($widget) },
3600 );
3601
3602 $self->{multiplexer}->add ($widget);
3603 }
3604
3605 sub get_current_page {
3606 my ($self) = @_;
3607
3608 $self->{multiplexer}->get_current_page
3609 }
3610
3611 sub set_current_page {
3612 my ($self, $page) = @_;
3613
3614 $self->{multiplexer}->set_current_page ($page);
3615 $self->emit (page_changed => $self->{multiplexer}{current});
3616 }
3617
3618 #############################################################################
3619
3620 package CFPlus::UI::Selector;
3621
3622 use utf8;
3623
3624 our @ISA = CFPlus::UI::Button::;
3625
3626 sub new {
3627 my $class = shift;
3628
3629 my $self = $class->SUPER::new (
3630 options => [], # [value, title, longdesc], ...
3631 value => undef,
3632 @_,
3633 );
3634
3635 $self->_set_value ($self->{value});
3636
3637 $self
3638 }
3639
3640 sub invoke_button_down {
3641 my ($self, $ev) = @_;
3642
3643 my @menu_items;
3644
3645 for (@{ $self->{options} }) {
3646 my ($value, $title, $tooltip) = @$_;
3647
3648 push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }];
3649 }
3650
3651 CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev);
3652 }
3653
3654 sub _set_value {
3655 my ($self, $value) = @_;
3656
3657 my ($item) = grep $_->[0] eq $value, @{ $self->{options} }
3658 or return;
3659
3660 $self->{value} = $item->[0];
3661 $self->set_markup ("$item->[1] ⇓");
3662 $self->set_tooltip ($item->[2]);
3663 }
3664
3665 sub set_value {
3666 my ($self, $value) = @_;
3667
3668 return unless $self->{value} ne $value;
3669
3670 $self->_set_value ($value);
3671 $self->emit (changed => $value);
3672 }
3673
3674 #############################################################################
3675
3676 package CFPlus::UI::Statusbox;
3677
3678 our @ISA = CFPlus::UI::VBox::;
3679
3680 sub new {
3681 my $class = shift;
3682
3683 my $self = $class->SUPER::new (
3684 fontsize => 0.8,
3685 @_,
3686 );
3687
3688 CFPlus::weaken (my $this = $self);
3689
3690 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3691
3692 $self
3693 }
3694
3695 sub reorder {
3696 my ($self) = @_;
3697 my $NOW = Time::HiRes::time;
3698
3699 # freeze display when hovering over any label
3700 return if $CFPlus::UI::TOOLTIP->{owner}
3701 && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label},
3702 values %{ $self->{item} };
3703
3704 while (my ($k, $v) = each %{ $self->{item} }) {
3705 delete $self->{item}{$k} if $v->{timeout} < $NOW;
3706 }
3707
3708 my @widgets;
3709
3710 my @items = sort {
3711 $a->{pri} <=> $b->{pri}
3712 or $b->{id} <=> $a->{id}
3713 } values %{ $self->{item} };
3714
3715 $self->{timer}->interval (1);
3716
3717 my $count = 10 + 1;
3718 for my $item (@items) {
3719 last unless --$count;
3720
3721 my $label = $item->{label} ||= do {
3722 # TODO: doesn't handle markup well (read as: at all)
3723 my $short = $item->{count} > 1
3724 ? "<b>$item->{count} ×</b> $item->{text}"
3725 : $item->{text};
3726
3727 for ($short) {
3728 s/^\s+//;
3729 s/\s+/ /g;
3730 }
3731
3732 new CFPlus::UI::Label
3733 markup => $short,
3734 tooltip => $item->{tooltip},
3735 tooltip_font => $::FONT_PROP,
3736 tooltip_width => 0.67,
3737 fontsize => $item->{fontsize} || $self->{fontsize},
3738 max_w => $::WIDTH * 0.44,
3739 fg => [@{ $item->{fg} }],
3740 can_events => 1,
3741 can_hover => 1
3742 };
3743
3744 if ((my $diff = $item->{timeout} - $NOW) < 2) {
3745 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
3746 $label->update;
3747 $label->set_max_size (undef, $label->{req_h} * $diff)
3748 if $diff < 1;
3749 $self->{timer}->interval (1/30);
3750 } else {
3751 $label->{fg}[3] = $item->{fg}[3] || 1;
3752 }
3753
3754 push @widgets, $label;
3755 }
3756
3757 $self->clear;
3758 $self->SUPER::add (reverse @widgets);
3759 }
3760
3761 sub add {
3762 my ($self, $text, %arg) = @_;
3763
3764 $text =~ s/^\s+//;
3765 $text =~ s/\s+$//;
3766
3767 return unless $text;
3768
3769 my $timeout = (int time) + ((delete $arg{timeout}) || 60);
3770
3771 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
3772
3773 if (my $item = $self->{item}{$group}) {
3774 if ($item->{text} eq $text) {
3775 $item->{count}++;
3776 } else {
3777 $item->{count} = 1;
3778 $item->{text} = $item->{tooltip} = $text;
3779 }
3780 $item->{id} += 0.2;#d#
3781 $item->{timeout} = $timeout;
3782 delete $item->{label};
3783 } else {
3784 $self->{item}{$group} = {
3785 id => ++$self->{id},
3786 text => $text,
3787 timeout => $timeout,
3788 tooltip => $text,
3789 fg => [0.8, 0.8, 0.8, 0.8],
3790 pri => 0,
3791 count => 1,
3792 %arg,
3793 };
3794 }
3795
3796 $ROOT->on_refresh (reorder => sub {
3797 $self->reorder;
3798 });
3799 }
3800
3801 sub reconfigure {
3802 my ($self) = @_;
3803
3804 delete $_->{label}
3805 for values %{ $self->{item} || {} };
3806
3807 $self->reorder;
3808 $self->SUPER::reconfigure;
3809 }
3810
3811 sub destroy {
3812 my ($self) = @_;
3813
3814 $self->{timer}->cancel;
3815
3816 $self->SUPER::destroy;
3817 }
3818
3819 #############################################################################
3820
3821 package CFPlus::UI::Root;
3822
3823 our @ISA = CFPlus::UI::Container::;
3824
3825 use List::Util qw(min max);
3826
3827 use CFPlus::OpenGL;
3828
3829 sub new {
3830 my $class = shift;
3831
3832 my $self = $class->SUPER::new (
3833 visible => 1,
3834 @_,
3835 );
3836
3837 CFPlus::weaken ($self->{root} = $self);
3838
3839 $self
3840 }
3841
3842 sub size_request {
3843 my ($self) = @_;
3844
3845 ($self->{w}, $self->{h})
3846 }
3847
3848 sub _to_pixel {
3849 my ($coord, $size, $max) = @_;
3850
3851 $coord =
3852 $coord eq "center" ? ($max - $size) * 0.5
3853 : $coord eq "max" ? $max
3854 : $coord;
3855
3856 $coord = 0 if $coord < 0;
3857 $coord = $max - $size if $coord > $max - $size;
3858
3859 int $coord + 0.5
3860 }
3861
3862 sub invoke_size_allocate {
3863 my ($self, $w, $h) = @_;
3864
3865 for my $child ($self->children) {
3866 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3867
3868 $X = $child->{force_x} if exists $child->{force_x};
3869 $Y = $child->{force_y} if exists $child->{force_y};
3870
3871 $X = _to_pixel $X, $W, $self->{w};
3872 $Y = _to_pixel $Y, $H, $self->{h};
3873
3874 $child->configure ($X, $Y, $W, $H);
3875 }
3876
3877 1
3878 }
3879
3880 sub coord2local {
3881 my ($self, $x, $y) = @_;
3882
3883 ($x, $y)
3884 }
3885
3886 sub coord2global {
3887 my ($self, $x, $y) = @_;
3888
3889 ($x, $y)
3890 }
3891
3892 sub update {
3893 my ($self) = @_;
3894
3895 $::WANT_REFRESH++;
3896 }
3897
3898 sub add {
3899 my ($self, @children) = @_;
3900
3901 $_->{is_toplevel} = 1
3902 for @children;
3903
3904 $self->SUPER::add (@children);
3905 }
3906
3907 sub remove {
3908 my ($self, @children) = @_;
3909
3910 $self->SUPER::remove (@children);
3911
3912 delete $self->{is_toplevel}
3913 for @children;
3914
3915 while (@children) {
3916 my $w = pop @children;
3917 push @children, $w->children;
3918 $w->set_invisible;
3919 }
3920 }
3921
3922 sub on_refresh {
3923 my ($self, $id, $cb) = @_;
3924
3925 $self->{refresh_hook}{$id} = $cb;
3926 }
3927
3928 sub on_post_alloc {
3929 my ($self, $id, $cb) = @_;
3930
3931 $self->{post_alloc_hook}{$id} = $cb;
3932 }
3933
3934 sub draw {
3935 my ($self) = @_;
3936
3937 while ($self->{refresh_hook}) {
3938 $_->()
3939 for values %{delete $self->{refresh_hook}};
3940 }
3941
3942 if ($self->{realloc}) {
3943 my %queue;
3944 my @queue;
3945 my $widget;
3946
3947 outer:
3948 while () {
3949 if (my $realloc = delete $self->{realloc}) {
3950 for $widget (values %$realloc) {
3951 $widget->{visible} or next; # do not resize invisible widgets
3952
3953 $queue{$widget+0}++ and next; # duplicates are common
3954
3955 push @{ $queue[$widget->{visible}] }, $widget;
3956 }
3957 }
3958
3959 while () {
3960 @queue or last outer;
3961
3962 $widget = pop @{ $queue[-1] || [] }
3963 and last;
3964
3965 pop @queue;
3966 }
3967
3968 delete $queue{$widget+0};
3969
3970 my ($w, $h) = $widget->size_request;
3971
3972 $w = max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3973 $h = max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3974
3975 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3976 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3977
3978 $w = $widget->{force_w} if exists $widget->{force_w};
3979 $h = $widget->{force_h} if exists $widget->{force_h};
3980
3981 if ($widget->{req_w} != $w || $widget->{req_h} != $h
3982 || delete $widget->{force_realloc}) {
3983 $widget->{req_w} = $w;
3984 $widget->{req_h} = $h;
3985
3986 $self->{size_alloc}{$widget+0} = $widget;
3987
3988 if (my $parent = $widget->{parent}) {
3989 $self->{realloc}{$parent+0} = $parent
3990 unless $queue{$parent+0};
3991
3992 $parent->{force_size_alloc} = 1;
3993 $self->{size_alloc}{$parent+0} = $parent;
3994 }
3995 }
3996
3997 delete $self->{realloc}{$widget+0};
3998 }
3999 }
4000
4001 while (my $size_alloc = delete $self->{size_alloc}) {
4002 my @queue = sort { $b->{visible} <=> $a->{visible} }
4003 values %$size_alloc;
4004
4005 while () {
4006 my $widget = pop @queue || last;
4007
4008 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
4009
4010 $w = 0 if $w < 0;
4011 $h = 0 if $h < 0;
4012
4013 $w = max $widget->{min_w}, $w;
4014 $h = max $widget->{min_h}, $h;
4015
4016 # $w = min $self->{w} - $widget->{x}, $w if $self->{w};
4017 # $h = min $self->{h} - $widget->{y}, $h if $self->{h};
4018
4019 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
4020 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
4021
4022 $w = int $w + 0.5;
4023 $h = int $h + 0.5;
4024
4025 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
4026 $widget->{old_w} = $widget->{w};
4027 $widget->{old_h} = $widget->{h};
4028
4029 $widget->{w} = $w;
4030 $widget->{h} = $h;
4031
4032 $widget->emit (size_allocate => $w, $h);
4033 }
4034 }
4035 }
4036
4037 while ($self->{post_alloc_hook}) {
4038 $_->()
4039 for values %{delete $self->{post_alloc_hook}};
4040 }
4041
4042
4043 glViewport 0, 0, $::WIDTH, $::HEIGHT;
4044 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
4045 glClear GL_COLOR_BUFFER_BIT;
4046
4047 glMatrixMode GL_PROJECTION;
4048 glLoadIdentity;
4049 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
4050 glMatrixMode GL_MODELVIEW;
4051 glLoadIdentity;
4052
4053 {
4054 package CFPlus::UI::Base;
4055
4056 local ($draw_x, $draw_y, $draw_w, $draw_h) =
4057 (0, 0, $self->{w}, $self->{h});
4058
4059 $self->_draw;
4060 }
4061 }
4062
4063 #############################################################################
4064
4065 package CFPlus::UI;
4066
4067 $ROOT = new CFPlus::UI::Root;
4068 $TOOLTIP = new CFPlus::UI::Tooltip z => 900;
4069
4070 1
4071