… | |
… | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $ROOT; |
12 | our $ROOT; |
|
|
13 | our $TOOLTIP; |
13 | our $BUTTON_STATE; |
14 | our $BUTTON_STATE; |
|
|
15 | |
|
|
16 | sub check_tooltip { |
|
|
17 | if (!$GRAB) { |
|
|
18 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
19 | if (length $widget->{tooltip}) { |
|
|
20 | |
|
|
21 | if ($TOOLTIP->{owner} != $widget) { |
|
|
22 | $TOOLTIP->{owner} = $widget; |
|
|
23 | |
|
|
24 | my $tip = $widget->{tooltip}; |
|
|
25 | |
|
|
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
27 | |
|
|
28 | $TOOLTIP->set_text ($widget->{tooltip}); |
|
|
29 | $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); |
|
|
30 | $TOOLTIP->show; |
|
|
31 | } |
|
|
32 | |
|
|
33 | return; |
|
|
34 | } |
|
|
35 | } |
|
|
36 | } |
|
|
37 | |
|
|
38 | $TOOLTIP->hide; |
|
|
39 | delete $TOOLTIP->{owner}; |
|
|
40 | } |
14 | |
41 | |
15 | # class methods for events |
42 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
43 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
44 | $FOCUS->key_down ($_[0]) if $FOCUS; |
18 | } |
45 | } |
… | |
… | |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
48 | $FOCUS->key_up ($_[0]) if $FOCUS; |
22 | } |
49 | } |
23 | |
50 | |
24 | sub feed_sdl_button_down_event { |
51 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
52 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
53 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
54 | |
28 | if (!$BUTTON_STATE) { |
55 | if (!$BUTTON_STATE) { |
29 | my $widget = $ROOT->find_widget ($x, $y); |
56 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
57 | |
31 | $GRAB = $widget; |
58 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
59 | $GRAB->update if $GRAB; |
33 | } |
|
|
34 | |
60 | |
|
|
61 | check_tooltip; |
|
|
62 | } |
|
|
63 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
64 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
65 | |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
66 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
38 | } |
67 | } |
39 | |
68 | |
40 | sub feed_sdl_button_up_event { |
69 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
70 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
71 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
72 | |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
73 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
74 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
75 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
76 | |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
77 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
49 | |
78 | |
50 | if (!$BUTTON_STATE) { |
79 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
80 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
81 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
82 | $GRAB->update if $GRAB; |
|
|
83 | |
|
|
84 | check_tooltip; |
54 | } |
85 | } |
55 | } |
86 | } |
56 | |
87 | |
57 | sub feed_sdl_motion_event { |
88 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
89 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
90 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
91 | |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
92 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
93 | |
63 | if ($widget != $HOVER) { |
94 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
95 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
96 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
97 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
98 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
|
|
99 | |
|
|
100 | check_tooltip; |
68 | } |
101 | } |
69 | |
102 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
103 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
71 | } |
104 | } |
72 | |
105 | |
… | |
… | |
87 | |
120 | |
88 | package CFClient::UI::Base; |
121 | package CFClient::UI::Base; |
89 | |
122 | |
90 | use strict; |
123 | use strict; |
91 | |
124 | |
92 | use SDL::OpenGL; |
125 | use CFClient::OpenGL; |
93 | |
126 | |
94 | sub new { |
127 | sub new { |
95 | my $class = shift; |
128 | my $class = shift; |
96 | |
129 | |
97 | my $self = bless { |
130 | my $self = bless { |
98 | x => 0, |
131 | x => 0, |
99 | y => 0, |
132 | y => 0, |
100 | z => 0, |
133 | z => 0, |
101 | w => -1, |
134 | can_events => 1, |
102 | h => -1, |
|
|
103 | @_ |
135 | @_ |
104 | }, $class; |
136 | }, $class; |
105 | |
137 | |
106 | for (keys %$self) { |
138 | for (keys %$self) { |
107 | if (/^connect_(.*)$/) { |
139 | if (/^connect_(.*)$/) { |
… | |
… | |
110 | } |
142 | } |
111 | |
143 | |
112 | $self |
144 | $self |
113 | } |
145 | } |
114 | |
146 | |
|
|
147 | sub destroy { |
|
|
148 | my ($self) = @_; |
|
|
149 | |
|
|
150 | $self->hide; |
|
|
151 | %$self = (); |
|
|
152 | } |
|
|
153 | |
|
|
154 | sub show { |
|
|
155 | my ($self) = @_; |
|
|
156 | |
|
|
157 | return if $self->{parent}; |
|
|
158 | |
|
|
159 | $CFClient::UI::ROOT->add ($self); |
|
|
160 | } |
|
|
161 | |
|
|
162 | sub hide { |
|
|
163 | my ($self) = @_; |
|
|
164 | |
|
|
165 | return unless $self->{parent}; |
|
|
166 | |
|
|
167 | $self->{parent}->remove ($self); |
|
|
168 | } |
|
|
169 | |
115 | sub move { |
170 | sub move { |
116 | my ($self, $x, $y, $z) = @_; |
171 | my ($self, $x, $y, $z) = @_; |
117 | |
172 | |
118 | $self->{x} = int $x; |
173 | $self->{x} = int $x; |
119 | $self->{y} = int $y; |
174 | $self->{y} = int $y; |
120 | $self->{z} = $z if defined $z; |
175 | $self->{z} = $z if defined $z; |
121 | |
176 | |
122 | $self->update; |
177 | $self->update; |
123 | } |
178 | } |
124 | |
179 | |
125 | sub needs_redraw { |
180 | sub set_size { |
126 | 0 |
181 | my ($self, $w, $h) = @_; |
|
|
182 | |
|
|
183 | $self->{user_w} = $w; |
|
|
184 | $self->{user_h} = $h; |
|
|
185 | |
|
|
186 | $self->check_size; |
127 | } |
187 | } |
128 | |
188 | |
129 | sub size_request { |
189 | sub size_request { |
130 | require Carp; |
190 | require Carp; |
131 | Carp::confess "size_request is abtract"; |
191 | Carp::confess "size_request is abstract"; |
132 | } |
192 | } |
133 | |
193 | |
134 | sub configure { |
194 | sub configure { |
135 | my ($self, $x, $y, $w, $h) = @_; |
195 | my ($self, $x, $y, $w, $h) = @_; |
136 | |
196 | |
|
|
197 | if ($self->{aspect}) { |
|
|
198 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
199 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
200 | |
|
|
201 | # use alignment to adjust x, y |
|
|
202 | |
|
|
203 | $x += int +($w - $w2) * 0.5; |
|
|
204 | $y += int +($h - $h2) * 0.5; |
|
|
205 | |
|
|
206 | ($w, $h) = ($w2, $h2); |
|
|
207 | } |
|
|
208 | |
|
|
209 | if ($self->{x} != $x || $self->{y} != $y) { |
137 | $self->{x} = $x; |
210 | $self->{x} = $x; |
138 | $self->{y} = $y; |
211 | $self->{y} = $y; |
|
|
212 | $self->update; |
|
|
213 | } |
139 | |
214 | |
140 | return unless $self->{w} != $w || $self->{h} != $h; |
215 | if ($self->{w} != $w || $self->{h} != $h) { |
141 | |
|
|
142 | $self->{w} = $w; |
216 | $self->{w} = $w; |
143 | $self->{h} = $h; |
217 | $self->{h} = $h; |
144 | |
218 | |
145 | $self->size_allocate ($w, $h); |
219 | $self->size_allocate ($w, $h); |
|
|
220 | $self->update; |
|
|
221 | } |
146 | } |
222 | } |
147 | |
223 | |
148 | sub size_allocate { |
224 | sub size_allocate { |
149 | # nothing to be done |
225 | # nothing to be done |
|
|
226 | } |
|
|
227 | |
|
|
228 | sub set_max_size { |
|
|
229 | my ($self, $w, $h) = @_; |
|
|
230 | |
|
|
231 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
|
|
232 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
150 | } |
233 | } |
151 | |
234 | |
152 | # return top left coordinates |
235 | # return top left coordinates |
153 | sub _topleft { |
236 | sub _topleft { |
154 | my ($self, $x, $y) = @_; |
237 | my ($self, $x, $y) = @_; |
… | |
… | |
248 | } |
331 | } |
249 | |
332 | |
250 | sub find_widget { |
333 | sub find_widget { |
251 | my ($self, $x, $y) = @_; |
334 | my ($self, $x, $y) = @_; |
252 | |
335 | |
|
|
336 | return () unless $self->{can_events}; |
|
|
337 | |
253 | return $self |
338 | return $self |
254 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
339 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
255 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
340 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
256 | |
341 | |
257 | () |
342 | () |
… | |
… | |
264 | } |
349 | } |
265 | |
350 | |
266 | sub check_size { |
351 | sub check_size { |
267 | my ($self) = @_; |
352 | my ($self) = @_; |
268 | |
353 | |
269 | my ($w, $h) = $self->size_request; |
354 | $self->{parent} |
|
|
355 | or return 1; |
270 | |
356 | |
|
|
357 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
|
|
358 | ? @$self{qw(user_w user_h)} |
|
|
359 | : $self->size_request; |
|
|
360 | |
271 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
361 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
272 | $self->{req_w} = $w; |
362 | $self->{req_w} = $w; |
273 | $self->{req_h} = $h; |
363 | $self->{req_h} = $h; |
274 | |
364 | |
275 | $self->{parent}->check_size |
365 | $self->{parent}->check_size |
276 | if $self->{parent}; |
|
|
277 | |
|
|
278 | $self->size_allocate ($w, $h); |
366 | or $self->size_allocate ( |
279 | $self->update; |
367 | (List::Util::max $self->{w}, $w), |
|
|
368 | (List::Util::max $self->{h}, $h), |
|
|
369 | ); |
|
|
370 | |
|
|
371 | 1 |
|
|
372 | } else { |
|
|
373 | 0 |
280 | } |
374 | } |
281 | } |
375 | } |
282 | |
376 | |
283 | sub update { |
377 | sub update { |
284 | my ($self) = @_; |
378 | my ($self) = @_; |
… | |
… | |
288 | } |
382 | } |
289 | |
383 | |
290 | sub connect { |
384 | sub connect { |
291 | my ($self, $signal, $cb) = @_; |
385 | my ($self, $signal, $cb) = @_; |
292 | |
386 | |
293 | push @{ $self->{cb}{$signal} }, $cb; |
387 | push @{ $self->{signal_cb}{$signal} }, $cb; |
294 | } |
388 | } |
295 | |
389 | |
296 | sub emit { |
390 | sub emit { |
297 | my ($self, $signal, @args) = @_; |
391 | my ($self, $signal, @args) = @_; |
298 | |
392 | |
|
|
393 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
299 | $_->($self, @args) |
394 | $cb->($self, @args); |
300 | for @{$self->{cb}{$signal} || []}; |
395 | } |
301 | } |
396 | } |
302 | |
397 | |
303 | sub DESTROY { |
398 | sub DESTROY { |
304 | my ($self) = @_; |
399 | my ($self) = @_; |
305 | |
400 | |
… | |
… | |
311 | package CFClient::UI::DrawBG; |
406 | package CFClient::UI::DrawBG; |
312 | |
407 | |
313 | our @ISA = CFClient::UI::Base::; |
408 | our @ISA = CFClient::UI::Base::; |
314 | |
409 | |
315 | use strict; |
410 | use strict; |
316 | use SDL::OpenGL; |
411 | use CFClient::OpenGL; |
317 | |
412 | |
318 | sub new { |
413 | sub new { |
319 | my $class = shift; |
414 | my $class = shift; |
320 | |
415 | |
321 | # range [value, low, high, page] |
416 | # range [value, low, high, page] |
… | |
… | |
350 | |
445 | |
351 | package CFClient::UI::Empty; |
446 | package CFClient::UI::Empty; |
352 | |
447 | |
353 | our @ISA = CFClient::UI::Base::; |
448 | our @ISA = CFClient::UI::Base::; |
354 | |
449 | |
|
|
450 | sub new { |
|
|
451 | my ($class, %arg) = @_; |
|
|
452 | $class->SUPER::new (can_events => 0, %arg); |
|
|
453 | } |
|
|
454 | |
355 | sub size_request { |
455 | sub size_request { |
356 | (0, 0) |
456 | (0, 0) |
357 | } |
457 | } |
358 | |
458 | |
359 | sub draw { } |
459 | sub draw { } |
… | |
… | |
367 | sub new { |
467 | sub new { |
368 | my ($class, %arg) = @_; |
468 | my ($class, %arg) = @_; |
369 | |
469 | |
370 | my $children = delete $arg{children} || []; |
470 | my $children = delete $arg{children} || []; |
371 | |
471 | |
372 | my $self = $class->SUPER::new (children => [], %arg); |
472 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
373 | $self->add ($_) for @$children; |
473 | $self->add ($_) for @$children; |
374 | |
474 | |
375 | $self |
475 | $self |
376 | } |
476 | } |
377 | |
477 | |
… | |
… | |
385 | $self->{children} = [ |
485 | $self->{children} = [ |
386 | sort { $a->{z} <=> $b->{z} } |
486 | sort { $a->{z} <=> $b->{z} } |
387 | @{$self->{children}}, $child |
487 | @{$self->{children}}, $child |
388 | ]; |
488 | ]; |
389 | |
489 | |
390 | $self->{w} = $self->{h} = -1; |
|
|
391 | |
|
|
392 | $child->check_size; |
490 | $child->check_size; |
393 | } |
491 | } |
394 | |
492 | |
395 | sub remove { |
493 | sub remove { |
396 | my ($self, $widget) = @_; |
494 | my ($self, $child) = @_; |
397 | |
495 | |
|
|
496 | delete $child->{parent}; |
|
|
497 | |
398 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
498 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
399 | |
499 | |
400 | $self->check_size; |
500 | $self->check_size; |
|
|
501 | $self->update; |
401 | } |
502 | } |
402 | |
503 | |
403 | sub find_widget { |
504 | sub find_widget { |
404 | my ($self, $x, $y) = @_; |
505 | my ($self, $x, $y) = @_; |
405 | |
506 | |
… | |
… | |
435 | |
536 | |
436 | $class->SUPER::new (children => [$child], %arg) |
537 | $class->SUPER::new (children => [$child], %arg) |
437 | } |
538 | } |
438 | |
539 | |
439 | sub add { |
540 | sub add { |
440 | my ($self, $widget) = @_; |
541 | my ($self, $child) = @_; |
441 | |
542 | |
442 | $self->{children} = []; |
543 | $self->{children} = []; |
443 | |
544 | |
444 | $self->SUPER::add ($widget); |
545 | $self->SUPER::add ($child); |
445 | } |
546 | } |
446 | |
547 | |
447 | sub remove { |
548 | sub remove { |
448 | my ($self, $widget) = @_; |
549 | my ($self, $widget) = @_; |
449 | |
550 | |
… | |
… | |
469 | |
570 | |
470 | package CFClient::UI::Window; |
571 | package CFClient::UI::Window; |
471 | |
572 | |
472 | our @ISA = CFClient::UI::Bin::; |
573 | our @ISA = CFClient::UI::Bin::; |
473 | |
574 | |
474 | use SDL::OpenGL; |
575 | use CFClient::OpenGL; |
475 | |
576 | |
476 | sub new { |
577 | sub new { |
477 | my ($class, %arg) = @_; |
578 | my ($class, %arg) = @_; |
478 | |
579 | |
479 | my $self = $class->SUPER::new (%arg); |
580 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
533 | sub new { die } |
634 | sub new { die } |
534 | |
635 | |
535 | sub size_request { |
636 | sub size_request { |
536 | my ($self) = @_; |
637 | my ($self) = @_; |
537 | |
638 | |
538 | @$self{qw(child_w child_h)} = $self->child->size_request; |
639 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
539 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
640 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
540 | |
641 | |
541 | @$self{qw(child_w child_h)} |
642 | @$self{qw(child_w child_h)} |
542 | } |
643 | } |
543 | |
644 | |
… | |
… | |
552 | |
653 | |
553 | package CFClient::UI::Frame; |
654 | package CFClient::UI::Frame; |
554 | |
655 | |
555 | our @ISA = CFClient::UI::Bin::; |
656 | our @ISA = CFClient::UI::Bin::; |
556 | |
657 | |
557 | use SDL::OpenGL; |
658 | use CFClient::OpenGL; |
558 | |
|
|
559 | sub size_request { |
|
|
560 | my ($self) = @_; |
|
|
561 | my $chld = $self->child |
|
|
562 | or return (0, 0); |
|
|
563 | |
|
|
564 | $chld->move (2, 2); |
|
|
565 | |
|
|
566 | map { $_ + 4 } $chld->size_request; |
|
|
567 | } |
|
|
568 | |
|
|
569 | sub size_allocate { |
|
|
570 | my ($self, $x, $y, $w, $h) = @_; |
|
|
571 | |
|
|
572 | $self->child->configure (2, 2, $w - 4, $h - 4); |
|
|
573 | } |
|
|
574 | |
|
|
575 | sub _draw { |
|
|
576 | my ($self) = @_; |
|
|
577 | |
|
|
578 | my $chld = $self->child; |
|
|
579 | |
|
|
580 | my ($w, $h) = $chld->size_request; |
|
|
581 | |
|
|
582 | glBegin GL_QUADS; |
|
|
583 | glColor 0, 0, 0; |
|
|
584 | glVertex 0 , 0; |
|
|
585 | glVertex 0 , $h + 4; |
|
|
586 | glVertex $w + 4 , $h + 4; |
|
|
587 | glVertex $w + 4 , 0; |
|
|
588 | glEnd; |
|
|
589 | |
|
|
590 | $chld->draw; |
|
|
591 | } |
|
|
592 | |
|
|
593 | ############################################################################# |
|
|
594 | |
|
|
595 | package CFClient::UI::FancyFrame; |
|
|
596 | |
|
|
597 | our @ISA = CFClient::UI::Bin::; |
|
|
598 | |
|
|
599 | use SDL::OpenGL; |
|
|
600 | |
|
|
601 | my @tex = |
|
|
602 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
603 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
604 | |
659 | |
605 | sub new { |
660 | sub new { |
606 | my $class = shift; |
661 | my $class = shift; |
607 | |
662 | |
608 | # TODO: user_x, user_y, overwrite moveto? |
|
|
609 | |
|
|
610 | $class->SUPER::new ( |
663 | my $self = $class->SUPER::new ( |
611 | bg => [1, 1, 1, 1], |
664 | bg => [1, 1, 1, 1], |
612 | border_bg => [1, 1, 1, 1], |
665 | border_bg => [1, 1, 1, 1], |
613 | border => int $::FONTSIZE * 0.8, |
666 | border => 0.8, |
614 | @_ |
667 | @_ |
615 | ) |
668 | ); |
|
|
669 | |
|
|
670 | $self |
|
|
671 | } |
|
|
672 | |
|
|
673 | sub _draw { |
|
|
674 | my ($self) = @_; |
|
|
675 | |
|
|
676 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
677 | |
|
|
678 | glEnable GL_BLEND; |
|
|
679 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
680 | glEnable GL_TEXTURE_2D; |
|
|
681 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
682 | |
|
|
683 | # glBegin GL_QUADS; |
|
|
684 | # glColor 0, 0, 0, 0; |
|
|
685 | # glVertex 0 , 0; |
|
|
686 | # glVertex 0 , $h; |
|
|
687 | # glVertex $w, $h; |
|
|
688 | # glVertex $w, 0; |
|
|
689 | # glEnd; |
|
|
690 | |
|
|
691 | |
|
|
692 | $self->child->draw; |
|
|
693 | glDisable GL_BLEND; |
|
|
694 | glDisable GL_TEXTURE_2D; |
|
|
695 | } |
|
|
696 | |
|
|
697 | ############################################################################# |
|
|
698 | |
|
|
699 | package CFClient::UI::FancyFrame; |
|
|
700 | |
|
|
701 | our @ISA = CFClient::UI::Bin::; |
|
|
702 | |
|
|
703 | use CFClient::OpenGL; |
|
|
704 | |
|
|
705 | my @tex = |
|
|
706 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
707 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
708 | |
|
|
709 | sub new { |
|
|
710 | my $class = shift; |
|
|
711 | |
|
|
712 | # TODO: user_x, user_y, overwrite moveto? |
|
|
713 | |
|
|
714 | my $self = $class->SUPER::new ( |
|
|
715 | bg => [1, 1, 1, 1], |
|
|
716 | border_bg => [1, 1, 1, 1], |
|
|
717 | border => 0.8, |
|
|
718 | can_events => 1, |
|
|
719 | @_ |
|
|
720 | ); |
|
|
721 | |
|
|
722 | $self->{title} &&= new CFClient::UI::Label |
|
|
723 | align => 0, |
|
|
724 | valign => 1, |
|
|
725 | text => $self->{title}, |
|
|
726 | fontsize => 1; |
|
|
727 | |
|
|
728 | $self |
|
|
729 | } |
|
|
730 | |
|
|
731 | sub border { |
|
|
732 | int $_[0]{border} * $::FONTSIZE |
616 | } |
733 | } |
617 | |
734 | |
618 | sub size_request { |
735 | sub size_request { |
619 | my ($self) = @_; |
736 | my ($self) = @_; |
620 | |
737 | |
621 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
622 | |
|
|
623 | my ($w, $h) = $self->SUPER::size_request; |
738 | my ($w, $h) = $self->SUPER::size_request; |
624 | |
739 | |
625 | ( |
740 | ( |
626 | $w + $self->{border} * 2, |
741 | $w + $self->border * 2, |
627 | $h + $self->{border} * 2, |
742 | $h + $self->border * 2, |
628 | ) |
743 | ) |
629 | } |
744 | } |
630 | |
745 | |
631 | sub size_allocate { |
746 | sub size_allocate { |
632 | my ($self, $w, $h) = @_; |
747 | my ($self, $w, $h) = @_; |
633 | |
748 | |
634 | $h -= List::Util::max 0, $self->{border} * 2; |
749 | $h -= List::Util::max 0, $self->border * 2; |
635 | $w -= List::Util::max 0, $self->{border} * 2; |
750 | $w -= List::Util::max 0, $self->border * 2; |
636 | |
751 | |
|
|
752 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
753 | if $self->{title}; |
|
|
754 | |
637 | $self->child->configure ($self->{border}, $self->{border}, $w, $h); |
755 | $self->child->configure ($self->border, $self->border, $w, $h); |
638 | } |
756 | } |
639 | |
757 | |
640 | sub button_down { |
758 | sub button_down { |
641 | my ($self, $ev, $x, $y) = @_; |
759 | my ($self, $ev, $x, $y) = @_; |
642 | |
760 | |
|
|
761 | my $border = $self->border; |
|
|
762 | |
643 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
763 | if ($x < $self->{w} && $x >= $self->{w} - $border |
644 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
764 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
645 | |
765 | |
646 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
766 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
647 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
767 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
648 | |
768 | |
649 | $self->{motion} = sub { |
769 | $self->{motion} = sub { |
650 | my ($ev, $x, $y) = @_; |
770 | my ($ev, $x, $y) = @_; |
651 | |
771 | |
652 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
772 | ($x, $y) = ($ev->{x}, $ev->{y}); |
653 | |
773 | |
654 | $self->{user_w} = $bw + $x - $ox; |
774 | $self->{user_w} = $bw + $x - $ox; |
655 | $self->{user_h} = $bh + $y - $oy; |
775 | $self->{user_h} = $bh + $y - $oy; |
656 | $self->check_size; |
776 | $self->check_size; |
657 | }; |
777 | }; |
658 | |
778 | |
659 | } elsif ($x >= 0 && $x < $self->{w} |
779 | } elsif ($x >= 0 && $x < $self->{w} |
660 | && $y >= 0 && $y < $self->{border}) { |
780 | && $y >= 0 && $y < $border) { |
661 | |
781 | |
662 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
782 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
663 | my ($bx, $by) = ($self->{x}, $self->{y}); |
783 | my ($bx, $by) = ($self->{x}, $self->{y}); |
664 | |
784 | |
665 | $self->{motion} = sub { |
785 | $self->{motion} = sub { |
666 | my ($ev, $x, $y) = @_; |
786 | my ($ev, $x, $y) = @_; |
667 | |
787 | |
668 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
788 | ($x, $y) = ($ev->{x}, $ev->{y}); |
669 | |
789 | |
670 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
790 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
671 | $self->update; |
791 | $self->update; |
672 | }; |
792 | }; |
673 | } |
793 | } |
… | |
… | |
694 | glEnable GL_BLEND; |
814 | glEnable GL_BLEND; |
695 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
815 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
696 | glEnable GL_TEXTURE_2D; |
816 | glEnable GL_TEXTURE_2D; |
697 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
817 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
698 | |
818 | |
|
|
819 | my $border = $self->border; |
|
|
820 | |
699 | glColor @{ $self->{border_bg} }; |
821 | glColor @{ $self->{border_bg} }; |
700 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
822 | $tex[1]->draw_quad (0, 0, $w, $border); |
701 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
823 | $tex[3]->draw_quad (0, $border, $border, $ch); |
702 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
824 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
703 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
825 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
704 | |
826 | |
705 | my $bg = $tex[0]; |
827 | my $bg = $tex[0]; |
706 | |
828 | |
707 | # TODO: repeat texture not scale |
829 | # TODO: repeat texture not scale |
708 | my $rep_x = $cw / $bg->{w}; |
830 | my $rep_x = $cw / $bg->{w}; |
… | |
… | |
711 | glColor @{ $self->{bg} }; |
833 | glColor @{ $self->{bg} }; |
712 | |
834 | |
713 | $bg->{s} = $rep_x; |
835 | $bg->{s} = $rep_x; |
714 | $bg->{t} = $rep_y; |
836 | $bg->{t} = $rep_y; |
715 | $bg->{wrap_mode} = 1; |
837 | $bg->{wrap_mode} = 1; |
716 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
838 | $bg->draw_quad ($border, $border, $cw, $ch); |
717 | |
839 | |
718 | glDisable GL_TEXTURE_2D; |
840 | glDisable GL_TEXTURE_2D; |
719 | glDisable GL_BLEND; |
841 | glDisable GL_BLEND; |
720 | |
842 | |
|
|
843 | $self->{title}->draw if $self->{title}; |
721 | $self->child->draw; |
844 | $self->child->draw; |
722 | } |
845 | } |
723 | |
846 | |
724 | ############################################################################# |
847 | ############################################################################# |
725 | |
848 | |
… | |
… | |
727 | |
850 | |
728 | our @ISA = CFClient::UI::Base::; |
851 | our @ISA = CFClient::UI::Base::; |
729 | |
852 | |
730 | use List::Util qw(max sum); |
853 | use List::Util qw(max sum); |
731 | |
854 | |
732 | use SDL::OpenGL; |
855 | use CFClient::OpenGL; |
733 | |
856 | |
734 | sub new { |
857 | sub new { |
735 | my $class = shift; |
858 | my $class = shift; |
736 | |
859 | |
737 | $class->SUPER::new ( |
860 | $class->SUPER::new ( |
… | |
… | |
744 | my ($self, $x, $y, $child) = @_; |
867 | my ($self, $x, $y, $child) = @_; |
745 | |
868 | |
746 | $child->set_parent ($self); |
869 | $child->set_parent ($self); |
747 | $self->{children}[$y][$x] = $child; |
870 | $self->{children}[$y][$x] = $child; |
748 | |
871 | |
749 | $self->{w} = $self->{h} = -1; |
|
|
750 | |
|
|
751 | $child->check_size; |
872 | $child->check_size; |
752 | } |
873 | } |
753 | |
874 | |
754 | # TODO: move to container class maybe? send childs a signal on removal? |
875 | # TODO: move to container class maybe? send childs a signal on removal? |
755 | sub clear { |
876 | sub clear { |
… | |
… | |
769 | or next; |
890 | or next; |
770 | |
891 | |
771 | for my $x (0 .. $#$row) { |
892 | for my $x (0 .. $#$row) { |
772 | my $widget = $row->[$x] |
893 | my $widget = $row->[$x] |
773 | or next; |
894 | or next; |
774 | my ($w, $h) = $widget->size_request; |
895 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
775 | |
896 | |
776 | $w[$x] = max $w[$x], $w; |
897 | $w[$x] = max $w[$x], $w; |
777 | $h[$y] = max $h[$y], $h; |
898 | $h[$y] = max $h[$y], $h; |
778 | } |
899 | } |
779 | } |
900 | } |
… | |
… | |
884 | |
1005 | |
885 | ($h, $w) = ($w, $h); |
1006 | ($h, $w) = ($w, $h); |
886 | |
1007 | |
887 | my $children = $self->{children}; |
1008 | my $children = $self->{children}; |
888 | |
1009 | |
889 | my @h = map +($_->size_request)[0], @$children; |
1010 | my @h = map $_->{req_w}, @$children; |
890 | |
1011 | |
891 | my $req_h = List::Util::sum @h; |
1012 | my $req_h = List::Util::sum @h; |
892 | |
1013 | |
893 | if ($req_h > $h) { |
1014 | if ($req_h > $h) { |
894 | # ah well, not enough space |
1015 | # ah well, not enough space |
… | |
… | |
942 | sub size_allocate { |
1063 | sub size_allocate { |
943 | my ($self, $w, $h) = @_; |
1064 | my ($self, $w, $h) = @_; |
944 | |
1065 | |
945 | my $children = $self->{children}; |
1066 | my $children = $self->{children}; |
946 | |
1067 | |
947 | my @h = map +($_->size_request)[1], @$children; |
1068 | my @h = map $_->{req_h}, @$children; |
948 | |
1069 | |
949 | my $req_h = List::Util::sum @h; |
1070 | my $req_h = List::Util::sum @h; |
950 | |
1071 | |
951 | if ($req_h > $h) { |
1072 | if ($req_h > $h) { |
952 | # ah well, not enough space |
1073 | # ah well, not enough space |
… | |
… | |
980 | |
1101 | |
981 | package CFClient::UI::Label; |
1102 | package CFClient::UI::Label; |
982 | |
1103 | |
983 | our @ISA = CFClient::UI::Base::; |
1104 | our @ISA = CFClient::UI::Base::; |
984 | |
1105 | |
985 | use SDL::OpenGL; |
1106 | use CFClient::OpenGL; |
986 | |
1107 | |
987 | sub new { |
1108 | sub new { |
988 | my ($class, %arg) = @_; |
1109 | my ($class, %arg) = @_; |
989 | |
1110 | |
990 | my $self = $class->SUPER::new ( |
1111 | my $self = $class->SUPER::new ( |
991 | fg => [1, 1, 1], |
1112 | fg => [1, 1, 1], |
|
|
1113 | #font => default_font |
992 | fontsize => $::FONTSIZE, |
1114 | fontsize => 1, |
993 | text => "", |
1115 | text => "", |
994 | align => -1, |
1116 | align => -1, |
995 | valign => -1, |
1117 | valign => -1, |
996 | padding => 2, |
1118 | padding => 2, |
997 | layout => new CFClient::Layout, |
1119 | layout => new CFClient::Layout, |
|
|
1120 | can_events => 0, |
998 | %arg |
1121 | %arg |
999 | ); |
1122 | ); |
1000 | |
1123 | |
1001 | $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# |
1124 | if (exists $self->{template}) { |
|
|
1125 | my $layout = new CFClient::Layout; |
|
|
1126 | $layout->set_text (delete $self->{template}); |
|
|
1127 | $self->{template} = $layout; |
|
|
1128 | } |
1002 | |
1129 | |
1003 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1130 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1004 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1131 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1005 | |
1132 | |
1006 | $self |
1133 | $self |
… | |
… | |
1020 | my ($self, $text) = @_; |
1147 | my ($self, $text) = @_; |
1021 | |
1148 | |
1022 | $self->{layout}->set_text ($text); |
1149 | $self->{layout}->set_text ($text); |
1023 | |
1150 | |
1024 | delete $self->{texture}; |
1151 | delete $self->{texture}; |
|
|
1152 | $self->check_size; |
1025 | $self->update; |
1153 | $self->update; |
1026 | } |
1154 | } |
1027 | |
1155 | |
1028 | sub set_markup { |
1156 | sub set_markup { |
1029 | my ($self, $markup) = @_; |
1157 | my ($self, $markup) = @_; |
1030 | |
1158 | |
1031 | $self->{layout}->set_markup ($markup); |
1159 | $self->{layout}->set_markup ($markup); |
1032 | |
1160 | |
1033 | delete $self->{texture}; |
1161 | delete $self->{texture}; |
|
|
1162 | $self->check_size; |
1034 | $self->update; |
1163 | $self->update; |
1035 | } |
1164 | } |
1036 | |
1165 | |
1037 | sub size_request { |
1166 | sub size_request { |
1038 | my ($self) = @_; |
1167 | my ($self) = @_; |
1039 | |
1168 | |
1040 | $self->{layout}->set_width; |
1169 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1170 | $self->{layout}->set_width ($self->{max_w} || -1); |
1041 | $self->{layout}->set_height ($self->{fontsize}); |
1171 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1042 | |
1172 | |
1043 | my ($w, $h) = $self->{layout}->size; |
1173 | my ($w, $h) = $self->{layout}->size; |
|
|
1174 | |
|
|
1175 | if (exists $self->{template}) { |
|
|
1176 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
|
|
1177 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1178 | |
|
|
1179 | my ($w2, $h2) = $self->{template}->size; |
|
|
1180 | |
|
|
1181 | $w = List::Util::max $w, $w2; |
|
|
1182 | $h = List::Util::max $h, $h2; |
|
|
1183 | } |
1044 | |
1184 | |
1045 | ( |
1185 | ( |
1046 | $w + $self->{padding} * 2, |
1186 | $w + $self->{padding} * 2, |
1047 | $h + $self->{padding} * 2, |
1187 | $h + $self->{padding} * 2, |
1048 | ) |
1188 | ) |
… | |
… | |
1052 | my ($self, $w, $h) = @_; |
1192 | my ($self, $w, $h) = @_; |
1053 | |
1193 | |
1054 | delete $self->{texture}; |
1194 | delete $self->{texture}; |
1055 | } |
1195 | } |
1056 | |
1196 | |
1057 | sub update { |
1197 | sub set_fontsize { |
1058 | my ($self) = @_; |
1198 | my ($self, $fontsize) = @_; |
1059 | |
1199 | |
|
|
1200 | $self->{fontsize} = $fontsize; |
1060 | delete $self->{texture}; |
1201 | delete $self->{texture}; |
|
|
1202 | $self->check_size; |
1061 | $self->SUPER::update; |
1203 | $self->update; |
1062 | } |
1204 | } |
1063 | |
1205 | |
1064 | sub _draw { |
1206 | sub _draw { |
1065 | my ($self) = @_; |
1207 | my ($self) = @_; |
1066 | |
1208 | |
1067 | my $tex = $self->{texture} ||= do { |
1209 | my $tex = $self->{texture} ||= do { |
|
|
1210 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1068 | $self->{layout}->set_width ($self->{w}); |
1211 | $self->{layout}->set_width ($self->{w}); |
1069 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); |
1212 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1070 | new_from_layout CFClient::Texture $self->{layout} |
1213 | new_from_layout CFClient::Texture $self->{layout} |
1071 | }; |
1214 | }; |
1072 | |
1215 | |
1073 | glEnable GL_BLEND; |
1216 | glEnable GL_BLEND; |
1074 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1217 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
… | |
… | |
1099 | |
1242 | |
1100 | package CFClient::UI::EntryBase; |
1243 | package CFClient::UI::EntryBase; |
1101 | |
1244 | |
1102 | our @ISA = CFClient::UI::Label::; |
1245 | our @ISA = CFClient::UI::Label::; |
1103 | |
1246 | |
1104 | use SDL; |
1247 | use CFClient::OpenGL; |
1105 | use SDL::OpenGL; |
|
|
1106 | |
1248 | |
1107 | sub new { |
1249 | sub new { |
1108 | my $class = shift; |
1250 | my $class = shift; |
1109 | |
1251 | |
1110 | $class->SUPER::new ( |
1252 | $class->SUPER::new ( |
… | |
… | |
1113 | active_bg => [1, 1, 1, 0.5], |
1255 | active_bg => [1, 1, 1, 0.5], |
1114 | active_fg => [0, 0, 0], |
1256 | active_fg => [0, 0, 0], |
1115 | can_hover => 1, |
1257 | can_hover => 1, |
1116 | can_focus => 1, |
1258 | can_focus => 1, |
1117 | valign => 0, |
1259 | valign => 0, |
|
|
1260 | can_events => 1, |
1118 | @_ |
1261 | @_ |
1119 | ) |
1262 | ) |
1120 | } |
1263 | } |
1121 | |
1264 | |
1122 | sub _set_text { |
1265 | sub _set_text { |
… | |
… | |
1124 | |
1267 | |
1125 | delete $self->{cur_h}; |
1268 | delete $self->{cur_h}; |
1126 | |
1269 | |
1127 | return if $self->{text} eq $text; |
1270 | return if $self->{text} eq $text; |
1128 | |
1271 | |
|
|
1272 | delete $self->{texture}; |
|
|
1273 | |
1129 | $self->{last_activity} = $::NOW; |
1274 | $self->{last_activity} = $::NOW; |
1130 | $self->{text} = $text; |
1275 | $self->{text} = $text; |
1131 | |
1276 | |
1132 | $text =~ s/./*/g if $self->{hidden}; |
1277 | $text =~ s/./*/g if $self->{hidden}; |
1133 | $self->{layout}->set_text ("$text "); |
1278 | $self->{layout}->set_text ("$text "); |
… | |
… | |
1148 | } |
1293 | } |
1149 | |
1294 | |
1150 | sub size_allocate { |
1295 | sub size_allocate { |
1151 | my ($self, $w, $h) = @_; |
1296 | my ($self, $w, $h) = @_; |
1152 | |
1297 | |
1153 | $self->_set_text ($self->{text}); |
1298 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1154 | } |
1299 | } |
1155 | |
1300 | |
1156 | sub set_text { |
1301 | sub set_text { |
1157 | my ($self, $text) = @_; |
1302 | my ($self, $text) = @_; |
1158 | |
1303 | |
… | |
… | |
1162 | } |
1307 | } |
1163 | |
1308 | |
1164 | sub key_down { |
1309 | sub key_down { |
1165 | my ($self, $ev) = @_; |
1310 | my ($self, $ev) = @_; |
1166 | |
1311 | |
1167 | my $mod = $ev->key_mod; |
1312 | my $mod = $ev->{mod}; |
1168 | my $sym = $ev->key_sym; |
1313 | my $sym = $ev->{sym}; |
1169 | |
|
|
1170 | my $uni = $ev->key_unicode; |
1314 | my $uni = $ev->{unicode}; |
1171 | |
1315 | |
1172 | my $text = $self->get_text; |
1316 | my $text = $self->get_text; |
1173 | |
1317 | |
1174 | if ($sym == SDLK_BACKSPACE) { |
1318 | if ($sym == 8) { |
1175 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1319 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1176 | } elsif ($sym == SDLK_DELETE) { |
1320 | } elsif ($sym == 127) { |
1177 | substr $text, $self->{cursor}, 1, ""; |
1321 | substr $text, $self->{cursor}, 1, ""; |
1178 | } elsif ($sym == SDLK_LEFT) { |
1322 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1179 | --$self->{cursor} if $self->{cursor}; |
1323 | --$self->{cursor} if $self->{cursor}; |
1180 | } elsif ($sym == SDLK_RIGHT) { |
1324 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1181 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1325 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1182 | } elsif ($sym == SDLK_HOME) { |
1326 | } elsif ($sym == CFClient::SDLK_HOME) { |
1183 | $self->{cursor} = 0; |
1327 | $self->{cursor} = 0; |
1184 | } elsif ($sym == SDLK_END) { |
1328 | } elsif ($sym == CFClient::SDLK_END) { |
1185 | $self->{cursor} = length $text; |
1329 | $self->{cursor} = length $text; |
1186 | } elsif ($sym == SDLK_ESCAPE) { |
1330 | } elsif ($sym == 27) { |
1187 | $self->emit ('escape'); |
1331 | $self->emit ('escape'); |
1188 | } elsif ($uni) { |
1332 | } elsif ($uni) { |
1189 | substr $text, $self->{cursor}++, 0, chr $uni; |
1333 | substr $text, $self->{cursor}++, 0, chr $uni; |
1190 | } |
1334 | } |
1191 | |
1335 | |
… | |
… | |
1266 | |
1410 | |
1267 | package CFClient::UI::Entry; |
1411 | package CFClient::UI::Entry; |
1268 | |
1412 | |
1269 | our @ISA = CFClient::UI::EntryBase::; |
1413 | our @ISA = CFClient::UI::EntryBase::; |
1270 | |
1414 | |
1271 | use SDL; |
1415 | use CFClient::OpenGL; |
1272 | use SDL::OpenGL; |
|
|
1273 | |
1416 | |
1274 | sub key_down { |
1417 | sub key_down { |
1275 | my ($self, $ev) = @_; |
1418 | my ($self, $ev) = @_; |
1276 | |
1419 | |
1277 | my $sym = $ev->key_sym; |
1420 | my $sym = $ev->{sym}; |
1278 | |
1421 | |
1279 | if ($sym == SDLK_RETURN) { |
1422 | if ($sym == 13) { |
1280 | $self->emit (activate => $self->get_text); |
1423 | $self->emit (activate => $self->get_text); |
1281 | $self->update; |
1424 | $self->update; |
1282 | |
1425 | |
1283 | } else { |
1426 | } else { |
1284 | $self->SUPER::key_down ($ev); |
1427 | $self->SUPER::key_down ($ev); |
… | |
… | |
1290 | |
1433 | |
1291 | package CFClient::UI::Button; |
1434 | package CFClient::UI::Button; |
1292 | |
1435 | |
1293 | our @ISA = CFClient::UI::Label::; |
1436 | our @ISA = CFClient::UI::Label::; |
1294 | |
1437 | |
1295 | use SDL; |
1438 | use CFClient::OpenGL; |
1296 | use SDL::OpenGL; |
|
|
1297 | |
1439 | |
1298 | my @tex = |
1440 | my @tex = |
1299 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1441 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1300 | qw(b1_button_active.png); |
1442 | qw(b1_button_active.png); |
1301 | |
1443 | |
1302 | sub new { |
1444 | sub new { |
1303 | my $class = shift; |
1445 | my $class = shift; |
1304 | |
1446 | |
… | |
… | |
1308 | bg => [1, 1, 1, 0.2], |
1450 | bg => [1, 1, 1, 0.2], |
1309 | active_fg => [0, 0, 1], |
1451 | active_fg => [0, 0, 1], |
1310 | can_hover => 1, |
1452 | can_hover => 1, |
1311 | align => 0, |
1453 | align => 0, |
1312 | valign => 0, |
1454 | valign => 0, |
|
|
1455 | can_events => 1, |
1313 | @_ |
1456 | @_ |
1314 | ) |
1457 | ) |
1315 | } |
1458 | } |
1316 | |
1459 | |
1317 | sub button_up { |
1460 | sub button_up { |
… | |
… | |
1351 | package CFClient::UI::CheckBox; |
1494 | package CFClient::UI::CheckBox; |
1352 | |
1495 | |
1353 | our @ISA = CFClient::UI::DrawBG::; |
1496 | our @ISA = CFClient::UI::DrawBG::; |
1354 | |
1497 | |
1355 | my @tex = |
1498 | my @tex = |
1356 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1499 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1357 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1500 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1358 | |
1501 | |
1359 | use SDL; |
1502 | use CFClient::OpenGL; |
1360 | use SDL::OpenGL; |
|
|
1361 | |
1503 | |
1362 | sub new { |
1504 | sub new { |
1363 | my $class = shift; |
1505 | my $class = shift; |
1364 | |
1506 | |
1365 | $class->SUPER::new ( |
1507 | $class->SUPER::new ( |
… | |
… | |
1411 | glDisable GL_BLEND; |
1553 | glDisable GL_BLEND; |
1412 | } |
1554 | } |
1413 | |
1555 | |
1414 | ############################################################################# |
1556 | ############################################################################# |
1415 | |
1557 | |
1416 | package CFClient::UI::VGauge; |
1558 | package CFClient::UI::Image; |
1417 | |
1559 | |
1418 | our @ISA = CFClient::UI::Base::; |
1560 | our @ISA = CFClient::UI::Base::; |
1419 | |
1561 | |
1420 | use SDL::OpenGL; |
1562 | use CFClient::OpenGL; |
|
|
1563 | use Carp qw/confess/; |
1421 | |
1564 | |
1422 | my %tex = ( |
1565 | our %loaded_images; |
1423 | food => [ |
|
|
1424 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1425 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1426 | ], |
|
|
1427 | grace => [ |
|
|
1428 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1429 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1430 | ], |
|
|
1431 | hp => [ |
|
|
1432 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1433 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1434 | ], |
|
|
1435 | mana => [ |
|
|
1436 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1437 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1438 | ], |
|
|
1439 | ); |
|
|
1440 | |
1566 | |
1441 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1442 | sub new { |
1567 | sub new { |
1443 | my $class = shift; |
1568 | my $class = shift; |
1444 | |
1569 | |
1445 | my $self = $class->SUPER::new (gauge => 'food', @_); |
1570 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1571 | |
|
|
1572 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1573 | |
|
|
1574 | $loaded_images{$self->{image}} ||= |
|
|
1575 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1576 | |
|
|
1577 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1578 | |
|
|
1579 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1580 | |
|
|
1581 | $self->{aspect} = $tex->{w} / $tex->{h}; |
1446 | |
1582 | |
1447 | $self |
1583 | $self |
1448 | } |
1584 | } |
1449 | |
1585 | |
1450 | sub size_request { |
1586 | sub size_request { |
1451 | my ($self) = @_; |
1587 | my ($self) = @_; |
1452 | |
1588 | |
1453 | my $tex = $tex{$self->{gauge}}[0]; |
1589 | ($self->{tex}->{w}, $self->{tex}->{h}) |
1454 | |
|
|
1455 | @$tex{qw(w h)} |
|
|
1456 | } |
|
|
1457 | |
|
|
1458 | sub set_max { |
|
|
1459 | my ($self, $max) = @_; |
|
|
1460 | |
|
|
1461 | $self->{max_val} = $max; |
|
|
1462 | } |
|
|
1463 | |
|
|
1464 | sub set_value { |
|
|
1465 | my ($self, $val, $max) = @_; |
|
|
1466 | |
|
|
1467 | $self->set_max ($max) |
|
|
1468 | if defined $max; |
|
|
1469 | |
|
|
1470 | $max = $self->{max_val}; |
|
|
1471 | $self->{val} = $val; |
|
|
1472 | |
|
|
1473 | $self->update; |
|
|
1474 | } |
1590 | } |
1475 | |
1591 | |
1476 | sub _draw { |
1592 | sub _draw { |
1477 | my ($self) = @_; |
1593 | my ($self) = @_; |
1478 | |
1594 | |
1479 | my $tex = $tex{$self->{gauge}}; |
1595 | my $tex = $self->{tex}; |
1480 | |
1596 | |
1481 | my ($w, $h) = ($self->{w}, $self->{h}); |
1597 | my ($w, $h) = ($self->{w}, $self->{h}); |
1482 | |
1598 | |
1483 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
1599 | if ($self->{rot90}) { |
1484 | $ycut = 1 if $self->{val} > $self->{max_val}; |
1600 | glRotate 90, 0, 0, 1; |
|
|
1601 | glTranslate 0, -$self->{w}, 0; |
1485 | |
1602 | |
1486 | my $t1 = $tex->[0]; |
1603 | ($w, $h) = ($h, $w); |
1487 | my $t2 = $tex->[1]; |
1604 | } |
1488 | |
1605 | |
1489 | glEnable GL_BLEND; |
1606 | glEnable GL_BLEND; |
1490 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1607 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1491 | glEnable GL_TEXTURE_2D; |
1608 | glEnable GL_TEXTURE_2D; |
1492 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1609 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1493 | |
1610 | |
1494 | my $h1 = $self->{h} - $ycut * $self->{h}; |
1611 | $tex->draw_quad (0, 0, $w, $h); |
1495 | my $h2 = $ycut * $self->{h}; |
|
|
1496 | |
|
|
1497 | my $yp = 0; |
|
|
1498 | |
|
|
1499 | glBindTexture (GL_TEXTURE_2D, $t1->{name}); |
|
|
1500 | glBegin (GL_QUADS); |
|
|
1501 | glTexCoord (0, 0); glVertex (0 , $yp); |
|
|
1502 | glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); |
|
|
1503 | glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); |
|
|
1504 | glTexCoord (1, 0); glVertex (0 + $w, $yp); |
|
|
1505 | glEnd (); |
|
|
1506 | |
|
|
1507 | $yp += $h1; |
|
|
1508 | |
|
|
1509 | glBindTexture (GL_TEXTURE_2D, $t2->{name}); |
|
|
1510 | glBegin (GL_QUADS); |
|
|
1511 | glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); |
|
|
1512 | glTexCoord (0, 1); glVertex (0 , $yp + $h2); |
|
|
1513 | glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); |
|
|
1514 | glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); |
|
|
1515 | glEnd (); |
|
|
1516 | |
1612 | |
1517 | glDisable GL_BLEND; |
1613 | glDisable GL_BLEND; |
1518 | glDisable GL_TEXTURE_2D; |
1614 | glDisable GL_TEXTURE_2D; |
1519 | } |
1615 | } |
1520 | |
1616 | |
1521 | ############################################################################# |
1617 | ############################################################################# |
1522 | |
1618 | |
|
|
1619 | package CFClient::UI::VGauge; |
|
|
1620 | |
|
|
1621 | our @ISA = CFClient::UI::Base::; |
|
|
1622 | |
|
|
1623 | use List::Util qw(min max); |
|
|
1624 | |
|
|
1625 | use CFClient::OpenGL; |
|
|
1626 | |
|
|
1627 | my %tex = ( |
|
|
1628 | food => [ |
|
|
1629 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1630 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1631 | ], |
|
|
1632 | grace => [ |
|
|
1633 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1634 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
|
|
1635 | ], |
|
|
1636 | hp => [ |
|
|
1637 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1638 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1639 | ], |
|
|
1640 | mana => [ |
|
|
1641 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1642 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
|
|
1643 | ], |
|
|
1644 | ); |
|
|
1645 | |
|
|
1646 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1647 | sub new { |
|
|
1648 | my $class = shift; |
|
|
1649 | |
|
|
1650 | my $self = $class->SUPER::new ( |
|
|
1651 | type => 'food', |
|
|
1652 | @_ |
|
|
1653 | ); |
|
|
1654 | |
|
|
1655 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1656 | |
|
|
1657 | $self |
|
|
1658 | } |
|
|
1659 | |
|
|
1660 | sub size_request { |
|
|
1661 | my ($self) = @_; |
|
|
1662 | |
|
|
1663 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1664 | #@$tex{qw(w h)} |
|
|
1665 | (0, 0) |
|
|
1666 | } |
|
|
1667 | |
|
|
1668 | sub set_max { |
|
|
1669 | my ($self, $max) = @_; |
|
|
1670 | |
|
|
1671 | $self->{max_val} = $max; |
|
|
1672 | } |
|
|
1673 | |
|
|
1674 | sub set_value { |
|
|
1675 | my ($self, $val, $max) = @_; |
|
|
1676 | |
|
|
1677 | $self->set_max ($max) |
|
|
1678 | if defined $max; |
|
|
1679 | |
|
|
1680 | $max = $self->{max_val}; |
|
|
1681 | $self->{val} = $val; |
|
|
1682 | |
|
|
1683 | $self->update; |
|
|
1684 | } |
|
|
1685 | |
|
|
1686 | sub _draw { |
|
|
1687 | my ($self) = @_; |
|
|
1688 | |
|
|
1689 | my $tex = $tex{$self->{type}}; |
|
|
1690 | my ($t1, $t2, $t3) = @$tex; |
|
|
1691 | |
|
|
1692 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1693 | |
|
|
1694 | if ($self->{vertical}) { |
|
|
1695 | glRotate 90, 0, 0, 1; |
|
|
1696 | glTranslate 0, -$self->{w}, 0; |
|
|
1697 | |
|
|
1698 | ($w, $h) = ($h, $w); |
|
|
1699 | } |
|
|
1700 | |
|
|
1701 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1702 | |
|
|
1703 | my $ycut1 = max 0, min 1, $ycut; |
|
|
1704 | my $ycut2 = max 0, min 1, $ycut - 1; |
|
|
1705 | |
|
|
1706 | my $h1 = $self->{h} * (1 - $ycut1); |
|
|
1707 | my $h2 = $self->{h} * (1 - $ycut2); |
|
|
1708 | |
|
|
1709 | glEnable GL_BLEND; |
|
|
1710 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1711 | glEnable GL_TEXTURE_2D; |
|
|
1712 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1713 | |
|
|
1714 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1715 | glBegin GL_QUADS; |
|
|
1716 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1717 | glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1718 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1719 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
|
|
1720 | glEnd; |
|
|
1721 | |
|
|
1722 | my $ycut1 = List::Util::min 1, $ycut; |
|
|
1723 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1724 | glBegin GL_QUADS; |
|
|
1725 | glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1726 | glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1727 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1728 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1729 | glEnd; |
|
|
1730 | |
|
|
1731 | if ($t3) { |
|
|
1732 | glBindTexture GL_TEXTURE_2D, $t3->{name}; |
|
|
1733 | glBegin GL_QUADS; |
|
|
1734 | glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1735 | glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; |
|
|
1736 | glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; |
|
|
1737 | glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1738 | glEnd; |
|
|
1739 | } |
|
|
1740 | |
|
|
1741 | glDisable GL_BLEND; |
|
|
1742 | glDisable GL_TEXTURE_2D; |
|
|
1743 | } |
|
|
1744 | |
|
|
1745 | ############################################################################# |
|
|
1746 | |
|
|
1747 | package CFClient::UI::Gauge; |
|
|
1748 | |
|
|
1749 | our @ISA = CFClient::UI::VBox::; |
|
|
1750 | |
|
|
1751 | sub new { |
|
|
1752 | my ($class, %arg) = @_; |
|
|
1753 | |
|
|
1754 | my $self = $class->SUPER::new ( |
|
|
1755 | tooltip => $arg{type}, |
|
|
1756 | can_events => 1, |
|
|
1757 | can_hover => 1, |
|
|
1758 | %arg, |
|
|
1759 | ); |
|
|
1760 | |
|
|
1761 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); |
|
|
1762 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
|
|
1763 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); |
|
|
1764 | |
|
|
1765 | $self |
|
|
1766 | } |
|
|
1767 | |
|
|
1768 | sub set_fontsize { |
|
|
1769 | my ($self, $fsize) = @_; |
|
|
1770 | |
|
|
1771 | $self->{value}->set_fontsize ($fsize); |
|
|
1772 | $self->{max} ->set_fontsize ($fsize); |
|
|
1773 | } |
|
|
1774 | |
|
|
1775 | sub set_value { |
|
|
1776 | my ($self, $val, $max) = @_; |
|
|
1777 | |
|
|
1778 | $self->set_max ($max) |
|
|
1779 | if defined $max; |
|
|
1780 | |
|
|
1781 | $self->{gauge}->set_value ($val, $max); |
|
|
1782 | $self->{value}->set_text ($val); |
|
|
1783 | } |
|
|
1784 | |
|
|
1785 | sub set_max { |
|
|
1786 | my ($self, $max) = @_; |
|
|
1787 | |
|
|
1788 | $self->{gauge}->set_max ($max); |
|
|
1789 | $self->{max}->set_text ($max); |
|
|
1790 | } |
|
|
1791 | |
|
|
1792 | ############################################################################# |
|
|
1793 | |
1523 | package CFClient::UI::Slider; |
1794 | package CFClient::UI::Slider; |
1524 | |
1795 | |
1525 | use strict; |
1796 | use strict; |
1526 | |
1797 | |
1527 | use SDL::OpenGL; |
1798 | use CFClient::OpenGL; |
1528 | |
1799 | |
1529 | our @ISA = CFClient::UI::DrawBG::; |
1800 | our @ISA = CFClient::UI::DrawBG::; |
1530 | |
1801 | |
1531 | my @tex = |
1802 | my @tex = |
1532 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1803 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1542 | # TODO: calculations are off |
1813 | # TODO: calculations are off |
1543 | my $self = $class->SUPER::new ( |
1814 | my $self = $class->SUPER::new ( |
1544 | fg => [1, 1, 1], |
1815 | fg => [1, 1, 1], |
1545 | active_fg => [0, 0, 0], |
1816 | active_fg => [0, 0, 0], |
1546 | range => [0, 0, 100, 10], |
1817 | range => [0, 0, 100, 10], |
1547 | req_w => 40, |
1818 | req_w => 20, |
1548 | req_h => 13, |
1819 | req_h => 20, |
1549 | vertical => 0, |
1820 | vertical => 0, |
1550 | can_hover => 1, |
1821 | can_hover => 1, |
1551 | inner_pad => 5, |
1822 | inner_pad => 5, |
1552 | @_ |
1823 | @_ |
1553 | ); |
1824 | ); |
… | |
… | |
1657 | |
1928 | |
1658 | package CFClient::UI::TextView; |
1929 | package CFClient::UI::TextView; |
1659 | |
1930 | |
1660 | our @ISA = CFClient::UI::HBox::; |
1931 | our @ISA = CFClient::UI::HBox::; |
1661 | |
1932 | |
1662 | use SDL::OpenGL; |
1933 | use CFClient::OpenGL; |
1663 | |
1934 | |
1664 | sub new { |
1935 | sub new { |
1665 | my $class = shift; |
1936 | my $class = shift; |
1666 | |
1937 | |
1667 | my $self = $class->SUPER::new ( |
1938 | my $self = $class->SUPER::new ( |
1668 | req_w => $::WIDTH / 6, |
|
|
1669 | req_h => $::HEIGHT / 6, |
|
|
1670 | fontsize => $::FONTSIZE, |
1939 | fontsize => 1, |
|
|
1940 | #font => default_font |
1671 | @_, |
1941 | @_, |
1672 | |
1942 | |
1673 | layout => (new CFClient::Layout), |
1943 | layout => (new CFClient::Layout), |
1674 | par => [], |
1944 | par => [], |
1675 | height => 0, |
1945 | height => 0, |
… | |
… | |
1696 | sub text_height { |
1966 | sub text_height { |
1697 | my ($self, $text) = @_; |
1967 | my ($self, $text) = @_; |
1698 | |
1968 | |
1699 | my $layout = $self->{layout}; |
1969 | my $layout = $self->{layout}; |
1700 | |
1970 | |
1701 | $layout->set_height ($self->{fontsize}); |
1971 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1702 | $layout->set_width ($self->{w}); |
1972 | $layout->set_width ($self->{w}); |
1703 | $layout->set_text ($text); |
1973 | $layout->set_text ($text); |
1704 | |
1974 | |
1705 | ($layout->size)[1] |
1975 | ($layout->size)[1] |
1706 | } |
1976 | } |
… | |
… | |
1710 | |
1980 | |
1711 | $self->{need_reflow}++; |
1981 | $self->{need_reflow}++; |
1712 | $self->update; |
1982 | $self->update; |
1713 | } |
1983 | } |
1714 | |
1984 | |
1715 | sub size_request { |
|
|
1716 | my ($self) = @_; |
|
|
1717 | |
|
|
1718 | ($self->{req_w}, $self->{req_h}) |
|
|
1719 | } |
|
|
1720 | |
|
|
1721 | sub size_allocate { |
1985 | sub size_allocate { |
1722 | my ($self, $w, $h) = @_; |
1986 | my ($self, $w, $h) = @_; |
1723 | |
1987 | |
|
|
1988 | $self->SUPER::size_allocate ($w, $h); |
|
|
1989 | |
|
|
1990 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1724 | $self->{layout}->set_height ($self->{fontsize}); |
1991 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1725 | $self->{layout}->set_width ($self->{w}); |
1992 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1726 | |
1993 | |
1727 | $self->reflow; |
1994 | $self->reflow; |
1728 | $self->update; |
|
|
1729 | } |
1995 | } |
1730 | |
1996 | |
1731 | sub add_paragraph { |
1997 | sub add_paragraph { |
1732 | my ($self, $color, $text) = @_; |
1998 | my ($self, $color, $text) = @_; |
1733 | |
1999 | |
… | |
… | |
1781 | my $y1 = $top + $self->{h}; |
2047 | my $y1 = $top + $self->{h}; |
1782 | |
2048 | |
1783 | my $y = 0; |
2049 | my $y = 0; |
1784 | |
2050 | |
1785 | my $layout = $self->{layout}; |
2051 | my $layout = $self->{layout}; |
|
|
2052 | |
|
|
2053 | $layout->set_font ($self->{font}) if $self->{font}; |
1786 | |
2054 | |
1787 | for my $par (@{$self->{par}}) { |
2055 | for my $par (@{$self->{par}}) { |
1788 | my $h = $par->[0]; |
2056 | my $h = $par->[0]; |
1789 | |
2057 | |
1790 | if ($y0 < $y + $h && $y < $y1) { |
2058 | if ($y0 < $y + $h && $y < $y1) { |
… | |
… | |
1818 | |
2086 | |
1819 | } |
2087 | } |
1820 | |
2088 | |
1821 | ############################################################################# |
2089 | ############################################################################# |
1822 | |
2090 | |
1823 | package CFClient::UI::MapWidget; |
|
|
1824 | |
|
|
1825 | use strict; |
|
|
1826 | |
|
|
1827 | use List::Util qw(min max); |
|
|
1828 | |
|
|
1829 | use SDL; |
|
|
1830 | use SDL::OpenGL; |
|
|
1831 | |
|
|
1832 | our @ISA = CFClient::UI::Base::; |
|
|
1833 | |
|
|
1834 | sub new { |
|
|
1835 | my $class = shift; |
|
|
1836 | |
|
|
1837 | $class->SUPER::new ( |
|
|
1838 | z => -1, |
|
|
1839 | can_focus => 1, |
|
|
1840 | list => (glGenLists 1), |
|
|
1841 | @_ |
|
|
1842 | ) |
|
|
1843 | } |
|
|
1844 | |
|
|
1845 | sub key_down { |
|
|
1846 | print "MAPKEYDOWN\n"; |
|
|
1847 | } |
|
|
1848 | |
|
|
1849 | sub key_up { |
|
|
1850 | } |
|
|
1851 | |
|
|
1852 | sub button_down { |
|
|
1853 | my ($self, $ev, $x, $y) = @_; |
|
|
1854 | |
|
|
1855 | $self->focus_in; |
|
|
1856 | |
|
|
1857 | if ($ev->button == 2) { |
|
|
1858 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1859 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1860 | |
|
|
1861 | $self->{motion} = sub { |
|
|
1862 | my ($ev, $x, $y) = @_; |
|
|
1863 | |
|
|
1864 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1865 | |
|
|
1866 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1867 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1868 | |
|
|
1869 | $self->update; |
|
|
1870 | }; |
|
|
1871 | } |
|
|
1872 | } |
|
|
1873 | |
|
|
1874 | sub button_up { |
|
|
1875 | my ($self, $ev, $x, $y) = @_; |
|
|
1876 | |
|
|
1877 | delete $self->{motion}; |
|
|
1878 | } |
|
|
1879 | |
|
|
1880 | sub mouse_motion { |
|
|
1881 | my ($self, $ev, $x, $y) = @_; |
|
|
1882 | |
|
|
1883 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1884 | } |
|
|
1885 | |
|
|
1886 | sub size_request { |
|
|
1887 | ( |
|
|
1888 | 1 + 32 * int $::WIDTH / 32, |
|
|
1889 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1890 | ) |
|
|
1891 | } |
|
|
1892 | |
|
|
1893 | sub update { |
|
|
1894 | my ($self) = @_; |
|
|
1895 | |
|
|
1896 | $self->{need_update} = 1; |
|
|
1897 | $self->SUPER::update; |
|
|
1898 | } |
|
|
1899 | |
|
|
1900 | sub draw { |
|
|
1901 | my ($self) = @_; |
|
|
1902 | |
|
|
1903 | if (delete $self->{need_update}) { |
|
|
1904 | glNewList $self->{list}, GL_COMPILE; |
|
|
1905 | |
|
|
1906 | if ($::MAP) { |
|
|
1907 | my $sw = int $::WIDTH / 32; |
|
|
1908 | my $sh = int $::HEIGHT / 32; |
|
|
1909 | |
|
|
1910 | my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32; |
|
|
1911 | my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32; |
|
|
1912 | |
|
|
1913 | glTranslate $sx0 - 32, $sy0 - 32, 0; |
|
|
1914 | |
|
|
1915 | my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1); |
|
|
1916 | |
|
|
1917 | if ($::CFG->{fow_enable}) { |
|
|
1918 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1919 | glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER); |
|
|
1920 | glConvolutionFilter2D ( |
|
|
1921 | GL_CONVOLUTION_2D, |
|
|
1922 | GL_ALPHA, |
|
|
1923 | 3, 3, |
|
|
1924 | GL_ALPHA, GL_FLOAT, |
|
|
1925 | pack "f*", |
|
|
1926 | 0.1, 0.1, 0.1, |
|
|
1927 | 0.1, 0.2, 0.1, |
|
|
1928 | 0.1, 0.1, 0.1, |
|
|
1929 | ); |
|
|
1930 | glEnable GL_CONVOLUTION_2D; |
|
|
1931 | } |
|
|
1932 | |
|
|
1933 | $self->{fow_texture} = new CFClient::Texture |
|
|
1934 | w => $w, |
|
|
1935 | h => $h, |
|
|
1936 | data => $data, |
|
|
1937 | internalformat => GL_ALPHA, |
|
|
1938 | format => GL_ALPHA; |
|
|
1939 | |
|
|
1940 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1941 | |
|
|
1942 | glEnable GL_BLEND; |
|
|
1943 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1944 | glEnable GL_TEXTURE_2D; |
|
|
1945 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1946 | |
|
|
1947 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1948 | $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1949 | |
|
|
1950 | glDisable GL_TEXTURE_2D; |
|
|
1951 | glDisable GL_BLEND; |
|
|
1952 | } |
|
|
1953 | |
|
|
1954 | # HACK BEGIN |
|
|
1955 | { |
|
|
1956 | glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove |
|
|
1957 | my ($w, $h) = (250, 250); |
|
|
1958 | |
|
|
1959 | glEnable GL_BLEND; |
|
|
1960 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1961 | glEnable GL_TEXTURE_2D; |
|
|
1962 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1963 | |
|
|
1964 | $self->{mapmap_texture} = |
|
|
1965 | new CFClient::Texture |
|
|
1966 | w => $w, |
|
|
1967 | h => $h, |
|
|
1968 | data => $::MAP->mapmap ($w, $h), |
|
|
1969 | type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
|
|
1970 | |
|
|
1971 | $self->{mapmap_texture}->draw_quad (100, 100); |
|
|
1972 | |
|
|
1973 | glDisable GL_TEXTURE_2D; |
|
|
1974 | glDisable GL_BLEND; |
|
|
1975 | } |
|
|
1976 | # HACK END |
|
|
1977 | } |
|
|
1978 | |
|
|
1979 | glEndList; |
|
|
1980 | } |
|
|
1981 | |
|
|
1982 | glPushMatrix; |
|
|
1983 | glCallList $self->{list}; |
|
|
1984 | glPopMatrix; |
|
|
1985 | |
|
|
1986 | if ($FOCUS != $self) { |
|
|
1987 | glColor 64/255, 64/255, 64/255; |
|
|
1988 | glLogicOp GL_AND; |
|
|
1989 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1990 | glBegin GL_QUADS; |
|
|
1991 | glVertex 0, 0; |
|
|
1992 | glVertex 0, $::HEIGHT; |
|
|
1993 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1994 | glVertex $::WIDTH, 0; |
|
|
1995 | glEnd; |
|
|
1996 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1997 | } |
|
|
1998 | } |
|
|
1999 | |
|
|
2000 | my %DIR = ( |
|
|
2001 | SDLK_KP8, [1, "north"], |
|
|
2002 | SDLK_KP9, [2, "northeast"], |
|
|
2003 | SDLK_KP6, [3, "east"], |
|
|
2004 | SDLK_KP3, [4, "southeast"], |
|
|
2005 | SDLK_KP2, [5, "south"], |
|
|
2006 | SDLK_KP1, [6, "southwest"], |
|
|
2007 | SDLK_KP4, [7, "west"], |
|
|
2008 | SDLK_KP7, [8, "northwest"], |
|
|
2009 | |
|
|
2010 | SDLK_UP, [1, "north"], |
|
|
2011 | SDLK_RIGHT, [3, "east"], |
|
|
2012 | SDLK_DOWN, [5, "south"], |
|
|
2013 | SDLK_LEFT, [7, "west"], |
|
|
2014 | ); |
|
|
2015 | |
|
|
2016 | sub key_down { |
|
|
2017 | my ($self, $ev) = @_; |
|
|
2018 | |
|
|
2019 | my $mod = $ev->key_mod; |
|
|
2020 | my $sym = $ev->key_sym; |
|
|
2021 | |
|
|
2022 | if ($sym == SDLK_KP5) { |
|
|
2023 | $::CONN->user_send ("stay fire"); |
|
|
2024 | } elsif ($sym == SDLK_a) { |
|
|
2025 | $::CONN->user_send ("apply"); |
|
|
2026 | } elsif ($sym == SDLK_QUOTE) { |
|
|
2027 | $self->emit ('activate_console'); |
|
|
2028 | } elsif ($sym == SDLK_SLASH) { |
|
|
2029 | $self->emit ('activate_console' => '/'); |
|
|
2030 | } elsif (exists $DIR{$sym}) { |
|
|
2031 | if ($mod & KMOD_SHIFT) { |
|
|
2032 | $self->{shft}++; |
|
|
2033 | $::CONN->user_send ("fire $DIR{$sym}[0]"); |
|
|
2034 | } elsif ($mod & KMOD_CTRL) { |
|
|
2035 | $self->{ctrl}++; |
|
|
2036 | $::CONN->user_send ("run $DIR{$sym}[0]"); |
|
|
2037 | } else { |
|
|
2038 | $::CONN->user_send ("$DIR{$sym}[1]"); |
|
|
2039 | } |
|
|
2040 | } |
|
|
2041 | } |
|
|
2042 | |
|
|
2043 | sub key_up { |
|
|
2044 | my ($self, $ev) = @_; |
|
|
2045 | |
|
|
2046 | my $mod = $ev->key_mod; |
|
|
2047 | my $sym = $ev->key_sym; |
|
|
2048 | |
|
|
2049 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
2050 | $::CONN->user_send ("fire_stop"); |
|
|
2051 | } |
|
|
2052 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
2053 | $::CONN->user_send ("run_stop"); |
|
|
2054 | } |
|
|
2055 | } |
|
|
2056 | |
|
|
2057 | ############################################################################# |
|
|
2058 | |
|
|
2059 | package CFClient::UI::Animator; |
2091 | package CFClient::UI::Animator; |
2060 | |
2092 | |
2061 | use SDL::OpenGL; |
2093 | use CFClient::OpenGL; |
2062 | |
2094 | |
2063 | our @ISA = CFClient::UI::Bin::; |
2095 | our @ISA = CFClient::UI::Bin::; |
2064 | |
2096 | |
2065 | sub moveto { |
2097 | sub moveto { |
2066 | my ($self, $x, $y) = @_; |
2098 | my ($self, $x, $y) = @_; |
… | |
… | |
2106 | my $class = shift; |
2138 | my $class = shift; |
2107 | |
2139 | |
2108 | my $self = $class->SUPER::new ( |
2140 | my $self = $class->SUPER::new ( |
2109 | state => 0, |
2141 | state => 0, |
2110 | connect_activate => \&toggle_flopper, |
2142 | connect_activate => \&toggle_flopper, |
|
|
2143 | can_events => 1, |
2111 | @_ |
2144 | @_ |
2112 | ); |
2145 | ); |
2113 | |
2146 | |
2114 | if ($self->{state}) { |
2147 | if ($self->{state}) { |
2115 | $self->{state} = 0; |
2148 | $self->{state} = 0; |
… | |
… | |
2135 | $self->emit (changed => $self->{state}); |
2168 | $self->emit (changed => $self->{state}); |
2136 | } |
2169 | } |
2137 | |
2170 | |
2138 | ############################################################################# |
2171 | ############################################################################# |
2139 | |
2172 | |
|
|
2173 | package CFClient::UI::Tooltip; |
|
|
2174 | |
|
|
2175 | our @ISA = CFClient::UI::Bin::; |
|
|
2176 | |
|
|
2177 | use CFClient::OpenGL; |
|
|
2178 | |
|
|
2179 | sub new { |
|
|
2180 | my $class = shift; |
|
|
2181 | |
|
|
2182 | $class->SUPER::new ( |
|
|
2183 | @_, |
|
|
2184 | can_events => 0, |
|
|
2185 | ) |
|
|
2186 | } |
|
|
2187 | |
|
|
2188 | sub set_text { |
|
|
2189 | my ($self, $text) = @_; |
|
|
2190 | |
|
|
2191 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
|
|
2192 | $self->{label}->set_text ($text); |
|
|
2193 | $self->add ($self->{label}); |
|
|
2194 | } |
|
|
2195 | |
|
|
2196 | sub size_request { |
|
|
2197 | my ($self) = @_; |
|
|
2198 | |
|
|
2199 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2200 | |
|
|
2201 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2202 | |
|
|
2203 | ($w + 4, $h + 4) |
|
|
2204 | } |
|
|
2205 | |
|
|
2206 | sub _draw { |
|
|
2207 | my ($self) = @_; |
|
|
2208 | |
|
|
2209 | glPushMatrix; |
|
|
2210 | glTranslate 0.375, 0.375; |
|
|
2211 | |
|
|
2212 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2213 | |
|
|
2214 | glColor 1, 0.8, 0.4; |
|
|
2215 | glBegin GL_QUADS; |
|
|
2216 | glVertex 0 , 0; |
|
|
2217 | glVertex 0 , $h; |
|
|
2218 | glVertex $w, $h; |
|
|
2219 | glVertex $w, 0; |
|
|
2220 | glEnd; |
|
|
2221 | |
|
|
2222 | glColor 0, 0, 0; |
|
|
2223 | glBegin GL_LINE_LOOP; |
|
|
2224 | glVertex 0 , 0; |
|
|
2225 | glVertex 0 , $h; |
|
|
2226 | glVertex $w, $h; |
|
|
2227 | glVertex $w, 0; |
|
|
2228 | glEnd; |
|
|
2229 | |
|
|
2230 | glPopMatrix; |
|
|
2231 | |
|
|
2232 | glTranslate 2, 2; |
|
|
2233 | $self->SUPER::_draw; |
|
|
2234 | } |
|
|
2235 | |
|
|
2236 | ############################################################################# |
|
|
2237 | |
2140 | package CFClient::UI::Root; |
2238 | package CFClient::UI::Root; |
2141 | |
2239 | |
2142 | our @ISA = CFClient::UI::Container::; |
2240 | our @ISA = CFClient::UI::Container::; |
2143 | |
2241 | |
2144 | use SDL::OpenGL; |
2242 | use CFClient::OpenGL; |
2145 | |
2243 | |
2146 | sub check_size { |
2244 | sub check_size { |
2147 | my ($self) = @_; |
2245 | my ($self) = @_; |
2148 | |
2246 | |
2149 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
2247 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
2150 | } |
2248 | } |
2151 | |
2249 | |
2152 | sub size_request { |
2250 | sub size_request { |
2153 | ($::WIDTH, $::HEIGHT) |
2251 | ($::WIDTH, $::HEIGHT) |
2154 | } |
2252 | } |
… | |
… | |
2156 | sub configure { |
2254 | sub configure { |
2157 | my ($self, $x, $y, $w, $h) = @_; |
2255 | my ($self, $x, $y, $w, $h) = @_; |
2158 | |
2256 | |
2159 | $self->SUPER::configure ($x, $y, $w, $h); |
2257 | $self->SUPER::configure ($x, $y, $w, $h); |
2160 | |
2258 | |
2161 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
|
|
2162 | for @{$self->{children}}; |
2259 | for my $child (@{$self->{children}}) { |
|
|
2260 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2261 | |
|
|
2262 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2263 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2264 | $child->configure ($X, $Y, $W,$H); |
|
|
2265 | } |
2163 | } |
2266 | } |
2164 | |
2267 | |
2165 | sub _topleft { |
2268 | sub _topleft { |
2166 | my ($self, $x, $y) = @_; |
2269 | my ($self, $x, $y) = @_; |
2167 | |
2270 | |
… | |
… | |
2175 | ::refresh (); |
2278 | ::refresh (); |
2176 | } |
2279 | } |
2177 | |
2280 | |
2178 | sub add { |
2281 | sub add { |
2179 | my ($self, $child) = @_; |
2282 | my ($self, $child) = @_; |
|
|
2283 | |
|
|
2284 | # integerize window positions |
|
|
2285 | $child->{x} = int $child->{x}; |
|
|
2286 | $child->{y} = int $child->{y}; |
2180 | |
2287 | |
2181 | $self->SUPER::add ($child); |
2288 | $self->SUPER::add ($child); |
2182 | } |
2289 | } |
2183 | |
2290 | |
2184 | sub on_refresh { |
2291 | sub on_refresh { |
… | |
… | |
2210 | ############################################################################# |
2317 | ############################################################################# |
2211 | |
2318 | |
2212 | package CFClient::UI; |
2319 | package CFClient::UI; |
2213 | |
2320 | |
2214 | $ROOT = new CFClient::UI::Root; |
2321 | $ROOT = new CFClient::UI::Root; |
|
|
2322 | $TOOLTIP = new CFClient::UI::Tooltip; |
2215 | |
2323 | |
2216 | 1 |
2324 | 1 |
2217 | |
2325 | |