ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.158
Committed: Mon Apr 24 02:41:48 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.157: +51 -43 lines
Log Message:
implmenet map scale - now fully playability at 640x480

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