… | |
… | |
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 (length $widget->{tooltip}) { |
|
|
20 | |
|
|
21 | if ($TOOLTIP->{owner} != $widget) { |
|
|
22 | $TOOLTIP->{owner} = $widget; |
|
|
23 | |
|
|
24 | my $tip = $widget->{tooltip}; |
|
|
25 | |
|
|
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
27 | |
|
|
28 | $TOOLTIP->set_text ($widget->{tooltip}); |
|
|
29 | $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); |
|
|
30 | $TOOLTIP->show; |
|
|
31 | } |
|
|
32 | |
|
|
33 | return; |
|
|
34 | } |
|
|
35 | } |
|
|
36 | } |
|
|
37 | |
|
|
38 | $TOOLTIP->hide; |
|
|
39 | delete $TOOLTIP->{owner}; |
|
|
40 | } |
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_(.*)$/) { |
… | |
… | |
108 | } |
142 | } |
109 | |
143 | |
110 | $self |
144 | $self |
111 | } |
145 | } |
112 | |
146 | |
|
|
147 | sub destroy { |
|
|
148 | my ($self) = @_; |
|
|
149 | |
|
|
150 | $self->hide; |
|
|
151 | %$self = (); |
|
|
152 | } |
|
|
153 | |
113 | sub show { |
154 | sub show { |
114 | my ($self) = @_; |
155 | my ($self) = @_; |
115 | |
156 | |
116 | return if $self->{parent}; |
157 | return if $self->{parent}; |
117 | |
158 | |
… | |
… | |
134 | $self->{z} = $z if defined $z; |
175 | $self->{z} = $z if defined $z; |
135 | |
176 | |
136 | $self->update; |
177 | $self->update; |
137 | } |
178 | } |
138 | |
179 | |
139 | sub needs_redraw { |
180 | sub set_size { |
140 | 0 |
181 | my ($self, $w, $h) = @_; |
|
|
182 | |
|
|
183 | $self->{user_w} = $w; |
|
|
184 | $self->{user_h} = $h; |
|
|
185 | |
|
|
186 | $self->check_size; |
141 | } |
187 | } |
142 | |
188 | |
143 | sub size_request { |
189 | sub size_request { |
144 | require Carp; |
190 | require Carp; |
145 | Carp::confess "size_request is abstract"; |
191 | Carp::confess "size_request is abstract"; |
… | |
… | |
175 | } |
221 | } |
176 | } |
222 | } |
177 | |
223 | |
178 | sub size_allocate { |
224 | sub size_allocate { |
179 | # nothing to be done |
225 | # nothing to be done |
|
|
226 | } |
|
|
227 | |
|
|
228 | sub set_max_size { |
|
|
229 | my ($self, $w, $h) = @_; |
|
|
230 | |
|
|
231 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
|
|
232 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
180 | } |
233 | } |
181 | |
234 | |
182 | # return top left coordinates |
235 | # return top left coordinates |
183 | sub _topleft { |
236 | sub _topleft { |
184 | my ($self, $x, $y) = @_; |
237 | my ($self, $x, $y) = @_; |
… | |
… | |
278 | } |
331 | } |
279 | |
332 | |
280 | sub find_widget { |
333 | sub find_widget { |
281 | my ($self, $x, $y) = @_; |
334 | my ($self, $x, $y) = @_; |
282 | |
335 | |
|
|
336 | return () unless $self->{can_events}; |
|
|
337 | |
283 | return $self |
338 | return $self |
284 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
339 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
285 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
340 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
286 | |
341 | |
287 | () |
342 | () |
… | |
… | |
294 | } |
349 | } |
295 | |
350 | |
296 | sub check_size { |
351 | sub check_size { |
297 | my ($self) = @_; |
352 | my ($self) = @_; |
298 | |
353 | |
299 | my ($w, $h) = $self->size_request; |
354 | $self->{parent} |
|
|
355 | or return 1; |
300 | |
356 | |
|
|
357 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
|
|
358 | ? @$self{qw(user_w user_h)} |
|
|
359 | : $self->size_request; |
|
|
360 | |
301 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
361 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
302 | $self->{req_w} = $w; |
362 | $self->{req_w} = $w; |
303 | $self->{req_h} = $h; |
363 | $self->{req_h} = $h; |
304 | |
364 | |
305 | $self->{parent}->check_size |
365 | $self->{parent}->check_size |
306 | if $self->{parent}; |
366 | or $self->size_allocate ( |
|
|
367 | (List::Util::max $self->{w}, $w), |
|
|
368 | (List::Util::max $self->{h}, $h), |
|
|
369 | ); |
|
|
370 | |
|
|
371 | 1 |
|
|
372 | } else { |
|
|
373 | 0 |
307 | } |
374 | } |
308 | } |
375 | } |
309 | |
376 | |
310 | sub update { |
377 | sub update { |
311 | my ($self) = @_; |
378 | my ($self) = @_; |
… | |
… | |
378 | |
445 | |
379 | package CFClient::UI::Empty; |
446 | package CFClient::UI::Empty; |
380 | |
447 | |
381 | our @ISA = CFClient::UI::Base::; |
448 | our @ISA = CFClient::UI::Base::; |
382 | |
449 | |
|
|
450 | sub new { |
|
|
451 | my ($class, %arg) = @_; |
|
|
452 | $class->SUPER::new (can_events => 0, %arg); |
|
|
453 | } |
|
|
454 | |
383 | sub size_request { |
455 | sub size_request { |
384 | (0, 0) |
456 | (0, 0) |
385 | } |
457 | } |
386 | |
458 | |
387 | sub draw { } |
459 | sub draw { } |
… | |
… | |
395 | sub new { |
467 | sub new { |
396 | my ($class, %arg) = @_; |
468 | my ($class, %arg) = @_; |
397 | |
469 | |
398 | my $children = delete $arg{children} || []; |
470 | my $children = delete $arg{children} || []; |
399 | |
471 | |
400 | my $self = $class->SUPER::new (children => [], %arg); |
472 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
401 | $self->add ($_) for @$children; |
473 | $self->add ($_) for @$children; |
402 | |
474 | |
403 | $self |
475 | $self |
404 | } |
476 | } |
405 | |
477 | |
… | |
… | |
424 | delete $child->{parent}; |
496 | delete $child->{parent}; |
425 | |
497 | |
426 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
498 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
427 | |
499 | |
428 | $self->check_size; |
500 | $self->check_size; |
|
|
501 | $self->update; |
429 | } |
502 | } |
430 | |
503 | |
431 | sub find_widget { |
504 | sub find_widget { |
432 | my ($self, $x, $y) = @_; |
505 | my ($self, $x, $y) = @_; |
433 | |
506 | |
… | |
… | |
561 | sub new { die } |
634 | sub new { die } |
562 | |
635 | |
563 | sub size_request { |
636 | sub size_request { |
564 | my ($self) = @_; |
637 | my ($self) = @_; |
565 | |
638 | |
566 | @$self{qw(child_w child_h)} = $self->child->size_request; |
639 | @$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)}); |
640 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
568 | |
641 | |
569 | @$self{qw(child_w child_h)} |
642 | @$self{qw(child_w child_h)} |
570 | } |
643 | } |
571 | |
644 | |
… | |
… | |
581 | package CFClient::UI::Frame; |
654 | package CFClient::UI::Frame; |
582 | |
655 | |
583 | our @ISA = CFClient::UI::Bin::; |
656 | our @ISA = CFClient::UI::Bin::; |
584 | |
657 | |
585 | use CFClient::OpenGL; |
658 | 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 $_, mipmap => 1 } |
|
|
631 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
632 | |
659 | |
633 | sub new { |
660 | sub new { |
634 | my $class = shift; |
661 | my $class = shift; |
635 | |
|
|
636 | # TODO: user_x, user_y, overwrite moveto? |
|
|
637 | |
662 | |
638 | my $self = $class->SUPER::new ( |
663 | my $self = $class->SUPER::new ( |
639 | bg => [1, 1, 1, 1], |
664 | bg => [1, 1, 1, 1], |
640 | border_bg => [1, 1, 1, 1], |
665 | border_bg => [1, 1, 1, 1], |
641 | border => 0.8, |
666 | border => 0.8, |
642 | @_ |
667 | @_ |
643 | ); |
668 | ); |
644 | |
669 | |
|
|
670 | $self |
|
|
671 | } |
|
|
672 | |
|
|
673 | sub _draw { |
|
|
674 | my ($self) = @_; |
|
|
675 | |
|
|
676 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
677 | |
|
|
678 | glEnable GL_BLEND; |
|
|
679 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
680 | glEnable GL_TEXTURE_2D; |
|
|
681 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
682 | |
|
|
683 | # glBegin GL_QUADS; |
|
|
684 | # glColor 0, 0, 0, 0; |
|
|
685 | # glVertex 0 , 0; |
|
|
686 | # glVertex 0 , $h; |
|
|
687 | # glVertex $w, $h; |
|
|
688 | # glVertex $w, 0; |
|
|
689 | # glEnd; |
|
|
690 | |
|
|
691 | |
|
|
692 | $self->child->draw; |
|
|
693 | glDisable GL_BLEND; |
|
|
694 | glDisable GL_TEXTURE_2D; |
|
|
695 | } |
|
|
696 | |
|
|
697 | ############################################################################# |
|
|
698 | |
|
|
699 | package CFClient::UI::FancyFrame; |
|
|
700 | |
|
|
701 | our @ISA = CFClient::UI::Bin::; |
|
|
702 | |
|
|
703 | use CFClient::OpenGL; |
|
|
704 | |
|
|
705 | my @tex = |
|
|
706 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
707 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
708 | |
|
|
709 | sub new { |
|
|
710 | my $class = shift; |
|
|
711 | |
|
|
712 | # TODO: user_x, user_y, overwrite moveto? |
|
|
713 | |
|
|
714 | my $self = $class->SUPER::new ( |
|
|
715 | bg => [1, 1, 1, 1], |
|
|
716 | border_bg => [1, 1, 1, 1], |
|
|
717 | border => 0.8, |
|
|
718 | can_events => 1, |
|
|
719 | @_ |
|
|
720 | ); |
|
|
721 | |
645 | $self->{title} &&= new CFClient::UI::Label |
722 | $self->{title} &&= new CFClient::UI::Label |
646 | align => 0, |
723 | align => 0, |
647 | valign => 1, |
724 | valign => 1, |
648 | text => $self->{title}, |
725 | text => $self->{title}, |
649 | fontsize => 1; |
726 | fontsize => 1; |
… | |
… | |
656 | } |
733 | } |
657 | |
734 | |
658 | sub size_request { |
735 | sub size_request { |
659 | my ($self) = @_; |
736 | my ($self) = @_; |
660 | |
737 | |
661 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
662 | |
|
|
663 | my ($w, $h) = $self->SUPER::size_request; |
738 | my ($w, $h) = $self->SUPER::size_request; |
664 | |
739 | |
665 | ( |
740 | ( |
666 | $w + $self->border * 2, |
741 | $w + $self->border * 2, |
667 | $h + $self->border * 2, |
742 | $h + $self->border * 2, |
… | |
… | |
815 | or next; |
890 | or next; |
816 | |
891 | |
817 | for my $x (0 .. $#$row) { |
892 | for my $x (0 .. $#$row) { |
818 | my $widget = $row->[$x] |
893 | my $widget = $row->[$x] |
819 | or next; |
894 | or next; |
820 | my ($w, $h) = $widget->size_request; |
895 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
821 | |
896 | |
822 | $w[$x] = max $w[$x], $w; |
897 | $w[$x] = max $w[$x], $w; |
823 | $h[$y] = max $h[$y], $h; |
898 | $h[$y] = max $h[$y], $h; |
824 | } |
899 | } |
825 | } |
900 | } |
… | |
… | |
930 | |
1005 | |
931 | ($h, $w) = ($w, $h); |
1006 | ($h, $w) = ($w, $h); |
932 | |
1007 | |
933 | my $children = $self->{children}; |
1008 | my $children = $self->{children}; |
934 | |
1009 | |
935 | my @h = map +($_->size_request)[0], @$children; |
1010 | my @h = map $_->{req_w}, @$children; |
936 | |
1011 | |
937 | my $req_h = List::Util::sum @h; |
1012 | my $req_h = List::Util::sum @h; |
938 | |
1013 | |
939 | if ($req_h > $h) { |
1014 | if ($req_h > $h) { |
940 | # ah well, not enough space |
1015 | # ah well, not enough space |
… | |
… | |
988 | sub size_allocate { |
1063 | sub size_allocate { |
989 | my ($self, $w, $h) = @_; |
1064 | my ($self, $w, $h) = @_; |
990 | |
1065 | |
991 | my $children = $self->{children}; |
1066 | my $children = $self->{children}; |
992 | |
1067 | |
993 | my @h = map +($_->size_request)[1], @$children; |
1068 | my @h = map $_->{req_h}, @$children; |
994 | |
1069 | |
995 | my $req_h = List::Util::sum @h; |
1070 | my $req_h = List::Util::sum @h; |
996 | |
1071 | |
997 | if ($req_h > $h) { |
1072 | if ($req_h > $h) { |
998 | # ah well, not enough space |
1073 | # ah well, not enough space |
… | |
… | |
1033 | sub new { |
1108 | sub new { |
1034 | my ($class, %arg) = @_; |
1109 | my ($class, %arg) = @_; |
1035 | |
1110 | |
1036 | my $self = $class->SUPER::new ( |
1111 | my $self = $class->SUPER::new ( |
1037 | fg => [1, 1, 1], |
1112 | fg => [1, 1, 1], |
|
|
1113 | #font => default_font |
1038 | fontsize => 1, |
1114 | fontsize => 1, |
1039 | text => "", |
1115 | text => "", |
1040 | align => -1, |
1116 | align => -1, |
1041 | valign => -1, |
1117 | valign => -1, |
1042 | padding => 2, |
1118 | padding => 2, |
1043 | layout => new CFClient::Layout, |
1119 | layout => new CFClient::Layout, |
|
|
1120 | can_events => 0, |
1044 | %arg |
1121 | %arg |
1045 | ); |
1122 | ); |
1046 | |
1123 | |
1047 | if (exists $self->{template}) { |
1124 | if (exists $self->{template}) { |
1048 | my $layout = new CFClient::Layout; |
1125 | my $layout = new CFClient::Layout; |
… | |
… | |
1070 | my ($self, $text) = @_; |
1147 | my ($self, $text) = @_; |
1071 | |
1148 | |
1072 | $self->{layout}->set_text ($text); |
1149 | $self->{layout}->set_text ($text); |
1073 | |
1150 | |
1074 | delete $self->{texture}; |
1151 | delete $self->{texture}; |
|
|
1152 | $self->check_size; |
1075 | $self->update; |
1153 | $self->update; |
1076 | } |
1154 | } |
1077 | |
1155 | |
1078 | sub set_markup { |
1156 | sub set_markup { |
1079 | my ($self, $markup) = @_; |
1157 | my ($self, $markup) = @_; |
1080 | |
1158 | |
1081 | $self->{layout}->set_markup ($markup); |
1159 | $self->{layout}->set_markup ($markup); |
1082 | |
1160 | |
1083 | delete $self->{texture}; |
1161 | delete $self->{texture}; |
|
|
1162 | $self->check_size; |
1084 | $self->update; |
1163 | $self->update; |
1085 | } |
1164 | } |
1086 | |
1165 | |
1087 | sub size_request { |
1166 | sub size_request { |
1088 | my ($self) = @_; |
1167 | my ($self) = @_; |
1089 | |
1168 | |
1090 | $self->{layout}->set_width; |
1169 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1170 | $self->{layout}->set_width ($self->{max_w} || -1); |
1091 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1171 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1092 | |
1172 | |
1093 | my ($w, $h) = $self->{layout}->size; |
1173 | my ($w, $h) = $self->{layout}->size; |
1094 | |
1174 | |
1095 | if (exists $self->{template}) { |
1175 | if (exists $self->{template}) { |
|
|
1176 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1096 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1177 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1097 | |
1178 | |
1098 | my ($w2, $h2) = $self->{template}->size; |
1179 | my ($w2, $h2) = $self->{template}->size; |
1099 | |
1180 | |
1100 | $w = List::Util::max $w, $w2; |
1181 | $w = List::Util::max $w, $w2; |
… | |
… | |
1115 | |
1196 | |
1116 | sub set_fontsize { |
1197 | sub set_fontsize { |
1117 | my ($self, $fontsize) = @_; |
1198 | my ($self, $fontsize) = @_; |
1118 | |
1199 | |
1119 | $self->{fontsize} = $fontsize; |
1200 | $self->{fontsize} = $fontsize; |
|
|
1201 | delete $self->{texture}; |
1120 | $self->check_size; |
1202 | $self->check_size; |
|
|
1203 | $self->update; |
1121 | } |
1204 | } |
1122 | |
1205 | |
1123 | sub _draw { |
1206 | sub _draw { |
1124 | my ($self) = @_; |
1207 | my ($self) = @_; |
1125 | |
1208 | |
1126 | my $tex = $self->{texture} ||= do { |
1209 | my $tex = $self->{texture} ||= do { |
|
|
1210 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1127 | $self->{layout}->set_width ($self->{w}); |
1211 | $self->{layout}->set_width ($self->{w}); |
1128 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1212 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1129 | new_from_layout CFClient::Texture $self->{layout} |
1213 | new_from_layout CFClient::Texture $self->{layout} |
1130 | }; |
1214 | }; |
1131 | |
1215 | |
… | |
… | |
1171 | active_bg => [1, 1, 1, 0.5], |
1255 | active_bg => [1, 1, 1, 0.5], |
1172 | active_fg => [0, 0, 0], |
1256 | active_fg => [0, 0, 0], |
1173 | can_hover => 1, |
1257 | can_hover => 1, |
1174 | can_focus => 1, |
1258 | can_focus => 1, |
1175 | valign => 0, |
1259 | valign => 0, |
|
|
1260 | can_events => 1, |
1176 | @_ |
1261 | @_ |
1177 | ) |
1262 | ) |
1178 | } |
1263 | } |
1179 | |
1264 | |
1180 | sub _set_text { |
1265 | sub _set_text { |
… | |
… | |
1208 | } |
1293 | } |
1209 | |
1294 | |
1210 | sub size_allocate { |
1295 | sub size_allocate { |
1211 | my ($self, $w, $h) = @_; |
1296 | my ($self, $w, $h) = @_; |
1212 | |
1297 | |
1213 | $self->_set_text ($self->{text}); |
1298 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1214 | } |
1299 | } |
1215 | |
1300 | |
1216 | sub set_text { |
1301 | sub set_text { |
1217 | my ($self, $text) = @_; |
1302 | my ($self, $text) = @_; |
1218 | |
1303 | |
… | |
… | |
1365 | bg => [1, 1, 1, 0.2], |
1450 | bg => [1, 1, 1, 0.2], |
1366 | active_fg => [0, 0, 1], |
1451 | active_fg => [0, 0, 1], |
1367 | can_hover => 1, |
1452 | can_hover => 1, |
1368 | align => 0, |
1453 | align => 0, |
1369 | valign => 0, |
1454 | valign => 0, |
|
|
1455 | can_events => 1, |
1370 | @_ |
1456 | @_ |
1371 | ) |
1457 | ) |
1372 | } |
1458 | } |
1373 | |
1459 | |
1374 | sub button_up { |
1460 | sub button_up { |
… | |
… | |
1479 | our %loaded_images; |
1565 | our %loaded_images; |
1480 | |
1566 | |
1481 | sub new { |
1567 | sub new { |
1482 | my $class = shift; |
1568 | my $class = shift; |
1483 | |
1569 | |
1484 | my $self = $class->SUPER::new (@_); |
1570 | my $self = $class->SUPER::new (can_events => 0, @_); |
1485 | |
1571 | |
1486 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
1572 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
1487 | |
1573 | |
1488 | $loaded_images{$self->{image}} ||= |
1574 | $loaded_images{$self->{image}} ||= |
1489 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
1575 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
… | |
… | |
1531 | ############################################################################# |
1617 | ############################################################################# |
1532 | |
1618 | |
1533 | package CFClient::UI::VGauge; |
1619 | package CFClient::UI::VGauge; |
1534 | |
1620 | |
1535 | our @ISA = CFClient::UI::Base::; |
1621 | our @ISA = CFClient::UI::Base::; |
|
|
1622 | |
|
|
1623 | use List::Util qw(min max); |
1536 | |
1624 | |
1537 | use CFClient::OpenGL; |
1625 | use CFClient::OpenGL; |
1538 | |
1626 | |
1539 | my %tex = ( |
1627 | my %tex = ( |
1540 | food => [ |
1628 | food => [ |
1541 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1629 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1542 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
1630 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
1543 | ], |
1631 | ], |
1544 | grace => [ |
1632 | grace => [ |
1545 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1633 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1546 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
1634 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
1547 | ], |
1635 | ], |
1548 | hp => [ |
1636 | hp => [ |
1549 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1637 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1550 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
1638 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
1551 | ], |
1639 | ], |
1552 | mana => [ |
1640 | mana => [ |
1553 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1641 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1554 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
1642 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
1555 | ], |
1643 | ], |
1556 | ); |
1644 | ); |
1557 | |
1645 | |
1558 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
1646 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
1559 | sub new { |
1647 | sub new { |
… | |
… | |
1597 | |
1685 | |
1598 | sub _draw { |
1686 | sub _draw { |
1599 | my ($self) = @_; |
1687 | my ($self) = @_; |
1600 | |
1688 | |
1601 | my $tex = $tex{$self->{type}}; |
1689 | my $tex = $tex{$self->{type}}; |
|
|
1690 | my ($t1, $t2, $t3) = @$tex; |
1602 | |
1691 | |
1603 | my ($w, $h) = ($self->{w}, $self->{h}); |
1692 | my ($w, $h) = ($self->{w}, $self->{h}); |
1604 | |
1693 | |
1605 | if ($self->{vertical}) { |
1694 | if ($self->{vertical}) { |
1606 | glRotate 90, 0, 0, 1; |
1695 | glRotate 90, 0, 0, 1; |
… | |
… | |
1608 | |
1697 | |
1609 | ($w, $h) = ($h, $w); |
1698 | ($w, $h) = ($h, $w); |
1610 | } |
1699 | } |
1611 | |
1700 | |
1612 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
1701 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
1613 | $ycut = 1 if $self->{val} > $self->{max_val}; |
|
|
1614 | |
1702 | |
1615 | my $t1 = $tex->[0]; |
1703 | my $ycut1 = max 0, min 1, $ycut; |
1616 | my $t2 = $tex->[1]; |
1704 | my $ycut2 = max 0, min 1, $ycut - 1; |
|
|
1705 | |
|
|
1706 | my $h1 = $self->{h} * (1 - $ycut1); |
|
|
1707 | my $h2 = $self->{h} * (1 - $ycut2); |
1617 | |
1708 | |
1618 | glEnable GL_BLEND; |
1709 | glEnable GL_BLEND; |
1619 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1710 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1620 | glEnable GL_TEXTURE_2D; |
1711 | glEnable GL_TEXTURE_2D; |
1621 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1712 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1622 | |
1713 | |
1623 | my $h1 = $self->{h} - $ycut * $self->{h}; |
|
|
1624 | my $h2 = $ycut * $self->{h}; |
|
|
1625 | |
|
|
1626 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
1714 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
1627 | glBegin GL_QUADS; |
1715 | glBegin GL_QUADS; |
1628 | glTexCoord 0 , 0; glVertex 0 , 0; |
1716 | glTexCoord 0 , 0; glVertex 0 , 0; |
1629 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; |
1717 | glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1; |
1630 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; |
1718 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1; |
1631 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
1719 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
1632 | glEnd; |
1720 | glEnd; |
1633 | |
1721 | |
|
|
1722 | my $ycut1 = List::Util::min 1, $ycut; |
1634 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
1723 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
1635 | glBegin GL_QUADS; |
1724 | glBegin GL_QUADS; |
1636 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; |
1725 | glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1; |
1637 | glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; |
1726 | glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2; |
1638 | glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; |
1727 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2; |
1639 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; |
1728 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1; |
1640 | glEnd; |
1729 | glEnd; |
|
|
1730 | |
|
|
1731 | if ($t3) { |
|
|
1732 | glBindTexture GL_TEXTURE_2D, $t3->{name}; |
|
|
1733 | glBegin GL_QUADS; |
|
|
1734 | glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1735 | glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; |
|
|
1736 | glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; |
|
|
1737 | glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1738 | glEnd; |
|
|
1739 | } |
1641 | |
1740 | |
1642 | glDisable GL_BLEND; |
1741 | glDisable GL_BLEND; |
1643 | glDisable GL_TEXTURE_2D; |
1742 | glDisable GL_TEXTURE_2D; |
1644 | } |
1743 | } |
1645 | |
1744 | |
… | |
… | |
1648 | package CFClient::UI::Gauge; |
1747 | package CFClient::UI::Gauge; |
1649 | |
1748 | |
1650 | our @ISA = CFClient::UI::VBox::; |
1749 | our @ISA = CFClient::UI::VBox::; |
1651 | |
1750 | |
1652 | sub new { |
1751 | sub new { |
1653 | my ($class, %arg) = shift; |
1752 | my ($class, %arg) = @_; |
1654 | |
1753 | |
1655 | my $self = $class->SUPER::new ( |
1754 | my $self = $class->SUPER::new ( |
1656 | @_, |
1755 | tooltip => $arg{type}, |
|
|
1756 | can_events => 1, |
|
|
1757 | can_hover => 1, |
|
|
1758 | %arg, |
1657 | ); |
1759 | ); |
1658 | |
1760 | |
1659 | $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
1761 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); |
1660 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); |
1762 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
1661 | $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
1763 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); |
1662 | |
1764 | |
1663 | $self |
1765 | $self |
1664 | } |
1766 | } |
1665 | |
1767 | |
1666 | sub set_fontsize { |
1768 | sub set_fontsize { |
… | |
… | |
1833 | sub new { |
1935 | sub new { |
1834 | my $class = shift; |
1936 | my $class = shift; |
1835 | |
1937 | |
1836 | my $self = $class->SUPER::new ( |
1938 | my $self = $class->SUPER::new ( |
1837 | fontsize => 1, |
1939 | fontsize => 1, |
|
|
1940 | #font => default_font |
1838 | @_, |
1941 | @_, |
1839 | |
1942 | |
1840 | layout => (new CFClient::Layout), |
1943 | layout => (new CFClient::Layout), |
1841 | par => [], |
1944 | par => [], |
1842 | height => 0, |
1945 | height => 0, |
… | |
… | |
1882 | sub size_allocate { |
1985 | sub size_allocate { |
1883 | my ($self, $w, $h) = @_; |
1986 | my ($self, $w, $h) = @_; |
1884 | |
1987 | |
1885 | $self->SUPER::size_allocate ($w, $h); |
1988 | $self->SUPER::size_allocate ($w, $h); |
1886 | |
1989 | |
|
|
1990 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1887 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1991 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1888 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1992 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1889 | |
1993 | |
1890 | $self->reflow; |
1994 | $self->reflow; |
1891 | } |
1995 | } |
… | |
… | |
1943 | my $y1 = $top + $self->{h}; |
2047 | my $y1 = $top + $self->{h}; |
1944 | |
2048 | |
1945 | my $y = 0; |
2049 | my $y = 0; |
1946 | |
2050 | |
1947 | my $layout = $self->{layout}; |
2051 | my $layout = $self->{layout}; |
|
|
2052 | |
|
|
2053 | $layout->set_font ($self->{font}) if $self->{font}; |
1948 | |
2054 | |
1949 | for my $par (@{$self->{par}}) { |
2055 | for my $par (@{$self->{par}}) { |
1950 | my $h = $par->[0]; |
2056 | my $h = $par->[0]; |
1951 | |
2057 | |
1952 | if ($y0 < $y + $h && $y < $y1) { |
2058 | if ($y0 < $y + $h && $y < $y1) { |
… | |
… | |
2032 | my $class = shift; |
2138 | my $class = shift; |
2033 | |
2139 | |
2034 | my $self = $class->SUPER::new ( |
2140 | my $self = $class->SUPER::new ( |
2035 | state => 0, |
2141 | state => 0, |
2036 | connect_activate => \&toggle_flopper, |
2142 | connect_activate => \&toggle_flopper, |
|
|
2143 | can_events => 1, |
2037 | @_ |
2144 | @_ |
2038 | ); |
2145 | ); |
2039 | |
2146 | |
2040 | if ($self->{state}) { |
2147 | if ($self->{state}) { |
2041 | $self->{state} = 0; |
2148 | $self->{state} = 0; |
… | |
… | |
2061 | $self->emit (changed => $self->{state}); |
2168 | $self->emit (changed => $self->{state}); |
2062 | } |
2169 | } |
2063 | |
2170 | |
2064 | ############################################################################# |
2171 | ############################################################################# |
2065 | |
2172 | |
|
|
2173 | package CFClient::UI::Tooltip; |
|
|
2174 | |
|
|
2175 | our @ISA = CFClient::UI::Bin::; |
|
|
2176 | |
|
|
2177 | use CFClient::OpenGL; |
|
|
2178 | |
|
|
2179 | sub new { |
|
|
2180 | my $class = shift; |
|
|
2181 | |
|
|
2182 | $class->SUPER::new ( |
|
|
2183 | @_, |
|
|
2184 | can_events => 0, |
|
|
2185 | ) |
|
|
2186 | } |
|
|
2187 | |
|
|
2188 | sub set_text { |
|
|
2189 | my ($self, $text) = @_; |
|
|
2190 | |
|
|
2191 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
|
|
2192 | $self->{label}->set_text ($text); |
|
|
2193 | $self->add ($self->{label}); |
|
|
2194 | } |
|
|
2195 | |
|
|
2196 | sub size_request { |
|
|
2197 | my ($self) = @_; |
|
|
2198 | |
|
|
2199 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2200 | |
|
|
2201 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2202 | |
|
|
2203 | ($w + 4, $h + 4) |
|
|
2204 | } |
|
|
2205 | |
|
|
2206 | sub _draw { |
|
|
2207 | my ($self) = @_; |
|
|
2208 | |
|
|
2209 | glPushMatrix; |
|
|
2210 | glTranslate 0.375, 0.375; |
|
|
2211 | |
|
|
2212 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2213 | |
|
|
2214 | glColor 1, 0.8, 0.4; |
|
|
2215 | glBegin GL_QUADS; |
|
|
2216 | glVertex 0 , 0; |
|
|
2217 | glVertex 0 , $h; |
|
|
2218 | glVertex $w, $h; |
|
|
2219 | glVertex $w, 0; |
|
|
2220 | glEnd; |
|
|
2221 | |
|
|
2222 | glColor 0, 0, 0; |
|
|
2223 | glBegin GL_LINE_LOOP; |
|
|
2224 | glVertex 0 , 0; |
|
|
2225 | glVertex 0 , $h; |
|
|
2226 | glVertex $w, $h; |
|
|
2227 | glVertex $w, 0; |
|
|
2228 | glEnd; |
|
|
2229 | |
|
|
2230 | glPopMatrix; |
|
|
2231 | |
|
|
2232 | glTranslate 2, 2; |
|
|
2233 | $self->SUPER::_draw; |
|
|
2234 | } |
|
|
2235 | |
|
|
2236 | ############################################################################# |
|
|
2237 | |
2066 | package CFClient::UI::Root; |
2238 | package CFClient::UI::Root; |
2067 | |
2239 | |
2068 | our @ISA = CFClient::UI::Container::; |
2240 | our @ISA = CFClient::UI::Container::; |
2069 | |
2241 | |
2070 | use CFClient::OpenGL; |
2242 | use CFClient::OpenGL; |
2071 | |
2243 | |
2072 | sub check_size { |
2244 | sub check_size { |
2073 | my ($self) = @_; |
2245 | my ($self) = @_; |
2074 | |
2246 | |
2075 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
2247 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
2076 | } |
2248 | } |
2077 | |
2249 | |
2078 | sub size_request { |
2250 | sub size_request { |
2079 | ($::WIDTH, $::HEIGHT) |
2251 | ($::WIDTH, $::HEIGHT) |
2080 | } |
2252 | } |
… | |
… | |
2082 | sub configure { |
2254 | sub configure { |
2083 | my ($self, $x, $y, $w, $h) = @_; |
2255 | my ($self, $x, $y, $w, $h) = @_; |
2084 | |
2256 | |
2085 | $self->SUPER::configure ($x, $y, $w, $h); |
2257 | $self->SUPER::configure ($x, $y, $w, $h); |
2086 | |
2258 | |
2087 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
|
|
2088 | for @{$self->{children}}; |
2259 | for my $child (@{$self->{children}}) { |
|
|
2260 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2261 | |
|
|
2262 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2263 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2264 | $child->configure ($X, $Y, $W,$H); |
|
|
2265 | } |
2089 | } |
2266 | } |
2090 | |
2267 | |
2091 | sub _topleft { |
2268 | sub _topleft { |
2092 | my ($self, $x, $y) = @_; |
2269 | my ($self, $x, $y) = @_; |
2093 | |
2270 | |
… | |
… | |
2140 | ############################################################################# |
2317 | ############################################################################# |
2141 | |
2318 | |
2142 | package CFClient::UI; |
2319 | package CFClient::UI; |
2143 | |
2320 | |
2144 | $ROOT = new CFClient::UI::Root; |
2321 | $ROOT = new CFClient::UI::Root; |
|
|
2322 | $TOOLTIP = new CFClient::UI::Tooltip; |
2145 | |
2323 | |
2146 | 1 |
2324 | 1 |
2147 | |
2325 | |