ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.439
Committed: Sun Sep 16 13:04:36 2007 UTC (16 years, 9 months ago) by root
Branch: MAIN
Changes since 1.438: +16 -2 lines
Log Message:
handle label widgets a tad more gracefully

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