ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.150
Committed: Sun Apr 23 00:08:29 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.149: +24 -4 lines
Log Message:
gauges scale differntly now, the range is displayed properly and implemented can_events

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