ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.108
Committed: Fri Apr 14 23:32:29 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.107: +42 -2 lines
Log Message:
middle mouse button drags map center

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