… | |
… | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $ROOT; |
12 | our $ROOT; |
|
|
13 | our $TOOLTIP; |
13 | our $BUTTON_STATE; |
14 | our $BUTTON_STATE; |
|
|
15 | |
|
|
16 | sub check_tooltip { |
|
|
17 | if (!$GRAB) { |
|
|
18 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
19 | if (exists $widget->{tooltip}) { |
|
|
20 | |
|
|
21 | if ($TOOLTIP->{owner} != $widget) { |
|
|
22 | $TOOLTIP->{owner} = $widget; |
|
|
23 | |
|
|
24 | my $tip = $widget->{tooltip}; |
|
|
25 | |
|
|
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
27 | |
|
|
28 | $TOOLTIP->set_text ($widget->{tooltip}); |
|
|
29 | $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); |
|
|
30 | $TOOLTIP->show; |
|
|
31 | } |
|
|
32 | |
|
|
33 | return; |
|
|
34 | } |
|
|
35 | } |
|
|
36 | } |
|
|
37 | |
|
|
38 | $TOOLTIP->hide; |
|
|
39 | delete $TOOLTIP->{owner}; |
|
|
40 | } |
14 | |
41 | |
15 | # class methods for events |
42 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
43 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
44 | $FOCUS->key_down ($_[0]) if $FOCUS; |
18 | } |
45 | } |
… | |
… | |
28 | if (!$BUTTON_STATE) { |
55 | if (!$BUTTON_STATE) { |
29 | my $widget = $ROOT->find_widget ($x, $y); |
56 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
57 | |
31 | $GRAB = $widget; |
58 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
59 | $GRAB->update if $GRAB; |
|
|
60 | |
|
|
61 | check_tooltip; |
33 | } |
62 | } |
34 | |
63 | |
35 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
64 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
65 | |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
66 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
… | |
… | |
49 | |
78 | |
50 | if (!$BUTTON_STATE) { |
79 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
80 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
81 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
82 | $GRAB->update if $GRAB; |
|
|
83 | |
|
|
84 | check_tooltip; |
54 | } |
85 | } |
55 | } |
86 | } |
56 | |
87 | |
57 | sub feed_sdl_motion_event { |
88 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
89 | my ($ev) = @_; |
… | |
… | |
63 | if ($widget != $HOVER) { |
94 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
95 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
96 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
97 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
98 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
|
|
99 | |
|
|
100 | check_tooltip; |
68 | } |
101 | } |
69 | |
102 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
103 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
71 | } |
104 | } |
72 | |
105 | |
… | |
… | |
96 | |
129 | |
97 | my $self = bless { |
130 | my $self = bless { |
98 | x => 0, |
131 | x => 0, |
99 | y => 0, |
132 | y => 0, |
100 | z => 0, |
133 | z => 0, |
|
|
134 | can_events => 1, |
101 | @_ |
135 | @_ |
102 | }, $class; |
136 | }, $class; |
103 | |
137 | |
104 | for (keys %$self) { |
138 | for (keys %$self) { |
105 | if (/^connect_(.*)$/) { |
139 | if (/^connect_(.*)$/) { |
… | |
… | |
140 | 0 |
174 | 0 |
141 | } |
175 | } |
142 | |
176 | |
143 | sub size_request { |
177 | sub size_request { |
144 | require Carp; |
178 | require Carp; |
145 | Carp::confess "size_request is abtract"; |
179 | Carp::confess "size_request is abstract"; |
146 | } |
180 | } |
147 | |
181 | |
148 | sub configure { |
182 | sub configure { |
149 | my ($self, $x, $y, $w, $h) = @_; |
183 | my ($self, $x, $y, $w, $h) = @_; |
150 | |
184 | |
… | |
… | |
152 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
186 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
153 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
187 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
154 | |
188 | |
155 | # use alignment to adjust x, y |
189 | # use alignment to adjust x, y |
156 | |
190 | |
157 | $x += ($w - $w2) * 0.5; |
191 | $x += int +($w - $w2) * 0.5; |
158 | $y += ($h - $h2) * 0.5; |
192 | $y += int +($h - $h2) * 0.5; |
159 | |
193 | |
160 | ($w, $h) = ($w2, $h2); |
194 | ($w, $h) = ($w2, $h2); |
161 | } |
195 | } |
162 | |
196 | |
163 | if ($self->{x} != $x || $self->{y} != $y) { |
197 | if ($self->{x} != $x || $self->{y} != $y) { |
… | |
… | |
175 | } |
209 | } |
176 | } |
210 | } |
177 | |
211 | |
178 | sub size_allocate { |
212 | sub size_allocate { |
179 | # nothing to be done |
213 | # nothing to be done |
|
|
214 | } |
|
|
215 | |
|
|
216 | sub set_max_size { |
|
|
217 | my ($self, $w, $h) = @_; |
|
|
218 | |
|
|
219 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
|
|
220 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
180 | } |
221 | } |
181 | |
222 | |
182 | # return top left coordinates |
223 | # return top left coordinates |
183 | sub _topleft { |
224 | sub _topleft { |
184 | my ($self, $x, $y) = @_; |
225 | my ($self, $x, $y) = @_; |
… | |
… | |
278 | } |
319 | } |
279 | |
320 | |
280 | sub find_widget { |
321 | sub find_widget { |
281 | my ($self, $x, $y) = @_; |
322 | my ($self, $x, $y) = @_; |
282 | |
323 | |
|
|
324 | return () unless $self->{can_events}; |
|
|
325 | |
283 | return $self |
326 | return $self |
284 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
327 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
285 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
328 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
286 | |
329 | |
287 | () |
330 | () |
… | |
… | |
294 | } |
337 | } |
295 | |
338 | |
296 | sub check_size { |
339 | sub check_size { |
297 | my ($self) = @_; |
340 | my ($self) = @_; |
298 | |
341 | |
|
|
342 | return unless $self->{parent}; |
|
|
343 | |
299 | my ($w, $h) = $self->size_request; |
344 | my ($w, $h) = $self->size_request; |
300 | |
345 | |
301 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
346 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
302 | $self->{req_w} = $w; |
347 | $self->{req_w} = $w; |
303 | $self->{req_h} = $h; |
348 | $self->{req_h} = $h; |
304 | |
349 | |
305 | $self->{parent}->check_size |
350 | $self->{parent}->check_size; |
306 | if $self->{parent}; |
|
|
307 | } |
351 | } |
308 | } |
352 | } |
309 | |
353 | |
310 | sub update { |
354 | sub update { |
311 | my ($self) = @_; |
355 | my ($self) = @_; |
… | |
… | |
378 | |
422 | |
379 | package CFClient::UI::Empty; |
423 | package CFClient::UI::Empty; |
380 | |
424 | |
381 | our @ISA = CFClient::UI::Base::; |
425 | our @ISA = CFClient::UI::Base::; |
382 | |
426 | |
|
|
427 | sub new { |
|
|
428 | my ($class, %arg) = @_; |
|
|
429 | $class->SUPER::new (can_events => 0, %arg); |
|
|
430 | } |
|
|
431 | |
383 | sub size_request { |
432 | sub size_request { |
384 | (0, 0) |
433 | (0, 0) |
385 | } |
434 | } |
386 | |
435 | |
387 | sub draw { } |
436 | sub draw { } |
… | |
… | |
395 | sub new { |
444 | sub new { |
396 | my ($class, %arg) = @_; |
445 | my ($class, %arg) = @_; |
397 | |
446 | |
398 | my $children = delete $arg{children} || []; |
447 | my $children = delete $arg{children} || []; |
399 | |
448 | |
400 | my $self = $class->SUPER::new (children => [], %arg); |
449 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
401 | $self->add ($_) for @$children; |
450 | $self->add ($_) for @$children; |
402 | |
451 | |
403 | $self |
452 | $self |
404 | } |
453 | } |
405 | |
454 | |
… | |
… | |
424 | delete $child->{parent}; |
473 | delete $child->{parent}; |
425 | |
474 | |
426 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
475 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
427 | |
476 | |
428 | $self->check_size; |
477 | $self->check_size; |
|
|
478 | $self->update; |
429 | } |
479 | } |
430 | |
480 | |
431 | sub find_widget { |
481 | sub find_widget { |
432 | my ($self, $x, $y) = @_; |
482 | my ($self, $x, $y) = @_; |
433 | |
483 | |
… | |
… | |
561 | sub new { die } |
611 | sub new { die } |
562 | |
612 | |
563 | sub size_request { |
613 | sub size_request { |
564 | my ($self) = @_; |
614 | my ($self) = @_; |
565 | |
615 | |
566 | @$self{qw(child_w child_h)} = $self->child->size_request; |
616 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
567 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
617 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
568 | |
618 | |
569 | @$self{qw(child_w child_h)} |
619 | @$self{qw(child_w child_h)} |
570 | } |
620 | } |
571 | |
621 | |
… | |
… | |
581 | package CFClient::UI::Frame; |
631 | package CFClient::UI::Frame; |
582 | |
632 | |
583 | our @ISA = CFClient::UI::Bin::; |
633 | our @ISA = CFClient::UI::Bin::; |
584 | |
634 | |
585 | use CFClient::OpenGL; |
635 | use CFClient::OpenGL; |
586 | |
|
|
587 | sub size_request { |
|
|
588 | my ($self) = @_; |
|
|
589 | my $chld = $self->child |
|
|
590 | or return (0, 0); |
|
|
591 | |
|
|
592 | $chld->move (2, 2); |
|
|
593 | |
|
|
594 | map { $_ + 4 } $chld->size_request; |
|
|
595 | } |
|
|
596 | |
|
|
597 | sub size_allocate { |
|
|
598 | my ($self, $x, $y, $w, $h) = @_; |
|
|
599 | |
|
|
600 | $self->child->configure (2, 2, $w - 4, $h - 4); |
|
|
601 | } |
|
|
602 | |
|
|
603 | sub _draw { |
|
|
604 | my ($self) = @_; |
|
|
605 | |
|
|
606 | my $chld = $self->child; |
|
|
607 | |
|
|
608 | my ($w, $h) = $chld->size_request; |
|
|
609 | |
|
|
610 | glBegin GL_QUADS; |
|
|
611 | glColor 0, 0, 0; |
|
|
612 | glVertex 0 , 0; |
|
|
613 | glVertex 0 , $h + 4; |
|
|
614 | glVertex $w + 4 , $h + 4; |
|
|
615 | glVertex $w + 4 , 0; |
|
|
616 | glEnd; |
|
|
617 | |
|
|
618 | $chld->draw; |
|
|
619 | } |
|
|
620 | |
|
|
621 | ############################################################################# |
|
|
622 | |
|
|
623 | package CFClient::UI::FancyFrame; |
|
|
624 | |
|
|
625 | our @ISA = CFClient::UI::Bin::; |
|
|
626 | |
|
|
627 | use CFClient::OpenGL; |
|
|
628 | |
|
|
629 | my @tex = |
|
|
630 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
631 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
632 | |
636 | |
633 | sub new { |
637 | sub new { |
634 | my $class = shift; |
638 | my $class = shift; |
635 | |
|
|
636 | # TODO: user_x, user_y, overwrite moveto? |
|
|
637 | |
639 | |
638 | my $self = $class->SUPER::new ( |
640 | my $self = $class->SUPER::new ( |
639 | bg => [1, 1, 1, 1], |
641 | bg => [1, 1, 1, 1], |
640 | border_bg => [1, 1, 1, 1], |
642 | border_bg => [1, 1, 1, 1], |
641 | border => 0.8, |
643 | border => 0.8, |
642 | @_ |
644 | @_ |
643 | ); |
645 | ); |
644 | |
646 | |
|
|
647 | $self |
|
|
648 | } |
|
|
649 | |
|
|
650 | sub set_size { |
|
|
651 | my ($self, $w, $h) = @_; |
|
|
652 | $self->{req_w} = $w; |
|
|
653 | $self->{req_h} = $h; |
|
|
654 | $self->check_size; |
|
|
655 | } |
|
|
656 | |
|
|
657 | sub size_request { |
|
|
658 | my ($self) = @_; |
|
|
659 | ($self->{req_w}, $self->{req_h}) |
|
|
660 | } |
|
|
661 | |
|
|
662 | sub size_allocate { |
|
|
663 | my ($self, $w, $h) = @_; |
|
|
664 | $self->{w} = $w; |
|
|
665 | $self->{h} = $h; |
|
|
666 | $self->child->configure (0, 0, $w, $h); |
|
|
667 | } |
|
|
668 | |
|
|
669 | sub _draw { |
|
|
670 | my ($self) = @_; |
|
|
671 | |
|
|
672 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
673 | |
|
|
674 | glEnable GL_BLEND; |
|
|
675 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
676 | glEnable GL_TEXTURE_2D; |
|
|
677 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
678 | |
|
|
679 | # glBegin GL_QUADS; |
|
|
680 | # glColor 0, 0, 0, 0; |
|
|
681 | # glVertex 0 , 0; |
|
|
682 | # glVertex 0 , $h; |
|
|
683 | # glVertex $w, $h; |
|
|
684 | # glVertex $w, 0; |
|
|
685 | # glEnd; |
|
|
686 | |
|
|
687 | |
|
|
688 | $self->child->draw; |
|
|
689 | glDisable GL_BLEND; |
|
|
690 | glDisable GL_TEXTURE_2D; |
|
|
691 | } |
|
|
692 | |
|
|
693 | ############################################################################# |
|
|
694 | |
|
|
695 | package CFClient::UI::FancyFrame; |
|
|
696 | |
|
|
697 | our @ISA = CFClient::UI::Bin::; |
|
|
698 | |
|
|
699 | use CFClient::OpenGL; |
|
|
700 | |
|
|
701 | my @tex = |
|
|
702 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
703 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
704 | |
|
|
705 | sub new { |
|
|
706 | my $class = shift; |
|
|
707 | |
|
|
708 | # TODO: user_x, user_y, overwrite moveto? |
|
|
709 | |
|
|
710 | my $self = $class->SUPER::new ( |
|
|
711 | bg => [1, 1, 1, 1], |
|
|
712 | border_bg => [1, 1, 1, 1], |
|
|
713 | border => 0.8, |
|
|
714 | can_events => 1, |
|
|
715 | @_ |
|
|
716 | ); |
|
|
717 | |
645 | $self->{title} &&= new CFClient::UI::Label |
718 | $self->{title} &&= new CFClient::UI::Label |
646 | align => 0, |
719 | align => 0, |
647 | valign => 1, |
720 | valign => 1, |
648 | text => $self->{title}, |
721 | text => $self->{title}, |
649 | fontsize => 1; |
722 | fontsize => 1; |
… | |
… | |
815 | or next; |
888 | or next; |
816 | |
889 | |
817 | for my $x (0 .. $#$row) { |
890 | for my $x (0 .. $#$row) { |
818 | my $widget = $row->[$x] |
891 | my $widget = $row->[$x] |
819 | or next; |
892 | or next; |
820 | my ($w, $h) = $widget->size_request; |
893 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
821 | |
894 | |
822 | $w[$x] = max $w[$x], $w; |
895 | $w[$x] = max $w[$x], $w; |
823 | $h[$y] = max $h[$y], $h; |
896 | $h[$y] = max $h[$y], $h; |
824 | } |
897 | } |
825 | } |
898 | } |
… | |
… | |
930 | |
1003 | |
931 | ($h, $w) = ($w, $h); |
1004 | ($h, $w) = ($w, $h); |
932 | |
1005 | |
933 | my $children = $self->{children}; |
1006 | my $children = $self->{children}; |
934 | |
1007 | |
935 | my @h = map +($_->size_request)[0], @$children; |
1008 | my @h = map $_->{req_w}, @$children; |
936 | |
1009 | |
937 | my $req_h = List::Util::sum @h; |
1010 | my $req_h = List::Util::sum @h; |
938 | |
1011 | |
939 | if ($req_h > $h) { |
1012 | if ($req_h > $h) { |
940 | # ah well, not enough space |
1013 | # ah well, not enough space |
… | |
… | |
988 | sub size_allocate { |
1061 | sub size_allocate { |
989 | my ($self, $w, $h) = @_; |
1062 | my ($self, $w, $h) = @_; |
990 | |
1063 | |
991 | my $children = $self->{children}; |
1064 | my $children = $self->{children}; |
992 | |
1065 | |
993 | my @h = map +($_->size_request)[1], @$children; |
1066 | my @h = map $_->{req_h}, @$children; |
994 | |
1067 | |
995 | my $req_h = List::Util::sum @h; |
1068 | my $req_h = List::Util::sum @h; |
996 | |
1069 | |
997 | if ($req_h > $h) { |
1070 | if ($req_h > $h) { |
998 | # ah well, not enough space |
1071 | # ah well, not enough space |
… | |
… | |
1033 | sub new { |
1106 | sub new { |
1034 | my ($class, %arg) = @_; |
1107 | my ($class, %arg) = @_; |
1035 | |
1108 | |
1036 | my $self = $class->SUPER::new ( |
1109 | my $self = $class->SUPER::new ( |
1037 | fg => [1, 1, 1], |
1110 | fg => [1, 1, 1], |
|
|
1111 | #font => default_font |
1038 | fontsize => 1, |
1112 | fontsize => 1, |
1039 | text => "", |
1113 | text => "", |
1040 | align => -1, |
1114 | align => -1, |
1041 | valign => -1, |
1115 | valign => -1, |
1042 | padding => 2, |
1116 | padding => 2, |
1043 | layout => new CFClient::Layout, |
1117 | layout => new CFClient::Layout, |
|
|
1118 | can_events => 0, |
1044 | %arg |
1119 | %arg |
1045 | ); |
1120 | ); |
1046 | |
1121 | |
1047 | if (exists $self->{template}) { |
1122 | if (exists $self->{template}) { |
1048 | my $layout = new CFClient::Layout; |
1123 | my $layout = new CFClient::Layout; |
… | |
… | |
1070 | my ($self, $text) = @_; |
1145 | my ($self, $text) = @_; |
1071 | |
1146 | |
1072 | $self->{layout}->set_text ($text); |
1147 | $self->{layout}->set_text ($text); |
1073 | |
1148 | |
1074 | delete $self->{texture}; |
1149 | delete $self->{texture}; |
|
|
1150 | $self->check_size; |
1075 | $self->update; |
1151 | $self->update; |
1076 | } |
1152 | } |
1077 | |
1153 | |
1078 | sub set_markup { |
1154 | sub set_markup { |
1079 | my ($self, $markup) = @_; |
1155 | my ($self, $markup) = @_; |
1080 | |
1156 | |
1081 | $self->{layout}->set_markup ($markup); |
1157 | $self->{layout}->set_markup ($markup); |
1082 | |
1158 | |
1083 | delete $self->{texture}; |
1159 | delete $self->{texture}; |
|
|
1160 | $self->check_size; |
1084 | $self->update; |
1161 | $self->update; |
1085 | } |
1162 | } |
1086 | |
1163 | |
1087 | sub size_request { |
1164 | sub size_request { |
1088 | my ($self) = @_; |
1165 | my ($self) = @_; |
1089 | |
1166 | |
1090 | $self->{layout}->set_width; |
1167 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1168 | $self->{layout}->set_width ($self->{max_w} || -1); |
1091 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1169 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1092 | |
1170 | |
1093 | my ($w, $h) = $self->{layout}->size; |
1171 | my ($w, $h) = $self->{layout}->size; |
1094 | |
1172 | |
1095 | if (exists $self->{template}) { |
1173 | if (exists $self->{template}) { |
|
|
1174 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1096 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1175 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1097 | |
1176 | |
1098 | my ($w2, $h2) = $self->{template}->size; |
1177 | my ($w2, $h2) = $self->{template}->size; |
1099 | |
1178 | |
1100 | $w = List::Util::max $w, $w2; |
1179 | $w = List::Util::max $w, $w2; |
… | |
… | |
1111 | my ($self, $w, $h) = @_; |
1190 | my ($self, $w, $h) = @_; |
1112 | |
1191 | |
1113 | delete $self->{texture}; |
1192 | delete $self->{texture}; |
1114 | } |
1193 | } |
1115 | |
1194 | |
|
|
1195 | sub set_fontsize { |
|
|
1196 | my ($self, $fontsize) = @_; |
|
|
1197 | |
|
|
1198 | $self->{fontsize} = $fontsize; |
|
|
1199 | delete $self->{texture}; |
|
|
1200 | $self->check_size; |
|
|
1201 | $self->update; |
|
|
1202 | } |
|
|
1203 | |
1116 | sub _draw { |
1204 | sub _draw { |
1117 | my ($self) = @_; |
1205 | my ($self) = @_; |
1118 | |
1206 | |
1119 | my $tex = $self->{texture} ||= do { |
1207 | my $tex = $self->{texture} ||= do { |
|
|
1208 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1120 | $self->{layout}->set_width ($self->{w}); |
1209 | $self->{layout}->set_width ($self->{w}); |
1121 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1210 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1122 | new_from_layout CFClient::Texture $self->{layout} |
1211 | new_from_layout CFClient::Texture $self->{layout} |
1123 | }; |
1212 | }; |
1124 | |
1213 | |
… | |
… | |
1164 | active_bg => [1, 1, 1, 0.5], |
1253 | active_bg => [1, 1, 1, 0.5], |
1165 | active_fg => [0, 0, 0], |
1254 | active_fg => [0, 0, 0], |
1166 | can_hover => 1, |
1255 | can_hover => 1, |
1167 | can_focus => 1, |
1256 | can_focus => 1, |
1168 | valign => 0, |
1257 | valign => 0, |
|
|
1258 | can_events => 1, |
1169 | @_ |
1259 | @_ |
1170 | ) |
1260 | ) |
1171 | } |
1261 | } |
1172 | |
1262 | |
1173 | sub _set_text { |
1263 | sub _set_text { |
… | |
… | |
1201 | } |
1291 | } |
1202 | |
1292 | |
1203 | sub size_allocate { |
1293 | sub size_allocate { |
1204 | my ($self, $w, $h) = @_; |
1294 | my ($self, $w, $h) = @_; |
1205 | |
1295 | |
1206 | $self->_set_text ($self->{text}); |
1296 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1207 | } |
1297 | } |
1208 | |
1298 | |
1209 | sub set_text { |
1299 | sub set_text { |
1210 | my ($self, $text) = @_; |
1300 | my ($self, $text) = @_; |
1211 | |
1301 | |
… | |
… | |
1344 | our @ISA = CFClient::UI::Label::; |
1434 | our @ISA = CFClient::UI::Label::; |
1345 | |
1435 | |
1346 | use CFClient::OpenGL; |
1436 | use CFClient::OpenGL; |
1347 | |
1437 | |
1348 | my @tex = |
1438 | my @tex = |
1349 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1439 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1350 | qw(b1_button_active.png); |
1440 | qw(b1_button_active.png); |
1351 | |
1441 | |
1352 | sub new { |
1442 | sub new { |
1353 | my $class = shift; |
1443 | my $class = shift; |
1354 | |
1444 | |
… | |
… | |
1358 | bg => [1, 1, 1, 0.2], |
1448 | bg => [1, 1, 1, 0.2], |
1359 | active_fg => [0, 0, 1], |
1449 | active_fg => [0, 0, 1], |
1360 | can_hover => 1, |
1450 | can_hover => 1, |
1361 | align => 0, |
1451 | align => 0, |
1362 | valign => 0, |
1452 | valign => 0, |
|
|
1453 | can_events => 1, |
1363 | @_ |
1454 | @_ |
1364 | ) |
1455 | ) |
1365 | } |
1456 | } |
1366 | |
1457 | |
1367 | sub button_up { |
1458 | sub button_up { |
… | |
… | |
1401 | package CFClient::UI::CheckBox; |
1492 | package CFClient::UI::CheckBox; |
1402 | |
1493 | |
1403 | our @ISA = CFClient::UI::DrawBG::; |
1494 | our @ISA = CFClient::UI::DrawBG::; |
1404 | |
1495 | |
1405 | my @tex = |
1496 | my @tex = |
1406 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1497 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1407 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1498 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1408 | |
1499 | |
1409 | use CFClient::OpenGL; |
1500 | use CFClient::OpenGL; |
1410 | |
1501 | |
1411 | sub new { |
1502 | sub new { |
… | |
… | |
1460 | glDisable GL_BLEND; |
1551 | glDisable GL_BLEND; |
1461 | } |
1552 | } |
1462 | |
1553 | |
1463 | ############################################################################# |
1554 | ############################################################################# |
1464 | |
1555 | |
|
|
1556 | package CFClient::UI::Image; |
|
|
1557 | |
|
|
1558 | our @ISA = CFClient::UI::Base::; |
|
|
1559 | |
|
|
1560 | use CFClient::OpenGL; |
|
|
1561 | use Carp qw/confess/; |
|
|
1562 | |
|
|
1563 | our %loaded_images; |
|
|
1564 | |
|
|
1565 | sub new { |
|
|
1566 | my $class = shift; |
|
|
1567 | |
|
|
1568 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1569 | |
|
|
1570 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1571 | |
|
|
1572 | $loaded_images{$self->{image}} ||= |
|
|
1573 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1574 | |
|
|
1575 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1576 | |
|
|
1577 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1578 | |
|
|
1579 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
1580 | |
|
|
1581 | $self |
|
|
1582 | } |
|
|
1583 | |
|
|
1584 | sub size_request { |
|
|
1585 | my ($self) = @_; |
|
|
1586 | |
|
|
1587 | ($self->{tex}->{w}, $self->{tex}->{h}) |
|
|
1588 | } |
|
|
1589 | |
|
|
1590 | sub _draw { |
|
|
1591 | my ($self) = @_; |
|
|
1592 | |
|
|
1593 | my $tex = $self->{tex}; |
|
|
1594 | |
|
|
1595 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1596 | |
|
|
1597 | if ($self->{rot90}) { |
|
|
1598 | glRotate 90, 0, 0, 1; |
|
|
1599 | glTranslate 0, -$self->{w}, 0; |
|
|
1600 | |
|
|
1601 | ($w, $h) = ($h, $w); |
|
|
1602 | } |
|
|
1603 | |
|
|
1604 | glEnable GL_BLEND; |
|
|
1605 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1606 | glEnable GL_TEXTURE_2D; |
|
|
1607 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1608 | |
|
|
1609 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1610 | |
|
|
1611 | glDisable GL_BLEND; |
|
|
1612 | glDisable GL_TEXTURE_2D; |
|
|
1613 | } |
|
|
1614 | |
|
|
1615 | ############################################################################# |
|
|
1616 | |
1465 | package CFClient::UI::VGauge; |
1617 | package CFClient::UI::VGauge; |
1466 | |
1618 | |
1467 | our @ISA = CFClient::UI::Base::; |
1619 | our @ISA = CFClient::UI::Base::; |
1468 | |
1620 | |
1469 | use CFClient::OpenGL; |
1621 | use CFClient::OpenGL; |
1470 | |
1622 | |
1471 | my %tex = ( |
1623 | my %tex = ( |
1472 | food => [ |
1624 | food => [ |
1473 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1625 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1474 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
1626 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
1475 | ], |
1627 | ], |
1476 | grace => [ |
1628 | grace => [ |
1477 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1629 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1478 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
1630 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
1479 | ], |
1631 | ], |
1480 | hp => [ |
1632 | hp => [ |
1481 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1633 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1482 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
1634 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
1483 | ], |
1635 | ], |
1484 | mana => [ |
1636 | mana => [ |
1485 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1637 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1486 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
1638 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
1487 | ], |
1639 | ], |
1488 | ); |
1640 | ); |
1489 | |
1641 | |
1490 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
1642 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
1491 | sub new { |
1643 | sub new { |
1492 | my $class = shift; |
1644 | my $class = shift; |
1493 | |
1645 | |
1494 | my $self = $class->SUPER::new ( |
1646 | my $self = $class->SUPER::new ( |
1495 | type => 'food', |
1647 | type => 'food', |
1496 | scale => 1, |
|
|
1497 | @_ |
1648 | @_ |
1498 | ); |
1649 | ); |
1499 | |
1650 | |
1500 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
1651 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
1501 | |
1652 | |
… | |
… | |
1503 | } |
1654 | } |
1504 | |
1655 | |
1505 | sub size_request { |
1656 | sub size_request { |
1506 | my ($self) = @_; |
1657 | my ($self) = @_; |
1507 | |
1658 | |
1508 | my $tex = $tex{$self->{type}}[0]; |
1659 | #my $tex = $tex{$self->{type}}[0]; |
1509 | |
1660 | #@$tex{qw(w h)} |
1510 | my ($w, $h) = map { $_ * $self->{scale} } @$tex{qw(w h)}; |
1661 | (0, 0) |
1511 | ($self->{vertical}) ? ($h, $w) : ($w, $h) |
|
|
1512 | } |
1662 | } |
1513 | |
1663 | |
1514 | sub set_max { |
1664 | sub set_max { |
1515 | my ($self, $max) = @_; |
1665 | my ($self, $max) = @_; |
1516 | |
1666 | |
… | |
… | |
1582 | package CFClient::UI::Gauge; |
1732 | package CFClient::UI::Gauge; |
1583 | |
1733 | |
1584 | our @ISA = CFClient::UI::VBox::; |
1734 | our @ISA = CFClient::UI::VBox::; |
1585 | |
1735 | |
1586 | sub new { |
1736 | sub new { |
1587 | my ($class, %arg) = shift; |
1737 | my ($class, %arg) = @_; |
1588 | |
1738 | |
1589 | my $self = $class->SUPER::new ( |
1739 | my $self = $class->SUPER::new ( |
1590 | @_, |
1740 | tooltip => $arg{type}, |
|
|
1741 | %arg, |
1591 | ); |
1742 | ); |
1592 | |
1743 | |
1593 | $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
1744 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1); |
1594 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, scale => ($self->{scale} || 0.5)); |
1745 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1); |
1595 | $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
1746 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1); |
1596 | |
1747 | |
1597 | $self |
1748 | $self |
|
|
1749 | } |
|
|
1750 | |
|
|
1751 | sub set_fontsize { |
|
|
1752 | my ($self, $fsize) = @_; |
|
|
1753 | |
|
|
1754 | $self->{value}->set_fontsize ($fsize); |
|
|
1755 | $self->{max} ->set_fontsize ($fsize); |
1598 | } |
1756 | } |
1599 | |
1757 | |
1600 | sub set_value { |
1758 | sub set_value { |
1601 | my ($self, $val, $max) = @_; |
1759 | my ($self, $val, $max) = @_; |
1602 | |
1760 | |
… | |
… | |
1760 | sub new { |
1918 | sub new { |
1761 | my $class = shift; |
1919 | my $class = shift; |
1762 | |
1920 | |
1763 | my $self = $class->SUPER::new ( |
1921 | my $self = $class->SUPER::new ( |
1764 | fontsize => 1, |
1922 | fontsize => 1, |
|
|
1923 | #font => default_font |
1765 | @_, |
1924 | @_, |
1766 | |
1925 | |
1767 | layout => (new CFClient::Layout), |
1926 | layout => (new CFClient::Layout), |
1768 | par => [], |
1927 | par => [], |
1769 | height => 0, |
1928 | height => 0, |
… | |
… | |
1809 | sub size_allocate { |
1968 | sub size_allocate { |
1810 | my ($self, $w, $h) = @_; |
1969 | my ($self, $w, $h) = @_; |
1811 | |
1970 | |
1812 | $self->SUPER::size_allocate ($w, $h); |
1971 | $self->SUPER::size_allocate ($w, $h); |
1813 | |
1972 | |
|
|
1973 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1814 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1974 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1815 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1975 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1816 | |
1976 | |
1817 | $self->reflow; |
1977 | $self->reflow; |
1818 | } |
1978 | } |
… | |
… | |
1870 | my $y1 = $top + $self->{h}; |
2030 | my $y1 = $top + $self->{h}; |
1871 | |
2031 | |
1872 | my $y = 0; |
2032 | my $y = 0; |
1873 | |
2033 | |
1874 | my $layout = $self->{layout}; |
2034 | my $layout = $self->{layout}; |
|
|
2035 | |
|
|
2036 | $layout->set_font ($self->{font}) if $self->{font}; |
1875 | |
2037 | |
1876 | for my $par (@{$self->{par}}) { |
2038 | for my $par (@{$self->{par}}) { |
1877 | my $h = $par->[0]; |
2039 | my $h = $par->[0]; |
1878 | |
2040 | |
1879 | if ($y0 < $y + $h && $y < $y1) { |
2041 | if ($y0 < $y + $h && $y < $y1) { |
… | |
… | |
1959 | my $class = shift; |
2121 | my $class = shift; |
1960 | |
2122 | |
1961 | my $self = $class->SUPER::new ( |
2123 | my $self = $class->SUPER::new ( |
1962 | state => 0, |
2124 | state => 0, |
1963 | connect_activate => \&toggle_flopper, |
2125 | connect_activate => \&toggle_flopper, |
|
|
2126 | can_events => 1, |
1964 | @_ |
2127 | @_ |
1965 | ); |
2128 | ); |
1966 | |
2129 | |
1967 | if ($self->{state}) { |
2130 | if ($self->{state}) { |
1968 | $self->{state} = 0; |
2131 | $self->{state} = 0; |
… | |
… | |
1988 | $self->emit (changed => $self->{state}); |
2151 | $self->emit (changed => $self->{state}); |
1989 | } |
2152 | } |
1990 | |
2153 | |
1991 | ############################################################################# |
2154 | ############################################################################# |
1992 | |
2155 | |
|
|
2156 | package CFClient::UI::Tooltip; |
|
|
2157 | |
|
|
2158 | our @ISA = CFClient::UI::Bin::; |
|
|
2159 | |
|
|
2160 | use CFClient::OpenGL; |
|
|
2161 | |
|
|
2162 | sub new { |
|
|
2163 | my $class = shift; |
|
|
2164 | |
|
|
2165 | $class->SUPER::new ( |
|
|
2166 | @_, |
|
|
2167 | can_events => 0, |
|
|
2168 | ) |
|
|
2169 | } |
|
|
2170 | |
|
|
2171 | sub set_text { |
|
|
2172 | my ($self, $text) = @_; |
|
|
2173 | |
|
|
2174 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
|
|
2175 | $self->{label}->set_text ($text); |
|
|
2176 | $self->add ($self->{label}); |
|
|
2177 | } |
|
|
2178 | |
|
|
2179 | sub size_request { |
|
|
2180 | my ($self) = @_; |
|
|
2181 | |
|
|
2182 | $self->child->set_max_size ($::WIDTH * 0.2); |
|
|
2183 | |
|
|
2184 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2185 | |
|
|
2186 | ($w + 4, $h + 4) |
|
|
2187 | } |
|
|
2188 | |
|
|
2189 | sub _draw { |
|
|
2190 | my ($self) = @_; |
|
|
2191 | |
|
|
2192 | glPushMatrix; |
|
|
2193 | glTranslate 0.375, 0.375; |
|
|
2194 | |
|
|
2195 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2196 | |
|
|
2197 | glColor 1, 0.8, 0.4; |
|
|
2198 | glBegin GL_QUADS; |
|
|
2199 | glVertex 0 , 0; |
|
|
2200 | glVertex 0 , $h; |
|
|
2201 | glVertex $w, $h; |
|
|
2202 | glVertex $w, 0; |
|
|
2203 | glEnd; |
|
|
2204 | |
|
|
2205 | glColor 0, 0, 0; |
|
|
2206 | glBegin GL_LINE_LOOP; |
|
|
2207 | glVertex 0 , 0; |
|
|
2208 | glVertex 0 , $h; |
|
|
2209 | glVertex $w, $h; |
|
|
2210 | glVertex $w, 0; |
|
|
2211 | glEnd; |
|
|
2212 | |
|
|
2213 | glPopMatrix; |
|
|
2214 | |
|
|
2215 | glTranslate 2, 2; |
|
|
2216 | $self->SUPER::_draw; |
|
|
2217 | } |
|
|
2218 | |
|
|
2219 | ############################################################################# |
|
|
2220 | |
1993 | package CFClient::UI::Root; |
2221 | package CFClient::UI::Root; |
1994 | |
2222 | |
1995 | our @ISA = CFClient::UI::Container::; |
2223 | our @ISA = CFClient::UI::Container::; |
1996 | |
2224 | |
1997 | use CFClient::OpenGL; |
2225 | use CFClient::OpenGL; |
1998 | |
2226 | |
1999 | sub check_size { |
2227 | sub check_size { |
2000 | my ($self) = @_; |
2228 | my ($self) = @_; |
2001 | |
2229 | |
2002 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
2230 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
2003 | } |
2231 | } |
2004 | |
2232 | |
2005 | sub size_request { |
2233 | sub size_request { |
2006 | ($::WIDTH, $::HEIGHT) |
2234 | ($::WIDTH, $::HEIGHT) |
2007 | } |
2235 | } |
… | |
… | |
2009 | sub configure { |
2237 | sub configure { |
2010 | my ($self, $x, $y, $w, $h) = @_; |
2238 | my ($self, $x, $y, $w, $h) = @_; |
2011 | |
2239 | |
2012 | $self->SUPER::configure ($x, $y, $w, $h); |
2240 | $self->SUPER::configure ($x, $y, $w, $h); |
2013 | |
2241 | |
2014 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
|
|
2015 | for @{$self->{children}}; |
2242 | for my $child (@{$self->{children}}) { |
|
|
2243 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2244 | |
|
|
2245 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2246 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2247 | $child->configure ($X, $Y, $W,$H); |
|
|
2248 | } |
2016 | } |
2249 | } |
2017 | |
2250 | |
2018 | sub _topleft { |
2251 | sub _topleft { |
2019 | my ($self, $x, $y) = @_; |
2252 | my ($self, $x, $y) = @_; |
2020 | |
2253 | |
… | |
… | |
2028 | ::refresh (); |
2261 | ::refresh (); |
2029 | } |
2262 | } |
2030 | |
2263 | |
2031 | sub add { |
2264 | sub add { |
2032 | my ($self, $child) = @_; |
2265 | my ($self, $child) = @_; |
|
|
2266 | |
|
|
2267 | # integerize window positions |
|
|
2268 | $child->{x} = int $child->{x}; |
|
|
2269 | $child->{y} = int $child->{y}; |
2033 | |
2270 | |
2034 | $self->SUPER::add ($child); |
2271 | $self->SUPER::add ($child); |
2035 | } |
2272 | } |
2036 | |
2273 | |
2037 | sub on_refresh { |
2274 | sub on_refresh { |
… | |
… | |
2063 | ############################################################################# |
2300 | ############################################################################# |
2064 | |
2301 | |
2065 | package CFClient::UI; |
2302 | package CFClient::UI; |
2066 | |
2303 | |
2067 | $ROOT = new CFClient::UI::Root; |
2304 | $ROOT = new CFClient::UI::Root; |
|
|
2305 | $TOOLTIP = new CFClient::UI::Tooltip; |
2068 | |
2306 | |
2069 | 1 |
2307 | 1 |
2070 | |
2308 | |