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

Comparing deliantra/Deliantra-Client/DC/MapWidget.pm (file contents):
Revision 1.1 by root, Wed Apr 19 06:21:07 2006 UTC vs.
Revision 1.15 by root, Mon Apr 24 13:01:13 2006 UTC

1package CFClient::MapWidget; 1package CFClient::MapWidget;
2 2
3use strict; 3use strict;
4use utf8;
4 5
5use List::Util qw(min max); 6use List::Util qw(min max);
6 7
7use SDL; 8use CFClient::OpenGL;
8use SDL::OpenGL;
9 9
10our @ISA = CFClient::UI::Base::; 10our @ISA = CFClient::UI::Base::;
11 11
12sub new { 12sub new {
13 my $class = shift; 13 my $class = shift;
14 14
15 $class->SUPER::new ( 15 $class->SUPER::new (
16 z => -1, 16 z => -1,
17 can_focus => 1, 17 can_focus => 1,
18 list => (glGenLists 1), 18 list => glGenList,
19 @_ 19 @_
20 ) 20 )
21} 21}
22 22
23sub key_down { 23sub DESTROY {
24 print "MAPKEYDOWN\n"; 24 my $self = shift;
25}
26 25
27sub key_up { 26 glDeleteList $self->{list};
27
28 $self->SUPER::DESTROY;
28} 29}
29 30
30sub button_down { 31sub button_down {
31 my ($self, $ev, $x, $y) = @_; 32 my ($self, $ev, $x, $y) = @_;
32 33
33 $self->focus_in; 34 $self->focus_in;
34 35
35 if ($ev->button == 2) { 36 if ($ev->{button} == 2) {
36 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 37 my ($ox, $oy) = ($ev->{x}, $ev->{y});
37 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); 38 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
38 39
39 $self->{motion} = sub { 40 $self->{motion} = sub {
40 my ($ev, $x, $y) = @_; 41 my ($ev, $x, $y) = @_;
41 42
42 ($x, $y) = ($ev->motion_x, $ev->motion_y); 43 ($x, $y) = ($ev->{x}, $ev->{y});
43 44
44 $::CFG->{map_shift_x} = $bw + $x - $ox; 45 $::CFG->{map_shift_x} = $bw + $x - $ox;
45 $::CFG->{map_shift_y} = $bh + $y - $oy; 46 $::CFG->{map_shift_y} = $bh + $y - $oy;
46 47
47 $self->update; 48 $self->update;
77 78
78sub draw { 79sub draw {
79 my ($self) = @_; 80 my ($self) = @_;
80 81
81 if (delete $self->{need_update}) { 82 if (delete $self->{need_update}) {
82 glNewList $self->{list}, GL_COMPILE; 83 glNewList $self->{list};
83 84
84 if ($::MAP) { 85 if ($::MAP) {
85 my $sw = int $::WIDTH / 32; 86 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale});
86 my $sh = int $::HEIGHT / 32; 87 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale});
87 88
89 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
90
88 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; 91 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
89 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; 92 my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
90 93
91 glTranslate $sx0 - 32, $sy0 - 32, 0; 94 glTranslate $sx0 - 32, $sy0 - 32, 0;
92 95
93 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); 96 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
94 97
99 GL_CONVOLUTION_2D, 102 GL_CONVOLUTION_2D,
100 GL_ALPHA, 103 GL_ALPHA,
101 3, 3, 104 3, 3,
102 GL_ALPHA, GL_FLOAT, 105 GL_ALPHA, GL_FLOAT,
103 pack "f*", 106 pack "f*",
104 0.1, 0.1, 0.1, 107 0.05, 0.13, 0.05,
105 0.1, 0.2, 0.1, 108 0.13, 0.30, 0.13,
106 0.1, 0.1, 0.1, 109 0.05, 0.13, 0.05,
107 ); 110 );
108 glEnable GL_CONVOLUTION_2D; 111 glEnable GL_CONVOLUTION_2D;
109 } 112 }
110 113
111 $self->{fow_texture} = new CFClient::Texture 114 $self->{fow_texture} = new CFClient::Texture
120 glEnable GL_BLEND; 123 glEnable GL_BLEND;
121 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 124 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
122 glEnable GL_TEXTURE_2D; 125 glEnable GL_TEXTURE_2D;
123 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 126 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
124 127
125 glColor +($::CFG->{fow_intensity}) x 3, 1; 128 glColor +($::CFG->{fow_intensity}) x 3, 0.8;
126 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); 129 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
127 130
128 glDisable GL_TEXTURE_2D; 131 glDisable GL_TEXTURE_2D;
129 glDisable GL_BLEND; 132 glDisable GL_BLEND;
130 } 133 }
131 134
132 # HACK BEGIN 135 # HACK BEGIN
133 { 136 {
134 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove 137 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
138
139 glTranslate 0, 30;
135 my ($w, $h) = (250, 250); 140 my ($w, $h) = (250, 250);
141
142 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
136 143
137 glEnable GL_BLEND; 144 glEnable GL_BLEND;
138 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 145 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
139 glEnable GL_TEXTURE_2D; 146 glEnable GL_TEXTURE_2D;
140 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 147 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
141 148
142 $self->{mapmap_texture} = 149 $self->{mapmap_texture} =
143 new CFClient::Texture 150 new CFClient::Texture
144 w => $w, 151 w => $w,
145 h => $h, 152 h => $h,
146 data => $::MAP->mapmap ($w, $h), 153 data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h),
147 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; 154 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
148 155
149 $self->{mapmap_texture}->draw_quad (100, 100); 156 $self->{mapmap_texture}->draw_quad (0, 0);
150 157
151 glDisable GL_TEXTURE_2D; 158 glDisable GL_TEXTURE_2D;
159
160 glTranslate 0.375, 0.375;
161
162 glColor 1, 1, 0, 1;
163 glBegin GL_LINE_LOOP;
164 glVertex $w * 0.5 - $sx , $h * 0.5 - $sy ;
165 glVertex $w * 0.5 - $sx , $h * 0.5 - $sy + $sh;
166 glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy + $sh;
167 glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy ;
168 glEnd;
169
152 glDisable GL_BLEND; 170 glDisable GL_BLEND;
153 } 171 }
154 # HACK END 172 # HACK END
155 } 173 }
156 174
174 glDisable GL_COLOR_LOGIC_OP; 192 glDisable GL_COLOR_LOGIC_OP;
175 } 193 }
176} 194}
177 195
178my %DIR = ( 196my %DIR = (
179 SDLK_KP8, [1, "north"], 197 CFClient::SDLK_KP8, [1, "north"],
180 SDLK_KP9, [2, "northeast"], 198 CFClient::SDLK_KP9, [2, "northeast"],
181 SDLK_KP6, [3, "east"], 199 CFClient::SDLK_KP6, [3, "east"],
182 SDLK_KP3, [4, "southeast"], 200 CFClient::SDLK_KP3, [4, "southeast"],
183 SDLK_KP2, [5, "south"], 201 CFClient::SDLK_KP2, [5, "south"],
184 SDLK_KP1, [6, "southwest"], 202 CFClient::SDLK_KP1, [6, "southwest"],
185 SDLK_KP4, [7, "west"], 203 CFClient::SDLK_KP4, [7, "west"],
186 SDLK_KP7, [8, "northwest"], 204 CFClient::SDLK_KP7, [8, "northwest"],
187 205
188 SDLK_UP, [1, "north"], 206 CFClient::SDLK_UP, [1, "north"],
189 SDLK_RIGHT, [3, "east"], 207 CFClient::SDLK_RIGHT, [3, "east"],
190 SDLK_DOWN, [5, "south"], 208 CFClient::SDLK_DOWN, [5, "south"],
191 SDLK_LEFT, [7, "west"], 209 CFClient::SDLK_LEFT, [7, "west"],
192); 210);
193 211
194sub key_down { 212sub key_down {
195 my ($self, $ev) = @_; 213 my ($self, $ev) = @_;
196 214
197 my $mod = $ev->key_mod; 215 my $mod = $ev->{mod};
198 my $sym = $ev->key_sym; 216 my $sym = $ev->{sym};
199 217
200 if ($sym == SDLK_KP5) { 218 if ($sym == CFClient::SDLK_KP5) {
201 $::CONN->user_send ("stay fire"); 219 $::CONN->user_send ("stay fire");
220 } elsif ($sym == ord ",") {
221 $::CONN->user_send ("take");
202 } elsif ($sym == SDLK_a) { 222 } elsif ($sym == ord "a") {
203 $::CONN->user_send ("apply"); 223 $::CONN->user_send ("apply");
204 } elsif ($sym == SDLK_QUOTE) { 224 } elsif ($sym == ord "'") {
205 $self->emit ('activate_console'); 225 $self->emit ('activate_console');
206 } elsif ($sym == SDLK_SLASH) { 226 } elsif ($sym == ord "/") {
207 $self->emit ('activate_console' => '/'); 227 $self->emit (activate_console => '/');
208 } elsif (exists $DIR{$sym}) { 228 } elsif (exists $DIR{$sym}) {
209 if ($mod & KMOD_SHIFT) { 229 if ($mod & CFClient::KMOD_SHIFT) {
210 $self->{shft}++; 230 $self->{shft}++;
211 $::CONN->user_send ("fire $DIR{$sym}[0]"); 231 $::CONN->user_send ("fire $DIR{$sym}[0]");
212 } elsif ($mod & KMOD_CTRL) { 232 } elsif ($mod & CFClient::KMOD_CTRL) {
213 $self->{ctrl}++; 233 $self->{ctrl}++;
214 $::CONN->user_send ("run $DIR{$sym}[0]"); 234 $::CONN->user_send ("run $DIR{$sym}[0]");
215 } else { 235 } else {
216 $::CONN->user_send ("$DIR{$sym}[1]"); 236 $::CONN->user_send ("$DIR{$sym}[1]");
217 } 237 }
238 } elsif ($ev->{unicode}) {
239 $self->{command_widget} ||=
240 new CFClient::MapWidget::Command::
241 command => $self->{command},
242 can_focus => 1,
243 connect_execute => sub {
244 # todo: support callback instead of user_send
245 $::CONN->user_send ($_[1][1]);
246 },
247 connect_close => sub {
248 (delete $self->{command_widget})->hide;
249 $self->focus_in;
250 },
251 ;
252 $self->{command_widget}->key_down ($ev);
253 return unless $self->{command_widget};
254 $self->{command_widget}->show;
255 $self->{command_widget}->focus_in;
218 } 256 }
219} 257}
220 258
221sub key_up { 259sub key_up {
222 my ($self, $ev) = @_; 260 my ($self, $ev) = @_;
223 261
224 my $mod = $ev->key_mod; 262 my $mod = $ev->{mod};
225 my $sym = $ev->key_sym; 263 my $sym = $ev->{sym};
226 264
227 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 265 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
228 $::CONN->user_send ("fire_stop"); 266 $::CONN->user_send ("fire_stop");
229 } 267 }
230 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 268 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
231 $::CONN->user_send ("run_stop"); 269 $::CONN->user_send ("run_stop");
232 } 270 }
233} 271}
234 272
273sub add_command {
274 my ($self, $command, $tooltip, $widget, $cb) = @_;
275
276 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g;
277
278 push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb];
279}
280
281package CFClient::MapWidget::Command;
282
283use strict;
284
285use CFClient::OpenGL;
286
287our @ISA = CFClient::UI::VBox::;
288
289sub new {
290 my $class = shift;
291
292 my $self = $class->SUPER::new (
293 @_,
294 children => [map
295 CFClient::UI::Label->new (
296 can_hover => 1,
297 can_events => 1,
298 fontsize => $_,
299 ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8
300 ],
301 );
302
303 $self
304}
305
306sub size_allocate {
307 my ($self, $w, $h) = @_;
308
309 $self->SUPER::size_allocate ($w, $h);
310 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
311}
312
313sub update_labels {
314 my ($self) = @_;
315
316 my $command = $self->{command};
317 my $search_abbrev = qr/^\Q$self->{search}/;
318 my $search_full = qr/\Q$self->{search}/;
319
320 my @found;
321
322 for (@$command) {
323 if ($_->[0] =~ $search_abbrev) {
324 push @found, [$_->[0], $_];
325 } elsif ($_->[1] =~ $search_full) {
326 push @found, [$_->[1], $_];
327 }
328 }
329
330 @found = sort { $a->[0] cmp $b->[0] } @found;
331
332 $self->{children}[0]->set_text ("$self->{search}_");
333
334 for (0..5) {
335 $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : "");
336 $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : "");
337 }
338
339 $self->{select} = $found[0][1]
340 if @found;
341
342 if (@found > 6) {
343 $self->{children}[6]->set_text ("...");
344 }
345
346 $self->check_size;
347}
348
349sub key_down {
350 my ($self, $ev) = @_;
351
352 if ($ev->{sym} == 8) {
353 substr $self->{search}, -1, 1, "";
354 $self->update_labels;
355 } elsif ($ev->{sym} == 13) {
356 if (exists $self->{select}) {
357 $self->emit (execute => $self->{select});
358 $self->emit ("close");
359 }
360 } elsif ($ev->{sym} == 27) {
361 $self->emit ("close");
362 return;
363 } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) {
364 $self->{search} .= chr $ev->{unicode};
365 $self->update_labels;
366 }
367
368 length $self->{search}
369 or $self->emit ("close");
370}
371
2351 3721

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines