ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.388
Committed: Fri Jul 20 18:30:03 2007 UTC (16 years, 10 months ago) by root
Branch: MAIN
Changes since 1.387: +10 -4 lines
Log Message:
support horizontal scrollwheel with shift

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