ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.162
Committed: Mon Apr 24 06:05:35 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.161: +71 -4 lines
Log Message:
misc improvements

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