ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.239
Committed: Thu May 25 22:29:57 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.238: +13 -4 lines
Log Message:
reduce set_range update results and fix inventory slider bug

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