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.7 by root, Thu Apr 20 21:28:50 2006 UTC vs.
Revision 1.18 by root, Tue Apr 25 12:56:34 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 CFClient::OpenGL; 8use CFClient::OpenGL;
8 9
25 glDeleteList $self->{list}; 26 glDeleteList $self->{list};
26 27
27 $self->SUPER::DESTROY; 28 $self->SUPER::DESTROY;
28} 29}
29 30
30sub key_down {
31 print "MAPKEYDOWN\n";
32}
33
34sub key_up {
35}
36
37sub button_down { 31sub button_down {
38 my ($self, $ev, $x, $y) = @_; 32 my ($self, $ev, $x, $y) = @_;
39 33
40 $self->focus_in; 34 $self->focus_in;
41 35
87 81
88 if (delete $self->{need_update}) { 82 if (delete $self->{need_update}) {
89 glNewList $self->{list}; 83 glNewList $self->{list};
90 84
91 if ($::MAP) { 85 if ($::MAP) {
92 my $sw = int $::WIDTH / 32; 86 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
93 my $sh = int $::HEIGHT / 32; 87 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
94 88
95 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; 89 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
96 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; 90 my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
91
92 glPushMatrix;
93 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
97 94
98 glTranslate $sx0 - 32, $sy0 - 32, 0; 95 glTranslate $sx0 - 32, $sy0 - 32, 0;
99 96
100 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); 97 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
101 98
134 131
135 glDisable GL_TEXTURE_2D; 132 glDisable GL_TEXTURE_2D;
136 glDisable GL_BLEND; 133 glDisable GL_BLEND;
137 } 134 }
138 135
139 # HACK BEGIN 136 glPopMatrix;
140 {
141 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
142
143 glTranslate 0, 30;
144 my ($w, $h) = (250, 250);
145
146 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
147
148 glEnable GL_BLEND;
149 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
150 glEnable GL_TEXTURE_2D;
151 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
152
153 $self->{mapmap_texture} =
154 new CFClient::Texture
155 w => $w,
156 h => $h,
157 data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h),
158 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
159
160 $self->{mapmap_texture}->draw_quad (0, 0);
161
162 glDisable GL_TEXTURE_2D;
163
164 glTranslate 0.375, 0.375;
165
166 glColor 1, 1, 0, 1;
167 glBegin GL_LINE_LOOP;
168 glVertex $w * 0.5 - $sx , $h * 0.5 - $sy ;
169 glVertex $w * 0.5 - $sx , $h * 0.5 - $sy + $sh;
170 glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy + $sh;
171 glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy ;
172 glEnd;
173
174 glDisable GL_BLEND;
175 }
176 # HACK END
177 } 137 }
178 138
179 glEndList; 139 glEndList;
180 } 140 }
181 141
219 my $mod = $ev->{mod}; 179 my $mod = $ev->{mod};
220 my $sym = $ev->{sym}; 180 my $sym = $ev->{sym};
221 181
222 if ($sym == CFClient::SDLK_KP5) { 182 if ($sym == CFClient::SDLK_KP5) {
223 $::CONN->user_send ("stay fire"); 183 $::CONN->user_send ("stay fire");
184 } elsif ($sym == ord ",") {
185 $::CONN->user_send ("take");
224 } elsif ($sym == ord "a") { 186 } elsif ($sym == ord "a") {
225 $::CONN->user_send ("apply"); 187 $::CONN->user_send ("apply");
226 } elsif ($sym == ord "'") { 188 } elsif ($sym == ord "'") {
227 $self->emit ('activate_console'); 189 $self->emit ('activate_console');
228 } elsif ($sym == ord "/") { 190 } elsif ($sym == ord "/") {
229 $self->emit ('activate_console' => '/'); 191 $self->emit (activate_console => '/');
230 } elsif (exists $DIR{$sym}) { 192 } elsif (exists $DIR{$sym}) {
231 if ($mod & CFClient::KMOD_SHIFT) { 193 if ($mod & CFClient::KMOD_SHIFT) {
232 $self->{shft}++; 194 $self->{shft}++;
233 $::CONN->user_send ("fire $DIR{$sym}[0]"); 195 $::CONN->user_send ("fire $DIR{$sym}[0]");
234 } elsif ($mod & CFClient::KMOD_CTRL) { 196 } elsif ($mod & CFClient::KMOD_CTRL) {
235 $self->{ctrl}++; 197 $self->{ctrl}++;
236 $::CONN->user_send ("run $DIR{$sym}[0]"); 198 $::CONN->user_send ("run $DIR{$sym}[0]");
237 } else { 199 } else {
238 $::CONN->user_send ("$DIR{$sym}[1]"); 200 $::CONN->user_send ("$DIR{$sym}[1]");
239 } 201 }
202 } elsif ($ev->{unicode}) {
203 $self->{command_widget} ||=
204 new CFClient::MapWidget::Command::
205 command => $self->{command},
206 can_focus => 1,
207 connect_execute => sub {
208 # todo: support callback instead of user_send
209 $::CONN->user_send ($_[1][1]);
210 },
211 connect_close => sub {
212 (delete $self->{command_widget})->hide;
213 $self->focus_in;
214 },
215 ;
216 $self->{command_widget}->key_down ($ev);
217 return unless $self->{command_widget};
218 $self->{command_widget}->show;
219 $self->{command_widget}->focus_in;
240 } 220 }
241} 221}
242 222
243sub key_up { 223sub key_up {
244 my ($self, $ev) = @_; 224 my ($self, $ev) = @_;
253 $::CONN->user_send ("run_stop"); 233 $::CONN->user_send ("run_stop");
254 } 234 }
255} 235}
256 236
257sub add_command { 237sub add_command {
258 my ($self, $command, $widget, $cb) = @_; 238 my ($self, $command, $tooltip, $widget, $cb) = @_;
259 239
260 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g; 240 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g;
261 warn "$command|$abbrev|$widget\n";#d# 241
242 push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb];
243}
244
245package CFClient::MapWidget::MapMap;
246
247use base CFClient::UI::Base::;
248
249use Time::HiRes qw(time);
250use CFClient::OpenGL;
251
252sub size_request {
253 ($::HEIGHT * 0.25, $::HEIGHT * 0.25)
254}
255
256sub size_allocate {
257 my ($self, $w, $h) = @_;
258
259 $self->SUPER::size_allocate ($w, $h);
260 $self->update;
261}
262
263sub update {
264 my ($self) = @_;
265
266 delete $self->{texture_atime};
267 $self->SUPER::update;
268}
269
270sub _draw {
271 my ($self) = @_;
272
273 $::MAP or return;
274
275 my ($w, $h) = @$self{qw(w h)};
276
277 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
278 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
279
280 my $sx = int $::CFG->{map_shift_x} / 32;
281 my $sy = int $::CFG->{map_shift_y} / 32;
282
283 my $ox = 0.5 * ($w - $sw);
284 my $oy = 0.5 * ($h - $sh);
285
286 glEnable GL_BLEND;
287 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
288 glEnable GL_TEXTURE_2D;
289 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
290
291 if ($self->{texture_atime} < time) {
292 $self->{texture_atime} = time + 1/3;
293
294 $self->{texture} =
295 new CFClient::Texture
296 w => $w,
297 h => $h,
298 data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
299 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
300 }
301
302 $self->{texture}->draw_quad (0, 0);
303
304 glDisable GL_TEXTURE_2D;
305
306 glTranslate 0.375, 0.375;
307
308 #TODO: map scale is completely borked
309
310 my $x0 = int $ox - $sx + 0.5;
311 my $y0 = int $oy - $sy + 0.5;
312
313 glColor 1, 1, 0, 1;
314 glBegin GL_LINE_LOOP;
315 glVertex $x0 , $y0 ;
316 glVertex $x0 , $y0 + $sh;
317 glVertex $x0 + $sw, $y0 + $sh;
318 glVertex $x0 + $sw, $y0 ;
319 glEnd;
320
321 glDisable GL_BLEND;
322}
323
324package CFClient::MapWidget::Command;
325
326use strict;
327
328use CFClient::OpenGL;
329
330our @ISA = CFClient::UI::VBox::;
331
332sub new {
333 my $class = shift;
334
335 my $self = $class->SUPER::new (
336 @_,
337 children => [map
338 CFClient::UI::Label->new (
339 can_hover => 1,
340 can_events => 1,
341 fontsize => $_,
342 ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8
343 ],
344 );
345
346 $self
347}
348
349sub size_allocate {
350 my ($self, $w, $h) = @_;
351
352 $self->SUPER::size_allocate ($w, $h);
353 $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
354}
355
356sub update_labels {
357 my ($self) = @_;
358
359 my $command = $self->{command};
360 my $search_abbrev = qr/^\Q$self->{search}/;
361 my $search_full = qr/\Q$self->{search}/;
362
363 my @found;
364
365 for (@$command) {
366 if ($_->[0] =~ $search_abbrev) {
367 push @found, [$_->[0], $_];
368 } elsif ($_->[1] =~ $search_full) {
369 push @found, [$_->[1], $_];
370 }
371 }
372
373 @found = sort { $a->[0] cmp $b->[0] } @found;
374
375 $self->{children}[0]->set_text ("$self->{search}_");
376
377 for (0..5) {
378 $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : "");
379 $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : "");
380 }
381
382 $self->{select} = $found[0][1]
383 if @found;
384
385 if (@found > 6) {
386 $self->{children}[6]->set_text ("...");
387 }
388
389 $self->check_size;
390}
391
392sub key_down {
393 my ($self, $ev) = @_;
394
395 if ($ev->{sym} == 8) {
396 substr $self->{search}, -1, 1, "";
397 $self->update_labels;
398 } elsif ($ev->{sym} == 13) {
399 if (exists $self->{select}) {
400 $self->emit (execute => $self->{select});
401 $self->emit ("close");
402 }
403 } elsif ($ev->{sym} == 27) {
404 $self->emit ("close");
405 return;
406 } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) {
407 $self->{search} .= chr $ev->{unicode};
408 $self->update_labels;
409 }
410
411 length $self->{search}
412 or $self->emit ("close");
262} 413}
263 414
2641 4151

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines