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.57 by root, Mon Apr 10 22:53:49 2006 UTC vs.
Revision 1.63 by root, Tue Apr 11 13:29:24 2006 UTC

1package Crossfire::Client::Widget; 1package CFClient::Widget;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9 9
10use Crossfire::Client; 10use CFClient;
11 11
12our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
13 13
14our $TOPLEVEL; 14our $TOPLEVEL;
15our $BUTTON_STATE; 15our $BUTTON_STATE;
34 $GRAB->update if $GRAB; 34 $GRAB->update if $GRAB;
35 } 35 }
36 36
37 $BUTTON_STATE |= 1 << ($ev->button - 1); 37 $BUTTON_STATE |= 1 << ($ev->button - 1);
38 38
39 $GRAB->button_down ($ev) if $GRAB; 39 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
40} 40}
41 41
42sub feed_sdl_button_up_event { 42sub feed_sdl_button_up_event {
43 my ($ev) = @_; 43 my ($ev) = @_;
44 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 44 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
45 45
46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 46 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y);
47 47
48 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 48 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
49
50 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB;
49 51
50 if (!$BUTTON_STATE) { 52 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 53 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 54 $grab->update if $grab;
53 $GRAB->update if $GRAB; 55 $GRAB->update if $GRAB;
65 67
66 $hover->update if $hover; 68 $hover->update if $hover;
67 $HOVER->update if $HOVER; 69 $HOVER->update if $HOVER;
68 } 70 }
69 71
70 $HOVER->mouse_motion ($ev) if $HOVER; 72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
71} 73}
72 74
73sub new { 75sub new {
74 my $class = shift; 76 my $class = shift;
75 77
95sub size_allocate { 97sub size_allocate {
96 my ($self, $w, $h) = @_; 98 my ($self, $w, $h) = @_;
97 99
98 $self->{w} = $w; 100 $self->{w} = $w;
99 $self->{h} = $h; 101 $self->{h} = $h;
102}
103
104# translate global koordinates to local coordinate system
105sub translate {
106 my ($self, $x, $y) = @_;
107
108 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
100} 109}
101 110
102sub focus_in { 111sub focus_in {
103 my ($self) = @_; 112 my ($self) = @_;
104 113
201 #$self->deactivate; 210 #$self->deactivate;
202} 211}
203 212
204############################################################################# 213#############################################################################
205 214
206package Crossfire::Client::Widget::Container; 215package CFClient::Widget::Container;
207 216
208our @ISA = Crossfire::Client::Widget::; 217our @ISA = CFClient::Widget::;
209 218
210sub new { 219sub new {
211 my ($class, @widgets) = @_; 220 my ($class, @widgets) = @_;
212 221
213 my $self = $class->SUPER::new (children => []); 222 my $self = $class->SUPER::new (children => []);
260 $_->draw for @{$self->{children}}; 269 $_->draw for @{$self->{children}};
261} 270}
262 271
263############################################################################# 272#############################################################################
264 273
265package Crossfire::Client::Widget::Bin; 274package CFClient::Widget::Bin;
266 275
267our @ISA = Crossfire::Client::Widget::Container::; 276our @ISA = CFClient::Widget::Container::;
268 277
269sub child { $_[0]->{children}[0] } 278sub child { $_[0]->{children}[0] }
270 279
271sub size_request { 280sub size_request {
272 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 281 $_[0]{children}[0]->size_request if $_[0]{children}[0];
280 if $self->{children}[0] 289 if $self->{children}[0]
281} 290}
282 291
283############################################################################# 292#############################################################################
284 293
285package Crossfire::Client::Widget::Window; 294package CFClient::Widget::Window;
286 295
287our @ISA = Crossfire::Client::Widget::Bin::; 296our @ISA = CFClient::Widget::Bin::;
288 297
289use SDL::OpenGL; 298use SDL::OpenGL;
290 299
291sub new { 300sub new {
292 my ($class, $x, $y, $z, $w, $h) = @_; 301 my ($class, $x, $y, $z, $w, $h) = @_;
297} 306}
298 307
299sub update { 308sub update {
300 my ($self) = @_; 309 my ($self) = @_;
301 310
311 # we want to do this delayed...
302 $self->render_chld; 312 $self->render_chld;
303 $self->SUPER::update; 313 $self->SUPER::update;
304} 314}
305 315
306sub render_chld { 316sub render_chld {
307 my ($self) = @_; 317 my ($self) = @_;
308 318
309 $self->{texture} = 319 $self->{texture} =
310 Crossfire::Client::Texture->new_from_opengl ( 320 CFClient::Texture->new_from_opengl (
311 $self->{w}, $self->{h}, sub { $self->child->draw } 321 $self->{w}, $self->{h}, sub { $self->child->draw }
312 ); 322 );
313} 323}
314 324
315sub size_allocate { 325sub size_allocate {
341 glDisable GL_TEXTURE_2D; 351 glDisable GL_TEXTURE_2D;
342} 352}
343 353
344############################################################################# 354#############################################################################
345 355
346package Crossfire::Client::Widget::Frame; 356package CFClient::Widget::Frame;
347 357
348our @ISA = Crossfire::Client::Widget::Bin::; 358our @ISA = CFClient::Widget::Bin::;
349 359
350use SDL::OpenGL; 360use SDL::OpenGL;
351 361
352sub size_request { 362sub size_request {
353 my ($self) = @_; 363 my ($self) = @_;
387 $chld->draw; 397 $chld->draw;
388} 398}
389 399
390############################################################################# 400#############################################################################
391 401
392package Crossfire::Client::Widget::FancyFrame; 402package CFClient::Widget::FancyFrame;
393 403
394our @ISA = Crossfire::Client::Widget::Bin::; 404our @ISA = CFClient::Widget::Bin::;
395 405
396use SDL::OpenGL; 406use SDL::OpenGL;
397 407
398my @tex = 408my @tex =
399 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 409 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
400 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 410 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
401 411
402sub size_request { 412sub size_request {
403 my ($self) = @_; 413 my ($self) = @_;
404 414
470 480
471} 481}
472 482
473############################################################################# 483#############################################################################
474 484
475package Crossfire::Client::Widget::Table; 485package CFClient::Widget::Table;
476 486
477our @ISA = Crossfire::Client::Widget::Bin::; 487our @ISA = CFClient::Widget::Bin::;
478 488
479use SDL::OpenGL; 489use SDL::OpenGL;
480 490
481sub add { 491sub add {
482 my ($self, $x, $y, $chld) = @_; 492 my ($self, $x, $y, $chld) = @_;
557 } 567 }
558} 568}
559 569
560############################################################################# 570#############################################################################
561 571
562package Crossfire::Client::Widget::VBox; 572package CFClient::Widget::VBox;
563 573
564our @ISA = Crossfire::Client::Widget::Container::; 574our @ISA = CFClient::Widget::Container::;
565 575
566use SDL::OpenGL; 576use SDL::OpenGL;
567 577
568sub size_request { 578sub size_request {
569 my ($self) = @_; 579 my ($self) = @_;
617 } 627 }
618} 628}
619 629
620############################################################################# 630#############################################################################
621 631
622package Crossfire::Client::Widget::Label; 632package CFClient::Widget::Label;
623 633
624our @ISA = Crossfire::Client::Widget::; 634our @ISA = CFClient::Widget::;
625 635
626use SDL::OpenGL; 636use SDL::OpenGL;
627 637
628sub new { 638sub new {
629 my ($class, $x, $y, $z, $height, $text) = @_; 639 my ($class, $x, $y, $z, $height, $text) = @_;
630 640
631 $height ||= $::FONTSIZE; 641 $height ||= $::FONTSIZE;
632 642
633 # TODO: color, and make height, xyz etc. optional 643 # TODO: color, and make height, xyz etc. optional
634 my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); 644 my $self = $class->SUPER::new (
645 color => [1, 1, 1],
646 x => $x,
647 y => $y,
648 z => $z,
649 height => $height,
650 layout => new CFClient::Layout $height,
651 );
635 652
636 $self->set_text ($text); 653 $self->set_text ($text);
637 654
638 $self 655 $self
639} 656}
640 657
641sub set_text { 658sub set_text {
642 my ($self, $text) = @_; 659 my ($self, $text) = @_;
643 660
644 $self->{text} = $text; 661 $self->{text} = $text;
645 $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; 662 $self->{layout}->set_markup ($text);
646 663
647 $self->update; 664 delete $self->{texture};
648} 665}
649 666
650sub get_text { 667sub get_text {
651 my ($self, $text) = @_; 668 my ($self, $text) = @_;
652 669
654} 671}
655 672
656sub size_request { 673sub size_request {
657 my ($self) = @_; 674 my ($self) = @_;
658 675
676 $self->{layout}->set_width;
677 $self->{layout}->size
659 if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack 678# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack
660 ( 679# (
661 $self->{texture}{width}, 680# $self->{texture}{width},
662 $self->{texture}{height}, 681# $self->{texture}{height},
663 ) 682# )
664 } else { 683# } else {
665 my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; 684# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{height};
666 685#
667 ($w, $h) 686# ($w, $h)
668 } 687# }
688}
669 689
690sub size_allocate {
691 my ($self, $w, $h) = @_;
692
693 $self->SUPER::size_allocate ($w, $h);
694 delete $self->{texture};
670} 695}
671 696
672sub _draw { 697sub _draw {
673 my ($self) = @_; 698 my ($self) = @_;
674 699
675 my $tex = $self->{texture}; 700 my $tex = $self->{texture} ||= do {
701 $self->{layout}->set_width ($self->{w});
702 new_from_layout CFClient::Texture $self->{layout};
703 };
676 704
677 glEnable GL_BLEND; 705 glEnable GL_BLEND;
678 glEnable GL_TEXTURE_2D; 706 glEnable GL_TEXTURE_2D;
679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 707 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
680 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 708 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
681 709
682 glColor 1, 0, 0, 1; # TODO color 710 glColor @{$self->{color}};
683 711
684 $tex->draw_quad (0, 0); 712 $tex->draw_quad (0, 0);
685 713
686 glDisable GL_BLEND; 714 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D; 715 glDisable GL_TEXTURE_2D;
688} 716}
689 717
690############################################################################# 718#############################################################################
691 719
692package Crossfire::Client::Widget::Entry; 720package CFClient::Widget::Entry;
693 721
694our @ISA = Crossfire::Client::Widget::Label::; 722our @ISA = CFClient::Widget::Label::;
695 723
696use SDL; 724use SDL;
697use SDL::OpenGL; 725use SDL::OpenGL;
698 726
699sub key_down { 727sub key_down {
708 736
709 if ($sym == SDLK_BACKSPACE) { 737 if ($sym == SDLK_BACKSPACE) {
710 substr $text, -1, 1, ''; 738 substr $text, -1, 1, '';
711 } elsif ($uni) { 739 } elsif ($uni) {
712 $text .= chr $uni; 740 $text .= chr $uni;
713 print "$uni <$text>\n";#d#
714 } 741 }
715 742
716 $self->set_text ($text); 743 $self->set_text ($text);
717} 744}
718 745
719sub button_down { 746sub button_down {
720 my ($self, $ev) = @_; 747 my ($self, $ev) = @_;
721 748
722 $self->focus_in; 749 $self->focus_in;
750}
751
752sub mouse_motion {
753 my ($self, $ev, $x, $y) = @_;
754 printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d#
723} 755}
724 756
725sub _draw { 757sub _draw {
726 my ($self) = @_; 758 my ($self) = @_;
727 759
741 $self->SUPER::_draw; 773 $self->SUPER::_draw;
742} 774}
743 775
744############################################################################# 776#############################################################################
745 777
746package Crossfire::Client::Widget::MapWidget; 778package CFClient::Widget::MapWidget;
747 779
748use strict; 780use strict;
749 781
750use List::Util qw(min max); 782use List::Util qw(min max);
751 783
752use SDL; 784use SDL;
753use SDL::OpenGL; 785use SDL::OpenGL;
754use SDL::OpenGL::Constants; 786use SDL::OpenGL::Constants;
755 787
756our @ISA = Crossfire::Client::Widget::; 788our @ISA = CFClient::Widget::;
757 789
758sub key_down { 790sub key_down {
759 print "MAPKEYDOWN\n"; 791 print "MAPKEYDOWN\n";
760} 792}
761 793
837# } 869# }
838 870
839 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 871 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 872 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
841 873
842 $darkness = new Crossfire::Client::Texture 874 $darkness = new CFClient::Texture
843 width => $sw4, 875 width => $sw4,
844 height => $sh, 876 height => $sh,
845 data => $darkness, 877 data => $darkness,
846 internalformat => GL_ALPHA, 878 internalformat => GL_ALPHA,
847 format => GL_ALPHA; 879 format => GL_ALPHA;
904 } 936 }
905} 937}
906 938
907############################################################################# 939#############################################################################
908 940
909package Crossfire::Client::Widget::Animator; 941package CFClient::Widget::Animator;
910 942
911use SDL::OpenGL; 943use SDL::OpenGL;
912 944
913our @ISA = Crossfire::Client::Widget::Bin::; 945our @ISA = CFClient::Widget::Bin::;
914 946
915sub moveto { 947sub moveto {
916 my ($self, $x, $y) = @_; 948 my ($self, $x, $y) = @_;
917 949
918 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 950 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
923} 955}
924 956
925sub animate { 957sub animate {
926 my ($self, $interval) = @_; 958 my ($self, $interval) = @_;
927 959
928 printf "%5.2f\n", 1 / $interval if $interval;#d#
929
930 $self->{time} -= $interval * $self->{speed}; 960 $self->{time} -= $interval * $self->{speed};
931 if ($self->{time} <= 0) { 961 if ($self->{time} <= 0) {
932 $self->{time} = 0; 962 $self->{time} = 0;
933 ::animation_stop $self; 963 ::animation_stop $self;
934 } 964 }
948 glPopMatrix; 978 glPopMatrix;
949} 979}
950 980
951############################################################################# 981#############################################################################
952 982
953package Crossfire::Client::Widget::Toplevel; 983package CFClient::Widget::Toplevel;
954 984
955our @ISA = Crossfire::Client::Widget::Container::; 985our @ISA = CFClient::Widget::Container::;
956 986
957sub size_request { 987sub size_request {
958 ($::WIDTH, $::HEIGHT) 988 ($::WIDTH, $::HEIGHT)
959} 989}
960 990
965 995
966 $_->size_allocate ($_->size_request) 996 $_->size_allocate ($_->size_request)
967 for @{$self->{children}}; 997 for @{$self->{children}};
968} 998}
969 999
1000sub translate {
1001 my ($self, $x, $y) = @_;
1002
1003 ($x, $y)
1004}
1005
970sub update { 1006sub update {
971 my ($self) = @_; 1007 my ($self) = @_;
972 1008
973 $self->size_allocate ($self->size_request); 1009 $self->size_allocate ($self->size_request);
974 ::refresh (); 1010 ::refresh ();
988 $self->_draw; 1024 $self->_draw;
989} 1025}
990 1026
991############################################################################# 1027#############################################################################
992 1028
993package Crossfire::Client::Widget; 1029package CFClient::Widget;
994 1030
995$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; 1031$TOPLEVEL = new CFClient::Widget::Toplevel;
996 1032
9971 10331
998 1034

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines