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.157 by root, Sun Apr 23 21:47:32 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
176 } 209 }
177} 210}
178 211
179sub size_allocate { 212sub size_allocate {
180 # nothing to be done 213 # nothing to be done
214}
215
216sub set_max_size {
217 my ($self, $w, $h) = @_;
218
219 delete $self->{max_w}; $self->{max_w} = $w if $w;
220 delete $self->{max_h}; $self->{max_h} = $h if $h;
181} 221}
182 222
183# return top left coordinates 223# return top left coordinates
184sub _topleft { 224sub _topleft {
185 my ($self, $x, $y) = @_; 225 my ($self, $x, $y) = @_;
433 delete $child->{parent}; 473 delete $child->{parent};
434 474
435 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 475 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
436 476
437 $self->check_size; 477 $self->check_size;
478 $self->update;
438} 479}
439 480
440sub find_widget { 481sub find_widget {
441 my ($self, $x, $y) = @_; 482 my ($self, $x, $y) = @_;
442 483
1065sub new { 1106sub new {
1066 my ($class, %arg) = @_; 1107 my ($class, %arg) = @_;
1067 1108
1068 my $self = $class->SUPER::new ( 1109 my $self = $class->SUPER::new (
1069 fg => [1, 1, 1], 1110 fg => [1, 1, 1],
1111 #font => default_font
1070 fontsize => 1, 1112 fontsize => 1,
1071 text => "", 1113 text => "",
1072 align => -1, 1114 align => -1,
1073 valign => -1, 1115 valign => -1,
1074 padding => 2, 1116 padding => 2,
1120} 1162}
1121 1163
1122sub size_request { 1164sub size_request {
1123 my ($self) = @_; 1165 my ($self) = @_;
1124 1166
1125 $self->{layout}->set_width; 1167 $self->{layout}->set_font ($self->{font}) if $self->{font};
1168 $self->{layout}->set_width ($self->{max_w} || -1);
1126 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1169 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1127 1170
1128 my ($w, $h) = $self->{layout}->size; 1171 my ($w, $h) = $self->{layout}->size;
1129 1172
1130 if (exists $self->{template}) { 1173 if (exists $self->{template}) {
1174 $self->{template}->set_font ($self->{font}) if $self->{font};
1131 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1175 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1132 1176
1133 my ($w2, $h2) = $self->{template}->size; 1177 my ($w2, $h2) = $self->{template}->size;
1134 1178
1135 $w = List::Util::max $w, $w2; 1179 $w = List::Util::max $w, $w2;
1150 1194
1151sub set_fontsize { 1195sub set_fontsize {
1152 my ($self, $fontsize) = @_; 1196 my ($self, $fontsize) = @_;
1153 1197
1154 $self->{fontsize} = $fontsize; 1198 $self->{fontsize} = $fontsize;
1199 delete $self->{texture};
1155 $self->check_size; 1200 $self->check_size;
1201 $self->update;
1156} 1202}
1157 1203
1158sub _draw { 1204sub _draw {
1159 my ($self) = @_; 1205 my ($self) = @_;
1160 1206
1161 my $tex = $self->{texture} ||= do { 1207 my $tex = $self->{texture} ||= do {
1208 $self->{layout}->set_font ($self->{font}) if $self->{font};
1162 $self->{layout}->set_width ($self->{w}); 1209 $self->{layout}->set_width ($self->{w});
1163 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1210 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1164 new_from_layout CFClient::Texture $self->{layout} 1211 new_from_layout CFClient::Texture $self->{layout}
1165 }; 1212 };
1166 1213
1685package CFClient::UI::Gauge; 1732package CFClient::UI::Gauge;
1686 1733
1687our @ISA = CFClient::UI::VBox::; 1734our @ISA = CFClient::UI::VBox::;
1688 1735
1689sub new { 1736sub new {
1690 my ($class, %arg) = shift; 1737 my ($class, %arg) = @_;
1691 1738
1692 my $self = $class->SUPER::new ( 1739 my $self = $class->SUPER::new (
1693 @_, 1740 tooltip => $arg{type},
1741 %arg,
1694 ); 1742 );
1695 1743
1696 $self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"; 1744 $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); 1745 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1700 $self->add ($self->{max}); 1746 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1701 1747
1702 $self 1748 $self
1703}
1704
1705sub size_request {
1706 my ($self) = @_;
1707 (($self->{max}->size_request)[0], 0)
1708} 1749}
1709 1750
1710sub set_fontsize { 1751sub set_fontsize {
1711 my ($self, $fsize) = @_; 1752 my ($self, $fsize) = @_;
1712 1753
1877sub new { 1918sub new {
1878 my $class = shift; 1919 my $class = shift;
1879 1920
1880 my $self = $class->SUPER::new ( 1921 my $self = $class->SUPER::new (
1881 fontsize => 1, 1922 fontsize => 1,
1923 #font => default_font
1882 @_, 1924 @_,
1883 1925
1884 layout => (new CFClient::Layout), 1926 layout => (new CFClient::Layout),
1885 par => [], 1927 par => [],
1886 height => 0, 1928 height => 0,
1926sub size_allocate { 1968sub size_allocate {
1927 my ($self, $w, $h) = @_; 1969 my ($self, $w, $h) = @_;
1928 1970
1929 $self->SUPER::size_allocate ($w, $h); 1971 $self->SUPER::size_allocate ($w, $h);
1930 1972
1973 $self->{layout}->set_font ($self->{font}) if $self->{font};
1931 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1974 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1932 $self->{layout}->set_width ($self->{children}[0]{w}); 1975 $self->{layout}->set_width ($self->{children}[0]{w});
1933 1976
1934 $self->reflow; 1977 $self->reflow;
1935} 1978}
1987 my $y1 = $top + $self->{h}; 2030 my $y1 = $top + $self->{h};
1988 2031
1989 my $y = 0; 2032 my $y = 0;
1990 2033
1991 my $layout = $self->{layout}; 2034 my $layout = $self->{layout};
2035
2036 $layout->set_font ($self->{font}) if $self->{font};
1992 2037
1993 for my $par (@{$self->{par}}) { 2038 for my $par (@{$self->{par}}) {
1994 my $h = $par->[0]; 2039 my $h = $par->[0];
1995 2040
1996 if ($y0 < $y + $h && $y < $y1) { 2041 if ($y0 < $y + $h && $y < $y1) {
2106 $self->emit (changed => $self->{state}); 2151 $self->emit (changed => $self->{state});
2107} 2152}
2108 2153
2109############################################################################# 2154#############################################################################
2110 2155
2156package CFClient::UI::Tooltip;
2157
2158our @ISA = CFClient::UI::Bin::;
2159
2160use CFClient::OpenGL;
2161
2162sub new {
2163 my $class = shift;
2164
2165 $class->SUPER::new (
2166 @_,
2167 can_events => 0,
2168 )
2169}
2170
2171sub set_text {
2172 my ($self, $text) = @_;
2173
2174 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2175 $self->{label}->set_text ($text);
2176 $self->add ($self->{label});
2177}
2178
2179sub size_request {
2180 my ($self) = @_;
2181
2182 $self->child->set_max_size ($::WIDTH * 0.2);
2183
2184 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2185
2186 ($w + 4, $h + 4)
2187}
2188
2189sub _draw {
2190 my ($self) = @_;
2191
2192 glPushMatrix;
2193 glTranslate 0.375, 0.375;
2194
2195 my ($w, $h) = @$self{qw(w h)};
2196
2197 glColor 1, 0.8, 0.4;
2198 glBegin GL_QUADS;
2199 glVertex 0 , 0;
2200 glVertex 0 , $h;
2201 glVertex $w, $h;
2202 glVertex $w, 0;
2203 glEnd;
2204
2205 glColor 0, 0, 0;
2206 glBegin GL_LINE_LOOP;
2207 glVertex 0 , 0;
2208 glVertex 0 , $h;
2209 glVertex $w, $h;
2210 glVertex $w, 0;
2211 glEnd;
2212
2213 glPopMatrix;
2214
2215 glTranslate 2, 2;
2216 $self->SUPER::_draw;
2217}
2218
2219#############################################################################
2220
2111package CFClient::UI::Root; 2221package CFClient::UI::Root;
2112 2222
2113our @ISA = CFClient::UI::Container::; 2223our @ISA = CFClient::UI::Container::;
2114 2224
2115use CFClient::OpenGL; 2225use CFClient::OpenGL;
2190############################################################################# 2300#############################################################################
2191 2301
2192package CFClient::UI; 2302package CFClient::UI;
2193 2303
2194$ROOT = new CFClient::UI::Root; 2304$ROOT = new CFClient::UI::Root;
2305$TOOLTIP = new CFClient::UI::Tooltip;
2195 2306
21961 23071
2197 2308

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines