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.65 by root, Tue Apr 11 14:04:27 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;
216} 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;
276}
277
278#############################################################################
279
280package CFClient::Widget::Empty;
281
282our @ISA = CFClient::Widget::;
283
284sub size_request {
285 (0, 0)
286}
287
288sub draw { }
217 289
218############################################################################# 290#############################################################################
219 291
220package CFClient::Widget::Container; 292package CFClient::Widget::Container;
221 293
236 my ($self, $chld, $expand) = @_; 308 my ($self, $chld, $expand) = @_;
237 309
238 $chld->{expand} = $expand; 310 $chld->{expand} = $expand;
239 $chld->set_parent ($self); 311 $chld->set_parent ($self);
240 312
241 @{$self->{children}} = 313 $self->{children} = [
242 sort { $a->{z} <=> $b->{z} } 314 sort { $a->{z} <=> $b->{z} }
243 @{$self->{children}}, $chld; 315 @{$self->{children}}, $chld
316 ];
244 317
245 $self->size_allocate ($self->{w}, $self->{h}) 318 $self->size_allocate ($self->{w}, $self->{h})
246 if $self->{w}; #TODO: check for "realised state" 319 if $self->{w}; #TODO: check for "realised state"
247} 320}
248 321
280 353
281package CFClient::Widget::Bin; 354package CFClient::Widget::Bin;
282 355
283our @ISA = CFClient::Widget::Container::; 356our @ISA = CFClient::Widget::Container::;
284 357
358sub new {
359 my ($class, %arg) = @_;
360
361 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::;
362
363 $class->SUPER::new (children => [$child], %arg)
364}
365
366sub add {
367 my ($self, $widget) = @_;
368
369 $self->{children} = [];
370
371 $self->SUPER::add ($widget);
372}
373
374sub remove {
375 my ($self, $widget) = @_;
376
377 $self->SUPER::remove ($widget);
378
379 $self->{children} = [new CFClient::Widget::Empty]
380 unless @{$self->{children}};
381}
382
285sub child { $_[0]->{children}[0] } 383sub child { $_[0]->{children}[0] }
286 384
287sub size_request { 385sub size_request {
288 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 386 $_[0]{children}[0]->size_request
289} 387}
290 388
291sub size_allocate { 389sub size_allocate {
292 my ($self, $w, $h) = @_; 390 my ($self, $w, $h) = @_;
293 391
392 return unless $self->{w} != $w || $self->{h} != $h;
393
294 $self->SUPER::size_allocate ($w, $h); 394 $self->SUPER::size_allocate ($w, $h);
295 $self->{children}[0]->size_allocate ($w, $h) 395 $self->{children}[0]->size_allocate ($w, $h);
296 if $self->{children}[0]
297} 396}
298 397
299############################################################################# 398#############################################################################
300 399
301package CFClient::Widget::Window; 400package CFClient::Widget::Window;
328} 427}
329 428
330sub size_allocate { 429sub size_allocate {
331 my ($self, $w, $h) = @_; 430 my ($self, $w, $h) = @_;
332 431
432 return unless $self->{w} != $w || $self->{h} != $h;
433
333 $self->{w} = $w; 434 $self->{w} = $w;
334 $self->{h} = $h; 435 $self->{h} = $h;
335 436
336 $self->child->size_allocate ($w, $h); 437 $self->child->size_allocate ($w, $h);
337 438
375} 476}
376 477
377sub size_allocate { 478sub size_allocate {
378 my ($self, $w, $h) = @_; 479 my ($self, $w, $h) = @_;
379 480
481 return unless $self->{w} != $w || $self->{h} != $h;
482
380 $self->{w} = $w; 483 $self->{w} = $w;
381 $self->{h} = $h; 484 $self->{h} = $h;
382 485
383 $self->child->size_allocate ($w - 4, $h - 4); 486 $self->child->size_allocate ($w - 4, $h - 4);
384 $self->child->move (2, 2); 487 $self->child->move (2, 2);
417sub size_request { 520sub size_request {
418 my ($self) = @_; 521 my ($self) = @_;
419 522
420 my ($w, $h) = $self->SUPER::size_request; 523 my ($w, $h) = $self->SUPER::size_request;
421 524
422 $h += $tex[1]->{height}; 525 $h += $tex[1]->{h};
423 $h += $tex[4]->{height}; 526 $h += $tex[4]->{h};
424 $w += $tex[2]->{width}; 527 $w += $tex[2]->{w};
425 $w += $tex[3]->{width}; 528 $w += $tex[3]->{w};
426 529
427 ($w, $h) 530 ($w, $h)
428} 531}
429 532
430sub size_allocate { 533sub size_allocate {
431 my ($self, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
432 535
536 return unless $self->{w} != $w || $self->{h} != $h;
537
433 $self->SUPER::size_allocate ($w, $h); 538 $self->SUPER::size_allocate ($w, $h);
434 539
435 $h -= $tex[1]->{height}; 540 $h -= $tex[1]->{h};
436 $h -= $tex[4]->{height}; 541 $h -= $tex[4]->{h};
437 $w -= $tex[2]->{width}; 542 $w -= $tex[2]->{w};
438 $w -= $tex[3]->{width}; 543 $w -= $tex[3]->{w};
439 544
440 $h = $h < 0 ? 0 : $h; 545 $h = $h < 0 ? 0 : $h;
441 $w = $w < 0 ? 0 : $w; 546 $w = $w < 0 ? 0 : $w;
442 547
548 my $child = $self->child;
549
443 $self->child->size_allocate ($w, $h); 550 $child->size_allocate ($w, $h);
444 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 551 $child->move ($tex[3]->{w}, $tex[1]->{h});
445} 552}
446 553
447sub _draw { 554sub _draw {
448 my ($self) = @_; 555 my ($self) = @_;
449 556
454 glEnable GL_TEXTURE_2D; 561 glEnable GL_TEXTURE_2D;
455 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 562 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
456 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
457 564
458 my $top = $tex[1]; 565 my $top = $tex[1];
459 $top->draw_quad (0, 0, $w, $top->{height}); 566 $top->draw_quad (0, 0, $w, $top->{h});
460 567
461 my $left = $tex[3]; 568 my $left = $tex[3];
462 $left->draw_quad (0, $top->{height}, $left->{width}, $ch); 569 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
463 570
464 my $right = $tex[2]; 571 my $right = $tex[2];
465 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); 572 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
466 573
467 my $bottom = $tex[4]; 574 my $bottom = $tex[4];
468 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); 575 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
469 576
470 my $bg = $tex[0]; 577 my $bg = $tex[0];
471 glBindTexture GL_TEXTURE_2D, $bg->{name}; 578 glBindTexture GL_TEXTURE_2D, $bg->{name};
472 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 579 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
473 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 580 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
474 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 581 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
475 582
476 my $rep_x = $cw / $bg->{width}; 583 my $rep_x = $cw / $bg->{w};
477 my $rep_y = $ch / $bg->{height}; 584 my $rep_y = $ch / $bg->{h};
478 585
479 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); 586 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
480 587
481 glDisable GL_BLEND; 588 glDisable GL_BLEND;
482 glDisable GL_TEXTURE_2D; 589 glDisable GL_TEXTURE_2D;
483 590
484 $self->child->draw; 591 $self->child->draw;
487 594
488############################################################################# 595#############################################################################
489 596
490package CFClient::Widget::Table; 597package CFClient::Widget::Table;
491 598
492our @ISA = CFClient::Widget::Bin::; 599our @ISA = CFClient::Widget::;
493 600
494use SDL::OpenGL; 601use SDL::OpenGL;
495 602
496sub add { 603sub add {
497 my ($self, $x, $y, $chld) = @_; 604 my ($self, $x, $y, $chld) = @_;
592} 699}
593 700
594sub size_allocate { 701sub size_allocate {
595 my ($self, $w, $h) = @_; 702 my ($self, $w, $h) = @_;
596 703
704 return unless $self->{w} != $w || $self->{h} != $h;
705
597 $self->w ($w); 706 $self->{w} = $w;
598 $self->h ($h); 707 $self->{h} = $h;
599 708
600 my $exp; 709 return unless $self->{h};
601 my @oth; 710
602 # find expand widget 711 my $children = $self->{children};
603 for (@{$self->{children}}) { 712
604 if ($_->{expand}) { 713 my @h = map +($_->size_request)[1], @$children;
605 $exp = $_; 714
606 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;
607 } 732 }
608 push @oth, $_;
609 }
610
611 my ($ow, $oh);
612
613 # get sizes of other widgets
614 for (@oth) {
615 my ($w, $h) = $_->size_request;
616 $oh += $h;
617 if ($ow < $w) { $ow = $w }
618 } 733 }
619 734
620 my $y = 0; 735 my $y = 0;
621 for (@{$self->{children}}) { 736 for (0 .. $#$children) {
737 my $child = $children->[$_];
738 my $h = $h[$_];
622 $_->move (0, $y); 739 $child->move (0, $y);
623
624 if ($_ == $exp) {
625 $_->size_allocate ($w, $h - $oh);
626 $y += $h - $oh;
627 } else {
628 my ($cw, $h) = $_->size_request;
629 $_->size_allocate ($w, $h); 740 $child->size_allocate ($w, $h);
741
630 $y += $h; 742 $y += $h;
631 }
632 } 743 }
633} 744}
634 745
635############################################################################# 746#############################################################################
636 747
641use SDL::OpenGL; 752use SDL::OpenGL;
642 753
643sub new { 754sub new {
644 my ($class, %arg) = @_; 755 my ($class, %arg) = @_;
645 756
646 # TODO: color, and make height, xyz etc. optional
647 my $self = $class->SUPER::new ( 757 my $self = $class->SUPER::new (
648 color => [1, 1, 1], 758 fg => [1, 1, 1],
649 height => $::FONTSIZE, 759 height => $::FONTSIZE,
650 text => "", 760 text => "",
761 align => -1,
651 layout => new CFClient::Layout, 762 layout => new CFClient::Layout,
652 %arg 763 %arg
653 ); 764 );
654 765
655 $self->set_text ($self->{text}); 766 $self->set_text ($self->{text});
656 767
657 $self 768 $self
658} 769}
659 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
660sub set_text { 781sub set_text {
661 my ($self, $text) = @_; 782 my ($self, $text) = @_;
662 783
663 $self->{text} = $text; 784 $self->{text} = $text;
664 $self->{layout}->set_markup ($text); 785 $self->{layout}->set_markup ($text);
665 786
666 delete $self->{texture}; 787 delete $self->{texture};
788 $self->update;
667} 789}
668 790
669sub get_text { 791sub get_text {
670 my ($self, $text) = @_; 792 my ($self, $text) = @_;
671 793
676 my ($self) = @_; 798 my ($self) = @_;
677 799
678 $self->{layout}->set_width; 800 $self->{layout}->set_width;
679 $self->{layout}->set_height ($self->{height}); 801 $self->{layout}->set_height ($self->{height});
680 $self->{layout}->size 802 $self->{layout}->size
681# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 803# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
682# ( 804# (
683# $self->{texture}{width}, 805# $self->{texture}{w},
684# $self->{texture}{height}, 806# $self->{texture}{h},
685# ) 807# )
686# } else { 808# } else {
687# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; 809# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
688# 810#
689# ($w, $h) 811# ($w, $h)
690# } 812# }
691} 813}
692 814
693sub size_allocate { 815sub size_allocate {
694 my ($self, $w, $h) = @_; 816 my ($self, $w, $h) = @_;
695 817
818 return unless $self->{w} != $w || $self->{h} != $h;
819
696 $self->SUPER::size_allocate ($w, $h); 820 $self->SUPER::size_allocate ($w, $h);
697 delete $self->{texture}; 821 delete $self->{texture};
822}
823
824sub update {
825 my ($self) = @_;
826
827 delete $self->{texture};
828 $self->SUPER::update;
698} 829}
699 830
700sub _draw { 831sub _draw {
701 my ($self) = @_; 832 my ($self) = @_;
702 833
708 glEnable GL_BLEND; 839 glEnable GL_BLEND;
709 glEnable GL_TEXTURE_2D; 840 glEnable GL_TEXTURE_2D;
710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 841 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
711 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 842 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
712 843
713 glColor @{$self->{color}}; 844 glColor @{$self->{fg}};
714 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
715 $tex->draw_quad (0, 0); 851 $tex->draw_quad ($x, 0);
716 852
717 glDisable GL_BLEND; 853 glDisable GL_BLEND;
718 glDisable GL_TEXTURE_2D; 854 glDisable GL_TEXTURE_2D;
719} 855}
720 856
725our @ISA = CFClient::Widget::Label::; 861our @ISA = CFClient::Widget::Label::;
726 862
727use SDL; 863use SDL;
728use SDL::OpenGL; 864use SDL::OpenGL;
729 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
730sub key_down { 923sub key_down {
731 my ($self, $ev) = @_; 924 my ($self, $ev) = @_;
732 925
733 my $mod = $ev->key_mod; 926 my $mod = $ev->key_mod;
734 my $sym = $ev->key_sym; 927 my $sym = $ev->key_sym;
736 my $uni = $ev->key_unicode; 929 my $uni = $ev->key_unicode;
737 930
738 my $text = $self->get_text; 931 my $text = $self->get_text;
739 932
740 if ($sym == SDLK_BACKSPACE) { 933 if ($sym == SDLK_BACKSPACE) {
741 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};
742 } elsif ($uni) { 941 } elsif ($uni) {
743 $text .= chr $uni; 942 substr $text, $self->{cursor}++, 0, chr $uni;
744 } 943 }
745 944
746 $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;
747} 955}
748 956
749sub button_down { 957sub button_down {
750 my ($self, $ev) = @_; 958 my ($self, $ev, $x, $y) = @_;
751 959
752 $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;
753} 971}
754 972
755sub mouse_motion { 973sub mouse_motion {
756 my ($self, $ev, $x, $y) = @_; 974 my ($self, $ev, $x, $y) = @_;
757 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#
758} 976}
759 977
760sub _draw { 978sub _draw {
761 my ($self) = @_; 979 my ($self) = @_;
762 980
981 local $self->{fg} = $self->{fg};
982
763 if ($FOCUS == $self) { 983 if ($FOCUS == $self) {
764 glColor 1, 1, 1; 984 glColor @{$self->{active_bg}};
985 $self->{fg} = $self->{active_fg};
765 } else { 986 } else {
766 glColor 0.7, 0.7, 0.7; 987 glColor @{$self->{bg}};
767 } 988 }
768 989
769 glBegin GL_QUADS; 990 glBegin GL_QUADS;
770 glVertex 0 , 0; 991 glVertex 0 , 0;
771 glVertex 0 , $self->{h} - 1; 992 glVertex 0 , $self->{h};
772 glVertex $self->{w} - 1, $self->{h} - 1; 993 glVertex $self->{w}, $self->{h};
773 glVertex $self->{w} - 1, 0; 994 glVertex $self->{w}, 0;
774 glEnd; 995 glEnd;
775 996
776 $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;
777} 1121}
778 1122
779############################################################################# 1123#############################################################################
780 1124
781package CFClient::Widget::MapWidget; 1125package CFClient::Widget::MapWidget;
870 } 1214 }
871 1215
872 for my $num (grep $_, @$cell[1,2,3]) { 1216 for my $num (grep $_, @$cell[1,2,3]) {
873 my $tex = $::CONN->{face}[$num]{texture} || next; 1217 my $tex = $::CONN->{face}[$num]{texture} || next;
874 1218
875 my $w = $tex->{width}; 1219 my ($w, $h) = @$tex{qw(w h)};
876 my $h = $tex->{height};
877 1220
878 $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);
879 } 1222 }
880 } 1223 }
881 } 1224 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines