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.139 by root, Wed Apr 19 23:55:58 2006 UTC vs.
Revision 1.145 by elmex, Fri Apr 21 12:27:20 2006 UTC

146} 146}
147 147
148sub configure { 148sub configure {
149 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
150 150
151 if ($self->{aspect}) {
152 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154
155 # use alignment to adjust x, y
156
157 $x += ($w - $w2) * 0.5;
158 $y += ($h - $h2) * 0.5;
159
160 ($w, $h) = ($w2, $h2);
161 }
162
163 if ($self->{x} != $x || $self->{y} != $y) {
151 $self->{x} = $x; 164 $self->{x} = $x;
152 $self->{y} = $y; 165 $self->{y} = $y;
166 $self->update;
167 }
153 168
154 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
155
156 $self->{w} = $w; 170 $self->{w} = $w;
157 $self->{h} = $h; 171 $self->{h} = $h;
158 172
159 $self->size_allocate ($w, $h); 173 $self->size_allocate ($w, $h);
160 $self->update; 174 $self->update;
175 }
161} 176}
162 177
163sub size_allocate { 178sub size_allocate {
164 # nothing to be done 179 # nothing to be done
165} 180}
610our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
611 626
612use CFClient::OpenGL; 627use CFClient::OpenGL;
613 628
614my @tex = 629my @tex =
615 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
616 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 631 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
617 632
618sub new { 633sub new {
619 my $class = shift; 634 my $class = shift;
620 635
621 # TODO: user_x, user_y, overwrite moveto? 636 # TODO: user_x, user_y, overwrite moveto?
622 637
623 $class->SUPER::new ( 638 my $self = $class->SUPER::new (
624 bg => [1, 1, 1, 1], 639 bg => [1, 1, 1, 1],
625 border_bg => [1, 1, 1, 1], 640 border_bg => [1, 1, 1, 1],
626 border => 0.8, 641 border => 0.8,
627 @_ 642 @_
628 ) 643 );
644
645 $self->{title} &&= new CFClient::UI::Label
646 align => 0,
647 valign => 1,
648 text => $self->{title},
649 fontsize => 1;
650
651 $self
629} 652}
630 653
631sub border { 654sub border {
632 int $_[0]{border} * $::FONTSIZE 655 int $_[0]{border} * $::FONTSIZE
633} 656}
648sub size_allocate { 671sub size_allocate {
649 my ($self, $w, $h) = @_; 672 my ($self, $w, $h) = @_;
650 673
651 $h -= List::Util::max 0, $self->border * 2; 674 $h -= List::Util::max 0, $self->border * 2;
652 $w -= List::Util::max 0, $self->border * 2; 675 $w -= List::Util::max 0, $self->border * 2;
676
677 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
678 if $self->{title};
653 679
654 $self->child->configure ($self->border, $self->border, $w, $h); 680 $self->child->configure ($self->border, $self->border, $w, $h);
655} 681}
656 682
657sub button_down { 683sub button_down {
737 $bg->draw_quad ($border, $border, $cw, $ch); 763 $bg->draw_quad ($border, $border, $cw, $ch);
738 764
739 glDisable GL_TEXTURE_2D; 765 glDisable GL_TEXTURE_2D;
740 glDisable GL_BLEND; 766 glDisable GL_BLEND;
741 767
768 $self->{title}->draw if $self->{title};
742 $self->child->draw; 769 $self->child->draw;
743} 770}
744 771
745############################################################################# 772#############################################################################
746 773
1015 padding => 2, 1042 padding => 2,
1016 layout => new CFClient::Layout, 1043 layout => new CFClient::Layout,
1017 %arg 1044 %arg
1018 ); 1045 );
1019 1046
1020 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1047 if (exists $self->{template}) {
1048 my $layout = new CFClient::Layout;
1049 $layout->set_text (delete $self->{template});
1050 $self->{template} = $layout;
1051 }
1021 1052
1022 $self->set_text (delete $self->{text}) if exists $self->{text}; 1053 $self->set_text (delete $self->{text}) if exists $self->{text};
1023 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1054 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1024 1055
1025 $self 1056 $self
1058 1089
1059 $self->{layout}->set_width; 1090 $self->{layout}->set_width;
1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061 1092
1062 my ($w, $h) = $self->{layout}->size; 1093 my ($w, $h) = $self->{layout}->size;
1094
1095 if (exists $self->{template}) {
1096 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1097
1098 my ($w2, $h2) = $self->{template}->size;
1099
1100 $w = List::Util::max $w, $w2;
1101 $h = List::Util::max $h, $h2;
1102 }
1063 1103
1064 ( 1104 (
1065 $w + $self->{padding} * 2, 1105 $w + $self->{padding} * 2,
1066 $h + $self->{padding} * 2, 1106 $h + $self->{padding} * 2,
1067 ) 1107 )
1304our @ISA = CFClient::UI::Label::; 1344our @ISA = CFClient::UI::Label::;
1305 1345
1306use CFClient::OpenGL; 1346use CFClient::OpenGL;
1307 1347
1308my @tex = 1348my @tex =
1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1310 qw(b1_button_active.png); 1350 qw(b1_button_active.png);
1311 1351
1312sub new { 1352sub new {
1313 my $class = shift; 1353 my $class = shift;
1314 1354
1361package CFClient::UI::CheckBox; 1401package CFClient::UI::CheckBox;
1362 1402
1363our @ISA = CFClient::UI::DrawBG::; 1403our @ISA = CFClient::UI::DrawBG::;
1364 1404
1365my @tex = 1405my @tex =
1366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1367 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1407 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1368 1408
1369use CFClient::OpenGL; 1409use CFClient::OpenGL;
1370 1410
1371sub new { 1411sub new {
1420 glDisable GL_BLEND; 1460 glDisable GL_BLEND;
1421} 1461}
1422 1462
1423############################################################################# 1463#############################################################################
1424 1464
1425package CFClient::UI::VGauge; 1465package CFClient::UI::Image;
1426 1466
1427our @ISA = CFClient::UI::Base::; 1467our @ISA = CFClient::UI::Base::;
1428 1468
1429use CFClient::OpenGL; 1469use CFClient::OpenGL;
1470use Carp qw/confess/;
1430 1471
1431my %tex = ( 1472our %loaded_images;
1432 food => [
1433 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1434 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1435 ],
1436 grace => [
1437 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1438 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1439 ],
1440 hp => [
1441 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1442 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1443 ],
1444 mana => [
1445 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1446 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1447 ],
1448);
1449 1473
1450# eg. VGauge->new (gauge => 'food'), default gauge: food
1451sub new { 1474sub new {
1452 my $class = shift; 1475 my $class = shift;
1453 1476
1454 my $self = $class->SUPER::new (gauge => 'food', @_); 1477 my $self = $class->SUPER::new (@_);
1478
1479 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1480
1481 $loaded_images{$self->{image}} ||=
1482 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1483
1484 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1485
1486 $self->{aspect} = $tex->{w} / $tex->{h};
1455 1487
1456 $self 1488 $self
1457} 1489}
1458 1490
1459sub size_request { 1491sub size_request {
1460 my ($self) = @_; 1492 my ($self) = @_;
1461 1493
1462 my $tex = $tex{$self->{gauge}}[0]; 1494 ($self->{tex}->{w}, $self->{tex}->{h})
1463
1464 @$tex{qw(w h)}
1465}
1466
1467sub set_max {
1468 my ($self, $max) = @_;
1469
1470 $self->{max_val} = $max;
1471}
1472
1473sub set_value {
1474 my ($self, $val, $max) = @_;
1475
1476 $self->set_max ($max)
1477 if defined $max;
1478
1479 $max = $self->{max_val};
1480 $self->{val} = $val;
1481
1482 $self->update;
1483} 1495}
1484 1496
1485sub _draw { 1497sub _draw {
1486 my ($self) = @_; 1498 my ($self) = @_;
1487 1499
1488 my $tex = $tex{$self->{gauge}}; 1500 my $tex = $self->{tex};
1489 1501
1490 my ($w, $h) = ($self->{w}, $self->{h}); 1502 my ($w, $h) = ($self->{w}, $self->{h});
1491 1503
1492 my $ycut = $self->{val} / ($self->{max_val} || 1); 1504 if ($self->{rot90}) {
1493 $ycut = 1 if $self->{val} > $self->{max_val}; 1505 glRotate 90, 0, 0, 1;
1506 glTranslate 0, -$self->{w}, 0;
1494 1507
1495 my $t1 = $tex->[0]; 1508 ($w, $h) = ($h, $w);
1496 my $t2 = $tex->[1]; 1509 }
1497 1510
1498 glEnable GL_BLEND; 1511 glEnable GL_BLEND;
1499 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1512 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1500 glEnable GL_TEXTURE_2D; 1513 glEnable GL_TEXTURE_2D;
1501 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1514 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1502 1515
1516 $tex->draw_quad (0, 0, $w, $h);
1517
1518 glDisable GL_BLEND;
1519 glDisable GL_TEXTURE_2D;
1520}
1521
1522#############################################################################
1523
1524package CFClient::UI::VGauge;
1525
1526our @ISA = CFClient::UI::Base::;
1527
1528use CFClient::OpenGL;
1529
1530my %tex = (
1531 food => [
1532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1533 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1534 ],
1535 grace => [
1536 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1537 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1538 ],
1539 hp => [
1540 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1541 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1542 ],
1543 mana => [
1544 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1545 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1546 ],
1547);
1548
1549# eg. VGauge->new (gauge => 'food'), default gauge: food
1550sub new {
1551 my $class = shift;
1552
1553 my $self = $class->SUPER::new (
1554 type => 'food',
1555 @_
1556 );
1557
1558 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1559
1560 $self
1561}
1562
1563sub size_request {
1564 my ($self) = @_;
1565
1566 #my $tex = $tex{$self->{type}}[0];
1567 #@$tex{qw(w h)}
1568 (0, 0)
1569}
1570
1571sub set_max {
1572 my ($self, $max) = @_;
1573
1574 $self->{max_val} = $max;
1575}
1576
1577sub set_value {
1578 my ($self, $val, $max) = @_;
1579
1580 $self->set_max ($max)
1581 if defined $max;
1582
1583 $max = $self->{max_val};
1584 $self->{val} = $val;
1585
1586 $self->update;
1587}
1588
1589sub _draw {
1590 my ($self) = @_;
1591
1592 my $tex = $tex{$self->{type}};
1593
1594 my ($w, $h) = ($self->{w}, $self->{h});
1595
1596 if ($self->{vertical}) {
1597 glRotate 90, 0, 0, 1;
1598 glTranslate 0, -$self->{w}, 0;
1599
1600 ($w, $h) = ($h, $w);
1601 }
1602
1603 my $ycut = $self->{val} / ($self->{max_val} || 1);
1604 $ycut = 1 if $self->{val} > $self->{max_val};
1605
1606 my $t1 = $tex->[0];
1607 my $t2 = $tex->[1];
1608
1609 glEnable GL_BLEND;
1610 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1611 glEnable GL_TEXTURE_2D;
1612 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1613
1503 my $h1 = $self->{h} - $ycut * $self->{h}; 1614 my $h1 = $self->{h} - $ycut * $self->{h};
1504 my $h2 = $ycut * $self->{h}; 1615 my $h2 = $ycut * $self->{h};
1505 1616
1506 my $yp = 0;
1507
1508 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1617 glBindTexture GL_TEXTURE_2D, $t1->{name};
1509 glBegin GL_QUADS; 1618 glBegin GL_QUADS;
1510 glTexCoord 0 , 0; glVertex 0 , $yp; 1619 glTexCoord 0 , 0; glVertex 0 , 0;
1511 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1620 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1512 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1621 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1513 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1622 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1514 glEnd; 1623 glEnd;
1515
1516 $yp += $h1;
1517 1624
1518 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1625 glBindTexture GL_TEXTURE_2D, $t2->{name};
1519 glBegin GL_QUADS; 1626 glBegin GL_QUADS;
1520 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1627 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1521 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1628 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1522 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; 1629 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1523 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1630 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1524 glEnd; 1631 glEnd;
1525 1632
1526 glDisable GL_BLEND; 1633 glDisable GL_BLEND;
1527 glDisable GL_TEXTURE_2D; 1634 glDisable GL_TEXTURE_2D;
1635}
1636
1637#############################################################################
1638
1639package CFClient::UI::Gauge;
1640
1641our @ISA = CFClient::UI::VBox::;
1642
1643sub new {
1644 my ($class, %arg) = shift;
1645
1646 my $self = $class->SUPER::new (
1647 @_,
1648 );
1649
1650 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1651 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1652 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1653
1654 $self
1655}
1656
1657sub set_value {
1658 my ($self, $val, $max) = @_;
1659
1660 $self->set_max ($max)
1661 if defined $max;
1662
1663 $self->{gauge}->set_value ($val, $max);
1664 $self->{value}->set_text ($val);
1665}
1666
1667sub set_max {
1668 my ($self, $max) = @_;
1669
1670 $self->{gauge}->set_max ($max);
1671 $self->{max}->set_text ($max);
1528} 1672}
1529 1673
1530############################################################################# 1674#############################################################################
1531 1675
1532package CFClient::UI::Slider; 1676package CFClient::UI::Slider;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines