ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.112
Committed: Sat Apr 15 12:29:46 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.111: +21 -4 lines
Log Message:
fix widget coordinate rounding problem

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