… | |
… | |
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; |
… | |
… | |
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 | |
|
|
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 += ($w - $w2) * 0.5; |
|
|
158 | $y += ($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 => $::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, |
|
|
1041 | valign => -1, |
955 | padding => 2, |
1042 | padding => 2, |
956 | layout => new CFClient::Layout, |
1043 | layout => new CFClient::Layout, |
957 | %arg |
1044 | %arg |
958 | ); |
1045 | ); |
959 | |
1046 | |
960 | $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}; |
961 | |
1055 | |
962 | $self |
1056 | $self |
963 | } |
1057 | } |
964 | |
1058 | |
965 | sub escape_text { |
1059 | sub escape_text { |
… | |
… | |
973 | } |
1067 | } |
974 | |
1068 | |
975 | sub set_text { |
1069 | sub set_text { |
976 | my ($self, $text) = @_; |
1070 | my ($self, $text) = @_; |
977 | |
1071 | |
978 | $self->{text} = $text; |
|
|
979 | $self->{layout}->set_markup ($text); |
1072 | $self->{layout}->set_text ($text); |
980 | |
1073 | |
981 | delete $self->{texture}; |
1074 | delete $self->{texture}; |
982 | # $self->{w} = $self->{h} = -1; |
|
|
983 | $self->update; |
1075 | $self->update; |
984 | } |
1076 | } |
985 | |
1077 | |
986 | sub get_text { |
1078 | sub set_markup { |
987 | my ($self, $text) = @_; |
1079 | my ($self, $markup) = @_; |
988 | |
1080 | |
989 | $self->{text} |
1081 | $self->{layout}->set_markup ($markup); |
|
|
1082 | |
|
|
1083 | delete $self->{texture}; |
|
|
1084 | $self->update; |
990 | } |
1085 | } |
991 | |
1086 | |
992 | sub size_request { |
1087 | sub size_request { |
993 | my ($self) = @_; |
1088 | my ($self) = @_; |
994 | |
1089 | |
995 | $self->{layout}->set_width; |
1090 | $self->{layout}->set_width; |
996 | $self->{layout}->set_height ($self->{fontsize}); |
1091 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1092 | |
997 | 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 | } |
998 | |
1103 | |
999 | ( |
1104 | ( |
1000 | $w + $self->{padding} * 2, |
1105 | $w + $self->{padding} * 2, |
1001 | $h + $self->{padding} * 2, |
1106 | $h + $self->{padding} * 2, |
1002 | ) |
1107 | ) |
1003 | } |
1108 | } |
1004 | |
1109 | |
1005 | sub size_allocate { |
1110 | sub size_allocate { |
1006 | my ($self, $x, $y, $w, $h) = @_; |
1111 | my ($self, $w, $h) = @_; |
1007 | |
|
|
1008 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
1009 | |
1112 | |
1010 | delete $self->{texture}; |
1113 | delete $self->{texture}; |
1011 | } |
|
|
1012 | |
|
|
1013 | sub update { |
|
|
1014 | my ($self) = @_; |
|
|
1015 | |
|
|
1016 | delete $self->{texture}; |
|
|
1017 | $self->SUPER::update; |
|
|
1018 | } |
1114 | } |
1019 | |
1115 | |
1020 | sub _draw { |
1116 | sub _draw { |
1021 | my ($self) = @_; |
1117 | my ($self) = @_; |
1022 | |
1118 | |
1023 | my $tex = $self->{texture} ||= do { |
1119 | my $tex = $self->{texture} ||= do { |
1024 | $self->{layout}->set_width ($self->{w}); |
1120 | $self->{layout}->set_width ($self->{w}); |
1025 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
1121 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1026 | new_from_layout CFClient::Texture $self->{layout} |
1122 | new_from_layout CFClient::Texture $self->{layout} |
1027 | }; |
1123 | }; |
1028 | |
1124 | |
1029 | glEnable GL_BLEND; |
1125 | glEnable GL_BLEND; |
1030 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1126 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1031 | glEnable GL_TEXTURE_2D; |
1127 | glEnable GL_TEXTURE_2D; |
1032 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1128 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1033 | |
1129 | |
1034 | glColor @{$self->{fg}}; |
1130 | glColor @{$self->{fg}}; |
1035 | |
1131 | |
1036 | my $x = |
1132 | $self->{ox} = int ( |
1037 | $self->{align} < 0 ? $self->{padding} |
1133 | $self->{align} < 0 ? $self->{padding} |
1038 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1134 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1039 | : ($self->{w} - $tex->{w}) * 0.5; |
1135 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1136 | ); |
1040 | |
1137 | |
1041 | $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); |
1138 | $self->{oy} = int ( |
|
|
1139 | $self->{valign} < 0 ? $self->{padding} |
|
|
1140 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1141 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1142 | ); |
|
|
1143 | |
|
|
1144 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
1042 | |
1145 | |
1043 | glDisable GL_TEXTURE_2D; |
1146 | glDisable GL_TEXTURE_2D; |
1044 | glDisable GL_BLEND; |
1147 | glDisable GL_BLEND; |
1045 | } |
1148 | } |
1046 | |
1149 | |
1047 | ############################################################################# |
1150 | ############################################################################# |
1048 | |
1151 | |
1049 | package CFClient::UI::Entry; |
1152 | package CFClient::UI::EntryBase; |
1050 | |
1153 | |
1051 | our @ISA = CFClient::UI::Label::; |
1154 | our @ISA = CFClient::UI::Label::; |
1052 | |
1155 | |
1053 | use SDL; |
1156 | use CFClient::OpenGL; |
1054 | use SDL::OpenGL; |
|
|
1055 | |
1157 | |
1056 | sub new { |
1158 | sub new { |
1057 | my $class = shift; |
1159 | my $class = shift; |
1058 | |
1160 | |
1059 | $class->SUPER::new ( |
1161 | $class->SUPER::new ( |
… | |
… | |
1061 | bg => [0, 0, 0, 0.2], |
1163 | bg => [0, 0, 0, 0.2], |
1062 | active_bg => [1, 1, 1, 0.5], |
1164 | active_bg => [1, 1, 1, 0.5], |
1063 | active_fg => [0, 0, 0], |
1165 | active_fg => [0, 0, 0], |
1064 | can_hover => 1, |
1166 | can_hover => 1, |
1065 | can_focus => 1, |
1167 | can_focus => 1, |
|
|
1168 | valign => 0, |
1066 | @_ |
1169 | @_ |
1067 | ) |
1170 | ) |
1068 | } |
1171 | } |
1069 | |
1172 | |
1070 | sub _set_text { |
1173 | sub _set_text { |
1071 | my ($self, $text) = @_; |
1174 | my ($self, $text) = @_; |
1072 | |
1175 | |
1073 | my $old_text = $self->{text}; |
1176 | delete $self->{cur_h}; |
|
|
1177 | |
|
|
1178 | return if $self->{text} eq $text; |
|
|
1179 | |
|
|
1180 | delete $self->{texture}; |
1074 | |
1181 | |
1075 | $self->{last_activity} = $::NOW; |
1182 | $self->{last_activity} = $::NOW; |
1076 | |
|
|
1077 | $self->{text} = $text; |
1183 | $self->{text} = $text; |
1078 | $self->{layout}->set_width ($self->{w}); |
|
|
1079 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
|
|
1080 | |
1184 | |
1081 | $text =~ s/./*/g if $self->{hidden}; |
1185 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1186 | $self->{layout}->set_text ("$text "); |
1082 | |
1187 | |
1083 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1188 | $self->emit (changed => $self->{text}); |
|
|
1189 | } |
1084 | |
1190 | |
1085 | $text = substr $text, 0, $self->{cursor}; |
1191 | sub get_text { |
1086 | utf8::encode $text; |
1192 | $_[0]{text} |
1087 | |
|
|
1088 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1089 | |
|
|
1090 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1091 | if $old_text ne $self->{text}; |
|
|
1092 | } |
1193 | } |
1093 | |
1194 | |
1094 | sub size_request { |
1195 | sub size_request { |
1095 | my ($self) = @_; |
1196 | my ($self) = @_; |
1096 | |
1197 | |
… | |
… | |
1098 | |
1199 | |
1099 | ($w + 1, $h) # add 1 for cursor |
1200 | ($w + 1, $h) # add 1 for cursor |
1100 | } |
1201 | } |
1101 | |
1202 | |
1102 | sub size_allocate { |
1203 | sub size_allocate { |
1103 | my ($self, $x, $y, $w, $h) = @_; |
1204 | my ($self, $w, $h) = @_; |
1104 | |
|
|
1105 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
1106 | |
1205 | |
1107 | $self->_set_text ($self->{text}); |
1206 | $self->_set_text ($self->{text}); |
1108 | } |
1207 | } |
1109 | |
1208 | |
1110 | sub set_text { |
1209 | sub set_text { |
… | |
… | |
1116 | } |
1215 | } |
1117 | |
1216 | |
1118 | sub key_down { |
1217 | sub key_down { |
1119 | my ($self, $ev) = @_; |
1218 | my ($self, $ev) = @_; |
1120 | |
1219 | |
1121 | my $mod = $ev->key_mod; |
1220 | my $mod = $ev->{mod}; |
1122 | my $sym = $ev->key_sym; |
1221 | my $sym = $ev->{sym}; |
1123 | |
|
|
1124 | my $uni = $ev->key_unicode; |
1222 | my $uni = $ev->{unicode}; |
1125 | |
1223 | |
1126 | my $text = $self->get_text; |
1224 | my $text = $self->get_text; |
1127 | |
1225 | |
1128 | if ($sym == SDLK_BACKSPACE) { |
1226 | if ($sym == 8) { |
1129 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1227 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1130 | } elsif ($sym == SDLK_DELETE) { |
1228 | } elsif ($sym == 127) { |
1131 | substr $text, $self->{cursor}, 1, ""; |
1229 | substr $text, $self->{cursor}, 1, ""; |
1132 | } elsif ($sym == SDLK_LEFT) { |
1230 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1133 | --$self->{cursor} if $self->{cursor}; |
1231 | --$self->{cursor} if $self->{cursor}; |
1134 | } elsif ($sym == SDLK_RIGHT) { |
1232 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1135 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1233 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1136 | } elsif ($sym == SDLK_HOME) { |
1234 | } elsif ($sym == CFClient::SDLK_HOME) { |
1137 | $self->{cursor} = 0; |
1235 | $self->{cursor} = 0; |
1138 | } elsif ($sym == SDLK_END) { |
1236 | } elsif ($sym == CFClient::SDLK_END) { |
1139 | $self->{cursor} = length $text; |
1237 | $self->{cursor} = length $text; |
1140 | } elsif ($sym == SDLK_ESCAPE) { |
1238 | } elsif ($sym == 27) { |
1141 | $self->emit ('escape'); |
1239 | $self->emit ('escape'); |
1142 | } elsif ($uni) { |
1240 | } elsif ($uni) { |
1143 | substr $text, $self->{cursor}++, 0, chr $uni; |
1241 | substr $text, $self->{cursor}++, 0, chr $uni; |
1144 | } |
1242 | } |
1145 | |
1243 | |
… | |
… | |
1200 | |
1298 | |
1201 | $self->SUPER::_draw; |
1299 | $self->SUPER::_draw; |
1202 | |
1300 | |
1203 | #TODO: force update every cursor change :( |
1301 | #TODO: force update every cursor change :( |
1204 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1302 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1303 | |
|
|
1304 | unless (exists $self->{cur_h}) { |
|
|
1305 | my $text = substr $self->{text}, 0, $self->{cursor}; |
|
|
1306 | utf8::encode $text; |
|
|
1307 | |
|
|
1308 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
|
|
1309 | } |
|
|
1310 | |
1205 | glColor @{$self->{fg}}; |
1311 | glColor @{$self->{fg}}; |
1206 | glBegin GL_LINES; |
1312 | glBegin GL_LINES; |
1207 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1208 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1313 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
|
|
1314 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1209 | glEnd; |
1315 | glEnd; |
1210 | } |
1316 | } |
1211 | } |
1317 | } |
1212 | |
1318 | |
1213 | package CFClient::UI::LineEntry; |
1319 | package CFClient::UI::Entry; |
1214 | |
1320 | |
1215 | our @ISA = CFClient::UI::Entry::; |
1321 | our @ISA = CFClient::UI::EntryBase::; |
1216 | |
1322 | |
1217 | use SDL; |
1323 | use CFClient::OpenGL; |
1218 | use SDL::OpenGL; |
|
|
1219 | |
1324 | |
1220 | sub key_down { |
1325 | sub key_down { |
1221 | my ($self, $ev) = @_; |
1326 | my ($self, $ev) = @_; |
1222 | |
1327 | |
1223 | my $sym = $ev->key_sym; |
1328 | my $sym = $ev->{sym}; |
1224 | |
1329 | |
1225 | if ($sym == SDLK_RETURN) { |
1330 | if ($sym == 13) { |
1226 | $self->emit (activate => $self->get_text); |
1331 | $self->emit (activate => $self->get_text); |
1227 | $self->update; |
1332 | $self->update; |
1228 | |
1333 | |
1229 | } else { |
1334 | } else { |
1230 | $self->SUPER::key_down ($ev); |
1335 | $self->SUPER::key_down ($ev); |
… | |
… | |
1236 | |
1341 | |
1237 | package CFClient::UI::Button; |
1342 | package CFClient::UI::Button; |
1238 | |
1343 | |
1239 | our @ISA = CFClient::UI::Label::; |
1344 | our @ISA = CFClient::UI::Label::; |
1240 | |
1345 | |
1241 | use SDL; |
1346 | use CFClient::OpenGL; |
1242 | use SDL::OpenGL; |
|
|
1243 | |
1347 | |
1244 | my @tex = |
1348 | my @tex = |
1245 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1349 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1246 | qw(b1_button_active.png); |
1350 | qw(b1_button_active.png); |
1247 | |
1351 | |
1248 | sub new { |
1352 | sub new { |
1249 | my $class = shift; |
1353 | my $class = shift; |
1250 | |
1354 | |
1251 | $class->SUPER::new ( |
1355 | $class->SUPER::new ( |
1252 | padding => 4, |
1356 | padding => 4, |
1253 | fg => [1, 1, 1], |
1357 | fg => [1, 1, 1], |
1254 | bg => [1, 1, 1, 0.2], |
1358 | bg => [1, 1, 1, 0.2], |
1255 | active_fg => [1, 1, 0], |
1359 | active_fg => [0, 0, 1], |
1256 | can_hover => 1, |
1360 | can_hover => 1, |
|
|
1361 | align => 0, |
|
|
1362 | valign => 0, |
1257 | @_ |
1363 | @_ |
1258 | ) |
1364 | ) |
1259 | } |
1365 | } |
1260 | |
1366 | |
1261 | sub button_up { |
1367 | sub button_up { |
… | |
… | |
1269 | |
1375 | |
1270 | sub _draw { |
1376 | sub _draw { |
1271 | my ($self) = @_; |
1377 | my ($self) = @_; |
1272 | |
1378 | |
1273 | local $self->{fg} = $self->{fg}; |
1379 | local $self->{fg} = $self->{fg}; |
1274 | my $tex = $tex[0]; |
|
|
1275 | |
|
|
1276 | glEnable GL_BLEND; |
|
|
1277 | glEnable GL_TEXTURE_2D; |
|
|
1278 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1279 | |
1380 | |
1280 | if ($GRAB == $self) { |
1381 | if ($GRAB == $self) { |
1281 | $self->{fg} = $self->{active_fg}; |
1382 | $self->{fg} = $self->{active_fg}; |
1282 | } |
1383 | } |
1283 | |
1384 | |
1284 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1385 | glEnable GL_BLEND; |
|
|
1386 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1387 | glEnable GL_TEXTURE_2D; |
1285 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1388 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1389 | glColor 0, 0, 0, 1; |
1286 | |
1390 | |
1287 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
1391 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1288 | |
1392 | |
1289 | glDisable GL_TEXTURE_2D; |
1393 | glDisable GL_TEXTURE_2D; |
1290 | glDisable GL_BLEND; |
1394 | glDisable GL_BLEND; |
1291 | |
1395 | |
1292 | $self->SUPER::_draw; |
1396 | $self->SUPER::_draw; |
… | |
… | |
1297 | package CFClient::UI::CheckBox; |
1401 | package CFClient::UI::CheckBox; |
1298 | |
1402 | |
1299 | our @ISA = CFClient::UI::DrawBG::; |
1403 | our @ISA = CFClient::UI::DrawBG::; |
1300 | |
1404 | |
1301 | my @tex = |
1405 | my @tex = |
1302 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1406 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1303 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1407 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1304 | |
1408 | |
1305 | use SDL; |
1409 | use CFClient::OpenGL; |
1306 | use SDL::OpenGL; |
|
|
1307 | |
1410 | |
1308 | sub new { |
1411 | sub new { |
1309 | my $class = shift; |
1412 | my $class = shift; |
1310 | |
1413 | |
1311 | $class->SUPER::new ( |
1414 | $class->SUPER::new ( |
… | |
… | |
1322 | my ($self) = @_; |
1425 | my ($self) = @_; |
1323 | |
1426 | |
1324 | ($self->{padding} * 2 + 6) x 2 |
1427 | ($self->{padding} * 2 + 6) x 2 |
1325 | } |
1428 | } |
1326 | |
1429 | |
1327 | sub size_allocate { |
|
|
1328 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1329 | |
|
|
1330 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1331 | } |
|
|
1332 | |
|
|
1333 | sub button_down { |
1430 | sub button_down { |
1334 | my ($self, $ev, $x, $y) = @_; |
1431 | my ($self, $ev, $x, $y) = @_; |
1335 | |
1432 | |
1336 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1433 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1337 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1434 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1363 | glDisable GL_BLEND; |
1460 | glDisable GL_BLEND; |
1364 | } |
1461 | } |
1365 | |
1462 | |
1366 | ############################################################################# |
1463 | ############################################################################# |
1367 | |
1464 | |
|
|
1465 | package CFClient::UI::Image; |
|
|
1466 | |
|
|
1467 | our @ISA = CFClient::UI::Base::; |
|
|
1468 | |
|
|
1469 | use CFClient::OpenGL; |
|
|
1470 | use Carp qw/confess/; |
|
|
1471 | |
|
|
1472 | our %loaded_images; |
|
|
1473 | |
|
|
1474 | sub new { |
|
|
1475 | my $class = shift; |
|
|
1476 | |
|
|
1477 | my $self = $class->SUPER::new (@_); |
|
|
1478 | |
|
|
1479 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1480 | |
|
|
1481 | $loaded_images{$self->{image}} ||= |
|
|
1482 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1483 | |
|
|
1484 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1485 | |
|
|
1486 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
1487 | |
|
|
1488 | $self |
|
|
1489 | } |
|
|
1490 | |
|
|
1491 | sub size_request { |
|
|
1492 | my ($self) = @_; |
|
|
1493 | |
|
|
1494 | ($self->{tex}->{w}, $self->{tex}->{h}) |
|
|
1495 | } |
|
|
1496 | |
|
|
1497 | sub _draw { |
|
|
1498 | my ($self) = @_; |
|
|
1499 | |
|
|
1500 | my $tex = $self->{tex}; |
|
|
1501 | |
|
|
1502 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1503 | |
|
|
1504 | if ($self->{rot90}) { |
|
|
1505 | glRotate 90, 0, 0, 1; |
|
|
1506 | glTranslate 0, -$self->{w}, 0; |
|
|
1507 | |
|
|
1508 | ($w, $h) = ($h, $w); |
|
|
1509 | } |
|
|
1510 | |
|
|
1511 | glEnable GL_BLEND; |
|
|
1512 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1513 | glEnable GL_TEXTURE_2D; |
|
|
1514 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1515 | |
|
|
1516 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1517 | |
|
|
1518 | glDisable GL_BLEND; |
|
|
1519 | glDisable GL_TEXTURE_2D; |
|
|
1520 | } |
|
|
1521 | |
|
|
1522 | ############################################################################# |
|
|
1523 | |
|
|
1524 | package CFClient::UI::VGauge; |
|
|
1525 | |
|
|
1526 | our @ISA = CFClient::UI::Base::; |
|
|
1527 | |
|
|
1528 | use CFClient::OpenGL; |
|
|
1529 | |
|
|
1530 | my %tex = ( |
|
|
1531 | food => [ |
|
|
1532 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1533 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1534 | ], |
|
|
1535 | grace => [ |
|
|
1536 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1537 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1538 | ], |
|
|
1539 | hp => [ |
|
|
1540 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1541 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1542 | ], |
|
|
1543 | mana => [ |
|
|
1544 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1545 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1546 | ], |
|
|
1547 | ); |
|
|
1548 | |
|
|
1549 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1550 | sub new { |
|
|
1551 | my $class = shift; |
|
|
1552 | |
|
|
1553 | my $self = $class->SUPER::new ( |
|
|
1554 | type => 'food', |
|
|
1555 | @_ |
|
|
1556 | ); |
|
|
1557 | |
|
|
1558 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1559 | |
|
|
1560 | $self |
|
|
1561 | } |
|
|
1562 | |
|
|
1563 | sub size_request { |
|
|
1564 | my ($self) = @_; |
|
|
1565 | |
|
|
1566 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1567 | #@$tex{qw(w h)} |
|
|
1568 | (0, 0) |
|
|
1569 | } |
|
|
1570 | |
|
|
1571 | sub set_max { |
|
|
1572 | my ($self, $max) = @_; |
|
|
1573 | |
|
|
1574 | $self->{max_val} = $max; |
|
|
1575 | } |
|
|
1576 | |
|
|
1577 | sub set_value { |
|
|
1578 | my ($self, $val, $max) = @_; |
|
|
1579 | |
|
|
1580 | $self->set_max ($max) |
|
|
1581 | if defined $max; |
|
|
1582 | |
|
|
1583 | $max = $self->{max_val}; |
|
|
1584 | $self->{val} = $val; |
|
|
1585 | |
|
|
1586 | $self->update; |
|
|
1587 | } |
|
|
1588 | |
|
|
1589 | sub _draw { |
|
|
1590 | my ($self) = @_; |
|
|
1591 | |
|
|
1592 | my $tex = $tex{$self->{type}}; |
|
|
1593 | |
|
|
1594 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1595 | |
|
|
1596 | if ($self->{vertical}) { |
|
|
1597 | glRotate 90, 0, 0, 1; |
|
|
1598 | glTranslate 0, -$self->{w}, 0; |
|
|
1599 | |
|
|
1600 | ($w, $h) = ($h, $w); |
|
|
1601 | } |
|
|
1602 | |
|
|
1603 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1604 | $ycut = 1 if $self->{val} > $self->{max_val}; |
|
|
1605 | |
|
|
1606 | my $t1 = $tex->[0]; |
|
|
1607 | my $t2 = $tex->[1]; |
|
|
1608 | |
|
|
1609 | glEnable GL_BLEND; |
|
|
1610 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1611 | glEnable GL_TEXTURE_2D; |
|
|
1612 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1613 | |
|
|
1614 | my $h1 = $self->{h} - $ycut * $self->{h}; |
|
|
1615 | my $h2 = $ycut * $self->{h}; |
|
|
1616 | |
|
|
1617 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1618 | glBegin GL_QUADS; |
|
|
1619 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1620 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; |
|
|
1621 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; |
|
|
1622 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
|
|
1623 | glEnd; |
|
|
1624 | |
|
|
1625 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1626 | glBegin GL_QUADS; |
|
|
1627 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; |
|
|
1628 | glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; |
|
|
1629 | glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; |
|
|
1630 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; |
|
|
1631 | glEnd; |
|
|
1632 | |
|
|
1633 | glDisable GL_BLEND; |
|
|
1634 | glDisable GL_TEXTURE_2D; |
|
|
1635 | } |
|
|
1636 | |
|
|
1637 | ############################################################################# |
|
|
1638 | |
|
|
1639 | package CFClient::UI::Gauge; |
|
|
1640 | |
|
|
1641 | our @ISA = CFClient::UI::VBox::; |
|
|
1642 | |
|
|
1643 | sub new { |
|
|
1644 | my ($class, %arg) = shift; |
|
|
1645 | |
|
|
1646 | my $self = $class->SUPER::new ( |
|
|
1647 | @_, |
|
|
1648 | ); |
|
|
1649 | |
|
|
1650 | $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
|
|
1651 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); |
|
|
1652 | $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
|
|
1653 | |
|
|
1654 | $self |
|
|
1655 | } |
|
|
1656 | |
|
|
1657 | sub set_value { |
|
|
1658 | my ($self, $val, $max) = @_; |
|
|
1659 | |
|
|
1660 | $self->set_max ($max) |
|
|
1661 | if defined $max; |
|
|
1662 | |
|
|
1663 | $self->{gauge}->set_value ($val, $max); |
|
|
1664 | $self->{value}->set_text ($val); |
|
|
1665 | } |
|
|
1666 | |
|
|
1667 | sub set_max { |
|
|
1668 | my ($self, $max) = @_; |
|
|
1669 | |
|
|
1670 | $self->{gauge}->set_max ($max); |
|
|
1671 | $self->{max}->set_text ($max); |
|
|
1672 | } |
|
|
1673 | |
|
|
1674 | ############################################################################# |
|
|
1675 | |
1368 | package CFClient::UI::Slider; |
1676 | package CFClient::UI::Slider; |
1369 | |
1677 | |
1370 | use strict; |
1678 | use strict; |
1371 | |
1679 | |
1372 | use SDL::OpenGL; |
1680 | use CFClient::OpenGL; |
1373 | |
1681 | |
1374 | our @ISA = CFClient::UI::DrawBG::; |
1682 | our @ISA = CFClient::UI::DrawBG::; |
1375 | |
1683 | |
1376 | my @tex = |
1684 | my @tex = |
1377 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1685 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1387 | # TODO: calculations are off |
1695 | # TODO: calculations are off |
1388 | my $self = $class->SUPER::new ( |
1696 | my $self = $class->SUPER::new ( |
1389 | fg => [1, 1, 1], |
1697 | fg => [1, 1, 1], |
1390 | active_fg => [0, 0, 0], |
1698 | active_fg => [0, 0, 0], |
1391 | range => [0, 0, 100, 10], |
1699 | range => [0, 0, 100, 10], |
1392 | req_w => 40, |
1700 | req_w => 20, |
1393 | req_h => 13, |
1701 | req_h => 20, |
1394 | vertical => 0, |
1702 | vertical => 0, |
1395 | can_hover => 1, |
1703 | can_hover => 1, |
1396 | inner_pad => 5, |
1704 | inner_pad => 5, |
1397 | @_ |
1705 | @_ |
1398 | ); |
1706 | ); |
… | |
… | |
1502 | |
1810 | |
1503 | package CFClient::UI::TextView; |
1811 | package CFClient::UI::TextView; |
1504 | |
1812 | |
1505 | our @ISA = CFClient::UI::HBox::; |
1813 | our @ISA = CFClient::UI::HBox::; |
1506 | |
1814 | |
1507 | use SDL::OpenGL; |
1815 | use CFClient::OpenGL; |
1508 | |
1816 | |
1509 | sub new { |
1817 | sub new { |
1510 | my $class = shift; |
1818 | my $class = shift; |
1511 | |
1819 | |
1512 | my $self = $class->SUPER::new ( |
1820 | my $self = $class->SUPER::new ( |
1513 | req_w => $::WIDTH / 6, |
|
|
1514 | req_h => $::HEIGHT / 6, |
|
|
1515 | fontsize => $::FONTSIZE, |
1821 | fontsize => 1, |
1516 | @_, |
1822 | @_, |
1517 | |
1823 | |
1518 | layout => (new CFClient::Layout), |
1824 | layout => (new CFClient::Layout), |
1519 | par => [], |
1825 | par => [], |
1520 | height => 0, |
1826 | height => 0, |
… | |
… | |
1541 | sub text_height { |
1847 | sub text_height { |
1542 | my ($self, $text) = @_; |
1848 | my ($self, $text) = @_; |
1543 | |
1849 | |
1544 | my $layout = $self->{layout}; |
1850 | my $layout = $self->{layout}; |
1545 | |
1851 | |
1546 | $layout->set_height ($self->{fontsize}); |
1852 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1547 | $layout->set_width ($self->{w}); |
1853 | $layout->set_width ($self->{w}); |
1548 | $layout->set_text ($text); |
1854 | $layout->set_text ($text); |
1549 | |
1855 | |
1550 | ($layout->size)[1] |
1856 | ($layout->size)[1] |
1551 | } |
1857 | } |
… | |
… | |
1555 | |
1861 | |
1556 | $self->{need_reflow}++; |
1862 | $self->{need_reflow}++; |
1557 | $self->update; |
1863 | $self->update; |
1558 | } |
1864 | } |
1559 | |
1865 | |
1560 | sub size_request { |
|
|
1561 | my ($self) = @_; |
|
|
1562 | |
|
|
1563 | ($self->{req_w}, $self->{req_h}) |
|
|
1564 | } |
|
|
1565 | |
|
|
1566 | sub size_allocate { |
1866 | sub size_allocate { |
1567 | my ($self, $x, $y, $w, $h) = @_; |
1867 | my ($self, $w, $h) = @_; |
1568 | |
1868 | |
1569 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1869 | $self->SUPER::size_allocate ($w, $h); |
1570 | |
1870 | |
1571 | $self->{layout}->set_height ($self->{fontsize}); |
1871 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1572 | $self->{layout}->set_width ($self->{w}); |
1872 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1573 | |
1873 | |
1574 | $self->reflow; |
1874 | $self->reflow; |
1575 | } |
1875 | } |
1576 | |
1876 | |
1577 | sub add_paragraph { |
1877 | sub add_paragraph { |
… | |
… | |
1664 | |
1964 | |
1665 | } |
1965 | } |
1666 | |
1966 | |
1667 | ############################################################################# |
1967 | ############################################################################# |
1668 | |
1968 | |
1669 | package CFClient::UI::MapWidget; |
|
|
1670 | |
|
|
1671 | use strict; |
|
|
1672 | |
|
|
1673 | use List::Util qw(min max); |
|
|
1674 | |
|
|
1675 | use SDL; |
|
|
1676 | use SDL::OpenGL; |
|
|
1677 | |
|
|
1678 | our @ISA = CFClient::UI::Base::; |
|
|
1679 | |
|
|
1680 | sub new { |
|
|
1681 | my $class = shift; |
|
|
1682 | |
|
|
1683 | $class->SUPER::new ( |
|
|
1684 | z => -1, |
|
|
1685 | can_focus => 1, |
|
|
1686 | list => (glGenLists 1), |
|
|
1687 | @_ |
|
|
1688 | ) |
|
|
1689 | } |
|
|
1690 | |
|
|
1691 | sub key_down { |
|
|
1692 | print "MAPKEYDOWN\n"; |
|
|
1693 | } |
|
|
1694 | |
|
|
1695 | sub key_up { |
|
|
1696 | } |
|
|
1697 | |
|
|
1698 | sub button_down { |
|
|
1699 | my ($self, $ev, $x, $y) = @_; |
|
|
1700 | |
|
|
1701 | $self->focus_in; |
|
|
1702 | |
|
|
1703 | if ($ev->button == 2) { |
|
|
1704 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1705 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1706 | |
|
|
1707 | $self->{motion} = sub { |
|
|
1708 | my ($ev, $x, $y) = @_; |
|
|
1709 | |
|
|
1710 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1711 | |
|
|
1712 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1713 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1714 | |
|
|
1715 | $self->update; |
|
|
1716 | }; |
|
|
1717 | } |
|
|
1718 | } |
|
|
1719 | |
|
|
1720 | sub button_up { |
|
|
1721 | my ($self, $ev, $x, $y) = @_; |
|
|
1722 | |
|
|
1723 | delete $self->{motion}; |
|
|
1724 | } |
|
|
1725 | |
|
|
1726 | sub mouse_motion { |
|
|
1727 | my ($self, $ev, $x, $y) = @_; |
|
|
1728 | |
|
|
1729 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1730 | } |
|
|
1731 | |
|
|
1732 | sub size_request { |
|
|
1733 | ( |
|
|
1734 | 1 + 32 * int $::WIDTH / 32, |
|
|
1735 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1736 | ) |
|
|
1737 | } |
|
|
1738 | |
|
|
1739 | sub update { |
|
|
1740 | my ($self) = @_; |
|
|
1741 | |
|
|
1742 | $self->{need_update} = 1; |
|
|
1743 | $self->SUPER::update; |
|
|
1744 | } |
|
|
1745 | |
|
|
1746 | sub draw { |
|
|
1747 | my ($self) = @_; |
|
|
1748 | |
|
|
1749 | if (delete $self->{need_update}) { |
|
|
1750 | glNewList $self->{list}, GL_COMPILE; |
|
|
1751 | |
|
|
1752 | if ($::MAP) { |
|
|
1753 | my $sw = int $::WIDTH / 32; |
|
|
1754 | my $sh = int $::HEIGHT / 32; |
|
|
1755 | |
|
|
1756 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1757 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1758 | |
|
|
1759 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1760 | |
|
|
1761 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1762 | |
|
|
1763 | if ($::CFG->{fow_enable}) { |
|
|
1764 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1765 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1766 | glConvolutionFilter2D |
|
|
1767 | GL_CONVOLUTION_2D, |
|
|
1768 | GL_ALPHA, |
|
|
1769 | 3, 3, |
|
|
1770 | GL_ALPHA, GL_FLOAT, |
|
|
1771 | pack "f*", |
|
|
1772 | 0.1, 0.1, 0.1, |
|
|
1773 | 0.1, 0.2, 0.1, |
|
|
1774 | 0.1, 0.1, 0.1, |
|
|
1775 | ; |
|
|
1776 | glEnable GL_CONVOLUTION_2D; |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | my $tex = new CFClient::Texture |
|
|
1780 | w => $w, |
|
|
1781 | h => $h, |
|
|
1782 | data => $data, |
|
|
1783 | internalformat => GL_ALPHA, |
|
|
1784 | format => GL_ALPHA; |
|
|
1785 | |
|
|
1786 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1787 | |
|
|
1788 | glEnable GL_BLEND; |
|
|
1789 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1790 | glEnable GL_TEXTURE_2D; |
|
|
1791 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1792 | |
|
|
1793 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1794 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1795 | |
|
|
1796 | glDisable GL_TEXTURE_2D; |
|
|
1797 | glDisable GL_BLEND; |
|
|
1798 | } |
|
|
1799 | |
|
|
1800 | # HACK BEGIN |
|
|
1801 | { |
|
|
1802 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1803 | my ($w, $h) = (250, 250); |
|
|
1804 | |
|
|
1805 | glEnable GL_BLEND; |
|
|
1806 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1807 | glEnable GL_TEXTURE_2D; |
|
|
1808 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1809 | |
|
|
1810 | CFClient::Texture->new ( |
|
|
1811 | w => $w, |
|
|
1812 | h => $h, |
|
|
1813 | data => $::MAP->mapmap ($w, $h), |
|
|
1814 | type => GL_UNSIGNED_INT_8_8_8_8_REV |
|
|
1815 | )->draw_quad (100, 100); |
|
|
1816 | |
|
|
1817 | glDisable GL_TEXTURE_2D; |
|
|
1818 | glDisable GL_BLEND; |
|
|
1819 | } |
|
|
1820 | # HACK END |
|
|
1821 | } |
|
|
1822 | |
|
|
1823 | glEndList; |
|
|
1824 | } |
|
|
1825 | |
|
|
1826 | glPushMatrix; |
|
|
1827 | glCallList $self->{list}; |
|
|
1828 | glPopMatrix; |
|
|
1829 | |
|
|
1830 | if ($FOCUS != $self) { |
|
|
1831 | glColor 64/255, 64/255, 64/255; |
|
|
1832 | glLogicOp GL_AND; |
|
|
1833 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1834 | glBegin GL_QUADS; |
|
|
1835 | glVertex 0, 0; |
|
|
1836 | glVertex 0, $::HEIGHT; |
|
|
1837 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1838 | glVertex $::WIDTH, 0; |
|
|
1839 | glEnd; |
|
|
1840 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1841 | } |
|
|
1842 | } |
|
|
1843 | |
|
|
1844 | my %DIR = ( |
|
|
1845 | SDLK_KP8, [1, "north"], |
|
|
1846 | SDLK_KP9, [2, "northeast"], |
|
|
1847 | SDLK_KP6, [3, "east"], |
|
|
1848 | SDLK_KP3, [4, "southeast"], |
|
|
1849 | SDLK_KP2, [5, "south"], |
|
|
1850 | SDLK_KP1, [6, "southwest"], |
|
|
1851 | SDLK_KP4, [7, "west"], |
|
|
1852 | SDLK_KP7, [8, "northwest"], |
|
|
1853 | |
|
|
1854 | SDLK_UP, [1, "north"], |
|
|
1855 | SDLK_RIGHT, [3, "east"], |
|
|
1856 | SDLK_DOWN, [5, "south"], |
|
|
1857 | SDLK_LEFT, [7, "west"], |
|
|
1858 | ); |
|
|
1859 | |
|
|
1860 | sub key_down { |
|
|
1861 | my ($self, $ev) = @_; |
|
|
1862 | |
|
|
1863 | my $mod = $ev->key_mod; |
|
|
1864 | my $sym = $ev->key_sym; |
|
|
1865 | |
|
|
1866 | if ($sym == SDLK_KP5) { |
|
|
1867 | $::CONN->user_send ("command stay fire"); |
|
|
1868 | } elsif ($sym == SDLK_a) { |
|
|
1869 | $::CONN->user_send ("command apply"); |
|
|
1870 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1871 | $self->emit ('activate_console'); |
|
|
1872 | } elsif ($sym == SDLK_SLASH) { |
|
|
1873 | $self->emit ('activate_console' => '/'); |
|
|
1874 | } elsif (exists $DIR{$sym}) { |
|
|
1875 | if ($mod & KMOD_SHIFT) { |
|
|
1876 | $self->{shft}++; |
|
|
1877 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
|
|
1878 | } elsif ($mod & KMOD_CTRL) { |
|
|
1879 | $self->{ctrl}++; |
|
|
1880 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
|
|
1881 | } else { |
|
|
1882 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
|
|
1883 | } |
|
|
1884 | } |
|
|
1885 | } |
|
|
1886 | |
|
|
1887 | sub key_up { |
|
|
1888 | my ($self, $ev) = @_; |
|
|
1889 | |
|
|
1890 | my $mod = $ev->key_mod; |
|
|
1891 | my $sym = $ev->key_sym; |
|
|
1892 | |
|
|
1893 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1894 | $::CONN->user_send ("command fire_stop"); |
|
|
1895 | } |
|
|
1896 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1897 | $::CONN->user_send ("command run_stop"); |
|
|
1898 | } |
|
|
1899 | } |
|
|
1900 | |
|
|
1901 | ############################################################################# |
|
|
1902 | |
|
|
1903 | package CFClient::UI::Animator; |
1969 | package CFClient::UI::Animator; |
1904 | |
1970 | |
1905 | use SDL::OpenGL; |
1971 | use CFClient::OpenGL; |
1906 | |
1972 | |
1907 | our @ISA = CFClient::UI::Bin::; |
1973 | our @ISA = CFClient::UI::Bin::; |
1908 | |
1974 | |
1909 | sub moveto { |
1975 | sub moveto { |
1910 | my ($self, $x, $y) = @_; |
1976 | my ($self, $x, $y) = @_; |
… | |
… | |
1983 | |
2049 | |
1984 | package CFClient::UI::Root; |
2050 | package CFClient::UI::Root; |
1985 | |
2051 | |
1986 | our @ISA = CFClient::UI::Container::; |
2052 | our @ISA = CFClient::UI::Container::; |
1987 | |
2053 | |
1988 | use SDL::OpenGL; |
2054 | use CFClient::OpenGL; |
|
|
2055 | |
|
|
2056 | sub check_size { |
|
|
2057 | my ($self) = @_; |
|
|
2058 | |
|
|
2059 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
|
|
2060 | } |
1989 | |
2061 | |
1990 | sub size_request { |
2062 | sub size_request { |
1991 | ($::WIDTH, $::HEIGHT) |
2063 | ($::WIDTH, $::HEIGHT) |
1992 | } |
2064 | } |
1993 | |
2065 | |
1994 | sub size_allocate { |
2066 | sub configure { |
1995 | my ($self, $x, $y, $w, $h) = @_; |
2067 | my ($self, $x, $y, $w, $h) = @_; |
1996 | |
2068 | |
1997 | $self->_size_allocate ($x, $y, $w, $h); |
2069 | $self->SUPER::configure ($x, $y, $w, $h); |
1998 | |
2070 | |
1999 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
2071 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
2000 | for @{$self->{children}}; |
2072 | for @{$self->{children}}; |
2001 | } |
2073 | } |
2002 | |
2074 | |
2003 | sub _topleft { |
2075 | sub _topleft { |
2004 | my ($self, $x, $y) = @_; |
2076 | my ($self, $x, $y) = @_; |
… | |
… | |
2007 | } |
2079 | } |
2008 | |
2080 | |
2009 | sub update { |
2081 | sub update { |
2010 | my ($self) = @_; |
2082 | my ($self) = @_; |
2011 | |
2083 | |
2012 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2084 | $self->check_size; |
2013 | ::refresh (); |
2085 | ::refresh (); |
2014 | } |
2086 | } |
2015 | |
2087 | |
2016 | sub add { |
2088 | sub add { |
2017 | my ($self, $widget) = @_; |
2089 | my ($self, $child) = @_; |
2018 | |
2090 | |
2019 | $self->SUPER::add ($widget); |
2091 | $self->SUPER::add ($child); |
2020 | |
|
|
2021 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
|
|
2022 | } |
2092 | } |
2023 | |
2093 | |
2024 | sub on_refresh { |
2094 | sub on_refresh { |
2025 | my ($self, $id, $cb) = @_; |
2095 | my ($self, $id, $cb) = @_; |
2026 | |
2096 | |