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.114 by root, Sat Apr 15 13:56:25 2006 UTC vs.
Revision 1.123 by root, Mon Apr 17 19:50:22 2006 UTC

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
176 176
177 return if $FOCUS == $self; 177 return if $FOCUS == $self;
178 return unless $self->{can_focus}; 178 return unless $self->{can_focus};
179 179
180 my $focus = $FOCUS; $FOCUS = $self; 180 my $focus = $FOCUS; $FOCUS = $self;
181
182 $self->emit (focus_in => $focus);
183
181 $focus->update if $focus; 184 $focus->update if $focus;
182 $FOCUS->update; 185 $FOCUS->update;
183} 186}
184 187
185sub focus_out { 188sub focus_out {
186 my ($self) = @_; 189 my ($self) = @_;
187 190
188 return unless $FOCUS == $self; 191 return unless $FOCUS == $self;
189 192
190 my $focus = $FOCUS; undef $FOCUS; 193 my $focus = $FOCUS; undef $FOCUS;
194
195 $self->emit (focus_out => $focus);
196
191 $focus->update if $focus; #? 197 $focus->update if $focus; #?
192} 198}
193 199
194sub mouse_motion { } 200sub mouse_motion { }
195sub button_up { } 201sub button_up { }
219 glPopMatrix; 225 glPopMatrix;
220 226
221 if ($self == $HOVER && $self->{can_hover}) { 227 if ($self == $HOVER && $self->{can_hover}) {
222 my ($x, $y) = @$self{qw(x y)}; 228 my ($x, $y) = @$self{qw(x y)};
223 229
224 glColor 0, 0, 1, 0.2; 230 glColor 1, 0.8, 0.5, 0.2;
225 glEnable GL_BLEND; 231 glEnable GL_BLEND;
226 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
227 glBegin GL_QUADS; 233 glBegin GL_QUADS;
228 glVertex $x , $y; 234 glVertex $x , $y;
229 glVertex $x + $self->{w}, $y; 235 glVertex $x + $self->{w}, $y;
565 # TODO: user_x, user_y, overwrite moveto? 571 # TODO: user_x, user_y, overwrite moveto?
566 572
567 $class->SUPER::new ( 573 $class->SUPER::new (
568 bg => [1, 1, 1, 1], 574 bg => [1, 1, 1, 1],
569 border_bg => [1, 1, 1, 1], 575 border_bg => [1, 1, 1, 1],
570 border => $::FONTSIZE * 0.8, 576 border => int $::FONTSIZE * 0.8,
571 @_ 577 @_
572 ) 578 )
573} 579}
574 580
575sub size_request { 581sub size_request {
704 710
705 $child->set_parent ($self); 711 $child->set_parent ($self);
706 $self->{children}[$y][$x] = $child; 712 $self->{children}[$y][$x] = $child;
707 713
708 $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};
709 $self->update; 723 $self->update;
710} 724}
711 725
712sub get_wh { 726sub get_wh {
713 my ($self) = @_; 727 my ($self) = @_;
942 my $self = $class->SUPER::new ( 956 my $self = $class->SUPER::new (
943 fg => [1, 1, 1], 957 fg => [1, 1, 1],
944 fontsize => $::FONTSIZE, 958 fontsize => $::FONTSIZE,
945 text => "", 959 text => "",
946 align => -1, 960 align => -1,
961 valign => -1,
947 padding => 2, 962 padding => 2,
948 layout => new CFClient::Layout, 963 layout => new CFClient::Layout,
949 %arg 964 %arg
950 ); 965 );
951 966
952 $self->set_text ($self->{text}); 967 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d#
968
969 $self->set_text (delete $self->{text}) if exists $self->{text};
970 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
953 971
954 $self 972 $self
955} 973}
956 974
957sub escape_text { 975sub escape_text {
965} 983}
966 984
967sub set_text { 985sub set_text {
968 my ($self, $text) = @_; 986 my ($self, $text) = @_;
969 987
970 $self->{text} = $text;
971 $self->{layout}->set_markup ($text); 988 $self->{layout}->set_text ($text);
972 989
973 delete $self->{texture}; 990 delete $self->{texture};
974# $self->{w} = $self->{h} = -1;
975 $self->update; 991 $self->update;
976} 992}
977 993
978sub get_text { 994sub set_markup {
979 my ($self, $text) = @_; 995 my ($self, $markup) = @_;
980 996
981 $self->{text} 997 $self->{layout}->set_markup ($markup);
998
999 delete $self->{texture};
1000 $self->update;
982} 1001}
983 1002
984sub size_request { 1003sub size_request {
985 my ($self) = @_; 1004 my ($self) = @_;
986 1005
987 $self->{layout}->set_width; 1006 $self->{layout}->set_width;
988 $self->{layout}->set_height ($self->{fontsize}); 1007 $self->{layout}->set_height ($self->{fontsize});
1008
989 my ($w, $h) = $self->{layout}->size; 1009 my ($w, $h) = $self->{layout}->size;
990 1010
991 ( 1011 (
992 $w + $self->{padding} * 2, 1012 $w + $self->{padding} * 2,
993 $h + $self->{padding} * 2, 1013 $h + $self->{padding} * 2,
1012sub _draw { 1032sub _draw {
1013 my ($self) = @_; 1033 my ($self) = @_;
1014 1034
1015 my $tex = $self->{texture} ||= do { 1035 my $tex = $self->{texture} ||= do {
1016 $self->{layout}->set_width ($self->{w}); 1036 $self->{layout}->set_width ($self->{w});
1017 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1037 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize});
1018 new_from_layout CFClient::Texture $self->{layout} 1038 new_from_layout CFClient::Texture $self->{layout}
1019 }; 1039 };
1020 1040
1021 glEnable GL_BLEND; 1041 glEnable GL_BLEND;
1022 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1042 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1023 glEnable GL_TEXTURE_2D; 1043 glEnable GL_TEXTURE_2D;
1024 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1044 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1025 1045
1026 glColor @{$self->{fg}}; 1046 glColor @{$self->{fg}};
1027 1047
1028 my $x = 1048 $self->{ox} = int (
1029 $self->{align} < 0 ? $self->{padding} 1049 $self->{align} < 0 ? $self->{padding}
1030 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1050 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1031 : ($self->{w} - $tex->{w}) * 0.5; 1051 : ($self->{w} - $tex->{w}) * 0.5
1052 );
1032 1053
1033 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1054 $self->{oy} = int (
1055 $self->{valign} < 0 ? $self->{padding}
1056 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1057 : ($self->{h} - $tex->{h}) * 0.5
1058 );
1059
1060 $tex->draw_quad ($self->{ox}, $self->{oy});
1034 1061
1035 glDisable GL_TEXTURE_2D; 1062 glDisable GL_TEXTURE_2D;
1036 glDisable GL_BLEND; 1063 glDisable GL_BLEND;
1037} 1064}
1038 1065
1039############################################################################# 1066#############################################################################
1040 1067
1041package CFClient::UI::Entry; 1068package CFClient::UI::EntryBase;
1042 1069
1043our @ISA = CFClient::UI::Label::; 1070our @ISA = CFClient::UI::Label::;
1044 1071
1045use SDL; 1072use SDL;
1046use SDL::OpenGL; 1073use SDL::OpenGL;
1053 bg => [0, 0, 0, 0.2], 1080 bg => [0, 0, 0, 0.2],
1054 active_bg => [1, 1, 1, 0.5], 1081 active_bg => [1, 1, 1, 0.5],
1055 active_fg => [0, 0, 0], 1082 active_fg => [0, 0, 0],
1056 can_hover => 1, 1083 can_hover => 1,
1057 can_focus => 1, 1084 can_focus => 1,
1085 valign => 0,
1058 @_ 1086 @_
1059 ) 1087 )
1060} 1088}
1061 1089
1062sub _set_text { 1090sub _set_text {
1063 my ($self, $text) = @_; 1091 my ($self, $text) = @_;
1064 1092
1065 my $old_text = $self->{text}; 1093 delete $self->{cur_h};
1094
1095 return if $self->{text} eq $text;
1066 1096
1067 $self->{last_activity} = $::NOW; 1097 $self->{last_activity} = $::NOW;
1068
1069 $self->{text} = $text; 1098 $self->{text} = $text;
1070 $self->{layout}->set_width ($self->{w});
1071 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1072 1099
1073 $text =~ s/./*/g if $self->{hidden}; 1100 $text =~ s/./*/g if $self->{hidden};
1101 $self->{layout}->set_text ("$text ");
1074 1102
1075 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1103 $self->emit (changed => $self->{text});
1104}
1076 1105
1077 $text = substr $text, 0, $self->{cursor}; 1106sub get_text {
1078 utf8::encode $text; 1107 $_[0]{text}
1079
1080 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1081
1082 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1083 if $old_text ne $self->{text};
1084} 1108}
1085 1109
1086sub size_request { 1110sub size_request {
1087 my ($self) = @_; 1111 my ($self) = @_;
1088 1112
1192 1216
1193 $self->SUPER::_draw; 1217 $self->SUPER::_draw;
1194 1218
1195 #TODO: force update every cursor change :( 1219 #TODO: force update every cursor change :(
1196 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1220 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1221
1222 unless (exists $self->{cur_h}) {
1223 my $text = substr $self->{text}, 0, $self->{cursor};
1224 utf8::encode $text;
1225
1226 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1227 }
1228
1197 glColor @{$self->{fg}}; 1229 glColor @{$self->{fg}};
1198 glBegin GL_LINES; 1230 glBegin GL_LINES;
1199 glVertex $self->{cur_x}, $self->{cur_y};
1200 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1231 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1232 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1201 glEnd; 1233 glEnd;
1202 } 1234 }
1203} 1235}
1204 1236
1205package CFClient::UI::LineEntry; 1237package CFClient::UI::Entry;
1206 1238
1207our @ISA = CFClient::UI::Entry::; 1239our @ISA = CFClient::UI::EntryBase::;
1208 1240
1209use SDL; 1241use SDL;
1210use SDL::OpenGL; 1242use SDL::OpenGL;
1211 1243
1212sub key_down { 1244sub key_down {
1242 1274
1243 $class->SUPER::new ( 1275 $class->SUPER::new (
1244 padding => 4, 1276 padding => 4,
1245 fg => [1, 1, 1], 1277 fg => [1, 1, 1],
1246 bg => [1, 1, 1, 0.2], 1278 bg => [1, 1, 1, 0.2],
1247 active_fg => [1, 1, 0], 1279 active_fg => [0, 0, 1],
1248 can_hover => 1, 1280 can_hover => 1,
1249 @_ 1281 @_
1250 ) 1282 )
1251} 1283}
1252 1284
1261 1293
1262sub _draw { 1294sub _draw {
1263 my ($self) = @_; 1295 my ($self) = @_;
1264 1296
1265 local $self->{fg} = $self->{fg}; 1297 local $self->{fg} = $self->{fg};
1266 my $tex = $tex[0];
1267
1268 glEnable GL_BLEND;
1269 glEnable GL_TEXTURE_2D;
1270 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1271 1298
1272 if ($GRAB == $self) { 1299 if ($GRAB == $self) {
1273 $self->{fg} = $self->{active_fg}; 1300 $self->{fg} = $self->{active_fg};
1274 } 1301 }
1275 1302
1276 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1303 glEnable GL_BLEND;
1304 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1305 glEnable GL_TEXTURE_2D;
1277 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1306 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1307 glColor 0, 0, 0, 1;
1278 1308
1279 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1309 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1280 1310
1281 glDisable GL_TEXTURE_2D; 1311 glDisable GL_TEXTURE_2D;
1282 glDisable GL_BLEND; 1312 glDisable GL_BLEND;
1283 1313
1284 $self->SUPER::_draw; 1314 $self->SUPER::_draw;
1766 0.1, 0.1, 0.1, 1796 0.1, 0.1, 0.1,
1767 ; 1797 ;
1768 glEnable GL_CONVOLUTION_2D; 1798 glEnable GL_CONVOLUTION_2D;
1769 } 1799 }
1770 1800
1771 my $tex = new CFClient::Texture 1801 $self->{fow_texture} = new CFClient::Texture
1772 w => $w, 1802 w => $w,
1773 h => $h, 1803 h => $h,
1774 data => $data, 1804 data => $data,
1775 internalformat => GL_ALPHA, 1805 internalformat => GL_ALPHA,
1776 format => GL_ALPHA; 1806 format => GL_ALPHA;
1781 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1811 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1782 glEnable GL_TEXTURE_2D; 1812 glEnable GL_TEXTURE_2D;
1783 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1813 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1784 1814
1785 glColor +($::CFG->{fow_intensity}) x 3, 1; 1815 glColor +($::CFG->{fow_intensity}) x 3, 1;
1786 $tex->draw_quad (0, 0, $w * 32, $h * 32); 1816 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1787 1817
1788 glDisable GL_TEXTURE_2D; 1818 glDisable GL_TEXTURE_2D;
1789 glDisable GL_BLEND; 1819 glDisable GL_BLEND;
1790 } 1820 }
1791 1821
1797 glEnable GL_BLEND; 1827 glEnable GL_BLEND;
1798 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1828 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1799 glEnable GL_TEXTURE_2D; 1829 glEnable GL_TEXTURE_2D;
1800 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1830 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1801 1831
1832 $self->{mapmap_texture} =
1802 CFClient::Texture->new ( 1833 new CFClient::Texture
1803 w => $w, 1834 w => $w,
1804 h => $h, 1835 h => $h,
1805 data => $::MAP->mapmap ($w, $h), 1836 data => $::MAP->mapmap ($w, $h),
1806 type => GL_UNSIGNED_INT_8_8_8_8_REV 1837 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1807 )->draw_quad (100, 100); 1838
1839 $self->{mapmap_texture}->draw_quad (100, 100);
1808 1840
1809 glDisable GL_TEXTURE_2D; 1841 glDisable GL_TEXTURE_2D;
1810 glDisable GL_BLEND; 1842 glDisable GL_BLEND;
1811 } 1843 }
1812 # HACK END 1844 # HACK END
1854 1886
1855 my $mod = $ev->key_mod; 1887 my $mod = $ev->key_mod;
1856 my $sym = $ev->key_sym; 1888 my $sym = $ev->key_sym;
1857 1889
1858 if ($sym == SDLK_KP5) { 1890 if ($sym == SDLK_KP5) {
1859 $::CONN->user_send ("command stay fire"); 1891 $::CONN->user_send ("stay fire");
1860 } elsif ($sym == SDLK_a) { 1892 } elsif ($sym == SDLK_a) {
1861 $::CONN->user_send ("command apply"); 1893 $::CONN->user_send ("apply");
1862 } elsif ($sym == SDLK_QUOTE) { 1894 } elsif ($sym == SDLK_QUOTE) {
1863 $self->emit ('activate_console'); 1895 $self->emit ('activate_console');
1864 } elsif ($sym == SDLK_SLASH) { 1896 } elsif ($sym == SDLK_SLASH) {
1865 $self->emit ('activate_console' => '/'); 1897 $self->emit ('activate_console' => '/');
1866 } elsif (exists $DIR{$sym}) { 1898 } elsif (exists $DIR{$sym}) {
1867 if ($mod & KMOD_SHIFT) { 1899 if ($mod & KMOD_SHIFT) {
1868 $self->{shft}++; 1900 $self->{shft}++;
1869 $::CONN->user_send ("command fire $DIR{$sym}[0]"); 1901 $::CONN->user_send ("fire $DIR{$sym}[0]");
1870 } elsif ($mod & KMOD_CTRL) { 1902 } elsif ($mod & KMOD_CTRL) {
1871 $self->{ctrl}++; 1903 $self->{ctrl}++;
1872 $::CONN->user_send ("command run $DIR{$sym}[0]"); 1904 $::CONN->user_send ("run $DIR{$sym}[0]");
1873 } else { 1905 } else {
1874 $::CONN->user_send ("command $DIR{$sym}[1]"); 1906 $::CONN->user_send ("$DIR{$sym}[1]");
1875 } 1907 }
1876 } 1908 }
1877} 1909}
1878 1910
1879sub key_up { 1911sub key_up {
1881 1913
1882 my $mod = $ev->key_mod; 1914 my $mod = $ev->key_mod;
1883 my $sym = $ev->key_sym; 1915 my $sym = $ev->key_sym;
1884 1916
1885 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 1917 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1886 $::CONN->user_send ("command fire_stop"); 1918 $::CONN->user_send ("fire_stop");
1887 } 1919 }
1888 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 1920 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1889 $::CONN->user_send ("command run_stop"); 1921 $::CONN->user_send ("run_stop");
1890 } 1922 }
1891} 1923}
1892 1924
1893############################################################################# 1925#############################################################################
1894 1926
2008sub add { 2040sub add {
2009 my ($self, $widget) = @_; 2041 my ($self, $widget) = @_;
2010 2042
2011 $self->SUPER::add ($widget); 2043 $self->SUPER::add ($widget);
2012 2044
2013 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2045 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2014} 2046}
2015 2047
2016sub on_refresh { 2048sub on_refresh {
2017 my ($self, $id, $cb) = @_; 2049 my ($self, $id, $cb) = @_;
2018 2050

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines