1 | package CFClient::UI; |
1 | package CFPlus::UI; |
2 | |
2 | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Scalar::Util (); |
6 | use Scalar::Util (); |
7 | use List::Util (); |
7 | use List::Util (); |
8 | use Event; |
8 | use Event; |
9 | |
9 | |
10 | use CFClient; |
10 | use CFPlus; |
|
|
11 | use CFPlus::Pod; |
11 | use CFClient::Texture; |
12 | use CFPlus::Texture; |
12 | |
13 | |
13 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
14 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
14 | |
15 | |
15 | our $LAYOUT; |
16 | our $LAYOUT; |
16 | our $ROOT; |
17 | our $ROOT; |
… | |
… | |
79 | sub feed_sdl_key_up_event { |
80 | sub feed_sdl_key_up_event { |
80 | $FOCUS->emit (key_up => $_[0]) |
81 | $FOCUS->emit (key_up => $_[0]) |
81 | if $FOCUS; |
82 | if $FOCUS; |
82 | } |
83 | } |
83 | |
84 | |
|
|
85 | sub check_hover { |
|
|
86 | my ($widget) = @_; |
|
|
87 | |
|
|
88 | if ($widget != $HOVER) { |
|
|
89 | my $hover = $HOVER; $HOVER = $widget; |
|
|
90 | |
|
|
91 | $hover->update if $hover && $hover->{can_hover}; |
|
|
92 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
|
|
93 | |
|
|
94 | $TOOLTIP_WATCHER->start; |
|
|
95 | } |
|
|
96 | } |
|
|
97 | |
84 | sub feed_sdl_button_down_event { |
98 | sub feed_sdl_button_down_event { |
85 | my ($ev) = @_; |
99 | my ($ev) = @_; |
86 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
100 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
87 | |
101 | |
88 | warn "button down $ev->{button}\n";#d# |
102 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
89 | |
103 | |
90 | if (!$BUTTON_STATE) { |
104 | unless ($GRAB) { |
91 | my $widget = $ROOT->find_widget ($x, $y); |
105 | my $widget = $ROOT->find_widget ($x, $y); |
92 | |
106 | |
93 | $GRAB = $widget; |
107 | $GRAB = $widget; |
94 | $GRAB->update if $GRAB; |
108 | $GRAB->update if $GRAB; |
95 | |
109 | |
96 | $TOOLTIP_WATCHER->cb->(); |
110 | $TOOLTIP_WATCHER->cb->(); |
97 | } |
111 | } |
98 | |
112 | |
99 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
113 | if ($GRAB) { |
100 | |
114 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
101 | $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) |
115 | # mousewheel |
102 | if $GRAB; |
116 | $ev->{dx} = 0; |
|
|
117 | $ev->{dy} = $ev->{button} * 2 - 9; |
|
|
118 | $GRAB->emit (mouse_wheel => $ev); |
|
|
119 | } else { |
|
|
120 | $GRAB->emit (button_down => $ev) |
|
|
121 | } |
|
|
122 | } |
103 | } |
123 | } |
104 | |
124 | |
105 | sub feed_sdl_button_up_event { |
125 | sub feed_sdl_button_up_event { |
106 | my ($ev) = @_; |
126 | my ($ev) = @_; |
107 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
|
|
108 | |
127 | |
109 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
128 | my $widget = $GRAB || $ROOT->find_widget ($ev->{x}, $ev->{y}); |
110 | |
129 | |
111 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
130 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
112 | |
131 | |
113 | $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) |
132 | $GRAB->emit (button_up => $ev) |
114 | if $GRAB; |
133 | if $GRAB && $ev->{button} != 4 && $ev->{button} != 5; |
115 | |
134 | |
116 | if (!$BUTTON_STATE) { |
135 | unless ($BUTTON_STATE) { |
117 | my $grab = $GRAB; undef $GRAB; |
136 | my $grab = $GRAB; undef $GRAB; |
118 | $grab->update if $grab; |
137 | $grab->update if $grab; |
119 | $GRAB->update if $GRAB; |
138 | $GRAB->update if $GRAB; |
120 | |
139 | |
|
|
140 | check_hover $widget; |
121 | $TOOLTIP_WATCHER->cb->(); |
141 | $TOOLTIP_WATCHER->cb->(); |
122 | } |
142 | } |
123 | } |
143 | } |
124 | |
144 | |
125 | sub feed_sdl_motion_event { |
145 | sub feed_sdl_motion_event { |
126 | my ($ev) = @_; |
146 | my ($ev) = @_; |
127 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
147 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
128 | |
148 | |
129 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
149 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
130 | |
150 | |
131 | if ($widget != $HOVER) { |
151 | check_hover $widget; |
132 | my $hover = $HOVER; $HOVER = $widget; |
|
|
133 | |
152 | |
134 | $hover->update if $hover && $hover->{can_hover}; |
153 | $HOVER->emit (mouse_motion => $ev) |
135 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
|
|
136 | |
|
|
137 | $TOOLTIP_WATCHER->start; |
|
|
138 | } |
|
|
139 | |
|
|
140 | $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) |
|
|
141 | if $HOVER; |
154 | if $HOVER; |
142 | } |
155 | } |
143 | |
156 | |
144 | # convert position array to integers |
157 | # convert position array to integers |
145 | sub harmonize { |
158 | sub harmonize { |
… | |
… | |
195 | reconfigure_widgets; |
208 | reconfigure_widgets; |
196 | } |
209 | } |
197 | |
210 | |
198 | ############################################################################# |
211 | ############################################################################# |
199 | |
212 | |
|
|
213 | package CFPlus::UI::Event; |
|
|
214 | |
|
|
215 | sub xy { |
|
|
216 | $_[1]->coord2local ($_[0]{x}, $_[0]{y}) |
|
|
217 | } |
|
|
218 | |
|
|
219 | ############################################################################# |
|
|
220 | |
200 | package CFClient::UI::Base; |
221 | package CFPlus::UI::Base; |
201 | |
222 | |
202 | use strict; |
223 | use strict; |
203 | |
224 | |
204 | use CFClient::OpenGL; |
225 | use CFPlus::OpenGL; |
205 | |
226 | |
206 | sub new { |
227 | sub new { |
207 | my $class = shift; |
228 | my $class = shift; |
208 | |
229 | |
209 | my $self = bless { |
230 | my $self = bless { |
… | |
… | |
214 | h => undef, |
235 | h => undef, |
215 | can_events => 1, |
236 | can_events => 1, |
216 | @_ |
237 | @_ |
217 | }, $class; |
238 | }, $class; |
218 | |
239 | |
219 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
240 | Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
220 | |
241 | |
221 | for (keys %$self) { |
242 | for (keys %$self) { |
222 | if (/^on_(.*)$/) { |
243 | if (/^on_(.*)$/) { |
223 | $self->connect ($1 => delete $self->{$_}); |
244 | $self->connect ($1 => delete $self->{$_}); |
224 | } |
245 | } |
225 | } |
246 | } |
226 | |
247 | |
227 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
248 | if (my $layout = $CFPlus::UI::LAYOUT->{$self->{name}}) { |
228 | $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{x}; |
249 | $self->{x} = $layout->{x} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{x}; |
229 | $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{y}; |
250 | $self->{y} = $layout->{y} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{y}; |
230 | $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{alloc_w} if exists $layout->{w}; |
251 | $self->{force_w} = $layout->{w} * $CFPlus::UI::ROOT->{alloc_w} if exists $layout->{w}; |
231 | $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{alloc_h} if exists $layout->{h}; |
252 | $self->{force_h} = $layout->{h} * $CFPlus::UI::ROOT->{alloc_h} if exists $layout->{h}; |
232 | |
253 | |
233 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
254 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
234 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
255 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
235 | |
256 | |
236 | $self->show if $layout->{show}; |
257 | $self->show if $layout->{show}; |
… | |
… | |
241 | |
262 | |
242 | sub destroy { |
263 | sub destroy { |
243 | my ($self) = @_; |
264 | my ($self) = @_; |
244 | |
265 | |
245 | $self->hide; |
266 | $self->hide; |
|
|
267 | $self->emit ("destroy"); |
246 | %$self = (); |
268 | %$self = (); |
247 | } |
269 | } |
248 | |
270 | |
249 | sub show { |
271 | sub show { |
250 | my ($self) = @_; |
272 | my ($self) = @_; |
251 | |
273 | |
252 | return if $self->{parent}; |
274 | return if $self->{parent}; |
253 | |
275 | |
254 | $CFClient::UI::ROOT->add ($self); |
276 | $CFPlus::UI::ROOT->add ($self); |
255 | } |
277 | } |
256 | |
278 | |
257 | sub set_visible { |
279 | sub set_visible { |
258 | my ($self) = @_; |
280 | my ($self) = @_; |
259 | |
281 | |
… | |
… | |
280 | delete $self->{root}; |
302 | delete $self->{root}; |
281 | |
303 | |
282 | undef $GRAB if $GRAB == $self; |
304 | undef $GRAB if $GRAB == $self; |
283 | undef $HOVER if $HOVER == $self; |
305 | undef $HOVER if $HOVER == $self; |
284 | |
306 | |
285 | $CFClient::UI::TOOLTIP_WATCHER->cb->() |
307 | $CFPlus::UI::TOOLTIP_WATCHER->cb->() |
286 | if $TOOLTIP->{owner} == $self; |
308 | if $TOOLTIP->{owner} == $self; |
287 | |
309 | |
288 | $self->emit ("focus_out"); |
310 | $self->emit ("focus_out"); |
289 | $self->emit (visibility_change => 0); |
311 | $self->emit (visibility_change => 0); |
290 | } |
312 | } |
… | |
… | |
347 | my ($self, $x, $y, $w, $h) = @_; |
369 | my ($self, $x, $y, $w, $h) = @_; |
348 | |
370 | |
349 | if ($self->{aspect}) { |
371 | if ($self->{aspect}) { |
350 | my ($ow, $oh) = ($w, $h); |
372 | my ($ow, $oh) = ($w, $h); |
351 | |
373 | |
352 | $w = List::Util::min $w, int $h * $self->{aspect}; |
374 | $w = List::Util::min $w, CFPlus::ceil $h * $self->{aspect}; |
353 | $h = List::Util::min $h, int $w / $self->{aspect}; |
375 | $h = List::Util::min $h, CFPlus::ceil $w / $self->{aspect}; |
354 | |
376 | |
355 | # use alignment to adjust x, y |
377 | # use alignment to adjust x, y |
356 | |
378 | |
357 | $x += int 0.5 * ($ow - $w); |
379 | $x += int 0.5 * ($ow - $w); |
358 | $y += int 0.5 * ($oh - $h); |
380 | $y += int 0.5 * ($oh - $h); |
… | |
… | |
399 | |
421 | |
400 | return if $self->{tooltip} eq $tooltip; |
422 | return if $self->{tooltip} eq $tooltip; |
401 | |
423 | |
402 | $self->{tooltip} = $tooltip; |
424 | $self->{tooltip} = $tooltip; |
403 | |
425 | |
404 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
426 | if ($CFPlus::UI::TOOLTIP->{owner} == $self) { |
405 | delete $CFClient::UI::TOOLTIP->{owner}; |
427 | delete $CFPlus::UI::TOOLTIP->{owner}; |
406 | $CFClient::UI::TOOLTIP_WATCHER->cb->(); |
428 | $CFPlus::UI::TOOLTIP_WATCHER->cb->(); |
407 | } |
429 | } |
408 | } |
430 | } |
409 | |
431 | |
410 | # translate global coordinates to local coordinate system |
432 | # translate global coordinates to local coordinate system |
411 | sub coord2local { |
433 | sub coord2local { |
412 | my ($self, $x, $y) = @_; |
434 | my ($self, $x, $y) = @_; |
413 | |
435 | |
|
|
436 | Carp::confess unless $self->{parent};#d# |
|
|
437 | |
414 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
438 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
415 | } |
439 | } |
416 | |
440 | |
417 | # translate local coordinates to global coordinate system |
441 | # translate local coordinates to global coordinate system |
418 | sub coord2global { |
442 | sub coord2global { |
419 | my ($self, $x, $y) = @_; |
443 | my ($self, $x, $y) = @_; |
|
|
444 | |
|
|
445 | Carp::confess unless $self->{parent};#d# |
420 | |
446 | |
421 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
447 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
422 | } |
448 | } |
423 | |
449 | |
424 | sub invoke_focus_in { |
450 | sub invoke_focus_in { |
… | |
… | |
458 | |
484 | |
459 | sub invoke_mouse_motion { 0 } |
485 | sub invoke_mouse_motion { 0 } |
460 | sub invoke_button_up { 0 } |
486 | sub invoke_button_up { 0 } |
461 | sub invoke_key_down { 0 } |
487 | sub invoke_key_down { 0 } |
462 | sub invoke_key_up { 0 } |
488 | sub invoke_key_up { 0 } |
|
|
489 | sub invoke_mouse_wheel { 0 } |
463 | |
490 | |
464 | sub invoke_button_down { |
491 | sub invoke_button_down { |
465 | my ($self, $ev, $x, $y) = @_; |
492 | my ($self, $ev, $x, $y) = @_; |
466 | |
493 | |
467 | $self->grab_focus; |
494 | $self->grab_focus; |
468 | |
495 | |
469 | warn "button down $ev->{button} $x $y\n";#d# |
|
|
470 | |
|
|
471 | 0 |
496 | 0 |
472 | } |
497 | } |
473 | |
498 | |
474 | sub connect { |
499 | sub connect { |
475 | my ($self, $signal, $cb) = @_; |
500 | my ($self, $signal, $cb) = @_; |
476 | |
501 | |
477 | push @{ $self->{signal_cb}{$signal} }, $cb; |
502 | push @{ $self->{signal_cb}{$signal} }, $cb; |
|
|
503 | |
|
|
504 | defined wantarray and CFPlus::guard { |
|
|
505 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
|
|
506 | @{ $self->{signal_cb}{$signal} }; |
|
|
507 | } |
478 | } |
508 | } |
|
|
509 | |
|
|
510 | sub disconnect_all { |
|
|
511 | my ($self, $signal) = @_; |
|
|
512 | |
|
|
513 | delete $self->{signal_cb}{$signal}; |
|
|
514 | } |
|
|
515 | |
|
|
516 | my %has_coords = ( |
|
|
517 | button_down => 1, |
|
|
518 | button_up => 1, |
|
|
519 | mouse_motion => 1, |
|
|
520 | mouse_wheel => 1, |
|
|
521 | ); |
479 | |
522 | |
480 | sub emit { |
523 | sub emit { |
481 | my ($self, $signal, @args) = @_; |
524 | my ($self, $signal, @args) = @_; |
482 | |
525 | |
|
|
526 | # I do not really like this solution, but I do not like duplication |
|
|
527 | # and needlessly verbose code, either. |
|
|
528 | my @append |
|
|
529 | = $has_coords{$signal} |
|
|
530 | ? $args[0]->xy ($self) |
|
|
531 | : (); |
|
|
532 | |
|
|
533 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
|
|
534 | |
483 | #d##TODO# stop propagating at first true, do not use sum |
535 | #d##TODO# stop propagating at first true, do not use sum |
484 | (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before |
536 | (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before |
485 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure |
537 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure |
486 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
538 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
487 | } |
539 | } |
488 | |
540 | |
489 | sub find_widget { |
541 | sub find_widget { |
490 | my ($self, $x, $y) = @_; |
542 | my ($self, $x, $y) = @_; |
491 | |
543 | |
… | |
… | |
575 | glVertex $self->{w} - 1, 0; |
627 | glVertex $self->{w} - 1, 0; |
576 | glVertex $self->{w} - 1, $self->{h} - 1; |
628 | glVertex $self->{w} - 1, $self->{h} - 1; |
577 | glVertex 0 , $self->{h} - 1; |
629 | glVertex 0 , $self->{h} - 1; |
578 | glEnd; |
630 | glEnd; |
579 | glPopMatrix; |
631 | glPopMatrix; |
580 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
632 | #CFPlus::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
581 | } |
633 | } |
582 | |
634 | |
583 | $self->_draw; |
635 | $self->_draw; |
584 | glPopMatrix; |
636 | glPopMatrix; |
585 | } |
637 | } |
… | |
… | |
591 | } |
643 | } |
592 | |
644 | |
593 | sub DESTROY { |
645 | sub DESTROY { |
594 | my ($self) = @_; |
646 | my ($self) = @_; |
595 | |
647 | |
596 | return if CFClient::in_destruct; |
648 | return if CFPlus::in_destruct; |
597 | |
|
|
598 | delete $WIDGET{$self+0}; |
|
|
599 | |
649 | |
600 | eval { $self->destroy }; |
650 | eval { $self->destroy }; |
601 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
651 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
|
|
652 | |
|
|
653 | delete $WIDGET{$self+0}; |
602 | } |
654 | } |
603 | |
655 | |
604 | ############################################################################# |
656 | ############################################################################# |
605 | |
657 | |
606 | package CFClient::UI::DrawBG; |
658 | package CFPlus::UI::DrawBG; |
607 | |
659 | |
608 | our @ISA = CFClient::UI::Base::; |
660 | our @ISA = CFPlus::UI::Base::; |
609 | |
661 | |
610 | use strict; |
662 | use strict; |
611 | use CFClient::OpenGL; |
663 | use CFPlus::OpenGL; |
612 | |
664 | |
613 | sub new { |
665 | sub new { |
614 | my $class = shift; |
666 | my $class = shift; |
615 | |
667 | |
616 | # range [value, low, high, page] |
668 | # range [value, low, high, page] |
… | |
… | |
647 | } |
699 | } |
648 | } |
700 | } |
649 | |
701 | |
650 | ############################################################################# |
702 | ############################################################################# |
651 | |
703 | |
652 | package CFClient::UI::Empty; |
704 | package CFPlus::UI::Empty; |
653 | |
705 | |
654 | our @ISA = CFClient::UI::Base::; |
706 | our @ISA = CFPlus::UI::Base::; |
655 | |
707 | |
656 | sub new { |
708 | sub new { |
657 | my ($class, %arg) = @_; |
709 | my ($class, %arg) = @_; |
658 | $class->SUPER::new (can_events => 0, %arg); |
710 | $class->SUPER::new (can_events => 0, %arg); |
659 | } |
711 | } |
… | |
… | |
666 | |
718 | |
667 | sub draw { } |
719 | sub draw { } |
668 | |
720 | |
669 | ############################################################################# |
721 | ############################################################################# |
670 | |
722 | |
671 | package CFClient::UI::Container; |
723 | package CFPlus::UI::Container; |
672 | |
724 | |
673 | our @ISA = CFClient::UI::Base::; |
725 | our @ISA = CFPlus::UI::Base::; |
674 | |
726 | |
675 | sub new { |
727 | sub new { |
676 | my ($class, %arg) = @_; |
728 | my ($class, %arg) = @_; |
677 | |
729 | |
678 | my $children = delete $arg{children}; |
730 | my $children = delete $arg{children}; |
… | |
… | |
687 | if $children; |
739 | if $children; |
688 | |
740 | |
689 | $self |
741 | $self |
690 | } |
742 | } |
691 | |
743 | |
|
|
744 | sub realloc { |
|
|
745 | my ($self) = @_; |
|
|
746 | |
|
|
747 | $self->{force_realloc} = 1; |
|
|
748 | $self->{force_size_alloc} = 1; |
|
|
749 | $self->SUPER::realloc; |
|
|
750 | } |
|
|
751 | |
692 | sub add { |
752 | sub add { |
693 | my ($self, @widgets) = @_; |
753 | my ($self, @widgets) = @_; |
694 | |
754 | |
695 | $_->set_parent ($self) |
755 | $_->set_parent ($self) |
696 | for @widgets; |
756 | for @widgets; |
… | |
… | |
756 | $_->draw for @{$self->{children}}; |
816 | $_->draw for @{$self->{children}}; |
757 | } |
817 | } |
758 | |
818 | |
759 | ############################################################################# |
819 | ############################################################################# |
760 | |
820 | |
761 | package CFClient::UI::Bin; |
821 | package CFPlus::UI::Bin; |
762 | |
822 | |
763 | our @ISA = CFClient::UI::Container::; |
823 | our @ISA = CFPlus::UI::Container::; |
764 | |
824 | |
765 | sub new { |
825 | sub new { |
766 | my ($class, %arg) = @_; |
826 | my ($class, %arg) = @_; |
767 | |
827 | |
768 | my $child = (delete $arg{child}) || new CFClient::UI::Empty::; |
828 | my $child = (delete $arg{child}) || new CFPlus::UI::Empty::; |
769 | |
829 | |
770 | $class->SUPER::new (children => [$child], %arg) |
830 | $class->SUPER::new (children => [$child], %arg) |
771 | } |
831 | } |
772 | |
832 | |
773 | sub add { |
833 | sub add { |
… | |
… | |
780 | sub remove { |
840 | sub remove { |
781 | my ($self, $widget) = @_; |
841 | my ($self, $widget) = @_; |
782 | |
842 | |
783 | $self->SUPER::remove ($widget); |
843 | $self->SUPER::remove ($widget); |
784 | |
844 | |
785 | $self->{children} = [new CFClient::UI::Empty] |
845 | $self->{children} = [new CFPlus::UI::Empty] |
786 | unless @{$self->{children}}; |
846 | unless @{$self->{children}}; |
787 | } |
847 | } |
788 | |
848 | |
789 | sub child { $_[0]->{children}[0] } |
849 | sub child { $_[0]->{children}[0] } |
790 | |
850 | |
… | |
… | |
802 | |
862 | |
803 | ############################################################################# |
863 | ############################################################################# |
804 | |
864 | |
805 | # back-buffered drawing area |
865 | # back-buffered drawing area |
806 | |
866 | |
807 | package CFClient::UI::Window; |
867 | package CFPlus::UI::Window; |
808 | |
868 | |
809 | our @ISA = CFClient::UI::Bin::; |
869 | our @ISA = CFPlus::UI::Bin::; |
810 | |
870 | |
811 | use CFClient::OpenGL; |
871 | use CFPlus::OpenGL; |
812 | |
872 | |
813 | sub new { |
873 | sub new { |
814 | my ($class, %arg) = @_; |
874 | my ($class, %arg) = @_; |
815 | |
875 | |
816 | my $self = $class->SUPER::new (%arg); |
876 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
838 | } |
898 | } |
839 | |
899 | |
840 | sub render_child { |
900 | sub render_child { |
841 | my ($self) = @_; |
901 | my ($self) = @_; |
842 | |
902 | |
843 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
903 | $self->{texture} = new_from_opengl CFPlus::Texture $self->{w}, $self->{h}, sub { |
844 | glClearColor 0, 0, 0, 0; |
904 | glClearColor 0, 0, 0, 0; |
845 | glClear GL_COLOR_BUFFER_BIT; |
905 | glClear GL_COLOR_BUFFER_BIT; |
846 | |
906 | |
847 | { |
907 | { |
848 | package CFClient::UI::Base; |
908 | package CFPlus::UI::Base; |
849 | |
909 | |
850 | ($draw_x, $draw_y, $draw_w, $draw_h) = |
910 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
851 | (0, 0, $self->{w}, $self->{h}); |
911 | (0, 0, $self->{w}, $self->{h}); |
|
|
912 | |
|
|
913 | $self->_render; |
852 | } |
914 | } |
853 | |
|
|
854 | $self->_render; |
|
|
855 | }; |
915 | }; |
856 | } |
916 | } |
857 | |
917 | |
858 | sub _draw { |
918 | sub _draw { |
859 | my ($self) = @_; |
919 | my ($self) = @_; |
860 | |
|
|
861 | my ($w, $h) = @$self{qw(w h)}; |
|
|
862 | |
920 | |
863 | my $tex = $self->{texture} |
921 | my $tex = $self->{texture} |
864 | or return; |
922 | or return; |
865 | |
923 | |
866 | glEnable GL_TEXTURE_2D; |
924 | glEnable GL_TEXTURE_2D; |
867 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
925 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
868 | glColor 0, 0, 0, 1; |
926 | glColor 0, 0, 0, 1; |
869 | |
927 | |
870 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
928 | $tex->draw_quad_alpha_premultiplied (0, 0); |
871 | |
929 | |
872 | glDisable GL_TEXTURE_2D; |
930 | glDisable GL_TEXTURE_2D; |
873 | } |
931 | } |
874 | |
932 | |
875 | ############################################################################# |
933 | ############################################################################# |
876 | |
934 | |
877 | package CFClient::UI::ViewPort; |
935 | package CFPlus::UI::ViewPort; |
878 | |
936 | |
879 | our @ISA = CFClient::UI::Window::; |
937 | our @ISA = CFPlus::UI::Window::; |
880 | |
938 | |
881 | sub new { |
939 | sub new { |
882 | my $class = shift; |
940 | my $class = shift; |
883 | |
941 | |
884 | $class->SUPER::new ( |
942 | $class->SUPER::new ( |
… | |
… | |
944 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
1002 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
945 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
1003 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
946 | ) { |
1004 | ) { |
947 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
1005 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
948 | } else { |
1006 | } else { |
949 | $self->CFClient::UI::Base::find_widget ($x, $y) |
1007 | $self->CFPlus::UI::Base::find_widget ($x, $y) |
950 | } |
1008 | } |
951 | } |
1009 | } |
952 | |
1010 | |
953 | sub _render { |
1011 | sub _render { |
954 | my ($self) = @_; |
1012 | my ($self) = @_; |
955 | |
1013 | |
956 | local $CFClient::UI::Base::draw_x = $CFClient::UI::Base::draw_x - $self->{view_x}; |
1014 | local $CFPlus::UI::Base::draw_x = $CFPlus::UI::Base::draw_x - $self->{view_x}; |
957 | local $CFClient::UI::Base::draw_y = $CFClient::UI::Base::draw_y - $self->{view_y}; |
1015 | local $CFPlus::UI::Base::draw_y = $CFPlus::UI::Base::draw_y - $self->{view_y}; |
958 | |
1016 | |
959 | CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
1017 | CFPlus::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
960 | |
1018 | |
961 | $self->SUPER::_render; |
1019 | $self->SUPER::_render; |
962 | } |
1020 | } |
963 | |
1021 | |
964 | ############################################################################# |
1022 | ############################################################################# |
965 | |
1023 | |
966 | package CFClient::UI::ScrolledWindow; |
1024 | package CFPlus::UI::ScrolledWindow; |
967 | |
1025 | |
968 | our @ISA = CFClient::UI::HBox::; |
1026 | our @ISA = CFPlus::UI::HBox::; |
969 | |
1027 | |
970 | sub new { |
1028 | sub new { |
971 | my ($class, %arg) = @_; |
1029 | my ($class, %arg) = @_; |
972 | |
1030 | |
973 | my $child = delete $arg{child}; |
1031 | my $child = delete $arg{child}; |
974 | |
1032 | |
975 | my $self; |
1033 | my $self; |
976 | |
1034 | |
977 | my $slider = new CFClient::UI::Slider |
1035 | my $slider = new CFPlus::UI::Slider |
978 | vertical => 1, |
1036 | vertical => 1, |
979 | range => [0, 0, 1, 0.01], # HACK fix |
1037 | range => [0, 0, 1, 0.01], # HACK fix |
980 | on_changed => sub { |
1038 | on_changed => sub { |
981 | $self->{vp}->set_offset (0, $_[1]); |
1039 | $self->{vp}->set_offset (0, $_[1]); |
982 | }, |
1040 | }, |
983 | ; |
1041 | ; |
984 | |
1042 | |
985 | $self = $class->SUPER::new ( |
1043 | $self = $class->SUPER::new ( |
986 | vp => (new CFClient::UI::ViewPort expand => 1), |
1044 | vp => (new CFPlus::UI::ViewPort expand => 1), |
|
|
1045 | can_events => 1, |
987 | slider => $slider, |
1046 | slider => $slider, |
988 | %arg, |
1047 | %arg, |
989 | ); |
1048 | ); |
990 | |
1049 | |
991 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
1050 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
992 | $self->add ($child) if $child; |
1051 | $self->add ($child) if $child; |
… | |
… | |
1000 | my ($self, $widget) = @_; |
1059 | my ($self, $widget) = @_; |
1001 | |
1060 | |
1002 | $self->{vp}->add ($self->{child} = $widget); |
1061 | $self->{vp}->add ($self->{child} = $widget); |
1003 | } |
1062 | } |
1004 | |
1063 | |
1005 | sub invoke_button_down { |
1064 | sub invoke_mouse_wheel { |
1006 | my ($self, $ev) = @_; |
1065 | my ($self, $ev) = @_; |
1007 | |
1066 | |
1008 | warn "button down $ev->{button}\n";#d# |
1067 | return 0 unless $ev->{dy}; # only vertical movements |
|
|
1068 | |
|
|
1069 | $self->{slider}->emit (mouse_wheel => $ev); |
|
|
1070 | |
1009 | |
1071 | 1 |
1010 | 0 |
|
|
1011 | } |
1072 | } |
1012 | |
1073 | |
1013 | sub update_slider { |
1074 | sub update_slider { |
1014 | my ($self) = @_; |
1075 | my ($self) = @_; |
1015 | |
1076 | |
… | |
… | |
1032 | $self->SUPER::invoke_size_allocate ($w, $h) |
1093 | $self->SUPER::invoke_size_allocate ($w, $h) |
1033 | } |
1094 | } |
1034 | |
1095 | |
1035 | ############################################################################# |
1096 | ############################################################################# |
1036 | |
1097 | |
1037 | package CFClient::UI::Frame; |
1098 | package CFPlus::UI::Frame; |
1038 | |
1099 | |
1039 | our @ISA = CFClient::UI::Bin::; |
1100 | our @ISA = CFPlus::UI::Bin::; |
1040 | |
1101 | |
1041 | use CFClient::OpenGL; |
1102 | use CFPlus::OpenGL; |
1042 | |
1103 | |
1043 | sub new { |
1104 | sub new { |
1044 | my $class = shift; |
1105 | my $class = shift; |
1045 | |
1106 | |
1046 | $class->SUPER::new ( |
1107 | $class->SUPER::new ( |
… | |
… | |
1072 | $self->SUPER::_draw; |
1133 | $self->SUPER::_draw; |
1073 | } |
1134 | } |
1074 | |
1135 | |
1075 | ############################################################################# |
1136 | ############################################################################# |
1076 | |
1137 | |
1077 | package CFClient::UI::FancyFrame; |
1138 | package CFPlus::UI::FancyFrame; |
1078 | |
1139 | |
1079 | our @ISA = CFClient::UI::Bin::; |
1140 | our @ISA = CFPlus::UI::Bin::; |
1080 | |
1141 | |
1081 | use CFClient::OpenGL; |
1142 | use CFPlus::OpenGL; |
|
|
1143 | |
|
|
1144 | sub new { |
|
|
1145 | my ($class, %arg) = @_; |
|
|
1146 | |
|
|
1147 | if ((exists $arg{label}) && !ref $arg{label}) { |
|
|
1148 | $arg{label} = new CFPlus::UI::Label |
|
|
1149 | align => 1, |
|
|
1150 | valign => 0, |
|
|
1151 | text => $arg{label}, |
|
|
1152 | fontsize => ($arg{border} || 0.8) * 0.75; |
|
|
1153 | } |
|
|
1154 | |
|
|
1155 | my $self = $class->SUPER::new ( |
|
|
1156 | # label => "", |
|
|
1157 | fg => [0.6, 0.3, 0.1], |
|
|
1158 | border => 0.8, |
|
|
1159 | style => 'single', |
|
|
1160 | %arg, |
|
|
1161 | ); |
|
|
1162 | |
|
|
1163 | $self |
|
|
1164 | } |
|
|
1165 | |
|
|
1166 | sub add { |
|
|
1167 | my ($self, @widgets) = @_; |
|
|
1168 | |
|
|
1169 | $self->SUPER::add (@widgets); |
|
|
1170 | $self->CFPlus::UI::Container::add ($self->{label}) if $self->{label}; |
|
|
1171 | } |
|
|
1172 | |
|
|
1173 | sub border { |
|
|
1174 | int $_[0]{border} * $::FONTSIZE |
|
|
1175 | } |
|
|
1176 | |
|
|
1177 | sub size_request { |
|
|
1178 | my ($self) = @_; |
|
|
1179 | |
|
|
1180 | ($self->{label_w}, undef) = $self->{label}->size_request |
|
|
1181 | if $self->{label}; |
|
|
1182 | |
|
|
1183 | my ($w, $h) = $self->SUPER::size_request; |
|
|
1184 | |
|
|
1185 | ( |
|
|
1186 | $w + $self->border * 2, |
|
|
1187 | $h + $self->border * 2, |
|
|
1188 | ) |
|
|
1189 | } |
|
|
1190 | |
|
|
1191 | sub invoke_size_allocate { |
|
|
1192 | my ($self, $w, $h) = @_; |
|
|
1193 | |
|
|
1194 | my $border = $self->border; |
|
|
1195 | |
|
|
1196 | $w -= List::Util::max 0, $border * 2; |
|
|
1197 | $h -= List::Util::max 0, $border * 2; |
|
|
1198 | |
|
|
1199 | if (my $label = $self->{label}) { |
|
|
1200 | $label->{w} = List::Util::max 0, List::Util::min $self->{label_w}, $w - $border * 2; |
|
|
1201 | $label->{h} = List::Util::min $h, $border; |
|
|
1202 | $label->invoke_size_allocate ($label->{w}, $label->{h}); |
|
|
1203 | } |
|
|
1204 | |
|
|
1205 | $self->child->configure ($border, $border, $w, $h); |
|
|
1206 | |
|
|
1207 | 1 |
|
|
1208 | } |
|
|
1209 | |
|
|
1210 | sub _draw { |
|
|
1211 | my ($self) = @_; |
|
|
1212 | |
|
|
1213 | my $child = $self->{children}[0]; |
|
|
1214 | |
|
|
1215 | my $border = $self->border; |
|
|
1216 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1217 | |
|
|
1218 | $child->draw; |
|
|
1219 | |
|
|
1220 | glColor @{$self->{fg}}; |
|
|
1221 | glBegin GL_LINE_STRIP; |
|
|
1222 | glVertex $border * 1.5 , $border * 0.5 + 0.5; |
|
|
1223 | glVertex $border * 0.5 + 0.5, $border * 0.5 + 0.5; |
|
|
1224 | glVertex $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
|
|
1225 | glVertex $w - $border * 0.5 + 0.5, $h - $border * 0.5 + 0.5; |
|
|
1226 | glVertex $w - $border * 0.5 + 0.5, $border * 0.5 + 0.5; |
|
|
1227 | glVertex $self->{label} ? $border * 2 + $self->{label}{w} : $border * 1.5, $border * 0.5 + 0.5; |
|
|
1228 | glEnd; |
|
|
1229 | |
|
|
1230 | if ($self->{label}) { |
|
|
1231 | glTranslate $border * 2, 0; |
|
|
1232 | $self->{label}->_draw; |
|
|
1233 | } |
|
|
1234 | } |
|
|
1235 | |
|
|
1236 | ############################################################################# |
|
|
1237 | |
|
|
1238 | package CFPlus::UI::Toplevel; |
|
|
1239 | |
|
|
1240 | our @ISA = CFPlus::UI::Bin::; |
|
|
1241 | |
|
|
1242 | use CFPlus::OpenGL; |
1082 | |
1243 | |
1083 | my $bg = |
1244 | my $bg = |
1084 | new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png", |
1245 | new_from_file CFPlus::Texture CFPlus::find_rcfile "d1_bg.png", |
1085 | mipmap => 1, wrap => 1; |
1246 | mipmap => 1, wrap => 1; |
1086 | |
1247 | |
1087 | my @border = |
1248 | my @border = |
1088 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1249 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1089 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1250 | qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1090 | |
1251 | |
1091 | sub new { |
1252 | sub new { |
1092 | my ($class, %arg) = @_; |
1253 | my ($class, %arg) = @_; |
1093 | |
1254 | |
… | |
… | |
1099 | min_w => 64, |
1260 | min_w => 64, |
1100 | min_h => 32, |
1261 | min_h => 32, |
1101 | %arg, |
1262 | %arg, |
1102 | ); |
1263 | ); |
1103 | |
1264 | |
1104 | $self->{title_widget} = new CFClient::UI::Label |
1265 | $self->{title_widget} = new CFPlus::UI::Label |
1105 | align => 0, |
1266 | align => 0, |
1106 | valign => 1, |
1267 | valign => 1, |
1107 | text => $self->{title}, |
1268 | text => $self->{title}, |
1108 | fontsize => $self->{border}, |
1269 | fontsize => $self->{border}, |
1109 | if exists $self->{title}; |
1270 | if exists $self->{title}; |
1110 | |
1271 | |
1111 | if ($self->{has_close_button}) { |
1272 | if ($self->{has_close_button}) { |
1112 | $self->{close_button} = |
1273 | $self->{close_button} = |
1113 | new CFClient::UI::ImageButton |
1274 | new CFPlus::UI::ImageButton |
1114 | path => 'x1_close.png', |
1275 | path => 'x1_close.png', |
1115 | on_activate => sub { $self->emit ("delete") }; |
1276 | on_activate => sub { $self->emit ("delete") }; |
1116 | |
1277 | |
1117 | $self->CFClient::UI::Container::add ($self->{close_button}); |
1278 | $self->CFPlus::UI::Container::add ($self->{close_button}); |
1118 | } |
1279 | } |
1119 | |
1280 | |
1120 | $self |
1281 | $self |
1121 | } |
1282 | } |
1122 | |
1283 | |
1123 | sub add { |
1284 | sub add { |
1124 | my ($self, @widgets) = @_; |
1285 | my ($self, @widgets) = @_; |
1125 | |
1286 | |
1126 | $self->SUPER::add (@widgets); |
1287 | $self->SUPER::add (@widgets); |
1127 | $self->CFClient::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1288 | $self->CFPlus::UI::Container::add ($self->{close_button}) if $self->{close_button}; |
1128 | $self->CFClient::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1289 | $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; |
1129 | } |
1290 | } |
1130 | |
1291 | |
1131 | sub border { |
1292 | sub border { |
1132 | int $_[0]{border} * $::FONTSIZE |
1293 | int $_[0]{border} * $::FONTSIZE |
1133 | } |
1294 | } |
… | |
… | |
1241 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
1402 | $self->{motion}->($ev, $x, $y) if $self->{motion}; |
1242 | |
1403 | |
1243 | ! ! $self->{motion} |
1404 | ! ! $self->{motion} |
1244 | } |
1405 | } |
1245 | |
1406 | |
|
|
1407 | sub invoke_visibility_change { |
|
|
1408 | my ($self, $visible) = @_; |
|
|
1409 | |
|
|
1410 | delete $self->{motion} unless $visible; |
|
|
1411 | |
|
|
1412 | 0 |
|
|
1413 | } |
|
|
1414 | |
1246 | sub _draw { |
1415 | sub _draw { |
1247 | my ($self) = @_; |
1416 | my ($self) = @_; |
1248 | |
1417 | |
1249 | my $child = $self->{children}[0]; |
1418 | my $child = $self->{children}[0]; |
1250 | |
1419 | |
… | |
… | |
1287 | if $self->{close_button}; |
1456 | if $self->{close_button}; |
1288 | } |
1457 | } |
1289 | |
1458 | |
1290 | ############################################################################# |
1459 | ############################################################################# |
1291 | |
1460 | |
1292 | package CFClient::UI::Table; |
1461 | package CFPlus::UI::Table; |
1293 | |
1462 | |
1294 | our @ISA = CFClient::UI::Base::; |
1463 | our @ISA = CFPlus::UI::Base::; |
1295 | |
1464 | |
1296 | use List::Util qw(max sum); |
1465 | use List::Util qw(max sum); |
1297 | |
1466 | |
1298 | use CFClient::OpenGL; |
1467 | use CFPlus::OpenGL; |
1299 | |
1468 | |
1300 | sub new { |
1469 | sub new { |
1301 | my $class = shift; |
1470 | my $class = shift; |
1302 | |
1471 | |
1303 | $class->SUPER::new ( |
1472 | $class->SUPER::new ( |
… | |
… | |
1309 | sub children { |
1478 | sub children { |
1310 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
1479 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
1311 | } |
1480 | } |
1312 | |
1481 | |
1313 | sub add { |
1482 | sub add { |
1314 | my ($self, $x, $y, $child) = @_; |
1483 | my ($self) = shift; |
1315 | |
1484 | |
|
|
1485 | while (@_) { |
|
|
1486 | my ($x, $y, $child) = splice @_, 0, 3, (); |
1316 | $child->set_parent ($self); |
1487 | $child->set_parent ($self); |
1317 | $self->{children}[$y][$x] = $child; |
1488 | $self->{children}[$y][$x] = $child; |
|
|
1489 | } |
1318 | |
1490 | |
|
|
1491 | $self->{force_realloc} = 1; |
|
|
1492 | $self->{force_size_alloc} = 1; |
1319 | $self->realloc; |
1493 | $self->realloc; |
1320 | } |
1494 | } |
1321 | |
1495 | |
1322 | sub remove { |
1496 | sub remove { |
1323 | my ($self, $child) = @_; |
1497 | my ($self, $child) = @_; |
… | |
… | |
1388 | |
1562 | |
1389 | # linearly scale sizes |
1563 | # linearly scale sizes |
1390 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1564 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1391 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
1565 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
1392 | |
1566 | |
1393 | CFClient::UI::harmonize $ws; |
1567 | CFPlus::UI::harmonize $ws; |
1394 | CFClient::UI::harmonize $hs; |
1568 | CFPlus::UI::harmonize $hs; |
1395 | |
1569 | |
1396 | my $y; |
1570 | my $y; |
1397 | |
1571 | |
1398 | for my $r (0 .. $#{$self->{children}}) { |
1572 | for my $r (0 .. $#{$self->{children}}) { |
1399 | my $row = $self->{children}[$r] |
1573 | my $row = $self->{children}[$r] |
… | |
… | |
1442 | } |
1616 | } |
1443 | } |
1617 | } |
1444 | |
1618 | |
1445 | ############################################################################# |
1619 | ############################################################################# |
1446 | |
1620 | |
1447 | package CFClient::UI::Box; |
1621 | package CFPlus::UI::Box; |
1448 | |
1622 | |
1449 | our @ISA = CFClient::UI::Container::; |
1623 | our @ISA = CFPlus::UI::Container::; |
1450 | |
1624 | |
1451 | sub size_request { |
1625 | sub size_request { |
1452 | my ($self) = @_; |
1626 | my ($self) = @_; |
1453 | |
1627 | |
1454 | $self->{vertical} |
1628 | $self->{vertical} |
… | |
… | |
1487 | $req[$_] += $space * $children[$_]{expand} |
1661 | $req[$_] += $space * $children[$_]{expand} |
1488 | for 0 .. $#children; |
1662 | for 0 .. $#children; |
1489 | } |
1663 | } |
1490 | } |
1664 | } |
1491 | |
1665 | |
1492 | CFClient::UI::harmonize \@req; |
1666 | CFPlus::UI::harmonize \@req; |
1493 | |
1667 | |
1494 | my $pos = 0; |
1668 | my $pos = 0; |
1495 | for (0 .. $#children) { |
1669 | for (0 .. $#children) { |
1496 | my $alloc = $req[$_]; |
1670 | my $alloc = $req[$_]; |
1497 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
1671 | $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
… | |
… | |
1502 | 1 |
1676 | 1 |
1503 | } |
1677 | } |
1504 | |
1678 | |
1505 | ############################################################################# |
1679 | ############################################################################# |
1506 | |
1680 | |
1507 | package CFClient::UI::HBox; |
1681 | package CFPlus::UI::HBox; |
1508 | |
1682 | |
1509 | our @ISA = CFClient::UI::Box::; |
1683 | our @ISA = CFPlus::UI::Box::; |
1510 | |
1684 | |
1511 | sub new { |
1685 | sub new { |
1512 | my $class = shift; |
1686 | my $class = shift; |
1513 | |
1687 | |
1514 | $class->SUPER::new ( |
1688 | $class->SUPER::new ( |
… | |
… | |
1517 | ) |
1691 | ) |
1518 | } |
1692 | } |
1519 | |
1693 | |
1520 | ############################################################################# |
1694 | ############################################################################# |
1521 | |
1695 | |
1522 | package CFClient::UI::VBox; |
1696 | package CFPlus::UI::VBox; |
1523 | |
1697 | |
1524 | our @ISA = CFClient::UI::Box::; |
1698 | our @ISA = CFPlus::UI::Box::; |
1525 | |
1699 | |
1526 | sub new { |
1700 | sub new { |
1527 | my $class = shift; |
1701 | my $class = shift; |
1528 | |
1702 | |
1529 | $class->SUPER::new ( |
1703 | $class->SUPER::new ( |
… | |
… | |
1532 | ) |
1706 | ) |
1533 | } |
1707 | } |
1534 | |
1708 | |
1535 | ############################################################################# |
1709 | ############################################################################# |
1536 | |
1710 | |
1537 | package CFClient::UI::Label; |
1711 | package CFPlus::UI::Label; |
1538 | |
1712 | |
1539 | our @ISA = CFClient::UI::DrawBG::; |
1713 | our @ISA = CFPlus::UI::DrawBG::; |
1540 | |
1714 | |
1541 | use CFClient::OpenGL; |
1715 | use CFPlus::OpenGL; |
1542 | |
1716 | |
1543 | sub new { |
1717 | sub new { |
1544 | my ($class, %arg) = @_; |
1718 | my ($class, %arg) = @_; |
1545 | |
1719 | |
1546 | my $self = $class->SUPER::new ( |
1720 | my $self = $class->SUPER::new ( |
… | |
… | |
1549 | #active_bg => none |
1723 | #active_bg => none |
1550 | #font => default_font |
1724 | #font => default_font |
1551 | #text => initial text |
1725 | #text => initial text |
1552 | #markup => initial narkup |
1726 | #markup => initial narkup |
1553 | #max_w => maximum pixel width |
1727 | #max_w => maximum pixel width |
|
|
1728 | #style => 0, # render flags |
1554 | ellipsise => 3, # end |
1729 | ellipsise => 3, # end |
1555 | layout => (new CFClient::Layout), |
1730 | layout => (new CFPlus::Layout), |
1556 | fontsize => 1, |
1731 | fontsize => 1, |
1557 | align => -1, |
1732 | align => -1, |
1558 | valign => -1, |
1733 | valign => -1, |
1559 | padding_x => 2, |
1734 | padding_x => 2, |
1560 | padding_y => 2, |
1735 | padding_y => 2, |
1561 | can_events => 0, |
1736 | can_events => 0, |
1562 | %arg |
1737 | %arg |
1563 | ); |
1738 | ); |
1564 | |
1739 | |
1565 | if (exists $self->{template}) { |
1740 | if (exists $self->{template}) { |
1566 | my $layout = new CFClient::Layout; |
1741 | my $layout = new CFPlus::Layout; |
1567 | $layout->set_text (delete $self->{template}); |
1742 | $layout->set_text (delete $self->{template}); |
1568 | $self->{template} = $layout; |
1743 | $self->{template} = $layout; |
1569 | } |
1744 | } |
1570 | |
1745 | |
1571 | if (exists $self->{markup}) { |
1746 | if (exists $self->{markup}) { |
… | |
… | |
1573 | } else { |
1748 | } else { |
1574 | $self->set_text (delete $self->{text}); |
1749 | $self->set_text (delete $self->{text}); |
1575 | } |
1750 | } |
1576 | |
1751 | |
1577 | $self |
1752 | $self |
1578 | } |
|
|
1579 | |
|
|
1580 | sub escape($) { |
|
|
1581 | local $_ = $_[0]; |
|
|
1582 | |
|
|
1583 | s/&/&/g; |
|
|
1584 | s/>/>/g; |
|
|
1585 | s/</</g; |
|
|
1586 | |
|
|
1587 | $_ |
|
|
1588 | } |
1753 | } |
1589 | |
1754 | |
1590 | sub update { |
1755 | sub update { |
1591 | my ($self) = @_; |
1756 | my ($self) = @_; |
1592 | |
1757 | |
… | |
… | |
1719 | }; |
1884 | }; |
1720 | |
1885 | |
1721 | my $w = List::Util::min $self->{w} + 4, $size->[0]; |
1886 | my $w = List::Util::min $self->{w} + 4, $size->[0]; |
1722 | my $h = List::Util::min $self->{h} + 2, $size->[1]; |
1887 | my $h = List::Util::min $self->{h} + 2, $size->[1]; |
1723 | |
1888 | |
1724 | $self->{layout}->render ($self->{ox}, $self->{oy}); |
1889 | $self->{layout}->render ($self->{ox}, $self->{oy}, $self->{style}); |
1725 | } |
1890 | } |
1726 | |
1891 | |
1727 | ############################################################################# |
1892 | ############################################################################# |
1728 | |
1893 | |
1729 | package CFClient::UI::EntryBase; |
1894 | package CFPlus::UI::EntryBase; |
1730 | |
1895 | |
1731 | our @ISA = CFClient::UI::Label::; |
1896 | our @ISA = CFPlus::UI::Label::; |
1732 | |
1897 | |
1733 | use CFClient::OpenGL; |
1898 | use CFPlus::OpenGL; |
1734 | |
1899 | |
1735 | sub new { |
1900 | sub new { |
1736 | my $class = shift; |
1901 | my $class = shift; |
1737 | |
1902 | |
1738 | $class->SUPER::new ( |
1903 | $class->SUPER::new ( |
… | |
… | |
1742 | active_fg => [0, 0, 0], |
1907 | active_fg => [0, 0, 0], |
1743 | can_hover => 1, |
1908 | can_hover => 1, |
1744 | can_focus => 1, |
1909 | can_focus => 1, |
1745 | valign => 0, |
1910 | valign => 0, |
1746 | can_events => 1, |
1911 | can_events => 1, |
|
|
1912 | ellipsise => 0, |
1747 | #text => ... |
1913 | #text => ... |
1748 | #hidden => "*", |
1914 | #hidden => "*", |
1749 | @_ |
1915 | @_ |
1750 | ) |
1916 | ) |
1751 | } |
1917 | } |
… | |
… | |
1800 | |
1966 | |
1801 | if ($uni == 8) { |
1967 | if ($uni == 8) { |
1802 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1968 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1803 | } elsif ($uni == 127) { |
1969 | } elsif ($uni == 127) { |
1804 | substr $text, $self->{cursor}, 1, ""; |
1970 | substr $text, $self->{cursor}, 1, ""; |
1805 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1971 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
1806 | --$self->{cursor} if $self->{cursor}; |
1972 | --$self->{cursor} if $self->{cursor}; |
1807 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1973 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
1808 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1974 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1809 | } elsif ($sym == CFClient::SDLK_HOME) { |
1975 | } elsif ($sym == CFPlus::SDLK_HOME) { |
1810 | $self->{cursor} = 0; |
1976 | $self->{cursor} = 0; |
1811 | } elsif ($sym == CFClient::SDLK_END) { |
1977 | } elsif ($sym == CFPlus::SDLK_END) { |
1812 | $self->{cursor} = length $text; |
1978 | $self->{cursor} = length $text; |
|
|
1979 | } elsif ($uni == 21) { # ctrl-u |
|
|
1980 | $text = ""; |
|
|
1981 | $self->{cursor} = 0; |
1813 | } elsif ($uni == 27) { |
1982 | } elsif ($uni == 27) { |
1814 | $self->emit ('escape'); |
1983 | $self->emit ('escape'); |
1815 | } elsif ($uni) { |
1984 | } elsif ($uni >= 0x20 || $uni == 0x0d) { |
1816 | substr $text, $self->{cursor}++, 0, chr $uni; |
1985 | substr $text, $self->{cursor}++, 0, chr $uni; |
1817 | } else { |
1986 | } else { |
1818 | return 0; |
1987 | return 0; |
1819 | } |
1988 | } |
1820 | |
1989 | |
1821 | $self->_set_text ($text); |
1990 | $self->_set_text ($text); |
1822 | |
1991 | |
1823 | $self->realloc; |
1992 | $self->realloc; |
|
|
1993 | $self->update; |
1824 | |
1994 | |
1825 | 1 |
1995 | 1 |
1826 | } |
1996 | } |
1827 | |
1997 | |
1828 | sub invoke_focus_in { |
1998 | sub invoke_focus_in { |
… | |
… | |
1890 | utf8::encode $text; |
2060 | utf8::encode $text; |
1891 | |
2061 | |
1892 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
2062 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
1893 | } |
2063 | } |
1894 | |
2064 | |
1895 | glColor @{$self->{fg}}; |
|
|
1896 | glBegin GL_LINES; |
2065 | glBegin GL_LINES; |
1897 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
2066 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
1898 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2067 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1899 | glEnd; |
2068 | glEnd; |
1900 | } |
2069 | } |
1901 | } |
2070 | } |
1902 | |
2071 | |
|
|
2072 | ############################################################################# |
|
|
2073 | |
1903 | package CFClient::UI::Entry; |
2074 | package CFPlus::UI::Entry; |
1904 | |
2075 | |
1905 | our @ISA = CFClient::UI::EntryBase::; |
2076 | our @ISA = CFPlus::UI::EntryBase::; |
1906 | |
2077 | |
1907 | use CFClient::OpenGL; |
2078 | use CFPlus::OpenGL; |
1908 | |
2079 | |
1909 | sub invoke_key_down { |
2080 | sub invoke_key_down { |
1910 | my ($self, $ev) = @_; |
2081 | my ($self, $ev) = @_; |
1911 | |
2082 | |
1912 | my $sym = $ev->{sym}; |
2083 | my $sym = $ev->{sym}; |
1913 | |
2084 | |
1914 | if ($sym == 13) { |
2085 | if ($ev->{uni} == 0x0d || $sym == 13) { |
1915 | unshift @{$self->{history}}, |
2086 | unshift @{$self->{history}}, |
1916 | my $txt = $self->get_text; |
2087 | my $txt = $self->get_text; |
1917 | |
2088 | |
1918 | $self->{history_pointer} = -1; |
2089 | $self->{history_pointer} = -1; |
1919 | $self->{history_saveback} = ''; |
2090 | $self->{history_saveback} = ''; |
1920 | $self->emit (activate => $txt); |
2091 | $self->emit (activate => $txt); |
1921 | $self->update; |
2092 | $self->update; |
1922 | |
2093 | |
1923 | } elsif ($sym == CFClient::SDLK_UP) { |
2094 | } elsif ($sym == CFPlus::SDLK_UP) { |
1924 | if ($self->{history_pointer} < 0) { |
2095 | if ($self->{history_pointer} < 0) { |
1925 | $self->{history_saveback} = $self->get_text; |
2096 | $self->{history_saveback} = $self->get_text; |
1926 | } |
2097 | } |
1927 | if (@{$self->{history} || []} > 0) { |
2098 | if (@{$self->{history} || []} > 0) { |
1928 | $self->{history_pointer}++; |
2099 | $self->{history_pointer}++; |
… | |
… | |
1930 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
2101 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
1931 | } |
2102 | } |
1932 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2103 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
1933 | } |
2104 | } |
1934 | |
2105 | |
1935 | } elsif ($sym == CFClient::SDLK_DOWN) { |
2106 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
1936 | $self->{history_pointer}--; |
2107 | $self->{history_pointer}--; |
1937 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
2108 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
1938 | |
2109 | |
1939 | if ($self->{history_pointer} >= 0) { |
2110 | if ($self->{history_pointer} >= 0) { |
1940 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
2111 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
… | |
… | |
1949 | 1 |
2120 | 1 |
1950 | } |
2121 | } |
1951 | |
2122 | |
1952 | ############################################################################# |
2123 | ############################################################################# |
1953 | |
2124 | |
|
|
2125 | package CFPlus::UI::TextEdit; |
|
|
2126 | |
|
|
2127 | our @ISA = CFPlus::UI::EntryBase::; |
|
|
2128 | |
|
|
2129 | use CFPlus::OpenGL; |
|
|
2130 | |
|
|
2131 | sub move_cursor_ver { |
|
|
2132 | my ($self, $dy) = @_; |
|
|
2133 | |
|
|
2134 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
|
|
2135 | |
|
|
2136 | $y += $dy; |
|
|
2137 | |
|
|
2138 | if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { |
|
|
2139 | $self->{cursor} = $index; |
|
|
2140 | delete $self->{cur_h}; |
|
|
2141 | $self->update; |
|
|
2142 | return; |
|
|
2143 | } |
|
|
2144 | } |
|
|
2145 | |
|
|
2146 | sub invoke_key_down { |
|
|
2147 | my ($self, $ev) = @_; |
|
|
2148 | |
|
|
2149 | my $sym = $ev->{sym}; |
|
|
2150 | |
|
|
2151 | if ($sym == CFPlus::SDLK_UP) { |
|
|
2152 | $self->move_cursor_ver (-1); |
|
|
2153 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
|
|
2154 | $self->move_cursor_ver (+1); |
|
|
2155 | } else { |
|
|
2156 | return $self->SUPER::invoke_key_down ($ev) |
|
|
2157 | } |
|
|
2158 | |
|
|
2159 | 1 |
|
|
2160 | } |
|
|
2161 | |
|
|
2162 | ############################################################################# |
|
|
2163 | |
1954 | package CFClient::UI::Button; |
2164 | package CFPlus::UI::Button; |
1955 | |
2165 | |
1956 | our @ISA = CFClient::UI::Label::; |
2166 | our @ISA = CFPlus::UI::Label::; |
1957 | |
2167 | |
1958 | use CFClient::OpenGL; |
2168 | use CFPlus::OpenGL; |
1959 | |
2169 | |
1960 | my @tex = |
2170 | my @tex = |
1961 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
2171 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
1962 | qw(b1_button_active.png); |
2172 | qw(b1_button_active.png); |
1963 | |
2173 | |
1964 | sub new { |
2174 | sub new { |
1965 | my $class = shift; |
2175 | my $class = shift; |
1966 | |
2176 | |
… | |
… | |
2003 | $self->SUPER::_draw; |
2213 | $self->SUPER::_draw; |
2004 | } |
2214 | } |
2005 | |
2215 | |
2006 | ############################################################################# |
2216 | ############################################################################# |
2007 | |
2217 | |
|
|
2218 | package CFPlus::UI::CheckBox; |
|
|
2219 | |
|
|
2220 | our @ISA = CFPlus::UI::DrawBG::; |
|
|
2221 | |
|
|
2222 | my @tex = |
|
|
2223 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
|
|
2224 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
|
|
2225 | |
|
|
2226 | use CFPlus::OpenGL; |
|
|
2227 | |
|
|
2228 | sub new { |
|
|
2229 | my $class = shift; |
|
|
2230 | |
|
|
2231 | $class->SUPER::new ( |
|
|
2232 | padding_x => 2, |
|
|
2233 | padding_y => 2, |
|
|
2234 | fg => [1, 1, 1], |
|
|
2235 | active_fg => [1, 1, 0], |
|
|
2236 | bg => [0, 0, 0, 0.2], |
|
|
2237 | active_bg => [1, 1, 1, 0.5], |
|
|
2238 | state => 0, |
|
|
2239 | can_hover => 1, |
|
|
2240 | @_ |
|
|
2241 | ) |
|
|
2242 | } |
|
|
2243 | |
|
|
2244 | sub size_request { |
|
|
2245 | my ($self) = @_; |
|
|
2246 | |
|
|
2247 | (6) x 2 |
|
|
2248 | } |
|
|
2249 | |
|
|
2250 | sub toggle { |
|
|
2251 | my ($self) = @_; |
|
|
2252 | |
|
|
2253 | $self->{state} = !$self->{state}; |
|
|
2254 | $self->emit (changed => $self->{state}); |
|
|
2255 | $self->update; |
|
|
2256 | } |
|
|
2257 | |
|
|
2258 | sub invoke_button_down { |
|
|
2259 | my ($self, $ev, $x, $y) = @_; |
|
|
2260 | |
|
|
2261 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
|
|
2262 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
|
|
2263 | $self->toggle; |
|
|
2264 | } else { |
|
|
2265 | return 0 |
|
|
2266 | } |
|
|
2267 | |
|
|
2268 | 1 |
|
|
2269 | } |
|
|
2270 | |
|
|
2271 | sub _draw { |
|
|
2272 | my ($self) = @_; |
|
|
2273 | |
|
|
2274 | $self->SUPER::_draw; |
|
|
2275 | |
|
|
2276 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
|
|
2277 | |
|
|
2278 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2279 | |
|
|
2280 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
|
|
2281 | |
|
|
2282 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
|
|
2283 | |
|
|
2284 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
2285 | |
|
|
2286 | glEnable GL_TEXTURE_2D; |
|
|
2287 | $tex->draw_quad_alpha (0, 0, $s, $s); |
|
|
2288 | glDisable GL_TEXTURE_2D; |
|
|
2289 | } |
|
|
2290 | |
|
|
2291 | ############################################################################# |
|
|
2292 | |
|
|
2293 | package CFPlus::UI::Image; |
|
|
2294 | |
|
|
2295 | our @ISA = CFPlus::UI::Base::; |
|
|
2296 | |
|
|
2297 | use CFPlus::OpenGL; |
|
|
2298 | |
|
|
2299 | our %texture_cache; |
|
|
2300 | |
|
|
2301 | sub new { |
|
|
2302 | my $class = shift; |
|
|
2303 | |
|
|
2304 | my $self = $class->SUPER::new ( |
|
|
2305 | can_events => 0, |
|
|
2306 | @_, |
|
|
2307 | ); |
|
|
2308 | |
|
|
2309 | $self->{path} || $self->{tex} |
|
|
2310 | or Carp::croak "'path' or 'tex' attributes required"; |
|
|
2311 | |
|
|
2312 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
|
|
2313 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
|
|
2314 | |
|
|
2315 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
|
|
2316 | |
|
|
2317 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
|
|
2318 | |
|
|
2319 | $self |
|
|
2320 | } |
|
|
2321 | |
|
|
2322 | sub STORABLE_freeze { |
|
|
2323 | my ($self, $cloning) = @_; |
|
|
2324 | |
|
|
2325 | $self->{path} |
|
|
2326 | or die "cannot serialise CFPlus::UI::Image on non-loadable images\n"; |
|
|
2327 | |
|
|
2328 | $self->{path} |
|
|
2329 | } |
|
|
2330 | |
|
|
2331 | sub STORABLE_attach { |
|
|
2332 | my ($self, $cloning, $path) = @_; |
|
|
2333 | |
|
|
2334 | $self->new (path => $path) |
|
|
2335 | } |
|
|
2336 | |
|
|
2337 | sub size_request { |
|
|
2338 | my ($self) = @_; |
|
|
2339 | |
|
|
2340 | ($self->{tex}{w}, $self->{tex}{h}) |
|
|
2341 | } |
|
|
2342 | |
|
|
2343 | sub _draw { |
|
|
2344 | my ($self) = @_; |
|
|
2345 | |
|
|
2346 | my $tex = $self->{tex}; |
|
|
2347 | |
|
|
2348 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
2349 | |
|
|
2350 | if ($self->{rot90}) { |
|
|
2351 | glRotate 90, 0, 0, 1; |
|
|
2352 | glTranslate 0, -$self->{w}, 0; |
|
|
2353 | |
|
|
2354 | ($w, $h) = ($h, $w); |
|
|
2355 | } |
|
|
2356 | |
|
|
2357 | glEnable GL_TEXTURE_2D; |
|
|
2358 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2359 | |
|
|
2360 | $tex->draw_quad (0, 0, $w, $h); |
|
|
2361 | |
|
|
2362 | glDisable GL_TEXTURE_2D; |
|
|
2363 | } |
|
|
2364 | |
|
|
2365 | ############################################################################# |
|
|
2366 | |
2008 | package CFClient::UI::ImageButton; |
2367 | package CFPlus::UI::ImageButton; |
2009 | |
2368 | |
2010 | our @ISA = CFClient::UI::Image::; |
2369 | our @ISA = CFPlus::UI::Image::; |
2011 | |
2370 | |
2012 | use CFClient::OpenGL; |
2371 | use CFPlus::OpenGL; |
2013 | |
2372 | |
2014 | my %textures; |
2373 | my %textures; |
2015 | |
2374 | |
2016 | sub new { |
2375 | sub new { |
2017 | my $class = shift; |
2376 | my $class = shift; |
… | |
… | |
2039 | 1 |
2398 | 1 |
2040 | } |
2399 | } |
2041 | |
2400 | |
2042 | ############################################################################# |
2401 | ############################################################################# |
2043 | |
2402 | |
2044 | package CFClient::UI::CheckBox; |
|
|
2045 | |
|
|
2046 | our @ISA = CFClient::UI::DrawBG::; |
|
|
2047 | |
|
|
2048 | my @tex = |
|
|
2049 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
2050 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
|
|
2051 | |
|
|
2052 | use CFClient::OpenGL; |
|
|
2053 | |
|
|
2054 | sub new { |
|
|
2055 | my $class = shift; |
|
|
2056 | |
|
|
2057 | $class->SUPER::new ( |
|
|
2058 | padding_x => 2, |
|
|
2059 | padding_y => 2, |
|
|
2060 | fg => [1, 1, 1], |
|
|
2061 | active_fg => [1, 1, 0], |
|
|
2062 | bg => [0, 0, 0, 0.2], |
|
|
2063 | active_bg => [1, 1, 1, 0.5], |
|
|
2064 | state => 0, |
|
|
2065 | can_hover => 1, |
|
|
2066 | @_ |
|
|
2067 | ) |
|
|
2068 | } |
|
|
2069 | |
|
|
2070 | sub size_request { |
|
|
2071 | my ($self) = @_; |
|
|
2072 | |
|
|
2073 | (6) x 2 |
|
|
2074 | } |
|
|
2075 | |
|
|
2076 | sub toggle { |
|
|
2077 | my ($self) = @_; |
|
|
2078 | |
|
|
2079 | $self->{state} = !$self->{state}; |
|
|
2080 | $self->emit (changed => $self->{state}); |
|
|
2081 | $self->update; |
|
|
2082 | } |
|
|
2083 | |
|
|
2084 | sub invoke_button_down { |
|
|
2085 | my ($self, $ev, $x, $y) = @_; |
|
|
2086 | |
|
|
2087 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
|
|
2088 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
|
|
2089 | $self->toggle; |
|
|
2090 | } else { |
|
|
2091 | return 0 |
|
|
2092 | } |
|
|
2093 | |
|
|
2094 | 1 |
|
|
2095 | } |
|
|
2096 | |
|
|
2097 | sub _draw { |
|
|
2098 | my ($self) = @_; |
|
|
2099 | |
|
|
2100 | $self->SUPER::_draw; |
|
|
2101 | |
|
|
2102 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
|
|
2103 | |
|
|
2104 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2105 | |
|
|
2106 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
|
|
2107 | |
|
|
2108 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
|
|
2109 | |
|
|
2110 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
2111 | |
|
|
2112 | glEnable GL_TEXTURE_2D; |
|
|
2113 | $tex->draw_quad_alpha (0, 0, $s, $s); |
|
|
2114 | glDisable GL_TEXTURE_2D; |
|
|
2115 | } |
|
|
2116 | |
|
|
2117 | ############################################################################# |
|
|
2118 | |
|
|
2119 | package CFClient::UI::Image; |
2403 | package CFPlus::UI::VGauge; |
2120 | |
2404 | |
2121 | our @ISA = CFClient::UI::Base::; |
2405 | our @ISA = CFPlus::UI::Base::; |
2122 | |
|
|
2123 | use CFClient::OpenGL; |
|
|
2124 | |
|
|
2125 | our %texture_cache; |
|
|
2126 | |
|
|
2127 | sub new { |
|
|
2128 | my $class = shift; |
|
|
2129 | |
|
|
2130 | my $self = $class->SUPER::new ( |
|
|
2131 | can_events => 0, |
|
|
2132 | @_, |
|
|
2133 | ); |
|
|
2134 | |
|
|
2135 | $self->{path} || $self->{tex} |
|
|
2136 | or Carp::croak "'path' or 'tex' attributes required"; |
|
|
2137 | |
|
|
2138 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
|
|
2139 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; |
|
|
2140 | |
|
|
2141 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
|
|
2142 | |
|
|
2143 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
|
|
2144 | |
|
|
2145 | $self |
|
|
2146 | } |
|
|
2147 | |
|
|
2148 | sub size_request { |
|
|
2149 | my ($self) = @_; |
|
|
2150 | |
|
|
2151 | ($self->{tex}{w}, $self->{tex}{h}) |
|
|
2152 | } |
|
|
2153 | |
|
|
2154 | sub _draw { |
|
|
2155 | my ($self) = @_; |
|
|
2156 | |
|
|
2157 | my $tex = $self->{tex}; |
|
|
2158 | |
|
|
2159 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
2160 | |
|
|
2161 | if ($self->{rot90}) { |
|
|
2162 | glRotate 90, 0, 0, 1; |
|
|
2163 | glTranslate 0, -$self->{w}, 0; |
|
|
2164 | |
|
|
2165 | ($w, $h) = ($h, $w); |
|
|
2166 | } |
|
|
2167 | |
|
|
2168 | glEnable GL_TEXTURE_2D; |
|
|
2169 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2170 | |
|
|
2171 | $tex->draw_quad_alpha (0, 0, $w, $h); |
|
|
2172 | |
|
|
2173 | glDisable GL_TEXTURE_2D; |
|
|
2174 | } |
|
|
2175 | |
|
|
2176 | ############################################################################# |
|
|
2177 | |
|
|
2178 | package CFClient::UI::VGauge; |
|
|
2179 | |
|
|
2180 | our @ISA = CFClient::UI::Base::; |
|
|
2181 | |
2406 | |
2182 | use List::Util qw(min max); |
2407 | use List::Util qw(min max); |
2183 | |
2408 | |
2184 | use CFClient::OpenGL; |
2409 | use CFPlus::OpenGL; |
2185 | |
2410 | |
2186 | my %tex = ( |
2411 | my %tex = ( |
2187 | food => [ |
2412 | food => [ |
2188 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
2413 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2189 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2414 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
2190 | ], |
2415 | ], |
2191 | grace => [ |
2416 | grace => [ |
2192 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
2417 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2193 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2418 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
2194 | ], |
2419 | ], |
2195 | hp => [ |
2420 | hp => [ |
2196 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
2421 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2197 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2422 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
2198 | ], |
2423 | ], |
2199 | mana => [ |
2424 | mana => [ |
2200 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
2425 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_, mipmap => 1 } |
2201 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2426 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
2202 | ], |
2427 | ], |
2203 | ); |
2428 | ); |
2204 | |
2429 | |
2205 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
2430 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
… | |
… | |
2308 | glDisable GL_TEXTURE_2D; |
2533 | glDisable GL_TEXTURE_2D; |
2309 | } |
2534 | } |
2310 | |
2535 | |
2311 | ############################################################################# |
2536 | ############################################################################# |
2312 | |
2537 | |
2313 | package CFClient::UI::Gauge; |
2538 | package CFPlus::UI::Gauge; |
2314 | |
2539 | |
2315 | our @ISA = CFClient::UI::VBox::; |
2540 | our @ISA = CFPlus::UI::VBox::; |
2316 | |
2541 | |
2317 | sub new { |
2542 | sub new { |
2318 | my ($class, %arg) = @_; |
2543 | my ($class, %arg) = @_; |
2319 | |
2544 | |
2320 | my $self = $class->SUPER::new ( |
2545 | my $self = $class->SUPER::new ( |
… | |
… | |
2322 | can_hover => 1, |
2547 | can_hover => 1, |
2323 | can_events => 1, |
2548 | can_events => 1, |
2324 | %arg, |
2549 | %arg, |
2325 | ); |
2550 | ); |
2326 | |
2551 | |
2327 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); |
2552 | $self->add ($self->{value} = new CFPlus::UI::Label valign => +1, align => 0, template => "999"); |
2328 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2553 | $self->add ($self->{gauge} = new CFPlus::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
2329 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); |
2554 | $self->add ($self->{max} = new CFPlus::UI::Label valign => -1, align => 0, template => "999"); |
2330 | |
2555 | |
2331 | $self |
2556 | $self |
2332 | } |
2557 | } |
2333 | |
2558 | |
2334 | sub set_fontsize { |
2559 | sub set_fontsize { |
… | |
… | |
2355 | $self->{value}->set_text ($val); |
2580 | $self->{value}->set_text ($val); |
2356 | } |
2581 | } |
2357 | |
2582 | |
2358 | ############################################################################# |
2583 | ############################################################################# |
2359 | |
2584 | |
2360 | package CFClient::UI::Slider; |
2585 | package CFPlus::UI::Slider; |
2361 | |
2586 | |
2362 | use strict; |
2587 | use strict; |
2363 | |
2588 | |
2364 | use CFClient::OpenGL; |
2589 | use CFPlus::OpenGL; |
2365 | |
2590 | |
2366 | our @ISA = CFClient::UI::DrawBG::; |
2591 | our @ISA = CFPlus::UI::DrawBG::; |
2367 | |
2592 | |
2368 | my @tex = |
2593 | my @tex = |
2369 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
2594 | map { new_from_file CFPlus::Texture CFPlus::find_rcfile $_ } |
2370 | qw(s1_slider.png s1_slider_bg.png); |
2595 | qw(s1_slider.png s1_slider_bg.png); |
2371 | |
2596 | |
2372 | sub new { |
2597 | sub new { |
2373 | my $class = shift; |
2598 | my $class = shift; |
2374 | |
2599 | |
… | |
… | |
2463 | } |
2688 | } |
2464 | |
2689 | |
2465 | 1 |
2690 | 1 |
2466 | } |
2691 | } |
2467 | |
2692 | |
|
|
2693 | sub invoke_mouse_wheel { |
|
|
2694 | my ($self, $ev) = @_; |
|
|
2695 | |
|
|
2696 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
|
|
2697 | |
|
|
2698 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); |
|
|
2699 | |
|
|
2700 | ! ! $delta |
|
|
2701 | } |
|
|
2702 | |
2468 | sub update { |
2703 | sub update { |
2469 | my ($self) = @_; |
2704 | my ($self) = @_; |
2470 | |
2705 | |
2471 | delete $self->{knob_w}; |
2706 | delete $self->{knob_w}; |
2472 | $self->SUPER::update; |
2707 | $self->SUPER::update; |
… | |
… | |
2519 | glDisable GL_TEXTURE_2D; |
2754 | glDisable GL_TEXTURE_2D; |
2520 | } |
2755 | } |
2521 | |
2756 | |
2522 | ############################################################################# |
2757 | ############################################################################# |
2523 | |
2758 | |
2524 | package CFClient::UI::ValSlider; |
2759 | package CFPlus::UI::ValSlider; |
2525 | |
2760 | |
2526 | our @ISA = CFClient::UI::HBox::; |
2761 | our @ISA = CFPlus::UI::HBox::; |
2527 | |
2762 | |
2528 | sub new { |
2763 | sub new { |
2529 | my ($class, %arg) = @_; |
2764 | my ($class, %arg) = @_; |
2530 | |
2765 | |
2531 | my $range = delete $arg{range}; |
2766 | my $range = delete $arg{range}; |
2532 | |
2767 | |
2533 | my $self = $class->SUPER::new ( |
2768 | my $self = $class->SUPER::new ( |
2534 | slider => (new CFClient::UI::Slider expand => 1, range => $range), |
2769 | slider => (new CFPlus::UI::Slider expand => 1, range => $range), |
2535 | entry => (new CFClient::UI::Label text => "", template => delete $arg{template}), |
2770 | entry => (new CFPlus::UI::Label text => "", template => delete $arg{template}), |
2536 | to_value => sub { shift }, |
2771 | to_value => sub { shift }, |
2537 | from_value => sub { shift }, |
2772 | from_value => sub { shift }, |
2538 | %arg, |
2773 | %arg, |
2539 | ); |
2774 | ); |
2540 | |
2775 | |
… | |
… | |
2560 | sub set_range { shift->{slider}->set_range (@_) } |
2795 | sub set_range { shift->{slider}->set_range (@_) } |
2561 | sub set_value { shift->{slider}->set_value (@_) } |
2796 | sub set_value { shift->{slider}->set_value (@_) } |
2562 | |
2797 | |
2563 | ############################################################################# |
2798 | ############################################################################# |
2564 | |
2799 | |
2565 | package CFClient::UI::TextScroller; |
2800 | package CFPlus::UI::TextScroller; |
2566 | |
2801 | |
2567 | our @ISA = CFClient::UI::HBox::; |
2802 | our @ISA = CFPlus::UI::HBox::; |
2568 | |
2803 | |
2569 | use CFClient::OpenGL; |
2804 | use CFPlus::OpenGL; |
2570 | |
2805 | |
2571 | sub new { |
2806 | sub new { |
2572 | my $class = shift; |
2807 | my $class = shift; |
2573 | |
2808 | |
2574 | my $self = $class->SUPER::new ( |
2809 | my $self = $class->SUPER::new ( |
2575 | fontsize => 1, |
2810 | fontsize => 1, |
2576 | can_events => 0, |
2811 | can_events => 1, |
2577 | indent => 0, |
2812 | indent => 0, |
2578 | #font => default_font |
2813 | #font => default_font |
2579 | @_, |
2814 | @_, |
2580 | |
2815 | |
2581 | layout => (new CFClient::Layout), |
2816 | layout => (new CFPlus::Layout), |
2582 | par => [], |
2817 | par => [], |
2583 | height => 0, |
2818 | height => 0, |
2584 | children => [ |
2819 | children => [ |
2585 | (new CFClient::UI::Empty expand => 1), |
2820 | (new CFPlus::UI::Empty expand => 1), |
2586 | (new CFClient::UI::Slider vertical => 1), |
2821 | (new CFPlus::UI::Slider vertical => 1), |
2587 | ], |
2822 | ], |
2588 | ); |
2823 | ); |
2589 | |
2824 | |
2590 | $self->{children}[1]->connect (changed => sub { $self->update }); |
2825 | $self->{children}[1]->connect (changed => sub { $self->update }); |
2591 | |
2826 | |
… | |
… | |
2621 | |
2856 | |
2622 | $self->reflow; |
2857 | $self->reflow; |
2623 | |
2858 | |
2624 | local $self->{children} = [$empty, $slider]; |
2859 | local $self->{children} = [$empty, $slider]; |
2625 | $self->SUPER::invoke_size_allocate ($w, $h) |
2860 | $self->SUPER::invoke_size_allocate ($w, $h) |
|
|
2861 | } |
|
|
2862 | |
|
|
2863 | sub invoke_mouse_wheel { |
|
|
2864 | my ($self, $ev) = @_; |
|
|
2865 | |
|
|
2866 | return 0 unless $ev->{dy}; # only vertical movements |
|
|
2867 | |
|
|
2868 | $self->{children}[1]->emit (mouse_wheel => $ev); |
|
|
2869 | |
|
|
2870 | 1 |
2626 | } |
2871 | } |
2627 | |
2872 | |
2628 | sub get_layout { |
2873 | sub get_layout { |
2629 | my ($self, $para) = @_; |
2874 | my ($self, $para) = @_; |
2630 | |
2875 | |
… | |
… | |
2658 | |
2903 | |
2659 | # todo: base offset on lines or so, not on pixels |
2904 | # todo: base offset on lines or so, not on pixels |
2660 | $self->{children}[1]->set_value ($offset); |
2905 | $self->{children}[1]->set_value ($offset); |
2661 | } |
2906 | } |
2662 | |
2907 | |
|
|
2908 | sub current_paragraph { |
|
|
2909 | my ($self) = @_; |
|
|
2910 | |
|
|
2911 | $self->{top_paragraph} - 1 |
|
|
2912 | } |
|
|
2913 | |
|
|
2914 | sub scroll_to { |
|
|
2915 | my ($self, $para) = @_; |
|
|
2916 | |
|
|
2917 | $para = List::Util::max 0, List::Util::min $#{$self->{par}}, $para; |
|
|
2918 | |
|
|
2919 | $self->{scroll_to} = $para; |
|
|
2920 | $self->update; |
|
|
2921 | } |
|
|
2922 | |
2663 | sub clear { |
2923 | sub clear { |
2664 | my ($self) = @_; |
2924 | my ($self) = @_; |
2665 | |
2925 | |
2666 | my (undef, undef, @other) = @{ $self->{children} }; |
2926 | my (undef, undef, @other) = @{ $self->{children} }; |
2667 | $self->remove ($_) for @other; |
2927 | $self->remove ($_) for @other; |
… | |
… | |
2670 | $self->{height} = 0; |
2930 | $self->{height} = 0; |
2671 | $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); |
2931 | $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); |
2672 | } |
2932 | } |
2673 | |
2933 | |
2674 | sub add_paragraph { |
2934 | sub add_paragraph { |
2675 | my ($self, $color, $para, $indent) = @_; |
2935 | my $self = shift; |
2676 | |
2936 | |
2677 | my ($text, @w) = ref $para ? @$para : $para; |
2937 | for my $para (@_) { |
2678 | |
|
|
2679 | $para = { |
2938 | $para = { |
|
|
2939 | fg => [1, 1, 1, 1], |
|
|
2940 | indent => 0, |
|
|
2941 | markup => "", |
|
|
2942 | widget => [], |
|
|
2943 | ref $para ? %$para : (markup => $para), |
2680 | w => 1e10, |
2944 | w => 1e10, |
2681 | wrapped => 1, |
2945 | wrapped => 1, |
2682 | fg => $color, |
|
|
2683 | indent => $indent, |
|
|
2684 | markup => $text, |
|
|
2685 | widget => \@w, |
|
|
2686 | }; |
2946 | }; |
2687 | |
2947 | |
2688 | $self->add (@w) if @w; |
2948 | $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; |
2689 | push @{$self->{par}}, $para; |
2949 | push @{$self->{par}}, $para; |
|
|
2950 | } |
2690 | |
2951 | |
2691 | $self->{need_reflow}++; |
2952 | $self->{need_reflow}++; |
2692 | $self->update; |
2953 | $self->update; |
2693 | } |
2954 | } |
2694 | |
2955 | |
2695 | sub scroll_to_bottom { |
2956 | sub scroll_to_bottom { |
2696 | my ($self) = @_; |
2957 | my ($self) = @_; |
2697 | |
2958 | |
2698 | $self->{scroll_to_bottom} = 1; |
2959 | $self->{scroll_to} = $#{$self->{par}}; |
2699 | $self->update; |
2960 | $self->update; |
2700 | } |
2961 | } |
2701 | |
2962 | |
|
|
2963 | sub force_uptodate { |
|
|
2964 | my ($self) = @_; |
|
|
2965 | |
|
|
2966 | if (delete $self->{need_reflow}) { |
|
|
2967 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
|
|
2968 | |
|
|
2969 | my $height = 0; |
|
|
2970 | |
|
|
2971 | for my $para (@{$self->{par}}) { |
|
|
2972 | if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { |
|
|
2973 | my $layout = $self->get_layout ($para); |
|
|
2974 | my ($w, $h) = $layout->size; |
|
|
2975 | |
|
|
2976 | $para->{w} = $w + $para->{indent}; |
|
|
2977 | $para->{h} = $h; |
|
|
2978 | $para->{wrapped} = $layout->has_wrapped; |
|
|
2979 | } |
|
|
2980 | |
|
|
2981 | $para->{y} = $height; |
|
|
2982 | $height += $para->{h}; |
|
|
2983 | } |
|
|
2984 | |
|
|
2985 | $self->{height} = $height; |
|
|
2986 | $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]); |
|
|
2987 | |
|
|
2988 | delete $self->{texture}; |
|
|
2989 | } |
|
|
2990 | |
|
|
2991 | if (my $paridx = delete $self->{scroll_to}) { |
|
|
2992 | $self->{children}[1]->set_value ($self->{par}[$paridx]{y}); |
|
|
2993 | } |
|
|
2994 | } |
|
|
2995 | |
2702 | sub update { |
2996 | sub update { |
2703 | my ($self) = @_; |
2997 | my ($self) = @_; |
2704 | |
2998 | |
2705 | $self->SUPER::update; |
2999 | $self->SUPER::update; |
2706 | |
3000 | |
2707 | return unless $self->{h} > 0; |
3001 | return unless $self->{h} > 0; |
2708 | |
3002 | |
2709 | delete $self->{texture}; |
3003 | delete $self->{texture}; |
2710 | |
3004 | |
2711 | $ROOT->on_post_alloc ($self => sub { |
3005 | $ROOT->on_post_alloc ($self => sub { |
|
|
3006 | $self->force_uptodate; |
|
|
3007 | |
2712 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
3008 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
2713 | |
3009 | |
2714 | if (delete $self->{need_reflow}) { |
|
|
2715 | my $height = 0; |
|
|
2716 | |
|
|
2717 | for my $para (@{$self->{par}}) { |
|
|
2718 | if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { |
|
|
2719 | my $layout = $self->get_layout ($para); |
|
|
2720 | my ($w, $h) = $layout->size; |
|
|
2721 | |
|
|
2722 | $para->{w} = $w + $para->{indent}; |
|
|
2723 | $para->{h} = $h; |
|
|
2724 | $para->{wrapped} = $layout->has_wrapped; |
|
|
2725 | } |
|
|
2726 | |
|
|
2727 | $height += $para->{h}; |
|
|
2728 | } |
|
|
2729 | |
|
|
2730 | $self->{height} = $height; |
|
|
2731 | |
|
|
2732 | $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]); |
|
|
2733 | |
|
|
2734 | delete $self->{texture}; |
|
|
2735 | } |
|
|
2736 | |
|
|
2737 | if (delete $self->{scroll_to_bottom}) { |
|
|
2738 | $self->{children}[1]->set_value (1e10); |
|
|
2739 | } |
|
|
2740 | |
|
|
2741 | $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { |
3010 | $self->{texture} ||= new_from_opengl CFPlus::Texture $W, $H, sub { |
2742 | glClearColor 0, 0, 0, 0; |
3011 | glClearColor 0, 0, 0, 0; |
2743 | glClear GL_COLOR_BUFFER_BIT; |
3012 | glClear GL_COLOR_BUFFER_BIT; |
2744 | |
3013 | |
|
|
3014 | package CFPlus::UI::Base; |
|
|
3015 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
|
|
3016 | (0, 0, $self->{w}, $self->{h}); |
|
|
3017 | |
|
|
3018 | my $top = int $self->{children}[1]{range}[0]; |
|
|
3019 | |
|
|
3020 | my $paridx = 0; |
|
|
3021 | my $top_paragraph; |
2745 | my $top = int $self->{children}[1]{range}[0]; |
3022 | my $top = int $self->{children}[1]{range}[0]; |
2746 | |
3023 | |
2747 | my $y0 = $top; |
3024 | my $y0 = $top; |
2748 | my $y1 = $top + $H; |
3025 | my $y1 = $top + $H; |
2749 | |
3026 | |
2750 | my $y = 0; |
|
|
2751 | |
|
|
2752 | for my $para (@{$self->{par}}) { |
3027 | for my $para (@{$self->{par}}) { |
2753 | my $h = $para->{h}; |
3028 | my $h = $para->{h}; |
|
|
3029 | my $y = $para->{y}; |
2754 | |
3030 | |
2755 | if ($y0 < $y + $h && $y < $y1) { |
3031 | if ($y0 < $y + $h && $y < $y1) { |
2756 | |
|
|
2757 | my $layout = $self->get_layout ($para); |
3032 | my $layout = $self->get_layout ($para); |
2758 | |
3033 | |
2759 | $layout->render ($para->{indent}, $y - $y0); |
3034 | $layout->render ($para->{indent}, $y - $y0); |
2760 | |
3035 | |
2761 | if (my @w = @{ $para->{widget} }) { |
3036 | if (my @w = @{ $para->{widget} }) { |
… | |
… | |
2770 | $_->draw; |
3045 | $_->draw; |
2771 | } |
3046 | } |
2772 | } |
3047 | } |
2773 | } |
3048 | } |
2774 | |
3049 | |
2775 | $y += $h; |
3050 | $paridx++; |
|
|
3051 | $top_paragraph ||= $paridx if $y >= $top; |
2776 | } |
3052 | } |
|
|
3053 | |
|
|
3054 | $self->{top_paragraph} = $top_paragraph; |
2777 | }; |
3055 | }; |
2778 | }); |
3056 | }); |
2779 | } |
3057 | } |
2780 | |
3058 | |
2781 | sub reconfigure { |
3059 | sub reconfigure { |
… | |
… | |
2799 | $self->{children}[1]->draw; |
3077 | $self->{children}[1]->draw; |
2800 | } |
3078 | } |
2801 | |
3079 | |
2802 | ############################################################################# |
3080 | ############################################################################# |
2803 | |
3081 | |
2804 | package CFClient::UI::Animator; |
3082 | package CFPlus::UI::Animator; |
2805 | |
3083 | |
2806 | use CFClient::OpenGL; |
3084 | use CFPlus::OpenGL; |
2807 | |
3085 | |
2808 | our @ISA = CFClient::UI::Bin::; |
3086 | our @ISA = CFPlus::UI::Bin::; |
2809 | |
3087 | |
2810 | sub moveto { |
3088 | sub moveto { |
2811 | my ($self, $x, $y) = @_; |
3089 | my ($self, $x, $y) = @_; |
2812 | |
3090 | |
2813 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
3091 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
2841 | glPopMatrix; |
3119 | glPopMatrix; |
2842 | } |
3120 | } |
2843 | |
3121 | |
2844 | ############################################################################# |
3122 | ############################################################################# |
2845 | |
3123 | |
2846 | package CFClient::UI::Flopper; |
3124 | package CFPlus::UI::Flopper; |
2847 | |
3125 | |
2848 | our @ISA = CFClient::UI::Button::; |
3126 | our @ISA = CFPlus::UI::Button::; |
2849 | |
3127 | |
2850 | sub new { |
3128 | sub new { |
2851 | my $class = shift; |
3129 | my $class = shift; |
2852 | |
3130 | |
2853 | my $self = $class->SUPER::new ( |
3131 | my $self = $class->SUPER::new ( |
… | |
… | |
2865 | $self->{other}->toggle_visibility; |
3143 | $self->{other}->toggle_visibility; |
2866 | } |
3144 | } |
2867 | |
3145 | |
2868 | ############################################################################# |
3146 | ############################################################################# |
2869 | |
3147 | |
2870 | package CFClient::UI::Tooltip; |
3148 | package CFPlus::UI::Tooltip; |
2871 | |
3149 | |
2872 | our @ISA = CFClient::UI::Bin::; |
3150 | our @ISA = CFPlus::UI::Bin::; |
2873 | |
3151 | |
2874 | use CFClient::OpenGL; |
3152 | use CFPlus::OpenGL; |
2875 | |
3153 | |
2876 | sub new { |
3154 | sub new { |
2877 | my $class = shift; |
3155 | my $class = shift; |
2878 | |
3156 | |
2879 | $class->SUPER::new ( |
3157 | $class->SUPER::new ( |
… | |
… | |
2882 | ) |
3160 | ) |
2883 | } |
3161 | } |
2884 | |
3162 | |
2885 | sub set_tooltip_from { |
3163 | sub set_tooltip_from { |
2886 | my ($self, $widget) = @_; |
3164 | my ($self, $widget) = @_; |
|
|
3165 | |
|
|
3166 | $widget->{tooltip} = CFPlus::Pod::section_label tooltip => $1 |
|
|
3167 | if $widget->{tooltip} =~ /^#(.*)$/; |
2887 | |
3168 | |
2888 | my $tooltip = $widget->{tooltip}; |
3169 | my $tooltip = $widget->{tooltip}; |
2889 | |
3170 | |
2890 | if ($ENV{CFPLUS_DEBUG} & 2) { |
3171 | if ($ENV{CFPLUS_DEBUG} & 2) { |
2891 | $tooltip .= "\n\n" . (ref $widget) . "\n" |
3172 | $tooltip .= "\n\n" . (ref $widget) . "\n" |
… | |
… | |
2895 | } |
3176 | } |
2896 | |
3177 | |
2897 | $tooltip =~ s/^\n+//; |
3178 | $tooltip =~ s/^\n+//; |
2898 | $tooltip =~ s/\n+$//; |
3179 | $tooltip =~ s/\n+$//; |
2899 | |
3180 | |
2900 | $self->add (new CFClient::UI::Label |
3181 | $self->add (new CFPlus::UI::Label |
2901 | markup => $tooltip, |
3182 | markup => $tooltip, |
2902 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
3183 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
2903 | fontsize => 0.8, |
3184 | fontsize => 0.8, |
2904 | fg => [0, 0, 0, 1], |
3185 | style => 1, # FLAG_INVERSE |
2905 | ellipsise => 0, |
3186 | ellipsise => 0, |
2906 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
3187 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
2907 | ); |
3188 | ); |
2908 | } |
3189 | } |
2909 | |
3190 | |
… | |
… | |
2928 | |
3209 | |
2929 | $self->{root}->on_post_alloc ("move_$self" => sub { |
3210 | $self->{root}->on_post_alloc ("move_$self" => sub { |
2930 | my $widget = $self->{owner} |
3211 | my $widget = $self->{owner} |
2931 | or return; |
3212 | or return; |
2932 | |
3213 | |
|
|
3214 | if ($widget->{visible}) { |
2933 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
3215 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
2934 | |
3216 | |
2935 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
3217 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
2936 | if $x + $self->{w} > $self->{root}{w}; |
3218 | if $x + $self->{w} > $self->{root}{w}; |
2937 | |
3219 | |
2938 | $self->move_abs ($x, $y); |
3220 | $self->move_abs ($x, $y); |
|
|
3221 | } else { |
|
|
3222 | $self->hide; |
|
|
3223 | } |
2939 | }); |
3224 | }); |
2940 | } |
3225 | } |
2941 | |
3226 | |
2942 | sub _draw { |
3227 | sub _draw { |
2943 | my ($self) = @_; |
3228 | my ($self) = @_; |
… | |
… | |
2967 | $self->SUPER::_draw; |
3252 | $self->SUPER::_draw; |
2968 | } |
3253 | } |
2969 | |
3254 | |
2970 | ############################################################################# |
3255 | ############################################################################# |
2971 | |
3256 | |
2972 | package CFClient::UI::Face; |
3257 | package CFPlus::UI::Face; |
2973 | |
3258 | |
2974 | our @ISA = CFClient::UI::Base::; |
3259 | our @ISA = CFPlus::UI::DrawBG::; |
2975 | |
3260 | |
2976 | use CFClient::OpenGL; |
3261 | use CFPlus::OpenGL; |
2977 | |
3262 | |
2978 | sub new { |
3263 | sub new { |
2979 | my $class = shift; |
3264 | my $class = shift; |
2980 | |
3265 | |
2981 | my $self = $class->SUPER::new ( |
3266 | my $self = $class->SUPER::new ( |
… | |
… | |
3016 | sub _draw { |
3301 | sub _draw { |
3017 | my ($self) = @_; |
3302 | my ($self) = @_; |
3018 | |
3303 | |
3019 | return unless $::CONN; |
3304 | return unless $::CONN; |
3020 | |
3305 | |
|
|
3306 | $self->SUPER::_draw; |
|
|
3307 | |
3021 | my $face; |
3308 | my $face; |
3022 | |
3309 | |
3023 | if ($self->{frame}) { |
3310 | if ($self->{frame}) { |
3024 | my $anim = $::CONN->{anim}[$self->{anim}]; |
3311 | my $anim = $::CONN->{anim}[$self->{anim}]; |
3025 | |
3312 | |
… | |
… | |
3047 | $self->SUPER::destroy; |
3334 | $self->SUPER::destroy; |
3048 | } |
3335 | } |
3049 | |
3336 | |
3050 | ############################################################################# |
3337 | ############################################################################# |
3051 | |
3338 | |
3052 | package CFClient::UI::Buttonbar; |
3339 | package CFPlus::UI::Buttonbar; |
3053 | |
3340 | |
3054 | our @ISA = CFClient::UI::HBox::; |
3341 | our @ISA = CFPlus::UI::HBox::; |
3055 | |
3342 | |
3056 | # TODO: should actualyl wrap buttons and other goodies. |
3343 | # TODO: should actually wrap buttons and other goodies. |
3057 | |
3344 | |
3058 | ############################################################################# |
3345 | ############################################################################# |
3059 | |
3346 | |
3060 | package CFClient::UI::Menu; |
3347 | package CFPlus::UI::Menu; |
3061 | |
3348 | |
3062 | our @ISA = CFClient::UI::FancyFrame::; |
3349 | our @ISA = CFPlus::UI::Toplevel::; |
3063 | |
3350 | |
3064 | use CFClient::OpenGL; |
3351 | use CFPlus::OpenGL; |
3065 | |
3352 | |
3066 | sub new { |
3353 | sub new { |
3067 | my $class = shift; |
3354 | my $class = shift; |
3068 | |
3355 | |
3069 | my $self = $class->SUPER::new ( |
3356 | my $self = $class->SUPER::new ( |
3070 | items => [], |
3357 | items => [], |
3071 | z => 100, |
3358 | z => 100, |
3072 | @_, |
3359 | @_, |
3073 | ); |
3360 | ); |
3074 | |
3361 | |
3075 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
3362 | $self->add ($self->{vbox} = new CFPlus::UI::VBox); |
3076 | |
3363 | |
3077 | for my $item (@{ $self->{items} }) { |
3364 | for my $item (@{ $self->{items} }) { |
3078 | my ($widget, $cb, $tooltip) = @$item; |
3365 | my ($widget, $cb, $tooltip) = @$item; |
3079 | |
3366 | |
3080 | # handle various types of items, only text for now |
3367 | # handle various types of items, only text for now |
3081 | if (!ref $widget) { |
3368 | if (!ref $widget) { |
3082 | if ($widget =~ /\t/) { |
3369 | if ($widget =~ /\t/) { |
3083 | my ($left, $right) = split /\t/, $widget, 2; |
3370 | my ($left, $right) = split /\t/, $widget, 2; |
3084 | |
3371 | |
3085 | $widget = new CFClient::UI::HBox |
3372 | $widget = new CFPlus::UI::HBox |
3086 | can_hover => 1, |
3373 | can_hover => 1, |
3087 | can_events => 1, |
3374 | can_events => 1, |
3088 | tooltip => $tooltip, |
3375 | tooltip => $tooltip, |
3089 | children => [ |
3376 | children => [ |
3090 | (new CFClient::UI::Label markup => $left, expand => 1), |
3377 | (new CFPlus::UI::Label markup => $left, expand => 1), |
3091 | (new CFClient::UI::Label markup => $right, align => +1), |
3378 | (new CFPlus::UI::Label markup => $right, align => +1), |
3092 | ], |
3379 | ], |
3093 | ; |
3380 | ; |
3094 | |
3381 | |
3095 | } else { |
3382 | } else { |
3096 | $widget = new CFClient::UI::Label |
3383 | $widget = new CFPlus::UI::Label |
3097 | can_hover => 1, |
3384 | can_hover => 1, |
3098 | can_events => 1, |
3385 | can_events => 1, |
3099 | markup => $widget, |
3386 | markup => $widget, |
3100 | tooltip => $tooltip; |
3387 | tooltip => $tooltip; |
3101 | } |
3388 | } |
… | |
… | |
3149 | 1 |
3436 | 1 |
3150 | } |
3437 | } |
3151 | |
3438 | |
3152 | ############################################################################# |
3439 | ############################################################################# |
3153 | |
3440 | |
3154 | package CFClient::UI::Multiplexer; |
3441 | package CFPlus::UI::Multiplexer; |
3155 | |
3442 | |
3156 | our @ISA = CFClient::UI::Container::; |
3443 | our @ISA = CFPlus::UI::Container::; |
3157 | |
3444 | |
3158 | sub new { |
3445 | sub new { |
3159 | my $class = shift; |
3446 | my $class = shift; |
3160 | |
3447 | |
3161 | my $self = $class->SUPER::new ( |
3448 | my $self = $class->SUPER::new ( |
… | |
… | |
3222 | $self->{current}->draw; |
3509 | $self->{current}->draw; |
3223 | } |
3510 | } |
3224 | |
3511 | |
3225 | ############################################################################# |
3512 | ############################################################################# |
3226 | |
3513 | |
3227 | package CFClient::UI::Notebook; |
3514 | package CFPlus::UI::Notebook; |
3228 | |
3515 | |
3229 | our @ISA = CFClient::UI::VBox::; |
3516 | our @ISA = CFPlus::UI::VBox::; |
3230 | |
3517 | |
3231 | sub new { |
3518 | sub new { |
3232 | my $class = shift; |
3519 | my $class = shift; |
3233 | |
3520 | |
3234 | my $self = $class->SUPER::new ( |
3521 | my $self = $class->SUPER::new ( |
3235 | buttonbar => (new CFClient::UI::Buttonbar), |
3522 | buttonbar => (new CFPlus::UI::Buttonbar), |
3236 | multiplexer => (new CFClient::UI::Multiplexer expand => 1), |
3523 | multiplexer => (new CFPlus::UI::Multiplexer expand => 1), |
3237 | # filter => # will be put between multiplexer and $self |
3524 | # filter => # will be put between multiplexer and $self |
3238 | @_, |
3525 | @_, |
3239 | ); |
3526 | ); |
3240 | |
3527 | |
3241 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
3528 | $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; |
… | |
… | |
3247 | sub add { |
3534 | sub add { |
3248 | my ($self, $title, $widget, $tooltip) = @_; |
3535 | my ($self, $title, $widget, $tooltip) = @_; |
3249 | |
3536 | |
3250 | Scalar::Util::weaken $self; |
3537 | Scalar::Util::weaken $self; |
3251 | |
3538 | |
3252 | $self->{buttonbar}->add (new CFClient::UI::Button |
3539 | $self->{buttonbar}->add (new CFPlus::UI::Button |
3253 | markup => $title, |
3540 | markup => $title, |
3254 | tooltip => $tooltip, |
3541 | tooltip => $tooltip, |
3255 | on_activate => sub { $self->set_current_page ($widget) }, |
3542 | on_activate => sub { $self->set_current_page ($widget) }, |
3256 | ); |
3543 | ); |
3257 | |
3544 | |
… | |
… | |
3271 | $self->emit (page_changed => $self->{multiplexer}{current}); |
3558 | $self->emit (page_changed => $self->{multiplexer}{current}); |
3272 | } |
3559 | } |
3273 | |
3560 | |
3274 | ############################################################################# |
3561 | ############################################################################# |
3275 | |
3562 | |
3276 | package CFClient::UI::Combobox; |
3563 | package CFPlus::UI::Selector; |
3277 | |
3564 | |
3278 | use utf8; |
3565 | use utf8; |
3279 | |
3566 | |
3280 | our @ISA = CFClient::UI::Button::; |
3567 | our @ISA = CFPlus::UI::Button::; |
3281 | |
3568 | |
3282 | sub new { |
3569 | sub new { |
3283 | my $class = shift; |
3570 | my $class = shift; |
3284 | |
3571 | |
3285 | my $self = $class->SUPER::new ( |
3572 | my $self = $class->SUPER::new ( |
… | |
… | |
3302 | my ($value, $title, $tooltip) = @$_; |
3589 | my ($value, $title, $tooltip) = @$_; |
3303 | |
3590 | |
3304 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
3591 | push @menu_items, [$tooltip || $title, sub { $self->set_value ($value) }]; |
3305 | } |
3592 | } |
3306 | |
3593 | |
3307 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
3594 | CFPlus::UI::Menu->new (items => \@menu_items)->popup ($ev); |
3308 | } |
3595 | } |
3309 | |
3596 | |
3310 | sub _set_value { |
3597 | sub _set_value { |
3311 | my ($self, $value) = @_; |
3598 | my ($self, $value) = @_; |
3312 | |
3599 | |
… | |
… | |
3327 | $self->emit (changed => $value); |
3614 | $self->emit (changed => $value); |
3328 | } |
3615 | } |
3329 | |
3616 | |
3330 | ############################################################################# |
3617 | ############################################################################# |
3331 | |
3618 | |
3332 | package CFClient::UI::Statusbox; |
3619 | package CFPlus::UI::Statusbox; |
3333 | |
3620 | |
3334 | our @ISA = CFClient::UI::VBox::; |
3621 | our @ISA = CFPlus::UI::VBox::; |
3335 | |
3622 | |
3336 | sub new { |
3623 | sub new { |
3337 | my $class = shift; |
3624 | my $class = shift; |
3338 | |
3625 | |
3339 | my $self = $class->SUPER::new ( |
3626 | my $self = $class->SUPER::new ( |
… | |
… | |
3351 | sub reorder { |
3638 | sub reorder { |
3352 | my ($self) = @_; |
3639 | my ($self) = @_; |
3353 | my $NOW = Time::HiRes::time; |
3640 | my $NOW = Time::HiRes::time; |
3354 | |
3641 | |
3355 | # freeze display when hovering over any label |
3642 | # freeze display when hovering over any label |
3356 | return if $CFClient::UI::TOOLTIP->{owner} |
3643 | return if $CFPlus::UI::TOOLTIP->{owner} |
3357 | && grep $CFClient::UI::TOOLTIP->{owner} == $_->{label}, |
3644 | && grep $CFPlus::UI::TOOLTIP->{owner} == $_->{label}, |
3358 | values %{ $self->{item} }; |
3645 | values %{ $self->{item} }; |
3359 | |
3646 | |
3360 | while (my ($k, $v) = each %{ $self->{item} }) { |
3647 | while (my ($k, $v) = each %{ $self->{item} }) { |
3361 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
3648 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
3362 | } |
3649 | } |
… | |
… | |
3383 | for ($short) { |
3670 | for ($short) { |
3384 | s/^\s+//; |
3671 | s/^\s+//; |
3385 | s/\s+/ /g; |
3672 | s/\s+/ /g; |
3386 | } |
3673 | } |
3387 | |
3674 | |
3388 | new CFClient::UI::Label |
3675 | new CFPlus::UI::Label |
3389 | markup => $short, |
3676 | markup => $short, |
3390 | tooltip => $item->{tooltip}, |
3677 | tooltip => $item->{tooltip}, |
3391 | tooltip_font => $::FONT_PROP, |
3678 | tooltip_font => $::FONT_PROP, |
3392 | tooltip_width => 0.67, |
3679 | tooltip_width => 0.67, |
3393 | fontsize => $item->{fontsize} || $self->{fontsize}, |
3680 | fontsize => $item->{fontsize} || $self->{fontsize}, |
… | |
… | |
3472 | $self->SUPER::destroy; |
3759 | $self->SUPER::destroy; |
3473 | } |
3760 | } |
3474 | |
3761 | |
3475 | ############################################################################# |
3762 | ############################################################################# |
3476 | |
3763 | |
3477 | package CFClient::UI::Inventory; |
|
|
3478 | |
|
|
3479 | our @ISA = CFClient::UI::Table::; |
|
|
3480 | |
|
|
3481 | sub new { |
|
|
3482 | my $class = shift; |
|
|
3483 | |
|
|
3484 | my $self = $class->SUPER::new ( |
|
|
3485 | col_expand => [0, 1, 0], |
|
|
3486 | @_, |
|
|
3487 | ); |
|
|
3488 | |
|
|
3489 | $self |
|
|
3490 | } |
|
|
3491 | |
|
|
3492 | sub set_items { |
|
|
3493 | my ($self, $items) = @_; |
|
|
3494 | |
|
|
3495 | $self->clear; |
|
|
3496 | return unless $items; |
|
|
3497 | |
|
|
3498 | my @items = sort { |
|
|
3499 | ($a->{type} <=> $b->{type}) |
|
|
3500 | or ($a->{name} cmp $b->{name}) |
|
|
3501 | } values %$items; |
|
|
3502 | |
|
|
3503 | $self->{real_items} = \@items; |
|
|
3504 | |
|
|
3505 | my $row = 0; |
|
|
3506 | for my $item (@items) { |
|
|
3507 | CFClient::Item::update_widgets $item; |
|
|
3508 | |
|
|
3509 | $self->add (0, $row, $item->{face_widget}); |
|
|
3510 | $self->add (1, $row, $item->{desc_widget}); |
|
|
3511 | $self->add (2, $row, $item->{weight_widget}); |
|
|
3512 | |
|
|
3513 | $row++; |
|
|
3514 | } |
|
|
3515 | } |
|
|
3516 | |
|
|
3517 | ############################################################################# |
|
|
3518 | |
|
|
3519 | package CFClient::UI::SpellList; |
|
|
3520 | |
|
|
3521 | our @ISA = CFClient::UI::Table::; |
|
|
3522 | |
|
|
3523 | sub new { |
|
|
3524 | my $class = shift; |
|
|
3525 | |
|
|
3526 | my $self = $class->SUPER::new ( |
|
|
3527 | binding => [], |
|
|
3528 | commands => [], |
|
|
3529 | @_, |
|
|
3530 | ) |
|
|
3531 | } |
|
|
3532 | |
|
|
3533 | my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>"; |
|
|
3534 | |
|
|
3535 | my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip => |
|
|
3536 | "<b>Name</b>. The name of the spell.$TOOLTIP_ALL"); |
|
|
3537 | my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip => |
|
|
3538 | "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL"); |
|
|
3539 | my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3540 | "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL"); |
|
|
3541 | my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3542 | "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL"); |
|
|
3543 | my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3544 | "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL"); |
|
|
3545 | |
|
|
3546 | sub rebuild_spell_list { |
|
|
3547 | my ($self) = @_; |
|
|
3548 | |
|
|
3549 | $CFClient::UI::ROOT->on_refresh ($self => sub { |
|
|
3550 | $self->clear; |
|
|
3551 | |
|
|
3552 | return unless $::CONN; |
|
|
3553 | |
|
|
3554 | $self->add (1, 0, new CFClient::UI::Label text => "Spell Name", @TOOLTIP_NAME); |
|
|
3555 | $self->add (2, 0, new CFClient::UI::Label text => "Skill", @TOOLTIP_SKILL); |
|
|
3556 | $self->add (3, 0, new CFClient::UI::Label text => "Lvl" , @TOOLTIP_LVL); |
|
|
3557 | $self->add (4, 0, new CFClient::UI::Label text => "Sp/Gp", @TOOLTIP_SP); |
|
|
3558 | $self->add (5, 0, new CFClient::UI::Label text => "Dmg" , @TOOLTIP_DMG); |
|
|
3559 | |
|
|
3560 | my $row = 0; |
|
|
3561 | |
|
|
3562 | for (sort { $a cmp $b } keys %{ $self->{spell} }) { |
|
|
3563 | my $spell = $self->{spell}{$_}; |
|
|
3564 | |
|
|
3565 | $row++; |
|
|
3566 | |
|
|
3567 | my $spell_cb = sub { |
|
|
3568 | my ($widget, $ev) = @_; |
|
|
3569 | |
|
|
3570 | if ($ev->{button} == 1) { |
|
|
3571 | $::CONN->user_send ("cast $spell->{name}"); |
|
|
3572 | } elsif ($ev->{button} == 2) { |
|
|
3573 | $::CONN->user_send ("invoke $spell->{name}"); |
|
|
3574 | } elsif ($ev->{button} == 3) { |
|
|
3575 | (new CFClient::UI::Menu |
|
|
3576 | items => [ |
|
|
3577 | ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], |
|
|
3578 | ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], |
|
|
3579 | ], |
|
|
3580 | )->popup ($ev); |
|
|
3581 | } else { |
|
|
3582 | return 0; |
|
|
3583 | } |
|
|
3584 | |
|
|
3585 | 1 |
|
|
3586 | }; |
|
|
3587 | |
|
|
3588 | my $tooltip = "$spell->{message}$TOOLTIP_ALL"; |
|
|
3589 | |
|
|
3590 | #TODO: add path info to tooltip |
|
|
3591 | #$self->add (6, $row, new CFClient::UI::Label text => $spell->{path}); |
|
|
3592 | |
|
|
3593 | $self->add (0, $row, new CFClient::UI::Face |
|
|
3594 | face => $spell->{face}, |
|
|
3595 | can_hover => 1, |
|
|
3596 | can_events => 1, |
|
|
3597 | tooltip => $tooltip, |
|
|
3598 | on_button_down => $spell_cb, |
|
|
3599 | ); |
|
|
3600 | |
|
|
3601 | $self->add (1, $row, new CFClient::UI::Label |
|
|
3602 | expand => 1, |
|
|
3603 | text => $spell->{name}, |
|
|
3604 | can_hover => 1, |
|
|
3605 | can_events => 1, |
|
|
3606 | tooltip => $tooltip, |
|
|
3607 | on_button_down => $spell_cb, |
|
|
3608 | ); |
|
|
3609 | |
|
|
3610 | $self->add (2, $row, new CFClient::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL); |
|
|
3611 | $self->add (3, $row, new CFClient::UI::Label text => $spell->{level}, @TOOLTIP_LVL); |
|
|
3612 | $self->add (4, $row, new CFClient::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP); |
|
|
3613 | $self->add (5, $row, new CFClient::UI::Label text => $spell->{damage}, @TOOLTIP_DMG); |
|
|
3614 | } |
|
|
3615 | }); |
|
|
3616 | } |
|
|
3617 | |
|
|
3618 | sub add_spell { |
|
|
3619 | my ($self, $spell) = @_; |
|
|
3620 | |
|
|
3621 | $self->{spell}->{$spell->{name}} = $spell; |
|
|
3622 | $self->rebuild_spell_list; |
|
|
3623 | } |
|
|
3624 | |
|
|
3625 | sub remove_spell { |
|
|
3626 | my ($self, $spell) = @_; |
|
|
3627 | |
|
|
3628 | delete $self->{spell}->{$spell->{name}}; |
|
|
3629 | $self->rebuild_spell_list; |
|
|
3630 | } |
|
|
3631 | |
|
|
3632 | sub clear_spells { |
|
|
3633 | my ($self) = @_; |
|
|
3634 | |
|
|
3635 | $self->{spell} = {}; |
|
|
3636 | $self->rebuild_spell_list; |
|
|
3637 | } |
|
|
3638 | |
|
|
3639 | ############################################################################# |
|
|
3640 | |
|
|
3641 | package CFClient::UI::Root; |
3764 | package CFPlus::UI::Root; |
3642 | |
3765 | |
3643 | our @ISA = CFClient::UI::Container::; |
3766 | our @ISA = CFPlus::UI::Container::; |
3644 | |
3767 | |
3645 | use List::Util qw(min max); |
3768 | use List::Util qw(min max); |
3646 | |
3769 | |
3647 | use CFClient::OpenGL; |
3770 | use CFPlus::OpenGL; |
3648 | |
3771 | |
3649 | sub new { |
3772 | sub new { |
3650 | my $class = shift; |
3773 | my $class = shift; |
3651 | |
3774 | |
3652 | my $self = $class->SUPER::new ( |
3775 | my $self = $class->SUPER::new ( |
… | |
… | |
3831 | $h = 0 if $h < 0; |
3954 | $h = 0 if $h < 0; |
3832 | |
3955 | |
3833 | $w = max $widget->{min_w}, $w; |
3956 | $w = max $widget->{min_w}, $w; |
3834 | $h = max $widget->{min_h}, $h; |
3957 | $h = max $widget->{min_h}, $h; |
3835 | |
3958 | |
|
|
3959 | # $w = min $self->{w} - $widget->{x}, $w if $self->{w}; |
|
|
3960 | # $h = min $self->{h} - $widget->{y}, $h if $self->{h}; |
|
|
3961 | |
3836 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
3962 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
3837 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
3963 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
3838 | |
3964 | |
3839 | $w = int $w + 0.5; |
3965 | $w = int $w + 0.5; |
3840 | $h = int $h + 0.5; |
3966 | $h = int $h + 0.5; |
… | |
… | |
3866 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
3992 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
3867 | glMatrixMode GL_MODELVIEW; |
3993 | glMatrixMode GL_MODELVIEW; |
3868 | glLoadIdentity; |
3994 | glLoadIdentity; |
3869 | |
3995 | |
3870 | { |
3996 | { |
3871 | package CFClient::UI::Base; |
3997 | package CFPlus::UI::Base; |
3872 | |
3998 | |
3873 | ($draw_x, $draw_y, $draw_w, $draw_h) = |
3999 | local ($draw_x, $draw_y, $draw_w, $draw_h) = |
3874 | (0, 0, $self->{w}, $self->{h}); |
4000 | (0, 0, $self->{w}, $self->{h}); |
3875 | } |
|
|
3876 | |
4001 | |
3877 | $self->_draw; |
4002 | $self->_draw; |
|
|
4003 | } |
3878 | } |
4004 | } |
3879 | |
4005 | |
3880 | ############################################################################# |
4006 | ############################################################################# |
3881 | |
4007 | |
3882 | package CFClient::UI; |
4008 | package CFPlus::UI; |
3883 | |
4009 | |
3884 | $ROOT = new CFClient::UI::Root; |
4010 | $ROOT = new CFPlus::UI::Root; |
3885 | $TOOLTIP = new CFClient::UI::Tooltip z => 900; |
4011 | $TOOLTIP = new CFPlus::UI::Tooltip z => 900; |
3886 | |
4012 | |
3887 | 1 |
4013 | 1 |
3888 | |
4014 | |