--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/11 13:29:24 1.63 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/11 14:36:59 1.67 @@ -75,7 +75,12 @@ sub new { my $class = shift; - bless { @_ }, $class + bless { + x => 0, + y => 0, + z => 0, + @_ + }, $class } sub move { @@ -212,15 +217,29 @@ ############################################################################# +package CFClient::Widget::Empty; + +our @ISA = CFClient::Widget::; + +sub size_request { + (0, 0) +} + +sub draw { } + +############################################################################# + package CFClient::Widget::Container; our @ISA = CFClient::Widget::; sub new { - my ($class, @widgets) = @_; + my ($class, %arg) = @_; - my $self = $class->SUPER::new (children => []); - $self->add ($_) for @widgets; + my $children = delete $arg{children} || []; + + my $self = $class->SUPER::new (children => [], %arg); + $self->add ($_) for @$children; $self } @@ -231,9 +250,10 @@ $chld->{expand} = $expand; $chld->set_parent ($self); - @{$self->{children}} = + $self->{children} = [ sort { $a->{z} <=> $b->{z} } - @{$self->{children}}, $chld; + @{$self->{children}}, $chld + ]; $self->size_allocate ($self->{w}, $self->{h}) if $self->{w}; #TODO: check for "realised state" @@ -275,6 +295,31 @@ our @ISA = CFClient::Widget::Container::; +sub new { + my ($class, %arg) = @_; + + my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; + + $class->SUPER::new (children => [$child], %arg) +} + +sub add { + my ($self, $widget) = @_; + + $self->{children} = []; + + $self->SUPER::add ($widget); +} + +sub remove { + my ($self, $widget) = @_; + + $self->SUPER::remove ($widget); + + $self->{children} = [new CFClient::Widget::Empty] + unless @{$self->{children}}; +} + sub child { $_[0]->{children}[0] } sub size_request { @@ -298,11 +343,9 @@ use SDL::OpenGL; sub new { - my ($class, $x, $y, $z, $w, $h) = @_; + my ($class, %arg) = @_; - my $self = $class->SUPER::new; - - @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); + my $self = $class->SUPER::new (%arg); } sub update { @@ -435,8 +478,10 @@ $h = $h < 0 ? 0 : $h; $w = $w < 0 ? 0 : $w; - $self->child->size_allocate ($w, $h); - $self->child->move ($tex[3]->{width}, $tex[1]->{height}); + my $child = $self->child; + + $child->size_allocate ($w, $h); + $child->move ($tex[3]->{width}, $tex[1]->{height}); } sub _draw { @@ -636,21 +681,18 @@ use SDL::OpenGL; sub new { - my ($class, $x, $y, $z, $height, $text) = @_; - - $height ||= $::FONTSIZE; + my ($class, %arg) = @_; # TODO: color, and make height, xyz etc. optional my $self = $class->SUPER::new ( - color => [1, 1, 1], - x => $x, - y => $y, - z => $z, - height => $height, - layout => new CFClient::Layout $height, + color => [1, 1, 1], + height => $::FONTSIZE, + text => "", + layout => new CFClient::Layout, + %arg ); - $self->set_text ($text); + $self->set_text ($self->{text}); $self } @@ -674,6 +716,7 @@ my ($self) = @_; $self->{layout}->set_width; + $self->{layout}->set_height ($self->{height}); $self->{layout}->size # if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack # ( @@ -787,6 +830,16 @@ our @ISA = CFClient::Widget::; +sub new { + my $class = shift; + + $class->SUPER::new ( + z => -1, + list => (glGenLists 1), + @_ + ) +} + sub key_down { print "MAPKEYDOWN\n"; } @@ -801,62 +854,71 @@ ) } +sub update { + my ($self) = @_; + + $self->{need_update} = 1; +} + sub _draw { my ($self) = @_; - my $mx = $::CONN->{mapx}; - my $my = $::CONN->{mapy}; + if (delete $self->{need_update}) { + glNewList $self->{list}, GL_COMPILE; - my $map = $::CONN->{map}; + my $mx = $::CONN->{mapx}; + my $my = $::CONN->{mapy}; - my ($xofs, $yofs); + my $map = $::CONN->{map}; - my $sw = 1 + int $::WIDTH / 32; - my $sh = 1 + int $::HEIGHT / 32; + my ($xofs, $yofs); - if ($::CONN->{mapw} > $sw) { - $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; - } else { - $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; - } + my $sw = 1 + int $::WIDTH / 32; + my $sh = 1 + int $::HEIGHT / 32; - if ($::CONN->{maph} > $sh) { - $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; - } else { - $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; - } + if ($::CONN->{mapw} > $sw) { + $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; + } else { + $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; + } - glEnable GL_TEXTURE_2D; - glEnable GL_BLEND; - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; + if ($::CONN->{maph} > $sh) { + $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; + } else { + $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; + } - my $sw4 = ($sw + 3) & ~3; - my $darkness = "\x00" x ($sw4 * $sh); + glEnable GL_TEXTURE_2D; + glEnable GL_BLEND; + glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; - for my $x (0 .. $sw - 1) { - my $row = $map->[$x + $xofs]; - for my $y (0 .. $sh - 1) { - - my $cell = $row->[$y + $yofs] - or next; - - my $dark = $cell->[0]; - if ($dark < 0) { - substr $darkness, $y * $sw4 + $x, 1, chr 224; - } else { - substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; - } + my $sw4 = ($sw + 3) & ~3; + my $darkness = "\x00" x ($sw4 * $sh); - for my $num (grep $_, @$cell[1,2,3]) { - my $tex = $::CONN->{face}[$num]{texture} || next; - - my $w = $tex->{width}; - my $h = $tex->{height}; + for my $x (0 .. $sw - 1) { + my $row = $map->[$x + $xofs]; + for my $y (0 .. $sh - 1) { + + my $cell = $row->[$y + $yofs] + or next; + + my $dark = $cell->[0]; + if ($dark < 0) { + substr $darkness, $y * $sw4 + $x, 1, chr 224; + } else { + substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; + } + + for my $num (grep $_, @$cell[1,2,3]) { + my $tex = $::CONN->{face}[$num]{texture} || next; + + my $w = $tex->{width}; + my $h = $tex->{height}; - $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); + $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); + } } } - } # if (1) { # higher quality darkness # $lighting =~ s/(.)/$1$1$1/gs; @@ -868,21 +930,26 @@ # $lighting =~ s/(.)../$1/gs; # } - glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; + glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; + glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; - $darkness = new CFClient::Texture - width => $sw4, - height => $sh, - data => $darkness, - internalformat => GL_ALPHA, - format => GL_ALPHA; + $darkness = new CFClient::Texture + width => $sw4, + height => $sh, + data => $darkness, + internalformat => GL_ALPHA, + format => GL_ALPHA; - glColor 0.45, 0.45, 0.45, 1; - $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); + glColor 0.45, 0.45, 0.45, 1; + $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); - glDisable GL_TEXTURE_2D; - glDisable GL_BLEND; + glDisable GL_TEXTURE_2D; + glDisable GL_BLEND; + + glEndList; + } + + glCallList $self->{list}; } my %DIR = (