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.155 by root, Sun Apr 23 02:22:54 2006 UTC vs.
Revision 1.162 by root, Mon Apr 24 06:05:35 2006 UTC

14our $BUTTON_STATE; 14our $BUTTON_STATE;
15 15
16sub check_tooltip { 16sub check_tooltip {
17 if (!$GRAB) { 17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) { 19 if (length $widget->{tooltip}) {
20 20
21 if ($TOOLTIP->{owner} != $widget) { 21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget; 22 $TOOLTIP->{owner} = $widget;
23 23
24 my $tip = $widget->{tooltip}; 24 my $tip = $widget->{tooltip};
25 25
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 27
28 $TOOLTIP->set_text ($widget->{tooltip}); 28 $TOOLTIP->set_markup ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); 29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show; 30 $TOOLTIP->show;
31 } 31 }
32 32
33 return; 33 return;
142 } 142 }
143 143
144 $self 144 $self
145} 145}
146 146
147sub destroy {
148 my ($self) = @_;
149
150 $self->hide;
151 %$self = ();
152}
153
147sub show { 154sub show {
148 my ($self) = @_; 155 my ($self) = @_;
149 156
150 return if $self->{parent}; 157 return if $self->{parent};
151 158
168 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
169 176
170 $self->update; 177 $self->update;
171} 178}
172 179
173sub needs_redraw { 180sub set_size {
174 0 181 my ($self, $w, $h) = @_;
182
183 $self->{user_w} = $w;
184 $self->{user_h} = $h;
185
186 $self->check_size;
175} 187}
176 188
177sub size_request { 189sub size_request {
178 require Carp; 190 require Carp;
179 Carp::confess "size_request is abstract"; 191 Carp::confess "size_request is abstract";
209 } 221 }
210} 222}
211 223
212sub size_allocate { 224sub size_allocate {
213 # nothing to be done 225 # nothing to be done
226}
227
228sub set_max_size {
229 my ($self, $w, $h) = @_;
230
231 delete $self->{max_w}; $self->{max_w} = $w if $w;
232 delete $self->{max_h}; $self->{max_h} = $h if $h;
214} 233}
215 234
216# return top left coordinates 235# return top left coordinates
217sub _topleft { 236sub _topleft {
218 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
301 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
302 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
303 glEnd; 322 glEnd;
304 glDisable GL_BLEND; 323 glDisable GL_BLEND;
305 } 324 }
325
326 if ($ENV{PCLIENT_DEBUG}) {
327 glPushMatrix;
328 glColor 1, 1, 0, 1;
329 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
330 glBegin GL_LINE_LOOP;
331 glVertex 0 , 0;
332 glVertex $self->{w}, 0;
333 glVertex $self->{w}, $self->{h};
334 glVertex 0 , $self->{h};
335 glEnd;
336 glPopMatrix;
337 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
338 }
306} 339}
307 340
308sub _draw { 341sub _draw {
309 my ($self) = @_; 342 my ($self) = @_;
310 343
330} 363}
331 364
332sub check_size { 365sub check_size {
333 my ($self) = @_; 366 my ($self) = @_;
334 367
335 return unless $self->{parent}; 368 $self->{parent}
369 or return 1;
336 370
337 my ($w, $h) = $self->size_request; 371 my ($w, $h) = $self->{user_w} && $self->{user_h}
372 ? @$self{qw(user_w user_h)}
373 : $self->size_request;
338 374
339 if ($w != $self->{req_w} || $h != $self->{req_h}) { 375 if ($w != $self->{req_w} || $h != $self->{req_h}) {
340 $self->{req_w} = $w; 376 $self->{req_w} = $w;
341 $self->{req_h} = $h; 377 $self->{req_h} = $h;
342 378
343 $self->{parent}->check_size; 379 $self->{parent}->check_size
380 or $self->size_allocate (
381 (List::Util::max $self->{w}, $w),
382 (List::Util::max $self->{h}, $h),
383 );
384
385 1
386 } else {
387 0
344 } 388 }
345} 389}
346 390
347sub update { 391sub update {
348 my ($self) = @_; 392 my ($self) = @_;
469 513
470 $self->check_size; 514 $self->check_size;
471 $self->update; 515 $self->update;
472} 516}
473 517
518sub clear {
519 my ($self) = @_;
520
521 delete $_->{parent}
522 for @{ delete $self->{children} };
523
524 $self->{children} = [];
525}
526
474sub find_widget { 527sub find_widget {
475 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
476 529
477 $x -= $self->{x}; 530 $x -= $self->{x};
478 $y -= $self->{y}; 531 $y -= $self->{y};
636 border => 0.8, 689 border => 0.8,
637 @_ 690 @_
638 ); 691 );
639 692
640 $self 693 $self
641}
642
643sub set_size {
644 my ($self, $w, $h) = @_;
645 $self->{req_w} = $w;
646 $self->{req_h} = $h;
647 $self->check_size;
648}
649
650sub size_request {
651 my ($self) = @_;
652 ($self->{req_w}, $self->{req_h})
653}
654
655sub size_allocate {
656 my ($self, $w, $h) = @_;
657 $self->{w} = $w;
658 $self->{h} = $h;
659 $self->child->configure (0, 0, $w, $h);
660} 694}
661 695
662sub _draw { 696sub _draw {
663 my ($self) = @_; 697 my ($self) = @_;
664 698
721 int $_[0]{border} * $::FONTSIZE 755 int $_[0]{border} * $::FONTSIZE
722} 756}
723 757
724sub size_request { 758sub size_request {
725 my ($self) = @_; 759 my ($self) = @_;
726
727 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
728 760
729 my ($w, $h) = $self->SUPER::size_request; 761 my ($w, $h) = $self->SUPER::size_request;
730 762
731 ( 763 (
732 $w + $self->border * 2, 764 $w + $self->border * 2,
1099sub new { 1131sub new {
1100 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
1101 1133
1102 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
1103 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
1136 #font => default_font
1104 fontsize => 1, 1137 fontsize => 1,
1105 text => "", 1138 text => "",
1106 align => -1, 1139 align => -1,
1107 valign => -1, 1140 valign => -1,
1108 padding => 2, 1141 padding => 2,
1121 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1154 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1122 1155
1123 $self 1156 $self
1124} 1157}
1125 1158
1126sub escape_text { 1159sub escape {
1127 local $_ = $_[1]; 1160 local $_ = $_[1];
1128 1161
1129 s/&/&/g; 1162 s/&/&/g;
1130 s/>/>/g; 1163 s/>/>/g;
1131 s/</&lt;/g; 1164 s/</&lt;/g;
1154} 1187}
1155 1188
1156sub size_request { 1189sub size_request {
1157 my ($self) = @_; 1190 my ($self) = @_;
1158 1191
1159 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1161 1195
1162 my ($w, $h) = $self->{layout}->size; 1196 my ($w, $h) = $self->{layout}->size;
1163 1197
1164 if (exists $self->{template}) { 1198 if (exists $self->{template}) {
1199 $self->{template}->set_font ($self->{font}) if $self->{font};
1165 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1200 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1166 1201
1167 my ($w2, $h2) = $self->{template}->size; 1202 my ($w2, $h2) = $self->{template}->size;
1168 1203
1169 $w = List::Util::max $w, $w2; 1204 $w = List::Util::max $w, $w2;
1193 1228
1194sub _draw { 1229sub _draw {
1195 my ($self) = @_; 1230 my ($self) = @_;
1196 1231
1197 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
1198 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
1199 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1235 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1200 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
1201 }; 1237 };
1202 1238
1605 1641
1606package CFClient::UI::VGauge; 1642package CFClient::UI::VGauge;
1607 1643
1608our @ISA = CFClient::UI::Base::; 1644our @ISA = CFClient::UI::Base::;
1609 1645
1646use List::Util qw(min max);
1647
1610use CFClient::OpenGL; 1648use CFClient::OpenGL;
1611 1649
1612my %tex = ( 1650my %tex = (
1613 food => [ 1651 food => [
1614 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1615 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1653 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1616 ], 1654 ],
1617 grace => [ 1655 grace => [
1618 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1656 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1619 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1657 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1620 ], 1658 ],
1621 hp => [ 1659 hp => [
1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1660 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1623 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1661 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1624 ], 1662 ],
1625 mana => [ 1663 mana => [
1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1664 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1627 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1665 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1628 ], 1666 ],
1629); 1667);
1630 1668
1631# eg. VGauge->new (gauge => 'food'), default gauge: food 1669# eg. VGauge->new (gauge => 'food'), default gauge: food
1632sub new { 1670sub new {
1670 1708
1671sub _draw { 1709sub _draw {
1672 my ($self) = @_; 1710 my ($self) = @_;
1673 1711
1674 my $tex = $tex{$self->{type}}; 1712 my $tex = $tex{$self->{type}};
1713 my ($t1, $t2, $t3) = @$tex;
1675 1714
1676 my ($w, $h) = ($self->{w}, $self->{h}); 1715 my ($w, $h) = ($self->{w}, $self->{h});
1677 1716
1678 if ($self->{vertical}) { 1717 if ($self->{vertical}) {
1679 glRotate 90, 0, 0, 1; 1718 glRotate 90, 0, 0, 1;
1681 1720
1682 ($w, $h) = ($h, $w); 1721 ($w, $h) = ($h, $w);
1683 } 1722 }
1684 1723
1685 my $ycut = $self->{val} / ($self->{max_val} || 1); 1724 my $ycut = $self->{val} / ($self->{max_val} || 1);
1686 $ycut = 1 if $self->{val} > $self->{max_val};
1687 1725
1688 my $t1 = $tex->[0]; 1726 my $ycut1 = max 0, min 1, $ycut;
1689 my $t2 = $tex->[1]; 1727 my $ycut2 = max 0, min 1, $ycut - 1;
1728
1729 my $h1 = $self->{h} * (1 - $ycut1);
1730 my $h2 = $self->{h} * (1 - $ycut2);
1690 1731
1691 glEnable GL_BLEND; 1732 glEnable GL_BLEND;
1692 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1733 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1693 glEnable GL_TEXTURE_2D; 1734 glEnable GL_TEXTURE_2D;
1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1695 1736
1696 my $h1 = $self->{h} - $ycut * $self->{h};
1697 my $h2 = $ycut * $self->{h};
1698
1699 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1737 glBindTexture GL_TEXTURE_2D, $t1->{name};
1700 glBegin GL_QUADS; 1738 glBegin GL_QUADS;
1701 glTexCoord 0 , 0; glVertex 0 , 0; 1739 glTexCoord 0 , 0; glVertex 0 , 0;
1702 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1740 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1703 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1741 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1704 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1742 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1705 glEnd; 1743 glEnd;
1706 1744
1745 my $ycut1 = List::Util::min 1, $ycut;
1707 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1746 glBindTexture GL_TEXTURE_2D, $t2->{name};
1708 glBegin GL_QUADS; 1747 glBegin GL_QUADS;
1709 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1748 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1710 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1749 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1711 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1750 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1712 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1751 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1713 glEnd; 1752 glEnd;
1753
1754 if ($t3) {
1755 glBindTexture GL_TEXTURE_2D, $t3->{name};
1756 glBegin GL_QUADS;
1757 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1758 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1759 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1760 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1761 glEnd;
1762 }
1714 1763
1715 glDisable GL_BLEND; 1764 glDisable GL_BLEND;
1716 glDisable GL_TEXTURE_2D; 1765 glDisable GL_TEXTURE_2D;
1717} 1766}
1718 1767
1725sub new { 1774sub new {
1726 my ($class, %arg) = @_; 1775 my ($class, %arg) = @_;
1727 1776
1728 my $self = $class->SUPER::new ( 1777 my $self = $class->SUPER::new (
1729 tooltip => $arg{type}, 1778 tooltip => $arg{type},
1779 can_events => 1,
1780 can_hover => 1,
1730 %arg, 1781 %arg,
1731 ); 1782 );
1732 1783
1733 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1734 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1735 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); 1786 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1736 1787
1737 $self 1788 $self
1738} 1789}
1739 1790
1907sub new { 1958sub new {
1908 my $class = shift; 1959 my $class = shift;
1909 1960
1910 my $self = $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1911 fontsize => 1, 1962 fontsize => 1,
1963 #font => default_font
1912 @_, 1964 @_,
1913 1965
1914 layout => (new CFClient::Layout), 1966 layout => (new CFClient::Layout),
1915 par => [], 1967 par => [],
1916 height => 0, 1968 height => 0,
1956sub size_allocate { 2008sub size_allocate {
1957 my ($self, $w, $h) = @_; 2009 my ($self, $w, $h) = @_;
1958 2010
1959 $self->SUPER::size_allocate ($w, $h); 2011 $self->SUPER::size_allocate ($w, $h);
1960 2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
1961 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1962 $self->{layout}->set_width ($self->{children}[0]{w}); 2015 $self->{layout}->set_width ($self->{children}[0]{w});
1963 2016
1964 $self->reflow; 2017 $self->reflow;
1965} 2018}
2017 my $y1 = $top + $self->{h}; 2070 my $y1 = $top + $self->{h};
2018 2071
2019 my $y = 0; 2072 my $y = 0;
2020 2073
2021 my $layout = $self->{layout}; 2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
2022 2077
2023 for my $par (@{$self->{par}}) { 2078 for my $par (@{$self->{par}}) {
2024 my $h = $par->[0]; 2079 my $h = $par->[0];
2025 2080
2026 if ($y0 < $y + $h && $y < $y1) { 2081 if ($y0 < $y + $h && $y < $y1) {
2151 @_, 2206 @_,
2152 can_events => 0, 2207 can_events => 0,
2153 ) 2208 )
2154} 2209}
2155 2210
2156sub set_text { 2211sub set_markup {
2157 my ($self, $text) = @_; 2212 my ($self, $text) = @_;
2158 2213
2159 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2214 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2160 $self->{label}->set_text ($text); 2215 $self->{label}->set_markup ($text);
2161 $self->add ($self->{label}); 2216 $self->add ($self->{label});
2162} 2217}
2163 2218
2164sub size_request { 2219sub size_request {
2165 my ($self) = @_; 2220 my ($self) = @_;
2166 2221
2222 $self->child->set_max_size ($::WIDTH * 0.3);
2223
2167 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2224 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2168 2225
2169 $w = List::Util::min $::WIDTH * 0.2, $w;
2170 $h = List::Util::max $::HEIGHT * 0.2, $h;
2171
2172 ($w + 4, $h + 4) 2226 ($w + 4, $h + 4)
2227}
2228
2229sub size_allocate {
2230 my ($self, $w, $h) = @_;
2231
2232 $self->SUPER::size_allocate ($w - 4, $h - 4);
2173} 2233}
2174 2234
2175sub _draw { 2235sub _draw {
2176 my ($self) = @_; 2236 my ($self) = @_;
2177 2237
2202 $self->SUPER::_draw; 2262 $self->SUPER::_draw;
2203} 2263}
2204 2264
2205############################################################################# 2265#############################################################################
2206 2266
2267package CFClient::UI::Face;
2268
2269our @ISA = CFClient::UI::Base::;
2270
2271use CFClient::OpenGL;
2272
2273sub new {
2274 my $class = shift;
2275
2276 $class->SUPER::new (
2277 aspect => 1,
2278 @_,
2279 )
2280}
2281
2282sub size_request {
2283 (32, 8)
2284}
2285
2286sub draw {
2287 my ($self) = @_;
2288
2289 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2290
2291 if ($tex) {
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 glColor 1, 1, 1, 1;
2297 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2298 glDisable GL_TEXTURE_2D;
2299 glDisable GL_BLEND;
2300 }
2301}
2302
2303#############################################################################
2304
2207package CFClient::UI::Root; 2305package CFClient::UI::Root;
2208 2306
2209our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
2210 2308
2211use CFClient::OpenGL; 2309use CFClient::OpenGL;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines