ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.17
Committed: Mon Apr 24 13:12:32 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.16: +2 -2 lines
Log Message:
fix (or actually: just improve) rounding issue

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 glPushMatrix;
90 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
91
92 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
93 my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
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 # HACK BEGIN
137 {
138 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
139
140 glTranslate 0, 30;
141 my ($w, $h) = (250, 250);
142
143 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
144
145 glEnable GL_BLEND;
146 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
147 glEnable GL_TEXTURE_2D;
148 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
149
150 glPopMatrix;
151
152 $self->{mapmap_texture} =
153 new CFClient::Texture
154 w => $w,
155 h => $h,
156 data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h),
157 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
158
159 $self->{mapmap_texture}->draw_quad (0, 0);
160
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
173 glDisable GL_BLEND;
174 }
175 # HACK END
176 }
177
178 glEndList;
179 }
180
181 glPushMatrix;
182 glCallList $self->{list};
183 glPopMatrix;
184
185 if ($CFClient::UI::FOCUS != $self) {
186 glColor 64/255, 64/255, 64/255;
187 glLogicOp GL_AND;
188 glEnable GL_COLOR_LOGIC_OP;
189 glBegin GL_QUADS;
190 glVertex 0, 0;
191 glVertex 0, $::HEIGHT;
192 glVertex $::WIDTH, $::HEIGHT;
193 glVertex $::WIDTH, 0;
194 glEnd;
195 glDisable GL_COLOR_LOGIC_OP;
196 }
197 }
198
199 my %DIR = (
200 CFClient::SDLK_KP8, [1, "north"],
201 CFClient::SDLK_KP9, [2, "northeast"],
202 CFClient::SDLK_KP6, [3, "east"],
203 CFClient::SDLK_KP3, [4, "southeast"],
204 CFClient::SDLK_KP2, [5, "south"],
205 CFClient::SDLK_KP1, [6, "southwest"],
206 CFClient::SDLK_KP4, [7, "west"],
207 CFClient::SDLK_KP7, [8, "northwest"],
208
209 CFClient::SDLK_UP, [1, "north"],
210 CFClient::SDLK_RIGHT, [3, "east"],
211 CFClient::SDLK_DOWN, [5, "south"],
212 CFClient::SDLK_LEFT, [7, "west"],
213 );
214
215 sub key_down {
216 my ($self, $ev) = @_;
217
218 my $mod = $ev->{mod};
219 my $sym = $ev->{sym};
220
221 if ($sym == CFClient::SDLK_KP5) {
222 $::CONN->user_send ("stay fire");
223 } elsif ($sym == ord ",") {
224 $::CONN->user_send ("take");
225 } elsif ($sym == ord "a") {
226 $::CONN->user_send ("apply");
227 } elsif ($sym == ord "'") {
228 $self->emit ('activate_console');
229 } elsif ($sym == ord "/") {
230 $self->emit (activate_console => '/');
231 } elsif (exists $DIR{$sym}) {
232 if ($mod & CFClient::KMOD_SHIFT) {
233 $self->{shft}++;
234 $::CONN->user_send ("fire $DIR{$sym}[0]");
235 } elsif ($mod & CFClient::KMOD_CTRL) {
236 $self->{ctrl}++;
237 $::CONN->user_send ("run $DIR{$sym}[0]");
238 } else {
239 $::CONN->user_send ("$DIR{$sym}[1]");
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;
259 }
260 }
261
262 sub key_up {
263 my ($self, $ev) = @_;
264
265 my $mod = $ev->{mod};
266 my $sym = $ev->{sym};
267
268 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
269 $::CONN->user_send ("fire_stop");
270 }
271 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
272 $::CONN->user_send ("run_stop");
273 }
274 }
275
276 sub add_command {
277 my ($self, $command, $tooltip, $widget, $cb) = @_;
278
279 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g;
280
281 push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb];
282 }
283
284 package CFClient::MapWidget::Command;
285
286 use strict;
287
288 use CFClient::OpenGL;
289
290 our @ISA = CFClient::UI::VBox::;
291
292 sub 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
309 sub 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
316 sub 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
352 sub 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");
373 }
374
375 1