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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines