ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.362
Committed: Sat Dec 9 21:26:46 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Changes since 1.361: +17 -12 lines
Log Message:
- do no longer depend on Scalar::Util for weaken
- use a different, cleaner, more correct and less efficient event propagation mechanism
- this fixes the segfault bug in map $_.

File Contents

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