1 | package CFClient::Widget; |
1 | package CFClient::UI; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util; |
6 | |
|
|
7 | use SDL::OpenGL; |
|
|
8 | use SDL::OpenGL::Constants; |
|
|
9 | |
6 | |
10 | use CFClient; |
7 | use CFClient; |
11 | |
8 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
9 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
10 | |
… | |
… | |
69 | $HOVER->update if $HOVER; |
66 | $HOVER->update if $HOVER; |
70 | } |
67 | } |
71 | |
68 | |
72 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
69 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
73 | } |
70 | } |
|
|
71 | |
|
|
72 | ############################################################################# |
|
|
73 | |
|
|
74 | package CFClient::UI::Base; |
|
|
75 | |
|
|
76 | use strict; |
|
|
77 | |
|
|
78 | use SDL::OpenGL; |
74 | |
79 | |
75 | sub new { |
80 | sub new { |
76 | my $class = shift; |
81 | my $class = shift; |
77 | |
82 | |
78 | bless { |
83 | bless { |
… | |
… | |
155 | return unless $self->{h} && $self->{w}; |
160 | return unless $self->{h} && $self->{w}; |
156 | |
161 | |
157 | glPushMatrix; |
162 | glPushMatrix; |
158 | glTranslate $self->{x}, $self->{y}, 0; |
163 | glTranslate $self->{x}, $self->{y}, 0; |
159 | $self->_draw; |
164 | $self->_draw; |
|
|
165 | glPopMatrix; |
|
|
166 | |
160 | if ($self == $HOVER) { |
167 | if ($self == $HOVER) { |
|
|
168 | my ($x, $y) = @$self{qw(x y)}; |
|
|
169 | |
161 | glColor 1, 1, 1, 0.4; |
170 | glColor 1, 1, 1, 0.1; |
162 | glEnable GL_BLEND; |
171 | glEnable GL_BLEND; |
163 | glBegin GL_QUADS; |
172 | glBegin GL_QUADS; |
164 | glVertex 0 , 0; |
173 | glVertex $x , $y; |
165 | glVertex $self->{w}, 0; |
174 | glVertex $x + $self->{w}, $y; |
166 | glVertex $self->{w}, $self->{h}; |
175 | glVertex $x + $self->{w}, $y + $self->{h}; |
167 | glVertex 0 , $self->{h}; |
176 | glVertex $x , $y + $self->{h}; |
168 | glEnd; |
177 | glEnd; |
169 | glDisable GL_BLEND; |
178 | glDisable GL_BLEND; |
170 | } |
179 | } |
171 | glPopMatrix; |
|
|
172 | } |
180 | } |
173 | |
181 | |
174 | sub _draw { |
182 | sub _draw { |
175 | my ($self) = @_; |
183 | my ($self) = @_; |
176 | |
184 | |
… | |
… | |
216 | |
224 | |
217 | $self->{parent}->update |
225 | $self->{parent}->update |
218 | if $self->{parent}; |
226 | if $self->{parent}; |
219 | } |
227 | } |
220 | |
228 | |
|
|
229 | sub connect { |
|
|
230 | my ($self, $signal, $cb) = @_; |
|
|
231 | |
|
|
232 | push @{ $self->{cb}{$signal} }, $cb; |
|
|
233 | } |
|
|
234 | |
|
|
235 | sub emit { |
|
|
236 | my ($self, $signal, @args) = @_; |
|
|
237 | |
|
|
238 | $_->($self, @args) |
|
|
239 | for @{$self->{cb}{$signal} || []}; |
|
|
240 | } |
|
|
241 | |
221 | sub DESTROY { |
242 | sub DESTROY { |
222 | my ($self) = @_; |
243 | my ($self) = @_; |
223 | |
244 | |
224 | #$self->deactivate; |
245 | #$self->deactivate; |
225 | } |
246 | } |
226 | |
247 | |
227 | ############################################################################# |
248 | ############################################################################# |
228 | |
249 | |
229 | package CFClient::Widget::DrawBG; |
250 | package CFClient::UI::DrawBG; |
230 | |
251 | |
231 | our @ISA = CFClient::Widget::; |
252 | our @ISA = CFClient::UI::Base::; |
232 | |
253 | |
233 | use strict; |
254 | use strict; |
234 | use SDL::OpenGL; |
255 | use SDL::OpenGL; |
235 | |
256 | |
236 | sub new { |
257 | sub new { |
… | |
… | |
259 | glEnd; |
280 | glEnd; |
260 | } |
281 | } |
261 | |
282 | |
262 | ############################################################################# |
283 | ############################################################################# |
263 | |
284 | |
264 | package CFClient::Widget::Empty; |
285 | package CFClient::UI::Empty; |
265 | |
286 | |
266 | our @ISA = CFClient::Widget::; |
287 | our @ISA = CFClient::UI::Base::; |
267 | |
288 | |
268 | sub size_request { |
289 | sub size_request { |
269 | (0, 0) |
290 | (0, 0) |
270 | } |
291 | } |
271 | |
292 | |
272 | sub draw { } |
293 | sub draw { } |
273 | |
294 | |
274 | ############################################################################# |
295 | ############################################################################# |
275 | |
296 | |
276 | package CFClient::Widget::Container; |
297 | package CFClient::UI::Container; |
277 | |
298 | |
278 | our @ISA = CFClient::Widget::; |
299 | our @ISA = CFClient::UI::Base::; |
279 | |
300 | |
280 | sub new { |
301 | sub new { |
281 | my ($class, %arg) = @_; |
302 | my ($class, %arg) = @_; |
282 | |
303 | |
283 | my $children = delete $arg{children} || []; |
304 | my $children = delete $arg{children} || []; |
… | |
… | |
333 | $_->draw for @{$self->{children}}; |
354 | $_->draw for @{$self->{children}}; |
334 | } |
355 | } |
335 | |
356 | |
336 | ############################################################################# |
357 | ############################################################################# |
337 | |
358 | |
338 | package CFClient::Widget::Bin; |
359 | package CFClient::UI::Bin; |
339 | |
360 | |
340 | our @ISA = CFClient::Widget::Container::; |
361 | our @ISA = CFClient::UI::Container::; |
341 | |
362 | |
342 | sub new { |
363 | sub new { |
343 | my ($class, %arg) = @_; |
364 | my ($class, %arg) = @_; |
344 | |
365 | |
345 | my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; |
366 | my $child = (delete $arg{child}) || new CFClient::UI::Empty::; |
346 | |
367 | |
347 | $class->SUPER::new (children => [$child], %arg) |
368 | $class->SUPER::new (children => [$child], %arg) |
348 | } |
369 | } |
349 | |
370 | |
350 | sub add { |
371 | sub add { |
… | |
… | |
358 | sub remove { |
379 | sub remove { |
359 | my ($self, $widget) = @_; |
380 | my ($self, $widget) = @_; |
360 | |
381 | |
361 | $self->SUPER::remove ($widget); |
382 | $self->SUPER::remove ($widget); |
362 | |
383 | |
363 | $self->{children} = [new CFClient::Widget::Empty] |
384 | $self->{children} = [new CFClient::UI::Empty] |
364 | unless @{$self->{children}}; |
385 | unless @{$self->{children}}; |
365 | } |
386 | } |
366 | |
387 | |
367 | sub child { $_[0]->{children}[0] } |
388 | sub child { $_[0]->{children}[0] } |
368 | |
389 | |
… | |
… | |
379 | $self->{children}[0]->size_allocate ($w, $h); |
400 | $self->{children}[0]->size_allocate ($w, $h); |
380 | } |
401 | } |
381 | |
402 | |
382 | ############################################################################# |
403 | ############################################################################# |
383 | |
404 | |
384 | package CFClient::Widget::Window; |
405 | package CFClient::UI::Window; |
385 | |
406 | |
386 | our @ISA = CFClient::Widget::Bin::; |
407 | our @ISA = CFClient::UI::Bin::; |
387 | |
408 | |
388 | use SDL::OpenGL; |
409 | use SDL::OpenGL; |
389 | |
410 | |
390 | sub new { |
411 | sub new { |
391 | my ($class, %arg) = @_; |
412 | my ($class, %arg) = @_; |
… | |
… | |
441 | glDisable GL_TEXTURE_2D; |
462 | glDisable GL_TEXTURE_2D; |
442 | } |
463 | } |
443 | |
464 | |
444 | ############################################################################# |
465 | ############################################################################# |
445 | |
466 | |
446 | package CFClient::Widget::Frame; |
467 | package CFClient::UI::Frame; |
447 | |
468 | |
448 | our @ISA = CFClient::Widget::Bin::; |
469 | our @ISA = CFClient::UI::Bin::; |
449 | |
470 | |
450 | use SDL::OpenGL; |
471 | use SDL::OpenGL; |
451 | |
472 | |
452 | sub size_request { |
473 | sub size_request { |
453 | my ($self) = @_; |
474 | my ($self) = @_; |
… | |
… | |
489 | $chld->draw; |
510 | $chld->draw; |
490 | } |
511 | } |
491 | |
512 | |
492 | ############################################################################# |
513 | ############################################################################# |
493 | |
514 | |
494 | package CFClient::Widget::FancyFrame; |
515 | package CFClient::UI::FancyFrame; |
495 | |
516 | |
496 | our @ISA = CFClient::Widget::Bin::; |
517 | our @ISA = CFClient::UI::Bin::; |
497 | |
518 | |
498 | use SDL::OpenGL; |
519 | use SDL::OpenGL; |
499 | |
520 | |
500 | my @tex = |
521 | my @tex = |
501 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
522 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
504 | sub size_request { |
525 | sub size_request { |
505 | my ($self) = @_; |
526 | my ($self) = @_; |
506 | |
527 | |
507 | my ($w, $h) = $self->SUPER::size_request; |
528 | my ($w, $h) = $self->SUPER::size_request; |
508 | |
529 | |
509 | $h += $tex[1]->{height}; |
530 | $h += $tex[1]->{h}; |
510 | $h += $tex[4]->{height}; |
531 | $h += $tex[4]->{h}; |
511 | $w += $tex[2]->{width}; |
532 | $w += $tex[2]->{w}; |
512 | $w += $tex[3]->{width}; |
533 | $w += $tex[3]->{w}; |
513 | |
534 | |
514 | ($w, $h) |
535 | ($w, $h) |
515 | } |
536 | } |
516 | |
537 | |
517 | sub size_allocate { |
538 | sub size_allocate { |
… | |
… | |
519 | |
540 | |
520 | return unless $self->{w} != $w || $self->{h} != $h; |
541 | return unless $self->{w} != $w || $self->{h} != $h; |
521 | |
542 | |
522 | $self->SUPER::size_allocate ($w, $h); |
543 | $self->SUPER::size_allocate ($w, $h); |
523 | |
544 | |
524 | $h -= $tex[1]->{height}; |
545 | $h -= $tex[1]->{h}; |
525 | $h -= $tex[4]->{height}; |
546 | $h -= $tex[4]->{h}; |
526 | $w -= $tex[2]->{width}; |
547 | $w -= $tex[2]->{w}; |
527 | $w -= $tex[3]->{width}; |
548 | $w -= $tex[3]->{w}; |
528 | |
549 | |
529 | $h = $h < 0 ? 0 : $h; |
550 | $h = $h < 0 ? 0 : $h; |
530 | $w = $w < 0 ? 0 : $w; |
551 | $w = $w < 0 ? 0 : $w; |
531 | |
552 | |
532 | my $child = $self->child; |
553 | my $child = $self->child; |
533 | |
554 | |
534 | $child->size_allocate ($w, $h); |
555 | $child->size_allocate ($w, $h); |
535 | $child->move ($tex[3]->{width}, $tex[1]->{height}); |
556 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
536 | } |
557 | } |
537 | |
558 | |
538 | sub _draw { |
559 | sub _draw { |
539 | my ($self) = @_; |
560 | my ($self) = @_; |
540 | |
561 | |
… | |
… | |
545 | glEnable GL_TEXTURE_2D; |
566 | glEnable GL_TEXTURE_2D; |
546 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
567 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
547 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
568 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
548 | |
569 | |
549 | my $top = $tex[1]; |
570 | my $top = $tex[1]; |
550 | $top->draw_quad (0, 0, $w, $top->{height}); |
571 | $top->draw_quad (0, 0, $w, $top->{h}); |
551 | |
572 | |
552 | my $left = $tex[3]; |
573 | my $left = $tex[3]; |
553 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
574 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
554 | |
575 | |
555 | my $right = $tex[2]; |
576 | my $right = $tex[2]; |
556 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
577 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
557 | |
578 | |
558 | my $bottom = $tex[4]; |
579 | my $bottom = $tex[4]; |
559 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
580 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
560 | |
581 | |
561 | my $bg = $tex[0]; |
582 | my $bg = $tex[0]; |
562 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
583 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
563 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
584 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
564 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
585 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
565 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
586 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
566 | |
587 | |
567 | my $rep_x = $cw / $bg->{width}; |
588 | my $rep_x = $cw / $bg->{w}; |
568 | my $rep_y = $ch / $bg->{height}; |
589 | my $rep_y = $ch / $bg->{h}; |
569 | |
590 | |
570 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
591 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
571 | |
592 | |
572 | glDisable GL_BLEND; |
593 | glDisable GL_BLEND; |
573 | glDisable GL_TEXTURE_2D; |
594 | glDisable GL_TEXTURE_2D; |
574 | |
595 | |
575 | $self->child->draw; |
596 | $self->child->draw; |
576 | |
597 | |
577 | } |
598 | } |
578 | |
599 | |
579 | ############################################################################# |
600 | ############################################################################# |
580 | |
601 | |
581 | package CFClient::Widget::Table; |
602 | package CFClient::UI::Table; |
582 | |
603 | |
583 | our @ISA = CFClient::Widget::Bin::; |
604 | our @ISA = CFClient::UI::Base::; |
584 | |
605 | |
585 | use SDL::OpenGL; |
606 | use SDL::OpenGL; |
586 | |
607 | |
587 | sub add { |
608 | sub add { |
588 | my ($self, $x, $y, $chld) = @_; |
609 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
663 | } |
684 | } |
664 | } |
685 | } |
665 | |
686 | |
666 | ############################################################################# |
687 | ############################################################################# |
667 | |
688 | |
668 | package CFClient::Widget::VBox; |
689 | package CFClient::UI::VBox; |
669 | |
690 | |
670 | our @ISA = CFClient::Widget::Container::; |
691 | our @ISA = CFClient::UI::Container::; |
671 | |
692 | |
672 | use SDL::OpenGL; |
693 | use SDL::OpenGL; |
673 | |
694 | |
674 | sub size_request { |
695 | sub size_request { |
675 | my ($self) = @_; |
696 | my ($self) = @_; |
… | |
… | |
727 | } |
748 | } |
728 | } |
749 | } |
729 | |
750 | |
730 | ############################################################################# |
751 | ############################################################################# |
731 | |
752 | |
732 | package CFClient::Widget::Label; |
753 | package CFClient::UI::Label; |
733 | |
754 | |
734 | our @ISA = CFClient::Widget::; |
755 | our @ISA = CFClient::UI::Base::; |
735 | |
756 | |
736 | use SDL::OpenGL; |
757 | use SDL::OpenGL; |
737 | |
758 | |
738 | sub new { |
759 | sub new { |
739 | my ($class, %arg) = @_; |
760 | my ($class, %arg) = @_; |
740 | |
761 | |
741 | my $self = $class->SUPER::new ( |
762 | my $self = $class->SUPER::new ( |
742 | fg => [1, 1, 1], |
763 | fg => [1, 1, 1], |
743 | height => $::FONTSIZE, |
764 | height => $::FONTSIZE, |
744 | text => "", |
765 | text => "", |
|
|
766 | align => -1, |
745 | layout => new CFClient::Layout, |
767 | layout => new CFClient::Layout, |
746 | %arg |
768 | %arg |
747 | ); |
769 | ); |
748 | |
770 | |
749 | $self->set_text ($self->{text}); |
771 | $self->set_text ($self->{text}); |
… | |
… | |
781 | my ($self) = @_; |
803 | my ($self) = @_; |
782 | |
804 | |
783 | $self->{layout}->set_width; |
805 | $self->{layout}->set_width; |
784 | $self->{layout}->set_height ($self->{height}); |
806 | $self->{layout}->set_height ($self->{height}); |
785 | $self->{layout}->size |
807 | $self->{layout}->size |
786 | # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
808 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
787 | # ( |
809 | # ( |
788 | # $self->{texture}{width}, |
810 | # $self->{texture}{w}, |
789 | # $self->{texture}{height}, |
811 | # $self->{texture}{h}, |
790 | # ) |
812 | # ) |
791 | # } else { |
813 | # } else { |
792 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; |
814 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
793 | # |
815 | # |
794 | # ($w, $h) |
816 | # ($w, $h) |
795 | # } |
817 | # } |
796 | } |
818 | } |
797 | |
819 | |
… | |
… | |
824 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
846 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
825 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
847 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
826 | |
848 | |
827 | glColor @{$self->{fg}}; |
849 | glColor @{$self->{fg}}; |
828 | |
850 | |
|
|
851 | my $x = |
|
|
852 | $self->{align} < 0 ? 0 |
|
|
853 | : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} |
|
|
854 | : ($self->{w} + $self->{texture}{w}) * 0.5; |
|
|
855 | |
829 | $tex->draw_quad (0, 0); |
856 | $tex->draw_quad ($x, 0); |
830 | |
857 | |
831 | glDisable GL_BLEND; |
858 | glDisable GL_BLEND; |
832 | glDisable GL_TEXTURE_2D; |
859 | glDisable GL_TEXTURE_2D; |
833 | } |
860 | } |
834 | |
861 | |
835 | ############################################################################# |
862 | ############################################################################# |
836 | |
863 | |
837 | package CFClient::Widget::Entry; |
864 | package CFClient::UI::Entry; |
838 | |
865 | |
839 | our @ISA = CFClient::Widget::Label::; |
866 | our @ISA = CFClient::UI::Label::; |
840 | |
867 | |
841 | use SDL; |
868 | use SDL; |
842 | use SDL::OpenGL; |
869 | use SDL::OpenGL; |
843 | |
870 | |
844 | sub new { |
871 | sub new { |
… | |
… | |
858 | |
885 | |
859 | $self->{last_activity} = $::NOW; |
886 | $self->{last_activity} = $::NOW; |
860 | |
887 | |
861 | $self->{text} = $text; |
888 | $self->{text} = $text; |
862 | $self->{layout}->set_width ($self->{w}); |
889 | $self->{layout}->set_width ($self->{w}); |
|
|
890 | |
|
|
891 | $text =~ s/./*/g if $self->{hidden}; |
|
|
892 | |
|
|
893 | |
863 | $self->{layout}->set_markup ($self->escape_text ($text)); |
894 | $self->{layout}->set_markup ($self->escape_text ($text)); |
864 | |
895 | |
865 | $text = substr $text, 0, $self->{cursor}; |
896 | $text = substr $text, 0, $self->{cursor}; |
866 | utf8::encode $text; |
897 | utf8::encode $text; |
867 | |
898 | |
… | |
… | |
980 | } |
1011 | } |
981 | } |
1012 | } |
982 | |
1013 | |
983 | ############################################################################# |
1014 | ############################################################################# |
984 | |
1015 | |
985 | package CFClient::Widget::Slider; |
1016 | package CFClient::UI::Slider; |
986 | |
1017 | |
987 | use strict; |
1018 | use strict; |
988 | |
1019 | |
989 | use SDL::OpenGL; |
1020 | use SDL::OpenGL; |
990 | use SDL::OpenGL::Constants; |
1021 | use SDL::OpenGL::Constants; |
991 | |
1022 | |
992 | our @ISA = CFClient::Widget::DrawBG::; |
1023 | our @ISA = CFClient::UI::DrawBG::; |
993 | |
1024 | |
994 | sub size_request { |
1025 | sub size_request { |
995 | my ($self) = @_; |
1026 | my ($self) = @_; |
996 | |
1027 | |
997 | my $w = |
1028 | my $w = 50; |
998 | my $h = 10; |
1029 | my $h = 10; |
999 | |
1030 | |
1000 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1031 | $self->{vertical} ? ($h, $w) : ($w, $h) |
1001 | } |
1032 | } |
1002 | |
1033 | |
… | |
… | |
1007 | |
1038 | |
1008 | $class->SUPER::new ( |
1039 | $class->SUPER::new ( |
1009 | fg => [1, 1, 1], |
1040 | fg => [1, 1, 1], |
1010 | active_fg => [0, 0, 0], |
1041 | active_fg => [0, 0, 0], |
1011 | range => [0, 0, 100, 10], |
1042 | range => [0, 0, 100, 10], |
1012 | vertical => 0, |
1043 | vertical => 1, |
1013 | @_ |
1044 | @_ |
1014 | ) |
1045 | ) |
|
|
1046 | } |
|
|
1047 | |
|
|
1048 | sub button_down { |
|
|
1049 | my ($self, $ev, $x, $y) = @_; |
|
|
1050 | |
|
|
1051 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
1052 | $self->mouse_motion ($ev, $x, $y); |
|
|
1053 | } |
|
|
1054 | |
|
|
1055 | sub mouse_motion { |
|
|
1056 | my ($self, $ev, $x, $y) = @_; |
|
|
1057 | |
|
|
1058 | if ($GRAB == $self) { |
|
|
1059 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1060 | |
|
|
1061 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
1062 | |
|
|
1063 | $x = $x * ($hi - $lo) / $w + $lo; |
|
|
1064 | $x = $lo if $x < $lo; |
|
|
1065 | $x = $hi - $page if $x > $hi - $page; |
|
|
1066 | $self->{range}[0] = $x; |
|
|
1067 | |
|
|
1068 | $self->emit (changed => $x); |
|
|
1069 | $self->update; |
|
|
1070 | } |
1015 | } |
1071 | } |
1016 | |
1072 | |
1017 | sub _draw { |
1073 | sub _draw { |
1018 | my ($self) = @_; |
1074 | my ($self) = @_; |
1019 | |
1075 | |
… | |
… | |
1022 | my ($w, $h) = @$self{qw(w h)}; |
1078 | my ($w, $h) = @$self{qw(w h)}; |
1023 | |
1079 | |
1024 | if ($self->{vertical}) { |
1080 | if ($self->{vertical}) { |
1025 | # draw a vertical slider like a rotated horizontal slider |
1081 | # draw a vertical slider like a rotated horizontal slider |
1026 | |
1082 | |
1027 | glTranslate 0, $self->{w}; |
|
|
1028 | glRotate 90, 0, 0, 1; |
1083 | glRotate 90, 0, 0, 1; |
|
|
1084 | glTranslate 0, -$self->{w}, 0; |
1029 | |
1085 | |
1030 | ($w, $h) = ($h, $w); |
1086 | ($w, $h) = ($h, $w); |
1031 | } |
1087 | } |
1032 | |
1088 | |
1033 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1089 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1034 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1090 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
|
|
1091 | |
|
|
1092 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1093 | |
|
|
1094 | $page = int $page * $w / ($hi - $lo); |
|
|
1095 | $value = int +($value - $lo) * $w / ($hi - $lo); |
|
|
1096 | |
|
|
1097 | $w -= $page; |
|
|
1098 | $page &= ~1; |
|
|
1099 | glTranslate $page * 0.5, 0, 0; |
1035 | |
1100 | |
1036 | glColor @$fg; |
1101 | glColor @$fg; |
1037 | glBegin GL_LINES; |
1102 | glBegin GL_LINES; |
1038 | glVertex 0, 0; glVertex 0, $h; |
1103 | glVertex 0, 0; glVertex 0, $h; |
1039 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
1104 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
1040 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
1105 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
1041 | glEnd; |
1106 | glEnd; |
1042 | } |
|
|
1043 | |
1107 | |
1044 | ############################################################################# |
1108 | my $knob_a = $value - $page * 0.5; |
|
|
1109 | my $knob_b = $value + $page * 0.5; |
1045 | |
1110 | |
|
|
1111 | glBegin GL_QUADS; |
|
|
1112 | glColor @$fg; |
|
|
1113 | glVertex $knob_a, 0; |
|
|
1114 | glVertex $knob_a, $h; |
|
|
1115 | glVertex $knob_b, $h; |
|
|
1116 | glVertex $knob_b, 0; |
|
|
1117 | |
|
|
1118 | if ($knob_a < $knob_b - 2) { |
|
|
1119 | glColor @$bg; |
|
|
1120 | glVertex $knob_a + 1, 1; |
|
|
1121 | glVertex $knob_a + 1, $h - 1; |
|
|
1122 | glVertex $knob_b - 1, $h - 1; |
|
|
1123 | glVertex $knob_b - 1, 1; |
|
|
1124 | } |
|
|
1125 | glEnd; |
|
|
1126 | } |
|
|
1127 | |
|
|
1128 | ############################################################################# |
|
|
1129 | |
1046 | package CFClient::Widget::MapWidget; |
1130 | package CFClient::UI::MapWidget; |
1047 | |
1131 | |
1048 | use strict; |
1132 | use strict; |
1049 | |
1133 | |
1050 | use List::Util qw(min max); |
1134 | use List::Util qw(min max); |
1051 | |
1135 | |
1052 | use SDL; |
1136 | use SDL; |
1053 | use SDL::OpenGL; |
1137 | use SDL::OpenGL; |
1054 | use SDL::OpenGL::Constants; |
1138 | use SDL::OpenGL::Constants; |
1055 | |
1139 | |
1056 | our @ISA = CFClient::Widget::; |
1140 | our @ISA = CFClient::UI::Base::; |
1057 | |
1141 | |
1058 | sub new { |
1142 | sub new { |
1059 | my $class = shift; |
1143 | my $class = shift; |
1060 | |
1144 | |
1061 | $class->SUPER::new ( |
1145 | $class->SUPER::new ( |
… | |
… | |
1135 | } |
1219 | } |
1136 | |
1220 | |
1137 | for my $num (grep $_, @$cell[1,2,3]) { |
1221 | for my $num (grep $_, @$cell[1,2,3]) { |
1138 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1222 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1139 | |
1223 | |
1140 | my $w = $tex->{width}; |
1224 | my ($w, $h) = @$tex{qw(w h)}; |
1141 | my $h = $tex->{height}; |
|
|
1142 | |
1225 | |
1143 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
1226 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
1144 | } |
1227 | } |
1145 | } |
1228 | } |
1146 | } |
1229 | } |
… | |
… | |
1228 | } |
1311 | } |
1229 | } |
1312 | } |
1230 | |
1313 | |
1231 | ############################################################################# |
1314 | ############################################################################# |
1232 | |
1315 | |
1233 | package CFClient::Widget::Animator; |
1316 | package CFClient::UI::Animator; |
1234 | |
1317 | |
1235 | use SDL::OpenGL; |
1318 | use SDL::OpenGL; |
1236 | |
1319 | |
1237 | our @ISA = CFClient::Widget::Bin::; |
1320 | our @ISA = CFClient::UI::Bin::; |
1238 | |
1321 | |
1239 | sub moveto { |
1322 | sub moveto { |
1240 | my ($self, $x, $y) = @_; |
1323 | my ($self, $x, $y) = @_; |
1241 | |
1324 | |
1242 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
1325 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
1270 | glPopMatrix; |
1353 | glPopMatrix; |
1271 | } |
1354 | } |
1272 | |
1355 | |
1273 | ############################################################################# |
1356 | ############################################################################# |
1274 | |
1357 | |
1275 | package CFClient::Widget::Toplevel; |
1358 | package CFClient::UI::Toplevel; |
1276 | |
1359 | |
1277 | our @ISA = CFClient::Widget::Container::; |
1360 | our @ISA = CFClient::UI::Container::; |
1278 | |
1361 | |
1279 | sub size_request { |
1362 | sub size_request { |
1280 | ($::WIDTH, $::HEIGHT) |
1363 | ($::WIDTH, $::HEIGHT) |
1281 | } |
1364 | } |
1282 | |
1365 | |
… | |
… | |
1316 | $self->_draw; |
1399 | $self->_draw; |
1317 | } |
1400 | } |
1318 | |
1401 | |
1319 | ############################################################################# |
1402 | ############################################################################# |
1320 | |
1403 | |
1321 | package CFClient::Widget; |
1404 | package CFClient::UI; |
1322 | |
1405 | |
1323 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
1406 | $TOPLEVEL = new CFClient::UI::Toplevel; |
1324 | |
1407 | |
1325 | 1 |
1408 | 1 |
1326 | |
1409 | |