ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
Revision: 1.174
Committed: Tue Apr 25 11:18:49 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.173: +21 -14 lines
Log Message:
do not require an alpha channel for the framebuffer for the sake of software-renderers

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