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