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