--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/09 21:05:50 1.37 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/09 22:24:43 1.47 @@ -7,11 +7,11 @@ use SDL::OpenGL; use SDL::OpenGL::Constants; -our $FOCUS; # the widget with current focus +use Crossfire::Client; # class methods for events -sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } -sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } +sub feed_sdl_key_down_event { $::FOCUS->key_down ($_[0]) if $::FOCUS } +sub feed_sdl_key_up_event { $::FOCUS->key_up ($_[0]) if $::FOCUS } sub feed_sdl_button_down_event { } sub feed_sdl_button_up_event { } @@ -39,13 +39,14 @@ sub size_allocate { my ($self, $w, $h) = @_; - $self->w ($w); - $self->h ($h); + + $self->{w} = $w; + $self->{h} = $h; } sub focus_in { my ($widget) = @_; - $FOCUS = $widget; + $::FOCUS = $widget; } sub focus_out { @@ -80,11 +81,23 @@ glPushMatrix; glTranslate $self->{x}, $self->{y}, 0; $self->_draw; + if ($self == $::HOVER) { + glColor 1, 1, 1, 1; + glBegin GL_LINES; + glVertex 0, 0; + glVertex $self->{w} - 1, 0; + glVertex $self->{w} - 1, $self->{h} - 1; + glVertex 0, $self->{h} - 1; + glVertex 0, 0; + glEnd; + } glPopMatrix; } sub _draw { - my ($widget) = @_; + my ($self) = @_; + + warn "no draw defined for $self\n"; } sub bbox { @@ -98,6 +111,16 @@ ) } +sub find_widget { + my ($self, $x, $y) = @_; + + return $self + if $x >= $self->{x} && $x < $self->{x} + $self->{w} + && $y >= $self->{y} && $y < $self->{y} + $self->{h}; + + () +} + sub del_parent { $_[0]->{parent} = undef } sub set_parent { @@ -124,124 +147,146 @@ #$self->deactivate; } -package Crossfire::Client::Widget::Bin; +############################################################################# + +package Crossfire::Client::Widget::Container; our @ISA = Crossfire::Client::Widget::; -sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->{expand} = $_[2] } -sub get { $_[0]->{child} } +sub new { + my ($class, @widgets) = @_; -sub remove { - my ($self, $chld) = @_; - delete $self->{child} - if $self->{child} == $chld; + my $self = $class->SUPER::new (children => []); + $self->add ($_) for @widgets; + + $self } -sub size_request { - $_[0]->{child}->size_request if $_[0]->{child} +sub add { + my ($self, $chld, $expand) = @_; + + $chld->{expand} = $expand; + $chld->set_parent ($self); + + @{$self->{children}} = + sort { $a->{z} <=> $b->{z} } + @{$self->{children}}, $chld; + + $self->size_allocate ($self->{w}, $self->{h}) + if $self->{w}; #TODO: check for "realised state" } -sub size_allocate { - my ($self, $w, $h) = @_; - $self->SUPER::size_allocate ($w, $h); - $self->{child}->size_allocate ($w, $h) - if $self->{child} + +sub remove { + my ($self, $widget) = @_; + + $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; + + $self->size_allocate ($self->{w}, $self->{h}); +} + +sub find_widget { + my ($self, $x, $y) = @_; + + $x -= $self->{x}; + $y -= $self->{y}; + + my $res; + + for (reverse @{ $self->{children} }) { + $res = $_->find_widget ($x, $y) + and return $res; + } + + $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) } sub _draw { my ($self) = @_; - $self->{child}->draw; + $_->draw for @{$self->{children}}; } -package Crossfire::Client::Widget::Toplevel; - -our @ISA = Crossfire::Client::Widget::; +############################################################################# -use SDL::OpenGL; +package Crossfire::Client::Widget::Bin; -sub add { - my ($self, $chld) = @_; +our @ISA = Crossfire::Client::Widget::Container::; - push @{$self->{childs}}, $chld; - @{$self->{childs}} = - sort { $a->{z} <=> $b->{z} } - @{$self->{childs}}; +sub child { $_[0]->{children}[0] } - $chld->set_parent ($self); - $chld->size_allocate ($chld->size_request); +sub size_request { + $_[0]{children}[0]->size_request if $_[0]{children}[0]; } -sub remove { - my ($self, $chld) = @_; - @{$self->{childs}} = - sort { $a->{z} <=> $b->{z} } - grep { $_ && $_ != $_[0] } - @{$self->{childs}} +sub size_allocate { + my ($self, $w, $h) = @_; + + $self->SUPER::size_allocate ($w, $h); + $self->{children}[0]->size_allocate ($w, $h) + if $self->{children}[0] } +############################################################################# + +package Crossfire::Client::Widget::Toplevel; + +our @ISA = Crossfire::Client::Widget::Container::; + sub update { my ($self) = @_; + ::refresh (); } -sub _draw { - my ($self) = @_; +sub add { + my ($self, $widget) = @_; + + $self->SUPER::add ($widget); - $_->draw for @{$self->{childs}}; + $widget->size_allocate ($widget->size_request); } +############################################################################# + package Crossfire::Client::Widget::Window; our @ISA = Crossfire::Client::Widget::Bin::; use SDL::OpenGL; -sub add { - my ($self, $chld) = @_; - warn "ADD $chld\n"; - $self->SUPER::add ($chld); - $chld->set_parent ($self); -} +sub new { + my ($class, $x, $y, $z, $w, $h) = @_; -sub remove { - my ($self) = @_; - # TODO FIXME: removing a child from a window will crash, see render_chld - # $self->update; + my $self = $class->SUPER::new; + + @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); } sub update { my ($self) = @_; + $self->render_chld; + $self->SUPER::update; } sub render_chld { my ($self) = @_; - my $chld = $self->get; - my ($w, $h) = $self->size_request; - require Carp; - Carp::cluck "RENDERCHI $w $h"; - warn "RENDERCHI $w $h\n"; $self->{texture} = Crossfire::Client::Texture->new_from_opengl ( - $w, $h, sub { $chld->draw } + $self->{w}, $self->{h}, sub { $self->child->draw } ); - $self->{texture}->upload; -} - -sub size_request { - my ($self) = @_; - ($self->w, $self->h) } sub size_allocate { my ($self, $w, $h) = @_; - $self->w ($w); - $self->h ($h); - $self->get->size_allocate ($w, $h); + $self->{w} = $w; + $self->{h} = $h; + + $self->child->size_allocate ($w, $h); - $self->update; #TODO: Move this to the size_request event propably? + $self->render_chld; } sub _draw { @@ -268,6 +313,8 @@ glDisable GL_TEXTURE_2D; } +############################################################################# + package Crossfire::Client::Widget::Frame; our @ISA = Crossfire::Client::Widget::Bin::; @@ -276,7 +323,7 @@ sub size_request { my ($self) = @_; - my $chld = $self->get + my $chld = $self->child or return (0, 0); $chld->move (2, 2); @@ -286,18 +333,18 @@ sub size_allocate { my ($self, $w, $h) = @_; + + $self->{w} = $w; + $self->{h} = $h; - $self->w ($w); - $self->h ($h); - - $self->get->size_allocate ($w - 4, $h - 4); - $self->get->move (2, 2); + $self->child->size_allocate ($w - 4, $h - 4); + $self->child->move (2, 2); } sub _draw { my ($self) = @_; - my $chld = $self->get; + my $chld = $self->child; my ($w, $h) = $chld->size_request; @@ -312,31 +359,27 @@ $chld->draw; } +############################################################################# + package Crossfire::Client::Widget::FancyFrame; -our @ISA = Crossfire::Client::Widget::Frame::; +our @ISA = Crossfire::Client::Widget::Bin::; use SDL::OpenGL; -sub new { - my ($self, $theme) = @_; - $self = $self->SUPER::new; - - $self->{txts} = [ +my @tex = map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } - qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ - ]; - $self -} + qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); sub size_request { my ($self) = @_; - my ($w, $h) = $self->get->size_request; - $h += $self->{txts}->[1]->{height}; - $h += $self->{txts}->[4]->{height}; - $w += $self->{txts}->[2]->{width}; - $w += $self->{txts}->[3]->{width}; + my ($w, $h) = $self->SUPER::size_request; + + $h += $tex[1]->{height}; + $h += $tex[4]->{height}; + $w += $tex[2]->{width}; + $w += $tex[3]->{width}; ($w, $h) } @@ -344,32 +387,32 @@ sub size_allocate { my ($self, $w, $h) = @_; - $self->w ($w); - $self->h ($h); - $h -= $self->{txts}->[1]->{height}; - $h -= $self->{txts}->[4]->{height}; - $w -= $self->{txts}->[2]->{width}; - $w -= $self->{txts}->[3]->{width}; + $self->SUPER::size_allocate ($w, $h); + + $h -= $tex[1]->{height}; + $h -= $tex[4]->{height}; + $w -= $tex[2]->{width}; + $w -= $tex[3]->{width}; $h = $h < 0 ? 0 : $h; $w = $w < 0 ? 0 : $w; - warn "CHILD:$w $h\n"; - $self->get->size_allocate ($w, $h); - $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); + + $self->child->size_allocate ($w, $h); + $self->child->move ($tex[3]->{width}, $tex[1]->{height}); } sub _draw { my ($self) = @_; - my ($w, $h) = ($self->w, $self->h); - my ($cw, $ch) = ($self->get->w, $self->get->h); + my ($w, $h) = ($self->{w}, $self->{h}); + my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); glEnable GL_BLEND; glEnable GL_TEXTURE_2D; glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; - my $top = $self->{txts}->[1]; + my $top = $tex[1]; glBindTexture GL_TEXTURE_2D, $top->{name}; glBegin GL_QUADS; @@ -379,7 +422,7 @@ glTexCoord 1, 0; glVertex $w , 0; glEnd; - my $left = $self->{txts}->[3]; + my $left = $tex[3]; glBindTexture GL_TEXTURE_2D, $left->{name}; glBegin GL_QUADS; @@ -389,7 +432,7 @@ glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; glEnd; - my $right = $self->{txts}->[2]; + my $right = $tex[2]; glBindTexture GL_TEXTURE_2D, $right->{name}; glBegin GL_QUADS; @@ -399,7 +442,7 @@ glTexCoord 1, 0; glVertex $w , $top->{height}; glEnd; - my $bottom = $self->{txts}->[4]; + my $bottom = $tex[4]; glBindTexture GL_TEXTURE_2D, $bottom->{name}; glBegin GL_QUADS; @@ -409,7 +452,7 @@ glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; glEnd; - my $bg = $self->{txts}->[0]; + my $bg = $tex[0]; glBindTexture GL_TEXTURE_2D, $bg->{name}; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; @@ -428,10 +471,12 @@ glDisable GL_BLEND; glDisable GL_TEXTURE_2D; - $self->get->draw; + $self->child->draw; } +############################################################################# + package Crossfire::Client::Widget::Table; our @ISA = Crossfire::Client::Widget::Bin::; @@ -440,9 +485,9 @@ sub add { my ($self, $x, $y, $chld) = @_; - my $old_chld = $self->{childs}[$y][$x]; + my $old_chld = $self->{children}[$y][$x]; - $self->{childs}[$y][$x] = $chld; + $self->{children}[$y][$x] = $chld; $chld->set_parent ($self); $self->update; } @@ -451,8 +496,8 @@ my ($self, $row) = @_; my $hs = 0; - for (my $xi = 0; $xi <= $#{$self->{childs}->[$row] || []}; $xi++) { - my $c = $self->{childs}->[$row]->[$xi]; + for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { + my $c = $self->{children}->[$row]->[$xi]; if ($c) { my ($w, $h) = $c->size_request; if ($hs < $h) { $hs = $h } @@ -465,8 +510,8 @@ my ($self, $col) = @_; my $ws = 0; - for (my $yi = 0; $yi <= $#{$self->{childs} || []}; $yi++) { - my $c = ($self->{childs}->[$yi] || [])->[$col]; + for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { + my $c = ($self->{children}->[$yi] || [])->[$col]; if ($c) { my ($w, $h) = $c->size_request; if ($ws < $w) { $ws = $w } @@ -480,13 +525,13 @@ my ($hs, $ws) = (0, 0); - for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { + for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { $hs += $self->max_row_height ($yi); } - for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { + for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { my $wm = 0; - for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { + for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { $wm += $self->max_col_width ($xi) } if ($ws < $wm) { $ws = $wm } @@ -499,12 +544,12 @@ my ($self) = @_; my $y = 0; - for (my $yi = 0; $yi <= $#{$self->{childs}}; $yi++) { + for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { my $x = 0; - for (my $xi = 0; $xi <= $#{$self->{childs}->[$yi]}; $xi++) { + for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { - my $c = $self->{childs}->[$yi]->[$xi]; + my $c = $self->{children}->[$yi]->[$xi]; if ($c) { $c->move ($x, $y, 0); #TODO: Move to size_request $c->draw if $c; @@ -517,31 +562,23 @@ } } +############################################################################# + package Crossfire::Client::Widget::VBox; -our @ISA = Crossfire::Client::Widget::Bin::; +our @ISA = Crossfire::Client::Widget::Container::; use SDL::OpenGL; -sub add { - my ($self, $chld, $expand) = @_; - push @{$self->{childs}}, $chld; - $chld->{expand} = $expand; - $chld->set_parent ($self); - $self->update; -} - sub size_request { my ($self) = @_; - my ($hs, $ws) = (0, 0); - for (@{$self->{childs} || []}) { - my ($w, $h) = $_->size_request; - $hs += $h; - if ($ws < $w) { $ws = $w } - } + my @alloc = map [$_->size_request], @{$self->{children}}; - return ($ws, $hs); + ( + (List::Util::max map $_->[0], @alloc), + (List::Util::sum map $_->[1], @alloc), + ) } sub size_allocate { @@ -553,7 +590,7 @@ my $exp; my @oth; # find expand widget - for (@{$self->{childs}}) { + for (@{$self->{children}}) { if ($_->{expand}) { $exp = $_; last; @@ -571,7 +608,7 @@ } my $y = 0; - for (@{$self->{childs}}) { + for (@{$self->{children}}) { $_->move (0, $y); if ($_ == $exp) { @@ -585,15 +622,7 @@ } } -sub _draw { - my ($self) = @_; - - my ($x, $y); - for (@{$self->{childs} || []}) { - $_->draw; - $y += $_->h; - } -} +############################################################################# package Crossfire::Client::Widget::Label; @@ -660,6 +689,8 @@ glDisable GL_TEXTURE_2D; } +############################################################################# + package Crossfire::Client::Widget::TextEntry; our @ISA = Crossfire::Client::Widget::Label::; @@ -687,6 +718,8 @@ $self->set_text ($text); } +############################################################################# + package Crossfire::Client::Widget::MapWidget; use strict; @@ -862,6 +895,8 @@ } } +############################################################################# + package Crossfire::Client::Widget::Animator; use SDL::OpenGL; @@ -898,7 +933,7 @@ glPushMatrix; glRotate $self->{time} * 10000, 0, 1, 0; - $self->{child}->draw; + $self->{children}[0]->draw; glPopMatrix; }