ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.112 by root, Sat Apr 15 12:29:46 2006 UTC vs.
Revision 1.120 by elmex, Sun Apr 16 17:43:17 2006 UTC

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->button - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->button - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->motion_x, $ev->motion_y);
60 60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover && $hover->{can_hover}; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 71}
72 72
73# convert position array to integers 73# convert position array to integers
74sub harmonize { 74sub harmonize {
75 my ($vals) = @_; 75 my ($vals) = @_;
76 76
77 my $rem = 0; 77 my $rem = 0;
78 78
79 for ($vals) { 79 for (@$vals) {
80 my $i = int $_ + $rem; 80 my $i = int $_ + $rem;
81 $rem += $_ - $i; 81 $rem += $_ - $i;
82 $_ = $i; 82 $_ = $i;
83 } 83 }
84} 84}
112 $self 112 $self
113} 113}
114 114
115sub move { 115sub move {
116 my ($self, $x, $y, $z) = @_; 116 my ($self, $x, $y, $z) = @_;
117 $self->{x} = $x; 117 $self->{x} = int $x;
118 $self->{y} = $y; 118 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 119 $self->{z} = $z if defined $z;
120} 120}
121 121
122sub needs_redraw { 122sub needs_redraw {
123 0 123 0
146 my ($self, $x, $y, $w, $h) = @_; 146 my ($self, $x, $y, $w, $h) = @_;
147 147
148 $self->_size_allocate ($x, $y, $w, $h); 148 $self->_size_allocate ($x, $y, $w, $h);
149} 149}
150 150
151# return top left coordinates
152sub _topleft {
153 my ($self, $x, $y) = @_;
154
155 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
156}
157
151# translate global coordinates to local coordinate system 158# translate global coordinates to local coordinate system
152sub translate { 159sub coord2local {
153 my ($self, $x, $y) = @_; 160 my ($self, $x, $y) = @_;
154 161
155 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 162 my ($X, $Y) = $self->_topleft;
163 ($x - $X, $y - $Y)
164}
165
166# translate local coordinates to global coordinate system
167sub coord2global {
168 my ($self, $x, $y) = @_;
169
170 my ($X, $Y) = $self->_topleft;
171 ($x + $X, $y + $Y)
156} 172}
157 173
158sub focus_in { 174sub focus_in {
159 my ($self) = @_; 175 my ($self) = @_;
160 176
161 return if $FOCUS == $self; 177 return if $FOCUS == $self;
162 return unless $self->{can_focus}; 178 return unless $self->{can_focus};
163 179
164 my $focus = $FOCUS; $FOCUS = $self; 180 my $focus = $FOCUS; $FOCUS = $self;
181
182 $self->emit (focus_in => $focus);
183
165 $focus->update if $focus; 184 $focus->update if $focus;
166 $FOCUS->update; 185 $FOCUS->update;
167} 186}
168 187
169sub focus_out { 188sub focus_out {
170 my ($self) = @_; 189 my ($self) = @_;
171 190
172 return unless $FOCUS == $self; 191 return unless $FOCUS == $self;
173 192
174 my $focus = $FOCUS; undef $FOCUS; 193 my $focus = $FOCUS; undef $FOCUS;
194
195 $self->emit (focus_out => $focus);
196
175 $focus->update if $focus; #? 197 $focus->update if $focus; #?
176} 198}
177 199
178sub mouse_motion { } 200sub mouse_motion { }
179sub button_up { } 201sub button_up { }
233 255
234 () 256 ()
235} 257}
236 258
237sub set_parent { 259sub set_parent {
238 my ($self, $par) = @_; 260 my ($self, $parent) = @_;
239 261
240 $self->{parent} = $par;
241 Scalar::Util::weaken $self->{parent}; 262 Scalar::Util::weaken ($self->{parent} = $parent);
242}
243
244sub get_parent {
245 $_[0]->{parent}
246} 263}
247 264
248sub update { 265sub update {
249 my ($self) = @_; 266 my ($self) = @_;
250 267
339 356
340 $self 357 $self
341} 358}
342 359
343sub add { 360sub add {
344 my ($self, $chld) = @_; 361 my ($self, $child) = @_;
345 362
346 $chld->set_parent ($self); 363 $child->set_parent ($self);
364
365 use sort 'stable';
347 366
348 $self->{children} = [ 367 $self->{children} = [
349 sort { $a->{z} <=> $b->{z} } 368 sort { $a->{z} <=> $b->{z} }
350 @{$self->{children}}, $chld 369 @{$self->{children}}, $child
351 ]; 370 ];
352 371
353 $self->{w} = $self->{h} = -1; 372 $self->{w} = $self->{h} = -1;
354 $self->update; 373 $self->update;
355} 374}
552 # TODO: user_x, user_y, overwrite moveto? 571 # TODO: user_x, user_y, overwrite moveto?
553 572
554 $class->SUPER::new ( 573 $class->SUPER::new (
555 bg => [1, 1, 1, 1], 574 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 575 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 576 border => int $::FONTSIZE * 0.8,
558 @_ 577 @_
559 ) 578 )
560} 579}
561 580
562sub size_request { 581sub size_request {
685 @_ 704 @_
686 ) 705 )
687} 706}
688 707
689sub add { 708sub add {
690 my ($self, $x, $y, $chld) = @_; 709 my ($self, $x, $y, $child) = @_;
691 710
711 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 712 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 713
695 $self->{w} = $self->{h} = -1; 714 $self->{w} = $self->{h} = -1;
715 $self->update;
716}
717
718# TODO: move to container class maybe? send childs a signal on removal?
719sub clear {
720 my ($self) = @_;
721
722 delete $self->{children};
696 $self->update; 723 $self->update;
697} 724}
698 725
699sub get_wh { 726sub get_wh {
700 my ($self) = @_; 727 my ($self) = @_;
929 my $self = $class->SUPER::new ( 956 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 957 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE, 958 fontsize => $::FONTSIZE,
932 text => "", 959 text => "",
933 align => -1, 960 align => -1,
961 valign => -1,
934 padding => 2, 962 padding => 2,
935 layout => new CFClient::Layout, 963 layout => new CFClient::Layout,
936 %arg 964 %arg
937 ); 965 );
938 966
1015 my $x = 1043 my $x =
1016 $self->{align} < 0 ? $self->{padding} 1044 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1045 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1046 : ($self->{w} - $tex->{w}) * 0.5;
1019 1047
1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1048 my $y =
1049 $self->{valign} < 0 ? $self->{padding}
1050 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1051 : ($self->{h} - $tex->{h}) * 0.5;
1052
1053 $tex->draw_quad (int $x, int $y);
1021 1054
1022 glDisable GL_TEXTURE_2D; 1055 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1056 glDisable GL_BLEND;
1024} 1057}
1025 1058
1040 bg => [0, 0, 0, 0.2], 1073 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1074 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1075 active_fg => [0, 0, 0],
1043 can_hover => 1, 1076 can_hover => 1,
1044 can_focus => 1, 1077 can_focus => 1,
1078 valign => 0,
1045 @_ 1079 @_
1046 ) 1080 )
1047} 1081}
1048 1082
1049sub _set_text { 1083sub _set_text {
1248 1282
1249sub _draw { 1283sub _draw {
1250 my ($self) = @_; 1284 my ($self) = @_;
1251 1285
1252 local $self->{fg} = $self->{fg}; 1286 local $self->{fg} = $self->{fg};
1253 my $tex = $tex[0];
1254
1255 glEnable GL_BLEND;
1256 glEnable GL_TEXTURE_2D;
1257 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1258 1287
1259 if ($GRAB == $self) { 1288 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1289 $self->{fg} = $self->{active_fg};
1261 } 1290 }
1262 1291
1263 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1292 glEnable GL_BLEND;
1293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1294 glEnable GL_TEXTURE_2D;
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1296 glColor 0, 0, 0, 1;
1265 1297
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1298 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1299
1268 glDisable GL_TEXTURE_2D; 1300 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1301 glDisable GL_BLEND;
1270 1302
1271 $self->SUPER::_draw; 1303 $self->SUPER::_draw;
1575 1607
1576 return unless $self->{h} > 0; 1608 return unless $self->{h} > 0;
1577 1609
1578 delete $self->{texture}; 1610 delete $self->{texture};
1579 1611
1580 $TOPLEVEL->on_refresh ($self, sub { 1612 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1613 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1614 my $height = 0;
1583 1615
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1616 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1617 for @{$self->{par}};
1753 0.1, 0.1, 0.1, 1785 0.1, 0.1, 0.1,
1754 ; 1786 ;
1755 glEnable GL_CONVOLUTION_2D; 1787 glEnable GL_CONVOLUTION_2D;
1756 } 1788 }
1757 1789
1758 my $tex = new CFClient::Texture 1790 $self->{fow_texture} = new CFClient::Texture
1759 w => $w, 1791 w => $w,
1760 h => $h, 1792 h => $h,
1761 data => $data, 1793 data => $data,
1762 internalformat => GL_ALPHA, 1794 internalformat => GL_ALPHA,
1763 format => GL_ALPHA; 1795 format => GL_ALPHA;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1800 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1769 glEnable GL_TEXTURE_2D; 1801 glEnable GL_TEXTURE_2D;
1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1802 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1771 1803
1772 glColor +($::CFG->{fow_intensity}) x 3, 1; 1804 glColor +($::CFG->{fow_intensity}) x 3, 1;
1773 $tex->draw_quad (0, 0, $w * 32, $h * 32); 1805 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1774 1806
1775 glDisable GL_TEXTURE_2D; 1807 glDisable GL_TEXTURE_2D;
1776 glDisable GL_BLEND; 1808 glDisable GL_BLEND;
1777 } 1809 }
1778 1810
1784 glEnable GL_BLEND; 1816 glEnable GL_BLEND;
1785 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1817 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1786 glEnable GL_TEXTURE_2D; 1818 glEnable GL_TEXTURE_2D;
1787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1819 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1788 1820
1821 $self->{mapmap_texture} =
1789 CFClient::Texture->new ( 1822 new CFClient::Texture
1790 w => $w, 1823 w => $w,
1791 h => $h, 1824 h => $h,
1792 data => $::MAP->mapmap ($w, $h), 1825 data => $::MAP->mapmap ($w, $h),
1793 type => GL_UNSIGNED_INT_8_8_8_8_REV 1826 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1794 )->draw_quad (100, 100); 1827
1828 $self->{mapmap_texture}->draw_quad (100, 100);
1795 1829
1796 glDisable GL_TEXTURE_2D; 1830 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND; 1831 glDisable GL_BLEND;
1798 } 1832 }
1799 # HACK END 1833 # HACK END
1945sub toggle_flopper { 1979sub toggle_flopper {
1946 my ($self) = @_; 1980 my ($self) = @_;
1947 1981
1948 # TODO: use animation 1982 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 1983 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 1984 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 1985 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 1986 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 1987 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 1988 $CFClient::UI::ROOT->remove ($self->{other});
1989 $self->emit ("close");
1957 } 1990 }
1958}
1959 1991
1960############################################################################# 1992 $self->emit (changed => $self->{state});
1993}
1961 1994
1995#############################################################################
1996
1962package CFClient::UI::Toplevel; 1997package CFClient::UI::Root;
1963 1998
1964our @ISA = CFClient::UI::Container::; 1999our @ISA = CFClient::UI::Container::;
1965 2000
1966use SDL::OpenGL; 2001use SDL::OpenGL;
1967 2002
1976 2011
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2012 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 2013 for @{$self->{children}};
1979} 2014}
1980 2015
1981sub translate { 2016sub _topleft {
1982 my ($self, $x, $y) = @_; 2017 my ($self, $x, $y) = @_;
1983 2018
1984 ($x, $y) 2019 ($x, $y)
1985} 2020}
1986 2021
1994sub add { 2029sub add {
1995 my ($self, $widget) = @_; 2030 my ($self, $widget) = @_;
1996 2031
1997 $self->SUPER::add ($widget); 2032 $self->SUPER::add ($widget);
1998 2033
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2034 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2000} 2035}
2001 2036
2002sub on_refresh { 2037sub on_refresh {
2003 my ($self, $id, $cb) = @_; 2038 my ($self, $id, $cb) = @_;
2004 2039
2027 2062
2028############################################################################# 2063#############################################################################
2029 2064
2030package CFClient::UI; 2065package CFClient::UI;
2031 2066
2032$TOPLEVEL = new CFClient::UI::Toplevel; 2067$ROOT = new CFClient::UI::Root;
2033 2068
20341 20691
2035 2070

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines