ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/MapWidget.pm
Revision: 1.36
Committed: Thu May 25 00:26:19 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.35: +184 -154 lines
Log Message:
improve focus model, improve completer behaviour

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 root 1.36 $self->{completer} = new CFClient::MapWidget::Command::
23     command => $self->{command},
24     can_focus => 1,
25     ;
26    
27 root 1.20 $self
28 root 1.1 }
29    
30 root 1.36 sub add_command {
31     my ($self, $command, $tooltip, $widget, $cb) = @_;
32    
33     (my $data = $command) =~ s/\\//g;
34    
35     $tooltip =~ s/^\s+//;
36    
37     $tooltip = "<big>$data</big>\n\n$tooltip";
38    
39     $tooltip =~ s/\s+$//;
40    
41     $self->{completer}{command}{$command} = [$data, $tooltip, $widget, $cb, ++$self->{command_id}];
42     }
43 root 1.4
44 root 1.36 sub clr_commands {
45     my ($self) = @_;
46 root 1.4
47 root 1.36 %{$self->{completer}{command}} = ();
48 root 1.4 }
49    
50 root 1.1 sub button_down {
51     my ($self, $ev, $x, $y) = @_;
52    
53     $self->focus_in;
54    
55 root 1.5 if ($ev->{button} == 2) {
56     my ($ox, $oy) = ($ev->{x}, $ev->{y});
57 root 1.1 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
58    
59     $self->{motion} = sub {
60     my ($ev, $x, $y) = @_;
61    
62 root 1.5 ($x, $y) = ($ev->{x}, $ev->{y});
63 root 1.1
64     $::CFG->{map_shift_x} = $bw + $x - $ox;
65     $::CFG->{map_shift_y} = $bh + $y - $oy;
66    
67     $self->update;
68     };
69     }
70     }
71    
72     sub button_up {
73     my ($self, $ev, $x, $y) = @_;
74    
75     delete $self->{motion};
76     }
77    
78     sub mouse_motion {
79     my ($self, $ev, $x, $y) = @_;
80    
81     $self->{motion}->($ev, $x, $y) if $self->{motion};
82     }
83    
84     sub size_request {
85     (
86     1 + 32 * int $::WIDTH / 32,
87     1 + 32 * int $::HEIGHT / 32,
88     )
89     }
90    
91     sub update {
92     my ($self) = @_;
93    
94     $self->{need_update} = 1;
95     $self->SUPER::update;
96     }
97    
98 root 1.36 my %DIR = (
99     CFClient::SDLK_KP8, [1, "north"],
100     CFClient::SDLK_KP9, [2, "northeast"],
101     CFClient::SDLK_KP6, [3, "east"],
102     CFClient::SDLK_KP3, [4, "southeast"],
103     CFClient::SDLK_KP2, [5, "south"],
104     CFClient::SDLK_KP1, [6, "southwest"],
105     CFClient::SDLK_KP4, [7, "west"],
106     CFClient::SDLK_KP7, [8, "northwest"],
107    
108     CFClient::SDLK_UP, [1, "north"],
109     CFClient::SDLK_RIGHT, [3, "east"],
110     CFClient::SDLK_DOWN, [5, "south"],
111     CFClient::SDLK_LEFT, [7, "west"],
112     );
113    
114     sub key_down {
115     my ($self, $ev) = @_;
116    
117     return unless $::CONN;
118    
119     my $mod = $ev->{mod};
120     my $sym = $ev->{sym};
121     my $uni = $ev->{unicode};
122    
123     if ($sym == CFClient::SDLK_KP5) {
124     $::CONN->user_send ("stay fire");
125     } elsif ($uni == ord ",") {
126     $::CONN->user_send ("take");
127     } elsif ($uni == ord " ") {
128     $::CONN->user_send ("apply");
129     } elsif ($uni == ord "\t") {
130     # TODO: toggle inventory
131     } elsif ($sym == CFClient::SDLK_KP_PLUS || $uni == ord "+") {
132     $::CONN->user_send ("rotateshoottype +");
133     } elsif ($sym == CFClient::SDLK_KP_MINUS || $uni == ord "-") {
134     $::CONN->user_send ("rotateshoottype -");
135     } elsif ($uni == ord '"') {
136     $self->{completer}->set_prefix ("$::CFG->{say_command} ");
137     $self->{completer}->show;
138     } elsif ($uni == ord "'") {
139     $self->{completer}->set_prefix ("");
140     $self->{completer}->show;
141     } elsif (exists $DIR{$sym}) {
142     if ($mod & CFClient::KMOD_SHIFT) {
143     $self->{shft}++;
144     $::CONN->user_send ("fire $DIR{$sym}[0]");
145     } elsif ($mod & CFClient::KMOD_CTRL) {
146     $self->{ctrl}++;
147     $::CONN->user_send ("run $DIR{$sym}[0]");
148     } else {
149     $::CONN->user_send ("$DIR{$sym}[1]");
150     }
151     } elsif ($ev->{unicode}) {
152     $self->{completer}->key_down ($ev);
153     $self->{completer}->show;
154     }
155     }
156    
157     sub key_up {
158     my ($self, $ev) = @_;
159    
160     my $mod = $ev->{mod};
161     my $sym = $ev->{sym};
162    
163     if (!($mod & CFClient::KMOD_SHIFT) && delete $self->{shft}) {
164     $::CONN->user_send ("fire_stop");
165     }
166     if (!($mod & CFClient::KMOD_CTRL ) && delete $self->{ctrl}) {
167     $::CONN->user_send ("run_stop");
168     }
169     }
170    
171 root 1.1 sub draw {
172     my ($self) = @_;
173    
174 root 1.36 my $focused = $CFClient::UI::FOCUS == $self
175     || $CFClient::UI::FOCUS == $self->{completer}{entry};
176    
177 root 1.26 return
178 root 1.36 unless $focused || !$::FAST;
179 root 1.26
180 root 1.1 if (delete $self->{need_update}) {
181 root 1.4 glNewList $self->{list};
182 root 1.1
183     if ($::MAP) {
184 root 1.17 my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
185     my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
186 root 1.1
187 root 1.18 my $sx = $::CFG->{map_shift_x} / $::CFG->{map_scale}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
188     my $sy = $::CFG->{map_shift_y} / $::CFG->{map_scale}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
189    
190 root 1.16 glPushMatrix;
191 root 1.9 glScale $::CFG->{map_scale}, $::CFG->{map_scale};
192    
193 root 1.1 glTranslate $sx0 - 32, $sy0 - 32, 0;
194    
195     my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
196    
197     if ($::CFG->{fow_enable}) {
198     if ($::CFG->{fow_smooth} && $CFClient::GL_VERSION >= 1.2) { # smooth fog of war
199     glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
200     glConvolutionFilter2D (
201     GL_CONVOLUTION_2D,
202     GL_ALPHA,
203     3, 3,
204     GL_ALPHA, GL_FLOAT,
205     pack "f*",
206 root 1.7 0.05, 0.13, 0.05,
207     0.13, 0.30, 0.13,
208     0.05, 0.13, 0.05,
209 root 1.1 );
210     glEnable GL_CONVOLUTION_2D;
211     }
212    
213 root 1.35 $self->{fow_texture_name} ||= glGenTexture;
214 root 1.31 # try to re-use the texture name: TODO improve texture class instead
215    
216 root 1.1 $self->{fow_texture} = new CFClient::Texture
217     w => $w,
218     h => $h,
219     data => $data,
220 root 1.35 name => $self->{fow_texture_name},
221 root 1.1 internalformat => GL_ALPHA,
222     format => GL_ALPHA;
223    
224     glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
225    
226     glEnable GL_TEXTURE_2D;
227     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
228    
229 root 1.7 glColor +($::CFG->{fow_intensity}) x 3, 0.8;
230 root 1.31 $self->{fow_texture}->draw_quad_alpha (0, 0, $w * 32, $h * 32);
231 root 1.1
232     glDisable GL_TEXTURE_2D;
233     }
234    
235 root 1.18 glPopMatrix;
236 root 1.1 }
237    
238     glEndList;
239     }
240    
241     glPushMatrix;
242     glCallList $self->{list};
243     glPopMatrix;
244    
245 root 1.29 # TNT2 emulates logops in software (or worse :)
246 root 1.36 if ($focused) {
247 root 1.32 (delete $self->{out_of_focus})->destroy
248     if $self->{out_of_focus};
249     } else {
250     glColor 0.4, 0.2, 0.2, 0.6;
251 root 1.29 glEnable GL_BLEND;
252     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
253 root 1.1 glBegin GL_QUADS;
254     glVertex 0, 0;
255     glVertex 0, $::HEIGHT;
256     glVertex $::WIDTH, $::HEIGHT;
257     glVertex $::WIDTH, 0;
258     glEnd;
259 root 1.29 glDisable GL_BLEND;
260 root 1.32
261     $self->{out_of_focus} ||= do {
262     my $label = new CFClient::UI::Label
263     x => 0,
264     y => 0,
265     z => 1,
266     ellipsise => 0,
267 root 1.33 text => "map out of focus (click map to play)";
268 root 1.32
269     $label->show;
270     $label->update;
271    
272     $CFClient::UI::ROOT->on_post_alloc ("$self$label" => sub {
273     $label->move (
274     ($::WIDTH - $label->{w}) * 0.5,
275     ($::HEIGHT - $label->{h}) * 0.5,
276     );
277     });
278    
279     $label
280     };
281 root 1.1 }
282     }
283    
284 root 1.36 sub DESTROY {
285     my $self = shift;
286 root 1.3
287 root 1.36 glDeleteList $self->{list};
288 root 1.1
289 root 1.36 $self->SUPER::DESTROY;
290 root 1.8 }
291    
292 root 1.18 package CFClient::MapWidget::MapMap;
293    
294 root 1.19 our @ISA = CFClient::UI::Base::;
295 root 1.18
296     use Time::HiRes qw(time);
297     use CFClient::OpenGL;
298    
299     sub size_request {
300     ($::HEIGHT * 0.25, $::HEIGHT * 0.25)
301     }
302    
303     sub size_allocate {
304     my ($self, $w, $h) = @_;
305    
306     $self->SUPER::size_allocate ($w, $h);
307     $self->update;
308     }
309    
310     sub update {
311     my ($self) = @_;
312    
313     delete $self->{texture_atime};
314     $self->SUPER::update;
315     }
316    
317     sub _draw {
318     my ($self) = @_;
319    
320     $::MAP or return;
321    
322     my ($w, $h) = @$self{qw(w h)};
323    
324     my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99;
325     my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99;
326    
327     my $sx = int $::CFG->{map_shift_x} / 32;
328     my $sy = int $::CFG->{map_shift_y} / 32;
329    
330     my $ox = 0.5 * ($w - $sw);
331     my $oy = 0.5 * ($h - $sh);
332    
333     glEnable GL_BLEND;
334     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
335     glEnable GL_TEXTURE_2D;
336     glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
337    
338     if ($self->{texture_atime} < time) {
339     $self->{texture_atime} = time + 1/3;
340    
341     $self->{texture} =
342     new CFClient::Texture
343     w => $w,
344     h => $h,
345     data => $::MAP->mapmap (-$ox, -$oy, $w, $h),
346     type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
347     }
348    
349     $self->{texture}->draw_quad (0, 0);
350    
351     glDisable GL_TEXTURE_2D;
352    
353     glTranslate 0.375, 0.375;
354    
355     #TODO: map scale is completely borked
356    
357     my $x0 = int $ox - $sx + 0.5;
358     my $y0 = int $oy - $sy + 0.5;
359    
360     glColor 1, 1, 0, 1;
361     glBegin GL_LINE_LOOP;
362     glVertex $x0 , $y0 ;
363     glVertex $x0 , $y0 + $sh;
364     glVertex $x0 + $sw, $y0 + $sh;
365     glVertex $x0 + $sw, $y0 ;
366     glEnd;
367    
368     glDisable GL_BLEND;
369     }
370    
371 root 1.8 package CFClient::MapWidget::Command;
372    
373     use strict;
374    
375     use CFClient::OpenGL;
376    
377 root 1.23 our @ISA = CFClient::UI::Frame::;
378 root 1.8
379     sub new {
380     my $class = shift;
381    
382     my $self = $class->SUPER::new (
383 root 1.23 bg => [0, 0, 0, 0.8],
384 root 1.8 @_,
385 root 1.23 );
386    
387     $self->add ($self->{vbox} = new CFClient::UI::VBox);
388    
389     $self->{label} = [
390     map
391 root 1.8 CFClient::UI::Label->new (
392 root 1.25 can_hover => 1,
393     can_events => 1,
394     tooltip_width => 0.33,
395     fontsize => $_,
396 root 1.27 ), (0.8) x 16
397 root 1.23 ];
398    
399     $self->{entry} = new CFClient::UI::Entry
400     connect_changed => sub {
401     $self->update_labels;
402 root 1.36 },
403     connect_key_down => sub {
404     my ($entry, $ev) = @_;
405    
406     my $self = $entry->{parent}{parent};
407    
408     if ($ev->{sym} == 13) {
409     if (exists $self->{select}) {
410     $::CONN->user_send ($self->{select});
411     $self->hide;
412     }
413     } elsif ($ev->{sym} == 27) {
414     $self->hide;
415     return;
416     } elsif ($ev->{sym} == CFClient::SDLK_DOWN) {
417     ++$self->{select_offset}
418     if $self->{select_offset} < $#{ $self->{last_match} || [] };
419     $self->update_labels;
420     } elsif ($ev->{sym} == CFClient::SDLK_UP) {
421     --$self->{select_offset}
422     if $self->{select_offset};
423     $self->update_labels;
424     } else {
425     return 0;
426     }
427    
428     1
429     }
430     ;
431 root 1.23
432     $self->{vbox}->add (
433     $self->{entry},
434     @{$self->{label}},
435 root 1.8 );
436    
437     $self
438     }
439    
440 root 1.36 sub set_prefix {
441     my ($self, $prefix) = @_;
442    
443     $self->{entry}->set_text ($prefix);
444     $self->show;
445     }
446    
447 root 1.8 sub size_allocate {
448     my ($self, $w, $h) = @_;
449    
450     $self->SUPER::size_allocate ($w, $h);
451     $self->move (($::WIDTH - $w) * 0.5, ($::HEIGHT - $h) * 0.6, 10);
452     }
453    
454 root 1.36 sub show {
455     my ($self) = @_;
456    
457     $self->SUPER::show;
458     $self->{entry}->focus_in;
459     }
460    
461     sub hide {
462     my ($self) = @_;
463    
464     $self->SUPER::hide;
465     $self->{entry}->set_text ("");
466     }
467    
468 root 1.23 sub key_down {
469     my ($self, $ev) = @_;
470    
471 root 1.36 $self->{entry}->key_down ($ev);
472 root 1.23 }
473    
474 root 1.8 sub update_labels {
475     my ($self) = @_;
476    
477 root 1.23 my $text = $self->{entry}->get_text;
478    
479     length $text
480 root 1.36 or return $self->hide;
481 root 1.23
482     my ($cmd, $arg) = $text =~ /^\s*([^[:space:]]*)(.*)$/;
483    
484 root 1.36 if ($text ne $self->{last_search}) {
485     my @match;
486 root 1.23
487 root 1.36 if ($text =~ /^(.*?)\s+$/) {
488     @match = [$cmd, "(appended whitespace suppresses completion)"];
489     } else {
490     my $regexp = do {
491     my ($beg, @chr) = split //, lc $cmd;
492 root 1.23
493 root 1.36 # the following regex is used to match our "completion entry"
494     # to an actual command - the parentheses match kind of "overhead"
495     # - the more characters the parentheses match, the less attractive
496     # is the match.
497     my $regexp = "^\Q$beg\E"
498     . join "", map "(?:.*?[ \\\\]\Q$_\E|(.*?)\Q$_\E)", @chr;
499     qr<$regexp>
500     };
501    
502     my @penalty;
503    
504     for (keys %{$self->{command}}) {
505     if (@penalty = $_ =~ $regexp) {
506     push @match, [$_, length join "", map "::$_", grep defined, @penalty];
507     }
508 root 1.23 }
509 root 1.36
510     @match = map $self->{command}{$_->[0]},
511     sort {
512     $a->[1] <=> $b->[1]
513     or $self->{command}{$a->[0]}[4] <=> $self->{command}{$b->[0]}[4]
514     or (length $a->[0]) <=> (length $b->[0])
515     } @match;
516 root 1.8 }
517 root 1.23
518     $self->{last_search} = $cmd;
519     $self->{last_match} = \@match;
520    
521     $self->{select_offset} = 0;
522 root 1.8 }
523    
524 root 1.23 my @labels = @{ $self->{label} };
525     my @matches = @{ $self->{last_match} || [] };
526 root 1.8
527 root 1.23 if ($self->{select_offset}) {
528     splice @matches, 0, $self->{select_offset}, ();
529 root 1.8
530 root 1.23 my $label = shift @labels;
531     $label->set_text ("...");
532     $label->set_tooltip ("Use Cursor-Up to view previous matches");
533 root 1.8 }
534    
535 root 1.23 for my $label (@labels) {
536     $label->{fg} = [1, 1, 1, 1];
537     $label->{bg} = [0, 0, 0, 0];
538     }
539    
540     if (@matches) {
541     $self->{select} = "$matches[0][0]$arg";
542    
543     $labels[0]->{fg} = [0, 0, 0, 1];
544     $labels[0]->{bg} = [1, 1, 1, 0.8];
545     } else {
546 root 1.24 $self->{select} = "$cmd$arg";
547 root 1.23 }
548    
549     for my $match (@matches) {
550     my $label = shift @labels;
551    
552     if (@labels) {
553     $label->set_text ("$match->[0]$arg");
554     $label->set_tooltip ($match->[1]);
555     } else {
556     $label->set_text ("...");
557     $label->set_tooltip ("Use Cursor-Down to view more matches");
558     last;
559     }
560     }
561 root 1.8
562 root 1.23 for my $label (@labels) {
563     $label->set_text ("");
564     $label->set_tooltip ("");
565 root 1.8 }
566    
567 root 1.23 $self->update;
568     ###
569 root 1.8 }
570    
571 root 1.23 sub _draw {
572     my ($self) = @_;
573 root 1.8
574 root 1.23 # hack
575     local $CFClient::UI::FOCUS = $self->{entry};
576 root 1.10
577 root 1.23 $self->SUPER::_draw;
578 root 1.2 }
579    
580 root 1.1 1