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.162 by root, Mon Apr 24 06:05:35 2006 UTC vs.
Revision 1.173 by root, Tue Apr 25 10:30:48 2006 UTC

39 delete $TOOLTIP->{owner}; 39 delete $TOOLTIP->{owner};
40} 40}
41 41
42# class methods for events 42# class methods for events
43sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
44 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
45 if $FOCUS;
45} 46}
46 47
47sub feed_sdl_key_up_event { 48sub feed_sdl_key_up_event {
48 $FOCUS->key_up ($_[0]) if $FOCUS; 49 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
50 if $FOCUS;
49} 51}
50 52
51sub feed_sdl_button_down_event { 53sub feed_sdl_button_down_event {
52 my ($ev) = @_; 54 my ($ev) = @_;
53 my ($x, $y) = ($ev->{x}, $ev->{y}); 55 my ($x, $y) = ($ev->{x}, $ev->{y});
61 check_tooltip; 63 check_tooltip;
62 } 64 }
63 65
64 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 66 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
65 67
66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 68 if ($GRAB) {
69 ($x, $y) = $GRAB->coord2local ($x, $y);
70 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
71 }
67} 72}
68 73
69sub feed_sdl_button_up_event { 74sub feed_sdl_button_up_event {
70 my ($ev) = @_; 75 my ($ev) = @_;
71 my ($x, $y) = ($ev->{x}, $ev->{y}); 76 my ($x, $y) = ($ev->{x}, $ev->{y});
72 77
73 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 78 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
74 79
75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 80 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
76 81
77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 82 if ($GRAB) {
83 ($x, $y) = $GRAB->coord2local ($x, $y);
84 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
85 }
78 86
79 if (!$BUTTON_STATE) { 87 if (!$BUTTON_STATE) {
80 my $grab = $GRAB; undef $GRAB; 88 my $grab = $GRAB; undef $GRAB;
81 $grab->update if $grab; 89 $grab->update if $grab;
82 $GRAB->update if $GRAB; 90 $GRAB->update if $GRAB;
98 $HOVER->update if $HOVER && $HOVER->{can_hover}; 106 $HOVER->update if $HOVER && $HOVER->{can_hover};
99 107
100 check_tooltip; 108 check_tooltip;
101 } 109 }
102 110
103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 111 if ($HOVER) {
112 ($x, $y) = $HOVER->coord2local ($x, $y);
113 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
114 }
104} 115}
105 116
106# convert position array to integers 117# convert position array to integers
107sub harmonize { 118sub harmonize {
108 my ($vals) = @_; 119 my ($vals) = @_;
126 137
127sub new { 138sub new {
128 my $class = shift; 139 my $class = shift;
129 140
130 my $self = bless { 141 my $self = bless {
131 x => 0, 142 x => 0,
132 y => 0, 143 y => 0,
133 z => 0, 144 z => 0,
134 can_events => 1, 145 can_events => 1,
135 @_ 146 @_
136 }, $class; 147 }, $class;
137 148
138 for (keys %$self) { 149 for (keys %$self) {
160} 171}
161 172
162sub hide { 173sub hide {
163 my ($self) = @_; 174 my ($self) = @_;
164 175
165 return unless $self->{parent}; 176 undef $GRAB if $GRAB == $self;
177 undef $HOVER if $HOVER == $self;
166 178
167 $self->{parent}->remove ($self); 179 $self->{parent}->remove ($self)
180 if $self->{parent};
168} 181}
169 182
170sub move { 183sub move {
171 my ($self, $x, $y, $z) = @_; 184 my ($self, $x, $y, $z) = @_;
172 185
223 236
224sub size_allocate { 237sub size_allocate {
225 # nothing to be done 238 # nothing to be done
226} 239}
227 240
241sub children {
242}
243
244# call when resoltuion changes etc.
245sub reconfigure {
246 my ($self) = @_;
247
248 $_->reconfigure
249 for $self->children;
250
251 $_->check_size;
252}
253
228sub set_max_size { 254sub set_max_size {
229 my ($self, $w, $h) = @_; 255 my ($self, $w, $h) = @_;
230 256
231 delete $self->{max_w}; $self->{max_w} = $w if $w; 257 delete $self->{max_w}; $self->{max_w} = $w if $w;
232 delete $self->{max_h}; $self->{max_h} = $h if $h; 258 delete $self->{max_h}; $self->{max_h} = $h if $h;
233} 259}
234 260
235# return top left coordinates 261# return top left coordinates
236sub _topleft { 262sub _topleft {
237 my ($self, $x, $y) = @_; 263 my ($self, $x, $y) = @_;
264
265 $self->{parent}
266 or Carp::confess "no parent widget in _topleft\n";#d#
238 267
239 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 268 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
240} 269}
241 270
242# translate global coordinates to local coordinate system 271# translate global coordinates to local coordinate system
402} 431}
403 432
404sub emit { 433sub emit {
405 my ($self, $signal, @args) = @_; 434 my ($self, $signal, @args) = @_;
406 435
407 for my $cb (@{$self->{signal_cb}{$signal} || []}) { 436 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
408 $cb->($self, @args);
409 }
410} 437}
411 438
412sub DESTROY { 439sub DESTROY {
413 my ($self) = @_; 440 my ($self) = @_;
414 441
481sub new { 508sub new {
482 my ($class, %arg) = @_; 509 my ($class, %arg) = @_;
483 510
484 my $children = delete $arg{children} || []; 511 my $children = delete $arg{children} || [];
485 512
486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg); 513 my $self = $class->SUPER::new (
514 children => [],
515 can_events => 0,
516 %arg,
517 );
487 $self->add ($_) for @$children; 518 $self->add ($_) for @$children;
488 519
489 $self 520 $self
490} 521}
491 522
502 ]; 533 ];
503 534
504 $child->check_size; 535 $child->check_size;
505} 536}
506 537
538sub children {
539 @{ $_[0]{children} }
540}
541
507sub remove { 542sub remove {
508 my ($self, $child) = @_; 543 my ($self, $child) = @_;
509 544
510 delete $child->{parent}; 545 delete $child->{parent};
546 $child->hide;
511 547
512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 548 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
513 549
514 $self->check_size; 550 $self->check_size;
515 $self->update; 551 $self->update;
516} 552}
517 553
518sub clear { 554sub clear {
519 my ($self) = @_; 555 my ($self) = @_;
520 556
521 delete $_->{parent} 557 my $children = delete $self->{children};
522 for @{ delete $self->{children} };
523
524 $self->{children} = []; 558 $self->{children} = [];
559
560 for (@$children) {
561 delete $_->{parent};
562 $_->hide;
563 }
525} 564}
526 565
527sub find_widget { 566sub find_widget {
528 my ($self, $x, $y) = @_; 567 my ($self, $x, $y) = @_;
529 568
733 my $class = shift; 772 my $class = shift;
734 773
735 # TODO: user_x, user_y, overwrite moveto? 774 # TODO: user_x, user_y, overwrite moveto?
736 775
737 my $self = $class->SUPER::new ( 776 my $self = $class->SUPER::new (
738 bg => [1, 1, 1, 1], 777 bg => [1, 1, 1, 1],
739 border_bg => [1, 1, 1, 1], 778 border_bg => [1, 1, 1, 1],
740 border => 0.8, 779 border => 0.8,
741 can_events => 1, 780 can_events => 1,
742 @_ 781 @_
743 ); 782 );
744 783
745 $self->{title} &&= new CFClient::UI::Label 784 $self->{title} &&= new CFClient::UI::Label
893 $self->{children}[$y][$x] = $child; 932 $self->{children}[$y][$x] = $child;
894 933
895 $child->check_size; 934 $child->check_size;
896} 935}
897 936
937sub children {
938 grep $_, map @$_, grep $_, @{ $_[0]{children} }
939}
940
898# TODO: move to container class maybe? send childs a signal on removal? 941# TODO: move to container class maybe? send childs a signal on removal?
899sub clear { 942sub clear {
900 my ($self) = @_; 943 my ($self) = @_;
901 944
945 my @children = $self->children;
902 delete $self->{children}; 946 delete $self->{children};
947
948 for (@children) {
949 delete $_->{parent};
950 $_->hide;
951 }
952
903 $self->update; 953 $self->update;
904} 954}
905 955
906sub get_wh { 956sub get_wh {
907 my ($self) = @_; 957 my ($self) = @_;
1130 1180
1131sub new { 1181sub new {
1132 my ($class, %arg) = @_; 1182 my ($class, %arg) = @_;
1133 1183
1134 my $self = $class->SUPER::new ( 1184 my $self = $class->SUPER::new (
1135 fg => [1, 1, 1], 1185 fg => [1, 1, 1],
1136 #font => default_font 1186 #font => default_font
1137 fontsize => 1, 1187 fontsize => 1,
1138 text => "", 1188 text => "",
1139 align => -1, 1189 align => -1,
1140 valign => -1, 1190 valign => -1,
1141 padding => 2, 1191 padding => 2,
1142 layout => new CFClient::Layout, 1192 layout => new CFClient::Layout,
1143 can_events => 0, 1193 can_events => 0,
1144 %arg 1194 %arg
1145 ); 1195 );
1146 1196
1147 if (exists $self->{template}) { 1197 if (exists $self->{template}) {
1164 s/</&lt;/g; 1214 s/</&lt;/g;
1165 1215
1166 $_[1] 1216 $_[1]
1167} 1217}
1168 1218
1219sub update {
1220 my ($self) = @_;
1221
1222 delete $self->{texture};
1223 $self->SUPER::update;
1224}
1225
1226sub reconfigure {
1227 my ($self) = @_;
1228
1229 delete $self->{texture};
1230}
1231
1169sub set_text { 1232sub set_text {
1170 my ($self, $text) = @_; 1233 my ($self, $text) = @_;
1171 1234
1235 return if $self->{text} eq "T$text";
1236 $self->{text} = "T$text";
1237
1172 $self->{layout}->set_text ($text); 1238 $self->{layout}->set_text ($text);
1173 1239
1174 delete $self->{texture}; 1240 delete $self->{texture};
1241 $self->update;
1175 $self->check_size; 1242 $self->check_size;
1176 $self->update;
1177} 1243}
1178 1244
1179sub set_markup { 1245sub set_markup {
1180 my ($self, $markup) = @_; 1246 my ($self, $markup) = @_;
1181 1247
1248 return if $self->{text} eq "M$markup";
1249 $self->{text} = "M$markup";
1250
1182 $self->{layout}->set_markup ($markup); 1251 $self->{layout}->set_markup ($markup);
1183 1252
1184 delete $self->{texture}; 1253 delete $self->{texture};
1254 $self->update;
1185 $self->check_size; 1255 $self->check_size;
1186 $self->update;
1187} 1256}
1188 1257
1189sub size_request { 1258sub size_request {
1190 my ($self) = @_; 1259 my ($self) = @_;
1191 1260
1271 1340
1272sub new { 1341sub new {
1273 my $class = shift; 1342 my $class = shift;
1274 1343
1275 $class->SUPER::new ( 1344 $class->SUPER::new (
1276 fg => [1, 1, 1], 1345 fg => [1, 1, 1],
1277 bg => [0, 0, 0, 0.2], 1346 bg => [0, 0, 0, 0.2],
1278 active_bg => [1, 1, 1, 0.5], 1347 active_bg => [1, 1, 1, 0.5],
1279 active_fg => [0, 0, 0], 1348 active_fg => [0, 0, 0],
1280 can_hover => 1, 1349 can_hover => 1,
1281 can_focus => 1, 1350 can_focus => 1,
1282 valign => 0, 1351 valign => 0,
1283 can_events => 1, 1352 can_events => 1,
1284 @_ 1353 @_
1285 ) 1354 )
1286} 1355}
1287 1356
1441 my ($self, $ev) = @_; 1510 my ($self, $ev) = @_;
1442 1511
1443 my $sym = $ev->{sym}; 1512 my $sym = $ev->{sym};
1444 1513
1445 if ($sym == 13) { 1514 if ($sym == 13) {
1515 unshift @{$self->{history}},
1516 my $txt = $self->get_text;
1517 $self->{history_pointer} = -1;
1518 $self->{history_saveback} = '';
1446 $self->emit (activate => $self->get_text); 1519 $self->emit (activate => $txt);
1447 $self->update; 1520 $self->update;
1521
1522 } elsif ($sym == CFClient::SDLK_UP) {
1523 if ($self->{history_pointer} < 0) {
1524 $self->{history_saveback} = $self->get_text;
1525 }
1526 if (@{$self->{history} || []} > 0) {
1527 $self->{history_pointer}++;
1528 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1529 $self->{history_pointer} = @{$self->{history} || []} - 1;
1530 }
1531 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1532 }
1533
1534 } elsif ($sym == CFClient::SDLK_DOWN) {
1535 $self->{history_pointer}--;
1536 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1537
1538 if ($self->{history_pointer} >= 0) {
1539 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1540 } else {
1541 $self->set_text ($self->{history_saveback});
1542 }
1448 1543
1449 } else { 1544 } else {
1450 $self->SUPER::key_down ($ev); 1545 $self->SUPER::key_down ($ev);
1451 } 1546 }
1452 1547
1466 1561
1467sub new { 1562sub new {
1468 my $class = shift; 1563 my $class = shift;
1469 1564
1470 $class->SUPER::new ( 1565 $class->SUPER::new (
1471 padding => 4, 1566 padding => 4,
1472 fg => [1, 1, 1], 1567 fg => [1, 1, 1],
1473 bg => [1, 1, 1, 0.2], 1568 bg => [1, 1, 1, 0.2],
1474 active_fg => [0, 0, 1], 1569 active_fg => [0, 0, 1],
1475 can_hover => 1, 1570 can_hover => 1,
1476 align => 0, 1571 align => 0,
1477 valign => 0, 1572 valign => 0,
1478 can_events => 1, 1573 can_events => 1,
1479 @_ 1574 @_
1480 ) 1575 )
1481} 1576}
1482 1577
1689} 1784}
1690 1785
1691sub set_max { 1786sub set_max {
1692 my ($self, $max) = @_; 1787 my ($self, $max) = @_;
1693 1788
1789 return if $self->{max_val} == $max;
1790
1694 $self->{max_val} = $max; 1791 $self->{max_val} = $max;
1792 $self->update;
1695} 1793}
1696 1794
1697sub set_value { 1795sub set_value {
1698 my ($self, $val, $max) = @_; 1796 my ($self, $val, $max) = @_;
1699 1797
1700 $self->set_max ($max) 1798 $self->set_max ($max)
1701 if defined $max; 1799 if defined $max;
1702 1800
1703 $max = $self->{max_val}; 1801 return if $self->{val} == $val;
1802
1704 $self->{val} = $val; 1803 $self->{val} = $val;
1705
1706 $self->update; 1804 $self->update;
1707} 1805}
1708 1806
1709sub _draw { 1807sub _draw {
1710 my ($self) = @_; 1808 my ($self) = @_;
1773 1871
1774sub new { 1872sub new {
1775 my ($class, %arg) = @_; 1873 my ($class, %arg) = @_;
1776 1874
1777 my $self = $class->SUPER::new ( 1875 my $self = $class->SUPER::new (
1778 tooltip => $arg{type}, 1876 tooltip => $arg{type},
1877 can_hover => 1,
1779 can_events => 1, 1878 can_events => 1,
1780 can_hover => 1,
1781 %arg, 1879 %arg,
1782 ); 1880 );
1783 1881
1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999"); 1882 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1); 1883 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1793 1891
1794 $self->{value}->set_fontsize ($fsize); 1892 $self->{value}->set_fontsize ($fsize);
1795 $self->{max} ->set_fontsize ($fsize); 1893 $self->{max} ->set_fontsize ($fsize);
1796} 1894}
1797 1895
1896sub set_max {
1897 my ($self, $max) = @_;
1898
1899 $self->{gauge}->set_max ($max);
1900 $self->{max}->set_text ($max);
1901}
1902
1798sub set_value { 1903sub set_value {
1799 my ($self, $val, $max) = @_; 1904 my ($self, $val, $max) = @_;
1800 1905
1801 $self->set_max ($max) 1906 $self->set_max ($max)
1802 if defined $max; 1907 if defined $max;
1803 1908
1804 $self->{gauge}->set_value ($val, $max); 1909 $self->{gauge}->set_value ($val, $max);
1805 $self->{value}->set_text ($val); 1910 $self->{value}->set_text ($val);
1806}
1807
1808sub set_max {
1809 my ($self, $max) = @_;
1810
1811 $self->{gauge}->set_max ($max);
1812 $self->{max}->set_text ($max);
1813} 1911}
1814 1912
1815############################################################################# 1913#############################################################################
1816 1914
1817package CFClient::UI::Slider; 1915package CFClient::UI::Slider;
1957 2055
1958sub new { 2056sub new {
1959 my $class = shift; 2057 my $class = shift;
1960 2058
1961 my $self = $class->SUPER::new ( 2059 my $self = $class->SUPER::new (
1962 fontsize => 1, 2060 fontsize => 1,
2061 can_events => 0,
1963 #font => default_font 2062 #font => default_font
1964 @_, 2063 @_,
1965 2064
1966 layout => (new CFClient::Layout), 2065 layout => (new CFClient::Layout),
1967 par => [], 2066 par => [],
1968 height => 0, 2067 height => 0,
1969 children => [ 2068 children => [
1970 (new CFClient::UI::Empty expand => 1), 2069 (new CFClient::UI::Empty expand => 1),
1971 (new CFClient::UI::Slider vertical => 1), 2070 (new CFClient::UI::Slider vertical => 1),
1972 ], 2071 ],
1973 ); 2072 );
1974 2073
2099 my ($self) = @_; 2198 my ($self) = @_;
2100 2199
2101 if ($self->{texture}) { 2200 if ($self->{texture}) {
2102 glEnable GL_TEXTURE_2D; 2201 glEnable GL_TEXTURE_2D;
2103 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2202 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2203 glColor 1, 1, 1, 1;
2104 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2204 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
2105 glDisable GL_TEXTURE_2D; 2205 glDisable GL_TEXTURE_2D;
2106 } 2206 }
2107 2207
2108 $self->{children}[1]->draw; 2208 $self->{children}[1]->draw;
2159 2259
2160sub new { 2260sub new {
2161 my $class = shift; 2261 my $class = shift;
2162 2262
2163 my $self = $class->SUPER::new ( 2263 my $self = $class->SUPER::new (
2164 state => 0, 2264 state => 0,
2165 connect_activate => \&toggle_flopper, 2265 connect_activate => \&toggle_flopper,
2166 can_events => 1,
2167 @_ 2266 @_
2168 ); 2267 );
2169 2268
2170 if ($self->{state}) { 2269 if ($self->{state}) {
2171 $self->{state} = 0; 2270 $self->{state} = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines