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