ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.112 by root, Sat Apr 15 12:29:46 2006 UTC vs.
Revision 1.127 by root, Mon Apr 17 21:03:31 2006 UTC

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->button - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60 60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover && $hover->{can_hover}; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 71}
72 72
73# convert position array to integers 73# convert position array to integers
74sub harmonize { 74sub harmonize {
75 my ($vals) = @_; 75 my ($vals) = @_;
76 76
77 my $rem = 0; 77 my $rem = 0;
78 78
79 for ($vals) { 79 for (@$vals) {
80 my $i = int $_ + $rem; 80 my $i = int $_ + $rem;
81 $rem += $_ - $i; 81 $rem += $_ - $i;
82 $_ = $i; 82 $_ = $i;
83 } 83 }
84} 84}
112 $self 112 $self
113} 113}
114 114
115sub move { 115sub move {
116 my ($self, $x, $y, $z) = @_; 116 my ($self, $x, $y, $z) = @_;
117 $self->{x} = $x; 117 $self->{x} = int $x;
118 $self->{y} = $y; 118 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 119 $self->{z} = $z if defined $z;
120} 120}
121 121
122sub needs_redraw { 122sub needs_redraw {
123 0 123 0
146 my ($self, $x, $y, $w, $h) = @_; 146 my ($self, $x, $y, $w, $h) = @_;
147 147
148 $self->_size_allocate ($x, $y, $w, $h); 148 $self->_size_allocate ($x, $y, $w, $h);
149} 149}
150 150
151# return top left coordinates
152sub _topleft {
153 my ($self, $x, $y) = @_;
154
155 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
156}
157
151# translate global coordinates to local coordinate system 158# translate global coordinates to local coordinate system
152sub translate { 159sub coord2local {
153 my ($self, $x, $y) = @_; 160 my ($self, $x, $y) = @_;
154 161
155 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 162 my ($X, $Y) = $self->_topleft;
163 ($x - $X, $y - $Y)
164}
165
166# translate local coordinates to global coordinate system
167sub coord2global {
168 my ($self, $x, $y) = @_;
169
170 my ($X, $Y) = $self->_topleft;
171 ($x + $X, $y + $Y)
156} 172}
157 173
158sub focus_in { 174sub focus_in {
159 my ($self) = @_; 175 my ($self) = @_;
160 176
161 return if $FOCUS == $self; 177 return if $FOCUS == $self;
162 return unless $self->{can_focus}; 178 return unless $self->{can_focus};
163 179
164 my $focus = $FOCUS; $FOCUS = $self; 180 my $focus = $FOCUS; $FOCUS = $self;
181
182 $self->emit (focus_in => $focus);
183
165 $focus->update if $focus; 184 $focus->update if $focus;
166 $FOCUS->update; 185 $FOCUS->update;
167} 186}
168 187
169sub focus_out { 188sub focus_out {
170 my ($self) = @_; 189 my ($self) = @_;
171 190
172 return unless $FOCUS == $self; 191 return unless $FOCUS == $self;
173 192
174 my $focus = $FOCUS; undef $FOCUS; 193 my $focus = $FOCUS; undef $FOCUS;
194
195 $self->emit (focus_out => $focus);
196
175 $focus->update if $focus; #? 197 $focus->update if $focus; #?
176} 198}
177 199
178sub mouse_motion { } 200sub mouse_motion { }
179sub button_up { } 201sub button_up { }
203 glPopMatrix; 225 glPopMatrix;
204 226
205 if ($self == $HOVER && $self->{can_hover}) { 227 if ($self == $HOVER && $self->{can_hover}) {
206 my ($x, $y) = @$self{qw(x y)}; 228 my ($x, $y) = @$self{qw(x y)};
207 229
208 glColor 0, 0, 1, 0.2; 230 glColor 1, 0.8, 0.5, 0.2;
209 glEnable GL_BLEND; 231 glEnable GL_BLEND;
210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
211 glBegin GL_QUADS; 233 glBegin GL_QUADS;
212 glVertex $x , $y; 234 glVertex $x , $y;
213 glVertex $x + $self->{w}, $y; 235 glVertex $x + $self->{w}, $y;
233 255
234 () 256 ()
235} 257}
236 258
237sub set_parent { 259sub set_parent {
238 my ($self, $par) = @_; 260 my ($self, $parent) = @_;
239 261
240 $self->{parent} = $par;
241 Scalar::Util::weaken $self->{parent}; 262 Scalar::Util::weaken ($self->{parent} = $parent);
242} 263}
243 264
244sub get_parent { 265sub check_size {
245 $_[0]->{parent} 266 my ($self) = @_;
267
268 my ($w, $h) = $self->size_request;
269
270 if ($w != $self->{req_w} || $h != $self->{req_h}) {
271 $self->{req_w} = $w;
272 $self->{req_h} = $h;
273
274 $self->{parent}->check_size
275 if $self->{parent};
276 }
246} 277}
247 278
248sub update { 279sub update {
249 my ($self) = @_; 280 my ($self) = @_;
250 281
339 370
340 $self 371 $self
341} 372}
342 373
343sub add { 374sub add {
344 my ($self, $chld) = @_; 375 my ($self, $child) = @_;
345 376
346 $chld->set_parent ($self); 377 $child->set_parent ($self);
378
379 use sort 'stable';
347 380
348 $self->{children} = [ 381 $self->{children} = [
349 sort { $a->{z} <=> $b->{z} } 382 sort { $a->{z} <=> $b->{z} }
350 @{$self->{children}}, $chld 383 @{$self->{children}}, $child
351 ]; 384 ];
352 385
353 $self->{w} = $self->{h} = -1; 386 $self->{w} = $self->{h} = -1;
354 $self->update; 387
388 $child->check_size;
355} 389}
356 390
357sub remove { 391sub remove {
358 my ($self, $widget) = @_; 392 my ($self, $widget) = @_;
359 393
360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 394 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
361 395
362 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 396 $self->check_size;
363} 397}
364 398
365sub find_widget { 399sub find_widget {
366 my ($self, $x, $y) = @_; 400 my ($self, $x, $y) = @_;
367 401
487 $tex->draw_quad (0, 0, $w, $h); 521 $tex->draw_quad (0, 0, $w, $h);
488 522
489 glDisable GL_BLEND; 523 glDisable GL_BLEND;
490 glDisable GL_TEXTURE_2D; 524 glDisable GL_TEXTURE_2D;
491} 525}
526
527#############################################################################
528
529package CFClient::UI::ViewPort;
530
531our @ISA = CFClient::UI::Window::;
532
533sub new { die }
534
535sub size_request {
536 my ($self) = @_;
537
538 @$self{qw(child_w child_h)} = $self->child->size_request;
539 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
540
541 @$self{qw(child_w child_h)}
542}
543
544sub size_allocate {
545 my ($self, $x, $y, $w, $h) = @_;
546
547 $self->_size_allocate ($x, $y, $w, $h) or return;
548}
549
550sub _draw {
551 my ($self) = @_;
552
553 $self->{children}[1]->draw;
554}
555
492 556
493############################################################################# 557#############################################################################
494 558
495package CFClient::UI::Frame; 559package CFClient::UI::Frame;
496 560
552 # TODO: user_x, user_y, overwrite moveto? 616 # TODO: user_x, user_y, overwrite moveto?
553 617
554 $class->SUPER::new ( 618 $class->SUPER::new (
555 bg => [1, 1, 1, 1], 619 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 620 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 621 border => int $::FONTSIZE * 0.8,
558 @_ 622 @_
559 ) 623 )
560} 624}
561 625
562sub size_request { 626sub size_request {
685 @_ 749 @_
686 ) 750 )
687} 751}
688 752
689sub add { 753sub add {
690 my ($self, $x, $y, $chld) = @_; 754 my ($self, $x, $y, $child) = @_;
691 755
756 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 757 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 758
695 $self->{w} = $self->{h} = -1; 759 $self->{w} = $self->{h} = -1;
760
761 $child->check_size;
762}
763
764# TODO: move to container class maybe? send childs a signal on removal?
765sub clear {
766 my ($self) = @_;
767
768 delete $self->{children};
696 $self->update; 769 $self->update;
697} 770}
698 771
699sub get_wh { 772sub get_wh {
700 my ($self) = @_; 773 my ($self) = @_;
819} 892}
820 893
821sub size_allocate { 894sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_; 895 my ($self, $x, $y, $w, $h) = @_;
823 896
824 $self->_size_allocate ($x, $y, $w, $h); 897 $self->_size_allocate ($x, $y, $w, $h) or return;
825 898
826 ($h, $w) = ($w, $h); 899 ($h, $w) = ($w, $h);
827 900
828 my $children = $self->{children}; 901 my $children = $self->{children};
829 902
855 my $h = $h[$_]; 928 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w); 929 $child->size_allocate ($y, 0, $h, $w);
857 930
858 $y += $h; 931 $y += $h;
859 } 932 }
933
934 1
860} 935}
861 936
862############################################################################# 937#############################################################################
863 938
864package CFClient::UI::VBox; 939package CFClient::UI::VBox;
879} 954}
880 955
881sub size_allocate { 956sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_; 957 my ($self, $x, $y, $w, $h) = @_;
883 958
884 $self->_size_allocate ($x, $y, $w, $h); 959 $self->_size_allocate ($x, $y, $w, $h) or return;
885 960
886 my $children = $self->{children}; 961 my $children = $self->{children};
887 962
888 my @h = map +($_->size_request)[1], @$children; 963 my @h = map +($_->size_request)[1], @$children;
889 964
911 my $h = $h[$_]; 986 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h); 987 $child->size_allocate (0, $y, $w, $h);
913 988
914 $y += $h; 989 $y += $h;
915 } 990 }
991
992 1
916} 993}
917 994
918############################################################################# 995#############################################################################
919 996
920package CFClient::UI::Label; 997package CFClient::UI::Label;
929 my $self = $class->SUPER::new ( 1006 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 1007 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE, 1008 fontsize => $::FONTSIZE,
932 text => "", 1009 text => "",
933 align => -1, 1010 align => -1,
1011 valign => -1,
934 padding => 2, 1012 padding => 2,
935 layout => new CFClient::Layout, 1013 layout => new CFClient::Layout,
936 %arg 1014 %arg
937 ); 1015 );
938 1016
939 $self->set_text ($self->{text}); 1017 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d#
1018
1019 $self->set_text (delete $self->{text}) if exists $self->{text};
1020 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
940 1021
941 $self 1022 $self
942} 1023}
943 1024
944sub escape_text { 1025sub escape_text {
952} 1033}
953 1034
954sub set_text { 1035sub set_text {
955 my ($self, $text) = @_; 1036 my ($self, $text) = @_;
956 1037
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text); 1038 $self->{layout}->set_text ($text);
959 1039
960 delete $self->{texture}; 1040 delete $self->{texture};
961# $self->{w} = $self->{h} = -1;
962 $self->update; 1041 $self->update;
963} 1042}
964 1043
965sub get_text { 1044sub set_markup {
966 my ($self, $text) = @_; 1045 my ($self, $markup) = @_;
967 1046
968 $self->{text} 1047 $self->{layout}->set_markup ($markup);
1048
1049 delete $self->{texture};
1050 $self->update;
969} 1051}
970 1052
971sub size_request { 1053sub size_request {
972 my ($self) = @_; 1054 my ($self) = @_;
973 1055
974 $self->{layout}->set_width; 1056 $self->{layout}->set_width;
975 $self->{layout}->set_height ($self->{fontsize}); 1057 $self->{layout}->set_height ($self->{fontsize});
1058
976 my ($w, $h) = $self->{layout}->size; 1059 my ($w, $h) = $self->{layout}->size;
977 1060
978 ( 1061 (
979 $w + $self->{padding} * 2, 1062 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2, 1063 $h + $self->{padding} * 2,
999sub _draw { 1082sub _draw {
1000 my ($self) = @_; 1083 my ($self) = @_;
1001 1084
1002 my $tex = $self->{texture} ||= do { 1085 my $tex = $self->{texture} ||= do {
1003 $self->{layout}->set_width ($self->{w}); 1086 $self->{layout}->set_width ($self->{w});
1004 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1087 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize});
1005 new_from_layout CFClient::Texture $self->{layout} 1088 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1089 };
1007 1090
1008 glEnable GL_BLEND; 1091 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1092 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1093 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1094 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1095
1013 glColor @{$self->{fg}}; 1096 glColor @{$self->{fg}};
1014 1097
1015 my $x = 1098 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1099 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1100 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1101 : ($self->{w} - $tex->{w}) * 0.5
1102 );
1019 1103
1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1104 $self->{oy} = int (
1105 $self->{valign} < 0 ? $self->{padding}
1106 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1107 : ($self->{h} - $tex->{h}) * 0.5
1108 );
1109
1110 $tex->draw_quad ($self->{ox}, $self->{oy});
1021 1111
1022 glDisable GL_TEXTURE_2D; 1112 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1113 glDisable GL_BLEND;
1024} 1114}
1025 1115
1026############################################################################# 1116#############################################################################
1027 1117
1028package CFClient::UI::Entry; 1118package CFClient::UI::EntryBase;
1029 1119
1030our @ISA = CFClient::UI::Label::; 1120our @ISA = CFClient::UI::Label::;
1031 1121
1032use SDL; 1122use SDL;
1033use SDL::OpenGL; 1123use SDL::OpenGL;
1040 bg => [0, 0, 0, 0.2], 1130 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1131 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1132 active_fg => [0, 0, 0],
1043 can_hover => 1, 1133 can_hover => 1,
1044 can_focus => 1, 1134 can_focus => 1,
1135 valign => 0,
1045 @_ 1136 @_
1046 ) 1137 )
1047} 1138}
1048 1139
1049sub _set_text { 1140sub _set_text {
1050 my ($self, $text) = @_; 1141 my ($self, $text) = @_;
1051 1142
1052 my $old_text = $self->{text}; 1143 delete $self->{cur_h};
1144
1145 return if $self->{text} eq $text;
1053 1146
1054 $self->{last_activity} = $::NOW; 1147 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1148 $self->{text} = $text;
1057 $self->{layout}->set_width ($self->{w});
1058 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1059 1149
1060 $text =~ s/./*/g if $self->{hidden}; 1150 $text =~ s/./*/g if $self->{hidden};
1151 $self->{layout}->set_text ("$text ");
1061 1152
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1153 $self->emit (changed => $self->{text});
1154}
1063 1155
1064 $text = substr $text, 0, $self->{cursor}; 1156sub get_text {
1065 utf8::encode $text; 1157 $_[0]{text}
1066
1067 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1068
1069 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1070 if $old_text ne $self->{text};
1071} 1158}
1072 1159
1073sub size_request { 1160sub size_request {
1074 my ($self) = @_; 1161 my ($self) = @_;
1075 1162
1079} 1166}
1080 1167
1081sub size_allocate { 1168sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1169 my ($self, $x, $y, $w, $h) = @_;
1083 1170
1084 $self->SUPER::size_allocate ($x, $y, $w, $h); 1171 $self->SUPER::size_allocate ($x, $y, $w, $h) or return;
1085 1172
1086 $self->_set_text ($self->{text}); 1173 $self->_set_text ($self->{text});
1087} 1174}
1088 1175
1089sub set_text { 1176sub set_text {
1179 1266
1180 $self->SUPER::_draw; 1267 $self->SUPER::_draw;
1181 1268
1182 #TODO: force update every cursor change :( 1269 #TODO: force update every cursor change :(
1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1270 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1271
1272 unless (exists $self->{cur_h}) {
1273 my $text = substr $self->{text}, 0, $self->{cursor};
1274 utf8::encode $text;
1275
1276 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1277 }
1278
1184 glColor @{$self->{fg}}; 1279 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1280 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1281 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1282 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1188 glEnd; 1283 glEnd;
1189 } 1284 }
1190} 1285}
1191 1286
1192package CFClient::UI::LineEntry; 1287package CFClient::UI::Entry;
1193 1288
1194our @ISA = CFClient::UI::Entry::; 1289our @ISA = CFClient::UI::EntryBase::;
1195 1290
1196use SDL; 1291use SDL;
1197use SDL::OpenGL; 1292use SDL::OpenGL;
1198 1293
1199sub key_down { 1294sub key_down {
1229 1324
1230 $class->SUPER::new ( 1325 $class->SUPER::new (
1231 padding => 4, 1326 padding => 4,
1232 fg => [1, 1, 1], 1327 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1328 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1329 active_fg => [0, 0, 1],
1235 can_hover => 1, 1330 can_hover => 1,
1331 align => 0,
1332 valign => 0,
1236 @_ 1333 @_
1237 ) 1334 )
1238} 1335}
1239 1336
1240sub button_up { 1337sub button_up {
1248 1345
1249sub _draw { 1346sub _draw {
1250 my ($self) = @_; 1347 my ($self) = @_;
1251 1348
1252 local $self->{fg} = $self->{fg}; 1349 local $self->{fg} = $self->{fg};
1253 my $tex = $tex[0];
1254
1255 glEnable GL_BLEND;
1256 glEnable GL_TEXTURE_2D;
1257 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1258 1350
1259 if ($GRAB == $self) { 1351 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1352 $self->{fg} = $self->{active_fg};
1261 } 1353 }
1262 1354
1263 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1355 glEnable GL_BLEND;
1356 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1357 glEnable GL_TEXTURE_2D;
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1358 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1359 glColor 0, 0, 0, 1;
1265 1360
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1361 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1362
1268 glDisable GL_TEXTURE_2D; 1363 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1364 glDisable GL_BLEND;
1270 1365
1271 $self->SUPER::_draw; 1366 $self->SUPER::_draw;
1304} 1399}
1305 1400
1306sub size_allocate { 1401sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_; 1402 my ($self, $x, $y, $w, $h) = @_;
1308 1403
1309 $self->_size_allocate ($x, $y, $w, $h); 1404 $self->_size_allocate ($x, $y, $w, $h) or return;
1310} 1405}
1311 1406
1312sub button_down { 1407sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1408 my ($self, $ev, $x, $y) = @_;
1314 1409
1338 1433
1339 $tex->draw_quad (0, 0, $s, $s); 1434 $tex->draw_quad (0, 0, $s, $s);
1340 1435
1341 glDisable GL_TEXTURE_2D; 1436 glDisable GL_TEXTURE_2D;
1342 glDisable GL_BLEND; 1437 glDisable GL_BLEND;
1438}
1439
1440#############################################################################
1441
1442package CFClient::UI::VGauge;
1443
1444our @ISA = CFClient::UI::Base::;
1445
1446use SDL::OpenGL;
1447
1448my %tex = (
1449 food => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1452 ],
1453 grace => [
1454 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1455 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1456 ],
1457 hp => [
1458 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1459 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1460 ],
1461 mana => [
1462 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1463 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1464 ],
1465);
1466
1467# eg. VGauge->new (gauge => 'food'), default gauge: food
1468sub new {
1469 my $class = shift;
1470
1471 my $self = $class->SUPER::new (gauge => 'food', @_);
1472
1473 $self
1474}
1475
1476sub size_request {
1477 my ($self) = @_;
1478
1479 my $tex = $tex{$self->{gauge}}[0];
1480
1481 @$tex{qw(w h)}
1482}
1483
1484sub set_max {
1485 my ($self, $max) = @_;
1486
1487 $self->{max_val} = $max;
1488}
1489
1490sub set_value {
1491 my ($self, $val, $max) = @_;
1492
1493 $self->set_max ($max)
1494 if defined $max;
1495
1496 $max = $self->{max_val};
1497 $self->{val} = $val;
1498
1499 $self->update;
1500}
1501
1502sub _draw {
1503 my ($self) = @_;
1504
1505 my $tex = $tex{$self->{gauge}};
1506
1507 my ($w, $h) = ($self->{w}, $self->{h});
1508
1509 my $ycut = $self->{val} / ($self->{max_val} || 1);
1510 $ycut = 1 if $self->{val} > $self->{max_val};
1511
1512 my $t1 = $tex->[0];
1513 my $t2 = $tex->[1];
1514
1515 glEnable GL_BLEND;
1516 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1517 glEnable GL_TEXTURE_2D;
1518 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1519
1520 my $h1 = $self->{h} - $ycut * $self->{h};
1521 my $h2 = $ycut * $self->{h};
1522
1523 my $yp = 0;
1524
1525 glBindTexture (GL_TEXTURE_2D, $t1->{name});
1526 glBegin (GL_QUADS);
1527 glTexCoord (0, 0); glVertex (0 , $yp);
1528 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1);
1529 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1);
1530 glTexCoord (1, 0); glVertex (0 + $w, $yp);
1531 glEnd ();
1532
1533 $yp += $h1;
1534
1535 glBindTexture (GL_TEXTURE_2D, $t2->{name});
1536 glBegin (GL_QUADS);
1537 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp);
1538 glTexCoord (0, 1); glVertex (0 , $yp + $h2);
1539 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2);
1540 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp);
1541 glEnd ();
1542
1543 glDisable GL_BLEND;
1544 glDisable GL_TEXTURE_2D;
1343} 1545}
1344 1546
1345############################################################################# 1547#############################################################################
1346 1548
1347package CFClient::UI::Slider; 1549package CFClient::UI::Slider;
1543} 1745}
1544 1746
1545sub size_allocate { 1747sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 1748 my ($self, $x, $y, $w, $h) = @_;
1547 1749
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 1750 $self->SUPER::size_allocate ($x, $y, $w, $h) or return;
1549 1751
1550 $self->{layout}->set_height ($self->{fontsize}); 1752 $self->{layout}->set_height ($self->{fontsize});
1551 $self->{layout}->set_width ($self->{w}); 1753 $self->{layout}->set_width ($self->{w});
1552 1754
1553 $self->reflow; 1755 $self->reflow;
1756 $self->update;
1554} 1757}
1555 1758
1556sub add_paragraph { 1759sub add_paragraph {
1557 my ($self, $color, $text) = @_; 1760 my ($self, $color, $text) = @_;
1558 1761
1575 1778
1576 return unless $self->{h} > 0; 1779 return unless $self->{h} > 0;
1577 1780
1578 delete $self->{texture}; 1781 delete $self->{texture};
1579 1782
1580 $TOPLEVEL->on_refresh ($self, sub { 1783 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1784 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1785 my $height = 0;
1583 1786
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1787 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1788 for @{$self->{par}};
1753 0.1, 0.1, 0.1, 1956 0.1, 0.1, 0.1,
1754 ; 1957 ;
1755 glEnable GL_CONVOLUTION_2D; 1958 glEnable GL_CONVOLUTION_2D;
1756 } 1959 }
1757 1960
1758 my $tex = new CFClient::Texture 1961 $self->{fow_texture} = new CFClient::Texture
1759 w => $w, 1962 w => $w,
1760 h => $h, 1963 h => $h,
1761 data => $data, 1964 data => $data,
1762 internalformat => GL_ALPHA, 1965 internalformat => GL_ALPHA,
1763 format => GL_ALPHA; 1966 format => GL_ALPHA;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1971 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1769 glEnable GL_TEXTURE_2D; 1972 glEnable GL_TEXTURE_2D;
1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1973 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1771 1974
1772 glColor +($::CFG->{fow_intensity}) x 3, 1; 1975 glColor +($::CFG->{fow_intensity}) x 3, 1;
1773 $tex->draw_quad (0, 0, $w * 32, $h * 32); 1976 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1774 1977
1775 glDisable GL_TEXTURE_2D; 1978 glDisable GL_TEXTURE_2D;
1776 glDisable GL_BLEND; 1979 glDisable GL_BLEND;
1777 } 1980 }
1778 1981
1784 glEnable GL_BLEND; 1987 glEnable GL_BLEND;
1785 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1988 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1786 glEnable GL_TEXTURE_2D; 1989 glEnable GL_TEXTURE_2D;
1787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1990 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1788 1991
1992 $self->{mapmap_texture} =
1789 CFClient::Texture->new ( 1993 new CFClient::Texture
1790 w => $w, 1994 w => $w,
1791 h => $h, 1995 h => $h,
1792 data => $::MAP->mapmap ($w, $h), 1996 data => $::MAP->mapmap ($w, $h),
1793 type => GL_UNSIGNED_INT_8_8_8_8_REV 1997 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1794 )->draw_quad (100, 100); 1998
1999 $self->{mapmap_texture}->draw_quad (100, 100);
1795 2000
1796 glDisable GL_TEXTURE_2D; 2001 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND; 2002 glDisable GL_BLEND;
1798 } 2003 }
1799 # HACK END 2004 # HACK END
1841 2046
1842 my $mod = $ev->key_mod; 2047 my $mod = $ev->key_mod;
1843 my $sym = $ev->key_sym; 2048 my $sym = $ev->key_sym;
1844 2049
1845 if ($sym == SDLK_KP5) { 2050 if ($sym == SDLK_KP5) {
1846 $::CONN->user_send ("command stay fire"); 2051 $::CONN->user_send ("stay fire");
1847 } elsif ($sym == SDLK_a) { 2052 } elsif ($sym == SDLK_a) {
1848 $::CONN->user_send ("command apply"); 2053 $::CONN->user_send ("apply");
1849 } elsif ($sym == SDLK_QUOTE) { 2054 } elsif ($sym == SDLK_QUOTE) {
1850 $self->emit ('activate_console'); 2055 $self->emit ('activate_console');
1851 } elsif ($sym == SDLK_SLASH) { 2056 } elsif ($sym == SDLK_SLASH) {
1852 $self->emit ('activate_console' => '/'); 2057 $self->emit ('activate_console' => '/');
1853 } elsif (exists $DIR{$sym}) { 2058 } elsif (exists $DIR{$sym}) {
1854 if ($mod & KMOD_SHIFT) { 2059 if ($mod & KMOD_SHIFT) {
1855 $self->{shft}++; 2060 $self->{shft}++;
1856 $::CONN->user_send ("command fire $DIR{$sym}[0]"); 2061 $::CONN->user_send ("fire $DIR{$sym}[0]");
1857 } elsif ($mod & KMOD_CTRL) { 2062 } elsif ($mod & KMOD_CTRL) {
1858 $self->{ctrl}++; 2063 $self->{ctrl}++;
1859 $::CONN->user_send ("command run $DIR{$sym}[0]"); 2064 $::CONN->user_send ("run $DIR{$sym}[0]");
1860 } else { 2065 } else {
1861 $::CONN->user_send ("command $DIR{$sym}[1]"); 2066 $::CONN->user_send ("$DIR{$sym}[1]");
1862 } 2067 }
1863 } 2068 }
1864} 2069}
1865 2070
1866sub key_up { 2071sub key_up {
1868 2073
1869 my $mod = $ev->key_mod; 2074 my $mod = $ev->key_mod;
1870 my $sym = $ev->key_sym; 2075 my $sym = $ev->key_sym;
1871 2076
1872 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 2077 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1873 $::CONN->user_send ("command fire_stop"); 2078 $::CONN->user_send ("fire_stop");
1874 } 2079 }
1875 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 2080 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1876 $::CONN->user_send ("command run_stop"); 2081 $::CONN->user_send ("run_stop");
1877 } 2082 }
1878} 2083}
1879 2084
1880############################################################################# 2085#############################################################################
1881 2086
1945sub toggle_flopper { 2150sub toggle_flopper {
1946 my ($self) = @_; 2151 my ($self) = @_;
1947 2152
1948 # TODO: use animation 2153 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 2154 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2155 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 2156 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 2157 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 2158 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2159 $CFClient::UI::ROOT->remove ($self->{other});
2160 $self->emit ("close");
1957 } 2161 }
1958}
1959 2162
1960############################################################################# 2163 $self->emit (changed => $self->{state});
2164}
1961 2165
2166#############################################################################
2167
1962package CFClient::UI::Toplevel; 2168package CFClient::UI::Root;
1963 2169
1964our @ISA = CFClient::UI::Container::; 2170our @ISA = CFClient::UI::Container::;
1965 2171
1966use SDL::OpenGL; 2172use SDL::OpenGL;
2173
2174sub check_size {
2175 my ($self) = @_;
2176
2177 $self->size_allocate (0, 0, $::WITH, $::HEIGHT);
2178}
1967 2179
1968sub size_request { 2180sub size_request {
1969 ($::WIDTH, $::HEIGHT) 2181 ($::WIDTH, $::HEIGHT)
1970} 2182}
1971 2183
1976 2188
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2189 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 2190 for @{$self->{children}};
1979} 2191}
1980 2192
1981sub translate { 2193sub _topleft {
1982 my ($self, $x, $y) = @_; 2194 my ($self, $x, $y) = @_;
1983 2195
1984 ($x, $y) 2196 ($x, $y)
1985} 2197}
1986 2198
1987sub update { 2199sub update {
1988 my ($self) = @_; 2200 my ($self) = @_;
1989 2201
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2202 $self->check_size;
1991 ::refresh (); 2203 ::refresh ();
1992} 2204}
1993 2205
1994sub add { 2206sub add {
1995 my ($self, $widget) = @_; 2207 my ($self, $widget) = @_;
1996 2208
1997 $self->SUPER::add ($widget); 2209 $self->SUPER::add ($widget);
1998 2210
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2211 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2000} 2212}
2001 2213
2002sub on_refresh { 2214sub on_refresh {
2003 my ($self, $id, $cb) = @_; 2215 my ($self, $id, $cb) = @_;
2004 2216
2027 2239
2028############################################################################# 2240#############################################################################
2029 2241
2030package CFClient::UI; 2242package CFClient::UI;
2031 2243
2032$TOPLEVEL = new CFClient::UI::Toplevel; 2244$ROOT = new CFClient::UI::Root;
2033 2245
20341 22461
2035 2247

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines