ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.330
Committed: Sun Jul 23 04:37:51 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Changes since 1.329: +78 -27 lines
Log Message:
preliminary mouse wheel support

File Contents

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