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.140 by root, Thu Apr 20 04:20:52 2006 UTC vs.
Revision 1.146 by elmex, Fri Apr 21 15:03:46 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 $self->{x} = $x;
152 $self->{y} = $y;
153
154 if ($self->{aspect}) { 151 if ($self->{aspect}) {
155 $w = List::Util::min $w, int $h * $self->{aspect}; 152 my $w2 = List::Util::min $w, int $h * $self->{aspect};
156 $h = List::Util::min $h, int $w / $self->{aspect}; 153 my $h2 = List::Util::min $h, int $w / $self->{aspect};
157 }
158 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) {
164 $self->{x} = $x;
165 $self->{y} = $y;
166 $self->update;
167 }
168
159 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
160
161 $self->{w} = $w; 170 $self->{w} = $w;
162 $self->{h} = $h; 171 $self->{h} = $h;
163 172
164 $self->size_allocate ($w, $h); 173 $self->size_allocate ($w, $h);
165 $self->update; 174 $self->update;
175 }
166} 176}
167 177
168sub size_allocate { 178sub size_allocate {
169 # nothing to be done 179 # nothing to be done
170} 180}
615our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
616 626
617use CFClient::OpenGL; 627use CFClient::OpenGL;
618 628
619my @tex = 629my @tex =
620 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
621 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);
622 632
623sub new { 633sub new {
624 my $class = shift; 634 my $class = shift;
625 635
626 # TODO: user_x, user_y, overwrite moveto? 636 # TODO: user_x, user_y, overwrite moveto?
627 637
628 $class->SUPER::new ( 638 my $self = $class->SUPER::new (
629 bg => [1, 1, 1, 1], 639 bg => [1, 1, 1, 1],
630 border_bg => [1, 1, 1, 1], 640 border_bg => [1, 1, 1, 1],
631 border => 0.8, 641 border => 0.8,
632 @_ 642 @_
633 ) 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
634} 652}
635 653
636sub border { 654sub border {
637 int $_[0]{border} * $::FONTSIZE 655 int $_[0]{border} * $::FONTSIZE
638} 656}
653sub size_allocate { 671sub size_allocate {
654 my ($self, $w, $h) = @_; 672 my ($self, $w, $h) = @_;
655 673
656 $h -= List::Util::max 0, $self->border * 2; 674 $h -= List::Util::max 0, $self->border * 2;
657 $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};
658 679
659 $self->child->configure ($self->border, $self->border, $w, $h); 680 $self->child->configure ($self->border, $self->border, $w, $h);
660} 681}
661 682
662sub button_down { 683sub button_down {
742 $bg->draw_quad ($border, $border, $cw, $ch); 763 $bg->draw_quad ($border, $border, $cw, $ch);
743 764
744 glDisable GL_TEXTURE_2D; 765 glDisable GL_TEXTURE_2D;
745 glDisable GL_BLEND; 766 glDisable GL_BLEND;
746 767
768 $self->{title}->draw if $self->{title};
747 $self->child->draw; 769 $self->child->draw;
748} 770}
749 771
750############################################################################# 772#############################################################################
751 773
1020 padding => 2, 1042 padding => 2,
1021 layout => new CFClient::Layout, 1043 layout => new CFClient::Layout,
1022 %arg 1044 %arg
1023 ); 1045 );
1024 1046
1025 $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 }
1026 1052
1027 $self->set_text (delete $self->{text}) if exists $self->{text}; 1053 $self->set_text (delete $self->{text}) if exists $self->{text};
1028 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1054 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1029 1055
1030 $self 1056 $self
1063 1089
1064 $self->{layout}->set_width; 1090 $self->{layout}->set_width;
1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1066 1092
1067 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 }
1068 1103
1069 ( 1104 (
1070 $w + $self->{padding} * 2, 1105 $w + $self->{padding} * 2,
1071 $h + $self->{padding} * 2, 1106 $h + $self->{padding} * 2,
1072 ) 1107 )
1074 1109
1075sub size_allocate { 1110sub size_allocate {
1076 my ($self, $w, $h) = @_; 1111 my ($self, $w, $h) = @_;
1077 1112
1078 delete $self->{texture}; 1113 delete $self->{texture};
1114}
1115
1116sub set_fontsize {
1117 my ($self, $fontsize) = @_;
1118
1119 $self->{fontsize} = $fontsize;
1120 $self->update;
1079} 1121}
1080 1122
1081sub _draw { 1123sub _draw {
1082 my ($self) = @_; 1124 my ($self) = @_;
1083 1125
1309our @ISA = CFClient::UI::Label::; 1351our @ISA = CFClient::UI::Label::;
1310 1352
1311use CFClient::OpenGL; 1353use CFClient::OpenGL;
1312 1354
1313my @tex = 1355my @tex =
1314 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1315 qw(b1_button_active.png); 1357 qw(b1_button_active.png);
1316 1358
1317sub new { 1359sub new {
1318 my $class = shift; 1360 my $class = shift;
1319 1361
1366package CFClient::UI::CheckBox; 1408package CFClient::UI::CheckBox;
1367 1409
1368our @ISA = CFClient::UI::DrawBG::; 1410our @ISA = CFClient::UI::DrawBG::;
1369 1411
1370my @tex = 1412my @tex =
1371 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1413 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1372 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1414 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1373 1415
1374use CFClient::OpenGL; 1416use CFClient::OpenGL;
1375 1417
1376sub new { 1418sub new {
1425 glDisable GL_BLEND; 1467 glDisable GL_BLEND;
1426} 1468}
1427 1469
1428############################################################################# 1470#############################################################################
1429 1471
1430package CFClient::UI::VGauge; 1472package CFClient::UI::Image;
1431 1473
1432our @ISA = CFClient::UI::Base::; 1474our @ISA = CFClient::UI::Base::;
1433 1475
1434use CFClient::OpenGL; 1476use CFClient::OpenGL;
1477use Carp qw/confess/;
1435 1478
1436my %tex = ( 1479our %loaded_images;
1437 food => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1440 ],
1441 grace => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1444 ],
1445 hp => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1448 ],
1449 mana => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1452 ],
1453);
1454 1480
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new { 1481sub new {
1457 my $class = shift; 1482 my $class = shift;
1458 1483
1459 my $self = $class->SUPER::new ( 1484 my $self = $class->SUPER::new (@_);
1460 gauge => 'food',
1461 @_
1462 );
1463 1485
1464 $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h}; 1486 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1487
1488 $loaded_images{$self->{image}} ||=
1489 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1490
1491 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1492
1493 $self->{aspect} = $tex->{w} / $tex->{h};
1465 1494
1466 $self 1495 $self
1467} 1496}
1468 1497
1469sub size_request { 1498sub size_request {
1470 my ($self) = @_; 1499 my ($self) = @_;
1471 1500
1472 my $tex = $tex{$self->{gauge}}[0]; 1501 ($self->{tex}->{w}, $self->{tex}->{h})
1473
1474 @$tex{qw(w h)}
1475}
1476
1477sub set_max {
1478 my ($self, $max) = @_;
1479
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493} 1502}
1494 1503
1495sub _draw { 1504sub _draw {
1496 my ($self) = @_; 1505 my ($self) = @_;
1497 1506
1498 my $tex = $tex{$self->{gauge}}; 1507 my $tex = $self->{tex};
1499 1508
1500 my ($w, $h) = ($self->{w}, $self->{h}); 1509 my ($w, $h) = ($self->{w}, $self->{h});
1501 1510
1502 my $ycut = $self->{val} / ($self->{max_val} || 1); 1511 if ($self->{rot90}) {
1503 $ycut = 1 if $self->{val} > $self->{max_val}; 1512 glRotate 90, 0, 0, 1;
1513 glTranslate 0, -$self->{w}, 0;
1504 1514
1505 my $t1 = $tex->[0]; 1515 ($w, $h) = ($h, $w);
1506 my $t2 = $tex->[1]; 1516 }
1507 1517
1508 glEnable GL_BLEND; 1518 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1519 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D; 1520 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1521 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 1522
1523 $tex->draw_quad (0, 0, $w, $h);
1524
1525 glDisable GL_BLEND;
1526 glDisable GL_TEXTURE_2D;
1527}
1528
1529#############################################################################
1530
1531package CFClient::UI::VGauge;
1532
1533our @ISA = CFClient::UI::Base::;
1534
1535use CFClient::OpenGL;
1536
1537my %tex = (
1538 food => [
1539 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1540 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1541 ],
1542 grace => [
1543 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1544 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1545 ],
1546 hp => [
1547 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1548 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1549 ],
1550 mana => [
1551 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1552 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1553 ],
1554);
1555
1556# eg. VGauge->new (gauge => 'food'), default gauge: food
1557sub new {
1558 my $class = shift;
1559
1560 my $self = $class->SUPER::new (
1561 type => 'food',
1562 @_
1563 );
1564
1565 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1566
1567 $self
1568}
1569
1570sub size_request {
1571 my ($self) = @_;
1572
1573 #my $tex = $tex{$self->{type}}[0];
1574 #@$tex{qw(w h)}
1575 (0, 0)
1576}
1577
1578sub set_max {
1579 my ($self, $max) = @_;
1580
1581 $self->{max_val} = $max;
1582}
1583
1584sub set_value {
1585 my ($self, $val, $max) = @_;
1586
1587 $self->set_max ($max)
1588 if defined $max;
1589
1590 $max = $self->{max_val};
1591 $self->{val} = $val;
1592
1593 $self->update;
1594}
1595
1596sub _draw {
1597 my ($self) = @_;
1598
1599 my $tex = $tex{$self->{type}};
1600
1601 my ($w, $h) = ($self->{w}, $self->{h});
1602
1603 if ($self->{vertical}) {
1604 glRotate 90, 0, 0, 1;
1605 glTranslate 0, -$self->{w}, 0;
1606
1607 ($w, $h) = ($h, $w);
1608 }
1609
1610 my $ycut = $self->{val} / ($self->{max_val} || 1);
1611 $ycut = 1 if $self->{val} > $self->{max_val};
1612
1613 my $t1 = $tex->[0];
1614 my $t2 = $tex->[1];
1615
1616 glEnable GL_BLEND;
1617 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1618 glEnable GL_TEXTURE_2D;
1619 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1620
1513 my $h1 = $self->{h} - $ycut * $self->{h}; 1621 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h}; 1622 my $h2 = $ycut * $self->{h};
1515 1623
1516 my $yp = 0;
1517
1518 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1624 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin GL_QUADS; 1625 glBegin GL_QUADS;
1520 glTexCoord 0 , 0; glVertex 0 , $yp; 1626 glTexCoord 0 , 0; glVertex 0 , 0;
1521 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1627 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1522 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1628 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1523 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1629 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1524 glEnd; 1630 glEnd;
1525
1526 $yp += $h1;
1527 1631
1528 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1632 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin GL_QUADS; 1633 glBegin GL_QUADS;
1530 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1634 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1531 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1635 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1532 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; 1636 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1533 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1637 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1534 glEnd; 1638 glEnd;
1535 1639
1536 glDisable GL_BLEND; 1640 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D; 1641 glDisable GL_TEXTURE_2D;
1642}
1643
1644#############################################################################
1645
1646package CFClient::UI::Gauge;
1647
1648our @ISA = CFClient::UI::VBox::;
1649
1650sub new {
1651 my ($class, %arg) = shift;
1652
1653 my $self = $class->SUPER::new (
1654 @_,
1655 );
1656
1657 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1658 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1659 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1660
1661 $self
1662}
1663
1664sub set_fontsize {
1665 my ($self, $fsize) = @_;
1666
1667 $self->{value}->set_fontsize ($fsize);
1668 $self->{max} ->set_fontsize ($fsize);
1669 $self->update;
1670}
1671
1672sub set_value {
1673 my ($self, $val, $max) = @_;
1674
1675 $self->set_max ($max)
1676 if defined $max;
1677
1678 $self->{gauge}->set_value ($val, $max);
1679 $self->{value}->set_text ($val);
1680}
1681
1682sub set_max {
1683 my ($self, $max) = @_;
1684
1685 $self->{gauge}->set_max ($max);
1686 $self->{max}->set_text ($max);
1538} 1687}
1539 1688
1540############################################################################# 1689#############################################################################
1541 1690
1542package CFClient::UI::Slider; 1691package CFClient::UI::Slider;
1952} 2101}
1953 2102
1954sub add { 2103sub add {
1955 my ($self, $child) = @_; 2104 my ($self, $child) = @_;
1956 2105
2106 # integerize window positions
2107 $child->{x} = int $child->{x};
2108 $child->{y} = int $child->{y};
2109
1957 $self->SUPER::add ($child); 2110 $self->SUPER::add ($child);
1958} 2111}
1959 2112
1960sub on_refresh { 2113sub on_refresh {
1961 my ($self, $id, $cb) = @_; 2114 my ($self, $id, $cb) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines