ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.172
Committed: Tue Apr 25 09:52:04 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.171: +15 -2 lines
Log Message:
*** empty log message ***

File Contents

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