ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.258
Committed: Tue May 30 02:55:45 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.257: +35 -29 lines
Log Message:
more tuning, implement generic (but suboptimal) padding

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.18
9 root 1.60 use CFClient;
10 root 1.240 use CFClient::Texture;
11 root 1.41
12 root 1.51 our ($FOCUS, $HOVER, $GRAB); # various widgets
13    
14 elmex 1.242 our $LAYOUT;
15 root 1.113 our $ROOT;
16 root 1.151 our $TOOLTIP;
17 root 1.51 our $BUTTON_STATE;
18    
19 root 1.202 our %WIDGET; # all widgets, weak-referenced
20    
21 elmex 1.242 sub get_layout {
22 root 1.256 my $layout;
23    
24 elmex 1.242 for (grep { $_->{name} } values %WIDGET) {
25 root 1.256 my $win = $layout->{$_->{name}} = { };
26    
27     $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
28     $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
29     $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
30     $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
31 root 1.258
32     $win->{show} = $_->{visible} && $_->{is_toplevel};
33 elmex 1.242 }
34    
35 root 1.256 $layout
36 elmex 1.242 }
37    
38     sub set_layout {
39     my ($layout) = @_;
40 root 1.258
41 elmex 1.242 $LAYOUT = $layout;
42     }
43    
44 root 1.151 sub check_tooltip {
45     if (!$GRAB) {
46     for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
47 root 1.160 if (length $widget->{tooltip}) {
48 root 1.151
49     if ($TOOLTIP->{owner} != $widget) {
50 root 1.253 $TOOLTIP->hide;
51    
52 root 1.151 $TOOLTIP->{owner} = $widget;
53 root 1.155
54     my $tip = $widget->{tooltip};
55    
56     $tip = $tip->($widget) if CODE:: eq ref $tip;
57    
58 root 1.196 $TOOLTIP->set_tooltip_from ($widget);
59 root 1.252 $TOOLTIP->show;
60 root 1.151 }
61    
62     return;
63     }
64     }
65     }
66    
67     $TOOLTIP->hide;
68     delete $TOOLTIP->{owner};
69     }
70    
71 elmex 1.1 # class methods for events
72 root 1.51 sub feed_sdl_key_down_event {
73 root 1.231 $FOCUS->emit (key_down => $_[0])
74 root 1.163 if $FOCUS;
75 root 1.51 }
76    
77     sub feed_sdl_key_up_event {
78 root 1.231 $FOCUS->emit (key_up => $_[0])
79 root 1.163 if $FOCUS;
80 root 1.51 }
81    
82     sub feed_sdl_button_down_event {
83     my ($ev) = @_;
84 root 1.137 my ($x, $y) = ($ev->{x}, $ev->{y});
85 root 1.51
86     if (!$BUTTON_STATE) {
87 root 1.113 my $widget = $ROOT->find_widget ($x, $y);
88 root 1.51
89     $GRAB = $widget;
90     $GRAB->update if $GRAB;
91 root 1.151
92     check_tooltip;
93 root 1.51 }
94    
95 root 1.137 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
96 root 1.51
97 root 1.231 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
98     if $GRAB;
99 root 1.51 }
100    
101     sub feed_sdl_button_up_event {
102     my ($ev) = @_;
103 root 1.137 my ($x, $y) = ($ev->{x}, $ev->{y});
104 root 1.51
105 root 1.113 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
106 root 1.51
107 root 1.137 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
108 root 1.51
109 root 1.231 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
110     if $GRAB;
111 root 1.58
112 root 1.51 if (!$BUTTON_STATE) {
113     my $grab = $GRAB; undef $GRAB;
114     $grab->update if $grab;
115     $GRAB->update if $GRAB;
116 root 1.151
117     check_tooltip;
118 root 1.51 }
119     }
120    
121     sub feed_sdl_motion_event {
122     my ($ev) = @_;
123 root 1.137 my ($x, $y) = ($ev->{x}, $ev->{y});
124 root 1.51
125 root 1.113 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
126 root 1.51
127     if ($widget != $HOVER) {
128     my $hover = $HOVER; $HOVER = $widget;
129    
130 root 1.105 $hover->update if $hover && $hover->{can_hover};
131     $HOVER->update if $HOVER && $HOVER->{can_hover};
132 root 1.151
133     check_tooltip;
134 root 1.51 }
135    
136 root 1.231 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
137     if $HOVER;
138 root 1.51 }
139 elmex 1.1
140 root 1.112 # convert position array to integers
141     sub harmonize {
142     my ($vals) = @_;
143    
144     my $rem = 0;
145    
146 root 1.116 for (@$vals) {
147 root 1.112 my $i = int $_ + $rem;
148     $rem += $_ - $i;
149     $_ = $i;
150     }
151     }
152    
153 root 1.220 sub full_refresh {
154     # make a copy, otherwise for complains about freed values.
155     my @widgets = values %WIDGET;
156    
157     $_->update
158     for @widgets;
159     }
160    
161 root 1.230 sub reconfigure_widgets {
162     # make a copy, otherwise C<for> complains about freed values.
163     my @widgets = values %WIDGET;
164    
165     $_->reconfigure
166     for @widgets;
167     }
168    
169 root 1.202 # call when resolution changes etc.
170     sub rescale_widgets {
171     my ($sx, $sy) = @_;
172    
173 root 1.230 for my $widget (values %WIDGET) {
174     if ($widget->{is_toplevel}) {
175 root 1.256 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
176     $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
177    
178     $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
179     $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
180     $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
181     $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
182     $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
183     $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
184    
185     $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
186     $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
187    
188 root 1.202 }
189 root 1.230 }
190 root 1.202
191 root 1.230 reconfigure_widgets;
192 root 1.202 }
193    
194 root 1.73 #############################################################################
195    
196     package CFClient::UI::Base;
197    
198     use strict;
199    
200 root 1.138 use CFClient::OpenGL;
201 root 1.73
202 elmex 1.1 sub new {
203     my $class = shift;
204 root 1.10
205 root 1.79 my $self = bless {
206 root 1.256 x => "center",
207     y => "center",
208 root 1.164 z => 0,
209 root 1.256 w => undef,
210     h => undef,
211 elmex 1.150 can_events => 1,
212 root 1.65 @_
213 root 1.79 }, $class;
214    
215 root 1.258 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
216    
217     for (keys %$self) {
218     if (/^on_(.*)$/) {
219     $self->connect ($1 => delete $self->{$_});
220     }
221     }
222    
223 root 1.256 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
224     $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x};
225     $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y};
226     $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w};
227     $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h};
228    
229     $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
230     $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
231    
232 root 1.258 $self->show if $layout->{show};
233 root 1.79 }
234    
235     $self
236 elmex 1.1 }
237    
238 root 1.159 sub destroy {
239     my ($self) = @_;
240    
241     $self->hide;
242     %$self = ();
243     }
244    
245 root 1.134 sub show {
246     my ($self) = @_;
247 root 1.248
248 root 1.134 return if $self->{parent};
249    
250     $CFClient::UI::ROOT->add ($self);
251     }
252    
253 root 1.246 sub set_visible {
254     my ($self) = @_;
255    
256     return if $self->{visible};
257    
258     $self->{root} = $self->{parent}{root};
259     $self->{visible} = $self->{parent}{visible} + 1;
260    
261 root 1.248 $self->emit (visibility_change => 1);
262 root 1.251
263 root 1.252 $self->realloc if !exists $self->{req_w};
264 root 1.251
265     $_->set_visible for $self->children;
266 root 1.246 }
267    
268 root 1.231 sub set_invisible {
269 root 1.134 my ($self) = @_;
270    
271 root 1.244 return unless $self->{visible};
272    
273 root 1.251 $_->set_invisible for $self->children;
274 root 1.231
275 root 1.241 delete $self->{root};
276 root 1.231 delete $self->{visible};
277    
278 root 1.163 undef $GRAB if $GRAB == $self;
279     undef $HOVER if $HOVER == $self;
280 root 1.134
281 root 1.232 CFClient::UI::check_tooltip
282 root 1.251 if $TOOLTIP->{owner} == $self;
283 root 1.232
284 root 1.231 $self->focus_out;
285 root 1.244
286     $self->emit (visibility_change => 0);
287 root 1.231 }
288    
289 root 1.250 sub set_visibility {
290     my ($self, $visible) = @_;
291    
292     return if $self->{visible} == $visible;
293    
294     $visible ? $self->hide
295     : $self->show;
296     }
297    
298     sub toggle_visibility {
299     my ($self) = @_;
300    
301     $self->{visible}
302     ? $self->hide
303     : $self->show;
304     }
305    
306 root 1.231 sub hide {
307     my ($self) = @_;
308    
309     $self->set_invisible;
310    
311 root 1.163 $self->{parent}->remove ($self)
312     if $self->{parent};
313 root 1.134 }
314    
315 root 1.256 sub move_abs {
316 root 1.18 my ($self, $x, $y, $z) = @_;
317 root 1.128
318 root 1.256 $self->{x} = List::Util::max 0, int $x;
319     $self->{y} = List::Util::max 0, int $y;
320 root 1.18 $self->{z} = $z if defined $z;
321 root 1.128
322     $self->update;
323 root 1.18 }
324    
325 root 1.158 sub set_size {
326     my ($self, $w, $h) = @_;
327    
328 root 1.256 $self->{force_w} = $w;
329     $self->{force_h} = $h;
330 root 1.158
331 root 1.251 $self->realloc;
332 elmex 1.20 }
333    
334 root 1.14 sub size_request {
335 elmex 1.36 require Carp;
336 root 1.147 Carp::confess "size_request is abstract";
337 elmex 1.36 }
338    
339 root 1.128 sub configure {
340 root 1.75 my ($self, $x, $y, $w, $h) = @_;
341    
342 root 1.141 if ($self->{aspect}) {
343 root 1.256 my ($ow, $oh) = ($w, $h);
344    
345     $w = List::Util::min $w, int $h * $self->{aspect};
346     $h = List::Util::min $h, int $w / $self->{aspect};
347 root 1.141
348     # use alignment to adjust x, y
349 root 1.75
350 root 1.256 $x += int 0.5 * ($ow - $w);
351     $y += int 0.5 * ($oh - $h);
352 root 1.140 }
353    
354 root 1.256 if ($self->{x} ne $x || $self->{y} ne $y) {
355 root 1.141 $self->{x} = $x;
356     $self->{y} = $y;
357     $self->update;
358     }
359 root 1.40
360 root 1.141 if ($self->{w} != $w || $self->{h} != $h) {
361 root 1.251 return unless $self->{visible};
362    
363     $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
364 root 1.141 }
365 root 1.75 }
366    
367     sub size_allocate {
368 root 1.128 # nothing to be done
369 root 1.14 }
370    
371 root 1.202 sub children {
372     }
373    
374 root 1.157 sub set_max_size {
375     my ($self, $w, $h) = @_;
376    
377     delete $self->{max_w}; $self->{max_w} = $w if $w;
378     delete $self->{max_h}; $self->{max_h} = $h if $h;
379     }
380    
381 root 1.209 sub set_tooltip {
382     my ($self, $tooltip) = @_;
383    
384 root 1.234 $tooltip =~ s/^\s+//;
385     $tooltip =~ s/\s+$//;
386    
387     return if $self->{tooltip} eq $tooltip;
388    
389 root 1.209 $self->{tooltip} = $tooltip;
390    
391     if ($CFClient::UI::TOOLTIP->{owner} == $self) {
392     delete $CFClient::UI::TOOLTIP->{owner};
393     CFClient::UI::check_tooltip;
394     }
395     }
396    
397 root 1.82 # translate global coordinates to local coordinate system
398 root 1.113 sub coord2local {
399 root 1.58 my ($self, $x, $y) = @_;
400    
401 root 1.191 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
402 root 1.113 }
403    
404     # translate local coordinates to global coordinate system
405     sub coord2global {
406     my ($self, $x, $y) = @_;
407    
408 root 1.191 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
409 root 1.58 }
410    
411 elmex 1.1 sub focus_in {
412 root 1.51 my ($self) = @_;
413    
414 root 1.68 return if $FOCUS == $self;
415 root 1.97 return unless $self->{can_focus};
416 root 1.68
417 root 1.51 my $focus = $FOCUS; $FOCUS = $self;
418 elmex 1.120
419 root 1.231 $self->_emit (focus_in => $focus);
420 elmex 1.120
421 root 1.51 $focus->update if $focus;
422     $FOCUS->update;
423 elmex 1.1 }
424 root 1.4
425 elmex 1.1 sub focus_out {
426 root 1.51 my ($self) = @_;
427 root 1.4
428 root 1.51 return unless $FOCUS == $self;
429 root 1.4
430 root 1.51 my $focus = $FOCUS; undef $FOCUS;
431 elmex 1.120
432 root 1.231 $self->_emit (focus_out => $focus);
433 elmex 1.120
434 root 1.51 $focus->update if $focus; #?
435 root 1.231
436     $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
437     unless $FOCUS;
438 elmex 1.1 }
439 root 1.4
440 root 1.51 sub mouse_motion { }
441 root 1.231 sub button_up { }
442     sub key_down { }
443     sub key_up { }
444 root 1.51
445 root 1.68 sub button_down {
446     my ($self, $ev, $x, $y) = @_;
447    
448     $self->focus_in;
449     }
450    
451 root 1.51 sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
452     sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
453     sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
454     sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
455     sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
456 elmex 1.11
457 root 1.251 sub find_widget {
458     my ($self, $x, $y) = @_;
459    
460     return () unless $self->{can_events};
461    
462     return $self
463     if $x >= $self->{x} && $x < $self->{x} + $self->{w}
464     && $y >= $self->{y} && $y < $self->{y} + $self->{h};
465    
466     ()
467     }
468    
469     sub set_parent {
470     my ($self, $parent) = @_;
471    
472     Scalar::Util::weaken ($self->{parent} = $parent);
473     $self->set_visible if $parent->{visible};
474     }
475    
476     sub connect {
477     my ($self, $signal, $cb) = @_;
478    
479     push @{ $self->{signal_cb}{$signal} }, $cb;
480     }
481    
482     sub _emit {
483     my ($self, $signal, @args) = @_;
484    
485     List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
486     }
487    
488     sub emit {
489     my ($self, $signal, @args) = @_;
490    
491     $self->_emit ($signal, @args)
492     || $self->$signal (@args);
493     }
494    
495     sub visibility_change {
496     #my ($self, $visible) = @_;
497     }
498    
499     sub realloc {
500     my ($self) = @_;
501    
502 root 1.252 if ($self->{visible}) {
503     return if $self->{root}{realloc}{$self};
504 root 1.251
505 root 1.252 $self->{root}{realloc}{$self} = $self;
506     $self->{root}->update;
507     } else {
508     delete $self->{req_w};
509     }
510 root 1.251 }
511    
512     sub update {
513     my ($self) = @_;
514    
515     $self->{parent}->update
516     if $self->{parent};
517     }
518    
519 root 1.256 sub reconfigure {
520     my ($self) = @_;
521    
522     $self->realloc;
523     $self->update;
524     }
525    
526 elmex 1.1 sub draw {
527 elmex 1.11 my ($self) = @_;
528    
529 root 1.68 return unless $self->{h} && $self->{w};
530    
531 elmex 1.11 glPushMatrix;
532 root 1.12 glTranslate $self->{x}, $self->{y}, 0;
533 elmex 1.11 $self->_draw;
534 root 1.72 glPopMatrix;
535    
536 root 1.97 if ($self == $HOVER && $self->{can_hover}) {
537 root 1.73 my ($x, $y) = @$self{qw(x y)};
538 root 1.72
539 root 1.122 glColor 1, 0.8, 0.5, 0.2;
540 root 1.50 glEnable GL_BLEND;
541 root 1.74 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
542 root 1.48 glBegin GL_QUADS;
543 root 1.72 glVertex $x , $y;
544     glVertex $x + $self->{w}, $y;
545     glVertex $x + $self->{w}, $y + $self->{h};
546     glVertex $x , $y + $self->{h};
547 root 1.47 glEnd;
548 root 1.50 glDisable GL_BLEND;
549 root 1.47 }
550 root 1.162
551 root 1.256 if ($ENV{CFPLUS_DEBUG}) {
552 root 1.162 glPushMatrix;
553     glColor 1, 1, 0, 1;
554     glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
555     glBegin GL_LINE_LOOP;
556 root 1.234 glVertex 0 , 0;
557     glVertex $self->{w} - 1, 0;
558     glVertex $self->{w} - 1, $self->{h} - 1;
559     glVertex 0 , $self->{h} - 1;
560 root 1.162 glEnd;
561     glPopMatrix;
562 root 1.205 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
563 root 1.162 }
564 elmex 1.11 }
565    
566     sub _draw {
567 root 1.38 my ($self) = @_;
568    
569     warn "no draw defined for $self\n";
570 elmex 1.1 }
571 root 1.4
572 root 1.18 sub DESTROY {
573     my ($self) = @_;
574    
575 root 1.202 delete $WIDGET{$self+0};
576 elmex 1.32 #$self->deactivate;
577 root 1.18 }
578    
579 root 1.39 #############################################################################
580    
581 root 1.73 package CFClient::UI::DrawBG;
582 root 1.68
583 root 1.73 our @ISA = CFClient::UI::Base::;
584 root 1.68
585     use strict;
586 root 1.138 use CFClient::OpenGL;
587 root 1.68
588     sub new {
589     my $class = shift;
590    
591     # range [value, low, high, page]
592    
593     $class->SUPER::new (
594 root 1.209 #bg => [0, 0, 0, 0.2],
595     #active_bg => [1, 1, 1, 0.5],
596 root 1.68 @_
597     )
598     }
599    
600     sub _draw {
601     my ($self) = @_;
602    
603 root 1.209 my $color = $FOCUS == $self && $self->{active_bg}
604     ? $self->{active_bg}
605     : $self->{bg};
606    
607     if ($color && (@$color < 4 || $color->[3])) {
608     my ($w, $h) = @$self{qw(w h)};
609 root 1.68
610 root 1.209 glEnable GL_BLEND;
611     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
612     glColor @$color;
613 root 1.76
614 root 1.209 glBegin GL_QUADS;
615     glVertex 0 , 0;
616     glVertex 0 , $h;
617     glVertex $w, $h;
618     glVertex $w, 0;
619     glEnd;
620 root 1.76
621 root 1.209 glDisable GL_BLEND;
622     }
623 root 1.68 }
624    
625     #############################################################################
626    
627 root 1.73 package CFClient::UI::Empty;
628 root 1.66
629 root 1.73 our @ISA = CFClient::UI::Base::;
630 root 1.66
631 elmex 1.150 sub new {
632     my ($class, %arg) = @_;
633     $class->SUPER::new (can_events => 0, %arg);
634     }
635    
636 root 1.66 sub size_request {
637 root 1.256 my ($self) = @_;
638    
639     ($self->{w} + 0, $self->{h} + 0)
640 root 1.66 }
641    
642 root 1.67 sub draw { }
643 root 1.66
644     #############################################################################
645    
646 root 1.73 package CFClient::UI::Container;
647 elmex 1.15
648 root 1.73 our @ISA = CFClient::UI::Base::;
649 elmex 1.15
650 root 1.38 sub new {
651 root 1.64 my ($class, %arg) = @_;
652    
653     my $children = delete $arg{children} || [];
654 root 1.38
655 root 1.166 my $self = $class->SUPER::new (
656     children => [],
657     can_events => 0,
658     %arg,
659     );
660 root 1.64 $self->add ($_) for @$children;
661 root 1.38
662     $self
663     }
664    
665     sub add {
666 root 1.188 my ($self, @widgets) = @_;
667 root 1.113
668 root 1.188 $_->set_parent ($self)
669     for @widgets;
670 root 1.38
671 root 1.113 use sort 'stable';
672 root 1.38
673 root 1.66 $self->{children} = [
674 root 1.38 sort { $a->{z} <=> $b->{z} }
675 root 1.188 @{$self->{children}}, @widgets
676 root 1.66 ];
677 root 1.38
678 root 1.251 $self->realloc;
679 root 1.38 }
680 root 1.35
681 root 1.172 sub children {
682     @{ $_[0]{children} }
683     }
684    
685 elmex 1.32 sub remove {
686 root 1.134 my ($self, $child) = @_;
687    
688     delete $child->{parent};
689 root 1.163 $child->hide;
690 root 1.38
691 root 1.134 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
692 root 1.38
693 root 1.251 $self->realloc;
694 root 1.38 }
695    
696 root 1.162 sub clear {
697     my ($self) = @_;
698    
699 root 1.163 my $children = delete $self->{children};
700     $self->{children} = [];
701 root 1.162
702 root 1.163 for (@$children) {
703     delete $_->{parent};
704     $_->hide;
705     }
706 root 1.182
707 root 1.251 $self->realloc;
708 root 1.162 }
709    
710 root 1.38 sub find_widget {
711     my ($self, $x, $y) = @_;
712    
713 root 1.45 $x -= $self->{x};
714     $y -= $self->{y};
715    
716 root 1.38 my $res;
717    
718 root 1.46 for (reverse @{ $self->{children} }) {
719 root 1.45 $res = $_->find_widget ($x, $y)
720 root 1.38 and return $res;
721     }
722    
723 root 1.46 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
724 elmex 1.32 }
725 elmex 1.15
726 root 1.35 sub _draw {
727     my ($self) = @_;
728    
729 root 1.38 $_->draw for @{$self->{children}};
730 root 1.35 }
731 elmex 1.15
732 root 1.39 #############################################################################
733    
734 root 1.73 package CFClient::UI::Bin;
735 elmex 1.32
736 root 1.73 our @ISA = CFClient::UI::Container::;
737 elmex 1.32
738 root 1.66 sub new {
739     my ($class, %arg) = @_;
740    
741 root 1.73 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
742 root 1.66
743     $class->SUPER::new (children => [$child], %arg)
744     }
745    
746     sub add {
747 root 1.134 my ($self, $child) = @_;
748 root 1.66
749     $self->{children} = [];
750    
751 root 1.134 $self->SUPER::add ($child);
752 root 1.66 }
753    
754     sub remove {
755     my ($self, $widget) = @_;
756    
757     $self->SUPER::remove ($widget);
758    
759 root 1.73 $self->{children} = [new CFClient::UI::Empty]
760 root 1.66 unless @{$self->{children}};
761     }
762    
763 root 1.39 sub child { $_[0]->{children}[0] }
764 elmex 1.32
765 root 1.38 sub size_request {
766 root 1.68 $_[0]{children}[0]->size_request
767 root 1.38 }
768 elmex 1.32
769 root 1.38 sub size_allocate {
770 root 1.251 my ($self, $w, $h, $changed) = @_;
771 root 1.42
772 root 1.128 $self->{children}[0]->configure (0, 0, $w, $h);
773 root 1.38 }
774 elmex 1.32
775 root 1.39 #############################################################################
776    
777 root 1.73 package CFClient::UI::Window;
778 elmex 1.20
779 root 1.73 our @ISA = CFClient::UI::Bin::;
780 elmex 1.20
781 root 1.138 use CFClient::OpenGL;
782 elmex 1.20
783 root 1.42 sub new {
784 root 1.64 my ($class, %arg) = @_;
785 elmex 1.32
786 root 1.64 my $self = $class->SUPER::new (%arg);
787 elmex 1.32 }
788    
789     sub update {
790     my ($self) = @_;
791 root 1.42
792 root 1.198 $ROOT->on_post_alloc ($self => sub { $self->render_child });
793 root 1.42 $self->SUPER::update;
794 elmex 1.20 }
795    
796 root 1.174 sub size_allocate {
797 root 1.251 my ($self, $w, $h, $changed) = @_;
798 root 1.174
799 root 1.251 $self->SUPER::size_allocate ($w, $h, $changed);
800     $self->update
801     if $changed;
802 root 1.174 }
803    
804 root 1.182 sub _render {
805     $_[0]{children}[0]->draw;
806     }
807    
808 root 1.174 sub render_child {
809 elmex 1.20 my ($self) = @_;
810    
811 root 1.105 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
812 root 1.216 glClearColor 0, 0, 0, 0;
813 root 1.105 glClear GL_COLOR_BUFFER_BIT;
814 root 1.182
815     $self->_render;
816 root 1.105 };
817 elmex 1.36 }
818    
819 elmex 1.20 sub _draw {
820     my ($self) = @_;
821    
822 elmex 1.36 my ($w, $h) = ($self->w, $self->h);
823 root 1.29
824 elmex 1.20 my $tex = $self->{texture}
825     or return;
826    
827     glEnable GL_TEXTURE_2D;
828 root 1.35 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
829 root 1.215 glColor 1, 1, 1, 1;
830 elmex 1.20
831 root 1.194 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
832 elmex 1.20
833     glDisable GL_TEXTURE_2D;
834     }
835    
836 root 1.39 #############################################################################
837    
838 root 1.125 package CFClient::UI::ViewPort;
839    
840     our @ISA = CFClient::UI::Window::;
841    
842 root 1.234 sub new {
843     my $class = shift;
844    
845     $class->SUPER::new (
846     scroll_x => 0,
847     scroll_y => 1,
848     @_,
849     )
850     }
851    
852 root 1.125 sub size_request {
853     my ($self) = @_;
854    
855 root 1.247 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
856    
857     $w = 10 if $self->{scroll_x};
858     $h = 10 if $self->{scroll_y};
859 root 1.125
860 root 1.247 ($w, $h)
861 root 1.125 }
862    
863 root 1.182 sub size_allocate {
864 root 1.251 my ($self, $w, $h, $changed) = @_;
865 root 1.182
866 root 1.234 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
867     $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
868    
869     $self->child->configure (0, 0, $w, $h);
870 root 1.182 $self->update;
871     }
872    
873     sub set_offset {
874     my ($self, $x, $y) = @_;
875    
876     $self->{view_x} = int $x;
877     $self->{view_y} = int $y;
878    
879     $self->update;
880     }
881    
882 root 1.191 # hmm, this does not work for topleft of $self... but we should not ask for that
883     sub coord2local {
884     my ($self, $x, $y) = @_;
885    
886     $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
887     }
888    
889     sub coord2global {
890 root 1.184 my ($self, $x, $y) = @_;
891    
892 root 1.191 $x = List::Util::min $self->{w}, $x - $self->{view_x};
893     $y = List::Util::min $self->{h}, $y - $self->{view_y};
894    
895     $self->SUPER::coord2global ($x, $y)
896 root 1.184 }
897    
898     sub find_widget {
899     my ($self, $x, $y) = @_;
900    
901     if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
902     && $y >= $self->{y} && $y < $self->{y} + $self->{h}
903     ) {
904     $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
905     } else {
906     $self->CFClient::UI::Base::find_widget ($x, $y)
907     }
908     }
909    
910 root 1.182 sub _render {
911 root 1.125 my ($self) = @_;
912    
913 root 1.182 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
914    
915     $self->SUPER::_render;
916     }
917    
918     #############################################################################
919    
920     package CFClient::UI::ScrolledWindow;
921    
922     our @ISA = CFClient::UI::HBox::;
923    
924     sub new {
925     my $class = shift;
926    
927     my $self;
928    
929     my $slider = new CFClient::UI::Slider
930 root 1.243 vertical => 1,
931     range => [0, 0, 1, 0.01], # HACK fix
932     on_changed => sub {
933 root 1.229 $self->{vp}->set_offset (0, $_[1]);
934 root 1.182 },
935     ;
936    
937     $self = $class->SUPER::new (
938 root 1.217 vp => (new CFClient::UI::ViewPort expand => 1),
939 root 1.182 slider => $slider,
940     @_,
941     );
942    
943     $self->{vp}->add ($self->{scrolled});
944     $self->add ($self->{vp});
945     $self->add ($self->{slider});
946    
947     $self
948 root 1.125 }
949    
950 root 1.239 sub update {
951     my ($self) = @_;
952    
953     $self->SUPER::update;
954    
955     # todo: overwrite size_allocate of child
956     my $child = $self->{vp}->child;
957     $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
958     }
959    
960 root 1.229 sub size_allocate {
961 root 1.251 my ($self, $w, $h, $changed) = @_;
962 root 1.229
963 root 1.251 $self->SUPER::size_allocate ($w, $h, $changed);
964 root 1.229
965     my $child = $self->{vp}->child;
966     $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
967     }
968    
969 root 1.199 #TODO# update range on size_allocate depending on child
970 root 1.182 # update viewport offset on scroll
971 root 1.125
972     #############################################################################
973    
974 root 1.73 package CFClient::UI::Frame;
975 elmex 1.15
976 root 1.73 our @ISA = CFClient::UI::Bin::;
977 elmex 1.15
978 root 1.138 use CFClient::OpenGL;
979 elmex 1.15
980 root 1.199 sub new {
981     my $class = shift;
982    
983     $class->SUPER::new (
984     bg => undef,
985     @_,
986     )
987     }
988    
989     sub _draw {
990     my ($self) = @_;
991    
992     if ($self->{bg}) {
993     my ($w, $h) = @$self{qw(w h)};
994    
995     glEnable GL_BLEND;
996     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
997     glColor @{ $self->{bg} };
998    
999     glBegin GL_QUADS;
1000     glVertex 0 , 0;
1001     glVertex 0 , $h;
1002     glVertex $w, $h;
1003     glVertex $w, 0;
1004     glEnd;
1005    
1006     glDisable GL_BLEND;
1007     }
1008    
1009     $self->SUPER::_draw;
1010     }
1011    
1012 root 1.39 #############################################################################
1013    
1014 root 1.73 package CFClient::UI::FancyFrame;
1015 elmex 1.31
1016 root 1.73 our @ISA = CFClient::UI::Bin::;
1017 elmex 1.31
1018 root 1.138 use CFClient::OpenGL;
1019 elmex 1.31
1020 root 1.255 my $bg =
1021     new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1022     mipmap => 1, wrap => 1;
1023    
1024     my @border =
1025 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1026 root 1.255 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1027 elmex 1.34
1028 root 1.97 sub new {
1029     my $class = shift;
1030    
1031 root 1.141 my $self = $class->SUPER::new (
1032 root 1.230 bg => [1, 1, 1, 1],
1033     border_bg => [1, 1, 1, 1],
1034     border => 0.6,
1035     can_events => 1,
1036 root 1.258 min_w => 16,
1037     min_h => 16,
1038 root 1.97 @_
1039 root 1.141 );
1040    
1041     $self->{title} &&= new CFClient::UI::Label
1042     align => 0,
1043     valign => 1,
1044     text => $self->{title},
1045 root 1.176 fontsize => $self->{border};
1046 root 1.141
1047     $self
1048 root 1.97 }
1049    
1050 root 1.134 sub border {
1051     int $_[0]{border} * $::FONTSIZE
1052     }
1053    
1054 elmex 1.34 sub size_request {
1055     my ($self) = @_;
1056 root 1.39
1057 root 1.78 my ($w, $h) = $self->SUPER::size_request;
1058 elmex 1.34
1059 root 1.97 (
1060 root 1.134 $w + $self->border * 2,
1061     $h + $self->border * 2,
1062 root 1.97 )
1063 elmex 1.36 }
1064    
1065     sub size_allocate {
1066 root 1.251 my ($self, $w, $h, $changed) = @_;
1067    
1068     return unless $changed;
1069 root 1.40
1070 root 1.134 $h -= List::Util::max 0, $self->border * 2;
1071     $w -= List::Util::max 0, $self->border * 2;
1072 elmex 1.36
1073 root 1.208 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1074 root 1.141 if $self->{title};
1075    
1076 root 1.134 $self->child->configure ($self->border, $self->border, $w, $h);
1077 elmex 1.34 }
1078    
1079 root 1.77 sub button_down {
1080     my ($self, $ev, $x, $y) = @_;
1081    
1082 root 1.176 my ($w, $h) = @$self{qw(w h)};
1083 root 1.134 my $border = $self->border;
1084    
1085 root 1.176 my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w);
1086     my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h);
1087 root 1.77
1088 root 1.176 if ($lr & $td) {
1089     my ($wx, $wy) = ($self->{x}, $self->{y});
1090 root 1.139 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1091 root 1.77 my ($bw, $bh) = ($self->{w}, $self->{h});
1092    
1093 root 1.176 my $mx = $x < $border;
1094     my $my = $y < $border;
1095    
1096 root 1.77 $self->{motion} = sub {
1097     my ($ev, $x, $y) = @_;
1098    
1099 root 1.176 my $dx = $ev->{x} - $ox;
1100     my $dy = $ev->{y} - $oy;
1101 root 1.77
1102 root 1.256 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1103     $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1104    
1105 root 1.251 $self->realloc;
1106 root 1.256 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1107 root 1.77 };
1108    
1109 root 1.176 } elsif ($lr ^ $td) {
1110 root 1.139 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1111 root 1.77 my ($bx, $by) = ($self->{x}, $self->{y});
1112    
1113     $self->{motion} = sub {
1114     my ($ev, $x, $y) = @_;
1115    
1116 root 1.139 ($x, $y) = ($ev->{x}, $ev->{y});
1117 root 1.77
1118 root 1.256 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1119 root 1.77 };
1120     }
1121     }
1122    
1123     sub button_up {
1124     my ($self, $ev, $x, $y) = @_;
1125    
1126     delete $self->{motion};
1127     }
1128    
1129     sub mouse_motion {
1130     my ($self, $ev, $x, $y) = @_;
1131    
1132     $self->{motion}->($ev, $x, $y) if $self->{motion};
1133     }
1134    
1135 elmex 1.34 sub _draw {
1136     my ($self) = @_;
1137    
1138 root 1.97 my ($w, $h ) = ($self->{w}, $self->{h});
1139 root 1.43 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
1140 elmex 1.34
1141     glEnable GL_TEXTURE_2D;
1142 root 1.97 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1143 elmex 1.34
1144 root 1.134 my $border = $self->border;
1145    
1146 root 1.97 glColor @{ $self->{border_bg} };
1147 root 1.255 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1148     $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1149     $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1150     $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1151 elmex 1.34
1152 root 1.177 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1153 root 1.255 glColor @{ $self->{bg} };
1154 root 1.76
1155 root 1.177 # TODO: repeat texture not scale
1156 root 1.255 # solve this better(?)
1157     $bg->{s} = $cw / $bg->{w};
1158     $bg->{t} = $ch / $bg->{h};
1159 root 1.197 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1160     }
1161 elmex 1.34
1162 root 1.197 glDisable GL_TEXTURE_2D;
1163 elmex 1.36
1164 root 1.141 $self->{title}->draw if $self->{title};
1165 root 1.177
1166 root 1.39 $self->child->draw;
1167 elmex 1.34 }
1168 elmex 1.31
1169 root 1.39 #############################################################################
1170    
1171 root 1.73 package CFClient::UI::Table;
1172 elmex 1.15
1173 root 1.73 our @ISA = CFClient::UI::Base::;
1174 elmex 1.15
1175 root 1.75 use List::Util qw(max sum);
1176    
1177 root 1.138 use CFClient::OpenGL;
1178 elmex 1.15
1179 root 1.78 sub new {
1180     my $class = shift;
1181    
1182     $class->SUPER::new (
1183     col_expand => [],
1184 root 1.234 @_,
1185 root 1.78 )
1186     }
1187    
1188 root 1.236 sub children {
1189     grep $_, map @$_, grep $_, @{ $_[0]{children} }
1190     }
1191    
1192 elmex 1.15 sub add {
1193 root 1.113 my ($self, $x, $y, $child) = @_;
1194 elmex 1.32
1195 root 1.113 $child->set_parent ($self);
1196     $self->{children}[$y][$x] = $child;
1197 root 1.75
1198 root 1.251 $self->realloc;
1199 root 1.172 }
1200    
1201 root 1.236 # TODO: move to container class maybe? send children a signal on removal?
1202 root 1.115 sub clear {
1203     my ($self) = @_;
1204    
1205 root 1.172 my @children = $self->children;
1206     delete $self->{children};
1207 root 1.163
1208 root 1.172 for (@children) {
1209 root 1.163 delete $_->{parent};
1210     $_->hide;
1211     }
1212    
1213 root 1.251 $self->realloc;
1214 root 1.115 }
1215    
1216 root 1.75 sub get_wh {
1217     my ($self) = @_;
1218    
1219     my (@w, @h);
1220 elmex 1.15
1221 root 1.75 for my $y (0 .. $#{$self->{children}}) {
1222     my $row = $self->{children}[$y]
1223     or next;
1224 elmex 1.15
1225 root 1.75 for my $x (0 .. $#$row) {
1226     my $widget = $row->[$x]
1227     or next;
1228 root 1.149 my ($w, $h) = @$widget{qw(req_w req_h)};
1229 elmex 1.15
1230 root 1.75 $w[$x] = max $w[$x], $w;
1231     $h[$y] = max $h[$y], $h;
1232 elmex 1.17 }
1233 elmex 1.15 }
1234 root 1.75
1235     (\@w, \@h)
1236 elmex 1.15 }
1237    
1238     sub size_request {
1239     my ($self) = @_;
1240    
1241 root 1.75 my ($ws, $hs) = $self->get_wh;
1242 elmex 1.15
1243 root 1.75 (
1244 root 1.78 (sum @$ws),
1245     (sum @$hs),
1246 root 1.75 )
1247     }
1248    
1249     sub size_allocate {
1250 root 1.251 my ($self, $w, $h, $changed) = @_;
1251 root 1.75
1252     my ($ws, $hs) = $self->get_wh;
1253    
1254 root 1.238 my $req_w = (sum @$ws) || 1;
1255     my $req_h = (sum @$hs) || 1;
1256 root 1.78
1257     # TODO: nicer code && do row_expand
1258     my @col_expand = @{$self->{col_expand}};
1259     @col_expand = (1) x @$ws unless @col_expand;
1260     my $col_expand = (sum @col_expand) || 1;
1261 elmex 1.15
1262 root 1.75 # linearly scale sizes
1263 root 1.78 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
1264     $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
1265 elmex 1.15
1266 root 1.112 CFClient::UI::harmonize $ws;
1267     CFClient::UI::harmonize $hs;
1268 root 1.106
1269 root 1.75 my $y;
1270 elmex 1.15
1271 root 1.75 for my $r (0 .. $#{$self->{children}}) {
1272     my $row = $self->{children}[$r]
1273     or next;
1274 elmex 1.15
1275     my $x = 0;
1276 root 1.75 my $row_h = $hs->[$r];
1277    
1278     for my $c (0 .. $#$row) {
1279     my $col_w = $ws->[$c];
1280 elmex 1.15
1281 root 1.83 if (my $widget = $row->[$c]) {
1282 root 1.128 $widget->configure ($x, $y, $col_w, $row_h);
1283 root 1.83 }
1284 elmex 1.15
1285 root 1.75 $x += $col_w;
1286 elmex 1.15 }
1287    
1288 root 1.75 $y += $row_h;
1289     }
1290    
1291     }
1292    
1293 root 1.76 sub find_widget {
1294     my ($self, $x, $y) = @_;
1295    
1296     $x -= $self->{x};
1297     $y -= $self->{y};
1298    
1299     my $res;
1300    
1301     for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
1302     $res = $_->find_widget ($x, $y)
1303     and return $res;
1304     }
1305    
1306     $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
1307     }
1308    
1309 root 1.75 sub _draw {
1310     my ($self) = @_;
1311    
1312     for (grep $_, @{$self->{children}}) {
1313     $_->draw for grep $_, @$_;
1314 elmex 1.15 }
1315     }
1316    
1317 root 1.39 #############################################################################
1318    
1319 root 1.246 package CFClient::UI::Box;
1320 root 1.76
1321     our @ISA = CFClient::UI::Container::;
1322    
1323     sub size_request {
1324     my ($self) = @_;
1325    
1326 root 1.246 $self->{vertical}
1327     ? (
1328     (List::Util::max map $_->{req_w}, @{$self->{children}}),
1329     (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1330     )
1331     : (
1332     (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1333     (List::Util::max map $_->{req_h}, @{$self->{children}}),
1334     )
1335 root 1.76 }
1336    
1337     sub size_allocate {
1338 root 1.251 my ($self, $w, $h, $changed) = @_;
1339 root 1.76
1340 root 1.246 my $space = $self->{vertical} ? $h : $w;
1341 root 1.76 my $children = $self->{children};
1342    
1343 root 1.247 my @req;
1344 root 1.76
1345 root 1.247 if ($self->{homogeneous}) {
1346     @req = ($space / (@$children || 1)) x @$children;
1347 root 1.76 } else {
1348 root 1.247 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1349     my $req = List::Util::sum @req;
1350    
1351     if ($req > $space) {
1352     # ah well, not enough space
1353     $_ *= $space / $req for @req;
1354     } else {
1355     my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1356    
1357     $space = ($space - $req) / $expand; # remaining space to give away
1358    
1359     $req[$_] += $space * $children->[$_]{expand}
1360     for 0 .. $#$children;
1361     }
1362 root 1.76 }
1363    
1364 root 1.246 CFClient::UI::harmonize \@req;
1365 root 1.112
1366 root 1.246 my $pos = 0;
1367 root 1.76 for (0 .. $#$children) {
1368 root 1.246 my $alloc = $req[$_];
1369     $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1370 root 1.76
1371 root 1.246 $pos += $alloc;
1372 root 1.76 }
1373 root 1.125
1374     1
1375 root 1.76 }
1376    
1377     #############################################################################
1378    
1379 root 1.246 package CFClient::UI::HBox;
1380 elmex 1.15
1381 root 1.246 our @ISA = CFClient::UI::Box::;
1382 root 1.76
1383 root 1.246 sub new {
1384     my $class = shift;
1385 elmex 1.15
1386 root 1.246 $class->SUPER::new (
1387     vertical => 0,
1388     @_,
1389 root 1.43 )
1390     }
1391    
1392 root 1.246 #############################################################################
1393 root 1.68
1394 root 1.246 package CFClient::UI::VBox;
1395 root 1.193
1396 root 1.246 our @ISA = CFClient::UI::Box::;
1397 root 1.68
1398 root 1.246 sub new {
1399     my $class = shift;
1400 root 1.68
1401 root 1.246 $class->SUPER::new (
1402     vertical => 1,
1403     @_,
1404     )
1405 elmex 1.36 }
1406    
1407 root 1.39 #############################################################################
1408    
1409 root 1.73 package CFClient::UI::Label;
1410 root 1.10
1411 root 1.209 our @ISA = CFClient::UI::DrawBG::;
1412 root 1.12
1413 root 1.138 use CFClient::OpenGL;
1414 root 1.10
1415     sub new {
1416 root 1.64 my ($class, %arg) = @_;
1417 root 1.51
1418 root 1.59 my $self = $class->SUPER::new (
1419 root 1.164 fg => [1, 1, 1],
1420 root 1.209 #bg => none
1421     #active_bg => none
1422 root 1.164 #font => default_font
1423 root 1.194 #text => initial text
1424     #markup => initial narkup
1425 root 1.213 #max_w => maximum pixel width
1426     ellipsise => 3, # end
1427 root 1.194 layout => (new CFClient::Layout),
1428 root 1.164 fontsize => 1,
1429     align => -1,
1430     valign => -1,
1431 root 1.258 padding_x => 2,
1432     padding_y => 2,
1433 elmex 1.150 can_events => 0,
1434 root 1.64 %arg
1435 root 1.59 );
1436 root 1.10
1437 root 1.141 if (exists $self->{template}) {
1438     my $layout = new CFClient::Layout;
1439     $layout->set_text (delete $self->{template});
1440     $self->{template} = $layout;
1441     }
1442 root 1.121
1443 root 1.194 if (exists $self->{markup}) {
1444     $self->set_markup (delete $self->{markup});
1445     } else {
1446     $self->set_text (delete $self->{text});
1447     }
1448 root 1.10
1449     $self
1450     }
1451    
1452 root 1.209 sub escape($) {
1453     local $_ = $_[0];
1454 root 1.68
1455     s/&/&amp;/g;
1456     s/>/&gt;/g;
1457     s/</&lt;/g;
1458    
1459 root 1.209 $_
1460 root 1.68 }
1461    
1462 root 1.173 sub update {
1463     my ($self) = @_;
1464    
1465     delete $self->{texture};
1466     $self->SUPER::update;
1467     }
1468    
1469 elmex 1.15 sub set_text {
1470     my ($self, $text) = @_;
1471 root 1.28
1472 root 1.173 return if $self->{text} eq "T$text";
1473     $self->{text} = "T$text";
1474    
1475 root 1.194 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1476 root 1.121 $self->{layout}->set_text ($text);
1477 root 1.28
1478 root 1.251 $self->realloc;
1479 root 1.252 $self->update;
1480 elmex 1.15 }
1481    
1482 root 1.121 sub set_markup {
1483     my ($self, $markup) = @_;
1484    
1485 root 1.173 return if $self->{text} eq "M$markup";
1486     $self->{text} = "M$markup";
1487    
1488 root 1.194 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1489    
1490     $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1491 root 1.121 $self->{layout}->set_markup ($markup);
1492 root 1.28
1493 root 1.251 $self->realloc;
1494 root 1.252 $self->update;
1495 elmex 1.15 }
1496    
1497 root 1.14 sub size_request {
1498     my ($self) = @_;
1499    
1500 root 1.157 $self->{layout}->set_font ($self->{font}) if $self->{font};
1501     $self->{layout}->set_width ($self->{max_w} || -1);
1502 root 1.213 $self->{layout}->set_ellipsise ($self->{ellipsise});
1503     $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1504 root 1.134 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1505 root 1.121
1506 root 1.76 my ($w, $h) = $self->{layout}->size;
1507    
1508 root 1.141 if (exists $self->{template}) {
1509 root 1.157 $self->{template}->set_font ($self->{font}) if $self->{font};
1510 root 1.141 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1511    
1512     my ($w2, $h2) = $self->{template}->size;
1513    
1514     $w = List::Util::max $w, $w2;
1515     $h = List::Util::max $h, $h2;
1516     }
1517    
1518 root 1.258 ($w, $h)
1519 root 1.59 }
1520 root 1.51
1521 root 1.59 sub size_allocate {
1522 root 1.251 my ($self, $w, $h, $changed) = @_;
1523 root 1.68
1524 root 1.251 delete $self->{texture}
1525     if $changed;
1526 root 1.14 }
1527    
1528 elmex 1.146 sub set_fontsize {
1529     my ($self, $fontsize) = @_;
1530    
1531     $self->{fontsize} = $fontsize;
1532 root 1.152 delete $self->{texture};
1533 root 1.186
1534 root 1.251 $self->realloc;
1535 elmex 1.146 }
1536    
1537 elmex 1.11 sub _draw {
1538 root 1.10 my ($self) = @_;
1539    
1540 root 1.209 $self->SUPER::_draw; # draw background, if applicable
1541    
1542 root 1.59 my $tex = $self->{texture} ||= do {
1543 root 1.194 $self->{layout}->set_foreground (@{$self->{fg}});
1544 root 1.157 $self->{layout}->set_font ($self->{font}) if $self->{font};
1545 root 1.59 $self->{layout}->set_width ($self->{w});
1546 root 1.213 $self->{layout}->set_ellipsise ($self->{ellipsise});
1547     $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1548     $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1549 root 1.194
1550     my $tex = new_from_layout CFClient::Texture $self->{layout};
1551    
1552 root 1.258 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1553     : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1554 root 1.208 : ($self->{w} - $tex->{w}) * 0.5);
1555    
1556 root 1.258 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1557     : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1558 root 1.208 : ($self->{h} - $tex->{h}) * 0.5);
1559 root 1.194
1560     $tex
1561 root 1.59 };
1562 root 1.10
1563     glEnable GL_TEXTURE_2D;
1564 root 1.105 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1565 root 1.10
1566 root 1.194 if ($tex->{format} == GL_ALPHA) {
1567     glColor @{$self->{fg}};
1568     $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1569     } else {
1570     $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1571     }
1572 root 1.10
1573 root 1.74 glDisable GL_TEXTURE_2D;
1574 root 1.10 }
1575    
1576 root 1.39 #############################################################################
1577    
1578 root 1.121 package CFClient::UI::EntryBase;
1579 elmex 1.31
1580 root 1.73 our @ISA = CFClient::UI::Label::;
1581 elmex 1.31
1582 root 1.138 use CFClient::OpenGL;
1583 elmex 1.31
1584 root 1.68 sub new {
1585     my $class = shift;
1586    
1587     $class->SUPER::new (
1588 root 1.164 fg => [1, 1, 1],
1589     bg => [0, 0, 0, 0.2],
1590     active_bg => [1, 1, 1, 0.5],
1591     active_fg => [0, 0, 0],
1592     can_hover => 1,
1593     can_focus => 1,
1594     valign => 0,
1595 elmex 1.150 can_events => 1,
1596 root 1.225 #text => ...
1597 root 1.68 @_
1598     )
1599     }
1600    
1601     sub _set_text {
1602     my ($self, $text) = @_;
1603    
1604 root 1.121 delete $self->{cur_h};
1605    
1606     return if $self->{text} eq $text;
1607 elmex 1.100
1608 root 1.134 delete $self->{texture};
1609    
1610 root 1.68 $self->{last_activity} = $::NOW;
1611     $self->{text} = $text;
1612 root 1.72
1613     $text =~ s/./*/g if $self->{hidden};
1614 root 1.121 $self->{layout}->set_text ("$text ");
1615 root 1.72
1616 root 1.231 $self->_emit (changed => $self->{text});
1617 root 1.121 }
1618 root 1.68
1619 root 1.194 sub set_text {
1620     my ($self, $text) = @_;
1621    
1622     $self->{cursor} = length $text;
1623     $self->_set_text ($text);
1624 root 1.251
1625     $self->realloc;
1626 root 1.194 }
1627    
1628 root 1.121 sub get_text {
1629     $_[0]{text}
1630 root 1.68 }
1631    
1632     sub size_request {
1633     my ($self) = @_;
1634    
1635     my ($w, $h) = $self->SUPER::size_request;
1636    
1637     ($w + 1, $h) # add 1 for cursor
1638     }
1639    
1640 elmex 1.31 sub key_down {
1641     my ($self, $ev) = @_;
1642    
1643 root 1.137 my $mod = $ev->{mod};
1644     my $sym = $ev->{sym};
1645     my $uni = $ev->{unicode};
1646 elmex 1.31
1647     my $text = $self->get_text;
1648    
1649 root 1.200 if ($uni == 8) {
1650 root 1.68 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1651 root 1.200 } elsif ($uni == 127) {
1652 root 1.68 substr $text, $self->{cursor}, 1, "";
1653 root 1.136 } elsif ($sym == CFClient::SDLK_LEFT) {
1654 root 1.68 --$self->{cursor} if $self->{cursor};
1655 root 1.136 } elsif ($sym == CFClient::SDLK_RIGHT) {
1656 root 1.68 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1657 root 1.136 } elsif ($sym == CFClient::SDLK_HOME) {
1658 root 1.76 $self->{cursor} = 0;
1659 root 1.136 } elsif ($sym == CFClient::SDLK_END) {
1660 root 1.76 $self->{cursor} = length $text;
1661 root 1.200 } elsif ($uni == 27) {
1662 root 1.231 $self->_emit ('escape');
1663 elmex 1.31 } elsif ($uni) {
1664 root 1.68 substr $text, $self->{cursor}++, 0, chr $uni;
1665 elmex 1.31 }
1666 root 1.51
1667 root 1.68 $self->_set_text ($text);
1668 root 1.251
1669     $self->realloc;
1670 root 1.68 }
1671    
1672     sub focus_in {
1673     my ($self) = @_;
1674    
1675     $self->{last_activity} = $::NOW;
1676    
1677     $self->SUPER::focus_in;
1678 elmex 1.31 }
1679    
1680 root 1.51 sub button_down {
1681 root 1.68 my ($self, $ev, $x, $y) = @_;
1682    
1683     $self->SUPER::button_down ($ev, $x, $y);
1684    
1685     my $idx = $self->{layout}->xy_to_index ($x, $y);
1686    
1687     # byte-index to char-index
1688 root 1.76 my $text = $self->{text};
1689 root 1.68 utf8::encode $text;
1690     $self->{cursor} = length substr $text, 0, $idx;
1691 root 1.51
1692 root 1.68 $self->_set_text ($self->{text});
1693     $self->update;
1694 root 1.51 }
1695    
1696 root 1.58 sub mouse_motion {
1697     my ($self, $ev, $x, $y) = @_;
1698 root 1.68 # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1699 root 1.58 }
1700    
1701 root 1.51 sub _draw {
1702     my ($self) = @_;
1703    
1704 root 1.68 local $self->{fg} = $self->{fg};
1705    
1706 root 1.51 if ($FOCUS == $self) {
1707 root 1.68 glColor @{$self->{active_bg}};
1708     $self->{fg} = $self->{active_fg};
1709 root 1.51 } else {
1710 root 1.68 glColor @{$self->{bg}};
1711 root 1.51 }
1712    
1713 root 1.76 glEnable GL_BLEND;
1714     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1715 root 1.51 glBegin GL_QUADS;
1716 root 1.68 glVertex 0 , 0;
1717     glVertex 0 , $self->{h};
1718     glVertex $self->{w}, $self->{h};
1719     glVertex $self->{w}, 0;
1720 root 1.51 glEnd;
1721 root 1.76 glDisable GL_BLEND;
1722 root 1.51
1723     $self->SUPER::_draw;
1724 root 1.68
1725     #TODO: force update every cursor change :(
1726     if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1727 root 1.121
1728     unless (exists $self->{cur_h}) {
1729     my $text = substr $self->{text}, 0, $self->{cursor};
1730     utf8::encode $text;
1731    
1732     @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1733     }
1734    
1735 root 1.68 glColor @{$self->{fg}};
1736     glBegin GL_LINES;
1737 root 1.122 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1738     glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1739 root 1.68 glEnd;
1740     }
1741     }
1742    
1743 root 1.121 package CFClient::UI::Entry;
1744 elmex 1.99
1745 root 1.121 our @ISA = CFClient::UI::EntryBase::;
1746 elmex 1.99
1747 root 1.138 use CFClient::OpenGL;
1748 elmex 1.99
1749     sub key_down {
1750     my ($self, $ev) = @_;
1751    
1752 root 1.137 my $sym = $ev->{sym};
1753 elmex 1.99
1754 root 1.136 if ($sym == 13) {
1755 elmex 1.167 unshift @{$self->{history}},
1756     my $txt = $self->get_text;
1757     $self->{history_pointer} = -1;
1758 elmex 1.169 $self->{history_saveback} = '';
1759 root 1.231 $self->_emit (activate => $txt);
1760 elmex 1.99 $self->update;
1761    
1762 elmex 1.167 } elsif ($sym == CFClient::SDLK_UP) {
1763     if ($self->{history_pointer} < 0) {
1764     $self->{history_saveback} = $self->get_text;
1765     }
1766 elmex 1.169 if (@{$self->{history} || []} > 0) {
1767     $self->{history_pointer}++;
1768     if ($self->{history_pointer} >= @{$self->{history} || []}) {
1769     $self->{history_pointer} = @{$self->{history} || []} - 1;
1770     }
1771     $self->set_text ($self->{history}->[$self->{history_pointer}]);
1772 elmex 1.167 }
1773    
1774     } elsif ($sym == CFClient::SDLK_DOWN) {
1775     $self->{history_pointer}--;
1776     $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1777    
1778     if ($self->{history_pointer} >= 0) {
1779     $self->set_text ($self->{history}->[$self->{history_pointer}]);
1780     } else {
1781     $self->set_text ($self->{history_saveback});
1782     }
1783    
1784 elmex 1.99 } else {
1785     $self->SUPER::key_down ($ev);
1786     }
1787    
1788     }
1789    
1790 root 1.68 #############################################################################
1791    
1792 root 1.79 package CFClient::UI::Button;
1793    
1794     our @ISA = CFClient::UI::Label::;
1795    
1796 root 1.138 use CFClient::OpenGL;
1797 root 1.79
1798 elmex 1.85 my @tex =
1799 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1800 elmex 1.85 qw(b1_button_active.png);
1801    
1802 root 1.79 sub new {
1803     my $class = shift;
1804    
1805     $class->SUPER::new (
1806 root 1.258 padding_x => 4,
1807     padding_y => 4,
1808 root 1.164 fg => [1, 1, 1],
1809     active_fg => [0, 0, 1],
1810     can_hover => 1,
1811     align => 0,
1812     valign => 0,
1813 elmex 1.150 can_events => 1,
1814 root 1.79 @_
1815     )
1816     }
1817    
1818 root 1.231 sub activate { }
1819    
1820 root 1.79 sub button_up {
1821     my ($self, $ev, $x, $y) = @_;
1822    
1823 root 1.231 $self->emit ("activate")
1824     if $x >= 0 && $x < $self->{w}
1825     && $y >= 0 && $y < $self->{h};
1826 root 1.79 }
1827    
1828     sub _draw {
1829     my ($self) = @_;
1830    
1831     local $self->{fg} = $self->{fg};
1832    
1833     if ($GRAB == $self) {
1834     $self->{fg} = $self->{active_fg};
1835     }
1836    
1837 root 1.119 glEnable GL_TEXTURE_2D;
1838 elmex 1.85 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1839 root 1.119 glColor 0, 0, 0, 1;
1840 elmex 1.85
1841 root 1.195 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1842 elmex 1.85
1843     glDisable GL_TEXTURE_2D;
1844 root 1.79
1845     $self->SUPER::_draw;
1846     }
1847    
1848     #############################################################################
1849    
1850 root 1.86 package CFClient::UI::CheckBox;
1851    
1852     our @ISA = CFClient::UI::DrawBG::;
1853    
1854 elmex 1.102 my @tex =
1855 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1856 elmex 1.102 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1857    
1858 root 1.138 use CFClient::OpenGL;
1859 root 1.86
1860     sub new {
1861     my $class = shift;
1862    
1863     $class->SUPER::new (
1864 root 1.258 padding_x => 2,
1865     padding_y => 2,
1866 root 1.86 fg => [1, 1, 1],
1867     active_fg => [1, 1, 0],
1868 root 1.209 bg => [0, 0, 0, 0.2],
1869     active_bg => [1, 1, 1, 0.5],
1870 root 1.86 state => 0,
1871 root 1.97 can_hover => 1,
1872 root 1.86 @_
1873     )
1874     }
1875    
1876 root 1.87 sub size_request {
1877     my ($self) = @_;
1878    
1879 root 1.258 (6) x 2
1880 root 1.87 }
1881    
1882 root 1.86 sub button_down {
1883     my ($self, $ev, $x, $y) = @_;
1884    
1885 root 1.258 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1886     && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1887 root 1.86 $self->{state} = !$self->{state};
1888 root 1.231 $self->_emit (changed => $self->{state});
1889 root 1.86 }
1890     }
1891    
1892     sub _draw {
1893     my ($self) = @_;
1894    
1895 root 1.87 $self->SUPER::_draw;
1896 root 1.86
1897 root 1.258 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
1898 root 1.86
1899 root 1.258 my ($w, $h) = @$self{qw(w h)};
1900    
1901     my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
1902 elmex 1.102
1903 root 1.87 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1904 root 1.86
1905 elmex 1.102 my $tex = $self->{state} ? $tex[1] : $tex[0];
1906    
1907 root 1.197 glEnable GL_TEXTURE_2D;
1908 root 1.195 $tex->draw_quad_alpha (0, 0, $s, $s);
1909 elmex 1.102 glDisable GL_TEXTURE_2D;
1910 root 1.86 }
1911    
1912     #############################################################################
1913    
1914 elmex 1.145 package CFClient::UI::Image;
1915    
1916     our @ISA = CFClient::UI::Base::;
1917    
1918     use CFClient::OpenGL;
1919     use Carp qw/confess/;
1920    
1921     our %loaded_images;
1922    
1923     sub new {
1924     my $class = shift;
1925    
1926 elmex 1.150 my $self = $class->SUPER::new (can_events => 0, @_);
1927 elmex 1.145
1928     $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1929    
1930     $loaded_images{$self->{image}} ||=
1931     new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1932    
1933     my $tex = $self->{tex} = $loaded_images{$self->{image}};
1934    
1935 root 1.147 Scalar::Util::weaken $loaded_images{$self->{image}};
1936    
1937 elmex 1.145 $self->{aspect} = $tex->{w} / $tex->{h};
1938    
1939     $self
1940     }
1941    
1942     sub size_request {
1943     my ($self) = @_;
1944    
1945     ($self->{tex}->{w}, $self->{tex}->{h})
1946     }
1947    
1948     sub _draw {
1949     my ($self) = @_;
1950    
1951     my $tex = $self->{tex};
1952    
1953     my ($w, $h) = ($self->{w}, $self->{h});
1954    
1955     if ($self->{rot90}) {
1956     glRotate 90, 0, 0, 1;
1957     glTranslate 0, -$self->{w}, 0;
1958    
1959     ($w, $h) = ($h, $w);
1960     }
1961    
1962     glEnable GL_TEXTURE_2D;
1963     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1964    
1965 root 1.195 $tex->draw_quad_alpha (0, 0, $w, $h);
1966 elmex 1.145
1967     glDisable GL_TEXTURE_2D;
1968     }
1969    
1970     #############################################################################
1971    
1972 elmex 1.124 package CFClient::UI::VGauge;
1973    
1974     our @ISA = CFClient::UI::Base::;
1975    
1976 root 1.158 use List::Util qw(min max);
1977    
1978 root 1.138 use CFClient::OpenGL;
1979 elmex 1.124
1980     my %tex = (
1981     food => [
1982 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1983 elmex 1.124 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1984     ],
1985     grace => [
1986 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1987 root 1.158 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1988 elmex 1.124 ],
1989     hp => [
1990 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1991 elmex 1.124 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1992     ],
1993     mana => [
1994 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1995 root 1.158 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1996 elmex 1.124 ],
1997     );
1998    
1999     # eg. VGauge->new (gauge => 'food'), default gauge: food
2000     sub new {
2001     my $class = shift;
2002    
2003 root 1.140 my $self = $class->SUPER::new (
2004 root 1.141 type => 'food',
2005 root 1.140 @_
2006     );
2007    
2008 root 1.141 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
2009 elmex 1.124
2010     $self
2011     }
2012    
2013     sub size_request {
2014     my ($self) = @_;
2015    
2016 root 1.143 #my $tex = $tex{$self->{type}}[0];
2017     #@$tex{qw(w h)}
2018     (0, 0)
2019 elmex 1.124 }
2020    
2021     sub set_max {
2022     my ($self, $max) = @_;
2023 root 1.127
2024 root 1.173 return if $self->{max_val} == $max;
2025    
2026 elmex 1.124 $self->{max_val} = $max;
2027 root 1.173 $self->update;
2028 elmex 1.124 }
2029    
2030     sub set_value {
2031     my ($self, $val, $max) = @_;
2032    
2033     $self->set_max ($max)
2034     if defined $max;
2035    
2036 root 1.173 return if $self->{val} == $val;
2037    
2038 elmex 1.124 $self->{val} = $val;
2039     $self->update;
2040     }
2041    
2042     sub _draw {
2043     my ($self) = @_;
2044    
2045 root 1.141 my $tex = $tex{$self->{type}};
2046 root 1.158 my ($t1, $t2, $t3) = @$tex;
2047 elmex 1.124
2048     my ($w, $h) = ($self->{w}, $self->{h});
2049    
2050 elmex 1.142 if ($self->{vertical}) {
2051     glRotate 90, 0, 0, 1;
2052     glTranslate 0, -$self->{w}, 0;
2053    
2054     ($w, $h) = ($h, $w);
2055     }
2056    
2057 elmex 1.124 my $ycut = $self->{val} / ($self->{max_val} || 1);
2058    
2059 root 1.158 my $ycut1 = max 0, min 1, $ycut;
2060     my $ycut2 = max 0, min 1, $ycut - 1;
2061    
2062     my $h1 = $self->{h} * (1 - $ycut1);
2063     my $h2 = $self->{h} * (1 - $ycut2);
2064 elmex 1.124
2065     glEnable GL_BLEND;
2066     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2067     glEnable GL_TEXTURE_2D;
2068     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2069    
2070 root 1.131 glBindTexture GL_TEXTURE_2D, $t1->{name};
2071     glBegin GL_QUADS;
2072 root 1.158 glTexCoord 0 , 0; glVertex 0 , 0;
2073     glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
2074     glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
2075     glTexCoord $t1->{s}, 0; glVertex $w, 0;
2076 root 1.131 glEnd;
2077 elmex 1.124
2078 root 1.158 my $ycut1 = List::Util::min 1, $ycut;
2079 root 1.131 glBindTexture GL_TEXTURE_2D, $t2->{name};
2080     glBegin GL_QUADS;
2081 root 1.158 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
2082     glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
2083     glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
2084     glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
2085 root 1.131 glEnd;
2086 elmex 1.124
2087 root 1.158 if ($t3) {
2088     glBindTexture GL_TEXTURE_2D, $t3->{name};
2089     glBegin GL_QUADS;
2090     glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
2091     glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
2092     glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
2093     glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
2094     glEnd;
2095     }
2096    
2097 elmex 1.124 glDisable GL_BLEND;
2098     glDisable GL_TEXTURE_2D;
2099     }
2100    
2101     #############################################################################
2102    
2103 root 1.141 package CFClient::UI::Gauge;
2104    
2105     our @ISA = CFClient::UI::VBox::;
2106    
2107     sub new {
2108 root 1.151 my ($class, %arg) = @_;
2109 root 1.141
2110     my $self = $class->SUPER::new (
2111 root 1.171 tooltip => $arg{type},
2112     can_hover => 1,
2113     can_events => 1,
2114 root 1.151 %arg,
2115 root 1.141 );
2116    
2117 root 1.161 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
2118     $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
2119     $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
2120 root 1.141
2121     $self
2122     }
2123    
2124 elmex 1.146 sub set_fontsize {
2125     my ($self, $fsize) = @_;
2126    
2127     $self->{value}->set_fontsize ($fsize);
2128     $self->{max} ->set_fontsize ($fsize);
2129     }
2130    
2131 root 1.173 sub set_max {
2132     my ($self, $max) = @_;
2133    
2134     $self->{gauge}->set_max ($max);
2135     $self->{max}->set_text ($max);
2136     }
2137    
2138 root 1.141 sub set_value {
2139     my ($self, $val, $max) = @_;
2140    
2141     $self->set_max ($max)
2142     if defined $max;
2143    
2144     $self->{gauge}->set_value ($val, $max);
2145     $self->{value}->set_text ($val);
2146     }
2147    
2148     #############################################################################
2149    
2150 root 1.73 package CFClient::UI::Slider;
2151 root 1.68
2152     use strict;
2153    
2154 root 1.138 use CFClient::OpenGL;
2155 root 1.68
2156 root 1.73 our @ISA = CFClient::UI::DrawBG::;
2157 root 1.68
2158 elmex 1.99 my @tex =
2159     map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
2160     qw(s1_slider.png s1_slider_bg.png);
2161    
2162 root 1.68 sub new {
2163     my $class = shift;
2164    
2165 root 1.206 # range [value, low, high, page, unit]
2166 root 1.68
2167 root 1.97 # TODO: 0-width page
2168     # TODO: req_w/h are wrong with vertical
2169     # TODO: calculations are off
2170 root 1.76 my $self = $class->SUPER::new (
2171 root 1.68 fg => [1, 1, 1],
2172     active_fg => [0, 0, 0],
2173 root 1.209 bg => [0, 0, 0, 0.2],
2174     active_bg => [1, 1, 1, 0.5],
2175 root 1.227 range => [0, 0, 100, 10, 0],
2176 root 1.257 min_w => $::WIDTH / 80,
2177     min_h => $::WIDTH / 80,
2178 root 1.76 vertical => 0,
2179 root 1.97 can_hover => 1,
2180 root 1.217 inner_pad => 0.02,
2181 root 1.68 @_
2182 root 1.76 );
2183    
2184 root 1.206 $self->set_value ($self->{range}[0]);
2185     $self->update;
2186    
2187 root 1.76 $self
2188     }
2189    
2190 root 1.251 sub changed { }
2191    
2192 root 1.225 sub set_range {
2193     my ($self, $range) = @_;
2194    
2195 root 1.239 ($range, $self->{range}) = ($self->{range}, $range);
2196 root 1.225
2197 root 1.239 $self->update
2198     if "@$range" ne "@{$self->{range}}";
2199 root 1.225 }
2200    
2201 root 1.206 sub set_value {
2202     my ($self, $value) = @_;
2203    
2204     my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2205    
2206     $hi = $lo + 1 if $hi <= $lo;
2207    
2208 root 1.227 $page = $hi - $lo if $page > $hi - $lo;
2209    
2210     $value = $lo if $value < $lo;
2211     $value = $hi - $page if $value > $hi - $page;
2212 root 1.206
2213     $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2214     if $unit;
2215    
2216     @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2217    
2218     if ($value != $old_value) {
2219 root 1.231 $self->_emit (changed => $value);
2220 root 1.206 $self->update;
2221     }
2222     }
2223    
2224 root 1.76 sub size_request {
2225     my ($self) = @_;
2226    
2227 root 1.257 ($self->{req_w}, $self->{req_h})
2228 root 1.68 }
2229    
2230 root 1.69 sub button_down {
2231     my ($self, $ev, $x, $y) = @_;
2232    
2233     $self->SUPER::button_down ($ev, $x, $y);
2234 root 1.227
2235     $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x];
2236    
2237 root 1.69 $self->mouse_motion ($ev, $x, $y);
2238     }
2239    
2240     sub mouse_motion {
2241     my ($self, $ev, $x, $y) = @_;
2242    
2243     if ($GRAB == $self) {
2244 root 1.71 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2245    
2246 root 1.206 my (undef, $lo, $hi, $page) = @{$self->{range}};
2247 elmex 1.103
2248 root 1.227 $x = ($x - $self->{click}[1]) / ($w * $self->{scale});
2249 root 1.69
2250 root 1.227 $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo));
2251 root 1.69 }
2252     }
2253    
2254 root 1.206 sub update {
2255     my ($self) = @_;
2256    
2257     $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2258     $self->set_value ($self->{range}[0]);
2259    
2260     my ($value, $lo, $hi, $page) = @{$self->{range}};
2261 root 1.227 my $range = ($hi - $page - $lo) || 1e-100;
2262 root 1.206
2263 root 1.227 my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1;
2264 root 1.206
2265 root 1.227 $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5;
2266     $self->{scale} = 1 - 2 * $self->{offset} || 1e-100;
2267 root 1.206
2268 root 1.227 $value = ($value - $lo) / $range;
2269     $value = $value * $self->{scale} + $self->{offset};
2270 root 1.206
2271 root 1.227 $self->{knob_x} = $value - $knob_w * 0.5;
2272     $self->{knob_w} = $knob_w;
2273 root 1.206 });
2274    
2275     $self->SUPER::update;
2276 elmex 1.103 }
2277    
2278 root 1.68 sub _draw {
2279     my ($self) = @_;
2280    
2281     $self->SUPER::_draw ();
2282    
2283 root 1.206 glScale $self->{w}, $self->{h};
2284 root 1.68
2285     if ($self->{vertical}) {
2286     # draw a vertical slider like a rotated horizontal slider
2287    
2288 root 1.214 glTranslate 1, 0, 0;
2289 root 1.68 glRotate 90, 0, 0, 1;
2290     }
2291    
2292     my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2293     my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2294    
2295 elmex 1.99 glEnable GL_TEXTURE_2D;
2296     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2297    
2298     # draw background
2299 root 1.206 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2300 root 1.69
2301 elmex 1.99 # draw handle
2302 root 1.206 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2303 root 1.69
2304 elmex 1.99 glDisable GL_TEXTURE_2D;
2305 root 1.51 }
2306    
2307 root 1.39 #############################################################################
2308    
2309 root 1.225 package CFClient::UI::ValSlider;
2310    
2311     our @ISA = CFClient::UI::HBox::;
2312    
2313     sub new {
2314     my ($class, %arg) = @_;
2315    
2316     my $range = delete $arg{range};
2317    
2318     my $self = $class->SUPER::new (
2319     slider => (new CFClient::UI::Slider expand => 1, range => $range),
2320     entry => (new CFClient::UI::Label text => "", template => delete $arg{template}),
2321     to_value => sub { shift },
2322     from_value => sub { shift },
2323     %arg,
2324     );
2325    
2326     $self->{slider}->connect (changed => sub {
2327     my ($self, $value) = @_;
2328     $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value));
2329     $self->{parent}->emit (changed => $value);
2330     });
2331    
2332     # $self->{entry}->connect (changed => sub {
2333     # my ($self, $value) = @_;
2334     # $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value));
2335     # $self->{parent}->emit (changed => $value);
2336     # });
2337    
2338     $self->add ($self->{slider}, $self->{entry});
2339    
2340     $self->{slider}->emit (changed => $self->{slider}{range}[0]);
2341    
2342     $self
2343     }
2344    
2345     sub set_range { shift->{slider}->set_range (@_) }
2346     sub set_value { shift->{slider}->set_value (@_) }
2347    
2348     #############################################################################
2349    
2350 root 1.97 package CFClient::UI::TextView;
2351    
2352     our @ISA = CFClient::UI::HBox::;
2353    
2354 root 1.138 use CFClient::OpenGL;
2355 root 1.97
2356     sub new {
2357     my $class = shift;
2358    
2359     my $self = $class->SUPER::new (
2360 root 1.164 fontsize => 1,
2361     can_events => 0,
2362     #font => default_font
2363 root 1.105 @_,
2364 root 1.164
2365 root 1.195 layout => (new CFClient::Layout 1),
2366 root 1.164 par => [],
2367     height => 0,
2368     children => [
2369 root 1.97 (new CFClient::UI::Empty expand => 1),
2370     (new CFClient::UI::Slider vertical => 1),
2371     ],
2372     );
2373    
2374 root 1.176 $self->{children}[1]->connect (changed => sub { $self->update });
2375 root 1.107
2376 root 1.97 $self
2377     }
2378    
2379 root 1.107 sub set_fontsize {
2380     my ($self, $fontsize) = @_;
2381    
2382     $self->{fontsize} = $fontsize;
2383     $self->reflow;
2384     }
2385    
2386 root 1.220 sub size_allocate {
2387 root 1.251 my ($self, $w, $h, $changed) = @_;
2388    
2389     $self->SUPER::size_allocate ($w, $h, $changed);
2390 root 1.220
2391 root 1.251 return unless $changed;
2392 root 1.220
2393     $self->{layout}->set_font ($self->{font}) if $self->{font};
2394     $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2395     $self->{layout}->set_width ($self->{children}[0]{w});
2396    
2397     $self->reflow;
2398     }
2399    
2400 root 1.228 sub text_size {
2401 root 1.220 my ($self, $text, $indent) = @_;
2402 root 1.105
2403     my $layout = $self->{layout};
2404    
2405 root 1.134 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2406 root 1.220 $layout->set_width ($self->{children}[0]{w} - $indent);
2407 root 1.195 $layout->set_markup ($text);
2408 root 1.105
2409 root 1.228 $layout->size
2410 root 1.105 }
2411    
2412     sub reflow {
2413     my ($self) = @_;
2414    
2415 root 1.107 $self->{need_reflow}++;
2416     $self->update;
2417 root 1.105 }
2418    
2419 root 1.227 sub set_offset {
2420     my ($self, $offset) = @_;
2421    
2422     # todo: base offset on lines or so, not on pixels
2423     $self->{children}[1]->set_value ($offset);
2424     }
2425    
2426 root 1.226 sub clear {
2427     my ($self) = @_;
2428    
2429     $self->{par} = [];
2430     $self->{height} = 0;
2431 root 1.227 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2432 root 1.226 }
2433    
2434 root 1.97 sub add_paragraph {
2435 root 1.220 my ($self, $color, $text, $indent) = @_;
2436 root 1.97
2437 root 1.220 for my $line (split /\n/, $text) {
2438 root 1.228 my ($w, $h) = $self->text_size ($line);
2439     $self->{height} += $h;
2440     push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line];
2441 root 1.220 }
2442 root 1.105
2443 root 1.227 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]);
2444 root 1.97 }
2445    
2446 root 1.105 sub update {
2447 root 1.97 my ($self) = @_;
2448    
2449 root 1.105 $self->SUPER::update;
2450    
2451     return unless $self->{h} > 0;
2452    
2453 root 1.107 delete $self->{texture};
2454    
2455 root 1.198 $ROOT->on_post_alloc ($self, sub {
2456 root 1.228 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2457    
2458 root 1.107 if (delete $self->{need_reflow}) {
2459     my $height = 0;
2460    
2461 root 1.228 my $layout = $self->{layout};
2462    
2463     $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2464    
2465     for (@{$self->{par}}) {
2466     if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support
2467     $layout->set_width ($W - $_->[3]);
2468     $layout->set_markup ($_->[4]);
2469     my ($w, $h) = $layout->size;
2470     $_->[0] = $w + $_->[3];
2471     $_->[1] = $h;
2472     }
2473    
2474     $height += $_->[1];
2475     }
2476 root 1.107
2477     $self->{height} = $height;
2478    
2479 root 1.228 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]);
2480 root 1.107
2481     delete $self->{texture};
2482     }
2483    
2484 root 1.228 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2485 root 1.215 glClearColor 0.5, 0.5, 0.5, 0;
2486 root 1.107 glClear GL_COLOR_BUFFER_BIT;
2487    
2488     my $top = int $self->{children}[1]{range}[0];
2489 root 1.105
2490 root 1.107 my $y0 = $top;
2491 root 1.228 my $y1 = $top + $H;
2492 root 1.105
2493 root 1.107 my $y = 0;
2494 root 1.97
2495 root 1.107 my $layout = $self->{layout};
2496 root 1.97
2497 root 1.157 $layout->set_font ($self->{font}) if $self->{font};
2498    
2499 root 1.220 glEnable GL_BLEND;
2500 root 1.228 #TODO# not correct in windows where rgba is forced off
2501 root 1.220 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2502    
2503 root 1.107 for my $par (@{$self->{par}}) {
2504 root 1.228 my $h = $par->[1];
2505 root 1.97
2506 root 1.107 if ($y0 < $y + $h && $y < $y1) {
2507 root 1.228 $layout->set_foreground (@{ $par->[2] });
2508     $layout->set_width ($W - $par->[3]);
2509     $layout->set_markup ($par->[4]);
2510 root 1.220
2511     my ($w, $h, $data, $format, $internalformat) = $layout->render;
2512 root 1.105
2513 root 1.228 glRasterPos $par->[3], $y - $y0;
2514 root 1.220 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2515 root 1.107 }
2516    
2517     $y += $h;
2518 root 1.105 }
2519    
2520 root 1.220 glDisable GL_BLEND;
2521 root 1.107 };
2522     });
2523 root 1.105 }
2524 root 1.97
2525 root 1.105 sub _draw {
2526     my ($self) = @_;
2527 root 1.97
2528 root 1.176 glEnable GL_TEXTURE_2D;
2529     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2530     glColor 1, 1, 1, 1;
2531 root 1.216 $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2532 root 1.176 glDisable GL_TEXTURE_2D;
2533 root 1.97
2534 root 1.106 $self->{children}[1]->draw;
2535    
2536 root 1.97 }
2537    
2538     #############################################################################
2539    
2540 root 1.73 package CFClient::UI::Animator;
2541 root 1.35
2542 root 1.138 use CFClient::OpenGL;
2543 root 1.35
2544 root 1.73 our @ISA = CFClient::UI::Bin::;
2545 root 1.35
2546     sub moveto {
2547     my ($self, $x, $y) = @_;
2548    
2549     $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
2550 root 1.56 $self->{speed} = 0.001;
2551 root 1.35 $self->{time} = 1;
2552    
2553     ::animation_start $self;
2554     }
2555    
2556     sub animate {
2557     my ($self, $interval) = @_;
2558    
2559     $self->{time} -= $interval * $self->{speed};
2560     if ($self->{time} <= 0) {
2561     $self->{time} = 0;
2562     ::animation_stop $self;
2563     }
2564    
2565     my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
2566    
2567     $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
2568     $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
2569     }
2570    
2571     sub _draw {
2572     my ($self) = @_;
2573    
2574     glPushMatrix;
2575 root 1.51 glRotate $self->{time} * 1000, 0, 1, 0;
2576 root 1.38 $self->{children}[0]->draw;
2577 root 1.35 glPopMatrix;
2578     }
2579    
2580 root 1.51 #############################################################################
2581    
2582 root 1.96 package CFClient::UI::Flopper;
2583    
2584     our @ISA = CFClient::UI::Button::;
2585    
2586     sub new {
2587     my $class = shift;
2588    
2589     my $self = $class->SUPER::new (
2590 root 1.243 state => 0,
2591     on_activate => \&toggle_flopper,
2592 root 1.96 @_
2593     );
2594    
2595     $self
2596     }
2597    
2598     sub toggle_flopper {
2599     my ($self) = @_;
2600    
2601 elmex 1.245 $self->{other}->toggle_visibility;
2602 root 1.96 }
2603    
2604     #############################################################################
2605    
2606 root 1.153 package CFClient::UI::Tooltip;
2607    
2608     our @ISA = CFClient::UI::Bin::;
2609    
2610     use CFClient::OpenGL;
2611    
2612     sub new {
2613     my $class = shift;
2614    
2615     $class->SUPER::new (
2616     @_,
2617     can_events => 0,
2618     )
2619     }
2620    
2621 root 1.196 sub set_tooltip_from {
2622     my ($self, $widget) = @_;
2623 root 1.195
2624 root 1.197 $self->add (new CFClient::UI::Label
2625 root 1.213 markup => $widget->{tooltip},
2626     max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2627     fontsize => 0.8,
2628     fg => [0, 0, 0, 1],
2629     ellipsise => 0,
2630     font => ($widget->{tooltip_font} || $::FONT_PROP),
2631 root 1.197 );
2632 root 1.153 }
2633    
2634     sub size_request {
2635     my ($self) = @_;
2636    
2637     my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2638    
2639 root 1.154 ($w + 4, $h + 4)
2640     }
2641    
2642 root 1.162 sub size_allocate {
2643 root 1.251 my ($self, $w, $h, $changed) = @_;
2644    
2645     return unless $changed;
2646 root 1.162
2647 root 1.251 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2648 root 1.162 }
2649    
2650 root 1.253 sub visibility_change {
2651     my ($self, $visible) = @_;
2652    
2653     return unless $visible;
2654    
2655     $self->{root}->on_post_alloc ("move_$self" => sub {
2656 root 1.254 my $widget = $self->{owner}
2657     or return;
2658 root 1.253
2659     my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2660    
2661     ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2662     if $x + $self->{w} > $::WIDTH;
2663    
2664 root 1.256 $self->move_abs ($x, $y);
2665 root 1.253 });
2666     }
2667    
2668 root 1.154 sub _draw {
2669     my ($self) = @_;
2670    
2671     glTranslate 0.375, 0.375;
2672    
2673     my ($w, $h) = @$self{qw(w h)};
2674    
2675     glColor 1, 0.8, 0.4;
2676     glBegin GL_QUADS;
2677     glVertex 0 , 0;
2678     glVertex 0 , $h;
2679     glVertex $w, $h;
2680     glVertex $w, 0;
2681     glEnd;
2682    
2683     glColor 0, 0, 0;
2684     glBegin GL_LINE_LOOP;
2685     glVertex 0 , 0;
2686     glVertex 0 , $h;
2687     glVertex $w, $h;
2688     glVertex $w, 0;
2689     glEnd;
2690    
2691 root 1.197 glTranslate 2 - 0.375, 2 - 0.375;
2692 root 1.252
2693 root 1.154 $self->SUPER::_draw;
2694 root 1.153 }
2695    
2696     #############################################################################
2697    
2698 root 1.162 package CFClient::UI::Face;
2699    
2700     our @ISA = CFClient::UI::Base::;
2701    
2702     use CFClient::OpenGL;
2703    
2704     sub new {
2705     my $class = shift;
2706    
2707 root 1.217 my $self = $class->SUPER::new (
2708 root 1.234 aspect => 1,
2709     can_events => 0,
2710 root 1.162 @_,
2711 root 1.217 );
2712    
2713     if ($self->{anim} && $self->{animspeed}) {
2714     Scalar::Util::weaken (my $widget = $self);
2715    
2716     $self->{timer} = Event->timer (
2717     at => $self->{animspeed} * int $::NOW / $self->{animspeed},
2718     hard => 1,
2719     interval => $self->{animspeed},
2720     cb => sub {
2721     ++$widget->{frame};
2722     $widget->update;
2723     },
2724     );
2725     }
2726    
2727     $self
2728 root 1.162 }
2729    
2730     sub size_request {
2731     (32, 8)
2732     }
2733    
2734 root 1.222 sub update {
2735     my ($self) = @_;
2736    
2737     return unless $self->{visible};
2738    
2739     $self->SUPER::update;
2740     }
2741    
2742 elmex 1.179 sub _draw {
2743 root 1.162 my ($self) = @_;
2744    
2745 root 1.227 return unless $::CONN;
2746 root 1.162
2747 root 1.217 my $face;
2748    
2749     if ($self->{frame}) {
2750     my $anim = $::CONN->{anim}[$self->{anim}];
2751    
2752     $face = $anim->[ $self->{frame} % @$anim ]
2753     if $anim && @$anim;
2754     }
2755    
2756     my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]];
2757    
2758 root 1.162 if ($tex) {
2759     glEnable GL_TEXTURE_2D;
2760     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2761     glColor 1, 1, 1, 1;
2762 root 1.195 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2763 root 1.162 glDisable GL_TEXTURE_2D;
2764     }
2765     }
2766    
2767 root 1.217 sub DESTROY {
2768     my ($self) = @_;
2769    
2770     $self->{timer}->cancel
2771     if $self->{timer};
2772    
2773     $self->SUPER::DESTROY;
2774     }
2775    
2776 root 1.162 #############################################################################
2777    
2778 root 1.186 package CFClient::UI::Inventory;
2779    
2780     our @ISA = CFClient::UI::ScrolledWindow::;
2781    
2782     sub new {
2783     my $class = shift;
2784    
2785     my $self = $class->SUPER::new (
2786 root 1.234 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2787 root 1.186 @_,
2788     );
2789    
2790     $self
2791     }
2792    
2793     sub set_items {
2794     my ($self, $items) = @_;
2795    
2796     $self->{scrolled}->clear;
2797     return unless $items;
2798    
2799 root 1.201 my @items = sort {
2800     ($a->{type} <=> $b->{type})
2801     or ($a->{name} cmp $b->{name})
2802     } @$items;
2803 root 1.186
2804     $self->{real_items} = \@items;
2805    
2806 root 1.234 my $row = 0;
2807 root 1.186 for my $item (@items) {
2808 root 1.234 CFClient::Item::update_widgets $item;
2809    
2810     $self->{scrolled}->add (0, $row, $item->{face_widget});
2811     $self->{scrolled}->add (1, $row, $item->{desc_widget});
2812     $self->{scrolled}->add (2, $row, $item->{weight_widget});
2813 root 1.186
2814 root 1.234 $row++;
2815 elmex 1.224 }
2816 root 1.186 }
2817    
2818     #############################################################################
2819    
2820 root 1.178 package CFClient::UI::Menu;
2821    
2822     our @ISA = CFClient::UI::FancyFrame::;
2823    
2824     use CFClient::OpenGL;
2825    
2826     sub new {
2827     my $class = shift;
2828    
2829     my $self = $class->SUPER::new (
2830     items => [],
2831     z => 100,
2832     @_,
2833     );
2834    
2835     $self->add ($self->{vbox} = new CFClient::UI::VBox);
2836    
2837     for my $item (@{ $self->{items} }) {
2838     my ($widget, $cb) = @$item;
2839    
2840     # handle various types of items, only text for now
2841     if (!ref $widget) {
2842     $widget = new CFClient::UI::Label
2843     can_hover => 1,
2844     can_events => 1,
2845     text => $widget;
2846     }
2847    
2848     $self->{item}{$widget} = $item;
2849    
2850     $self->{vbox}->add ($widget);
2851     }
2852    
2853     $self
2854     }
2855    
2856     # popup given the event (must be a mouse button down event currently)
2857     sub popup {
2858     my ($self, $ev) = @_;
2859    
2860 root 1.231 $self->_emit ("popdown");
2861 root 1.178
2862     # maybe save $GRAB? must be careful about events...
2863     $GRAB = $self;
2864     $self->{button} = $ev->{button};
2865    
2866     $self->show;
2867 root 1.258 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2868 root 1.178 }
2869    
2870     sub mouse_motion {
2871     my ($self, $ev, $x, $y) = @_;
2872    
2873 root 1.182 # TODO: should use vbox->find_widget or so
2874 root 1.178 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2875     $self->{hover} = $self->{item}{$HOVER};
2876     }
2877    
2878     sub button_up {
2879     my ($self, $ev, $x, $y) = @_;
2880    
2881     if ($ev->{button} == $self->{button}) {
2882     undef $GRAB;
2883     $self->hide;
2884    
2885 root 1.231 $self->_emit ("popdown");
2886 root 1.178 $self->{hover}[1]->() if $self->{hover};
2887     }
2888     }
2889    
2890     #############################################################################
2891    
2892 root 1.194 package CFClient::UI::Statusbox;
2893    
2894     our @ISA = CFClient::UI::VBox::;
2895    
2896 root 1.210 sub new {
2897     my $class = shift;
2898    
2899     $class->SUPER::new (
2900     fontsize => 0.8,
2901     @_,
2902     )
2903     }
2904    
2905 root 1.194 sub reorder {
2906     my ($self) = @_;
2907     my $NOW = time;
2908    
2909     while (my ($k, $v) = each %{ $self->{item} }) {
2910     delete $self->{item}{$k} if $v->{timeout} < $NOW;
2911     }
2912    
2913     my @widgets;
2914 root 1.197
2915     my @items = sort {
2916     $a->{pri} <=> $b->{pri}
2917     or $b->{id} <=> $a->{id}
2918     } values %{ $self->{item} };
2919    
2920 root 1.194 my $count = 10 + 1;
2921     for my $item (@items) {
2922     last unless --$count;
2923    
2924     push @widgets, $item->{label} ||= do {
2925     # TODO: doesn't handle markup well (read as: at all)
2926 root 1.197 my $short = $item->{count} > 1
2927     ? "<b>$item->{count} ×</b> $item->{text}"
2928     : $item->{text};
2929    
2930 root 1.194 for ($short) {
2931     s/^\s+//;
2932 root 1.205 s/\s+/ /g;
2933 root 1.194 }
2934    
2935     new CFClient::UI::Label
2936 root 1.196 markup => $short,
2937 root 1.197 tooltip => $item->{tooltip},
2938 root 1.196 tooltip_font => $::FONT_PROP,
2939 root 1.197 tooltip_width => 0.67,
2940 root 1.213 fontsize => $item->{fontsize} || $self->{fontsize},
2941     max_w => $::WIDTH * 0.44,
2942 root 1.205 fg => $item->{fg},
2943 root 1.196 can_events => 1,
2944 root 1.197 can_hover => 1
2945 root 1.194 };
2946     }
2947    
2948     $self->clear;
2949 root 1.197 $self->SUPER::add (reverse @widgets);
2950 root 1.194 }
2951    
2952     sub add {
2953     my ($self, $text, %arg) = @_;
2954    
2955 root 1.198 $text =~ s/^\s+//;
2956     $text =~ s/\s+$//;
2957    
2958 root 1.233 return unless $text;
2959    
2960 root 1.197 my $timeout = time + ((delete $arg{timeout}) || 60);
2961 root 1.194
2962 root 1.197 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2963 root 1.194
2964 root 1.197 if (my $item = $self->{item}{$group}) {
2965     if ($item->{text} eq $text) {
2966     $item->{count}++;
2967     } else {
2968     $item->{count} = 1;
2969     $item->{text} = $item->{tooltip} = $text;
2970     }
2971 root 1.198 $item->{id} = ++$self->{id};
2972 root 1.197 $item->{timeout} = $timeout;
2973     delete $item->{label};
2974     } else {
2975     $self->{item}{$group} = {
2976     id => ++$self->{id},
2977     text => $text,
2978     timeout => $timeout,
2979     tooltip => $text,
2980 root 1.205 fg => [0.8, 0.8, 0.8, 0.8],
2981 root 1.197 pri => 0,
2982     count => 1,
2983     %arg,
2984     };
2985     }
2986 root 1.194
2987     $self->reorder;
2988     }
2989    
2990 root 1.213 sub reconfigure {
2991     my ($self) = @_;
2992    
2993     delete $_->{label}
2994     for values %{ $self->{item} || {} };
2995    
2996     $self->reorder;
2997     $self->SUPER::reconfigure;
2998     }
2999    
3000 root 1.194 #############################################################################
3001    
3002 root 1.113 package CFClient::UI::Root;
3003 root 1.51
3004 root 1.73 our @ISA = CFClient::UI::Container::;
3005 root 1.51
3006 root 1.138 use CFClient::OpenGL;
3007 root 1.107
3008 root 1.191 sub new {
3009     my $class = shift;
3010    
3011 root 1.251 my $self = $class->SUPER::new (
3012 root 1.241 visible => 1,
3013 root 1.191 @_,
3014 root 1.251 );
3015    
3016     Scalar::Util::weaken ($self->{root} = $self);
3017    
3018     $self
3019 root 1.191 }
3020    
3021     sub configure {
3022     my ($self, $x, $y, $w, $h) = @_;
3023    
3024     $self->{w} = $w;
3025     $self->{h} = $h;
3026     }
3027    
3028 root 1.251 sub reconfigure {
3029 root 1.125 my ($self) = @_;
3030    
3031 root 1.251 $self->SUPER::reconfigure;
3032    
3033     $self->size_allocate ($self->{w}, $self->{h}, 1)
3034 root 1.191 if $self->{w};
3035 root 1.125 }
3036    
3037 root 1.51 sub size_request {
3038 root 1.186 my ($self) = @_;
3039    
3040     ($self->{w}, $self->{h})
3041     }
3042    
3043 root 1.256 sub _to_pixel {
3044     my ($coord, $size, $max) = @_;
3045    
3046     $coord =
3047     $coord eq "center" ? ($max - $size) * 0.5
3048     : $coord eq "max" ? $max
3049     : $coord;
3050    
3051     $coord = 0 if $coord < 0;
3052     $coord = $max - $size if $coord > $max - $size;
3053    
3054     int $coord + 0.5
3055     }
3056    
3057 root 1.186 sub size_allocate {
3058 root 1.251 my ($self, $w, $h, $changed) = @_;
3059 root 1.186
3060     for my $child ($self->children) {
3061 root 1.149 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3062    
3063 root 1.256 $X = $child->{force_x} if exists $child->{force_x};
3064     $Y = $child->{force_y} if exists $child->{force_y};
3065 root 1.205
3066 root 1.256 $X = _to_pixel $X, $W, $self->{w};
3067     $Y = _to_pixel $Y, $H, $self->{h};
3068 root 1.205
3069 root 1.186 $child->configure ($X, $Y, $W, $H);
3070 root 1.149 }
3071 root 1.191 }
3072    
3073     sub coord2local {
3074     my ($self, $x, $y) = @_;
3075    
3076     ($x, $y)
3077 root 1.51 }
3078    
3079 root 1.191 sub coord2global {
3080 root 1.58 my ($self, $x, $y) = @_;
3081    
3082     ($x, $y)
3083     }
3084    
3085 root 1.51 sub update {
3086     my ($self) = @_;
3087    
3088 root 1.191 $::WANT_REFRESH++;
3089 root 1.51 }
3090    
3091     sub add {
3092 root 1.203 my ($self, @children) = @_;
3093    
3094 root 1.256 $_->{is_toplevel} = 1
3095     for @children;
3096 elmex 1.146
3097 root 1.203 $self->SUPER::add (@children);
3098 root 1.222 }
3099    
3100     sub remove {
3101     my ($self, @children) = @_;
3102    
3103     $self->SUPER::remove (@children);
3104    
3105 root 1.256 delete $self->{is_toplevel}
3106     for @children;
3107    
3108 root 1.222 while (@children) {
3109     my $w = pop @children;
3110     push @children, $w->children;
3111 root 1.231 $w->set_invisible;
3112 root 1.222 }
3113 root 1.51 }
3114    
3115 root 1.107 sub on_refresh {
3116     my ($self, $id, $cb) = @_;
3117    
3118     $self->{refresh_hook}{$id} = $cb;
3119     }
3120    
3121 root 1.198 sub on_post_alloc {
3122     my ($self, $id, $cb) = @_;
3123    
3124     $self->{post_alloc_hook}{$id} = $cb;
3125     }
3126    
3127 root 1.191 sub draw {
3128 root 1.51 my ($self) = @_;
3129    
3130 root 1.198 while ($self->{refresh_hook}) {
3131     $_->()
3132     for values %{delete $self->{refresh_hook}};
3133     }
3134    
3135 root 1.251 if ($self->{realloc}) {
3136 root 1.243 my @queue;
3137 root 1.191
3138 root 1.250 while () {
3139 root 1.251 if ($self->{realloc}) {
3140 root 1.243 #TODO use array-of-depth approach
3141    
3142     @queue = sort { $a->{visible} <=> $b->{visible} }
3143 root 1.251 @queue, values %{delete $self->{realloc}};
3144 root 1.191 }
3145    
3146 root 1.243 my $widget = pop @queue || last;
3147    
3148 root 1.251 $widget->{visible} or last; # do not resize invisible widgets
3149 root 1.191
3150 root 1.257 my ($w, $h) = $widget->size_request;
3151 root 1.256
3152 root 1.258 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3153     $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3154 root 1.257
3155     $w = $widget->{force_w} if exists $widget->{force_w};
3156     $h = $widget->{force_h} if exists $widget->{force_h};
3157 root 1.191
3158 root 1.251 $widget->{req_w} = $w;
3159     $widget->{req_h} = $h;
3160 root 1.191
3161 root 1.256 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3162 root 1.191
3163 root 1.251 push @queue, $widget->{parent}
3164 root 1.257 if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent};
3165 root 1.191 }
3166     }
3167    
3168 root 1.250 while (my $size_alloc = delete $self->{size_alloc}) {
3169     my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3170     values %$size_alloc;
3171    
3172     while () {
3173     my ($widget, $w, $h) = @{ pop @queue or last };
3174 root 1.191
3175 root 1.256 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3176     $h = $widget->{h} || $widget->{req_h} unless defined $h;
3177    
3178 root 1.193 $w = 0 if $w < 0;
3179     $h = 0 if $h < 0;
3180    
3181 root 1.256 $w = int $w + 0.5;
3182     $h = int $h + 0.5;
3183    
3184 root 1.252 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3185    
3186 root 1.191 $widget->{w} = $w;
3187     $widget->{h} = $h;
3188 root 1.252
3189     $widget->emit (size_allocate => $w, $h, $changed);
3190 root 1.191 }
3191     }
3192    
3193 root 1.198 while ($self->{post_alloc_hook}) {
3194 root 1.191 $_->()
3195 root 1.198 for values %{delete $self->{post_alloc_hook}};
3196 root 1.107 }
3197    
3198     glViewport 0, 0, $::WIDTH, $::HEIGHT;
3199     glClearColor +($::CFG->{fow_intensity}) x 3, 1;
3200     glClear GL_COLOR_BUFFER_BIT;
3201    
3202     glMatrixMode GL_PROJECTION;
3203     glLoadIdentity;
3204 root 1.219 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000;
3205 root 1.107 glMatrixMode GL_MODELVIEW;
3206     glLoadIdentity;
3207    
3208 root 1.51 $self->_draw;
3209     }
3210    
3211     #############################################################################
3212    
3213 root 1.73 package CFClient::UI;
3214 root 1.51
3215 root 1.113 $ROOT = new CFClient::UI::Root;
3216 root 1.213 $TOOLTIP = new CFClient::UI::Tooltip z => 900;
3217 root 1.51
3218     1
3219 root 1.5