ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.373
Committed: Sun Jul 15 22:39:30 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.372: +37 -17 lines
Log Message:
be more flexible about face allocations (feels a bit hackish to have req_, alloc_, force_, size_ all do different things, though

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