… | |
… | |
23 | |
23 | |
24 | my $tip = $widget->{tooltip}; |
24 | my $tip = $widget->{tooltip}; |
25 | |
25 | |
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
27 | |
27 | |
28 | $TOOLTIP->set_text ($widget->{tooltip}); |
28 | $TOOLTIP->set_markup ($widget->{tooltip}); |
29 | $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); |
29 | |
30 | $TOOLTIP->show; |
30 | $TOOLTIP->show; |
|
|
31 | |
|
|
32 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
|
|
33 | |
|
|
34 | if ($x + $TOOLTIP->{w} > $::WIDTH) { |
|
|
35 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); |
|
|
36 | } |
|
|
37 | |
|
|
38 | $TOOLTIP->move ($x, $y); |
31 | } |
39 | } |
32 | |
40 | |
33 | return; |
41 | return; |
34 | } |
42 | } |
35 | } |
43 | } |
… | |
… | |
39 | delete $TOOLTIP->{owner}; |
47 | delete $TOOLTIP->{owner}; |
40 | } |
48 | } |
41 | |
49 | |
42 | # class methods for events |
50 | # class methods for events |
43 | sub feed_sdl_key_down_event { |
51 | sub feed_sdl_key_down_event { |
44 | $FOCUS->key_down ($_[0]) if $FOCUS; |
52 | $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) |
|
|
53 | if $FOCUS; |
45 | } |
54 | } |
46 | |
55 | |
47 | sub feed_sdl_key_up_event { |
56 | sub feed_sdl_key_up_event { |
48 | $FOCUS->key_up ($_[0]) if $FOCUS; |
57 | $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) |
|
|
58 | if $FOCUS; |
49 | } |
59 | } |
50 | |
60 | |
51 | sub feed_sdl_button_down_event { |
61 | sub feed_sdl_button_down_event { |
52 | my ($ev) = @_; |
62 | my ($ev) = @_; |
53 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
63 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
… | |
… | |
61 | check_tooltip; |
71 | check_tooltip; |
62 | } |
72 | } |
63 | |
73 | |
64 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
74 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
65 | |
75 | |
66 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
76 | if ($GRAB) { |
|
|
77 | ($x, $y) = $GRAB->coord2local ($x, $y); |
|
|
78 | $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y); |
|
|
79 | } |
67 | } |
80 | } |
68 | |
81 | |
69 | sub feed_sdl_button_up_event { |
82 | sub feed_sdl_button_up_event { |
70 | my ($ev) = @_; |
83 | my ($ev) = @_; |
71 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
84 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
72 | |
85 | |
73 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
86 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
74 | |
87 | |
75 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
88 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
76 | |
89 | |
77 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
90 | if ($GRAB) { |
|
|
91 | ($x, $y) = $GRAB->coord2local ($x, $y); |
|
|
92 | $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y); |
|
|
93 | } |
78 | |
94 | |
79 | if (!$BUTTON_STATE) { |
95 | if (!$BUTTON_STATE) { |
80 | my $grab = $GRAB; undef $GRAB; |
96 | my $grab = $GRAB; undef $GRAB; |
81 | $grab->update if $grab; |
97 | $grab->update if $grab; |
82 | $GRAB->update if $GRAB; |
98 | $GRAB->update if $GRAB; |
… | |
… | |
98 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
114 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
99 | |
115 | |
100 | check_tooltip; |
116 | check_tooltip; |
101 | } |
117 | } |
102 | |
118 | |
103 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
119 | if ($HOVER) { |
|
|
120 | ($x, $y) = $HOVER->coord2local ($x, $y); |
|
|
121 | $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); |
|
|
122 | } |
104 | } |
123 | } |
105 | |
124 | |
106 | # convert position array to integers |
125 | # convert position array to integers |
107 | sub harmonize { |
126 | sub harmonize { |
108 | my ($vals) = @_; |
127 | my ($vals) = @_; |
… | |
… | |
126 | |
145 | |
127 | sub new { |
146 | sub new { |
128 | my $class = shift; |
147 | my $class = shift; |
129 | |
148 | |
130 | my $self = bless { |
149 | my $self = bless { |
131 | x => 0, |
150 | x => 0, |
132 | y => 0, |
151 | y => 0, |
133 | z => 0, |
152 | z => 0, |
134 | can_events => 1, |
153 | can_events => 1, |
135 | @_ |
154 | @_ |
136 | }, $class; |
155 | }, $class; |
137 | |
156 | |
138 | for (keys %$self) { |
157 | for (keys %$self) { |
… | |
… | |
160 | } |
179 | } |
161 | |
180 | |
162 | sub hide { |
181 | sub hide { |
163 | my ($self) = @_; |
182 | my ($self) = @_; |
164 | |
183 | |
165 | return unless $self->{parent}; |
184 | undef $GRAB if $GRAB == $self; |
|
|
185 | undef $HOVER if $HOVER == $self; |
166 | |
186 | |
167 | $self->{parent}->remove ($self); |
187 | $self->{parent}->remove ($self) |
|
|
188 | if $self->{parent}; |
168 | } |
189 | } |
169 | |
190 | |
170 | sub move { |
191 | sub move { |
171 | my ($self, $x, $y, $z) = @_; |
192 | my ($self, $x, $y, $z) = @_; |
172 | |
193 | |
… | |
… | |
216 | $self->{w} = $w; |
237 | $self->{w} = $w; |
217 | $self->{h} = $h; |
238 | $self->{h} = $h; |
218 | |
239 | |
219 | $self->size_allocate ($w, $h); |
240 | $self->size_allocate ($w, $h); |
220 | $self->update; |
241 | $self->update; |
|
|
242 | $self->emit (size_allocate => $w, $h); |
221 | } |
243 | } |
222 | } |
244 | } |
223 | |
245 | |
224 | sub size_allocate { |
246 | sub size_allocate { |
225 | # nothing to be done |
247 | # nothing to be done |
|
|
248 | } |
|
|
249 | |
|
|
250 | sub children { |
|
|
251 | } |
|
|
252 | |
|
|
253 | # call when resolution changes etc. |
|
|
254 | sub reconfigure { |
|
|
255 | my ($self) = @_; |
|
|
256 | |
|
|
257 | $_->reconfigure |
|
|
258 | for $self->children; |
|
|
259 | |
|
|
260 | $self->check_size; |
|
|
261 | $self->size_allocate ($self->{w}, $self->{h}); |
|
|
262 | $self->update; |
226 | } |
263 | } |
227 | |
264 | |
228 | sub set_max_size { |
265 | sub set_max_size { |
229 | my ($self, $w, $h) = @_; |
266 | my ($self, $w, $h) = @_; |
230 | |
267 | |
… | |
… | |
233 | } |
270 | } |
234 | |
271 | |
235 | # return top left coordinates |
272 | # return top left coordinates |
236 | sub _topleft { |
273 | sub _topleft { |
237 | my ($self, $x, $y) = @_; |
274 | my ($self, $x, $y) = @_; |
|
|
275 | |
|
|
276 | $self->{parent} |
|
|
277 | or Carp::confess "no parent widget in _topleft\n";#d# |
238 | |
278 | |
239 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
279 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
240 | } |
280 | } |
241 | |
281 | |
242 | # translate global coordinates to local coordinate system |
282 | # translate global coordinates to local coordinate system |
… | |
… | |
320 | glVertex $x + $self->{w}, $y + $self->{h}; |
360 | glVertex $x + $self->{w}, $y + $self->{h}; |
321 | glVertex $x , $y + $self->{h}; |
361 | glVertex $x , $y + $self->{h}; |
322 | glEnd; |
362 | glEnd; |
323 | glDisable GL_BLEND; |
363 | glDisable GL_BLEND; |
324 | } |
364 | } |
|
|
365 | |
|
|
366 | if ($ENV{PCLIENT_DEBUG}) { |
|
|
367 | glPushMatrix; |
|
|
368 | glColor 1, 1, 0, 1; |
|
|
369 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
|
|
370 | glBegin GL_LINE_LOOP; |
|
|
371 | glVertex 0 , 0; |
|
|
372 | glVertex $self->{w}, 0; |
|
|
373 | glVertex $self->{w}, $self->{h}; |
|
|
374 | glVertex 0 , $self->{h}; |
|
|
375 | glEnd; |
|
|
376 | glPopMatrix; |
|
|
377 | CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
|
|
378 | } |
325 | } |
379 | } |
326 | |
380 | |
327 | sub _draw { |
381 | sub _draw { |
328 | my ($self) = @_; |
382 | my ($self) = @_; |
329 | |
383 | |
… | |
… | |
388 | } |
442 | } |
389 | |
443 | |
390 | sub emit { |
444 | sub emit { |
391 | my ($self, $signal, @args) = @_; |
445 | my ($self, $signal, @args) = @_; |
392 | |
446 | |
393 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
447 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
394 | $cb->($self, @args); |
|
|
395 | } |
|
|
396 | } |
448 | } |
397 | |
449 | |
398 | sub DESTROY { |
450 | sub DESTROY { |
399 | my ($self) = @_; |
451 | my ($self) = @_; |
400 | |
452 | |
… | |
… | |
467 | sub new { |
519 | sub new { |
468 | my ($class, %arg) = @_; |
520 | my ($class, %arg) = @_; |
469 | |
521 | |
470 | my $children = delete $arg{children} || []; |
522 | my $children = delete $arg{children} || []; |
471 | |
523 | |
472 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
524 | my $self = $class->SUPER::new ( |
|
|
525 | children => [], |
|
|
526 | can_events => 0, |
|
|
527 | %arg, |
|
|
528 | ); |
473 | $self->add ($_) for @$children; |
529 | $self->add ($_) for @$children; |
474 | |
530 | |
475 | $self |
531 | $self |
476 | } |
532 | } |
477 | |
533 | |
… | |
… | |
486 | sort { $a->{z} <=> $b->{z} } |
542 | sort { $a->{z} <=> $b->{z} } |
487 | @{$self->{children}}, $child |
543 | @{$self->{children}}, $child |
488 | ]; |
544 | ]; |
489 | |
545 | |
490 | $child->check_size; |
546 | $child->check_size; |
|
|
547 | $self->update; |
|
|
548 | } |
|
|
549 | |
|
|
550 | sub children { |
|
|
551 | @{ $_[0]{children} } |
491 | } |
552 | } |
492 | |
553 | |
493 | sub remove { |
554 | sub remove { |
494 | my ($self, $child) = @_; |
555 | my ($self, $child) = @_; |
495 | |
556 | |
496 | delete $child->{parent}; |
557 | delete $child->{parent}; |
|
|
558 | $child->hide; |
497 | |
559 | |
498 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
560 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
|
|
561 | |
|
|
562 | $self->check_size; |
|
|
563 | $self->update; |
|
|
564 | } |
|
|
565 | |
|
|
566 | sub clear { |
|
|
567 | my ($self) = @_; |
|
|
568 | |
|
|
569 | my $children = delete $self->{children}; |
|
|
570 | $self->{children} = []; |
|
|
571 | |
|
|
572 | for (@$children) { |
|
|
573 | delete $_->{parent}; |
|
|
574 | $_->hide; |
|
|
575 | } |
499 | |
576 | |
500 | $self->check_size; |
577 | $self->check_size; |
501 | $self->update; |
578 | $self->update; |
502 | } |
579 | } |
503 | |
580 | |
… | |
… | |
581 | } |
658 | } |
582 | |
659 | |
583 | sub update { |
660 | sub update { |
584 | my ($self) = @_; |
661 | my ($self) = @_; |
585 | |
662 | |
586 | # we want to do this delayed... |
663 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
587 | $self->render_chld; |
|
|
588 | $self->SUPER::update; |
664 | $self->SUPER::update; |
589 | } |
|
|
590 | |
|
|
591 | sub render_chld { |
|
|
592 | my ($self) = @_; |
|
|
593 | |
|
|
594 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
|
|
595 | glClearColor 0, 0, 0, 1; |
|
|
596 | glClear GL_COLOR_BUFFER_BIT; |
|
|
597 | $self->child->draw; |
|
|
598 | }; |
|
|
599 | } |
665 | } |
600 | |
666 | |
601 | sub size_allocate { |
667 | sub size_allocate { |
602 | my ($self, $w, $h) = @_; |
668 | my ($self, $w, $h) = @_; |
603 | |
669 | |
604 | $self->child->configure (0, 0, $w, $h); |
670 | $self->SUPER::size_allocate ($w, $h); |
|
|
671 | $self->update; |
|
|
672 | } |
605 | |
673 | |
|
|
674 | sub _render { |
|
|
675 | $_[0]{children}[0]->draw; |
|
|
676 | } |
|
|
677 | |
|
|
678 | sub render_child { |
|
|
679 | my ($self) = @_; |
|
|
680 | |
|
|
681 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
|
|
682 | glClearColor 0, 0, 0, 0; |
|
|
683 | glClear GL_COLOR_BUFFER_BIT; |
|
|
684 | |
606 | $self->render_chld; |
685 | $self->_render; |
|
|
686 | # glColorMask 1, 1, 1, 0; |
|
|
687 | # glEnable GL_BLEND; |
|
|
688 | # glBlendFunc GL_SRC_ALPHA, GL_ZERO; |
|
|
689 | # glRasterPos 0, 0; |
|
|
690 | # glCopyPixels 0, 0, $self->{w}, $self->{h}; |
|
|
691 | # glDisable GL_BLEND; |
|
|
692 | # glColorMask 1, 1, 1, 1; |
|
|
693 | }; |
607 | } |
694 | } |
608 | |
695 | |
609 | sub _draw { |
696 | sub _draw { |
610 | my ($self) = @_; |
697 | my ($self) = @_; |
611 | |
698 | |
… | |
… | |
613 | |
700 | |
614 | my $tex = $self->{texture} |
701 | my $tex = $self->{texture} |
615 | or return; |
702 | or return; |
616 | |
703 | |
617 | glEnable GL_BLEND; |
704 | glEnable GL_BLEND; |
618 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
705 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
619 | glEnable GL_TEXTURE_2D; |
706 | glEnable GL_TEXTURE_2D; |
620 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
707 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
708 | glColor 0, 0, 0, 1; |
621 | |
709 | |
622 | $tex->draw_quad (0, 0, $w, $h); |
710 | $tex->draw_quad (0, 0, $w, $h); |
623 | |
711 | |
624 | glDisable GL_BLEND; |
712 | glDisable GL_BLEND; |
625 | glDisable GL_TEXTURE_2D; |
713 | glDisable GL_TEXTURE_2D; |
… | |
… | |
629 | |
717 | |
630 | package CFClient::UI::ViewPort; |
718 | package CFClient::UI::ViewPort; |
631 | |
719 | |
632 | our @ISA = CFClient::UI::Window::; |
720 | our @ISA = CFClient::UI::Window::; |
633 | |
721 | |
634 | sub new { die } |
|
|
635 | |
|
|
636 | sub size_request { |
722 | sub size_request { |
637 | my ($self) = @_; |
723 | my ($self) = @_; |
638 | |
724 | |
639 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
725 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
640 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
726 | $self->child->configure (0, 0, @$self{qw(child_w child_h)}); |
641 | |
727 | |
642 | @$self{qw(child_w child_h)} |
728 | @$self{qw(child_w child_h)} |
643 | } |
729 | } |
644 | |
730 | |
645 | sub _draw { |
731 | sub size_allocate { |
646 | my ($self) = @_; |
732 | my ($self, $w, $h) = @_; |
647 | |
733 | |
648 | $self->{children}[1]->draw; |
734 | $self->update; |
649 | } |
735 | } |
650 | |
736 | |
|
|
737 | sub set_offset { |
|
|
738 | my ($self, $x, $y) = @_; |
|
|
739 | |
|
|
740 | $self->{view_x} = int $x; |
|
|
741 | $self->{view_y} = int $y; |
|
|
742 | |
|
|
743 | $self->update; |
|
|
744 | } |
|
|
745 | |
|
|
746 | # hmm, this does not work for topleft of $self... but we should not aks for that |
|
|
747 | sub _topleft { |
|
|
748 | my ($self, $x, $y) = @_; |
|
|
749 | |
|
|
750 | $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) |
|
|
751 | } |
|
|
752 | |
|
|
753 | sub find_widget { |
|
|
754 | my ($self, $x, $y) = @_; |
|
|
755 | |
|
|
756 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
757 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
|
|
758 | ) { |
|
|
759 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
760 | } else { |
|
|
761 | $self->CFClient::UI::Base::find_widget ($x, $y) |
|
|
762 | } |
|
|
763 | } |
|
|
764 | |
|
|
765 | sub _render { |
|
|
766 | my ($self) = @_; |
|
|
767 | |
|
|
768 | CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y}; |
|
|
769 | |
|
|
770 | $self->SUPER::_render; |
|
|
771 | } |
|
|
772 | |
|
|
773 | ############################################################################# |
|
|
774 | |
|
|
775 | package CFClient::UI::ScrolledWindow; |
|
|
776 | |
|
|
777 | our @ISA = CFClient::UI::HBox::; |
|
|
778 | |
|
|
779 | sub new { |
|
|
780 | my $class = shift; |
|
|
781 | |
|
|
782 | my $self; |
|
|
783 | |
|
|
784 | my $slider = new CFClient::UI::Slider |
|
|
785 | vertical => 1, |
|
|
786 | range => [0, 0, 1, 0.01], # HACK fix |
|
|
787 | connect_changed => sub { |
|
|
788 | $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); |
|
|
789 | }, |
|
|
790 | ; |
|
|
791 | |
|
|
792 | $self = $class->SUPER::new ( |
|
|
793 | vp => (new CFClient::UI::ViewPort), |
|
|
794 | slider => $slider, |
|
|
795 | @_, |
|
|
796 | ); |
|
|
797 | |
|
|
798 | $self->{vp}->add ($self->{scrolled}); |
|
|
799 | $self->add ($self->{vp}); |
|
|
800 | $self->add ($self->{slider}); |
|
|
801 | |
|
|
802 | $self |
|
|
803 | } |
|
|
804 | |
|
|
805 | #TODO# update range on size_allocate depeneing on child |
|
|
806 | # update viewport offset on scroll |
651 | |
807 | |
652 | ############################################################################# |
808 | ############################################################################# |
653 | |
809 | |
654 | package CFClient::UI::Frame; |
810 | package CFClient::UI::Frame; |
655 | |
811 | |
… | |
… | |
710 | my $class = shift; |
866 | my $class = shift; |
711 | |
867 | |
712 | # TODO: user_x, user_y, overwrite moveto? |
868 | # TODO: user_x, user_y, overwrite moveto? |
713 | |
869 | |
714 | my $self = $class->SUPER::new ( |
870 | my $self = $class->SUPER::new ( |
715 | bg => [1, 1, 1, 1], |
871 | bg => [1, 1, 1, 1], |
716 | border_bg => [1, 1, 1, 1], |
872 | border_bg => [1, 1, 1, 1], |
717 | border => 0.8, |
873 | border => 0.6, |
718 | can_events => 1, |
874 | can_events => 1, |
719 | @_ |
875 | @_ |
720 | ); |
876 | ); |
721 | |
877 | |
722 | $self->{title} &&= new CFClient::UI::Label |
878 | $self->{title} &&= new CFClient::UI::Label |
723 | align => 0, |
879 | align => 0, |
724 | valign => 1, |
880 | valign => 1, |
725 | text => $self->{title}, |
881 | text => $self->{title}, |
726 | fontsize => 1; |
882 | fontsize => $self->{border}; |
727 | |
883 | |
728 | $self |
884 | $self |
729 | } |
885 | } |
730 | |
886 | |
731 | sub border { |
887 | sub border { |
… | |
… | |
756 | } |
912 | } |
757 | |
913 | |
758 | sub button_down { |
914 | sub button_down { |
759 | my ($self, $ev, $x, $y) = @_; |
915 | my ($self, $ev, $x, $y) = @_; |
760 | |
916 | |
|
|
917 | my ($w, $h) = @$self{qw(w h)}; |
761 | my $border = $self->border; |
918 | my $border = $self->border; |
762 | |
919 | |
763 | if ($x < $self->{w} && $x >= $self->{w} - $border |
920 | my $lr = ($x >= 0 && $x < $border) || ($x > $w - $border && $x < $w); |
764 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
921 | my $td = ($y >= 0 && $y < $border) || ($y > $h - $border && $y < $h); |
765 | |
922 | |
|
|
923 | if ($lr & $td) { |
|
|
924 | my ($wx, $wy) = ($self->{x}, $self->{y}); |
766 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
925 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
767 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
926 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
768 | |
927 | |
|
|
928 | my $mx = $x < $border; |
|
|
929 | my $my = $y < $border; |
|
|
930 | |
769 | $self->{motion} = sub { |
931 | $self->{motion} = sub { |
770 | my ($ev, $x, $y) = @_; |
932 | my ($ev, $x, $y) = @_; |
771 | |
933 | |
772 | ($x, $y) = ($ev->{x}, $ev->{y}); |
934 | my $dx = $ev->{x} - $ox; |
|
|
935 | my $dy = $ev->{y} - $oy; |
773 | |
936 | |
774 | $self->{user_w} = $bw + $x - $ox; |
937 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
775 | $self->{user_h} = $bh + $y - $oy; |
938 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
|
|
939 | $self->move ($wx + $dx * $mx, $wy + $dy * $my); |
776 | $self->check_size; |
940 | $self->check_size; |
777 | }; |
941 | }; |
778 | |
942 | |
779 | } elsif ($x >= 0 && $x < $self->{w} |
943 | } elsif ($lr ^ $td) { |
780 | && $y >= 0 && $y < $border) { |
|
|
781 | |
|
|
782 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
944 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
783 | my ($bx, $by) = ($self->{x}, $self->{y}); |
945 | my ($bx, $by) = ($self->{x}, $self->{y}); |
784 | |
946 | |
785 | $self->{motion} = sub { |
947 | $self->{motion} = sub { |
786 | my ($ev, $x, $y) = @_; |
948 | my ($ev, $x, $y) = @_; |
… | |
… | |
822 | $tex[1]->draw_quad (0, 0, $w, $border); |
984 | $tex[1]->draw_quad (0, 0, $w, $border); |
823 | $tex[3]->draw_quad (0, $border, $border, $ch); |
985 | $tex[3]->draw_quad (0, $border, $border, $ch); |
824 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
986 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
825 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
987 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
826 | |
988 | |
|
|
989 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
827 | my $bg = $tex[0]; |
990 | my $bg = $tex[0]; |
828 | |
991 | |
829 | # TODO: repeat texture not scale |
992 | # TODO: repeat texture not scale |
830 | my $rep_x = $cw / $bg->{w}; |
993 | my $rep_x = $cw / $bg->{w}; |
831 | my $rep_y = $ch / $bg->{h}; |
994 | my $rep_y = $ch / $bg->{h}; |
832 | |
995 | |
833 | glColor @{ $self->{bg} }; |
996 | glColor @{ $self->{bg} }; |
834 | |
997 | |
835 | $bg->{s} = $rep_x; |
998 | $bg->{s} = $rep_x; |
836 | $bg->{t} = $rep_y; |
999 | $bg->{t} = $rep_y; |
837 | $bg->{wrap_mode} = 1; |
1000 | $bg->{wrap_mode} = 1; |
838 | $bg->draw_quad ($border, $border, $cw, $ch); |
1001 | $bg->draw_quad ($border, $border, $cw, $ch); |
839 | |
1002 | |
840 | glDisable GL_TEXTURE_2D; |
1003 | glDisable GL_TEXTURE_2D; |
841 | glDisable GL_BLEND; |
1004 | glDisable GL_BLEND; |
|
|
1005 | } |
842 | |
1006 | |
843 | $self->{title}->draw if $self->{title}; |
1007 | $self->{title}->draw if $self->{title}; |
|
|
1008 | |
844 | $self->child->draw; |
1009 | $self->child->draw; |
845 | } |
1010 | } |
846 | |
1011 | |
847 | ############################################################################# |
1012 | ############################################################################# |
848 | |
1013 | |
… | |
… | |
870 | $self->{children}[$y][$x] = $child; |
1035 | $self->{children}[$y][$x] = $child; |
871 | |
1036 | |
872 | $child->check_size; |
1037 | $child->check_size; |
873 | } |
1038 | } |
874 | |
1039 | |
|
|
1040 | sub children { |
|
|
1041 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1042 | } |
|
|
1043 | |
875 | # TODO: move to container class maybe? send childs a signal on removal? |
1044 | # TODO: move to container class maybe? send childs a signal on removal? |
876 | sub clear { |
1045 | sub clear { |
877 | my ($self) = @_; |
1046 | my ($self) = @_; |
878 | |
1047 | |
|
|
1048 | my @children = $self->children; |
879 | delete $self->{children}; |
1049 | delete $self->{children}; |
|
|
1050 | |
|
|
1051 | for (@children) { |
|
|
1052 | delete $_->{parent}; |
|
|
1053 | $_->hide; |
|
|
1054 | } |
|
|
1055 | |
880 | $self->update; |
1056 | $self->update; |
881 | } |
1057 | } |
882 | |
1058 | |
883 | sub get_wh { |
1059 | sub get_wh { |
884 | my ($self) = @_; |
1060 | my ($self) = @_; |
… | |
… | |
1107 | |
1283 | |
1108 | sub new { |
1284 | sub new { |
1109 | my ($class, %arg) = @_; |
1285 | my ($class, %arg) = @_; |
1110 | |
1286 | |
1111 | my $self = $class->SUPER::new ( |
1287 | my $self = $class->SUPER::new ( |
1112 | fg => [1, 1, 1], |
1288 | fg => [1, 1, 1], |
1113 | #font => default_font |
1289 | #font => default_font |
1114 | fontsize => 1, |
1290 | fontsize => 1, |
1115 | text => "", |
1291 | text => "", |
1116 | align => -1, |
1292 | align => -1, |
1117 | valign => -1, |
1293 | valign => -1, |
1118 | padding => 2, |
1294 | padding => 2, |
1119 | layout => new CFClient::Layout, |
1295 | layout => new CFClient::Layout, |
1120 | can_events => 0, |
1296 | can_events => 0, |
1121 | %arg |
1297 | %arg |
1122 | ); |
1298 | ); |
1123 | |
1299 | |
1124 | if (exists $self->{template}) { |
1300 | if (exists $self->{template}) { |
… | |
… | |
1131 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1307 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1132 | |
1308 | |
1133 | $self |
1309 | $self |
1134 | } |
1310 | } |
1135 | |
1311 | |
1136 | sub escape_text { |
1312 | sub escape { |
1137 | local $_ = $_[1]; |
1313 | local $_ = $_[1]; |
1138 | |
1314 | |
1139 | s/&/&/g; |
1315 | s/&/&/g; |
1140 | s/>/>/g; |
1316 | s/>/>/g; |
1141 | s/</</g; |
1317 | s/</</g; |
1142 | |
1318 | |
1143 | $_[1] |
1319 | $_[1] |
1144 | } |
1320 | } |
1145 | |
1321 | |
|
|
1322 | sub update { |
|
|
1323 | my ($self) = @_; |
|
|
1324 | |
|
|
1325 | delete $self->{texture}; |
|
|
1326 | $self->SUPER::update; |
|
|
1327 | } |
|
|
1328 | |
1146 | sub set_text { |
1329 | sub set_text { |
1147 | my ($self, $text) = @_; |
1330 | my ($self, $text) = @_; |
1148 | |
1331 | |
|
|
1332 | return if $self->{text} eq "T$text"; |
|
|
1333 | $self->{text} = "T$text"; |
|
|
1334 | |
1149 | $self->{layout}->set_text ($text); |
1335 | $self->{layout}->set_text ($text); |
1150 | |
1336 | |
1151 | delete $self->{texture}; |
1337 | delete $self->{texture}; |
|
|
1338 | $self->update; |
1152 | $self->check_size; |
1339 | $self->check_size; |
1153 | $self->update; |
|
|
1154 | } |
1340 | } |
1155 | |
1341 | |
1156 | sub set_markup { |
1342 | sub set_markup { |
1157 | my ($self, $markup) = @_; |
1343 | my ($self, $markup) = @_; |
1158 | |
1344 | |
|
|
1345 | return if $self->{text} eq "M$markup"; |
|
|
1346 | $self->{text} = "M$markup"; |
|
|
1347 | |
1159 | $self->{layout}->set_markup ($markup); |
1348 | $self->{layout}->set_markup ($markup); |
1160 | |
1349 | |
1161 | delete $self->{texture}; |
1350 | delete $self->{texture}; |
|
|
1351 | $self->update; |
1162 | $self->check_size; |
1352 | $self->check_size; |
1163 | $self->update; |
|
|
1164 | } |
1353 | } |
1165 | |
1354 | |
1166 | sub size_request { |
1355 | sub size_request { |
1167 | my ($self) = @_; |
1356 | my ($self) = @_; |
1168 | |
1357 | |
… | |
… | |
1197 | sub set_fontsize { |
1386 | sub set_fontsize { |
1198 | my ($self, $fontsize) = @_; |
1387 | my ($self, $fontsize) = @_; |
1199 | |
1388 | |
1200 | $self->{fontsize} = $fontsize; |
1389 | $self->{fontsize} = $fontsize; |
1201 | delete $self->{texture}; |
1390 | delete $self->{texture}; |
|
|
1391 | |
|
|
1392 | $self->update; |
1202 | $self->check_size; |
1393 | $self->check_size; |
1203 | $self->update; |
|
|
1204 | } |
1394 | } |
1205 | |
1395 | |
1206 | sub _draw { |
1396 | sub _draw { |
1207 | my ($self) = @_; |
1397 | my ($self) = @_; |
1208 | |
1398 | |
… | |
… | |
1248 | |
1438 | |
1249 | sub new { |
1439 | sub new { |
1250 | my $class = shift; |
1440 | my $class = shift; |
1251 | |
1441 | |
1252 | $class->SUPER::new ( |
1442 | $class->SUPER::new ( |
1253 | fg => [1, 1, 1], |
1443 | fg => [1, 1, 1], |
1254 | bg => [0, 0, 0, 0.2], |
1444 | bg => [0, 0, 0, 0.2], |
1255 | active_bg => [1, 1, 1, 0.5], |
1445 | active_bg => [1, 1, 1, 0.5], |
1256 | active_fg => [0, 0, 0], |
1446 | active_fg => [0, 0, 0], |
1257 | can_hover => 1, |
1447 | can_hover => 1, |
1258 | can_focus => 1, |
1448 | can_focus => 1, |
1259 | valign => 0, |
1449 | valign => 0, |
1260 | can_events => 1, |
1450 | can_events => 1, |
1261 | @_ |
1451 | @_ |
1262 | ) |
1452 | ) |
1263 | } |
1453 | } |
1264 | |
1454 | |
… | |
… | |
1418 | my ($self, $ev) = @_; |
1608 | my ($self, $ev) = @_; |
1419 | |
1609 | |
1420 | my $sym = $ev->{sym}; |
1610 | my $sym = $ev->{sym}; |
1421 | |
1611 | |
1422 | if ($sym == 13) { |
1612 | if ($sym == 13) { |
|
|
1613 | unshift @{$self->{history}}, |
|
|
1614 | my $txt = $self->get_text; |
|
|
1615 | $self->{history_pointer} = -1; |
|
|
1616 | $self->{history_saveback} = ''; |
1423 | $self->emit (activate => $self->get_text); |
1617 | $self->emit (activate => $txt); |
1424 | $self->update; |
1618 | $self->update; |
|
|
1619 | |
|
|
1620 | } elsif ($sym == CFClient::SDLK_UP) { |
|
|
1621 | if ($self->{history_pointer} < 0) { |
|
|
1622 | $self->{history_saveback} = $self->get_text; |
|
|
1623 | } |
|
|
1624 | if (@{$self->{history} || []} > 0) { |
|
|
1625 | $self->{history_pointer}++; |
|
|
1626 | if ($self->{history_pointer} >= @{$self->{history} || []}) { |
|
|
1627 | $self->{history_pointer} = @{$self->{history} || []} - 1; |
|
|
1628 | } |
|
|
1629 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
|
|
1630 | } |
|
|
1631 | |
|
|
1632 | } elsif ($sym == CFClient::SDLK_DOWN) { |
|
|
1633 | $self->{history_pointer}--; |
|
|
1634 | $self->{history_pointer} = -1 if $self->{history_pointer} < 0; |
|
|
1635 | |
|
|
1636 | if ($self->{history_pointer} >= 0) { |
|
|
1637 | $self->set_text ($self->{history}->[$self->{history_pointer}]); |
|
|
1638 | } else { |
|
|
1639 | $self->set_text ($self->{history_saveback}); |
|
|
1640 | } |
1425 | |
1641 | |
1426 | } else { |
1642 | } else { |
1427 | $self->SUPER::key_down ($ev); |
1643 | $self->SUPER::key_down ($ev); |
1428 | } |
1644 | } |
1429 | |
1645 | |
… | |
… | |
1443 | |
1659 | |
1444 | sub new { |
1660 | sub new { |
1445 | my $class = shift; |
1661 | my $class = shift; |
1446 | |
1662 | |
1447 | $class->SUPER::new ( |
1663 | $class->SUPER::new ( |
1448 | padding => 4, |
1664 | padding => 4, |
1449 | fg => [1, 1, 1], |
1665 | fg => [1, 1, 1], |
1450 | bg => [1, 1, 1, 0.2], |
1666 | bg => [1, 1, 1, 0.2], |
1451 | active_fg => [0, 0, 1], |
1667 | active_fg => [0, 0, 1], |
1452 | can_hover => 1, |
1668 | can_hover => 1, |
1453 | align => 0, |
1669 | align => 0, |
1454 | valign => 0, |
1670 | valign => 0, |
1455 | can_events => 1, |
1671 | can_events => 1, |
1456 | @_ |
1672 | @_ |
1457 | ) |
1673 | ) |
1458 | } |
1674 | } |
1459 | |
1675 | |
… | |
… | |
1666 | } |
1882 | } |
1667 | |
1883 | |
1668 | sub set_max { |
1884 | sub set_max { |
1669 | my ($self, $max) = @_; |
1885 | my ($self, $max) = @_; |
1670 | |
1886 | |
|
|
1887 | return if $self->{max_val} == $max; |
|
|
1888 | |
1671 | $self->{max_val} = $max; |
1889 | $self->{max_val} = $max; |
|
|
1890 | $self->update; |
1672 | } |
1891 | } |
1673 | |
1892 | |
1674 | sub set_value { |
1893 | sub set_value { |
1675 | my ($self, $val, $max) = @_; |
1894 | my ($self, $val, $max) = @_; |
1676 | |
1895 | |
1677 | $self->set_max ($max) |
1896 | $self->set_max ($max) |
1678 | if defined $max; |
1897 | if defined $max; |
1679 | |
1898 | |
1680 | $max = $self->{max_val}; |
1899 | return if $self->{val} == $val; |
|
|
1900 | |
1681 | $self->{val} = $val; |
1901 | $self->{val} = $val; |
1682 | |
|
|
1683 | $self->update; |
1902 | $self->update; |
1684 | } |
1903 | } |
1685 | |
1904 | |
1686 | sub _draw { |
1905 | sub _draw { |
1687 | my ($self) = @_; |
1906 | my ($self) = @_; |
… | |
… | |
1750 | |
1969 | |
1751 | sub new { |
1970 | sub new { |
1752 | my ($class, %arg) = @_; |
1971 | my ($class, %arg) = @_; |
1753 | |
1972 | |
1754 | my $self = $class->SUPER::new ( |
1973 | my $self = $class->SUPER::new ( |
1755 | tooltip => $arg{type}, |
1974 | tooltip => $arg{type}, |
|
|
1975 | can_hover => 1, |
|
|
1976 | can_events => 1, |
1756 | %arg, |
1977 | %arg, |
1757 | ); |
1978 | ); |
1758 | |
1979 | |
1759 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1); |
1980 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); |
1760 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1); |
1981 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); |
1761 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1); |
1982 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); |
1762 | |
1983 | |
1763 | $self |
1984 | $self |
1764 | } |
1985 | } |
1765 | |
1986 | |
1766 | sub set_fontsize { |
1987 | sub set_fontsize { |
… | |
… | |
1768 | |
1989 | |
1769 | $self->{value}->set_fontsize ($fsize); |
1990 | $self->{value}->set_fontsize ($fsize); |
1770 | $self->{max} ->set_fontsize ($fsize); |
1991 | $self->{max} ->set_fontsize ($fsize); |
1771 | } |
1992 | } |
1772 | |
1993 | |
|
|
1994 | sub set_max { |
|
|
1995 | my ($self, $max) = @_; |
|
|
1996 | |
|
|
1997 | $self->{gauge}->set_max ($max); |
|
|
1998 | $self->{max}->set_text ($max); |
|
|
1999 | } |
|
|
2000 | |
1773 | sub set_value { |
2001 | sub set_value { |
1774 | my ($self, $val, $max) = @_; |
2002 | my ($self, $val, $max) = @_; |
1775 | |
2003 | |
1776 | $self->set_max ($max) |
2004 | $self->set_max ($max) |
1777 | if defined $max; |
2005 | if defined $max; |
1778 | |
2006 | |
1779 | $self->{gauge}->set_value ($val, $max); |
2007 | $self->{gauge}->set_value ($val, $max); |
1780 | $self->{value}->set_text ($val); |
2008 | $self->{value}->set_text ($val); |
1781 | } |
|
|
1782 | |
|
|
1783 | sub set_max { |
|
|
1784 | my ($self, $max) = @_; |
|
|
1785 | |
|
|
1786 | $self->{gauge}->set_max ($max); |
|
|
1787 | $self->{max}->set_text ($max); |
|
|
1788 | } |
2009 | } |
1789 | |
2010 | |
1790 | ############################################################################# |
2011 | ############################################################################# |
1791 | |
2012 | |
1792 | package CFClient::UI::Slider; |
2013 | package CFClient::UI::Slider; |
… | |
… | |
1811 | # TODO: calculations are off |
2032 | # TODO: calculations are off |
1812 | my $self = $class->SUPER::new ( |
2033 | my $self = $class->SUPER::new ( |
1813 | fg => [1, 1, 1], |
2034 | fg => [1, 1, 1], |
1814 | active_fg => [0, 0, 0], |
2035 | active_fg => [0, 0, 0], |
1815 | range => [0, 0, 100, 10], |
2036 | range => [0, 0, 100, 10], |
1816 | req_w => 20, |
2037 | req_w => $::WIDTH / 80, |
1817 | req_h => 20, |
2038 | req_h => $::WIDTH / 80, |
1818 | vertical => 0, |
2039 | vertical => 0, |
1819 | can_hover => 1, |
2040 | can_hover => 1, |
1820 | inner_pad => 5, |
2041 | inner_pad => 5, |
1821 | @_ |
2042 | @_ |
1822 | ); |
2043 | ); |
… | |
… | |
1932 | |
2153 | |
1933 | sub new { |
2154 | sub new { |
1934 | my $class = shift; |
2155 | my $class = shift; |
1935 | |
2156 | |
1936 | my $self = $class->SUPER::new ( |
2157 | my $self = $class->SUPER::new ( |
1937 | fontsize => 1, |
2158 | fontsize => 1, |
|
|
2159 | can_events => 0, |
1938 | #font => default_font |
2160 | #font => default_font |
1939 | @_, |
2161 | @_, |
1940 | |
2162 | |
1941 | layout => (new CFClient::Layout), |
2163 | layout => (new CFClient::Layout), |
1942 | par => [], |
2164 | par => [], |
1943 | height => 0, |
2165 | height => 0, |
1944 | children => [ |
2166 | children => [ |
1945 | (new CFClient::UI::Empty expand => 1), |
2167 | (new CFClient::UI::Empty expand => 1), |
1946 | (new CFClient::UI::Slider vertical => 1), |
2168 | (new CFClient::UI::Slider vertical => 1), |
1947 | ], |
2169 | ], |
1948 | ); |
2170 | ); |
1949 | |
2171 | |
1950 | $self->{children}[1]->connect (changed => sub { |
2172 | $self->{children}[1]->connect (changed => sub { $self->update }); |
1951 | $self->update; |
|
|
1952 | }); |
|
|
1953 | |
2173 | |
1954 | $self |
2174 | $self |
1955 | } |
2175 | } |
1956 | |
2176 | |
1957 | sub set_fontsize { |
2177 | sub set_fontsize { |
… | |
… | |
1965 | my ($self, $text) = @_; |
2185 | my ($self, $text) = @_; |
1966 | |
2186 | |
1967 | my $layout = $self->{layout}; |
2187 | my $layout = $self->{layout}; |
1968 | |
2188 | |
1969 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2189 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
1970 | $layout->set_width ($self->{w}); |
2190 | $layout->set_width ($self->{children}[0]{w}); |
1971 | $layout->set_text ($text); |
2191 | $layout->set_text ($text); |
1972 | |
2192 | |
1973 | ($layout->size)[1] |
2193 | ($layout->size)[1] |
1974 | } |
2194 | } |
1975 | |
2195 | |
… | |
… | |
2028 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
2248 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
2029 | |
2249 | |
2030 | delete $self->{texture}; |
2250 | delete $self->{texture}; |
2031 | } |
2251 | } |
2032 | |
2252 | |
2033 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
2253 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2034 | glClearColor 0, 0, 0, 1; |
2254 | glClearColor 0, 0, 0, 0; |
2035 | glClear GL_COLOR_BUFFER_BIT; |
2255 | glClear GL_COLOR_BUFFER_BIT; |
2036 | |
2256 | |
2037 | glEnable GL_BLEND; |
2257 | glEnable GL_BLEND; |
2038 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
2258 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
2039 | glEnable GL_TEXTURE_2D; |
2259 | glEnable GL_TEXTURE_2D; |
… | |
… | |
2071 | } |
2291 | } |
2072 | |
2292 | |
2073 | sub _draw { |
2293 | sub _draw { |
2074 | my ($self) = @_; |
2294 | my ($self) = @_; |
2075 | |
2295 | |
2076 | if ($self->{texture}) { |
2296 | glEnable GL_BLEND; |
|
|
2297 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2077 | glEnable GL_TEXTURE_2D; |
2298 | glEnable GL_TEXTURE_2D; |
2078 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2299 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2300 | glColor 1, 1, 1, 1; |
2079 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
2301 | $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2080 | glDisable GL_TEXTURE_2D; |
2302 | glDisable GL_TEXTURE_2D; |
2081 | } |
2303 | glDisable GL_BLEND; |
2082 | |
2304 | |
2083 | $self->{children}[1]->draw; |
2305 | $self->{children}[1]->draw; |
2084 | |
2306 | |
2085 | } |
2307 | } |
2086 | |
2308 | |
… | |
… | |
2134 | |
2356 | |
2135 | sub new { |
2357 | sub new { |
2136 | my $class = shift; |
2358 | my $class = shift; |
2137 | |
2359 | |
2138 | my $self = $class->SUPER::new ( |
2360 | my $self = $class->SUPER::new ( |
2139 | state => 0, |
2361 | state => 0, |
2140 | connect_activate => \&toggle_flopper, |
2362 | connect_activate => \&toggle_flopper, |
2141 | can_events => 1, |
|
|
2142 | @_ |
2363 | @_ |
2143 | ); |
2364 | ); |
2144 | |
2365 | |
2145 | if ($self->{state}) { |
2366 | if ($self->{state}) { |
2146 | $self->{state} = 0; |
2367 | $self->{state} = 0; |
… | |
… | |
2181 | @_, |
2402 | @_, |
2182 | can_events => 0, |
2403 | can_events => 0, |
2183 | ) |
2404 | ) |
2184 | } |
2405 | } |
2185 | |
2406 | |
2186 | sub set_text { |
2407 | sub set_markup { |
2187 | my ($self, $text) = @_; |
2408 | my ($self, $text) = @_; |
2188 | |
2409 | |
2189 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
2410 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
2190 | $self->{label}->set_text ($text); |
2411 | $self->{label}->set_markup ($text); |
2191 | $self->add ($self->{label}); |
2412 | $self->add ($self->{label}); |
2192 | } |
2413 | } |
2193 | |
2414 | |
2194 | sub size_request { |
2415 | sub size_request { |
2195 | my ($self) = @_; |
2416 | my ($self) = @_; |
… | |
… | |
2197 | $self->child->set_max_size ($::WIDTH * 0.3); |
2418 | $self->child->set_max_size ($::WIDTH * 0.3); |
2198 | |
2419 | |
2199 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2420 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2200 | |
2421 | |
2201 | ($w + 4, $h + 4) |
2422 | ($w + 4, $h + 4) |
|
|
2423 | } |
|
|
2424 | |
|
|
2425 | sub size_allocate { |
|
|
2426 | my ($self, $w, $h) = @_; |
|
|
2427 | |
|
|
2428 | $self->SUPER::size_allocate ($w - 4, $h - 4); |
2202 | } |
2429 | } |
2203 | |
2430 | |
2204 | sub _draw { |
2431 | sub _draw { |
2205 | my ($self) = @_; |
2432 | my ($self) = @_; |
2206 | |
2433 | |
… | |
… | |
2231 | $self->SUPER::_draw; |
2458 | $self->SUPER::_draw; |
2232 | } |
2459 | } |
2233 | |
2460 | |
2234 | ############################################################################# |
2461 | ############################################################################# |
2235 | |
2462 | |
|
|
2463 | package CFClient::UI::Face; |
|
|
2464 | |
|
|
2465 | our @ISA = CFClient::UI::Base::; |
|
|
2466 | |
|
|
2467 | use CFClient::OpenGL; |
|
|
2468 | |
|
|
2469 | sub new { |
|
|
2470 | my $class = shift; |
|
|
2471 | |
|
|
2472 | $class->SUPER::new ( |
|
|
2473 | aspect => 1, |
|
|
2474 | @_, |
|
|
2475 | ) |
|
|
2476 | } |
|
|
2477 | |
|
|
2478 | sub size_request { |
|
|
2479 | (32, 8) |
|
|
2480 | } |
|
|
2481 | |
|
|
2482 | sub _draw { |
|
|
2483 | my ($self) = @_; |
|
|
2484 | |
|
|
2485 | return unless $::CONN;#d# manage and cache textures differently |
|
|
2486 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
|
|
2487 | |
|
|
2488 | # TODO animation |
|
|
2489 | if ($tex) { |
|
|
2490 | glEnable GL_BLEND; |
|
|
2491 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2492 | glEnable GL_TEXTURE_2D; |
|
|
2493 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2494 | glColor 1, 1, 1, 1; |
|
|
2495 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
|
|
2496 | glDisable GL_TEXTURE_2D; |
|
|
2497 | glDisable GL_BLEND; |
|
|
2498 | } |
|
|
2499 | } |
|
|
2500 | |
|
|
2501 | ############################################################################# |
|
|
2502 | |
|
|
2503 | package CFClient::UI::InventoryItem; |
|
|
2504 | |
|
|
2505 | our @ISA = CFClient::UI::HBox::; |
|
|
2506 | |
|
|
2507 | sub new { |
|
|
2508 | my $class = shift; |
|
|
2509 | |
|
|
2510 | my %args = @_; |
|
|
2511 | |
|
|
2512 | my $item = $args{item}; |
|
|
2513 | |
|
|
2514 | my $desc = $item->{nrof} < 2 |
|
|
2515 | ? $item->{name} |
|
|
2516 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2517 | |
|
|
2518 | |
|
|
2519 | my $self = $class->SUPER::new ( |
|
|
2520 | can_hover => 1, |
|
|
2521 | can_events => 1, |
|
|
2522 | tooltip => (CFClient::UI::Label->escape ($desc) |
|
|
2523 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
|
|
2524 | connect_button_down => sub { |
|
|
2525 | my ($self, $ev, $x, $y) = @_; |
|
|
2526 | |
|
|
2527 | # todo: maybe put examine on 1? but should just be a tooltip :( |
|
|
2528 | if ($ev->{button} == 1) { |
|
|
2529 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
|
|
2530 | } elsif ($ev->{button} == 2) { |
|
|
2531 | $::CONN->send ("apply $item->{tag}"); |
|
|
2532 | } elsif ($ev->{button} == 3) { |
|
|
2533 | CFClient::UI::Menu->new ( |
|
|
2534 | items => [ |
|
|
2535 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2536 | [ |
|
|
2537 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
|
|
2538 | sub { $::CONN->send ("lock $item->{tag}") }, |
|
|
2539 | ], |
|
|
2540 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2541 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2542 | ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }], |
|
|
2543 | ], |
|
|
2544 | )->popup ($ev); |
|
|
2545 | } |
|
|
2546 | |
|
|
2547 | 1 |
|
|
2548 | }, |
|
|
2549 | %args |
|
|
2550 | ); |
|
|
2551 | $self->add(new CFClient::UI::Face |
|
|
2552 | can_events => 0, |
|
|
2553 | face => $item->{face}, |
|
|
2554 | anim => $item->{anim}, |
|
|
2555 | animspeed => $item->{animspeed}); |
|
|
2556 | $self->add(new CFClient::UI::Label |
|
|
2557 | can_events => 0, |
|
|
2558 | text => $desc); |
|
|
2559 | |
|
|
2560 | $self |
|
|
2561 | } |
|
|
2562 | |
|
|
2563 | ############################################################################# |
|
|
2564 | |
|
|
2565 | package CFClient::UI::Inventory; |
|
|
2566 | |
|
|
2567 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
2568 | |
|
|
2569 | sub new { |
|
|
2570 | my $class = shift; |
|
|
2571 | |
|
|
2572 | my $self = $class->SUPER::new ( |
|
|
2573 | scrolled => (new CFClient::UI::VBox), |
|
|
2574 | @_, |
|
|
2575 | ); |
|
|
2576 | |
|
|
2577 | $self |
|
|
2578 | } |
|
|
2579 | |
|
|
2580 | sub set_items { |
|
|
2581 | my ($self, $items) = @_; |
|
|
2582 | |
|
|
2583 | $self->{scrolled}->clear; |
|
|
2584 | return unless $items; |
|
|
2585 | |
|
|
2586 | my @items = sort { $a->{type} <=> $b->{type} } @$items; |
|
|
2587 | |
|
|
2588 | $self->{real_items} = \@items; |
|
|
2589 | |
|
|
2590 | for my $item (@items) { |
|
|
2591 | my $desc = $item->{nrof} < 2 |
|
|
2592 | ? $item->{name} |
|
|
2593 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2594 | |
|
|
2595 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
|
|
2596 | } |
|
|
2597 | |
|
|
2598 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2599 | } |
|
|
2600 | |
|
|
2601 | sub size_request { |
|
|
2602 | my ($self) = @_; |
|
|
2603 | ($self->{req_w}, $self->{req_h}); |
|
|
2604 | } |
|
|
2605 | |
|
|
2606 | ############################################################################# |
|
|
2607 | |
|
|
2608 | package CFClient::UI::Menu; |
|
|
2609 | |
|
|
2610 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
2611 | |
|
|
2612 | use CFClient::OpenGL; |
|
|
2613 | |
|
|
2614 | sub new { |
|
|
2615 | my $class = shift; |
|
|
2616 | |
|
|
2617 | my $self = $class->SUPER::new ( |
|
|
2618 | items => [], |
|
|
2619 | z => 100, |
|
|
2620 | @_, |
|
|
2621 | ); |
|
|
2622 | |
|
|
2623 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
2624 | |
|
|
2625 | for my $item (@{ $self->{items} }) { |
|
|
2626 | my ($widget, $cb) = @$item; |
|
|
2627 | |
|
|
2628 | # handle various types of items, only text for now |
|
|
2629 | if (!ref $widget) { |
|
|
2630 | $widget = new CFClient::UI::Label |
|
|
2631 | can_hover => 1, |
|
|
2632 | can_events => 1, |
|
|
2633 | text => $widget; |
|
|
2634 | } |
|
|
2635 | |
|
|
2636 | $self->{item}{$widget} = $item; |
|
|
2637 | |
|
|
2638 | $self->{vbox}->add ($widget); |
|
|
2639 | } |
|
|
2640 | |
|
|
2641 | $self |
|
|
2642 | } |
|
|
2643 | |
|
|
2644 | # popup given the event (must be a mouse button down event currently) |
|
|
2645 | sub popup { |
|
|
2646 | my ($self, $ev) = @_; |
|
|
2647 | |
|
|
2648 | $self->emit ("popdown"); |
|
|
2649 | |
|
|
2650 | # maybe save $GRAB? must be careful about events... |
|
|
2651 | $GRAB = $self; |
|
|
2652 | $self->{button} = $ev->{button}; |
|
|
2653 | |
|
|
2654 | $self->show; |
|
|
2655 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
|
|
2656 | } |
|
|
2657 | |
|
|
2658 | sub mouse_motion { |
|
|
2659 | my ($self, $ev, $x, $y) = @_; |
|
|
2660 | |
|
|
2661 | # TODO: should use vbox->find_widget or so |
|
|
2662 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
|
|
2663 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2664 | } |
|
|
2665 | |
|
|
2666 | sub button_up { |
|
|
2667 | my ($self, $ev, $x, $y) = @_; |
|
|
2668 | |
|
|
2669 | if ($ev->{button} == $self->{button}) { |
|
|
2670 | undef $GRAB; |
|
|
2671 | $self->hide; |
|
|
2672 | |
|
|
2673 | $self->emit ("popdown"); |
|
|
2674 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2675 | } |
|
|
2676 | } |
|
|
2677 | |
|
|
2678 | ############################################################################# |
|
|
2679 | |
2236 | package CFClient::UI::Root; |
2680 | package CFClient::UI::Root; |
2237 | |
2681 | |
2238 | our @ISA = CFClient::UI::Container::; |
2682 | our @ISA = CFClient::UI::Container::; |
2239 | |
2683 | |
2240 | use CFClient::OpenGL; |
2684 | use CFClient::OpenGL; |
2241 | |
2685 | |
2242 | sub check_size { |
2686 | sub check_size { |
2243 | my ($self) = @_; |
2687 | my ($self) = @_; |
2244 | |
2688 | |
2245 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
2689 | $self->configure (0, 0, $self->{w}, $self->{h}); |
2246 | } |
2690 | } |
2247 | |
2691 | |
2248 | sub size_request { |
2692 | sub size_request { |
2249 | ($::WIDTH, $::HEIGHT) |
2693 | my ($self) = @_; |
|
|
2694 | |
|
|
2695 | ($self->{w}, $self->{h}) |
|
|
2696 | } |
|
|
2697 | |
|
|
2698 | sub size_allocate { |
|
|
2699 | my ($self, $w, $h) = @_; |
|
|
2700 | |
|
|
2701 | my $old_w = $self->{old_w}; |
|
|
2702 | my $old_h = $self->{old_h}; |
|
|
2703 | |
|
|
2704 | if ($old_w && $old_h) { |
|
|
2705 | for my $child ($self->children) { |
|
|
2706 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2707 | $child->{w} = int 0.5 + $child->{req_w} * $w / $old_w; |
|
|
2708 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2709 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2710 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2711 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2712 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2713 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2714 | } |
|
|
2715 | } |
|
|
2716 | |
|
|
2717 | $self->{old_w} = $w; |
|
|
2718 | $self->{old_h} = $h; |
2250 | } |
2719 | } |
2251 | |
2720 | |
2252 | sub configure { |
2721 | sub configure { |
2253 | my ($self, $x, $y, $w, $h) = @_; |
2722 | my ($self, $x, $y, $w, $h) = @_; |
2254 | |
2723 | |
2255 | $self->SUPER::configure ($x, $y, $w, $h); |
2724 | $self->SUPER::configure ($x, $y, $w, $h); |
2256 | |
2725 | |
2257 | for my $child (@{$self->{children}}) { |
2726 | for my $child ($self->children) { |
2258 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2727 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2259 | |
2728 | |
2260 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2729 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
2261 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2730 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
2262 | $child->configure ($X, $Y, $W,$H); |
2731 | $child->configure ($X, $Y, $W, $H); |
2263 | } |
2732 | } |
2264 | } |
2733 | } |
2265 | |
2734 | |
2266 | sub _topleft { |
2735 | sub _topleft { |
2267 | my ($self, $x, $y) = @_; |
2736 | my ($self, $x, $y) = @_; |