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.143 by root, Thu Apr 20 21:28:51 2006 UTC vs.
Revision 1.149 by root, Sat Apr 22 23:11:32 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
152 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 152 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 153 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154 154
155 # use alignment to adjust x, y 155 # use alignment to adjust x, y
156 156
157 $x += ($w - $w2) * 0.5; 157 $x += int +($w - $w2) * 0.5;
158 $y += ($h - $h2) * 0.5; 158 $y += int +($h - $h2) * 0.5;
159 159
160 ($w, $h) = ($w2, $h2); 160 ($w, $h) = ($w2, $h2);
161 } 161 }
162 162
163 if ($self->{x} != $x || $self->{y} != $y) { 163 if ($self->{x} != $x || $self->{y} != $y) {
294} 294}
295 295
296sub check_size { 296sub check_size {
297 my ($self) = @_; 297 my ($self) = @_;
298 298
299 return unless $self->{parent};
300
299 my ($w, $h) = $self->size_request; 301 my ($w, $h) = $self->size_request;
300 302
301 if ($w != $self->{req_w} || $h != $self->{req_h}) { 303 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w; 304 $self->{req_w} = $w;
303 $self->{req_h} = $h; 305 $self->{req_h} = $h;
304 306
305 $self->{parent}->check_size 307 $self->{parent}->check_size;
306 if $self->{parent};
307 } 308 }
308} 309}
309 310
310sub update { 311sub update {
311 my ($self) = @_; 312 my ($self) = @_;
561sub new { die } 562sub new { die }
562 563
563sub size_request { 564sub size_request {
564 my ($self) = @_; 565 my ($self) = @_;
565 566
566 @$self{qw(child_w child_h)} = $self->child->size_request; 567 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
567 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 568 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568 569
569 @$self{qw(child_w child_h)} 570 @$self{qw(child_w child_h)}
570} 571}
571 572
581package CFClient::UI::Frame; 582package CFClient::UI::Frame;
582 583
583our @ISA = CFClient::UI::Bin::; 584our @ISA = CFClient::UI::Bin::;
584 585
585use CFClient::OpenGL; 586use CFClient::OpenGL;
586
587sub size_request {
588 my ($self) = @_;
589 my $chld = $self->child
590 or return (0, 0);
591
592 $chld->move (2, 2);
593
594 map { $_ + 4 } $chld->size_request;
595}
596
597sub size_allocate {
598 my ($self, $x, $y, $w, $h) = @_;
599
600 $self->child->configure (2, 2, $w - 4, $h - 4);
601}
602
603sub _draw {
604 my ($self) = @_;
605
606 my $chld = $self->child;
607
608 my ($w, $h) = $chld->size_request;
609
610 glBegin GL_QUADS;
611 glColor 0, 0, 0;
612 glVertex 0 , 0;
613 glVertex 0 , $h + 4;
614 glVertex $w + 4 , $h + 4;
615 glVertex $w + 4 , 0;
616 glEnd;
617
618 $chld->draw;
619}
620
621#############################################################################
622
623package CFClient::UI::FancyFrame;
624
625our @ISA = CFClient::UI::Bin::;
626
627use CFClient::OpenGL;
628
629my @tex =
630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
631 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
632 587
633sub new { 588sub new {
634 my $class = shift; 589 my $class = shift;
635
636 # TODO: user_x, user_y, overwrite moveto?
637 590
638 my $self = $class->SUPER::new ( 591 my $self = $class->SUPER::new (
639 bg => [1, 1, 1, 1], 592 bg => [1, 1, 1, 1],
640 border_bg => [1, 1, 1, 1], 593 border_bg => [1, 1, 1, 1],
641 border => 0.8, 594 border => 0.8,
642 @_ 595 @_
643 ); 596 );
644 597
598 $self
599}
600
601sub set_size {
602 my ($self, $w, $h) = @_;
603 $self->{req_w} = $w;
604 $self->{req_h} = $h;
605 $self->check_size;
606}
607
608sub size_request {
609 my ($self) = @_;
610 ($self->{req_w}, $self->{req_h})
611}
612
613sub size_allocate {
614 my ($self, $w, $h) = @_;
615 $self->{w} = $w;
616 $self->{h} = $h;
617 $self->child->configure (0, 0, $w, $h);
618}
619
620sub _draw {
621 my ($self) = @_;
622
623 my ($w, $h) = ($self->{w}, $self->{h});
624
625 glEnable GL_BLEND;
626 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
627 glEnable GL_TEXTURE_2D;
628 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
629
630# glBegin GL_QUADS;
631# glColor 0, 0, 0, 0;
632# glVertex 0 , 0;
633# glVertex 0 , $h;
634# glVertex $w, $h;
635# glVertex $w, 0;
636# glEnd;
637
638
639 $self->child->draw;
640 glDisable GL_BLEND;
641 glDisable GL_TEXTURE_2D;
642}
643
644#############################################################################
645
646package CFClient::UI::FancyFrame;
647
648our @ISA = CFClient::UI::Bin::;
649
650use CFClient::OpenGL;
651
652my @tex =
653 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
654 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
655
656sub new {
657 my $class = shift;
658
659 # TODO: user_x, user_y, overwrite moveto?
660
661 my $self = $class->SUPER::new (
662 bg => [1, 1, 1, 1],
663 border_bg => [1, 1, 1, 1],
664 border => 0.8,
665 @_
666 );
667
645 $self->{title} &&= new CFClient::UI::Label 668 $self->{title} &&= new CFClient::UI::Label
646 align => 0, 669 align => 0,
647 valign => 1, 670 valign => 1,
648 text => $self->{title}, 671 text => $self->{title},
649 fontsize => 1; 672 fontsize => 1;
815 or next; 838 or next;
816 839
817 for my $x (0 .. $#$row) { 840 for my $x (0 .. $#$row) {
818 my $widget = $row->[$x] 841 my $widget = $row->[$x]
819 or next; 842 or next;
820 my ($w, $h) = $widget->size_request; 843 my ($w, $h) = @$widget{qw(req_w req_h)};
821 844
822 $w[$x] = max $w[$x], $w; 845 $w[$x] = max $w[$x], $w;
823 $h[$y] = max $h[$y], $h; 846 $h[$y] = max $h[$y], $h;
824 } 847 }
825 } 848 }
930 953
931 ($h, $w) = ($w, $h); 954 ($h, $w) = ($w, $h);
932 955
933 my $children = $self->{children}; 956 my $children = $self->{children};
934 957
935 my @h = map +($_->size_request)[0], @$children; 958 my @h = map $_->{req_w}, @$children;
936 959
937 my $req_h = List::Util::sum @h; 960 my $req_h = List::Util::sum @h;
938 961
939 if ($req_h > $h) { 962 if ($req_h > $h) {
940 # ah well, not enough space 963 # ah well, not enough space
988sub size_allocate { 1011sub size_allocate {
989 my ($self, $w, $h) = @_; 1012 my ($self, $w, $h) = @_;
990 1013
991 my $children = $self->{children}; 1014 my $children = $self->{children};
992 1015
993 my @h = map +($_->size_request)[1], @$children; 1016 my @h = map $_->{req_h}, @$children;
994 1017
995 my $req_h = List::Util::sum @h; 1018 my $req_h = List::Util::sum @h;
996 1019
997 if ($req_h > $h) { 1020 if ($req_h > $h) {
998 # ah well, not enough space 1021 # ah well, not enough space
1070 my ($self, $text) = @_; 1093 my ($self, $text) = @_;
1071 1094
1072 $self->{layout}->set_text ($text); 1095 $self->{layout}->set_text ($text);
1073 1096
1074 delete $self->{texture}; 1097 delete $self->{texture};
1098 $self->check_size;
1075 $self->update; 1099 $self->update;
1076} 1100}
1077 1101
1078sub set_markup { 1102sub set_markup {
1079 my ($self, $markup) = @_; 1103 my ($self, $markup) = @_;
1080 1104
1081 $self->{layout}->set_markup ($markup); 1105 $self->{layout}->set_markup ($markup);
1082 1106
1083 delete $self->{texture}; 1107 delete $self->{texture};
1108 $self->check_size;
1084 $self->update; 1109 $self->update;
1085} 1110}
1086 1111
1087sub size_request { 1112sub size_request {
1088 my ($self) = @_; 1113 my ($self) = @_;
1109 1134
1110sub size_allocate { 1135sub size_allocate {
1111 my ($self, $w, $h) = @_; 1136 my ($self, $w, $h) = @_;
1112 1137
1113 delete $self->{texture}; 1138 delete $self->{texture};
1139}
1140
1141sub set_fontsize {
1142 my ($self, $fontsize) = @_;
1143
1144 $self->{fontsize} = $fontsize;
1145 $self->check_size;
1114} 1146}
1115 1147
1116sub _draw { 1148sub _draw {
1117 my ($self) = @_; 1149 my ($self) = @_;
1118 1150
1201} 1233}
1202 1234
1203sub size_allocate { 1235sub size_allocate {
1204 my ($self, $w, $h) = @_; 1236 my ($self, $w, $h) = @_;
1205 1237
1206 $self->_set_text ($self->{text}); 1238 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1207} 1239}
1208 1240
1209sub set_text { 1241sub set_text {
1210 my ($self, $text) = @_; 1242 my ($self, $text) = @_;
1211 1243
1344our @ISA = CFClient::UI::Label::; 1376our @ISA = CFClient::UI::Label::;
1345 1377
1346use CFClient::OpenGL; 1378use CFClient::OpenGL;
1347 1379
1348my @tex = 1380my @tex =
1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1381 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1350 qw(b1_button_active.png); 1382 qw(b1_button_active.png);
1351 1383
1352sub new { 1384sub new {
1353 my $class = shift; 1385 my $class = shift;
1354 1386
1401package CFClient::UI::CheckBox; 1433package CFClient::UI::CheckBox;
1402 1434
1403our @ISA = CFClient::UI::DrawBG::; 1435our @ISA = CFClient::UI::DrawBG::;
1404 1436
1405my @tex = 1437my @tex =
1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1407 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1439 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1408 1440
1409use CFClient::OpenGL; 1441use CFClient::OpenGL;
1410 1442
1411sub new { 1443sub new {
1460 glDisable GL_BLEND; 1492 glDisable GL_BLEND;
1461} 1493}
1462 1494
1463############################################################################# 1495#############################################################################
1464 1496
1497package CFClient::UI::Image;
1498
1499our @ISA = CFClient::UI::Base::;
1500
1501use CFClient::OpenGL;
1502use Carp qw/confess/;
1503
1504our %loaded_images;
1505
1506sub new {
1507 my $class = shift;
1508
1509 my $self = $class->SUPER::new (@_);
1510
1511 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1512
1513 $loaded_images{$self->{image}} ||=
1514 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1515
1516 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1517
1518 Scalar::Util::weaken $loaded_images{$self->{image}};
1519
1520 $self->{aspect} = $tex->{w} / $tex->{h};
1521
1522 $self
1523}
1524
1525sub size_request {
1526 my ($self) = @_;
1527
1528 ($self->{tex}->{w}, $self->{tex}->{h})
1529}
1530
1531sub _draw {
1532 my ($self) = @_;
1533
1534 my $tex = $self->{tex};
1535
1536 my ($w, $h) = ($self->{w}, $self->{h});
1537
1538 if ($self->{rot90}) {
1539 glRotate 90, 0, 0, 1;
1540 glTranslate 0, -$self->{w}, 0;
1541
1542 ($w, $h) = ($h, $w);
1543 }
1544
1545 glEnable GL_BLEND;
1546 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1547 glEnable GL_TEXTURE_2D;
1548 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1549
1550 $tex->draw_quad (0, 0, $w, $h);
1551
1552 glDisable GL_BLEND;
1553 glDisable GL_TEXTURE_2D;
1554}
1555
1556#############################################################################
1557
1465package CFClient::UI::VGauge; 1558package CFClient::UI::VGauge;
1466 1559
1467our @ISA = CFClient::UI::Base::; 1560our @ISA = CFClient::UI::Base::;
1468 1561
1469use CFClient::OpenGL; 1562use CFClient::OpenGL;
1470 1563
1471my %tex = ( 1564my %tex = (
1472 food => [ 1565 food => [
1473 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1566 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1474 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1567 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1475 ], 1568 ],
1476 grace => [ 1569 grace => [
1477 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1570 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1478 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1571 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1479 ], 1572 ],
1480 hp => [ 1573 hp => [
1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1574 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1482 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1575 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1483 ], 1576 ],
1484 mana => [ 1577 mana => [
1485 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1486 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1579 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1487 ], 1580 ],
1488); 1581);
1489 1582
1490# eg. VGauge->new (gauge => 'food'), default gauge: food 1583# eg. VGauge->new (gauge => 'food'), default gauge: food
1591 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1684 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1592 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1685 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1593 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1686 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1594 1687
1595 $self 1688 $self
1689}
1690
1691sub set_fontsize {
1692 my ($self, $fsize) = @_;
1693
1694 $self->{value}->set_fontsize ($fsize);
1695 $self->{max} ->set_fontsize ($fsize);
1596} 1696}
1597 1697
1598sub set_value { 1698sub set_value {
1599 my ($self, $val, $max) = @_; 1699 my ($self, $val, $max) = @_;
1600 1700
1995use CFClient::OpenGL; 2095use CFClient::OpenGL;
1996 2096
1997sub check_size { 2097sub check_size {
1998 my ($self) = @_; 2098 my ($self) = @_;
1999 2099
2000 $self->configure (0, 0, $::WITH, $::HEIGHT); 2100 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2001} 2101}
2002 2102
2003sub size_request { 2103sub size_request {
2004 ($::WIDTH, $::HEIGHT) 2104 ($::WIDTH, $::HEIGHT)
2005} 2105}
2007sub configure { 2107sub configure {
2008 my ($self, $x, $y, $w, $h) = @_; 2108 my ($self, $x, $y, $w, $h) = @_;
2009 2109
2010 $self->SUPER::configure ($x, $y, $w, $h); 2110 $self->SUPER::configure ($x, $y, $w, $h);
2011 2111
2012 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2013 for @{$self->{children}}; 2112 for my $child (@{$self->{children}}) {
2113 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2114
2115 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2116 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2117 $child->configure ($X, $Y, $W,$H);
2118 }
2014} 2119}
2015 2120
2016sub _topleft { 2121sub _topleft {
2017 my ($self, $x, $y) = @_; 2122 my ($self, $x, $y) = @_;
2018 2123
2026 ::refresh (); 2131 ::refresh ();
2027} 2132}
2028 2133
2029sub add { 2134sub add {
2030 my ($self, $child) = @_; 2135 my ($self, $child) = @_;
2136
2137 # integerize window positions
2138 $child->{x} = int $child->{x};
2139 $child->{y} = int $child->{y};
2031 2140
2032 $self->SUPER::add ($child); 2141 $self->SUPER::add ($child);
2033} 2142}
2034 2143
2035sub on_refresh { 2144sub on_refresh {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines