ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.363
Committed: Sun Dec 10 00:06:49 2006 UTC (17 years, 5 months ago) by root
Branch: MAIN
Changes since 1.362: +13 -3 lines
Log Message:
implement home/end i the most annoying way for multiline edits

File Contents

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