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.99 by elmex, Fri Apr 14 10:57:35 2006 UTC vs.
Revision 1.106 by root, Fri Apr 14 15:14:25 2006 UTC

61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $TOPLEVEL->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; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $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->translate ($x, $y)) if $HOVER;
71} 71}
72 72
439} 439}
440 440
441sub render_chld { 441sub render_chld {
442 my ($self) = @_; 442 my ($self) = @_;
443 443
444 $self->{texture} = 444 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
445 CFClient::Texture->new_from_opengl ( 445 glClearColor 0, 0, 0, 1;
446 $self->{w}, $self->{h}, sub { $self->child->draw } 446 glClear GL_COLOR_BUFFER_BIT;
447 ); 447 $self->child->draw;
448 };
448} 449}
449 450
450sub size_allocate { 451sub size_allocate {
451 my ($self, $x, $y, $w, $h) = @_; 452 my ($self, $x, $y, $w, $h) = @_;
452 453
639 # TODO: repeat texture not scale 640 # TODO: repeat texture not scale
640 my $rep_x = $cw / $bg->{w}; 641 my $rep_x = $cw / $bg->{w};
641 my $rep_y = $ch / $bg->{h}; 642 my $rep_y = $ch / $bg->{h};
642 643
643 glColor @{ $self->{bg} }; 644 glColor @{ $self->{bg} };
645
646 $bg->{s} = $rep_x;
647 $bg->{t} = $rep_y;
648 $bg->{wrap_mode} = 1;
644 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 649 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch);
645 650
646 glDisable GL_TEXTURE_2D; 651 glDisable GL_TEXTURE_2D;
647 glDisable GL_BLEND; 652 glDisable GL_BLEND;
648 653
727 my $col_expand = (sum @col_expand) || 1; 732 my $col_expand = (sum @col_expand) || 1;
728 733
729 # linearly scale sizes 734 # linearly scale sizes
730 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
731 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 736 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
737
738 $_ = int $_ for @$ws; #TODO: avoid rounding problems
739 $_ = int $_ for @$hs; #TODO: avoid rounding problems
732 740
733 my $y; 741 my $y;
734 742
735 for my $r (0 .. $#{$self->{children}}) { 743 for my $r (0 .. $#{$self->{children}}) {
736 my $row = $self->{children}[$r] 744 my $row = $self->{children}[$r]
827 } 835 }
828 836
829 my $y = 0; 837 my $y = 0;
830 for (0 .. $#$children) { 838 for (0 .. $#$children) {
831 my $child = $children->[$_]; 839 my $child = $children->[$_];
832 my $h = $h[$_]; 840 my $h = int $h[$_];
833 $child->size_allocate ($y, 0, $h, $w); 841 $child->size_allocate ($y, 0, $h, $w);
834 842
835 $y += $h; 843 $y += $h;
836 } 844 }
837} 845}
881 } 889 }
882 890
883 my $y = 0; 891 my $y = 0;
884 for (0 .. $#$children) { 892 for (0 .. $#$children) {
885 my $child = $children->[$_]; 893 my $child = $children->[$_];
886 my $h = $h[$_]; 894 my $h = int $h[$_];
887 $child->size_allocate (0, $y, $w, $h); 895 $child->size_allocate (0, $y, $w, $h);
888 896
889 $y += $h; 897 $y += $h;
890 } 898 }
891} 899}
900 908
901sub new { 909sub new {
902 my ($class, %arg) = @_; 910 my ($class, %arg) = @_;
903 911
904 my $self = $class->SUPER::new ( 912 my $self = $class->SUPER::new (
905 fg => [1, 1, 1], 913 fg => [1, 1, 1],
906 height => $::FONTSIZE, 914 fontsize => $::FONTSIZE,
907 text => "", 915 text => "",
908 align => -1, 916 align => -1,
909 padding => 2, 917 padding => 2,
910 layout => new CFClient::Layout, 918 layout => new CFClient::Layout,
911 %arg 919 %arg
912 ); 920 );
913 921
914 $self->set_text ($self->{text}); 922 $self->set_text ($self->{text});
915 923
945 953
946sub size_request { 954sub size_request {
947 my ($self) = @_; 955 my ($self) = @_;
948 956
949 $self->{layout}->set_width; 957 $self->{layout}->set_width;
950 $self->{layout}->set_height ($self->{height}); 958 $self->{layout}->set_height ($self->{fontsize});
951 my ($w, $h) = $self->{layout}->size; 959 my ($w, $h) = $self->{layout}->size;
952 960
953 ( 961 (
954 $w + $self->{padding} * 2, 962 $w + $self->{padding} * 2,
955 $h + $self->{padding} * 2, 963 $h + $self->{padding} * 2,
974sub _draw { 982sub _draw {
975 my ($self) = @_; 983 my ($self) = @_;
976 984
977 my $tex = $self->{texture} ||= do { 985 my $tex = $self->{texture} ||= do {
978 $self->{layout}->set_width ($self->{w}); 986 $self->{layout}->set_width ($self->{w});
979 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 987 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
980 new_from_layout CFClient::Texture $self->{layout} 988 new_from_layout CFClient::Texture $self->{layout}
981 }; 989 };
982 990
983 glEnable GL_BLEND; 991 glEnable GL_BLEND;
984 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 992 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
985 glEnable GL_TEXTURE_2D; 993 glEnable GL_TEXTURE_2D;
986 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 994 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
987 995
988 glColor @{$self->{fg}}; 996 glColor @{$self->{fg}};
989 997
990 my $x = 998 my $x =
991 $self->{align} < 0 ? $self->{padding} 999 $self->{align} < 0 ? $self->{padding}
992 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1000 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
993 : ($self->{w} - $tex->{w}) * 0.5; 1001 : ($self->{w} - $tex->{w}) * 0.5;
994 1002
995 glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; 1003 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5);
996 $tex->draw_quad (0, 0);
997 1004
998 glDisable GL_TEXTURE_2D; 1005 glDisable GL_TEXTURE_2D;
999 glDisable GL_BLEND; 1006 glDisable GL_BLEND;
1000} 1007}
1001 1008
1023} 1030}
1024 1031
1025sub _set_text { 1032sub _set_text {
1026 my ($self, $text) = @_; 1033 my ($self, $text) = @_;
1027 1034
1035 my $old_text = $self->{text};
1036
1028 $self->{last_activity} = $::NOW; 1037 $self->{last_activity} = $::NOW;
1029 1038
1030 $self->{text} = $text; 1039 $self->{text} = $text;
1031 $self->{layout}->set_width ($self->{w}); 1040 $self->{layout}->set_width ($self->{w});
1032 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 1041 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1033 1042
1034 $text =~ s/./*/g if $self->{hidden}; 1043 $text =~ s/./*/g if $self->{hidden};
1035 1044
1036 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1045 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
1037 1046
1038 $text = substr $text, 0, $self->{cursor}; 1047 $text = substr $text, 0, $self->{cursor};
1039 utf8::encode $text; 1048 utf8::encode $text;
1040 1049
1041 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 1050 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1051
1052 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1053 if $old_text ne $self->{text};
1042} 1054}
1043 1055
1044sub size_request { 1056sub size_request {
1045 my ($self) = @_; 1057 my ($self) = @_;
1046 1058
1085 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1097 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1086 } elsif ($sym == SDLK_HOME) { 1098 } elsif ($sym == SDLK_HOME) {
1087 $self->{cursor} = 0; 1099 $self->{cursor} = 0;
1088 } elsif ($sym == SDLK_END) { 1100 } elsif ($sym == SDLK_END) {
1089 $self->{cursor} = length $text; 1101 $self->{cursor} = length $text;
1102 } elsif ($sym == SDLK_ESCAPE) {
1103 $self->emit ('escape');
1090 } elsif ($uni) { 1104 } elsif ($uni) {
1091 substr $text, $self->{cursor}++, 0, chr $uni; 1105 substr $text, $self->{cursor}++, 0, chr $uni;
1092 } 1106 }
1093 1107
1094 $self->_set_text ($text); 1108 $self->_set_text ($text);
1244 1258
1245package CFClient::UI::CheckBox; 1259package CFClient::UI::CheckBox;
1246 1260
1247our @ISA = CFClient::UI::DrawBG::; 1261our @ISA = CFClient::UI::DrawBG::;
1248 1262
1263my @tex =
1264 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1265 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1266
1249use SDL; 1267use SDL;
1250use SDL::OpenGL; 1268use SDL::OpenGL;
1251 1269
1252sub new { 1270sub new {
1253 my $class = shift; 1271 my $class = shift;
1290 $self->SUPER::_draw; 1308 $self->SUPER::_draw;
1291 1309
1292 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1310 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1293 1311
1294 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1312 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1295 1313
1296 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1314 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1297 1315
1298 glBegin GL_LINE_LOOP; 1316 glEnable GL_BLEND;
1299 glVertex 0 , 0; 1317 glEnable GL_TEXTURE_2D;
1300 glVertex 0 , $s; 1318 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1301 glVertex $s, $s;
1302 glVertex $s, 0;
1303 glEnd;
1304 1319
1305 if ($self->{state}) { 1320 my $tex = $self->{state} ? $tex[1] : $tex[0];
1306 glBegin GL_LINES; 1321
1307 glVertex 0 , 0; 1322 $tex->draw_quad (0, 0, $s, $s);
1308 glVertex $s, $s; 1323
1309 glVertex $s, 0; 1324 glDisable GL_TEXTURE_2D;
1310 glVertex 0 , $s; 1325 glDisable GL_BLEND;
1311 glEnd;
1312 }
1313} 1326}
1314 1327
1315############################################################################# 1328#############################################################################
1316 1329
1317package CFClient::UI::Slider; 1330package CFClient::UI::Slider;
1340 range => [0, 0, 100, 10], 1353 range => [0, 0, 100, 10],
1341 req_w => 40, 1354 req_w => 40,
1342 req_h => 13, 1355 req_h => 13,
1343 vertical => 0, 1356 vertical => 0,
1344 can_hover => 1, 1357 can_hover => 1,
1358 inner_pad => 5,
1345 @_ 1359 @_
1346 ); 1360 );
1347 1361
1348 $self 1362 $self
1349} 1363}
1370 if ($GRAB == $self) { 1384 if ($GRAB == $self) {
1371 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1385 my ($value, $lo, $hi, $page) = @{$self->{range}};
1372 1386
1373 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1387 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1374 1388
1389 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1390 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1391
1392 $x -= $inner_pad_px; # substract the padding
1375 $x = $x * ($hi - $lo) / $w + $lo; 1393 $x = $x * ($hi - $lo) / $inner_w + $lo;
1376 $x = $lo if $x < $lo; 1394 $x = $lo if $x < $lo;
1377 $x = $hi - $page if $x > $hi - $page; 1395 $x = $hi - $page if $x > $hi - $page;
1378 $self->{range}[0] = $x; 1396 $self->{range}[0] = $x;
1379 1397
1380 $self->emit (changed => $x); 1398 $self->emit (changed => $x);
1381 $self->update; 1399 $self->update;
1382 } 1400 }
1401}
1402
1403# the inner_* stuff is for generating a padding for the slider handle,
1404# so that the handle doesn't leave the texture. This calculation isn't 100%
1405# correct propably, but it does the job for now
1406sub _calc_inner_pad_px {
1407 my ($self, $w) = @_;
1408 ($w / 100) * $self->{inner_pad} # % to pixels
1383} 1409}
1384 1410
1385sub _draw { 1411sub _draw {
1386 my ($self) = @_; 1412 my ($self) = @_;
1387 1413
1401 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1427 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1402 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1428 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1403 1429
1404 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1430 my ($value, $lo, $hi, $page) = @{$self->{range}};
1405 1431
1406 # the inner_* stuff is for generating a padding for the slider handle, 1432 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1407 # so that the handle doesn't leave the texture. This calculation isn't 100%
1408 # correct propably, but it does the job for now
1409 my $inner_pad = 5; # 5% of width for slider bg texture border
1410 my $inner_pad_px = ($w / 100) * $inner_pad; # % to pixels
1411 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 1433 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1412 1434
1413 $page = int $page * $inner_w / ($hi - $lo); 1435 $page = int $page * $inner_w / ($hi - $lo);
1414 $value = int +($value - $lo) * $inner_w / ($hi - $lo); 1436 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1415 1437
1416 $w -= $page; 1438 $w -= $page;
1446 1468
1447sub new { 1469sub new {
1448 my $class = shift; 1470 my $class = shift;
1449 1471
1450 my $self = $class->SUPER::new ( 1472 my $self = $class->SUPER::new (
1473 req_w => $::WIDTH / 6,
1474 req_h => $::HEIGHT / 6,
1475 fontsize => $::FONTSIZE,
1476 @_,
1477
1478 layout => (new CFClient::Layout),
1451 par => [], 1479 par => [],
1452 @_, 1480 height => 0,
1453 children => [ 1481 children => [
1454 (new CFClient::UI::Empty expand => 1), 1482 (new CFClient::UI::Empty expand => 1),
1455 (new CFClient::UI::Slider vertical => 1), 1483 (new CFClient::UI::Slider vertical => 1),
1456 ], 1484 ],
1457 ); 1485 );
1458 1486
1459 $self 1487 $self
1460} 1488}
1461 1489
1490sub text_height {
1491 my ($self, $text) = @_;
1492
1493 my $layout = $self->{layout};
1494
1495 $layout->set_height ($self->{fontsize});
1496 $layout->set_width ($self->{w});
1497 $layout->set_text ($text);
1498
1499 ($layout->size)[1]
1500}
1501
1502sub reflow {
1503 my ($self) = @_;
1504
1505 my $height = 0;
1506
1507 for (@{$self->{par}}) {
1508 $height += $_->[0] = $self->text_height ($_->[2]);
1509 }
1510
1511 $self->{height} = $height;
1512
1513 $self->{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1514}
1515
1516sub size_request {
1517 my ($self) = @_;
1518
1519 ($self->{req_w}, $self->{req_h})
1520}
1521
1522sub size_allocate {
1523 my ($self, $x, $y, $w, $h) = @_;
1524
1525 $self->SUPER::size_allocate ($x, $y, $w, $h);
1526
1527 $self->{layout}->set_height ($self->{fontsize});
1528 $self->{layout}->set_width ($self->{w});
1529
1530 $self->reflow;
1531 $self->update;
1532}
1533
1462sub add_paragraph { 1534sub add_paragraph {
1463 my ($self, $color, $text) = @_; 1535 my ($self, $color, $text) = @_;
1464 1536
1537 #TODO: intelligently "reformat" paragraph
1538
1539 my $height = $self->text_height ($text);
1540
1541 $self->{height} += $height;
1542
1465 push @{$self->{par}}, [$color, $text]; 1543 push @{$self->{par}}, [$height, $color, $text];
1466 my $count = scalar @{$self->{par}}; 1544
1467 $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; 1545 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1468 $self->{children}[1]->update; 1546 $self->{children}[1]->update;
1469} 1547}
1470 1548
1549sub update {
1550 my ($self) = @_;
1551
1552 $self->SUPER::update;
1553
1554 return unless $self->{h} > 0;
1555
1556 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1557 glClearColor 0, 0, 0, 1;
1558 glClear GL_COLOR_BUFFER_BIT;
1559
1560 my $y0 = $self->{height} - $self->{h};
1561 my $y1 = $self->{height};
1562
1563 glEnable GL_BLEND;
1564 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1565 glEnable GL_TEXTURE_2D;
1566 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1567
1568 my $y = 0;
1569
1570 my $layout = $self->{layout};
1571
1572 for my $par (@{$self->{par}}) {
1573 my $h = $par->[0];
1574
1575 if ($y0 < $y + $h && $y < $y1) {
1576 $layout->set_text ($par->[2]);
1577
1578 glColor @{ $par->[1] };
1579 my ($W, $H) = $layout->size;
1580 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1581 }
1582
1583 $y += $h;
1584 }
1585
1586 glDisable GL_TEXTURE_2D;
1587 glDisable GL_BLEND;
1588 };
1589}
1590
1471sub _draw { 1591sub _draw {
1472 my ($self) = @_; 1592 my ($self) = @_;
1473 1593
1474 my $par = $self->{par}; 1594 if ($self->{texture}) {
1475
1476 my $bottom = (scalar @$par) - 1;
1477
1478 my $w = $self->{children}[0]{w};
1479 my $y = $self->{h};
1480
1481 glEnable GL_BLEND;
1482 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1483 glEnable GL_TEXTURE_2D; 1595 glEnable GL_TEXTURE_2D;
1484 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1485 1597 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1486 # TODO: everything!
1487 while ($y > 0 && $bottom >= 0) {
1488 my $par = $par->[$bottom--];
1489
1490 my $layout = new CFClient::Layout;
1491
1492 $layout->set_height ($::FONTSIZE);
1493 $layout->set_width ($w);
1494 $layout->set_text ($par->[1]);
1495 my $tex = new_from_layout CFClient::Texture $layout;
1496 $y -= $tex->{h};
1497
1498 glColor @{ $par->[0] };
1499 $tex->draw_quad (0, $y);
1500 }
1501
1502 glDisable GL_TEXTURE_2D; 1598 glDisable GL_TEXTURE_2D;
1503 glDisable GL_BLEND; 1599 }
1600
1601 $self->{children}[1]->draw;
1602
1504} 1603}
1505 1604
1506############################################################################# 1605#############################################################################
1507 1606
1508package CFClient::UI::MapWidget; 1607package CFClient::UI::MapWidget;
1641 1740
1642 if ($sym == SDLK_KP5) { 1741 if ($sym == SDLK_KP5) {
1643 $::CONN->user_send ("command stay fire"); 1742 $::CONN->user_send ("command stay fire");
1644 } elsif ($sym == SDLK_a) { 1743 } elsif ($sym == SDLK_a) {
1645 $::CONN->user_send ("command apply"); 1744 $::CONN->user_send ("command apply");
1745 } elsif ($sym == SDLK_QUOTE) {
1746 $self->emit ('activate_console');
1747 } elsif ($sym == SDLK_SLASH) {
1748 $self->emit ('activate_console' => '/');
1646 } elsif (exists $DIR{$sym}) { 1749 } elsif (exists $DIR{$sym}) {
1647 if ($mod & KMOD_SHIFT) { 1750 if ($mod & KMOD_SHIFT) {
1648 $self->{shft}++; 1751 $self->{shft}++;
1649 $::CONN->user_send ("command fire $DIR{$sym}[0]"); 1752 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1650 } elsif ($mod & KMOD_CTRL) { 1753 } elsif ($mod & KMOD_CTRL) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines