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.148 by elmex, Sat Apr 22 12:14:45 2006 UTC

140 0 140 0
141} 141}
142 142
143sub size_request { 143sub size_request {
144 require Carp; 144 require Carp;
145 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abstract";
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 += int +($w - $w2) * 0.5;
158 $y += int +($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}
572 582
573our @ISA = CFClient::UI::Bin::; 583our @ISA = CFClient::UI::Bin::;
574 584
575use CFClient::OpenGL; 585use CFClient::OpenGL;
576 586
577sub size_request {
578 my ($self) = @_;
579 my $chld = $self->child
580 or return (0, 0);
581
582 $chld->move (2, 2);
583
584 map { $_ + 4 } $chld->size_request;
585}
586
587sub size_allocate {
588 my ($self, $x, $y, $w, $h) = @_;
589
590 $self->child->configure (2, 2, $w - 4, $h - 4);
591}
592
593sub _draw {
594 my ($self) = @_;
595
596 my $chld = $self->child;
597
598 my ($w, $h) = $chld->size_request;
599
600 glBegin GL_QUADS;
601 glColor 0, 0, 0;
602 glVertex 0 , 0;
603 glVertex 0 , $h + 4;
604 glVertex $w + 4 , $h + 4;
605 glVertex $w + 4 , 0;
606 glEnd;
607
608 $chld->draw;
609}
610
611#############################################################################
612
613package CFClient::UI::FancyFrame;
614
615our @ISA = CFClient::UI::Bin::;
616
617use CFClient::OpenGL;
618
619my @tex =
620 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
621 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
622
623sub new { 587sub new {
624 my $class = shift; 588 my $class = shift;
625 589
626 # TODO: user_x, user_y, overwrite moveto?
627
628 $class->SUPER::new ( 590 my $self = $class->SUPER::new (
629 bg => [1, 1, 1, 1], 591 bg => [1, 1, 1, 1],
630 border_bg => [1, 1, 1, 1], 592 border_bg => [1, 1, 1, 1],
631 border => 0.8, 593 border => 0.8,
632 @_ 594 @_
633 ) 595 );
596
597 $self
598}
599
600sub set_size {
601 my ($self, $w, $h) = @_;
602 $self->{req_w} = $w;
603 $self->{req_h} = $h;
604 $self->check_size;
605}
606
607sub size_request {
608 my ($self) = @_;
609 ($self->{req_w}, $self->{req_h})
610}
611
612sub size_allocate {
613 my ($self, $w, $h) = @_;
614 $self->{w} = $w;
615 $self->{h} = $h;
616 $self->child->configure (0, 0, $w, $h);
617}
618
619sub _draw {
620 my ($self) = @_;
621
622 my ($w, $h) = ($self->{w}, $self->{h});
623
624 glEnable GL_BLEND;
625 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
626 glEnable GL_TEXTURE_2D;
627 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
628
629# glBegin GL_QUADS;
630# glColor 0, 0, 0, 0;
631# glVertex 0 , 0;
632# glVertex 0 , $h;
633# glVertex $w, $h;
634# glVertex $w, 0;
635# glEnd;
636
637
638 $self->child->draw;
639 glDisable GL_BLEND;
640 glDisable GL_TEXTURE_2D;
641}
642
643#############################################################################
644
645package CFClient::UI::FancyFrame;
646
647our @ISA = CFClient::UI::Bin::;
648
649use CFClient::OpenGL;
650
651my @tex =
652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
653 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
654
655sub new {
656 my $class = shift;
657
658 # TODO: user_x, user_y, overwrite moveto?
659
660 my $self = $class->SUPER::new (
661 bg => [1, 1, 1, 1],
662 border_bg => [1, 1, 1, 1],
663 border => 0.8,
664 @_
665 );
666
667 $self->{title} &&= new CFClient::UI::Label
668 align => 0,
669 valign => 1,
670 text => $self->{title},
671 fontsize => 1;
672
673 $self
634} 674}
635 675
636sub border { 676sub border {
637 int $_[0]{border} * $::FONTSIZE 677 int $_[0]{border} * $::FONTSIZE
638} 678}
653sub size_allocate { 693sub size_allocate {
654 my ($self, $w, $h) = @_; 694 my ($self, $w, $h) = @_;
655 695
656 $h -= List::Util::max 0, $self->border * 2; 696 $h -= List::Util::max 0, $self->border * 2;
657 $w -= List::Util::max 0, $self->border * 2; 697 $w -= List::Util::max 0, $self->border * 2;
698
699 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
700 if $self->{title};
658 701
659 $self->child->configure ($self->border, $self->border, $w, $h); 702 $self->child->configure ($self->border, $self->border, $w, $h);
660} 703}
661 704
662sub button_down { 705sub button_down {
742 $bg->draw_quad ($border, $border, $cw, $ch); 785 $bg->draw_quad ($border, $border, $cw, $ch);
743 786
744 glDisable GL_TEXTURE_2D; 787 glDisable GL_TEXTURE_2D;
745 glDisable GL_BLEND; 788 glDisable GL_BLEND;
746 789
790 $self->{title}->draw if $self->{title};
747 $self->child->draw; 791 $self->child->draw;
748} 792}
749 793
750############################################################################# 794#############################################################################
751 795
1020 padding => 2, 1064 padding => 2,
1021 layout => new CFClient::Layout, 1065 layout => new CFClient::Layout,
1022 %arg 1066 %arg
1023 ); 1067 );
1024 1068
1025 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1069 if (exists $self->{template}) {
1070 my $layout = new CFClient::Layout;
1071 $layout->set_text (delete $self->{template});
1072 $self->{template} = $layout;
1073 }
1026 1074
1027 $self->set_text (delete $self->{text}) if exists $self->{text}; 1075 $self->set_text (delete $self->{text}) if exists $self->{text};
1028 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1076 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1029 1077
1030 $self 1078 $self
1063 1111
1064 $self->{layout}->set_width; 1112 $self->{layout}->set_width;
1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1113 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1066 1114
1067 my ($w, $h) = $self->{layout}->size; 1115 my ($w, $h) = $self->{layout}->size;
1116
1117 if (exists $self->{template}) {
1118 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1119
1120 my ($w2, $h2) = $self->{template}->size;
1121
1122 $w = List::Util::max $w, $w2;
1123 $h = List::Util::max $h, $h2;
1124 }
1068 1125
1069 ( 1126 (
1070 $w + $self->{padding} * 2, 1127 $w + $self->{padding} * 2,
1071 $h + $self->{padding} * 2, 1128 $h + $self->{padding} * 2,
1072 ) 1129 )
1074 1131
1075sub size_allocate { 1132sub size_allocate {
1076 my ($self, $w, $h) = @_; 1133 my ($self, $w, $h) = @_;
1077 1134
1078 delete $self->{texture}; 1135 delete $self->{texture};
1136}
1137
1138sub set_fontsize {
1139 my ($self, $fontsize) = @_;
1140
1141 $self->{fontsize} = $fontsize;
1142 $self->check_size;
1079} 1143}
1080 1144
1081sub _draw { 1145sub _draw {
1082 my ($self) = @_; 1146 my ($self) = @_;
1083 1147
1309our @ISA = CFClient::UI::Label::; 1373our @ISA = CFClient::UI::Label::;
1310 1374
1311use CFClient::OpenGL; 1375use CFClient::OpenGL;
1312 1376
1313my @tex = 1377my @tex =
1314 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1378 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1315 qw(b1_button_active.png); 1379 qw(b1_button_active.png);
1316 1380
1317sub new { 1381sub new {
1318 my $class = shift; 1382 my $class = shift;
1319 1383
1366package CFClient::UI::CheckBox; 1430package CFClient::UI::CheckBox;
1367 1431
1368our @ISA = CFClient::UI::DrawBG::; 1432our @ISA = CFClient::UI::DrawBG::;
1369 1433
1370my @tex = 1434my @tex =
1371 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1435 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1372 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1436 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1373 1437
1374use CFClient::OpenGL; 1438use CFClient::OpenGL;
1375 1439
1376sub new { 1440sub new {
1425 glDisable GL_BLEND; 1489 glDisable GL_BLEND;
1426} 1490}
1427 1491
1428############################################################################# 1492#############################################################################
1429 1493
1430package CFClient::UI::VGauge; 1494package CFClient::UI::Image;
1431 1495
1432our @ISA = CFClient::UI::Base::; 1496our @ISA = CFClient::UI::Base::;
1433 1497
1434use CFClient::OpenGL; 1498use CFClient::OpenGL;
1499use Carp qw/confess/;
1435 1500
1436my %tex = ( 1501our %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 1502
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new { 1503sub new {
1457 my $class = shift; 1504 my $class = shift;
1458 1505
1459 my $self = $class->SUPER::new ( 1506 my $self = $class->SUPER::new (@_);
1460 gauge => 'food',
1461 @_
1462 );
1463 1507
1464 $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h}; 1508 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1509
1510 $loaded_images{$self->{image}} ||=
1511 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1512
1513 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1514
1515 Scalar::Util::weaken $loaded_images{$self->{image}};
1516
1517 $self->{aspect} = $tex->{w} / $tex->{h};
1465 1518
1466 $self 1519 $self
1467} 1520}
1468 1521
1469sub size_request { 1522sub size_request {
1470 my ($self) = @_; 1523 my ($self) = @_;
1471 1524
1472 my $tex = $tex{$self->{gauge}}[0]; 1525 ($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} 1526}
1494 1527
1495sub _draw { 1528sub _draw {
1496 my ($self) = @_; 1529 my ($self) = @_;
1497 1530
1498 my $tex = $tex{$self->{gauge}}; 1531 my $tex = $self->{tex};
1499 1532
1500 my ($w, $h) = ($self->{w}, $self->{h}); 1533 my ($w, $h) = ($self->{w}, $self->{h});
1501 1534
1502 my $ycut = $self->{val} / ($self->{max_val} || 1); 1535 if ($self->{rot90}) {
1503 $ycut = 1 if $self->{val} > $self->{max_val}; 1536 glRotate 90, 0, 0, 1;
1537 glTranslate 0, -$self->{w}, 0;
1504 1538
1505 my $t1 = $tex->[0]; 1539 ($w, $h) = ($h, $w);
1506 my $t2 = $tex->[1]; 1540 }
1507 1541
1508 glEnable GL_BLEND; 1542 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1543 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D; 1544 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1545 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 1546
1547 $tex->draw_quad (0, 0, $w, $h);
1548
1549 glDisable GL_BLEND;
1550 glDisable GL_TEXTURE_2D;
1551}
1552
1553#############################################################################
1554
1555package CFClient::UI::VGauge;
1556
1557our @ISA = CFClient::UI::Base::;
1558
1559use CFClient::OpenGL;
1560
1561my %tex = (
1562 food => [
1563 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1564 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1565 ],
1566 grace => [
1567 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1568 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1569 ],
1570 hp => [
1571 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1572 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1573 ],
1574 mana => [
1575 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1576 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1577 ],
1578);
1579
1580# eg. VGauge->new (gauge => 'food'), default gauge: food
1581sub new {
1582 my $class = shift;
1583
1584 my $self = $class->SUPER::new (
1585 type => 'food',
1586 @_
1587 );
1588
1589 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1590
1591 $self
1592}
1593
1594sub size_request {
1595 my ($self) = @_;
1596
1597 #my $tex = $tex{$self->{type}}[0];
1598 #@$tex{qw(w h)}
1599 (0, 0)
1600}
1601
1602sub set_max {
1603 my ($self, $max) = @_;
1604
1605 $self->{max_val} = $max;
1606}
1607
1608sub set_value {
1609 my ($self, $val, $max) = @_;
1610
1611 $self->set_max ($max)
1612 if defined $max;
1613
1614 $max = $self->{max_val};
1615 $self->{val} = $val;
1616
1617 $self->update;
1618}
1619
1620sub _draw {
1621 my ($self) = @_;
1622
1623 my $tex = $tex{$self->{type}};
1624
1625 my ($w, $h) = ($self->{w}, $self->{h});
1626
1627 if ($self->{vertical}) {
1628 glRotate 90, 0, 0, 1;
1629 glTranslate 0, -$self->{w}, 0;
1630
1631 ($w, $h) = ($h, $w);
1632 }
1633
1634 my $ycut = $self->{val} / ($self->{max_val} || 1);
1635 $ycut = 1 if $self->{val} > $self->{max_val};
1636
1637 my $t1 = $tex->[0];
1638 my $t2 = $tex->[1];
1639
1640 glEnable GL_BLEND;
1641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1642 glEnable GL_TEXTURE_2D;
1643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1644
1513 my $h1 = $self->{h} - $ycut * $self->{h}; 1645 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h}; 1646 my $h2 = $ycut * $self->{h};
1515 1647
1516 my $yp = 0;
1517
1518 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1648 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin GL_QUADS; 1649 glBegin GL_QUADS;
1520 glTexCoord 0 , 0; glVertex 0 , $yp; 1650 glTexCoord 0 , 0; glVertex 0 , 0;
1521 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1; 1651 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1522 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1; 1652 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1523 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp; 1653 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1524 glEnd; 1654 glEnd;
1525
1526 $yp += $h1;
1527 1655
1528 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1656 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin GL_QUADS; 1657 glBegin GL_QUADS;
1530 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp; 1658 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1531 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2; 1659 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1532 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2; 1660 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1533 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp; 1661 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1534 glEnd; 1662 glEnd;
1535 1663
1536 glDisable GL_BLEND; 1664 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D; 1665 glDisable GL_TEXTURE_2D;
1666}
1667
1668#############################################################################
1669
1670package CFClient::UI::Gauge;
1671
1672our @ISA = CFClient::UI::VBox::;
1673
1674sub new {
1675 my ($class, %arg) = shift;
1676
1677 my $self = $class->SUPER::new (
1678 @_,
1679 );
1680
1681 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1682 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1683 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1684
1685 $self
1686}
1687
1688sub set_fontsize {
1689 my ($self, $fsize) = @_;
1690
1691 $self->{value}->set_fontsize ($fsize);
1692 $self->{max} ->set_fontsize ($fsize);
1693}
1694
1695sub set_value {
1696 my ($self, $val, $max) = @_;
1697
1698 $self->set_max ($max)
1699 if defined $max;
1700
1701 $self->{gauge}->set_value ($val, $max);
1702 $self->{value}->set_text ($val);
1703}
1704
1705sub set_max {
1706 my ($self, $max) = @_;
1707
1708 $self->{gauge}->set_max ($max);
1709 $self->{max}->set_text ($max);
1538} 1710}
1539 1711
1540############################################################################# 1712#############################################################################
1541 1713
1542package CFClient::UI::Slider; 1714package CFClient::UI::Slider;
1952} 2124}
1953 2125
1954sub add { 2126sub add {
1955 my ($self, $child) = @_; 2127 my ($self, $child) = @_;
1956 2128
2129 # integerize window positions
2130 $child->{x} = int $child->{x};
2131 $child->{y} = int $child->{y};
2132
1957 $self->SUPER::add ($child); 2133 $self->SUPER::add ($child);
1958} 2134}
1959 2135
1960sub on_refresh { 2136sub on_refresh {
1961 my ($self, $id, $cb) = @_; 2137 my ($self, $id, $cb) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines