ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.67 by root, Tue Apr 11 14:36:59 2006 UTC vs.
Revision 1.72 by root, Tue Apr 11 18:00:45 2006 UTC

114} 114}
115 115
116sub focus_in { 116sub focus_in {
117 my ($self) = @_; 117 my ($self) = @_;
118 118
119 return if $FOCUS == $self;
120
119 my $focus = $FOCUS; $FOCUS = $self; 121 my $focus = $FOCUS; $FOCUS = $self;
120 $focus->update if $focus; 122 $focus->update if $focus;
121 $FOCUS->update; 123 $FOCUS->update;
122} 124}
123 125
130 $focus->update if $focus; #? 132 $focus->update if $focus; #?
131} 133}
132 134
133sub mouse_motion { } 135sub mouse_motion { }
134sub button_up { } 136sub button_up { }
135sub button_down { }
136sub key_down { } 137sub key_down { }
137sub key_up { } 138sub key_up { }
139
140sub button_down {
141 my ($self, $ev, $x, $y) = @_;
142
143 $self->focus_in;
144}
138 145
139sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 146sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
140sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 147sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
141sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 148sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
142sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 149sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
143sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 150sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
144 151
145sub draw { 152sub draw {
146 my ($self) = @_; 153 my ($self) = @_;
147 154
155 return unless $self->{h} && $self->{w};
156
148 glPushMatrix; 157 glPushMatrix;
149 glTranslate $self->{x}, $self->{y}, 0; 158 glTranslate $self->{x}, $self->{y}, 0;
150 $self->_draw; 159 $self->_draw;
160 glPopMatrix;
161
151 if ($self == $HOVER) { 162 if ($self == $HOVER) {
163 my ($x, $y) = @$self->{qw(x y)};
164
152 glColor 1, 1, 1, 0.4; 165 glColor 1, 1, 1, 0.1;
153 glEnable GL_BLEND; 166 glEnable GL_BLEND;
154 glBegin GL_QUADS; 167 glBegin GL_QUADS;
155 glVertex 0 , 0; 168 glVertex $x , $y;
156 glVertex $self->{w}, 0; 169 glVertex $x + $self->{w}, $y;
157 glVertex $self->{w}, $self->{h}; 170 glVertex $x + $self->{w}, $y + $self->{h};
158 glVertex 0 , $self->{h}; 171 glVertex $x , $y + $self->{h};
159 glEnd; 172 glEnd;
160 glDisable GL_BLEND; 173 glDisable GL_BLEND;
161 } 174 }
162 glPopMatrix;
163} 175}
164 176
165sub _draw { 177sub _draw {
166 my ($self) = @_; 178 my ($self) = @_;
167 179
207 219
208 $self->{parent}->update 220 $self->{parent}->update
209 if $self->{parent}; 221 if $self->{parent};
210} 222}
211 223
224sub connect {
225 my ($self, $signal, $cb) = @_;
226
227 push @{ $self->{cb}{$signal} }, $cb;
228}
229
230sub emit {
231 my ($self, $signal, @args) = @_;
232
233 $_->($self, @args)
234 for @{$self->{cb}{$signal} || []};
235}
236
212sub DESTROY { 237sub DESTROY {
213 my ($self) = @_; 238 my ($self) = @_;
214 239
215 #$self->deactivate; 240 #$self->deactivate;
241}
242
243#############################################################################
244
245package CFClient::Widget::DrawBG;
246
247our @ISA = CFClient::Widget::;
248
249use strict;
250use SDL::OpenGL;
251
252sub new {
253 my $class = shift;
254
255 # range [value, low, high, page]
256
257 $class->SUPER::new (
258 bg => [0, 0, 0, 0.4],
259 active_bg => [1, 1, 1],
260 @_
261 )
262}
263
264sub _draw {
265 my ($self) = @_;
266
267 my ($w, $h) = @$self{qw(w h)};
268
269 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
270 glBegin GL_QUADS;
271 glVertex 0 , 0;
272 glVertex 0 , $h;
273 glVertex $w, $h;
274 glVertex $w, 0;
275 glEnd;
216} 276}
217 277
218############################################################################# 278#############################################################################
219 279
220package CFClient::Widget::Empty; 280package CFClient::Widget::Empty;
321} 381}
322 382
323sub child { $_[0]->{children}[0] } 383sub child { $_[0]->{children}[0] }
324 384
325sub size_request { 385sub size_request {
326 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 386 $_[0]{children}[0]->size_request
327} 387}
328 388
329sub size_allocate { 389sub size_allocate {
330 my ($self, $w, $h) = @_; 390 my ($self, $w, $h) = @_;
331 391
392 return unless $self->{w} != $w || $self->{h} != $h;
393
332 $self->SUPER::size_allocate ($w, $h); 394 $self->SUPER::size_allocate ($w, $h);
333 $self->{children}[0]->size_allocate ($w, $h) 395 $self->{children}[0]->size_allocate ($w, $h);
334 if $self->{children}[0]
335} 396}
336 397
337############################################################################# 398#############################################################################
338 399
339package CFClient::Widget::Window; 400package CFClient::Widget::Window;
366} 427}
367 428
368sub size_allocate { 429sub size_allocate {
369 my ($self, $w, $h) = @_; 430 my ($self, $w, $h) = @_;
370 431
432 return unless $self->{w} != $w || $self->{h} != $h;
433
371 $self->{w} = $w; 434 $self->{w} = $w;
372 $self->{h} = $h; 435 $self->{h} = $h;
373 436
374 $self->child->size_allocate ($w, $h); 437 $self->child->size_allocate ($w, $h);
375 438
413} 476}
414 477
415sub size_allocate { 478sub size_allocate {
416 my ($self, $w, $h) = @_; 479 my ($self, $w, $h) = @_;
417 480
481 return unless $self->{w} != $w || $self->{h} != $h;
482
418 $self->{w} = $w; 483 $self->{w} = $w;
419 $self->{h} = $h; 484 $self->{h} = $h;
420 485
421 $self->child->size_allocate ($w - 4, $h - 4); 486 $self->child->size_allocate ($w - 4, $h - 4);
422 $self->child->move (2, 2); 487 $self->child->move (2, 2);
455sub size_request { 520sub size_request {
456 my ($self) = @_; 521 my ($self) = @_;
457 522
458 my ($w, $h) = $self->SUPER::size_request; 523 my ($w, $h) = $self->SUPER::size_request;
459 524
460 $h += $tex[1]->{height}; 525 $h += $tex[1]->{h};
461 $h += $tex[4]->{height}; 526 $h += $tex[4]->{h};
462 $w += $tex[2]->{width}; 527 $w += $tex[2]->{w};
463 $w += $tex[3]->{width}; 528 $w += $tex[3]->{w};
464 529
465 ($w, $h) 530 ($w, $h)
466} 531}
467 532
468sub size_allocate { 533sub size_allocate {
469 my ($self, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
470 535
536 return unless $self->{w} != $w || $self->{h} != $h;
537
471 $self->SUPER::size_allocate ($w, $h); 538 $self->SUPER::size_allocate ($w, $h);
472 539
473 $h -= $tex[1]->{height}; 540 $h -= $tex[1]->{h};
474 $h -= $tex[4]->{height}; 541 $h -= $tex[4]->{h};
475 $w -= $tex[2]->{width}; 542 $w -= $tex[2]->{w};
476 $w -= $tex[3]->{width}; 543 $w -= $tex[3]->{w};
477 544
478 $h = $h < 0 ? 0 : $h; 545 $h = $h < 0 ? 0 : $h;
479 $w = $w < 0 ? 0 : $w; 546 $w = $w < 0 ? 0 : $w;
480 547
481 my $child = $self->child; 548 my $child = $self->child;
482 549
483 $child->size_allocate ($w, $h); 550 $child->size_allocate ($w, $h);
484 $child->move ($tex[3]->{width}, $tex[1]->{height}); 551 $child->move ($tex[3]->{w}, $tex[1]->{h});
485} 552}
486 553
487sub _draw { 554sub _draw {
488 my ($self) = @_; 555 my ($self) = @_;
489 556
494 glEnable GL_TEXTURE_2D; 561 glEnable GL_TEXTURE_2D;
495 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 562 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
497 564
498 my $top = $tex[1]; 565 my $top = $tex[1];
499 $top->draw_quad (0, 0, $w, $top->{height}); 566 $top->draw_quad (0, 0, $w, $top->{h});
500 567
501 my $left = $tex[3]; 568 my $left = $tex[3];
502 $left->draw_quad (0, $top->{height}, $left->{width}, $ch); 569 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
503 570
504 my $right = $tex[2]; 571 my $right = $tex[2];
505 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); 572 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
506 573
507 my $bottom = $tex[4]; 574 my $bottom = $tex[4];
508 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); 575 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
509 576
510 my $bg = $tex[0]; 577 my $bg = $tex[0];
511 glBindTexture GL_TEXTURE_2D, $bg->{name}; 578 glBindTexture GL_TEXTURE_2D, $bg->{name};
512 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 579 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
513 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 580 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
514 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 581 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
515 582
516 my $rep_x = $cw / $bg->{width}; 583 my $rep_x = $cw / $bg->{w};
517 my $rep_y = $ch / $bg->{height}; 584 my $rep_y = $ch / $bg->{h};
518 585
519 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); 586 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
520 587
521 glDisable GL_BLEND; 588 glDisable GL_BLEND;
522 glDisable GL_TEXTURE_2D; 589 glDisable GL_TEXTURE_2D;
523 590
524 $self->child->draw; 591 $self->child->draw;
527 594
528############################################################################# 595#############################################################################
529 596
530package CFClient::Widget::Table; 597package CFClient::Widget::Table;
531 598
532our @ISA = CFClient::Widget::Bin::; 599our @ISA = CFClient::Widget::;
533 600
534use SDL::OpenGL; 601use SDL::OpenGL;
535 602
536sub add { 603sub add {
537 my ($self, $x, $y, $chld) = @_; 604 my ($self, $x, $y, $chld) = @_;
632} 699}
633 700
634sub size_allocate { 701sub size_allocate {
635 my ($self, $w, $h) = @_; 702 my ($self, $w, $h) = @_;
636 703
704 return unless $self->{w} != $w || $self->{h} != $h;
705
637 $self->w ($w); 706 $self->{w} = $w;
638 $self->h ($h); 707 $self->{h} = $h;
639 708
640 my $exp; 709 return unless $self->{h};
641 my @oth; 710
642 # find expand widget 711 my $children = $self->{children};
643 for (@{$self->{children}}) { 712
644 if ($_->{expand}) { 713 my @h = map +($_->size_request)[1], @$children;
645 $exp = $_; 714
646 last; 715 my $req_h = List::Util::sum @h;
716
717 if ($req_h > $h) {
718 # ah well, not enough space
719 $_ = $h[$_] * $h / $req_h for @h;
720 } else {
721 my @exp = grep $_->{expand}, @$children;
722 @exp = @$children unless @exp;
723
724 my %exp = map +($_ => 1), @exp;
725
726 for (0 .. $#$children) {
727 my $child = $children->[$_];
728
729 my $alloc_h = $h[$_];
730 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
731 $h[$_] = $alloc_h;
647 } 732 }
648 push @oth, $_;
649 }
650
651 my ($ow, $oh);
652
653 # get sizes of other widgets
654 for (@oth) {
655 my ($w, $h) = $_->size_request;
656 $oh += $h;
657 if ($ow < $w) { $ow = $w }
658 } 733 }
659 734
660 my $y = 0; 735 my $y = 0;
661 for (@{$self->{children}}) { 736 for (0 .. $#$children) {
737 my $child = $children->[$_];
738 my $h = $h[$_];
662 $_->move (0, $y); 739 $child->move (0, $y);
663
664 if ($_ == $exp) {
665 $_->size_allocate ($w, $h - $oh);
666 $y += $h - $oh;
667 } else {
668 my ($cw, $h) = $_->size_request;
669 $_->size_allocate ($w, $h); 740 $child->size_allocate ($w, $h);
741
670 $y += $h; 742 $y += $h;
671 }
672 } 743 }
673} 744}
674 745
675############################################################################# 746#############################################################################
676 747
681use SDL::OpenGL; 752use SDL::OpenGL;
682 753
683sub new { 754sub new {
684 my ($class, %arg) = @_; 755 my ($class, %arg) = @_;
685 756
686 # TODO: color, and make height, xyz etc. optional
687 my $self = $class->SUPER::new ( 757 my $self = $class->SUPER::new (
688 color => [1, 1, 1], 758 fg => [1, 1, 1],
689 height => $::FONTSIZE, 759 height => $::FONTSIZE,
690 text => "", 760 text => "",
761 align => -1,
691 layout => new CFClient::Layout, 762 layout => new CFClient::Layout,
692 %arg 763 %arg
693 ); 764 );
694 765
695 $self->set_text ($self->{text}); 766 $self->set_text ($self->{text});
696 767
697 $self 768 $self
698} 769}
699 770
771sub escape_text {
772 local $_ = $_[1];
773
774 s/&/&amp;/g;
775 s/>/&gt;/g;
776 s/</&lt;/g;
777
778 $_[1]
779}
780
700sub set_text { 781sub set_text {
701 my ($self, $text) = @_; 782 my ($self, $text) = @_;
702 783
703 $self->{text} = $text; 784 $self->{text} = $text;
704 $self->{layout}->set_markup ($text); 785 $self->{layout}->set_markup ($text);
705 786
706 delete $self->{texture}; 787 delete $self->{texture};
788 $self->update;
707} 789}
708 790
709sub get_text { 791sub get_text {
710 my ($self, $text) = @_; 792 my ($self, $text) = @_;
711 793
716 my ($self) = @_; 798 my ($self) = @_;
717 799
718 $self->{layout}->set_width; 800 $self->{layout}->set_width;
719 $self->{layout}->set_height ($self->{height}); 801 $self->{layout}->set_height ($self->{height});
720 $self->{layout}->size 802 $self->{layout}->size
721# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 803# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
722# ( 804# (
723# $self->{texture}{width}, 805# $self->{texture}{w},
724# $self->{texture}{height}, 806# $self->{texture}{h},
725# ) 807# )
726# } else { 808# } else {
727# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; 809# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
728# 810#
729# ($w, $h) 811# ($w, $h)
730# } 812# }
731} 813}
732 814
733sub size_allocate { 815sub size_allocate {
734 my ($self, $w, $h) = @_; 816 my ($self, $w, $h) = @_;
735 817
818 return unless $self->{w} != $w || $self->{h} != $h;
819
736 $self->SUPER::size_allocate ($w, $h); 820 $self->SUPER::size_allocate ($w, $h);
737 delete $self->{texture}; 821 delete $self->{texture};
822}
823
824sub update {
825 my ($self) = @_;
826
827 delete $self->{texture};
828 $self->SUPER::update;
738} 829}
739 830
740sub _draw { 831sub _draw {
741 my ($self) = @_; 832 my ($self) = @_;
742 833
748 glEnable GL_BLEND; 839 glEnable GL_BLEND;
749 glEnable GL_TEXTURE_2D; 840 glEnable GL_TEXTURE_2D;
750 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 841 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
751 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 842 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
752 843
753 glColor @{$self->{color}}; 844 glColor @{$self->{fg}};
754 845
846 my $x =
847 $self->{align} < 0 ? 0
848 : $self->{align} > 0 ? $self->{w} - $self->{texture}{w}
849 : ($self->{w} + $self->{texture}{w}) * 0.5;
850
755 $tex->draw_quad (0, 0); 851 $tex->draw_quad ($x, 0);
756 852
757 glDisable GL_BLEND; 853 glDisable GL_BLEND;
758 glDisable GL_TEXTURE_2D; 854 glDisable GL_TEXTURE_2D;
759} 855}
760 856
765our @ISA = CFClient::Widget::Label::; 861our @ISA = CFClient::Widget::Label::;
766 862
767use SDL; 863use SDL;
768use SDL::OpenGL; 864use SDL::OpenGL;
769 865
866sub new {
867 my $class = shift;
868
869 $class->SUPER::new (
870 fg => [1, 1, 1],
871 bg => [0, 0, 0, 0.4],
872 active_bg => [1, 1, 1],
873 active_fg => [0, 0, 0],
874 @_
875 )
876}
877
878sub _set_text {
879 my ($self, $text) = @_;
880
881 $self->{last_activity} = $::NOW;
882
883 $self->{text} = $text;
884 $self->{layout}->set_width ($self->{w});
885
886 $text =~ s/./*/g if $self->{hidden};
887
888
889 $self->{layout}->set_markup ($self->escape_text ($text));
890
891 $text = substr $text, 0, $self->{cursor};
892 utf8::encode $text;
893
894 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
895}
896
897sub size_request {
898 my ($self) = @_;
899
900 my ($w, $h) = $self->SUPER::size_request;
901
902 ($w + 1, $h) # add 1 for cursor
903}
904
905sub size_allocate {
906 my ($self, $w, $h) = @_;
907
908 return unless $self->{w} != $w || $self->{h} != $h;
909
910 $self->SUPER::size_allocate ($w, $h);
911
912 $self->_set_text ($self->{text});
913}
914
915sub set_text {
916 my ($self, $text) = @_;
917
918 $self->{cursor} = length $text;
919 $self->_set_text ($text);
920 $self->update;
921}
922
770sub key_down { 923sub key_down {
771 my ($self, $ev) = @_; 924 my ($self, $ev) = @_;
772 925
773 my $mod = $ev->key_mod; 926 my $mod = $ev->key_mod;
774 my $sym = $ev->key_sym; 927 my $sym = $ev->key_sym;
776 my $uni = $ev->key_unicode; 929 my $uni = $ev->key_unicode;
777 930
778 my $text = $self->get_text; 931 my $text = $self->get_text;
779 932
780 if ($sym == SDLK_BACKSPACE) { 933 if ($sym == SDLK_BACKSPACE) {
781 substr $text, -1, 1, ''; 934 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
935 } elsif ($sym == SDLK_DELETE) {
936 substr $text, $self->{cursor}, 1, "";
937 } elsif ($sym == SDLK_LEFT) {
938 --$self->{cursor} if $self->{cursor};
939 } elsif ($sym == SDLK_RIGHT) {
940 ++$self->{cursor} if $self->{cursor} < length $self->{text};
782 } elsif ($uni) { 941 } elsif ($uni) {
783 $text .= chr $uni; 942 substr $text, $self->{cursor}++, 0, chr $uni;
784 } 943 }
785 944
786 $self->set_text ($text); 945 $self->_set_text ($text);
946 $self->update;
947}
948
949sub focus_in {
950 my ($self) = @_;
951
952 $self->{last_activity} = $::NOW;
953
954 $self->SUPER::focus_in;
787} 955}
788 956
789sub button_down { 957sub button_down {
790 my ($self, $ev) = @_; 958 my ($self, $ev, $x, $y) = @_;
791 959
792 $self->focus_in; 960 $self->SUPER::button_down ($ev, $x, $y);
961
962 my $idx = $self->{layout}->xy_to_index ($x, $y);
963
964 # byte-index to char-index
965 my $text = $self->{layout};
966 utf8::encode $text;
967 $self->{cursor} = length substr $text, 0, $idx;
968
969 $self->_set_text ($self->{text});
970 $self->update;
793} 971}
794 972
795sub mouse_motion { 973sub mouse_motion {
796 my ($self, $ev, $x, $y) = @_; 974 my ($self, $ev, $x, $y) = @_;
797 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# 975# printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
798} 976}
799 977
800sub _draw { 978sub _draw {
801 my ($self) = @_; 979 my ($self) = @_;
802 980
981 local $self->{fg} = $self->{fg};
982
803 if ($FOCUS == $self) { 983 if ($FOCUS == $self) {
804 glColor 1, 1, 1; 984 glColor @{$self->{active_bg}};
985 $self->{fg} = $self->{active_fg};
805 } else { 986 } else {
806 glColor 0.7, 0.7, 0.7; 987 glColor @{$self->{bg}};
807 } 988 }
808 989
809 glBegin GL_QUADS; 990 glBegin GL_QUADS;
810 glVertex 0 , 0; 991 glVertex 0 , 0;
811 glVertex 0 , $self->{h} - 1; 992 glVertex 0 , $self->{h};
812 glVertex $self->{w} - 1, $self->{h} - 1; 993 glVertex $self->{w}, $self->{h};
813 glVertex $self->{w} - 1, 0; 994 glVertex $self->{w}, 0;
814 glEnd; 995 glEnd;
815 996
816 $self->SUPER::_draw; 997 $self->SUPER::_draw;
998
999 #TODO: force update every cursor change :(
1000 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1001 glColor @{$self->{fg}};
1002 glBegin GL_LINES;
1003 glVertex $self->{cur_x}, $self->{cur_y};
1004 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1005 glEnd;
1006 }
1007}
1008
1009#############################################################################
1010
1011package CFClient::Widget::Slider;
1012
1013use strict;
1014
1015use SDL::OpenGL;
1016use SDL::OpenGL::Constants;
1017
1018our @ISA = CFClient::Widget::DrawBG::;
1019
1020sub size_request {
1021 my ($self) = @_;
1022
1023 my $w = 50;
1024 my $h = 10;
1025
1026 $self->{vertical} ? ($h, $w) : ($w, $h)
1027}
1028
1029sub new {
1030 my $class = shift;
1031
1032 # range [value, low, high, page]
1033
1034 $class->SUPER::new (
1035 fg => [1, 1, 1],
1036 active_fg => [0, 0, 0],
1037 range => [0, 0, 100, 10],
1038 vertical => 1,
1039 @_
1040 )
1041}
1042
1043sub button_down {
1044 my ($self, $ev, $x, $y) = @_;
1045
1046 $self->SUPER::button_down ($ev, $x, $y);
1047 $self->mouse_motion ($ev, $x, $y);
1048}
1049
1050sub mouse_motion {
1051 my ($self, $ev, $x, $y) = @_;
1052
1053 if ($GRAB == $self) {
1054 my ($value, $lo, $hi, $page) = @{$self->{range}};
1055
1056 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1057
1058 $x = $x * ($hi - $lo) / $w + $lo;
1059 $x = $lo if $x < $lo;
1060 $x = $hi - $page if $x > $hi - $page;
1061 $self->{range}[0] = $x;
1062
1063 $self->emit (changed => $x);
1064 $self->update;
1065 }
1066}
1067
1068sub _draw {
1069 my ($self) = @_;
1070
1071 $self->SUPER::_draw ();
1072
1073 my ($w, $h) = @$self{qw(w h)};
1074
1075 if ($self->{vertical}) {
1076 # draw a vertical slider like a rotated horizontal slider
1077
1078 glRotate 90, 0, 0, 1;
1079 glTranslate 0, -$self->{w}, 0;
1080
1081 ($w, $h) = ($h, $w);
1082 }
1083
1084 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1085 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1086
1087 my ($value, $lo, $hi, $page) = @{$self->{range}};
1088
1089 $page = int $page * $w / ($hi - $lo);
1090 $value = int +($value - $lo) * $w / ($hi - $lo);
1091
1092 $w -= $page;
1093 $page &= ~1;
1094 glTranslate $page * 0.5, 0, 0;
1095
1096 glColor @$fg;
1097 glBegin GL_LINES;
1098 glVertex 0, 0; glVertex 0, $h;
1099 glVertex $w - 1, 0; glVertex $w - 1, $h;
1100 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1101 glEnd;
1102
1103 my $knob_a = $value - $page * 0.5;
1104 my $knob_b = $value + $page * 0.5;
1105
1106 glBegin GL_QUADS;
1107 glColor @$fg;
1108 glVertex $knob_a, 0;
1109 glVertex $knob_a, $h;
1110 glVertex $knob_b, $h;
1111 glVertex $knob_b, 0;
1112
1113 if ($knob_a < $knob_b - 2) {
1114 glColor @$bg;
1115 glVertex $knob_a + 1, 1;
1116 glVertex $knob_a + 1, $h - 1;
1117 glVertex $knob_b - 1, $h - 1;
1118 glVertex $knob_b - 1, 1;
1119 }
1120 glEnd;
817} 1121}
818 1122
819############################################################################# 1123#############################################################################
820 1124
821package CFClient::Widget::MapWidget; 1125package CFClient::Widget::MapWidget;
910 } 1214 }
911 1215
912 for my $num (grep $_, @$cell[1,2,3]) { 1216 for my $num (grep $_, @$cell[1,2,3]) {
913 my $tex = $::CONN->{face}[$num]{texture} || next; 1217 my $tex = $::CONN->{face}[$num]{texture} || next;
914 1218
915 my $w = $tex->{width}; 1219 my ($w, $h) = @$tex{qw(w h)};
916 my $h = $tex->{height};
917 1220
918 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); 1221 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
919 } 1222 }
920 } 1223 }
921 } 1224 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines