ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.155
Committed: Sun Apr 23 02:22:54 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.154: +5 -0 lines
Log Message:
*** empty log message ***

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