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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines