ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.157
Committed: Sun Apr 23 21:47:32 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.156: +18 -4 lines
Log Message:
support multiple fonts, add dejavu sans and sans mono fonts (bitstream++)

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