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.60 by root, Tue Apr 11 13:14:36 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
415 # we want to do this delayed...
311 $self->render_chld; 416 $self->render_chld;
312 $self->SUPER::update; 417 $self->SUPER::update;
313} 418}
314 419
315sub render_chld { 420sub render_chld {
322} 427}
323 428
324sub size_allocate { 429sub size_allocate {
325 my ($self, $w, $h) = @_; 430 my ($self, $w, $h) = @_;
326 431
432 return unless $self->{w} != $w || $self->{h} != $h;
433
327 $self->{w} = $w; 434 $self->{w} = $w;
328 $self->{h} = $h; 435 $self->{h} = $h;
329 436
330 $self->child->size_allocate ($w, $h); 437 $self->child->size_allocate ($w, $h);
331 438
369} 476}
370 477
371sub size_allocate { 478sub size_allocate {
372 my ($self, $w, $h) = @_; 479 my ($self, $w, $h) = @_;
373 480
481 return unless $self->{w} != $w || $self->{h} != $h;
482
374 $self->{w} = $w; 483 $self->{w} = $w;
375 $self->{h} = $h; 484 $self->{h} = $h;
376 485
377 $self->child->size_allocate ($w - 4, $h - 4); 486 $self->child->size_allocate ($w - 4, $h - 4);
378 $self->child->move (2, 2); 487 $self->child->move (2, 2);
411sub size_request { 520sub size_request {
412 my ($self) = @_; 521 my ($self) = @_;
413 522
414 my ($w, $h) = $self->SUPER::size_request; 523 my ($w, $h) = $self->SUPER::size_request;
415 524
416 $h += $tex[1]->{height}; 525 $h += $tex[1]->{h};
417 $h += $tex[4]->{height}; 526 $h += $tex[4]->{h};
418 $w += $tex[2]->{width}; 527 $w += $tex[2]->{w};
419 $w += $tex[3]->{width}; 528 $w += $tex[3]->{w};
420 529
421 ($w, $h) 530 ($w, $h)
422} 531}
423 532
424sub size_allocate { 533sub size_allocate {
425 my ($self, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
426 535
536 return unless $self->{w} != $w || $self->{h} != $h;
537
427 $self->SUPER::size_allocate ($w, $h); 538 $self->SUPER::size_allocate ($w, $h);
428 539
429 $h -= $tex[1]->{height}; 540 $h -= $tex[1]->{h};
430 $h -= $tex[4]->{height}; 541 $h -= $tex[4]->{h};
431 $w -= $tex[2]->{width}; 542 $w -= $tex[2]->{w};
432 $w -= $tex[3]->{width}; 543 $w -= $tex[3]->{w};
433 544
434 $h = $h < 0 ? 0 : $h; 545 $h = $h < 0 ? 0 : $h;
435 $w = $w < 0 ? 0 : $w; 546 $w = $w < 0 ? 0 : $w;
436 547
548 my $child = $self->child;
549
437 $self->child->size_allocate ($w, $h); 550 $child->size_allocate ($w, $h);
438 $self->child->move ($tex[3]->{width}, $tex[1]->{height}); 551 $child->move ($tex[3]->{w}, $tex[1]->{h});
439} 552}
440 553
441sub _draw { 554sub _draw {
442 my ($self) = @_; 555 my ($self) = @_;
443 556
448 glEnable GL_TEXTURE_2D; 561 glEnable GL_TEXTURE_2D;
449 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 562 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
450 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
451 564
452 my $top = $tex[1]; 565 my $top = $tex[1];
453 $top->draw_quad (0, 0, $w, $top->{height}); 566 $top->draw_quad (0, 0, $w, $top->{h});
454 567
455 my $left = $tex[3]; 568 my $left = $tex[3];
456 $left->draw_quad (0, $top->{height}, $left->{width}, $ch); 569 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
457 570
458 my $right = $tex[2]; 571 my $right = $tex[2];
459 $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); 572 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
460 573
461 my $bottom = $tex[4]; 574 my $bottom = $tex[4];
462 $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); 575 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
463 576
464 my $bg = $tex[0]; 577 my $bg = $tex[0];
465 glBindTexture GL_TEXTURE_2D, $bg->{name}; 578 glBindTexture GL_TEXTURE_2D, $bg->{name};
466 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 579 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
467 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 580 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
468 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 581 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
469 582
470 my $rep_x = $cw / $bg->{width}; 583 my $rep_x = $cw / $bg->{w};
471 my $rep_y = $ch / $bg->{height}; 584 my $rep_y = $ch / $bg->{h};
472 585
473 $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); 586 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
474 587
475 glDisable GL_BLEND; 588 glDisable GL_BLEND;
476 glDisable GL_TEXTURE_2D; 589 glDisable GL_TEXTURE_2D;
477 590
478 $self->child->draw; 591 $self->child->draw;
481 594
482############################################################################# 595#############################################################################
483 596
484package CFClient::Widget::Table; 597package CFClient::Widget::Table;
485 598
486our @ISA = CFClient::Widget::Bin::; 599our @ISA = CFClient::Widget::;
487 600
488use SDL::OpenGL; 601use SDL::OpenGL;
489 602
490sub add { 603sub add {
491 my ($self, $x, $y, $chld) = @_; 604 my ($self, $x, $y, $chld) = @_;
586} 699}
587 700
588sub size_allocate { 701sub size_allocate {
589 my ($self, $w, $h) = @_; 702 my ($self, $w, $h) = @_;
590 703
704 return unless $self->{w} != $w || $self->{h} != $h;
705
591 $self->w ($w); 706 $self->{w} = $w;
592 $self->h ($h); 707 $self->{h} = $h;
593 708
594 my $exp; 709 return unless $self->{h};
595 my @oth; 710
596 # find expand widget 711 my $children = $self->{children};
597 for (@{$self->{children}}) { 712
598 if ($_->{expand}) { 713 my @h = map +($_->size_request)[1], @$children;
599 $exp = $_; 714
600 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;
601 } 732 }
602 push @oth, $_;
603 }
604
605 my ($ow, $oh);
606
607 # get sizes of other widgets
608 for (@oth) {
609 my ($w, $h) = $_->size_request;
610 $oh += $h;
611 if ($ow < $w) { $ow = $w }
612 } 733 }
613 734
614 my $y = 0; 735 my $y = 0;
615 for (@{$self->{children}}) { 736 for (0 .. $#$children) {
737 my $child = $children->[$_];
738 my $h = $h[$_];
616 $_->move (0, $y); 739 $child->move (0, $y);
617
618 if ($_ == $exp) {
619 $_->size_allocate ($w, $h - $oh);
620 $y += $h - $oh;
621 } else {
622 my ($cw, $h) = $_->size_request;
623 $_->size_allocate ($w, $h); 740 $child->size_allocate ($w, $h);
741
624 $y += $h; 742 $y += $h;
625 }
626 } 743 }
627} 744}
628 745
629############################################################################# 746#############################################################################
630 747
633our @ISA = CFClient::Widget::; 750our @ISA = CFClient::Widget::;
634 751
635use SDL::OpenGL; 752use SDL::OpenGL;
636 753
637sub new { 754sub new {
638 my ($class, $x, $y, $z, $height, $text) = @_; 755 my ($class, %arg) = @_;
639 756
640 $height ||= $::FONTSIZE;
641
642 # TODO: color, and make height, xyz etc. optional
643 my $self = $class->SUPER::new ( 757 my $self = $class->SUPER::new (
644 x => $x, 758 fg => [1, 1, 1],
645 y => $y, 759 height => $::FONTSIZE,
646 z => $z, 760 text => "",
647 height => $height, 761 align => -1,
648 layout => new CFClient::Layout, 762 layout => new CFClient::Layout,
763 %arg
649 ); 764 );
650 765
651 $self->set_text ($text); 766 $self->set_text ($self->{text});
652 767
653 $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]
654} 779}
655 780
656sub set_text { 781sub set_text {
657 my ($self, $text) = @_; 782 my ($self, $text) = @_;
658 783
659 $self->{text} = $text; 784 $self->{text} = $text;
660 $self->{layout}->set_markup ($text); 785 $self->{layout}->set_markup ($text);
661 786
662 delete $self->{texture}; 787 delete $self->{texture};
788 $self->update;
663} 789}
664 790
665sub get_text { 791sub get_text {
666 my ($self, $text) = @_; 792 my ($self, $text) = @_;
667 793
670 796
671sub size_request { 797sub size_request {
672 my ($self) = @_; 798 my ($self) = @_;
673 799
674 $self->{layout}->set_width; 800 $self->{layout}->set_width;
801 $self->{layout}->set_height ($self->{height});
675 $self->{layout}->size 802 $self->{layout}->size
676# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 803# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
677# ( 804# (
678# $self->{texture}{width}, 805# $self->{texture}{w},
679# $self->{texture}{height}, 806# $self->{texture}{h},
680# ) 807# )
681# } else { 808# } else {
682# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height}; 809# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
683# 810#
684# ($w, $h) 811# ($w, $h)
685# } 812# }
686} 813}
687 814
688sub size_allocate { 815sub size_allocate {
689 my ($self, $w, $h) = @_; 816 my ($self, $w, $h) = @_;
690 817
818 return unless $self->{w} != $w || $self->{h} != $h;
819
691 $self->SUPER::size_allocate ($w, $h); 820 $self->SUPER::size_allocate ($w, $h);
692 delete $self->{texture}; 821 delete $self->{texture};
822}
823
824sub update {
825 my ($self) = @_;
826
827 delete $self->{texture};
828 $self->SUPER::update;
693} 829}
694 830
695sub _draw { 831sub _draw {
696 my ($self) = @_; 832 my ($self) = @_;
697 833
703 glEnable GL_BLEND; 839 glEnable GL_BLEND;
704 glEnable GL_TEXTURE_2D; 840 glEnable GL_TEXTURE_2D;
705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 841 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 842 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
707 843
708 glColor 1, 0, 0, 1; # TODO color 844 glColor @{$self->{fg}};
709 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
710 $tex->draw_quad (0, 0); 851 $tex->draw_quad ($x, 0);
711 852
712 glDisable GL_BLEND; 853 glDisable GL_BLEND;
713 glDisable GL_TEXTURE_2D; 854 glDisable GL_TEXTURE_2D;
714} 855}
715 856
720our @ISA = CFClient::Widget::Label::; 861our @ISA = CFClient::Widget::Label::;
721 862
722use SDL; 863use SDL;
723use SDL::OpenGL; 864use SDL::OpenGL;
724 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
725sub key_down { 923sub key_down {
726 my ($self, $ev) = @_; 924 my ($self, $ev) = @_;
727 925
728 my $mod = $ev->key_mod; 926 my $mod = $ev->key_mod;
729 my $sym = $ev->key_sym; 927 my $sym = $ev->key_sym;
731 my $uni = $ev->key_unicode; 929 my $uni = $ev->key_unicode;
732 930
733 my $text = $self->get_text; 931 my $text = $self->get_text;
734 932
735 if ($sym == SDLK_BACKSPACE) { 933 if ($sym == SDLK_BACKSPACE) {
736 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};
737 } elsif ($uni) { 941 } elsif ($uni) {
738 $text .= chr $uni; 942 substr $text, $self->{cursor}++, 0, chr $uni;
739 } 943 }
740 944
741 $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;
742} 955}
743 956
744sub button_down { 957sub button_down {
745 my ($self, $ev) = @_; 958 my ($self, $ev, $x, $y) = @_;
746 959
747 $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;
748} 971}
749 972
750sub mouse_motion { 973sub mouse_motion {
751 my ($self, $ev, $x, $y) = @_; 974 my ($self, $ev, $x, $y) = @_;
752 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#
753} 976}
754 977
755sub _draw { 978sub _draw {
756 my ($self) = @_; 979 my ($self) = @_;
757 980
981 local $self->{fg} = $self->{fg};
982
758 if ($FOCUS == $self) { 983 if ($FOCUS == $self) {
759 glColor 1, 1, 1; 984 glColor @{$self->{active_bg}};
985 $self->{fg} = $self->{active_fg};
760 } else { 986 } else {
761 glColor 0.7, 0.7, 0.7; 987 glColor @{$self->{bg}};
762 } 988 }
763 989
764 glBegin GL_QUADS; 990 glBegin GL_QUADS;
765 glVertex 0 , 0; 991 glVertex 0 , 0;
766 glVertex 0 , $self->{h} - 1; 992 glVertex 0 , $self->{h};
767 glVertex $self->{w} - 1, $self->{h} - 1; 993 glVertex $self->{w}, $self->{h};
768 glVertex $self->{w} - 1, 0; 994 glVertex $self->{w}, 0;
769 glEnd; 995 glEnd;
770 996
771 $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;
772} 1121}
773 1122
774############################################################################# 1123#############################################################################
775 1124
776package CFClient::Widget::MapWidget; 1125package CFClient::Widget::MapWidget;
782use SDL; 1131use SDL;
783use SDL::OpenGL; 1132use SDL::OpenGL;
784use SDL::OpenGL::Constants; 1133use SDL::OpenGL::Constants;
785 1134
786our @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}
787 1146
788sub key_down { 1147sub key_down {
789 print "MAPKEYDOWN\n"; 1148 print "MAPKEYDOWN\n";
790} 1149}
791 1150
797 1 + int $::WIDTH / 32, 1156 1 + int $::WIDTH / 32,
798 1 + int $::HEIGHT / 32, 1157 1 + int $::HEIGHT / 32,
799 ) 1158 )
800} 1159}
801 1160
1161sub update {
1162 my ($self) = @_;
1163
1164 $self->{need_update} = 1;
1165}
1166
802sub _draw { 1167sub _draw {
803 my ($self) = @_; 1168 my ($self) = @_;
804 1169
1170 if (delete $self->{need_update}) {
1171 glNewList $self->{list}, GL_COMPILE;
1172
805 my $mx = $::CONN->{mapx}; 1173 my $mx = $::CONN->{mapx};
806 my $my = $::CONN->{mapy}; 1174 my $my = $::CONN->{mapy};
807 1175
808 my $map = $::CONN->{map}; 1176 my $map = $::CONN->{map};
809 1177
810 my ($xofs, $yofs); 1178 my ($xofs, $yofs);
811 1179
812 my $sw = 1 + int $::WIDTH / 32; 1180 my $sw = 1 + int $::WIDTH / 32;
813 my $sh = 1 + int $::HEIGHT / 32; 1181 my $sh = 1 + int $::HEIGHT / 32;
814 1182
815 if ($::CONN->{mapw} > $sw) { 1183 if ($::CONN->{mapw} > $sw) {
816 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1184 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
817 } else { 1185 } else {
818 $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};
819 } 1187 }
820 1188
821 if ($::CONN->{maph} > $sh) { 1189 if ($::CONN->{maph} > $sh) {
822 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 1190 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
823 } else { 1191 } else {
824 $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};
825 } 1193 }
826 1194
827 glEnable GL_TEXTURE_2D; 1195 glEnable GL_TEXTURE_2D;
828 glEnable GL_BLEND; 1196 glEnable GL_BLEND;
829 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1197 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
830 1198
831 my $sw4 = ($sw + 3) & ~3; 1199 my $sw4 = ($sw + 3) & ~3;
832 my $darkness = "\x00" x ($sw4 * $sh); 1200 my $darkness = "\x00" x ($sw4 * $sh);
833 1201
834 for my $x (0 .. $sw - 1) { 1202 for my $x (0 .. $sw - 1) {
835 my $row = $map->[$x + $xofs]; 1203 my $row = $map->[$x + $xofs];
836 for my $y (0 .. $sh - 1) { 1204 for my $y (0 .. $sh - 1) {
837 1205
838 my $cell = $row->[$y + $yofs] 1206 my $cell = $row->[$y + $yofs]
839 or next; 1207 or next;
840 1208
841 my $dark = $cell->[0]; 1209 my $dark = $cell->[0];
842 if ($dark < 0) { 1210 if ($dark < 0) {
843 substr $darkness, $y * $sw4 + $x, 1, chr 224; 1211 substr $darkness, $y * $sw4 + $x, 1, chr 224;
844 } else { 1212 } else {
845 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; 1213 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
846 } 1214 }
847 1215
848 for my $num (grep $_, @$cell[1,2,3]) { 1216 for my $num (grep $_, @$cell[1,2,3]) {
849 my $tex = $::CONN->{face}[$num]{texture} || next; 1217 my $tex = $::CONN->{face}[$num]{texture} || next;
850 1218
851 my $w = $tex->{width}; 1219 my ($w, $h) = @$tex{qw(w h)};
852 my $h = $tex->{height};
853 1220
854 $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 }
855 } 1223 }
856 } 1224 }
857 }
858 1225
859# if (1) { # higher quality darkness 1226# if (1) { # higher quality darkness
860# $lighting =~ s/(.)/$1$1$1/gs; 1227# $lighting =~ s/(.)/$1$1$1/gs;
861# 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;
862# 1229#
864# 1231#
865# $lighting = $pb->get_pixels; 1232# $lighting = $pb->get_pixels;
866# $lighting =~ s/(.)../$1/gs; 1233# $lighting =~ s/(.)../$1/gs;
867# } 1234# }
868 1235
869 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1236 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
870 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1237 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
871 1238
872 $darkness = new CFClient::Texture 1239 $darkness = new CFClient::Texture
873 width => $sw4, 1240 width => $sw4,
874 height => $sh, 1241 height => $sh,
875 data => $darkness, 1242 data => $darkness,
876 internalformat => GL_ALPHA, 1243 internalformat => GL_ALPHA,
877 format => GL_ALPHA; 1244 format => GL_ALPHA;
878 1245
879 glColor 0.45, 0.45, 0.45, 1; 1246 glColor 0.45, 0.45, 0.45, 1;
880 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); 1247 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
881 1248
882 glDisable GL_TEXTURE_2D; 1249 glDisable GL_TEXTURE_2D;
883 glDisable GL_BLEND; 1250 glDisable GL_BLEND;
1251
1252 glEndList;
1253 }
1254
1255 glCallList $self->{list};
884} 1256}
885 1257
886my %DIR = ( 1258my %DIR = (
887 SDLK_KP8, [1, "north"], 1259 SDLK_KP8, [1, "north"],
888 SDLK_KP9, [2, "northeast"], 1260 SDLK_KP9, [2, "northeast"],

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines