ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.165
Committed: Mon Apr 24 08:22:22 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.164: +0 -1 lines
Log Message:
bugfix

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