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.98 by root, Fri Apr 14 02:06:22 2006 UTC vs.
Revision 1.104 by elmex, Fri Apr 14 14:02:52 2006 UTC

639 # TODO: repeat texture not scale 639 # TODO: repeat texture not scale
640 my $rep_x = $cw / $bg->{w}; 640 my $rep_x = $cw / $bg->{w};
641 my $rep_y = $ch / $bg->{h}; 641 my $rep_y = $ch / $bg->{h};
642 642
643 glColor @{ $self->{bg} }; 643 glColor @{ $self->{bg} };
644
645 $bg->{s} = $rep_x;
646 $bg->{t} = $rep_y;
647 $bg->{wrap_mode} = 1;
644 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 648 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch);
645 649
646 glDisable GL_TEXTURE_2D; 650 glDisable GL_TEXTURE_2D;
647 glDisable GL_BLEND; 651 glDisable GL_BLEND;
648 652
1023} 1027}
1024 1028
1025sub _set_text { 1029sub _set_text {
1026 my ($self, $text) = @_; 1030 my ($self, $text) = @_;
1027 1031
1032 my $old_text = $self->{text};
1033
1028 $self->{last_activity} = $::NOW; 1034 $self->{last_activity} = $::NOW;
1029 1035
1030 $self->{text} = $text; 1036 $self->{text} = $text;
1031 $self->{layout}->set_width ($self->{w}); 1037 $self->{layout}->set_width ($self->{w});
1032 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 1038 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
1037 1043
1038 $text = substr $text, 0, $self->{cursor}; 1044 $text = substr $text, 0, $self->{cursor};
1039 utf8::encode $text; 1045 utf8::encode $text;
1040 1046
1041 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1047 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1048
1049 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1050 if $old_text ne $self->{text};
1042} 1051}
1043 1052
1044sub size_request { 1053sub size_request {
1045 my ($self) = @_; 1054 my ($self) = @_;
1046 1055
1085 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1094 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1086 } elsif ($sym == SDLK_HOME) { 1095 } elsif ($sym == SDLK_HOME) {
1087 $self->{cursor} = 0; 1096 $self->{cursor} = 0;
1088 } elsif ($sym == SDLK_END) { 1097 } elsif ($sym == SDLK_END) {
1089 $self->{cursor} = length $text; 1098 $self->{cursor} = length $text;
1099 } elsif ($sym == SDLK_ESCAPE) {
1100 $self->emit ('escape');
1090 } elsif ($uni) { 1101 } elsif ($uni) {
1091 substr $text, $self->{cursor}++, 0, chr $uni; 1102 substr $text, $self->{cursor}++, 0, chr $uni;
1092 } 1103 }
1093 1104
1094 $self->_set_text ($text); 1105 $self->_set_text ($text);
1156 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1167 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1157 glEnd; 1168 glEnd;
1158 } 1169 }
1159} 1170}
1160 1171
1172package CFClient::UI::LineEntry;
1173
1174our @ISA = CFClient::UI::Entry::;
1175
1176use SDL;
1177use SDL::OpenGL;
1178
1179sub key_down {
1180 my ($self, $ev) = @_;
1181
1182 my $sym = $ev->key_sym;
1183
1184 if ($sym == SDLK_RETURN) {
1185 $self->emit (activate => $self->get_text);
1186 $self->update;
1187
1188 } else {
1189 $self->SUPER::key_down ($ev);
1190 }
1191
1192}
1193
1161############################################################################# 1194#############################################################################
1162 1195
1163package CFClient::UI::Button; 1196package CFClient::UI::Button;
1164 1197
1165our @ISA = CFClient::UI::Label::; 1198our @ISA = CFClient::UI::Label::;
1222 1255
1223package CFClient::UI::CheckBox; 1256package CFClient::UI::CheckBox;
1224 1257
1225our @ISA = CFClient::UI::DrawBG::; 1258our @ISA = CFClient::UI::DrawBG::;
1226 1259
1260my @tex =
1261 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1262 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1263
1227use SDL; 1264use SDL;
1228use SDL::OpenGL; 1265use SDL::OpenGL;
1229 1266
1230sub new { 1267sub new {
1231 my $class = shift; 1268 my $class = shift;
1268 $self->SUPER::_draw; 1305 $self->SUPER::_draw;
1269 1306
1270 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1307 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1271 1308
1272 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1309 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1273 1310
1274 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1311 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1275 1312
1276 glBegin GL_LINE_LOOP; 1313 glEnable GL_BLEND;
1277 glVertex 0 , 0; 1314 glEnable GL_TEXTURE_2D;
1278 glVertex 0 , $s; 1315 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1279 glVertex $s, $s;
1280 glVertex $s, 0;
1281 glEnd;
1282 1316
1283 if ($self->{state}) { 1317 my $tex = $self->{state} ? $tex[1] : $tex[0];
1284 glBegin GL_LINES; 1318
1285 glVertex 0 , 0; 1319 $tex->draw_quad (0, 0, $s, $s);
1286 glVertex $s, $s; 1320
1287 glVertex $s, 0; 1321 glDisable GL_TEXTURE_2D;
1288 glVertex 0 , $s; 1322 glDisable GL_BLEND;
1289 glEnd;
1290 }
1291} 1323}
1292 1324
1293############################################################################# 1325#############################################################################
1294 1326
1295package CFClient::UI::Slider; 1327package CFClient::UI::Slider;
1297use strict; 1329use strict;
1298 1330
1299use SDL::OpenGL; 1331use SDL::OpenGL;
1300 1332
1301our @ISA = CFClient::UI::DrawBG::; 1333our @ISA = CFClient::UI::DrawBG::;
1334
1335my @tex =
1336 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1337 qw(s1_slider.png s1_slider_bg.png);
1302 1338
1303sub new { 1339sub new {
1304 my $class = shift; 1340 my $class = shift;
1305 1341
1306 # range [value, low, high, page] 1342 # range [value, low, high, page]
1314 range => [0, 0, 100, 10], 1350 range => [0, 0, 100, 10],
1315 req_w => 40, 1351 req_w => 40,
1316 req_h => 13, 1352 req_h => 13,
1317 vertical => 0, 1353 vertical => 0,
1318 can_hover => 1, 1354 can_hover => 1,
1355 inner_pad => 5,
1319 @_ 1356 @_
1320 ); 1357 );
1321 1358
1322 $self 1359 $self
1323} 1360}
1344 if ($GRAB == $self) { 1381 if ($GRAB == $self) {
1345 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1382 my ($value, $lo, $hi, $page) = @{$self->{range}};
1346 1383
1347 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1384 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1348 1385
1386 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1387 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1388
1389 $x -= $inner_pad_px; # substract the padding
1349 $x = $x * ($hi - $lo) / $w + $lo; 1390 $x = $x * ($hi - $lo) / $inner_w + $lo;
1350 $x = $lo if $x < $lo; 1391 $x = $lo if $x < $lo;
1351 $x = $hi - $page if $x > $hi - $page; 1392 $x = $hi - $page if $x > $hi - $page;
1352 $self->{range}[0] = $x; 1393 $self->{range}[0] = $x;
1353 1394
1354 $self->emit (changed => $x); 1395 $self->emit (changed => $x);
1355 $self->update; 1396 $self->update;
1356 } 1397 }
1398}
1399
1400# the inner_* stuff is for generating a padding for the slider handle,
1401# so that the handle doesn't leave the texture. This calculation isn't 100%
1402# correct propably, but it does the job for now
1403sub _calc_inner_pad_px {
1404 my ($self, $w) = @_;
1405 ($w / 100) * $self->{inner_pad} # % to pixels
1357} 1406}
1358 1407
1359sub _draw { 1408sub _draw {
1360 my ($self) = @_; 1409 my ($self) = @_;
1361 1410
1375 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1424 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1376 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1425 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1377 1426
1378 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1427 my ($value, $lo, $hi, $page) = @{$self->{range}};
1379 1428
1429 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1430 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1431
1380 $page = int $page * $w / ($hi - $lo); 1432 $page = int $page * $inner_w / ($hi - $lo);
1381 $value = int +($value - $lo) * $w / ($hi - $lo); 1433 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1382 1434
1383 $w -= $page; 1435 $w -= $page;
1384 $page &= ~1; 1436 $page &= ~1;
1385 glTranslate $page * 0.5, 0, 0; 1437 glTranslate $page * 0.5, 0, 0;
1386 $page ||= 2; 1438 $page ||= 2;
1387 1439
1388 glColor @$fg;
1389 glBegin GL_LINES;
1390 glVertex 0, 0; glVertex 0, $h;
1391 glVertex $w - 1, 0; glVertex $w - 1, $h;
1392 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1393 glEnd;
1394
1395 my $knob_a = $value - $page * 0.5; 1440 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1396 my $knob_b = $value + $page * 0.5; 1441 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1397 1442
1398 glBegin GL_QUADS; 1443 glEnable GL_BLEND;
1399 glColor @$fg; 1444 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1400 glVertex $knob_a, 0; 1445 glEnable GL_TEXTURE_2D;
1401 glVertex $knob_a, $h; 1446 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1402 glVertex $knob_b, $h;
1403 glVertex $knob_b, 0;
1404 1447
1405 if ($knob_a < $knob_b - 2) { 1448 # draw background
1406 glColor @$bg; 1449 $tex[1]->draw_quad (0, 0, $w, $h);
1407 glVertex $knob_a + 1, 1; 1450
1408 glVertex $knob_a + 1, $h - 1; 1451 # draw handle
1409 glVertex $knob_b - 1, $h - 1; 1452 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1410 glVertex $knob_b - 1, 1; 1453
1411 } 1454 glDisable GL_BLEND;
1412 glEnd; 1455 glDisable GL_TEXTURE_2D;
1413} 1456}
1414 1457
1415############################################################################# 1458#############################################################################
1416 1459
1417package CFClient::UI::TextView; 1460package CFClient::UI::TextView;
1617 1660
1618 if ($sym == SDLK_KP5) { 1661 if ($sym == SDLK_KP5) {
1619 $::CONN->user_send ("command stay fire"); 1662 $::CONN->user_send ("command stay fire");
1620 } elsif ($sym == SDLK_a) { 1663 } elsif ($sym == SDLK_a) {
1621 $::CONN->user_send ("command apply"); 1664 $::CONN->user_send ("command apply");
1665 } elsif ($sym == SDLK_QUOTE) {
1666 $self->emit ('activate_console');
1667 } elsif ($sym == SDLK_SLASH) {
1668 $self->emit ('activate_console' => '/');
1622 } elsif (exists $DIR{$sym}) { 1669 } elsif (exists $DIR{$sym}) {
1623 if ($mod & KMOD_SHIFT) { 1670 if ($mod & KMOD_SHIFT) {
1624 $self->{shft}++; 1671 $self->{shft}++;
1625 $::CONN->user_send ("command fire $DIR{$sym}[0]"); 1672 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1626 } elsif ($mod & KMOD_CTRL) { 1673 } elsif ($mod & KMOD_CTRL) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines