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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines