ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.161
Committed: Mon Apr 24 03:43:51 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.160: +5 -3 lines
Log Message:
more tooltips and goodies

File Contents

# User Rev Content
1 root 1.73 package CFClient::UI;
2 root 1.8
3 elmex 1.1 use strict;
4 root 1.18
5 root 1.74 use Scalar::Util ();
6     use List::Util ();
7 root 1.18
8 root 1.60 use CFClient;
9 root 1.41
10 root 1.51 our ($FOCUS, $HOVER, $GRAB); # various widgets
11    
12 root 1.113 our $ROOT;
13 root 1.151 our $TOOLTIP;
14 root 1.51 our $BUTTON_STATE;
15    
16 root 1.151 sub check_tooltip {
17     if (!$GRAB) {
18     for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 root 1.160 if (length $widget->{tooltip}) {
20 root 1.151
21     if ($TOOLTIP->{owner} != $widget) {
22     $TOOLTIP->{owner} = $widget;
23 root 1.155
24     my $tip = $widget->{tooltip};
25    
26     $tip = $tip->($widget) if CODE:: eq ref $tip;
27    
28 root 1.151 $TOOLTIP->set_text ($widget->{tooltip});
29     $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30     $TOOLTIP->show;
31     }
32    
33     return;
34     }
35     }
36     }
37    
38     $TOOLTIP->hide;
39     delete $TOOLTIP->{owner};
40     }
41    
42 elmex 1.1 # class methods for events
43 root 1.51 sub feed_sdl_key_down_event {
44     $FOCUS->key_down ($_[0]) if $FOCUS;
45     }
46    
47     sub feed_sdl_key_up_event {
48     $FOCUS->key_up ($_[0]) if $FOCUS;
49     }
50    
51     sub feed_sdl_button_down_event {
52     my ($ev) = @_;
53 root 1.137 my ($x, $y) = ($ev->{x}, $ev->{y});
54 root 1.51
55     if (!$BUTTON_STATE) {
56 root 1.113 my $widget = $ROOT->find_widget ($x, $y);
57 root 1.51
58     $GRAB = $widget;
59     $GRAB->update if $GRAB;
60 root 1.151
61     check_tooltip;
62 root 1.51 }
63    
64 root 1.137 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
65 root 1.51
66 root 1.113 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
67 root 1.51 }
68    
69     sub feed_sdl_button_up_event {
70     my ($ev) = @_;
71 root 1.137 my ($x, $y) = ($ev->{x}, $ev->{y});
72 root 1.51
73 root 1.113 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
74 root 1.51
75 root 1.137 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
76 root 1.51
77 root 1.113 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
78 root 1.58
79 root 1.51 if (!$BUTTON_STATE) {
80     my $grab = $GRAB; undef $GRAB;
81     $grab->update if $grab;
82     $GRAB->update if $GRAB;
83 root 1.151
84     check_tooltip;
85 root 1.51 }
86     }
87    
88     sub feed_sdl_motion_event {
89     my ($ev) = @_;
90 root 1.137 my ($x, $y) = ($ev->{x}, $ev->{y});
91 root 1.51
92 root 1.113 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
93 root 1.51
94     if ($widget != $HOVER) {
95     my $hover = $HOVER; $HOVER = $widget;
96    
97 root 1.105 $hover->update if $hover && $hover->{can_hover};
98     $HOVER->update if $HOVER && $HOVER->{can_hover};
99 root 1.151
100     check_tooltip;
101 root 1.51 }
102    
103 root 1.113 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
104 root 1.51 }
105 elmex 1.1
106 root 1.112 # convert position array to integers
107     sub harmonize {
108     my ($vals) = @_;
109    
110     my $rem = 0;
111    
112 root 1.116 for (@$vals) {
113 root 1.112 my $i = int $_ + $rem;
114     $rem += $_ - $i;
115     $_ = $i;
116     }
117     }
118    
119 root 1.73 #############################################################################
120    
121     package CFClient::UI::Base;
122    
123     use strict;
124    
125 root 1.138 use CFClient::OpenGL;
126 root 1.73
127 elmex 1.1 sub new {
128     my $class = shift;
129 root 1.10
130 root 1.79 my $self = bless {
131 root 1.65 x => 0,
132     y => 0,
133     z => 0,
134 elmex 1.150 can_events => 1,
135 root 1.65 @_
136 root 1.79 }, $class;
137    
138     for (keys %$self) {
139     if (/^connect_(.*)$/) {
140     $self->connect ($1 => delete $self->{$_});
141     }
142     }
143    
144     $self
145 elmex 1.1 }
146    
147 root 1.159 sub destroy {
148     my ($self) = @_;
149    
150     $self->hide;
151     %$self = ();
152     }
153    
154 root 1.134 sub show {
155     my ($self) = @_;
156    
157     return if $self->{parent};
158    
159     $CFClient::UI::ROOT->add ($self);
160     }
161    
162     sub hide {
163     my ($self) = @_;
164    
165     return unless $self->{parent};
166    
167     $self->{parent}->remove ($self);
168     }
169    
170 root 1.18 sub move {
171     my ($self, $x, $y, $z) = @_;
172 root 1.128
173 root 1.117 $self->{x} = int $x;
174     $self->{y} = int $y;
175 root 1.18 $self->{z} = $z if defined $z;
176 root 1.128
177     $self->update;
178 root 1.18 }
179    
180 root 1.158 sub set_size {
181     my ($self, $w, $h) = @_;
182    
183     $self->{user_w} = $w;
184     $self->{user_h} = $h;
185    
186     $self->check_size;
187 elmex 1.20 }
188    
189 root 1.14 sub size_request {
190 elmex 1.36 require Carp;
191 root 1.147 Carp::confess "size_request is abstract";
192 elmex 1.36 }
193    
194 root 1.128 sub configure {
195 root 1.75 my ($self, $x, $y, $w, $h) = @_;
196    
197 root 1.141 if ($self->{aspect}) {
198     my $w2 = List::Util::min $w, int $h * $self->{aspect};
199     my $h2 = List::Util::min $h, int $w / $self->{aspect};
200    
201     # use alignment to adjust x, y
202 root 1.75
203 root 1.147 $x += int +($w - $w2) * 0.5;
204     $y += int +($h - $h2) * 0.5;
205 root 1.141
206     ($w, $h) = ($w2, $h2);
207 root 1.140 }
208    
209 root 1.141 if ($self->{x} != $x || $self->{y} != $y) {
210     $self->{x} = $x;
211     $self->{y} = $y;
212     $self->update;
213     }
214 root 1.40
215 root 1.141 if ($self->{w} != $w || $self->{h} != $h) {
216     $self->{w} = $w;
217     $self->{h} = $h;
218 root 1.75
219 root 1.141 $self->size_allocate ($w, $h);
220     $self->update;
221     }
222 root 1.75 }
223    
224     sub size_allocate {
225 root 1.128 # nothing to be done
226 root 1.14 }
227    
228 root 1.157 sub set_max_size {
229     my ($self, $w, $h) = @_;
230    
231     delete $self->{max_w}; $self->{max_w} = $w if $w;
232     delete $self->{max_h}; $self->{max_h} = $h if $h;
233     }
234    
235 root 1.113 # return top left coordinates
236     sub _topleft {
237     my ($self, $x, $y) = @_;
238    
239     $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
240     }
241    
242 root 1.82 # translate global coordinates to local coordinate system
243 root 1.113 sub coord2local {
244 root 1.58 my ($self, $x, $y) = @_;
245    
246 root 1.113 my ($X, $Y) = $self->_topleft;
247     ($x - $X, $y - $Y)
248     }
249    
250     # translate local coordinates to global coordinate system
251     sub coord2global {
252     my ($self, $x, $y) = @_;
253    
254     my ($X, $Y) = $self->_topleft;
255     ($x + $X, $y + $Y)
256 root 1.58 }
257    
258 elmex 1.1 sub focus_in {
259 root 1.51 my ($self) = @_;
260    
261 root 1.68 return if $FOCUS == $self;
262 root 1.97 return unless $self->{can_focus};
263 root 1.68
264 root 1.51 my $focus = $FOCUS; $FOCUS = $self;
265 elmex 1.120
266     $self->emit (focus_in => $focus);
267    
268 root 1.51 $focus->update if $focus;
269     $FOCUS->update;
270 elmex 1.1 }
271 root 1.4
272 elmex 1.1 sub focus_out {
273 root 1.51 my ($self) = @_;
274 root 1.4
275 root 1.51 return unless $FOCUS == $self;
276 root 1.4
277 root 1.51 my $focus = $FOCUS; undef $FOCUS;
278 elmex 1.120
279     $self->emit (focus_out => $focus);
280    
281 root 1.51 $focus->update if $focus; #?
282 elmex 1.1 }
283 root 1.4
284 root 1.51 sub mouse_motion { }
285     sub button_up { }
286     sub key_down { }
287     sub key_up { }
288    
289 root 1.68 sub button_down {
290     my ($self, $ev, $x, $y) = @_;
291    
292     $self->focus_in;
293     }
294    
295 root 1.51 sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
296     sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
297     sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
298     sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
299     sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
300 elmex 1.11
301 elmex 1.1 sub draw {
302 elmex 1.11 my ($self) = @_;
303    
304 root 1.68 return unless $self->{h} && $self->{w};
305    
306 elmex 1.11 glPushMatrix;
307 root 1.12 glTranslate $self->{x}, $self->{y}, 0;
308 elmex 1.11 $self->_draw;
309 root 1.72 glPopMatrix;
310    
311 root 1.97 if ($self == $HOVER && $self->{can_hover}) {
312 root 1.73 my ($x, $y) = @$self{qw(x y)};
313 root 1.72
314 root 1.122 glColor 1, 0.8, 0.5, 0.2;
315 root 1.50 glEnable GL_BLEND;
316 root 1.74 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
317 root 1.48 glBegin GL_QUADS;
318 root 1.72 glVertex $x , $y;
319     glVertex $x + $self->{w}, $y;
320     glVertex $x + $self->{w}, $y + $self->{h};
321     glVertex $x , $y + $self->{h};
322 root 1.47 glEnd;
323 root 1.50 glDisable GL_BLEND;
324 root 1.47 }
325 elmex 1.11 }
326    
327     sub _draw {
328 root 1.38 my ($self) = @_;
329    
330     warn "no draw defined for $self\n";
331 elmex 1.1 }
332 root 1.4
333 root 1.38 sub find_widget {
334     my ($self, $x, $y) = @_;
335    
336 elmex 1.150 return () unless $self->{can_events};
337    
338 root 1.38 return $self
339     if $x >= $self->{x} && $x < $self->{x} + $self->{w}
340     && $y >= $self->{y} && $y < $self->{y} + $self->{h};
341    
342     ()
343     }
344    
345 elmex 1.32 sub set_parent {
346 root 1.113 my ($self, $parent) = @_;
347 elmex 1.32
348 root 1.113 Scalar::Util::weaken ($self->{parent} = $parent);
349 elmex 1.32 }
350    
351 root 1.125 sub check_size {
352     my ($self) = @_;
353    
354 root 1.158 $self->{parent}
355     or return 1;
356 root 1.149
357 root 1.158 my ($w, $h) = $self->{user_w} && $self->{user_h}
358     ? @$self{qw(user_w user_h)}
359     : $self->size_request;
360 root 1.125
361 root 1.158 if ($w != $self->{req_w} || $h != $self->{req_h}) {
362 root 1.125 $self->{req_w} = $w;
363     $self->{req_h} = $h;
364    
365 root 1.158 $self->{parent}->check_size
366     or $self->size_allocate (
367     (List::Util::max $self->{w}, $w),
368     (List::Util::max $self->{h}, $h),
369     );
370    
371     1
372     } else {
373     0
374 root 1.125 }
375     }
376    
377 elmex 1.32 sub update {
378     my ($self) = @_;
379    
380     $self->{parent}->update
381     if $self->{parent};
382 elmex 1.1 }
383 elmex 1.2
384 root 1.72 sub connect {
385     my ($self, $signal, $cb) = @_;
386    
387 root 1.133 push @{ $self->{signal_cb}{$signal} }, $cb;
388 root 1.72 }
389    
390     sub emit {
391     my ($self, $signal, @args) = @_;
392    
393 root 1.133 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
394     $cb->($self, @args);
395     }
396 root 1.72 }
397    
398 root 1.18 sub DESTROY {
399     my ($self) = @_;
400    
401 elmex 1.32 #$self->deactivate;
402 root 1.18 }
403    
404 root 1.39 #############################################################################
405    
406 root 1.73 package CFClient::UI::DrawBG;
407 root 1.68
408 root 1.73 our @ISA = CFClient::UI::Base::;
409 root 1.68
410     use strict;
411 root 1.138 use CFClient::OpenGL;
412 root 1.68
413     sub new {
414     my $class = shift;
415    
416     # range [value, low, high, page]
417    
418     $class->SUPER::new (
419 root 1.76 bg => [0, 0, 0, 0.2],
420 root 1.79 active_bg => [1, 1, 1, 0.5],
421 root 1.68 @_
422     )
423     }
424    
425     sub _draw {
426     my ($self) = @_;
427    
428     my ($w, $h) = @$self{qw(w h)};
429    
430 root 1.76 glEnable GL_BLEND;
431     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
432 root 1.68 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
433 root 1.76
434 root 1.68 glBegin GL_QUADS;
435     glVertex 0 , 0;
436     glVertex 0 , $h;
437     glVertex $w, $h;
438     glVertex $w, 0;
439     glEnd;
440 root 1.76
441     glDisable GL_BLEND;
442 root 1.68 }
443    
444     #############################################################################
445    
446 root 1.73 package CFClient::UI::Empty;
447 root 1.66
448 root 1.73 our @ISA = CFClient::UI::Base::;
449 root 1.66
450 elmex 1.150 sub new {
451     my ($class, %arg) = @_;
452     $class->SUPER::new (can_events => 0, %arg);
453     }
454    
455 root 1.66 sub size_request {
456     (0, 0)
457     }
458    
459 root 1.67 sub draw { }
460 root 1.66
461     #############################################################################
462    
463 root 1.73 package CFClient::UI::Container;
464 elmex 1.15
465 root 1.73 our @ISA = CFClient::UI::Base::;
466 elmex 1.15
467 root 1.38 sub new {
468 root 1.64 my ($class, %arg) = @_;
469    
470     my $children = delete $arg{children} || [];
471 root 1.38
472 elmex 1.150 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
473 root 1.64 $self->add ($_) for @$children;
474 root 1.38
475     $self
476     }
477    
478     sub add {
479 root 1.113 my ($self, $child) = @_;
480    
481     $child->set_parent ($self);
482 root 1.38
483 root 1.113 use sort 'stable';
484 root 1.38
485 root 1.66 $self->{children} = [
486 root 1.38 sort { $a->{z} <=> $b->{z} }
487 root 1.113 @{$self->{children}}, $child
488 root 1.66 ];
489 root 1.38
490 root 1.126 $child->check_size;
491 root 1.38 }
492 root 1.35
493 elmex 1.32 sub remove {
494 root 1.134 my ($self, $child) = @_;
495    
496     delete $child->{parent};
497 root 1.38
498 root 1.134 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
499 root 1.38
500 root 1.125 $self->check_size;
501 root 1.151 $self->update;
502 root 1.38 }
503    
504     sub find_widget {
505     my ($self, $x, $y) = @_;
506    
507 root 1.45 $x -= $self->{x};
508     $y -= $self->{y};
509    
510 root 1.38 my $res;
511    
512 root 1.46 for (reverse @{ $self->{children} }) {
513 root 1.45 $res = $_->find_widget ($x, $y)
514 root 1.38 and return $res;
515     }
516    
517 root 1.46 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
518 elmex 1.32 }
519 elmex 1.15
520 root 1.35 sub _draw {
521     my ($self) = @_;
522    
523 root 1.38 $_->draw for @{$self->{children}};
524 root 1.35 }
525 elmex 1.15
526 root 1.39 #############################################################################
527    
528 root 1.73 package CFClient::UI::Bin;
529 elmex 1.32
530 root 1.73 our @ISA = CFClient::UI::Container::;
531 elmex 1.32
532 root 1.66 sub new {
533     my ($class, %arg) = @_;
534    
535 root 1.73 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
536 root 1.66
537     $class->SUPER::new (children => [$child], %arg)
538     }
539    
540     sub add {
541 root 1.134 my ($self, $child) = @_;
542 root 1.66
543     $self->{children} = [];
544    
545 root 1.134 $self->SUPER::add ($child);
546 root 1.66 }
547    
548     sub remove {
549     my ($self, $widget) = @_;
550    
551     $self->SUPER::remove ($widget);
552    
553 root 1.73 $self->{children} = [new CFClient::UI::Empty]
554 root 1.66 unless @{$self->{children}};
555     }
556    
557 root 1.39 sub child { $_[0]->{children}[0] }
558 elmex 1.32
559 root 1.38 sub size_request {
560 root 1.68 $_[0]{children}[0]->size_request
561 root 1.38 }
562 elmex 1.32
563 root 1.38 sub size_allocate {
564 root 1.128 my ($self, $w, $h) = @_;
565 root 1.42
566 root 1.128 $self->{children}[0]->configure (0, 0, $w, $h);
567 root 1.38 }
568 elmex 1.32
569 root 1.39 #############################################################################
570    
571 root 1.73 package CFClient::UI::Window;
572 elmex 1.20
573 root 1.73 our @ISA = CFClient::UI::Bin::;
574 elmex 1.20
575 root 1.138 use CFClient::OpenGL;
576 elmex 1.20
577 root 1.42 sub new {
578 root 1.64 my ($class, %arg) = @_;
579 elmex 1.32
580 root 1.64 my $self = $class->SUPER::new (%arg);
581 elmex 1.32 }
582    
583     sub update {
584     my ($self) = @_;
585 root 1.42
586 root 1.63 # we want to do this delayed...
587 elmex 1.32 $self->render_chld;
588 root 1.42 $self->SUPER::update;
589 elmex 1.20 }
590    
591     sub render_chld {
592     my ($self) = @_;
593    
594 root 1.105 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
595     glClearColor 0, 0, 0, 1;
596     glClear GL_COLOR_BUFFER_BIT;
597     $self->child->draw;
598     };
599 elmex 1.36 }
600    
601     sub size_allocate {
602 root 1.128 my ($self, $w, $h) = @_;
603 elmex 1.36
604 root 1.128 $self->child->configure (0, 0, $w, $h);
605 elmex 1.36
606 root 1.42 $self->render_chld;
607 elmex 1.20 }
608    
609     sub _draw {
610     my ($self) = @_;
611    
612 elmex 1.36 my ($w, $h) = ($self->w, $self->h);
613 root 1.29
614 elmex 1.20 my $tex = $self->{texture}
615     or return;
616    
617     glEnable GL_BLEND;
618 root 1.74 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
619 elmex 1.20 glEnable GL_TEXTURE_2D;
620 root 1.35 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
621 elmex 1.20
622 root 1.56 $tex->draw_quad (0, 0, $w, $h);
623 elmex 1.20
624     glDisable GL_BLEND;
625     glDisable GL_TEXTURE_2D;
626     }
627    
628 root 1.39 #############################################################################
629    
630 root 1.125 package CFClient::UI::ViewPort;
631    
632     our @ISA = CFClient::UI::Window::;
633    
634     sub new { die }
635    
636     sub size_request {
637     my ($self) = @_;
638    
639 root 1.149 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
640 root 1.125 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
641    
642     @$self{qw(child_w child_h)}
643     }
644    
645     sub _draw {
646     my ($self) = @_;
647    
648     $self->{children}[1]->draw;
649     }
650    
651    
652     #############################################################################
653    
654 root 1.73 package CFClient::UI::Frame;
655 elmex 1.15
656 root 1.73 our @ISA = CFClient::UI::Bin::;
657 elmex 1.15
658 root 1.138 use CFClient::OpenGL;
659 elmex 1.15
660 elmex 1.148 sub new {
661     my $class = shift;
662    
663     my $self = $class->SUPER::new (
664     bg => [1, 1, 1, 1],
665     border_bg => [1, 1, 1, 1],
666     border => 0.8,
667     @_
668     );
669    
670     $self
671     }
672    
673 elmex 1.15 sub _draw {
674     my ($self) = @_;
675    
676 elmex 1.148 my ($w, $h) = ($self->{w}, $self->{h});
677    
678     glEnable GL_BLEND;
679     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
680     glEnable GL_TEXTURE_2D;
681     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
682 elmex 1.15
683 elmex 1.148 # glBegin GL_QUADS;
684     # glColor 0, 0, 0, 0;
685     # glVertex 0 , 0;
686     # glVertex 0 , $h;
687     # glVertex $w, $h;
688     # glVertex $w, 0;
689     # glEnd;
690 elmex 1.15
691    
692 elmex 1.148 $self->child->draw;
693     glDisable GL_BLEND;
694     glDisable GL_TEXTURE_2D;
695 elmex 1.15 }
696    
697 root 1.39 #############################################################################
698    
699 root 1.73 package CFClient::UI::FancyFrame;
700 elmex 1.31
701 root 1.73 our @ISA = CFClient::UI::Bin::;
702 elmex 1.31
703 root 1.138 use CFClient::OpenGL;
704 elmex 1.31
705 root 1.41 my @tex =
706 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
707 root 1.41 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
708 elmex 1.34
709 root 1.97 sub new {
710     my $class = shift;
711    
712     # TODO: user_x, user_y, overwrite moveto?
713    
714 root 1.141 my $self = $class->SUPER::new (
715 root 1.97 bg => [1, 1, 1, 1],
716     border_bg => [1, 1, 1, 1],
717 root 1.134 border => 0.8,
718 elmex 1.150 can_events => 1,
719 root 1.97 @_
720 root 1.141 );
721    
722     $self->{title} &&= new CFClient::UI::Label
723     align => 0,
724     valign => 1,
725     text => $self->{title},
726     fontsize => 1;
727    
728     $self
729 root 1.97 }
730    
731 root 1.134 sub border {
732     int $_[0]{border} * $::FONTSIZE
733     }
734    
735 elmex 1.34 sub size_request {
736     my ($self) = @_;
737 root 1.39
738 root 1.78 my ($w, $h) = $self->SUPER::size_request;
739 elmex 1.34
740 root 1.97 (
741 root 1.134 $w + $self->border * 2,
742     $h + $self->border * 2,
743 root 1.97 )
744 elmex 1.36 }
745    
746     sub size_allocate {
747 root 1.128 my ($self, $w, $h) = @_;
748 root 1.40
749 root 1.134 $h -= List::Util::max 0, $self->border * 2;
750     $w -= List::Util::max 0, $self->border * 2;
751 elmex 1.36
752 root 1.141 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
753     if $self->{title};
754    
755 root 1.134 $self->child->configure ($self->border, $self->border, $w, $h);
756 elmex 1.34 }
757    
758 root 1.77 sub button_down {
759     my ($self, $ev, $x, $y) = @_;
760    
761 root 1.134 my $border = $self->border;
762    
763     if ($x < $self->{w} && $x >= $self->{w} - $border
764     && $y < $self->{h} && $y >= $self->{h} - $border) {
765 root 1.77
766 root 1.139 my ($ox, $oy) = ($ev->{x}, $ev->{y});
767 root 1.77 my ($bw, $bh) = ($self->{w}, $self->{h});
768    
769     $self->{motion} = sub {
770     my ($ev, $x, $y) = @_;
771    
772 root 1.139 ($x, $y) = ($ev->{x}, $ev->{y});
773 root 1.77
774     $self->{user_w} = $bw + $x - $ox;
775     $self->{user_h} = $bh + $y - $oy;
776 root 1.128 $self->check_size;
777 root 1.77 };
778    
779     } elsif ($x >= 0 && $x < $self->{w}
780 root 1.134 && $y >= 0 && $y < $border) {
781 root 1.77
782 root 1.139 my ($ox, $oy) = ($ev->{x}, $ev->{y});
783 root 1.77 my ($bx, $by) = ($self->{x}, $self->{y});
784    
785     $self->{motion} = sub {
786     my ($ev, $x, $y) = @_;
787    
788 root 1.139 ($x, $y) = ($ev->{x}, $ev->{y});
789 root 1.77
790     $self->move ($bx + $x - $ox, $by + $y - $oy);
791     $self->update;
792     };
793     }
794     }
795    
796     sub button_up {
797     my ($self, $ev, $x, $y) = @_;
798    
799     delete $self->{motion};
800     }
801    
802     sub mouse_motion {
803     my ($self, $ev, $x, $y) = @_;
804    
805     $self->{motion}->($ev, $x, $y) if $self->{motion};
806     }
807    
808 elmex 1.34 sub _draw {
809     my ($self) = @_;
810    
811 root 1.97 my ($w, $h ) = ($self->{w}, $self->{h});
812 root 1.43 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
813 elmex 1.34
814     glEnable GL_BLEND;
815 root 1.74 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
816 elmex 1.34 glEnable GL_TEXTURE_2D;
817 root 1.97 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
818 elmex 1.34
819 root 1.134 my $border = $self->border;
820    
821 root 1.97 glColor @{ $self->{border_bg} };
822 root 1.134 $tex[1]->draw_quad (0, 0, $w, $border);
823     $tex[3]->draw_quad (0, $border, $border, $ch);
824     $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
825     $tex[4]->draw_quad (0, $h - $border, $w, $border);
826 elmex 1.34
827 root 1.41 my $bg = $tex[0];
828 root 1.76
829 root 1.97 # TODO: repeat texture not scale
830 root 1.72 my $rep_x = $cw / $bg->{w};
831     my $rep_y = $ch / $bg->{h};
832 elmex 1.34
833 root 1.97 glColor @{ $self->{bg} };
834 elmex 1.100
835     $bg->{s} = $rep_x;
836     $bg->{t} = $rep_y;
837     $bg->{wrap_mode} = 1;
838 root 1.134 $bg->draw_quad ($border, $border, $cw, $ch);
839 elmex 1.34
840 root 1.76 glDisable GL_TEXTURE_2D;
841 elmex 1.34 glDisable GL_BLEND;
842 elmex 1.36
843 root 1.141 $self->{title}->draw if $self->{title};
844 root 1.39 $self->child->draw;
845 elmex 1.34 }
846 elmex 1.31
847 root 1.39 #############################################################################
848    
849 root 1.73 package CFClient::UI::Table;
850 elmex 1.15
851 root 1.73 our @ISA = CFClient::UI::Base::;
852 elmex 1.15
853 root 1.75 use List::Util qw(max sum);
854    
855 root 1.138 use CFClient::OpenGL;
856 elmex 1.15
857 root 1.78 sub new {
858     my $class = shift;
859    
860     $class->SUPER::new (
861     col_expand => [],
862     @_
863     )
864     }
865    
866 elmex 1.15 sub add {
867 root 1.113 my ($self, $x, $y, $child) = @_;
868 elmex 1.32
869 root 1.113 $child->set_parent ($self);
870     $self->{children}[$y][$x] = $child;
871 root 1.75
872 root 1.126 $child->check_size;
873 elmex 1.15 }
874    
875 root 1.115 # TODO: move to container class maybe? send childs a signal on removal?
876     sub clear {
877     my ($self) = @_;
878    
879     delete $self->{children};
880     $self->update;
881     }
882    
883 root 1.75 sub get_wh {
884     my ($self) = @_;
885    
886     my (@w, @h);
887 elmex 1.15
888 root 1.75 for my $y (0 .. $#{$self->{children}}) {
889     my $row = $self->{children}[$y]
890     or next;
891 elmex 1.15
892 root 1.75 for my $x (0 .. $#$row) {
893     my $widget = $row->[$x]
894     or next;
895 root 1.149 my ($w, $h) = @$widget{qw(req_w req_h)};
896 elmex 1.15
897 root 1.75 $w[$x] = max $w[$x], $w;
898     $h[$y] = max $h[$y], $h;
899 elmex 1.17 }
900 elmex 1.15 }
901 root 1.75
902     (\@w, \@h)
903 elmex 1.15 }
904    
905     sub size_request {
906     my ($self) = @_;
907    
908 root 1.75 my ($ws, $hs) = $self->get_wh;
909 elmex 1.15
910 root 1.75 (
911 root 1.78 (sum @$ws),
912     (sum @$hs),
913 root 1.75 )
914     }
915    
916     sub size_allocate {
917 root 1.128 my ($self, $w, $h) = @_;
918 root 1.75
919     my ($ws, $hs) = $self->get_wh;
920    
921 root 1.78 my $req_w = sum @$ws;
922     my $req_h = sum @$hs;
923    
924     # TODO: nicer code && do row_expand
925     my @col_expand = @{$self->{col_expand}};
926     @col_expand = (1) x @$ws unless @col_expand;
927     my $col_expand = (sum @col_expand) || 1;
928 elmex 1.15
929 root 1.75 # linearly scale sizes
930 root 1.78 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
931     $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
932 elmex 1.15
933 root 1.112 CFClient::UI::harmonize $ws;
934     CFClient::UI::harmonize $hs;
935 root 1.106
936 root 1.75 my $y;
937 elmex 1.15
938 root 1.75 for my $r (0 .. $#{$self->{children}}) {
939     my $row = $self->{children}[$r]
940     or next;
941 elmex 1.15
942     my $x = 0;
943 root 1.75 my $row_h = $hs->[$r];
944    
945     for my $c (0 .. $#$row) {
946     my $col_w = $ws->[$c];
947 elmex 1.15
948 root 1.83 if (my $widget = $row->[$c]) {
949 root 1.128 $widget->configure ($x, $y, $col_w, $row_h);
950 root 1.83 }
951 elmex 1.15
952 root 1.75 $x += $col_w;
953 elmex 1.15 }
954    
955 root 1.75 $y += $row_h;
956     }
957    
958     }
959    
960 root 1.76 sub find_widget {
961     my ($self, $x, $y) = @_;
962    
963     $x -= $self->{x};
964     $y -= $self->{y};
965    
966     my $res;
967    
968     for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
969     $res = $_->find_widget ($x, $y)
970     and return $res;
971     }
972    
973     $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
974     }
975    
976 root 1.75 sub _draw {
977     my ($self) = @_;
978    
979     for (grep $_, @{$self->{children}}) {
980     $_->draw for grep $_, @$_;
981 elmex 1.15 }
982     }
983    
984 root 1.39 #############################################################################
985    
986 root 1.76 package CFClient::UI::HBox;
987    
988     # TODO: wrap into common Box base class
989    
990     our @ISA = CFClient::UI::Container::;
991    
992     sub size_request {
993     my ($self) = @_;
994    
995     my @alloc = map [$_->size_request], @{$self->{children}};
996    
997     (
998     (List::Util::sum map $_->[0], @alloc),
999     (List::Util::max map $_->[1], @alloc),
1000     )
1001     }
1002    
1003     sub size_allocate {
1004 root 1.128 my ($self, $w, $h) = @_;
1005 root 1.76
1006     ($h, $w) = ($w, $h);
1007    
1008     my $children = $self->{children};
1009    
1010 root 1.149 my @h = map $_->{req_w}, @$children;
1011 root 1.76
1012     my $req_h = List::Util::sum @h;
1013    
1014     if ($req_h > $h) {
1015     # ah well, not enough space
1016 root 1.78 $_ *= $h / $req_h for @h;
1017 root 1.76 } else {
1018 root 1.77 my $exp = List::Util::sum map $_->{expand}, @$children;
1019     $exp ||= 1;
1020 root 1.76
1021     for (0 .. $#$children) {
1022     my $child = $children->[$_];
1023    
1024     my $alloc_h = $h[$_];
1025 root 1.77 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1026 root 1.76 $h[$_] = $alloc_h;
1027     }
1028     }
1029    
1030 root 1.112 CFClient::UI::harmonize \@h;
1031    
1032 root 1.76 my $y = 0;
1033     for (0 .. $#$children) {
1034     my $child = $children->[$_];
1035 root 1.112 my $h = $h[$_];
1036 root 1.128 $child->configure ($y, 0, $h, $w);
1037 root 1.76
1038     $y += $h;
1039     }
1040 root 1.125
1041     1
1042 root 1.76 }
1043    
1044     #############################################################################
1045    
1046 root 1.73 package CFClient::UI::VBox;
1047 elmex 1.15
1048 root 1.76 # TODO: wrap into common Box base class
1049    
1050 root 1.73 our @ISA = CFClient::UI::Container::;
1051 elmex 1.15
1052 root 1.43 sub size_request {
1053     my ($self) = @_;
1054    
1055     my @alloc = map [$_->size_request], @{$self->{children}};
1056    
1057     (
1058     (List::Util::max map $_->[0], @alloc),
1059     (List::Util::sum map $_->[1], @alloc),
1060     )
1061     }
1062    
1063 elmex 1.36 sub size_allocate {
1064 root 1.128 my ($self, $w, $h) = @_;
1065 root 1.68
1066     my $children = $self->{children};
1067    
1068 root 1.149 my @h = map $_->{req_h}, @$children;
1069 root 1.68
1070     my $req_h = List::Util::sum @h;
1071    
1072     if ($req_h > $h) {
1073     # ah well, not enough space
1074 root 1.78 $_ *= $h / $req_h for @h;
1075 root 1.68 } else {
1076 root 1.77 my $exp = List::Util::sum map $_->{expand}, @$children;
1077     $exp ||= 1;
1078 root 1.68
1079     for (0 .. $#$children) {
1080     my $child = $children->[$_];
1081 elmex 1.36
1082 root 1.77 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1083 root 1.68 }
1084 elmex 1.36 }
1085    
1086 root 1.112 CFClient::UI::harmonize \@h;
1087    
1088 elmex 1.36 my $y = 0;
1089 root 1.68 for (0 .. $#$children) {
1090     my $child = $children->[$_];
1091 root 1.112 my $h = $h[$_];
1092 root 1.128 $child->configure (0, $y, $w, $h);
1093 elmex 1.36
1094 root 1.68 $y += $h;
1095 elmex 1.36 }
1096 root 1.125
1097     1
1098 elmex 1.36 }
1099    
1100 root 1.39 #############################################################################
1101    
1102 root 1.73 package CFClient::UI::Label;
1103 root 1.10
1104 root 1.73 our @ISA = CFClient::UI::Base::;
1105 root 1.12
1106 root 1.138 use CFClient::OpenGL;
1107 root 1.10
1108     sub new {
1109 root 1.64 my ($class, %arg) = @_;
1110 root 1.51
1111 root 1.59 my $self = $class->SUPER::new (
1112 root 1.105 fg => [1, 1, 1],
1113 root 1.157 #font => default_font
1114 root 1.134 fontsize => 1,
1115 root 1.105 text => "",
1116     align => -1,
1117 root 1.116 valign => -1,
1118 root 1.105 padding => 2,
1119     layout => new CFClient::Layout,
1120 elmex 1.150 can_events => 0,
1121 root 1.64 %arg
1122 root 1.59 );
1123 root 1.10
1124 root 1.141 if (exists $self->{template}) {
1125     my $layout = new CFClient::Layout;
1126     $layout->set_text (delete $self->{template});
1127     $self->{template} = $layout;
1128     }
1129 root 1.121
1130     $self->set_text (delete $self->{text}) if exists $self->{text};
1131     $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1132 root 1.10
1133     $self
1134     }
1135    
1136 root 1.68 sub escape_text {
1137     local $_ = $_[1];
1138    
1139     s/&/&amp;/g;
1140     s/>/&gt;/g;
1141     s/</&lt;/g;
1142    
1143     $_[1]
1144     }
1145    
1146 elmex 1.15 sub set_text {
1147     my ($self, $text) = @_;
1148 root 1.28
1149 root 1.121 $self->{layout}->set_text ($text);
1150 root 1.28
1151 root 1.59 delete $self->{texture};
1152 root 1.149 $self->check_size;
1153 root 1.68 $self->update;
1154 elmex 1.15 }
1155    
1156 root 1.121 sub set_markup {
1157     my ($self, $markup) = @_;
1158    
1159     $self->{layout}->set_markup ($markup);
1160 root 1.28
1161 root 1.121 delete $self->{texture};
1162 root 1.149 $self->check_size;
1163 root 1.121 $self->update;
1164 elmex 1.15 }
1165    
1166 root 1.14 sub size_request {
1167     my ($self) = @_;
1168    
1169 root 1.157 $self->{layout}->set_font ($self->{font}) if $self->{font};
1170     $self->{layout}->set_width ($self->{max_w} || -1);
1171 root 1.134 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1172 root 1.121
1173 root 1.76 my ($w, $h) = $self->{layout}->size;
1174    
1175 root 1.141 if (exists $self->{template}) {
1176 root 1.157 $self->{template}->set_font ($self->{font}) if $self->{font};
1177 root 1.141 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1178    
1179     my ($w2, $h2) = $self->{template}->size;
1180    
1181     $w = List::Util::max $w, $w2;
1182     $h = List::Util::max $h, $h2;
1183     }
1184    
1185 root 1.76 (
1186     $w + $self->{padding} * 2,
1187     $h + $self->{padding} * 2,
1188     )
1189 root 1.59 }
1190 root 1.51
1191 root 1.59 sub size_allocate {
1192 root 1.128 my ($self, $w, $h) = @_;
1193 root 1.68
1194 root 1.59 delete $self->{texture};
1195 root 1.14 }
1196    
1197 elmex 1.146 sub set_fontsize {
1198     my ($self, $fontsize) = @_;
1199    
1200     $self->{fontsize} = $fontsize;
1201 root 1.152 delete $self->{texture};
1202 root 1.147 $self->check_size;
1203 root 1.152 $self->update;
1204 elmex 1.146 }
1205    
1206 elmex 1.11 sub _draw {
1207 root 1.10 my ($self) = @_;
1208    
1209 root 1.59 my $tex = $self->{texture} ||= do {
1210 root 1.157 $self->{layout}->set_font ($self->{font}) if $self->{font};
1211 root 1.59 $self->{layout}->set_width ($self->{w});
1212 root 1.134 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1213 root 1.74 new_from_layout CFClient::Texture $self->{layout}
1214 root 1.59 };
1215 root 1.10
1216 root 1.12 glEnable GL_BLEND;
1217 root 1.74 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1218 root 1.10 glEnable GL_TEXTURE_2D;
1219 root 1.105 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1220 root 1.10
1221 root 1.68 glColor @{$self->{fg}};
1222 root 1.12
1223 root 1.122 $self->{ox} = int (
1224 root 1.76 $self->{align} < 0 ? $self->{padding}
1225     : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1226 root 1.122 : ($self->{w} - $tex->{w}) * 0.5
1227     );
1228 root 1.72
1229 root 1.122 $self->{oy} = int (
1230 root 1.116 $self->{valign} < 0 ? $self->{padding}
1231     : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1232 root 1.122 : ($self->{h} - $tex->{h}) * 0.5
1233     );
1234 root 1.116
1235 root 1.122 $tex->draw_quad ($self->{ox}, $self->{oy});
1236 root 1.10
1237 root 1.74 glDisable GL_TEXTURE_2D;
1238 root 1.12 glDisable GL_BLEND;
1239 root 1.10 }
1240    
1241 root 1.39 #############################################################################
1242    
1243 root 1.121 package CFClient::UI::EntryBase;
1244 elmex 1.31
1245 root 1.73 our @ISA = CFClient::UI::Label::;
1246 elmex 1.31
1247 root 1.138 use CFClient::OpenGL;
1248 elmex 1.31
1249 root 1.68 sub new {
1250     my $class = shift;
1251    
1252     $class->SUPER::new (
1253     fg => [1, 1, 1],
1254 root 1.76 bg => [0, 0, 0, 0.2],
1255 root 1.79 active_bg => [1, 1, 1, 0.5],
1256 root 1.68 active_fg => [0, 0, 0],
1257 root 1.97 can_hover => 1,
1258     can_focus => 1,
1259 root 1.116 valign => 0,
1260 elmex 1.150 can_events => 1,
1261 root 1.68 @_
1262     )
1263     }
1264    
1265     sub _set_text {
1266     my ($self, $text) = @_;
1267    
1268 root 1.121 delete $self->{cur_h};
1269    
1270     return if $self->{text} eq $text;
1271 elmex 1.100
1272 root 1.134 delete $self->{texture};
1273    
1274 root 1.68 $self->{last_activity} = $::NOW;
1275     $self->{text} = $text;
1276 root 1.72
1277     $text =~ s/./*/g if $self->{hidden};
1278 root 1.121 $self->{layout}->set_text ("$text ");
1279 root 1.72
1280 root 1.121 $self->emit (changed => $self->{text});
1281     }
1282 root 1.68
1283 root 1.121 sub get_text {
1284     $_[0]{text}
1285 root 1.68 }
1286    
1287     sub size_request {
1288     my ($self) = @_;
1289    
1290     my ($w, $h) = $self->SUPER::size_request;
1291    
1292     ($w + 1, $h) # add 1 for cursor
1293     }
1294    
1295     sub size_allocate {
1296 root 1.128 my ($self, $w, $h) = @_;
1297 root 1.68
1298 root 1.149 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1299 root 1.68 }
1300    
1301     sub set_text {
1302     my ($self, $text) = @_;
1303    
1304     $self->{cursor} = length $text;
1305     $self->_set_text ($text);
1306     $self->update;
1307     }
1308    
1309 elmex 1.31 sub key_down {
1310     my ($self, $ev) = @_;
1311    
1312 root 1.137 my $mod = $ev->{mod};
1313     my $sym = $ev->{sym};
1314     my $uni = $ev->{unicode};
1315 elmex 1.31
1316     my $text = $self->get_text;
1317    
1318 root 1.136 if ($sym == 8) {
1319 root 1.68 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1320 root 1.136 } elsif ($sym == 127) {
1321 root 1.68 substr $text, $self->{cursor}, 1, "";
1322 root 1.136 } elsif ($sym == CFClient::SDLK_LEFT) {
1323 root 1.68 --$self->{cursor} if $self->{cursor};
1324 root 1.136 } elsif ($sym == CFClient::SDLK_RIGHT) {
1325 root 1.68 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1326 root 1.136 } elsif ($sym == CFClient::SDLK_HOME) {
1327 root 1.76 $self->{cursor} = 0;
1328 root 1.136 } elsif ($sym == CFClient::SDLK_END) {
1329 root 1.76 $self->{cursor} = length $text;
1330 root 1.136 } elsif ($sym == 27) {
1331 elmex 1.101 $self->emit ('escape');
1332 elmex 1.31 } elsif ($uni) {
1333 root 1.68 substr $text, $self->{cursor}++, 0, chr $uni;
1334 elmex 1.31 }
1335 root 1.51
1336 root 1.68 $self->_set_text ($text);
1337     $self->update;
1338     }
1339    
1340     sub focus_in {
1341     my ($self) = @_;
1342    
1343     $self->{last_activity} = $::NOW;
1344    
1345     $self->SUPER::focus_in;
1346 elmex 1.31 }
1347    
1348 root 1.51 sub button_down {
1349 root 1.68 my ($self, $ev, $x, $y) = @_;
1350    
1351     $self->SUPER::button_down ($ev, $x, $y);
1352    
1353     my $idx = $self->{layout}->xy_to_index ($x, $y);
1354    
1355     # byte-index to char-index
1356 root 1.76 my $text = $self->{text};
1357 root 1.68 utf8::encode $text;
1358     $self->{cursor} = length substr $text, 0, $idx;
1359 root 1.51
1360 root 1.68 $self->_set_text ($self->{text});
1361     $self->update;
1362 root 1.51 }
1363    
1364 root 1.58 sub mouse_motion {
1365     my ($self, $ev, $x, $y) = @_;
1366 root 1.68 # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
1367 root 1.58 }
1368    
1369 root 1.51 sub _draw {
1370     my ($self) = @_;
1371    
1372 root 1.68 local $self->{fg} = $self->{fg};
1373    
1374 root 1.51 if ($FOCUS == $self) {
1375 root 1.68 glColor @{$self->{active_bg}};
1376     $self->{fg} = $self->{active_fg};
1377 root 1.51 } else {
1378 root 1.68 glColor @{$self->{bg}};
1379 root 1.51 }
1380    
1381 root 1.76 glEnable GL_BLEND;
1382     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1383 root 1.51 glBegin GL_QUADS;
1384 root 1.68 glVertex 0 , 0;
1385     glVertex 0 , $self->{h};
1386     glVertex $self->{w}, $self->{h};
1387     glVertex $self->{w}, 0;
1388 root 1.51 glEnd;
1389 root 1.76 glDisable GL_BLEND;
1390 root 1.51
1391     $self->SUPER::_draw;
1392 root 1.68
1393     #TODO: force update every cursor change :(
1394     if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1395 root 1.121
1396     unless (exists $self->{cur_h}) {
1397     my $text = substr $self->{text}, 0, $self->{cursor};
1398     utf8::encode $text;
1399    
1400     @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1401     }
1402    
1403 root 1.68 glColor @{$self->{fg}};
1404     glBegin GL_LINES;
1405 root 1.122 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1406     glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1407 root 1.68 glEnd;
1408     }
1409     }
1410    
1411 root 1.121 package CFClient::UI::Entry;
1412 elmex 1.99
1413 root 1.121 our @ISA = CFClient::UI::EntryBase::;
1414 elmex 1.99
1415 root 1.138 use CFClient::OpenGL;
1416 elmex 1.99
1417     sub key_down {
1418     my ($self, $ev) = @_;
1419    
1420 root 1.137 my $sym = $ev->{sym};
1421 elmex 1.99
1422 root 1.136 if ($sym == 13) {
1423 elmex 1.99 $self->emit (activate => $self->get_text);
1424     $self->update;
1425    
1426     } else {
1427     $self->SUPER::key_down ($ev);
1428     }
1429    
1430     }
1431    
1432 root 1.68 #############################################################################
1433    
1434 root 1.79 package CFClient::UI::Button;
1435    
1436     our @ISA = CFClient::UI::Label::;
1437    
1438 root 1.138 use CFClient::OpenGL;
1439 root 1.79
1440 elmex 1.85 my @tex =
1441 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1442 elmex 1.85 qw(b1_button_active.png);
1443    
1444 root 1.79 sub new {
1445     my $class = shift;
1446    
1447     $class->SUPER::new (
1448     padding => 4,
1449     fg => [1, 1, 1],
1450     bg => [1, 1, 1, 0.2],
1451 root 1.123 active_fg => [0, 0, 1],
1452 root 1.97 can_hover => 1,
1453 root 1.126 align => 0,
1454     valign => 0,
1455 elmex 1.150 can_events => 1,
1456 root 1.79 @_
1457     )
1458     }
1459    
1460     sub button_up {
1461     my ($self, $ev, $x, $y) = @_;
1462    
1463     if ($x >= 0 && $x < $self->{w}
1464     && $y >= 0 && $y < $self->{h}) {
1465     $self->emit ("activate");
1466     }
1467     }
1468    
1469     sub _draw {
1470     my ($self) = @_;
1471    
1472     local $self->{fg} = $self->{fg};
1473    
1474     if ($GRAB == $self) {
1475     $self->{fg} = $self->{active_fg};
1476     }
1477    
1478 root 1.119 glEnable GL_BLEND;
1479     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1480     glEnable GL_TEXTURE_2D;
1481 elmex 1.85 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1482 root 1.119 glColor 0, 0, 0, 1;
1483 elmex 1.85
1484 root 1.119 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1485 elmex 1.85
1486     glDisable GL_TEXTURE_2D;
1487 root 1.79 glDisable GL_BLEND;
1488    
1489     $self->SUPER::_draw;
1490     }
1491    
1492     #############################################################################
1493    
1494 root 1.86 package CFClient::UI::CheckBox;
1495    
1496     our @ISA = CFClient::UI::DrawBG::;
1497    
1498 elmex 1.102 my @tex =
1499 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1500 elmex 1.102 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1501    
1502 root 1.138 use CFClient::OpenGL;
1503 root 1.86
1504     sub new {
1505     my $class = shift;
1506    
1507     $class->SUPER::new (
1508 root 1.87 padding => 2,
1509 root 1.86 fg => [1, 1, 1],
1510     active_fg => [1, 1, 0],
1511     state => 0,
1512 root 1.97 can_hover => 1,
1513 root 1.86 @_
1514     )
1515     }
1516    
1517 root 1.87 sub size_request {
1518     my ($self) = @_;
1519    
1520     ($self->{padding} * 2 + 6) x 2
1521     }
1522    
1523 root 1.86 sub button_down {
1524     my ($self, $ev, $x, $y) = @_;
1525    
1526     if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1527     && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1528     $self->{state} = !$self->{state};
1529 root 1.87 $self->emit (changed => $self->{state});
1530 root 1.86 }
1531     }
1532    
1533     sub _draw {
1534     my ($self) = @_;
1535    
1536 root 1.87 $self->SUPER::_draw;
1537 root 1.86
1538 root 1.87 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1539 root 1.86
1540 root 1.87 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1541 elmex 1.102
1542 root 1.87 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1543 root 1.86
1544 elmex 1.102 glEnable GL_BLEND;
1545     glEnable GL_TEXTURE_2D;
1546     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1547 root 1.86
1548 elmex 1.102 my $tex = $self->{state} ? $tex[1] : $tex[0];
1549    
1550     $tex->draw_quad (0, 0, $s, $s);
1551    
1552     glDisable GL_TEXTURE_2D;
1553     glDisable GL_BLEND;
1554 root 1.86 }
1555    
1556     #############################################################################
1557    
1558 elmex 1.145 package CFClient::UI::Image;
1559    
1560     our @ISA = CFClient::UI::Base::;
1561    
1562     use CFClient::OpenGL;
1563     use Carp qw/confess/;
1564    
1565     our %loaded_images;
1566    
1567     sub new {
1568     my $class = shift;
1569    
1570 elmex 1.150 my $self = $class->SUPER::new (can_events => 0, @_);
1571 elmex 1.145
1572     $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1573    
1574     $loaded_images{$self->{image}} ||=
1575     new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1576    
1577     my $tex = $self->{tex} = $loaded_images{$self->{image}};
1578    
1579 root 1.147 Scalar::Util::weaken $loaded_images{$self->{image}};
1580    
1581 elmex 1.145 $self->{aspect} = $tex->{w} / $tex->{h};
1582    
1583     $self
1584     }
1585    
1586     sub size_request {
1587     my ($self) = @_;
1588    
1589     ($self->{tex}->{w}, $self->{tex}->{h})
1590     }
1591    
1592     sub _draw {
1593     my ($self) = @_;
1594    
1595     my $tex = $self->{tex};
1596    
1597     my ($w, $h) = ($self->{w}, $self->{h});
1598    
1599     if ($self->{rot90}) {
1600     glRotate 90, 0, 0, 1;
1601     glTranslate 0, -$self->{w}, 0;
1602    
1603     ($w, $h) = ($h, $w);
1604     }
1605    
1606     glEnable GL_BLEND;
1607     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1608     glEnable GL_TEXTURE_2D;
1609     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1610    
1611     $tex->draw_quad (0, 0, $w, $h);
1612    
1613     glDisable GL_BLEND;
1614     glDisable GL_TEXTURE_2D;
1615     }
1616    
1617     #############################################################################
1618    
1619 elmex 1.124 package CFClient::UI::VGauge;
1620    
1621     our @ISA = CFClient::UI::Base::;
1622    
1623 root 1.158 use List::Util qw(min max);
1624    
1625 root 1.138 use CFClient::OpenGL;
1626 elmex 1.124
1627     my %tex = (
1628     food => [
1629 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1630 elmex 1.124 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1631     ],
1632     grace => [
1633 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1634 root 1.158 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1635 elmex 1.124 ],
1636     hp => [
1637 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1638 elmex 1.124 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1639     ],
1640     mana => [
1641 root 1.144 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1642 root 1.158 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1643 elmex 1.124 ],
1644     );
1645    
1646     # eg. VGauge->new (gauge => 'food'), default gauge: food
1647     sub new {
1648     my $class = shift;
1649    
1650 root 1.140 my $self = $class->SUPER::new (
1651 root 1.141 type => 'food',
1652 root 1.140 @_
1653     );
1654    
1655 root 1.141 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1656 elmex 1.124
1657     $self
1658     }
1659    
1660     sub size_request {
1661     my ($self) = @_;
1662    
1663 root 1.143 #my $tex = $tex{$self->{type}}[0];
1664     #@$tex{qw(w h)}
1665     (0, 0)
1666 elmex 1.124 }
1667    
1668     sub set_max {
1669     my ($self, $max) = @_;
1670 root 1.127
1671 elmex 1.124 $self->{max_val} = $max;
1672     }
1673    
1674     sub set_value {
1675     my ($self, $val, $max) = @_;
1676    
1677     $self->set_max ($max)
1678     if defined $max;
1679    
1680     $max = $self->{max_val};
1681     $self->{val} = $val;
1682    
1683     $self->update;
1684     }
1685    
1686     sub _draw {
1687     my ($self) = @_;
1688    
1689 root 1.141 my $tex = $tex{$self->{type}};
1690 root 1.158 my ($t1, $t2, $t3) = @$tex;
1691 elmex 1.124
1692     my ($w, $h) = ($self->{w}, $self->{h});
1693    
1694 elmex 1.142 if ($self->{vertical}) {
1695     glRotate 90, 0, 0, 1;
1696     glTranslate 0, -$self->{w}, 0;
1697    
1698     ($w, $h) = ($h, $w);
1699     }
1700    
1701 elmex 1.124 my $ycut = $self->{val} / ($self->{max_val} || 1);
1702    
1703 root 1.158 my $ycut1 = max 0, min 1, $ycut;
1704     my $ycut2 = max 0, min 1, $ycut - 1;
1705    
1706     my $h1 = $self->{h} * (1 - $ycut1);
1707     my $h2 = $self->{h} * (1 - $ycut2);
1708 elmex 1.124
1709     glEnable GL_BLEND;
1710     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1711     glEnable GL_TEXTURE_2D;
1712     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1713    
1714 root 1.131 glBindTexture GL_TEXTURE_2D, $t1->{name};
1715     glBegin GL_QUADS;
1716 root 1.158 glTexCoord 0 , 0; glVertex 0 , 0;
1717     glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1718     glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1719     glTexCoord $t1->{s}, 0; glVertex $w, 0;
1720 root 1.131 glEnd;
1721 elmex 1.124
1722 root 1.158 my $ycut1 = List::Util::min 1, $ycut;
1723 root 1.131 glBindTexture GL_TEXTURE_2D, $t2->{name};
1724     glBegin GL_QUADS;
1725 root 1.158 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1726     glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1727     glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1728     glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1729 root 1.131 glEnd;
1730 elmex 1.124
1731 root 1.158 if ($t3) {
1732     glBindTexture GL_TEXTURE_2D, $t3->{name};
1733     glBegin GL_QUADS;
1734     glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1735     glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1736     glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1737     glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1738     glEnd;
1739     }
1740    
1741 elmex 1.124 glDisable GL_BLEND;
1742     glDisable GL_TEXTURE_2D;
1743     }
1744    
1745     #############################################################################
1746    
1747 root 1.141 package CFClient::UI::Gauge;
1748    
1749     our @ISA = CFClient::UI::VBox::;
1750    
1751     sub new {
1752 root 1.151 my ($class, %arg) = @_;
1753 root 1.141
1754     my $self = $class->SUPER::new (
1755 root 1.151 tooltip => $arg{type},
1756 root 1.161 can_events => 1,
1757     can_hover => 1,
1758 root 1.151 %arg,
1759 root 1.141 );
1760    
1761 root 1.161 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1762     $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1763     $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1764 root 1.141
1765     $self
1766     }
1767    
1768 elmex 1.146 sub set_fontsize {
1769     my ($self, $fsize) = @_;
1770    
1771     $self->{value}->set_fontsize ($fsize);
1772     $self->{max} ->set_fontsize ($fsize);
1773     }
1774    
1775 root 1.141 sub set_value {
1776     my ($self, $val, $max) = @_;
1777    
1778     $self->set_max ($max)
1779     if defined $max;
1780    
1781     $self->{gauge}->set_value ($val, $max);
1782     $self->{value}->set_text ($val);
1783     }
1784    
1785     sub set_max {
1786     my ($self, $max) = @_;
1787    
1788     $self->{gauge}->set_max ($max);
1789     $self->{max}->set_text ($max);
1790     }
1791    
1792     #############################################################################
1793    
1794 root 1.73 package CFClient::UI::Slider;
1795 root 1.68
1796     use strict;
1797    
1798 root 1.138 use CFClient::OpenGL;
1799 root 1.68
1800 root 1.73 our @ISA = CFClient::UI::DrawBG::;
1801 root 1.68
1802 elmex 1.99 my @tex =
1803     map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1804     qw(s1_slider.png s1_slider_bg.png);
1805    
1806 root 1.68 sub new {
1807     my $class = shift;
1808    
1809     # range [value, low, high, page]
1810    
1811 root 1.97 # TODO: 0-width page
1812     # TODO: req_w/h are wrong with vertical
1813     # TODO: calculations are off
1814 root 1.76 my $self = $class->SUPER::new (
1815 root 1.68 fg => [1, 1, 1],
1816     active_fg => [0, 0, 0],
1817     range => [0, 0, 100, 10],
1818 root 1.134 req_w => 20,
1819     req_h => 20,
1820 root 1.76 vertical => 0,
1821 root 1.97 can_hover => 1,
1822 elmex 1.103 inner_pad => 5,
1823 root 1.68 @_
1824 root 1.76 );
1825    
1826     $self
1827     }
1828    
1829     sub size_request {
1830     my ($self) = @_;
1831    
1832     my $w = $self->{req_w};
1833     my $h = $self->{req_h};
1834    
1835     $self->{vertical} ? ($h, $w) : ($w, $h)
1836 root 1.68 }
1837    
1838 root 1.69 sub button_down {
1839     my ($self, $ev, $x, $y) = @_;
1840    
1841     $self->SUPER::button_down ($ev, $x, $y);
1842     $self->mouse_motion ($ev, $x, $y);
1843     }
1844    
1845     sub mouse_motion {
1846     my ($self, $ev, $x, $y) = @_;
1847    
1848     if ($GRAB == $self) {
1849     my ($value, $lo, $hi, $page) = @{$self->{range}};
1850    
1851 root 1.71 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1852    
1853 elmex 1.103 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1854     my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1855    
1856     $x -= $inner_pad_px; # substract the padding
1857     $x = $x * ($hi - $lo) / $inner_w + $lo;
1858 root 1.69 $x = $lo if $x < $lo;
1859     $x = $hi - $page if $x > $hi - $page;
1860     $self->{range}[0] = $x;
1861    
1862 root 1.72 $self->emit (changed => $x);
1863 root 1.69 $self->update;
1864     }
1865     }
1866    
1867 elmex 1.103 # the inner_* stuff is for generating a padding for the slider handle,
1868     # so that the handle doesn't leave the texture. This calculation isn't 100%
1869     # correct propably, but it does the job for now
1870     sub _calc_inner_pad_px {
1871     my ($self, $w) = @_;
1872     ($w / 100) * $self->{inner_pad} # % to pixels
1873     }
1874    
1875 root 1.68 sub _draw {
1876     my ($self) = @_;
1877    
1878     $self->SUPER::_draw ();
1879    
1880     my ($w, $h) = @$self{qw(w h)};
1881    
1882     if ($self->{vertical}) {
1883     # draw a vertical slider like a rotated horizontal slider
1884    
1885     glRotate 90, 0, 0, 1;
1886 root 1.71 glTranslate 0, -$self->{w}, 0;
1887 root 1.68
1888     ($w, $h) = ($h, $w);
1889     }
1890    
1891     my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1892     my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1893    
1894 root 1.69 my ($value, $lo, $hi, $page) = @{$self->{range}};
1895    
1896 root 1.107 $hi = $value + 1 if $lo == $hi;
1897    
1898 elmex 1.103 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1899     my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1900 elmex 1.99
1901     $page = int $page * $inner_w / ($hi - $lo);
1902     $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1903 root 1.69
1904     $w -= $page;
1905     $page &= ~1;
1906     glTranslate $page * 0.5, 0, 0;
1907 root 1.80 $page ||= 2;
1908 root 1.69
1909 elmex 1.99 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1910     my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1911    
1912     glEnable GL_BLEND;
1913     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1914     glEnable GL_TEXTURE_2D;
1915     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1916    
1917     # draw background
1918     $tex[1]->draw_quad (0, 0, $w, $h);
1919 root 1.69
1920 elmex 1.99 # draw handle
1921     $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1922 root 1.69
1923 elmex 1.99 glDisable GL_BLEND;
1924     glDisable GL_TEXTURE_2D;
1925 root 1.51 }
1926    
1927 root 1.39 #############################################################################
1928    
1929 root 1.97 package CFClient::UI::TextView;
1930    
1931     our @ISA = CFClient::UI::HBox::;
1932    
1933 root 1.138 use CFClient::OpenGL;
1934 root 1.97
1935     sub new {
1936     my $class = shift;
1937    
1938     my $self = $class->SUPER::new (
1939 root 1.134 fontsize => 1,
1940 root 1.157 #font => default_font
1941 root 1.105 @_,
1942    
1943     layout => (new CFClient::Layout),
1944 root 1.97 par => [],
1945 root 1.105 height => 0,
1946 root 1.97 children => [
1947     (new CFClient::UI::Empty expand => 1),
1948     (new CFClient::UI::Slider vertical => 1),
1949     ],
1950     );
1951    
1952 root 1.107 $self->{children}[1]->connect (changed => sub {
1953     $self->update;
1954     });
1955    
1956 root 1.97 $self
1957     }
1958    
1959 root 1.107 sub set_fontsize {
1960     my ($self, $fontsize) = @_;
1961    
1962     $self->{fontsize} = $fontsize;
1963     $self->reflow;
1964     }
1965    
1966 root 1.105 sub text_height {
1967     my ($self, $text) = @_;
1968    
1969     my $layout = $self->{layout};
1970    
1971 root 1.134 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1972 root 1.105 $layout->set_width ($self->{w});
1973     $layout->set_text ($text);
1974    
1975     ($layout->size)[1]
1976     }
1977    
1978     sub reflow {
1979     my ($self) = @_;
1980    
1981 root 1.107 $self->{need_reflow}++;
1982     $self->update;
1983 root 1.105 }
1984    
1985     sub size_allocate {
1986 root 1.128 my ($self, $w, $h) = @_;
1987 root 1.105
1988 root 1.134 $self->SUPER::size_allocate ($w, $h);
1989    
1990 root 1.157 $self->{layout}->set_font ($self->{font}) if $self->{font};
1991 root 1.134 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1992     $self->{layout}->set_width ($self->{children}[0]{w});
1993 root 1.105
1994     $self->reflow;
1995     }
1996    
1997 root 1.97 sub add_paragraph {
1998     my ($self, $color, $text) = @_;
1999    
2000 root 1.105 #TODO: intelligently "reformat" paragraph
2001    
2002     my $height = $self->text_height ($text);
2003    
2004     $self->{height} += $height;
2005    
2006     push @{$self->{par}}, [$height, $color, $text];
2007    
2008     $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
2009 root 1.97 $self->{children}[1]->update;
2010     }
2011    
2012 root 1.105 sub update {
2013 root 1.97 my ($self) = @_;
2014    
2015 root 1.105 $self->SUPER::update;
2016    
2017     return unless $self->{h} > 0;
2018    
2019 root 1.107 delete $self->{texture};
2020    
2021 root 1.113 $ROOT->on_refresh ($self, sub {
2022 root 1.107 if (delete $self->{need_reflow}) {
2023     my $height = 0;
2024    
2025     $height += $_->[0] = $self->text_height ($_->[2])
2026     for @{$self->{par}};
2027    
2028     $self->{height} = $height;
2029    
2030     $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2031    
2032     delete $self->{texture};
2033     }
2034    
2035     $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
2036     glClearColor 0, 0, 0, 1;
2037     glClear GL_COLOR_BUFFER_BIT;
2038    
2039     glEnable GL_BLEND;
2040     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2041     glEnable GL_TEXTURE_2D;
2042     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2043 root 1.105
2044 root 1.107 my $top = int $self->{children}[1]{range}[0];
2045 root 1.105
2046 root 1.107 my $y0 = $top;
2047     my $y1 = $top + $self->{h};
2048 root 1.105
2049 root 1.107 my $y = 0;
2050 root 1.97
2051 root 1.107 my $layout = $self->{layout};
2052 root 1.97
2053 root 1.157 $layout->set_font ($self->{font}) if $self->{font};
2054    
2055 root 1.107 for my $par (@{$self->{par}}) {
2056     my $h = $par->[0];
2057 root 1.97
2058 root 1.107 if ($y0 < $y + $h && $y < $y1) {
2059     $layout->set_text ($par->[2]);
2060 root 1.105
2061 root 1.107 glColor @{ $par->[1] };
2062     my ($W, $H) = $layout->size;
2063     CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
2064     }
2065    
2066     $y += $h;
2067 root 1.105 }
2068    
2069 root 1.107 glDisable GL_TEXTURE_2D;
2070     glDisable GL_BLEND;
2071     };
2072     });
2073 root 1.105 }
2074 root 1.97
2075 root 1.105 sub _draw {
2076     my ($self) = @_;
2077 root 1.97
2078 root 1.105 if ($self->{texture}) {
2079     glEnable GL_TEXTURE_2D;
2080     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2081     $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
2082     glDisable GL_TEXTURE_2D;
2083 root 1.97 }
2084    
2085 root 1.106 $self->{children}[1]->draw;
2086    
2087 root 1.97 }
2088    
2089     #############################################################################
2090    
2091 root 1.73 package CFClient::UI::Animator;
2092 root 1.35
2093 root 1.138 use CFClient::OpenGL;
2094 root 1.35
2095 root 1.73 our @ISA = CFClient::UI::Bin::;
2096 root 1.35
2097     sub moveto {
2098     my ($self, $x, $y) = @_;
2099    
2100     $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
2101 root 1.56 $self->{speed} = 0.001;
2102 root 1.35 $self->{time} = 1;
2103    
2104     ::animation_start $self;
2105     }
2106    
2107     sub animate {
2108     my ($self, $interval) = @_;
2109    
2110     $self->{time} -= $interval * $self->{speed};
2111     if ($self->{time} <= 0) {
2112     $self->{time} = 0;
2113     ::animation_stop $self;
2114     }
2115    
2116     my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
2117    
2118     $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
2119     $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
2120     }
2121    
2122     sub _draw {
2123     my ($self) = @_;
2124    
2125     glPushMatrix;
2126 root 1.51 glRotate $self->{time} * 1000, 0, 1, 0;
2127 root 1.38 $self->{children}[0]->draw;
2128 root 1.35 glPopMatrix;
2129     }
2130    
2131 root 1.51 #############################################################################
2132    
2133 root 1.96 package CFClient::UI::Flopper;
2134    
2135     our @ISA = CFClient::UI::Button::;
2136    
2137     sub new {
2138     my $class = shift;
2139    
2140     my $self = $class->SUPER::new (
2141     state => 0,
2142     connect_activate => \&toggle_flopper,
2143 elmex 1.150 can_events => 1,
2144 root 1.96 @_
2145     );
2146    
2147     if ($self->{state}) {
2148     $self->{state} = 0;
2149     $self->toggle_flopper;
2150     }
2151    
2152     $self
2153     }
2154    
2155     sub toggle_flopper {
2156     my ($self) = @_;
2157    
2158 root 1.97 # TODO: use animation
2159 root 1.96 if ($self->{state} = !$self->{state}) {
2160 root 1.113 $CFClient::UI::ROOT->add ($self->{other});
2161     $self->{other}->move ($self->coord2global (0, $self->{h}));
2162 root 1.114 $self->emit ("open");
2163 root 1.96 } else {
2164 root 1.113 $CFClient::UI::ROOT->remove ($self->{other});
2165 root 1.114 $self->emit ("close");
2166 root 1.96 }
2167 root 1.114
2168     $self->emit (changed => $self->{state});
2169 root 1.96 }
2170    
2171     #############################################################################
2172    
2173 root 1.153 package CFClient::UI::Tooltip;
2174    
2175     our @ISA = CFClient::UI::Bin::;
2176    
2177     use CFClient::OpenGL;
2178    
2179     sub new {
2180     my $class = shift;
2181    
2182     $class->SUPER::new (
2183     @_,
2184     can_events => 0,
2185     )
2186     }
2187    
2188     sub set_text {
2189     my ($self, $text) = @_;
2190    
2191 root 1.154 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2192 root 1.153 $self->{label}->set_text ($text);
2193     $self->add ($self->{label});
2194     }
2195    
2196     sub size_request {
2197     my ($self) = @_;
2198    
2199 root 1.160 $self->child->set_max_size ($::WIDTH * 0.3);
2200 root 1.157
2201 root 1.153 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2202    
2203 root 1.154 ($w + 4, $h + 4)
2204     }
2205    
2206     sub _draw {
2207     my ($self) = @_;
2208    
2209     glPushMatrix;
2210     glTranslate 0.375, 0.375;
2211    
2212     my ($w, $h) = @$self{qw(w h)};
2213    
2214     glColor 1, 0.8, 0.4;
2215     glBegin GL_QUADS;
2216     glVertex 0 , 0;
2217     glVertex 0 , $h;
2218     glVertex $w, $h;
2219     glVertex $w, 0;
2220     glEnd;
2221    
2222     glColor 0, 0, 0;
2223     glBegin GL_LINE_LOOP;
2224     glVertex 0 , 0;
2225     glVertex 0 , $h;
2226     glVertex $w, $h;
2227     glVertex $w, 0;
2228     glEnd;
2229    
2230     glPopMatrix;
2231    
2232     glTranslate 2, 2;
2233     $self->SUPER::_draw;
2234 root 1.153 }
2235    
2236     #############################################################################
2237    
2238 root 1.113 package CFClient::UI::Root;
2239 root 1.51
2240 root 1.73 our @ISA = CFClient::UI::Container::;
2241 root 1.51
2242 root 1.138 use CFClient::OpenGL;
2243 root 1.107
2244 root 1.125 sub check_size {
2245     my ($self) = @_;
2246    
2247 root 1.149 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2248 root 1.125 }
2249    
2250 root 1.51 sub size_request {
2251     ($::WIDTH, $::HEIGHT)
2252     }
2253    
2254 root 1.128 sub configure {
2255 root 1.75 my ($self, $x, $y, $w, $h) = @_;
2256 root 1.51
2257 root 1.128 $self->SUPER::configure ($x, $y, $w, $h);
2258 root 1.51
2259 root 1.149 for my $child (@{$self->{children}}) {
2260     my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2261    
2262     $X = List::Util::max 0, List::Util::min $w - $W, $X;
2263     $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2264     $child->configure ($X, $Y, $W,$H);
2265     }
2266 root 1.51 }
2267    
2268 root 1.113 sub _topleft {
2269 root 1.58 my ($self, $x, $y) = @_;
2270    
2271     ($x, $y)
2272     }
2273    
2274 root 1.51 sub update {
2275     my ($self) = @_;
2276    
2277 root 1.125 $self->check_size;
2278 root 1.51 ::refresh ();
2279     }
2280    
2281     sub add {
2282 root 1.128 my ($self, $child) = @_;
2283 root 1.51
2284 elmex 1.146 # integerize window positions
2285     $child->{x} = int $child->{x};
2286     $child->{y} = int $child->{y};
2287    
2288 root 1.128 $self->SUPER::add ($child);
2289 root 1.51 }
2290    
2291 root 1.107 sub on_refresh {
2292     my ($self, $id, $cb) = @_;
2293    
2294     $self->{refresh_hook}{$id} = $cb;
2295     }
2296    
2297 root 1.51 sub draw {
2298     my ($self) = @_;
2299    
2300 root 1.107 while (my $rcb = delete $self->{refresh_hook}) {
2301     $_->() for values %$rcb;
2302     }
2303    
2304     glViewport 0, 0, $::WIDTH, $::HEIGHT;
2305     glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2306     glClear GL_COLOR_BUFFER_BIT;
2307    
2308     glMatrixMode GL_PROJECTION;
2309     glLoadIdentity;
2310     glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2311     glMatrixMode GL_MODELVIEW;
2312     glLoadIdentity;
2313    
2314 root 1.51 $self->_draw;
2315     }
2316    
2317     #############################################################################
2318    
2319 root 1.73 package CFClient::UI;
2320 root 1.51
2321 root 1.113 $ROOT = new CFClient::UI::Root;
2322 root 1.153 $TOOLTIP = new CFClient::UI::Tooltip;
2323 root 1.51
2324     1
2325 root 1.5