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.153 by root, Sun Apr 23 02:14:33 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;
1188 1205
1189sub _draw { 1206sub _draw {
1190 my ($self) = @_; 1207 my ($self) = @_;
1191 1208
1192 my $tex = $self->{texture} ||= do { 1209 my $tex = $self->{texture} ||= do {
1210 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{w}); 1211 $self->{layout}->set_width ($self->{w});
1194 $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);
1195 new_from_layout CFClient::Texture $self->{layout} 1213 new_from_layout CFClient::Texture $self->{layout}
1196 }; 1214 };
1197 1215
1600 1618
1601package CFClient::UI::VGauge; 1619package CFClient::UI::VGauge;
1602 1620
1603our @ISA = CFClient::UI::Base::; 1621our @ISA = CFClient::UI::Base::;
1604 1622
1623use List::Util qw(min max);
1624
1605use CFClient::OpenGL; 1625use CFClient::OpenGL;
1606 1626
1607my %tex = ( 1627my %tex = (
1608 food => [ 1628 food => [
1609 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1629 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1610 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1630 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1611 ], 1631 ],
1612 grace => [ 1632 grace => [
1613 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1633 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1614 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/
1615 ], 1635 ],
1616 hp => [ 1636 hp => [
1617 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1637 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1618 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1638 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1619 ], 1639 ],
1620 mana => [ 1640 mana => [
1621 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1641 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1622 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/
1623 ], 1643 ],
1624); 1644);
1625 1645
1626# eg. VGauge->new (gauge => 'food'), default gauge: food 1646# eg. VGauge->new (gauge => 'food'), default gauge: food
1627sub new { 1647sub new {
1665 1685
1666sub _draw { 1686sub _draw {
1667 my ($self) = @_; 1687 my ($self) = @_;
1668 1688
1669 my $tex = $tex{$self->{type}}; 1689 my $tex = $tex{$self->{type}};
1690 my ($t1, $t2, $t3) = @$tex;
1670 1691
1671 my ($w, $h) = ($self->{w}, $self->{h}); 1692 my ($w, $h) = ($self->{w}, $self->{h});
1672 1693
1673 if ($self->{vertical}) { 1694 if ($self->{vertical}) {
1674 glRotate 90, 0, 0, 1; 1695 glRotate 90, 0, 0, 1;
1676 1697
1677 ($w, $h) = ($h, $w); 1698 ($w, $h) = ($h, $w);
1678 } 1699 }
1679 1700
1680 my $ycut = $self->{val} / ($self->{max_val} || 1); 1701 my $ycut = $self->{val} / ($self->{max_val} || 1);
1681 $ycut = 1 if $self->{val} > $self->{max_val};
1682 1702
1683 my $t1 = $tex->[0]; 1703 my $ycut1 = max 0, min 1, $ycut;
1684 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);
1685 1708
1686 glEnable GL_BLEND; 1709 glEnable GL_BLEND;
1687 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1710 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1688 glEnable GL_TEXTURE_2D; 1711 glEnable GL_TEXTURE_2D;
1689 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1712 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1690 1713
1691 my $h1 = $self->{h} - $ycut * $self->{h};
1692 my $h2 = $ycut * $self->{h};
1693
1694 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1714 glBindTexture GL_TEXTURE_2D, $t1->{name};
1695 glBegin GL_QUADS; 1715 glBegin GL_QUADS;
1696 glTexCoord 0 , 0; glVertex 0 , 0; 1716 glTexCoord 0 , 0; glVertex 0 , 0;
1697 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1717 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1698 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1718 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1699 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1719 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1700 glEnd; 1720 glEnd;
1701 1721
1722 my $ycut1 = List::Util::min 1, $ycut;
1702 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1723 glBindTexture GL_TEXTURE_2D, $t2->{name};
1703 glBegin GL_QUADS; 1724 glBegin GL_QUADS;
1704 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1725 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1705 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1726 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1706 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1727 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1707 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1728 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1708 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 }
1709 1740
1710 glDisable GL_BLEND; 1741 glDisable GL_BLEND;
1711 glDisable GL_TEXTURE_2D; 1742 glDisable GL_TEXTURE_2D;
1712} 1743}
1713 1744
1720sub new { 1751sub new {
1721 my ($class, %arg) = @_; 1752 my ($class, %arg) = @_;
1722 1753
1723 my $self = $class->SUPER::new ( 1754 my $self = $class->SUPER::new (
1724 tooltip => $arg{type}, 1755 tooltip => $arg{type},
1756 can_events => 1,
1757 can_hover => 1,
1725 %arg, 1758 %arg,
1726 ); 1759 );
1727 1760
1728 $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");
1729 $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);
1730 $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");
1731 1764
1732 $self 1765 $self
1733} 1766}
1734 1767
1902sub new { 1935sub new {
1903 my $class = shift; 1936 my $class = shift;
1904 1937
1905 my $self = $class->SUPER::new ( 1938 my $self = $class->SUPER::new (
1906 fontsize => 1, 1939 fontsize => 1,
1940 #font => default_font
1907 @_, 1941 @_,
1908 1942
1909 layout => (new CFClient::Layout), 1943 layout => (new CFClient::Layout),
1910 par => [], 1944 par => [],
1911 height => 0, 1945 height => 0,
1951sub size_allocate { 1985sub size_allocate {
1952 my ($self, $w, $h) = @_; 1986 my ($self, $w, $h) = @_;
1953 1987
1954 $self->SUPER::size_allocate ($w, $h); 1988 $self->SUPER::size_allocate ($w, $h);
1955 1989
1990 $self->{layout}->set_font ($self->{font}) if $self->{font};
1956 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1991 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1957 $self->{layout}->set_width ($self->{children}[0]{w}); 1992 $self->{layout}->set_width ($self->{children}[0]{w});
1958 1993
1959 $self->reflow; 1994 $self->reflow;
1960} 1995}
2012 my $y1 = $top + $self->{h}; 2047 my $y1 = $top + $self->{h};
2013 2048
2014 my $y = 0; 2049 my $y = 0;
2015 2050
2016 my $layout = $self->{layout}; 2051 my $layout = $self->{layout};
2052
2053 $layout->set_font ($self->{font}) if $self->{font};
2017 2054
2018 for my $par (@{$self->{par}}) { 2055 for my $par (@{$self->{par}}) {
2019 my $h = $par->[0]; 2056 my $h = $par->[0];
2020 2057
2021 if ($y0 < $y + $h && $y < $y1) { 2058 if ($y0 < $y + $h && $y < $y1) {
2149} 2186}
2150 2187
2151sub set_text { 2188sub set_text {
2152 my ($self, $text) = @_; 2189 my ($self, $text) = @_;
2153 2190
2154 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8; 2191 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2155 $self->{label}->set_text ($text); 2192 $self->{label}->set_text ($text);
2156 $self->add ($self->{label}); 2193 $self->add ($self->{label});
2157} 2194}
2158 2195
2159sub size_request { 2196sub size_request {
2160 my ($self) = @_; 2197 my ($self) = @_;
2161 2198
2199 $self->child->set_max_size ($::WIDTH * 0.3);
2200
2162 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2201 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2163 2202
2164 $w = List::Util::min $::WIDTH * 0.2, $w; 2203 ($w + 4, $h + 4)
2204}
2165 2205
2166 ($w, $h) 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;
2167} 2234}
2168 2235
2169############################################################################# 2236#############################################################################
2170 2237
2171package CFClient::UI::Root; 2238package CFClient::UI::Root;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines