… | |
… | |
260 | my ($self, $parent) = @_; |
260 | my ($self, $parent) = @_; |
261 | |
261 | |
262 | Scalar::Util::weaken ($self->{parent} = $parent); |
262 | Scalar::Util::weaken ($self->{parent} = $parent); |
263 | } |
263 | } |
264 | |
264 | |
|
|
265 | sub check_size { |
|
|
266 | my ($self) = @_; |
|
|
267 | |
|
|
268 | my ($w, $h) = $self->size_request; |
|
|
269 | |
|
|
270 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
271 | $self->{req_w} = $w; |
|
|
272 | $self->{req_h} = $h; |
|
|
273 | |
|
|
274 | $self->{parent}->check_size |
|
|
275 | if $self->{parent}; |
|
|
276 | } |
|
|
277 | } |
|
|
278 | |
265 | sub update { |
279 | sub update { |
266 | my ($self) = @_; |
280 | my ($self) = @_; |
267 | |
281 | |
268 | $self->{parent}->update |
282 | $self->{parent}->update |
269 | if $self->{parent}; |
283 | if $self->{parent}; |
… | |
… | |
368 | sort { $a->{z} <=> $b->{z} } |
382 | sort { $a->{z} <=> $b->{z} } |
369 | @{$self->{children}}, $child |
383 | @{$self->{children}}, $child |
370 | ]; |
384 | ]; |
371 | |
385 | |
372 | $self->{w} = $self->{h} = -1; |
386 | $self->{w} = $self->{h} = -1; |
373 | $self->update; |
387 | |
|
|
388 | $child->check_size; |
374 | } |
389 | } |
375 | |
390 | |
376 | sub remove { |
391 | sub remove { |
377 | my ($self, $widget) = @_; |
392 | my ($self, $widget) = @_; |
378 | |
393 | |
379 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
394 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
380 | |
395 | |
381 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
396 | $self->check_size; |
382 | } |
397 | } |
383 | |
398 | |
384 | sub find_widget { |
399 | sub find_widget { |
385 | my ($self, $x, $y) = @_; |
400 | my ($self, $x, $y) = @_; |
386 | |
401 | |
… | |
… | |
506 | $tex->draw_quad (0, 0, $w, $h); |
521 | $tex->draw_quad (0, 0, $w, $h); |
507 | |
522 | |
508 | glDisable GL_BLEND; |
523 | glDisable GL_BLEND; |
509 | glDisable GL_TEXTURE_2D; |
524 | glDisable GL_TEXTURE_2D; |
510 | } |
525 | } |
|
|
526 | |
|
|
527 | ############################################################################# |
|
|
528 | |
|
|
529 | package CFClient::UI::ViewPort; |
|
|
530 | |
|
|
531 | our @ISA = CFClient::UI::Window::; |
|
|
532 | |
|
|
533 | sub new { die } |
|
|
534 | |
|
|
535 | sub size_request { |
|
|
536 | my ($self) = @_; |
|
|
537 | |
|
|
538 | @$self{qw(child_w child_h)} = $self->child->size_request; |
|
|
539 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
|
|
540 | |
|
|
541 | @$self{qw(child_w child_h)} |
|
|
542 | } |
|
|
543 | |
|
|
544 | sub size_allocate { |
|
|
545 | my ($self, $x, $y, $w, $h) = @_; |
|
|
546 | |
|
|
547 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
548 | } |
|
|
549 | |
|
|
550 | sub _draw { |
|
|
551 | my ($self) = @_; |
|
|
552 | |
|
|
553 | $self->{children}[1]->draw; |
|
|
554 | } |
|
|
555 | |
511 | |
556 | |
512 | ############################################################################# |
557 | ############################################################################# |
513 | |
558 | |
514 | package CFClient::UI::Frame; |
559 | package CFClient::UI::Frame; |
515 | |
560 | |
… | |
… | |
710 | |
755 | |
711 | $child->set_parent ($self); |
756 | $child->set_parent ($self); |
712 | $self->{children}[$y][$x] = $child; |
757 | $self->{children}[$y][$x] = $child; |
713 | |
758 | |
714 | $self->{w} = $self->{h} = -1; |
759 | $self->{w} = $self->{h} = -1; |
715 | $self->update; |
760 | |
|
|
761 | $child->check_size; |
716 | } |
762 | } |
717 | |
763 | |
718 | # TODO: move to container class maybe? send childs a signal on removal? |
764 | # TODO: move to container class maybe? send childs a signal on removal? |
719 | sub clear { |
765 | sub clear { |
720 | my ($self) = @_; |
766 | my ($self) = @_; |
… | |
… | |
846 | } |
892 | } |
847 | |
893 | |
848 | sub size_allocate { |
894 | sub size_allocate { |
849 | my ($self, $x, $y, $w, $h) = @_; |
895 | my ($self, $x, $y, $w, $h) = @_; |
850 | |
896 | |
851 | $self->_size_allocate ($x, $y, $w, $h); |
897 | $self->_size_allocate ($x, $y, $w, $h) or return; |
852 | |
898 | |
853 | ($h, $w) = ($w, $h); |
899 | ($h, $w) = ($w, $h); |
854 | |
900 | |
855 | my $children = $self->{children}; |
901 | my $children = $self->{children}; |
856 | |
902 | |
… | |
… | |
882 | my $h = $h[$_]; |
928 | my $h = $h[$_]; |
883 | $child->size_allocate ($y, 0, $h, $w); |
929 | $child->size_allocate ($y, 0, $h, $w); |
884 | |
930 | |
885 | $y += $h; |
931 | $y += $h; |
886 | } |
932 | } |
|
|
933 | |
|
|
934 | 1 |
887 | } |
935 | } |
888 | |
936 | |
889 | ############################################################################# |
937 | ############################################################################# |
890 | |
938 | |
891 | package CFClient::UI::VBox; |
939 | package CFClient::UI::VBox; |
… | |
… | |
906 | } |
954 | } |
907 | |
955 | |
908 | sub size_allocate { |
956 | sub size_allocate { |
909 | my ($self, $x, $y, $w, $h) = @_; |
957 | my ($self, $x, $y, $w, $h) = @_; |
910 | |
958 | |
911 | $self->_size_allocate ($x, $y, $w, $h); |
959 | $self->_size_allocate ($x, $y, $w, $h) or return; |
912 | |
960 | |
913 | my $children = $self->{children}; |
961 | my $children = $self->{children}; |
914 | |
962 | |
915 | my @h = map +($_->size_request)[1], @$children; |
963 | my @h = map +($_->size_request)[1], @$children; |
916 | |
964 | |
… | |
… | |
938 | my $h = $h[$_]; |
986 | my $h = $h[$_]; |
939 | $child->size_allocate (0, $y, $w, $h); |
987 | $child->size_allocate (0, $y, $w, $h); |
940 | |
988 | |
941 | $y += $h; |
989 | $y += $h; |
942 | } |
990 | } |
|
|
991 | |
|
|
992 | 1 |
943 | } |
993 | } |
944 | |
994 | |
945 | ############################################################################# |
995 | ############################################################################# |
946 | |
996 | |
947 | package CFClient::UI::Label; |
997 | package CFClient::UI::Label; |
… | |
… | |
1116 | } |
1166 | } |
1117 | |
1167 | |
1118 | sub size_allocate { |
1168 | sub size_allocate { |
1119 | my ($self, $x, $y, $w, $h) = @_; |
1169 | my ($self, $x, $y, $w, $h) = @_; |
1120 | |
1170 | |
1121 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1171 | $self->SUPER::size_allocate ($x, $y, $w, $h) or return; |
1122 | |
1172 | |
1123 | $self->_set_text ($self->{text}); |
1173 | $self->_set_text ($self->{text}); |
1124 | } |
1174 | } |
1125 | |
1175 | |
1126 | sub set_text { |
1176 | sub set_text { |
… | |
… | |
1276 | padding => 4, |
1326 | padding => 4, |
1277 | fg => [1, 1, 1], |
1327 | fg => [1, 1, 1], |
1278 | bg => [1, 1, 1, 0.2], |
1328 | bg => [1, 1, 1, 0.2], |
1279 | active_fg => [0, 0, 1], |
1329 | active_fg => [0, 0, 1], |
1280 | can_hover => 1, |
1330 | can_hover => 1, |
|
|
1331 | align => 0, |
|
|
1332 | valign => 0, |
1281 | @_ |
1333 | @_ |
1282 | ) |
1334 | ) |
1283 | } |
1335 | } |
1284 | |
1336 | |
1285 | sub button_up { |
1337 | sub button_up { |
… | |
… | |
1347 | } |
1399 | } |
1348 | |
1400 | |
1349 | sub size_allocate { |
1401 | sub size_allocate { |
1350 | my ($self, $x, $y, $w, $h) = @_; |
1402 | my ($self, $x, $y, $w, $h) = @_; |
1351 | |
1403 | |
1352 | $self->_size_allocate ($x, $y, $w, $h); |
1404 | $self->_size_allocate ($x, $y, $w, $h) or return; |
1353 | } |
1405 | } |
1354 | |
1406 | |
1355 | sub button_down { |
1407 | sub button_down { |
1356 | my ($self, $ev, $x, $y) = @_; |
1408 | my ($self, $ev, $x, $y) = @_; |
1357 | |
1409 | |
… | |
… | |
1381 | |
1433 | |
1382 | $tex->draw_quad (0, 0, $s, $s); |
1434 | $tex->draw_quad (0, 0, $s, $s); |
1383 | |
1435 | |
1384 | glDisable GL_TEXTURE_2D; |
1436 | glDisable GL_TEXTURE_2D; |
1385 | glDisable GL_BLEND; |
1437 | glDisable GL_BLEND; |
|
|
1438 | } |
|
|
1439 | |
|
|
1440 | ############################################################################# |
|
|
1441 | |
|
|
1442 | package CFClient::UI::VGauge; |
|
|
1443 | |
|
|
1444 | our @ISA = CFClient::UI::Base::; |
|
|
1445 | |
|
|
1446 | use SDL::OpenGL; |
|
|
1447 | |
|
|
1448 | my %tex = ( |
|
|
1449 | food => [ |
|
|
1450 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1451 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1452 | ], |
|
|
1453 | grace => [ |
|
|
1454 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1455 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ |
|
|
1456 | ], |
|
|
1457 | hp => [ |
|
|
1458 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1459 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1460 | ], |
|
|
1461 | mana => [ |
|
|
1462 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1463 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ |
|
|
1464 | ], |
|
|
1465 | ); |
|
|
1466 | |
|
|
1467 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1468 | sub new { |
|
|
1469 | my $class = shift; |
|
|
1470 | |
|
|
1471 | my $self = $class->SUPER::new (gauge => 'food', @_); |
|
|
1472 | |
|
|
1473 | $self |
|
|
1474 | } |
|
|
1475 | |
|
|
1476 | sub size_request { |
|
|
1477 | my ($self) = @_; |
|
|
1478 | |
|
|
1479 | (30, 100) |
|
|
1480 | } |
|
|
1481 | |
|
|
1482 | sub set_max { |
|
|
1483 | my ($self, $max) = @_; |
|
|
1484 | $self->{max_val} = $max; |
|
|
1485 | } |
|
|
1486 | |
|
|
1487 | sub set_value { |
|
|
1488 | my ($self, $val, $max) = @_; |
|
|
1489 | |
|
|
1490 | $self->set_max ($max) |
|
|
1491 | if defined $max; |
|
|
1492 | |
|
|
1493 | $max = $self->{max_val}; |
|
|
1494 | $self->{val} = $val; |
|
|
1495 | |
|
|
1496 | $self->update; |
|
|
1497 | } |
|
|
1498 | |
|
|
1499 | sub _draw { |
|
|
1500 | my ($self) = @_; |
|
|
1501 | |
|
|
1502 | my $tex = $tex{$self->{gauge}}; |
|
|
1503 | |
|
|
1504 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1505 | |
|
|
1506 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1507 | $ycut = 1 if $self->{val} > $self->{max_val}; |
|
|
1508 | |
|
|
1509 | my $t1 = $tex->[0]; |
|
|
1510 | my $t2 = $tex->[1]; |
|
|
1511 | |
|
|
1512 | glEnable GL_BLEND; |
|
|
1513 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1514 | glEnable GL_TEXTURE_2D; |
|
|
1515 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1516 | |
|
|
1517 | my $h1 = $self->{h} - $ycut * $self->{h}; |
|
|
1518 | my $h2 = $ycut * $self->{h}; |
|
|
1519 | |
|
|
1520 | my $yp = 0; |
|
|
1521 | |
|
|
1522 | glBindTexture (GL_TEXTURE_2D, $t1->{name}); |
|
|
1523 | glBegin (GL_QUADS); |
|
|
1524 | glTexCoord (0, 0); glVertex (0 , $yp); |
|
|
1525 | glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); |
|
|
1526 | glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); |
|
|
1527 | glTexCoord (1, 0); glVertex (0 + $w, $yp); |
|
|
1528 | glEnd (); |
|
|
1529 | |
|
|
1530 | $yp += $h1; |
|
|
1531 | |
|
|
1532 | glBindTexture (GL_TEXTURE_2D, $t2->{name}); |
|
|
1533 | glBegin (GL_QUADS); |
|
|
1534 | glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); |
|
|
1535 | glTexCoord (0, 1); glVertex (0 , $yp + $h2); |
|
|
1536 | glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); |
|
|
1537 | glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); |
|
|
1538 | glEnd (); |
|
|
1539 | |
|
|
1540 | glDisable GL_BLEND; |
|
|
1541 | glDisable GL_TEXTURE_2D; |
1386 | } |
1542 | } |
1387 | |
1543 | |
1388 | ############################################################################# |
1544 | ############################################################################# |
1389 | |
1545 | |
1390 | package CFClient::UI::Slider; |
1546 | package CFClient::UI::Slider; |
… | |
… | |
1586 | } |
1742 | } |
1587 | |
1743 | |
1588 | sub size_allocate { |
1744 | sub size_allocate { |
1589 | my ($self, $x, $y, $w, $h) = @_; |
1745 | my ($self, $x, $y, $w, $h) = @_; |
1590 | |
1746 | |
1591 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1747 | $self->SUPER::size_allocate ($x, $y, $w, $h) or return; |
1592 | |
1748 | |
1593 | $self->{layout}->set_height ($self->{fontsize}); |
1749 | $self->{layout}->set_height ($self->{fontsize}); |
1594 | $self->{layout}->set_width ($self->{w}); |
1750 | $self->{layout}->set_width ($self->{w}); |
1595 | |
1751 | |
1596 | $self->reflow; |
1752 | $self->reflow; |
|
|
1753 | $self->update; |
1597 | } |
1754 | } |
1598 | |
1755 | |
1599 | sub add_paragraph { |
1756 | sub add_paragraph { |
1600 | my ($self, $color, $text) = @_; |
1757 | my ($self, $color, $text) = @_; |
1601 | |
1758 | |
… | |
… | |
2009 | |
2166 | |
2010 | our @ISA = CFClient::UI::Container::; |
2167 | our @ISA = CFClient::UI::Container::; |
2011 | |
2168 | |
2012 | use SDL::OpenGL; |
2169 | use SDL::OpenGL; |
2013 | |
2170 | |
|
|
2171 | sub check_size { |
|
|
2172 | my ($self) = @_; |
|
|
2173 | |
|
|
2174 | $self->size_allocate (0, 0, $::WITH, $::HEIGHT); |
|
|
2175 | } |
|
|
2176 | |
2014 | sub size_request { |
2177 | sub size_request { |
2015 | ($::WIDTH, $::HEIGHT) |
2178 | ($::WIDTH, $::HEIGHT) |
2016 | } |
2179 | } |
2017 | |
2180 | |
2018 | sub size_allocate { |
2181 | sub size_allocate { |
… | |
… | |
2031 | } |
2194 | } |
2032 | |
2195 | |
2033 | sub update { |
2196 | sub update { |
2034 | my ($self) = @_; |
2197 | my ($self) = @_; |
2035 | |
2198 | |
2036 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2199 | $self->check_size; |
2037 | ::refresh (); |
2200 | ::refresh (); |
2038 | } |
2201 | } |
2039 | |
2202 | |
2040 | sub add { |
2203 | sub add { |
2041 | my ($self, $widget) = @_; |
2204 | my ($self, $widget) = @_; |