--- deliantra/Deliantra-Client/DC/UI.pm 2006/04/09 22:21:02 1.46 +++ deliantra/Deliantra-Client/DC/UI.pm 2006/04/11 13:05:12 1.59 @@ -9,13 +9,68 @@ use Crossfire::Client; -our $FOCUS; # the widget with current focus +our ($FOCUS, $HOVER, $GRAB); # various widgets + +our $TOPLEVEL; +our $BUTTON_STATE; # 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_button_down_event { } -sub feed_sdl_button_up_event { } +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 { + my ($ev) = @_; + my ($x, $y) = ($ev->motion_x, $ev->motion_y); + + if (!$BUTTON_STATE) { + my $widget = $TOPLEVEL->find_widget ($x, $y); + + $GRAB = $widget; + $GRAB->update if $GRAB; + } + + $BUTTON_STATE |= 1 << ($ev->button - 1); + + $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; +} + +sub feed_sdl_button_up_event { + my ($ev) = @_; + my ($x, $y) = ($ev->motion_x, $ev->motion_y); + + my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); + + $BUTTON_STATE &= ~(1 << ($ev->button - 1)); + + $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; + + if (!$BUTTON_STATE) { + my $grab = $GRAB; undef $GRAB; + $grab->update if $grab; + $GRAB->update if $GRAB; + } +} + +sub feed_sdl_motion_event { + my ($ev) = @_; + my ($x, $y) = ($ev->motion_x, $ev->motion_y); + + my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); + + if ($widget != $HOVER) { + my $hover = $HOVER; $HOVER = $widget; + + $hover->update if $hover; + $HOVER->update if $HOVER; + } + + $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; +} sub new { my $class = shift; @@ -46,36 +101,41 @@ $self->{h} = $h; } -sub focus_in { - my ($widget) = @_; - $FOCUS = $widget; -} +# translate global koordinates to local coordinate system +sub translate { + my ($self, $x, $y) = @_; -sub focus_out { - my ($widget) = @_; + $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); } -sub key_down { - my ($widget, $sdlev) = @_; -} +sub focus_in { + my ($self) = @_; -sub key_up { - my ($widget, $sdlev) = @_; + my $focus = $FOCUS; $FOCUS = $self; + $focus->update if $focus; + $FOCUS->update; } -sub button_down { - my ($widget, $sdlev) = @_; -} +sub focus_out { + my ($self) = @_; + + return unless $FOCUS == $self; -sub button_up { - my ($widget, $sdlev) = @_; + my $focus = $FOCUS; undef $FOCUS; + $focus->update if $focus; #? } -sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } -sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } -sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } -sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } -sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } +sub mouse_motion { } +sub button_up { } +sub button_down { } +sub key_down { } +sub key_up { } + +sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } +sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } +sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } +sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } +sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } sub draw { my ($self) = @_; @@ -83,6 +143,17 @@ glPushMatrix; glTranslate $self->{x}, $self->{y}, 0; $self->_draw; + if ($self == $HOVER) { + glColor 1, 1, 1, 0.4; + glEnable GL_BLEND; + glBegin GL_QUADS; + glVertex 0 , 0; + glVertex $self->{w}, 0; + glVertex $self->{w}, $self->{h}; + glVertex 0 , $self->{h}; + glEnd; + glDisable GL_BLEND; + } glPopMatrix; } @@ -220,26 +291,6 @@ ############################################################################# -package Crossfire::Client::Widget::Toplevel; - -our @ISA = Crossfire::Client::Widget::Container::; - -sub update { - my ($self) = @_; - - ::refresh (); -} - -sub add { - my ($self, $widget) = @_; - - $self->SUPER::add ($widget); - - $widget->size_allocate ($widget->size_request); -} - -############################################################################# - package Crossfire::Client::Widget::Window; our @ISA = Crossfire::Client::Widget::Bin::; @@ -292,14 +343,8 @@ glEnable GL_BLEND; glEnable GL_TEXTURE_2D; glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; - glBindTexture GL_TEXTURE_2D, $tex->{name}; - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex 0, 0; - glTexCoord 0, 1; glVertex 0, $h; - glTexCoord 1, 1; glVertex $w, $h; - glTexCoord 1, 0; glVertex $w, 0; - glEnd; + $tex->draw_quad (0, 0, $w, $h); glDisable GL_BLEND; glDisable GL_TEXTURE_2D; @@ -342,10 +387,10 @@ glBegin GL_QUADS; glColor 0, 0, 0; - glTexCoord 0, 0; glVertex 0 , 0; - glTexCoord 0, 1; glVertex 0 , $h + 4; - glTexCoord 1, 1; glVertex $w + 4 , $h + 4; - glTexCoord 1, 0; glVertex $w + 4 , 0; + glVertex 0 , 0; + glVertex 0 , $h + 4; + glVertex $w + 4 , $h + 4; + glVertex $w + 4 , 0; glEnd; $chld->draw; @@ -405,44 +450,16 @@ glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; my $top = $tex[1]; - glBindTexture GL_TEXTURE_2D, $top->{name}; - - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex 0 , 0; - glTexCoord 0, 1; glVertex 0 , $top->{height}; - glTexCoord 1, 1; glVertex $w , $top->{height}; - glTexCoord 1, 0; glVertex $w , 0; - glEnd; + $top->draw_quad (0, 0, $w, $top->{height}); my $left = $tex[3]; - glBindTexture GL_TEXTURE_2D, $left->{name}; - - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex 0 , $top->{height}; - glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; - glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; - glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; - glEnd; + $left->draw_quad (0, $top->{height}, $left->{width}, $ch); my $right = $tex[2]; - glBindTexture GL_TEXTURE_2D, $right->{name}; - - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; - glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; - glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; - glTexCoord 1, 0; glVertex $w , $top->{height}; - glEnd; + $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); my $bottom = $tex[4]; - glBindTexture GL_TEXTURE_2D, $bottom->{name}; - - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; - glTexCoord 0, 1; glVertex 0 , $h; - glTexCoord 1, 1; glVertex $w , $h; - glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; - glEnd; + $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); my $bg = $tex[0]; glBindTexture GL_TEXTURE_2D, $bg->{name}; @@ -453,12 +470,7 @@ my $rep_x = $cw / $bg->{width}; my $rep_y = $ch / $bg->{height}; - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; - glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; - glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; - glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; - glEnd; + $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); glDisable GL_BLEND; glDisable GL_TEXTURE_2D; @@ -625,8 +637,16 @@ sub new { my ($class, $x, $y, $z, $height, $text) = @_; + $height ||= $::FONTSIZE; + # TODO: color, and make height, xyz etc. optional - my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); + my $self = $class->SUPER::new ( + x => $x, + y => $y, + z => $z, + height => $height, + layout => new Crossfire::Client::Layout, + ); $self->set_text ($text); @@ -637,9 +657,9 @@ my ($self, $text) = @_; $self->{text} = $text; - $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; + $self->{layout}->set_markup ($text); - $self->update; + delete $self->{texture}; } sub get_text { @@ -651,31 +671,43 @@ sub size_request { my ($self) = @_; - ( - $self->{texture}{width}, - $self->{texture}{height}, - ) + $self->{layout}->set_width; + $self->{layout}->size +# if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack +# ( +# $self->{texture}{width}, +# $self->{texture}{height}, +# ) +# } else { +# my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; +# +# ($w, $h) +# } +} + +sub size_allocate { + my ($self, $w, $h) = @_; + + $self->SUPER::size_allocate ($w, $h); + delete $self->{texture}; } sub _draw { my ($self) = @_; - my $tex = $self->{texture}; + my $tex = $self->{texture} ||= do { + $self->{layout}->set_width ($self->{w}); + new_from_layout Crossfire::Client::Texture $self->{layout}; + }; 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_MODULATE; - glBindTexture GL_TEXTURE_2D, $tex->{name}; glColor 1, 0, 0, 1; # TODO color - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex 0 , 0; - glTexCoord 0, 1; glVertex 0 , $tex->{height}; - glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; - glTexCoord 1, 0; glVertex $tex->{width}, 0; - glEnd; + $tex->draw_quad (0, 0); glDisable GL_BLEND; glDisable GL_TEXTURE_2D; @@ -683,7 +715,7 @@ ############################################################################# -package Crossfire::Client::Widget::TextEntry; +package Crossfire::Client::Widget::Entry; our @ISA = Crossfire::Client::Widget::Label::; @@ -696,20 +728,49 @@ my $mod = $ev->key_mod; my $sym = $ev->key_sym; - $ev->set_unicode (1); my $uni = $ev->key_unicode; my $text = $self->get_text; if ($sym == SDLK_BACKSPACE) { substr $text, -1, 1, ''; - } elsif ($uni) { $text .= chr $uni; } + $self->set_text ($text); } +sub button_down { + my ($self, $ev) = @_; + + $self->focus_in; +} + +sub mouse_motion { + my ($self, $ev, $x, $y) = @_; + printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# +} + +sub _draw { + my ($self) = @_; + + if ($FOCUS == $self) { + glColor 1, 1, 1; + } else { + glColor 0.7, 0.7, 0.7; + } + + glBegin GL_QUADS; + glVertex 0 , 0; + glVertex 0 , $self->{h} - 1; + glVertex $self->{w} - 1, $self->{h} - 1; + glVertex $self->{w} - 1, 0; + glEnd; + + $self->SUPER::_draw; +} + ############################################################################# package Crossfire::Client::Widget::MapWidget; @@ -732,10 +793,10 @@ } sub size_request { - -} - -sub size_allocate { + ( + 1 + int $::WIDTH / 32, + 1 + int $::HEIGHT / 32, + ) } sub _draw { @@ -768,37 +829,29 @@ glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; my $sw4 = ($sw + 3) & ~3; - my $lighting = "\x00" x ($sw4 * $sh); + my $darkness = "\x00" x ($sw4 * $sh); for my $x (0 .. $sw - 1) { + my $row = $map->[$x + $xofs]; for my $y (0 .. $sh - 1) { - my $cell = $map->[$x + $xofs][$y + $yofs] + my $cell = $row->[$y + $yofs] or next; - my $darkness = $cell->[0] * (1 / 255); - if ($darkness < 0) { - $darkness = 0.15; + 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; } - substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; for my $num (grep $_, @$cell[1,2,3]) { my $tex = $::CONN->{face}[$num]{texture} || next; - - glBindTexture GL_TEXTURE_2D, $tex->{name}; - + my $w = $tex->{width}; my $h = $tex->{height}; - my $px = ($x + 1) * 32 - $w; - my $py = ($y + 1) * 32 - $h; - - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex $px , $py; - glTexCoord 0, 1; glVertex $px , $py + $h; - glTexCoord 1, 1; glVertex $px + $w, $py + $h; - glTexCoord 1, 0; glVertex $px + $w, $py; - glEnd; + $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); } } } @@ -813,24 +866,18 @@ # $lighting =~ s/(.)../$1/gs; # } - $lighting = new Crossfire::Client::Texture + glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; + glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; + + $darkness = new Crossfire::Client::Texture width => $sw4, height => $sh, - data => $lighting, - internalformat => GL_ALPHA4, + data => $darkness, + internalformat => GL_ALPHA, format => GL_ALPHA; - glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; - glColor 0, 0, 0, 0.75; - glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; - glBindTexture GL_TEXTURE_2D, $lighting->{name}; - glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; - glBegin GL_QUADS; - glTexCoord 0, 0; glVertex 0 , 0; - glTexCoord 0, 1; glVertex 0 , $sh * 32; - glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; - glTexCoord 1, 0; glVertex $sw4 * 32, 0; - glEnd; + glColor 0.45, 0.45, 0.45, 1; + $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); glDisable GL_TEXTURE_2D; glDisable GL_BLEND; @@ -899,7 +946,7 @@ my ($self, $x, $y) = @_; $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; - $self->{speed} = 0.2; + $self->{speed} = 0.001; $self->{time} = 1; ::animation_start $self; @@ -924,10 +971,62 @@ my ($self) = @_; glPushMatrix; - glRotate $self->{time} * 10000, 0, 1, 0; + glRotate $self->{time} * 1000, 0, 1, 0; $self->{children}[0]->draw; glPopMatrix; } -1; +############################################################################# + +package Crossfire::Client::Widget::Toplevel; + +our @ISA = Crossfire::Client::Widget::Container::; + +sub size_request { + ($::WIDTH, $::HEIGHT) +} + +sub size_allocate { + my ($self, $w, $h) = @_; + + $self->SUPER::size_allocate ($w, $h); + + $_->size_allocate ($_->size_request) + for @{$self->{children}}; +} + +sub translate { + my ($self, $x, $y) = @_; + + ($x, $y) +} + +sub update { + my ($self) = @_; + + $self->size_allocate ($self->size_request); + ::refresh (); +} + +sub add { + my ($self, $widget) = @_; + + $self->SUPER::add ($widget); + + $widget->size_allocate ($widget->size_request); +} + +sub draw { + my ($self) = @_; + + $self->_draw; +} + +############################################################################# + +package Crossfire::Client::Widget; + +$TOPLEVEL = new Crossfire::Client::Widget::Toplevel; + +1