… | |
… | |
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; |
66 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $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, |
101 | can_events => 1, |
89 | h => -1, |
|
|
90 | @_ |
102 | @_ |
91 | }, $class; |
103 | }, $class; |
92 | |
104 | |
93 | for (keys %$self) { |
105 | for (keys %$self) { |
94 | if (/^connect_(.*)$/) { |
106 | if (/^connect_(.*)$/) { |
… | |
… | |
97 | } |
109 | } |
98 | |
110 | |
99 | $self |
111 | $self |
100 | } |
112 | } |
101 | |
113 | |
|
|
114 | sub show { |
|
|
115 | my ($self) = @_; |
|
|
116 | |
|
|
117 | return if $self->{parent}; |
|
|
118 | |
|
|
119 | $CFClient::UI::ROOT->add ($self); |
|
|
120 | } |
|
|
121 | |
|
|
122 | sub hide { |
|
|
123 | my ($self) = @_; |
|
|
124 | |
|
|
125 | return unless $self->{parent}; |
|
|
126 | |
|
|
127 | $self->{parent}->remove ($self); |
|
|
128 | } |
|
|
129 | |
102 | sub move { |
130 | sub move { |
103 | my ($self, $x, $y, $z) = @_; |
131 | my ($self, $x, $y, $z) = @_; |
|
|
132 | |
104 | $self->{x} = $x; |
133 | $self->{x} = int $x; |
105 | $self->{y} = $y; |
134 | $self->{y} = int $y; |
106 | $self->{z} = $z if defined $z; |
135 | $self->{z} = $z if defined $z; |
|
|
136 | |
|
|
137 | $self->update; |
107 | } |
138 | } |
108 | |
139 | |
109 | sub needs_redraw { |
140 | sub needs_redraw { |
110 | 0 |
141 | 0 |
111 | } |
142 | } |
112 | |
143 | |
113 | sub size_request { |
144 | sub size_request { |
114 | require Carp; |
145 | require Carp; |
115 | Carp::confess "size_request is abtract"; |
146 | Carp::confess "size_request is abstract"; |
116 | } |
147 | } |
117 | |
148 | |
118 | sub _size_allocate { |
149 | sub configure { |
119 | my ($self, $x, $y, $w, $h) = @_; |
150 | my ($self, $x, $y, $w, $h) = @_; |
120 | |
151 | |
|
|
152 | if ($self->{aspect}) { |
|
|
153 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
154 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
155 | |
|
|
156 | # use alignment to adjust x, y |
|
|
157 | |
|
|
158 | $x += int +($w - $w2) * 0.5; |
|
|
159 | $y += int +($h - $h2) * 0.5; |
|
|
160 | |
|
|
161 | ($w, $h) = ($w2, $h2); |
|
|
162 | } |
|
|
163 | |
|
|
164 | if ($self->{x} != $x || $self->{y} != $y) { |
121 | $self->{x} = int $x; |
165 | $self->{x} = $x; |
122 | $self->{y} = int $y; |
166 | $self->{y} = $y; |
|
|
167 | $self->update; |
|
|
168 | } |
123 | |
169 | |
124 | return unless $self->{w} != $w || $self->{h} != $h; |
170 | if ($self->{w} != $w || $self->{h} != $h) { |
125 | |
|
|
126 | $self->{w} = int $w; |
171 | $self->{w} = $w; |
127 | $self->{h} = int $h; |
172 | $self->{h} = $h; |
128 | |
173 | |
|
|
174 | $self->size_allocate ($w, $h); |
|
|
175 | $self->update; |
129 | 1 |
176 | } |
130 | } |
177 | } |
131 | |
178 | |
132 | sub size_allocate { |
179 | sub size_allocate { |
|
|
180 | # nothing to be done |
|
|
181 | } |
|
|
182 | |
|
|
183 | # return top left coordinates |
|
|
184 | sub _topleft { |
133 | my ($self, $x, $y, $w, $h) = @_; |
185 | my ($self, $x, $y) = @_; |
134 | |
186 | |
135 | $self->_size_allocate ($x, $y, $w, $h); |
187 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
136 | } |
188 | } |
137 | |
189 | |
138 | # translate global coordinates to local coordinate system |
190 | # translate global coordinates to local coordinate system |
139 | sub translate { |
191 | sub coord2local { |
140 | my ($self, $x, $y) = @_; |
192 | my ($self, $x, $y) = @_; |
141 | |
193 | |
142 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
194 | my ($X, $Y) = $self->_topleft; |
|
|
195 | ($x - $X, $y - $Y) |
|
|
196 | } |
|
|
197 | |
|
|
198 | # translate local coordinates to global coordinate system |
|
|
199 | sub coord2global { |
|
|
200 | my ($self, $x, $y) = @_; |
|
|
201 | |
|
|
202 | my ($X, $Y) = $self->_topleft; |
|
|
203 | ($x + $X, $y + $Y) |
143 | } |
204 | } |
144 | |
205 | |
145 | sub focus_in { |
206 | sub focus_in { |
146 | my ($self) = @_; |
207 | my ($self) = @_; |
147 | |
208 | |
148 | return if $FOCUS == $self; |
209 | return if $FOCUS == $self; |
149 | return unless $self->{can_focus}; |
210 | return unless $self->{can_focus}; |
150 | |
211 | |
151 | my $focus = $FOCUS; $FOCUS = $self; |
212 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
213 | |
|
|
214 | $self->emit (focus_in => $focus); |
|
|
215 | |
152 | $focus->update if $focus; |
216 | $focus->update if $focus; |
153 | $FOCUS->update; |
217 | $FOCUS->update; |
154 | } |
218 | } |
155 | |
219 | |
156 | sub focus_out { |
220 | sub focus_out { |
157 | my ($self) = @_; |
221 | my ($self) = @_; |
158 | |
222 | |
159 | return unless $FOCUS == $self; |
223 | return unless $FOCUS == $self; |
160 | |
224 | |
161 | my $focus = $FOCUS; undef $FOCUS; |
225 | my $focus = $FOCUS; undef $FOCUS; |
|
|
226 | |
|
|
227 | $self->emit (focus_out => $focus); |
|
|
228 | |
162 | $focus->update if $focus; #? |
229 | $focus->update if $focus; #? |
163 | } |
230 | } |
164 | |
231 | |
165 | sub mouse_motion { } |
232 | sub mouse_motion { } |
166 | sub button_up { } |
233 | sub button_up { } |
… | |
… | |
190 | glPopMatrix; |
257 | glPopMatrix; |
191 | |
258 | |
192 | if ($self == $HOVER && $self->{can_hover}) { |
259 | if ($self == $HOVER && $self->{can_hover}) { |
193 | my ($x, $y) = @$self{qw(x y)}; |
260 | my ($x, $y) = @$self{qw(x y)}; |
194 | |
261 | |
195 | glColor 0, 0, 1, 0.2; |
262 | glColor 1, 0.8, 0.5, 0.2; |
196 | glEnable GL_BLEND; |
263 | glEnable GL_BLEND; |
197 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
264 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
198 | glBegin GL_QUADS; |
265 | glBegin GL_QUADS; |
199 | glVertex $x , $y; |
266 | glVertex $x , $y; |
200 | glVertex $x + $self->{w}, $y; |
267 | glVertex $x + $self->{w}, $y; |
… | |
… | |
212 | } |
279 | } |
213 | |
280 | |
214 | sub find_widget { |
281 | sub find_widget { |
215 | my ($self, $x, $y) = @_; |
282 | my ($self, $x, $y) = @_; |
216 | |
283 | |
|
|
284 | return () unless $self->{can_events}; |
|
|
285 | |
217 | return $self |
286 | return $self |
218 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
287 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
219 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
288 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
220 | |
289 | |
221 | () |
290 | () |
222 | } |
291 | } |
223 | |
292 | |
224 | sub set_parent { |
293 | sub set_parent { |
225 | my ($self, $par) = @_; |
294 | my ($self, $parent) = @_; |
226 | |
295 | |
227 | $self->{parent} = $par; |
|
|
228 | Scalar::Util::weaken $self->{parent}; |
296 | Scalar::Util::weaken ($self->{parent} = $parent); |
229 | } |
297 | } |
230 | |
298 | |
231 | sub get_parent { |
299 | sub check_size { |
232 | $_[0]->{parent} |
300 | my ($self) = @_; |
|
|
301 | |
|
|
302 | return unless $self->{parent}; |
|
|
303 | |
|
|
304 | my ($w, $h) = $self->size_request; |
|
|
305 | |
|
|
306 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
307 | $self->{req_w} = $w; |
|
|
308 | $self->{req_h} = $h; |
|
|
309 | |
|
|
310 | $self->{parent}->check_size; |
|
|
311 | } |
233 | } |
312 | } |
234 | |
313 | |
235 | sub update { |
314 | sub update { |
236 | my ($self) = @_; |
315 | my ($self) = @_; |
237 | |
316 | |
… | |
… | |
240 | } |
319 | } |
241 | |
320 | |
242 | sub connect { |
321 | sub connect { |
243 | my ($self, $signal, $cb) = @_; |
322 | my ($self, $signal, $cb) = @_; |
244 | |
323 | |
245 | push @{ $self->{cb}{$signal} }, $cb; |
324 | push @{ $self->{signal_cb}{$signal} }, $cb; |
246 | } |
325 | } |
247 | |
326 | |
248 | sub emit { |
327 | sub emit { |
249 | my ($self, $signal, @args) = @_; |
328 | my ($self, $signal, @args) = @_; |
250 | |
329 | |
|
|
330 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
251 | $_->($self, @args) |
331 | $cb->($self, @args); |
252 | for @{$self->{cb}{$signal} || []}; |
332 | } |
253 | } |
333 | } |
254 | |
334 | |
255 | sub DESTROY { |
335 | sub DESTROY { |
256 | my ($self) = @_; |
336 | my ($self) = @_; |
257 | |
337 | |
… | |
… | |
263 | package CFClient::UI::DrawBG; |
343 | package CFClient::UI::DrawBG; |
264 | |
344 | |
265 | our @ISA = CFClient::UI::Base::; |
345 | our @ISA = CFClient::UI::Base::; |
266 | |
346 | |
267 | use strict; |
347 | use strict; |
268 | use SDL::OpenGL; |
348 | use CFClient::OpenGL; |
269 | |
349 | |
270 | sub new { |
350 | sub new { |
271 | my $class = shift; |
351 | my $class = shift; |
272 | |
352 | |
273 | # range [value, low, high, page] |
353 | # range [value, low, high, page] |
… | |
… | |
302 | |
382 | |
303 | package CFClient::UI::Empty; |
383 | package CFClient::UI::Empty; |
304 | |
384 | |
305 | our @ISA = CFClient::UI::Base::; |
385 | our @ISA = CFClient::UI::Base::; |
306 | |
386 | |
|
|
387 | sub new { |
|
|
388 | my ($class, %arg) = @_; |
|
|
389 | $class->SUPER::new (can_events => 0, %arg); |
|
|
390 | } |
|
|
391 | |
307 | sub size_request { |
392 | sub size_request { |
308 | (0, 0) |
393 | (0, 0) |
309 | } |
394 | } |
310 | |
395 | |
311 | sub draw { } |
396 | sub draw { } |
… | |
… | |
319 | sub new { |
404 | sub new { |
320 | my ($class, %arg) = @_; |
405 | my ($class, %arg) = @_; |
321 | |
406 | |
322 | my $children = delete $arg{children} || []; |
407 | my $children = delete $arg{children} || []; |
323 | |
408 | |
324 | my $self = $class->SUPER::new (children => [], %arg); |
409 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
325 | $self->add ($_) for @$children; |
410 | $self->add ($_) for @$children; |
326 | |
411 | |
327 | $self |
412 | $self |
328 | } |
413 | } |
329 | |
414 | |
330 | sub add { |
415 | sub add { |
331 | my ($self, $chld) = @_; |
416 | my ($self, $child) = @_; |
332 | |
417 | |
333 | $chld->set_parent ($self); |
418 | $child->set_parent ($self); |
|
|
419 | |
|
|
420 | use sort 'stable'; |
334 | |
421 | |
335 | $self->{children} = [ |
422 | $self->{children} = [ |
336 | sort { $a->{z} <=> $b->{z} } |
423 | sort { $a->{z} <=> $b->{z} } |
337 | @{$self->{children}}, $chld |
424 | @{$self->{children}}, $child |
338 | ]; |
425 | ]; |
339 | |
426 | |
340 | $self->{w} = $self->{h} = -1; |
427 | $child->check_size; |
341 | $self->update; |
|
|
342 | } |
428 | } |
343 | |
429 | |
344 | sub remove { |
430 | sub remove { |
345 | my ($self, $widget) = @_; |
431 | my ($self, $child) = @_; |
346 | |
432 | |
|
|
433 | delete $child->{parent}; |
|
|
434 | |
347 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
435 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
348 | |
436 | |
349 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
437 | $self->check_size; |
350 | } |
438 | } |
351 | |
439 | |
352 | sub find_widget { |
440 | sub find_widget { |
353 | my ($self, $x, $y) = @_; |
441 | my ($self, $x, $y) = @_; |
354 | |
442 | |
… | |
… | |
384 | |
472 | |
385 | $class->SUPER::new (children => [$child], %arg) |
473 | $class->SUPER::new (children => [$child], %arg) |
386 | } |
474 | } |
387 | |
475 | |
388 | sub add { |
476 | sub add { |
389 | my ($self, $widget) = @_; |
477 | my ($self, $child) = @_; |
390 | |
478 | |
391 | $self->{children} = []; |
479 | $self->{children} = []; |
392 | |
480 | |
393 | $self->SUPER::add ($widget); |
481 | $self->SUPER::add ($child); |
394 | } |
482 | } |
395 | |
483 | |
396 | sub remove { |
484 | sub remove { |
397 | my ($self, $widget) = @_; |
485 | my ($self, $widget) = @_; |
398 | |
486 | |
… | |
… | |
407 | sub size_request { |
495 | sub size_request { |
408 | $_[0]{children}[0]->size_request |
496 | $_[0]{children}[0]->size_request |
409 | } |
497 | } |
410 | |
498 | |
411 | sub size_allocate { |
499 | sub size_allocate { |
412 | my ($self, $x, $y, $w, $h) = @_; |
500 | my ($self, $w, $h) = @_; |
413 | |
501 | |
414 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
415 | |
|
|
416 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
502 | $self->{children}[0]->configure (0, 0, $w, $h); |
417 | } |
503 | } |
418 | |
504 | |
419 | ############################################################################# |
505 | ############################################################################# |
420 | |
506 | |
421 | package CFClient::UI::Window; |
507 | package CFClient::UI::Window; |
422 | |
508 | |
423 | our @ISA = CFClient::UI::Bin::; |
509 | our @ISA = CFClient::UI::Bin::; |
424 | |
510 | |
425 | use SDL::OpenGL; |
511 | use CFClient::OpenGL; |
426 | |
512 | |
427 | sub new { |
513 | sub new { |
428 | my ($class, %arg) = @_; |
514 | my ($class, %arg) = @_; |
429 | |
515 | |
430 | my $self = $class->SUPER::new (%arg); |
516 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
439 | } |
525 | } |
440 | |
526 | |
441 | sub render_chld { |
527 | sub render_chld { |
442 | my ($self) = @_; |
528 | my ($self) = @_; |
443 | |
529 | |
444 | $self->{texture} = |
530 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
445 | CFClient::Texture->new_from_opengl ( |
531 | glClearColor 0, 0, 0, 1; |
446 | $self->{w}, $self->{h}, sub { $self->child->draw } |
532 | glClear GL_COLOR_BUFFER_BIT; |
447 | ); |
533 | $self->child->draw; |
|
|
534 | }; |
448 | } |
535 | } |
449 | |
536 | |
450 | sub size_allocate { |
537 | sub size_allocate { |
451 | my ($self, $x, $y, $w, $h) = @_; |
538 | my ($self, $w, $h) = @_; |
452 | |
539 | |
453 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
454 | |
|
|
455 | $self->child->size_allocate (0, 0, $w, $h); |
540 | $self->child->configure (0, 0, $w, $h); |
456 | |
541 | |
457 | $self->render_chld; |
542 | $self->render_chld; |
458 | } |
543 | } |
459 | |
544 | |
460 | sub _draw { |
545 | sub _draw { |
… | |
… | |
476 | glDisable GL_TEXTURE_2D; |
561 | glDisable GL_TEXTURE_2D; |
477 | } |
562 | } |
478 | |
563 | |
479 | ############################################################################# |
564 | ############################################################################# |
480 | |
565 | |
|
|
566 | package CFClient::UI::ViewPort; |
|
|
567 | |
|
|
568 | our @ISA = CFClient::UI::Window::; |
|
|
569 | |
|
|
570 | sub new { die } |
|
|
571 | |
|
|
572 | sub size_request { |
|
|
573 | my ($self) = @_; |
|
|
574 | |
|
|
575 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
|
|
576 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
|
|
577 | |
|
|
578 | @$self{qw(child_w child_h)} |
|
|
579 | } |
|
|
580 | |
|
|
581 | sub _draw { |
|
|
582 | my ($self) = @_; |
|
|
583 | |
|
|
584 | $self->{children}[1]->draw; |
|
|
585 | } |
|
|
586 | |
|
|
587 | |
|
|
588 | ############################################################################# |
|
|
589 | |
481 | package CFClient::UI::Frame; |
590 | package CFClient::UI::Frame; |
482 | |
591 | |
483 | our @ISA = CFClient::UI::Bin::; |
592 | our @ISA = CFClient::UI::Bin::; |
484 | |
593 | |
485 | use SDL::OpenGL; |
594 | use CFClient::OpenGL; |
486 | |
|
|
487 | sub size_request { |
|
|
488 | my ($self) = @_; |
|
|
489 | my $chld = $self->child |
|
|
490 | or return (0, 0); |
|
|
491 | |
|
|
492 | $chld->move (2, 2); |
|
|
493 | |
|
|
494 | map { $_ + 4 } $chld->size_request; |
|
|
495 | } |
|
|
496 | |
|
|
497 | sub size_allocate { |
|
|
498 | my ($self, $x, $y, $w, $h) = @_; |
|
|
499 | |
|
|
500 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
501 | |
|
|
502 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
|
|
503 | } |
|
|
504 | |
|
|
505 | sub _draw { |
|
|
506 | my ($self) = @_; |
|
|
507 | |
|
|
508 | my $chld = $self->child; |
|
|
509 | |
|
|
510 | my ($w, $h) = $chld->size_request; |
|
|
511 | |
|
|
512 | glBegin GL_QUADS; |
|
|
513 | glColor 0, 0, 0; |
|
|
514 | glVertex 0 , 0; |
|
|
515 | glVertex 0 , $h + 4; |
|
|
516 | glVertex $w + 4 , $h + 4; |
|
|
517 | glVertex $w + 4 , 0; |
|
|
518 | glEnd; |
|
|
519 | |
|
|
520 | $chld->draw; |
|
|
521 | } |
|
|
522 | |
|
|
523 | ############################################################################# |
|
|
524 | |
|
|
525 | package CFClient::UI::FancyFrame; |
|
|
526 | |
|
|
527 | our @ISA = CFClient::UI::Bin::; |
|
|
528 | |
|
|
529 | use SDL::OpenGL; |
|
|
530 | |
|
|
531 | my @tex = |
|
|
532 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
533 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
534 | |
595 | |
535 | sub new { |
596 | sub new { |
536 | my $class = shift; |
597 | my $class = shift; |
537 | |
598 | |
538 | # TODO: user_x, user_y, overwrite moveto? |
|
|
539 | |
|
|
540 | $class->SUPER::new ( |
599 | my $self = $class->SUPER::new ( |
541 | bg => [1, 1, 1, 1], |
600 | bg => [1, 1, 1, 1], |
542 | border_bg => [1, 1, 1, 1], |
601 | border_bg => [1, 1, 1, 1], |
543 | border => $::FONTSIZE * 0.8, |
602 | border => 0.8, |
544 | @_ |
603 | @_ |
|
|
604 | ); |
|
|
605 | |
|
|
606 | $self |
|
|
607 | } |
|
|
608 | |
|
|
609 | sub set_size { |
|
|
610 | my ($self, $w, $h) = @_; |
|
|
611 | $self->{req_w} = $w; |
|
|
612 | $self->{req_h} = $h; |
|
|
613 | $self->check_size; |
|
|
614 | } |
|
|
615 | |
|
|
616 | sub size_request { |
|
|
617 | my ($self) = @_; |
|
|
618 | ($self->{req_w}, $self->{req_h}) |
|
|
619 | } |
|
|
620 | |
|
|
621 | sub size_allocate { |
|
|
622 | my ($self, $w, $h) = @_; |
|
|
623 | $self->{w} = $w; |
|
|
624 | $self->{h} = $h; |
|
|
625 | $self->child->configure (0, 0, $w, $h); |
|
|
626 | } |
|
|
627 | |
|
|
628 | sub _draw { |
|
|
629 | my ($self) = @_; |
|
|
630 | |
|
|
631 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
632 | |
|
|
633 | glEnable GL_BLEND; |
|
|
634 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
635 | glEnable GL_TEXTURE_2D; |
|
|
636 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
637 | |
|
|
638 | # glBegin GL_QUADS; |
|
|
639 | # glColor 0, 0, 0, 0; |
|
|
640 | # glVertex 0 , 0; |
|
|
641 | # glVertex 0 , $h; |
|
|
642 | # glVertex $w, $h; |
|
|
643 | # glVertex $w, 0; |
|
|
644 | # glEnd; |
|
|
645 | |
|
|
646 | |
|
|
647 | $self->child->draw; |
|
|
648 | glDisable GL_BLEND; |
|
|
649 | glDisable GL_TEXTURE_2D; |
|
|
650 | } |
|
|
651 | |
|
|
652 | ############################################################################# |
|
|
653 | |
|
|
654 | package CFClient::UI::FancyFrame; |
|
|
655 | |
|
|
656 | our @ISA = CFClient::UI::Bin::; |
|
|
657 | |
|
|
658 | use CFClient::OpenGL; |
|
|
659 | |
|
|
660 | my @tex = |
|
|
661 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
662 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
663 | |
|
|
664 | sub new { |
|
|
665 | my $class = shift; |
|
|
666 | |
|
|
667 | # TODO: user_x, user_y, overwrite moveto? |
|
|
668 | |
|
|
669 | my $self = $class->SUPER::new ( |
|
|
670 | bg => [1, 1, 1, 1], |
|
|
671 | border_bg => [1, 1, 1, 1], |
|
|
672 | border => 0.8, |
|
|
673 | can_events => 1, |
|
|
674 | @_ |
|
|
675 | ); |
|
|
676 | |
|
|
677 | $self->{title} &&= new CFClient::UI::Label |
|
|
678 | align => 0, |
|
|
679 | valign => 1, |
|
|
680 | text => $self->{title}, |
|
|
681 | fontsize => 1; |
|
|
682 | |
|
|
683 | $self |
|
|
684 | } |
|
|
685 | |
|
|
686 | sub border { |
|
|
687 | int $_[0]{border} * $::FONTSIZE |
|
|
688 | } |
|
|
689 | |
|
|
690 | sub size_request { |
|
|
691 | my ($self) = @_; |
|
|
692 | |
|
|
693 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
694 | |
|
|
695 | my ($w, $h) = $self->SUPER::size_request; |
|
|
696 | |
|
|
697 | ( |
|
|
698 | $w + $self->border * 2, |
|
|
699 | $h + $self->border * 2, |
545 | ) |
700 | ) |
546 | } |
701 | } |
547 | |
702 | |
548 | sub size_request { |
|
|
549 | my ($self) = @_; |
|
|
550 | |
|
|
551 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
552 | |
|
|
553 | my ($w, $h) = $self->SUPER::size_request; |
|
|
554 | |
|
|
555 | ( |
|
|
556 | $w + $self->{border} * 2, |
|
|
557 | $h + $self->{border} * 2, |
|
|
558 | ) |
|
|
559 | } |
|
|
560 | |
|
|
561 | sub size_allocate { |
703 | sub size_allocate { |
562 | my ($self, $x, $y, $w, $h) = @_; |
704 | my ($self, $w, $h) = @_; |
563 | |
705 | |
564 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
565 | |
|
|
566 | $h -= List::Util::max 0, $self->{border} * 2; |
706 | $h -= List::Util::max 0, $self->border * 2; |
567 | $w -= List::Util::max 0, $self->{border} * 2; |
707 | $w -= List::Util::max 0, $self->border * 2; |
568 | |
708 | |
|
|
709 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
710 | if $self->{title}; |
|
|
711 | |
569 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
712 | $self->child->configure ($self->border, $self->border, $w, $h); |
570 | } |
713 | } |
571 | |
714 | |
572 | sub button_down { |
715 | sub button_down { |
573 | my ($self, $ev, $x, $y) = @_; |
716 | my ($self, $ev, $x, $y) = @_; |
574 | |
717 | |
|
|
718 | my $border = $self->border; |
|
|
719 | |
575 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
720 | if ($x < $self->{w} && $x >= $self->{w} - $border |
576 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
721 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
577 | |
722 | |
578 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
723 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
579 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
724 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
580 | |
725 | |
581 | $self->{motion} = sub { |
726 | $self->{motion} = sub { |
582 | my ($ev, $x, $y) = @_; |
727 | my ($ev, $x, $y) = @_; |
583 | |
728 | |
584 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
729 | ($x, $y) = ($ev->{x}, $ev->{y}); |
585 | |
730 | |
586 | $self->{user_w} = $bw + $x - $ox; |
731 | $self->{user_w} = $bw + $x - $ox; |
587 | $self->{user_h} = $bh + $y - $oy; |
732 | $self->{user_h} = $bh + $y - $oy; |
588 | $self->update; |
733 | $self->check_size; |
589 | }; |
734 | }; |
590 | |
735 | |
591 | } elsif ($x >= 0 && $x < $self->{w} |
736 | } elsif ($x >= 0 && $x < $self->{w} |
592 | && $y >= 0 && $y < $self->{border}) { |
737 | && $y >= 0 && $y < $border) { |
593 | |
738 | |
594 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
739 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
595 | my ($bx, $by) = ($self->{x}, $self->{y}); |
740 | my ($bx, $by) = ($self->{x}, $self->{y}); |
596 | |
741 | |
597 | $self->{motion} = sub { |
742 | $self->{motion} = sub { |
598 | my ($ev, $x, $y) = @_; |
743 | my ($ev, $x, $y) = @_; |
599 | |
744 | |
600 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
745 | ($x, $y) = ($ev->{x}, $ev->{y}); |
601 | |
746 | |
602 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
747 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
603 | $self->update; |
748 | $self->update; |
604 | }; |
749 | }; |
605 | } |
750 | } |
… | |
… | |
626 | glEnable GL_BLEND; |
771 | glEnable GL_BLEND; |
627 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
772 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
628 | glEnable GL_TEXTURE_2D; |
773 | glEnable GL_TEXTURE_2D; |
629 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
774 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
630 | |
775 | |
|
|
776 | my $border = $self->border; |
|
|
777 | |
631 | glColor @{ $self->{border_bg} }; |
778 | glColor @{ $self->{border_bg} }; |
632 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
779 | $tex[1]->draw_quad (0, 0, $w, $border); |
633 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
780 | $tex[3]->draw_quad (0, $border, $border, $ch); |
634 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
781 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
635 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
782 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
636 | |
783 | |
637 | my $bg = $tex[0]; |
784 | my $bg = $tex[0]; |
638 | |
785 | |
639 | # TODO: repeat texture not scale |
786 | # TODO: repeat texture not scale |
640 | my $rep_x = $cw / $bg->{w}; |
787 | my $rep_x = $cw / $bg->{w}; |
641 | my $rep_y = $ch / $bg->{h}; |
788 | my $rep_y = $ch / $bg->{h}; |
642 | |
789 | |
643 | glColor @{ $self->{bg} }; |
790 | glColor @{ $self->{bg} }; |
|
|
791 | |
|
|
792 | $bg->{s} = $rep_x; |
|
|
793 | $bg->{t} = $rep_y; |
|
|
794 | $bg->{wrap_mode} = 1; |
644 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
795 | $bg->draw_quad ($border, $border, $cw, $ch); |
645 | |
796 | |
646 | glDisable GL_TEXTURE_2D; |
797 | glDisable GL_TEXTURE_2D; |
647 | glDisable GL_BLEND; |
798 | glDisable GL_BLEND; |
648 | |
799 | |
|
|
800 | $self->{title}->draw if $self->{title}; |
649 | $self->child->draw; |
801 | $self->child->draw; |
650 | } |
802 | } |
651 | |
803 | |
652 | ############################################################################# |
804 | ############################################################################# |
653 | |
805 | |
… | |
… | |
655 | |
807 | |
656 | our @ISA = CFClient::UI::Base::; |
808 | our @ISA = CFClient::UI::Base::; |
657 | |
809 | |
658 | use List::Util qw(max sum); |
810 | use List::Util qw(max sum); |
659 | |
811 | |
660 | use SDL::OpenGL; |
812 | use CFClient::OpenGL; |
661 | |
813 | |
662 | sub new { |
814 | sub new { |
663 | my $class = shift; |
815 | my $class = shift; |
664 | |
816 | |
665 | $class->SUPER::new ( |
817 | $class->SUPER::new ( |
… | |
… | |
667 | @_ |
819 | @_ |
668 | ) |
820 | ) |
669 | } |
821 | } |
670 | |
822 | |
671 | sub add { |
823 | sub add { |
672 | my ($self, $x, $y, $chld) = @_; |
824 | my ($self, $x, $y, $child) = @_; |
673 | |
825 | |
|
|
826 | $child->set_parent ($self); |
674 | $self->{children}[$y][$x] = $chld; |
827 | $self->{children}[$y][$x] = $child; |
675 | $chld->set_parent ($self); |
|
|
676 | |
828 | |
677 | $self->{w} = $self->{h} = -1; |
829 | $child->check_size; |
|
|
830 | } |
|
|
831 | |
|
|
832 | # TODO: move to container class maybe? send childs a signal on removal? |
|
|
833 | sub clear { |
|
|
834 | my ($self) = @_; |
|
|
835 | |
|
|
836 | delete $self->{children}; |
678 | $self->update; |
837 | $self->update; |
679 | } |
838 | } |
680 | |
839 | |
681 | sub get_wh { |
840 | sub get_wh { |
682 | my ($self) = @_; |
841 | my ($self) = @_; |
… | |
… | |
688 | or next; |
847 | or next; |
689 | |
848 | |
690 | for my $x (0 .. $#$row) { |
849 | for my $x (0 .. $#$row) { |
691 | my $widget = $row->[$x] |
850 | my $widget = $row->[$x] |
692 | or next; |
851 | or next; |
693 | my ($w, $h) = $widget->size_request; |
852 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
694 | |
853 | |
695 | $w[$x] = max $w[$x], $w; |
854 | $w[$x] = max $w[$x], $w; |
696 | $h[$y] = max $h[$y], $h; |
855 | $h[$y] = max $h[$y], $h; |
697 | } |
856 | } |
698 | } |
857 | } |
… | |
… | |
710 | (sum @$hs), |
869 | (sum @$hs), |
711 | ) |
870 | ) |
712 | } |
871 | } |
713 | |
872 | |
714 | sub size_allocate { |
873 | sub size_allocate { |
715 | my ($self, $x, $y, $w, $h) = @_; |
874 | my ($self, $w, $h) = @_; |
716 | |
|
|
717 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
718 | |
875 | |
719 | my ($ws, $hs) = $self->get_wh; |
876 | my ($ws, $hs) = $self->get_wh; |
720 | |
877 | |
721 | my $req_w = sum @$ws; |
878 | my $req_w = sum @$ws; |
722 | my $req_h = sum @$hs; |
879 | my $req_h = sum @$hs; |
… | |
… | |
727 | my $col_expand = (sum @col_expand) || 1; |
884 | my $col_expand = (sum @col_expand) || 1; |
728 | |
885 | |
729 | # linearly scale sizes |
886 | # linearly scale sizes |
730 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
887 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
731 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
888 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
889 | |
|
|
890 | CFClient::UI::harmonize $ws; |
|
|
891 | CFClient::UI::harmonize $hs; |
732 | |
892 | |
733 | my $y; |
893 | my $y; |
734 | |
894 | |
735 | for my $r (0 .. $#{$self->{children}}) { |
895 | for my $r (0 .. $#{$self->{children}}) { |
736 | my $row = $self->{children}[$r] |
896 | my $row = $self->{children}[$r] |
… | |
… | |
741 | |
901 | |
742 | for my $c (0 .. $#$row) { |
902 | for my $c (0 .. $#$row) { |
743 | my $col_w = $ws->[$c]; |
903 | my $col_w = $ws->[$c]; |
744 | |
904 | |
745 | if (my $widget = $row->[$c]) { |
905 | if (my $widget = $row->[$c]) { |
746 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
906 | $widget->configure ($x, $y, $col_w, $row_h); |
747 | } |
907 | } |
748 | |
908 | |
749 | $x += $col_w; |
909 | $x += $col_w; |
750 | } |
910 | } |
751 | |
911 | |
… | |
… | |
796 | (List::Util::max map $_->[1], @alloc), |
956 | (List::Util::max map $_->[1], @alloc), |
797 | ) |
957 | ) |
798 | } |
958 | } |
799 | |
959 | |
800 | sub size_allocate { |
960 | sub size_allocate { |
801 | my ($self, $x, $y, $w, $h) = @_; |
961 | my ($self, $w, $h) = @_; |
802 | |
|
|
803 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
804 | |
962 | |
805 | ($h, $w) = ($w, $h); |
963 | ($h, $w) = ($w, $h); |
806 | |
964 | |
807 | my $children = $self->{children}; |
965 | my $children = $self->{children}; |
808 | |
966 | |
809 | my @h = map +($_->size_request)[0], @$children; |
967 | my @h = map $_->{req_w}, @$children; |
810 | |
968 | |
811 | my $req_h = List::Util::sum @h; |
969 | my $req_h = List::Util::sum @h; |
812 | |
970 | |
813 | if ($req_h > $h) { |
971 | if ($req_h > $h) { |
814 | # ah well, not enough space |
972 | # ah well, not enough space |
… | |
… | |
824 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
982 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
825 | $h[$_] = $alloc_h; |
983 | $h[$_] = $alloc_h; |
826 | } |
984 | } |
827 | } |
985 | } |
828 | |
986 | |
|
|
987 | CFClient::UI::harmonize \@h; |
|
|
988 | |
829 | my $y = 0; |
989 | my $y = 0; |
830 | for (0 .. $#$children) { |
990 | for (0 .. $#$children) { |
831 | my $child = $children->[$_]; |
991 | my $child = $children->[$_]; |
832 | my $h = $h[$_]; |
992 | my $h = $h[$_]; |
833 | $child->size_allocate ($y, 0, $h, $w); |
993 | $child->configure ($y, 0, $h, $w); |
834 | |
994 | |
835 | $y += $h; |
995 | $y += $h; |
836 | } |
996 | } |
|
|
997 | |
|
|
998 | 1 |
837 | } |
999 | } |
838 | |
1000 | |
839 | ############################################################################# |
1001 | ############################################################################# |
840 | |
1002 | |
841 | package CFClient::UI::VBox; |
1003 | package CFClient::UI::VBox; |
… | |
… | |
854 | (List::Util::sum map $_->[1], @alloc), |
1016 | (List::Util::sum map $_->[1], @alloc), |
855 | ) |
1017 | ) |
856 | } |
1018 | } |
857 | |
1019 | |
858 | sub size_allocate { |
1020 | sub size_allocate { |
859 | my ($self, $x, $y, $w, $h) = @_; |
1021 | my ($self, $w, $h) = @_; |
860 | |
|
|
861 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
862 | |
1022 | |
863 | my $children = $self->{children}; |
1023 | my $children = $self->{children}; |
864 | |
1024 | |
865 | my @h = map +($_->size_request)[1], @$children; |
1025 | my @h = map $_->{req_h}, @$children; |
866 | |
1026 | |
867 | my $req_h = List::Util::sum @h; |
1027 | my $req_h = List::Util::sum @h; |
868 | |
1028 | |
869 | if ($req_h > $h) { |
1029 | if ($req_h > $h) { |
870 | # ah well, not enough space |
1030 | # ah well, not enough space |
… | |
… | |
878 | |
1038 | |
879 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
1039 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
880 | } |
1040 | } |
881 | } |
1041 | } |
882 | |
1042 | |
|
|
1043 | CFClient::UI::harmonize \@h; |
|
|
1044 | |
883 | my $y = 0; |
1045 | my $y = 0; |
884 | for (0 .. $#$children) { |
1046 | for (0 .. $#$children) { |
885 | my $child = $children->[$_]; |
1047 | my $child = $children->[$_]; |
886 | my $h = $h[$_]; |
1048 | my $h = $h[$_]; |
887 | $child->size_allocate (0, $y, $w, $h); |
1049 | $child->configure (0, $y, $w, $h); |
888 | |
1050 | |
889 | $y += $h; |
1051 | $y += $h; |
890 | } |
1052 | } |
|
|
1053 | |
|
|
1054 | 1 |
891 | } |
1055 | } |
892 | |
1056 | |
893 | ############################################################################# |
1057 | ############################################################################# |
894 | |
1058 | |
895 | package CFClient::UI::Label; |
1059 | package CFClient::UI::Label; |
896 | |
1060 | |
897 | our @ISA = CFClient::UI::Base::; |
1061 | our @ISA = CFClient::UI::Base::; |
898 | |
1062 | |
899 | use SDL::OpenGL; |
1063 | use CFClient::OpenGL; |
900 | |
1064 | |
901 | sub new { |
1065 | sub new { |
902 | my ($class, %arg) = @_; |
1066 | my ($class, %arg) = @_; |
903 | |
1067 | |
904 | my $self = $class->SUPER::new ( |
1068 | my $self = $class->SUPER::new ( |
905 | fg => [1, 1, 1], |
1069 | fg => [1, 1, 1], |
906 | height => $::FONTSIZE, |
1070 | fontsize => 1, |
907 | text => "", |
1071 | text => "", |
908 | align => -1, |
1072 | align => -1, |
|
|
1073 | valign => -1, |
909 | padding => 2, |
1074 | padding => 2, |
910 | layout => new CFClient::Layout, |
1075 | layout => new CFClient::Layout, |
|
|
1076 | can_events => 0, |
911 | %arg |
1077 | %arg |
912 | ); |
1078 | ); |
913 | |
1079 | |
914 | $self->set_text ($self->{text}); |
1080 | if (exists $self->{template}) { |
|
|
1081 | my $layout = new CFClient::Layout; |
|
|
1082 | $layout->set_text (delete $self->{template}); |
|
|
1083 | $self->{template} = $layout; |
|
|
1084 | } |
|
|
1085 | |
|
|
1086 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
|
|
1087 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
915 | |
1088 | |
916 | $self |
1089 | $self |
917 | } |
1090 | } |
918 | |
1091 | |
919 | sub escape_text { |
1092 | sub escape_text { |
… | |
… | |
927 | } |
1100 | } |
928 | |
1101 | |
929 | sub set_text { |
1102 | sub set_text { |
930 | my ($self, $text) = @_; |
1103 | my ($self, $text) = @_; |
931 | |
1104 | |
932 | $self->{text} = $text; |
|
|
933 | $self->{layout}->set_markup ($text); |
1105 | $self->{layout}->set_text ($text); |
934 | |
1106 | |
935 | delete $self->{texture}; |
1107 | delete $self->{texture}; |
936 | # $self->{w} = $self->{h} = -1; |
1108 | $self->check_size; |
937 | $self->update; |
1109 | $self->update; |
938 | } |
1110 | } |
939 | |
1111 | |
940 | sub get_text { |
1112 | sub set_markup { |
941 | my ($self, $text) = @_; |
1113 | my ($self, $markup) = @_; |
942 | |
1114 | |
943 | $self->{text} |
1115 | $self->{layout}->set_markup ($markup); |
|
|
1116 | |
|
|
1117 | delete $self->{texture}; |
|
|
1118 | $self->check_size; |
|
|
1119 | $self->update; |
944 | } |
1120 | } |
945 | |
1121 | |
946 | sub size_request { |
1122 | sub size_request { |
947 | my ($self) = @_; |
1123 | my ($self) = @_; |
948 | |
1124 | |
949 | $self->{layout}->set_width; |
1125 | $self->{layout}->set_width; |
950 | $self->{layout}->set_height ($self->{height}); |
1126 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1127 | |
951 | my ($w, $h) = $self->{layout}->size; |
1128 | my ($w, $h) = $self->{layout}->size; |
|
|
1129 | |
|
|
1130 | if (exists $self->{template}) { |
|
|
1131 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1132 | |
|
|
1133 | my ($w2, $h2) = $self->{template}->size; |
|
|
1134 | |
|
|
1135 | $w = List::Util::max $w, $w2; |
|
|
1136 | $h = List::Util::max $h, $h2; |
|
|
1137 | } |
952 | |
1138 | |
953 | ( |
1139 | ( |
954 | $w + $self->{padding} * 2, |
1140 | $w + $self->{padding} * 2, |
955 | $h + $self->{padding} * 2, |
1141 | $h + $self->{padding} * 2, |
956 | ) |
1142 | ) |
957 | } |
1143 | } |
958 | |
1144 | |
959 | sub size_allocate { |
1145 | sub size_allocate { |
960 | my ($self, $x, $y, $w, $h) = @_; |
1146 | my ($self, $w, $h) = @_; |
961 | |
|
|
962 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
963 | |
1147 | |
964 | delete $self->{texture}; |
1148 | delete $self->{texture}; |
965 | } |
1149 | } |
966 | |
1150 | |
967 | sub update { |
1151 | sub set_fontsize { |
968 | my ($self) = @_; |
1152 | my ($self, $fontsize) = @_; |
969 | |
1153 | |
970 | delete $self->{texture}; |
1154 | $self->{fontsize} = $fontsize; |
971 | $self->SUPER::update; |
1155 | $self->check_size; |
972 | } |
1156 | } |
973 | |
1157 | |
974 | sub _draw { |
1158 | sub _draw { |
975 | my ($self) = @_; |
1159 | my ($self) = @_; |
976 | |
1160 | |
977 | my $tex = $self->{texture} ||= do { |
1161 | my $tex = $self->{texture} ||= do { |
978 | $self->{layout}->set_width ($self->{w}); |
1162 | $self->{layout}->set_width ($self->{w}); |
979 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
1163 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
980 | new_from_layout CFClient::Texture $self->{layout} |
1164 | new_from_layout CFClient::Texture $self->{layout} |
981 | }; |
1165 | }; |
982 | |
1166 | |
983 | glEnable GL_BLEND; |
1167 | glEnable GL_BLEND; |
984 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1168 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
985 | glEnable GL_TEXTURE_2D; |
1169 | glEnable GL_TEXTURE_2D; |
986 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1170 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
987 | |
1171 | |
988 | glColor @{$self->{fg}}; |
1172 | glColor @{$self->{fg}}; |
989 | |
1173 | |
990 | my $x = |
1174 | $self->{ox} = int ( |
991 | $self->{align} < 0 ? $self->{padding} |
1175 | $self->{align} < 0 ? $self->{padding} |
992 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1176 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
993 | : ($self->{w} - $tex->{w}) * 0.5; |
1177 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1178 | ); |
994 | |
1179 | |
995 | glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; |
1180 | $self->{oy} = int ( |
996 | $tex->draw_quad (0, 0); |
1181 | $self->{valign} < 0 ? $self->{padding} |
|
|
1182 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1183 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1184 | ); |
|
|
1185 | |
|
|
1186 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
997 | |
1187 | |
998 | glDisable GL_TEXTURE_2D; |
1188 | glDisable GL_TEXTURE_2D; |
999 | glDisable GL_BLEND; |
1189 | glDisable GL_BLEND; |
1000 | } |
1190 | } |
1001 | |
1191 | |
1002 | ############################################################################# |
1192 | ############################################################################# |
1003 | |
1193 | |
1004 | package CFClient::UI::Entry; |
1194 | package CFClient::UI::EntryBase; |
1005 | |
1195 | |
1006 | our @ISA = CFClient::UI::Label::; |
1196 | our @ISA = CFClient::UI::Label::; |
1007 | |
1197 | |
1008 | use SDL; |
1198 | use CFClient::OpenGL; |
1009 | use SDL::OpenGL; |
|
|
1010 | |
1199 | |
1011 | sub new { |
1200 | sub new { |
1012 | my $class = shift; |
1201 | my $class = shift; |
1013 | |
1202 | |
1014 | $class->SUPER::new ( |
1203 | $class->SUPER::new ( |
… | |
… | |
1016 | bg => [0, 0, 0, 0.2], |
1205 | bg => [0, 0, 0, 0.2], |
1017 | active_bg => [1, 1, 1, 0.5], |
1206 | active_bg => [1, 1, 1, 0.5], |
1018 | active_fg => [0, 0, 0], |
1207 | active_fg => [0, 0, 0], |
1019 | can_hover => 1, |
1208 | can_hover => 1, |
1020 | can_focus => 1, |
1209 | can_focus => 1, |
|
|
1210 | valign => 0, |
|
|
1211 | can_events => 1, |
1021 | @_ |
1212 | @_ |
1022 | ) |
1213 | ) |
1023 | } |
1214 | } |
1024 | |
1215 | |
1025 | sub _set_text { |
1216 | sub _set_text { |
1026 | my ($self, $text) = @_; |
1217 | my ($self, $text) = @_; |
1027 | |
1218 | |
|
|
1219 | delete $self->{cur_h}; |
|
|
1220 | |
|
|
1221 | return if $self->{text} eq $text; |
|
|
1222 | |
|
|
1223 | delete $self->{texture}; |
|
|
1224 | |
1028 | $self->{last_activity} = $::NOW; |
1225 | $self->{last_activity} = $::NOW; |
1029 | |
|
|
1030 | $self->{text} = $text; |
1226 | $self->{text} = $text; |
1031 | $self->{layout}->set_width ($self->{w}); |
|
|
1032 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
|
|
1033 | |
1227 | |
1034 | $text =~ s/./*/g if $self->{hidden}; |
1228 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1229 | $self->{layout}->set_text ("$text "); |
1035 | |
1230 | |
1036 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1231 | $self->emit (changed => $self->{text}); |
|
|
1232 | } |
1037 | |
1233 | |
1038 | $text = substr $text, 0, $self->{cursor}; |
1234 | sub get_text { |
1039 | utf8::encode $text; |
1235 | $_[0]{text} |
1040 | |
|
|
1041 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1042 | } |
1236 | } |
1043 | |
1237 | |
1044 | sub size_request { |
1238 | sub size_request { |
1045 | my ($self) = @_; |
1239 | my ($self) = @_; |
1046 | |
1240 | |
… | |
… | |
1048 | |
1242 | |
1049 | ($w + 1, $h) # add 1 for cursor |
1243 | ($w + 1, $h) # add 1 for cursor |
1050 | } |
1244 | } |
1051 | |
1245 | |
1052 | sub size_allocate { |
1246 | sub size_allocate { |
1053 | my ($self, $x, $y, $w, $h) = @_; |
1247 | my ($self, $w, $h) = @_; |
1054 | |
1248 | |
1055 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1249 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1056 | |
|
|
1057 | $self->_set_text ($self->{text}); |
|
|
1058 | } |
1250 | } |
1059 | |
1251 | |
1060 | sub set_text { |
1252 | sub set_text { |
1061 | my ($self, $text) = @_; |
1253 | my ($self, $text) = @_; |
1062 | |
1254 | |
… | |
… | |
1066 | } |
1258 | } |
1067 | |
1259 | |
1068 | sub key_down { |
1260 | sub key_down { |
1069 | my ($self, $ev) = @_; |
1261 | my ($self, $ev) = @_; |
1070 | |
1262 | |
1071 | my $mod = $ev->key_mod; |
1263 | my $mod = $ev->{mod}; |
1072 | my $sym = $ev->key_sym; |
1264 | my $sym = $ev->{sym}; |
1073 | |
|
|
1074 | my $uni = $ev->key_unicode; |
1265 | my $uni = $ev->{unicode}; |
1075 | |
1266 | |
1076 | my $text = $self->get_text; |
1267 | my $text = $self->get_text; |
1077 | |
1268 | |
1078 | if ($sym == SDLK_BACKSPACE) { |
1269 | if ($sym == 8) { |
1079 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1270 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1080 | } elsif ($sym == SDLK_DELETE) { |
1271 | } elsif ($sym == 127) { |
1081 | substr $text, $self->{cursor}, 1, ""; |
1272 | substr $text, $self->{cursor}, 1, ""; |
1082 | } elsif ($sym == SDLK_LEFT) { |
1273 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1083 | --$self->{cursor} if $self->{cursor}; |
1274 | --$self->{cursor} if $self->{cursor}; |
1084 | } elsif ($sym == SDLK_RIGHT) { |
1275 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1085 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1276 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1086 | } elsif ($sym == SDLK_HOME) { |
1277 | } elsif ($sym == CFClient::SDLK_HOME) { |
1087 | $self->{cursor} = 0; |
1278 | $self->{cursor} = 0; |
1088 | } elsif ($sym == SDLK_END) { |
1279 | } elsif ($sym == CFClient::SDLK_END) { |
1089 | $self->{cursor} = length $text; |
1280 | $self->{cursor} = length $text; |
|
|
1281 | } elsif ($sym == 27) { |
|
|
1282 | $self->emit ('escape'); |
1090 | } elsif ($uni) { |
1283 | } elsif ($uni) { |
1091 | substr $text, $self->{cursor}++, 0, chr $uni; |
1284 | substr $text, $self->{cursor}++, 0, chr $uni; |
1092 | } |
1285 | } |
1093 | |
1286 | |
1094 | $self->_set_text ($text); |
1287 | $self->_set_text ($text); |
… | |
… | |
1148 | |
1341 | |
1149 | $self->SUPER::_draw; |
1342 | $self->SUPER::_draw; |
1150 | |
1343 | |
1151 | #TODO: force update every cursor change :( |
1344 | #TODO: force update every cursor change :( |
1152 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1345 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1346 | |
|
|
1347 | unless (exists $self->{cur_h}) { |
|
|
1348 | my $text = substr $self->{text}, 0, $self->{cursor}; |
|
|
1349 | utf8::encode $text; |
|
|
1350 | |
|
|
1351 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
|
|
1352 | } |
|
|
1353 | |
1153 | glColor @{$self->{fg}}; |
1354 | glColor @{$self->{fg}}; |
1154 | glBegin GL_LINES; |
1355 | glBegin GL_LINES; |
1155 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1156 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1356 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
|
|
1357 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1157 | glEnd; |
1358 | glEnd; |
1158 | } |
1359 | } |
1159 | } |
1360 | } |
1160 | |
1361 | |
|
|
1362 | package CFClient::UI::Entry; |
|
|
1363 | |
|
|
1364 | our @ISA = CFClient::UI::EntryBase::; |
|
|
1365 | |
|
|
1366 | use CFClient::OpenGL; |
|
|
1367 | |
|
|
1368 | sub key_down { |
|
|
1369 | my ($self, $ev) = @_; |
|
|
1370 | |
|
|
1371 | my $sym = $ev->{sym}; |
|
|
1372 | |
|
|
1373 | if ($sym == 13) { |
|
|
1374 | $self->emit (activate => $self->get_text); |
|
|
1375 | $self->update; |
|
|
1376 | |
|
|
1377 | } else { |
|
|
1378 | $self->SUPER::key_down ($ev); |
|
|
1379 | } |
|
|
1380 | |
|
|
1381 | } |
|
|
1382 | |
1161 | ############################################################################# |
1383 | ############################################################################# |
1162 | |
1384 | |
1163 | package CFClient::UI::Button; |
1385 | package CFClient::UI::Button; |
1164 | |
1386 | |
1165 | our @ISA = CFClient::UI::Label::; |
1387 | our @ISA = CFClient::UI::Label::; |
1166 | |
1388 | |
1167 | use SDL; |
1389 | use CFClient::OpenGL; |
1168 | use SDL::OpenGL; |
|
|
1169 | |
1390 | |
1170 | my @tex = |
1391 | my @tex = |
1171 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1392 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1172 | qw(b1_button_active.png); |
1393 | qw(b1_button_active.png); |
1173 | |
1394 | |
1174 | sub new { |
1395 | sub new { |
1175 | my $class = shift; |
1396 | my $class = shift; |
1176 | |
1397 | |
1177 | $class->SUPER::new ( |
1398 | $class->SUPER::new ( |
1178 | padding => 4, |
1399 | padding => 4, |
1179 | fg => [1, 1, 1], |
1400 | fg => [1, 1, 1], |
1180 | bg => [1, 1, 1, 0.2], |
1401 | bg => [1, 1, 1, 0.2], |
1181 | active_fg => [1, 1, 0], |
1402 | active_fg => [0, 0, 1], |
1182 | can_hover => 1, |
1403 | can_hover => 1, |
|
|
1404 | align => 0, |
|
|
1405 | valign => 0, |
|
|
1406 | can_events => 1, |
1183 | @_ |
1407 | @_ |
1184 | ) |
1408 | ) |
1185 | } |
1409 | } |
1186 | |
1410 | |
1187 | sub button_up { |
1411 | sub button_up { |
… | |
… | |
1195 | |
1419 | |
1196 | sub _draw { |
1420 | sub _draw { |
1197 | my ($self) = @_; |
1421 | my ($self) = @_; |
1198 | |
1422 | |
1199 | local $self->{fg} = $self->{fg}; |
1423 | local $self->{fg} = $self->{fg}; |
1200 | my $tex = $tex[0]; |
|
|
1201 | |
|
|
1202 | glEnable GL_BLEND; |
|
|
1203 | glEnable GL_TEXTURE_2D; |
|
|
1204 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1205 | |
1424 | |
1206 | if ($GRAB == $self) { |
1425 | if ($GRAB == $self) { |
1207 | $self->{fg} = $self->{active_fg}; |
1426 | $self->{fg} = $self->{active_fg}; |
1208 | } |
1427 | } |
1209 | |
1428 | |
1210 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1429 | glEnable GL_BLEND; |
|
|
1430 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1431 | glEnable GL_TEXTURE_2D; |
1211 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1432 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1433 | glColor 0, 0, 0, 1; |
1212 | |
1434 | |
1213 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
1435 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1214 | |
1436 | |
1215 | glDisable GL_TEXTURE_2D; |
1437 | glDisable GL_TEXTURE_2D; |
1216 | glDisable GL_BLEND; |
1438 | glDisable GL_BLEND; |
1217 | |
1439 | |
1218 | $self->SUPER::_draw; |
1440 | $self->SUPER::_draw; |
… | |
… | |
1222 | |
1444 | |
1223 | package CFClient::UI::CheckBox; |
1445 | package CFClient::UI::CheckBox; |
1224 | |
1446 | |
1225 | our @ISA = CFClient::UI::DrawBG::; |
1447 | our @ISA = CFClient::UI::DrawBG::; |
1226 | |
1448 | |
1227 | use SDL; |
1449 | my @tex = |
1228 | use SDL::OpenGL; |
1450 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1451 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
|
|
1452 | |
|
|
1453 | use CFClient::OpenGL; |
1229 | |
1454 | |
1230 | sub new { |
1455 | sub new { |
1231 | my $class = shift; |
1456 | my $class = shift; |
1232 | |
1457 | |
1233 | $class->SUPER::new ( |
1458 | $class->SUPER::new ( |
… | |
… | |
1244 | my ($self) = @_; |
1469 | my ($self) = @_; |
1245 | |
1470 | |
1246 | ($self->{padding} * 2 + 6) x 2 |
1471 | ($self->{padding} * 2 + 6) x 2 |
1247 | } |
1472 | } |
1248 | |
1473 | |
1249 | sub size_allocate { |
|
|
1250 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1251 | |
|
|
1252 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1253 | } |
|
|
1254 | |
|
|
1255 | sub button_down { |
1474 | sub button_down { |
1256 | my ($self, $ev, $x, $y) = @_; |
1475 | my ($self, $ev, $x, $y) = @_; |
1257 | |
1476 | |
1258 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1477 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1259 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1478 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1268 | $self->SUPER::_draw; |
1487 | $self->SUPER::_draw; |
1269 | |
1488 | |
1270 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1489 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1271 | |
1490 | |
1272 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1491 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1273 | |
1492 | |
1274 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1493 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1275 | |
1494 | |
1276 | glBegin GL_LINE_LOOP; |
1495 | glEnable GL_BLEND; |
1277 | glVertex 0 , 0; |
1496 | glEnable GL_TEXTURE_2D; |
1278 | glVertex 0 , $s; |
1497 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1279 | glVertex $s, $s; |
1498 | |
1280 | glVertex $s, 0; |
1499 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1500 | |
|
|
1501 | $tex->draw_quad (0, 0, $s, $s); |
|
|
1502 | |
|
|
1503 | glDisable GL_TEXTURE_2D; |
|
|
1504 | glDisable GL_BLEND; |
|
|
1505 | } |
|
|
1506 | |
|
|
1507 | ############################################################################# |
|
|
1508 | |
|
|
1509 | package CFClient::UI::Image; |
|
|
1510 | |
|
|
1511 | our @ISA = CFClient::UI::Base::; |
|
|
1512 | |
|
|
1513 | use CFClient::OpenGL; |
|
|
1514 | use Carp qw/confess/; |
|
|
1515 | |
|
|
1516 | our %loaded_images; |
|
|
1517 | |
|
|
1518 | sub new { |
|
|
1519 | my $class = shift; |
|
|
1520 | |
|
|
1521 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1522 | |
|
|
1523 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1524 | |
|
|
1525 | $loaded_images{$self->{image}} ||= |
|
|
1526 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1527 | |
|
|
1528 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1529 | |
|
|
1530 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1531 | |
|
|
1532 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
1533 | |
|
|
1534 | $self |
|
|
1535 | } |
|
|
1536 | |
|
|
1537 | sub size_request { |
|
|
1538 | my ($self) = @_; |
|
|
1539 | |
|
|
1540 | ($self->{tex}->{w}, $self->{tex}->{h}) |
|
|
1541 | } |
|
|
1542 | |
|
|
1543 | sub _draw { |
|
|
1544 | my ($self) = @_; |
|
|
1545 | |
|
|
1546 | my $tex = $self->{tex}; |
|
|
1547 | |
|
|
1548 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1549 | |
|
|
1550 | if ($self->{rot90}) { |
|
|
1551 | glRotate 90, 0, 0, 1; |
|
|
1552 | glTranslate 0, -$self->{w}, 0; |
|
|
1553 | |
|
|
1554 | ($w, $h) = ($h, $w); |
|
|
1555 | } |
|
|
1556 | |
|
|
1557 | glEnable GL_BLEND; |
|
|
1558 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1559 | glEnable GL_TEXTURE_2D; |
|
|
1560 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1561 | |
|
|
1562 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1563 | |
|
|
1564 | glDisable GL_BLEND; |
|
|
1565 | glDisable GL_TEXTURE_2D; |
|
|
1566 | } |
|
|
1567 | |
|
|
1568 | ############################################################################# |
|
|
1569 | |
|
|
1570 | package CFClient::UI::VGauge; |
|
|
1571 | |
|
|
1572 | our @ISA = CFClient::UI::Base::; |
|
|
1573 | |
|
|
1574 | use CFClient::OpenGL; |
|
|
1575 | |
|
|
1576 | my %tex = ( |
|
|
1577 | food => [ |
|
|
1578 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1579 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1580 | ], |
|
|
1581 | grace => [ |
|
|
1582 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1583 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1584 | ], |
|
|
1585 | hp => [ |
|
|
1586 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1587 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1588 | ], |
|
|
1589 | mana => [ |
|
|
1590 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1591 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1592 | ], |
|
|
1593 | ); |
|
|
1594 | |
|
|
1595 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1596 | sub new { |
|
|
1597 | my $class = shift; |
|
|
1598 | |
|
|
1599 | my $self = $class->SUPER::new ( |
|
|
1600 | type => 'food', |
|
|
1601 | @_ |
|
|
1602 | ); |
|
|
1603 | |
|
|
1604 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1605 | |
|
|
1606 | $self |
|
|
1607 | } |
|
|
1608 | |
|
|
1609 | sub size_request { |
|
|
1610 | my ($self) = @_; |
|
|
1611 | |
|
|
1612 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1613 | #@$tex{qw(w h)} |
|
|
1614 | (0, 0) |
|
|
1615 | } |
|
|
1616 | |
|
|
1617 | sub set_max { |
|
|
1618 | my ($self, $max) = @_; |
|
|
1619 | |
|
|
1620 | $self->{max_val} = $max; |
|
|
1621 | } |
|
|
1622 | |
|
|
1623 | sub set_value { |
|
|
1624 | my ($self, $val, $max) = @_; |
|
|
1625 | |
|
|
1626 | $self->set_max ($max) |
|
|
1627 | if defined $max; |
|
|
1628 | |
|
|
1629 | $max = $self->{max_val}; |
|
|
1630 | $self->{val} = $val; |
|
|
1631 | |
|
|
1632 | $self->update; |
|
|
1633 | } |
|
|
1634 | |
|
|
1635 | sub _draw { |
|
|
1636 | my ($self) = @_; |
|
|
1637 | |
|
|
1638 | my $tex = $tex{$self->{type}}; |
|
|
1639 | |
|
|
1640 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1641 | |
|
|
1642 | if ($self->{vertical}) { |
|
|
1643 | glRotate 90, 0, 0, 1; |
|
|
1644 | glTranslate 0, -$self->{w}, 0; |
|
|
1645 | |
|
|
1646 | ($w, $h) = ($h, $w); |
|
|
1647 | } |
|
|
1648 | |
|
|
1649 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1650 | $ycut = 1 if $self->{val} > $self->{max_val}; |
|
|
1651 | |
|
|
1652 | my $t1 = $tex->[0]; |
|
|
1653 | my $t2 = $tex->[1]; |
|
|
1654 | |
|
|
1655 | glEnable GL_BLEND; |
|
|
1656 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1657 | glEnable GL_TEXTURE_2D; |
|
|
1658 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1659 | |
|
|
1660 | my $h1 = $self->{h} - $ycut * $self->{h}; |
|
|
1661 | my $h2 = $ycut * $self->{h}; |
|
|
1662 | |
|
|
1663 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1664 | glBegin GL_QUADS; |
|
|
1665 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1666 | glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; |
|
|
1667 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; |
|
|
1668 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
1281 | glEnd; |
1669 | glEnd; |
1282 | |
1670 | |
1283 | if ($self->{state}) { |
1671 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
1284 | glBegin GL_LINES; |
1672 | glBegin GL_QUADS; |
1285 | glVertex 0 , 0; |
1673 | glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; |
1286 | glVertex $s, $s; |
1674 | glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; |
1287 | glVertex $s, 0; |
1675 | glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; |
1288 | glVertex 0 , $s; |
1676 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; |
1289 | glEnd; |
1677 | glEnd; |
1290 | } |
1678 | |
|
|
1679 | glDisable GL_BLEND; |
|
|
1680 | glDisable GL_TEXTURE_2D; |
|
|
1681 | } |
|
|
1682 | |
|
|
1683 | ############################################################################# |
|
|
1684 | |
|
|
1685 | package CFClient::UI::Gauge; |
|
|
1686 | |
|
|
1687 | our @ISA = CFClient::UI::VBox::; |
|
|
1688 | |
|
|
1689 | sub new { |
|
|
1690 | my ($class, %arg) = shift; |
|
|
1691 | |
|
|
1692 | my $self = $class->SUPER::new ( |
|
|
1693 | @_, |
|
|
1694 | ); |
|
|
1695 | |
|
|
1696 | $self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"; |
|
|
1697 | $self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"; |
|
|
1698 | $self->add ($self->{value}); |
|
|
1699 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); |
|
|
1700 | $self->add ($self->{max}); |
|
|
1701 | |
|
|
1702 | $self |
|
|
1703 | } |
|
|
1704 | |
|
|
1705 | sub size_request { |
|
|
1706 | my ($self) = @_; |
|
|
1707 | (($self->{max}->size_request)[0], 0) |
|
|
1708 | } |
|
|
1709 | |
|
|
1710 | sub set_fontsize { |
|
|
1711 | my ($self, $fsize) = @_; |
|
|
1712 | |
|
|
1713 | $self->{value}->set_fontsize ($fsize); |
|
|
1714 | $self->{max} ->set_fontsize ($fsize); |
|
|
1715 | } |
|
|
1716 | |
|
|
1717 | sub set_value { |
|
|
1718 | my ($self, $val, $max) = @_; |
|
|
1719 | |
|
|
1720 | $self->set_max ($max) |
|
|
1721 | if defined $max; |
|
|
1722 | |
|
|
1723 | $self->{gauge}->set_value ($val, $max); |
|
|
1724 | $self->{value}->set_text ($val); |
|
|
1725 | } |
|
|
1726 | |
|
|
1727 | sub set_max { |
|
|
1728 | my ($self, $max) = @_; |
|
|
1729 | |
|
|
1730 | $self->{gauge}->set_max ($max); |
|
|
1731 | $self->{max}->set_text ($max); |
1291 | } |
1732 | } |
1292 | |
1733 | |
1293 | ############################################################################# |
1734 | ############################################################################# |
1294 | |
1735 | |
1295 | package CFClient::UI::Slider; |
1736 | package CFClient::UI::Slider; |
1296 | |
1737 | |
1297 | use strict; |
1738 | use strict; |
1298 | |
1739 | |
1299 | use SDL::OpenGL; |
1740 | use CFClient::OpenGL; |
1300 | |
1741 | |
1301 | our @ISA = CFClient::UI::DrawBG::; |
1742 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1743 | |
|
|
1744 | my @tex = |
|
|
1745 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1746 | qw(s1_slider.png s1_slider_bg.png); |
1302 | |
1747 | |
1303 | sub new { |
1748 | sub new { |
1304 | my $class = shift; |
1749 | my $class = shift; |
1305 | |
1750 | |
1306 | # range [value, low, high, page] |
1751 | # range [value, low, high, page] |
… | |
… | |
1310 | # TODO: calculations are off |
1755 | # TODO: calculations are off |
1311 | my $self = $class->SUPER::new ( |
1756 | my $self = $class->SUPER::new ( |
1312 | fg => [1, 1, 1], |
1757 | fg => [1, 1, 1], |
1313 | active_fg => [0, 0, 0], |
1758 | active_fg => [0, 0, 0], |
1314 | range => [0, 0, 100, 10], |
1759 | range => [0, 0, 100, 10], |
1315 | req_w => 40, |
1760 | req_w => 20, |
1316 | req_h => 13, |
1761 | req_h => 20, |
1317 | vertical => 0, |
1762 | vertical => 0, |
1318 | can_hover => 1, |
1763 | can_hover => 1, |
|
|
1764 | inner_pad => 5, |
1319 | @_ |
1765 | @_ |
1320 | ); |
1766 | ); |
1321 | |
1767 | |
1322 | $self |
1768 | $self |
1323 | } |
1769 | } |
… | |
… | |
1344 | if ($GRAB == $self) { |
1790 | if ($GRAB == $self) { |
1345 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1791 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1346 | |
1792 | |
1347 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1793 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1348 | |
1794 | |
|
|
1795 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1796 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1797 | |
|
|
1798 | $x -= $inner_pad_px; # substract the padding |
1349 | $x = $x * ($hi - $lo) / $w + $lo; |
1799 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
1350 | $x = $lo if $x < $lo; |
1800 | $x = $lo if $x < $lo; |
1351 | $x = $hi - $page if $x > $hi - $page; |
1801 | $x = $hi - $page if $x > $hi - $page; |
1352 | $self->{range}[0] = $x; |
1802 | $self->{range}[0] = $x; |
1353 | |
1803 | |
1354 | $self->emit (changed => $x); |
1804 | $self->emit (changed => $x); |
1355 | $self->update; |
1805 | $self->update; |
1356 | } |
1806 | } |
|
|
1807 | } |
|
|
1808 | |
|
|
1809 | # the inner_* stuff is for generating a padding for the slider handle, |
|
|
1810 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
1811 | # correct propably, but it does the job for now |
|
|
1812 | sub _calc_inner_pad_px { |
|
|
1813 | my ($self, $w) = @_; |
|
|
1814 | ($w / 100) * $self->{inner_pad} # % to pixels |
1357 | } |
1815 | } |
1358 | |
1816 | |
1359 | sub _draw { |
1817 | sub _draw { |
1360 | my ($self) = @_; |
1818 | my ($self) = @_; |
1361 | |
1819 | |
… | |
… | |
1375 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1833 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1376 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1834 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1377 | |
1835 | |
1378 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1836 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1379 | |
1837 | |
|
|
1838 | $hi = $value + 1 if $lo == $hi; |
|
|
1839 | |
|
|
1840 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1841 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1842 | |
1380 | $page = int $page * $w / ($hi - $lo); |
1843 | $page = int $page * $inner_w / ($hi - $lo); |
1381 | $value = int +($value - $lo) * $w / ($hi - $lo); |
1844 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
1382 | |
1845 | |
1383 | $w -= $page; |
1846 | $w -= $page; |
1384 | $page &= ~1; |
1847 | $page &= ~1; |
1385 | glTranslate $page * 0.5, 0, 0; |
1848 | glTranslate $page * 0.5, 0, 0; |
1386 | $page ||= 2; |
1849 | $page ||= 2; |
1387 | |
1850 | |
1388 | glColor @$fg; |
|
|
1389 | glBegin GL_LINES; |
|
|
1390 | glVertex 0, 0; glVertex 0, $h; |
|
|
1391 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1392 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1393 | glEnd; |
|
|
1394 | |
|
|
1395 | my $knob_a = $value - $page * 0.5; |
1851 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
1396 | my $knob_b = $value + $page * 0.5; |
1852 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
1397 | |
1853 | |
1398 | glBegin GL_QUADS; |
1854 | glEnable GL_BLEND; |
1399 | glColor @$fg; |
1855 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1400 | glVertex $knob_a, 0; |
1856 | glEnable GL_TEXTURE_2D; |
1401 | glVertex $knob_a, $h; |
1857 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1402 | glVertex $knob_b, $h; |
|
|
1403 | glVertex $knob_b, 0; |
|
|
1404 | |
1858 | |
1405 | if ($knob_a < $knob_b - 2) { |
1859 | # draw background |
1406 | glColor @$bg; |
1860 | $tex[1]->draw_quad (0, 0, $w, $h); |
1407 | glVertex $knob_a + 1, 1; |
1861 | |
1408 | glVertex $knob_a + 1, $h - 1; |
1862 | # draw handle |
1409 | glVertex $knob_b - 1, $h - 1; |
1863 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
1410 | glVertex $knob_b - 1, 1; |
1864 | |
1411 | } |
1865 | glDisable GL_BLEND; |
1412 | glEnd; |
1866 | glDisable GL_TEXTURE_2D; |
1413 | } |
1867 | } |
1414 | |
1868 | |
1415 | ############################################################################# |
1869 | ############################################################################# |
1416 | |
1870 | |
1417 | package CFClient::UI::TextView; |
1871 | package CFClient::UI::TextView; |
1418 | |
1872 | |
1419 | our @ISA = CFClient::UI::HBox::; |
1873 | our @ISA = CFClient::UI::HBox::; |
1420 | |
1874 | |
1421 | use SDL::OpenGL; |
1875 | use CFClient::OpenGL; |
1422 | |
1876 | |
1423 | sub new { |
1877 | sub new { |
1424 | my $class = shift; |
1878 | my $class = shift; |
1425 | |
1879 | |
1426 | my $self = $class->SUPER::new ( |
1880 | my $self = $class->SUPER::new ( |
|
|
1881 | fontsize => 1, |
|
|
1882 | @_, |
|
|
1883 | |
|
|
1884 | layout => (new CFClient::Layout), |
1427 | par => [], |
1885 | par => [], |
1428 | @_, |
1886 | height => 0, |
1429 | children => [ |
1887 | children => [ |
1430 | (new CFClient::UI::Empty expand => 1), |
1888 | (new CFClient::UI::Empty expand => 1), |
1431 | (new CFClient::UI::Slider vertical => 1), |
1889 | (new CFClient::UI::Slider vertical => 1), |
1432 | ], |
1890 | ], |
1433 | ); |
1891 | ); |
1434 | |
1892 | |
|
|
1893 | $self->{children}[1]->connect (changed => sub { |
|
|
1894 | $self->update; |
|
|
1895 | }); |
|
|
1896 | |
1435 | $self |
1897 | $self |
|
|
1898 | } |
|
|
1899 | |
|
|
1900 | sub set_fontsize { |
|
|
1901 | my ($self, $fontsize) = @_; |
|
|
1902 | |
|
|
1903 | $self->{fontsize} = $fontsize; |
|
|
1904 | $self->reflow; |
|
|
1905 | } |
|
|
1906 | |
|
|
1907 | sub text_height { |
|
|
1908 | my ($self, $text) = @_; |
|
|
1909 | |
|
|
1910 | my $layout = $self->{layout}; |
|
|
1911 | |
|
|
1912 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1913 | $layout->set_width ($self->{w}); |
|
|
1914 | $layout->set_text ($text); |
|
|
1915 | |
|
|
1916 | ($layout->size)[1] |
|
|
1917 | } |
|
|
1918 | |
|
|
1919 | sub reflow { |
|
|
1920 | my ($self) = @_; |
|
|
1921 | |
|
|
1922 | $self->{need_reflow}++; |
|
|
1923 | $self->update; |
|
|
1924 | } |
|
|
1925 | |
|
|
1926 | sub size_allocate { |
|
|
1927 | my ($self, $w, $h) = @_; |
|
|
1928 | |
|
|
1929 | $self->SUPER::size_allocate ($w, $h); |
|
|
1930 | |
|
|
1931 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1932 | $self->{layout}->set_width ($self->{children}[0]{w}); |
|
|
1933 | |
|
|
1934 | $self->reflow; |
1436 | } |
1935 | } |
1437 | |
1936 | |
1438 | sub add_paragraph { |
1937 | sub add_paragraph { |
1439 | my ($self, $color, $text) = @_; |
1938 | my ($self, $color, $text) = @_; |
1440 | |
1939 | |
|
|
1940 | #TODO: intelligently "reformat" paragraph |
|
|
1941 | |
|
|
1942 | my $height = $self->text_height ($text); |
|
|
1943 | |
|
|
1944 | $self->{height} += $height; |
|
|
1945 | |
1441 | push @{$self->{par}}, [$color, $text]; |
1946 | push @{$self->{par}}, [$height, $color, $text]; |
1442 | my $count = scalar @{$self->{par}}; |
1947 | |
1443 | $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; |
1948 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
1444 | $self->{children}[1]->update; |
1949 | $self->{children}[1]->update; |
1445 | } |
1950 | } |
1446 | |
1951 | |
|
|
1952 | sub update { |
|
|
1953 | my ($self) = @_; |
|
|
1954 | |
|
|
1955 | $self->SUPER::update; |
|
|
1956 | |
|
|
1957 | return unless $self->{h} > 0; |
|
|
1958 | |
|
|
1959 | delete $self->{texture}; |
|
|
1960 | |
|
|
1961 | $ROOT->on_refresh ($self, sub { |
|
|
1962 | if (delete $self->{need_reflow}) { |
|
|
1963 | my $height = 0; |
|
|
1964 | |
|
|
1965 | $height += $_->[0] = $self->text_height ($_->[2]) |
|
|
1966 | for @{$self->{par}}; |
|
|
1967 | |
|
|
1968 | $self->{height} = $height; |
|
|
1969 | |
|
|
1970 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
|
|
1971 | |
|
|
1972 | delete $self->{texture}; |
|
|
1973 | } |
|
|
1974 | |
|
|
1975 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
|
|
1976 | glClearColor 0, 0, 0, 1; |
|
|
1977 | glClear GL_COLOR_BUFFER_BIT; |
|
|
1978 | |
|
|
1979 | glEnable GL_BLEND; |
|
|
1980 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1981 | glEnable GL_TEXTURE_2D; |
|
|
1982 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1983 | |
|
|
1984 | my $top = int $self->{children}[1]{range}[0]; |
|
|
1985 | |
|
|
1986 | my $y0 = $top; |
|
|
1987 | my $y1 = $top + $self->{h}; |
|
|
1988 | |
|
|
1989 | my $y = 0; |
|
|
1990 | |
|
|
1991 | my $layout = $self->{layout}; |
|
|
1992 | |
|
|
1993 | for my $par (@{$self->{par}}) { |
|
|
1994 | my $h = $par->[0]; |
|
|
1995 | |
|
|
1996 | if ($y0 < $y + $h && $y < $y1) { |
|
|
1997 | $layout->set_text ($par->[2]); |
|
|
1998 | |
|
|
1999 | glColor @{ $par->[1] }; |
|
|
2000 | my ($W, $H) = $layout->size; |
|
|
2001 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
|
|
2002 | } |
|
|
2003 | |
|
|
2004 | $y += $h; |
|
|
2005 | } |
|
|
2006 | |
|
|
2007 | glDisable GL_TEXTURE_2D; |
|
|
2008 | glDisable GL_BLEND; |
|
|
2009 | }; |
|
|
2010 | }); |
|
|
2011 | } |
|
|
2012 | |
1447 | sub _draw { |
2013 | sub _draw { |
1448 | my ($self) = @_; |
2014 | my ($self) = @_; |
1449 | |
2015 | |
1450 | my $par = $self->{par}; |
2016 | if ($self->{texture}) { |
1451 | |
|
|
1452 | my $bottom = (scalar @$par) - 1; |
|
|
1453 | |
|
|
1454 | my $w = $self->{children}[0]{w}; |
|
|
1455 | my $y = $self->{h}; |
|
|
1456 | |
|
|
1457 | glEnable GL_BLEND; |
|
|
1458 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1459 | glEnable GL_TEXTURE_2D; |
2017 | glEnable GL_TEXTURE_2D; |
1460 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
2018 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1461 | |
2019 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
1462 | # TODO: everything! |
|
|
1463 | while ($y > 0 && $bottom >= 0) { |
|
|
1464 | my $par = $par->[$bottom--]; |
|
|
1465 | |
|
|
1466 | my $layout = new CFClient::Layout; |
|
|
1467 | |
|
|
1468 | $layout->set_height ($::FONTSIZE); |
|
|
1469 | $layout->set_width ($w); |
|
|
1470 | $layout->set_text ($par->[1]); |
|
|
1471 | my $tex = new_from_layout CFClient::Texture $layout; |
|
|
1472 | $y -= $tex->{h}; |
|
|
1473 | |
|
|
1474 | glColor @{ $par->[0] }; |
|
|
1475 | $tex->draw_quad (0, $y); |
|
|
1476 | } |
|
|
1477 | |
|
|
1478 | glDisable GL_TEXTURE_2D; |
2020 | glDisable GL_TEXTURE_2D; |
1479 | glDisable GL_BLEND; |
|
|
1480 | } |
|
|
1481 | |
|
|
1482 | ############################################################################# |
|
|
1483 | |
|
|
1484 | package CFClient::UI::MapWidget; |
|
|
1485 | |
|
|
1486 | use strict; |
|
|
1487 | |
|
|
1488 | use List::Util qw(min max); |
|
|
1489 | |
|
|
1490 | use SDL; |
|
|
1491 | use SDL::OpenGL; |
|
|
1492 | |
|
|
1493 | our @ISA = CFClient::UI::Base::; |
|
|
1494 | |
|
|
1495 | sub new { |
|
|
1496 | my $class = shift; |
|
|
1497 | |
|
|
1498 | $class->SUPER::new ( |
|
|
1499 | z => -1, |
|
|
1500 | can_focus => 1, |
|
|
1501 | list => (glGenLists 1), |
|
|
1502 | @_ |
|
|
1503 | ) |
2021 | } |
1504 | } |
|
|
1505 | |
2022 | |
1506 | sub key_down { |
2023 | $self->{children}[1]->draw; |
1507 | print "MAPKEYDOWN\n"; |
|
|
1508 | } |
|
|
1509 | |
2024 | |
1510 | sub key_up { |
|
|
1511 | } |
|
|
1512 | |
|
|
1513 | sub size_request { |
|
|
1514 | ( |
|
|
1515 | 1 + 32 * int $::WIDTH / 32, |
|
|
1516 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1517 | ) |
|
|
1518 | } |
|
|
1519 | |
|
|
1520 | sub update { |
|
|
1521 | my ($self) = @_; |
|
|
1522 | |
|
|
1523 | $self->{need_update} = 1; |
|
|
1524 | $self->SUPER::update; |
|
|
1525 | } |
|
|
1526 | |
|
|
1527 | sub draw { |
|
|
1528 | my ($self) = @_; |
|
|
1529 | |
|
|
1530 | $self->{need_update}++;#d# |
|
|
1531 | if (delete $self->{need_update}) { |
|
|
1532 | glNewList $self->{list}, GL_COMPILE; |
|
|
1533 | |
|
|
1534 | my $sw = int $::WIDTH / 32; |
|
|
1535 | my $sh = int $::HEIGHT / 32; |
|
|
1536 | |
|
|
1537 | if ($::MAP) { |
|
|
1538 | my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh); |
|
|
1539 | |
|
|
1540 | if ($::CFG->{fow_enable}) { |
|
|
1541 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1542 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1543 | glConvolutionFilter2D |
|
|
1544 | GL_CONVOLUTION_2D, |
|
|
1545 | GL_ALPHA, |
|
|
1546 | 3, 3, |
|
|
1547 | GL_ALPHA, GL_FLOAT, |
|
|
1548 | pack "f*", |
|
|
1549 | 0.1, 0.1, 0.1, |
|
|
1550 | 0.1, 0.2, 0.1, |
|
|
1551 | 0.1, 0.1, 0.1, |
|
|
1552 | ; |
|
|
1553 | glEnable GL_CONVOLUTION_2D; |
|
|
1554 | } |
|
|
1555 | |
|
|
1556 | my $tex = new CFClient::Texture |
|
|
1557 | w => $w, |
|
|
1558 | h => $h, |
|
|
1559 | data => $data, |
|
|
1560 | internalformat => GL_ALPHA, |
|
|
1561 | format => GL_ALPHA; |
|
|
1562 | |
|
|
1563 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1564 | |
|
|
1565 | glEnable GL_BLEND; |
|
|
1566 | glEnable GL_TEXTURE_2D; |
|
|
1567 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1568 | |
|
|
1569 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1570 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1571 | |
|
|
1572 | glDisable GL_TEXTURE_2D; |
|
|
1573 | glDisable GL_BLEND; |
|
|
1574 | } |
|
|
1575 | } |
|
|
1576 | |
|
|
1577 | glEndList; |
|
|
1578 | } |
|
|
1579 | |
|
|
1580 | glCallList $self->{list}; |
|
|
1581 | |
|
|
1582 | if ($FOCUS != $self) { |
|
|
1583 | glColor 64/255, 64/255, 64/255; |
|
|
1584 | glLogicOp GL_AND; |
|
|
1585 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1586 | glBegin GL_QUADS; |
|
|
1587 | glVertex 0, 0; |
|
|
1588 | glVertex 0, $::HEIGHT; |
|
|
1589 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1590 | glVertex $::WIDTH, 0; |
|
|
1591 | glEnd; |
|
|
1592 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1593 | } |
|
|
1594 | } |
|
|
1595 | |
|
|
1596 | my %DIR = ( |
|
|
1597 | SDLK_KP8, [1, "north"], |
|
|
1598 | SDLK_KP9, [2, "northeast"], |
|
|
1599 | SDLK_KP6, [3, "east"], |
|
|
1600 | SDLK_KP3, [4, "southeast"], |
|
|
1601 | SDLK_KP2, [5, "south"], |
|
|
1602 | SDLK_KP1, [6, "southwest"], |
|
|
1603 | SDLK_KP4, [7, "west"], |
|
|
1604 | SDLK_KP7, [8, "northwest"], |
|
|
1605 | |
|
|
1606 | SDLK_UP, [1, "north"], |
|
|
1607 | SDLK_RIGHT, [3, "east"], |
|
|
1608 | SDLK_DOWN, [5, "south"], |
|
|
1609 | SDLK_LEFT, [7, "west"], |
|
|
1610 | ); |
|
|
1611 | |
|
|
1612 | sub key_down { |
|
|
1613 | my ($self, $ev) = @_; |
|
|
1614 | |
|
|
1615 | my $mod = $ev->key_mod; |
|
|
1616 | my $sym = $ev->key_sym; |
|
|
1617 | |
|
|
1618 | if ($sym == SDLK_KP5) { |
|
|
1619 | $::CONN->user_send ("command stay fire"); |
|
|
1620 | } elsif ($sym == SDLK_a) { |
|
|
1621 | $::CONN->user_send ("command apply"); |
|
|
1622 | } elsif (exists $DIR{$sym}) { |
|
|
1623 | if ($mod & KMOD_SHIFT) { |
|
|
1624 | $self->{shft}++; |
|
|
1625 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
|
|
1626 | } elsif ($mod & KMOD_CTRL) { |
|
|
1627 | $self->{ctrl}++; |
|
|
1628 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
|
|
1629 | } else { |
|
|
1630 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
|
|
1631 | } |
|
|
1632 | } |
|
|
1633 | } |
|
|
1634 | |
|
|
1635 | sub key_up { |
|
|
1636 | my ($self, $ev) = @_; |
|
|
1637 | |
|
|
1638 | my $mod = $ev->key_mod; |
|
|
1639 | my $sym = $ev->key_sym; |
|
|
1640 | |
|
|
1641 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1642 | $::CONN->user_send ("command fire_stop"); |
|
|
1643 | } |
|
|
1644 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1645 | $::CONN->user_send ("command run_stop"); |
|
|
1646 | } |
|
|
1647 | } |
2025 | } |
1648 | |
2026 | |
1649 | ############################################################################# |
2027 | ############################################################################# |
1650 | |
2028 | |
1651 | package CFClient::UI::Animator; |
2029 | package CFClient::UI::Animator; |
1652 | |
2030 | |
1653 | use SDL::OpenGL; |
2031 | use CFClient::OpenGL; |
1654 | |
2032 | |
1655 | our @ISA = CFClient::UI::Bin::; |
2033 | our @ISA = CFClient::UI::Bin::; |
1656 | |
2034 | |
1657 | sub moveto { |
2035 | sub moveto { |
1658 | my ($self, $x, $y) = @_; |
2036 | my ($self, $x, $y) = @_; |
… | |
… | |
1698 | my $class = shift; |
2076 | my $class = shift; |
1699 | |
2077 | |
1700 | my $self = $class->SUPER::new ( |
2078 | my $self = $class->SUPER::new ( |
1701 | state => 0, |
2079 | state => 0, |
1702 | connect_activate => \&toggle_flopper, |
2080 | connect_activate => \&toggle_flopper, |
|
|
2081 | can_events => 1, |
1703 | @_ |
2082 | @_ |
1704 | ); |
2083 | ); |
1705 | |
2084 | |
1706 | if ($self->{state}) { |
2085 | if ($self->{state}) { |
1707 | $self->{state} = 0; |
2086 | $self->{state} = 0; |
… | |
… | |
1714 | sub toggle_flopper { |
2093 | sub toggle_flopper { |
1715 | my ($self) = @_; |
2094 | my ($self) = @_; |
1716 | |
2095 | |
1717 | # TODO: use animation |
2096 | # TODO: use animation |
1718 | if ($self->{state} = !$self->{state}) { |
2097 | if ($self->{state} = !$self->{state}) { |
1719 | $CFClient::UI::TOPLEVEL->add ($self->{other}); |
2098 | $CFClient::UI::ROOT->add ($self->{other}); |
1720 | $self->{other}->move ( |
2099 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
1721 | ($::WIDTH - $self->{other}{w}) * 0.5, |
2100 | $self->emit ("open"); |
1722 | ($::HEIGHT - $self->{other}{h}) * 0.5, |
|
|
1723 | ); |
|
|
1724 | } else { |
2101 | } else { |
1725 | $CFClient::UI::TOPLEVEL->remove ($self->{other}); |
2102 | $CFClient::UI::ROOT->remove ($self->{other}); |
|
|
2103 | $self->emit ("close"); |
1726 | } |
2104 | } |
1727 | } |
|
|
1728 | |
2105 | |
1729 | ############################################################################# |
2106 | $self->emit (changed => $self->{state}); |
|
|
2107 | } |
1730 | |
2108 | |
|
|
2109 | ############################################################################# |
|
|
2110 | |
1731 | package CFClient::UI::Toplevel; |
2111 | package CFClient::UI::Root; |
1732 | |
2112 | |
1733 | our @ISA = CFClient::UI::Container::; |
2113 | our @ISA = CFClient::UI::Container::; |
|
|
2114 | |
|
|
2115 | use CFClient::OpenGL; |
|
|
2116 | |
|
|
2117 | sub check_size { |
|
|
2118 | my ($self) = @_; |
|
|
2119 | |
|
|
2120 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
|
|
2121 | } |
1734 | |
2122 | |
1735 | sub size_request { |
2123 | sub size_request { |
1736 | ($::WIDTH, $::HEIGHT) |
2124 | ($::WIDTH, $::HEIGHT) |
1737 | } |
2125 | } |
1738 | |
2126 | |
1739 | sub size_allocate { |
2127 | sub configure { |
1740 | my ($self, $x, $y, $w, $h) = @_; |
2128 | my ($self, $x, $y, $w, $h) = @_; |
1741 | |
2129 | |
1742 | $self->_size_allocate ($x, $y, $w, $h); |
2130 | $self->SUPER::configure ($x, $y, $w, $h); |
1743 | |
2131 | |
1744 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
|
|
1745 | for @{$self->{children}}; |
2132 | for my $child (@{$self->{children}}) { |
1746 | } |
2133 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
1747 | |
2134 | |
1748 | sub translate { |
2135 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2136 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2137 | $child->configure ($X, $Y, $W,$H); |
|
|
2138 | } |
|
|
2139 | } |
|
|
2140 | |
|
|
2141 | sub _topleft { |
1749 | my ($self, $x, $y) = @_; |
2142 | my ($self, $x, $y) = @_; |
1750 | |
2143 | |
1751 | ($x, $y) |
2144 | ($x, $y) |
1752 | } |
2145 | } |
1753 | |
2146 | |
1754 | sub update { |
2147 | sub update { |
1755 | my ($self) = @_; |
2148 | my ($self) = @_; |
1756 | |
2149 | |
1757 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2150 | $self->check_size; |
1758 | ::refresh (); |
2151 | ::refresh (); |
1759 | } |
2152 | } |
1760 | |
2153 | |
1761 | sub add { |
2154 | sub add { |
1762 | my ($self, $widget) = @_; |
2155 | my ($self, $child) = @_; |
1763 | |
2156 | |
|
|
2157 | # integerize window positions |
|
|
2158 | $child->{x} = int $child->{x}; |
|
|
2159 | $child->{y} = int $child->{y}; |
|
|
2160 | |
1764 | $self->SUPER::add ($widget); |
2161 | $self->SUPER::add ($child); |
|
|
2162 | } |
1765 | |
2163 | |
1766 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
2164 | sub on_refresh { |
|
|
2165 | my ($self, $id, $cb) = @_; |
|
|
2166 | |
|
|
2167 | $self->{refresh_hook}{$id} = $cb; |
1767 | } |
2168 | } |
1768 | |
2169 | |
1769 | sub draw { |
2170 | sub draw { |
1770 | my ($self) = @_; |
2171 | my ($self) = @_; |
1771 | |
2172 | |
|
|
2173 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2174 | $_->() for values %$rcb; |
|
|
2175 | } |
|
|
2176 | |
|
|
2177 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2178 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2179 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2180 | |
|
|
2181 | glMatrixMode GL_PROJECTION; |
|
|
2182 | glLoadIdentity; |
|
|
2183 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2184 | glMatrixMode GL_MODELVIEW; |
|
|
2185 | glLoadIdentity; |
|
|
2186 | |
1772 | $self->_draw; |
2187 | $self->_draw; |
1773 | } |
2188 | } |
1774 | |
2189 | |
1775 | ############################################################################# |
2190 | ############################################################################# |
1776 | |
2191 | |
1777 | package CFClient::UI; |
2192 | package CFClient::UI; |
1778 | |
2193 | |
1779 | $TOPLEVEL = new CFClient::UI::Toplevel; |
2194 | $ROOT = new CFClient::UI::Root; |
1780 | |
2195 | |
1781 | 1 |
2196 | 1 |
1782 | |
2197 | |