… | |
… | |
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 = $ROOT->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->coord2local ($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 || $ROOT->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->coord2local ($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; |
… | |
… | |
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 || $ROOT->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; |
… | |
… | |
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} = int $x; |
132 | $self->{x} = int $x; |
118 | $self->{y} = int $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 | } |
125 | |
142 | |
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 abstract"; |
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 | |
|
|
151 | if ($self->{aspect}) { |
|
|
152 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
153 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
154 | |
|
|
155 | # use alignment to adjust x, y |
|
|
156 | |
|
|
157 | $x += int +($w - $w2) * 0.5; |
|
|
158 | $y += int +($h - $h2) * 0.5; |
|
|
159 | |
|
|
160 | ($w, $h) = ($w2, $h2); |
|
|
161 | } |
|
|
162 | |
|
|
163 | if ($self->{x} != $x || $self->{y} != $y) { |
134 | $self->{x} = $x; |
164 | $self->{x} = $x; |
135 | $self->{y} = $y; |
165 | $self->{y} = $y; |
|
|
166 | $self->update; |
|
|
167 | } |
136 | |
168 | |
137 | return unless $self->{w} != $w || $self->{h} != $h; |
169 | if ($self->{w} != $w || $self->{h} != $h) { |
138 | |
|
|
139 | $self->{w} = $w; |
170 | $self->{w} = $w; |
140 | $self->{h} = $h; |
171 | $self->{h} = $h; |
141 | |
172 | |
|
|
173 | $self->size_allocate ($w, $h); |
|
|
174 | $self->update; |
142 | 1 |
175 | } |
143 | } |
176 | } |
144 | |
177 | |
145 | sub size_allocate { |
178 | sub size_allocate { |
146 | my ($self, $x, $y, $w, $h) = @_; |
179 | # nothing to be done |
147 | |
|
|
148 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
149 | } |
180 | } |
150 | |
181 | |
151 | # return top left coordinates |
182 | # return top left coordinates |
152 | sub _topleft { |
183 | sub _topleft { |
153 | my ($self, $x, $y) = @_; |
184 | my ($self, $x, $y) = @_; |
… | |
… | |
176 | |
207 | |
177 | return if $FOCUS == $self; |
208 | return if $FOCUS == $self; |
178 | return unless $self->{can_focus}; |
209 | return unless $self->{can_focus}; |
179 | |
210 | |
180 | my $focus = $FOCUS; $FOCUS = $self; |
211 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
212 | |
|
|
213 | $self->emit (focus_in => $focus); |
|
|
214 | |
181 | $focus->update if $focus; |
215 | $focus->update if $focus; |
182 | $FOCUS->update; |
216 | $FOCUS->update; |
183 | } |
217 | } |
184 | |
218 | |
185 | sub focus_out { |
219 | sub focus_out { |
186 | my ($self) = @_; |
220 | my ($self) = @_; |
187 | |
221 | |
188 | return unless $FOCUS == $self; |
222 | return unless $FOCUS == $self; |
189 | |
223 | |
190 | my $focus = $FOCUS; undef $FOCUS; |
224 | my $focus = $FOCUS; undef $FOCUS; |
|
|
225 | |
|
|
226 | $self->emit (focus_out => $focus); |
|
|
227 | |
191 | $focus->update if $focus; #? |
228 | $focus->update if $focus; #? |
192 | } |
229 | } |
193 | |
230 | |
194 | sub mouse_motion { } |
231 | sub mouse_motion { } |
195 | sub button_up { } |
232 | sub button_up { } |
… | |
… | |
219 | glPopMatrix; |
256 | glPopMatrix; |
220 | |
257 | |
221 | if ($self == $HOVER && $self->{can_hover}) { |
258 | if ($self == $HOVER && $self->{can_hover}) { |
222 | my ($x, $y) = @$self{qw(x y)}; |
259 | my ($x, $y) = @$self{qw(x y)}; |
223 | |
260 | |
224 | glColor 0, 0, 1, 0.2; |
261 | glColor 1, 0.8, 0.5, 0.2; |
225 | glEnable GL_BLEND; |
262 | glEnable GL_BLEND; |
226 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
263 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
227 | glBegin GL_QUADS; |
264 | glBegin GL_QUADS; |
228 | glVertex $x , $y; |
265 | glVertex $x , $y; |
229 | glVertex $x + $self->{w}, $y; |
266 | glVertex $x + $self->{w}, $y; |
… | |
… | |
254 | my ($self, $parent) = @_; |
291 | my ($self, $parent) = @_; |
255 | |
292 | |
256 | Scalar::Util::weaken ($self->{parent} = $parent); |
293 | Scalar::Util::weaken ($self->{parent} = $parent); |
257 | } |
294 | } |
258 | |
295 | |
|
|
296 | sub check_size { |
|
|
297 | my ($self) = @_; |
|
|
298 | |
|
|
299 | my ($w, $h) = $self->size_request; |
|
|
300 | |
|
|
301 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
302 | $self->{req_w} = $w; |
|
|
303 | $self->{req_h} = $h; |
|
|
304 | |
|
|
305 | $self->{parent}->check_size |
|
|
306 | if $self->{parent}; |
|
|
307 | } |
|
|
308 | } |
|
|
309 | |
259 | sub update { |
310 | sub update { |
260 | my ($self) = @_; |
311 | my ($self) = @_; |
261 | |
312 | |
262 | $self->{parent}->update |
313 | $self->{parent}->update |
263 | if $self->{parent}; |
314 | if $self->{parent}; |
264 | } |
315 | } |
265 | |
316 | |
266 | sub connect { |
317 | sub connect { |
267 | my ($self, $signal, $cb) = @_; |
318 | my ($self, $signal, $cb) = @_; |
268 | |
319 | |
269 | push @{ $self->{cb}{$signal} }, $cb; |
320 | push @{ $self->{signal_cb}{$signal} }, $cb; |
270 | } |
321 | } |
271 | |
322 | |
272 | sub emit { |
323 | sub emit { |
273 | my ($self, $signal, @args) = @_; |
324 | my ($self, $signal, @args) = @_; |
274 | |
325 | |
|
|
326 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
275 | $_->($self, @args) |
327 | $cb->($self, @args); |
276 | for @{$self->{cb}{$signal} || []}; |
328 | } |
277 | } |
329 | } |
278 | |
330 | |
279 | sub DESTROY { |
331 | sub DESTROY { |
280 | my ($self) = @_; |
332 | my ($self) = @_; |
281 | |
333 | |
… | |
… | |
287 | package CFClient::UI::DrawBG; |
339 | package CFClient::UI::DrawBG; |
288 | |
340 | |
289 | our @ISA = CFClient::UI::Base::; |
341 | our @ISA = CFClient::UI::Base::; |
290 | |
342 | |
291 | use strict; |
343 | use strict; |
292 | use SDL::OpenGL; |
344 | use CFClient::OpenGL; |
293 | |
345 | |
294 | sub new { |
346 | sub new { |
295 | my $class = shift; |
347 | my $class = shift; |
296 | |
348 | |
297 | # range [value, low, high, page] |
349 | # range [value, low, high, page] |
… | |
… | |
361 | $self->{children} = [ |
413 | $self->{children} = [ |
362 | sort { $a->{z} <=> $b->{z} } |
414 | sort { $a->{z} <=> $b->{z} } |
363 | @{$self->{children}}, $child |
415 | @{$self->{children}}, $child |
364 | ]; |
416 | ]; |
365 | |
417 | |
366 | $self->{w} = $self->{h} = -1; |
418 | $child->check_size; |
367 | $self->update; |
|
|
368 | } |
419 | } |
369 | |
420 | |
370 | sub remove { |
421 | sub remove { |
371 | my ($self, $widget) = @_; |
422 | my ($self, $child) = @_; |
372 | |
423 | |
|
|
424 | delete $child->{parent}; |
|
|
425 | |
373 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
426 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
374 | |
427 | |
375 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
428 | $self->check_size; |
376 | } |
429 | } |
377 | |
430 | |
378 | sub find_widget { |
431 | sub find_widget { |
379 | my ($self, $x, $y) = @_; |
432 | my ($self, $x, $y) = @_; |
380 | |
433 | |
… | |
… | |
410 | |
463 | |
411 | $class->SUPER::new (children => [$child], %arg) |
464 | $class->SUPER::new (children => [$child], %arg) |
412 | } |
465 | } |
413 | |
466 | |
414 | sub add { |
467 | sub add { |
415 | my ($self, $widget) = @_; |
468 | my ($self, $child) = @_; |
416 | |
469 | |
417 | $self->{children} = []; |
470 | $self->{children} = []; |
418 | |
471 | |
419 | $self->SUPER::add ($widget); |
472 | $self->SUPER::add ($child); |
420 | } |
473 | } |
421 | |
474 | |
422 | sub remove { |
475 | sub remove { |
423 | my ($self, $widget) = @_; |
476 | my ($self, $widget) = @_; |
424 | |
477 | |
… | |
… | |
433 | sub size_request { |
486 | sub size_request { |
434 | $_[0]{children}[0]->size_request |
487 | $_[0]{children}[0]->size_request |
435 | } |
488 | } |
436 | |
489 | |
437 | sub size_allocate { |
490 | sub size_allocate { |
438 | my ($self, $x, $y, $w, $h) = @_; |
491 | my ($self, $w, $h) = @_; |
439 | |
492 | |
440 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
441 | |
|
|
442 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
493 | $self->{children}[0]->configure (0, 0, $w, $h); |
443 | } |
494 | } |
444 | |
495 | |
445 | ############################################################################# |
496 | ############################################################################# |
446 | |
497 | |
447 | package CFClient::UI::Window; |
498 | package CFClient::UI::Window; |
448 | |
499 | |
449 | our @ISA = CFClient::UI::Bin::; |
500 | our @ISA = CFClient::UI::Bin::; |
450 | |
501 | |
451 | use SDL::OpenGL; |
502 | use CFClient::OpenGL; |
452 | |
503 | |
453 | sub new { |
504 | sub new { |
454 | my ($class, %arg) = @_; |
505 | my ($class, %arg) = @_; |
455 | |
506 | |
456 | my $self = $class->SUPER::new (%arg); |
507 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
473 | $self->child->draw; |
524 | $self->child->draw; |
474 | }; |
525 | }; |
475 | } |
526 | } |
476 | |
527 | |
477 | sub size_allocate { |
528 | sub size_allocate { |
478 | my ($self, $x, $y, $w, $h) = @_; |
529 | my ($self, $w, $h) = @_; |
479 | |
530 | |
480 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
481 | |
|
|
482 | $self->child->size_allocate (0, 0, $w, $h); |
531 | $self->child->configure (0, 0, $w, $h); |
483 | |
532 | |
484 | $self->render_chld; |
533 | $self->render_chld; |
485 | } |
534 | } |
486 | |
535 | |
487 | sub _draw { |
536 | sub _draw { |
… | |
… | |
503 | glDisable GL_TEXTURE_2D; |
552 | glDisable GL_TEXTURE_2D; |
504 | } |
553 | } |
505 | |
554 | |
506 | ############################################################################# |
555 | ############################################################################# |
507 | |
556 | |
|
|
557 | package CFClient::UI::ViewPort; |
|
|
558 | |
|
|
559 | our @ISA = CFClient::UI::Window::; |
|
|
560 | |
|
|
561 | sub new { die } |
|
|
562 | |
|
|
563 | sub size_request { |
|
|
564 | my ($self) = @_; |
|
|
565 | |
|
|
566 | @$self{qw(child_w child_h)} = $self->child->size_request; |
|
|
567 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
|
|
568 | |
|
|
569 | @$self{qw(child_w child_h)} |
|
|
570 | } |
|
|
571 | |
|
|
572 | sub _draw { |
|
|
573 | my ($self) = @_; |
|
|
574 | |
|
|
575 | $self->{children}[1]->draw; |
|
|
576 | } |
|
|
577 | |
|
|
578 | |
|
|
579 | ############################################################################# |
|
|
580 | |
508 | package CFClient::UI::Frame; |
581 | package CFClient::UI::Frame; |
509 | |
582 | |
510 | our @ISA = CFClient::UI::Bin::; |
583 | our @ISA = CFClient::UI::Bin::; |
511 | |
584 | |
512 | use SDL::OpenGL; |
585 | use CFClient::OpenGL; |
513 | |
586 | |
514 | sub size_request { |
587 | sub size_request { |
515 | my ($self) = @_; |
588 | my ($self) = @_; |
516 | my $chld = $self->child |
589 | my $chld = $self->child |
517 | or return (0, 0); |
590 | or return (0, 0); |
… | |
… | |
522 | } |
595 | } |
523 | |
596 | |
524 | sub size_allocate { |
597 | sub size_allocate { |
525 | my ($self, $x, $y, $w, $h) = @_; |
598 | my ($self, $x, $y, $w, $h) = @_; |
526 | |
599 | |
527 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
528 | |
|
|
529 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
600 | $self->child->configure (2, 2, $w - 4, $h - 4); |
530 | } |
601 | } |
531 | |
602 | |
532 | sub _draw { |
603 | sub _draw { |
533 | my ($self) = @_; |
604 | my ($self) = @_; |
534 | |
605 | |
… | |
… | |
551 | |
622 | |
552 | package CFClient::UI::FancyFrame; |
623 | package CFClient::UI::FancyFrame; |
553 | |
624 | |
554 | our @ISA = CFClient::UI::Bin::; |
625 | our @ISA = CFClient::UI::Bin::; |
555 | |
626 | |
556 | use SDL::OpenGL; |
627 | use CFClient::OpenGL; |
557 | |
628 | |
558 | my @tex = |
629 | my @tex = |
559 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
630 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
560 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
631 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
561 | |
632 | |
562 | sub new { |
633 | sub new { |
563 | my $class = shift; |
634 | my $class = shift; |
564 | |
635 | |
565 | # TODO: user_x, user_y, overwrite moveto? |
636 | # TODO: user_x, user_y, overwrite moveto? |
566 | |
637 | |
567 | $class->SUPER::new ( |
638 | my $self = $class->SUPER::new ( |
568 | bg => [1, 1, 1, 1], |
639 | bg => [1, 1, 1, 1], |
569 | border_bg => [1, 1, 1, 1], |
640 | border_bg => [1, 1, 1, 1], |
570 | border => int $::FONTSIZE * 0.8, |
641 | border => 0.8, |
571 | @_ |
642 | @_ |
|
|
643 | ); |
|
|
644 | |
|
|
645 | $self->{title} &&= new CFClient::UI::Label |
|
|
646 | align => 0, |
|
|
647 | valign => 1, |
|
|
648 | text => $self->{title}, |
|
|
649 | fontsize => 1; |
|
|
650 | |
|
|
651 | $self |
|
|
652 | } |
|
|
653 | |
|
|
654 | sub border { |
|
|
655 | int $_[0]{border} * $::FONTSIZE |
|
|
656 | } |
|
|
657 | |
|
|
658 | sub size_request { |
|
|
659 | my ($self) = @_; |
|
|
660 | |
|
|
661 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
662 | |
|
|
663 | my ($w, $h) = $self->SUPER::size_request; |
|
|
664 | |
|
|
665 | ( |
|
|
666 | $w + $self->border * 2, |
|
|
667 | $h + $self->border * 2, |
572 | ) |
668 | ) |
573 | } |
669 | } |
574 | |
670 | |
575 | sub size_request { |
|
|
576 | my ($self) = @_; |
|
|
577 | |
|
|
578 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
579 | |
|
|
580 | my ($w, $h) = $self->SUPER::size_request; |
|
|
581 | |
|
|
582 | ( |
|
|
583 | $w + $self->{border} * 2, |
|
|
584 | $h + $self->{border} * 2, |
|
|
585 | ) |
|
|
586 | } |
|
|
587 | |
|
|
588 | sub size_allocate { |
671 | sub size_allocate { |
589 | my ($self, $x, $y, $w, $h) = @_; |
672 | my ($self, $w, $h) = @_; |
590 | |
673 | |
591 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
592 | |
|
|
593 | $h -= List::Util::max 0, $self->{border} * 2; |
674 | $h -= List::Util::max 0, $self->border * 2; |
594 | $w -= List::Util::max 0, $self->{border} * 2; |
675 | $w -= List::Util::max 0, $self->border * 2; |
595 | |
676 | |
|
|
677 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
678 | if $self->{title}; |
|
|
679 | |
596 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
680 | $self->child->configure ($self->border, $self->border, $w, $h); |
597 | } |
681 | } |
598 | |
682 | |
599 | sub button_down { |
683 | sub button_down { |
600 | my ($self, $ev, $x, $y) = @_; |
684 | my ($self, $ev, $x, $y) = @_; |
601 | |
685 | |
|
|
686 | my $border = $self->border; |
|
|
687 | |
602 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
688 | if ($x < $self->{w} && $x >= $self->{w} - $border |
603 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
689 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
604 | |
690 | |
605 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
691 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
606 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
692 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
607 | |
693 | |
608 | $self->{motion} = sub { |
694 | $self->{motion} = sub { |
609 | my ($ev, $x, $y) = @_; |
695 | my ($ev, $x, $y) = @_; |
610 | |
696 | |
611 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
697 | ($x, $y) = ($ev->{x}, $ev->{y}); |
612 | |
698 | |
613 | $self->{user_w} = $bw + $x - $ox; |
699 | $self->{user_w} = $bw + $x - $ox; |
614 | $self->{user_h} = $bh + $y - $oy; |
700 | $self->{user_h} = $bh + $y - $oy; |
615 | $self->update; |
701 | $self->check_size; |
616 | }; |
702 | }; |
617 | |
703 | |
618 | } elsif ($x >= 0 && $x < $self->{w} |
704 | } elsif ($x >= 0 && $x < $self->{w} |
619 | && $y >= 0 && $y < $self->{border}) { |
705 | && $y >= 0 && $y < $border) { |
620 | |
706 | |
621 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
707 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
622 | my ($bx, $by) = ($self->{x}, $self->{y}); |
708 | my ($bx, $by) = ($self->{x}, $self->{y}); |
623 | |
709 | |
624 | $self->{motion} = sub { |
710 | $self->{motion} = sub { |
625 | my ($ev, $x, $y) = @_; |
711 | my ($ev, $x, $y) = @_; |
626 | |
712 | |
627 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
713 | ($x, $y) = ($ev->{x}, $ev->{y}); |
628 | |
714 | |
629 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
715 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
630 | $self->update; |
716 | $self->update; |
631 | }; |
717 | }; |
632 | } |
718 | } |
… | |
… | |
653 | glEnable GL_BLEND; |
739 | glEnable GL_BLEND; |
654 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
740 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
655 | glEnable GL_TEXTURE_2D; |
741 | glEnable GL_TEXTURE_2D; |
656 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
742 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
657 | |
743 | |
|
|
744 | my $border = $self->border; |
|
|
745 | |
658 | glColor @{ $self->{border_bg} }; |
746 | glColor @{ $self->{border_bg} }; |
659 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
747 | $tex[1]->draw_quad (0, 0, $w, $border); |
660 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
748 | $tex[3]->draw_quad (0, $border, $border, $ch); |
661 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
749 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
662 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
750 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
663 | |
751 | |
664 | my $bg = $tex[0]; |
752 | my $bg = $tex[0]; |
665 | |
753 | |
666 | # TODO: repeat texture not scale |
754 | # TODO: repeat texture not scale |
667 | my $rep_x = $cw / $bg->{w}; |
755 | my $rep_x = $cw / $bg->{w}; |
… | |
… | |
670 | glColor @{ $self->{bg} }; |
758 | glColor @{ $self->{bg} }; |
671 | |
759 | |
672 | $bg->{s} = $rep_x; |
760 | $bg->{s} = $rep_x; |
673 | $bg->{t} = $rep_y; |
761 | $bg->{t} = $rep_y; |
674 | $bg->{wrap_mode} = 1; |
762 | $bg->{wrap_mode} = 1; |
675 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
763 | $bg->draw_quad ($border, $border, $cw, $ch); |
676 | |
764 | |
677 | glDisable GL_TEXTURE_2D; |
765 | glDisable GL_TEXTURE_2D; |
678 | glDisable GL_BLEND; |
766 | glDisable GL_BLEND; |
679 | |
767 | |
|
|
768 | $self->{title}->draw if $self->{title}; |
680 | $self->child->draw; |
769 | $self->child->draw; |
681 | } |
770 | } |
682 | |
771 | |
683 | ############################################################################# |
772 | ############################################################################# |
684 | |
773 | |
… | |
… | |
686 | |
775 | |
687 | our @ISA = CFClient::UI::Base::; |
776 | our @ISA = CFClient::UI::Base::; |
688 | |
777 | |
689 | use List::Util qw(max sum); |
778 | use List::Util qw(max sum); |
690 | |
779 | |
691 | use SDL::OpenGL; |
780 | use CFClient::OpenGL; |
692 | |
781 | |
693 | sub new { |
782 | sub new { |
694 | my $class = shift; |
783 | my $class = shift; |
695 | |
784 | |
696 | $class->SUPER::new ( |
785 | $class->SUPER::new ( |
… | |
… | |
703 | my ($self, $x, $y, $child) = @_; |
792 | my ($self, $x, $y, $child) = @_; |
704 | |
793 | |
705 | $child->set_parent ($self); |
794 | $child->set_parent ($self); |
706 | $self->{children}[$y][$x] = $child; |
795 | $self->{children}[$y][$x] = $child; |
707 | |
796 | |
708 | $self->{w} = $self->{h} = -1; |
797 | $child->check_size; |
709 | $self->update; |
|
|
710 | } |
798 | } |
711 | |
799 | |
712 | # TODO: move to container class maybe? send childs a signal on removal? |
800 | # TODO: move to container class maybe? send childs a signal on removal? |
713 | sub clear { |
801 | sub clear { |
714 | my ($self) = @_; |
802 | my ($self) = @_; |
… | |
… | |
749 | (sum @$hs), |
837 | (sum @$hs), |
750 | ) |
838 | ) |
751 | } |
839 | } |
752 | |
840 | |
753 | sub size_allocate { |
841 | sub size_allocate { |
754 | my ($self, $x, $y, $w, $h) = @_; |
842 | my ($self, $w, $h) = @_; |
755 | |
|
|
756 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
757 | |
843 | |
758 | my ($ws, $hs) = $self->get_wh; |
844 | my ($ws, $hs) = $self->get_wh; |
759 | |
845 | |
760 | my $req_w = sum @$ws; |
846 | my $req_w = sum @$ws; |
761 | my $req_h = sum @$hs; |
847 | my $req_h = sum @$hs; |
… | |
… | |
783 | |
869 | |
784 | for my $c (0 .. $#$row) { |
870 | for my $c (0 .. $#$row) { |
785 | my $col_w = $ws->[$c]; |
871 | my $col_w = $ws->[$c]; |
786 | |
872 | |
787 | if (my $widget = $row->[$c]) { |
873 | if (my $widget = $row->[$c]) { |
788 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
874 | $widget->configure ($x, $y, $col_w, $row_h); |
789 | } |
875 | } |
790 | |
876 | |
791 | $x += $col_w; |
877 | $x += $col_w; |
792 | } |
878 | } |
793 | |
879 | |
… | |
… | |
838 | (List::Util::max map $_->[1], @alloc), |
924 | (List::Util::max map $_->[1], @alloc), |
839 | ) |
925 | ) |
840 | } |
926 | } |
841 | |
927 | |
842 | sub size_allocate { |
928 | sub size_allocate { |
843 | my ($self, $x, $y, $w, $h) = @_; |
929 | my ($self, $w, $h) = @_; |
844 | |
|
|
845 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
846 | |
930 | |
847 | ($h, $w) = ($w, $h); |
931 | ($h, $w) = ($w, $h); |
848 | |
932 | |
849 | my $children = $self->{children}; |
933 | my $children = $self->{children}; |
850 | |
934 | |
… | |
… | |
872 | |
956 | |
873 | my $y = 0; |
957 | my $y = 0; |
874 | for (0 .. $#$children) { |
958 | for (0 .. $#$children) { |
875 | my $child = $children->[$_]; |
959 | my $child = $children->[$_]; |
876 | my $h = $h[$_]; |
960 | my $h = $h[$_]; |
877 | $child->size_allocate ($y, 0, $h, $w); |
961 | $child->configure ($y, 0, $h, $w); |
878 | |
962 | |
879 | $y += $h; |
963 | $y += $h; |
880 | } |
964 | } |
|
|
965 | |
|
|
966 | 1 |
881 | } |
967 | } |
882 | |
968 | |
883 | ############################################################################# |
969 | ############################################################################# |
884 | |
970 | |
885 | package CFClient::UI::VBox; |
971 | package CFClient::UI::VBox; |
… | |
… | |
898 | (List::Util::sum map $_->[1], @alloc), |
984 | (List::Util::sum map $_->[1], @alloc), |
899 | ) |
985 | ) |
900 | } |
986 | } |
901 | |
987 | |
902 | sub size_allocate { |
988 | sub size_allocate { |
903 | my ($self, $x, $y, $w, $h) = @_; |
989 | my ($self, $w, $h) = @_; |
904 | |
|
|
905 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
906 | |
990 | |
907 | my $children = $self->{children}; |
991 | my $children = $self->{children}; |
908 | |
992 | |
909 | my @h = map +($_->size_request)[1], @$children; |
993 | my @h = map +($_->size_request)[1], @$children; |
910 | |
994 | |
… | |
… | |
928 | |
1012 | |
929 | my $y = 0; |
1013 | my $y = 0; |
930 | for (0 .. $#$children) { |
1014 | for (0 .. $#$children) { |
931 | my $child = $children->[$_]; |
1015 | my $child = $children->[$_]; |
932 | my $h = $h[$_]; |
1016 | my $h = $h[$_]; |
933 | $child->size_allocate (0, $y, $w, $h); |
1017 | $child->configure (0, $y, $w, $h); |
934 | |
1018 | |
935 | $y += $h; |
1019 | $y += $h; |
936 | } |
1020 | } |
|
|
1021 | |
|
|
1022 | 1 |
937 | } |
1023 | } |
938 | |
1024 | |
939 | ############################################################################# |
1025 | ############################################################################# |
940 | |
1026 | |
941 | package CFClient::UI::Label; |
1027 | package CFClient::UI::Label; |
942 | |
1028 | |
943 | our @ISA = CFClient::UI::Base::; |
1029 | our @ISA = CFClient::UI::Base::; |
944 | |
1030 | |
945 | use SDL::OpenGL; |
1031 | use CFClient::OpenGL; |
946 | |
1032 | |
947 | sub new { |
1033 | sub new { |
948 | my ($class, %arg) = @_; |
1034 | my ($class, %arg) = @_; |
949 | |
1035 | |
950 | my $self = $class->SUPER::new ( |
1036 | my $self = $class->SUPER::new ( |
951 | fg => [1, 1, 1], |
1037 | fg => [1, 1, 1], |
952 | fontsize => $::FONTSIZE, |
1038 | fontsize => 1, |
953 | text => "", |
1039 | text => "", |
954 | align => -1, |
1040 | align => -1, |
955 | valign => -1, |
1041 | valign => -1, |
956 | padding => 2, |
1042 | padding => 2, |
957 | layout => new CFClient::Layout, |
1043 | layout => new CFClient::Layout, |
958 | %arg |
1044 | %arg |
959 | ); |
1045 | ); |
960 | |
1046 | |
961 | $self->set_text ($self->{text}); |
1047 | if (exists $self->{template}) { |
|
|
1048 | my $layout = new CFClient::Layout; |
|
|
1049 | $layout->set_text (delete $self->{template}); |
|
|
1050 | $self->{template} = $layout; |
|
|
1051 | } |
|
|
1052 | |
|
|
1053 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
|
|
1054 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
962 | |
1055 | |
963 | $self |
1056 | $self |
964 | } |
1057 | } |
965 | |
1058 | |
966 | sub escape_text { |
1059 | sub escape_text { |
… | |
… | |
974 | } |
1067 | } |
975 | |
1068 | |
976 | sub set_text { |
1069 | sub set_text { |
977 | my ($self, $text) = @_; |
1070 | my ($self, $text) = @_; |
978 | |
1071 | |
979 | $self->{text} = $text; |
|
|
980 | $self->{layout}->set_markup ($text); |
1072 | $self->{layout}->set_text ($text); |
981 | |
1073 | |
982 | delete $self->{texture}; |
1074 | delete $self->{texture}; |
983 | # $self->{w} = $self->{h} = -1; |
|
|
984 | $self->update; |
1075 | $self->update; |
985 | } |
1076 | } |
986 | |
1077 | |
987 | sub get_text { |
1078 | sub set_markup { |
988 | my ($self, $text) = @_; |
1079 | my ($self, $markup) = @_; |
989 | |
1080 | |
990 | $self->{text} |
1081 | $self->{layout}->set_markup ($markup); |
|
|
1082 | |
|
|
1083 | delete $self->{texture}; |
|
|
1084 | $self->update; |
991 | } |
1085 | } |
992 | |
1086 | |
993 | sub size_request { |
1087 | sub size_request { |
994 | my ($self) = @_; |
1088 | my ($self) = @_; |
995 | |
1089 | |
996 | $self->{layout}->set_width; |
1090 | $self->{layout}->set_width; |
997 | $self->{layout}->set_height ($self->{fontsize}); |
1091 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1092 | |
998 | my ($w, $h) = $self->{layout}->size; |
1093 | my ($w, $h) = $self->{layout}->size; |
|
|
1094 | |
|
|
1095 | if (exists $self->{template}) { |
|
|
1096 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1097 | |
|
|
1098 | my ($w2, $h2) = $self->{template}->size; |
|
|
1099 | |
|
|
1100 | $w = List::Util::max $w, $w2; |
|
|
1101 | $h = List::Util::max $h, $h2; |
|
|
1102 | } |
999 | |
1103 | |
1000 | ( |
1104 | ( |
1001 | $w + $self->{padding} * 2, |
1105 | $w + $self->{padding} * 2, |
1002 | $h + $self->{padding} * 2, |
1106 | $h + $self->{padding} * 2, |
1003 | ) |
1107 | ) |
1004 | } |
1108 | } |
1005 | |
1109 | |
1006 | sub size_allocate { |
1110 | sub size_allocate { |
1007 | my ($self, $x, $y, $w, $h) = @_; |
1111 | my ($self, $w, $h) = @_; |
1008 | |
|
|
1009 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
1010 | |
1112 | |
1011 | delete $self->{texture}; |
1113 | delete $self->{texture}; |
1012 | } |
1114 | } |
1013 | |
1115 | |
1014 | sub update { |
1116 | sub set_fontsize { |
1015 | my ($self) = @_; |
1117 | my ($self, $fontsize) = @_; |
1016 | |
1118 | |
1017 | delete $self->{texture}; |
1119 | $self->{fontsize} = $fontsize; |
1018 | $self->SUPER::update; |
1120 | $self->check_size; |
1019 | } |
1121 | } |
1020 | |
1122 | |
1021 | sub _draw { |
1123 | sub _draw { |
1022 | my ($self) = @_; |
1124 | my ($self) = @_; |
1023 | |
1125 | |
1024 | my $tex = $self->{texture} ||= do { |
1126 | my $tex = $self->{texture} ||= do { |
1025 | $self->{layout}->set_width ($self->{w}); |
1127 | $self->{layout}->set_width ($self->{w}); |
1026 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
1128 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1027 | new_from_layout CFClient::Texture $self->{layout} |
1129 | new_from_layout CFClient::Texture $self->{layout} |
1028 | }; |
1130 | }; |
1029 | |
1131 | |
1030 | glEnable GL_BLEND; |
1132 | glEnable GL_BLEND; |
1031 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1133 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1032 | glEnable GL_TEXTURE_2D; |
1134 | glEnable GL_TEXTURE_2D; |
1033 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1135 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1034 | |
1136 | |
1035 | glColor @{$self->{fg}}; |
1137 | glColor @{$self->{fg}}; |
1036 | |
1138 | |
1037 | my $x = |
1139 | $self->{ox} = int ( |
1038 | $self->{align} < 0 ? $self->{padding} |
1140 | $self->{align} < 0 ? $self->{padding} |
1039 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1141 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1040 | : ($self->{w} - $tex->{w}) * 0.5; |
1142 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1143 | ); |
1041 | |
1144 | |
1042 | my $y = |
1145 | $self->{oy} = int ( |
1043 | $self->{valign} < 0 ? $self->{padding} |
1146 | $self->{valign} < 0 ? $self->{padding} |
1044 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1147 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1045 | : ($self->{h} - $tex->{h}) * 0.5; |
1148 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1149 | ); |
1046 | |
1150 | |
1047 | $tex->draw_quad (int $x, int $y); |
1151 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
1048 | |
1152 | |
1049 | glDisable GL_TEXTURE_2D; |
1153 | glDisable GL_TEXTURE_2D; |
1050 | glDisable GL_BLEND; |
1154 | glDisable GL_BLEND; |
1051 | } |
1155 | } |
1052 | |
1156 | |
1053 | ############################################################################# |
1157 | ############################################################################# |
1054 | |
1158 | |
1055 | package CFClient::UI::Entry; |
1159 | package CFClient::UI::EntryBase; |
1056 | |
1160 | |
1057 | our @ISA = CFClient::UI::Label::; |
1161 | our @ISA = CFClient::UI::Label::; |
1058 | |
1162 | |
1059 | use SDL; |
1163 | use CFClient::OpenGL; |
1060 | use SDL::OpenGL; |
|
|
1061 | |
1164 | |
1062 | sub new { |
1165 | sub new { |
1063 | my $class = shift; |
1166 | my $class = shift; |
1064 | |
1167 | |
1065 | $class->SUPER::new ( |
1168 | $class->SUPER::new ( |
… | |
… | |
1075 | } |
1178 | } |
1076 | |
1179 | |
1077 | sub _set_text { |
1180 | sub _set_text { |
1078 | my ($self, $text) = @_; |
1181 | my ($self, $text) = @_; |
1079 | |
1182 | |
1080 | my $old_text = $self->{text}; |
1183 | delete $self->{cur_h}; |
|
|
1184 | |
|
|
1185 | return if $self->{text} eq $text; |
|
|
1186 | |
|
|
1187 | delete $self->{texture}; |
1081 | |
1188 | |
1082 | $self->{last_activity} = $::NOW; |
1189 | $self->{last_activity} = $::NOW; |
1083 | |
|
|
1084 | $self->{text} = $text; |
1190 | $self->{text} = $text; |
1085 | $self->{layout}->set_width ($self->{w}); |
|
|
1086 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
|
|
1087 | |
1191 | |
1088 | $text =~ s/./*/g if $self->{hidden}; |
1192 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1193 | $self->{layout}->set_text ("$text "); |
1089 | |
1194 | |
1090 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1195 | $self->emit (changed => $self->{text}); |
|
|
1196 | } |
1091 | |
1197 | |
1092 | $text = substr $text, 0, $self->{cursor}; |
1198 | sub get_text { |
1093 | utf8::encode $text; |
1199 | $_[0]{text} |
1094 | |
|
|
1095 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1096 | |
|
|
1097 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1098 | if $old_text ne $self->{text}; |
|
|
1099 | } |
1200 | } |
1100 | |
1201 | |
1101 | sub size_request { |
1202 | sub size_request { |
1102 | my ($self) = @_; |
1203 | my ($self) = @_; |
1103 | |
1204 | |
… | |
… | |
1105 | |
1206 | |
1106 | ($w + 1, $h) # add 1 for cursor |
1207 | ($w + 1, $h) # add 1 for cursor |
1107 | } |
1208 | } |
1108 | |
1209 | |
1109 | sub size_allocate { |
1210 | sub size_allocate { |
1110 | my ($self, $x, $y, $w, $h) = @_; |
1211 | my ($self, $w, $h) = @_; |
1111 | |
|
|
1112 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
1113 | |
1212 | |
1114 | $self->_set_text ($self->{text}); |
1213 | $self->_set_text ($self->{text}); |
1115 | } |
1214 | } |
1116 | |
1215 | |
1117 | sub set_text { |
1216 | sub set_text { |
… | |
… | |
1123 | } |
1222 | } |
1124 | |
1223 | |
1125 | sub key_down { |
1224 | sub key_down { |
1126 | my ($self, $ev) = @_; |
1225 | my ($self, $ev) = @_; |
1127 | |
1226 | |
1128 | my $mod = $ev->key_mod; |
1227 | my $mod = $ev->{mod}; |
1129 | my $sym = $ev->key_sym; |
1228 | my $sym = $ev->{sym}; |
1130 | |
|
|
1131 | my $uni = $ev->key_unicode; |
1229 | my $uni = $ev->{unicode}; |
1132 | |
1230 | |
1133 | my $text = $self->get_text; |
1231 | my $text = $self->get_text; |
1134 | |
1232 | |
1135 | if ($sym == SDLK_BACKSPACE) { |
1233 | if ($sym == 8) { |
1136 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1234 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1137 | } elsif ($sym == SDLK_DELETE) { |
1235 | } elsif ($sym == 127) { |
1138 | substr $text, $self->{cursor}, 1, ""; |
1236 | substr $text, $self->{cursor}, 1, ""; |
1139 | } elsif ($sym == SDLK_LEFT) { |
1237 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1140 | --$self->{cursor} if $self->{cursor}; |
1238 | --$self->{cursor} if $self->{cursor}; |
1141 | } elsif ($sym == SDLK_RIGHT) { |
1239 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1142 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1240 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1143 | } elsif ($sym == SDLK_HOME) { |
1241 | } elsif ($sym == CFClient::SDLK_HOME) { |
1144 | $self->{cursor} = 0; |
1242 | $self->{cursor} = 0; |
1145 | } elsif ($sym == SDLK_END) { |
1243 | } elsif ($sym == CFClient::SDLK_END) { |
1146 | $self->{cursor} = length $text; |
1244 | $self->{cursor} = length $text; |
1147 | } elsif ($sym == SDLK_ESCAPE) { |
1245 | } elsif ($sym == 27) { |
1148 | $self->emit ('escape'); |
1246 | $self->emit ('escape'); |
1149 | } elsif ($uni) { |
1247 | } elsif ($uni) { |
1150 | substr $text, $self->{cursor}++, 0, chr $uni; |
1248 | substr $text, $self->{cursor}++, 0, chr $uni; |
1151 | } |
1249 | } |
1152 | |
1250 | |
… | |
… | |
1207 | |
1305 | |
1208 | $self->SUPER::_draw; |
1306 | $self->SUPER::_draw; |
1209 | |
1307 | |
1210 | #TODO: force update every cursor change :( |
1308 | #TODO: force update every cursor change :( |
1211 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1309 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1310 | |
|
|
1311 | unless (exists $self->{cur_h}) { |
|
|
1312 | my $text = substr $self->{text}, 0, $self->{cursor}; |
|
|
1313 | utf8::encode $text; |
|
|
1314 | |
|
|
1315 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
|
|
1316 | } |
|
|
1317 | |
1212 | glColor @{$self->{fg}}; |
1318 | glColor @{$self->{fg}}; |
1213 | glBegin GL_LINES; |
1319 | glBegin GL_LINES; |
1214 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1215 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1320 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
|
|
1321 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1216 | glEnd; |
1322 | glEnd; |
1217 | } |
1323 | } |
1218 | } |
1324 | } |
1219 | |
1325 | |
1220 | package CFClient::UI::LineEntry; |
1326 | package CFClient::UI::Entry; |
1221 | |
1327 | |
1222 | our @ISA = CFClient::UI::Entry::; |
1328 | our @ISA = CFClient::UI::EntryBase::; |
1223 | |
1329 | |
1224 | use SDL; |
1330 | use CFClient::OpenGL; |
1225 | use SDL::OpenGL; |
|
|
1226 | |
1331 | |
1227 | sub key_down { |
1332 | sub key_down { |
1228 | my ($self, $ev) = @_; |
1333 | my ($self, $ev) = @_; |
1229 | |
1334 | |
1230 | my $sym = $ev->key_sym; |
1335 | my $sym = $ev->{sym}; |
1231 | |
1336 | |
1232 | if ($sym == SDLK_RETURN) { |
1337 | if ($sym == 13) { |
1233 | $self->emit (activate => $self->get_text); |
1338 | $self->emit (activate => $self->get_text); |
1234 | $self->update; |
1339 | $self->update; |
1235 | |
1340 | |
1236 | } else { |
1341 | } else { |
1237 | $self->SUPER::key_down ($ev); |
1342 | $self->SUPER::key_down ($ev); |
… | |
… | |
1243 | |
1348 | |
1244 | package CFClient::UI::Button; |
1349 | package CFClient::UI::Button; |
1245 | |
1350 | |
1246 | our @ISA = CFClient::UI::Label::; |
1351 | our @ISA = CFClient::UI::Label::; |
1247 | |
1352 | |
1248 | use SDL; |
1353 | use CFClient::OpenGL; |
1249 | use SDL::OpenGL; |
|
|
1250 | |
1354 | |
1251 | my @tex = |
1355 | my @tex = |
1252 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1356 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1253 | qw(b1_button_active.png); |
1357 | qw(b1_button_active.png); |
1254 | |
1358 | |
1255 | sub new { |
1359 | sub new { |
1256 | my $class = shift; |
1360 | my $class = shift; |
1257 | |
1361 | |
1258 | $class->SUPER::new ( |
1362 | $class->SUPER::new ( |
1259 | padding => 4, |
1363 | padding => 4, |
1260 | fg => [1, 1, 1], |
1364 | fg => [1, 1, 1], |
1261 | bg => [1, 1, 1, 0.2], |
1365 | bg => [1, 1, 1, 0.2], |
1262 | active_fg => [1, 1, 0], |
1366 | active_fg => [0, 0, 1], |
1263 | can_hover => 1, |
1367 | can_hover => 1, |
|
|
1368 | align => 0, |
|
|
1369 | valign => 0, |
1264 | @_ |
1370 | @_ |
1265 | ) |
1371 | ) |
1266 | } |
1372 | } |
1267 | |
1373 | |
1268 | sub button_up { |
1374 | sub button_up { |
… | |
… | |
1276 | |
1382 | |
1277 | sub _draw { |
1383 | sub _draw { |
1278 | my ($self) = @_; |
1384 | my ($self) = @_; |
1279 | |
1385 | |
1280 | local $self->{fg} = $self->{fg}; |
1386 | local $self->{fg} = $self->{fg}; |
1281 | my $tex = $tex[0]; |
|
|
1282 | |
|
|
1283 | glEnable GL_BLEND; |
|
|
1284 | glEnable GL_TEXTURE_2D; |
|
|
1285 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1286 | |
1387 | |
1287 | if ($GRAB == $self) { |
1388 | if ($GRAB == $self) { |
1288 | $self->{fg} = $self->{active_fg}; |
1389 | $self->{fg} = $self->{active_fg}; |
1289 | } |
1390 | } |
1290 | |
1391 | |
1291 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1392 | glEnable GL_BLEND; |
|
|
1393 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1394 | glEnable GL_TEXTURE_2D; |
1292 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1395 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1396 | glColor 0, 0, 0, 1; |
1293 | |
1397 | |
1294 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
1398 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1295 | |
1399 | |
1296 | glDisable GL_TEXTURE_2D; |
1400 | glDisable GL_TEXTURE_2D; |
1297 | glDisable GL_BLEND; |
1401 | glDisable GL_BLEND; |
1298 | |
1402 | |
1299 | $self->SUPER::_draw; |
1403 | $self->SUPER::_draw; |
… | |
… | |
1304 | package CFClient::UI::CheckBox; |
1408 | package CFClient::UI::CheckBox; |
1305 | |
1409 | |
1306 | our @ISA = CFClient::UI::DrawBG::; |
1410 | our @ISA = CFClient::UI::DrawBG::; |
1307 | |
1411 | |
1308 | my @tex = |
1412 | my @tex = |
1309 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1413 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1310 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1414 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1311 | |
1415 | |
1312 | use SDL; |
1416 | use CFClient::OpenGL; |
1313 | use SDL::OpenGL; |
|
|
1314 | |
1417 | |
1315 | sub new { |
1418 | sub new { |
1316 | my $class = shift; |
1419 | my $class = shift; |
1317 | |
1420 | |
1318 | $class->SUPER::new ( |
1421 | $class->SUPER::new ( |
… | |
… | |
1329 | my ($self) = @_; |
1432 | my ($self) = @_; |
1330 | |
1433 | |
1331 | ($self->{padding} * 2 + 6) x 2 |
1434 | ($self->{padding} * 2 + 6) x 2 |
1332 | } |
1435 | } |
1333 | |
1436 | |
1334 | sub size_allocate { |
|
|
1335 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1336 | |
|
|
1337 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1338 | } |
|
|
1339 | |
|
|
1340 | sub button_down { |
1437 | sub button_down { |
1341 | my ($self, $ev, $x, $y) = @_; |
1438 | my ($self, $ev, $x, $y) = @_; |
1342 | |
1439 | |
1343 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1440 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1344 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1441 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1370 | glDisable GL_BLEND; |
1467 | glDisable GL_BLEND; |
1371 | } |
1468 | } |
1372 | |
1469 | |
1373 | ############################################################################# |
1470 | ############################################################################# |
1374 | |
1471 | |
|
|
1472 | package CFClient::UI::Image; |
|
|
1473 | |
|
|
1474 | our @ISA = CFClient::UI::Base::; |
|
|
1475 | |
|
|
1476 | use CFClient::OpenGL; |
|
|
1477 | use Carp qw/confess/; |
|
|
1478 | |
|
|
1479 | our %loaded_images; |
|
|
1480 | |
|
|
1481 | sub new { |
|
|
1482 | my $class = shift; |
|
|
1483 | |
|
|
1484 | my $self = $class->SUPER::new (@_); |
|
|
1485 | |
|
|
1486 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1487 | |
|
|
1488 | $loaded_images{$self->{image}} ||= |
|
|
1489 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1490 | |
|
|
1491 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1492 | |
|
|
1493 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1494 | |
|
|
1495 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
1496 | |
|
|
1497 | $self |
|
|
1498 | } |
|
|
1499 | |
|
|
1500 | sub size_request { |
|
|
1501 | my ($self) = @_; |
|
|
1502 | |
|
|
1503 | ($self->{tex}->{w}, $self->{tex}->{h}) |
|
|
1504 | } |
|
|
1505 | |
|
|
1506 | sub _draw { |
|
|
1507 | my ($self) = @_; |
|
|
1508 | |
|
|
1509 | my $tex = $self->{tex}; |
|
|
1510 | |
|
|
1511 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1512 | |
|
|
1513 | if ($self->{rot90}) { |
|
|
1514 | glRotate 90, 0, 0, 1; |
|
|
1515 | glTranslate 0, -$self->{w}, 0; |
|
|
1516 | |
|
|
1517 | ($w, $h) = ($h, $w); |
|
|
1518 | } |
|
|
1519 | |
|
|
1520 | glEnable GL_BLEND; |
|
|
1521 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1522 | glEnable GL_TEXTURE_2D; |
|
|
1523 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1524 | |
|
|
1525 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1526 | |
|
|
1527 | glDisable GL_BLEND; |
|
|
1528 | glDisable GL_TEXTURE_2D; |
|
|
1529 | } |
|
|
1530 | |
|
|
1531 | ############################################################################# |
|
|
1532 | |
|
|
1533 | package CFClient::UI::VGauge; |
|
|
1534 | |
|
|
1535 | our @ISA = CFClient::UI::Base::; |
|
|
1536 | |
|
|
1537 | use CFClient::OpenGL; |
|
|
1538 | |
|
|
1539 | my %tex = ( |
|
|
1540 | food => [ |
|
|
1541 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1542 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1543 | ], |
|
|
1544 | grace => [ |
|
|
1545 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1546 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1547 | ], |
|
|
1548 | hp => [ |
|
|
1549 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1550 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1551 | ], |
|
|
1552 | mana => [ |
|
|
1553 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1554 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1555 | ], |
|
|
1556 | ); |
|
|
1557 | |
|
|
1558 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1559 | sub new { |
|
|
1560 | my $class = shift; |
|
|
1561 | |
|
|
1562 | my $self = $class->SUPER::new ( |
|
|
1563 | type => 'food', |
|
|
1564 | @_ |
|
|
1565 | ); |
|
|
1566 | |
|
|
1567 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1568 | |
|
|
1569 | $self |
|
|
1570 | } |
|
|
1571 | |
|
|
1572 | sub size_request { |
|
|
1573 | my ($self) = @_; |
|
|
1574 | |
|
|
1575 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1576 | #@$tex{qw(w h)} |
|
|
1577 | (0, 0) |
|
|
1578 | } |
|
|
1579 | |
|
|
1580 | sub set_max { |
|
|
1581 | my ($self, $max) = @_; |
|
|
1582 | |
|
|
1583 | $self->{max_val} = $max; |
|
|
1584 | } |
|
|
1585 | |
|
|
1586 | sub set_value { |
|
|
1587 | my ($self, $val, $max) = @_; |
|
|
1588 | |
|
|
1589 | $self->set_max ($max) |
|
|
1590 | if defined $max; |
|
|
1591 | |
|
|
1592 | $max = $self->{max_val}; |
|
|
1593 | $self->{val} = $val; |
|
|
1594 | |
|
|
1595 | $self->update; |
|
|
1596 | } |
|
|
1597 | |
|
|
1598 | sub _draw { |
|
|
1599 | my ($self) = @_; |
|
|
1600 | |
|
|
1601 | my $tex = $tex{$self->{type}}; |
|
|
1602 | |
|
|
1603 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1604 | |
|
|
1605 | if ($self->{vertical}) { |
|
|
1606 | glRotate 90, 0, 0, 1; |
|
|
1607 | glTranslate 0, -$self->{w}, 0; |
|
|
1608 | |
|
|
1609 | ($w, $h) = ($h, $w); |
|
|
1610 | } |
|
|
1611 | |
|
|
1612 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1613 | $ycut = 1 if $self->{val} > $self->{max_val}; |
|
|
1614 | |
|
|
1615 | my $t1 = $tex->[0]; |
|
|
1616 | my $t2 = $tex->[1]; |
|
|
1617 | |
|
|
1618 | glEnable GL_BLEND; |
|
|
1619 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1620 | glEnable GL_TEXTURE_2D; |
|
|
1621 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1622 | |
|
|
1623 | my $h1 = $self->{h} - $ycut * $self->{h}; |
|
|
1624 | my $h2 = $ycut * $self->{h}; |
|
|
1625 | |
|
|
1626 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1627 | glBegin GL_QUADS; |
|
|
1628 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1629 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; |
|
|
1630 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; |
|
|
1631 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
|
|
1632 | glEnd; |
|
|
1633 | |
|
|
1634 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1635 | glBegin GL_QUADS; |
|
|
1636 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; |
|
|
1637 | glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; |
|
|
1638 | glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; |
|
|
1639 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; |
|
|
1640 | glEnd; |
|
|
1641 | |
|
|
1642 | glDisable GL_BLEND; |
|
|
1643 | glDisable GL_TEXTURE_2D; |
|
|
1644 | } |
|
|
1645 | |
|
|
1646 | ############################################################################# |
|
|
1647 | |
|
|
1648 | package CFClient::UI::Gauge; |
|
|
1649 | |
|
|
1650 | our @ISA = CFClient::UI::VBox::; |
|
|
1651 | |
|
|
1652 | sub new { |
|
|
1653 | my ($class, %arg) = shift; |
|
|
1654 | |
|
|
1655 | my $self = $class->SUPER::new ( |
|
|
1656 | @_, |
|
|
1657 | ); |
|
|
1658 | |
|
|
1659 | $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
|
|
1660 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); |
|
|
1661 | $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
|
|
1662 | |
|
|
1663 | $self |
|
|
1664 | } |
|
|
1665 | |
|
|
1666 | sub set_fontsize { |
|
|
1667 | my ($self, $fsize) = @_; |
|
|
1668 | |
|
|
1669 | $self->{value}->set_fontsize ($fsize); |
|
|
1670 | $self->{max} ->set_fontsize ($fsize); |
|
|
1671 | } |
|
|
1672 | |
|
|
1673 | sub set_value { |
|
|
1674 | my ($self, $val, $max) = @_; |
|
|
1675 | |
|
|
1676 | $self->set_max ($max) |
|
|
1677 | if defined $max; |
|
|
1678 | |
|
|
1679 | $self->{gauge}->set_value ($val, $max); |
|
|
1680 | $self->{value}->set_text ($val); |
|
|
1681 | } |
|
|
1682 | |
|
|
1683 | sub set_max { |
|
|
1684 | my ($self, $max) = @_; |
|
|
1685 | |
|
|
1686 | $self->{gauge}->set_max ($max); |
|
|
1687 | $self->{max}->set_text ($max); |
|
|
1688 | } |
|
|
1689 | |
|
|
1690 | ############################################################################# |
|
|
1691 | |
1375 | package CFClient::UI::Slider; |
1692 | package CFClient::UI::Slider; |
1376 | |
1693 | |
1377 | use strict; |
1694 | use strict; |
1378 | |
1695 | |
1379 | use SDL::OpenGL; |
1696 | use CFClient::OpenGL; |
1380 | |
1697 | |
1381 | our @ISA = CFClient::UI::DrawBG::; |
1698 | our @ISA = CFClient::UI::DrawBG::; |
1382 | |
1699 | |
1383 | my @tex = |
1700 | my @tex = |
1384 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1701 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1394 | # TODO: calculations are off |
1711 | # TODO: calculations are off |
1395 | my $self = $class->SUPER::new ( |
1712 | my $self = $class->SUPER::new ( |
1396 | fg => [1, 1, 1], |
1713 | fg => [1, 1, 1], |
1397 | active_fg => [0, 0, 0], |
1714 | active_fg => [0, 0, 0], |
1398 | range => [0, 0, 100, 10], |
1715 | range => [0, 0, 100, 10], |
1399 | req_w => 40, |
1716 | req_w => 20, |
1400 | req_h => 13, |
1717 | req_h => 20, |
1401 | vertical => 0, |
1718 | vertical => 0, |
1402 | can_hover => 1, |
1719 | can_hover => 1, |
1403 | inner_pad => 5, |
1720 | inner_pad => 5, |
1404 | @_ |
1721 | @_ |
1405 | ); |
1722 | ); |
… | |
… | |
1509 | |
1826 | |
1510 | package CFClient::UI::TextView; |
1827 | package CFClient::UI::TextView; |
1511 | |
1828 | |
1512 | our @ISA = CFClient::UI::HBox::; |
1829 | our @ISA = CFClient::UI::HBox::; |
1513 | |
1830 | |
1514 | use SDL::OpenGL; |
1831 | use CFClient::OpenGL; |
1515 | |
1832 | |
1516 | sub new { |
1833 | sub new { |
1517 | my $class = shift; |
1834 | my $class = shift; |
1518 | |
1835 | |
1519 | my $self = $class->SUPER::new ( |
1836 | my $self = $class->SUPER::new ( |
1520 | req_w => $::WIDTH / 6, |
|
|
1521 | req_h => $::HEIGHT / 6, |
|
|
1522 | fontsize => $::FONTSIZE, |
1837 | fontsize => 1, |
1523 | @_, |
1838 | @_, |
1524 | |
1839 | |
1525 | layout => (new CFClient::Layout), |
1840 | layout => (new CFClient::Layout), |
1526 | par => [], |
1841 | par => [], |
1527 | height => 0, |
1842 | height => 0, |
… | |
… | |
1548 | sub text_height { |
1863 | sub text_height { |
1549 | my ($self, $text) = @_; |
1864 | my ($self, $text) = @_; |
1550 | |
1865 | |
1551 | my $layout = $self->{layout}; |
1866 | my $layout = $self->{layout}; |
1552 | |
1867 | |
1553 | $layout->set_height ($self->{fontsize}); |
1868 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1554 | $layout->set_width ($self->{w}); |
1869 | $layout->set_width ($self->{w}); |
1555 | $layout->set_text ($text); |
1870 | $layout->set_text ($text); |
1556 | |
1871 | |
1557 | ($layout->size)[1] |
1872 | ($layout->size)[1] |
1558 | } |
1873 | } |
… | |
… | |
1562 | |
1877 | |
1563 | $self->{need_reflow}++; |
1878 | $self->{need_reflow}++; |
1564 | $self->update; |
1879 | $self->update; |
1565 | } |
1880 | } |
1566 | |
1881 | |
1567 | sub size_request { |
|
|
1568 | my ($self) = @_; |
|
|
1569 | |
|
|
1570 | ($self->{req_w}, $self->{req_h}) |
|
|
1571 | } |
|
|
1572 | |
|
|
1573 | sub size_allocate { |
1882 | sub size_allocate { |
1574 | my ($self, $x, $y, $w, $h) = @_; |
1883 | my ($self, $w, $h) = @_; |
1575 | |
1884 | |
1576 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1885 | $self->SUPER::size_allocate ($w, $h); |
1577 | |
1886 | |
1578 | $self->{layout}->set_height ($self->{fontsize}); |
1887 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1579 | $self->{layout}->set_width ($self->{w}); |
1888 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1580 | |
1889 | |
1581 | $self->reflow; |
1890 | $self->reflow; |
1582 | } |
1891 | } |
1583 | |
1892 | |
1584 | sub add_paragraph { |
1893 | sub add_paragraph { |
… | |
… | |
1671 | |
1980 | |
1672 | } |
1981 | } |
1673 | |
1982 | |
1674 | ############################################################################# |
1983 | ############################################################################# |
1675 | |
1984 | |
1676 | package CFClient::UI::MapWidget; |
|
|
1677 | |
|
|
1678 | use strict; |
|
|
1679 | |
|
|
1680 | use List::Util qw(min max); |
|
|
1681 | |
|
|
1682 | use SDL; |
|
|
1683 | use SDL::OpenGL; |
|
|
1684 | |
|
|
1685 | our @ISA = CFClient::UI::Base::; |
|
|
1686 | |
|
|
1687 | sub new { |
|
|
1688 | my $class = shift; |
|
|
1689 | |
|
|
1690 | $class->SUPER::new ( |
|
|
1691 | z => -1, |
|
|
1692 | can_focus => 1, |
|
|
1693 | list => (glGenLists 1), |
|
|
1694 | @_ |
|
|
1695 | ) |
|
|
1696 | } |
|
|
1697 | |
|
|
1698 | sub key_down { |
|
|
1699 | print "MAPKEYDOWN\n"; |
|
|
1700 | } |
|
|
1701 | |
|
|
1702 | sub key_up { |
|
|
1703 | } |
|
|
1704 | |
|
|
1705 | sub button_down { |
|
|
1706 | my ($self, $ev, $x, $y) = @_; |
|
|
1707 | |
|
|
1708 | $self->focus_in; |
|
|
1709 | |
|
|
1710 | if ($ev->button == 2) { |
|
|
1711 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1712 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1713 | |
|
|
1714 | $self->{motion} = sub { |
|
|
1715 | my ($ev, $x, $y) = @_; |
|
|
1716 | |
|
|
1717 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1718 | |
|
|
1719 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1720 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1721 | |
|
|
1722 | $self->update; |
|
|
1723 | }; |
|
|
1724 | } |
|
|
1725 | } |
|
|
1726 | |
|
|
1727 | sub button_up { |
|
|
1728 | my ($self, $ev, $x, $y) = @_; |
|
|
1729 | |
|
|
1730 | delete $self->{motion}; |
|
|
1731 | } |
|
|
1732 | |
|
|
1733 | sub mouse_motion { |
|
|
1734 | my ($self, $ev, $x, $y) = @_; |
|
|
1735 | |
|
|
1736 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1737 | } |
|
|
1738 | |
|
|
1739 | sub size_request { |
|
|
1740 | ( |
|
|
1741 | 1 + 32 * int $::WIDTH / 32, |
|
|
1742 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1743 | ) |
|
|
1744 | } |
|
|
1745 | |
|
|
1746 | sub update { |
|
|
1747 | my ($self) = @_; |
|
|
1748 | |
|
|
1749 | $self->{need_update} = 1; |
|
|
1750 | $self->SUPER::update; |
|
|
1751 | } |
|
|
1752 | |
|
|
1753 | sub draw { |
|
|
1754 | my ($self) = @_; |
|
|
1755 | |
|
|
1756 | if (delete $self->{need_update}) { |
|
|
1757 | glNewList $self->{list}, GL_COMPILE; |
|
|
1758 | |
|
|
1759 | if ($::MAP) { |
|
|
1760 | my $sw = int $::WIDTH / 32; |
|
|
1761 | my $sh = int $::HEIGHT / 32; |
|
|
1762 | |
|
|
1763 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1764 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1765 | |
|
|
1766 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1767 | |
|
|
1768 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1769 | |
|
|
1770 | if ($::CFG->{fow_enable}) { |
|
|
1771 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1772 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1773 | glConvolutionFilter2D |
|
|
1774 | GL_CONVOLUTION_2D, |
|
|
1775 | GL_ALPHA, |
|
|
1776 | 3, 3, |
|
|
1777 | GL_ALPHA, GL_FLOAT, |
|
|
1778 | pack "f*", |
|
|
1779 | 0.1, 0.1, 0.1, |
|
|
1780 | 0.1, 0.2, 0.1, |
|
|
1781 | 0.1, 0.1, 0.1, |
|
|
1782 | ; |
|
|
1783 | glEnable GL_CONVOLUTION_2D; |
|
|
1784 | } |
|
|
1785 | |
|
|
1786 | $self->{fow_texture} = new CFClient::Texture |
|
|
1787 | w => $w, |
|
|
1788 | h => $h, |
|
|
1789 | data => $data, |
|
|
1790 | internalformat => GL_ALPHA, |
|
|
1791 | format => GL_ALPHA; |
|
|
1792 | |
|
|
1793 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1794 | |
|
|
1795 | glEnable GL_BLEND; |
|
|
1796 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1797 | glEnable GL_TEXTURE_2D; |
|
|
1798 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1799 | |
|
|
1800 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1801 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1802 | |
|
|
1803 | glDisable GL_TEXTURE_2D; |
|
|
1804 | glDisable GL_BLEND; |
|
|
1805 | } |
|
|
1806 | |
|
|
1807 | # HACK BEGIN |
|
|
1808 | { |
|
|
1809 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1810 | my ($w, $h) = (250, 250); |
|
|
1811 | |
|
|
1812 | glEnable GL_BLEND; |
|
|
1813 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1814 | glEnable GL_TEXTURE_2D; |
|
|
1815 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1816 | |
|
|
1817 | $self->{mapmap_texture} = |
|
|
1818 | new CFClient::Texture |
|
|
1819 | w => $w, |
|
|
1820 | h => $h, |
|
|
1821 | data => $::MAP->mapmap ($w, $h), |
|
|
1822 | type => GL_UNSIGNED_INT_8_8_8_8_REV; |
|
|
1823 | |
|
|
1824 | $self->{mapmap_texture}->draw_quad (100, 100); |
|
|
1825 | |
|
|
1826 | glDisable GL_TEXTURE_2D; |
|
|
1827 | glDisable GL_BLEND; |
|
|
1828 | } |
|
|
1829 | # HACK END |
|
|
1830 | } |
|
|
1831 | |
|
|
1832 | glEndList; |
|
|
1833 | } |
|
|
1834 | |
|
|
1835 | glPushMatrix; |
|
|
1836 | glCallList $self->{list}; |
|
|
1837 | glPopMatrix; |
|
|
1838 | |
|
|
1839 | if ($FOCUS != $self) { |
|
|
1840 | glColor 64/255, 64/255, 64/255; |
|
|
1841 | glLogicOp GL_AND; |
|
|
1842 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1843 | glBegin GL_QUADS; |
|
|
1844 | glVertex 0, 0; |
|
|
1845 | glVertex 0, $::HEIGHT; |
|
|
1846 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1847 | glVertex $::WIDTH, 0; |
|
|
1848 | glEnd; |
|
|
1849 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1850 | } |
|
|
1851 | } |
|
|
1852 | |
|
|
1853 | my %DIR = ( |
|
|
1854 | SDLK_KP8, [1, "north"], |
|
|
1855 | SDLK_KP9, [2, "northeast"], |
|
|
1856 | SDLK_KP6, [3, "east"], |
|
|
1857 | SDLK_KP3, [4, "southeast"], |
|
|
1858 | SDLK_KP2, [5, "south"], |
|
|
1859 | SDLK_KP1, [6, "southwest"], |
|
|
1860 | SDLK_KP4, [7, "west"], |
|
|
1861 | SDLK_KP7, [8, "northwest"], |
|
|
1862 | |
|
|
1863 | SDLK_UP, [1, "north"], |
|
|
1864 | SDLK_RIGHT, [3, "east"], |
|
|
1865 | SDLK_DOWN, [5, "south"], |
|
|
1866 | SDLK_LEFT, [7, "west"], |
|
|
1867 | ); |
|
|
1868 | |
|
|
1869 | sub key_down { |
|
|
1870 | my ($self, $ev) = @_; |
|
|
1871 | |
|
|
1872 | my $mod = $ev->key_mod; |
|
|
1873 | my $sym = $ev->key_sym; |
|
|
1874 | |
|
|
1875 | if ($sym == SDLK_KP5) { |
|
|
1876 | $::CONN->user_send ("command stay fire"); |
|
|
1877 | } elsif ($sym == SDLK_a) { |
|
|
1878 | $::CONN->user_send ("command apply"); |
|
|
1879 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1880 | $self->emit ('activate_console'); |
|
|
1881 | } elsif ($sym == SDLK_SLASH) { |
|
|
1882 | $self->emit ('activate_console' => '/'); |
|
|
1883 | } elsif (exists $DIR{$sym}) { |
|
|
1884 | if ($mod & KMOD_SHIFT) { |
|
|
1885 | $self->{shft}++; |
|
|
1886 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
|
|
1887 | } elsif ($mod & KMOD_CTRL) { |
|
|
1888 | $self->{ctrl}++; |
|
|
1889 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
|
|
1890 | } else { |
|
|
1891 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
|
|
1892 | } |
|
|
1893 | } |
|
|
1894 | } |
|
|
1895 | |
|
|
1896 | sub key_up { |
|
|
1897 | my ($self, $ev) = @_; |
|
|
1898 | |
|
|
1899 | my $mod = $ev->key_mod; |
|
|
1900 | my $sym = $ev->key_sym; |
|
|
1901 | |
|
|
1902 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1903 | $::CONN->user_send ("command fire_stop"); |
|
|
1904 | } |
|
|
1905 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1906 | $::CONN->user_send ("command run_stop"); |
|
|
1907 | } |
|
|
1908 | } |
|
|
1909 | |
|
|
1910 | ############################################################################# |
|
|
1911 | |
|
|
1912 | package CFClient::UI::Animator; |
1985 | package CFClient::UI::Animator; |
1913 | |
1986 | |
1914 | use SDL::OpenGL; |
1987 | use CFClient::OpenGL; |
1915 | |
1988 | |
1916 | our @ISA = CFClient::UI::Bin::; |
1989 | our @ISA = CFClient::UI::Bin::; |
1917 | |
1990 | |
1918 | sub moveto { |
1991 | sub moveto { |
1919 | my ($self, $x, $y) = @_; |
1992 | my ($self, $x, $y) = @_; |
… | |
… | |
1992 | |
2065 | |
1993 | package CFClient::UI::Root; |
2066 | package CFClient::UI::Root; |
1994 | |
2067 | |
1995 | our @ISA = CFClient::UI::Container::; |
2068 | our @ISA = CFClient::UI::Container::; |
1996 | |
2069 | |
1997 | use SDL::OpenGL; |
2070 | use CFClient::OpenGL; |
|
|
2071 | |
|
|
2072 | sub check_size { |
|
|
2073 | my ($self) = @_; |
|
|
2074 | |
|
|
2075 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
|
|
2076 | } |
1998 | |
2077 | |
1999 | sub size_request { |
2078 | sub size_request { |
2000 | ($::WIDTH, $::HEIGHT) |
2079 | ($::WIDTH, $::HEIGHT) |
2001 | } |
2080 | } |
2002 | |
2081 | |
2003 | sub size_allocate { |
2082 | sub configure { |
2004 | my ($self, $x, $y, $w, $h) = @_; |
2083 | my ($self, $x, $y, $w, $h) = @_; |
2005 | |
2084 | |
2006 | $self->_size_allocate ($x, $y, $w, $h); |
2085 | $self->SUPER::configure ($x, $y, $w, $h); |
2007 | |
2086 | |
2008 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
2087 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
2009 | for @{$self->{children}}; |
2088 | for @{$self->{children}}; |
2010 | } |
2089 | } |
2011 | |
2090 | |
2012 | sub _topleft { |
2091 | sub _topleft { |
2013 | my ($self, $x, $y) = @_; |
2092 | my ($self, $x, $y) = @_; |
… | |
… | |
2016 | } |
2095 | } |
2017 | |
2096 | |
2018 | sub update { |
2097 | sub update { |
2019 | my ($self) = @_; |
2098 | my ($self) = @_; |
2020 | |
2099 | |
2021 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2100 | $self->check_size; |
2022 | ::refresh (); |
2101 | ::refresh (); |
2023 | } |
2102 | } |
2024 | |
2103 | |
2025 | sub add { |
2104 | sub add { |
2026 | my ($self, $widget) = @_; |
2105 | my ($self, $child) = @_; |
2027 | |
2106 | |
|
|
2107 | # integerize window positions |
|
|
2108 | $child->{x} = int $child->{x}; |
|
|
2109 | $child->{y} = int $child->{y}; |
|
|
2110 | |
2028 | $self->SUPER::add ($widget); |
2111 | $self->SUPER::add ($child); |
2029 | |
|
|
2030 | $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request); |
|
|
2031 | } |
2112 | } |
2032 | |
2113 | |
2033 | sub on_refresh { |
2114 | sub on_refresh { |
2034 | my ($self, $id, $cb) = @_; |
2115 | my ($self, $id, $cb) = @_; |
2035 | |
2116 | |