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.125 by root, Mon Apr 17 20:52:15 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 $self->update;
355} 388}
357sub remove { 390sub remove {
358 my ($self, $widget) = @_; 391 my ($self, $widget) = @_;
359 392
360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 393 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
361 394
362 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 395 $self->check_size;
363} 396}
364 397
365sub find_widget { 398sub find_widget {
366 my ($self, $x, $y) = @_; 399 my ($self, $x, $y) = @_;
367 400
487 $tex->draw_quad (0, 0, $w, $h); 520 $tex->draw_quad (0, 0, $w, $h);
488 521
489 glDisable GL_BLEND; 522 glDisable GL_BLEND;
490 glDisable GL_TEXTURE_2D; 523 glDisable GL_TEXTURE_2D;
491} 524}
525
526#############################################################################
527
528package CFClient::UI::ViewPort;
529
530our @ISA = CFClient::UI::Window::;
531
532sub new { die }
533
534sub size_request {
535 my ($self) = @_;
536
537 @$self{qw(child_w child_h)} = $self->child->size_request;
538 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
539
540 @$self{qw(child_w child_h)}
541}
542
543sub size_allocate {
544 my ($self, $x, $y, $w, $h) = @_;
545
546 $self->_size_allocate ($x, $y, $w, $h) or return;
547}
548
549sub _draw {
550 my ($self) = @_;
551
552 $self->{children}[1]->draw;
553}
554
492 555
493############################################################################# 556#############################################################################
494 557
495package CFClient::UI::Frame; 558package CFClient::UI::Frame;
496 559
552 # TODO: user_x, user_y, overwrite moveto? 615 # TODO: user_x, user_y, overwrite moveto?
553 616
554 $class->SUPER::new ( 617 $class->SUPER::new (
555 bg => [1, 1, 1, 1], 618 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 619 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 620 border => int $::FONTSIZE * 0.8,
558 @_ 621 @_
559 ) 622 )
560} 623}
561 624
562sub size_request { 625sub size_request {
685 @_ 748 @_
686 ) 749 )
687} 750}
688 751
689sub add { 752sub add {
690 my ($self, $x, $y, $chld) = @_; 753 my ($self, $x, $y, $child) = @_;
691 754
755 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 756 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 757
695 $self->{w} = $self->{h} = -1; 758 $self->{w} = $self->{h} = -1;
759 $self->update;
760}
761
762# TODO: move to container class maybe? send childs a signal on removal?
763sub clear {
764 my ($self) = @_;
765
766 delete $self->{children};
696 $self->update; 767 $self->update;
697} 768}
698 769
699sub get_wh { 770sub get_wh {
700 my ($self) = @_; 771 my ($self) = @_;
819} 890}
820 891
821sub size_allocate { 892sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_; 893 my ($self, $x, $y, $w, $h) = @_;
823 894
824 $self->_size_allocate ($x, $y, $w, $h); 895 $self->_size_allocate ($x, $y, $w, $h) or return;
825 896
826 ($h, $w) = ($w, $h); 897 ($h, $w) = ($w, $h);
827 898
828 my $children = $self->{children}; 899 my $children = $self->{children};
829 900
855 my $h = $h[$_]; 926 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w); 927 $child->size_allocate ($y, 0, $h, $w);
857 928
858 $y += $h; 929 $y += $h;
859 } 930 }
931
932 1
860} 933}
861 934
862############################################################################# 935#############################################################################
863 936
864package CFClient::UI::VBox; 937package CFClient::UI::VBox;
879} 952}
880 953
881sub size_allocate { 954sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_; 955 my ($self, $x, $y, $w, $h) = @_;
883 956
884 $self->_size_allocate ($x, $y, $w, $h); 957 $self->_size_allocate ($x, $y, $w, $h) or return;
885 958
886 my $children = $self->{children}; 959 my $children = $self->{children};
887 960
888 my @h = map +($_->size_request)[1], @$children; 961 my @h = map +($_->size_request)[1], @$children;
889 962
911 my $h = $h[$_]; 984 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h); 985 $child->size_allocate (0, $y, $w, $h);
913 986
914 $y += $h; 987 $y += $h;
915 } 988 }
989
990 1
916} 991}
917 992
918############################################################################# 993#############################################################################
919 994
920package CFClient::UI::Label; 995package CFClient::UI::Label;
929 my $self = $class->SUPER::new ( 1004 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 1005 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE, 1006 fontsize => $::FONTSIZE,
932 text => "", 1007 text => "",
933 align => -1, 1008 align => -1,
1009 valign => -1,
934 padding => 2, 1010 padding => 2,
935 layout => new CFClient::Layout, 1011 layout => new CFClient::Layout,
936 %arg 1012 %arg
937 ); 1013 );
938 1014
939 $self->set_text ($self->{text}); 1015 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d#
1016
1017 $self->set_text (delete $self->{text}) if exists $self->{text};
1018 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
940 1019
941 $self 1020 $self
942} 1021}
943 1022
944sub escape_text { 1023sub escape_text {
952} 1031}
953 1032
954sub set_text { 1033sub set_text {
955 my ($self, $text) = @_; 1034 my ($self, $text) = @_;
956 1035
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text); 1036 $self->{layout}->set_text ($text);
959 1037
960 delete $self->{texture}; 1038 delete $self->{texture};
961# $self->{w} = $self->{h} = -1;
962 $self->update; 1039 $self->update;
963} 1040}
964 1041
965sub get_text { 1042sub set_markup {
966 my ($self, $text) = @_; 1043 my ($self, $markup) = @_;
967 1044
968 $self->{text} 1045 $self->{layout}->set_markup ($markup);
1046
1047 delete $self->{texture};
1048 $self->update;
969} 1049}
970 1050
971sub size_request { 1051sub size_request {
972 my ($self) = @_; 1052 my ($self) = @_;
973 1053
974 $self->{layout}->set_width; 1054 $self->{layout}->set_width;
975 $self->{layout}->set_height ($self->{fontsize}); 1055 $self->{layout}->set_height ($self->{fontsize});
1056
976 my ($w, $h) = $self->{layout}->size; 1057 my ($w, $h) = $self->{layout}->size;
977 1058
978 ( 1059 (
979 $w + $self->{padding} * 2, 1060 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2, 1061 $h + $self->{padding} * 2,
999sub _draw { 1080sub _draw {
1000 my ($self) = @_; 1081 my ($self) = @_;
1001 1082
1002 my $tex = $self->{texture} ||= do { 1083 my $tex = $self->{texture} ||= do {
1003 $self->{layout}->set_width ($self->{w}); 1084 $self->{layout}->set_width ($self->{w});
1004 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1085 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize});
1005 new_from_layout CFClient::Texture $self->{layout} 1086 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1087 };
1007 1088
1008 glEnable GL_BLEND; 1089 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1090 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1091 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1092 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1093
1013 glColor @{$self->{fg}}; 1094 glColor @{$self->{fg}};
1014 1095
1015 my $x = 1096 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1097 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1098 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1099 : ($self->{w} - $tex->{w}) * 0.5
1100 );
1019 1101
1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1102 $self->{oy} = int (
1103 $self->{valign} < 0 ? $self->{padding}
1104 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1105 : ($self->{h} - $tex->{h}) * 0.5
1106 );
1107
1108 $tex->draw_quad ($self->{ox}, $self->{oy});
1021 1109
1022 glDisable GL_TEXTURE_2D; 1110 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1111 glDisable GL_BLEND;
1024} 1112}
1025 1113
1026############################################################################# 1114#############################################################################
1027 1115
1028package CFClient::UI::Entry; 1116package CFClient::UI::EntryBase;
1029 1117
1030our @ISA = CFClient::UI::Label::; 1118our @ISA = CFClient::UI::Label::;
1031 1119
1032use SDL; 1120use SDL;
1033use SDL::OpenGL; 1121use SDL::OpenGL;
1040 bg => [0, 0, 0, 0.2], 1128 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1129 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1130 active_fg => [0, 0, 0],
1043 can_hover => 1, 1131 can_hover => 1,
1044 can_focus => 1, 1132 can_focus => 1,
1133 valign => 0,
1045 @_ 1134 @_
1046 ) 1135 )
1047} 1136}
1048 1137
1049sub _set_text { 1138sub _set_text {
1050 my ($self, $text) = @_; 1139 my ($self, $text) = @_;
1051 1140
1052 my $old_text = $self->{text}; 1141 delete $self->{cur_h};
1142
1143 return if $self->{text} eq $text;
1053 1144
1054 $self->{last_activity} = $::NOW; 1145 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1146 $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 1147
1060 $text =~ s/./*/g if $self->{hidden}; 1148 $text =~ s/./*/g if $self->{hidden};
1149 $self->{layout}->set_text ("$text ");
1061 1150
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1151 $self->emit (changed => $self->{text});
1152}
1063 1153
1064 $text = substr $text, 0, $self->{cursor}; 1154sub get_text {
1065 utf8::encode $text; 1155 $_[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} 1156}
1072 1157
1073sub size_request { 1158sub size_request {
1074 my ($self) = @_; 1159 my ($self) = @_;
1075 1160
1079} 1164}
1080 1165
1081sub size_allocate { 1166sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1167 my ($self, $x, $y, $w, $h) = @_;
1083 1168
1084 $self->SUPER::size_allocate ($x, $y, $w, $h); 1169 $self->SUPER::size_allocate ($x, $y, $w, $h) or return;
1085 1170
1086 $self->_set_text ($self->{text}); 1171 $self->_set_text ($self->{text});
1087} 1172}
1088 1173
1089sub set_text { 1174sub set_text {
1179 1264
1180 $self->SUPER::_draw; 1265 $self->SUPER::_draw;
1181 1266
1182 #TODO: force update every cursor change :( 1267 #TODO: force update every cursor change :(
1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1268 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1269
1270 unless (exists $self->{cur_h}) {
1271 my $text = substr $self->{text}, 0, $self->{cursor};
1272 utf8::encode $text;
1273
1274 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1275 }
1276
1184 glColor @{$self->{fg}}; 1277 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1278 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1279 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1280 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1188 glEnd; 1281 glEnd;
1189 } 1282 }
1190} 1283}
1191 1284
1192package CFClient::UI::LineEntry; 1285package CFClient::UI::Entry;
1193 1286
1194our @ISA = CFClient::UI::Entry::; 1287our @ISA = CFClient::UI::EntryBase::;
1195 1288
1196use SDL; 1289use SDL;
1197use SDL::OpenGL; 1290use SDL::OpenGL;
1198 1291
1199sub key_down { 1292sub key_down {
1229 1322
1230 $class->SUPER::new ( 1323 $class->SUPER::new (
1231 padding => 4, 1324 padding => 4,
1232 fg => [1, 1, 1], 1325 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1326 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1327 active_fg => [0, 0, 1],
1235 can_hover => 1, 1328 can_hover => 1,
1236 @_ 1329 @_
1237 ) 1330 )
1238} 1331}
1239 1332
1248 1341
1249sub _draw { 1342sub _draw {
1250 my ($self) = @_; 1343 my ($self) = @_;
1251 1344
1252 local $self->{fg} = $self->{fg}; 1345 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 1346
1259 if ($GRAB == $self) { 1347 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1348 $self->{fg} = $self->{active_fg};
1261 } 1349 }
1262 1350
1263 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1351 glEnable GL_BLEND;
1352 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1353 glEnable GL_TEXTURE_2D;
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1354 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1355 glColor 0, 0, 0, 1;
1265 1356
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1357 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1358
1268 glDisable GL_TEXTURE_2D; 1359 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1360 glDisable GL_BLEND;
1270 1361
1271 $self->SUPER::_draw; 1362 $self->SUPER::_draw;
1304} 1395}
1305 1396
1306sub size_allocate { 1397sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_; 1398 my ($self, $x, $y, $w, $h) = @_;
1308 1399
1309 $self->_size_allocate ($x, $y, $w, $h); 1400 $self->_size_allocate ($x, $y, $w, $h) or return;
1310} 1401}
1311 1402
1312sub button_down { 1403sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1404 my ($self, $ev, $x, $y) = @_;
1314 1405
1338 1429
1339 $tex->draw_quad (0, 0, $s, $s); 1430 $tex->draw_quad (0, 0, $s, $s);
1340 1431
1341 glDisable GL_TEXTURE_2D; 1432 glDisable GL_TEXTURE_2D;
1342 glDisable GL_BLEND; 1433 glDisable GL_BLEND;
1434}
1435
1436#############################################################################
1437
1438package CFClient::UI::VGauge;
1439
1440our @ISA = CFClient::UI::Base::;
1441
1442use SDL::OpenGL;
1443
1444my %tex = (
1445 food => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1448 ],
1449 grace => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1452 ],
1453 hp => [
1454 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1455 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1456 ],
1457 mana => [
1458 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1459 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1460 ],
1461);
1462
1463# eg. VGauge->new (gauge => 'food'), default gauge: food
1464sub new {
1465 my $class = shift;
1466
1467 my $self = $class->SUPER::new (gauge => 'food', @_);
1468
1469 $self
1470}
1471
1472sub size_request {
1473 my ($self) = @_;
1474
1475 ($self->{w}, $self->{h})
1476}
1477
1478sub set_max {
1479 my ($self, $max) = @_;
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493}
1494
1495sub _draw {
1496 my ($self) = @_;
1497
1498 my $tex = $tex{$self->{gauge}};
1499
1500 my ($w, $h) = ($self->{w}, $self->{h});
1501
1502 my $ycut = $self->{val} / ($self->{max_val} || 1);
1503 $ycut = 1 if $self->{val} > $self->{max_val};
1504
1505 my $t1 = $tex->[0];
1506 my $t2 = $tex->[1];
1507
1508 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512
1513 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h};
1515
1516 my $yp = 0;
1517
1518 glBindTexture (GL_TEXTURE_2D, $t1->{name});
1519 glBegin (GL_QUADS);
1520 glTexCoord (0, 0); glVertex (0 , $yp);
1521 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1);
1522 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1);
1523 glTexCoord (1, 0); glVertex (0 + $w, $yp);
1524 glEnd ();
1525
1526 $yp += $h1;
1527
1528 glBindTexture (GL_TEXTURE_2D, $t2->{name});
1529 glBegin (GL_QUADS);
1530 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp);
1531 glTexCoord (0, 1); glVertex (0 , $yp + $h2);
1532 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2);
1533 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp);
1534 glEnd ();
1535
1536 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D;
1343} 1538}
1344 1539
1345############################################################################# 1540#############################################################################
1346 1541
1347package CFClient::UI::Slider; 1542package CFClient::UI::Slider;
1543} 1738}
1544 1739
1545sub size_allocate { 1740sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 1741 my ($self, $x, $y, $w, $h) = @_;
1547 1742
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 1743 $self->SUPER::size_allocate ($x, $y, $w, $h) or return;
1549 1744
1550 $self->{layout}->set_height ($self->{fontsize}); 1745 $self->{layout}->set_height ($self->{fontsize});
1551 $self->{layout}->set_width ($self->{w}); 1746 $self->{layout}->set_width ($self->{w});
1552 1747
1553 $self->reflow; 1748 $self->reflow;
1749 $self->update;
1554} 1750}
1555 1751
1556sub add_paragraph { 1752sub add_paragraph {
1557 my ($self, $color, $text) = @_; 1753 my ($self, $color, $text) = @_;
1558 1754
1575 1771
1576 return unless $self->{h} > 0; 1772 return unless $self->{h} > 0;
1577 1773
1578 delete $self->{texture}; 1774 delete $self->{texture};
1579 1775
1580 $TOPLEVEL->on_refresh ($self, sub { 1776 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1777 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1778 my $height = 0;
1583 1779
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1780 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1781 for @{$self->{par}};
1753 0.1, 0.1, 0.1, 1949 0.1, 0.1, 0.1,
1754 ; 1950 ;
1755 glEnable GL_CONVOLUTION_2D; 1951 glEnable GL_CONVOLUTION_2D;
1756 } 1952 }
1757 1953
1758 my $tex = new CFClient::Texture 1954 $self->{fow_texture} = new CFClient::Texture
1759 w => $w, 1955 w => $w,
1760 h => $h, 1956 h => $h,
1761 data => $data, 1957 data => $data,
1762 internalformat => GL_ALPHA, 1958 internalformat => GL_ALPHA,
1763 format => GL_ALPHA; 1959 format => GL_ALPHA;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1964 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1769 glEnable GL_TEXTURE_2D; 1965 glEnable GL_TEXTURE_2D;
1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1966 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1771 1967
1772 glColor +($::CFG->{fow_intensity}) x 3, 1; 1968 glColor +($::CFG->{fow_intensity}) x 3, 1;
1773 $tex->draw_quad (0, 0, $w * 32, $h * 32); 1969 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1774 1970
1775 glDisable GL_TEXTURE_2D; 1971 glDisable GL_TEXTURE_2D;
1776 glDisable GL_BLEND; 1972 glDisable GL_BLEND;
1777 } 1973 }
1778 1974
1784 glEnable GL_BLEND; 1980 glEnable GL_BLEND;
1785 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1981 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1786 glEnable GL_TEXTURE_2D; 1982 glEnable GL_TEXTURE_2D;
1787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1983 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1788 1984
1985 $self->{mapmap_texture} =
1789 CFClient::Texture->new ( 1986 new CFClient::Texture
1790 w => $w, 1987 w => $w,
1791 h => $h, 1988 h => $h,
1792 data => $::MAP->mapmap ($w, $h), 1989 data => $::MAP->mapmap ($w, $h),
1793 type => GL_UNSIGNED_INT_8_8_8_8_REV 1990 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1794 )->draw_quad (100, 100); 1991
1992 $self->{mapmap_texture}->draw_quad (100, 100);
1795 1993
1796 glDisable GL_TEXTURE_2D; 1994 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND; 1995 glDisable GL_BLEND;
1798 } 1996 }
1799 # HACK END 1997 # HACK END
1841 2039
1842 my $mod = $ev->key_mod; 2040 my $mod = $ev->key_mod;
1843 my $sym = $ev->key_sym; 2041 my $sym = $ev->key_sym;
1844 2042
1845 if ($sym == SDLK_KP5) { 2043 if ($sym == SDLK_KP5) {
1846 $::CONN->user_send ("command stay fire"); 2044 $::CONN->user_send ("stay fire");
1847 } elsif ($sym == SDLK_a) { 2045 } elsif ($sym == SDLK_a) {
1848 $::CONN->user_send ("command apply"); 2046 $::CONN->user_send ("apply");
1849 } elsif ($sym == SDLK_QUOTE) { 2047 } elsif ($sym == SDLK_QUOTE) {
1850 $self->emit ('activate_console'); 2048 $self->emit ('activate_console');
1851 } elsif ($sym == SDLK_SLASH) { 2049 } elsif ($sym == SDLK_SLASH) {
1852 $self->emit ('activate_console' => '/'); 2050 $self->emit ('activate_console' => '/');
1853 } elsif (exists $DIR{$sym}) { 2051 } elsif (exists $DIR{$sym}) {
1854 if ($mod & KMOD_SHIFT) { 2052 if ($mod & KMOD_SHIFT) {
1855 $self->{shft}++; 2053 $self->{shft}++;
1856 $::CONN->user_send ("command fire $DIR{$sym}[0]"); 2054 $::CONN->user_send ("fire $DIR{$sym}[0]");
1857 } elsif ($mod & KMOD_CTRL) { 2055 } elsif ($mod & KMOD_CTRL) {
1858 $self->{ctrl}++; 2056 $self->{ctrl}++;
1859 $::CONN->user_send ("command run $DIR{$sym}[0]"); 2057 $::CONN->user_send ("run $DIR{$sym}[0]");
1860 } else { 2058 } else {
1861 $::CONN->user_send ("command $DIR{$sym}[1]"); 2059 $::CONN->user_send ("$DIR{$sym}[1]");
1862 } 2060 }
1863 } 2061 }
1864} 2062}
1865 2063
1866sub key_up { 2064sub key_up {
1868 2066
1869 my $mod = $ev->key_mod; 2067 my $mod = $ev->key_mod;
1870 my $sym = $ev->key_sym; 2068 my $sym = $ev->key_sym;
1871 2069
1872 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 2070 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1873 $::CONN->user_send ("command fire_stop"); 2071 $::CONN->user_send ("fire_stop");
1874 } 2072 }
1875 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 2073 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1876 $::CONN->user_send ("command run_stop"); 2074 $::CONN->user_send ("run_stop");
1877 } 2075 }
1878} 2076}
1879 2077
1880############################################################################# 2078#############################################################################
1881 2079
1945sub toggle_flopper { 2143sub toggle_flopper {
1946 my ($self) = @_; 2144 my ($self) = @_;
1947 2145
1948 # TODO: use animation 2146 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 2147 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2148 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 2149 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 2150 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 2151 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2152 $CFClient::UI::ROOT->remove ($self->{other});
2153 $self->emit ("close");
1957 } 2154 }
1958}
1959 2155
1960############################################################################# 2156 $self->emit (changed => $self->{state});
2157}
1961 2158
2159#############################################################################
2160
1962package CFClient::UI::Toplevel; 2161package CFClient::UI::Root;
1963 2162
1964our @ISA = CFClient::UI::Container::; 2163our @ISA = CFClient::UI::Container::;
1965 2164
1966use SDL::OpenGL; 2165use SDL::OpenGL;
2166
2167sub check_size {
2168 my ($self) = @_;
2169
2170 $self->size_allocate (0, 0, $::WITH, $::HEIGHT);
2171}
1967 2172
1968sub size_request { 2173sub size_request {
1969 ($::WIDTH, $::HEIGHT) 2174 ($::WIDTH, $::HEIGHT)
1970} 2175}
1971 2176
1976 2181
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2182 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 2183 for @{$self->{children}};
1979} 2184}
1980 2185
1981sub translate { 2186sub _topleft {
1982 my ($self, $x, $y) = @_; 2187 my ($self, $x, $y) = @_;
1983 2188
1984 ($x, $y) 2189 ($x, $y)
1985} 2190}
1986 2191
1987sub update { 2192sub update {
1988 my ($self) = @_; 2193 my ($self) = @_;
1989 2194
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2195 $self->check_size;
1991 ::refresh (); 2196 ::refresh ();
1992} 2197}
1993 2198
1994sub add { 2199sub add {
1995 my ($self, $widget) = @_; 2200 my ($self, $widget) = @_;
1996 2201
1997 $self->SUPER::add ($widget); 2202 $self->SUPER::add ($widget);
1998 2203
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2204 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2000} 2205}
2001 2206
2002sub on_refresh { 2207sub on_refresh {
2003 my ($self, $id, $cb) = @_; 2208 my ($self, $id, $cb) = @_;
2004 2209
2027 2232
2028############################################################################# 2233#############################################################################
2029 2234
2030package CFClient::UI; 2235package CFClient::UI;
2031 2236
2032$TOPLEVEL = new CFClient::UI::Toplevel; 2237$ROOT = new CFClient::UI::Root;
2033 2238
20341 22391
2035 2240

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines