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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines