… | |
… | |
7 | |
7 | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $TOPLEVEL; |
12 | our $ROOT; |
13 | our $BUTTON_STATE; |
13 | our $BUTTON_STATE; |
14 | |
14 | |
15 | # class methods for events |
15 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
16 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
… | |
… | |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
22 | } |
22 | } |
23 | |
23 | |
24 | sub feed_sdl_button_down_event { |
24 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
25 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
26 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
27 | |
28 | if (!$BUTTON_STATE) { |
28 | if (!$BUTTON_STATE) { |
29 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
29 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
30 | |
31 | $GRAB = $widget; |
31 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
32 | $GRAB->update if $GRAB; |
33 | } |
33 | } |
34 | |
34 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
35 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
36 | |
37 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
38 | } |
38 | } |
39 | |
39 | |
40 | sub feed_sdl_button_up_event { |
40 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
41 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
42 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
43 | |
44 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
45 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
46 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
47 | |
48 | $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
49 | |
49 | |
50 | if (!$BUTTON_STATE) { |
50 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
51 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
52 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
53 | $GRAB->update if $GRAB; |
54 | } |
54 | } |
55 | } |
55 | } |
56 | |
56 | |
57 | sub feed_sdl_motion_event { |
57 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
58 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
59 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
60 | |
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
62 | |
63 | if ($widget != $HOVER) { |
63 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
64 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
65 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
66 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
68 | } |
68 | } |
69 | |
69 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
71 | } |
71 | } |
72 | |
72 | |
73 | # convert position array to integers |
73 | # convert position array to integers |
74 | sub harmonize { |
74 | sub harmonize { |
75 | my ($vals) = @_; |
75 | my ($vals) = @_; |
76 | |
76 | |
77 | my $rem = 0; |
77 | my $rem = 0; |
78 | |
78 | |
79 | for ($vals) { |
79 | for (@$vals) { |
80 | my $i = int $_ + $rem; |
80 | my $i = int $_ + $rem; |
81 | $rem += $_ - $i; |
81 | $rem += $_ - $i; |
82 | $_ = $i; |
82 | $_ = $i; |
83 | } |
83 | } |
84 | } |
84 | } |
… | |
… | |
87 | |
87 | |
88 | package CFClient::UI::Base; |
88 | package CFClient::UI::Base; |
89 | |
89 | |
90 | use strict; |
90 | use strict; |
91 | |
91 | |
92 | use SDL::OpenGL; |
92 | use CFClient::OpenGL; |
93 | |
93 | |
94 | sub new { |
94 | sub new { |
95 | my $class = shift; |
95 | my $class = shift; |
96 | |
96 | |
97 | my $self = bless { |
97 | my $self = bless { |
98 | x => 0, |
98 | x => 0, |
99 | y => 0, |
99 | y => 0, |
100 | z => 0, |
100 | z => 0, |
101 | w => -1, |
|
|
102 | h => -1, |
|
|
103 | @_ |
101 | @_ |
104 | }, $class; |
102 | }, $class; |
105 | |
103 | |
106 | for (keys %$self) { |
104 | for (keys %$self) { |
107 | if (/^connect_(.*)$/) { |
105 | if (/^connect_(.*)$/) { |
… | |
… | |
110 | } |
108 | } |
111 | |
109 | |
112 | $self |
110 | $self |
113 | } |
111 | } |
114 | |
112 | |
|
|
113 | sub show { |
|
|
114 | my ($self) = @_; |
|
|
115 | |
|
|
116 | return if $self->{parent}; |
|
|
117 | |
|
|
118 | $CFClient::UI::ROOT->add ($self); |
|
|
119 | } |
|
|
120 | |
|
|
121 | sub hide { |
|
|
122 | my ($self) = @_; |
|
|
123 | |
|
|
124 | return unless $self->{parent}; |
|
|
125 | |
|
|
126 | $self->{parent}->remove ($self); |
|
|
127 | } |
|
|
128 | |
115 | sub move { |
129 | sub move { |
116 | my ($self, $x, $y, $z) = @_; |
130 | my ($self, $x, $y, $z) = @_; |
|
|
131 | |
117 | $self->{x} = $x; |
132 | $self->{x} = int $x; |
118 | $self->{y} = $y; |
133 | $self->{y} = int $y; |
119 | $self->{z} = $z if defined $z; |
134 | $self->{z} = $z if defined $z; |
|
|
135 | |
|
|
136 | $self->update; |
120 | } |
137 | } |
121 | |
138 | |
122 | sub needs_redraw { |
139 | sub needs_redraw { |
123 | 0 |
140 | 0 |
124 | } |
141 | } |
… | |
… | |
126 | sub size_request { |
143 | sub size_request { |
127 | require Carp; |
144 | require Carp; |
128 | Carp::confess "size_request is abtract"; |
145 | Carp::confess "size_request is abtract"; |
129 | } |
146 | } |
130 | |
147 | |
131 | sub _size_allocate { |
148 | sub configure { |
132 | my ($self, $x, $y, $w, $h) = @_; |
149 | my ($self, $x, $y, $w, $h) = @_; |
133 | |
150 | |
134 | $self->{x} = $x; |
151 | $self->{x} = $x; |
135 | $self->{y} = $y; |
152 | $self->{y} = $y; |
136 | |
153 | |
137 | return unless $self->{w} != $w || $self->{h} != $h; |
154 | return unless $self->{w} != $w || $self->{h} != $h; |
138 | |
155 | |
139 | $self->{w} = $w; |
156 | $self->{w} = $w; |
140 | $self->{h} = $h; |
157 | $self->{h} = $h; |
141 | |
158 | |
142 | 1 |
159 | $self->size_allocate ($w, $h); |
|
|
160 | $self->update; |
143 | } |
161 | } |
144 | |
162 | |
145 | sub size_allocate { |
163 | sub size_allocate { |
|
|
164 | # nothing to be done |
|
|
165 | } |
|
|
166 | |
|
|
167 | # return top left coordinates |
|
|
168 | sub _topleft { |
146 | my ($self, $x, $y, $w, $h) = @_; |
169 | my ($self, $x, $y) = @_; |
147 | |
170 | |
148 | $self->_size_allocate ($x, $y, $w, $h); |
171 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
149 | } |
172 | } |
150 | |
173 | |
151 | # translate global coordinates to local coordinate system |
174 | # translate global coordinates to local coordinate system |
152 | sub translate { |
175 | sub coord2local { |
153 | my ($self, $x, $y) = @_; |
176 | my ($self, $x, $y) = @_; |
154 | |
177 | |
155 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
178 | my ($X, $Y) = $self->_topleft; |
|
|
179 | ($x - $X, $y - $Y) |
|
|
180 | } |
|
|
181 | |
|
|
182 | # translate local coordinates to global coordinate system |
|
|
183 | sub coord2global { |
|
|
184 | my ($self, $x, $y) = @_; |
|
|
185 | |
|
|
186 | my ($X, $Y) = $self->_topleft; |
|
|
187 | ($x + $X, $y + $Y) |
156 | } |
188 | } |
157 | |
189 | |
158 | sub focus_in { |
190 | sub focus_in { |
159 | my ($self) = @_; |
191 | my ($self) = @_; |
160 | |
192 | |
161 | return if $FOCUS == $self; |
193 | return if $FOCUS == $self; |
162 | return unless $self->{can_focus}; |
194 | return unless $self->{can_focus}; |
163 | |
195 | |
164 | my $focus = $FOCUS; $FOCUS = $self; |
196 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
197 | |
|
|
198 | $self->emit (focus_in => $focus); |
|
|
199 | |
165 | $focus->update if $focus; |
200 | $focus->update if $focus; |
166 | $FOCUS->update; |
201 | $FOCUS->update; |
167 | } |
202 | } |
168 | |
203 | |
169 | sub focus_out { |
204 | sub focus_out { |
170 | my ($self) = @_; |
205 | my ($self) = @_; |
171 | |
206 | |
172 | return unless $FOCUS == $self; |
207 | return unless $FOCUS == $self; |
173 | |
208 | |
174 | my $focus = $FOCUS; undef $FOCUS; |
209 | my $focus = $FOCUS; undef $FOCUS; |
|
|
210 | |
|
|
211 | $self->emit (focus_out => $focus); |
|
|
212 | |
175 | $focus->update if $focus; #? |
213 | $focus->update if $focus; #? |
176 | } |
214 | } |
177 | |
215 | |
178 | sub mouse_motion { } |
216 | sub mouse_motion { } |
179 | sub button_up { } |
217 | sub button_up { } |
… | |
… | |
203 | glPopMatrix; |
241 | glPopMatrix; |
204 | |
242 | |
205 | if ($self == $HOVER && $self->{can_hover}) { |
243 | if ($self == $HOVER && $self->{can_hover}) { |
206 | my ($x, $y) = @$self{qw(x y)}; |
244 | my ($x, $y) = @$self{qw(x y)}; |
207 | |
245 | |
208 | glColor 0, 0, 1, 0.2; |
246 | glColor 1, 0.8, 0.5, 0.2; |
209 | glEnable GL_BLEND; |
247 | glEnable GL_BLEND; |
210 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
248 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
211 | glBegin GL_QUADS; |
249 | glBegin GL_QUADS; |
212 | glVertex $x , $y; |
250 | glVertex $x , $y; |
213 | glVertex $x + $self->{w}, $y; |
251 | glVertex $x + $self->{w}, $y; |
… | |
… | |
233 | |
271 | |
234 | () |
272 | () |
235 | } |
273 | } |
236 | |
274 | |
237 | sub set_parent { |
275 | sub set_parent { |
238 | my ($self, $par) = @_; |
276 | my ($self, $parent) = @_; |
239 | |
277 | |
240 | $self->{parent} = $par; |
|
|
241 | Scalar::Util::weaken $self->{parent}; |
278 | Scalar::Util::weaken ($self->{parent} = $parent); |
242 | } |
279 | } |
243 | |
280 | |
244 | sub get_parent { |
281 | sub check_size { |
245 | $_[0]->{parent} |
282 | my ($self) = @_; |
|
|
283 | |
|
|
284 | my ($w, $h) = $self->size_request; |
|
|
285 | |
|
|
286 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
287 | $self->{req_w} = $w; |
|
|
288 | $self->{req_h} = $h; |
|
|
289 | |
|
|
290 | $self->{parent}->check_size |
|
|
291 | if $self->{parent}; |
|
|
292 | } |
246 | } |
293 | } |
247 | |
294 | |
248 | sub update { |
295 | sub update { |
249 | my ($self) = @_; |
296 | my ($self) = @_; |
250 | |
297 | |
… | |
… | |
253 | } |
300 | } |
254 | |
301 | |
255 | sub connect { |
302 | sub connect { |
256 | my ($self, $signal, $cb) = @_; |
303 | my ($self, $signal, $cb) = @_; |
257 | |
304 | |
258 | push @{ $self->{cb}{$signal} }, $cb; |
305 | push @{ $self->{signal_cb}{$signal} }, $cb; |
259 | } |
306 | } |
260 | |
307 | |
261 | sub emit { |
308 | sub emit { |
262 | my ($self, $signal, @args) = @_; |
309 | my ($self, $signal, @args) = @_; |
263 | |
310 | |
|
|
311 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
264 | $_->($self, @args) |
312 | $cb->($self, @args); |
265 | for @{$self->{cb}{$signal} || []}; |
313 | } |
266 | } |
314 | } |
267 | |
315 | |
268 | sub DESTROY { |
316 | sub DESTROY { |
269 | my ($self) = @_; |
317 | my ($self) = @_; |
270 | |
318 | |
… | |
… | |
276 | package CFClient::UI::DrawBG; |
324 | package CFClient::UI::DrawBG; |
277 | |
325 | |
278 | our @ISA = CFClient::UI::Base::; |
326 | our @ISA = CFClient::UI::Base::; |
279 | |
327 | |
280 | use strict; |
328 | use strict; |
281 | use SDL::OpenGL; |
329 | use CFClient::OpenGL; |
282 | |
330 | |
283 | sub new { |
331 | sub new { |
284 | my $class = shift; |
332 | my $class = shift; |
285 | |
333 | |
286 | # range [value, low, high, page] |
334 | # range [value, low, high, page] |
… | |
… | |
339 | |
387 | |
340 | $self |
388 | $self |
341 | } |
389 | } |
342 | |
390 | |
343 | sub add { |
391 | sub add { |
344 | my ($self, $chld) = @_; |
392 | my ($self, $child) = @_; |
345 | |
393 | |
346 | $chld->set_parent ($self); |
394 | $child->set_parent ($self); |
|
|
395 | |
|
|
396 | use sort 'stable'; |
347 | |
397 | |
348 | $self->{children} = [ |
398 | $self->{children} = [ |
349 | sort { $a->{z} <=> $b->{z} } |
399 | sort { $a->{z} <=> $b->{z} } |
350 | @{$self->{children}}, $chld |
400 | @{$self->{children}}, $child |
351 | ]; |
401 | ]; |
352 | |
402 | |
353 | $self->{w} = $self->{h} = -1; |
403 | $child->check_size; |
354 | $self->update; |
|
|
355 | } |
404 | } |
356 | |
405 | |
357 | sub remove { |
406 | sub remove { |
358 | my ($self, $widget) = @_; |
407 | my ($self, $child) = @_; |
359 | |
408 | |
|
|
409 | delete $child->{parent}; |
|
|
410 | |
360 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
411 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
361 | |
412 | |
362 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
413 | $self->check_size; |
363 | } |
414 | } |
364 | |
415 | |
365 | sub find_widget { |
416 | sub find_widget { |
366 | my ($self, $x, $y) = @_; |
417 | my ($self, $x, $y) = @_; |
367 | |
418 | |
… | |
… | |
397 | |
448 | |
398 | $class->SUPER::new (children => [$child], %arg) |
449 | $class->SUPER::new (children => [$child], %arg) |
399 | } |
450 | } |
400 | |
451 | |
401 | sub add { |
452 | sub add { |
402 | my ($self, $widget) = @_; |
453 | my ($self, $child) = @_; |
403 | |
454 | |
404 | $self->{children} = []; |
455 | $self->{children} = []; |
405 | |
456 | |
406 | $self->SUPER::add ($widget); |
457 | $self->SUPER::add ($child); |
407 | } |
458 | } |
408 | |
459 | |
409 | sub remove { |
460 | sub remove { |
410 | my ($self, $widget) = @_; |
461 | my ($self, $widget) = @_; |
411 | |
462 | |
… | |
… | |
420 | sub size_request { |
471 | sub size_request { |
421 | $_[0]{children}[0]->size_request |
472 | $_[0]{children}[0]->size_request |
422 | } |
473 | } |
423 | |
474 | |
424 | sub size_allocate { |
475 | sub size_allocate { |
425 | my ($self, $x, $y, $w, $h) = @_; |
476 | my ($self, $w, $h) = @_; |
426 | |
477 | |
427 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
428 | |
|
|
429 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
478 | $self->{children}[0]->configure (0, 0, $w, $h); |
430 | } |
479 | } |
431 | |
480 | |
432 | ############################################################################# |
481 | ############################################################################# |
433 | |
482 | |
434 | package CFClient::UI::Window; |
483 | package CFClient::UI::Window; |
435 | |
484 | |
436 | our @ISA = CFClient::UI::Bin::; |
485 | our @ISA = CFClient::UI::Bin::; |
437 | |
486 | |
438 | use SDL::OpenGL; |
487 | use CFClient::OpenGL; |
439 | |
488 | |
440 | sub new { |
489 | sub new { |
441 | my ($class, %arg) = @_; |
490 | my ($class, %arg) = @_; |
442 | |
491 | |
443 | my $self = $class->SUPER::new (%arg); |
492 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
460 | $self->child->draw; |
509 | $self->child->draw; |
461 | }; |
510 | }; |
462 | } |
511 | } |
463 | |
512 | |
464 | sub size_allocate { |
513 | sub size_allocate { |
465 | my ($self, $x, $y, $w, $h) = @_; |
514 | my ($self, $w, $h) = @_; |
466 | |
515 | |
467 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
468 | |
|
|
469 | $self->child->size_allocate (0, 0, $w, $h); |
516 | $self->child->configure (0, 0, $w, $h); |
470 | |
517 | |
471 | $self->render_chld; |
518 | $self->render_chld; |
472 | } |
519 | } |
473 | |
520 | |
474 | sub _draw { |
521 | sub _draw { |
… | |
… | |
490 | glDisable GL_TEXTURE_2D; |
537 | glDisable GL_TEXTURE_2D; |
491 | } |
538 | } |
492 | |
539 | |
493 | ############################################################################# |
540 | ############################################################################# |
494 | |
541 | |
|
|
542 | package CFClient::UI::ViewPort; |
|
|
543 | |
|
|
544 | our @ISA = CFClient::UI::Window::; |
|
|
545 | |
|
|
546 | sub new { die } |
|
|
547 | |
|
|
548 | sub size_request { |
|
|
549 | my ($self) = @_; |
|
|
550 | |
|
|
551 | @$self{qw(child_w child_h)} = $self->child->size_request; |
|
|
552 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
|
|
553 | |
|
|
554 | @$self{qw(child_w child_h)} |
|
|
555 | } |
|
|
556 | |
|
|
557 | sub _draw { |
|
|
558 | my ($self) = @_; |
|
|
559 | |
|
|
560 | $self->{children}[1]->draw; |
|
|
561 | } |
|
|
562 | |
|
|
563 | |
|
|
564 | ############################################################################# |
|
|
565 | |
495 | package CFClient::UI::Frame; |
566 | package CFClient::UI::Frame; |
496 | |
567 | |
497 | our @ISA = CFClient::UI::Bin::; |
568 | our @ISA = CFClient::UI::Bin::; |
498 | |
569 | |
499 | use SDL::OpenGL; |
570 | use CFClient::OpenGL; |
500 | |
571 | |
501 | sub size_request { |
572 | sub size_request { |
502 | my ($self) = @_; |
573 | my ($self) = @_; |
503 | my $chld = $self->child |
574 | my $chld = $self->child |
504 | or return (0, 0); |
575 | or return (0, 0); |
… | |
… | |
509 | } |
580 | } |
510 | |
581 | |
511 | sub size_allocate { |
582 | sub size_allocate { |
512 | my ($self, $x, $y, $w, $h) = @_; |
583 | my ($self, $x, $y, $w, $h) = @_; |
513 | |
584 | |
514 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
515 | |
|
|
516 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
585 | $self->child->configure (2, 2, $w - 4, $h - 4); |
517 | } |
586 | } |
518 | |
587 | |
519 | sub _draw { |
588 | sub _draw { |
520 | my ($self) = @_; |
589 | my ($self) = @_; |
521 | |
590 | |
… | |
… | |
538 | |
607 | |
539 | package CFClient::UI::FancyFrame; |
608 | package CFClient::UI::FancyFrame; |
540 | |
609 | |
541 | our @ISA = CFClient::UI::Bin::; |
610 | our @ISA = CFClient::UI::Bin::; |
542 | |
611 | |
543 | use SDL::OpenGL; |
612 | use CFClient::OpenGL; |
544 | |
613 | |
545 | my @tex = |
614 | my @tex = |
546 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
615 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
547 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
616 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
548 | |
617 | |
… | |
… | |
552 | # TODO: user_x, user_y, overwrite moveto? |
621 | # TODO: user_x, user_y, overwrite moveto? |
553 | |
622 | |
554 | $class->SUPER::new ( |
623 | $class->SUPER::new ( |
555 | bg => [1, 1, 1, 1], |
624 | bg => [1, 1, 1, 1], |
556 | border_bg => [1, 1, 1, 1], |
625 | border_bg => [1, 1, 1, 1], |
557 | border => $::FONTSIZE * 0.8, |
626 | border => 0.8, |
558 | @_ |
627 | @_ |
559 | ) |
628 | ) |
560 | } |
629 | } |
561 | |
630 | |
|
|
631 | sub border { |
|
|
632 | int $_[0]{border} * $::FONTSIZE |
|
|
633 | } |
|
|
634 | |
562 | sub size_request { |
635 | sub size_request { |
563 | my ($self) = @_; |
636 | my ($self) = @_; |
564 | |
637 | |
565 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
638 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
566 | |
639 | |
567 | my ($w, $h) = $self->SUPER::size_request; |
640 | my ($w, $h) = $self->SUPER::size_request; |
568 | |
641 | |
569 | ( |
642 | ( |
570 | $w + $self->{border} * 2, |
643 | $w + $self->border * 2, |
571 | $h + $self->{border} * 2, |
644 | $h + $self->border * 2, |
572 | ) |
645 | ) |
573 | } |
646 | } |
574 | |
647 | |
575 | sub size_allocate { |
648 | sub size_allocate { |
576 | my ($self, $x, $y, $w, $h) = @_; |
649 | my ($self, $w, $h) = @_; |
577 | |
650 | |
578 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
579 | |
|
|
580 | $h -= List::Util::max 0, $self->{border} * 2; |
651 | $h -= List::Util::max 0, $self->border * 2; |
581 | $w -= List::Util::max 0, $self->{border} * 2; |
652 | $w -= List::Util::max 0, $self->border * 2; |
582 | |
653 | |
583 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
654 | $self->child->configure ($self->border, $self->border, $w, $h); |
584 | } |
655 | } |
585 | |
656 | |
586 | sub button_down { |
657 | sub button_down { |
587 | my ($self, $ev, $x, $y) = @_; |
658 | my ($self, $ev, $x, $y) = @_; |
588 | |
659 | |
|
|
660 | my $border = $self->border; |
|
|
661 | |
589 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
662 | if ($x < $self->{w} && $x >= $self->{w} - $border |
590 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
663 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
591 | |
664 | |
592 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
665 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
593 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
666 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
594 | |
667 | |
595 | $self->{motion} = sub { |
668 | $self->{motion} = sub { |
… | |
… | |
597 | |
670 | |
598 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
671 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
599 | |
672 | |
600 | $self->{user_w} = $bw + $x - $ox; |
673 | $self->{user_w} = $bw + $x - $ox; |
601 | $self->{user_h} = $bh + $y - $oy; |
674 | $self->{user_h} = $bh + $y - $oy; |
602 | $self->update; |
675 | $self->check_size; |
603 | }; |
676 | }; |
604 | |
677 | |
605 | } elsif ($x >= 0 && $x < $self->{w} |
678 | } elsif ($x >= 0 && $x < $self->{w} |
606 | && $y >= 0 && $y < $self->{border}) { |
679 | && $y >= 0 && $y < $border) { |
607 | |
680 | |
608 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
681 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
609 | my ($bx, $by) = ($self->{x}, $self->{y}); |
682 | my ($bx, $by) = ($self->{x}, $self->{y}); |
610 | |
683 | |
611 | $self->{motion} = sub { |
684 | $self->{motion} = sub { |
… | |
… | |
640 | glEnable GL_BLEND; |
713 | glEnable GL_BLEND; |
641 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
714 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
642 | glEnable GL_TEXTURE_2D; |
715 | glEnable GL_TEXTURE_2D; |
643 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
716 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
644 | |
717 | |
|
|
718 | my $border = $self->border; |
|
|
719 | |
645 | glColor @{ $self->{border_bg} }; |
720 | glColor @{ $self->{border_bg} }; |
646 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
721 | $tex[1]->draw_quad (0, 0, $w, $border); |
647 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
722 | $tex[3]->draw_quad (0, $border, $border, $ch); |
648 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
723 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
649 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
724 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
650 | |
725 | |
651 | my $bg = $tex[0]; |
726 | my $bg = $tex[0]; |
652 | |
727 | |
653 | # TODO: repeat texture not scale |
728 | # TODO: repeat texture not scale |
654 | my $rep_x = $cw / $bg->{w}; |
729 | my $rep_x = $cw / $bg->{w}; |
… | |
… | |
657 | glColor @{ $self->{bg} }; |
732 | glColor @{ $self->{bg} }; |
658 | |
733 | |
659 | $bg->{s} = $rep_x; |
734 | $bg->{s} = $rep_x; |
660 | $bg->{t} = $rep_y; |
735 | $bg->{t} = $rep_y; |
661 | $bg->{wrap_mode} = 1; |
736 | $bg->{wrap_mode} = 1; |
662 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
737 | $bg->draw_quad ($border, $border, $cw, $ch); |
663 | |
738 | |
664 | glDisable GL_TEXTURE_2D; |
739 | glDisable GL_TEXTURE_2D; |
665 | glDisable GL_BLEND; |
740 | glDisable GL_BLEND; |
666 | |
741 | |
667 | $self->child->draw; |
742 | $self->child->draw; |
… | |
… | |
673 | |
748 | |
674 | our @ISA = CFClient::UI::Base::; |
749 | our @ISA = CFClient::UI::Base::; |
675 | |
750 | |
676 | use List::Util qw(max sum); |
751 | use List::Util qw(max sum); |
677 | |
752 | |
678 | use SDL::OpenGL; |
753 | use CFClient::OpenGL; |
679 | |
754 | |
680 | sub new { |
755 | sub new { |
681 | my $class = shift; |
756 | my $class = shift; |
682 | |
757 | |
683 | $class->SUPER::new ( |
758 | $class->SUPER::new ( |
… | |
… | |
685 | @_ |
760 | @_ |
686 | ) |
761 | ) |
687 | } |
762 | } |
688 | |
763 | |
689 | sub add { |
764 | sub add { |
690 | my ($self, $x, $y, $chld) = @_; |
765 | my ($self, $x, $y, $child) = @_; |
691 | |
766 | |
|
|
767 | $child->set_parent ($self); |
692 | $self->{children}[$y][$x] = $chld; |
768 | $self->{children}[$y][$x] = $child; |
693 | $chld->set_parent ($self); |
|
|
694 | |
769 | |
695 | $self->{w} = $self->{h} = -1; |
770 | $child->check_size; |
|
|
771 | } |
|
|
772 | |
|
|
773 | # TODO: move to container class maybe? send childs a signal on removal? |
|
|
774 | sub clear { |
|
|
775 | my ($self) = @_; |
|
|
776 | |
|
|
777 | delete $self->{children}; |
696 | $self->update; |
778 | $self->update; |
697 | } |
779 | } |
698 | |
780 | |
699 | sub get_wh { |
781 | sub get_wh { |
700 | my ($self) = @_; |
782 | my ($self) = @_; |
… | |
… | |
728 | (sum @$hs), |
810 | (sum @$hs), |
729 | ) |
811 | ) |
730 | } |
812 | } |
731 | |
813 | |
732 | sub size_allocate { |
814 | sub size_allocate { |
733 | my ($self, $x, $y, $w, $h) = @_; |
815 | my ($self, $w, $h) = @_; |
734 | |
|
|
735 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
736 | |
816 | |
737 | my ($ws, $hs) = $self->get_wh; |
817 | my ($ws, $hs) = $self->get_wh; |
738 | |
818 | |
739 | my $req_w = sum @$ws; |
819 | my $req_w = sum @$ws; |
740 | my $req_h = sum @$hs; |
820 | my $req_h = sum @$hs; |
… | |
… | |
762 | |
842 | |
763 | for my $c (0 .. $#$row) { |
843 | for my $c (0 .. $#$row) { |
764 | my $col_w = $ws->[$c]; |
844 | my $col_w = $ws->[$c]; |
765 | |
845 | |
766 | if (my $widget = $row->[$c]) { |
846 | if (my $widget = $row->[$c]) { |
767 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
847 | $widget->configure ($x, $y, $col_w, $row_h); |
768 | } |
848 | } |
769 | |
849 | |
770 | $x += $col_w; |
850 | $x += $col_w; |
771 | } |
851 | } |
772 | |
852 | |
… | |
… | |
817 | (List::Util::max map $_->[1], @alloc), |
897 | (List::Util::max map $_->[1], @alloc), |
818 | ) |
898 | ) |
819 | } |
899 | } |
820 | |
900 | |
821 | sub size_allocate { |
901 | sub size_allocate { |
822 | my ($self, $x, $y, $w, $h) = @_; |
902 | my ($self, $w, $h) = @_; |
823 | |
|
|
824 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
825 | |
903 | |
826 | ($h, $w) = ($w, $h); |
904 | ($h, $w) = ($w, $h); |
827 | |
905 | |
828 | my $children = $self->{children}; |
906 | my $children = $self->{children}; |
829 | |
907 | |
… | |
… | |
851 | |
929 | |
852 | my $y = 0; |
930 | my $y = 0; |
853 | for (0 .. $#$children) { |
931 | for (0 .. $#$children) { |
854 | my $child = $children->[$_]; |
932 | my $child = $children->[$_]; |
855 | my $h = $h[$_]; |
933 | my $h = $h[$_]; |
856 | $child->size_allocate ($y, 0, $h, $w); |
934 | $child->configure ($y, 0, $h, $w); |
857 | |
935 | |
858 | $y += $h; |
936 | $y += $h; |
859 | } |
937 | } |
|
|
938 | |
|
|
939 | 1 |
860 | } |
940 | } |
861 | |
941 | |
862 | ############################################################################# |
942 | ############################################################################# |
863 | |
943 | |
864 | package CFClient::UI::VBox; |
944 | package CFClient::UI::VBox; |
… | |
… | |
877 | (List::Util::sum map $_->[1], @alloc), |
957 | (List::Util::sum map $_->[1], @alloc), |
878 | ) |
958 | ) |
879 | } |
959 | } |
880 | |
960 | |
881 | sub size_allocate { |
961 | sub size_allocate { |
882 | my ($self, $x, $y, $w, $h) = @_; |
962 | my ($self, $w, $h) = @_; |
883 | |
|
|
884 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
885 | |
963 | |
886 | my $children = $self->{children}; |
964 | my $children = $self->{children}; |
887 | |
965 | |
888 | my @h = map +($_->size_request)[1], @$children; |
966 | my @h = map +($_->size_request)[1], @$children; |
889 | |
967 | |
… | |
… | |
907 | |
985 | |
908 | my $y = 0; |
986 | my $y = 0; |
909 | for (0 .. $#$children) { |
987 | for (0 .. $#$children) { |
910 | my $child = $children->[$_]; |
988 | my $child = $children->[$_]; |
911 | my $h = $h[$_]; |
989 | my $h = $h[$_]; |
912 | $child->size_allocate (0, $y, $w, $h); |
990 | $child->configure (0, $y, $w, $h); |
913 | |
991 | |
914 | $y += $h; |
992 | $y += $h; |
915 | } |
993 | } |
|
|
994 | |
|
|
995 | 1 |
916 | } |
996 | } |
917 | |
997 | |
918 | ############################################################################# |
998 | ############################################################################# |
919 | |
999 | |
920 | package CFClient::UI::Label; |
1000 | package CFClient::UI::Label; |
921 | |
1001 | |
922 | our @ISA = CFClient::UI::Base::; |
1002 | our @ISA = CFClient::UI::Base::; |
923 | |
1003 | |
924 | use SDL::OpenGL; |
1004 | use CFClient::OpenGL; |
925 | |
1005 | |
926 | sub new { |
1006 | sub new { |
927 | my ($class, %arg) = @_; |
1007 | my ($class, %arg) = @_; |
928 | |
1008 | |
929 | my $self = $class->SUPER::new ( |
1009 | my $self = $class->SUPER::new ( |
930 | fg => [1, 1, 1], |
1010 | fg => [1, 1, 1], |
931 | fontsize => $::FONTSIZE, |
1011 | fontsize => 1, |
932 | text => "", |
1012 | text => "", |
933 | align => -1, |
1013 | align => -1, |
|
|
1014 | valign => -1, |
934 | padding => 2, |
1015 | padding => 2, |
935 | layout => new CFClient::Layout, |
1016 | layout => new CFClient::Layout, |
936 | %arg |
1017 | %arg |
937 | ); |
1018 | ); |
938 | |
1019 | |
939 | $self->set_text ($self->{text}); |
1020 | $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# |
|
|
1021 | |
|
|
1022 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
|
|
1023 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
940 | |
1024 | |
941 | $self |
1025 | $self |
942 | } |
1026 | } |
943 | |
1027 | |
944 | sub escape_text { |
1028 | sub escape_text { |
… | |
… | |
952 | } |
1036 | } |
953 | |
1037 | |
954 | sub set_text { |
1038 | sub set_text { |
955 | my ($self, $text) = @_; |
1039 | my ($self, $text) = @_; |
956 | |
1040 | |
957 | $self->{text} = $text; |
|
|
958 | $self->{layout}->set_markup ($text); |
1041 | $self->{layout}->set_text ($text); |
959 | |
1042 | |
960 | delete $self->{texture}; |
1043 | delete $self->{texture}; |
961 | # $self->{w} = $self->{h} = -1; |
|
|
962 | $self->update; |
1044 | $self->update; |
963 | } |
1045 | } |
964 | |
1046 | |
965 | sub get_text { |
1047 | sub set_markup { |
966 | my ($self, $text) = @_; |
1048 | my ($self, $markup) = @_; |
967 | |
1049 | |
968 | $self->{text} |
1050 | $self->{layout}->set_markup ($markup); |
|
|
1051 | |
|
|
1052 | delete $self->{texture}; |
|
|
1053 | $self->update; |
969 | } |
1054 | } |
970 | |
1055 | |
971 | sub size_request { |
1056 | sub size_request { |
972 | my ($self) = @_; |
1057 | my ($self) = @_; |
973 | |
1058 | |
974 | $self->{layout}->set_width; |
1059 | $self->{layout}->set_width; |
975 | $self->{layout}->set_height ($self->{fontsize}); |
1060 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1061 | |
976 | my ($w, $h) = $self->{layout}->size; |
1062 | my ($w, $h) = $self->{layout}->size; |
977 | |
1063 | |
978 | ( |
1064 | ( |
979 | $w + $self->{padding} * 2, |
1065 | $w + $self->{padding} * 2, |
980 | $h + $self->{padding} * 2, |
1066 | $h + $self->{padding} * 2, |
981 | ) |
1067 | ) |
982 | } |
1068 | } |
983 | |
1069 | |
984 | sub size_allocate { |
1070 | sub size_allocate { |
985 | my ($self, $x, $y, $w, $h) = @_; |
1071 | my ($self, $w, $h) = @_; |
986 | |
|
|
987 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
988 | |
1072 | |
989 | delete $self->{texture}; |
1073 | delete $self->{texture}; |
990 | } |
|
|
991 | |
|
|
992 | sub update { |
|
|
993 | my ($self) = @_; |
|
|
994 | |
|
|
995 | delete $self->{texture}; |
|
|
996 | $self->SUPER::update; |
|
|
997 | } |
1074 | } |
998 | |
1075 | |
999 | sub _draw { |
1076 | sub _draw { |
1000 | my ($self) = @_; |
1077 | my ($self) = @_; |
1001 | |
1078 | |
1002 | my $tex = $self->{texture} ||= do { |
1079 | my $tex = $self->{texture} ||= do { |
1003 | $self->{layout}->set_width ($self->{w}); |
1080 | $self->{layout}->set_width ($self->{w}); |
1004 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
1081 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1005 | new_from_layout CFClient::Texture $self->{layout} |
1082 | new_from_layout CFClient::Texture $self->{layout} |
1006 | }; |
1083 | }; |
1007 | |
1084 | |
1008 | glEnable GL_BLEND; |
1085 | glEnable GL_BLEND; |
1009 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1086 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1010 | glEnable GL_TEXTURE_2D; |
1087 | glEnable GL_TEXTURE_2D; |
1011 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1088 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1012 | |
1089 | |
1013 | glColor @{$self->{fg}}; |
1090 | glColor @{$self->{fg}}; |
1014 | |
1091 | |
1015 | my $x = |
1092 | $self->{ox} = int ( |
1016 | $self->{align} < 0 ? $self->{padding} |
1093 | $self->{align} < 0 ? $self->{padding} |
1017 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1094 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1018 | : ($self->{w} - $tex->{w}) * 0.5; |
1095 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1096 | ); |
1019 | |
1097 | |
1020 | $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); |
1098 | $self->{oy} = int ( |
|
|
1099 | $self->{valign} < 0 ? $self->{padding} |
|
|
1100 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1101 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1102 | ); |
|
|
1103 | |
|
|
1104 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
1021 | |
1105 | |
1022 | glDisable GL_TEXTURE_2D; |
1106 | glDisable GL_TEXTURE_2D; |
1023 | glDisable GL_BLEND; |
1107 | glDisable GL_BLEND; |
1024 | } |
1108 | } |
1025 | |
1109 | |
1026 | ############################################################################# |
1110 | ############################################################################# |
1027 | |
1111 | |
1028 | package CFClient::UI::Entry; |
1112 | package CFClient::UI::EntryBase; |
1029 | |
1113 | |
1030 | our @ISA = CFClient::UI::Label::; |
1114 | our @ISA = CFClient::UI::Label::; |
1031 | |
1115 | |
1032 | use SDL; |
1116 | use CFClient::OpenGL; |
1033 | use SDL::OpenGL; |
|
|
1034 | |
1117 | |
1035 | sub new { |
1118 | sub new { |
1036 | my $class = shift; |
1119 | my $class = shift; |
1037 | |
1120 | |
1038 | $class->SUPER::new ( |
1121 | $class->SUPER::new ( |
… | |
… | |
1040 | bg => [0, 0, 0, 0.2], |
1123 | bg => [0, 0, 0, 0.2], |
1041 | active_bg => [1, 1, 1, 0.5], |
1124 | active_bg => [1, 1, 1, 0.5], |
1042 | active_fg => [0, 0, 0], |
1125 | active_fg => [0, 0, 0], |
1043 | can_hover => 1, |
1126 | can_hover => 1, |
1044 | can_focus => 1, |
1127 | can_focus => 1, |
|
|
1128 | valign => 0, |
1045 | @_ |
1129 | @_ |
1046 | ) |
1130 | ) |
1047 | } |
1131 | } |
1048 | |
1132 | |
1049 | sub _set_text { |
1133 | sub _set_text { |
1050 | my ($self, $text) = @_; |
1134 | my ($self, $text) = @_; |
1051 | |
1135 | |
1052 | my $old_text = $self->{text}; |
1136 | delete $self->{cur_h}; |
|
|
1137 | |
|
|
1138 | return if $self->{text} eq $text; |
|
|
1139 | |
|
|
1140 | delete $self->{texture}; |
1053 | |
1141 | |
1054 | $self->{last_activity} = $::NOW; |
1142 | $self->{last_activity} = $::NOW; |
1055 | |
|
|
1056 | $self->{text} = $text; |
1143 | $self->{text} = $text; |
1057 | $self->{layout}->set_width ($self->{w}); |
|
|
1058 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
|
|
1059 | |
1144 | |
1060 | $text =~ s/./*/g if $self->{hidden}; |
1145 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1146 | $self->{layout}->set_text ("$text "); |
1061 | |
1147 | |
1062 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1148 | $self->emit (changed => $self->{text}); |
|
|
1149 | } |
1063 | |
1150 | |
1064 | $text = substr $text, 0, $self->{cursor}; |
1151 | sub get_text { |
1065 | utf8::encode $text; |
1152 | $_[0]{text} |
1066 | |
|
|
1067 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1068 | |
|
|
1069 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1070 | if $old_text ne $self->{text}; |
|
|
1071 | } |
1153 | } |
1072 | |
1154 | |
1073 | sub size_request { |
1155 | sub size_request { |
1074 | my ($self) = @_; |
1156 | my ($self) = @_; |
1075 | |
1157 | |
… | |
… | |
1077 | |
1159 | |
1078 | ($w + 1, $h) # add 1 for cursor |
1160 | ($w + 1, $h) # add 1 for cursor |
1079 | } |
1161 | } |
1080 | |
1162 | |
1081 | sub size_allocate { |
1163 | sub size_allocate { |
1082 | my ($self, $x, $y, $w, $h) = @_; |
1164 | my ($self, $w, $h) = @_; |
1083 | |
|
|
1084 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
1085 | |
1165 | |
1086 | $self->_set_text ($self->{text}); |
1166 | $self->_set_text ($self->{text}); |
1087 | } |
1167 | } |
1088 | |
1168 | |
1089 | sub set_text { |
1169 | sub set_text { |
… | |
… | |
1095 | } |
1175 | } |
1096 | |
1176 | |
1097 | sub key_down { |
1177 | sub key_down { |
1098 | my ($self, $ev) = @_; |
1178 | my ($self, $ev) = @_; |
1099 | |
1179 | |
1100 | my $mod = $ev->key_mod; |
1180 | my $mod = $ev->{mod}; |
1101 | my $sym = $ev->key_sym; |
1181 | my $sym = $ev->{sym}; |
1102 | |
|
|
1103 | my $uni = $ev->key_unicode; |
1182 | my $uni = $ev->{unicode}; |
1104 | |
1183 | |
1105 | my $text = $self->get_text; |
1184 | my $text = $self->get_text; |
1106 | |
1185 | |
1107 | if ($sym == SDLK_BACKSPACE) { |
1186 | if ($sym == 8) { |
1108 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1187 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1109 | } elsif ($sym == SDLK_DELETE) { |
1188 | } elsif ($sym == 127) { |
1110 | substr $text, $self->{cursor}, 1, ""; |
1189 | substr $text, $self->{cursor}, 1, ""; |
1111 | } elsif ($sym == SDLK_LEFT) { |
1190 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1112 | --$self->{cursor} if $self->{cursor}; |
1191 | --$self->{cursor} if $self->{cursor}; |
1113 | } elsif ($sym == SDLK_RIGHT) { |
1192 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1114 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1193 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1115 | } elsif ($sym == SDLK_HOME) { |
1194 | } elsif ($sym == CFClient::SDLK_HOME) { |
1116 | $self->{cursor} = 0; |
1195 | $self->{cursor} = 0; |
1117 | } elsif ($sym == SDLK_END) { |
1196 | } elsif ($sym == CFClient::SDLK_END) { |
1118 | $self->{cursor} = length $text; |
1197 | $self->{cursor} = length $text; |
1119 | } elsif ($sym == SDLK_ESCAPE) { |
1198 | } elsif ($sym == 27) { |
1120 | $self->emit ('escape'); |
1199 | $self->emit ('escape'); |
1121 | } elsif ($uni) { |
1200 | } elsif ($uni) { |
1122 | substr $text, $self->{cursor}++, 0, chr $uni; |
1201 | substr $text, $self->{cursor}++, 0, chr $uni; |
1123 | } |
1202 | } |
1124 | |
1203 | |
… | |
… | |
1179 | |
1258 | |
1180 | $self->SUPER::_draw; |
1259 | $self->SUPER::_draw; |
1181 | |
1260 | |
1182 | #TODO: force update every cursor change :( |
1261 | #TODO: force update every cursor change :( |
1183 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1262 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1263 | |
|
|
1264 | unless (exists $self->{cur_h}) { |
|
|
1265 | my $text = substr $self->{text}, 0, $self->{cursor}; |
|
|
1266 | utf8::encode $text; |
|
|
1267 | |
|
|
1268 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
|
|
1269 | } |
|
|
1270 | |
1184 | glColor @{$self->{fg}}; |
1271 | glColor @{$self->{fg}}; |
1185 | glBegin GL_LINES; |
1272 | glBegin GL_LINES; |
1186 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1187 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1273 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
|
|
1274 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1188 | glEnd; |
1275 | glEnd; |
1189 | } |
1276 | } |
1190 | } |
1277 | } |
1191 | |
1278 | |
1192 | package CFClient::UI::LineEntry; |
1279 | package CFClient::UI::Entry; |
1193 | |
1280 | |
1194 | our @ISA = CFClient::UI::Entry::; |
1281 | our @ISA = CFClient::UI::EntryBase::; |
1195 | |
1282 | |
1196 | use SDL; |
1283 | use CFClient::OpenGL; |
1197 | use SDL::OpenGL; |
|
|
1198 | |
1284 | |
1199 | sub key_down { |
1285 | sub key_down { |
1200 | my ($self, $ev) = @_; |
1286 | my ($self, $ev) = @_; |
1201 | |
1287 | |
1202 | my $sym = $ev->key_sym; |
1288 | my $sym = $ev->{sym}; |
1203 | |
1289 | |
1204 | if ($sym == SDLK_RETURN) { |
1290 | if ($sym == 13) { |
1205 | $self->emit (activate => $self->get_text); |
1291 | $self->emit (activate => $self->get_text); |
1206 | $self->update; |
1292 | $self->update; |
1207 | |
1293 | |
1208 | } else { |
1294 | } else { |
1209 | $self->SUPER::key_down ($ev); |
1295 | $self->SUPER::key_down ($ev); |
… | |
… | |
1215 | |
1301 | |
1216 | package CFClient::UI::Button; |
1302 | package CFClient::UI::Button; |
1217 | |
1303 | |
1218 | our @ISA = CFClient::UI::Label::; |
1304 | our @ISA = CFClient::UI::Label::; |
1219 | |
1305 | |
1220 | use SDL; |
1306 | use CFClient::OpenGL; |
1221 | use SDL::OpenGL; |
|
|
1222 | |
1307 | |
1223 | my @tex = |
1308 | my @tex = |
1224 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1309 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1225 | qw(b1_button_active.png); |
1310 | qw(b1_button_active.png); |
1226 | |
1311 | |
… | |
… | |
1229 | |
1314 | |
1230 | $class->SUPER::new ( |
1315 | $class->SUPER::new ( |
1231 | padding => 4, |
1316 | padding => 4, |
1232 | fg => [1, 1, 1], |
1317 | fg => [1, 1, 1], |
1233 | bg => [1, 1, 1, 0.2], |
1318 | bg => [1, 1, 1, 0.2], |
1234 | active_fg => [1, 1, 0], |
1319 | active_fg => [0, 0, 1], |
1235 | can_hover => 1, |
1320 | can_hover => 1, |
|
|
1321 | align => 0, |
|
|
1322 | valign => 0, |
1236 | @_ |
1323 | @_ |
1237 | ) |
1324 | ) |
1238 | } |
1325 | } |
1239 | |
1326 | |
1240 | sub button_up { |
1327 | sub button_up { |
… | |
… | |
1248 | |
1335 | |
1249 | sub _draw { |
1336 | sub _draw { |
1250 | my ($self) = @_; |
1337 | my ($self) = @_; |
1251 | |
1338 | |
1252 | local $self->{fg} = $self->{fg}; |
1339 | local $self->{fg} = $self->{fg}; |
1253 | my $tex = $tex[0]; |
|
|
1254 | |
|
|
1255 | glEnable GL_BLEND; |
|
|
1256 | glEnable GL_TEXTURE_2D; |
|
|
1257 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1258 | |
1340 | |
1259 | if ($GRAB == $self) { |
1341 | if ($GRAB == $self) { |
1260 | $self->{fg} = $self->{active_fg}; |
1342 | $self->{fg} = $self->{active_fg}; |
1261 | } |
1343 | } |
1262 | |
1344 | |
1263 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1345 | glEnable GL_BLEND; |
|
|
1346 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1347 | glEnable GL_TEXTURE_2D; |
1264 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1348 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1349 | glColor 0, 0, 0, 1; |
1265 | |
1350 | |
1266 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
1351 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1267 | |
1352 | |
1268 | glDisable GL_TEXTURE_2D; |
1353 | glDisable GL_TEXTURE_2D; |
1269 | glDisable GL_BLEND; |
1354 | glDisable GL_BLEND; |
1270 | |
1355 | |
1271 | $self->SUPER::_draw; |
1356 | $self->SUPER::_draw; |
… | |
… | |
1279 | |
1364 | |
1280 | my @tex = |
1365 | my @tex = |
1281 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1366 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1282 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1367 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1283 | |
1368 | |
1284 | use SDL; |
1369 | use CFClient::OpenGL; |
1285 | use SDL::OpenGL; |
|
|
1286 | |
1370 | |
1287 | sub new { |
1371 | sub new { |
1288 | my $class = shift; |
1372 | my $class = shift; |
1289 | |
1373 | |
1290 | $class->SUPER::new ( |
1374 | $class->SUPER::new ( |
… | |
… | |
1301 | my ($self) = @_; |
1385 | my ($self) = @_; |
1302 | |
1386 | |
1303 | ($self->{padding} * 2 + 6) x 2 |
1387 | ($self->{padding} * 2 + 6) x 2 |
1304 | } |
1388 | } |
1305 | |
1389 | |
1306 | sub size_allocate { |
|
|
1307 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1308 | |
|
|
1309 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1310 | } |
|
|
1311 | |
|
|
1312 | sub button_down { |
1390 | sub button_down { |
1313 | my ($self, $ev, $x, $y) = @_; |
1391 | my ($self, $ev, $x, $y) = @_; |
1314 | |
1392 | |
1315 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1393 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1316 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1394 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1342 | glDisable GL_BLEND; |
1420 | glDisable GL_BLEND; |
1343 | } |
1421 | } |
1344 | |
1422 | |
1345 | ############################################################################# |
1423 | ############################################################################# |
1346 | |
1424 | |
|
|
1425 | package CFClient::UI::VGauge; |
|
|
1426 | |
|
|
1427 | our @ISA = CFClient::UI::Base::; |
|
|
1428 | |
|
|
1429 | use CFClient::OpenGL; |
|
|
1430 | |
|
|
1431 | my %tex = ( |
|
|
1432 | food => [ |
|
|
1433 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1434 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1435 | ], |
|
|
1436 | grace => [ |
|
|
1437 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1438 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1439 | ], |
|
|
1440 | hp => [ |
|
|
1441 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1442 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1443 | ], |
|
|
1444 | mana => [ |
|
|
1445 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1446 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1447 | ], |
|
|
1448 | ); |
|
|
1449 | |
|
|
1450 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1451 | sub new { |
|
|
1452 | my $class = shift; |
|
|
1453 | |
|
|
1454 | my $self = $class->SUPER::new (gauge => 'food', @_); |
|
|
1455 | |
|
|
1456 | $self |
|
|
1457 | } |
|
|
1458 | |
|
|
1459 | sub size_request { |
|
|
1460 | my ($self) = @_; |
|
|
1461 | |
|
|
1462 | my $tex = $tex{$self->{gauge}}[0]; |
|
|
1463 | |
|
|
1464 | @$tex{qw(w h)} |
|
|
1465 | } |
|
|
1466 | |
|
|
1467 | sub set_max { |
|
|
1468 | my ($self, $max) = @_; |
|
|
1469 | |
|
|
1470 | $self->{max_val} = $max; |
|
|
1471 | } |
|
|
1472 | |
|
|
1473 | sub set_value { |
|
|
1474 | my ($self, $val, $max) = @_; |
|
|
1475 | |
|
|
1476 | $self->set_max ($max) |
|
|
1477 | if defined $max; |
|
|
1478 | |
|
|
1479 | $max = $self->{max_val}; |
|
|
1480 | $self->{val} = $val; |
|
|
1481 | |
|
|
1482 | $self->update; |
|
|
1483 | } |
|
|
1484 | |
|
|
1485 | sub _draw { |
|
|
1486 | my ($self) = @_; |
|
|
1487 | |
|
|
1488 | my $tex = $tex{$self->{gauge}}; |
|
|
1489 | |
|
|
1490 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1491 | |
|
|
1492 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1493 | $ycut = 1 if $self->{val} > $self->{max_val}; |
|
|
1494 | |
|
|
1495 | my $t1 = $tex->[0]; |
|
|
1496 | my $t2 = $tex->[1]; |
|
|
1497 | |
|
|
1498 | glEnable GL_BLEND; |
|
|
1499 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1500 | glEnable GL_TEXTURE_2D; |
|
|
1501 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1502 | |
|
|
1503 | my $h1 = $self->{h} - $ycut * $self->{h}; |
|
|
1504 | my $h2 = $ycut * $self->{h}; |
|
|
1505 | |
|
|
1506 | my $yp = 0; |
|
|
1507 | |
|
|
1508 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1509 | glBegin GL_QUADS; |
|
|
1510 | glTexCoord 0 , 0; glVertex 0 , $yp; |
|
|
1511 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; |
|
|
1512 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; |
|
|
1513 | glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; |
|
|
1514 | glEnd; |
|
|
1515 | |
|
|
1516 | $yp += $h1; |
|
|
1517 | |
|
|
1518 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1519 | glBegin GL_QUADS; |
|
|
1520 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; |
|
|
1521 | glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; |
|
|
1522 | glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; |
|
|
1523 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; |
|
|
1524 | glEnd; |
|
|
1525 | |
|
|
1526 | glDisable GL_BLEND; |
|
|
1527 | glDisable GL_TEXTURE_2D; |
|
|
1528 | } |
|
|
1529 | |
|
|
1530 | ############################################################################# |
|
|
1531 | |
1347 | package CFClient::UI::Slider; |
1532 | package CFClient::UI::Slider; |
1348 | |
1533 | |
1349 | use strict; |
1534 | use strict; |
1350 | |
1535 | |
1351 | use SDL::OpenGL; |
1536 | use CFClient::OpenGL; |
1352 | |
1537 | |
1353 | our @ISA = CFClient::UI::DrawBG::; |
1538 | our @ISA = CFClient::UI::DrawBG::; |
1354 | |
1539 | |
1355 | my @tex = |
1540 | my @tex = |
1356 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1541 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1366 | # TODO: calculations are off |
1551 | # TODO: calculations are off |
1367 | my $self = $class->SUPER::new ( |
1552 | my $self = $class->SUPER::new ( |
1368 | fg => [1, 1, 1], |
1553 | fg => [1, 1, 1], |
1369 | active_fg => [0, 0, 0], |
1554 | active_fg => [0, 0, 0], |
1370 | range => [0, 0, 100, 10], |
1555 | range => [0, 0, 100, 10], |
1371 | req_w => 40, |
1556 | req_w => 20, |
1372 | req_h => 13, |
1557 | req_h => 20, |
1373 | vertical => 0, |
1558 | vertical => 0, |
1374 | can_hover => 1, |
1559 | can_hover => 1, |
1375 | inner_pad => 5, |
1560 | inner_pad => 5, |
1376 | @_ |
1561 | @_ |
1377 | ); |
1562 | ); |
… | |
… | |
1481 | |
1666 | |
1482 | package CFClient::UI::TextView; |
1667 | package CFClient::UI::TextView; |
1483 | |
1668 | |
1484 | our @ISA = CFClient::UI::HBox::; |
1669 | our @ISA = CFClient::UI::HBox::; |
1485 | |
1670 | |
1486 | use SDL::OpenGL; |
1671 | use CFClient::OpenGL; |
1487 | |
1672 | |
1488 | sub new { |
1673 | sub new { |
1489 | my $class = shift; |
1674 | my $class = shift; |
1490 | |
1675 | |
1491 | my $self = $class->SUPER::new ( |
1676 | my $self = $class->SUPER::new ( |
1492 | req_w => $::WIDTH / 6, |
|
|
1493 | req_h => $::HEIGHT / 6, |
|
|
1494 | fontsize => $::FONTSIZE, |
1677 | fontsize => 1, |
1495 | @_, |
1678 | @_, |
1496 | |
1679 | |
1497 | layout => (new CFClient::Layout), |
1680 | layout => (new CFClient::Layout), |
1498 | par => [], |
1681 | par => [], |
1499 | height => 0, |
1682 | height => 0, |
… | |
… | |
1520 | sub text_height { |
1703 | sub text_height { |
1521 | my ($self, $text) = @_; |
1704 | my ($self, $text) = @_; |
1522 | |
1705 | |
1523 | my $layout = $self->{layout}; |
1706 | my $layout = $self->{layout}; |
1524 | |
1707 | |
1525 | $layout->set_height ($self->{fontsize}); |
1708 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1526 | $layout->set_width ($self->{w}); |
1709 | $layout->set_width ($self->{w}); |
1527 | $layout->set_text ($text); |
1710 | $layout->set_text ($text); |
1528 | |
1711 | |
1529 | ($layout->size)[1] |
1712 | ($layout->size)[1] |
1530 | } |
1713 | } |
… | |
… | |
1534 | |
1717 | |
1535 | $self->{need_reflow}++; |
1718 | $self->{need_reflow}++; |
1536 | $self->update; |
1719 | $self->update; |
1537 | } |
1720 | } |
1538 | |
1721 | |
1539 | sub size_request { |
|
|
1540 | my ($self) = @_; |
|
|
1541 | |
|
|
1542 | ($self->{req_w}, $self->{req_h}) |
|
|
1543 | } |
|
|
1544 | |
|
|
1545 | sub size_allocate { |
1722 | sub size_allocate { |
1546 | my ($self, $x, $y, $w, $h) = @_; |
1723 | my ($self, $w, $h) = @_; |
1547 | |
1724 | |
1548 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1725 | $self->SUPER::size_allocate ($w, $h); |
1549 | |
1726 | |
1550 | $self->{layout}->set_height ($self->{fontsize}); |
1727 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1551 | $self->{layout}->set_width ($self->{w}); |
1728 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1552 | |
1729 | |
1553 | $self->reflow; |
1730 | $self->reflow; |
1554 | } |
1731 | } |
1555 | |
1732 | |
1556 | sub add_paragraph { |
1733 | sub add_paragraph { |
… | |
… | |
1575 | |
1752 | |
1576 | return unless $self->{h} > 0; |
1753 | return unless $self->{h} > 0; |
1577 | |
1754 | |
1578 | delete $self->{texture}; |
1755 | delete $self->{texture}; |
1579 | |
1756 | |
1580 | $TOPLEVEL->on_refresh ($self, sub { |
1757 | $ROOT->on_refresh ($self, sub { |
1581 | if (delete $self->{need_reflow}) { |
1758 | if (delete $self->{need_reflow}) { |
1582 | my $height = 0; |
1759 | my $height = 0; |
1583 | |
1760 | |
1584 | $height += $_->[0] = $self->text_height ($_->[2]) |
1761 | $height += $_->[0] = $self->text_height ($_->[2]) |
1585 | for @{$self->{par}}; |
1762 | for @{$self->{par}}; |
… | |
… | |
1643 | |
1820 | |
1644 | } |
1821 | } |
1645 | |
1822 | |
1646 | ############################################################################# |
1823 | ############################################################################# |
1647 | |
1824 | |
1648 | package CFClient::UI::MapWidget; |
|
|
1649 | |
|
|
1650 | use strict; |
|
|
1651 | |
|
|
1652 | use List::Util qw(min max); |
|
|
1653 | |
|
|
1654 | use SDL; |
|
|
1655 | use SDL::OpenGL; |
|
|
1656 | |
|
|
1657 | our @ISA = CFClient::UI::Base::; |
|
|
1658 | |
|
|
1659 | sub new { |
|
|
1660 | my $class = shift; |
|
|
1661 | |
|
|
1662 | $class->SUPER::new ( |
|
|
1663 | z => -1, |
|
|
1664 | can_focus => 1, |
|
|
1665 | list => (glGenLists 1), |
|
|
1666 | @_ |
|
|
1667 | ) |
|
|
1668 | } |
|
|
1669 | |
|
|
1670 | sub key_down { |
|
|
1671 | print "MAPKEYDOWN\n"; |
|
|
1672 | } |
|
|
1673 | |
|
|
1674 | sub key_up { |
|
|
1675 | } |
|
|
1676 | |
|
|
1677 | sub button_down { |
|
|
1678 | my ($self, $ev, $x, $y) = @_; |
|
|
1679 | |
|
|
1680 | $self->focus_in; |
|
|
1681 | |
|
|
1682 | if ($ev->button == 2) { |
|
|
1683 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1684 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1685 | |
|
|
1686 | $self->{motion} = sub { |
|
|
1687 | my ($ev, $x, $y) = @_; |
|
|
1688 | |
|
|
1689 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1690 | |
|
|
1691 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1692 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1693 | |
|
|
1694 | $self->update; |
|
|
1695 | }; |
|
|
1696 | } |
|
|
1697 | } |
|
|
1698 | |
|
|
1699 | sub button_up { |
|
|
1700 | my ($self, $ev, $x, $y) = @_; |
|
|
1701 | |
|
|
1702 | delete $self->{motion}; |
|
|
1703 | } |
|
|
1704 | |
|
|
1705 | sub mouse_motion { |
|
|
1706 | my ($self, $ev, $x, $y) = @_; |
|
|
1707 | |
|
|
1708 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1709 | } |
|
|
1710 | |
|
|
1711 | sub size_request { |
|
|
1712 | ( |
|
|
1713 | 1 + 32 * int $::WIDTH / 32, |
|
|
1714 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1715 | ) |
|
|
1716 | } |
|
|
1717 | |
|
|
1718 | sub update { |
|
|
1719 | my ($self) = @_; |
|
|
1720 | |
|
|
1721 | $self->{need_update} = 1; |
|
|
1722 | $self->SUPER::update; |
|
|
1723 | } |
|
|
1724 | |
|
|
1725 | sub draw { |
|
|
1726 | my ($self) = @_; |
|
|
1727 | |
|
|
1728 | if (delete $self->{need_update}) { |
|
|
1729 | glNewList $self->{list}, GL_COMPILE; |
|
|
1730 | |
|
|
1731 | if ($::MAP) { |
|
|
1732 | my $sw = int $::WIDTH / 32; |
|
|
1733 | my $sh = int $::HEIGHT / 32; |
|
|
1734 | |
|
|
1735 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1736 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1737 | |
|
|
1738 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1739 | |
|
|
1740 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1741 | |
|
|
1742 | if ($::CFG->{fow_enable}) { |
|
|
1743 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1744 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1745 | glConvolutionFilter2D |
|
|
1746 | GL_CONVOLUTION_2D, |
|
|
1747 | GL_ALPHA, |
|
|
1748 | 3, 3, |
|
|
1749 | GL_ALPHA, GL_FLOAT, |
|
|
1750 | pack "f*", |
|
|
1751 | 0.1, 0.1, 0.1, |
|
|
1752 | 0.1, 0.2, 0.1, |
|
|
1753 | 0.1, 0.1, 0.1, |
|
|
1754 | ; |
|
|
1755 | glEnable GL_CONVOLUTION_2D; |
|
|
1756 | } |
|
|
1757 | |
|
|
1758 | my $tex = new CFClient::Texture |
|
|
1759 | w => $w, |
|
|
1760 | h => $h, |
|
|
1761 | data => $data, |
|
|
1762 | internalformat => GL_ALPHA, |
|
|
1763 | format => GL_ALPHA; |
|
|
1764 | |
|
|
1765 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1766 | |
|
|
1767 | glEnable GL_BLEND; |
|
|
1768 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1769 | glEnable GL_TEXTURE_2D; |
|
|
1770 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1771 | |
|
|
1772 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1773 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1774 | |
|
|
1775 | glDisable GL_TEXTURE_2D; |
|
|
1776 | glDisable GL_BLEND; |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | # HACK BEGIN |
|
|
1780 | { |
|
|
1781 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1782 | my ($w, $h) = (250, 250); |
|
|
1783 | |
|
|
1784 | glEnable GL_BLEND; |
|
|
1785 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1786 | glEnable GL_TEXTURE_2D; |
|
|
1787 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1788 | |
|
|
1789 | CFClient::Texture->new ( |
|
|
1790 | w => $w, |
|
|
1791 | h => $h, |
|
|
1792 | data => $::MAP->mapmap ($w, $h), |
|
|
1793 | type => GL_UNSIGNED_INT_8_8_8_8_REV |
|
|
1794 | )->draw_quad (100, 100); |
|
|
1795 | |
|
|
1796 | glDisable GL_TEXTURE_2D; |
|
|
1797 | glDisable GL_BLEND; |
|
|
1798 | } |
|
|
1799 | # HACK END |
|
|
1800 | } |
|
|
1801 | |
|
|
1802 | glEndList; |
|
|
1803 | } |
|
|
1804 | |
|
|
1805 | glPushMatrix; |
|
|
1806 | glCallList $self->{list}; |
|
|
1807 | glPopMatrix; |
|
|
1808 | |
|
|
1809 | if ($FOCUS != $self) { |
|
|
1810 | glColor 64/255, 64/255, 64/255; |
|
|
1811 | glLogicOp GL_AND; |
|
|
1812 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1813 | glBegin GL_QUADS; |
|
|
1814 | glVertex 0, 0; |
|
|
1815 | glVertex 0, $::HEIGHT; |
|
|
1816 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1817 | glVertex $::WIDTH, 0; |
|
|
1818 | glEnd; |
|
|
1819 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1820 | } |
|
|
1821 | } |
|
|
1822 | |
|
|
1823 | my %DIR = ( |
|
|
1824 | SDLK_KP8, [1, "north"], |
|
|
1825 | SDLK_KP9, [2, "northeast"], |
|
|
1826 | SDLK_KP6, [3, "east"], |
|
|
1827 | SDLK_KP3, [4, "southeast"], |
|
|
1828 | SDLK_KP2, [5, "south"], |
|
|
1829 | SDLK_KP1, [6, "southwest"], |
|
|
1830 | SDLK_KP4, [7, "west"], |
|
|
1831 | SDLK_KP7, [8, "northwest"], |
|
|
1832 | |
|
|
1833 | SDLK_UP, [1, "north"], |
|
|
1834 | SDLK_RIGHT, [3, "east"], |
|
|
1835 | SDLK_DOWN, [5, "south"], |
|
|
1836 | SDLK_LEFT, [7, "west"], |
|
|
1837 | ); |
|
|
1838 | |
|
|
1839 | sub key_down { |
|
|
1840 | my ($self, $ev) = @_; |
|
|
1841 | |
|
|
1842 | my $mod = $ev->key_mod; |
|
|
1843 | my $sym = $ev->key_sym; |
|
|
1844 | |
|
|
1845 | if ($sym == SDLK_KP5) { |
|
|
1846 | $::CONN->user_send ("command stay fire"); |
|
|
1847 | } elsif ($sym == SDLK_a) { |
|
|
1848 | $::CONN->user_send ("command apply"); |
|
|
1849 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1850 | $self->emit ('activate_console'); |
|
|
1851 | } elsif ($sym == SDLK_SLASH) { |
|
|
1852 | $self->emit ('activate_console' => '/'); |
|
|
1853 | } elsif (exists $DIR{$sym}) { |
|
|
1854 | if ($mod & KMOD_SHIFT) { |
|
|
1855 | $self->{shft}++; |
|
|
1856 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
|
|
1857 | } elsif ($mod & KMOD_CTRL) { |
|
|
1858 | $self->{ctrl}++; |
|
|
1859 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
|
|
1860 | } else { |
|
|
1861 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
|
|
1862 | } |
|
|
1863 | } |
|
|
1864 | } |
|
|
1865 | |
|
|
1866 | sub key_up { |
|
|
1867 | my ($self, $ev) = @_; |
|
|
1868 | |
|
|
1869 | my $mod = $ev->key_mod; |
|
|
1870 | my $sym = $ev->key_sym; |
|
|
1871 | |
|
|
1872 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1873 | $::CONN->user_send ("command fire_stop"); |
|
|
1874 | } |
|
|
1875 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1876 | $::CONN->user_send ("command run_stop"); |
|
|
1877 | } |
|
|
1878 | } |
|
|
1879 | |
|
|
1880 | ############################################################################# |
|
|
1881 | |
|
|
1882 | package CFClient::UI::Animator; |
1825 | package CFClient::UI::Animator; |
1883 | |
1826 | |
1884 | use SDL::OpenGL; |
1827 | use CFClient::OpenGL; |
1885 | |
1828 | |
1886 | our @ISA = CFClient::UI::Bin::; |
1829 | our @ISA = CFClient::UI::Bin::; |
1887 | |
1830 | |
1888 | sub moveto { |
1831 | sub moveto { |
1889 | my ($self, $x, $y) = @_; |
1832 | my ($self, $x, $y) = @_; |
… | |
… | |
1945 | sub toggle_flopper { |
1888 | sub toggle_flopper { |
1946 | my ($self) = @_; |
1889 | my ($self) = @_; |
1947 | |
1890 | |
1948 | # TODO: use animation |
1891 | # TODO: use animation |
1949 | if ($self->{state} = !$self->{state}) { |
1892 | if ($self->{state} = !$self->{state}) { |
1950 | $CFClient::UI::TOPLEVEL->add ($self->{other}); |
1893 | $CFClient::UI::ROOT->add ($self->{other}); |
1951 | $self->{other}->move ( |
1894 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
1952 | ($::WIDTH - $self->{other}{w}) * 0.5, |
1895 | $self->emit ("open"); |
1953 | ($::HEIGHT - $self->{other}{h}) * 0.5, |
|
|
1954 | ); |
|
|
1955 | } else { |
1896 | } else { |
1956 | $CFClient::UI::TOPLEVEL->remove ($self->{other}); |
1897 | $CFClient::UI::ROOT->remove ($self->{other}); |
|
|
1898 | $self->emit ("close"); |
1957 | } |
1899 | } |
1958 | } |
|
|
1959 | |
1900 | |
1960 | ############################################################################# |
1901 | $self->emit (changed => $self->{state}); |
|
|
1902 | } |
1961 | |
1903 | |
|
|
1904 | ############################################################################# |
|
|
1905 | |
1962 | package CFClient::UI::Toplevel; |
1906 | package CFClient::UI::Root; |
1963 | |
1907 | |
1964 | our @ISA = CFClient::UI::Container::; |
1908 | our @ISA = CFClient::UI::Container::; |
1965 | |
1909 | |
1966 | use SDL::OpenGL; |
1910 | use CFClient::OpenGL; |
|
|
1911 | |
|
|
1912 | sub check_size { |
|
|
1913 | my ($self) = @_; |
|
|
1914 | |
|
|
1915 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
|
|
1916 | } |
1967 | |
1917 | |
1968 | sub size_request { |
1918 | sub size_request { |
1969 | ($::WIDTH, $::HEIGHT) |
1919 | ($::WIDTH, $::HEIGHT) |
1970 | } |
1920 | } |
1971 | |
1921 | |
1972 | sub size_allocate { |
1922 | sub configure { |
1973 | my ($self, $x, $y, $w, $h) = @_; |
1923 | my ($self, $x, $y, $w, $h) = @_; |
1974 | |
1924 | |
1975 | $self->_size_allocate ($x, $y, $w, $h); |
1925 | $self->SUPER::configure ($x, $y, $w, $h); |
1976 | |
1926 | |
1977 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1927 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
1978 | for @{$self->{children}}; |
1928 | for @{$self->{children}}; |
1979 | } |
1929 | } |
1980 | |
1930 | |
1981 | sub translate { |
1931 | sub _topleft { |
1982 | my ($self, $x, $y) = @_; |
1932 | my ($self, $x, $y) = @_; |
1983 | |
1933 | |
1984 | ($x, $y) |
1934 | ($x, $y) |
1985 | } |
1935 | } |
1986 | |
1936 | |
1987 | sub update { |
1937 | sub update { |
1988 | my ($self) = @_; |
1938 | my ($self) = @_; |
1989 | |
1939 | |
1990 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1940 | $self->check_size; |
1991 | ::refresh (); |
1941 | ::refresh (); |
1992 | } |
1942 | } |
1993 | |
1943 | |
1994 | sub add { |
1944 | sub add { |
1995 | my ($self, $widget) = @_; |
1945 | my ($self, $child) = @_; |
1996 | |
1946 | |
1997 | $self->SUPER::add ($widget); |
1947 | $self->SUPER::add ($child); |
1998 | |
|
|
1999 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
|
|
2000 | } |
1948 | } |
2001 | |
1949 | |
2002 | sub on_refresh { |
1950 | sub on_refresh { |
2003 | my ($self, $id, $cb) = @_; |
1951 | my ($self, $id, $cb) = @_; |
2004 | |
1952 | |
… | |
… | |
2027 | |
1975 | |
2028 | ############################################################################# |
1976 | ############################################################################# |
2029 | |
1977 | |
2030 | package CFClient::UI; |
1978 | package CFClient::UI; |
2031 | |
1979 | |
2032 | $TOPLEVEL = new CFClient::UI::Toplevel; |
1980 | $ROOT = new CFClient::UI::Root; |
2033 | |
1981 | |
2034 | 1 |
1982 | 1 |
2035 | |
1983 | |