ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.3 by elmex, Fri Apr 7 18:16:51 2006 UTC vs.
Revision 1.77 by root, Tue Apr 11 22:14:13 2006 UTC

1package Client::Widget; 1package CFClient::UI;
2
2use strict; 3use strict;
3 4
4our $FOCUS; # the widget with current focus 5use Scalar::Util ();
5our %ACTIVE_WIDGETS; 6use List::Util ();
7
8use CFClient;
9
10our ($FOCUS, $HOVER, $GRAB); # various widgets
11
12our $TOPLEVEL;
13our $BUTTON_STATE;
6 14
7# class methods for events 15# class methods for events
8sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 16sub feed_sdl_key_down_event {
9sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } 17 $FOCUS->key_down ($_[0]) if $FOCUS;
10sub feed_sdl_button_down_event { $FOCUS->button_down ($_[0]) if $FOCUS } 18}
11sub feed_sdl_button_up_event { $FOCUS->button_up ($_[0]) if $FOCUS } 19
20sub feed_sdl_key_up_event {
21 $FOCUS->key_up ($_[0]) if $FOCUS;
22}
23
24sub 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 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
38}
39
40sub 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 $GRAB->button_up ($ev, $GRAB->translate ($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
57sub 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 $hover->update if $hover;
67 $HOVER->update if $HOVER;
68 }
69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
71}
72
73#############################################################################
74
75package CFClient::UI::Base;
76
77use strict;
78
79use SDL::OpenGL;
12 80
13sub new { 81sub new {
14 my $class = shift; 82 my $class = shift;
15 my $self = { @_ };
16 bless $self, $class;
17 return $self;
18}
19 83
20sub activate { 84 bless {
21 $ACTIVE_WIDGETS{$_[0]} = $_[0]; 85 x => 0,
86 y => 0,
87 z => 0,
88 w => -1,
89 h => -1,
90 @_
91 }, $class
22} 92}
23sub deactivate { 93
24 delete $ACTIVE_WIDGETS{$_[0]}; 94sub move {
95 my ($self, $x, $y, $z) = @_;
96 $self->{x} = $x;
97 $self->{y} = $y;
98 $self->{z} = $z if defined $z;
99}
100
101sub needs_redraw {
102 0
103}
104
105sub size_request {
106 require Carp;
107 Carp::confess "size_request is abtract";
108}
109
110sub _size_allocate {
111 my ($self, $x, $y, $w, $h) = @_;
112
113 $self->{x} = $x;
114 $self->{y} = $y;
115
116 return unless $self->{w} != $w || $self->{h} != $h;
117
118 $self->{w} = $w;
119 $self->{h} = $h;
120
121 1
122}
123
124sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_;
126
127 $self->_size_allocate ($x, $y, $w, $h);
128}
129
130# translate global koordinates to local coordinate system
131sub translate {
132 my ($self, $x, $y) = @_;
133
134 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
25} 135}
26 136
27sub focus_in { 137sub focus_in {
28 my ($widget) = @_; 138 my ($self) = @_;
29 $FOCUS = $widget; 139
140 return if $FOCUS == $self;
141
142 my $focus = $FOCUS; $FOCUS = $self;
143 $focus->update if $focus;
144 $FOCUS->update;
30} 145}
146
31sub focus_out { 147sub focus_out {
148 my ($self) = @_;
149
150 return unless $FOCUS == $self;
151
152 my $focus = $FOCUS; undef $FOCUS;
153 $focus->update if $focus; #?
154}
155
156sub mouse_motion { }
157sub button_up { }
158sub key_down { }
159sub key_up { }
160
161sub button_down {
162 my ($self, $ev, $x, $y) = @_;
163
164 $self->focus_in;
165}
166
167sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
168sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
169sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
170sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
171sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
172
173sub draw {
174 my ($self) = @_;
175
176 return unless $self->{h} && $self->{w};
177
178 glPushMatrix;
179 glTranslate $self->{x}, $self->{y}, 0;
180 $self->_draw;
181 glPopMatrix;
182
183 if ($self == $HOVER) {
184 my ($x, $y) = @$self{qw(x y)};
185
186 glColor 0, 0, 1, 0.2;
187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
189 glBegin GL_QUADS;
190 glVertex $x , $y;
191 glVertex $x + $self->{w}, $y;
192 glVertex $x + $self->{w}, $y + $self->{h};
193 glVertex $x , $y + $self->{h};
194 glEnd;
195 glDisable GL_BLEND;
196 }
197}
198
199sub _draw {
200 my ($self) = @_;
201
202 warn "no draw defined for $self\n";
203}
204
205sub bbox {
206 my ($self) = @_;
207 my ($w, $h) = $self->size_request;
208 (
209 $self->{x},
210 $self->{y},
211 $self->{x} = $w,
212 $self->{y} = $h
213 )
214}
215
216sub find_widget {
217 my ($self, $x, $y) = @_;
218
219 return $self
220 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
221 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
222
223 ()
224}
225
226sub del_parent { $_[0]->{parent} = undef }
227
228sub set_parent {
229 my ($self, $par) = @_;
230
231 $self->{parent} = $par;
232 Scalar::Util::weaken $self->{parent};
233}
234
235sub get_parent {
236 $_[0]->{parent}
237}
238
239sub update {
240 my ($self) = @_;
241
242 $self->{parent}->update
243 if $self->{parent};
244}
245
246sub connect {
247 my ($self, $signal, $cb) = @_;
248
249 push @{ $self->{cb}{$signal} }, $cb;
250}
251
252sub emit {
253 my ($self, $signal, @args) = @_;
254
255 $_->($self, @args)
256 for @{$self->{cb}{$signal} || []};
257}
258
259sub DESTROY {
260 my ($self) = @_;
261
262 #$self->deactivate;
263}
264
265#############################################################################
266
267package CFClient::UI::DrawBG;
268
269our @ISA = CFClient::UI::Base::;
270
271use strict;
272use SDL::OpenGL;
273
274sub new {
275 my $class = shift;
276
277 # range [value, low, high, page]
278
279 $class->SUPER::new (
280 bg => [0, 0, 0, 0.2],
281 active_bg => [1, 1, 1],
282 @_
283 )
284}
285
286sub _draw {
287 my ($self) = @_;
288
289 my ($w, $h) = @$self{qw(w h)};
290
291 glEnable GL_BLEND;
292 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
293 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
294
295 glBegin GL_QUADS;
296 glVertex 0 , 0;
297 glVertex 0 , $h;
298 glVertex $w, $h;
299 glVertex $w, 0;
300 glEnd;
301
302 glDisable GL_BLEND;
303}
304
305#############################################################################
306
307package CFClient::UI::Empty;
308
309our @ISA = CFClient::UI::Base::;
310
311sub size_request {
312 (0, 0)
313}
314
315sub draw { }
316
317#############################################################################
318
319package CFClient::UI::Container;
320
321our @ISA = CFClient::UI::Base::;
322
323sub new {
324 my ($class, %arg) = @_;
325
326 my $children = delete $arg{children} || [];
327
328 my $self = $class->SUPER::new (children => [], %arg);
329 $self->add ($_) for @$children;
330
331 $self
332}
333
334sub add {
335 my ($self, $chld) = @_;
336
337 $chld->set_parent ($self);
338
339 $self->{children} = [
340 sort { $a->{z} <=> $b->{z} }
341 @{$self->{children}}, $chld
342 ];
343
344 $self->{w} = $self->{h} = -1;
345 $self->update;
346}
347
348sub remove {
32 my ($widget) = @_; 349 my ($self, $widget) = @_;
350
351 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
352
353 $self->size_allocate (0, 0, $self->{w}, $self->{h});
33} 354}
355
356sub find_widget {
357 my ($self, $x, $y) = @_;
358
359 $x -= $self->{x};
360 $y -= $self->{y};
361
362 my $res;
363
364 for (reverse @{ $self->{children} }) {
365 $res = $_->find_widget ($x, $y)
366 and return $res;
367 }
368
369 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
370}
371
372sub _draw {
373 my ($self) = @_;
374
375 $_->draw for @{$self->{children}};
376}
377
378#############################################################################
379
380package CFClient::UI::Bin;
381
382our @ISA = CFClient::UI::Container::;
383
384sub new {
385 my ($class, %arg) = @_;
386
387 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
388
389 $class->SUPER::new (children => [$child], %arg)
390}
391
392sub add {
393 my ($self, $widget) = @_;
394
395 $self->{children} = [];
396
397 $self->SUPER::add ($widget);
398}
399
400sub remove {
401 my ($self, $widget) = @_;
402
403 $self->SUPER::remove ($widget);
404
405 $self->{children} = [new CFClient::UI::Empty]
406 unless @{$self->{children}};
407}
408
409sub child { $_[0]->{children}[0] }
410
411sub size_request {
412 $_[0]{children}[0]->size_request
413}
414
415sub size_allocate {
416 my ($self, $x, $y, $w, $h) = @_;
417
418 $self->_size_allocate ($x, $y, $w, $h) or return;
419
420 $self->{children}[0]->size_allocate (0, 0, $w, $h);
421}
422
423#############################################################################
424
425package CFClient::UI::Window;
426
427our @ISA = CFClient::UI::Bin::;
428
429use SDL::OpenGL;
430
431sub new {
432 my ($class, %arg) = @_;
433
434 my $self = $class->SUPER::new (%arg);
435}
436
437sub update {
438 my ($self) = @_;
439
440 # we want to do this delayed...
441 $self->render_chld;
442 $self->SUPER::update;
443}
444
445sub render_chld {
446 my ($self) = @_;
447
448 $self->{texture} =
449 CFClient::Texture->new_from_opengl (
450 $self->{w}, $self->{h}, sub { $self->child->draw }
451 );
452}
453
454sub size_allocate {
455 my ($self, $x, $y, $w, $h) = @_;
456
457 $self->_size_allocate ($x, $y, $w, $h) or return;
458
459 $self->child->size_allocate (0, 0, $w, $h);
460
461 $self->render_chld;
462}
463
464sub _draw {
465 my ($self) = @_;
466
467 my ($w, $h) = ($self->w, $self->h);
468
469 my $tex = $self->{texture}
470 or return;
471
472 glEnable GL_BLEND;
473 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
474 glEnable GL_TEXTURE_2D;
475 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
476
477 $tex->draw_quad (0, 0, $w, $h);
478
479 glDisable GL_BLEND;
480 glDisable GL_TEXTURE_2D;
481}
482
483#############################################################################
484
485package CFClient::UI::Frame;
486
487our @ISA = CFClient::UI::Bin::;
488
489use SDL::OpenGL;
490
491sub size_request {
492 my ($self) = @_;
493 my $chld = $self->child
494 or return (0, 0);
495
496 $chld->move (2, 2);
497
498 map { $_ + 4 } $chld->size_request;
499}
500
501sub size_allocate {
502 my ($self, $x, $y, $w, $h) = @_;
503
504 $self->_size_allocate ($x, $y, $w, $h) or return;
505
506 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
507}
508
509sub _draw {
510 my ($self) = @_;
511
512 my $chld = $self->child;
513
514 my ($w, $h) = $chld->size_request;
515
516 glBegin GL_QUADS;
517 glColor 0, 0, 0;
518 glVertex 0 , 0;
519 glVertex 0 , $h + 4;
520 glVertex $w + 4 , $h + 4;
521 glVertex $w + 4 , 0;
522 glEnd;
523
524 $chld->draw;
525}
526
527#############################################################################
528
529package CFClient::UI::FancyFrame;
530
531our @ISA = CFClient::UI::Bin::;
532
533use SDL::OpenGL;
534
535my @tex =
536 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
537 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
538
539sub size_request {
540 my ($self) = @_;
541
542 my ($w, $h) =
543 $self->{user_w} && $self->{user_h}
544 ? ($self->{user_w}, $self->{user_h})
545 : ($self->SUPER::size_request);
546
547 $h += $tex[1]->{h};
548 $h += $tex[4]->{h};
549 $w += $tex[2]->{w};
550 $w += $tex[3]->{w};
551
552 ($w, $h)
553}
554
555sub size_allocate {
556 my ($self, $x, $y, $w, $h) = @_;
557
558 $self->_size_allocate ($x, $y, $w, $h) or return;
559
560 $h -= $tex[1]{h};
561 $h -= $tex[4]{h};
562 $w -= $tex[2]{w};
563 $w -= $tex[3]{w};
564
565 $h = $h < 0 ? 0 : $h;
566 $w = $w < 0 ? 0 : $w;
567
568 my $child = $self->child;
569
570 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
571}
572
573sub button_down {
574 my ($self, $ev, $x, $y) = @_;
575
576 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
577 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
578
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
580 my ($bw, $bh) = ($self->{w}, $self->{h});
581
582 $self->{motion} = sub {
583 my ($ev, $x, $y) = @_;
584
585 ($x, $y) = ($ev->motion_x, $ev->motion_y);
586
587 $self->{user_w} = $bw + $x - $ox;
588 $self->{user_h} = $bh + $y - $oy;
589 $self->update;
590 };
591
592 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $tex[1]{h}) {
594
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
596 my ($bx, $by) = ($self->{x}, $self->{y});
597
598 $self->{motion} = sub {
599 my ($ev, $x, $y) = @_;
600
601 ($x, $y) = ($ev->motion_x, $ev->motion_y);
602
603 $self->move ($bx + $x - $ox, $by + $y - $oy);
604 $self->update;
605 };
606 }
607}
608
609sub button_up {
610 my ($self, $ev, $x, $y) = @_;
611
612 delete $self->{motion};
613}
614
615sub mouse_motion {
616 my ($self, $ev, $x, $y) = @_;
617
618 $self->{motion}->($ev, $x, $y) if $self->{motion};
619}
620
621sub _draw {
622 my ($self) = @_;
623
624 my ($w, $h) = ($self->{w}, $self->{h});
625 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
626
627 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
629 glEnable GL_TEXTURE_2D;
630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
631
632 my $top = $tex[1];
633 $top->draw_quad (0, 0, $w, $top->{h});
634
635 my $left = $tex[3];
636 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
637
638 my $right = $tex[2];
639 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
640
641 my $bottom = $tex[4];
642 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
643
644 my $bg = $tex[0];
645
646 glBindTexture GL_TEXTURE_2D, $bg->{name};
647 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
648 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
649 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
650
651 my $rep_x = $cw / $bg->{w};
652 my $rep_y = $ch / $bg->{h};
653
654 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
655
656 glDisable GL_TEXTURE_2D;
657 glDisable GL_BLEND;
658
659 $self->child->draw;
660
661}
662
663#############################################################################
664
665package CFClient::UI::Table;
666
667our @ISA = CFClient::UI::Base::;
668
669use List::Util qw(max sum);
670
671use SDL::OpenGL;
672
673sub add {
674 my ($self, $x, $y, $chld) = @_;
675
676 $self->{children}[$y][$x] = $chld;
677 $chld->set_parent ($self);
678
679 $self->{w} = $self->{h} = -1;
680 $self->update;
681}
682
683sub get_wh {
684 my ($self) = @_;
685
686 my (@w, @h);
687
688 for my $y (0 .. $#{$self->{children}}) {
689 my $row = $self->{children}[$y]
690 or next;
691
692 for my $x (0 .. $#$row) {
693 my $widget = $row->[$x]
694 or next;
695 my ($w, $h) = $widget->size_request;
696
697 $w[$x] = max $w[$x], $w;
698 $h[$y] = max $h[$y], $h;
699 }
700 }
701
702 (\@w, \@h)
703}
704
705sub size_request {
706 my ($self) = @_;
707
708 my ($ws, $hs) = $self->get_wh;
709
710 (
711 (List::Util::sum @$ws),
712 (List::Util::sum @$hs),
713 )
714}
715
716sub size_allocate {
717 my ($self, $x, $y, $w, $h) = @_;
718
719 $self->_size_allocate ($x, $y, $w, $h) or return;
720
721 my ($ws, $hs) = $self->get_wh;
722
723 my $req_w = List::Util::sum @$ws;
724 my $req_h = List::Util::sum @$hs;
725
726 # linearly scale sizes
727 $_ *= $w / $req_w for @$ws;
728 $_ *= $h / $req_h for @$hs;
729
730 my $y;
731
732 for my $r (0 .. $#{$self->{children}}) {
733 my $row = $self->{children}[$r]
734 or next;
735
736 my $x = 0;
737 my $row_h = $hs->[$r];
738
739 for my $c (0 .. $#$row) {
740 my $widget = $row->[$c]
741 or next;
742
743 my $col_w = $ws->[$c];
744
745 $widget->size_allocate ($x, $y, $col_w, $row_h);
746
747 $x += $col_w;
748 }
749
750 $y += $row_h;
751 }
752
753}
754
755sub find_widget {
756 my ($self, $x, $y) = @_;
757
758 $x -= $self->{x};
759 $y -= $self->{y};
760
761 my $res;
762
763 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
764 $res = $_->find_widget ($x, $y)
765 and return $res;
766 }
767
768 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
769}
770
771sub _draw {
772 my ($self) = @_;
773
774 for (grep $_, @{$self->{children}}) {
775 $_->draw for grep $_, @$_;
776 }
777}
778
779#############################################################################
780
781package CFClient::UI::HBox;
782
783# TODO: wrap into common Box base class
784
785our @ISA = CFClient::UI::Container::;
786
787sub size_request {
788 my ($self) = @_;
789
790 my @alloc = map [$_->size_request], @{$self->{children}};
791
792 (
793 (List::Util::sum map $_->[0], @alloc),
794 (List::Util::max map $_->[1], @alloc),
795 )
796}
797
798sub size_allocate {
799 my ($self, $x, $y, $w, $h) = @_;
800
801 $self->_size_allocate ($x, $y, $w, $h) or return;
802
803 return unless $self->{w};
804
805 ($h, $w) = ($w, $h);
806
807 my $children = $self->{children};
808
809 my @h = map +($_->size_request)[0], @$children;
810
811 my $req_h = List::Util::sum @h;
812
813 if ($req_h > $h) {
814 # ah well, not enough space
815 $_ = $h[$_] * $h / $req_h for @h;
816 } else {
817 my $exp = List::Util::sum map $_->{expand}, @$children;
818 $exp ||= 1;
819
820 for (0 .. $#$children) {
821 my $child = $children->[$_];
822
823 my $alloc_h = $h[$_];
824 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
825 $h[$_] = $alloc_h;
826 }
827 }
828
829 my $y = 0;
830 for (0 .. $#$children) {
831 my $child = $children->[$_];
832 my $h = $h[$_];
833 $child->size_allocate ($y, 0, $h, $w);
834
835 $y += $h;
836 }
837}
838
839#############################################################################
840
841package CFClient::UI::VBox;
842
843# TODO: wrap into common Box base class
844
845our @ISA = CFClient::UI::Container::;
846
847sub size_request {
848 my ($self) = @_;
849
850 my @alloc = map [$_->size_request], @{$self->{children}};
851
852 (
853 (List::Util::max map $_->[0], @alloc),
854 (List::Util::sum map $_->[1], @alloc),
855 )
856}
857
858sub size_allocate {
859 my ($self, $x, $y, $w, $h) = @_;
860
861 $self->_size_allocate ($x, $y, $w, $h) or return;
862
863 return unless $self->{h};
864
865 my $children = $self->{children};
866
867 my @h = map +($_->size_request)[1], @$children;
868
869 my $req_h = List::Util::sum @h;
870
871 if ($req_h > $h) {
872 # ah well, not enough space
873 $_ = $h[$_] * $h / $req_h for @h;
874 } else {
875 my $exp = List::Util::sum map $_->{expand}, @$children;
876 $exp ||= 1;
877
878 for (0 .. $#$children) {
879 my $child = $children->[$_];
880
881 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
882 }
883 }
884
885 my $y = 0;
886 for (0 .. $#$children) {
887 my $child = $children->[$_];
888 my $h = $h[$_];
889 $child->size_allocate (0, $y, $w, $h);
890
891 $y += $h;
892 }
893}
894
895#############################################################################
896
897package CFClient::UI::Label;
898
899our @ISA = CFClient::UI::Base::;
900
901use SDL::OpenGL;
902
903sub new {
904 my ($class, %arg) = @_;
905
906 my $self = $class->SUPER::new (
907 fg => [1, 1, 1],
908 height => $::FONTSIZE,
909 text => "",
910 align => -1,
911 padding => 2,
912 layout => new CFClient::Layout,
913 %arg
914 );
915
916 $self->set_text ($self->{text});
917
918 $self
919}
920
921sub escape_text {
922 local $_ = $_[1];
923
924 s/&/&amp;/g;
925 s/>/&gt;/g;
926 s/</&lt;/g;
927
928 $_[1]
929}
930
931sub set_text {
932 my ($self, $text) = @_;
933
934 $self->{text} = $text;
935 $self->{layout}->set_markup ($text);
936
937 delete $self->{texture};
938 $self->update;
939}
940
941sub get_text {
942 my ($self, $text) = @_;
943
944 $self->{text}
945}
946
947sub size_request {
948 my ($self) = @_;
949
950 $self->{layout}->set_width;
951 $self->{layout}->set_height ($self->{height});
952 my ($w, $h) = $self->{layout}->size;
953
954 (
955 $w + $self->{padding} * 2,
956 $h + $self->{padding} * 2,
957 )
958}
959
960sub size_allocate {
961 my ($self, $x, $y, $w, $h) = @_;
962
963 $self->_size_allocate ($x, $y, $w, $h) or return;
964
965 delete $self->{texture};
966}
967
968sub update {
969 my ($self) = @_;
970
971 delete $self->{texture};
972 $self->SUPER::update;
973}
974
975sub _draw {
976 my ($self) = @_;
977
978 my $tex = $self->{texture} ||= do {
979 $self->{layout}->set_width ($self->{w});
980 new_from_layout CFClient::Texture $self->{layout}
981 };
982
983 glEnable GL_BLEND;
984 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
985 glEnable GL_TEXTURE_2D;
986 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
987
988 glColor @{$self->{fg}};
989
990 my $x =
991 $self->{align} < 0 ? $self->{padding}
992 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
993 : ($self->{w} - $tex->{w}) * 0.5;
994
995 $tex->draw_quad ($x, $self->{padding});
996
997 glDisable GL_TEXTURE_2D;
998 glDisable GL_BLEND;
999}
1000
1001#############################################################################
1002
1003package CFClient::UI::Entry;
1004
1005our @ISA = CFClient::UI::Label::;
1006
1007use SDL;
1008use SDL::OpenGL;
1009
1010sub new {
1011 my $class = shift;
1012
1013 $class->SUPER::new (
1014 fg => [1, 1, 1],
1015 bg => [0, 0, 0, 0.2],
1016 active_bg => [1, 1, 1],
1017 active_fg => [0, 0, 0],
1018 @_
1019 )
1020}
1021
1022sub _set_text {
1023 my ($self, $text) = @_;
1024
1025 $self->{last_activity} = $::NOW;
1026
1027 $self->{text} = $text;
1028 $self->{layout}->set_width ($self->{w});
1029
1030 $text =~ s/./*/g if $self->{hidden};
1031
1032 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
1033
1034 $text = substr $text, 0, $self->{cursor};
1035 utf8::encode $text;
1036
1037 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1038}
1039
1040sub size_request {
1041 my ($self) = @_;
1042
1043 my ($w, $h) = $self->SUPER::size_request;
1044
1045 ($w + 1, $h) # add 1 for cursor
1046}
1047
1048sub size_allocate {
1049 my ($self, $x, $y, $w, $h) = @_;
1050
1051 $self->SUPER::size_allocate ($x, $y, $w, $h);
1052
1053 $self->_set_text ($self->{text});
1054}
1055
1056sub set_text {
1057 my ($self, $text) = @_;
1058
1059 $self->{cursor} = length $text;
1060 $self->_set_text ($text);
1061 $self->update;
1062}
1063
34sub key_down { 1064sub key_down {
35 my ($widget, $sdlev) = @_; 1065 my ($self, $ev) = @_;
1066
1067 my $mod = $ev->key_mod;
1068 my $sym = $ev->key_sym;
1069
1070 my $uni = $ev->key_unicode;
1071
1072 my $text = $self->get_text;
1073
1074 if ($sym == SDLK_BACKSPACE) {
1075 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1076 } elsif ($sym == SDLK_DELETE) {
1077 substr $text, $self->{cursor}, 1, "";
1078 } elsif ($sym == SDLK_LEFT) {
1079 --$self->{cursor} if $self->{cursor};
1080 } elsif ($sym == SDLK_RIGHT) {
1081 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1082 } elsif ($sym == SDLK_HOME) {
1083 $self->{cursor} = 0;
1084 } elsif ($sym == SDLK_END) {
1085 $self->{cursor} = length $text;
1086 } elsif ($uni) {
1087 substr $text, $self->{cursor}++, 0, chr $uni;
1088 }
1089
1090 $self->_set_text ($text);
1091 $self->update;
36} 1092}
37sub key_up { 1093
38 my ($widget, $sdlev) = @_; 1094sub focus_in {
1095 my ($self) = @_;
1096
1097 $self->{last_activity} = $::NOW;
1098
1099 $self->SUPER::focus_in;
39} 1100}
1101
40sub button_down { 1102sub button_down {
41 my ($widget, $sdlev) = @_; 1103 my ($self, $ev, $x, $y) = @_;
1104
1105 $self->SUPER::button_down ($ev, $x, $y);
1106
1107 my $idx = $self->{layout}->xy_to_index ($x, $y);
1108
1109 # byte-index to char-index
1110 my $text = $self->{text};
1111 utf8::encode $text;
1112 $self->{cursor} = length substr $text, 0, $idx;
1113
1114 $self->_set_text ($self->{text});
1115 $self->update;
42} 1116}
43sub button_up { 1117
44 my ($widget, $sdlev) = @_; 1118sub mouse_motion {
1119 my ($self, $ev, $x, $y) = @_;
1120# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
45} 1121}
1122
46sub draw { 1123sub _draw {
47 my ($widget) = @_; 1124 my ($self) = @_;
48}
49sub bbox {
50 my ($widget) = @_;
51}
52 1125
53package Client::TextView; 1126 local $self->{fg} = $self->{fg};
1127
1128 if ($FOCUS == $self) {
1129 glColor @{$self->{active_bg}};
1130 $self->{fg} = $self->{active_fg};
1131 } else {
1132 glColor @{$self->{bg}};
1133 }
1134
1135 glEnable GL_BLEND;
1136 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1137 glBegin GL_QUADS;
1138 glVertex 0 , 0;
1139 glVertex 0 , $self->{h};
1140 glVertex $self->{w}, $self->{h};
1141 glVertex $self->{w}, 0;
1142 glEnd;
1143 glDisable GL_BLEND;
1144
1145 $self->SUPER::_draw;
1146
1147 #TODO: force update every cursor change :(
1148 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1149 glColor @{$self->{fg}};
1150 glBegin GL_LINES;
1151 glVertex $self->{cur_x}, $self->{cur_y};
1152 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1153 glEnd;
1154 }
1155}
1156
1157#############################################################################
1158
1159package CFClient::UI::Slider;
1160
54use strict; 1161use strict;
55our @ISA = qw/Client::Widget/;
56 1162
57use SDL::OpenGL; 1163use SDL::OpenGL;
58use SDL::OpenGL::Constants;
59 1164
60sub add_line { 1165our @ISA = CFClient::UI::DrawBG::;
1166
1167sub new {
1168 my $class = shift;
1169
1170 # range [value, low, high, page]
1171
1172 my $self = $class->SUPER::new (
1173 fg => [1, 1, 1],
1174 active_fg => [0, 0, 0],
1175 range => [0, 0, 100, 10],
1176 req_w => 40,
1177 req_h => 10,
1178 vertical => 0,
1179 @_
1180 );
1181
1182 $self
1183}
1184
1185sub size_request {
61 my ($self, $line) = @_; 1186 my ($self) = @_;
62 push @{$self->{lines}}, $line;
63}
64 1187
1188 my $w = $self->{req_w};
1189 my $h = $self->{req_h};
1190
1191 $self->{vertical} ? ($h, $w) : ($w, $h)
1192}
1193
1194sub button_down {
1195 my ($self, $ev, $x, $y) = @_;
1196
1197 $self->SUPER::button_down ($ev, $x, $y);
1198 $self->mouse_motion ($ev, $x, $y);
1199}
1200
1201sub mouse_motion {
1202 my ($self, $ev, $x, $y) = @_;
1203
1204 if ($GRAB == $self) {
1205 my ($value, $lo, $hi, $page) = @{$self->{range}};
1206
1207 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1208
1209 $x = $x * ($hi - $lo) / $w + $lo;
1210 $x = $lo if $x < $lo;
1211 $x = $hi - $page if $x > $hi - $page;
1212 $self->{range}[0] = $x;
1213
1214 $self->emit (changed => $x);
1215 $self->update;
1216 }
1217}
1218
65sub draw { 1219sub _draw {
66 my ($self) = @_; 1220 my ($self) = @_;
67 1221
68} 1222 $self->SUPER::_draw ();
69 1223
1224 my ($w, $h) = @$self{qw(w h)};
1225
1226 if ($self->{vertical}) {
1227 # draw a vertical slider like a rotated horizontal slider
1228
1229 glRotate 90, 0, 0, 1;
1230 glTranslate 0, -$self->{w}, 0;
1231
1232 ($w, $h) = ($h, $w);
1233 }
1234
1235 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1236 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1237
1238 my ($value, $lo, $hi, $page) = @{$self->{range}};
1239
1240 $page = int $page * $w / ($hi - $lo);
1241 $value = int +($value - $lo) * $w / ($hi - $lo);
1242
1243 $w -= $page;
1244 $page &= ~1;
1245 glTranslate $page * 0.5, 0, 0;
1246
1247 glColor @$fg;
1248 glBegin GL_LINES;
1249 glVertex 0, 0; glVertex 0, $h;
1250 glVertex $w - 1, 0; glVertex $w - 1, $h;
1251 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1252 glEnd;
1253
1254 my $knob_a = $value - $page * 0.5;
1255 my $knob_b = $value + $page * 0.5;
1256
1257 glBegin GL_QUADS;
1258 glColor @$fg;
1259 glVertex $knob_a, 0;
1260 glVertex $knob_a, $h;
1261 glVertex $knob_b, $h;
1262 glVertex $knob_b, 0;
1263
1264 if ($knob_a < $knob_b - 2) {
1265 glColor @$bg;
1266 glVertex $knob_a + 1, 1;
1267 glVertex $knob_a + 1, $h - 1;
1268 glVertex $knob_b - 1, $h - 1;
1269 glVertex $knob_b - 1, 1;
1270 }
1271 glEnd;
1272}
1273
1274#############################################################################
1275
70package Client::MapWidget; 1276package CFClient::UI::MapWidget;
1277
71use strict; 1278use strict;
72our @ISA = qw/Client::Widget/;
73 1279
1280use List::Util qw(min max);
1281
1282use SDL;
74use SDL::OpenGL; 1283use SDL::OpenGL;
75use SDL::OpenGL::Constants; 1284
1285our @ISA = CFClient::UI::Base::;
1286
1287sub new {
1288 my $class = shift;
1289
1290 $class->SUPER::new (
1291 z => -1,
1292 list => (glGenLists 1),
1293 @_
1294 )
1295}
76 1296
77sub key_down { 1297sub key_down {
78 print "MAPKEYDOWN\n"; 1298 print "MAPKEYDOWN\n";
79} 1299}
80 1300
81sub key_up { 1301sub key_up {
82} 1302}
83 1303
1304sub size_request {
1305 (
1306 1 + 32 * int $::WIDTH / 32,
1307 1 + 32 * int $::HEIGHT / 32,
1308 )
1309}
1310
1311sub update {
1312 my ($self) = @_;
1313
1314 $self->{need_update} = 1;
1315 $self->SUPER::update;
1316}
1317
84sub draw { 1318sub draw {
85 glEnable GL_TEXTURE_2D; 1319 my ($self) = @_;
86 glEnable GL_BLEND;
87 1320
1321 if (delete $self->{need_update}) {
1322 glNewList $self->{list}, GL_COMPILE;
1323
1324 my $mx = $::CONN->{mapx};
1325 my $my = $::CONN->{mapy};
1326
88 my $map = $::CONN->{map}; 1327 my $map = $::CONN->{map};
89 1328
90 for my $x (0 .. $::CONN->{mapw} - 1) { 1329 my ($xofs, $yofs);
91 for my $y (0 .. $::CONN->{maph} - 1) {
92 1330
93 my $cell = $map->[$x][$y] 1331 my $sw = 1 + int $::WIDTH / 32;
1332 my $sh = 1 + int $::HEIGHT / 32;
1333
1334 if ($::CONN->{mapw} > $sw) {
1335 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
1336 } else {
1337 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
1338 }
1339
1340 if ($::CONN->{maph} > $sh) {
1341 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
1342 } else {
1343 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1344 }
1345
1346 glEnable GL_TEXTURE_2D;
1347 glEnable GL_BLEND;
1348 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1349 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1350
1351 my $sw4 = ($sw + 3) & ~3;
1352 my $darkness = "\x00" x ($sw4 * $sh);
1353
1354 for my $x (0 .. $sw - 1) {
1355 my $row = $map->[$x + $xofs];
1356 for my $y (0 .. $sh - 1) {
1357
1358 my $cell = $row->[$y + $yofs]
94 or next; 1359 or next;
95 1360
96 my $darkness = $cell->[3] * (1 / 255); 1361 my $dark = $cell->[0];
97 glColor $darkness, $darkness, $darkness; 1362 if ($dark < 0) {
1363 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1364 } else {
1365 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1366 }
98 1367
99 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { 1368 for my $num (grep $_, @$cell[1,2,3]) {
100 my $tex = $::CONN->{face}[$num]{texture} || 0; 1369 my $tex = $::CONN->{face}[$num]{texture} || next;
1370
1371 my ($w, $h) = @$tex{qw(w h)};
101 1372
102 glBindTexture GL_TEXTURE_2D, $tex; 1373 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
103
104 glBegin GL_QUADS;
105 glTexCoord 0, 0; glVertex $x, $y;
106 glTexCoord 0, 1; glVertex $x, $y + 1;
107 glTexCoord 1, 1; glVertex $x + 1, $y + 1;
108 glTexCoord 1, 0; glVertex $x + 1, $y;
109 glEnd; 1374 }
110 } 1375 }
111 } 1376 }
1377
1378# if (1) { # higher quality darkness
1379# $lighting =~ s/(.)/$1$1$1/gs;
1380# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1381#
1382# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1383#
1384# $lighting = $pb->get_pixels;
1385# $lighting =~ s/(.)../$1/gs;
112 } 1386# }
113 1387
1388 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1389
1390 $darkness = new CFClient::Texture
1391 w => $sw4,
1392 h => $sh,
1393 data => $darkness,
1394 internalformat => GL_ALPHA,
1395 format => GL_ALPHA;
1396
1397 glColor 0.45, 0.45, 0.45, 1;
1398 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1399
114 glDisable GL_TEXTURE_2D; 1400 glDisable GL_TEXTURE_2D;
115 glDisable GL_BLEND; 1401 glDisable GL_BLEND;
116}
117 1402
1181; 1403 glEndList;
1404 }
1405
1406 glCallList $self->{list};
1407}
1408
1409my %DIR = (
1410 SDLK_KP8, [1, "north"],
1411 SDLK_KP9, [2, "northeast"],
1412 SDLK_KP6, [3, "east"],
1413 SDLK_KP3, [4, "southeast"],
1414 SDLK_KP2, [5, "south"],
1415 SDLK_KP1, [6, "southwest"],
1416 SDLK_KP4, [7, "west"],
1417 SDLK_KP7, [8, "northwest"],
1418
1419 SDLK_UP, [1, "north"],
1420 SDLK_RIGHT, [3, "east"],
1421 SDLK_DOWN, [5, "south"],
1422 SDLK_LEFT, [7, "west"],
1423);
1424
1425sub key_down {
1426 my ($self, $ev) = @_;
1427
1428 my $mod = $ev->key_mod;
1429 my $sym = $ev->key_sym;
1430
1431 if ($sym == SDLK_KP5) {
1432 $::CONN->send ("command stay fire");
1433 } elsif (exists $DIR{$sym}) {
1434 if ($mod & KMOD_SHIFT) {
1435 $self->{shft}++;
1436 $::CONN->send ("command fire $DIR{$sym}[0]");
1437 } elsif ($mod & KMOD_CTRL) {
1438 $self->{ctrl}++;
1439 $::CONN->send ("command run $DIR{$sym}[0]");
1440 } else {
1441 $::CONN->send ("command $DIR{$sym}[1]");
1442 }
1443 }
1444}
1445
1446sub key_up {
1447 my ($self, $ev) = @_;
1448
1449 my $mod = $ev->key_mod;
1450 my $sym = $ev->key_sym;
1451
1452 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1453 $::CONN->send ("command fire_stop");
1454 }
1455 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1456 $::CONN->send ("command run_stop");
1457 }
1458}
1459
1460#############################################################################
1461
1462package CFClient::UI::Animator;
1463
1464use SDL::OpenGL;
1465
1466our @ISA = CFClient::UI::Bin::;
1467
1468sub moveto {
1469 my ($self, $x, $y) = @_;
1470
1471 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
1472 $self->{speed} = 0.001;
1473 $self->{time} = 1;
1474
1475 ::animation_start $self;
1476}
1477
1478sub animate {
1479 my ($self, $interval) = @_;
1480
1481 $self->{time} -= $interval * $self->{speed};
1482 if ($self->{time} <= 0) {
1483 $self->{time} = 0;
1484 ::animation_stop $self;
1485 }
1486
1487 my ($x0, $y0, $x1, $y1) = @{$self->{moveto}};
1488
1489 $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time});
1490 $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time});
1491}
1492
1493sub _draw {
1494 my ($self) = @_;
1495
1496 glPushMatrix;
1497 glRotate $self->{time} * 1000, 0, 1, 0;
1498 $self->{children}[0]->draw;
1499 glPopMatrix;
1500}
1501
1502#############################################################################
1503
1504package CFClient::UI::Toplevel;
1505
1506our @ISA = CFClient::UI::Container::;
1507
1508sub size_request {
1509 ($::WIDTH, $::HEIGHT)
1510}
1511
1512sub size_allocate {
1513 my ($self, $x, $y, $w, $h) = @_;
1514
1515 $self->_size_allocate ($x, $y, $w, $h);
1516
1517 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1518 for @{$self->{children}};
1519}
1520
1521sub translate {
1522 my ($self, $x, $y) = @_;
1523
1524 ($x, $y)
1525}
1526
1527sub update {
1528 my ($self) = @_;
1529
1530 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1531 ::refresh ();
1532}
1533
1534sub add {
1535 my ($self, $widget) = @_;
1536
1537 $self->SUPER::add ($widget);
1538
1539 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1540}
1541
1542sub draw {
1543 my ($self) = @_;
1544
1545 $self->_draw;
1546}
1547
1548#############################################################################
1549
1550package CFClient::UI;
1551
1552$TOPLEVEL = new CFClient::UI::Toplevel;
1553
15541
1555

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines