ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.140
Committed: Thu Apr 20 04:20:52 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.139: +11 -1 lines
Log Message:
support widget aspect ratio

File Contents

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