ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.160
Committed: Mon Apr 24 03:33:51 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.159: +2 -2 lines
Log Message:
*** empty log message ***

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