… | |
… | |
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 { |
|
|
45 | return if $ENV{CFPLUS_DEBUG} & 8; |
|
|
46 | |
40 | if (!$GRAB) { |
47 | if (!$GRAB) { |
41 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
48 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
42 | if (length $widget->{tooltip}) { |
49 | if (length $widget->{tooltip}) { |
43 | |
|
|
44 | if ($TOOLTIP->{owner} != $widget) { |
50 | if ($TOOLTIP->{owner} != $widget) { |
45 | $TOOLTIP->hide; |
51 | $TOOLTIP->hide; |
46 | |
52 | |
47 | $TOOLTIP->{owner} = $widget; |
53 | $TOOLTIP->{owner} = $widget; |
48 | |
54 | |
… | |
… | |
165 | sub rescale_widgets { |
171 | sub rescale_widgets { |
166 | my ($sx, $sy) = @_; |
172 | my ($sx, $sy) = @_; |
167 | |
173 | |
168 | for my $widget (values %WIDGET) { |
174 | for my $widget (values %WIDGET) { |
169 | if ($widget->{is_toplevel}) { |
175 | if ($widget->{is_toplevel}) { |
|
|
176 | $widget->{x} += int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
|
|
177 | $widget->{y} += int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
|
|
178 | |
170 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
179 | $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; |
171 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
180 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
172 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
181 | $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w}; |
173 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
182 | $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/; |
174 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
183 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
175 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
184 | $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h}; |
|
|
185 | |
|
|
186 | $widget->{x} -= int $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
|
|
187 | $widget->{y} -= int $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
|
|
188 | |
176 | } |
189 | } |
177 | } |
190 | } |
178 | |
191 | |
179 | reconfigure_widgets; |
192 | reconfigure_widgets; |
180 | } |
193 | } |
… | |
… | |
189 | |
202 | |
190 | sub new { |
203 | sub new { |
191 | my $class = shift; |
204 | my $class = shift; |
192 | |
205 | |
193 | my $self = bless { |
206 | my $self = bless { |
194 | x => 0, |
207 | x => "center", |
195 | y => 0, |
208 | y => "center", |
196 | z => 0, |
209 | z => 0, |
|
|
210 | w => undef, |
|
|
211 | h => undef, |
197 | can_events => 1, |
212 | can_events => 1, |
198 | @_ |
213 | @_ |
199 | }, $class; |
214 | }, $class; |
|
|
215 | |
|
|
216 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
200 | |
217 | |
201 | for (keys %$self) { |
218 | for (keys %$self) { |
202 | if (/^on_(.*)$/) { |
219 | if (/^on_(.*)$/) { |
203 | $self->connect ($1 => delete $self->{$_}); |
220 | $self->connect ($1 => delete $self->{$_}); |
204 | } |
221 | } |
205 | } |
222 | } |
206 | |
223 | |
207 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
208 | |
|
|
209 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
224 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
210 | $self->{req_x} = $layout->{x} * $::WIDTH; |
225 | $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{x}; |
211 | $self->{req_y} = $layout->{y} * $::HEIGHT; |
226 | $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{y}; |
212 | $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; |
227 | $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{w}; |
213 | $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; |
228 | $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{h}; |
|
|
229 | |
|
|
230 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
|
|
231 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
|
|
232 | |
|
|
233 | $self->show if $layout->{show}; |
214 | } |
234 | } |
215 | |
235 | |
216 | $self |
236 | $self |
217 | } |
237 | } |
218 | |
238 | |
… | |
… | |
227 | my ($self) = @_; |
247 | my ($self) = @_; |
228 | |
248 | |
229 | return if $self->{parent}; |
249 | return if $self->{parent}; |
230 | |
250 | |
231 | $CFClient::UI::ROOT->add ($self); |
251 | $CFClient::UI::ROOT->add ($self); |
232 | } |
|
|
233 | |
|
|
234 | sub center { |
|
|
235 | my ($self) = @_; |
|
|
236 | |
|
|
237 | $CFClient::UI::ROOT->on_post_alloc ( |
|
|
238 | "center_$self" => sub { |
|
|
239 | $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5); |
|
|
240 | }, |
|
|
241 | ); |
|
|
242 | |
|
|
243 | $self->update; |
|
|
244 | } |
252 | } |
245 | |
253 | |
246 | sub set_visible { |
254 | sub set_visible { |
247 | my ($self) = @_; |
255 | my ($self) = @_; |
248 | |
256 | |
… | |
… | |
303 | |
311 | |
304 | $self->{parent}->remove ($self) |
312 | $self->{parent}->remove ($self) |
305 | if $self->{parent}; |
313 | if $self->{parent}; |
306 | } |
314 | } |
307 | |
315 | |
308 | sub move { |
316 | sub move_abs { |
309 | my ($self, $x, $y, $z) = @_; |
317 | my ($self, $x, $y, $z) = @_; |
310 | |
318 | |
311 | $self->{x} = int $x; |
319 | $self->{x} = List::Util::max 0, int $x; |
312 | $self->{y} = int $y; |
320 | $self->{y} = List::Util::max 0, int $y; |
313 | $self->{z} = $z if defined $z; |
321 | $self->{z} = $z if defined $z; |
314 | |
322 | |
315 | $self->update; |
323 | $self->update; |
316 | } |
324 | } |
317 | |
325 | |
318 | sub set_size { |
326 | sub set_size { |
319 | my ($self, $w, $h) = @_; |
327 | my ($self, $w, $h) = @_; |
320 | |
328 | |
321 | $self->{def_w} = $w; |
329 | $self->{force_w} = $w; |
322 | $self->{def_h} = $h; |
330 | $self->{force_h} = $h; |
323 | |
331 | |
324 | $self->realloc; |
332 | $self->realloc; |
325 | } |
333 | } |
326 | |
334 | |
327 | sub size_request { |
335 | sub size_request { |
… | |
… | |
331 | |
339 | |
332 | sub configure { |
340 | sub configure { |
333 | my ($self, $x, $y, $w, $h) = @_; |
341 | my ($self, $x, $y, $w, $h) = @_; |
334 | |
342 | |
335 | if ($self->{aspect}) { |
343 | if ($self->{aspect}) { |
|
|
344 | my ($ow, $oh) = ($w, $h); |
|
|
345 | |
336 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
346 | $w = List::Util::min $w, int $h * $self->{aspect}; |
337 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
347 | $h = List::Util::min $h, int $w / $self->{aspect}; |
338 | |
348 | |
339 | # use alignment to adjust x, y |
349 | # use alignment to adjust x, y |
340 | |
350 | |
341 | $x += int +($w - $w2) * 0.5; |
351 | $x += int 0.5 * ($ow - $w); |
342 | $y += int +($h - $h2) * 0.5; |
352 | $y += int 0.5 * ($oh - $h); |
343 | |
|
|
344 | ($w, $h) = ($w2, $h2); |
|
|
345 | } |
353 | } |
346 | |
354 | |
347 | if ($self->{x} != $x || $self->{y} != $y) { |
355 | if ($self->{x} ne $x || $self->{y} ne $y) { |
348 | $self->{x} = $x; |
356 | $self->{x} = $x; |
349 | $self->{y} = $y; |
357 | $self->{y} = $y; |
350 | $self->update; |
358 | $self->update; |
351 | } |
359 | } |
352 | |
360 | |
353 | if ($self->{w} != $w || $self->{h} != $h) { |
361 | if ($self->{alloc_w} != $w || $self->{alloc_h} != $h) { |
354 | return unless $self->{visible}; |
362 | return unless $self->{visible}; |
355 | |
363 | |
|
|
364 | $self->{alloc_w} = $w; |
|
|
365 | $self->{alloc_h} = $h; |
|
|
366 | |
356 | $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h]; |
367 | $self->{root}{size_alloc}{$self+0} = $self; |
357 | } |
368 | } |
358 | } |
369 | } |
359 | |
370 | |
360 | sub size_allocate { |
371 | sub size_allocate { |
361 | # nothing to be done |
372 | # nothing to be done |
362 | } |
373 | } |
363 | |
374 | |
364 | sub children { |
375 | sub children { |
|
|
376 | # nop |
|
|
377 | } |
|
|
378 | |
|
|
379 | sub visible_children { |
|
|
380 | $_[0]->children |
365 | } |
381 | } |
366 | |
382 | |
367 | sub set_max_size { |
383 | sub set_max_size { |
368 | my ($self, $w, $h) = @_; |
384 | my ($self, $w, $h) = @_; |
369 | |
385 | |
… | |
… | |
428 | |
444 | |
429 | $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus |
445 | $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus |
430 | unless $FOCUS; |
446 | unless $FOCUS; |
431 | } |
447 | } |
432 | |
448 | |
433 | sub mouse_motion { } |
449 | sub mouse_motion { 0 } |
434 | sub button_up { } |
450 | sub button_up { 0 } |
435 | sub key_down { } |
451 | sub key_down { 0 } |
436 | sub key_up { } |
452 | sub key_up { 0 } |
437 | |
453 | |
438 | sub button_down { |
454 | sub button_down { |
439 | my ($self, $ev, $x, $y) = @_; |
455 | my ($self, $ev, $x, $y) = @_; |
440 | |
456 | |
441 | $self->focus_in; |
457 | $self->focus_in; |
442 | } |
|
|
443 | |
458 | |
444 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
459 | 0 |
445 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
460 | } |
446 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
|
|
447 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
|
|
448 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
|
|
449 | |
461 | |
450 | sub find_widget { |
462 | sub find_widget { |
451 | my ($self, $x, $y) = @_; |
463 | my ($self, $x, $y) = @_; |
452 | |
464 | |
453 | return () unless $self->{can_events}; |
465 | return () unless $self->{can_events}; |
… | |
… | |
461 | |
473 | |
462 | sub set_parent { |
474 | sub set_parent { |
463 | my ($self, $parent) = @_; |
475 | my ($self, $parent) = @_; |
464 | |
476 | |
465 | Scalar::Util::weaken ($self->{parent} = $parent); |
477 | Scalar::Util::weaken ($self->{parent} = $parent); |
466 | |
|
|
467 | $self->set_visible if $parent->{visible}; |
478 | $self->set_visible if $parent->{visible}; |
468 | } |
479 | } |
469 | |
480 | |
470 | sub connect { |
481 | sub connect { |
471 | my ($self, $signal, $cb) = @_; |
482 | my ($self, $signal, $cb) = @_; |
… | |
… | |
492 | |
503 | |
493 | sub realloc { |
504 | sub realloc { |
494 | my ($self) = @_; |
505 | my ($self) = @_; |
495 | |
506 | |
496 | if ($self->{visible}) { |
507 | if ($self->{visible}) { |
497 | return if $self->{root}{realloc}{$self}; |
508 | return if $self->{root}{realloc}{$self+0}; |
498 | |
509 | |
499 | $self->{root}{realloc}{$self} = $self; |
510 | $self->{root}{realloc}{$self+0} = $self; |
500 | $self->{root}->update; |
511 | $self->{root}->update; |
501 | } else { |
512 | } else { |
502 | delete $self->{req_w}; |
513 | delete $self->{req_w}; |
|
|
514 | delete $self->{req_h}; |
503 | } |
515 | } |
504 | } |
516 | } |
505 | |
517 | |
506 | sub update { |
518 | sub update { |
507 | my ($self) = @_; |
519 | my ($self) = @_; |
508 | |
520 | |
509 | $self->{parent}->update |
521 | $self->{parent}->update |
510 | if $self->{parent}; |
522 | if $self->{parent}; |
511 | } |
523 | } |
512 | |
524 | |
|
|
525 | sub reconfigure { |
|
|
526 | my ($self) = @_; |
|
|
527 | |
|
|
528 | $self->realloc; |
|
|
529 | $self->update; |
|
|
530 | } |
|
|
531 | |
|
|
532 | # using global variables seems a bit hacky, but passing through all drawing |
|
|
533 | # functions seems pointless. |
|
|
534 | our ($draw_x, $draw_y, $draw_w, $draw_h); # screen rectangle being drawn |
|
|
535 | |
513 | sub draw { |
536 | sub draw { |
514 | my ($self) = @_; |
537 | my ($self) = @_; |
515 | |
538 | |
516 | return unless $self->{h} && $self->{w}; |
539 | return unless $self->{h} && $self->{w}; |
|
|
540 | |
|
|
541 | # update screen rectangle |
|
|
542 | local $draw_x = $draw_x + $self->{x}; |
|
|
543 | local $draw_y = $draw_y + $self->{y}; |
|
|
544 | local $draw_w = $draw_x + $self->{w}; |
|
|
545 | local $draw_h = $draw_y + $self->{h}; |
|
|
546 | |
|
|
547 | # skip widgets that are entirely outside the drawing area |
|
|
548 | return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) |
|
|
549 | || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); |
517 | |
550 | |
518 | glPushMatrix; |
551 | glPushMatrix; |
519 | glTranslate $self->{x}, $self->{y}, 0; |
552 | glTranslate $self->{x}, $self->{y}, 0; |
520 | $self->_draw; |
|
|
521 | glPopMatrix; |
|
|
522 | |
553 | |
523 | if ($self == $HOVER && $self->{can_hover}) { |
554 | if ($self == $HOVER && $self->{can_hover}) { |
524 | my ($x, $y) = @$self{qw(x y)}; |
|
|
525 | |
|
|
526 | glColor 1, 0.8, 0.5, 0.2; |
555 | glColor 1*0.2, 0.8*0.2, 0.5*0.2, 0.2; |
527 | glEnable GL_BLEND; |
556 | glEnable GL_BLEND; |
528 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
557 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
529 | glBegin GL_QUADS; |
558 | glBegin GL_QUADS; |
530 | glVertex $x , $y; |
559 | glVertex 0 , 0; |
531 | glVertex $x + $self->{w}, $y; |
560 | glVertex $self->{w}, 0; |
532 | glVertex $x + $self->{w}, $y + $self->{h}; |
561 | glVertex $self->{w}, $self->{h}; |
533 | glVertex $x , $y + $self->{h}; |
562 | glVertex 0 , $self->{h}; |
534 | glEnd; |
563 | glEnd; |
535 | glDisable GL_BLEND; |
564 | glDisable GL_BLEND; |
536 | } |
565 | } |
537 | |
566 | |
538 | if ($ENV{PCLIENT_DEBUG}) { |
567 | if ($ENV{CFPLUS_DEBUG} & 1) { |
539 | glPushMatrix; |
568 | glPushMatrix; |
540 | glColor 1, 1, 0, 1; |
569 | glColor 1, 1, 0, 1; |
541 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
570 | glTranslate 0.375, 0.375; |
542 | glBegin GL_LINE_LOOP; |
571 | glBegin GL_LINE_LOOP; |
543 | glVertex 0 , 0; |
572 | glVertex 0 , 0; |
544 | glVertex $self->{w} - 1, 0; |
573 | glVertex $self->{w} - 1, 0; |
545 | glVertex $self->{w} - 1, $self->{h} - 1; |
574 | glVertex $self->{w} - 1, $self->{h} - 1; |
546 | glVertex 0 , $self->{h} - 1; |
575 | glVertex 0 , $self->{h} - 1; |
547 | glEnd; |
576 | glEnd; |
548 | glPopMatrix; |
577 | glPopMatrix; |
549 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
578 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
550 | } |
579 | } |
|
|
580 | |
|
|
581 | $self->_draw; |
|
|
582 | glPopMatrix; |
551 | } |
583 | } |
552 | |
584 | |
553 | sub _draw { |
585 | sub _draw { |
554 | my ($self) = @_; |
586 | my ($self) = @_; |
555 | |
587 | |
… | |
… | |
593 | |
625 | |
594 | if ($color && (@$color < 4 || $color->[3])) { |
626 | if ($color && (@$color < 4 || $color->[3])) { |
595 | my ($w, $h) = @$self{qw(w h)}; |
627 | my ($w, $h) = @$self{qw(w h)}; |
596 | |
628 | |
597 | glEnable GL_BLEND; |
629 | glEnable GL_BLEND; |
598 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
630 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
599 | glColor @$color; |
631 | glColor_premultiply @$color; |
600 | |
632 | |
601 | glBegin GL_QUADS; |
633 | glBegin GL_QUADS; |
602 | glVertex 0 , 0; |
634 | glVertex 0 , 0; |
603 | glVertex 0 , $h; |
635 | glVertex 0 , $h; |
604 | glVertex $w, $h; |
636 | glVertex $w, $h; |
… | |
… | |
619 | my ($class, %arg) = @_; |
651 | my ($class, %arg) = @_; |
620 | $class->SUPER::new (can_events => 0, %arg); |
652 | $class->SUPER::new (can_events => 0, %arg); |
621 | } |
653 | } |
622 | |
654 | |
623 | sub size_request { |
655 | sub size_request { |
624 | (0, 0) |
656 | my ($self) = @_; |
|
|
657 | |
|
|
658 | ($self->{w} + 0, $self->{h} + 0) |
625 | } |
659 | } |
626 | |
660 | |
627 | sub draw { } |
661 | sub draw { } |
628 | |
662 | |
629 | ############################################################################# |
663 | ############################################################################# |
… | |
… | |
633 | our @ISA = CFClient::UI::Base::; |
667 | our @ISA = CFClient::UI::Base::; |
634 | |
668 | |
635 | sub new { |
669 | sub new { |
636 | my ($class, %arg) = @_; |
670 | my ($class, %arg) = @_; |
637 | |
671 | |
638 | my $children = delete $arg{children} || []; |
672 | my $children = delete $arg{children}; |
639 | |
673 | |
640 | my $self = $class->SUPER::new ( |
674 | my $self = $class->SUPER::new ( |
641 | children => [], |
675 | children => [], |
642 | can_events => 0, |
676 | can_events => 0, |
643 | %arg, |
677 | %arg, |
644 | ); |
678 | ); |
|
|
679 | |
645 | $self->add ($_) for @$children; |
680 | $self->add (@$children) |
|
|
681 | if $children; |
646 | |
682 | |
647 | $self |
683 | $self |
648 | } |
684 | } |
649 | |
685 | |
650 | sub add { |
686 | sub add { |
… | |
… | |
698 | $x -= $self->{x}; |
734 | $x -= $self->{x}; |
699 | $y -= $self->{y}; |
735 | $y -= $self->{y}; |
700 | |
736 | |
701 | my $res; |
737 | my $res; |
702 | |
738 | |
703 | for (reverse @{ $self->{children} }) { |
739 | for (reverse $self->visible_children) { |
704 | $res = $_->find_widget ($x, $y) |
740 | $res = $_->find_widget ($x, $y) |
705 | and return $res; |
741 | and return $res; |
706 | } |
742 | } |
707 | |
743 | |
708 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
744 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
… | |
… | |
750 | sub size_request { |
786 | sub size_request { |
751 | $_[0]{children}[0]->size_request |
787 | $_[0]{children}[0]->size_request |
752 | } |
788 | } |
753 | |
789 | |
754 | sub size_allocate { |
790 | sub size_allocate { |
755 | my ($self, $w, $h, $changed) = @_; |
791 | my ($self, $w, $h) = @_; |
756 | |
792 | |
757 | $self->{children}[0]->configure (0, 0, $w, $h); |
793 | $self->{children}[0]->configure (0, 0, $w, $h); |
758 | } |
794 | } |
759 | |
795 | |
760 | ############################################################################# |
796 | ############################################################################# |
|
|
797 | |
|
|
798 | # back-buffered drawing area |
761 | |
799 | |
762 | package CFClient::UI::Window; |
800 | package CFClient::UI::Window; |
763 | |
801 | |
764 | our @ISA = CFClient::UI::Bin::; |
802 | our @ISA = CFClient::UI::Bin::; |
765 | |
803 | |
… | |
… | |
777 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
815 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
778 | $self->SUPER::update; |
816 | $self->SUPER::update; |
779 | } |
817 | } |
780 | |
818 | |
781 | sub size_allocate { |
819 | sub size_allocate { |
782 | my ($self, $w, $h, $changed) = @_; |
820 | my ($self, $w, $h) = @_; |
783 | |
821 | |
784 | $self->SUPER::size_allocate ($w, $h, $changed); |
822 | $self->SUPER::size_allocate ($w, $h); |
785 | $self->update |
823 | $self->update; |
786 | if $changed; |
|
|
787 | } |
824 | } |
788 | |
825 | |
789 | sub _render { |
826 | sub _render { |
|
|
827 | my ($self) = @_; |
|
|
828 | |
790 | $_[0]{children}[0]->draw; |
829 | $self->{children}[0]->draw; |
791 | } |
830 | } |
792 | |
831 | |
793 | sub render_child { |
832 | sub render_child { |
794 | my ($self) = @_; |
833 | my ($self) = @_; |
795 | |
834 | |
796 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
835 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
797 | glClearColor 0, 0, 0, 0; |
836 | glClearColor 0, 0, 0, 0; |
798 | glClear GL_COLOR_BUFFER_BIT; |
837 | glClear GL_COLOR_BUFFER_BIT; |
799 | |
838 | |
|
|
839 | { |
|
|
840 | package CFClient::UI::Base; |
|
|
841 | |
|
|
842 | ($draw_x, $draw_y, $draw_w, $draw_h) = |
|
|
843 | (0, 0, $self->{w}, $self->{h}); |
|
|
844 | } |
|
|
845 | |
800 | $self->_render; |
846 | $self->_render; |
801 | }; |
847 | }; |
802 | } |
848 | } |
803 | |
849 | |
804 | sub _draw { |
850 | sub _draw { |
805 | my ($self) = @_; |
851 | my ($self) = @_; |
806 | |
852 | |
807 | my ($w, $h) = ($self->w, $self->h); |
853 | my ($w, $h) = @$self{qw(w h)}; |
808 | |
854 | |
809 | my $tex = $self->{texture} |
855 | my $tex = $self->{texture} |
810 | or return; |
856 | or return; |
811 | |
857 | |
812 | glEnable GL_TEXTURE_2D; |
858 | glEnable GL_TEXTURE_2D; |
813 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
859 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
860 | glEnable GL_BLEND; |
|
|
861 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
814 | glColor 1, 1, 1, 1; |
862 | glColor 0, 0, 0, 1; |
815 | |
863 | |
816 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
864 | $tex->draw_quad (0, 0, $w, $h); |
817 | |
865 | |
|
|
866 | glDisable GL_BLEND; |
818 | glDisable GL_TEXTURE_2D; |
867 | glDisable GL_TEXTURE_2D; |
819 | } |
868 | } |
820 | |
869 | |
821 | ############################################################################# |
870 | ############################################################################# |
822 | |
871 | |
… | |
… | |
835 | } |
884 | } |
836 | |
885 | |
837 | sub size_request { |
886 | sub size_request { |
838 | my ($self) = @_; |
887 | my ($self) = @_; |
839 | |
888 | |
840 | my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
889 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
841 | |
890 | |
842 | $w = 10 if $self->{scroll_x}; |
891 | $w = 10 if $self->{scroll_x}; |
843 | $h = 10 if $self->{scroll_y}; |
892 | $h = 10 if $self->{scroll_y}; |
844 | |
893 | |
845 | ($w, $h) |
894 | ($w, $h) |
846 | } |
895 | } |
847 | |
896 | |
848 | sub size_allocate { |
897 | sub size_allocate { |
849 | my ($self, $w, $h, $changed) = @_; |
898 | my ($self, $w, $h) = @_; |
850 | |
899 | |
|
|
900 | my $child = $self->child; |
|
|
901 | |
851 | $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; |
902 | $w = $child->{req_w} if $self->{scroll_x} && $child->{req_w}; |
852 | $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; |
903 | $h = $child->{req_h} if $self->{scroll_y} && $child->{req_h}; |
853 | |
904 | |
854 | $self->child->configure (0, 0, $w, $h); |
905 | $self->child->configure (0, 0, $w, $h); |
855 | $self->update; |
906 | $self->update; |
856 | } |
907 | } |
857 | |
908 | |
… | |
… | |
893 | } |
944 | } |
894 | |
945 | |
895 | sub _render { |
946 | sub _render { |
896 | my ($self) = @_; |
947 | my ($self) = @_; |
897 | |
948 | |
|
|
949 | local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x}; |
|
|
950 | local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y}; |
|
|
951 | |
898 | CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
952 | CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
899 | |
953 | |
900 | $self->SUPER::_render; |
954 | $self->SUPER::_render; |
901 | } |
955 | } |
902 | |
956 | |
… | |
… | |
905 | package CFClient::UI::ScrolledWindow; |
959 | package CFClient::UI::ScrolledWindow; |
906 | |
960 | |
907 | our @ISA = CFClient::UI::HBox::; |
961 | our @ISA = CFClient::UI::HBox::; |
908 | |
962 | |
909 | sub new { |
963 | sub new { |
910 | my $class = shift; |
964 | my ($class, %arg) = @_; |
|
|
965 | |
|
|
966 | my $child = delete $arg{child}; |
911 | |
967 | |
912 | my $self; |
968 | my $self; |
913 | |
969 | |
914 | my $slider = new CFClient::UI::Slider |
970 | my $slider = new CFClient::UI::Slider |
915 | vertical => 1, |
971 | vertical => 1, |
… | |
… | |
920 | ; |
976 | ; |
921 | |
977 | |
922 | $self = $class->SUPER::new ( |
978 | $self = $class->SUPER::new ( |
923 | vp => (new CFClient::UI::ViewPort expand => 1), |
979 | vp => (new CFClient::UI::ViewPort expand => 1), |
924 | slider => $slider, |
980 | slider => $slider, |
925 | @_, |
981 | %arg, |
926 | ); |
982 | ); |
927 | |
983 | |
928 | $self->{vp}->add ($self->{scrolled}); |
|
|
929 | $self->add ($self->{vp}); |
|
|
930 | $self->add ($self->{slider}); |
984 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
|
|
985 | $self->add ($child) if $child; |
931 | |
986 | |
932 | $self |
987 | $self |
|
|
988 | } |
|
|
989 | |
|
|
990 | sub add { |
|
|
991 | my ($self, $widget) = @_; |
|
|
992 | |
|
|
993 | $self->{vp}->add ($self->{child} = $widget); |
933 | } |
994 | } |
934 | |
995 | |
935 | sub update { |
996 | sub update { |
936 | my ($self) = @_; |
997 | my ($self) = @_; |
937 | |
998 | |
… | |
… | |
941 | my $child = $self->{vp}->child; |
1002 | my $child = $self->{vp}->child; |
942 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
1003 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
943 | } |
1004 | } |
944 | |
1005 | |
945 | sub size_allocate { |
1006 | sub size_allocate { |
946 | my ($self, $w, $h, $changed) = @_; |
1007 | my ($self, $w, $h) = @_; |
947 | |
1008 | |
948 | $self->SUPER::size_allocate ($w, $h, $changed); |
1009 | $self->SUPER::size_allocate ($w, $h); |
949 | |
1010 | |
950 | my $child = $self->{vp}->child; |
1011 | my $child = $self->{vp}->child; |
951 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
1012 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
952 | } |
1013 | } |
953 | |
1014 | |
… | |
… | |
976 | |
1037 | |
977 | if ($self->{bg}) { |
1038 | if ($self->{bg}) { |
978 | my ($w, $h) = @$self{qw(w h)}; |
1039 | my ($w, $h) = @$self{qw(w h)}; |
979 | |
1040 | |
980 | glEnable GL_BLEND; |
1041 | glEnable GL_BLEND; |
981 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1042 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
982 | glColor @{ $self->{bg} }; |
1043 | glColor_premultiply @{ $self->{bg} }; |
983 | |
1044 | |
984 | glBegin GL_QUADS; |
1045 | glBegin GL_QUADS; |
985 | glVertex 0 , 0; |
1046 | glVertex 0 , 0; |
986 | glVertex 0 , $h; |
1047 | glVertex 0 , $h; |
987 | glVertex $w, $h; |
1048 | glVertex $w, $h; |
… | |
… | |
1000 | |
1061 | |
1001 | our @ISA = CFClient::UI::Bin::; |
1062 | our @ISA = CFClient::UI::Bin::; |
1002 | |
1063 | |
1003 | use CFClient::OpenGL; |
1064 | use CFClient::OpenGL; |
1004 | |
1065 | |
1005 | my @tex = |
1066 | my $bg = |
|
|
1067 | new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png", |
|
|
1068 | mipmap => 1, wrap => 1; |
|
|
1069 | |
|
|
1070 | my @border = |
1006 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1071 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1007 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1072 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1008 | |
1073 | |
1009 | sub new { |
1074 | sub new { |
1010 | my $class = shift; |
1075 | my ($class, %arg) = @_; |
|
|
1076 | |
|
|
1077 | my $title = delete $arg{title}; |
1011 | |
1078 | |
1012 | my $self = $class->SUPER::new ( |
1079 | my $self = $class->SUPER::new ( |
1013 | bg => [1, 1, 1, 1], |
1080 | bg => [1, 1, 1, 1], |
1014 | border_bg => [1, 1, 1, 1], |
1081 | border_bg => [1, 1, 1, 1], |
1015 | border => 0.6, |
1082 | border => 0.6, |
1016 | is_toplevel => 1, |
|
|
1017 | can_events => 1, |
1083 | can_events => 1, |
1018 | @_ |
1084 | min_w => 16, |
|
|
1085 | min_h => 16, |
|
|
1086 | %arg, |
1019 | ); |
1087 | ); |
1020 | |
1088 | |
1021 | $self->{title} &&= new CFClient::UI::Label |
1089 | $self->{title} = new CFClient::UI::Label |
1022 | align => 0, |
1090 | align => 0, |
1023 | valign => 1, |
1091 | valign => 1, |
1024 | text => $self->{title}, |
1092 | text => $title, |
1025 | fontsize => $self->{border}; |
1093 | fontsize => $self->{border} |
|
|
1094 | if defined $title; |
1026 | |
1095 | |
1027 | $self |
1096 | $self |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | sub add { |
|
|
1100 | my ($self, @widgets) = @_; |
|
|
1101 | |
|
|
1102 | $self->SUPER::add (@widgets); |
|
|
1103 | $self->CFClient::UI::Container::add ($self->{title}) if $self->{title}; |
1028 | } |
1104 | } |
1029 | |
1105 | |
1030 | sub border { |
1106 | sub border { |
1031 | int $_[0]{border} * $::FONTSIZE |
1107 | int $_[0]{border} * $::FONTSIZE |
1032 | } |
1108 | } |
1033 | |
1109 | |
1034 | sub size_request { |
1110 | sub size_request { |
1035 | my ($self) = @_; |
1111 | my ($self) = @_; |
|
|
1112 | |
|
|
1113 | $self->{title}->size_request |
|
|
1114 | if $self->{title}; |
1036 | |
1115 | |
1037 | my ($w, $h) = $self->SUPER::size_request; |
1116 | my ($w, $h) = $self->SUPER::size_request; |
1038 | |
1117 | |
1039 | ( |
1118 | ( |
1040 | $w + $self->border * 2, |
1119 | $w + $self->border * 2, |
1041 | $h + $self->border * 2, |
1120 | $h + $self->border * 2, |
1042 | ) |
1121 | ) |
1043 | } |
1122 | } |
1044 | |
1123 | |
1045 | sub size_allocate { |
1124 | sub size_allocate { |
1046 | my ($self, $w, $h, $changed) = @_; |
1125 | my ($self, $w, $h) = @_; |
1047 | |
1126 | |
1048 | return unless $changed; |
1127 | if ($self->{title}) { |
|
|
1128 | $self->{title}{w} = $w; |
|
|
1129 | $self->{title}{h} = $h; |
|
|
1130 | $self->{title}->size_allocate ($w, $h); |
|
|
1131 | } |
1049 | |
1132 | |
|
|
1133 | my $border = $self->border; |
|
|
1134 | |
1050 | $h -= List::Util::max 0, $self->border * 2; |
1135 | $h -= List::Util::max 0, $border * 2; |
1051 | $w -= List::Util::max 0, $self->border * 2; |
1136 | $w -= List::Util::max 0, $border * 2; |
1052 | |
1137 | |
1053 | $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) |
|
|
1054 | if $self->{title}; |
|
|
1055 | |
|
|
1056 | $self->child->configure ($self->border, $self->border, $w, $h); |
1138 | $self->child->configure ($border, $border, $w, $h); |
1057 | } |
1139 | } |
1058 | |
1140 | |
1059 | sub button_down { |
1141 | sub button_down { |
1060 | my ($self, $ev, $x, $y) = @_; |
1142 | my ($self, $ev, $x, $y) = @_; |
1061 | |
1143 | |
… | |
… | |
1077 | my ($ev, $x, $y) = @_; |
1159 | my ($ev, $x, $y) = @_; |
1078 | |
1160 | |
1079 | my $dx = $ev->{x} - $ox; |
1161 | my $dx = $ev->{x} - $ox; |
1080 | my $dy = $ev->{y} - $oy; |
1162 | my $dy = $ev->{y} - $oy; |
1081 | |
1163 | |
1082 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1083 | $self->{user_y} = $wy + $dy * $my; |
|
|
1084 | $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); |
1164 | $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); |
1085 | $self->{def_h} = $bh + $dy * ($my ? -1 : 1); |
1165 | $self->{force_h} = $bh + $dy * ($my ? -1 : 1); |
1086 | $self->move ($self->{user_x}, $self->{user_y}); |
1166 | |
|
|
1167 | $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); |
1087 | $self->realloc; |
1168 | $self->realloc; |
1088 | }; |
1169 | }; |
1089 | |
1170 | |
1090 | } elsif ($lr ^ $td) { |
1171 | } elsif ($lr ^ $td) { |
1091 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1172 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
… | |
… | |
1094 | $self->{motion} = sub { |
1175 | $self->{motion} = sub { |
1095 | my ($ev, $x, $y) = @_; |
1176 | my ($ev, $x, $y) = @_; |
1096 | |
1177 | |
1097 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1178 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1098 | |
1179 | |
1099 | $self->{user_x} = $bx + $x - $ox; |
1180 | $self->move_abs ($bx + $x - $ox, $by + $y - $oy); |
1100 | $self->{user_y} = $by + $y - $oy; |
1181 | # HACK: the next line is required to enforce placement |
1101 | |
1182 | $self->{parent}->size_allocate ($self->{parent}{w}, $self->{parent}{h}); |
1102 | $self->move ($self->{user_x}, $self->{user_y}); |
|
|
1103 | $self->realloc; |
|
|
1104 | }; |
1183 | }; |
|
|
1184 | } else { |
|
|
1185 | return 0; |
|
|
1186 | } |
|
|
1187 | |
1105 | } |
1188 | 1 |
1106 | } |
1189 | } |
1107 | |
1190 | |
1108 | sub button_up { |
1191 | sub button_up { |
1109 | my ($self, $ev, $x, $y) = @_; |
1192 | my ($self, $ev, $x, $y) = @_; |
1110 | |
1193 | |
1111 | delete $self->{motion}; |
1194 | !!delete $self->{motion} |
1112 | } |
1195 | } |
1113 | |
1196 | |
1114 | sub mouse_motion { |
1197 | sub mouse_motion { |
1115 | my ($self, $ev, $x, $y) = @_; |
1198 | my ($self, $ev, $x, $y) = @_; |
1116 | |
1199 | |
1117 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
1200 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
|
|
1201 | |
|
|
1202 | !!$self->{motion} |
1118 | } |
1203 | } |
1119 | |
1204 | |
1120 | sub _draw { |
1205 | sub _draw { |
1121 | my ($self) = @_; |
1206 | my ($self) = @_; |
1122 | |
1207 | |
|
|
1208 | my $child = $self->{children}[0]; |
|
|
1209 | |
1123 | my ($w, $h ) = ($self->{w}, $self->{h}); |
1210 | my ($w, $h ) = ($self->{w}, $self->{h}); |
1124 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
1211 | my ($cw, $ch) = ($child->{w}, $child->{h}); |
1125 | |
1212 | |
1126 | glEnable GL_TEXTURE_2D; |
1213 | glEnable GL_TEXTURE_2D; |
1127 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1214 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1128 | |
1215 | |
1129 | my $border = $self->border; |
1216 | my $border = $self->border; |
1130 | |
1217 | |
1131 | glColor @{ $self->{border_bg} }; |
1218 | glColor @{ $self->{border_bg} }; |
1132 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
1219 | $border[0]->draw_quad_alpha (0, 0, $w, $border); |
1133 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
1220 | $border[1]->draw_quad_alpha (0, $border, $border, $ch); |
1134 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1221 | $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
1135 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
1222 | $border[3]->draw_quad_alpha (0, $h - $border, $w, $border); |
1136 | |
1223 | |
1137 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1224 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
1138 | my $bg = $tex[0]; |
1225 | glColor @{ $self->{bg} }; |
1139 | |
1226 | |
1140 | # TODO: repeat texture not scale |
1227 | # TODO: repeat texture not scale |
|
|
1228 | # solve this better(?) |
1141 | my $rep_x = $cw / $bg->{w}; |
1229 | $bg->{s} = $cw / $bg->{w}; |
1142 | my $rep_y = $ch / $bg->{h}; |
1230 | $bg->{t} = $ch / $bg->{h}; |
1143 | |
|
|
1144 | glColor @{ $self->{bg} }; |
|
|
1145 | |
|
|
1146 | $bg->{s} = $rep_x; |
|
|
1147 | $bg->{t} = $rep_y; |
|
|
1148 | $bg->{wrap_mode} = 1; |
|
|
1149 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
1231 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
1150 | } |
1232 | } |
1151 | |
1233 | |
1152 | glDisable GL_TEXTURE_2D; |
1234 | glDisable GL_TEXTURE_2D; |
1153 | |
1235 | |
1154 | $self->{title}->draw if $self->{title}; |
|
|
1155 | |
|
|
1156 | $self->child->draw; |
1236 | $child->draw; |
|
|
1237 | |
|
|
1238 | if ($self->{title}) { |
|
|
1239 | glTranslate 0, $border - $self->{h}; |
|
|
1240 | $self->{title}->_draw; |
|
|
1241 | } |
1157 | } |
1242 | } |
1158 | |
1243 | |
1159 | ############################################################################# |
1244 | ############################################################################# |
1160 | |
1245 | |
1161 | package CFClient::UI::Table; |
1246 | package CFClient::UI::Table; |
… | |
… | |
1235 | (sum @$hs), |
1320 | (sum @$hs), |
1236 | ) |
1321 | ) |
1237 | } |
1322 | } |
1238 | |
1323 | |
1239 | sub size_allocate { |
1324 | sub size_allocate { |
1240 | my ($self, $w, $h, $changed) = @_; |
1325 | my ($self, $w, $h) = @_; |
1241 | |
1326 | |
1242 | my ($ws, $hs) = $self->get_wh; |
1327 | my ($ws, $hs) = $self->get_wh; |
1243 | |
1328 | |
1244 | my $req_w = (sum @$ws) || 1; |
1329 | my $req_w = (sum @$ws) || 1; |
1245 | my $req_h = (sum @$hs) || 1; |
1330 | my $req_h = (sum @$hs) || 1; |
… | |
… | |
1323 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1408 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1324 | ) |
1409 | ) |
1325 | } |
1410 | } |
1326 | |
1411 | |
1327 | sub size_allocate { |
1412 | sub size_allocate { |
1328 | my ($self, $w, $h, $changed) = @_; |
1413 | my ($self, $w, $h) = @_; |
1329 | |
1414 | |
1330 | my $space = $self->{vertical} ? $h : $w; |
1415 | my $space = $self->{vertical} ? $h : $w; |
1331 | my $children = $self->{children}; |
1416 | my $children = $self->{children}; |
1332 | |
1417 | |
1333 | my @req; |
1418 | my @req; |
… | |
… | |
1416 | ellipsise => 3, # end |
1501 | ellipsise => 3, # end |
1417 | layout => (new CFClient::Layout), |
1502 | layout => (new CFClient::Layout), |
1418 | fontsize => 1, |
1503 | fontsize => 1, |
1419 | align => -1, |
1504 | align => -1, |
1420 | valign => -1, |
1505 | valign => -1, |
1421 | padding => 2, |
1506 | padding_x => 2, |
|
|
1507 | padding_y => 2, |
1422 | can_events => 0, |
1508 | can_events => 0, |
1423 | %arg |
1509 | %arg |
1424 | ); |
1510 | ); |
1425 | |
1511 | |
1426 | if (exists $self->{template}) { |
1512 | if (exists $self->{template}) { |
… | |
… | |
1502 | |
1588 | |
1503 | $w = List::Util::max $w, $w2; |
1589 | $w = List::Util::max $w, $w2; |
1504 | $h = List::Util::max $h, $h2; |
1590 | $h = List::Util::max $h, $h2; |
1505 | } |
1591 | } |
1506 | |
1592 | |
1507 | ( |
1593 | ($w, $h) |
1508 | $w + $self->{padding} * 2, |
|
|
1509 | $h + $self->{padding} * 2, |
|
|
1510 | ) |
|
|
1511 | } |
1594 | } |
1512 | |
1595 | |
1513 | sub size_allocate { |
1596 | sub size_allocate { |
1514 | my ($self, $w, $h, $changed) = @_; |
1597 | my ($self, $w, $h) = @_; |
|
|
1598 | |
|
|
1599 | delete $self->{ox}; |
1515 | |
1600 | |
1516 | delete $self->{texture} |
1601 | delete $self->{texture} |
1517 | if $changed; |
1602 | unless $w >= $self->{req_w} && $self->{old_w} >= $self->{req_w}; |
1518 | } |
1603 | } |
1519 | |
1604 | |
1520 | sub set_fontsize { |
1605 | sub set_fontsize { |
1521 | my ($self, $fontsize) = @_; |
1606 | my ($self, $fontsize) = @_; |
1522 | |
1607 | |
… | |
… | |
1537 | $self->{layout}->set_width ($self->{w}); |
1622 | $self->{layout}->set_width ($self->{w}); |
1538 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1623 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1539 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1624 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1540 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1625 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1541 | |
1626 | |
1542 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1627 | new_from_layout CFClient::Texture $self->{layout} |
|
|
1628 | }; |
1543 | |
1629 | |
|
|
1630 | unless (exists $self->{ox}) { |
1544 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding} |
1631 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
1545 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1632 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} |
1546 | : ($self->{w} - $tex->{w}) * 0.5); |
1633 | : ($self->{w} - $tex->{w}) * 0.5); |
1547 | |
1634 | |
1548 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} |
1635 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
1549 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1636 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} |
1550 | : ($self->{h} - $tex->{h}) * 0.5); |
1637 | : ($self->{h} - $tex->{h}) * 0.5); |
1551 | |
|
|
1552 | $tex |
|
|
1553 | }; |
1638 | }; |
1554 | |
1639 | |
1555 | glEnable GL_TEXTURE_2D; |
1640 | glEnable GL_TEXTURE_2D; |
1556 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1641 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1557 | |
1642 | |
… | |
… | |
1595 | |
1680 | |
1596 | delete $self->{cur_h}; |
1681 | delete $self->{cur_h}; |
1597 | |
1682 | |
1598 | return if $self->{text} eq $text; |
1683 | return if $self->{text} eq $text; |
1599 | |
1684 | |
1600 | delete $self->{texture}; |
|
|
1601 | |
|
|
1602 | $self->{last_activity} = $::NOW; |
1685 | $self->{last_activity} = $::NOW; |
1603 | $self->{text} = $text; |
1686 | $self->{text} = $text; |
1604 | |
1687 | |
1605 | $text =~ s/./*/g if $self->{hidden}; |
1688 | $text =~ s/./*/g if $self->{hidden}; |
1606 | $self->{layout}->set_text ("$text "); |
1689 | $self->{layout}->set_text ("$text "); |
1607 | |
1690 | |
1608 | $self->_emit (changed => $self->{text}); |
1691 | $self->_emit (changed => $self->{text}); |
|
|
1692 | $self->update; |
1609 | } |
1693 | } |
1610 | |
1694 | |
1611 | sub set_text { |
1695 | sub set_text { |
1612 | my ($self, $text) = @_; |
1696 | my ($self, $text) = @_; |
1613 | |
1697 | |
… | |
… | |
1652 | $self->{cursor} = length $text; |
1736 | $self->{cursor} = length $text; |
1653 | } elsif ($uni == 27) { |
1737 | } elsif ($uni == 27) { |
1654 | $self->_emit ('escape'); |
1738 | $self->_emit ('escape'); |
1655 | } elsif ($uni) { |
1739 | } elsif ($uni) { |
1656 | substr $text, $self->{cursor}++, 0, chr $uni; |
1740 | substr $text, $self->{cursor}++, 0, chr $uni; |
|
|
1741 | } else { |
|
|
1742 | return 0; |
1657 | } |
1743 | } |
1658 | |
1744 | |
1659 | $self->_set_text ($text); |
1745 | $self->_set_text ($text); |
1660 | |
1746 | |
1661 | $self->realloc; |
1747 | $self->realloc; |
|
|
1748 | |
|
|
1749 | 1 |
1662 | } |
1750 | } |
1663 | |
1751 | |
1664 | sub focus_in { |
1752 | sub focus_in { |
1665 | my ($self) = @_; |
1753 | my ($self) = @_; |
1666 | |
1754 | |
… | |
… | |
1681 | utf8::encode $text; |
1769 | utf8::encode $text; |
1682 | $self->{cursor} = length substr $text, 0, $idx; |
1770 | $self->{cursor} = length substr $text, 0, $idx; |
1683 | |
1771 | |
1684 | $self->_set_text ($self->{text}); |
1772 | $self->_set_text ($self->{text}); |
1685 | $self->update; |
1773 | $self->update; |
|
|
1774 | |
|
|
1775 | 1 |
1686 | } |
1776 | } |
1687 | |
1777 | |
1688 | sub mouse_motion { |
1778 | sub mouse_motion { |
1689 | my ($self, $ev, $x, $y) = @_; |
1779 | my ($self, $ev, $x, $y) = @_; |
1690 | # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
1780 | # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
|
|
1781 | |
|
|
1782 | 0 |
1691 | } |
1783 | } |
1692 | |
1784 | |
1693 | sub _draw { |
1785 | sub _draw { |
1694 | my ($self) = @_; |
1786 | my ($self) = @_; |
1695 | |
1787 | |
1696 | local $self->{fg} = $self->{fg}; |
1788 | local $self->{fg} = $self->{fg}; |
1697 | |
1789 | |
1698 | if ($FOCUS == $self) { |
1790 | if ($FOCUS == $self) { |
1699 | glColor @{$self->{active_bg}}; |
1791 | glColor_premultiply @{$self->{active_bg}}; |
1700 | $self->{fg} = $self->{active_fg}; |
1792 | $self->{fg} = $self->{active_fg}; |
1701 | } else { |
1793 | } else { |
1702 | glColor @{$self->{bg}}; |
1794 | glColor_premultiply @{$self->{bg}}; |
1703 | } |
1795 | } |
1704 | |
1796 | |
1705 | glEnable GL_BLEND; |
1797 | glEnable GL_BLEND; |
1706 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1798 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
1707 | glBegin GL_QUADS; |
1799 | glBegin GL_QUADS; |
1708 | glVertex 0 , 0; |
1800 | glVertex 0 , 0; |
1709 | glVertex 0 , $self->{h}; |
1801 | glVertex 0 , $self->{h}; |
1710 | glVertex $self->{w}, $self->{h}; |
1802 | glVertex $self->{w}, $self->{h}; |
1711 | glVertex $self->{w}, 0; |
1803 | glVertex $self->{w}, 0; |
… | |
… | |
1772 | } else { |
1864 | } else { |
1773 | $self->set_text ($self->{history_saveback}); |
1865 | $self->set_text ($self->{history_saveback}); |
1774 | } |
1866 | } |
1775 | |
1867 | |
1776 | } else { |
1868 | } else { |
1777 | $self->SUPER::key_down ($ev); |
1869 | return $self->SUPER::key_down ($ev) |
|
|
1870 | } |
|
|
1871 | |
1778 | } |
1872 | 1 |
1779 | |
|
|
1780 | } |
1873 | } |
1781 | |
1874 | |
1782 | ############################################################################# |
1875 | ############################################################################# |
1783 | |
1876 | |
1784 | package CFClient::UI::Button; |
1877 | package CFClient::UI::Button; |
… | |
… | |
1793 | |
1886 | |
1794 | sub new { |
1887 | sub new { |
1795 | my $class = shift; |
1888 | my $class = shift; |
1796 | |
1889 | |
1797 | $class->SUPER::new ( |
1890 | $class->SUPER::new ( |
1798 | padding => 4, |
1891 | padding_x => 4, |
|
|
1892 | padding_y => 4, |
1799 | fg => [1, 1, 1], |
1893 | fg => [1, 1, 1], |
1800 | active_fg => [0, 0, 1], |
1894 | active_fg => [0, 0, 1], |
1801 | can_hover => 1, |
1895 | can_hover => 1, |
1802 | align => 0, |
1896 | align => 0, |
1803 | valign => 0, |
1897 | valign => 0, |
… | |
… | |
1812 | my ($self, $ev, $x, $y) = @_; |
1906 | my ($self, $ev, $x, $y) = @_; |
1813 | |
1907 | |
1814 | $self->emit ("activate") |
1908 | $self->emit ("activate") |
1815 | if $x >= 0 && $x < $self->{w} |
1909 | if $x >= 0 && $x < $self->{w} |
1816 | && $y >= 0 && $y < $self->{h}; |
1910 | && $y >= 0 && $y < $self->{h}; |
|
|
1911 | |
|
|
1912 | 1 |
1817 | } |
1913 | } |
1818 | |
1914 | |
1819 | sub _draw { |
1915 | sub _draw { |
1820 | my ($self) = @_; |
1916 | my ($self) = @_; |
1821 | |
1917 | |
… | |
… | |
1850 | |
1946 | |
1851 | sub new { |
1947 | sub new { |
1852 | my $class = shift; |
1948 | my $class = shift; |
1853 | |
1949 | |
1854 | $class->SUPER::new ( |
1950 | $class->SUPER::new ( |
1855 | padding => 2, |
1951 | padding_x => 2, |
|
|
1952 | padding_y => 2, |
1856 | fg => [1, 1, 1], |
1953 | fg => [1, 1, 1], |
1857 | active_fg => [1, 1, 0], |
1954 | active_fg => [1, 1, 0], |
1858 | bg => [0, 0, 0, 0.2], |
1955 | bg => [0, 0, 0, 0.2], |
1859 | active_bg => [1, 1, 1, 0.5], |
1956 | active_bg => [1, 1, 1, 0.5], |
1860 | state => 0, |
1957 | state => 0, |
… | |
… | |
1864 | } |
1961 | } |
1865 | |
1962 | |
1866 | sub size_request { |
1963 | sub size_request { |
1867 | my ($self) = @_; |
1964 | my ($self) = @_; |
1868 | |
1965 | |
1869 | ($self->{padding} * 2 + 6) x 2 |
1966 | (6) x 2 |
1870 | } |
1967 | } |
1871 | |
1968 | |
1872 | sub button_down { |
1969 | sub button_down { |
1873 | my ($self, $ev, $x, $y) = @_; |
1970 | my ($self, $ev, $x, $y) = @_; |
1874 | |
1971 | |
1875 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1972 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
1876 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1973 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
1877 | $self->{state} = !$self->{state}; |
1974 | $self->{state} = !$self->{state}; |
1878 | $self->_emit (changed => $self->{state}); |
1975 | $self->_emit (changed => $self->{state}); |
|
|
1976 | } else { |
|
|
1977 | return 0 |
|
|
1978 | } |
|
|
1979 | |
1879 | } |
1980 | 1 |
1880 | } |
1981 | } |
1881 | |
1982 | |
1882 | sub _draw { |
1983 | sub _draw { |
1883 | my ($self) = @_; |
1984 | my ($self) = @_; |
1884 | |
1985 | |
1885 | $self->SUPER::_draw; |
1986 | $self->SUPER::_draw; |
1886 | |
1987 | |
1887 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1988 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
1888 | |
1989 | |
1889 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1990 | my ($w, $h) = @$self{qw(w h)}; |
|
|
1991 | |
|
|
1992 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
1890 | |
1993 | |
1891 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1994 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1892 | |
1995 | |
1893 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1996 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1894 | |
1997 | |
… | |
… | |
2049 | |
2152 | |
2050 | my $h1 = $self->{h} * (1 - $ycut1); |
2153 | my $h1 = $self->{h} * (1 - $ycut1); |
2051 | my $h2 = $self->{h} * (1 - $ycut2); |
2154 | my $h2 = $self->{h} * (1 - $ycut2); |
2052 | |
2155 | |
2053 | glEnable GL_BLEND; |
2156 | glEnable GL_BLEND; |
2054 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
2157 | glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
|
|
2158 | GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2055 | glEnable GL_TEXTURE_2D; |
2159 | glEnable GL_TEXTURE_2D; |
2056 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2160 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2057 | |
2161 | |
2058 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
2162 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
2059 | glBegin GL_QUADS; |
2163 | glBegin GL_QUADS; |
… | |
… | |
2159 | fg => [1, 1, 1], |
2263 | fg => [1, 1, 1], |
2160 | active_fg => [0, 0, 0], |
2264 | active_fg => [0, 0, 0], |
2161 | bg => [0, 0, 0, 0.2], |
2265 | bg => [0, 0, 0, 0.2], |
2162 | active_bg => [1, 1, 1, 0.5], |
2266 | active_bg => [1, 1, 1, 0.5], |
2163 | range => [0, 0, 100, 10, 0], |
2267 | range => [0, 0, 100, 10, 0], |
2164 | req_w => $::WIDTH / 80, |
2268 | min_w => $::WIDTH / 80, |
2165 | req_h => $::WIDTH / 80, |
2269 | min_h => $::WIDTH / 80, |
2166 | vertical => 0, |
2270 | vertical => 0, |
2167 | can_hover => 1, |
2271 | can_hover => 1, |
2168 | inner_pad => 0.02, |
2272 | inner_pad => 0.02, |
2169 | @_ |
2273 | @_ |
2170 | ); |
2274 | ); |
… | |
… | |
2210 | } |
2314 | } |
2211 | |
2315 | |
2212 | sub size_request { |
2316 | sub size_request { |
2213 | my ($self) = @_; |
2317 | my ($self) = @_; |
2214 | |
2318 | |
2215 | my $w = $self->{req_w}; |
2319 | ($self->{req_w}, $self->{req_h}) |
2216 | my $h = $self->{req_h}; |
|
|
2217 | |
|
|
2218 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
2219 | } |
2320 | } |
2220 | |
2321 | |
2221 | sub button_down { |
2322 | sub button_down { |
2222 | my ($self, $ev, $x, $y) = @_; |
2323 | my ($self, $ev, $x, $y) = @_; |
2223 | |
2324 | |
2224 | $self->SUPER::button_down ($ev, $x, $y); |
2325 | $self->SUPER::button_down ($ev, $x, $y); |
2225 | |
2326 | |
2226 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
2327 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
2227 | |
2328 | |
2228 | $self->mouse_motion ($ev, $x, $y); |
2329 | $self->mouse_motion ($ev, $x, $y) |
2229 | } |
2330 | } |
2230 | |
2331 | |
2231 | sub mouse_motion { |
2332 | sub mouse_motion { |
2232 | my ($self, $ev, $x, $y) = @_; |
2333 | my ($self, $ev, $x, $y) = @_; |
2233 | |
2334 | |
… | |
… | |
2237 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2338 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2238 | |
2339 | |
2239 | $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); |
2340 | $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); |
2240 | |
2341 | |
2241 | $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); |
2342 | $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); |
|
|
2343 | } else { |
|
|
2344 | return 0; |
|
|
2345 | } |
|
|
2346 | |
2242 | } |
2347 | 1 |
2243 | } |
2348 | } |
2244 | |
2349 | |
2245 | sub update { |
2350 | sub update { |
2246 | my ($self) = @_; |
2351 | my ($self) = @_; |
2247 | |
2352 | |
2248 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
2353 | delete $self->{knob_w}; |
|
|
2354 | $self->SUPER::update; |
|
|
2355 | } |
|
|
2356 | |
|
|
2357 | sub _draw { |
|
|
2358 | my ($self) = @_; |
|
|
2359 | |
|
|
2360 | unless ($self->{knob_w}) { |
2249 | $self->set_value ($self->{range}[0]); |
2361 | $self->set_value ($self->{range}[0]); |
2250 | |
2362 | |
2251 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2363 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2252 | my $range = ($hi - $page - $lo) || 1e-100; |
2364 | my $range = ($hi - $page - $lo) || 1e-100; |
2253 | |
2365 | |
… | |
… | |
2259 | $value = ($value - $lo) / $range; |
2371 | $value = ($value - $lo) / $range; |
2260 | $value = $value * $self->{scale} + $self->{offset}; |
2372 | $value = $value * $self->{scale} + $self->{offset}; |
2261 | |
2373 | |
2262 | $self->{knob_x} = $value - $knob_w * 0.5; |
2374 | $self->{knob_x} = $value - $knob_w * 0.5; |
2263 | $self->{knob_w} = $knob_w; |
2375 | $self->{knob_w} = $knob_w; |
2264 | }); |
2376 | } |
2265 | |
|
|
2266 | $self->SUPER::update; |
|
|
2267 | } |
|
|
2268 | |
|
|
2269 | sub _draw { |
|
|
2270 | my ($self) = @_; |
|
|
2271 | |
2377 | |
2272 | $self->SUPER::_draw (); |
2378 | $self->SUPER::_draw (); |
2273 | |
2379 | |
2274 | glScale $self->{w}, $self->{h}; |
2380 | glScale $self->{w}, $self->{h}; |
2275 | |
2381 | |
… | |
… | |
2373 | $self->{fontsize} = $fontsize; |
2479 | $self->{fontsize} = $fontsize; |
2374 | $self->reflow; |
2480 | $self->reflow; |
2375 | } |
2481 | } |
2376 | |
2482 | |
2377 | sub size_allocate { |
2483 | sub size_allocate { |
2378 | my ($self, $w, $h, $changed) = @_; |
2484 | my ($self, $w, $h) = @_; |
2379 | |
2485 | |
2380 | $self->SUPER::size_allocate ($w, $h, $changed); |
2486 | $self->SUPER::size_allocate ($w, $h); |
2381 | |
|
|
2382 | return unless $changed; |
|
|
2383 | |
2487 | |
2384 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
2488 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
2385 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2489 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2386 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2490 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2387 | |
2491 | |
… | |
… | |
2610 | } |
2714 | } |
2611 | |
2715 | |
2612 | sub set_tooltip_from { |
2716 | sub set_tooltip_from { |
2613 | my ($self, $widget) = @_; |
2717 | my ($self, $widget) = @_; |
2614 | |
2718 | |
|
|
2719 | my $tooltip = $widget->{tooltip}; |
|
|
2720 | |
|
|
2721 | if ($ENV{CFPLUS_DEBUG} & 2) { |
|
|
2722 | $tooltip .= "\n\n" . (ref $widget) . "\n" |
|
|
2723 | . "$widget->{x} $widget->{y} $widget->{w} $widget->{h}\n" |
|
|
2724 | . "req $widget->{req_w} $widget->{req_h}\n" |
|
|
2725 | . "visible $widget->{visible}"; |
|
|
2726 | } |
|
|
2727 | |
2615 | $self->add (new CFClient::UI::Label |
2728 | $self->add (new CFClient::UI::Label |
2616 | markup => $widget->{tooltip}, |
2729 | markup => $tooltip, |
2617 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
2730 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
2618 | fontsize => 0.8, |
2731 | fontsize => 0.8, |
2619 | fg => [0, 0, 0, 1], |
2732 | fg => [0, 0, 0, 1], |
2620 | ellipsise => 0, |
2733 | ellipsise => 0, |
2621 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
2734 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
… | |
… | |
2629 | |
2742 | |
2630 | ($w + 4, $h + 4) |
2743 | ($w + 4, $h + 4) |
2631 | } |
2744 | } |
2632 | |
2745 | |
2633 | sub size_allocate { |
2746 | sub size_allocate { |
2634 | my ($self, $w, $h, $changed) = @_; |
2747 | my ($self, $w, $h) = @_; |
2635 | |
2748 | |
2636 | return unless $changed; |
|
|
2637 | |
|
|
2638 | $self->SUPER::size_allocate ($w - 4, $h - 4, $changed); |
2749 | $self->SUPER::size_allocate ($w - 4, $h - 4); |
2639 | } |
2750 | } |
2640 | |
2751 | |
2641 | sub visibility_change { |
2752 | sub visibility_change { |
2642 | my ($self, $visible) = @_; |
2753 | my ($self, $visible) = @_; |
2643 | |
2754 | |
… | |
… | |
2650 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
2761 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
2651 | |
2762 | |
2652 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
2763 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
2653 | if $x + $self->{w} > $::WIDTH; |
2764 | if $x + $self->{w} > $::WIDTH; |
2654 | |
2765 | |
2655 | $self->move ($x, $y); |
2766 | $self->move_abs ($x, $y); |
2656 | }); |
2767 | }); |
2657 | } |
2768 | } |
2658 | |
2769 | |
2659 | sub _draw { |
2770 | sub _draw { |
2660 | my ($self) = @_; |
2771 | my ($self) = @_; |
… | |
… | |
2764 | $self->SUPER::DESTROY; |
2875 | $self->SUPER::DESTROY; |
2765 | } |
2876 | } |
2766 | |
2877 | |
2767 | ############################################################################# |
2878 | ############################################################################# |
2768 | |
2879 | |
2769 | package CFClient::UI::Inventory; |
2880 | package CFClient::UI::Buttonbar; |
2770 | |
2881 | |
2771 | our @ISA = CFClient::UI::ScrolledWindow::; |
2882 | our @ISA = CFClient::UI::HBox::; |
2772 | |
2883 | |
2773 | sub new { |
2884 | # TODO: should actualyl wrap buttons and other goodies. |
2774 | my $class = shift; |
|
|
2775 | |
|
|
2776 | my $self = $class->SUPER::new ( |
|
|
2777 | scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]), |
|
|
2778 | @_, |
|
|
2779 | ); |
|
|
2780 | |
|
|
2781 | $self |
|
|
2782 | } |
|
|
2783 | |
|
|
2784 | sub set_items { |
|
|
2785 | my ($self, $items) = @_; |
|
|
2786 | |
|
|
2787 | $self->{scrolled}->clear; |
|
|
2788 | return unless $items; |
|
|
2789 | |
|
|
2790 | my @items = sort { |
|
|
2791 | ($a->{type} <=> $b->{type}) |
|
|
2792 | or ($a->{name} cmp $b->{name}) |
|
|
2793 | } @$items; |
|
|
2794 | |
|
|
2795 | $self->{real_items} = \@items; |
|
|
2796 | |
|
|
2797 | my $row = 0; |
|
|
2798 | for my $item (@items) { |
|
|
2799 | CFClient::Item::update_widgets $item; |
|
|
2800 | |
|
|
2801 | $self->{scrolled}->add (0, $row, $item->{face_widget}); |
|
|
2802 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
|
|
2803 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
|
|
2804 | |
|
|
2805 | $row++; |
|
|
2806 | } |
|
|
2807 | } |
|
|
2808 | |
2885 | |
2809 | ############################################################################# |
2886 | ############################################################################# |
2810 | |
2887 | |
2811 | package CFClient::UI::Menu; |
2888 | package CFClient::UI::Menu; |
2812 | |
2889 | |
… | |
… | |
2853 | # maybe save $GRAB? must be careful about events... |
2930 | # maybe save $GRAB? must be careful about events... |
2854 | $GRAB = $self; |
2931 | $GRAB = $self; |
2855 | $self->{button} = $ev->{button}; |
2932 | $self->{button} = $ev->{button}; |
2856 | |
2933 | |
2857 | $self->show; |
2934 | $self->show; |
2858 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
2935 | $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
2859 | } |
2936 | } |
2860 | |
2937 | |
2861 | sub mouse_motion { |
2938 | sub mouse_motion { |
2862 | my ($self, $ev, $x, $y) = @_; |
2939 | my ($self, $ev, $x, $y) = @_; |
2863 | |
2940 | |
2864 | # TODO: should use vbox->find_widget or so |
2941 | # TODO: should use vbox->find_widget or so |
2865 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
2942 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
2866 | $self->{hover} = $self->{item}{$HOVER}; |
2943 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2944 | |
|
|
2945 | 0 |
2867 | } |
2946 | } |
2868 | |
2947 | |
2869 | sub button_up { |
2948 | sub button_up { |
2870 | my ($self, $ev, $x, $y) = @_; |
2949 | my ($self, $ev, $x, $y) = @_; |
2871 | |
2950 | |
… | |
… | |
2873 | undef $GRAB; |
2952 | undef $GRAB; |
2874 | $self->hide; |
2953 | $self->hide; |
2875 | |
2954 | |
2876 | $self->_emit ("popdown"); |
2955 | $self->_emit ("popdown"); |
2877 | $self->{hover}[1]->() if $self->{hover}; |
2956 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2957 | } else { |
|
|
2958 | return 0 |
|
|
2959 | } |
|
|
2960 | |
|
|
2961 | 1 |
|
|
2962 | } |
|
|
2963 | |
|
|
2964 | ############################################################################# |
|
|
2965 | |
|
|
2966 | package CFClient::UI::Multiplexer; |
|
|
2967 | |
|
|
2968 | our @ISA = CFClient::UI::Container::; |
|
|
2969 | |
|
|
2970 | sub new { |
|
|
2971 | my $class = shift; |
|
|
2972 | |
|
|
2973 | my $self = $class->SUPER::new ( |
|
|
2974 | @_, |
|
|
2975 | ); |
|
|
2976 | |
|
|
2977 | $self->{current} = $self->{children}[0] |
|
|
2978 | if @{ $self->{children} }; |
|
|
2979 | |
|
|
2980 | $self |
|
|
2981 | } |
|
|
2982 | |
|
|
2983 | sub add { |
|
|
2984 | my ($self, @widgets) = @_; |
|
|
2985 | |
|
|
2986 | $self->SUPER::add (@widgets); |
|
|
2987 | |
|
|
2988 | $self->{current} = $self->{children}[0] |
|
|
2989 | if @{ $self->{children} }; |
|
|
2990 | } |
|
|
2991 | |
|
|
2992 | sub set_current_page { |
|
|
2993 | my ($self, $page_or_widget) = @_; |
|
|
2994 | |
|
|
2995 | my $widget = ref $page_or_widget |
|
|
2996 | ? $page_or_widget |
|
|
2997 | : $self->{children}[$page_or_widget]; |
|
|
2998 | |
|
|
2999 | $self->{current} = $widget; |
|
|
3000 | $self->{current}->configure (0, 0, $self->{w}, $self->{h}); |
|
|
3001 | |
|
|
3002 | $self->_emit (page_changed => $self->{current}); |
|
|
3003 | |
|
|
3004 | $self->realloc; |
|
|
3005 | } |
|
|
3006 | |
|
|
3007 | sub visible_children { |
|
|
3008 | $_[0]{current} |
|
|
3009 | } |
|
|
3010 | |
|
|
3011 | sub size_request { |
|
|
3012 | my ($self) = @_; |
|
|
3013 | |
|
|
3014 | $self->{current}->size_request |
|
|
3015 | } |
|
|
3016 | |
|
|
3017 | sub size_allocate { |
|
|
3018 | my ($self, $w, $h) = @_; |
|
|
3019 | |
|
|
3020 | $self->{current}->configure (0, 0, $w, $h); |
|
|
3021 | } |
|
|
3022 | |
|
|
3023 | sub _draw { |
|
|
3024 | my ($self) = @_; |
|
|
3025 | |
|
|
3026 | $self->{current}->draw; |
|
|
3027 | } |
|
|
3028 | |
|
|
3029 | ############################################################################# |
|
|
3030 | |
|
|
3031 | package CFClient::UI::Notebook; |
|
|
3032 | |
|
|
3033 | our @ISA = CFClient::UI::VBox::; |
|
|
3034 | |
|
|
3035 | sub new { |
|
|
3036 | my $class = shift; |
|
|
3037 | |
|
|
3038 | my $self = $class->SUPER::new ( |
|
|
3039 | buttonbar => (new CFClient::UI::Buttonbar), |
|
|
3040 | multiplexer => (new CFClient::UI::Multiplexer expand => 1), |
|
|
3041 | # filter => # will be put between multiplexer and $self |
|
|
3042 | @_, |
|
|
3043 | ); |
2878 | } |
3044 | |
|
|
3045 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
|
|
3046 | $self->SUPER::add ($self->{buttonbar}, $self->{filter} || $self->{multiplexer}); |
|
|
3047 | |
|
|
3048 | $self |
|
|
3049 | } |
|
|
3050 | |
|
|
3051 | sub add { |
|
|
3052 | my ($self, $title, $widget, $tooltip) = @_; |
|
|
3053 | |
|
|
3054 | Scalar::Util::weaken $self; |
|
|
3055 | |
|
|
3056 | $self->{buttonbar}->add (new CFClient::UI::Button |
|
|
3057 | markup => $title, |
|
|
3058 | tooltip => $tooltip, |
|
|
3059 | on_activate => sub { $self->set_current_page ($widget) }, |
|
|
3060 | ); |
|
|
3061 | |
|
|
3062 | $self->{multiplexer}->add ($widget); |
|
|
3063 | } |
|
|
3064 | |
|
|
3065 | sub set_current_page { |
|
|
3066 | my ($self, $page) = @_; |
|
|
3067 | |
|
|
3068 | $self->{multiplexer}->set_current_page ($page); |
|
|
3069 | $self->_emit (page_changed => $self->{multiplexer}{current}); |
2879 | } |
3070 | } |
2880 | |
3071 | |
2881 | ############################################################################# |
3072 | ############################################################################# |
2882 | |
3073 | |
2883 | package CFClient::UI::Statusbox; |
3074 | package CFClient::UI::Statusbox; |
… | |
… | |
2988 | $self->SUPER::reconfigure; |
3179 | $self->SUPER::reconfigure; |
2989 | } |
3180 | } |
2990 | |
3181 | |
2991 | ############################################################################# |
3182 | ############################################################################# |
2992 | |
3183 | |
|
|
3184 | package CFClient::UI::Inventory; |
|
|
3185 | |
|
|
3186 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
3187 | |
|
|
3188 | sub new { |
|
|
3189 | my $class = shift; |
|
|
3190 | |
|
|
3191 | my $self = $class->SUPER::new ( |
|
|
3192 | child => (new CFClient::UI::Table col_expand => [0, 1, 0]), |
|
|
3193 | @_, |
|
|
3194 | ); |
|
|
3195 | |
|
|
3196 | $self |
|
|
3197 | } |
|
|
3198 | |
|
|
3199 | sub set_items { |
|
|
3200 | my ($self, $items) = @_; |
|
|
3201 | |
|
|
3202 | $self->{child}->clear; |
|
|
3203 | return unless $items; |
|
|
3204 | |
|
|
3205 | my @items = sort { |
|
|
3206 | ($a->{type} <=> $b->{type}) |
|
|
3207 | or ($a->{name} cmp $b->{name}) |
|
|
3208 | } @$items; |
|
|
3209 | |
|
|
3210 | $self->{real_items} = \@items; |
|
|
3211 | |
|
|
3212 | my $row = 0; |
|
|
3213 | for my $item (@items) { |
|
|
3214 | CFClient::Item::update_widgets $item; |
|
|
3215 | |
|
|
3216 | $self->{child}->add (0, $row, $item->{face_widget}); |
|
|
3217 | $self->{child}->add (1, $row, $item->{desc_widget}); |
|
|
3218 | $self->{child}->add (2, $row, $item->{weight_widget}); |
|
|
3219 | |
|
|
3220 | $row++; |
|
|
3221 | } |
|
|
3222 | } |
|
|
3223 | |
|
|
3224 | ############################################################################# |
|
|
3225 | |
|
|
3226 | package CFClient::UI::BindEditor; |
|
|
3227 | |
|
|
3228 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
3229 | |
|
|
3230 | sub new { |
|
|
3231 | my $class = shift; |
|
|
3232 | |
|
|
3233 | my $self = $class->SUPER::new (binding => [], commands => [], @_); |
|
|
3234 | |
|
|
3235 | $self->add (my $vb = new CFClient::UI::VBox); |
|
|
3236 | |
|
|
3237 | |
|
|
3238 | $vb->add ($self->{rec_btn} = new CFClient::UI::Button |
|
|
3239 | text => "start recording", |
|
|
3240 | tooltip => "Start/Stops recording of actions." |
|
|
3241 | ."All subsequent actions after the recording started will be captured." |
|
|
3242 | ."The actions are displayed after the record was stopped." |
|
|
3243 | ."To bind the action you have to click on the 'Bind' button", |
|
|
3244 | on_activate => sub { |
|
|
3245 | unless ($self->{recording}) { |
|
|
3246 | $self->start; |
|
|
3247 | } else { |
|
|
3248 | $self->stop; |
|
|
3249 | } |
|
|
3250 | }); |
|
|
3251 | |
|
|
3252 | $vb->add (new CFClient::UI::Label text => "Actions:"); |
|
|
3253 | $vb->add ($self->{cmdbox} = new CFClient::UI::VBox); |
|
|
3254 | |
|
|
3255 | $vb->add (new CFClient::UI::Label text => "Bound to: "); |
|
|
3256 | $vb->add (my $hb = new CFClient::UI::HBox); |
|
|
3257 | $hb->add ($self->{keylbl} = new CFClient::UI::Label expand => 1); |
|
|
3258 | $hb->add (new CFClient::UI::Button |
|
|
3259 | text => "bind", |
|
|
3260 | tooltip => "This opens a query where you have to press the key combination to bind the recorded actions", |
|
|
3261 | on_activate => sub { |
|
|
3262 | $self->ask_for_bind; |
|
|
3263 | }); |
|
|
3264 | |
|
|
3265 | $vb->add (my $hb = new CFClient::UI::HBox); |
|
|
3266 | $hb->add (new CFClient::UI::Button |
|
|
3267 | text => "ok", |
|
|
3268 | expand => 1, |
|
|
3269 | tooltip => "This closes the binding editor and saves the binding", |
|
|
3270 | on_activate => sub { |
|
|
3271 | $self->hide; |
|
|
3272 | $self->commit; |
|
|
3273 | }); |
|
|
3274 | |
|
|
3275 | $hb->add (new CFClient::UI::Button |
|
|
3276 | text => "cancel", |
|
|
3277 | expand => 1, |
|
|
3278 | tooltip => "This closes the binding editor without saving", |
|
|
3279 | on_activate => sub { |
|
|
3280 | $self->hide; |
|
|
3281 | $self->{binding_cancel}->() |
|
|
3282 | if $self->{binding_cancel}; |
|
|
3283 | }); |
|
|
3284 | |
|
|
3285 | $self->update_binding_widgets; |
|
|
3286 | |
|
|
3287 | $self |
|
|
3288 | } |
|
|
3289 | |
|
|
3290 | sub commit { |
|
|
3291 | my ($self) = @_; |
|
|
3292 | my ($mod, $sym, $cmds) = $self->get_binding; |
|
|
3293 | if ($sym != 0 && @$cmds > 0) { |
|
|
3294 | $::STATUSBOX->add ("Bound actions to '".CFClient::Binder::keycombo_to_name ($mod, $sym) |
|
|
3295 | ."'. Don't forget 'Save Config'!"); |
|
|
3296 | $self->{binding_change}->($mod, $sym, $cmds) |
|
|
3297 | if $self->{binding_change}; |
|
|
3298 | } else { |
|
|
3299 | $::STATUSBOX->add ("No action bound, no key or action specified!"); |
|
|
3300 | $self->{binding_cancel}->() |
|
|
3301 | if $self->{binding_cancel}; |
|
|
3302 | } |
|
|
3303 | } |
|
|
3304 | |
|
|
3305 | sub start { |
|
|
3306 | my ($self) = @_; |
|
|
3307 | |
|
|
3308 | $self->{rec_btn}->set_text ("stop recording"); |
|
|
3309 | $self->{recording} = 1; |
|
|
3310 | $self->clear_command_list; |
|
|
3311 | $::CONN->start_record if $::CONN; |
|
|
3312 | } |
|
|
3313 | |
|
|
3314 | sub stop { |
|
|
3315 | my ($self) = @_; |
|
|
3316 | |
|
|
3317 | $self->{rec_btn}->set_text ("start recording"); |
|
|
3318 | $self->{recording} = 0; |
|
|
3319 | |
|
|
3320 | my $rec; |
|
|
3321 | $rec = $::CONN->stop_record if $::CONN; |
|
|
3322 | return unless ref $rec eq 'ARRAY'; |
|
|
3323 | $self->set_command_list ($rec); |
|
|
3324 | } |
|
|
3325 | |
|
|
3326 | |
|
|
3327 | sub ask_for_bind_and_commit { |
|
|
3328 | my ($self) = @_; |
|
|
3329 | $self->ask_for_bind (1); |
|
|
3330 | } |
|
|
3331 | |
|
|
3332 | sub ask_for_bind { |
|
|
3333 | my ($self, $commit) = @_; |
|
|
3334 | |
|
|
3335 | CFClient::Binder::open_binding_dialog (sub { |
|
|
3336 | my ($mod, $sym) = @_; |
|
|
3337 | $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak? |
|
|
3338 | $self->update_binding_widgets; |
|
|
3339 | $self->commit if $commit; |
|
|
3340 | }); |
|
|
3341 | } |
|
|
3342 | |
|
|
3343 | # $mod and $sym are the modifiers and key symbol |
|
|
3344 | # $cmds is a array ref of strings (the commands) |
|
|
3345 | # $cb is the callback that is executed on OK |
|
|
3346 | # $ccb is the callback that is executed on CANCEL and |
|
|
3347 | # when the binding was unsuccessful on OK |
|
|
3348 | sub set_binding { |
|
|
3349 | my ($self, $mod, $sym, $cmds, $cb, $ccb) = @_; |
|
|
3350 | |
|
|
3351 | $self->clear_command_list; |
|
|
3352 | $self->{recording} = 0; |
|
|
3353 | $self->{rec_btn}->set_text ("start recording"); |
|
|
3354 | |
|
|
3355 | $self->{binding} = [$mod, $sym]; |
|
|
3356 | $self->{commands} = $cmds; |
|
|
3357 | |
|
|
3358 | $self->{binding_change} = $cb; |
|
|
3359 | $self->{binding_cancel} = $ccb; |
|
|
3360 | |
|
|
3361 | $self->update_binding_widgets; |
|
|
3362 | } |
|
|
3363 | |
|
|
3364 | # this is a shortcut method that asks for a binding |
|
|
3365 | # and then just binds it. |
|
|
3366 | sub do_quick_binding { |
|
|
3367 | my ($self, $cmds) = @_; |
|
|
3368 | $self->set_binding (undef, undef, $cmds, sub { |
|
|
3369 | $::CFG->{bindings}->{$_[0]}->{$_[1]} = $_[2]; |
|
|
3370 | }); |
|
|
3371 | $self->ask_for_bind (1); |
|
|
3372 | } |
|
|
3373 | |
|
|
3374 | sub update_binding_widgets { |
|
|
3375 | my ($self) = @_; |
|
|
3376 | my ($mod, $sym, $cmds) = $self->get_binding; |
|
|
3377 | $self->{keylbl}->set_text (CFClient::Binder::keycombo_to_name ($mod, $sym)); |
|
|
3378 | $self->set_command_list ($cmds); |
|
|
3379 | } |
|
|
3380 | |
|
|
3381 | sub get_binding { |
|
|
3382 | my ($self) = @_; |
|
|
3383 | return ( |
|
|
3384 | $self->{binding}->[0], |
|
|
3385 | $self->{binding}->[1], |
|
|
3386 | [ grep { defined $_ } @{$self->{commands}} ] |
|
|
3387 | ); |
|
|
3388 | } |
|
|
3389 | |
|
|
3390 | sub clear_command_list { |
|
|
3391 | my ($self) = @_; |
|
|
3392 | $self->{cmdbox}->clear (); |
|
|
3393 | } |
|
|
3394 | |
|
|
3395 | sub set_command_list { |
|
|
3396 | my ($self, $cmds) = @_; |
|
|
3397 | |
|
|
3398 | $self->{cmdbox}->clear (); |
|
|
3399 | $self->{commands} = $cmds; |
|
|
3400 | |
|
|
3401 | my $idx = 0; |
|
|
3402 | |
|
|
3403 | for (@$cmds) { |
|
|
3404 | $self->{cmdbox}->add (my $hb = new CFClient::UI::HBox); |
|
|
3405 | |
|
|
3406 | my $i = $idx; |
|
|
3407 | $hb->add (new CFClient::UI::Label text => $_); |
|
|
3408 | $hb->add (new CFClient::UI::Button |
|
|
3409 | text => "delete", |
|
|
3410 | tooltip => "Deletes the action from the record", |
|
|
3411 | on_activate => sub { |
|
|
3412 | $self->{cmdbox}->remove ($hb); |
|
|
3413 | $cmds->[$i] = undef; |
|
|
3414 | }); |
|
|
3415 | |
|
|
3416 | |
|
|
3417 | $idx++ |
|
|
3418 | } |
|
|
3419 | } |
|
|
3420 | |
|
|
3421 | ############################################################################# |
|
|
3422 | |
|
|
3423 | package CFClient::UI::SpellList; |
|
|
3424 | |
|
|
3425 | our @ISA = CFClient::UI::Table::; |
|
|
3426 | |
|
|
3427 | sub new { |
|
|
3428 | my $class = shift; |
|
|
3429 | |
|
|
3430 | my $self = $class->SUPER::new ( |
|
|
3431 | binding => [], |
|
|
3432 | commands => [], |
|
|
3433 | @_, |
|
|
3434 | ) |
|
|
3435 | } |
|
|
3436 | |
|
|
3437 | # XXX: Do sorting? Argl... |
|
|
3438 | sub add_spell { |
|
|
3439 | my ($self, $spell) = @_; |
|
|
3440 | $self->{spells}->{$spell->{name}} = $spell; |
|
|
3441 | |
|
|
3442 | $self->add (0, $self->{tbl_idx}, new CFClient::UI::Face |
|
|
3443 | face => $spell->{face}, |
|
|
3444 | can_hover => 1, |
|
|
3445 | can_events => 1, |
|
|
3446 | tooltip => $spell->{message}); |
|
|
3447 | |
|
|
3448 | $self->add (1, $self->{tbl_idx}, new CFClient::UI::Label |
|
|
3449 | text => $spell->{name}, |
|
|
3450 | can_hover => 1, |
|
|
3451 | can_events => 1, |
|
|
3452 | tooltip => $spell->{message}, |
|
|
3453 | expand => 1); |
|
|
3454 | |
|
|
3455 | $self->add (2, $self->{tbl_idx}, new CFClient::UI::Label |
|
|
3456 | text => (sprintf "lvl: %2d sp: %2d dmg: %2d", |
|
|
3457 | $spell->{level}, ($spell->{mana} || $spell->{grace}), $spell->{damage}), |
|
|
3458 | expand => 1); |
|
|
3459 | |
|
|
3460 | $self->add (3, $self->{tbl_idx}++, new CFClient::UI::Button |
|
|
3461 | text => "bind to key", |
|
|
3462 | on_activate => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }); |
|
|
3463 | } |
|
|
3464 | |
|
|
3465 | sub rebuild_spell_list { |
|
|
3466 | my ($self) = @_; |
|
|
3467 | $self->{tbl_idx} = 0; |
|
|
3468 | $self->add_spell ($_) for values %{$self->{spells}}; |
|
|
3469 | } |
|
|
3470 | |
|
|
3471 | sub remove_spell { |
|
|
3472 | my ($self, $spell) = @_; |
|
|
3473 | delete $self->{spells}->{$spell->{name}}; |
|
|
3474 | $self->rebuild_spell_list; |
|
|
3475 | } |
|
|
3476 | |
|
|
3477 | ############################################################################# |
|
|
3478 | |
2993 | package CFClient::UI::Root; |
3479 | package CFClient::UI::Root; |
2994 | |
3480 | |
2995 | our @ISA = CFClient::UI::Container::; |
3481 | our @ISA = CFClient::UI::Container::; |
2996 | |
3482 | |
2997 | use CFClient::OpenGL; |
3483 | use CFClient::OpenGL; |
… | |
… | |
3007 | Scalar::Util::weaken ($self->{root} = $self); |
3493 | Scalar::Util::weaken ($self->{root} = $self); |
3008 | |
3494 | |
3009 | $self |
3495 | $self |
3010 | } |
3496 | } |
3011 | |
3497 | |
3012 | sub configure { |
|
|
3013 | my ($self, $x, $y, $w, $h) = @_; |
|
|
3014 | |
|
|
3015 | $self->{w} = $w; |
|
|
3016 | $self->{h} = $h; |
|
|
3017 | } |
|
|
3018 | |
|
|
3019 | sub reconfigure { |
|
|
3020 | my ($self) = @_; |
|
|
3021 | |
|
|
3022 | $self->SUPER::reconfigure; |
|
|
3023 | |
|
|
3024 | $self->size_allocate ($self->{w}, $self->{h}, 1) |
|
|
3025 | if $self->{w}; |
|
|
3026 | } |
|
|
3027 | |
|
|
3028 | sub size_request { |
3498 | sub size_request { |
3029 | my ($self) = @_; |
3499 | my ($self) = @_; |
3030 | |
3500 | |
3031 | ($self->{w}, $self->{h}) |
3501 | ($self->{w}, $self->{h}) |
3032 | } |
3502 | } |
3033 | |
3503 | |
|
|
3504 | sub _to_pixel { |
|
|
3505 | my ($coord, $size, $max) = @_; |
|
|
3506 | |
|
|
3507 | $coord = |
|
|
3508 | $coord eq "center" ? ($max - $size) * 0.5 |
|
|
3509 | : $coord eq "max" ? $max |
|
|
3510 | : $coord; |
|
|
3511 | |
|
|
3512 | $coord = 0 if $coord < 0; |
|
|
3513 | $coord = $max - $size if $coord > $max - $size; |
|
|
3514 | |
|
|
3515 | int $coord + 0.5 |
|
|
3516 | } |
|
|
3517 | |
3034 | sub size_allocate { |
3518 | sub size_allocate { |
3035 | my ($self, $w, $h, $changed) = @_; |
3519 | my ($self, $w, $h) = @_; |
3036 | |
3520 | |
3037 | for my $child ($self->children) { |
3521 | for my $child ($self->children) { |
3038 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
3522 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
3039 | |
3523 | |
3040 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
3524 | $X = $child->{force_x} if exists $child->{force_x}; |
3041 | if exists $child->{req_x}; |
3525 | $Y = $child->{force_y} if exists $child->{force_y}; |
3042 | |
3526 | |
3043 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3527 | $X = _to_pixel $X, $W, $self->{w}; |
3044 | if exists $child->{req_y}; |
3528 | $Y = _to_pixel $Y, $H, $self->{h}; |
3045 | |
|
|
3046 | #delete @$child{qw(req_x req_y)};#d# def_x, def_y |
|
|
3047 | |
|
|
3048 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
|
|
3049 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
|
|
3050 | |
3529 | |
3051 | $child->configure ($X, $Y, $W, $H); |
3530 | $child->configure ($X, $Y, $W, $H); |
3052 | } |
3531 | } |
3053 | } |
3532 | } |
3054 | |
3533 | |
… | |
… | |
3071 | } |
3550 | } |
3072 | |
3551 | |
3073 | sub add { |
3552 | sub add { |
3074 | my ($self, @children) = @_; |
3553 | my ($self, @children) = @_; |
3075 | |
3554 | |
3076 | for my $child (@children) { |
|
|
3077 | $child->{is_toplevel} = 1; |
3555 | $_->{is_toplevel} = 1 |
3078 | |
3556 | for @children; |
3079 | # integerise window positions |
|
|
3080 | $child->{x} = int $child->{x}; |
|
|
3081 | $child->{y} = int $child->{y}; |
|
|
3082 | } |
|
|
3083 | |
3557 | |
3084 | $self->SUPER::add (@children); |
3558 | $self->SUPER::add (@children); |
3085 | } |
3559 | } |
3086 | |
3560 | |
3087 | sub remove { |
3561 | sub remove { |
3088 | my ($self, @children) = @_; |
3562 | my ($self, @children) = @_; |
3089 | |
3563 | |
3090 | $self->SUPER::remove (@children); |
3564 | $self->SUPER::remove (@children); |
|
|
3565 | |
|
|
3566 | delete $self->{is_toplevel} |
|
|
3567 | for @children; |
3091 | |
3568 | |
3092 | while (@children) { |
3569 | while (@children) { |
3093 | my $w = pop @children; |
3570 | my $w = pop @children; |
3094 | push @children, $w->children; |
3571 | push @children, $w->children; |
3095 | $w->set_invisible; |
3572 | $w->set_invisible; |
… | |
… | |
3115 | $_->() |
3592 | $_->() |
3116 | for values %{delete $self->{refresh_hook}}; |
3593 | for values %{delete $self->{refresh_hook}}; |
3117 | } |
3594 | } |
3118 | |
3595 | |
3119 | if ($self->{realloc}) { |
3596 | if ($self->{realloc}) { |
|
|
3597 | my %queue; |
3120 | my @queue; |
3598 | my @queue; |
|
|
3599 | my $widget; |
3121 | |
3600 | |
|
|
3601 | outer: |
3122 | while () { |
3602 | while () { |
3123 | if ($self->{realloc}) { |
3603 | if (my $realloc = delete $self->{realloc}) { |
3124 | #TODO use array-of-depth approach |
3604 | for $widget (values %$realloc) { |
|
|
3605 | $widget->{visible} or next; # do not resize invisible widgets |
3125 | |
3606 | |
3126 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3607 | $queue{$widget+0}++ and next; # duplicates are common |
3127 | @queue, values %{delete $self->{realloc}}; |
3608 | |
|
|
3609 | push @{ $queue[$widget->{visible}] }, $widget; |
|
|
3610 | } |
3128 | } |
3611 | } |
3129 | |
3612 | |
|
|
3613 | while () { |
|
|
3614 | @queue or last outer; |
|
|
3615 | |
|
|
3616 | $widget = pop @{ $queue[-1] || [] } |
|
|
3617 | and last; |
|
|
3618 | |
|
|
3619 | pop @queue; |
|
|
3620 | } |
|
|
3621 | |
|
|
3622 | delete $queue{$widget+0}; |
|
|
3623 | |
|
|
3624 | my ($w, $h) = $widget->size_request; |
|
|
3625 | |
|
|
3626 | $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2; |
|
|
3627 | $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2; |
|
|
3628 | |
|
|
3629 | $w = $widget->{force_w} if exists $widget->{force_w}; |
|
|
3630 | $h = $widget->{force_h} if exists $widget->{force_h}; |
|
|
3631 | |
|
|
3632 | if ($widget->{req_w} != $w || $widget->{req_h} != $h |
|
|
3633 | || delete $widget->{force_realloc}) { |
|
|
3634 | $widget->{req_w} = $w; |
|
|
3635 | $widget->{req_h} = $h; |
|
|
3636 | |
|
|
3637 | $self->{size_alloc}{$widget+0} = $widget; |
|
|
3638 | |
|
|
3639 | if (my $parent = $widget->{parent}) { |
|
|
3640 | $self->{realloc}{$parent+0} = $parent |
|
|
3641 | unless $queue{$parent+0}; |
|
|
3642 | |
|
|
3643 | $parent->{force_size_alloc} = 1; |
|
|
3644 | $self->{size_alloc}{$parent+0} = $parent; |
|
|
3645 | } |
|
|
3646 | } |
|
|
3647 | |
|
|
3648 | delete $self->{realloc}{$widget+0}; |
|
|
3649 | } |
|
|
3650 | } |
|
|
3651 | |
|
|
3652 | while (my $size_alloc = delete $self->{size_alloc}) { |
|
|
3653 | my @queue = sort { $b->{visible} <=> $a->{visible} } |
|
|
3654 | values %$size_alloc; |
|
|
3655 | |
|
|
3656 | while () { |
3130 | my $widget = pop @queue || last; |
3657 | my $widget = pop @queue || last; |
3131 | |
3658 | |
3132 | $widget->{visible} or last; # do not resize invisible widgets |
3659 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
3133 | |
|
|
3134 | my ($w, $h) = $widget->{def_w} && $widget->{def_h} |
|
|
3135 | ? @$widget{qw(def_w def_h)} |
|
|
3136 | : $widget->size_request; |
|
|
3137 | |
|
|
3138 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
|
|
3139 | |
|
|
3140 | $widget->{req_w} = $w; |
|
|
3141 | $widget->{req_h} = $h; |
|
|
3142 | |
|
|
3143 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
|
|
3144 | |
|
|
3145 | push @queue, $widget->{parent} |
|
|
3146 | if $widget->{parent}; |
|
|
3147 | } |
|
|
3148 | } |
|
|
3149 | |
|
|
3150 | while (my $size_alloc = delete $self->{size_alloc}) { |
|
|
3151 | my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, |
|
|
3152 | values %$size_alloc; |
|
|
3153 | |
|
|
3154 | while () { |
|
|
3155 | my ($widget, $w, $h) = @{ pop @queue or last }; |
|
|
3156 | |
3660 | |
3157 | $w = 0 if $w < 0; |
3661 | $w = 0 if $w < 0; |
3158 | $h = 0 if $h < 0; |
3662 | $h = 0 if $h < 0; |
3159 | |
3663 | |
3160 | my $changed = $widget->{w} != $w || $widget->{h} != $h; |
3664 | $w = int $w + 0.5; |
|
|
3665 | $h = int $h + 0.5; |
3161 | |
3666 | |
|
|
3667 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
|
|
3668 | $widget->{old_w} = $widget->{w}; |
|
|
3669 | $widget->{old_h} = $widget->{h}; |
|
|
3670 | |
3162 | $widget->{w} = $w; |
3671 | $widget->{w} = $w; |
3163 | $widget->{h} = $h; |
3672 | $widget->{h} = $h; |
3164 | |
3673 | |
3165 | $widget->emit (size_allocate => $w, $h, $changed); |
3674 | $widget->emit (size_allocate => $w, $h); |
|
|
3675 | } |
3166 | } |
3676 | } |
3167 | } |
3677 | } |
3168 | |
3678 | |
3169 | while ($self->{post_alloc_hook}) { |
3679 | while ($self->{post_alloc_hook}) { |
3170 | $_->() |
3680 | $_->() |
3171 | for values %{delete $self->{post_alloc_hook}}; |
3681 | for values %{delete $self->{post_alloc_hook}}; |
3172 | } |
3682 | } |
|
|
3683 | |
3173 | |
3684 | |
3174 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
3685 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
3175 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
3686 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
3176 | glClear GL_COLOR_BUFFER_BIT; |
3687 | glClear GL_COLOR_BUFFER_BIT; |
3177 | |
3688 | |
… | |
… | |
3179 | glLoadIdentity; |
3690 | glLoadIdentity; |
3180 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
3691 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
3181 | glMatrixMode GL_MODELVIEW; |
3692 | glMatrixMode GL_MODELVIEW; |
3182 | glLoadIdentity; |
3693 | glLoadIdentity; |
3183 | |
3694 | |
|
|
3695 | { |
|
|
3696 | package CFClient::UI::Base; |
|
|
3697 | |
|
|
3698 | ($draw_x, $draw_y, $draw_w, $draw_h) = |
|
|
3699 | (0, 0, $self->{w}, $self->{h}); |
|
|
3700 | } |
|
|
3701 | |
3184 | $self->_draw; |
3702 | $self->_draw; |
3185 | } |
3703 | } |
3186 | |
3704 | |
3187 | ############################################################################# |
3705 | ############################################################################# |
3188 | |
3706 | |