ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.125
Committed: Mon Apr 17 20:52:15 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.124: +62 -7 lines
Log Message:
implement check_resize and use it, first half of more intelligent allocation support

File Contents

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