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.151 by root, Sun Apr 23 00:57:39 2006 UTC vs.
Revision 1.161 by root, Mon Apr 24 03:43:51 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
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
23 $TOOLTIP->set_text ($widget->{tooltip}); 28 $TOOLTIP->set_text ($widget->{tooltip});
24 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); 29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
25 $TOOLTIP->show; 30 $TOOLTIP->show;
26 } 31 }
27 32
137 } 142 }
138 143
139 $self 144 $self
140} 145}
141 146
147sub destroy {
148 my ($self) = @_;
149
150 $self->hide;
151 %$self = ();
152}
153
142sub show { 154sub show {
143 my ($self) = @_; 155 my ($self) = @_;
144 156
145 return if $self->{parent}; 157 return if $self->{parent};
146 158
163 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
164 176
165 $self->update; 177 $self->update;
166} 178}
167 179
168sub needs_redraw { 180sub set_size {
169 0 181 my ($self, $w, $h) = @_;
182
183 $self->{user_w} = $w;
184 $self->{user_h} = $h;
185
186 $self->check_size;
170} 187}
171 188
172sub size_request { 189sub size_request {
173 require Carp; 190 require Carp;
174 Carp::confess "size_request is abstract"; 191 Carp::confess "size_request is abstract";
204 } 221 }
205} 222}
206 223
207sub size_allocate { 224sub size_allocate {
208 # 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;
209} 233}
210 234
211# return top left coordinates 235# return top left coordinates
212sub _topleft { 236sub _topleft {
213 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
325} 349}
326 350
327sub check_size { 351sub check_size {
328 my ($self) = @_; 352 my ($self) = @_;
329 353
330 return unless $self->{parent}; 354 $self->{parent}
355 or return 1;
331 356
332 my ($w, $h) = $self->size_request; 357 my ($w, $h) = $self->{user_w} && $self->{user_h}
358 ? @$self{qw(user_w user_h)}
359 : $self->size_request;
333 360
334 if ($w != $self->{req_w} || $h != $self->{req_h}) { 361 if ($w != $self->{req_w} || $h != $self->{req_h}) {
335 $self->{req_w} = $w; 362 $self->{req_w} = $w;
336 $self->{req_h} = $h; 363 $self->{req_h} = $h;
337 364
338 $self->{parent}->check_size; 365 $self->{parent}->check_size
366 or $self->size_allocate (
367 (List::Util::max $self->{w}, $w),
368 (List::Util::max $self->{h}, $h),
369 );
370
371 1
372 } else {
373 0
339 } 374 }
340} 375}
341 376
342sub update { 377sub update {
343 my ($self) = @_; 378 my ($self) = @_;
631 border => 0.8, 666 border => 0.8,
632 @_ 667 @_
633 ); 668 );
634 669
635 $self 670 $self
636}
637
638sub set_size {
639 my ($self, $w, $h) = @_;
640 $self->{req_w} = $w;
641 $self->{req_h} = $h;
642 $self->check_size;
643}
644
645sub size_request {
646 my ($self) = @_;
647 ($self->{req_w}, $self->{req_h})
648}
649
650sub size_allocate {
651 my ($self, $w, $h) = @_;
652 $self->{w} = $w;
653 $self->{h} = $h;
654 $self->child->configure (0, 0, $w, $h);
655} 671}
656 672
657sub _draw { 673sub _draw {
658 my ($self) = @_; 674 my ($self) = @_;
659 675
716 int $_[0]{border} * $::FONTSIZE 732 int $_[0]{border} * $::FONTSIZE
717} 733}
718 734
719sub size_request { 735sub size_request {
720 my ($self) = @_; 736 my ($self) = @_;
721
722 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
723 737
724 my ($w, $h) = $self->SUPER::size_request; 738 my ($w, $h) = $self->SUPER::size_request;
725 739
726 ( 740 (
727 $w + $self->border * 2, 741 $w + $self->border * 2,
1094sub new { 1108sub new {
1095 my ($class, %arg) = @_; 1109 my ($class, %arg) = @_;
1096 1110
1097 my $self = $class->SUPER::new ( 1111 my $self = $class->SUPER::new (
1098 fg => [1, 1, 1], 1112 fg => [1, 1, 1],
1113 #font => default_font
1099 fontsize => 1, 1114 fontsize => 1,
1100 text => "", 1115 text => "",
1101 align => -1, 1116 align => -1,
1102 valign => -1, 1117 valign => -1,
1103 padding => 2, 1118 padding => 2,
1149} 1164}
1150 1165
1151sub size_request { 1166sub size_request {
1152 my ($self) = @_; 1167 my ($self) = @_;
1153 1168
1154 $self->{layout}->set_width; 1169 $self->{layout}->set_font ($self->{font}) if $self->{font};
1170 $self->{layout}->set_width ($self->{max_w} || -1);
1155 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1171 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1156 1172
1157 my ($w, $h) = $self->{layout}->size; 1173 my ($w, $h) = $self->{layout}->size;
1158 1174
1159 if (exists $self->{template}) { 1175 if (exists $self->{template}) {
1176 $self->{template}->set_font ($self->{font}) if $self->{font};
1160 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1177 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1161 1178
1162 my ($w2, $h2) = $self->{template}->size; 1179 my ($w2, $h2) = $self->{template}->size;
1163 1180
1164 $w = List::Util::max $w, $w2; 1181 $w = List::Util::max $w, $w2;
1179 1196
1180sub set_fontsize { 1197sub set_fontsize {
1181 my ($self, $fontsize) = @_; 1198 my ($self, $fontsize) = @_;
1182 1199
1183 $self->{fontsize} = $fontsize; 1200 $self->{fontsize} = $fontsize;
1201 delete $self->{texture};
1184 $self->check_size; 1202 $self->check_size;
1203 $self->update;
1185} 1204}
1186 1205
1187sub _draw { 1206sub _draw {
1188 my ($self) = @_; 1207 my ($self) = @_;
1189 1208
1190 my $tex = $self->{texture} ||= do { 1209 my $tex = $self->{texture} ||= do {
1210 $self->{layout}->set_font ($self->{font}) if $self->{font};
1191 $self->{layout}->set_width ($self->{w}); 1211 $self->{layout}->set_width ($self->{w});
1192 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1212 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1193 new_from_layout CFClient::Texture $self->{layout} 1213 new_from_layout CFClient::Texture $self->{layout}
1194 }; 1214 };
1195 1215
1598 1618
1599package CFClient::UI::VGauge; 1619package CFClient::UI::VGauge;
1600 1620
1601our @ISA = CFClient::UI::Base::; 1621our @ISA = CFClient::UI::Base::;
1602 1622
1623use List::Util qw(min max);
1624
1603use CFClient::OpenGL; 1625use CFClient::OpenGL;
1604 1626
1605my %tex = ( 1627my %tex = (
1606 food => [ 1628 food => [
1607 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1629 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1608 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1630 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1609 ], 1631 ],
1610 grace => [ 1632 grace => [
1611 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1633 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1612 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1634 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1613 ], 1635 ],
1614 hp => [ 1636 hp => [
1615 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1637 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1616 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1638 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1617 ], 1639 ],
1618 mana => [ 1640 mana => [
1619 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1641 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1620 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1642 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1621 ], 1643 ],
1622); 1644);
1623 1645
1624# eg. VGauge->new (gauge => 'food'), default gauge: food 1646# eg. VGauge->new (gauge => 'food'), default gauge: food
1625sub new { 1647sub new {
1663 1685
1664sub _draw { 1686sub _draw {
1665 my ($self) = @_; 1687 my ($self) = @_;
1666 1688
1667 my $tex = $tex{$self->{type}}; 1689 my $tex = $tex{$self->{type}};
1690 my ($t1, $t2, $t3) = @$tex;
1668 1691
1669 my ($w, $h) = ($self->{w}, $self->{h}); 1692 my ($w, $h) = ($self->{w}, $self->{h});
1670 1693
1671 if ($self->{vertical}) { 1694 if ($self->{vertical}) {
1672 glRotate 90, 0, 0, 1; 1695 glRotate 90, 0, 0, 1;
1674 1697
1675 ($w, $h) = ($h, $w); 1698 ($w, $h) = ($h, $w);
1676 } 1699 }
1677 1700
1678 my $ycut = $self->{val} / ($self->{max_val} || 1); 1701 my $ycut = $self->{val} / ($self->{max_val} || 1);
1679 $ycut = 1 if $self->{val} > $self->{max_val};
1680 1702
1681 my $t1 = $tex->[0]; 1703 my $ycut1 = max 0, min 1, $ycut;
1682 my $t2 = $tex->[1]; 1704 my $ycut2 = max 0, min 1, $ycut - 1;
1705
1706 my $h1 = $self->{h} * (1 - $ycut1);
1707 my $h2 = $self->{h} * (1 - $ycut2);
1683 1708
1684 glEnable GL_BLEND; 1709 glEnable GL_BLEND;
1685 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1686 glEnable GL_TEXTURE_2D; 1711 glEnable GL_TEXTURE_2D;
1687 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1712 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1688 1713
1689 my $h1 = $self->{h} - $ycut * $self->{h};
1690 my $h2 = $ycut * $self->{h};
1691
1692 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1714 glBindTexture GL_TEXTURE_2D, $t1->{name};
1693 glBegin GL_QUADS; 1715 glBegin GL_QUADS;
1694 glTexCoord 0 , 0; glVertex 0 , 0; 1716 glTexCoord 0 , 0; glVertex 0 , 0;
1695 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1717 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1696 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1718 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1697 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1719 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1698 glEnd; 1720 glEnd;
1699 1721
1722 my $ycut1 = List::Util::min 1, $ycut;
1700 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1723 glBindTexture GL_TEXTURE_2D, $t2->{name};
1701 glBegin GL_QUADS; 1724 glBegin GL_QUADS;
1702 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1725 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1703 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1726 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1704 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1727 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1705 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1728 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1706 glEnd; 1729 glEnd;
1730
1731 if ($t3) {
1732 glBindTexture GL_TEXTURE_2D, $t3->{name};
1733 glBegin GL_QUADS;
1734 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1735 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1736 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1737 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1738 glEnd;
1739 }
1707 1740
1708 glDisable GL_BLEND; 1741 glDisable GL_BLEND;
1709 glDisable GL_TEXTURE_2D; 1742 glDisable GL_TEXTURE_2D;
1710} 1743}
1711 1744
1718sub new { 1751sub new {
1719 my ($class, %arg) = @_; 1752 my ($class, %arg) = @_;
1720 1753
1721 my $self = $class->SUPER::new ( 1754 my $self = $class->SUPER::new (
1722 tooltip => $arg{type}, 1755 tooltip => $arg{type},
1756 can_events => 1,
1757 can_hover => 1,
1723 %arg, 1758 %arg,
1724 ); 1759 );
1725 1760
1726 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1761 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1727 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1762 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1728 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999"); 1763 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1729 1764
1730 $self 1765 $self
1731} 1766}
1732 1767
1900sub new { 1935sub new {
1901 my $class = shift; 1936 my $class = shift;
1902 1937
1903 my $self = $class->SUPER::new ( 1938 my $self = $class->SUPER::new (
1904 fontsize => 1, 1939 fontsize => 1,
1940 #font => default_font
1905 @_, 1941 @_,
1906 1942
1907 layout => (new CFClient::Layout), 1943 layout => (new CFClient::Layout),
1908 par => [], 1944 par => [],
1909 height => 0, 1945 height => 0,
1949sub size_allocate { 1985sub size_allocate {
1950 my ($self, $w, $h) = @_; 1986 my ($self, $w, $h) = @_;
1951 1987
1952 $self->SUPER::size_allocate ($w, $h); 1988 $self->SUPER::size_allocate ($w, $h);
1953 1989
1990 $self->{layout}->set_font ($self->{font}) if $self->{font};
1954 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1991 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1955 $self->{layout}->set_width ($self->{children}[0]{w}); 1992 $self->{layout}->set_width ($self->{children}[0]{w});
1956 1993
1957 $self->reflow; 1994 $self->reflow;
1958} 1995}
2010 my $y1 = $top + $self->{h}; 2047 my $y1 = $top + $self->{h};
2011 2048
2012 my $y = 0; 2049 my $y = 0;
2013 2050
2014 my $layout = $self->{layout}; 2051 my $layout = $self->{layout};
2052
2053 $layout->set_font ($self->{font}) if $self->{font};
2015 2054
2016 for my $par (@{$self->{par}}) { 2055 for my $par (@{$self->{par}}) {
2017 my $h = $par->[0]; 2056 my $h = $par->[0];
2018 2057
2019 if ($y0 < $y + $h && $y < $y1) { 2058 if ($y0 < $y + $h && $y < $y1) {
2129 $self->emit (changed => $self->{state}); 2168 $self->emit (changed => $self->{state});
2130} 2169}
2131 2170
2132############################################################################# 2171#############################################################################
2133 2172
2173package CFClient::UI::Tooltip;
2174
2175our @ISA = CFClient::UI::Bin::;
2176
2177use CFClient::OpenGL;
2178
2179sub new {
2180 my $class = shift;
2181
2182 $class->SUPER::new (
2183 @_,
2184 can_events => 0,
2185 )
2186}
2187
2188sub set_text {
2189 my ($self, $text) = @_;
2190
2191 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2192 $self->{label}->set_text ($text);
2193 $self->add ($self->{label});
2194}
2195
2196sub size_request {
2197 my ($self) = @_;
2198
2199 $self->child->set_max_size ($::WIDTH * 0.3);
2200
2201 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2202
2203 ($w + 4, $h + 4)
2204}
2205
2206sub _draw {
2207 my ($self) = @_;
2208
2209 glPushMatrix;
2210 glTranslate 0.375, 0.375;
2211
2212 my ($w, $h) = @$self{qw(w h)};
2213
2214 glColor 1, 0.8, 0.4;
2215 glBegin GL_QUADS;
2216 glVertex 0 , 0;
2217 glVertex 0 , $h;
2218 glVertex $w, $h;
2219 glVertex $w, 0;
2220 glEnd;
2221
2222 glColor 0, 0, 0;
2223 glBegin GL_LINE_LOOP;
2224 glVertex 0 , 0;
2225 glVertex 0 , $h;
2226 glVertex $w, $h;
2227 glVertex $w, 0;
2228 glEnd;
2229
2230 glPopMatrix;
2231
2232 glTranslate 2, 2;
2233 $self->SUPER::_draw;
2234}
2235
2236#############################################################################
2237
2134package CFClient::UI::Root; 2238package CFClient::UI::Root;
2135 2239
2136our @ISA = CFClient::UI::Container::; 2240our @ISA = CFClient::UI::Container::;
2137 2241
2138use CFClient::OpenGL; 2242use CFClient::OpenGL;
2213############################################################################# 2317#############################################################################
2214 2318
2215package CFClient::UI; 2319package CFClient::UI;
2216 2320
2217$ROOT = new CFClient::UI::Root; 2321$ROOT = new CFClient::UI::Root;
2218$TOOLTIP = new CFClient::UI::Label fontsize => 0.8, can_events => 0; 2322$TOOLTIP = new CFClient::UI::Tooltip;
2219 2323
22201 23241
2221 2325

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines