--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/09 21:05:50 1.37 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/09 21:34:49 1.38 @@ -84,7 +84,9 @@ } sub _draw { - my ($widget) = @_; + my ($self) = @_; + + warn "no draw defined for $self\n"; } sub bbox { @@ -98,6 +100,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,70 +136,97 @@ #$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) = @_; + + my $self = $class->SUPER::new (children => []); + $self->add ($_) for @widgets; -sub remove { - my ($self, $chld) = @_; - delete $self->{child} - if $self->{child} == $chld; + $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}); } -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 _draw { +sub find_widget { + my ($self, $x, $y) = @_; + + my $res; + + for (@{ $self->{children} }) { + $res = $_->find_widget ($x, $y) + and return $res; + } + + () +} + +sub size_request { my ($self) = @_; - $self->{child}->draw; + my ($hs, $ws) = (0, 0); + for (@{$self->{children} || []}) { + my ($w, $h) = $_->size_request; + $hs += $h; + if ($ws < $w) { $ws = $w } + } + + return ($ws, $hs); } -package Crossfire::Client::Widget::Toplevel; +sub _draw { + my ($self) = @_; -our @ISA = Crossfire::Client::Widget::; + $_->draw for @{$self->{children}}; +} -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 get { $_[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] } -sub update { - my ($self) = @_; - ::refresh (); -} +package Crossfire::Client::Widget::Toplevel; -sub _draw { +our @ISA = Crossfire::Client::Widget::Container::; + +sub update { my ($self) = @_; - $_->draw for @{$self->{childs}}; + ::refresh (); } package Crossfire::Client::Widget::Window; @@ -440,9 +479,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 +490,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 +504,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 +519,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 +538,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; @@ -519,31 +558,10 @@ 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 } - } - - return ($ws, $hs); -} - sub size_allocate { my ($self, $w, $h) = @_; @@ -553,7 +571,7 @@ my $exp; my @oth; # find expand widget - for (@{$self->{childs}}) { + for (@{$self->{children}}) { if ($_->{expand}) { $exp = $_; last; @@ -571,7 +589,7 @@ } my $y = 0; - for (@{$self->{childs}}) { + for (@{$self->{children}}) { $_->move (0, $y); if ($_ == $exp) { @@ -589,7 +607,7 @@ my ($self) = @_; my ($x, $y); - for (@{$self->{childs} || []}) { + for (@{$self->{children} || []}) { $_->draw; $y += $_->h; } @@ -898,7 +916,7 @@ glPushMatrix; glRotate $self->{time} * 10000, 0, 1, 0; - $self->{child}->draw; + $self->{children}[0]->draw; glPopMatrix; }