ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.22
Committed: Sat May 13 00:27:08 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.21: +5 -4 lines
Log Message:
improve keyboard logic

File Contents

# User Rev Content
1 root 1.1 package CFClient::MapWidget;
2    
3     use strict;
4 root 1.14 use utf8;
5 root 1.1
6     use List::Util qw(min max);
7    
8 root 1.4 use CFClient::OpenGL;
9 root 1.1
10     our @ISA = CFClient::UI::Base::;
11    
12     sub new {
13     my $class = shift;
14    
15 root 1.20 my $self = $class->SUPER::new (
16 root 1.1 z => -1,
17     can_focus => 1,
18 root 1.4 list => glGenList,
19 root 1.1 @_
20 root 1.20 );
21    
22     $self->add_command ("killpets", "kills all your summoned and charmed creates");
23    
24     $self
25 root 1.1 }
26    
27 root 1.4 sub DESTROY {
28     my $self = shift;
29    
30     glDeleteList $self->{list};
31    
32     $self->SUPER::DESTROY;
33     }
34    
35 root 1.1 sub button_down {
36     my ($self, $ev, $x, $y) = @_;
37    
38     $self->focus_in;
39    
40 root 1.5 if ($ev->{button} == 2) {
41     my ($ox, $oy) = ($ev->{x}, $ev->{y});
42 root 1.1 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
43    
44     $self->{motion} = sub {
45     my ($ev, $x, $y) = @_;
46    
47 root 1.5 ($x, $y) = ($ev->{x}, $ev->{y});
48 root 1.1
49     $::CFG->{map_shift_x} = $bw + $x - $ox;
50     $::CFG->{map_shift_y} = $bh + $y - $oy;
51    
52     $self->update;
53     };
54     }
55     }
56    
57     sub button_up {
58     my ($self, $ev, $x, $y) = @_;
59    
60     delete $self->{motion};
61     }
62    
63     sub mouse_motion {
64     my ($self, $ev, $x, $y) = @_;
65    
66     $self->{motion}->($ev, $x, $y) if $self->{motion};
67     }
68    
69     sub size_request {
70     (
71     1 + 32 * int $::WIDTH / 32,
72     1 + 32 * int $::HEIGHT / 32,
73     )
74     }
75    
76     sub update {
77     my ($self) = @_;
78    
79     $self->{need_update} = 1;
80     $self->SUPER::update;
81     }
82    
83     sub draw {
84     my ($self) = @_;
85    
86     if (delete $self->{need_update}) {
87 root 1.4 glNewList $self->{list};
88 root 1.1
89     if ($::MAP) {
90 root 1.17 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
91     my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
92 root 1.1
93 root 1.18 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
94     my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
95    
96 root 1.16 glPushMatrix;
97 root 1.9 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
98    
99 root 1.1 glTranslate $sx0 - 32, $sy0 - 32, 0;
100    
101     my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
102    
103     if ($::CFG->{fow_enable}) {
104     if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war
105     glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
106     glConvolutionFilter2D (
107     GL_CONVOLUTION_2D,
108     GL_ALPHA,
109     3, 3,
110     GL_ALPHA, GL_FLOAT,
111     pack "f*",
112 root 1.7 0.05, 0.13, 0.05,
113     0.13, 0.30, 0.13,
114     0.05, 0.13, 0.05,
115 root 1.1 );
116     glEnable GL_CONVOLUTION_2D;
117     }
118    
119     $self->{fow_texture} = new CFClient::Texture
120     w => $w,
121     h => $h,
122     data => $data,
123     internalformat => GL_ALPHA,
124     format => GL_ALPHA;
125    
126     glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
127    
128     glEnable GL_BLEND;
129     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
130     glEnable GL_TEXTURE_2D;
131     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
132    
133 root 1.7 glColor +($::CFG->{fow_intensity}) x 3, 0.8;
134 root 1.1 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
135    
136     glDisable GL_TEXTURE_2D;
137     glDisable GL_BLEND;
138     }
139    
140 root 1.18 glPopMatrix;
141 root 1.1 }
142    
143     glEndList;
144     }
145    
146     glPushMatrix;
147     glCallList $self->{list};
148     glPopMatrix;
149    
150     if ($CFClient::UI::FOCUS != $self) {
151     glColor 64/255, 64/255, 64/255;
152     glLogicOp GL_AND;
153     glEnable GL_COLOR_LOGIC_OP;
154     glBegin GL_QUADS;
155     glVertex 0, 0;
156     glVertex 0, $::HEIGHT;
157     glVertex $::WIDTH, $::HEIGHT;
158     glVertex $::WIDTH, 0;
159     glEnd;
160     glDisable GL_COLOR_LOGIC_OP;
161     }
162     }
163    
164     my %DIR = (
165 root 1.3 CFClient::SDLK_KP8, [1, "north"],
166     CFClient::SDLK_KP9, [2, "northeast"],
167     CFClient::SDLK_KP6, [3, "east"],
168     CFClient::SDLK_KP3, [4, "southeast"],
169     CFClient::SDLK_KP2, [5, "south"],
170     CFClient::SDLK_KP1, [6, "southwest"],
171     CFClient::SDLK_KP4, [7, "west"],
172     CFClient::SDLK_KP7, [8, "northwest"],
173    
174     CFClient::SDLK_UP, [1, "north"],
175     CFClient::SDLK_RIGHT, [3, "east"],
176     CFClient::SDLK_DOWN, [5, "south"],
177     CFClient::SDLK_LEFT, [7, "west"],
178 root 1.1 );
179    
180     sub key_down {
181     my ($self, $ev) = @_;
182    
183 root 1.21 return unless $::CONN;
184    
185 root 1.5 my $mod = $ev->{mod};
186     my $sym = $ev->{sym};
187 root 1.22 my $uni = $ev->{unicode};
188 root 1.1
189 root 1.3 if ($sym == CFClient::SDLK_KP5) {
190 root 1.1 $::CONN->user_send ("stay fire");
191 root 1.22 } elsif ($uni == ord ",") {
192 root 1.14 $::CONN->user_send ("take");
193 root 1.22 } elsif ($uni == ord "a") {
194 root 1.1 $::CONN->user_send ("apply");
195 root 1.22 } elsif ($uni == ord "'") {
196 root 1.1 $self->emit ('activate_console');
197 root 1.22 } elsif ($uni == ord "/") {
198 root 1.8 $self->emit (activate_console => '/');
199 root 1.1 } elsif (exists $DIR{$sym}) {
200 root 1.3 if ($mod & CFClient::KMOD_SHIFT) {
201 root 1.1 $self->{shft}++;
202     $::CONN->user_send ("fire $DIR{$sym}[0]");
203 root 1.3 } elsif ($mod & CFClient::KMOD_CTRL) {
204 root 1.1 $self->{ctrl}++;
205     $::CONN->user_send ("run $DIR{$sym}[0]");
206     } else {
207     $::CONN->user_send ("$DIR{$sym}[1]");
208     }
209 root 1.8 } 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 root 1.11 # todo: support callback instead of user_send
216     $::CONN->user_send ($_[1][1]);
217 root 1.8 },
218     connect_close => sub {
219     (delete $self->{command_widget})->hide;
220     $self->focus_in;
221     },
222     ;
223     $self->{command_widget}->key_down ($ev);
224 root 1.12 return unless $self->{command_widget};
225 root 1.8 $self->{command_widget}->show;
226     $self->{command_widget}->focus_in;
227 root 1.1 }
228     }
229    
230     sub key_up {
231     my ($self, $ev) = @_;
232    
233 root 1.5 my $mod = $ev->{mod};
234     my $sym = $ev->{sym};
235 root 1.1
236 root 1.3 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
237 root 1.1 $::CONN->user_send ("fire_stop");
238     }
239 root 1.3 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
240 root 1.1 $::CONN->user_send ("run_stop");
241     }
242     }
243    
244 root 1.2 sub add_command {
245 root 1.11 my ($self, $command, $tooltip, $widget, $cb) = @_;
246 root 1.2
247 root 1.3 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g;
248 root 1.8
249 root 1.20 $self->{command} = [
250     [$abbrev, $command, $tooltip, $widget, $cb],
251     grep $_->[1] ne $command, @{ $self->{command} },
252     ];
253 root 1.8 }
254    
255 root 1.18 package CFClient::MapWidget::MapMap;
256    
257 root 1.19 our @ISA = CFClient::UI::Base::;
258 root 1.18
259     use Time::HiRes qw(time);
260     use CFClient::OpenGL;
261    
262     sub size_request {
263     ($::HEIGHT * 0.25, $::HEIGHT * 0.25)
264     }
265    
266     sub size_allocate {
267     my ($self, $w, $h) = @_;
268    
269     $self->SUPER::size_allocate ($w, $h);
270     $self->update;
271     }
272    
273     sub update {
274     my ($self) = @_;
275    
276     delete $self->{texture_atime};
277     $self->SUPER::update;
278     }
279    
280     sub _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    
334 root 1.8 package CFClient::MapWidget::Command;
335    
336     use strict;
337    
338     use CFClient::OpenGL;
339    
340     our @ISA = CFClient::UI::VBox::;
341    
342     sub new {
343     my $class = shift;
344    
345     my $self = $class->SUPER::new (
346     @_,
347     children => [map
348     CFClient::UI::Label->new (
349 root 1.11 can_hover => 1,
350     can_events => 1,
351     fontsize => $_,
352 root 1.8 ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8
353     ],
354     );
355    
356     $self
357     }
358    
359     sub 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    
366     sub 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 root 1.20 } elsif (2 < length $self->{search} and $_->[1] =~ $search_full) {
379 root 1.8 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 root 1.11 $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : "");
390 root 1.8 }
391    
392 root 1.11 $self->{select} = $found[0][1]
393 root 1.8 if @found;
394    
395     if (@found > 6) {
396     $self->{children}[6]->set_text ("...");
397     }
398    
399     $self->check_size;
400     }
401    
402     sub 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 root 1.13 return;
416 root 1.10 } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) {
417 root 1.8 $self->{search} .= chr $ev->{unicode};
418     $self->update_labels;
419     }
420 root 1.10
421     length $self->{search}
422     or $self->emit ("close");
423 root 1.2 }
424    
425 root 1.1 1