ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.11
Committed: Mon Apr 24 03:33:51 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.10: +9 -7 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 package CFClient::MapWidget;
2    
3     use strict;
4    
5     use List::Util qw(min max);
6    
7 root 1.4 use CFClient::OpenGL;
8 root 1.1
9     our @ISA = CFClient::UI::Base::;
10    
11     sub new {
12     my $class = shift;
13    
14     $class->SUPER::new (
15     z => -1,
16     can_focus => 1,
17 root 1.4 list => glGenList,
18 root 1.1 @_
19     )
20     }
21    
22 root 1.4 sub DESTROY {
23     my $self = shift;
24    
25     glDeleteList $self->{list};
26    
27     $self->SUPER::DESTROY;
28     }
29    
30 root 1.1 sub button_down {
31     my ($self, $ev, $x, $y) = @_;
32    
33     $self->focus_in;
34    
35 root 1.5 if ($ev->{button} == 2) {
36     my ($ox, $oy) = ($ev->{x}, $ev->{y});
37 root 1.1 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
38    
39     $self->{motion} = sub {
40     my ($ev, $x, $y) = @_;
41    
42 root 1.5 ($x, $y) = ($ev->{x}, $ev->{y});
43 root 1.1
44     $::CFG->{map_shift_x} = $bw + $x - $ox;
45     $::CFG->{map_shift_y} = $bh + $y - $oy;
46    
47     $self->update;
48     };
49     }
50     }
51    
52     sub button_up {
53     my ($self, $ev, $x, $y) = @_;
54    
55     delete $self->{motion};
56     }
57    
58     sub mouse_motion {
59     my ($self, $ev, $x, $y) = @_;
60    
61     $self->{motion}->($ev, $x, $y) if $self->{motion};
62     }
63    
64     sub size_request {
65     (
66     1 + 32 * int $::WIDTH / 32,
67     1 + 32 * int $::HEIGHT / 32,
68     )
69     }
70    
71     sub update {
72     my ($self) = @_;
73    
74     $self->{need_update} = 1;
75     $self->SUPER::update;
76     }
77    
78     sub draw {
79     my ($self) = @_;
80    
81     if (delete $self->{need_update}) {
82 root 1.4 glNewList $self->{list};
83 root 1.1
84     if ($::MAP) {
85 root 1.9 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale});
86     my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale});
87 root 1.1
88 root 1.9 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
89    
90     my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
91     my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
92 root 1.1
93     glTranslate $sx0 - 32, $sy0 - 32, 0;
94    
95     my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
96    
97     if ($::CFG->{fow_enable}) {
98     if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war
99     glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
100     glConvolutionFilter2D (
101     GL_CONVOLUTION_2D,
102     GL_ALPHA,
103     3, 3,
104     GL_ALPHA, GL_FLOAT,
105     pack "f*",
106 root 1.7 0.05, 0.13, 0.05,
107     0.13, 0.30, 0.13,
108     0.05, 0.13, 0.05,
109 root 1.1 );
110     glEnable GL_CONVOLUTION_2D;
111     }
112    
113     $self->{fow_texture} = new CFClient::Texture
114     w => $w,
115     h => $h,
116     data => $data,
117     internalformat => GL_ALPHA,
118     format => GL_ALPHA;
119    
120     glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
121    
122     glEnable GL_BLEND;
123     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
124     glEnable GL_TEXTURE_2D;
125     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
126    
127 root 1.7 glColor +($::CFG->{fow_intensity}) x 3, 0.8;
128 root 1.1 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
129    
130     glDisable GL_TEXTURE_2D;
131     glDisable GL_BLEND;
132     }
133    
134     # HACK BEGIN
135     {
136     glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
137 root 1.6
138     glTranslate 0, 30;
139 root 1.1 my ($w, $h) = (250, 250);
140    
141 root 1.6 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
142    
143 root 1.1 glEnable GL_BLEND;
144     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
145     glEnable GL_TEXTURE_2D;
146     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
147    
148     $self->{mapmap_texture} =
149     new CFClient::Texture
150     w => $w,
151     h => $h,
152 root 1.6 data => $::MAP->mapmap (- $w * 0.5, - $h * 0.5, $w, $h),
153 root 1.1 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
154    
155 root 1.6 $self->{mapmap_texture}->draw_quad (0, 0);
156 root 1.1
157     glDisable GL_TEXTURE_2D;
158 root 1.6
159     glTranslate 0.375, 0.375;
160    
161     glColor 1, 1, 0, 1;
162     glBegin GL_LINE_LOOP;
163     glVertex $w * 0.5 - $sx , $h * 0.5 - $sy ;
164     glVertex $w * 0.5 - $sx , $h * 0.5 - $sy + $sh;
165     glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy + $sh;
166     glVertex $w * 0.5 - $sx + $sw, $h * 0.5 - $sy ;
167     glEnd;
168    
169 root 1.1 glDisable GL_BLEND;
170     }
171     # HACK END
172     }
173    
174     glEndList;
175     }
176    
177     glPushMatrix;
178     glCallList $self->{list};
179     glPopMatrix;
180    
181     if ($CFClient::UI::FOCUS != $self) {
182     glColor 64/255, 64/255, 64/255;
183     glLogicOp GL_AND;
184     glEnable GL_COLOR_LOGIC_OP;
185     glBegin GL_QUADS;
186     glVertex 0, 0;
187     glVertex 0, $::HEIGHT;
188     glVertex $::WIDTH, $::HEIGHT;
189     glVertex $::WIDTH, 0;
190     glEnd;
191     glDisable GL_COLOR_LOGIC_OP;
192     }
193     }
194    
195     my %DIR = (
196 root 1.3 CFClient::SDLK_KP8, [1, "north"],
197     CFClient::SDLK_KP9, [2, "northeast"],
198     CFClient::SDLK_KP6, [3, "east"],
199     CFClient::SDLK_KP3, [4, "southeast"],
200     CFClient::SDLK_KP2, [5, "south"],
201     CFClient::SDLK_KP1, [6, "southwest"],
202     CFClient::SDLK_KP4, [7, "west"],
203     CFClient::SDLK_KP7, [8, "northwest"],
204    
205     CFClient::SDLK_UP, [1, "north"],
206     CFClient::SDLK_RIGHT, [3, "east"],
207     CFClient::SDLK_DOWN, [5, "south"],
208     CFClient::SDLK_LEFT, [7, "west"],
209 root 1.1 );
210    
211     sub key_down {
212     my ($self, $ev) = @_;
213    
214 root 1.5 my $mod = $ev->{mod};
215     my $sym = $ev->{sym};
216 root 1.1
217 root 1.3 if ($sym == CFClient::SDLK_KP5) {
218 root 1.1 $::CONN->user_send ("stay fire");
219 root 1.3 } elsif ($sym == ord "a") {
220 root 1.1 $::CONN->user_send ("apply");
221 root 1.3 } elsif ($sym == ord "'") {
222 root 1.1 $self->emit ('activate_console');
223 root 1.3 } elsif ($sym == ord "/") {
224 root 1.8 $self->emit (activate_console => '/');
225 root 1.1 } elsif (exists $DIR{$sym}) {
226 root 1.3 if ($mod & CFClient::KMOD_SHIFT) {
227 root 1.1 $self->{shft}++;
228     $::CONN->user_send ("fire $DIR{$sym}[0]");
229 root 1.3 } elsif ($mod & CFClient::KMOD_CTRL) {
230 root 1.1 $self->{ctrl}++;
231     $::CONN->user_send ("run $DIR{$sym}[0]");
232     } else {
233     $::CONN->user_send ("$DIR{$sym}[1]");
234     }
235 root 1.8 } elsif ($ev->{unicode}) {
236     $self->{command_widget} ||=
237     new CFClient::MapWidget::Command::
238     command => $self->{command},
239     can_focus => 1,
240     connect_execute => sub {
241 root 1.11 # todo: support callback instead of user_send
242     $::CONN->user_send ($_[1][1]);
243 root 1.8 },
244     connect_close => sub {
245     (delete $self->{command_widget})->hide;
246     $self->focus_in;
247     },
248     ;
249     $self->{command_widget}->key_down ($ev);
250     $self->{command_widget}->show;
251     $self->{command_widget}->focus_in;
252 root 1.1 }
253     }
254    
255     sub key_up {
256     my ($self, $ev) = @_;
257    
258 root 1.5 my $mod = $ev->{mod};
259     my $sym = $ev->{sym};
260 root 1.1
261 root 1.3 if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
262 root 1.1 $::CONN->user_send ("fire_stop");
263     }
264 root 1.3 if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
265 root 1.1 $::CONN->user_send ("run_stop");
266     }
267     }
268    
269 root 1.2 sub add_command {
270 root 1.11 my ($self, $command, $tooltip, $widget, $cb) = @_;
271 root 1.2
272 root 1.3 (my $abbrev = $command) =~ s/(\S)[^[:space:]_]*[[:space:]_]+/$1/g;
273 root 1.8
274 root 1.11 push @{$self->{command}}, [$abbrev, $command, $tooltip, $widget, $cb];
275 root 1.8 }
276    
277     package CFClient::MapWidget::Command;
278    
279     use strict;
280    
281     use CFClient::OpenGL;
282    
283     our @ISA = CFClient::UI::VBox::;
284    
285     sub new {
286     my $class = shift;
287    
288     my $self = $class->SUPER::new (
289     @_,
290     children => [map
291     CFClient::UI::Label->new (
292 root 1.11 can_hover => 1,
293     can_events => 1,
294     fontsize => $_,
295 root 1.8 ), 1, 1, 0.8, 0.8, 0.8, 0.8, 0.8
296     ],
297     );
298    
299     $self
300     }
301    
302     sub size_allocate {
303     my ($self, $w, $h) = @_;
304    
305     $self->SUPER::size_allocate ($w, $h);
306     $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
307     }
308    
309     sub update_labels {
310     my ($self) = @_;
311    
312     my $command = $self->{command};
313     my $search_abbrev = qr/^\Q$self->{search}/;
314     my $search_full = qr/\Q$self->{search}/;
315    
316     my @found;
317    
318     for (@$command) {
319     if ($_->[0] =~ $search_abbrev) {
320     push @found, [$_->[0], $_];
321     } elsif ($_[1] =~ $search_full) {
322     push @found, [$_->[1], $_];
323     }
324     }
325    
326     @found = sort { $a->[0] cmp $b->[0] } @found;
327    
328     $self->{children}[0]->set_text ("$self->{search}_");
329    
330     for (0..5) {
331     $self->{children}[$_ + 1]->set_text ($found[$_] ? "$found[$_][0] ($found[$_][1][1])" : "");
332 root 1.11 $self->{children}[$_ + 1]{tooltip} = ($found[$_] ? $found[$_][1][2] : "");
333 root 1.8 }
334    
335 root 1.11 $self->{select} = $found[0][1]
336 root 1.8 if @found;
337    
338     if (@found > 6) {
339     $self->{children}[6]->set_text ("...");
340     }
341    
342     $self->check_size;
343     }
344    
345     sub key_down {
346     my ($self, $ev) = @_;
347    
348     if ($ev->{sym} == 8) {
349     substr $self->{search}, -1, 1, "";
350     $self->update_labels;
351     } elsif ($ev->{sym} == 13) {
352     if (exists $self->{select}) {
353     $self->emit (execute => $self->{select});
354     $self->emit ("close");
355     }
356     } elsif ($ev->{sym} == 27) {
357     $self->emit ("close");
358 root 1.10 } elsif ((chr $ev->{unicode}) =~ /^[[:alpha:]]$/) {
359 root 1.8 $self->{search} .= chr $ev->{unicode};
360     $self->update_labels;
361     }
362 root 1.10
363     length $self->{search}
364     or $self->emit ("close");
365 root 1.2 }
366    
367 root 1.1 1