… | |
… | |
17 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
18 | |
18 | |
19 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
20 | |
20 | |
21 | sub get_layout { |
21 | sub get_layout { |
|
|
22 | my $layout; |
|
|
23 | |
22 | for (grep { $_->{name} } values %WIDGET) { |
24 | for (grep { $_->{name} } values %WIDGET) { |
23 | $LAYOUT->{$_->{name}} = { |
25 | my $win = $layout->{$_->{name}} = { }; |
24 | x => $_->{x} / $::WIDTH, |
|
|
25 | y => $_->{y} / $::HEIGHT, |
|
|
26 | w => $_->{w} / $::WIDTH, |
|
|
27 | h => $_->{h} / $::HEIGHT |
|
|
28 | }; |
26 | |
29 | } |
27 | $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/; |
|
|
28 | $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/; |
|
|
29 | $win->{w} = $_->{w} / $::WIDTH if defined $_->{w}; |
|
|
30 | $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h}; |
30 | |
31 | |
31 | return $LAYOUT; |
32 | $win->{show} = $_->{visible} && $_->{is_toplevel}; |
|
|
33 | } |
|
|
34 | |
|
|
35 | $layout |
32 | } |
36 | } |
33 | |
37 | |
34 | sub set_layout { |
38 | sub set_layout { |
35 | my ($layout) = @_; |
39 | my ($layout) = @_; |
|
|
40 | |
36 | $LAYOUT = $layout; |
41 | $LAYOUT = $layout; |
37 | } |
42 | } |
38 | |
43 | |
39 | sub check_tooltip { |
44 | sub check_tooltip { |
40 | if (!$GRAB) { |
45 | if (!$GRAB) { |
41 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
46 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
42 | if (length $widget->{tooltip}) { |
47 | if (length $widget->{tooltip}) { |
43 | |
48 | |
44 | if ($TOOLTIP->{owner} != $widget) { |
49 | if ($TOOLTIP->{owner} != $widget) { |
|
|
50 | $TOOLTIP->hide; |
|
|
51 | |
45 | $TOOLTIP->{owner} = $widget; |
52 | $TOOLTIP->{owner} = $widget; |
46 | |
53 | |
47 | my $tip = $widget->{tooltip}; |
54 | my $tip = $widget->{tooltip}; |
48 | |
55 | |
49 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
56 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
50 | |
57 | |
51 | $TOOLTIP->set_tooltip_from ($widget); |
58 | $TOOLTIP->set_tooltip_from ($widget); |
52 | $TOOLTIP->show; |
59 | $TOOLTIP->show; |
53 | |
|
|
54 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
|
|
55 | |
|
|
56 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0) |
|
|
57 | if $x + $TOOLTIP->{w} > $::WIDTH; |
|
|
58 | |
|
|
59 | $TOOLTIP->move ($x, $y); |
|
|
60 | $TOOLTIP->check_size; |
|
|
61 | $TOOLTIP->update; |
|
|
62 | } |
60 | } |
63 | |
61 | |
64 | return; |
62 | return; |
65 | } |
63 | } |
66 | } |
64 | } |
… | |
… | |
172 | sub rescale_widgets { |
170 | sub rescale_widgets { |
173 | my ($sx, $sy) = @_; |
171 | my ($sx, $sy) = @_; |
174 | |
172 | |
175 | for my $widget (values %WIDGET) { |
173 | for my $widget (values %WIDGET) { |
176 | if ($widget->{is_toplevel}) { |
174 | if ($widget->{is_toplevel}) { |
|
|
175 | $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
|
|
176 | $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
|
|
177 | |
177 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
178 | $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; |
178 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
179 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
179 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
180 | $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w}; |
180 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
181 | $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/; |
181 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
182 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
182 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
183 | $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h}; |
|
|
184 | |
|
|
185 | $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
|
|
186 | $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
|
|
187 | |
183 | } |
188 | } |
184 | } |
189 | } |
185 | |
190 | |
186 | reconfigure_widgets; |
191 | reconfigure_widgets; |
187 | } |
192 | } |
… | |
… | |
196 | |
201 | |
197 | sub new { |
202 | sub new { |
198 | my $class = shift; |
203 | my $class = shift; |
199 | |
204 | |
200 | my $self = bless { |
205 | my $self = bless { |
201 | x => 0, |
206 | x => "center", |
202 | y => 0, |
207 | y => "center", |
203 | z => 0, |
208 | z => 0, |
|
|
209 | w => undef, |
|
|
210 | h => undef, |
204 | can_events => 1, |
211 | can_events => 1, |
205 | @_ |
212 | @_ |
206 | }, $class; |
213 | }, $class; |
|
|
214 | |
|
|
215 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
207 | |
216 | |
208 | for (keys %$self) { |
217 | for (keys %$self) { |
209 | if (/^on_(.*)$/) { |
218 | if (/^on_(.*)$/) { |
210 | $self->connect ($1 => delete $self->{$_}); |
219 | $self->connect ($1 => delete $self->{$_}); |
211 | } |
220 | } |
212 | } |
221 | } |
213 | |
222 | |
214 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
215 | |
|
|
216 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
223 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
217 | $self->{req_x} = $layout->{x} * $::WIDTH; |
224 | $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x}; |
218 | $self->{req_y} = $layout->{y} * $::HEIGHT; |
225 | $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y}; |
219 | $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; |
226 | $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w}; |
220 | $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; |
227 | $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h}; |
|
|
228 | |
|
|
229 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
|
|
230 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
|
|
231 | |
|
|
232 | $self->show if $layout->{show}; |
221 | } |
233 | } |
222 | |
234 | |
223 | $self |
235 | $self |
224 | } |
|
|
225 | |
|
|
226 | sub toggle_visibility { |
|
|
227 | my ($self) = @_; |
|
|
228 | |
|
|
229 | if ($self->{visible}) { |
|
|
230 | $self->hide; |
|
|
231 | } else { |
|
|
232 | $self->show; |
|
|
233 | } |
|
|
234 | } |
236 | } |
235 | |
237 | |
236 | sub destroy { |
238 | sub destroy { |
237 | my ($self) = @_; |
239 | my ($self) = @_; |
238 | |
240 | |
… | |
… | |
240 | %$self = (); |
242 | %$self = (); |
241 | } |
243 | } |
242 | |
244 | |
243 | sub show { |
245 | sub show { |
244 | my ($self) = @_; |
246 | my ($self) = @_; |
|
|
247 | |
245 | return if $self->{parent}; |
248 | return if $self->{parent}; |
246 | |
249 | |
247 | $CFClient::UI::ROOT->add ($self); |
250 | $CFClient::UI::ROOT->add ($self); |
248 | } |
|
|
249 | |
|
|
250 | sub center { |
|
|
251 | my ($self) = @_; |
|
|
252 | |
|
|
253 | $CFClient::UI::ROOT->on_post_alloc ( |
|
|
254 | "center_$self" => sub { |
|
|
255 | $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); |
|
|
256 | }, |
|
|
257 | ); |
|
|
258 | |
|
|
259 | $self->update; |
|
|
260 | } |
251 | } |
261 | |
252 | |
262 | sub set_visible { |
253 | sub set_visible { |
263 | my ($self) = @_; |
254 | my ($self) = @_; |
264 | |
255 | |
265 | return if $self->{visible}; |
256 | return if $self->{visible}; |
266 | |
257 | |
267 | $self->{root} = $self->{parent}{root}; |
258 | $self->{root} = $self->{parent}{root}; |
268 | $self->{visible} = $self->{parent}{visible} + 1; |
259 | $self->{visible} = $self->{parent}{visible} + 1; |
269 | |
260 | |
270 | $self->emit (visibility_change => 1) |
261 | $self->emit (visibility_change => 1); |
|
|
262 | |
|
|
263 | $self->realloc if !exists $self->{req_w}; |
|
|
264 | |
|
|
265 | $_->set_visible for $self->children; |
271 | } |
266 | } |
272 | |
267 | |
273 | sub set_invisible { |
268 | sub set_invisible { |
274 | my ($self) = @_; |
269 | my ($self) = @_; |
275 | |
270 | |
276 | return unless $self->{visible}; |
271 | return unless $self->{visible}; |
277 | |
272 | |
278 | # broken show/hide model |
273 | $_->set_invisible for $self->children; |
279 | |
274 | |
280 | delete $self->{root}; |
275 | delete $self->{root}; |
281 | delete $self->{visible}; |
276 | delete $self->{visible}; |
282 | |
277 | |
283 | undef $GRAB if $GRAB == $self; |
278 | undef $GRAB if $GRAB == $self; |
284 | undef $HOVER if $HOVER == $self; |
279 | undef $HOVER if $HOVER == $self; |
285 | |
280 | |
286 | CFClient::UI::check_tooltip |
281 | CFClient::UI::check_tooltip |
287 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
282 | if $TOOLTIP->{owner} == $self; |
288 | |
283 | |
289 | $self->focus_out; |
284 | $self->focus_out; |
290 | |
285 | |
291 | $self->emit (visibility_change => 0); |
286 | $self->emit (visibility_change => 0); |
|
|
287 | } |
|
|
288 | |
|
|
289 | sub set_visibility { |
|
|
290 | my ($self, $visible) = @_; |
|
|
291 | |
|
|
292 | return if $self->{visible} == $visible; |
|
|
293 | |
|
|
294 | $visible ? $self->hide |
|
|
295 | : $self->show; |
|
|
296 | } |
|
|
297 | |
|
|
298 | sub toggle_visibility { |
|
|
299 | my ($self) = @_; |
|
|
300 | |
|
|
301 | $self->{visible} |
|
|
302 | ? $self->hide |
|
|
303 | : $self->show; |
292 | } |
304 | } |
293 | |
305 | |
294 | sub hide { |
306 | sub hide { |
295 | my ($self) = @_; |
307 | my ($self) = @_; |
296 | |
308 | |
… | |
… | |
298 | |
310 | |
299 | $self->{parent}->remove ($self) |
311 | $self->{parent}->remove ($self) |
300 | if $self->{parent}; |
312 | if $self->{parent}; |
301 | } |
313 | } |
302 | |
314 | |
303 | sub move { |
315 | sub move_abs { |
304 | my ($self, $x, $y, $z) = @_; |
316 | my ($self, $x, $y, $z) = @_; |
305 | |
317 | |
306 | $self->{x} = int $x; |
318 | $self->{x} = List::Util::max 0, int $x; |
307 | $self->{y} = int $y; |
319 | $self->{y} = List::Util::max 0, int $y; |
308 | $self->{z} = $z if defined $z; |
320 | $self->{z} = $z if defined $z; |
309 | |
321 | |
310 | $self->update; |
322 | $self->update; |
311 | } |
323 | } |
312 | |
324 | |
313 | sub set_size { |
325 | sub set_size { |
314 | my ($self, $w, $h) = @_; |
326 | my ($self, $w, $h) = @_; |
315 | |
327 | |
316 | $self->{def_w} = $w; |
328 | $self->{force_w} = $w; |
317 | $self->{def_h} = $h; |
329 | $self->{force_h} = $h; |
318 | |
330 | |
319 | $self->check_size; |
331 | $self->realloc; |
320 | } |
332 | } |
321 | |
333 | |
322 | sub size_request { |
334 | sub size_request { |
323 | require Carp; |
335 | require Carp; |
324 | Carp::confess "size_request is abstract"; |
336 | Carp::confess "size_request is abstract"; |
… | |
… | |
326 | |
338 | |
327 | sub configure { |
339 | sub configure { |
328 | my ($self, $x, $y, $w, $h) = @_; |
340 | my ($self, $x, $y, $w, $h) = @_; |
329 | |
341 | |
330 | if ($self->{aspect}) { |
342 | if ($self->{aspect}) { |
|
|
343 | my ($ow, $oh) = ($w, $h); |
|
|
344 | |
331 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
345 | $w = List::Util::min $w, int $h * $self->{aspect}; |
332 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
346 | $h = List::Util::min $h, int $w / $self->{aspect}; |
333 | |
347 | |
334 | # use alignment to adjust x, y |
348 | # use alignment to adjust x, y |
335 | |
349 | |
336 | $x += int +($w - $w2) * 0.5; |
350 | $x += int 0.5 * ($ow - $w); |
337 | $y += int +($h - $h2) * 0.5; |
351 | $y += int 0.5 * ($oh - $h); |
338 | |
|
|
339 | ($w, $h) = ($w2, $h2); |
|
|
340 | } |
352 | } |
341 | |
353 | |
342 | if ($self->{x} != $x || $self->{y} != $y) { |
354 | if ($self->{x} ne $x || $self->{y} ne $y) { |
343 | $self->{x} = $x; |
355 | $self->{x} = $x; |
344 | $self->{y} = $y; |
356 | $self->{y} = $y; |
345 | $self->update; |
357 | $self->update; |
346 | } |
358 | } |
347 | |
359 | |
348 | if ($self->{w} != $w || $self->{h} != $h) { |
360 | if ($self->{w} != $w || $self->{h} != $h) { |
|
|
361 | return unless $self->{visible}; |
|
|
362 | |
349 | $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; |
363 | $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h]; |
350 | } |
364 | } |
351 | } |
365 | } |
352 | |
366 | |
353 | sub size_allocate { |
367 | sub size_allocate { |
354 | # nothing to be done |
368 | # nothing to be done |
355 | } |
|
|
356 | |
|
|
357 | sub reconfigure { |
|
|
358 | my ($self) = @_; |
|
|
359 | |
|
|
360 | $self->check_size (1); |
|
|
361 | $self->update; |
|
|
362 | } |
369 | } |
363 | |
370 | |
364 | sub children { |
371 | sub children { |
365 | } |
372 | } |
366 | |
373 | |
… | |
… | |
444 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
451 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
445 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
452 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
446 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
453 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
447 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
454 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
448 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
455 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
|
|
456 | |
|
|
457 | sub find_widget { |
|
|
458 | my ($self, $x, $y) = @_; |
|
|
459 | |
|
|
460 | return () unless $self->{can_events}; |
|
|
461 | |
|
|
462 | return $self |
|
|
463 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
464 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
|
|
465 | |
|
|
466 | () |
|
|
467 | } |
|
|
468 | |
|
|
469 | sub set_parent { |
|
|
470 | my ($self, $parent) = @_; |
|
|
471 | |
|
|
472 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
473 | $self->set_visible if $parent->{visible}; |
|
|
474 | } |
|
|
475 | |
|
|
476 | sub connect { |
|
|
477 | my ($self, $signal, $cb) = @_; |
|
|
478 | |
|
|
479 | push @{ $self->{signal_cb}{$signal} }, $cb; |
|
|
480 | } |
|
|
481 | |
|
|
482 | sub _emit { |
|
|
483 | my ($self, $signal, @args) = @_; |
|
|
484 | |
|
|
485 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
|
|
486 | } |
|
|
487 | |
|
|
488 | sub emit { |
|
|
489 | my ($self, $signal, @args) = @_; |
|
|
490 | |
|
|
491 | $self->_emit ($signal, @args) |
|
|
492 | || $self->$signal (@args); |
|
|
493 | } |
|
|
494 | |
|
|
495 | sub visibility_change { |
|
|
496 | #my ($self, $visible) = @_; |
|
|
497 | } |
|
|
498 | |
|
|
499 | sub realloc { |
|
|
500 | my ($self) = @_; |
|
|
501 | |
|
|
502 | if ($self->{visible}) { |
|
|
503 | return if $self->{root}{realloc}{$self}; |
|
|
504 | |
|
|
505 | $self->{root}{realloc}{$self} = $self; |
|
|
506 | $self->{root}->update; |
|
|
507 | } else { |
|
|
508 | delete $self->{req_w}; |
|
|
509 | } |
|
|
510 | } |
|
|
511 | |
|
|
512 | sub update { |
|
|
513 | my ($self) = @_; |
|
|
514 | |
|
|
515 | $self->{parent}->update |
|
|
516 | if $self->{parent}; |
|
|
517 | } |
|
|
518 | |
|
|
519 | sub reconfigure { |
|
|
520 | my ($self) = @_; |
|
|
521 | |
|
|
522 | $self->realloc; |
|
|
523 | $self->update; |
|
|
524 | } |
449 | |
525 | |
450 | sub draw { |
526 | sub draw { |
451 | my ($self) = @_; |
527 | my ($self) = @_; |
452 | |
528 | |
453 | return unless $self->{h} && $self->{w}; |
529 | return unless $self->{h} && $self->{w}; |
… | |
… | |
470 | glVertex $x , $y + $self->{h}; |
546 | glVertex $x , $y + $self->{h}; |
471 | glEnd; |
547 | glEnd; |
472 | glDisable GL_BLEND; |
548 | glDisable GL_BLEND; |
473 | } |
549 | } |
474 | |
550 | |
475 | if ($ENV{PCLIENT_DEBUG}) { |
551 | if ($ENV{CFPLUS_DEBUG}) { |
476 | glPushMatrix; |
552 | glPushMatrix; |
477 | glColor 1, 1, 0, 1; |
553 | glColor 1, 1, 0, 1; |
478 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
554 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
479 | glBegin GL_LINE_LOOP; |
555 | glBegin GL_LINE_LOOP; |
480 | glVertex 0 , 0; |
556 | glVertex 0 , 0; |
… | |
… | |
491 | my ($self) = @_; |
567 | my ($self) = @_; |
492 | |
568 | |
493 | warn "no draw defined for $self\n"; |
569 | warn "no draw defined for $self\n"; |
494 | } |
570 | } |
495 | |
571 | |
496 | sub find_widget { |
|
|
497 | my ($self, $x, $y) = @_; |
|
|
498 | |
|
|
499 | return () unless $self->{can_events}; |
|
|
500 | |
|
|
501 | return $self |
|
|
502 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
503 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
|
|
504 | |
|
|
505 | () |
|
|
506 | } |
|
|
507 | |
|
|
508 | sub set_parent { |
|
|
509 | my ($self, $parent) = @_; |
|
|
510 | |
|
|
511 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
512 | |
|
|
513 | $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible}; |
|
|
514 | |
|
|
515 | $self->check_size; |
|
|
516 | } |
|
|
517 | |
|
|
518 | sub check_size { |
|
|
519 | my ($self, $forced) = @_; |
|
|
520 | |
|
|
521 | $self->{force_alloc} = 1 if $forced; |
|
|
522 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
|
|
523 | } |
|
|
524 | |
|
|
525 | sub update { |
|
|
526 | my ($self) = @_; |
|
|
527 | |
|
|
528 | $self->{parent}->update |
|
|
529 | if $self->{parent}; |
|
|
530 | } |
|
|
531 | |
|
|
532 | sub connect { |
|
|
533 | my ($self, $signal, $cb) = @_; |
|
|
534 | |
|
|
535 | push @{ $self->{signal_cb}{$signal} }, $cb; |
|
|
536 | } |
|
|
537 | |
|
|
538 | sub _emit { |
|
|
539 | my ($self, $signal, @args) = @_; |
|
|
540 | |
|
|
541 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
|
|
542 | } |
|
|
543 | |
|
|
544 | sub emit { |
|
|
545 | my ($self, $signal, @args) = @_; |
|
|
546 | |
|
|
547 | $self->_emit ($signal, @args) |
|
|
548 | || $self->$signal (@args); |
|
|
549 | } |
|
|
550 | |
|
|
551 | sub visibility_change { |
|
|
552 | #my ($self, $visible) = @_; |
|
|
553 | } |
|
|
554 | |
|
|
555 | sub DESTROY { |
572 | sub DESTROY { |
556 | my ($self) = @_; |
573 | my ($self) = @_; |
557 | |
574 | |
558 | delete $WIDGET{$self+0}; |
575 | delete $WIDGET{$self+0}; |
559 | #$self->deactivate; |
576 | #$self->deactivate; |
… | |
… | |
615 | my ($class, %arg) = @_; |
632 | my ($class, %arg) = @_; |
616 | $class->SUPER::new (can_events => 0, %arg); |
633 | $class->SUPER::new (can_events => 0, %arg); |
617 | } |
634 | } |
618 | |
635 | |
619 | sub size_request { |
636 | sub size_request { |
620 | (0, 0) |
637 | my ($self) = @_; |
|
|
638 | |
|
|
639 | ($self->{w} + 0, $self->{h} + 0) |
621 | } |
640 | } |
622 | |
641 | |
623 | sub draw { } |
642 | sub draw { } |
624 | |
643 | |
625 | ############################################################################# |
644 | ############################################################################# |
… | |
… | |
654 | $self->{children} = [ |
673 | $self->{children} = [ |
655 | sort { $a->{z} <=> $b->{z} } |
674 | sort { $a->{z} <=> $b->{z} } |
656 | @{$self->{children}}, @widgets |
675 | @{$self->{children}}, @widgets |
657 | ]; |
676 | ]; |
658 | |
677 | |
659 | $self->check_size (1); |
678 | $self->realloc; |
660 | $self->update; |
|
|
661 | } |
679 | } |
662 | |
680 | |
663 | sub children { |
681 | sub children { |
664 | @{ $_[0]{children} } |
682 | @{ $_[0]{children} } |
665 | } |
683 | } |
… | |
… | |
670 | delete $child->{parent}; |
688 | delete $child->{parent}; |
671 | $child->hide; |
689 | $child->hide; |
672 | |
690 | |
673 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
691 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
674 | |
692 | |
675 | $self->check_size (1); |
693 | $self->realloc; |
676 | $self->update; |
|
|
677 | } |
694 | } |
678 | |
695 | |
679 | sub clear { |
696 | sub clear { |
680 | my ($self) = @_; |
697 | my ($self) = @_; |
681 | |
698 | |
… | |
… | |
685 | for (@$children) { |
702 | for (@$children) { |
686 | delete $_->{parent}; |
703 | delete $_->{parent}; |
687 | $_->hide; |
704 | $_->hide; |
688 | } |
705 | } |
689 | |
706 | |
690 | $self->check_size; |
707 | $self->realloc; |
691 | $self->update; |
|
|
692 | } |
708 | } |
693 | |
709 | |
694 | sub find_widget { |
710 | sub find_widget { |
695 | my ($self, $x, $y) = @_; |
711 | my ($self, $x, $y) = @_; |
696 | |
712 | |
… | |
… | |
749 | sub size_request { |
765 | sub size_request { |
750 | $_[0]{children}[0]->size_request |
766 | $_[0]{children}[0]->size_request |
751 | } |
767 | } |
752 | |
768 | |
753 | sub size_allocate { |
769 | sub size_allocate { |
754 | my ($self, $w, $h) = @_; |
770 | my ($self, $w, $h, $changed) = @_; |
755 | |
771 | |
756 | $self->{children}[0]->configure (0, 0, $w, $h); |
772 | $self->{children}[0]->configure (0, 0, $w, $h); |
757 | } |
773 | } |
758 | |
774 | |
759 | ############################################################################# |
775 | ############################################################################# |
… | |
… | |
776 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
792 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
777 | $self->SUPER::update; |
793 | $self->SUPER::update; |
778 | } |
794 | } |
779 | |
795 | |
780 | sub size_allocate { |
796 | sub size_allocate { |
781 | my ($self, $w, $h) = @_; |
797 | my ($self, $w, $h, $changed) = @_; |
782 | |
798 | |
783 | $self->SUPER::size_allocate ($w, $h); |
799 | $self->SUPER::size_allocate ($w, $h, $changed); |
784 | $self->update; |
800 | $self->update |
|
|
801 | if $changed; |
785 | } |
802 | } |
786 | |
803 | |
787 | sub _render { |
804 | sub _render { |
788 | $_[0]{children}[0]->draw; |
805 | $_[0]{children}[0]->draw; |
789 | } |
806 | } |
… | |
… | |
842 | |
859 | |
843 | ($w, $h) |
860 | ($w, $h) |
844 | } |
861 | } |
845 | |
862 | |
846 | sub size_allocate { |
863 | sub size_allocate { |
847 | my ($self, $w, $h) = @_; |
864 | my ($self, $w, $h, $changed) = @_; |
848 | |
865 | |
849 | $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; |
866 | $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; |
850 | $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; |
867 | $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; |
851 | |
868 | |
852 | $self->child->configure (0, 0, $w, $h); |
869 | $self->child->configure (0, 0, $w, $h); |
… | |
… | |
939 | my $child = $self->{vp}->child; |
956 | my $child = $self->{vp}->child; |
940 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
957 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
941 | } |
958 | } |
942 | |
959 | |
943 | sub size_allocate { |
960 | sub size_allocate { |
944 | my ($self, $w, $h) = @_; |
961 | my ($self, $w, $h, $changed) = @_; |
945 | |
962 | |
946 | $self->SUPER::size_allocate ($w, $h); |
963 | $self->SUPER::size_allocate ($w, $h, $changed); |
947 | |
964 | |
948 | my $child = $self->{vp}->child; |
965 | my $child = $self->{vp}->child; |
949 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
966 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
950 | } |
967 | } |
951 | |
968 | |
… | |
… | |
998 | |
1015 | |
999 | our @ISA = CFClient::UI::Bin::; |
1016 | our @ISA = CFClient::UI::Bin::; |
1000 | |
1017 | |
1001 | use CFClient::OpenGL; |
1018 | use CFClient::OpenGL; |
1002 | |
1019 | |
1003 | my @tex = |
1020 | my $bg = |
|
|
1021 | new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png", |
|
|
1022 | mipmap => 1, wrap => 1; |
|
|
1023 | |
|
|
1024 | my @border = |
1004 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1025 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1005 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1026 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1006 | |
1027 | |
1007 | sub new { |
1028 | sub new { |
1008 | my $class = shift; |
1029 | my $class = shift; |
1009 | |
1030 | |
1010 | my $self = $class->SUPER::new ( |
1031 | my $self = $class->SUPER::new ( |
1011 | bg => [1, 1, 1, 1], |
1032 | bg => [1, 1, 1, 1], |
1012 | border_bg => [1, 1, 1, 1], |
1033 | border_bg => [1, 1, 1, 1], |
1013 | border => 0.6, |
1034 | border => 0.6, |
1014 | is_toplevel => 1, |
|
|
1015 | can_events => 1, |
1035 | can_events => 1, |
|
|
1036 | min_w => 16, |
|
|
1037 | min_h => 16, |
1016 | @_ |
1038 | @_ |
1017 | ); |
1039 | ); |
1018 | |
1040 | |
1019 | $self->{title} &&= new CFClient::UI::Label |
1041 | $self->{title} &&= new CFClient::UI::Label |
1020 | align => 0, |
1042 | align => 0, |
… | |
… | |
1039 | $h + $self->border * 2, |
1061 | $h + $self->border * 2, |
1040 | ) |
1062 | ) |
1041 | } |
1063 | } |
1042 | |
1064 | |
1043 | sub size_allocate { |
1065 | sub size_allocate { |
1044 | my ($self, $w, $h) = @_; |
1066 | my ($self, $w, $h, $changed) = @_; |
|
|
1067 | |
|
|
1068 | return unless $changed; |
1045 | |
1069 | |
1046 | $h -= List::Util::max 0, $self->border * 2; |
1070 | $h -= List::Util::max 0, $self->border * 2; |
1047 | $w -= List::Util::max 0, $self->border * 2; |
1071 | $w -= List::Util::max 0, $self->border * 2; |
1048 | |
1072 | |
1049 | $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) |
1073 | $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) |
… | |
… | |
1073 | my ($ev, $x, $y) = @_; |
1097 | my ($ev, $x, $y) = @_; |
1074 | |
1098 | |
1075 | my $dx = $ev->{x} - $ox; |
1099 | my $dx = $ev->{x} - $ox; |
1076 | my $dy = $ev->{y} - $oy; |
1100 | my $dy = $ev->{y} - $oy; |
1077 | |
1101 | |
1078 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1079 | $self->{user_y} = $wy + $dy * $my; |
|
|
1080 | $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); |
1102 | $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); |
1081 | $self->{def_h} = $bh + $dy * ($my ? -1 : 1); |
1103 | $self->{force_h} = $bh + $dy * ($my ? -1 : 1); |
1082 | $self->move ($self->{user_x}, $self->{user_y}); |
1104 | |
1083 | $self->check_size; |
1105 | $self->realloc; |
|
|
1106 | $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); |
1084 | }; |
1107 | }; |
1085 | |
1108 | |
1086 | } elsif ($lr ^ $td) { |
1109 | } elsif ($lr ^ $td) { |
1087 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1110 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1088 | my ($bx, $by) = ($self->{x}, $self->{y}); |
1111 | my ($bx, $by) = ($self->{x}, $self->{y}); |
… | |
… | |
1090 | $self->{motion} = sub { |
1113 | $self->{motion} = sub { |
1091 | my ($ev, $x, $y) = @_; |
1114 | my ($ev, $x, $y) = @_; |
1092 | |
1115 | |
1093 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1116 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1094 | |
1117 | |
1095 | $self->{user_x} = $bx + $x - $ox; |
1118 | $self->move_abs ($bx + $x - $ox, $by + $y - $oy); |
1096 | $self->{user_y} = $by + $y - $oy; |
|
|
1097 | |
|
|
1098 | $self->move ($self->{user_x}, $self->{user_y}); |
|
|
1099 | $self->update; |
|
|
1100 | }; |
1119 | }; |
1101 | } |
1120 | } |
1102 | } |
1121 | } |
1103 | |
1122 | |
1104 | sub button_up { |
1123 | sub button_up { |
… | |
… | |
1123 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1142 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1124 | |
1143 | |
1125 | my $border = $self->border; |
1144 | my $border = $self->border; |
1126 | |
1145 | |
1127 | glColor @{ $self->{border_bg} }; |
1146 | glColor @{ $self->{border_bg} }; |
1128 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
1147 | $border[0]->draw_quad_alpha (0, 0, $w, $border); |
1129 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
1148 | $border[1]->draw_quad_alpha (0, $border, $border, $ch); |
1130 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1149 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1131 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
1150 | $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); |
1132 | |
1151 | |
1133 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1152 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1134 | my $bg = $tex[0]; |
1153 | glColor @{ $self->{bg} }; |
1135 | |
1154 | |
1136 | # TODO: repeat texture not scale |
1155 | # TODO: repeat texture not scale |
|
|
1156 | # solve this better(?) |
1137 | my $rep_x = $cw / $bg->{w}; |
1157 | $bg->{s} = $cw / $bg->{w}; |
1138 | my $rep_y = $ch / $bg->{h}; |
1158 | $bg->{t} = $ch / $bg->{h}; |
1139 | |
|
|
1140 | glColor @{ $self->{bg} }; |
|
|
1141 | |
|
|
1142 | $bg->{s} = $rep_x; |
|
|
1143 | $bg->{t} = $rep_y; |
|
|
1144 | $bg->{wrap_mode} = 1; |
|
|
1145 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
1159 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
1146 | } |
1160 | } |
1147 | |
1161 | |
1148 | glDisable GL_TEXTURE_2D; |
1162 | glDisable GL_TEXTURE_2D; |
1149 | |
1163 | |
… | |
… | |
1179 | my ($self, $x, $y, $child) = @_; |
1193 | my ($self, $x, $y, $child) = @_; |
1180 | |
1194 | |
1181 | $child->set_parent ($self); |
1195 | $child->set_parent ($self); |
1182 | $self->{children}[$y][$x] = $child; |
1196 | $self->{children}[$y][$x] = $child; |
1183 | |
1197 | |
1184 | $self->check_size (1); |
1198 | $self->realloc; |
1185 | } |
1199 | } |
1186 | |
1200 | |
1187 | # TODO: move to container class maybe? send children a signal on removal? |
1201 | # TODO: move to container class maybe? send children a signal on removal? |
1188 | sub clear { |
1202 | sub clear { |
1189 | my ($self) = @_; |
1203 | my ($self) = @_; |
… | |
… | |
1194 | for (@children) { |
1208 | for (@children) { |
1195 | delete $_->{parent}; |
1209 | delete $_->{parent}; |
1196 | $_->hide; |
1210 | $_->hide; |
1197 | } |
1211 | } |
1198 | |
1212 | |
1199 | $self->check_size (1); |
1213 | $self->realloc; |
1200 | $self->update; |
|
|
1201 | } |
1214 | } |
1202 | |
1215 | |
1203 | sub get_wh { |
1216 | sub get_wh { |
1204 | my ($self) = @_; |
1217 | my ($self) = @_; |
1205 | |
1218 | |
… | |
… | |
1232 | (sum @$hs), |
1245 | (sum @$hs), |
1233 | ) |
1246 | ) |
1234 | } |
1247 | } |
1235 | |
1248 | |
1236 | sub size_allocate { |
1249 | sub size_allocate { |
1237 | my ($self, $w, $h) = @_; |
1250 | my ($self, $w, $h, $changed) = @_; |
1238 | |
1251 | |
1239 | my ($ws, $hs) = $self->get_wh; |
1252 | my ($ws, $hs) = $self->get_wh; |
1240 | |
1253 | |
1241 | my $req_w = (sum @$ws) || 1; |
1254 | my $req_w = (sum @$ws) || 1; |
1242 | my $req_h = (sum @$hs) || 1; |
1255 | my $req_h = (sum @$hs) || 1; |
… | |
… | |
1320 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1333 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1321 | ) |
1334 | ) |
1322 | } |
1335 | } |
1323 | |
1336 | |
1324 | sub size_allocate { |
1337 | sub size_allocate { |
1325 | my ($self, $w, $h) = @_; |
1338 | my ($self, $w, $h, $changed) = @_; |
1326 | |
1339 | |
1327 | my $space = $self->{vertical} ? $h : $w; |
1340 | my $space = $self->{vertical} ? $h : $w; |
1328 | my $children = $self->{children}; |
1341 | my $children = $self->{children}; |
1329 | |
1342 | |
1330 | my @req; |
1343 | my @req; |
… | |
… | |
1413 | ellipsise => 3, # end |
1426 | ellipsise => 3, # end |
1414 | layout => (new CFClient::Layout), |
1427 | layout => (new CFClient::Layout), |
1415 | fontsize => 1, |
1428 | fontsize => 1, |
1416 | align => -1, |
1429 | align => -1, |
1417 | valign => -1, |
1430 | valign => -1, |
1418 | padding => 2, |
1431 | padding_x => 2, |
|
|
1432 | padding_y => 2, |
1419 | can_events => 0, |
1433 | can_events => 0, |
1420 | %arg |
1434 | %arg |
1421 | ); |
1435 | ); |
1422 | |
1436 | |
1423 | if (exists $self->{template}) { |
1437 | if (exists $self->{template}) { |
… | |
… | |
1459 | $self->{text} = "T$text"; |
1473 | $self->{text} = "T$text"; |
1460 | |
1474 | |
1461 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1475 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1462 | $self->{layout}->set_text ($text); |
1476 | $self->{layout}->set_text ($text); |
1463 | |
1477 | |
|
|
1478 | $self->realloc; |
1464 | $self->update; |
1479 | $self->update; |
1465 | $self->check_size; |
|
|
1466 | } |
1480 | } |
1467 | |
1481 | |
1468 | sub set_markup { |
1482 | sub set_markup { |
1469 | my ($self, $markup) = @_; |
1483 | my ($self, $markup) = @_; |
1470 | |
1484 | |
… | |
… | |
1474 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1488 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1475 | |
1489 | |
1476 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1490 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1477 | $self->{layout}->set_markup ($markup); |
1491 | $self->{layout}->set_markup ($markup); |
1478 | |
1492 | |
|
|
1493 | $self->realloc; |
1479 | $self->update; |
1494 | $self->update; |
1480 | $self->check_size; |
|
|
1481 | } |
1495 | } |
1482 | |
1496 | |
1483 | sub size_request { |
1497 | sub size_request { |
1484 | my ($self) = @_; |
1498 | my ($self) = @_; |
1485 | |
1499 | |
… | |
… | |
1499 | |
1513 | |
1500 | $w = List::Util::max $w, $w2; |
1514 | $w = List::Util::max $w, $w2; |
1501 | $h = List::Util::max $h, $h2; |
1515 | $h = List::Util::max $h, $h2; |
1502 | } |
1516 | } |
1503 | |
1517 | |
1504 | ( |
1518 | ($w, $h) |
1505 | $w + $self->{padding} * 2, |
|
|
1506 | $h + $self->{padding} * 2, |
|
|
1507 | ) |
|
|
1508 | } |
1519 | } |
1509 | |
1520 | |
1510 | sub size_allocate { |
1521 | sub size_allocate { |
1511 | my ($self, $w, $h) = @_; |
1522 | my ($self, $w, $h, $changed) = @_; |
1512 | |
1523 | |
1513 | delete $self->{texture}; |
1524 | delete $self->{texture} |
|
|
1525 | if $changed; |
1514 | } |
1526 | } |
1515 | |
1527 | |
1516 | sub set_fontsize { |
1528 | sub set_fontsize { |
1517 | my ($self, $fontsize) = @_; |
1529 | my ($self, $fontsize) = @_; |
1518 | |
1530 | |
1519 | $self->{fontsize} = $fontsize; |
1531 | $self->{fontsize} = $fontsize; |
1520 | delete $self->{texture}; |
1532 | delete $self->{texture}; |
1521 | |
1533 | |
1522 | $self->update; |
1534 | $self->realloc; |
1523 | $self->check_size; |
|
|
1524 | } |
1535 | } |
1525 | |
1536 | |
1526 | sub _draw { |
1537 | sub _draw { |
1527 | my ($self) = @_; |
1538 | my ($self) = @_; |
1528 | |
1539 | |
… | |
… | |
1536 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1547 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1537 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1548 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1538 | |
1549 | |
1539 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1550 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1540 | |
1551 | |
1541 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding} |
1552 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
1542 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1553 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} |
1543 | : ($self->{w} - $tex->{w}) * 0.5); |
1554 | : ($self->{w} - $tex->{w}) * 0.5); |
1544 | |
1555 | |
1545 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} |
1556 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
1546 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1557 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} |
1547 | : ($self->{h} - $tex->{h}) * 0.5); |
1558 | : ($self->{h} - $tex->{h}) * 0.5); |
1548 | |
1559 | |
1549 | $tex |
1560 | $tex |
1550 | }; |
1561 | }; |
1551 | |
1562 | |
… | |
… | |
1608 | sub set_text { |
1619 | sub set_text { |
1609 | my ($self, $text) = @_; |
1620 | my ($self, $text) = @_; |
1610 | |
1621 | |
1611 | $self->{cursor} = length $text; |
1622 | $self->{cursor} = length $text; |
1612 | $self->_set_text ($text); |
1623 | $self->_set_text ($text); |
1613 | $self->update; |
1624 | |
1614 | $self->check_size; |
1625 | $self->realloc; |
1615 | } |
1626 | } |
1616 | |
1627 | |
1617 | sub get_text { |
1628 | sub get_text { |
1618 | $_[0]{text} |
1629 | $_[0]{text} |
1619 | } |
1630 | } |
… | |
… | |
1652 | } elsif ($uni) { |
1663 | } elsif ($uni) { |
1653 | substr $text, $self->{cursor}++, 0, chr $uni; |
1664 | substr $text, $self->{cursor}++, 0, chr $uni; |
1654 | } |
1665 | } |
1655 | |
1666 | |
1656 | $self->_set_text ($text); |
1667 | $self->_set_text ($text); |
1657 | $self->update; |
1668 | |
1658 | $self->check_size; |
1669 | $self->realloc; |
1659 | } |
1670 | } |
1660 | |
1671 | |
1661 | sub focus_in { |
1672 | sub focus_in { |
1662 | my ($self) = @_; |
1673 | my ($self) = @_; |
1663 | |
1674 | |
… | |
… | |
1790 | |
1801 | |
1791 | sub new { |
1802 | sub new { |
1792 | my $class = shift; |
1803 | my $class = shift; |
1793 | |
1804 | |
1794 | $class->SUPER::new ( |
1805 | $class->SUPER::new ( |
1795 | padding => 4, |
1806 | padding_x => 4, |
|
|
1807 | padding_y => 4, |
1796 | fg => [1, 1, 1], |
1808 | fg => [1, 1, 1], |
1797 | active_fg => [0, 0, 1], |
1809 | active_fg => [0, 0, 1], |
1798 | can_hover => 1, |
1810 | can_hover => 1, |
1799 | align => 0, |
1811 | align => 0, |
1800 | valign => 0, |
1812 | valign => 0, |
… | |
… | |
1847 | |
1859 | |
1848 | sub new { |
1860 | sub new { |
1849 | my $class = shift; |
1861 | my $class = shift; |
1850 | |
1862 | |
1851 | $class->SUPER::new ( |
1863 | $class->SUPER::new ( |
1852 | padding => 2, |
1864 | padding_x => 2, |
|
|
1865 | padding_y => 2, |
1853 | fg => [1, 1, 1], |
1866 | fg => [1, 1, 1], |
1854 | active_fg => [1, 1, 0], |
1867 | active_fg => [1, 1, 0], |
1855 | bg => [0, 0, 0, 0.2], |
1868 | bg => [0, 0, 0, 0.2], |
1856 | active_bg => [1, 1, 1, 0.5], |
1869 | active_bg => [1, 1, 1, 0.5], |
1857 | state => 0, |
1870 | state => 0, |
… | |
… | |
1861 | } |
1874 | } |
1862 | |
1875 | |
1863 | sub size_request { |
1876 | sub size_request { |
1864 | my ($self) = @_; |
1877 | my ($self) = @_; |
1865 | |
1878 | |
1866 | ($self->{padding} * 2 + 6) x 2 |
1879 | (6) x 2 |
1867 | } |
1880 | } |
1868 | |
1881 | |
1869 | sub button_down { |
1882 | sub button_down { |
1870 | my ($self, $ev, $x, $y) = @_; |
1883 | my ($self, $ev, $x, $y) = @_; |
1871 | |
1884 | |
1872 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1885 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
1873 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1886 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
1874 | $self->{state} = !$self->{state}; |
1887 | $self->{state} = !$self->{state}; |
1875 | $self->_emit (changed => $self->{state}); |
1888 | $self->_emit (changed => $self->{state}); |
1876 | } |
1889 | } |
1877 | } |
1890 | } |
1878 | |
1891 | |
1879 | sub _draw { |
1892 | sub _draw { |
1880 | my ($self) = @_; |
1893 | my ($self) = @_; |
1881 | |
1894 | |
1882 | $self->SUPER::_draw; |
1895 | $self->SUPER::_draw; |
1883 | |
1896 | |
1884 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1897 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
1885 | |
1898 | |
1886 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1899 | my ($w, $h) = @$self{qw(w h)}; |
|
|
1900 | |
|
|
1901 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
1887 | |
1902 | |
1888 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1903 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1889 | |
1904 | |
1890 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1905 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1891 | |
1906 | |
… | |
… | |
2156 | fg => [1, 1, 1], |
2171 | fg => [1, 1, 1], |
2157 | active_fg => [0, 0, 0], |
2172 | active_fg => [0, 0, 0], |
2158 | bg => [0, 0, 0, 0.2], |
2173 | bg => [0, 0, 0, 0.2], |
2159 | active_bg => [1, 1, 1, 0.5], |
2174 | active_bg => [1, 1, 1, 0.5], |
2160 | range => [0, 0, 100, 10, 0], |
2175 | range => [0, 0, 100, 10, 0], |
2161 | req_w => $::WIDTH / 80, |
2176 | min_w => $::WIDTH / 80, |
2162 | req_h => $::WIDTH / 80, |
2177 | min_h => $::WIDTH / 80, |
2163 | vertical => 0, |
2178 | vertical => 0, |
2164 | can_hover => 1, |
2179 | can_hover => 1, |
2165 | inner_pad => 0.02, |
2180 | inner_pad => 0.02, |
2166 | @_ |
2181 | @_ |
2167 | ); |
2182 | ); |
… | |
… | |
2170 | $self->update; |
2185 | $self->update; |
2171 | |
2186 | |
2172 | $self |
2187 | $self |
2173 | } |
2188 | } |
2174 | |
2189 | |
|
|
2190 | sub changed { } |
|
|
2191 | |
2175 | sub set_range { |
2192 | sub set_range { |
2176 | my ($self, $range) = @_; |
2193 | my ($self, $range) = @_; |
2177 | |
2194 | |
2178 | ($range, $self->{range}) = ($self->{range}, $range); |
2195 | ($range, $self->{range}) = ($self->{range}, $range); |
2179 | |
2196 | |
… | |
… | |
2205 | } |
2222 | } |
2206 | |
2223 | |
2207 | sub size_request { |
2224 | sub size_request { |
2208 | my ($self) = @_; |
2225 | my ($self) = @_; |
2209 | |
2226 | |
2210 | my $w = $self->{req_w}; |
2227 | ($self->{req_w}, $self->{req_h}) |
2211 | my $h = $self->{req_h}; |
|
|
2212 | |
|
|
2213 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
2214 | } |
2228 | } |
2215 | |
2229 | |
2216 | sub button_down { |
2230 | sub button_down { |
2217 | my ($self, $ev, $x, $y) = @_; |
2231 | my ($self, $ev, $x, $y) = @_; |
2218 | |
2232 | |
… | |
… | |
2368 | $self->{fontsize} = $fontsize; |
2382 | $self->{fontsize} = $fontsize; |
2369 | $self->reflow; |
2383 | $self->reflow; |
2370 | } |
2384 | } |
2371 | |
2385 | |
2372 | sub size_allocate { |
2386 | sub size_allocate { |
2373 | my ($self, $w, $h) = @_; |
2387 | my ($self, $w, $h, $changed) = @_; |
2374 | |
2388 | |
2375 | $self->SUPER::size_allocate ($w, $h); |
2389 | $self->SUPER::size_allocate ($w, $h, $changed); |
|
|
2390 | |
|
|
2391 | return unless $changed; |
2376 | |
2392 | |
2377 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
2393 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
2378 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2394 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2379 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2395 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2380 | |
2396 | |
… | |
… | |
2622 | |
2638 | |
2623 | ($w + 4, $h + 4) |
2639 | ($w + 4, $h + 4) |
2624 | } |
2640 | } |
2625 | |
2641 | |
2626 | sub size_allocate { |
2642 | sub size_allocate { |
2627 | my ($self, $w, $h) = @_; |
2643 | my ($self, $w, $h, $changed) = @_; |
2628 | |
2644 | |
|
|
2645 | return unless $changed; |
|
|
2646 | |
2629 | $self->SUPER::size_allocate ($w - 4, $h - 4); |
2647 | $self->SUPER::size_allocate ($w - 4, $h - 4, $changed); |
|
|
2648 | } |
|
|
2649 | |
|
|
2650 | sub visibility_change { |
|
|
2651 | my ($self, $visible) = @_; |
|
|
2652 | |
|
|
2653 | return unless $visible; |
|
|
2654 | |
|
|
2655 | $self->{root}->on_post_alloc ("move_$self" => sub { |
|
|
2656 | my $widget = $self->{owner} |
|
|
2657 | or return; |
|
|
2658 | |
|
|
2659 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
|
|
2660 | |
|
|
2661 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
|
|
2662 | if $x + $self->{w} > $::WIDTH; |
|
|
2663 | |
|
|
2664 | $self->move_abs ($x, $y); |
|
|
2665 | }); |
2630 | } |
2666 | } |
2631 | |
2667 | |
2632 | sub _draw { |
2668 | sub _draw { |
2633 | my ($self) = @_; |
2669 | my ($self) = @_; |
2634 | |
2670 | |
… | |
… | |
2651 | glVertex $w, $h; |
2687 | glVertex $w, $h; |
2652 | glVertex $w, 0; |
2688 | glVertex $w, 0; |
2653 | glEnd; |
2689 | glEnd; |
2654 | |
2690 | |
2655 | glTranslate 2 - 0.375, 2 - 0.375; |
2691 | glTranslate 2 - 0.375, 2 - 0.375; |
|
|
2692 | |
2656 | $self->SUPER::_draw; |
2693 | $self->SUPER::_draw; |
2657 | } |
2694 | } |
2658 | |
2695 | |
2659 | ############################################################################# |
2696 | ############################################################################# |
2660 | |
2697 | |
… | |
… | |
2825 | # maybe save $GRAB? must be careful about events... |
2862 | # maybe save $GRAB? must be careful about events... |
2826 | $GRAB = $self; |
2863 | $GRAB = $self; |
2827 | $self->{button} = $ev->{button}; |
2864 | $self->{button} = $ev->{button}; |
2828 | |
2865 | |
2829 | $self->show; |
2866 | $self->show; |
2830 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
2867 | $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
2831 | } |
2868 | } |
2832 | |
2869 | |
2833 | sub mouse_motion { |
2870 | sub mouse_motion { |
2834 | my ($self, $ev, $x, $y) = @_; |
2871 | my ($self, $ev, $x, $y) = @_; |
2835 | |
2872 | |
… | |
… | |
2969 | use CFClient::OpenGL; |
3006 | use CFClient::OpenGL; |
2970 | |
3007 | |
2971 | sub new { |
3008 | sub new { |
2972 | my $class = shift; |
3009 | my $class = shift; |
2973 | |
3010 | |
2974 | $class->SUPER::new ( |
3011 | my $self = $class->SUPER::new ( |
2975 | visible => 1, |
3012 | visible => 1, |
2976 | @_, |
3013 | @_, |
2977 | ) |
3014 | ); |
|
|
3015 | |
|
|
3016 | Scalar::Util::weaken ($self->{root} = $self); |
|
|
3017 | |
|
|
3018 | $self |
2978 | } |
3019 | } |
2979 | |
3020 | |
2980 | sub configure { |
3021 | sub configure { |
2981 | my ($self, $x, $y, $w, $h) = @_; |
3022 | my ($self, $x, $y, $w, $h) = @_; |
2982 | |
3023 | |
2983 | $self->{w} = $w; |
3024 | $self->{w} = $w; |
2984 | $self->{h} = $h; |
3025 | $self->{h} = $h; |
2985 | } |
3026 | } |
2986 | |
3027 | |
2987 | sub check_size { |
3028 | sub reconfigure { |
2988 | my ($self) = @_; |
3029 | my ($self) = @_; |
2989 | |
3030 | |
|
|
3031 | $self->SUPER::reconfigure; |
|
|
3032 | |
2990 | $self->size_allocate ($self->{w}, $self->{h}) |
3033 | $self->size_allocate ($self->{w}, $self->{h}, 1) |
2991 | if $self->{w}; |
3034 | if $self->{w}; |
2992 | } |
3035 | } |
2993 | |
3036 | |
2994 | sub size_request { |
3037 | sub size_request { |
2995 | my ($self) = @_; |
3038 | my ($self) = @_; |
2996 | |
3039 | |
2997 | ($self->{w}, $self->{h}) |
3040 | ($self->{w}, $self->{h}) |
2998 | } |
3041 | } |
2999 | |
3042 | |
|
|
3043 | sub _to_pixel { |
|
|
3044 | my ($coord, $size, $max) = @_; |
|
|
3045 | |
|
|
3046 | $coord = |
|
|
3047 | $coord eq "center" ? ($max - $size) * 0.5 |
|
|
3048 | : $coord eq "max" ? $max |
|
|
3049 | : $coord; |
|
|
3050 | |
|
|
3051 | $coord = 0 if $coord < 0; |
|
|
3052 | $coord = $max - $size if $coord > $max - $size; |
|
|
3053 | |
|
|
3054 | int $coord + 0.5 |
|
|
3055 | } |
|
|
3056 | |
3000 | sub size_allocate { |
3057 | sub size_allocate { |
3001 | my ($self, $w, $h) = @_; |
3058 | my ($self, $w, $h, $changed) = @_; |
3002 | |
3059 | |
3003 | for my $child ($self->children) { |
3060 | for my $child ($self->children) { |
3004 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
3061 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
3005 | |
3062 | |
3006 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
3063 | $X = $child->{force_x} if exists $child->{force_x}; |
3007 | if exists $child->{req_x}; |
3064 | $Y = $child->{force_y} if exists $child->{force_y}; |
3008 | |
3065 | |
3009 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3066 | $X = _to_pixel $X, $W, $self->{w}; |
3010 | if exists $child->{req_y}; |
3067 | $Y = _to_pixel $Y, $H, $self->{h}; |
3011 | |
|
|
3012 | delete @$child{qw(req_x req_y)}; |
|
|
3013 | |
|
|
3014 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
|
|
3015 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
|
|
3016 | |
3068 | |
3017 | $child->configure ($X, $Y, $W, $H); |
3069 | $child->configure ($X, $Y, $W, $H); |
3018 | } |
3070 | } |
3019 | } |
3071 | } |
3020 | |
3072 | |
… | |
… | |
3031 | } |
3083 | } |
3032 | |
3084 | |
3033 | sub update { |
3085 | sub update { |
3034 | my ($self) = @_; |
3086 | my ($self) = @_; |
3035 | |
3087 | |
3036 | $self->check_size; |
|
|
3037 | $::WANT_REFRESH++; |
3088 | $::WANT_REFRESH++; |
3038 | } |
3089 | } |
3039 | |
3090 | |
3040 | sub add { |
3091 | sub add { |
3041 | my ($self, @children) = @_; |
3092 | my ($self, @children) = @_; |
3042 | |
3093 | |
3043 | for my $child (@children) { |
|
|
3044 | $child->{is_toplevel} = 1; |
3094 | $_->{is_toplevel} = 1 |
3045 | |
3095 | for @children; |
3046 | # integerise window positions |
|
|
3047 | $child->{x} = int $child->{x}; |
|
|
3048 | $child->{y} = int $child->{y}; |
|
|
3049 | } |
|
|
3050 | |
3096 | |
3051 | $self->SUPER::add (@children); |
3097 | $self->SUPER::add (@children); |
3052 | |
|
|
3053 | for (my @widgets = @children; my $w = pop @widgets; ) { |
|
|
3054 | push @widgets, $w->children; |
|
|
3055 | $w->set_visible; |
|
|
3056 | } |
|
|
3057 | |
|
|
3058 | } |
3098 | } |
3059 | |
3099 | |
3060 | sub remove { |
3100 | sub remove { |
3061 | my ($self, @children) = @_; |
3101 | my ($self, @children) = @_; |
3062 | |
3102 | |
3063 | $self->SUPER::remove (@children); |
3103 | $self->SUPER::remove (@children); |
|
|
3104 | |
|
|
3105 | delete $self->{is_toplevel} |
|
|
3106 | for @children; |
3064 | |
3107 | |
3065 | while (@children) { |
3108 | while (@children) { |
3066 | my $w = pop @children; |
3109 | my $w = pop @children; |
3067 | push @children, $w->children; |
3110 | push @children, $w->children; |
3068 | $w->set_invisible; |
3111 | $w->set_invisible; |
… | |
… | |
3087 | while ($self->{refresh_hook}) { |
3130 | while ($self->{refresh_hook}) { |
3088 | $_->() |
3131 | $_->() |
3089 | for values %{delete $self->{refresh_hook}}; |
3132 | for values %{delete $self->{refresh_hook}}; |
3090 | } |
3133 | } |
3091 | |
3134 | |
3092 | if ($self->{check_size}) { |
3135 | if ($self->{realloc}) { |
3093 | my @queue; |
3136 | my @queue; |
3094 | |
3137 | |
3095 | for (;;) { |
3138 | while () { |
3096 | if ($self->{check_size}) { |
3139 | if ($self->{realloc}) { |
3097 | #TODO use array-of-depth approach |
3140 | #TODO use array-of-depth approach |
3098 | |
3141 | |
3099 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3142 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3100 | @queue, values %{delete $self->{check_size}}; |
3143 | @queue, values %{delete $self->{realloc}}; |
3101 | } |
3144 | } |
3102 | |
3145 | |
3103 | my $widget = pop @queue || last; |
3146 | my $widget = pop @queue || last; |
3104 | |
3147 | |
3105 | defined $widget->{visible} or last; # do not resize invisible widgets |
3148 | $widget->{visible} or last; # do not resize invisible widgets |
3106 | |
3149 | |
3107 | my ($w, $h) = $widget->{def_w} && $widget->{def_h} |
|
|
3108 | ? @$widget{qw(def_w def_h)} |
|
|
3109 | : $widget->size_request; |
3150 | my ($w, $h) = $widget->size_request; |
3110 | |
3151 | |
3111 | if (delete $widget->{force_alloc} |
3152 | $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2; |
3112 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
3153 | $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2; |
3113 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
3154 | |
3114 | |
3155 | $w = $widget->{force_w} if exists $widget->{force_w}; |
|
|
3156 | $h = $widget->{force_h} if exists $widget->{force_h}; |
|
|
3157 | |
3115 | $widget->{req_w} = $w; |
3158 | $widget->{req_w} = $w; |
3116 | $widget->{req_h} = $h; |
3159 | $widget->{req_h} = $h; |
3117 | |
3160 | |
3118 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
3161 | $self->{size_alloc}{$widget} = [$widget, undef, undef]; |
3119 | |
3162 | |
3120 | $widget->{parent}->check_size |
3163 | push @queue, $widget->{parent} |
3121 | if $widget->{parent}; |
3164 | if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent}; |
3122 | } |
|
|
3123 | } |
3165 | } |
3124 | } |
3166 | } |
3125 | |
3167 | |
3126 | while ($self->{size_alloc}) { |
3168 | while (my $size_alloc = delete $self->{size_alloc}) { |
3127 | for (values %{delete $self->{size_alloc}}) { |
3169 | my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, |
3128 | my ($widget, $w, $h) = @$_; |
3170 | values %$size_alloc; |
|
|
3171 | |
|
|
3172 | while () { |
|
|
3173 | my ($widget, $w, $h) = @{ pop @queue or last }; |
|
|
3174 | |
|
|
3175 | $w = $widget->{w} || $widget->{req_w} unless defined $w; |
|
|
3176 | $h = $widget->{h} || $widget->{req_h} unless defined $h; |
3129 | |
3177 | |
3130 | $w = 0 if $w < 0; |
3178 | $w = 0 if $w < 0; |
3131 | $h = 0 if $h < 0; |
3179 | $h = 0 if $h < 0; |
3132 | |
3180 | |
|
|
3181 | $w = int $w + 0.5; |
|
|
3182 | $h = int $h + 0.5; |
|
|
3183 | |
|
|
3184 | my $changed = $widget->{w} != $w || $widget->{h} != $h; |
|
|
3185 | |
3133 | $widget->{w} = $w; |
3186 | $widget->{w} = $w; |
3134 | $widget->{h} = $h; |
3187 | $widget->{h} = $h; |
|
|
3188 | |
3135 | $widget->emit (size_allocate => $w, $h); |
3189 | $widget->emit (size_allocate => $w, $h, $changed); |
3136 | } |
3190 | } |
3137 | } |
3191 | } |
3138 | |
3192 | |
3139 | while ($self->{post_alloc_hook}) { |
3193 | while ($self->{post_alloc_hook}) { |
3140 | $_->() |
3194 | $_->() |