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.149 by root, Sat Apr 22 23:11:32 2006 UTC vs.
Revision 1.155 by root, Sun Apr 23 02:22:54 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
96 129
97 my $self = bless { 130 my $self = bless {
98 x => 0, 131 x => 0,
99 y => 0, 132 y => 0,
100 z => 0, 133 z => 0,
134 can_events => 1,
101 @_ 135 @_
102 }, $class; 136 }, $class;
103 137
104 for (keys %$self) { 138 for (keys %$self) {
105 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
278} 312}
279 313
280sub find_widget { 314sub find_widget {
281 my ($self, $x, $y) = @_; 315 my ($self, $x, $y) = @_;
282 316
317 return () unless $self->{can_events};
318
283 return $self 319 return $self
284 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 320 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
285 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 321 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
286 322
287 () 323 ()
379 415
380package CFClient::UI::Empty; 416package CFClient::UI::Empty;
381 417
382our @ISA = CFClient::UI::Base::; 418our @ISA = CFClient::UI::Base::;
383 419
420sub new {
421 my ($class, %arg) = @_;
422 $class->SUPER::new (can_events => 0, %arg);
423}
424
384sub size_request { 425sub size_request {
385 (0, 0) 426 (0, 0)
386} 427}
387 428
388sub draw { } 429sub draw { }
396sub new { 437sub new {
397 my ($class, %arg) = @_; 438 my ($class, %arg) = @_;
398 439
399 my $children = delete $arg{children} || []; 440 my $children = delete $arg{children} || [];
400 441
401 my $self = $class->SUPER::new (children => [], %arg); 442 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
402 $self->add ($_) for @$children; 443 $self->add ($_) for @$children;
403 444
404 $self 445 $self
405} 446}
406 447
425 delete $child->{parent}; 466 delete $child->{parent};
426 467
427 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 468 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
428 469
429 $self->check_size; 470 $self->check_size;
471 $self->update;
430} 472}
431 473
432sub find_widget { 474sub find_widget {
433 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
434 476
660 702
661 my $self = $class->SUPER::new ( 703 my $self = $class->SUPER::new (
662 bg => [1, 1, 1, 1], 704 bg => [1, 1, 1, 1],
663 border_bg => [1, 1, 1, 1], 705 border_bg => [1, 1, 1, 1],
664 border => 0.8, 706 border => 0.8,
707 can_events => 1,
665 @_ 708 @_
666 ); 709 );
667 710
668 $self->{title} &&= new CFClient::UI::Label 711 $self->{title} &&= new CFClient::UI::Label
669 align => 0, 712 align => 0,
1062 text => "", 1105 text => "",
1063 align => -1, 1106 align => -1,
1064 valign => -1, 1107 valign => -1,
1065 padding => 2, 1108 padding => 2,
1066 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
1067 %arg 1111 %arg
1068 ); 1112 );
1069 1113
1070 if (exists $self->{template}) { 1114 if (exists $self->{template}) {
1071 my $layout = new CFClient::Layout; 1115 my $layout = new CFClient::Layout;
1140 1184
1141sub set_fontsize { 1185sub set_fontsize {
1142 my ($self, $fontsize) = @_; 1186 my ($self, $fontsize) = @_;
1143 1187
1144 $self->{fontsize} = $fontsize; 1188 $self->{fontsize} = $fontsize;
1189 delete $self->{texture};
1145 $self->check_size; 1190 $self->check_size;
1191 $self->update;
1146} 1192}
1147 1193
1148sub _draw { 1194sub _draw {
1149 my ($self) = @_; 1195 my ($self) = @_;
1150 1196
1196 active_bg => [1, 1, 1, 0.5], 1242 active_bg => [1, 1, 1, 0.5],
1197 active_fg => [0, 0, 0], 1243 active_fg => [0, 0, 0],
1198 can_hover => 1, 1244 can_hover => 1,
1199 can_focus => 1, 1245 can_focus => 1,
1200 valign => 0, 1246 valign => 0,
1247 can_events => 1,
1201 @_ 1248 @_
1202 ) 1249 )
1203} 1250}
1204 1251
1205sub _set_text { 1252sub _set_text {
1390 bg => [1, 1, 1, 0.2], 1437 bg => [1, 1, 1, 0.2],
1391 active_fg => [0, 0, 1], 1438 active_fg => [0, 0, 1],
1392 can_hover => 1, 1439 can_hover => 1,
1393 align => 0, 1440 align => 0,
1394 valign => 0, 1441 valign => 0,
1442 can_events => 1,
1395 @_ 1443 @_
1396 ) 1444 )
1397} 1445}
1398 1446
1399sub button_up { 1447sub button_up {
1504our %loaded_images; 1552our %loaded_images;
1505 1553
1506sub new { 1554sub new {
1507 my $class = shift; 1555 my $class = shift;
1508 1556
1509 my $self = $class->SUPER::new (@_); 1557 my $self = $class->SUPER::new (can_events => 0, @_);
1510 1558
1511 $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; 1559 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1512 1560
1513 $loaded_images{$self->{image}} ||= 1561 $loaded_images{$self->{image}} ||=
1514 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; 1562 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1673package CFClient::UI::Gauge; 1721package CFClient::UI::Gauge;
1674 1722
1675our @ISA = CFClient::UI::VBox::; 1723our @ISA = CFClient::UI::VBox::;
1676 1724
1677sub new { 1725sub new {
1678 my ($class, %arg) = shift; 1726 my ($class, %arg) = @_;
1679 1727
1680 my $self = $class->SUPER::new ( 1728 my $self = $class->SUPER::new (
1681 @_, 1729 tooltip => $arg{type},
1730 %arg,
1682 ); 1731 );
1683 1732
1684 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1733 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1685 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1); 1734 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1686 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999"); 1735 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1687 1736
1688 $self 1737 $self
1689} 1738}
1690 1739
1691sub set_fontsize { 1740sub set_fontsize {
2057 my $class = shift; 2106 my $class = shift;
2058 2107
2059 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
2060 state => 0, 2109 state => 0,
2061 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
2062 @_ 2112 @_
2063 ); 2113 );
2064 2114
2065 if ($self->{state}) { 2115 if ($self->{state}) {
2066 $self->{state} = 0; 2116 $self->{state} = 0;
2086 $self->emit (changed => $self->{state}); 2136 $self->emit (changed => $self->{state});
2087} 2137}
2088 2138
2089############################################################################# 2139#############################################################################
2090 2140
2141package CFClient::UI::Tooltip;
2142
2143our @ISA = CFClient::UI::Bin::;
2144
2145use CFClient::OpenGL;
2146
2147sub new {
2148 my $class = shift;
2149
2150 $class->SUPER::new (
2151 @_,
2152 can_events => 0,
2153 )
2154}
2155
2156sub set_text {
2157 my ($self, $text) = @_;
2158
2159 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2160 $self->{label}->set_text ($text);
2161 $self->add ($self->{label});
2162}
2163
2164sub size_request {
2165 my ($self) = @_;
2166
2167 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2168
2169 $w = List::Util::min $::WIDTH * 0.2, $w;
2170 $h = List::Util::max $::HEIGHT * 0.2, $h;
2171
2172 ($w + 4, $h + 4)
2173}
2174
2175sub _draw {
2176 my ($self) = @_;
2177
2178 glPushMatrix;
2179 glTranslate 0.375, 0.375;
2180
2181 my ($w, $h) = @$self{qw(w h)};
2182
2183 glColor 1, 0.8, 0.4;
2184 glBegin GL_QUADS;
2185 glVertex 0 , 0;
2186 glVertex 0 , $h;
2187 glVertex $w, $h;
2188 glVertex $w, 0;
2189 glEnd;
2190
2191 glColor 0, 0, 0;
2192 glBegin GL_LINE_LOOP;
2193 glVertex 0 , 0;
2194 glVertex 0 , $h;
2195 glVertex $w, $h;
2196 glVertex $w, 0;
2197 glEnd;
2198
2199 glPopMatrix;
2200
2201 glTranslate 2, 2;
2202 $self->SUPER::_draw;
2203}
2204
2205#############################################################################
2206
2091package CFClient::UI::Root; 2207package CFClient::UI::Root;
2092 2208
2093our @ISA = CFClient::UI::Container::; 2209our @ISA = CFClient::UI::Container::;
2094 2210
2095use CFClient::OpenGL; 2211use CFClient::OpenGL;
2170############################################################################# 2286#############################################################################
2171 2287
2172package CFClient::UI; 2288package CFClient::UI;
2173 2289
2174$ROOT = new CFClient::UI::Root; 2290$ROOT = new CFClient::UI::Root;
2291$TOOLTIP = new CFClient::UI::Tooltip;
2175 2292
21761 22931
2177 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines