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.63 by root, Tue Apr 11 13:29:24 2006 UTC vs.
Revision 1.72 by root, Tue Apr 11 18:00:45 2006 UTC

73} 73}
74 74
75sub new { 75sub new {
76 my $class = shift; 76 my $class = shift;
77 77
78 bless { @_ }, $class 78 bless {
79 x => 0,
80 y => 0,
81 z => 0,
82 @_
83 }, $class
79} 84}
80 85
81sub move { 86sub move {
82 my ($self, $x, $y, $z) = @_; 87 my ($self, $x, $y, $z) = @_;
83 $self->{x} = $x; 88 $self->{x} = $x;
109} 114}
110 115
111sub focus_in { 116sub focus_in {
112 my ($self) = @_; 117 my ($self) = @_;
113 118
119 return if $FOCUS == $self;
120
114 my $focus = $FOCUS; $FOCUS = $self; 121 my $focus = $FOCUS; $FOCUS = $self;
115 $focus->update if $focus; 122 $focus->update if $focus;
116 $FOCUS->update; 123 $FOCUS->update;
117} 124}
118 125
125 $focus->update if $focus; #? 132 $focus->update if $focus; #?
126} 133}
127 134
128sub mouse_motion { } 135sub mouse_motion { }
129sub button_up { } 136sub button_up { }
130sub button_down { }
131sub key_down { } 137sub key_down { }
132sub key_up { } 138sub key_up { }
139
140sub button_down {
141 my ($self, $ev, $x, $y) = @_;
142
143 $self->focus_in;
144}
133 145
134sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 146sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
135sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 147sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
136sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 148sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
137sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 149sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
138sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 150sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
139 151
140sub draw { 152sub draw {
141 my ($self) = @_; 153 my ($self) = @_;
142 154
155 return unless $self->{h} && $self->{w};
156
143 glPushMatrix; 157 glPushMatrix;
144 glTranslate $self->{x}, $self->{y}, 0; 158 glTranslate $self->{x}, $self->{y}, 0;
145 $self->_draw; 159 $self->_draw;
160 glPopMatrix;
161
146 if ($self == $HOVER) { 162 if ($self == $HOVER) {
163 my ($x, $y) = @$self->{qw(x y)};
164
147 glColor 1, 1, 1, 0.4; 165 glColor 1, 1, 1, 0.1;
148 glEnable GL_BLEND; 166 glEnable GL_BLEND;
149 glBegin GL_QUADS; 167 glBegin GL_QUADS;
150 glVertex 0 , 0; 168 glVertex $x , $y;
151 glVertex $self->{w}, 0; 169 glVertex $x + $self->{w}, $y;
152 glVertex $self->{w}, $self->{h}; 170 glVertex $x + $self->{w}, $y + $self->{h};
153 glVertex 0 , $self->{h}; 171 glVertex $x , $y + $self->{h};
154 glEnd; 172 glEnd;
155 glDisable GL_BLEND; 173 glDisable GL_BLEND;
156 } 174 }
157 glPopMatrix;
158} 175}
159 176
160sub _draw { 177sub _draw {
161 my ($self) = @_; 178 my ($self) = @_;
162 179
202 219
203 $self->{parent}->update 220 $self->{parent}->update
204 if $self->{parent}; 221 if $self->{parent};
205} 222}
206 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
207sub DESTROY { 237sub DESTROY {
208 my ($self) = @_; 238 my ($self) = @_;
209 239
210 #$self->deactivate; 240 #$self->deactivate;
211} 241}
212 242
213############################################################################# 243#############################################################################
214 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 { }
289
290#############################################################################
291
215package CFClient::Widget::Container; 292package CFClient::Widget::Container;
216 293
217our @ISA = CFClient::Widget::; 294our @ISA = CFClient::Widget::;
218 295
219sub new { 296sub new {
220 my ($class, @widgets) = @_; 297 my ($class, %arg) = @_;
221 298
299 my $children = delete $arg{children} || [];
300
222 my $self = $class->SUPER::new (children => []); 301 my $self = $class->SUPER::new (children => [], %arg);
223 $self->add ($_) for @widgets; 302 $self->add ($_) for @$children;
224 303
225 $self 304 $self
226} 305}
227 306
228sub add { 307sub add {
229 my ($self, $chld, $expand) = @_; 308 my ($self, $chld, $expand) = @_;
230 309
231 $chld->{expand} = $expand; 310 $chld->{expand} = $expand;
232 $chld->set_parent ($self); 311 $chld->set_parent ($self);
233 312
234 @{$self->{children}} = 313 $self->{children} = [
235 sort { $a->{z} <=> $b->{z} } 314 sort { $a->{z} <=> $b->{z} }
236 @{$self->{children}}, $chld; 315 @{$self->{children}}, $chld
316 ];
237 317
238 $self->size_allocate ($self->{w}, $self->{h}) 318 $self->size_allocate ($self->{w}, $self->{h})
239 if $self->{w}; #TODO: check for "realised state" 319 if $self->{w}; #TODO: check for "realised state"
240} 320}
241 321
273 353
274package CFClient::Widget::Bin; 354package CFClient::Widget::Bin;
275 355
276our @ISA = CFClient::Widget::Container::; 356our @ISA = CFClient::Widget::Container::;
277 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
278sub child { $_[0]->{children}[0] } 383sub child { $_[0]->{children}[0] }
279 384
280sub size_request { 385sub size_request {
281 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 386 $_[0]{children}[0]->size_request
282} 387}
283 388
284sub size_allocate { 389sub size_allocate {
285 my ($self, $w, $h) = @_; 390 my ($self, $w, $h) = @_;
286 391
392 return unless $self->{w} != $w || $self->{h} != $h;
393
287 $self->SUPER::size_allocate ($w, $h); 394 $self->SUPER::size_allocate ($w, $h);
288 $self->{children}[0]->size_allocate ($w, $h) 395 $self->{children}[0]->size_allocate ($w, $h);
289 if $self->{children}[0]
290} 396}
291 397
292############################################################################# 398#############################################################################
293 399
294package CFClient::Widget::Window; 400package CFClient::Widget::Window;
296our @ISA = CFClient::Widget::Bin::; 402our @ISA = CFClient::Widget::Bin::;
297 403
298use SDL::OpenGL; 404use SDL::OpenGL;
299 405
300sub new { 406sub new {
301 my ($class, $x, $y, $z, $w, $h) = @_; 407 my ($class, %arg) = @_;
302 408
303 my $self = $class->SUPER::new; 409 my $self = $class->SUPER::new (%arg);
304
305 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
306} 410}
307 411
308sub update { 412sub update {
309 my ($self) = @_; 413 my ($self) = @_;
310 414
323} 427}
324 428
325sub size_allocate { 429sub size_allocate {
326 my ($self, $w, $h) = @_; 430 my ($self, $w, $h) = @_;
327 431
432 return unless $self->{w} != $w || $self->{h} != $h;
433
328 $self->{w} = $w; 434 $self->{w} = $w;
329 $self->{h} = $h; 435 $self->{h} = $h;
330 436
331 $self->child->size_allocate ($w, $h); 437 $self->child->size_allocate ($w, $h);
332 438
370} 476}
371 477
372sub size_allocate { 478sub size_allocate {
373 my ($self, $w, $h) = @_; 479 my ($self, $w, $h) = @_;
374 480
481 return unless $self->{w} != $w || $self->{h} != $h;
482
375 $self->{w} = $w; 483 $self->{w} = $w;
376 $self->{h} = $h; 484 $self->{h} = $h;
377 485
378 $self->child->size_allocate ($w - 4, $h - 4); 486 $self->child->size_allocate ($w - 4, $h - 4);
379 $self->child->move (2, 2); 487 $self->child->move (2, 2);
412sub size_request { 520sub size_request {
413 my ($self) = @_; 521 my ($self) = @_;
414 522
415 my ($w, $h) = $self->SUPER::size_request; 523 my ($w, $h) = $self->SUPER::size_request;
416 524
417 $h += $tex[1]->{height}; 525 $h += $tex[1]->{h};
418 $h += $tex[4]->{height}; 526 $h += $tex[4]->{h};
419 $w += $tex[2]->{width}; 527 $w += $tex[2]->{w};
420 $w += $tex[3]->{width}; 528 $w += $tex[3]->{w};
421 529
422 ($w, $h) 530 ($w, $h)
423} 531}
424 532
425sub size_allocate { 533sub size_allocate {
426 my ($self, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
427 535
536 return unless $self->{w} != $w || $self->{h} != $h;
537
428 $self->SUPER::size_allocate ($w, $h); 538 $self->SUPER::size_allocate ($w, $h);
429 539
430 $h -= $tex[1]->{height}; 540 $h -= $tex[1]->{h};
431 $h -= $tex[4]->{height}; 541 $h -= $tex[4]->{h};
432 $w -= $tex[2]->{width}; 542 $w -= $tex[2]->{w};
433 $w -= $tex[3]->{width}; 543 $w -= $tex[3]->{w};
434 544
435 $h = $h < 0 ? 0 : $h; 545 $h = $h < 0 ? 0 : $h;
436 $w = $w < 0 ? 0 : $w; 546 $w = $w < 0 ? 0 : $w;
437 547
548 my $child = $self->child;
549
438 $self->child->size_allocate ($w, $h); 550 $child->size_allocate ($w, $h);
439 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 551 $child->move ($tex[3]->{w}, $tex[1]->{h});
440} 552}
441 553
442sub _draw { 554sub _draw {
443 my ($self) = @_; 555 my ($self) = @_;
444 556
449 glEnable GL_TEXTURE_2D; 561 glEnable GL_TEXTURE_2D;
450 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 562 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
451 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
452 564
453 my $top = $tex[1]; 565 my $top = $tex[1];
454 $top->draw_quad (0, 0, $w, $top->{height}); 566 $top->draw_quad (0, 0, $w, $top->{h});
455 567
456 my $left = $tex[3]; 568 my $left = $tex[3];
457 $left->draw_quad (0, $top->{height}, $left->{width}, $ch); 569 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
458 570
459 my $right = $tex[2]; 571 my $right = $tex[2];
460 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); 572 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
461 573
462 my $bottom = $tex[4]; 574 my $bottom = $tex[4];
463 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); 575 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
464 576
465 my $bg = $tex[0]; 577 my $bg = $tex[0];
466 glBindTexture GL_TEXTURE_2D, $bg->{name}; 578 glBindTexture GL_TEXTURE_2D, $bg->{name};
467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 579 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 580 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
469 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 581 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
470 582
471 my $rep_x = $cw / $bg->{width}; 583 my $rep_x = $cw / $bg->{w};
472 my $rep_y = $ch / $bg->{height}; 584 my $rep_y = $ch / $bg->{h};
473 585
474 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); 586 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
475 587
476 glDisable GL_BLEND; 588 glDisable GL_BLEND;
477 glDisable GL_TEXTURE_2D; 589 glDisable GL_TEXTURE_2D;
478 590
479 $self->child->draw; 591 $self->child->draw;
482 594
483############################################################################# 595#############################################################################
484 596
485package CFClient::Widget::Table; 597package CFClient::Widget::Table;
486 598
487our @ISA = CFClient::Widget::Bin::; 599our @ISA = CFClient::Widget::;
488 600
489use SDL::OpenGL; 601use SDL::OpenGL;
490 602
491sub add { 603sub add {
492 my ($self, $x, $y, $chld) = @_; 604 my ($self, $x, $y, $chld) = @_;
587} 699}
588 700
589sub size_allocate { 701sub size_allocate {
590 my ($self, $w, $h) = @_; 702 my ($self, $w, $h) = @_;
591 703
704 return unless $self->{w} != $w || $self->{h} != $h;
705
592 $self->w ($w); 706 $self->{w} = $w;
593 $self->h ($h); 707 $self->{h} = $h;
594 708
595 my $exp; 709 return unless $self->{h};
596 my @oth; 710
597 # find expand widget 711 my $children = $self->{children};
598 for (@{$self->{children}}) { 712
599 if ($_->{expand}) { 713 my @h = map +($_->size_request)[1], @$children;
600 $exp = $_; 714
601 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;
602 } 732 }
603 push @oth, $_;
604 }
605
606 my ($ow, $oh);
607
608 # get sizes of other widgets
609 for (@oth) {
610 my ($w, $h) = $_->size_request;
611 $oh += $h;
612 if ($ow < $w) { $ow = $w }
613 } 733 }
614 734
615 my $y = 0; 735 my $y = 0;
616 for (@{$self->{children}}) { 736 for (0 .. $#$children) {
737 my $child = $children->[$_];
738 my $h = $h[$_];
617 $_->move (0, $y); 739 $child->move (0, $y);
618
619 if ($_ == $exp) {
620 $_->size_allocate ($w, $h - $oh);
621 $y += $h - $oh;
622 } else {
623 my ($cw, $h) = $_->size_request;
624 $_->size_allocate ($w, $h); 740 $child->size_allocate ($w, $h);
741
625 $y += $h; 742 $y += $h;
626 }
627 } 743 }
628} 744}
629 745
630############################################################################# 746#############################################################################
631 747
634our @ISA = CFClient::Widget::; 750our @ISA = CFClient::Widget::;
635 751
636use SDL::OpenGL; 752use SDL::OpenGL;
637 753
638sub new { 754sub new {
639 my ($class, $x, $y, $z, $height, $text) = @_; 755 my ($class, %arg) = @_;
640 756
641 $height ||= $::FONTSIZE;
642
643 # TODO: color, and make height, xyz etc. optional
644 my $self = $class->SUPER::new ( 757 my $self = $class->SUPER::new (
645 color => [1, 1, 1], 758 fg => [1, 1, 1],
646 x => $x, 759 height => $::FONTSIZE,
647 y => $y, 760 text => "",
648 z => $z, 761 align => -1,
649 height => $height,
650 layout => new CFClient::Layout $height, 762 layout => new CFClient::Layout,
763 %arg
651 ); 764 );
652 765
653 $self->set_text ($text); 766 $self->set_text ($self->{text});
654 767
655 $self 768 $self
769}
770
771sub escape_text {
772 local $_ = $_[1];
773
774 s/&/&amp;/g;
775 s/>/&gt;/g;
776 s/</&lt;/g;
777
778 $_[1]
656} 779}
657 780
658sub set_text { 781sub set_text {
659 my ($self, $text) = @_; 782 my ($self, $text) = @_;
660 783
661 $self->{text} = $text; 784 $self->{text} = $text;
662 $self->{layout}->set_markup ($text); 785 $self->{layout}->set_markup ($text);
663 786
664 delete $self->{texture}; 787 delete $self->{texture};
788 $self->update;
665} 789}
666 790
667sub get_text { 791sub get_text {
668 my ($self, $text) = @_; 792 my ($self, $text) = @_;
669 793
672 796
673sub size_request { 797sub size_request {
674 my ($self) = @_; 798 my ($self) = @_;
675 799
676 $self->{layout}->set_width; 800 $self->{layout}->set_width;
801 $self->{layout}->set_height ($self->{height});
677 $self->{layout}->size 802 $self->{layout}->size
678# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 803# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
679# ( 804# (
680# $self->{texture}{width}, 805# $self->{texture}{w},
681# $self->{texture}{height}, 806# $self->{texture}{h},
682# ) 807# )
683# } else { 808# } else {
684# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; 809# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
685# 810#
686# ($w, $h) 811# ($w, $h)
687# } 812# }
688} 813}
689 814
690sub size_allocate { 815sub size_allocate {
691 my ($self, $w, $h) = @_; 816 my ($self, $w, $h) = @_;
692 817
818 return unless $self->{w} != $w || $self->{h} != $h;
819
693 $self->SUPER::size_allocate ($w, $h); 820 $self->SUPER::size_allocate ($w, $h);
694 delete $self->{texture}; 821 delete $self->{texture};
822}
823
824sub update {
825 my ($self) = @_;
826
827 delete $self->{texture};
828 $self->SUPER::update;
695} 829}
696 830
697sub _draw { 831sub _draw {
698 my ($self) = @_; 832 my ($self) = @_;
699 833
705 glEnable GL_BLEND; 839 glEnable GL_BLEND;
706 glEnable GL_TEXTURE_2D; 840 glEnable GL_TEXTURE_2D;
707 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 841 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
708 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 842 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
709 843
710 glColor @{$self->{color}}; 844 glColor @{$self->{fg}};
711 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
712 $tex->draw_quad (0, 0); 851 $tex->draw_quad ($x, 0);
713 852
714 glDisable GL_BLEND; 853 glDisable GL_BLEND;
715 glDisable GL_TEXTURE_2D; 854 glDisable GL_TEXTURE_2D;
716} 855}
717 856
722our @ISA = CFClient::Widget::Label::; 861our @ISA = CFClient::Widget::Label::;
723 862
724use SDL; 863use SDL;
725use SDL::OpenGL; 864use SDL::OpenGL;
726 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
727sub key_down { 923sub key_down {
728 my ($self, $ev) = @_; 924 my ($self, $ev) = @_;
729 925
730 my $mod = $ev->key_mod; 926 my $mod = $ev->key_mod;
731 my $sym = $ev->key_sym; 927 my $sym = $ev->key_sym;
733 my $uni = $ev->key_unicode; 929 my $uni = $ev->key_unicode;
734 930
735 my $text = $self->get_text; 931 my $text = $self->get_text;
736 932
737 if ($sym == SDLK_BACKSPACE) { 933 if ($sym == SDLK_BACKSPACE) {
738 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};
739 } elsif ($uni) { 941 } elsif ($uni) {
740 $text .= chr $uni; 942 substr $text, $self->{cursor}++, 0, chr $uni;
741 } 943 }
742 944
743 $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;
744} 955}
745 956
746sub button_down { 957sub button_down {
747 my ($self, $ev) = @_; 958 my ($self, $ev, $x, $y) = @_;
748 959
749 $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;
750} 971}
751 972
752sub mouse_motion { 973sub mouse_motion {
753 my ($self, $ev, $x, $y) = @_; 974 my ($self, $ev, $x, $y) = @_;
754 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#
755} 976}
756 977
757sub _draw { 978sub _draw {
758 my ($self) = @_; 979 my ($self) = @_;
759 980
981 local $self->{fg} = $self->{fg};
982
760 if ($FOCUS == $self) { 983 if ($FOCUS == $self) {
761 glColor 1, 1, 1; 984 glColor @{$self->{active_bg}};
985 $self->{fg} = $self->{active_fg};
762 } else { 986 } else {
763 glColor 0.7, 0.7, 0.7; 987 glColor @{$self->{bg}};
764 } 988 }
765 989
766 glBegin GL_QUADS; 990 glBegin GL_QUADS;
767 glVertex 0 , 0; 991 glVertex 0 , 0;
768 glVertex 0 , $self->{h} - 1; 992 glVertex 0 , $self->{h};
769 glVertex $self->{w} - 1, $self->{h} - 1; 993 glVertex $self->{w}, $self->{h};
770 glVertex $self->{w} - 1, 0; 994 glVertex $self->{w}, 0;
771 glEnd; 995 glEnd;
772 996
773 $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;
774} 1121}
775 1122
776############################################################################# 1123#############################################################################
777 1124
778package CFClient::Widget::MapWidget; 1125package CFClient::Widget::MapWidget;
784use SDL; 1131use SDL;
785use SDL::OpenGL; 1132use SDL::OpenGL;
786use SDL::OpenGL::Constants; 1133use SDL::OpenGL::Constants;
787 1134
788our @ISA = CFClient::Widget::; 1135our @ISA = CFClient::Widget::;
1136
1137sub new {
1138 my $class = shift;
1139
1140 $class->SUPER::new (
1141 z => -1,
1142 list => (glGenLists 1),
1143 @_
1144 )
1145}
789 1146
790sub key_down { 1147sub key_down {
791 print "MAPKEYDOWN\n"; 1148 print "MAPKEYDOWN\n";
792} 1149}
793 1150
799 1 + int $::WIDTH / 32, 1156 1 + int $::WIDTH / 32,
800 1 + int $::HEIGHT / 32, 1157 1 + int $::HEIGHT / 32,
801 ) 1158 )
802} 1159}
803 1160
1161sub update {
1162 my ($self) = @_;
1163
1164 $self->{need_update} = 1;
1165}
1166
804sub _draw { 1167sub _draw {
805 my ($self) = @_; 1168 my ($self) = @_;
806 1169
1170 if (delete $self->{need_update}) {
1171 glNewList $self->{list}, GL_COMPILE;
1172
807 my $mx = $::CONN->{mapx}; 1173 my $mx = $::CONN->{mapx};
808 my $my = $::CONN->{mapy}; 1174 my $my = $::CONN->{mapy};
809 1175
810 my $map = $::CONN->{map}; 1176 my $map = $::CONN->{map};
811 1177
812 my ($xofs, $yofs); 1178 my ($xofs, $yofs);
813 1179
814 my $sw = 1 + int $::WIDTH / 32; 1180 my $sw = 1 + int $::WIDTH / 32;
815 my $sh = 1 + int $::HEIGHT / 32; 1181 my $sh = 1 + int $::HEIGHT / 32;
816 1182
817 if ($::CONN->{mapw} > $sw) { 1183 if ($::CONN->{mapw} > $sw) {
818 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1184 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
819 } else { 1185 } else {
820 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 1186 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
821 } 1187 }
822 1188
823 if ($::CONN->{maph} > $sh) { 1189 if ($::CONN->{maph} > $sh) {
824 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1190 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
825 } else { 1191 } else {
826 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1192 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
827 } 1193 }
828 1194
829 glEnable GL_TEXTURE_2D; 1195 glEnable GL_TEXTURE_2D;
830 glEnable GL_BLEND; 1196 glEnable GL_BLEND;
831 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1197 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
832 1198
833 my $sw4 = ($sw + 3) & ~3; 1199 my $sw4 = ($sw + 3) & ~3;
834 my $darkness = "\x00" x ($sw4 * $sh); 1200 my $darkness = "\x00" x ($sw4 * $sh);
835 1201
836 for my $x (0 .. $sw - 1) { 1202 for my $x (0 .. $sw - 1) {
837 my $row = $map->[$x + $xofs]; 1203 my $row = $map->[$x + $xofs];
838 for my $y (0 .. $sh - 1) { 1204 for my $y (0 .. $sh - 1) {
839 1205
840 my $cell = $row->[$y + $yofs] 1206 my $cell = $row->[$y + $yofs]
841 or next; 1207 or next;
842 1208
843 my $dark = $cell->[0]; 1209 my $dark = $cell->[0];
844 if ($dark < 0) { 1210 if ($dark < 0) {
845 substr $darkness, $y * $sw4 + $x, 1, chr 224; 1211 substr $darkness, $y * $sw4 + $x, 1, chr 224;
846 } else { 1212 } else {
847 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; 1213 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
848 } 1214 }
849 1215
850 for my $num (grep $_, @$cell[1,2,3]) { 1216 for my $num (grep $_, @$cell[1,2,3]) {
851 my $tex = $::CONN->{face}[$num]{texture} || next; 1217 my $tex = $::CONN->{face}[$num]{texture} || next;
852 1218
853 my $w = $tex->{width}; 1219 my ($w, $h) = @$tex{qw(w h)};
854 my $h = $tex->{height};
855 1220
856 $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);
1222 }
857 } 1223 }
858 } 1224 }
859 }
860 1225
861# if (1) { # higher quality darkness 1226# if (1) { # higher quality darkness
862# $lighting =~ s/(.)/$1$1$1/gs; 1227# $lighting =~ s/(.)/$1$1$1/gs;
863# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; 1228# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
864# 1229#
866# 1231#
867# $lighting = $pb->get_pixels; 1232# $lighting = $pb->get_pixels;
868# $lighting =~ s/(.)../$1/gs; 1233# $lighting =~ s/(.)../$1/gs;
869# } 1234# }
870 1235
871 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1236 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1237 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
873 1238
874 $darkness = new CFClient::Texture 1239 $darkness = new CFClient::Texture
875 width => $sw4, 1240 width => $sw4,
876 height => $sh, 1241 height => $sh,
877 data => $darkness, 1242 data => $darkness,
878 internalformat => GL_ALPHA, 1243 internalformat => GL_ALPHA,
879 format => GL_ALPHA; 1244 format => GL_ALPHA;
880 1245
881 glColor 0.45, 0.45, 0.45, 1; 1246 glColor 0.45, 0.45, 0.45, 1;
882 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); 1247 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
883 1248
884 glDisable GL_TEXTURE_2D; 1249 glDisable GL_TEXTURE_2D;
885 glDisable GL_BLEND; 1250 glDisable GL_BLEND;
1251
1252 glEndList;
1253 }
1254
1255 glCallList $self->{list};
886} 1256}
887 1257
888my %DIR = ( 1258my %DIR = (
889 SDLK_KP8, [1, "north"], 1259 SDLK_KP8, [1, "north"],
890 SDLK_KP9, [2, "northeast"], 1260 SDLK_KP9, [2, "northeast"],

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines