ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.68
Committed: Tue Apr 11 17:02:36 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.67: +272 -47 lines
Log Message:
*** empty log message ***

File Contents

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