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.150 by elmex, Sun Apr 23 00:08:29 2006 UTC vs.
Revision 1.158 by root, Mon Apr 24 02:41:48 2006 UTC

8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $ROOT; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_text ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
28 if (!$BUTTON_STATE) { 55 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 56 my $widget = $ROOT->find_widget ($x, $y);
30 57
31 $GRAB = $widget; 58 $GRAB = $widget;
32 $GRAB->update if $GRAB; 59 $GRAB->update if $GRAB;
60
61 check_tooltip;
33 } 62 }
34 63
35 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 65
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 78
50 if (!$BUTTON_STATE) { 79 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 80 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 81 $grab->update if $grab;
53 $GRAB->update if $GRAB; 82 $GRAB->update if $GRAB;
83
84 check_tooltip;
54 } 85 }
55} 86}
56 87
57sub feed_sdl_motion_event { 88sub feed_sdl_motion_event {
58 my ($ev) = @_; 89 my ($ev) = @_;
63 if ($widget != $HOVER) { 94 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 95 my $hover = $HOVER; $HOVER = $widget;
65 96
66 $hover->update if $hover && $hover->{can_hover}; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 98 $HOVER->update if $HOVER && $HOVER->{can_hover};
99
100 check_tooltip;
68 } 101 }
69 102
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 104}
72 105
135 $self->{z} = $z if defined $z; 168 $self->{z} = $z if defined $z;
136 169
137 $self->update; 170 $self->update;
138} 171}
139 172
140sub needs_redraw { 173sub set_size {
141 0 174 my ($self, $w, $h) = @_;
175
176 $self->{user_w} = $w;
177 $self->{user_h} = $h;
178
179 $self->check_size;
142} 180}
143 181
144sub size_request { 182sub size_request {
145 require Carp; 183 require Carp;
146 Carp::confess "size_request is abstract"; 184 Carp::confess "size_request is abstract";
176 } 214 }
177} 215}
178 216
179sub size_allocate { 217sub size_allocate {
180 # nothing to be done 218 # nothing to be done
219}
220
221sub set_max_size {
222 my ($self, $w, $h) = @_;
223
224 delete $self->{max_w}; $self->{max_w} = $w if $w;
225 delete $self->{max_h}; $self->{max_h} = $h if $h;
181} 226}
182 227
183# return top left coordinates 228# return top left coordinates
184sub _topleft { 229sub _topleft {
185 my ($self, $x, $y) = @_; 230 my ($self, $x, $y) = @_;
297} 342}
298 343
299sub check_size { 344sub check_size {
300 my ($self) = @_; 345 my ($self) = @_;
301 346
302 return unless $self->{parent}; 347 $self->{parent}
348 or return 1;
303 349
304 my ($w, $h) = $self->size_request; 350 my ($w, $h) = $self->{user_w} && $self->{user_h}
351 ? @$self{qw(user_w user_h)}
352 : $self->size_request;
305 353
306 if ($w != $self->{req_w} || $h != $self->{req_h}) { 354 if ($w != $self->{req_w} || $h != $self->{req_h}) {
307 $self->{req_w} = $w; 355 $self->{req_w} = $w;
308 $self->{req_h} = $h; 356 $self->{req_h} = $h;
309 357
310 $self->{parent}->check_size; 358 $self->{parent}->check_size
359 or $self->size_allocate (
360 (List::Util::max $self->{w}, $w),
361 (List::Util::max $self->{h}, $h),
362 );
363
364 1
365 } else {
366 0
311 } 367 }
312} 368}
313 369
314sub update { 370sub update {
315 my ($self) = @_; 371 my ($self) = @_;
433 delete $child->{parent}; 489 delete $child->{parent};
434 490
435 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 491 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
436 492
437 $self->check_size; 493 $self->check_size;
494 $self->update;
438} 495}
439 496
440sub find_widget { 497sub find_widget {
441 my ($self, $x, $y) = @_; 498 my ($self, $x, $y) = @_;
442 499
602 border => 0.8, 659 border => 0.8,
603 @_ 660 @_
604 ); 661 );
605 662
606 $self 663 $self
607}
608
609sub set_size {
610 my ($self, $w, $h) = @_;
611 $self->{req_w} = $w;
612 $self->{req_h} = $h;
613 $self->check_size;
614}
615
616sub size_request {
617 my ($self) = @_;
618 ($self->{req_w}, $self->{req_h})
619}
620
621sub size_allocate {
622 my ($self, $w, $h) = @_;
623 $self->{w} = $w;
624 $self->{h} = $h;
625 $self->child->configure (0, 0, $w, $h);
626} 664}
627 665
628sub _draw { 666sub _draw {
629 my ($self) = @_; 667 my ($self) = @_;
630 668
687 int $_[0]{border} * $::FONTSIZE 725 int $_[0]{border} * $::FONTSIZE
688} 726}
689 727
690sub size_request { 728sub size_request {
691 my ($self) = @_; 729 my ($self) = @_;
692
693 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
694 730
695 my ($w, $h) = $self->SUPER::size_request; 731 my ($w, $h) = $self->SUPER::size_request;
696 732
697 ( 733 (
698 $w + $self->border * 2, 734 $w + $self->border * 2,
1065sub new { 1101sub new {
1066 my ($class, %arg) = @_; 1102 my ($class, %arg) = @_;
1067 1103
1068 my $self = $class->SUPER::new ( 1104 my $self = $class->SUPER::new (
1069 fg => [1, 1, 1], 1105 fg => [1, 1, 1],
1106 #font => default_font
1070 fontsize => 1, 1107 fontsize => 1,
1071 text => "", 1108 text => "",
1072 align => -1, 1109 align => -1,
1073 valign => -1, 1110 valign => -1,
1074 padding => 2, 1111 padding => 2,
1120} 1157}
1121 1158
1122sub size_request { 1159sub size_request {
1123 my ($self) = @_; 1160 my ($self) = @_;
1124 1161
1125 $self->{layout}->set_width; 1162 $self->{layout}->set_font ($self->{font}) if $self->{font};
1163 $self->{layout}->set_width ($self->{max_w} || -1);
1126 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1164 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1127 1165
1128 my ($w, $h) = $self->{layout}->size; 1166 my ($w, $h) = $self->{layout}->size;
1129 1167
1130 if (exists $self->{template}) { 1168 if (exists $self->{template}) {
1169 $self->{template}->set_font ($self->{font}) if $self->{font};
1131 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1170 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1132 1171
1133 my ($w2, $h2) = $self->{template}->size; 1172 my ($w2, $h2) = $self->{template}->size;
1134 1173
1135 $w = List::Util::max $w, $w2; 1174 $w = List::Util::max $w, $w2;
1150 1189
1151sub set_fontsize { 1190sub set_fontsize {
1152 my ($self, $fontsize) = @_; 1191 my ($self, $fontsize) = @_;
1153 1192
1154 $self->{fontsize} = $fontsize; 1193 $self->{fontsize} = $fontsize;
1194 delete $self->{texture};
1155 $self->check_size; 1195 $self->check_size;
1196 $self->update;
1156} 1197}
1157 1198
1158sub _draw { 1199sub _draw {
1159 my ($self) = @_; 1200 my ($self) = @_;
1160 1201
1161 my $tex = $self->{texture} ||= do { 1202 my $tex = $self->{texture} ||= do {
1203 $self->{layout}->set_font ($self->{font}) if $self->{font};
1162 $self->{layout}->set_width ($self->{w}); 1204 $self->{layout}->set_width ($self->{w});
1163 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1205 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1164 new_from_layout CFClient::Texture $self->{layout} 1206 new_from_layout CFClient::Texture $self->{layout}
1165 }; 1207 };
1166 1208
1569 1611
1570package CFClient::UI::VGauge; 1612package CFClient::UI::VGauge;
1571 1613
1572our @ISA = CFClient::UI::Base::; 1614our @ISA = CFClient::UI::Base::;
1573 1615
1616use List::Util qw(min max);
1617
1574use CFClient::OpenGL; 1618use CFClient::OpenGL;
1575 1619
1576my %tex = ( 1620my %tex = (
1577 food => [ 1621 food => [
1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1579 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ 1623 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1580 ], 1624 ],
1581 grace => [ 1625 grace => [
1582 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1583 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/ 1627 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1584 ], 1628 ],
1585 hp => [ 1629 hp => [
1586 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1587 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ 1631 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1588 ], 1632 ],
1589 mana => [ 1633 mana => [
1590 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1634 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1591 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/ 1635 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1592 ], 1636 ],
1593); 1637);
1594 1638
1595# eg. VGauge->new (gauge => 'food'), default gauge: food 1639# eg. VGauge->new (gauge => 'food'), default gauge: food
1596sub new { 1640sub new {
1634 1678
1635sub _draw { 1679sub _draw {
1636 my ($self) = @_; 1680 my ($self) = @_;
1637 1681
1638 my $tex = $tex{$self->{type}}; 1682 my $tex = $tex{$self->{type}};
1683 my ($t1, $t2, $t3) = @$tex;
1639 1684
1640 my ($w, $h) = ($self->{w}, $self->{h}); 1685 my ($w, $h) = ($self->{w}, $self->{h});
1641 1686
1642 if ($self->{vertical}) { 1687 if ($self->{vertical}) {
1643 glRotate 90, 0, 0, 1; 1688 glRotate 90, 0, 0, 1;
1645 1690
1646 ($w, $h) = ($h, $w); 1691 ($w, $h) = ($h, $w);
1647 } 1692 }
1648 1693
1649 my $ycut = $self->{val} / ($self->{max_val} || 1); 1694 my $ycut = $self->{val} / ($self->{max_val} || 1);
1650 $ycut = 1 if $self->{val} > $self->{max_val};
1651 1695
1652 my $t1 = $tex->[0]; 1696 my $ycut1 = max 0, min 1, $ycut;
1653 my $t2 = $tex->[1]; 1697 my $ycut2 = max 0, min 1, $ycut - 1;
1698
1699 my $h1 = $self->{h} * (1 - $ycut1);
1700 my $h2 = $self->{h} * (1 - $ycut2);
1654 1701
1655 glEnable GL_BLEND; 1702 glEnable GL_BLEND;
1656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1703 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1657 glEnable GL_TEXTURE_2D; 1704 glEnable GL_TEXTURE_2D;
1658 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1659 1706
1660 my $h1 = $self->{h} - $ycut * $self->{h};
1661 my $h2 = $ycut * $self->{h};
1662
1663 glBindTexture GL_TEXTURE_2D, $t1->{name}; 1707 glBindTexture GL_TEXTURE_2D, $t1->{name};
1664 glBegin GL_QUADS; 1708 glBegin GL_QUADS;
1665 glTexCoord 0 , 0; glVertex 0 , 0; 1709 glTexCoord 0 , 0; glVertex 0 , 0;
1666 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1; 1710 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1667 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1; 1711 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1668 glTexCoord $t1->{s}, 0; glVertex $w, 0; 1712 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1669 glEnd; 1713 glEnd;
1670 1714
1715 my $ycut1 = List::Util::min 1, $ycut;
1671 glBindTexture GL_TEXTURE_2D, $t2->{name}; 1716 glBindTexture GL_TEXTURE_2D, $t2->{name};
1672 glBegin GL_QUADS; 1717 glBegin GL_QUADS;
1673 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1; 1718 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1674 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2; 1719 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1675 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2; 1720 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1676 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1; 1721 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1677 glEnd; 1722 glEnd;
1723
1724 if ($t3) {
1725 glBindTexture GL_TEXTURE_2D, $t3->{name};
1726 glBegin GL_QUADS;
1727 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1728 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1729 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1730 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1731 glEnd;
1732 }
1678 1733
1679 glDisable GL_BLEND; 1734 glDisable GL_BLEND;
1680 glDisable GL_TEXTURE_2D; 1735 glDisable GL_TEXTURE_2D;
1681} 1736}
1682 1737
1685package CFClient::UI::Gauge; 1740package CFClient::UI::Gauge;
1686 1741
1687our @ISA = CFClient::UI::VBox::; 1742our @ISA = CFClient::UI::VBox::;
1688 1743
1689sub new { 1744sub new {
1690 my ($class, %arg) = shift; 1745 my ($class, %arg) = @_;
1691 1746
1692 my $self = $class->SUPER::new ( 1747 my $self = $class->SUPER::new (
1693 @_, 1748 tooltip => $arg{type},
1749 %arg,
1694 ); 1750 );
1695 1751
1696 $self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"; 1752 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1);
1697 $self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1698 $self->add ($self->{value});
1699 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1753 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1700 $self->add ($self->{max}); 1754 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1701 1755
1702 $self 1756 $self
1703}
1704
1705sub size_request {
1706 my ($self) = @_;
1707 (($self->{max}->size_request)[0], 0)
1708} 1757}
1709 1758
1710sub set_fontsize { 1759sub set_fontsize {
1711 my ($self, $fsize) = @_; 1760 my ($self, $fsize) = @_;
1712 1761
1877sub new { 1926sub new {
1878 my $class = shift; 1927 my $class = shift;
1879 1928
1880 my $self = $class->SUPER::new ( 1929 my $self = $class->SUPER::new (
1881 fontsize => 1, 1930 fontsize => 1,
1931 #font => default_font
1882 @_, 1932 @_,
1883 1933
1884 layout => (new CFClient::Layout), 1934 layout => (new CFClient::Layout),
1885 par => [], 1935 par => [],
1886 height => 0, 1936 height => 0,
1926sub size_allocate { 1976sub size_allocate {
1927 my ($self, $w, $h) = @_; 1977 my ($self, $w, $h) = @_;
1928 1978
1929 $self->SUPER::size_allocate ($w, $h); 1979 $self->SUPER::size_allocate ($w, $h);
1930 1980
1981 $self->{layout}->set_font ($self->{font}) if $self->{font};
1931 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1982 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1932 $self->{layout}->set_width ($self->{children}[0]{w}); 1983 $self->{layout}->set_width ($self->{children}[0]{w});
1933 1984
1934 $self->reflow; 1985 $self->reflow;
1935} 1986}
1987 my $y1 = $top + $self->{h}; 2038 my $y1 = $top + $self->{h};
1988 2039
1989 my $y = 0; 2040 my $y = 0;
1990 2041
1991 my $layout = $self->{layout}; 2042 my $layout = $self->{layout};
2043
2044 $layout->set_font ($self->{font}) if $self->{font};
1992 2045
1993 for my $par (@{$self->{par}}) { 2046 for my $par (@{$self->{par}}) {
1994 my $h = $par->[0]; 2047 my $h = $par->[0];
1995 2048
1996 if ($y0 < $y + $h && $y < $y1) { 2049 if ($y0 < $y + $h && $y < $y1) {
2106 $self->emit (changed => $self->{state}); 2159 $self->emit (changed => $self->{state});
2107} 2160}
2108 2161
2109############################################################################# 2162#############################################################################
2110 2163
2164package CFClient::UI::Tooltip;
2165
2166our @ISA = CFClient::UI::Bin::;
2167
2168use CFClient::OpenGL;
2169
2170sub new {
2171 my $class = shift;
2172
2173 $class->SUPER::new (
2174 @_,
2175 can_events => 0,
2176 )
2177}
2178
2179sub set_text {
2180 my ($self, $text) = @_;
2181
2182 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2183 $self->{label}->set_text ($text);
2184 $self->add ($self->{label});
2185}
2186
2187sub size_request {
2188 my ($self) = @_;
2189
2190 $self->child->set_max_size ($::WIDTH * 0.2);
2191
2192 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2193
2194 ($w + 4, $h + 4)
2195}
2196
2197sub _draw {
2198 my ($self) = @_;
2199
2200 glPushMatrix;
2201 glTranslate 0.375, 0.375;
2202
2203 my ($w, $h) = @$self{qw(w h)};
2204
2205 glColor 1, 0.8, 0.4;
2206 glBegin GL_QUADS;
2207 glVertex 0 , 0;
2208 glVertex 0 , $h;
2209 glVertex $w, $h;
2210 glVertex $w, 0;
2211 glEnd;
2212
2213 glColor 0, 0, 0;
2214 glBegin GL_LINE_LOOP;
2215 glVertex 0 , 0;
2216 glVertex 0 , $h;
2217 glVertex $w, $h;
2218 glVertex $w, 0;
2219 glEnd;
2220
2221 glPopMatrix;
2222
2223 glTranslate 2, 2;
2224 $self->SUPER::_draw;
2225}
2226
2227#############################################################################
2228
2111package CFClient::UI::Root; 2229package CFClient::UI::Root;
2112 2230
2113our @ISA = CFClient::UI::Container::; 2231our @ISA = CFClient::UI::Container::;
2114 2232
2115use CFClient::OpenGL; 2233use CFClient::OpenGL;
2190############################################################################# 2308#############################################################################
2191 2309
2192package CFClient::UI; 2310package CFClient::UI;
2193 2311
2194$ROOT = new CFClient::UI::Root; 2312$ROOT = new CFClient::UI::Root;
2313$TOOLTIP = new CFClient::UI::Tooltip;
2195 2314
21961 23151
2197 2316

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines