--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/09 21:41:11 1.40 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/09 22:21:02 1.46 @@ -7,6 +7,8 @@ use SDL::OpenGL; use SDL::OpenGL::Constants; +use Crossfire::Client; + our $FOCUS; # the widget with current focus # class methods for events @@ -162,7 +164,8 @@ sort { $a->{z} <=> $b->{z} } @{$self->{children}}, $chld; - $self->size_allocate ($self->{w}, $self->{h}); + $self->size_allocate ($self->{w}, $self->{h}) + if $self->{w}; #TODO: check for "realised state" } sub remove { @@ -176,27 +179,17 @@ sub find_widget { my ($self, $x, $y) = @_; + $x -= $self->{x}; + $y -= $self->{y}; + my $res; - for (@{ $self->{children} }) { + for (reverse @{ $self->{children} }) { $res = $_->find_widget ($x, $y) and return $res; } - () -} - -sub size_request { - my ($self) = @_; - - my ($hs, $ws) = (0, 0); - for (@{$self->{children} || []}) { - my ($w, $h) = $_->size_request; - $hs += $h; - if ($ws < $w) { $ws = $w } - } - - return ($ws, $hs); + $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) } sub _draw { @@ -219,6 +212,7 @@ sub size_allocate { my ($self, $w, $h) = @_; + $self->SUPER::size_allocate ($w, $h); $self->{children}[0]->size_allocate ($w, $h) if $self->{children}[0] @@ -236,6 +230,14 @@ ::refresh (); } +sub add { + my ($self, $widget) = @_; + + $self->SUPER::add ($widget); + + $widget->size_allocate ($widget->size_request); +} + ############################################################################# package Crossfire::Client::Widget::Window; @@ -244,52 +246,39 @@ 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->update; #TODO: Move this to the size_request event propably? + $self->child->size_allocate ($w, $h); + + $self->render_chld; } sub _draw { @@ -336,9 +325,9 @@ sub size_allocate { my ($self, $w, $h) = @_; - - $self->w ($w); - $self->h ($h); + + $self->{w} = $w; + $self->{h} = $h; $self->child->size_allocate ($w - 4, $h - 4); $self->child->move (2, 2); @@ -366,30 +355,23 @@ 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->SUPER::size_request; - $h += $self->{txts}->[1]->{height}; - $h += $self->{txts}->[4]->{height}; - $w += $self->{txts}->[2]->{width}; - $w += $self->{txts}->[3]->{width}; + $h += $tex[1]->{height}; + $h += $tex[4]->{height}; + $w += $tex[2]->{width}; + $w += $tex[3]->{width}; ($w, $h) } @@ -399,30 +381,30 @@ $self->SUPER::size_allocate ($w, $h); - $h -= $self->{txts}->[1]->{height}; - $h -= $self->{txts}->[4]->{height}; - $w -= $self->{txts}->[2]->{width}; - $w -= $self->{txts}->[3]->{width}; + $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->child->size_allocate ($w, $h); - $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); + $self->child->move ($tex[3]->{width}, $tex[1]->{height}); } sub _draw { my ($self) = @_; - my ($w, $h) = ($self->w, $self->h); - my ($cw, $ch) = ($self->child->w, $self->child->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; @@ -432,7 +414,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; @@ -442,7 +424,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; @@ -452,7 +434,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; @@ -462,7 +444,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; @@ -580,6 +562,17 @@ use SDL::OpenGL; +sub size_request { + my ($self) = @_; + + my @alloc = map [$_->size_request], @{$self->{children}}; + + ( + (List::Util::max map $_->[0], @alloc), + (List::Util::sum map $_->[1], @alloc), + ) +} + sub size_allocate { my ($self, $w, $h) = @_; @@ -621,16 +614,6 @@ } } -sub _draw { - my ($self) = @_; - - my ($x, $y); - for (@{$self->{children} || []}) { - $_->draw; - $y += $_->h; - } -} - ############################################################################# package Crossfire::Client::Widget::Label;