… | |
… | |
7 | |
7 | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $TOPLEVEL; |
12 | our $ROOT; |
13 | our $BUTTON_STATE; |
13 | our $BUTTON_STATE; |
14 | |
14 | |
15 | # class methods for events |
15 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
16 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
… | |
… | |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
22 | } |
22 | } |
23 | |
23 | |
24 | sub feed_sdl_button_down_event { |
24 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
25 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
26 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
27 | |
28 | if (!$BUTTON_STATE) { |
28 | if (!$BUTTON_STATE) { |
29 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
29 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
30 | |
31 | $GRAB = $widget; |
31 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
32 | $GRAB->update if $GRAB; |
33 | } |
33 | } |
34 | |
34 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
35 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
36 | |
37 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
37 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
38 | } |
38 | } |
39 | |
39 | |
40 | sub feed_sdl_button_up_event { |
40 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
41 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
42 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
43 | |
44 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
44 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
45 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
46 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
47 | |
48 | $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
48 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
49 | |
49 | |
50 | if (!$BUTTON_STATE) { |
50 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
51 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
52 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
53 | $GRAB->update if $GRAB; |
54 | } |
54 | } |
55 | } |
55 | } |
56 | |
56 | |
57 | sub feed_sdl_motion_event { |
57 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
58 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
59 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
60 | |
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
61 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
62 | |
63 | if ($widget != $HOVER) { |
63 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
64 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
65 | |
66 | $hover->update if $hover && $hover->{can_hover}; |
66 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
67 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
68 | } |
68 | } |
69 | |
69 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
70 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
|
|
71 | } |
|
|
72 | |
|
|
73 | # convert position array to integers |
|
|
74 | sub harmonize { |
|
|
75 | my ($vals) = @_; |
|
|
76 | |
|
|
77 | my $rem = 0; |
|
|
78 | |
|
|
79 | for (@$vals) { |
|
|
80 | my $i = int $_ + $rem; |
|
|
81 | $rem += $_ - $i; |
|
|
82 | $_ = $i; |
|
|
83 | } |
71 | } |
84 | } |
72 | |
85 | |
73 | ############################################################################# |
86 | ############################################################################# |
74 | |
87 | |
75 | package CFClient::UI::Base; |
88 | package CFClient::UI::Base; |
76 | |
89 | |
77 | use strict; |
90 | use strict; |
78 | |
91 | |
79 | use SDL::OpenGL; |
92 | use CFClient::OpenGL; |
80 | |
93 | |
81 | sub new { |
94 | sub new { |
82 | my $class = shift; |
95 | my $class = shift; |
83 | |
96 | |
84 | my $self = bless { |
97 | my $self = bless { |
85 | x => 0, |
98 | x => 0, |
86 | y => 0, |
99 | y => 0, |
87 | z => 0, |
100 | z => 0, |
88 | w => -1, |
|
|
89 | h => -1, |
|
|
90 | @_ |
101 | @_ |
91 | }, $class; |
102 | }, $class; |
92 | |
103 | |
93 | for (keys %$self) { |
104 | for (keys %$self) { |
94 | if (/^connect_(.*)$/) { |
105 | if (/^connect_(.*)$/) { |
… | |
… | |
97 | } |
108 | } |
98 | |
109 | |
99 | $self |
110 | $self |
100 | } |
111 | } |
101 | |
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 | |
102 | sub move { |
129 | sub move { |
103 | my ($self, $x, $y, $z) = @_; |
130 | my ($self, $x, $y, $z) = @_; |
|
|
131 | |
104 | $self->{x} = $x; |
132 | $self->{x} = int $x; |
105 | $self->{y} = $y; |
133 | $self->{y} = int $y; |
106 | $self->{z} = $z if defined $z; |
134 | $self->{z} = $z if defined $z; |
|
|
135 | |
|
|
136 | $self->update; |
107 | } |
137 | } |
108 | |
138 | |
109 | sub needs_redraw { |
139 | sub needs_redraw { |
110 | 0 |
140 | 0 |
111 | } |
141 | } |
… | |
… | |
113 | sub size_request { |
143 | sub size_request { |
114 | require Carp; |
144 | require Carp; |
115 | Carp::confess "size_request is abtract"; |
145 | Carp::confess "size_request is abtract"; |
116 | } |
146 | } |
117 | |
147 | |
118 | sub _size_allocate { |
148 | sub configure { |
119 | my ($self, $x, $y, $w, $h) = @_; |
149 | my ($self, $x, $y, $w, $h) = @_; |
120 | |
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) { |
121 | $self->{x} = $x; |
164 | $self->{x} = $x; |
122 | $self->{y} = $y; |
165 | $self->{y} = $y; |
|
|
166 | $self->update; |
|
|
167 | } |
123 | |
168 | |
124 | return unless $self->{w} != $w || $self->{h} != $h; |
169 | if ($self->{w} != $w || $self->{h} != $h) { |
125 | |
|
|
126 | $self->{w} = $w; |
170 | $self->{w} = $w; |
127 | $self->{h} = $h; |
171 | $self->{h} = $h; |
128 | |
172 | |
|
|
173 | $self->size_allocate ($w, $h); |
|
|
174 | $self->update; |
129 | 1 |
175 | } |
130 | } |
176 | } |
131 | |
177 | |
132 | sub size_allocate { |
178 | sub size_allocate { |
|
|
179 | # nothing to be done |
|
|
180 | } |
|
|
181 | |
|
|
182 | # return top left coordinates |
|
|
183 | sub _topleft { |
133 | my ($self, $x, $y, $w, $h) = @_; |
184 | my ($self, $x, $y) = @_; |
134 | |
185 | |
135 | $self->_size_allocate ($x, $y, $w, $h); |
186 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
136 | } |
187 | } |
137 | |
188 | |
138 | # translate global coordinates to local coordinate system |
189 | # translate global coordinates to local coordinate system |
139 | sub translate { |
190 | sub coord2local { |
140 | my ($self, $x, $y) = @_; |
191 | my ($self, $x, $y) = @_; |
141 | |
192 | |
142 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
193 | my ($X, $Y) = $self->_topleft; |
|
|
194 | ($x - $X, $y - $Y) |
|
|
195 | } |
|
|
196 | |
|
|
197 | # translate local coordinates to global coordinate system |
|
|
198 | sub coord2global { |
|
|
199 | my ($self, $x, $y) = @_; |
|
|
200 | |
|
|
201 | my ($X, $Y) = $self->_topleft; |
|
|
202 | ($x + $X, $y + $Y) |
143 | } |
203 | } |
144 | |
204 | |
145 | sub focus_in { |
205 | sub focus_in { |
146 | my ($self) = @_; |
206 | my ($self) = @_; |
147 | |
207 | |
148 | return if $FOCUS == $self; |
208 | return if $FOCUS == $self; |
149 | return unless $self->{can_focus}; |
209 | return unless $self->{can_focus}; |
150 | |
210 | |
151 | my $focus = $FOCUS; $FOCUS = $self; |
211 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
212 | |
|
|
213 | $self->emit (focus_in => $focus); |
|
|
214 | |
152 | $focus->update if $focus; |
215 | $focus->update if $focus; |
153 | $FOCUS->update; |
216 | $FOCUS->update; |
154 | } |
217 | } |
155 | |
218 | |
156 | sub focus_out { |
219 | sub focus_out { |
157 | my ($self) = @_; |
220 | my ($self) = @_; |
158 | |
221 | |
159 | return unless $FOCUS == $self; |
222 | return unless $FOCUS == $self; |
160 | |
223 | |
161 | my $focus = $FOCUS; undef $FOCUS; |
224 | my $focus = $FOCUS; undef $FOCUS; |
|
|
225 | |
|
|
226 | $self->emit (focus_out => $focus); |
|
|
227 | |
162 | $focus->update if $focus; #? |
228 | $focus->update if $focus; #? |
163 | } |
229 | } |
164 | |
230 | |
165 | sub mouse_motion { } |
231 | sub mouse_motion { } |
166 | sub button_up { } |
232 | sub button_up { } |
… | |
… | |
190 | glPopMatrix; |
256 | glPopMatrix; |
191 | |
257 | |
192 | if ($self == $HOVER && $self->{can_hover}) { |
258 | if ($self == $HOVER && $self->{can_hover}) { |
193 | my ($x, $y) = @$self{qw(x y)}; |
259 | my ($x, $y) = @$self{qw(x y)}; |
194 | |
260 | |
195 | glColor 0, 0, 1, 0.2; |
261 | glColor 1, 0.8, 0.5, 0.2; |
196 | glEnable GL_BLEND; |
262 | glEnable GL_BLEND; |
197 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
263 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
198 | glBegin GL_QUADS; |
264 | glBegin GL_QUADS; |
199 | glVertex $x , $y; |
265 | glVertex $x , $y; |
200 | glVertex $x + $self->{w}, $y; |
266 | glVertex $x + $self->{w}, $y; |
… | |
… | |
220 | |
286 | |
221 | () |
287 | () |
222 | } |
288 | } |
223 | |
289 | |
224 | sub set_parent { |
290 | sub set_parent { |
225 | my ($self, $par) = @_; |
291 | my ($self, $parent) = @_; |
226 | |
292 | |
227 | $self->{parent} = $par; |
|
|
228 | Scalar::Util::weaken $self->{parent}; |
293 | Scalar::Util::weaken ($self->{parent} = $parent); |
229 | } |
294 | } |
230 | |
295 | |
231 | sub get_parent { |
296 | sub check_size { |
232 | $_[0]->{parent} |
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 | } |
233 | } |
308 | } |
234 | |
309 | |
235 | sub update { |
310 | sub update { |
236 | my ($self) = @_; |
311 | my ($self) = @_; |
237 | |
312 | |
… | |
… | |
240 | } |
315 | } |
241 | |
316 | |
242 | sub connect { |
317 | sub connect { |
243 | my ($self, $signal, $cb) = @_; |
318 | my ($self, $signal, $cb) = @_; |
244 | |
319 | |
245 | push @{ $self->{cb}{$signal} }, $cb; |
320 | push @{ $self->{signal_cb}{$signal} }, $cb; |
246 | } |
321 | } |
247 | |
322 | |
248 | sub emit { |
323 | sub emit { |
249 | my ($self, $signal, @args) = @_; |
324 | my ($self, $signal, @args) = @_; |
250 | |
325 | |
|
|
326 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
251 | $_->($self, @args) |
327 | $cb->($self, @args); |
252 | for @{$self->{cb}{$signal} || []}; |
328 | } |
253 | } |
329 | } |
254 | |
330 | |
255 | sub DESTROY { |
331 | sub DESTROY { |
256 | my ($self) = @_; |
332 | my ($self) = @_; |
257 | |
333 | |
… | |
… | |
263 | package CFClient::UI::DrawBG; |
339 | package CFClient::UI::DrawBG; |
264 | |
340 | |
265 | our @ISA = CFClient::UI::Base::; |
341 | our @ISA = CFClient::UI::Base::; |
266 | |
342 | |
267 | use strict; |
343 | use strict; |
268 | use SDL::OpenGL; |
344 | use CFClient::OpenGL; |
269 | |
345 | |
270 | sub new { |
346 | sub new { |
271 | my $class = shift; |
347 | my $class = shift; |
272 | |
348 | |
273 | # range [value, low, high, page] |
349 | # range [value, low, high, page] |
… | |
… | |
326 | |
402 | |
327 | $self |
403 | $self |
328 | } |
404 | } |
329 | |
405 | |
330 | sub add { |
406 | sub add { |
331 | my ($self, $chld) = @_; |
407 | my ($self, $child) = @_; |
332 | |
408 | |
333 | $chld->set_parent ($self); |
409 | $child->set_parent ($self); |
|
|
410 | |
|
|
411 | use sort 'stable'; |
334 | |
412 | |
335 | $self->{children} = [ |
413 | $self->{children} = [ |
336 | sort { $a->{z} <=> $b->{z} } |
414 | sort { $a->{z} <=> $b->{z} } |
337 | @{$self->{children}}, $chld |
415 | @{$self->{children}}, $child |
338 | ]; |
416 | ]; |
339 | |
417 | |
340 | $self->{w} = $self->{h} = -1; |
418 | $child->check_size; |
341 | $self->update; |
|
|
342 | } |
419 | } |
343 | |
420 | |
344 | sub remove { |
421 | sub remove { |
345 | my ($self, $widget) = @_; |
422 | my ($self, $child) = @_; |
346 | |
423 | |
|
|
424 | delete $child->{parent}; |
|
|
425 | |
347 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
426 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
348 | |
427 | |
349 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
428 | $self->check_size; |
350 | } |
429 | } |
351 | |
430 | |
352 | sub find_widget { |
431 | sub find_widget { |
353 | my ($self, $x, $y) = @_; |
432 | my ($self, $x, $y) = @_; |
354 | |
433 | |
… | |
… | |
384 | |
463 | |
385 | $class->SUPER::new (children => [$child], %arg) |
464 | $class->SUPER::new (children => [$child], %arg) |
386 | } |
465 | } |
387 | |
466 | |
388 | sub add { |
467 | sub add { |
389 | my ($self, $widget) = @_; |
468 | my ($self, $child) = @_; |
390 | |
469 | |
391 | $self->{children} = []; |
470 | $self->{children} = []; |
392 | |
471 | |
393 | $self->SUPER::add ($widget); |
472 | $self->SUPER::add ($child); |
394 | } |
473 | } |
395 | |
474 | |
396 | sub remove { |
475 | sub remove { |
397 | my ($self, $widget) = @_; |
476 | my ($self, $widget) = @_; |
398 | |
477 | |
… | |
… | |
407 | sub size_request { |
486 | sub size_request { |
408 | $_[0]{children}[0]->size_request |
487 | $_[0]{children}[0]->size_request |
409 | } |
488 | } |
410 | |
489 | |
411 | sub size_allocate { |
490 | sub size_allocate { |
412 | my ($self, $x, $y, $w, $h) = @_; |
491 | my ($self, $w, $h) = @_; |
413 | |
492 | |
414 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
415 | |
|
|
416 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
493 | $self->{children}[0]->configure (0, 0, $w, $h); |
417 | } |
494 | } |
418 | |
495 | |
419 | ############################################################################# |
496 | ############################################################################# |
420 | |
497 | |
421 | package CFClient::UI::Window; |
498 | package CFClient::UI::Window; |
422 | |
499 | |
423 | our @ISA = CFClient::UI::Bin::; |
500 | our @ISA = CFClient::UI::Bin::; |
424 | |
501 | |
425 | use SDL::OpenGL; |
502 | use CFClient::OpenGL; |
426 | |
503 | |
427 | sub new { |
504 | sub new { |
428 | my ($class, %arg) = @_; |
505 | my ($class, %arg) = @_; |
429 | |
506 | |
430 | my $self = $class->SUPER::new (%arg); |
507 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
447 | $self->child->draw; |
524 | $self->child->draw; |
448 | }; |
525 | }; |
449 | } |
526 | } |
450 | |
527 | |
451 | sub size_allocate { |
528 | sub size_allocate { |
452 | my ($self, $x, $y, $w, $h) = @_; |
529 | my ($self, $w, $h) = @_; |
453 | |
530 | |
454 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
455 | |
|
|
456 | $self->child->size_allocate (0, 0, $w, $h); |
531 | $self->child->configure (0, 0, $w, $h); |
457 | |
532 | |
458 | $self->render_chld; |
533 | $self->render_chld; |
459 | } |
534 | } |
460 | |
535 | |
461 | sub _draw { |
536 | sub _draw { |
… | |
… | |
477 | glDisable GL_TEXTURE_2D; |
552 | glDisable GL_TEXTURE_2D; |
478 | } |
553 | } |
479 | |
554 | |
480 | ############################################################################# |
555 | ############################################################################# |
481 | |
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 | |
482 | package CFClient::UI::Frame; |
581 | package CFClient::UI::Frame; |
483 | |
582 | |
484 | our @ISA = CFClient::UI::Bin::; |
583 | our @ISA = CFClient::UI::Bin::; |
485 | |
584 | |
486 | use SDL::OpenGL; |
585 | use CFClient::OpenGL; |
487 | |
586 | |
488 | sub size_request { |
587 | sub size_request { |
489 | my ($self) = @_; |
588 | my ($self) = @_; |
490 | my $chld = $self->child |
589 | my $chld = $self->child |
491 | or return (0, 0); |
590 | or return (0, 0); |
… | |
… | |
496 | } |
595 | } |
497 | |
596 | |
498 | sub size_allocate { |
597 | sub size_allocate { |
499 | my ($self, $x, $y, $w, $h) = @_; |
598 | my ($self, $x, $y, $w, $h) = @_; |
500 | |
599 | |
501 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
502 | |
|
|
503 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
600 | $self->child->configure (2, 2, $w - 4, $h - 4); |
504 | } |
601 | } |
505 | |
602 | |
506 | sub _draw { |
603 | sub _draw { |
507 | my ($self) = @_; |
604 | my ($self) = @_; |
508 | |
605 | |
… | |
… | |
525 | |
622 | |
526 | package CFClient::UI::FancyFrame; |
623 | package CFClient::UI::FancyFrame; |
527 | |
624 | |
528 | our @ISA = CFClient::UI::Bin::; |
625 | our @ISA = CFClient::UI::Bin::; |
529 | |
626 | |
530 | use SDL::OpenGL; |
627 | use CFClient::OpenGL; |
531 | |
628 | |
532 | my @tex = |
629 | my @tex = |
533 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
630 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
534 | 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); |
535 | |
632 | |
536 | sub new { |
633 | sub new { |
537 | my $class = shift; |
634 | my $class = shift; |
538 | |
635 | |
539 | # TODO: user_x, user_y, overwrite moveto? |
636 | # TODO: user_x, user_y, overwrite moveto? |
540 | |
637 | |
541 | $class->SUPER::new ( |
638 | my $self = $class->SUPER::new ( |
542 | bg => [1, 1, 1, 1], |
639 | bg => [1, 1, 1, 1], |
543 | border_bg => [1, 1, 1, 1], |
640 | border_bg => [1, 1, 1, 1], |
544 | border => $::FONTSIZE * 0.8, |
641 | border => 0.8, |
545 | @_ |
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, |
546 | ) |
668 | ) |
547 | } |
669 | } |
548 | |
670 | |
549 | sub size_request { |
|
|
550 | my ($self) = @_; |
|
|
551 | |
|
|
552 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
553 | |
|
|
554 | my ($w, $h) = $self->SUPER::size_request; |
|
|
555 | |
|
|
556 | ( |
|
|
557 | $w + $self->{border} * 2, |
|
|
558 | $h + $self->{border} * 2, |
|
|
559 | ) |
|
|
560 | } |
|
|
561 | |
|
|
562 | sub size_allocate { |
671 | sub size_allocate { |
563 | my ($self, $x, $y, $w, $h) = @_; |
672 | my ($self, $w, $h) = @_; |
564 | |
673 | |
565 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
566 | |
|
|
567 | $h -= List::Util::max 0, $self->{border} * 2; |
674 | $h -= List::Util::max 0, $self->border * 2; |
568 | $w -= List::Util::max 0, $self->{border} * 2; |
675 | $w -= List::Util::max 0, $self->border * 2; |
569 | |
676 | |
|
|
677 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
678 | if $self->{title}; |
|
|
679 | |
570 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
680 | $self->child->configure ($self->border, $self->border, $w, $h); |
571 | } |
681 | } |
572 | |
682 | |
573 | sub button_down { |
683 | sub button_down { |
574 | my ($self, $ev, $x, $y) = @_; |
684 | my ($self, $ev, $x, $y) = @_; |
575 | |
685 | |
|
|
686 | my $border = $self->border; |
|
|
687 | |
576 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
688 | if ($x < $self->{w} && $x >= $self->{w} - $border |
577 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
689 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
578 | |
690 | |
579 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
691 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
580 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
692 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
581 | |
693 | |
582 | $self->{motion} = sub { |
694 | $self->{motion} = sub { |
583 | my ($ev, $x, $y) = @_; |
695 | my ($ev, $x, $y) = @_; |
584 | |
696 | |
585 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
697 | ($x, $y) = ($ev->{x}, $ev->{y}); |
586 | |
698 | |
587 | $self->{user_w} = $bw + $x - $ox; |
699 | $self->{user_w} = $bw + $x - $ox; |
588 | $self->{user_h} = $bh + $y - $oy; |
700 | $self->{user_h} = $bh + $y - $oy; |
589 | $self->update; |
701 | $self->check_size; |
590 | }; |
702 | }; |
591 | |
703 | |
592 | } elsif ($x >= 0 && $x < $self->{w} |
704 | } elsif ($x >= 0 && $x < $self->{w} |
593 | && $y >= 0 && $y < $self->{border}) { |
705 | && $y >= 0 && $y < $border) { |
594 | |
706 | |
595 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
707 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
596 | my ($bx, $by) = ($self->{x}, $self->{y}); |
708 | my ($bx, $by) = ($self->{x}, $self->{y}); |
597 | |
709 | |
598 | $self->{motion} = sub { |
710 | $self->{motion} = sub { |
599 | my ($ev, $x, $y) = @_; |
711 | my ($ev, $x, $y) = @_; |
600 | |
712 | |
601 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
713 | ($x, $y) = ($ev->{x}, $ev->{y}); |
602 | |
714 | |
603 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
715 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
604 | $self->update; |
716 | $self->update; |
605 | }; |
717 | }; |
606 | } |
718 | } |
… | |
… | |
627 | glEnable GL_BLEND; |
739 | glEnable GL_BLEND; |
628 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
740 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
629 | glEnable GL_TEXTURE_2D; |
741 | glEnable GL_TEXTURE_2D; |
630 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
742 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
631 | |
743 | |
|
|
744 | my $border = $self->border; |
|
|
745 | |
632 | glColor @{ $self->{border_bg} }; |
746 | glColor @{ $self->{border_bg} }; |
633 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
747 | $tex[1]->draw_quad (0, 0, $w, $border); |
634 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
748 | $tex[3]->draw_quad (0, $border, $border, $ch); |
635 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
749 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
636 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
750 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
637 | |
751 | |
638 | my $bg = $tex[0]; |
752 | my $bg = $tex[0]; |
639 | |
753 | |
640 | # TODO: repeat texture not scale |
754 | # TODO: repeat texture not scale |
641 | my $rep_x = $cw / $bg->{w}; |
755 | my $rep_x = $cw / $bg->{w}; |
… | |
… | |
644 | glColor @{ $self->{bg} }; |
758 | glColor @{ $self->{bg} }; |
645 | |
759 | |
646 | $bg->{s} = $rep_x; |
760 | $bg->{s} = $rep_x; |
647 | $bg->{t} = $rep_y; |
761 | $bg->{t} = $rep_y; |
648 | $bg->{wrap_mode} = 1; |
762 | $bg->{wrap_mode} = 1; |
649 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
763 | $bg->draw_quad ($border, $border, $cw, $ch); |
650 | |
764 | |
651 | glDisable GL_TEXTURE_2D; |
765 | glDisable GL_TEXTURE_2D; |
652 | glDisable GL_BLEND; |
766 | glDisable GL_BLEND; |
653 | |
767 | |
|
|
768 | $self->{title}->draw if $self->{title}; |
654 | $self->child->draw; |
769 | $self->child->draw; |
655 | } |
770 | } |
656 | |
771 | |
657 | ############################################################################# |
772 | ############################################################################# |
658 | |
773 | |
… | |
… | |
660 | |
775 | |
661 | our @ISA = CFClient::UI::Base::; |
776 | our @ISA = CFClient::UI::Base::; |
662 | |
777 | |
663 | use List::Util qw(max sum); |
778 | use List::Util qw(max sum); |
664 | |
779 | |
665 | use SDL::OpenGL; |
780 | use CFClient::OpenGL; |
666 | |
781 | |
667 | sub new { |
782 | sub new { |
668 | my $class = shift; |
783 | my $class = shift; |
669 | |
784 | |
670 | $class->SUPER::new ( |
785 | $class->SUPER::new ( |
… | |
… | |
672 | @_ |
787 | @_ |
673 | ) |
788 | ) |
674 | } |
789 | } |
675 | |
790 | |
676 | sub add { |
791 | sub add { |
677 | my ($self, $x, $y, $chld) = @_; |
792 | my ($self, $x, $y, $child) = @_; |
678 | |
793 | |
|
|
794 | $child->set_parent ($self); |
679 | $self->{children}[$y][$x] = $chld; |
795 | $self->{children}[$y][$x] = $child; |
680 | $chld->set_parent ($self); |
|
|
681 | |
796 | |
682 | $self->{w} = $self->{h} = -1; |
797 | $child->check_size; |
|
|
798 | } |
|
|
799 | |
|
|
800 | # TODO: move to container class maybe? send childs a signal on removal? |
|
|
801 | sub clear { |
|
|
802 | my ($self) = @_; |
|
|
803 | |
|
|
804 | delete $self->{children}; |
683 | $self->update; |
805 | $self->update; |
684 | } |
806 | } |
685 | |
807 | |
686 | sub get_wh { |
808 | sub get_wh { |
687 | my ($self) = @_; |
809 | my ($self) = @_; |
… | |
… | |
715 | (sum @$hs), |
837 | (sum @$hs), |
716 | ) |
838 | ) |
717 | } |
839 | } |
718 | |
840 | |
719 | sub size_allocate { |
841 | sub size_allocate { |
720 | my ($self, $x, $y, $w, $h) = @_; |
842 | my ($self, $w, $h) = @_; |
721 | |
|
|
722 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
723 | |
843 | |
724 | my ($ws, $hs) = $self->get_wh; |
844 | my ($ws, $hs) = $self->get_wh; |
725 | |
845 | |
726 | my $req_w = sum @$ws; |
846 | my $req_w = sum @$ws; |
727 | my $req_h = sum @$hs; |
847 | my $req_h = sum @$hs; |
… | |
… | |
733 | |
853 | |
734 | # linearly scale sizes |
854 | # linearly scale sizes |
735 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
855 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
736 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
856 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
737 | |
857 | |
738 | $_ = int $_ for @$ws; #TODO: avoid rounding problems |
858 | CFClient::UI::harmonize $ws; |
739 | $_ = int $_ for @$hs; #TODO: avoid rounding problems |
859 | CFClient::UI::harmonize $hs; |
740 | |
860 | |
741 | my $y; |
861 | my $y; |
742 | |
862 | |
743 | for my $r (0 .. $#{$self->{children}}) { |
863 | for my $r (0 .. $#{$self->{children}}) { |
744 | my $row = $self->{children}[$r] |
864 | my $row = $self->{children}[$r] |
… | |
… | |
749 | |
869 | |
750 | for my $c (0 .. $#$row) { |
870 | for my $c (0 .. $#$row) { |
751 | my $col_w = $ws->[$c]; |
871 | my $col_w = $ws->[$c]; |
752 | |
872 | |
753 | if (my $widget = $row->[$c]) { |
873 | if (my $widget = $row->[$c]) { |
754 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
874 | $widget->configure ($x, $y, $col_w, $row_h); |
755 | } |
875 | } |
756 | |
876 | |
757 | $x += $col_w; |
877 | $x += $col_w; |
758 | } |
878 | } |
759 | |
879 | |
… | |
… | |
804 | (List::Util::max map $_->[1], @alloc), |
924 | (List::Util::max map $_->[1], @alloc), |
805 | ) |
925 | ) |
806 | } |
926 | } |
807 | |
927 | |
808 | sub size_allocate { |
928 | sub size_allocate { |
809 | my ($self, $x, $y, $w, $h) = @_; |
929 | my ($self, $w, $h) = @_; |
810 | |
|
|
811 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
812 | |
930 | |
813 | ($h, $w) = ($w, $h); |
931 | ($h, $w) = ($w, $h); |
814 | |
932 | |
815 | my $children = $self->{children}; |
933 | my $children = $self->{children}; |
816 | |
934 | |
… | |
… | |
832 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
950 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
833 | $h[$_] = $alloc_h; |
951 | $h[$_] = $alloc_h; |
834 | } |
952 | } |
835 | } |
953 | } |
836 | |
954 | |
|
|
955 | CFClient::UI::harmonize \@h; |
|
|
956 | |
837 | my $y = 0; |
957 | my $y = 0; |
838 | for (0 .. $#$children) { |
958 | for (0 .. $#$children) { |
839 | my $child = $children->[$_]; |
959 | my $child = $children->[$_]; |
840 | my $h = int $h[$_]; |
960 | my $h = $h[$_]; |
841 | $child->size_allocate ($y, 0, $h, $w); |
961 | $child->configure ($y, 0, $h, $w); |
842 | |
962 | |
843 | $y += $h; |
963 | $y += $h; |
844 | } |
964 | } |
|
|
965 | |
|
|
966 | 1 |
845 | } |
967 | } |
846 | |
968 | |
847 | ############################################################################# |
969 | ############################################################################# |
848 | |
970 | |
849 | package CFClient::UI::VBox; |
971 | package CFClient::UI::VBox; |
… | |
… | |
862 | (List::Util::sum map $_->[1], @alloc), |
984 | (List::Util::sum map $_->[1], @alloc), |
863 | ) |
985 | ) |
864 | } |
986 | } |
865 | |
987 | |
866 | sub size_allocate { |
988 | sub size_allocate { |
867 | my ($self, $x, $y, $w, $h) = @_; |
989 | my ($self, $w, $h) = @_; |
868 | |
|
|
869 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
870 | |
990 | |
871 | my $children = $self->{children}; |
991 | my $children = $self->{children}; |
872 | |
992 | |
873 | my @h = map +($_->size_request)[1], @$children; |
993 | my @h = map +($_->size_request)[1], @$children; |
874 | |
994 | |
… | |
… | |
886 | |
1006 | |
887 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
1007 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
888 | } |
1008 | } |
889 | } |
1009 | } |
890 | |
1010 | |
|
|
1011 | CFClient::UI::harmonize \@h; |
|
|
1012 | |
891 | my $y = 0; |
1013 | my $y = 0; |
892 | for (0 .. $#$children) { |
1014 | for (0 .. $#$children) { |
893 | my $child = $children->[$_]; |
1015 | my $child = $children->[$_]; |
894 | my $h = int $h[$_]; |
1016 | my $h = $h[$_]; |
895 | $child->size_allocate (0, $y, $w, $h); |
1017 | $child->configure (0, $y, $w, $h); |
896 | |
1018 | |
897 | $y += $h; |
1019 | $y += $h; |
898 | } |
1020 | } |
|
|
1021 | |
|
|
1022 | 1 |
899 | } |
1023 | } |
900 | |
1024 | |
901 | ############################################################################# |
1025 | ############################################################################# |
902 | |
1026 | |
903 | package CFClient::UI::Label; |
1027 | package CFClient::UI::Label; |
904 | |
1028 | |
905 | our @ISA = CFClient::UI::Base::; |
1029 | our @ISA = CFClient::UI::Base::; |
906 | |
1030 | |
907 | use SDL::OpenGL; |
1031 | use CFClient::OpenGL; |
908 | |
1032 | |
909 | sub new { |
1033 | sub new { |
910 | my ($class, %arg) = @_; |
1034 | my ($class, %arg) = @_; |
911 | |
1035 | |
912 | my $self = $class->SUPER::new ( |
1036 | my $self = $class->SUPER::new ( |
913 | fg => [1, 1, 1], |
1037 | fg => [1, 1, 1], |
914 | fontsize => $::FONTSIZE, |
1038 | fontsize => 1, |
915 | text => "", |
1039 | text => "", |
916 | align => -1, |
1040 | align => -1, |
|
|
1041 | valign => -1, |
917 | padding => 2, |
1042 | padding => 2, |
918 | layout => new CFClient::Layout, |
1043 | layout => new CFClient::Layout, |
919 | %arg |
1044 | %arg |
920 | ); |
1045 | ); |
921 | |
1046 | |
922 | $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}; |
923 | |
1055 | |
924 | $self |
1056 | $self |
925 | } |
1057 | } |
926 | |
1058 | |
927 | sub escape_text { |
1059 | sub escape_text { |
… | |
… | |
935 | } |
1067 | } |
936 | |
1068 | |
937 | sub set_text { |
1069 | sub set_text { |
938 | my ($self, $text) = @_; |
1070 | my ($self, $text) = @_; |
939 | |
1071 | |
940 | $self->{text} = $text; |
|
|
941 | $self->{layout}->set_markup ($text); |
1072 | $self->{layout}->set_text ($text); |
942 | |
1073 | |
943 | delete $self->{texture}; |
1074 | delete $self->{texture}; |
944 | # $self->{w} = $self->{h} = -1; |
|
|
945 | $self->update; |
1075 | $self->update; |
946 | } |
1076 | } |
947 | |
1077 | |
948 | sub get_text { |
1078 | sub set_markup { |
949 | my ($self, $text) = @_; |
1079 | my ($self, $markup) = @_; |
950 | |
1080 | |
951 | $self->{text} |
1081 | $self->{layout}->set_markup ($markup); |
|
|
1082 | |
|
|
1083 | delete $self->{texture}; |
|
|
1084 | $self->update; |
952 | } |
1085 | } |
953 | |
1086 | |
954 | sub size_request { |
1087 | sub size_request { |
955 | my ($self) = @_; |
1088 | my ($self) = @_; |
956 | |
1089 | |
957 | $self->{layout}->set_width; |
1090 | $self->{layout}->set_width; |
958 | $self->{layout}->set_height ($self->{fontsize}); |
1091 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1092 | |
959 | 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 | } |
960 | |
1103 | |
961 | ( |
1104 | ( |
962 | $w + $self->{padding} * 2, |
1105 | $w + $self->{padding} * 2, |
963 | $h + $self->{padding} * 2, |
1106 | $h + $self->{padding} * 2, |
964 | ) |
1107 | ) |
965 | } |
1108 | } |
966 | |
1109 | |
967 | sub size_allocate { |
1110 | sub size_allocate { |
968 | my ($self, $x, $y, $w, $h) = @_; |
1111 | my ($self, $w, $h) = @_; |
969 | |
|
|
970 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
971 | |
1112 | |
972 | delete $self->{texture}; |
1113 | delete $self->{texture}; |
973 | } |
|
|
974 | |
|
|
975 | sub update { |
|
|
976 | my ($self) = @_; |
|
|
977 | |
|
|
978 | delete $self->{texture}; |
|
|
979 | $self->SUPER::update; |
|
|
980 | } |
1114 | } |
981 | |
1115 | |
982 | sub _draw { |
1116 | sub _draw { |
983 | my ($self) = @_; |
1117 | my ($self) = @_; |
984 | |
1118 | |
985 | my $tex = $self->{texture} ||= do { |
1119 | my $tex = $self->{texture} ||= do { |
986 | $self->{layout}->set_width ($self->{w}); |
1120 | $self->{layout}->set_width ($self->{w}); |
987 | $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); |
988 | new_from_layout CFClient::Texture $self->{layout} |
1122 | new_from_layout CFClient::Texture $self->{layout} |
989 | }; |
1123 | }; |
990 | |
1124 | |
991 | glEnable GL_BLEND; |
1125 | glEnable GL_BLEND; |
992 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1126 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
993 | glEnable GL_TEXTURE_2D; |
1127 | glEnable GL_TEXTURE_2D; |
994 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1128 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
995 | |
1129 | |
996 | glColor @{$self->{fg}}; |
1130 | glColor @{$self->{fg}}; |
997 | |
1131 | |
998 | my $x = |
1132 | $self->{ox} = int ( |
999 | $self->{align} < 0 ? $self->{padding} |
1133 | $self->{align} < 0 ? $self->{padding} |
1000 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1134 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1001 | : ($self->{w} - $tex->{w}) * 0.5; |
1135 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1136 | ); |
1002 | |
1137 | |
1003 | $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}); |
1004 | |
1145 | |
1005 | glDisable GL_TEXTURE_2D; |
1146 | glDisable GL_TEXTURE_2D; |
1006 | glDisable GL_BLEND; |
1147 | glDisable GL_BLEND; |
1007 | } |
1148 | } |
1008 | |
1149 | |
1009 | ############################################################################# |
1150 | ############################################################################# |
1010 | |
1151 | |
1011 | package CFClient::UI::Entry; |
1152 | package CFClient::UI::EntryBase; |
1012 | |
1153 | |
1013 | our @ISA = CFClient::UI::Label::; |
1154 | our @ISA = CFClient::UI::Label::; |
1014 | |
1155 | |
1015 | use SDL; |
1156 | use CFClient::OpenGL; |
1016 | use SDL::OpenGL; |
|
|
1017 | |
1157 | |
1018 | sub new { |
1158 | sub new { |
1019 | my $class = shift; |
1159 | my $class = shift; |
1020 | |
1160 | |
1021 | $class->SUPER::new ( |
1161 | $class->SUPER::new ( |
… | |
… | |
1023 | bg => [0, 0, 0, 0.2], |
1163 | bg => [0, 0, 0, 0.2], |
1024 | active_bg => [1, 1, 1, 0.5], |
1164 | active_bg => [1, 1, 1, 0.5], |
1025 | active_fg => [0, 0, 0], |
1165 | active_fg => [0, 0, 0], |
1026 | can_hover => 1, |
1166 | can_hover => 1, |
1027 | can_focus => 1, |
1167 | can_focus => 1, |
|
|
1168 | valign => 0, |
1028 | @_ |
1169 | @_ |
1029 | ) |
1170 | ) |
1030 | } |
1171 | } |
1031 | |
1172 | |
1032 | sub _set_text { |
1173 | sub _set_text { |
1033 | my ($self, $text) = @_; |
1174 | my ($self, $text) = @_; |
1034 | |
1175 | |
1035 | my $old_text = $self->{text}; |
1176 | delete $self->{cur_h}; |
|
|
1177 | |
|
|
1178 | return if $self->{text} eq $text; |
|
|
1179 | |
|
|
1180 | delete $self->{texture}; |
1036 | |
1181 | |
1037 | $self->{last_activity} = $::NOW; |
1182 | $self->{last_activity} = $::NOW; |
1038 | |
|
|
1039 | $self->{text} = $text; |
1183 | $self->{text} = $text; |
1040 | $self->{layout}->set_width ($self->{w}); |
|
|
1041 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); |
|
|
1042 | |
1184 | |
1043 | $text =~ s/./*/g if $self->{hidden}; |
1185 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1186 | $self->{layout}->set_text ("$text "); |
1044 | |
1187 | |
1045 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1188 | $self->emit (changed => $self->{text}); |
|
|
1189 | } |
1046 | |
1190 | |
1047 | $text = substr $text, 0, $self->{cursor}; |
1191 | sub get_text { |
1048 | utf8::encode $text; |
1192 | $_[0]{text} |
1049 | |
|
|
1050 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1051 | |
|
|
1052 | $self->emit (changed => $self->{text}) # XXX: is this the right place to do this? |
|
|
1053 | if $old_text ne $self->{text}; |
|
|
1054 | } |
1193 | } |
1055 | |
1194 | |
1056 | sub size_request { |
1195 | sub size_request { |
1057 | my ($self) = @_; |
1196 | my ($self) = @_; |
1058 | |
1197 | |
… | |
… | |
1060 | |
1199 | |
1061 | ($w + 1, $h) # add 1 for cursor |
1200 | ($w + 1, $h) # add 1 for cursor |
1062 | } |
1201 | } |
1063 | |
1202 | |
1064 | sub size_allocate { |
1203 | sub size_allocate { |
1065 | my ($self, $x, $y, $w, $h) = @_; |
1204 | my ($self, $w, $h) = @_; |
1066 | |
|
|
1067 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
|
|
1068 | |
1205 | |
1069 | $self->_set_text ($self->{text}); |
1206 | $self->_set_text ($self->{text}); |
1070 | } |
1207 | } |
1071 | |
1208 | |
1072 | sub set_text { |
1209 | sub set_text { |
… | |
… | |
1078 | } |
1215 | } |
1079 | |
1216 | |
1080 | sub key_down { |
1217 | sub key_down { |
1081 | my ($self, $ev) = @_; |
1218 | my ($self, $ev) = @_; |
1082 | |
1219 | |
1083 | my $mod = $ev->key_mod; |
1220 | my $mod = $ev->{mod}; |
1084 | my $sym = $ev->key_sym; |
1221 | my $sym = $ev->{sym}; |
1085 | |
|
|
1086 | my $uni = $ev->key_unicode; |
1222 | my $uni = $ev->{unicode}; |
1087 | |
1223 | |
1088 | my $text = $self->get_text; |
1224 | my $text = $self->get_text; |
1089 | |
1225 | |
1090 | if ($sym == SDLK_BACKSPACE) { |
1226 | if ($sym == 8) { |
1091 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1227 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1092 | } elsif ($sym == SDLK_DELETE) { |
1228 | } elsif ($sym == 127) { |
1093 | substr $text, $self->{cursor}, 1, ""; |
1229 | substr $text, $self->{cursor}, 1, ""; |
1094 | } elsif ($sym == SDLK_LEFT) { |
1230 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1095 | --$self->{cursor} if $self->{cursor}; |
1231 | --$self->{cursor} if $self->{cursor}; |
1096 | } elsif ($sym == SDLK_RIGHT) { |
1232 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1097 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1233 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1098 | } elsif ($sym == SDLK_HOME) { |
1234 | } elsif ($sym == CFClient::SDLK_HOME) { |
1099 | $self->{cursor} = 0; |
1235 | $self->{cursor} = 0; |
1100 | } elsif ($sym == SDLK_END) { |
1236 | } elsif ($sym == CFClient::SDLK_END) { |
1101 | $self->{cursor} = length $text; |
1237 | $self->{cursor} = length $text; |
1102 | } elsif ($sym == SDLK_ESCAPE) { |
1238 | } elsif ($sym == 27) { |
1103 | $self->emit ('escape'); |
1239 | $self->emit ('escape'); |
1104 | } elsif ($uni) { |
1240 | } elsif ($uni) { |
1105 | substr $text, $self->{cursor}++, 0, chr $uni; |
1241 | substr $text, $self->{cursor}++, 0, chr $uni; |
1106 | } |
1242 | } |
1107 | |
1243 | |
… | |
… | |
1162 | |
1298 | |
1163 | $self->SUPER::_draw; |
1299 | $self->SUPER::_draw; |
1164 | |
1300 | |
1165 | #TODO: force update every cursor change :( |
1301 | #TODO: force update every cursor change :( |
1166 | 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 | |
1167 | glColor @{$self->{fg}}; |
1311 | glColor @{$self->{fg}}; |
1168 | glBegin GL_LINES; |
1312 | glBegin GL_LINES; |
1169 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1170 | 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}; |
1171 | glEnd; |
1315 | glEnd; |
1172 | } |
1316 | } |
1173 | } |
1317 | } |
1174 | |
1318 | |
1175 | package CFClient::UI::LineEntry; |
1319 | package CFClient::UI::Entry; |
1176 | |
1320 | |
1177 | our @ISA = CFClient::UI::Entry::; |
1321 | our @ISA = CFClient::UI::EntryBase::; |
1178 | |
1322 | |
1179 | use SDL; |
1323 | use CFClient::OpenGL; |
1180 | use SDL::OpenGL; |
|
|
1181 | |
1324 | |
1182 | sub key_down { |
1325 | sub key_down { |
1183 | my ($self, $ev) = @_; |
1326 | my ($self, $ev) = @_; |
1184 | |
1327 | |
1185 | my $sym = $ev->key_sym; |
1328 | my $sym = $ev->{sym}; |
1186 | |
1329 | |
1187 | if ($sym == SDLK_RETURN) { |
1330 | if ($sym == 13) { |
1188 | $self->emit (activate => $self->get_text); |
1331 | $self->emit (activate => $self->get_text); |
1189 | $self->update; |
1332 | $self->update; |
1190 | |
1333 | |
1191 | } else { |
1334 | } else { |
1192 | $self->SUPER::key_down ($ev); |
1335 | $self->SUPER::key_down ($ev); |
… | |
… | |
1198 | |
1341 | |
1199 | package CFClient::UI::Button; |
1342 | package CFClient::UI::Button; |
1200 | |
1343 | |
1201 | our @ISA = CFClient::UI::Label::; |
1344 | our @ISA = CFClient::UI::Label::; |
1202 | |
1345 | |
1203 | use SDL; |
1346 | use CFClient::OpenGL; |
1204 | use SDL::OpenGL; |
|
|
1205 | |
1347 | |
1206 | my @tex = |
1348 | my @tex = |
1207 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1349 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1208 | qw(b1_button_active.png); |
1350 | qw(b1_button_active.png); |
1209 | |
1351 | |
… | |
… | |
1212 | |
1354 | |
1213 | $class->SUPER::new ( |
1355 | $class->SUPER::new ( |
1214 | padding => 4, |
1356 | padding => 4, |
1215 | fg => [1, 1, 1], |
1357 | fg => [1, 1, 1], |
1216 | bg => [1, 1, 1, 0.2], |
1358 | bg => [1, 1, 1, 0.2], |
1217 | active_fg => [1, 1, 0], |
1359 | active_fg => [0, 0, 1], |
1218 | can_hover => 1, |
1360 | can_hover => 1, |
|
|
1361 | align => 0, |
|
|
1362 | valign => 0, |
1219 | @_ |
1363 | @_ |
1220 | ) |
1364 | ) |
1221 | } |
1365 | } |
1222 | |
1366 | |
1223 | sub button_up { |
1367 | sub button_up { |
… | |
… | |
1231 | |
1375 | |
1232 | sub _draw { |
1376 | sub _draw { |
1233 | my ($self) = @_; |
1377 | my ($self) = @_; |
1234 | |
1378 | |
1235 | local $self->{fg} = $self->{fg}; |
1379 | local $self->{fg} = $self->{fg}; |
1236 | my $tex = $tex[0]; |
|
|
1237 | |
|
|
1238 | glEnable GL_BLEND; |
|
|
1239 | glEnable GL_TEXTURE_2D; |
|
|
1240 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1241 | |
1380 | |
1242 | if ($GRAB == $self) { |
1381 | if ($GRAB == $self) { |
1243 | $self->{fg} = $self->{active_fg}; |
1382 | $self->{fg} = $self->{active_fg}; |
1244 | } |
1383 | } |
1245 | |
1384 | |
1246 | 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; |
1247 | 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; |
1248 | |
1390 | |
1249 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
1391 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1250 | |
1392 | |
1251 | glDisable GL_TEXTURE_2D; |
1393 | glDisable GL_TEXTURE_2D; |
1252 | glDisable GL_BLEND; |
1394 | glDisable GL_BLEND; |
1253 | |
1395 | |
1254 | $self->SUPER::_draw; |
1396 | $self->SUPER::_draw; |
… | |
… | |
1262 | |
1404 | |
1263 | my @tex = |
1405 | my @tex = |
1264 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1406 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1265 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1407 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
1266 | |
1408 | |
1267 | use SDL; |
1409 | use CFClient::OpenGL; |
1268 | use SDL::OpenGL; |
|
|
1269 | |
1410 | |
1270 | sub new { |
1411 | sub new { |
1271 | my $class = shift; |
1412 | my $class = shift; |
1272 | |
1413 | |
1273 | $class->SUPER::new ( |
1414 | $class->SUPER::new ( |
… | |
… | |
1284 | my ($self) = @_; |
1425 | my ($self) = @_; |
1285 | |
1426 | |
1286 | ($self->{padding} * 2 + 6) x 2 |
1427 | ($self->{padding} * 2 + 6) x 2 |
1287 | } |
1428 | } |
1288 | |
1429 | |
1289 | sub size_allocate { |
|
|
1290 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1291 | |
|
|
1292 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1293 | } |
|
|
1294 | |
|
|
1295 | sub button_down { |
1430 | sub button_down { |
1296 | my ($self, $ev, $x, $y) = @_; |
1431 | my ($self, $ev, $x, $y) = @_; |
1297 | |
1432 | |
1298 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1433 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1299 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1434 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1325 | glDisable GL_BLEND; |
1460 | glDisable GL_BLEND; |
1326 | } |
1461 | } |
1327 | |
1462 | |
1328 | ############################################################################# |
1463 | ############################################################################# |
1329 | |
1464 | |
|
|
1465 | package CFClient::UI::VGauge; |
|
|
1466 | |
|
|
1467 | our @ISA = CFClient::UI::Base::; |
|
|
1468 | |
|
|
1469 | use CFClient::OpenGL; |
|
|
1470 | |
|
|
1471 | my %tex = ( |
|
|
1472 | food => [ |
|
|
1473 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1474 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1475 | ], |
|
|
1476 | grace => [ |
|
|
1477 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1478 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1479 | ], |
|
|
1480 | hp => [ |
|
|
1481 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1482 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1483 | ], |
|
|
1484 | mana => [ |
|
|
1485 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1486 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1487 | ], |
|
|
1488 | ); |
|
|
1489 | |
|
|
1490 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1491 | sub new { |
|
|
1492 | my $class = shift; |
|
|
1493 | |
|
|
1494 | my $self = $class->SUPER::new ( |
|
|
1495 | type => 'food', |
|
|
1496 | @_ |
|
|
1497 | ); |
|
|
1498 | |
|
|
1499 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1500 | |
|
|
1501 | $self |
|
|
1502 | } |
|
|
1503 | |
|
|
1504 | sub size_request { |
|
|
1505 | my ($self) = @_; |
|
|
1506 | |
|
|
1507 | my $tex = $tex{$self->{type}}[0]; |
|
|
1508 | |
|
|
1509 | @$tex{qw(w h)} |
|
|
1510 | } |
|
|
1511 | |
|
|
1512 | sub set_max { |
|
|
1513 | my ($self, $max) = @_; |
|
|
1514 | |
|
|
1515 | $self->{max_val} = $max; |
|
|
1516 | } |
|
|
1517 | |
|
|
1518 | sub set_value { |
|
|
1519 | my ($self, $val, $max) = @_; |
|
|
1520 | |
|
|
1521 | $self->set_max ($max) |
|
|
1522 | if defined $max; |
|
|
1523 | |
|
|
1524 | $max = $self->{max_val}; |
|
|
1525 | $self->{val} = $val; |
|
|
1526 | |
|
|
1527 | $self->update; |
|
|
1528 | } |
|
|
1529 | |
|
|
1530 | sub _draw { |
|
|
1531 | my ($self) = @_; |
|
|
1532 | |
|
|
1533 | my $tex = $tex{$self->{type}}; |
|
|
1534 | |
|
|
1535 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1536 | |
|
|
1537 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1538 | $ycut = 1 if $self->{val} > $self->{max_val}; |
|
|
1539 | |
|
|
1540 | my $t1 = $tex->[0]; |
|
|
1541 | my $t2 = $tex->[1]; |
|
|
1542 | |
|
|
1543 | glEnable GL_BLEND; |
|
|
1544 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1545 | glEnable GL_TEXTURE_2D; |
|
|
1546 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1547 | |
|
|
1548 | my $h1 = $self->{h} - $ycut * $self->{h}; |
|
|
1549 | my $h2 = $ycut * $self->{h}; |
|
|
1550 | |
|
|
1551 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1552 | glBegin GL_QUADS; |
|
|
1553 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1554 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; |
|
|
1555 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; |
|
|
1556 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
|
|
1557 | glEnd; |
|
|
1558 | |
|
|
1559 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1560 | glBegin GL_QUADS; |
|
|
1561 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; |
|
|
1562 | glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; |
|
|
1563 | glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; |
|
|
1564 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; |
|
|
1565 | glEnd; |
|
|
1566 | |
|
|
1567 | glDisable GL_BLEND; |
|
|
1568 | glDisable GL_TEXTURE_2D; |
|
|
1569 | } |
|
|
1570 | |
|
|
1571 | ############################################################################# |
|
|
1572 | |
|
|
1573 | package CFClient::UI::Gauge; |
|
|
1574 | |
|
|
1575 | our @ISA = CFClient::UI::VBox::; |
|
|
1576 | |
|
|
1577 | sub new { |
|
|
1578 | my ($class, %arg) = shift; |
|
|
1579 | |
|
|
1580 | my $self = $class->SUPER::new ( |
|
|
1581 | @_, |
|
|
1582 | ); |
|
|
1583 | |
|
|
1584 | $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
|
|
1585 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); |
|
|
1586 | $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); |
|
|
1587 | |
|
|
1588 | $self |
|
|
1589 | } |
|
|
1590 | |
|
|
1591 | sub set_value { |
|
|
1592 | my ($self, $val, $max) = @_; |
|
|
1593 | |
|
|
1594 | $self->set_max ($max) |
|
|
1595 | if defined $max; |
|
|
1596 | |
|
|
1597 | $self->{gauge}->set_value ($val, $max); |
|
|
1598 | $self->{value}->set_text ($val); |
|
|
1599 | } |
|
|
1600 | |
|
|
1601 | sub set_max { |
|
|
1602 | my ($self, $max) = @_; |
|
|
1603 | |
|
|
1604 | $self->{gauge}->set_max ($max); |
|
|
1605 | $self->{max}->set_text ($max); |
|
|
1606 | } |
|
|
1607 | |
|
|
1608 | ############################################################################# |
|
|
1609 | |
1330 | package CFClient::UI::Slider; |
1610 | package CFClient::UI::Slider; |
1331 | |
1611 | |
1332 | use strict; |
1612 | use strict; |
1333 | |
1613 | |
1334 | use SDL::OpenGL; |
1614 | use CFClient::OpenGL; |
1335 | |
1615 | |
1336 | our @ISA = CFClient::UI::DrawBG::; |
1616 | our @ISA = CFClient::UI::DrawBG::; |
1337 | |
1617 | |
1338 | my @tex = |
1618 | my @tex = |
1339 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1619 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
1349 | # TODO: calculations are off |
1629 | # TODO: calculations are off |
1350 | my $self = $class->SUPER::new ( |
1630 | my $self = $class->SUPER::new ( |
1351 | fg => [1, 1, 1], |
1631 | fg => [1, 1, 1], |
1352 | active_fg => [0, 0, 0], |
1632 | active_fg => [0, 0, 0], |
1353 | range => [0, 0, 100, 10], |
1633 | range => [0, 0, 100, 10], |
1354 | req_w => 40, |
1634 | req_w => 20, |
1355 | req_h => 13, |
1635 | req_h => 20, |
1356 | vertical => 0, |
1636 | vertical => 0, |
1357 | can_hover => 1, |
1637 | can_hover => 1, |
1358 | inner_pad => 5, |
1638 | inner_pad => 5, |
1359 | @_ |
1639 | @_ |
1360 | ); |
1640 | ); |
… | |
… | |
1464 | |
1744 | |
1465 | package CFClient::UI::TextView; |
1745 | package CFClient::UI::TextView; |
1466 | |
1746 | |
1467 | our @ISA = CFClient::UI::HBox::; |
1747 | our @ISA = CFClient::UI::HBox::; |
1468 | |
1748 | |
1469 | use SDL::OpenGL; |
1749 | use CFClient::OpenGL; |
1470 | |
1750 | |
1471 | sub new { |
1751 | sub new { |
1472 | my $class = shift; |
1752 | my $class = shift; |
1473 | |
1753 | |
1474 | my $self = $class->SUPER::new ( |
1754 | my $self = $class->SUPER::new ( |
1475 | req_w => $::WIDTH / 6, |
|
|
1476 | req_h => $::HEIGHT / 6, |
|
|
1477 | fontsize => $::FONTSIZE, |
1755 | fontsize => 1, |
1478 | @_, |
1756 | @_, |
1479 | |
1757 | |
1480 | layout => (new CFClient::Layout), |
1758 | layout => (new CFClient::Layout), |
1481 | par => [], |
1759 | par => [], |
1482 | height => 0, |
1760 | height => 0, |
… | |
… | |
1503 | sub text_height { |
1781 | sub text_height { |
1504 | my ($self, $text) = @_; |
1782 | my ($self, $text) = @_; |
1505 | |
1783 | |
1506 | my $layout = $self->{layout}; |
1784 | my $layout = $self->{layout}; |
1507 | |
1785 | |
1508 | $layout->set_height ($self->{fontsize}); |
1786 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1509 | $layout->set_width ($self->{w}); |
1787 | $layout->set_width ($self->{w}); |
1510 | $layout->set_text ($text); |
1788 | $layout->set_text ($text); |
1511 | |
1789 | |
1512 | ($layout->size)[1] |
1790 | ($layout->size)[1] |
1513 | } |
1791 | } |
… | |
… | |
1517 | |
1795 | |
1518 | $self->{need_reflow}++; |
1796 | $self->{need_reflow}++; |
1519 | $self->update; |
1797 | $self->update; |
1520 | } |
1798 | } |
1521 | |
1799 | |
1522 | sub size_request { |
|
|
1523 | my ($self) = @_; |
|
|
1524 | |
|
|
1525 | ($self->{req_w}, $self->{req_h}) |
|
|
1526 | } |
|
|
1527 | |
|
|
1528 | sub size_allocate { |
1800 | sub size_allocate { |
1529 | my ($self, $x, $y, $w, $h) = @_; |
1801 | my ($self, $w, $h) = @_; |
1530 | |
1802 | |
1531 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1803 | $self->SUPER::size_allocate ($w, $h); |
1532 | |
1804 | |
1533 | $self->{layout}->set_height ($self->{fontsize}); |
1805 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1534 | $self->{layout}->set_width ($self->{w}); |
1806 | $self->{layout}->set_width ($self->{children}[0]{w}); |
1535 | |
1807 | |
1536 | $self->reflow; |
1808 | $self->reflow; |
1537 | } |
1809 | } |
1538 | |
1810 | |
1539 | sub add_paragraph { |
1811 | sub add_paragraph { |
… | |
… | |
1558 | |
1830 | |
1559 | return unless $self->{h} > 0; |
1831 | return unless $self->{h} > 0; |
1560 | |
1832 | |
1561 | delete $self->{texture}; |
1833 | delete $self->{texture}; |
1562 | |
1834 | |
1563 | $TOPLEVEL->on_refresh ($self, sub { |
1835 | $ROOT->on_refresh ($self, sub { |
1564 | if (delete $self->{need_reflow}) { |
1836 | if (delete $self->{need_reflow}) { |
1565 | my $height = 0; |
1837 | my $height = 0; |
1566 | |
1838 | |
1567 | $height += $_->[0] = $self->text_height ($_->[2]) |
1839 | $height += $_->[0] = $self->text_height ($_->[2]) |
1568 | for @{$self->{par}}; |
1840 | for @{$self->{par}}; |
… | |
… | |
1626 | |
1898 | |
1627 | } |
1899 | } |
1628 | |
1900 | |
1629 | ############################################################################# |
1901 | ############################################################################# |
1630 | |
1902 | |
1631 | package CFClient::UI::MapWidget; |
|
|
1632 | |
|
|
1633 | use strict; |
|
|
1634 | |
|
|
1635 | use List::Util qw(min max); |
|
|
1636 | |
|
|
1637 | use SDL; |
|
|
1638 | use SDL::OpenGL; |
|
|
1639 | |
|
|
1640 | our @ISA = CFClient::UI::Base::; |
|
|
1641 | |
|
|
1642 | sub new { |
|
|
1643 | my $class = shift; |
|
|
1644 | |
|
|
1645 | $class->SUPER::new ( |
|
|
1646 | z => -1, |
|
|
1647 | can_focus => 1, |
|
|
1648 | list => (glGenLists 1), |
|
|
1649 | @_ |
|
|
1650 | ) |
|
|
1651 | } |
|
|
1652 | |
|
|
1653 | sub key_down { |
|
|
1654 | print "MAPKEYDOWN\n"; |
|
|
1655 | } |
|
|
1656 | |
|
|
1657 | sub key_up { |
|
|
1658 | } |
|
|
1659 | |
|
|
1660 | sub button_down { |
|
|
1661 | my ($self, $ev, $x, $y) = @_; |
|
|
1662 | |
|
|
1663 | $self->focus_in; |
|
|
1664 | |
|
|
1665 | if ($ev->button == 2) { |
|
|
1666 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
|
|
1667 | my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y}); |
|
|
1668 | |
|
|
1669 | $self->{motion} = sub { |
|
|
1670 | my ($ev, $x, $y) = @_; |
|
|
1671 | |
|
|
1672 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
1673 | |
|
|
1674 | $::CFG->{map_shift_x} = $bw + $x - $ox; |
|
|
1675 | $::CFG->{map_shift_y} = $bh + $y - $oy; |
|
|
1676 | |
|
|
1677 | $self->update; |
|
|
1678 | }; |
|
|
1679 | } |
|
|
1680 | } |
|
|
1681 | |
|
|
1682 | sub button_up { |
|
|
1683 | my ($self, $ev, $x, $y) = @_; |
|
|
1684 | |
|
|
1685 | delete $self->{motion}; |
|
|
1686 | } |
|
|
1687 | |
|
|
1688 | sub mouse_motion { |
|
|
1689 | my ($self, $ev, $x, $y) = @_; |
|
|
1690 | |
|
|
1691 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1692 | } |
|
|
1693 | |
|
|
1694 | sub size_request { |
|
|
1695 | ( |
|
|
1696 | 1 + 32 * int $::WIDTH / 32, |
|
|
1697 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1698 | ) |
|
|
1699 | } |
|
|
1700 | |
|
|
1701 | sub update { |
|
|
1702 | my ($self) = @_; |
|
|
1703 | |
|
|
1704 | $self->{need_update} = 1; |
|
|
1705 | $self->SUPER::update; |
|
|
1706 | } |
|
|
1707 | |
|
|
1708 | sub draw { |
|
|
1709 | my ($self) = @_; |
|
|
1710 | |
|
|
1711 | if (delete $self->{need_update}) { |
|
|
1712 | glNewList $self->{list}, GL_COMPILE; |
|
|
1713 | |
|
|
1714 | my $sw = int $::WIDTH / 32; |
|
|
1715 | my $sh = int $::HEIGHT / 32; |
|
|
1716 | |
|
|
1717 | if ($::MAP) { |
|
|
1718 | my $sx = $::CFG->{map_shift_x}; |
|
|
1719 | my $sy = $::CFG->{map_shift_y}; |
|
|
1720 | |
|
|
1721 | glTranslate +($sx & 31) - 32, ($sy & 31) - 32, 0; |
|
|
1722 | |
|
|
1723 | my ($w, $h, $data) = $::MAP->draw ($sx >> 5, $sy >> 5, 0, 0, $sw + 1, $sh + 1); |
|
|
1724 | |
|
|
1725 | if ($::CFG->{fow_enable}) { |
|
|
1726 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1727 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1728 | glConvolutionFilter2D |
|
|
1729 | GL_CONVOLUTION_2D, |
|
|
1730 | GL_ALPHA, |
|
|
1731 | 3, 3, |
|
|
1732 | GL_ALPHA, GL_FLOAT, |
|
|
1733 | pack "f*", |
|
|
1734 | 0.1, 0.1, 0.1, |
|
|
1735 | 0.1, 0.2, 0.1, |
|
|
1736 | 0.1, 0.1, 0.1, |
|
|
1737 | ; |
|
|
1738 | glEnable GL_CONVOLUTION_2D; |
|
|
1739 | } |
|
|
1740 | |
|
|
1741 | my $tex = new CFClient::Texture |
|
|
1742 | w => $w, |
|
|
1743 | h => $h, |
|
|
1744 | data => $data, |
|
|
1745 | internalformat => GL_ALPHA, |
|
|
1746 | format => GL_ALPHA; |
|
|
1747 | |
|
|
1748 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1749 | |
|
|
1750 | glEnable GL_BLEND; |
|
|
1751 | glEnable GL_TEXTURE_2D; |
|
|
1752 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1753 | |
|
|
1754 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1755 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1756 | |
|
|
1757 | glDisable GL_TEXTURE_2D; |
|
|
1758 | glDisable GL_BLEND; |
|
|
1759 | } |
|
|
1760 | } |
|
|
1761 | |
|
|
1762 | glEndList; |
|
|
1763 | } |
|
|
1764 | |
|
|
1765 | glPushMatrix; |
|
|
1766 | glCallList $self->{list}; |
|
|
1767 | glPopMatrix; |
|
|
1768 | |
|
|
1769 | if ($FOCUS != $self) { |
|
|
1770 | glColor 64/255, 64/255, 64/255; |
|
|
1771 | glLogicOp GL_AND; |
|
|
1772 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1773 | glBegin GL_QUADS; |
|
|
1774 | glVertex 0, 0; |
|
|
1775 | glVertex 0, $::HEIGHT; |
|
|
1776 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1777 | glVertex $::WIDTH, 0; |
|
|
1778 | glEnd; |
|
|
1779 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1780 | } |
|
|
1781 | } |
|
|
1782 | |
|
|
1783 | my %DIR = ( |
|
|
1784 | SDLK_KP8, [1, "north"], |
|
|
1785 | SDLK_KP9, [2, "northeast"], |
|
|
1786 | SDLK_KP6, [3, "east"], |
|
|
1787 | SDLK_KP3, [4, "southeast"], |
|
|
1788 | SDLK_KP2, [5, "south"], |
|
|
1789 | SDLK_KP1, [6, "southwest"], |
|
|
1790 | SDLK_KP4, [7, "west"], |
|
|
1791 | SDLK_KP7, [8, "northwest"], |
|
|
1792 | |
|
|
1793 | SDLK_UP, [1, "north"], |
|
|
1794 | SDLK_RIGHT, [3, "east"], |
|
|
1795 | SDLK_DOWN, [5, "south"], |
|
|
1796 | SDLK_LEFT, [7, "west"], |
|
|
1797 | ); |
|
|
1798 | |
|
|
1799 | sub key_down { |
|
|
1800 | my ($self, $ev) = @_; |
|
|
1801 | |
|
|
1802 | my $mod = $ev->key_mod; |
|
|
1803 | my $sym = $ev->key_sym; |
|
|
1804 | |
|
|
1805 | if ($sym == SDLK_KP5) { |
|
|
1806 | $::CONN->user_send ("command stay fire"); |
|
|
1807 | } elsif ($sym == SDLK_a) { |
|
|
1808 | $::CONN->user_send ("command apply"); |
|
|
1809 | } elsif ($sym == SDLK_QUOTE) { |
|
|
1810 | $self->emit ('activate_console'); |
|
|
1811 | } elsif ($sym == SDLK_SLASH) { |
|
|
1812 | $self->emit ('activate_console' => '/'); |
|
|
1813 | } elsif (exists $DIR{$sym}) { |
|
|
1814 | if ($mod & KMOD_SHIFT) { |
|
|
1815 | $self->{shft}++; |
|
|
1816 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
|
|
1817 | } elsif ($mod & KMOD_CTRL) { |
|
|
1818 | $self->{ctrl}++; |
|
|
1819 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
|
|
1820 | } else { |
|
|
1821 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
|
|
1822 | } |
|
|
1823 | } |
|
|
1824 | } |
|
|
1825 | |
|
|
1826 | sub key_up { |
|
|
1827 | my ($self, $ev) = @_; |
|
|
1828 | |
|
|
1829 | my $mod = $ev->key_mod; |
|
|
1830 | my $sym = $ev->key_sym; |
|
|
1831 | |
|
|
1832 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1833 | $::CONN->user_send ("command fire_stop"); |
|
|
1834 | } |
|
|
1835 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1836 | $::CONN->user_send ("command run_stop"); |
|
|
1837 | } |
|
|
1838 | } |
|
|
1839 | |
|
|
1840 | ############################################################################# |
|
|
1841 | |
|
|
1842 | package CFClient::UI::Animator; |
1903 | package CFClient::UI::Animator; |
1843 | |
1904 | |
1844 | use SDL::OpenGL; |
1905 | use CFClient::OpenGL; |
1845 | |
1906 | |
1846 | our @ISA = CFClient::UI::Bin::; |
1907 | our @ISA = CFClient::UI::Bin::; |
1847 | |
1908 | |
1848 | sub moveto { |
1909 | sub moveto { |
1849 | my ($self, $x, $y) = @_; |
1910 | my ($self, $x, $y) = @_; |
… | |
… | |
1905 | sub toggle_flopper { |
1966 | sub toggle_flopper { |
1906 | my ($self) = @_; |
1967 | my ($self) = @_; |
1907 | |
1968 | |
1908 | # TODO: use animation |
1969 | # TODO: use animation |
1909 | if ($self->{state} = !$self->{state}) { |
1970 | if ($self->{state} = !$self->{state}) { |
1910 | $CFClient::UI::TOPLEVEL->add ($self->{other}); |
1971 | $CFClient::UI::ROOT->add ($self->{other}); |
1911 | $self->{other}->move ( |
1972 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
1912 | ($::WIDTH - $self->{other}{w}) * 0.5, |
1973 | $self->emit ("open"); |
1913 | ($::HEIGHT - $self->{other}{h}) * 0.5, |
|
|
1914 | ); |
|
|
1915 | } else { |
1974 | } else { |
1916 | $CFClient::UI::TOPLEVEL->remove ($self->{other}); |
1975 | $CFClient::UI::ROOT->remove ($self->{other}); |
|
|
1976 | $self->emit ("close"); |
1917 | } |
1977 | } |
1918 | } |
|
|
1919 | |
1978 | |
1920 | ############################################################################# |
1979 | $self->emit (changed => $self->{state}); |
|
|
1980 | } |
1921 | |
1981 | |
|
|
1982 | ############################################################################# |
|
|
1983 | |
1922 | package CFClient::UI::Toplevel; |
1984 | package CFClient::UI::Root; |
1923 | |
1985 | |
1924 | our @ISA = CFClient::UI::Container::; |
1986 | our @ISA = CFClient::UI::Container::; |
1925 | |
1987 | |
1926 | use SDL::OpenGL; |
1988 | use CFClient::OpenGL; |
|
|
1989 | |
|
|
1990 | sub check_size { |
|
|
1991 | my ($self) = @_; |
|
|
1992 | |
|
|
1993 | $self->configure (0, 0, $::WITH, $::HEIGHT); |
|
|
1994 | } |
1927 | |
1995 | |
1928 | sub size_request { |
1996 | sub size_request { |
1929 | ($::WIDTH, $::HEIGHT) |
1997 | ($::WIDTH, $::HEIGHT) |
1930 | } |
1998 | } |
1931 | |
1999 | |
1932 | sub size_allocate { |
2000 | sub configure { |
1933 | my ($self, $x, $y, $w, $h) = @_; |
2001 | my ($self, $x, $y, $w, $h) = @_; |
1934 | |
2002 | |
1935 | $self->_size_allocate ($x, $y, $w, $h); |
2003 | $self->SUPER::configure ($x, $y, $w, $h); |
1936 | |
2004 | |
1937 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
2005 | $_->configure ($_->{x}, $_->{y}, $_->size_request) |
1938 | for @{$self->{children}}; |
2006 | for @{$self->{children}}; |
1939 | } |
2007 | } |
1940 | |
2008 | |
1941 | sub translate { |
2009 | sub _topleft { |
1942 | my ($self, $x, $y) = @_; |
2010 | my ($self, $x, $y) = @_; |
1943 | |
2011 | |
1944 | ($x, $y) |
2012 | ($x, $y) |
1945 | } |
2013 | } |
1946 | |
2014 | |
1947 | sub update { |
2015 | sub update { |
1948 | my ($self) = @_; |
2016 | my ($self) = @_; |
1949 | |
2017 | |
1950 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2018 | $self->check_size; |
1951 | ::refresh (); |
2019 | ::refresh (); |
1952 | } |
2020 | } |
1953 | |
2021 | |
1954 | sub add { |
2022 | sub add { |
1955 | my ($self, $widget) = @_; |
2023 | my ($self, $child) = @_; |
1956 | |
2024 | |
1957 | $self->SUPER::add ($widget); |
2025 | $self->SUPER::add ($child); |
1958 | |
|
|
1959 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
|
|
1960 | } |
2026 | } |
1961 | |
2027 | |
1962 | sub on_refresh { |
2028 | sub on_refresh { |
1963 | my ($self, $id, $cb) = @_; |
2029 | my ($self, $id, $cb) = @_; |
1964 | |
2030 | |
… | |
… | |
1987 | |
2053 | |
1988 | ############################################################################# |
2054 | ############################################################################# |
1989 | |
2055 | |
1990 | package CFClient::UI; |
2056 | package CFClient::UI; |
1991 | |
2057 | |
1992 | $TOPLEVEL = new CFClient::UI::Toplevel; |
2058 | $ROOT = new CFClient::UI::Root; |
1993 | |
2059 | |
1994 | 1 |
2060 | 1 |
1995 | |
2061 | |