ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.361
Committed: Sat Dec 9 02:21:25 2006 UTC (17 years, 5 months ago) by root
Branch: MAIN
Changes since 1.360: +9 -187 lines
Log Message:
- slightly cleaned up and "outsourced" components
- completely rewrote keybind bindings
  - not functional yet
  - buggy a shell
  - hits perlbug

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