ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.18
Committed: Tue Apr 25 12:56:34 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.17: +83 -43 lines
Log Message:
update mapmap three times/s max.

File Contents

# Content
1 package CFClient::MapWidget;
2
3 use strict;
4 use utf8;
5
6 use List::Util qw(min max);
7
8 use CFClient::OpenGL;
9
10 our @ISA = CFClient::UI::Base::;
11
12 sub new {
13 my $class = shift;
14
15 $class->SUPER::new (
16 z => -1,
17 can_focus => 1,
18 list => glGenList,
19 @_
20 )
21 }
22
23 sub DESTROY {
24 my $self = shift;
25
26 glDeleteList $self->{list};
27
28 $self->SUPER::DESTROY;
29 }
30
31 sub button_down {
32 my ($self, $ev, $x, $y) = @_;
33
34 $self->focus_in;
35
36 if ($ev->{button} == 2) {
37 my ($ox, $oy) = ($ev->{x}, $ev->{y});
38 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
39
40 $self->{motion} = sub {
41 my ($ev, $x, $y) = @_;
42
43 ($x, $y) = ($ev->{x}, $ev->{y});
44
45 $::CFG->{map_shift_x} = $bw + $x - $ox;
46 $::CFG->{map_shift_y} = $bh + $y - $oy;
47
48 $self->update;
49 };
50 }
51 }
52
53 sub button_up {
54 my ($self, $ev, $x, $y) = @_;
55
56 delete $self->{motion};
57 }
58
59 sub mouse_motion {
60 my ($self, $ev, $x, $y) = @_;
61
62 $self->{motion}->($ev, $x, $y) if $self->{motion};
63 }
64
65 sub size_request {
66 (
67 1 + 32 * int $::WIDTH / 32,
68 1 + 32 * int $::HEIGHT / 32,
69 )
70 }
71
72 sub update {
73 my ($self) = @_;
74
75 $self->{need_update} = 1;
76 $self->SUPER::update;
77 }
78
79 sub draw {
80 my ($self) = @_;
81
82 if (delete $self->{need_update}) {
83 glNewList $self->{list};
84
85 if ($::MAP) {
86 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
87 my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
88
89 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 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};
94
95 glTranslate $sx0 - 32, $sy0 - 32, 0;
96
97 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
98
99 if ($::CFG->{fow_enable}) {
100 if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war
101 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
102 glConvolutionFilter2D (
103 GL_CONVOLUTION_2D,
104 GL_ALPHA,
105 3, 3,
106 GL_ALPHA, GL_FLOAT,
107 pack "f*",
108 0.05, 0.13, 0.05,
109 0.13, 0.30, 0.13,
110 0.05, 0.13, 0.05,
111 );
112 glEnable GL_CONVOLUTION_2D;
113 }
114
115 $self->{fow_texture} = new CFClient::Texture
116 w => $w,
117 h => $h,
118 data => $data,
119 internalformat => GL_ALPHA,
120 format => GL_ALPHA;
121
122 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
123
124 glEnable GL_BLEND;
125 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
126 glEnable GL_TEXTURE_2D;
127 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
128
129 glColor +($::CFG->{fow_intensity}) x 3, 0.8;
130 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
131
132 glDisable GL_TEXTURE_2D;
133 glDisable GL_BLEND;
134 }
135
136 glPopMatrix;
137 }
138
139 glEndList;
140 }
141
142 glPushMatrix;
143 glCallList $self->{list};
144 glPopMatrix;
145
146 if ($CFClient::UI::FOCUS != $self) {
147 glColor 64/255, 64/255, 64/255;
148 glLogicOp GL_AND;
149 glEnable GL_COLOR_LOGIC_OP;
150 glBegin GL_QUADS;
151 glVertex 0, 0;
152 glVertex 0, $::HEIGHT;
153 glVertex $::WIDTH, $::HEIGHT;
154 glVertex $::WIDTH, 0;
155 glEnd;
156 glDisable GL_COLOR_LOGIC_OP;
157 }
158 }
159
160 my %DIR = (
161 CFClient::SDLK_KP8, [1, "north"],
162 CFClient::SDLK_KP9, [2, "northeast"],
163 CFClient::SDLK_KP6, [3, "east"],
164 CFClient::SDLK_KP3, [4, "southeast"],
165 CFClient::SDLK_KP2, [5, "south"],
166 CFClient::SDLK_KP1, [6, "southwest"],
167 CFClient::SDLK_KP4, [7, "west"],
168 CFClient::SDLK_KP7, [8, "northwest"],
169
170 CFClient::SDLK_UP, [1, "north"],
171 CFClient::SDLK_RIGHT, [3, "east"],
172 CFClient::SDLK_DOWN, [5, "south"],
173 CFClient::SDLK_LEFT, [7, "west"],
174 );
175
176 sub key_down {
177 my ($self, $ev) = @_;
178
179 my $mod = $ev->{mod};
180 my $sym = $ev->{sym};
181
182 if ($sym == CFClient::SDLK_KP5) {
183 $::CONN->user_send ("stay fire");
184 } elsif ($sym == ord ",") {
185 $::CONN->user_send ("take");
186 } elsif ($sym == ord "a") {
187 $::CONN->user_send ("apply");
188 } elsif ($sym == ord "'") {
189 $self->emit ('activate_console');
190 } elsif ($sym == ord "/") {
191 $self->emit (activate_console => '/');
192 } elsif (exists $DIR{$sym}) {
193 if ($mod & CFClient::KMOD_SHIFT) {
194 $self->{shft}++;
195 $::CONN->user_send ("fire $DIR{$sym}[0]");
196 } elsif ($mod & CFClient::KMOD_CTRL) {
197 $self->{ctrl}++;
198 $::CONN->user_send ("run $DIR{$sym}[0]");
199 } else {
200 $::CONN->user_send ("$DIR{$sym}[1]");
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;
220 }
221 }
222
223 sub key_up {
224 my ($self, $ev) = @_;
225
226 my $mod = $ev->{mod};
227 my $sym = $ev->{sym};
228
229 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
230 $::CONN->user_send ("fire_stop");
231 }
232 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
233 $::CONN->user_send ("run_stop");
234 }
235 }
236
237 sub add_command {
238 my ($self, $command, $tooltip, $widget, $cb) = @_;
239
240 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g;
241
242 push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb];
243 }
244
245 package CFClient::MapWidget::MapMap;
246
247 use base CFClient::UI::Base::;
248
249 use Time::HiRes qw(time);
250 use CFClient::OpenGL;
251
252 sub size_request {
253 ($::HEIGHT * 0.25, $::HEIGHT * 0.25)
254 }
255
256 sub size_allocate {
257 my ($self, $w, $h) = @_;
258
259 $self->SUPER::size_allocate ($w, $h);
260 $self->update;
261 }
262
263 sub update {
264 my ($self) = @_;
265
266 delete $self->{texture_atime};
267 $self->SUPER::update;
268 }
269
270 sub _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
324 package CFClient::MapWidget::Command;
325
326 use strict;
327
328 use CFClient::OpenGL;
329
330 our @ISA = CFClient::UI::VBox::;
331
332 sub 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
349 sub 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
356 sub 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
392 sub 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");
413 }
414
415 1