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.183 by elmex, Sun Apr 30 13:10:46 2006 UTC vs.
Revision 1.203 by root, Sun May 14 21:13:11 2006 UTC

1package CFClient::UI; 1package CFClient::UI;
2 2
3use utf8;
3use strict; 4use strict;
4 5
5use Scalar::Util (); 6use Scalar::Util ();
6use List::Util (); 7use List::Util ();
7 8
10our ($FOCUS, $HOVER, $GRAB); # various widgets 11our ($FOCUS, $HOVER, $GRAB); # various widgets
11 12
12our $ROOT; 13our $ROOT;
13our $TOOLTIP; 14our $TOOLTIP;
14our $BUTTON_STATE; 15our $BUTTON_STATE;
16
17our %WIDGET; # all widgets, weak-referenced
15 18
16sub check_tooltip { 19sub check_tooltip {
17 if (!$GRAB) { 20 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (length $widget->{tooltip}) { 22 if (length $widget->{tooltip}) {
23 26
24 my $tip = $widget->{tooltip}; 27 my $tip = $widget->{tooltip};
25 28
26 $tip = $tip->($widget) if CODE:: eq ref $tip; 29 $tip = $tip->($widget) if CODE:: eq ref $tip;
27 30
28 $TOOLTIP->set_markup ($widget->{tooltip}); 31 $TOOLTIP->set_tooltip_from ($widget);
29
30 $TOOLTIP->show; 32 $TOOLTIP->show;
31 33
32 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
33 35
34 if ($x + $TOOLTIP->{w} > $::WIDTH) { 36 if ($x + $TOOLTIP->{w} > $::WIDTH) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 37 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0);
36 } 38 }
37 39
38 $TOOLTIP->move ($x, $y); 40 $TOOLTIP->move ($x, $y);
41 $TOOLTIP->check_size;
42 $TOOLTIP->update;
39 } 43 }
40 44
41 return; 45 return;
42 } 46 }
43 } 47 }
130 134
131 for (@$vals) { 135 for (@$vals) {
132 my $i = int $_ + $rem; 136 my $i = int $_ + $rem;
133 $rem += $_ - $i; 137 $rem += $_ - $i;
134 $_ = $i; 138 $_ = $i;
139 }
140}
141
142# call when resolution changes etc.
143sub rescale_widgets {
144 my ($sx, $sy) = @_;
145
146 for my $widget (values %WIDGET) {
147 if ($widget->{toplevel}) {
148 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
149 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
150 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
151 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
152 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
153 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
154 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
155 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
156 }
157
158 $widget->reconfigure;
135 } 159 }
136} 160}
137 161
138############################################################################# 162#############################################################################
139 163
158 if (/^connect_(.*)$/) { 182 if (/^connect_(.*)$/) {
159 $self->connect ($1 => delete $self->{$_}); 183 $self->connect ($1 => delete $self->{$_});
160 } 184 }
161 } 185 }
162 186
187 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
188
163 $self 189 $self
164} 190}
165 191
166sub destroy { 192sub destroy {
167 my ($self) = @_; 193 my ($self) = @_;
232 $self->{y} = $y; 258 $self->{y} = $y;
233 $self->update; 259 $self->update;
234 } 260 }
235 261
236 if ($self->{w} != $w || $self->{h} != $h) { 262 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 263 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
238 $self->{h} = $h;
239
240 $self->size_allocate ($w, $h);
241 $self->update;
242 $self->emit (size_allocate => $w, $h);
243 } 264 }
244} 265}
245 266
246sub size_allocate { 267sub size_allocate {
247 # nothing to be done 268 # nothing to be done
248} 269}
249 270
271sub reconfigure {
272 my ($self) = @_;
273
274 $self->check_size (1);
275 $self->update;
276}
277
250sub children { 278sub children {
251}
252
253# call when resolution changes etc.
254sub reconfigure {
255 my ($self) = @_;
256
257 $_->reconfigure
258 for $self->children;
259
260 $_->check_size;
261} 279}
262 280
263sub set_max_size { 281sub set_max_size {
264 my ($self, $w, $h) = @_; 282 my ($self, $w, $h) = @_;
265 283
266 delete $self->{max_w}; $self->{max_w} = $w if $w; 284 delete $self->{max_w}; $self->{max_w} = $w if $w;
267 delete $self->{max_h}; $self->{max_h} = $h if $h; 285 delete $self->{max_h}; $self->{max_h} = $h if $h;
268}
269
270# return top left coordinates
271sub _topleft {
272 my ($self, $x, $y) = @_;
273
274 $self->{parent}
275 or Carp::confess "no parent widget in _topleft\n";#d#
276
277 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
278} 286}
279 287
280# translate global coordinates to local coordinate system 288# translate global coordinates to local coordinate system
281sub coord2local { 289sub coord2local {
282 my ($self, $x, $y) = @_; 290 my ($self, $x, $y) = @_;
283 291
284 my ($X, $Y) = $self->_topleft; 292 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
285 ($x - $X, $y - $Y)
286} 293}
287 294
288# translate local coordinates to global coordinate system 295# translate local coordinates to global coordinate system
289sub coord2global { 296sub coord2global {
290 my ($self, $x, $y) = @_; 297 my ($self, $x, $y) = @_;
291 298
292 my ($X, $Y) = $self->_topleft; 299 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
293 ($x + $X, $y + $Y)
294} 300}
295 301
296sub focus_in { 302sub focus_in {
297 my ($self) = @_; 303 my ($self) = @_;
298 304
396 402
397sub set_parent { 403sub set_parent {
398 my ($self, $parent) = @_; 404 my ($self, $parent) = @_;
399 405
400 Scalar::Util::weaken ($self->{parent} = $parent); 406 Scalar::Util::weaken ($self->{parent} = $parent);
407
408 # TODO: req_w _does_change after ->reconfigure
409 $self->check_size
410 unless exists $self->{req_w};
401} 411}
402 412
403sub check_size { 413sub check_size {
404 my ($self) = @_; 414 my ($self, $forced) = @_;
405 415
406 $self->{parent} 416 $self->{force_alloc} = 1 if $forced;
407 or return 1; 417 $CFClient::UI::ROOT->{check_size}{$self} = $self;
408
409 my ($w, $h) = $self->{user_w} && $self->{user_h}
410 ? @$self{qw(user_w user_h)}
411 : $self->size_request;
412
413 if ($w != $self->{req_w} || $h != $self->{req_h}) {
414 $self->{req_w} = $w;
415 $self->{req_h} = $h;
416
417 $self->{parent}->check_size
418 or $self->size_allocate (
419 (List::Util::max $self->{w}, $w),
420 (List::Util::max $self->{h}, $h),
421 );
422
423 1
424 } else {
425 0
426 }
427} 418}
428 419
429sub update { 420sub update {
430 my ($self) = @_; 421 my ($self) = @_;
431 422
446} 437}
447 438
448sub DESTROY { 439sub DESTROY {
449 my ($self) = @_; 440 my ($self) = @_;
450 441
442 delete $WIDGET{$self+0};
451 #$self->deactivate; 443 #$self->deactivate;
452} 444}
453 445
454############################################################################# 446#############################################################################
455 447
528 520
529 $self 521 $self
530} 522}
531 523
532sub add { 524sub add {
533 my ($self, $child) = @_; 525 my ($self, @widgets) = @_;
534 526
535 $child->set_parent ($self); 527 $_->set_parent ($self)
528 for @widgets;
536 529
537 use sort 'stable'; 530 use sort 'stable';
538 531
539 $self->{children} = [ 532 $self->{children} = [
540 sort { $a->{z} <=> $b->{z} } 533 sort { $a->{z} <=> $b->{z} }
541 @{$self->{children}}, $child 534 @{$self->{children}}, @widgets
542 ]; 535 ];
543 536
544 $child->check_size; 537 $self->check_size (1);
545 $self->update; 538 $self->update;
546} 539}
547 540
548sub children { 541sub children {
549 @{ $_[0]{children} } 542 @{ $_[0]{children} }
656} 649}
657 650
658sub update { 651sub update {
659 my ($self) = @_; 652 my ($self) = @_;
660 653
661 $ROOT->on_refresh ($self => sub { $self->render_child }); 654 $ROOT->on_post_alloc ($self => sub { $self->render_child });
662 $self->SUPER::update; 655 $self->SUPER::update;
663} 656}
664 657
665sub size_allocate { 658sub size_allocate {
666 my ($self, $w, $h) = @_; 659 my ($self, $w, $h) = @_;
697 my ($w, $h) = ($self->w, $self->h); 690 my ($w, $h) = ($self->w, $self->h);
698 691
699 my $tex = $self->{texture} 692 my $tex = $self->{texture}
700 or return; 693 or return;
701 694
702 glEnable GL_BLEND;
703 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
704 glEnable GL_TEXTURE_2D; 695 glEnable GL_TEXTURE_2D;
705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 696 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
706 glColor 0, 0, 0, 1; 697 glColor 0, 0, 0, 1;
707 698
708 $tex->draw_quad (0, 0, $w, $h); 699 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
709 700
710 glDisable GL_BLEND;
711 glDisable GL_TEXTURE_2D; 701 glDisable GL_TEXTURE_2D;
712} 702}
713 703
714############################################################################# 704#############################################################################
715 705
737 727
738 $self->{view_x} = int $x; 728 $self->{view_x} = int $x;
739 $self->{view_y} = int $y; 729 $self->{view_y} = int $y;
740 730
741 $self->update; 731 $self->update;
732}
733
734# hmm, this does not work for topleft of $self... but we should not ask for that
735sub coord2local {
736 my ($self, $x, $y) = @_;
737
738 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
739}
740
741sub coord2global {
742 my ($self, $x, $y) = @_;
743
744 $x = List::Util::min $self->{w}, $x - $self->{view_x};
745 $y = List::Util::min $self->{h}, $y - $self->{view_y};
746
747 $self->SUPER::coord2global ($x, $y)
748}
749
750sub find_widget {
751 my ($self, $x, $y) = @_;
752
753 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
754 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
755 ) {
756 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
757 } else {
758 $self->CFClient::UI::Base::find_widget ($x, $y)
759 }
742} 760}
743 761
744sub _render { 762sub _render {
745 my ($self) = @_; 763 my ($self) = @_;
746 764
779 $self->add ($self->{slider}); 797 $self->add ($self->{slider});
780 798
781 $self 799 $self
782} 800}
783 801
784#TODO# update range on size_allocate depeneing on child 802#TODO# update range on size_allocate depending on child
785# update viewport offset on scroll 803# update viewport offset on scroll
786 804
787############################################################################# 805#############################################################################
788 806
789package CFClient::UI::Frame; 807package CFClient::UI::Frame;
793use CFClient::OpenGL; 811use CFClient::OpenGL;
794 812
795sub new { 813sub new {
796 my $class = shift; 814 my $class = shift;
797 815
798 my $self = $class->SUPER::new ( 816 $class->SUPER::new (
799 bg => [1, 1, 1, 1], 817 bg => undef,
800 border_bg => [1, 1, 1, 1],
801 border => 0.8,
802 @_ 818 @_,
803 ); 819 )
804
805 $self
806} 820}
807 821
808sub _draw { 822sub _draw {
809 my ($self) = @_; 823 my ($self) = @_;
810 824
811 my ($w, $h) = ($self->{w}, $self->{h}); 825 if ($self->{bg}) {
826 my ($w, $h) = @$self{qw(w h)};
812 827
813 glEnable GL_BLEND; 828 glEnable GL_BLEND;
814 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 829 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
815 glEnable GL_TEXTURE_2D; 830 glColor @{ $self->{bg} };
816 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
817 831
818# glBegin GL_QUADS; 832 glBegin GL_QUADS;
819# glColor 0, 0, 0, 0;
820# glVertex 0 , 0; 833 glVertex 0 , 0;
821# glVertex 0 , $h; 834 glVertex 0 , $h;
822# glVertex $w, $h; 835 glVertex $w, $h;
823# glVertex $w, 0; 836 glVertex $w, 0;
824# glEnd; 837 glEnd;
825 838
826
827 $self->child->draw;
828 glDisable GL_BLEND; 839 glDisable GL_BLEND;
829 glDisable GL_TEXTURE_2D; 840 }
841
842 $self->SUPER::_draw;
830} 843}
831 844
832############################################################################# 845#############################################################################
833 846
834package CFClient::UI::FancyFrame; 847package CFClient::UI::FancyFrame;
848 861
849 my $self = $class->SUPER::new ( 862 my $self = $class->SUPER::new (
850 bg => [1, 1, 1, 1], 863 bg => [1, 1, 1, 1],
851 border_bg => [1, 1, 1, 1], 864 border_bg => [1, 1, 1, 1],
852 border => 0.6, 865 border => 0.6,
866 toplevel => 1,
853 can_events => 1, 867 can_events => 1,
854 @_ 868 @_
855 ); 869 );
856 870
857 $self->{title} &&= new CFClient::UI::Label 871 $self->{title} &&= new CFClient::UI::Label
950 my ($self) = @_; 964 my ($self) = @_;
951 965
952 my ($w, $h ) = ($self->{w}, $self->{h}); 966 my ($w, $h ) = ($self->{w}, $self->{h});
953 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 967 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
954 968
955 glEnable GL_BLEND;
956 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
957 glEnable GL_TEXTURE_2D; 969 glEnable GL_TEXTURE_2D;
958 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 970 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
959 971
960 my $border = $self->border; 972 my $border = $self->border;
961 973
962 glColor @{ $self->{border_bg} }; 974 glColor @{ $self->{border_bg} };
963 $tex[1]->draw_quad (0, 0, $w, $border); 975 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
964 $tex[3]->draw_quad (0, $border, $border, $ch); 976 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
965 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 977 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
966 $tex[4]->draw_quad (0, $h - $border, $w, $border); 978 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
967 979
968 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 980 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
969 my $bg = $tex[0]; 981 my $bg = $tex[0];
970 982
971 # TODO: repeat texture not scale 983 # TODO: repeat texture not scale
975 glColor @{ $self->{bg} }; 987 glColor @{ $self->{bg} };
976 988
977 $bg->{s} = $rep_x; 989 $bg->{s} = $rep_x;
978 $bg->{t} = $rep_y; 990 $bg->{t} = $rep_y;
979 $bg->{wrap_mode} = 1; 991 $bg->{wrap_mode} = 1;
980 $bg->draw_quad ($border, $border, $cw, $ch); 992 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
993 }
981 994
982 glDisable GL_TEXTURE_2D; 995 glDisable GL_TEXTURE_2D;
983 glDisable GL_BLEND;
984 }
985 996
986 $self->{title}->draw if $self->{title}; 997 $self->{title}->draw if $self->{title};
987 998
988 $self->child->draw; 999 $self->child->draw;
989} 1000}
1216} 1227}
1217 1228
1218sub size_allocate { 1229sub size_allocate {
1219 my ($self, $w, $h) = @_; 1230 my ($self, $w, $h) = @_;
1220 1231
1232 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1233
1221 my $children = $self->{children}; 1234 my $children = $self->{children};
1222 1235
1223 my @h = map $_->{req_h}, @$children; 1236 my @h = map $_->{req_h}, @$children;
1224 1237
1225 my $req_h = List::Util::sum @h; 1238 my $req_h = List::Util::sum @h;
1264 my ($class, %arg) = @_; 1277 my ($class, %arg) = @_;
1265 1278
1266 my $self = $class->SUPER::new ( 1279 my $self = $class->SUPER::new (
1267 fg => [1, 1, 1], 1280 fg => [1, 1, 1],
1268 #font => default_font 1281 #font => default_font
1282 #text => initial text
1283 #markup => initial narkup
1284 layout => (new CFClient::Layout),
1269 fontsize => 1, 1285 fontsize => 1,
1270 text => "",
1271 align => -1, 1286 align => -1,
1272 valign => -1, 1287 valign => -1,
1273 padding => 2, 1288 padding => 2,
1274 layout => new CFClient::Layout,
1275 can_events => 0, 1289 can_events => 0,
1276 %arg 1290 %arg
1277 ); 1291 );
1278 1292
1279 if (exists $self->{template}) { 1293 if (exists $self->{template}) {
1280 my $layout = new CFClient::Layout; 1294 my $layout = new CFClient::Layout;
1281 $layout->set_text (delete $self->{template}); 1295 $layout->set_text (delete $self->{template});
1282 $self->{template} = $layout; 1296 $self->{template} = $layout;
1283 } 1297 }
1284 1298
1285 $self->set_text (delete $self->{text}) if exists $self->{text}; 1299 if (exists $self->{markup}) {
1286 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1300 $self->set_markup (delete $self->{markup});
1301 } else {
1302 $self->set_text (delete $self->{text});
1303 }
1287 1304
1288 $self 1305 $self
1289} 1306}
1290 1307
1291sub escape { 1308sub escape {
1303 1320
1304 delete $self->{texture}; 1321 delete $self->{texture};
1305 $self->SUPER::update; 1322 $self->SUPER::update;
1306} 1323}
1307 1324
1308sub reconfigure {
1309 my ($self) = @_;
1310
1311 delete $self->{texture};
1312}
1313
1314sub set_text { 1325sub set_text {
1315 my ($self, $text) = @_; 1326 my ($self, $text) = @_;
1316 1327
1317 return if $self->{text} eq "T$text"; 1328 return if $self->{text} eq "T$text";
1318 $self->{text} = "T$text"; 1329 $self->{text} = "T$text";
1319 1330
1331 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1320 $self->{layout}->set_text ($text); 1332 $self->{layout}->set_text ($text);
1321 1333
1322 delete $self->{texture};
1323 $self->update; 1334 $self->update;
1324 $self->check_size; 1335 $self->check_size;
1325} 1336}
1326 1337
1327sub set_markup { 1338sub set_markup {
1328 my ($self, $markup) = @_; 1339 my ($self, $markup) = @_;
1329 1340
1330 return if $self->{text} eq "M$markup"; 1341 return if $self->{text} eq "M$markup";
1331 $self->{text} = "M$markup"; 1342 $self->{text} = "M$markup";
1332 1343
1344 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1345
1346 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1333 $self->{layout}->set_markup ($markup); 1347 $self->{layout}->set_markup ($markup);
1334 1348
1335 delete $self->{texture};
1336 $self->update; 1349 $self->update;
1337 $self->check_size; 1350 $self->check_size;
1338} 1351}
1339 1352
1340sub size_request { 1353sub size_request {
1371sub set_fontsize { 1384sub set_fontsize {
1372 my ($self, $fontsize) = @_; 1385 my ($self, $fontsize) = @_;
1373 1386
1374 $self->{fontsize} = $fontsize; 1387 $self->{fontsize} = $fontsize;
1375 delete $self->{texture}; 1388 delete $self->{texture};
1389
1390 $self->update;
1376 $self->check_size; 1391 $self->check_size;
1377 $self->update;
1378} 1392}
1379 1393
1380sub _draw { 1394sub _draw {
1381 my ($self) = @_; 1395 my ($self) = @_;
1382 1396
1383 my $tex = $self->{texture} ||= do { 1397 my $tex = $self->{texture} ||= do {
1398 $self->{layout}->set_foreground (@{$self->{fg}});
1384 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1399 $self->{layout}->set_font ($self->{font}) if $self->{font};
1385 $self->{layout}->set_width ($self->{w}); 1400 $self->{layout}->set_width ($self->{w});
1386 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1401 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1402
1387 new_from_layout CFClient::Texture $self->{layout} 1403 my $tex = new_from_layout CFClient::Texture $self->{layout};
1404
1405 $self->{ox} = int $self->{align} < 0 ? $self->{padding}
1406 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1407 : ($self->{w} - $tex->{w}) * 0.5;
1408
1409 $self->{oy} = int $self->{valign} < 0 ? $self->{padding}
1410 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1411 : ($self->{h} - $tex->{h}) * 0.5;
1412
1413 $tex
1388 }; 1414 };
1389 1415
1390 glEnable GL_BLEND;
1391 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1392 glEnable GL_TEXTURE_2D; 1416 glEnable GL_TEXTURE_2D;
1393 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1394 1418
1419 if ($tex->{format} == GL_ALPHA) {
1395 glColor @{$self->{fg}}; 1420 glColor @{$self->{fg}};
1396
1397 $self->{ox} = int (
1398 $self->{align} < 0 ? $self->{padding}
1399 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1400 : ($self->{w} - $tex->{w}) * 0.5
1401 );
1402
1403 $self->{oy} = int (
1404 $self->{valign} < 0 ? $self->{padding}
1405 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1406 : ($self->{h} - $tex->{h}) * 0.5
1407 );
1408
1409 $tex->draw_quad ($self->{ox}, $self->{oy}); 1421 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1422 } else {
1423 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1424 }
1410 1425
1411 glDisable GL_TEXTURE_2D; 1426 glDisable GL_TEXTURE_2D;
1412 glDisable GL_BLEND;
1413} 1427}
1414 1428
1415############################################################################# 1429#############################################################################
1416 1430
1417package CFClient::UI::EntryBase; 1431package CFClient::UI::EntryBase;
1452 $self->{layout}->set_text ("$text "); 1466 $self->{layout}->set_text ("$text ");
1453 1467
1454 $self->emit (changed => $self->{text}); 1468 $self->emit (changed => $self->{text});
1455} 1469}
1456 1470
1471sub set_text {
1472 my ($self, $text) = @_;
1473
1474 $self->{cursor} = length $text;
1475 $self->_set_text ($text);
1476 $self->check_size;
1477 $self->update;
1478}
1479
1457sub get_text { 1480sub get_text {
1458 $_[0]{text} 1481 $_[0]{text}
1459} 1482}
1460 1483
1461sub size_request { 1484sub size_request {
1468 1491
1469sub size_allocate { 1492sub size_allocate {
1470 my ($self, $w, $h) = @_; 1493 my ($self, $w, $h) = @_;
1471 1494
1472 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text 1495 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1473}
1474
1475sub set_text {
1476 my ($self, $text) = @_;
1477
1478 $self->{cursor} = length $text;
1479 $self->_set_text ($text);
1480 $self->update;
1481} 1496}
1482 1497
1483sub key_down { 1498sub key_down {
1484 my ($self, $ev) = @_; 1499 my ($self, $ev) = @_;
1485 1500
1487 my $sym = $ev->{sym}; 1502 my $sym = $ev->{sym};
1488 my $uni = $ev->{unicode}; 1503 my $uni = $ev->{unicode};
1489 1504
1490 my $text = $self->get_text; 1505 my $text = $self->get_text;
1491 1506
1492 if ($sym == 8) { 1507 if ($uni == 8) {
1493 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1508 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1494 } elsif ($sym == 127) { 1509 } elsif ($uni == 127) {
1495 substr $text, $self->{cursor}, 1, ""; 1510 substr $text, $self->{cursor}, 1, "";
1496 } elsif ($sym == CFClient::SDLK_LEFT) { 1511 } elsif ($sym == CFClient::SDLK_LEFT) {
1497 --$self->{cursor} if $self->{cursor}; 1512 --$self->{cursor} if $self->{cursor};
1498 } elsif ($sym == CFClient::SDLK_RIGHT) { 1513 } elsif ($sym == CFClient::SDLK_RIGHT) {
1499 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1514 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1500 } elsif ($sym == CFClient::SDLK_HOME) { 1515 } elsif ($sym == CFClient::SDLK_HOME) {
1501 $self->{cursor} = 0; 1516 $self->{cursor} = 0;
1502 } elsif ($sym == CFClient::SDLK_END) { 1517 } elsif ($sym == CFClient::SDLK_END) {
1503 $self->{cursor} = length $text; 1518 $self->{cursor} = length $text;
1504 } elsif ($sym == 27) { 1519 } elsif ($uni == 27) {
1505 $self->emit ('escape'); 1520 $self->emit ('escape');
1506 } elsif ($uni) { 1521 } elsif ($uni) {
1507 substr $text, $self->{cursor}++, 0, chr $uni; 1522 substr $text, $self->{cursor}++, 0, chr $uni;
1508 } 1523 }
1509 1524
1673 1688
1674 if ($GRAB == $self) { 1689 if ($GRAB == $self) {
1675 $self->{fg} = $self->{active_fg}; 1690 $self->{fg} = $self->{active_fg};
1676 } 1691 }
1677 1692
1678 glEnable GL_BLEND;
1679 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1680 glEnable GL_TEXTURE_2D; 1693 glEnable GL_TEXTURE_2D;
1681 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1682 glColor 0, 0, 0, 1; 1695 glColor 0, 0, 0, 1;
1683 1696
1684 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1697 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1685 1698
1686 glDisable GL_TEXTURE_2D; 1699 glDisable GL_TEXTURE_2D;
1687 glDisable GL_BLEND;
1688 1700
1689 $self->SUPER::_draw; 1701 $self->SUPER::_draw;
1690} 1702}
1691 1703
1692############################################################################# 1704#############################################################################
1739 1751
1740 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1752 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1741 1753
1742 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1754 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1743 1755
1744 glEnable GL_BLEND; 1756 my $tex = $self->{state} ? $tex[1] : $tex[0];
1757
1745 glEnable GL_TEXTURE_2D; 1758 glEnable GL_TEXTURE_2D;
1746 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1747
1748 my $tex = $self->{state} ? $tex[1] : $tex[0];
1749
1750 $tex->draw_quad (0, 0, $s, $s); 1759 $tex->draw_quad_alpha (0, 0, $s, $s);
1751
1752 glDisable GL_TEXTURE_2D; 1760 glDisable GL_TEXTURE_2D;
1753 glDisable GL_BLEND;
1754} 1761}
1755 1762
1756############################################################################# 1763#############################################################################
1757 1764
1758package CFClient::UI::Image; 1765package CFClient::UI::Image;
1801 glTranslate 0, -$self->{w}, 0; 1808 glTranslate 0, -$self->{w}, 0;
1802 1809
1803 ($w, $h) = ($h, $w); 1810 ($w, $h) = ($h, $w);
1804 } 1811 }
1805 1812
1806 glEnable GL_BLEND;
1807 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1808 glEnable GL_TEXTURE_2D; 1813 glEnable GL_TEXTURE_2D;
1809 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1814 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1810 1815
1811 $tex->draw_quad (0, 0, $w, $h); 1816 $tex->draw_quad_alpha (0, 0, $w, $h);
1812 1817
1813 glDisable GL_BLEND;
1814 glDisable GL_TEXTURE_2D; 1818 glDisable GL_TEXTURE_2D;
1815} 1819}
1816 1820
1817############################################################################# 1821#############################################################################
1818 1822
2110 $page ||= 2; 2114 $page ||= 2;
2111 2115
2112 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2116 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2113 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2117 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2114 2118
2115 glEnable GL_BLEND;
2116 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2117 glEnable GL_TEXTURE_2D; 2119 glEnable GL_TEXTURE_2D;
2118 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2120 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2119 2121
2120 # draw background 2122 # draw background
2121 $tex[1]->draw_quad (0, 0, $w, $h); 2123 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2122 2124
2123 # draw handle 2125 # draw handle
2124 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2126 $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h);
2125 2127
2126 glDisable GL_BLEND;
2127 glDisable GL_TEXTURE_2D; 2128 glDisable GL_TEXTURE_2D;
2128} 2129}
2129 2130
2130############################################################################# 2131#############################################################################
2131 2132
2142 fontsize => 1, 2143 fontsize => 1,
2143 can_events => 0, 2144 can_events => 0,
2144 #font => default_font 2145 #font => default_font
2145 @_, 2146 @_,
2146 2147
2147 layout => (new CFClient::Layout), 2148 layout => (new CFClient::Layout 1),
2148 par => [], 2149 par => [],
2149 height => 0, 2150 height => 0,
2150 children => [ 2151 children => [
2151 (new CFClient::UI::Empty expand => 1), 2152 (new CFClient::UI::Empty expand => 1),
2152 (new CFClient::UI::Slider vertical => 1), 2153 (new CFClient::UI::Slider vertical => 1),
2170 2171
2171 my $layout = $self->{layout}; 2172 my $layout = $self->{layout};
2172 2173
2173 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2174 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2174 $layout->set_width ($self->{children}[0]{w}); 2175 $layout->set_width ($self->{children}[0]{w});
2175 $layout->set_text ($text); 2176 $layout->set_markup ($text);
2176 2177
2177 ($layout->size)[1] 2178 ($layout->size)[1]
2178} 2179}
2179 2180
2180sub reflow { 2181sub reflow {
2218 2219
2219 return unless $self->{h} > 0; 2220 return unless $self->{h} > 0;
2220 2221
2221 delete $self->{texture}; 2222 delete $self->{texture};
2222 2223
2223 $ROOT->on_refresh ($self, sub { 2224 $ROOT->on_post_alloc ($self, sub {
2224 if (delete $self->{need_reflow}) { 2225 if (delete $self->{need_reflow}) {
2225 my $height = 0; 2226 my $height = 0;
2226 2227
2227 $height += $_->[0] = $self->text_height ($_->[2]) 2228 $height += $_->[0] = $self->text_height ($_->[2])
2228 for @{$self->{par}}; 2229 for @{$self->{par}};
2236 2237
2237 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2238 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2238 glClearColor 0, 0, 0, 0; 2239 glClearColor 0, 0, 0, 0;
2239 glClear GL_COLOR_BUFFER_BIT; 2240 glClear GL_COLOR_BUFFER_BIT;
2240 2241
2241 glEnable GL_BLEND;
2242 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2243 glEnable GL_TEXTURE_2D; 2242 glEnable GL_TEXTURE_2D;
2244 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2243 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2245 2244
2246 my $top = int $self->{children}[1]{range}[0]; 2245 my $top = int $self->{children}[1]{range}[0];
2247 2246
2256 2255
2257 for my $par (@{$self->{par}}) { 2256 for my $par (@{$self->{par}}) {
2258 my $h = $par->[0]; 2257 my $h = $par->[0];
2259 2258
2260 if ($y0 < $y + $h && $y < $y1) { 2259 if ($y0 < $y + $h && $y < $y1) {
2260 $layout->set_foreground (@{ $par->[1] });
2261 $layout->set_text ($par->[2]); 2261 $layout->set_markup ($par->[2]);
2262 2262
2263 glColor @{ $par->[1] };
2264 my ($W, $H) = $layout->size; 2263 my ($W, $H) = $layout->size;
2265 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2264 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2266 } 2265 }
2267 2266
2268 $y += $h; 2267 $y += $h;
2269 } 2268 }
2270 2269
2271 glDisable GL_TEXTURE_2D; 2270 glDisable GL_TEXTURE_2D;
2272 glDisable GL_BLEND;
2273 }; 2271 };
2274 }); 2272 });
2275} 2273}
2276 2274
2277sub _draw { 2275sub _draw {
2278 my ($self) = @_; 2276 my ($self) = @_;
2279 2277
2280 glEnable GL_BLEND;
2281 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2282 glEnable GL_TEXTURE_2D; 2278 glEnable GL_TEXTURE_2D;
2283 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2279 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2284 glColor 1, 1, 1, 1; 2280 glColor 1, 1, 1, 1;
2285 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2281 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2286 glDisable GL_TEXTURE_2D; 2282 glDisable GL_TEXTURE_2D;
2287 glDisable GL_BLEND;
2288 2283
2289 $self->{children}[1]->draw; 2284 $self->{children}[1]->draw;
2290 2285
2291} 2286}
2292 2287
2386 @_, 2381 @_,
2387 can_events => 0, 2382 can_events => 0,
2388 ) 2383 )
2389} 2384}
2390 2385
2391sub set_markup { 2386sub set_tooltip_from {
2392 my ($self, $text) = @_; 2387 my ($self, $widget) = @_;
2393 2388
2394 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2389 $self->add (new CFClient::UI::Label
2395 $self->{label}->set_markup ($text); 2390 markup => $widget->{tooltip},
2396 $self->add ($self->{label}); 2391 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2392 fontsize => 0.8,
2393 fg => [0, 0, 0, 1],
2394 font => ($widget->{tooltip_font} || $::FONT_PROP),
2395 );
2397} 2396}
2398 2397
2399sub size_request { 2398sub size_request {
2400 my ($self) = @_; 2399 my ($self) = @_;
2401
2402 $self->child->set_max_size ($::WIDTH * 0.3);
2403 2400
2404 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2401 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2405 2402
2406 ($w + 4, $h + 4) 2403 ($w + 4, $h + 4)
2407} 2404}
2413} 2410}
2414 2411
2415sub _draw { 2412sub _draw {
2416 my ($self) = @_; 2413 my ($self) = @_;
2417 2414
2418 glPushMatrix;
2419 glTranslate 0.375, 0.375; 2415 glTranslate 0.375, 0.375;
2420 2416
2421 my ($w, $h) = @$self{qw(w h)}; 2417 my ($w, $h) = @$self{qw(w h)};
2422 2418
2423 glColor 1, 0.8, 0.4; 2419 glColor 1, 0.8, 0.4;
2434 glVertex 0 , $h; 2430 glVertex 0 , $h;
2435 glVertex $w, $h; 2431 glVertex $w, $h;
2436 glVertex $w, 0; 2432 glVertex $w, 0;
2437 glEnd; 2433 glEnd;
2438 2434
2439 glPopMatrix; 2435 glTranslate 2 - 0.375, 2 - 0.375;
2440
2441 glTranslate 2, 2;
2442 $self->SUPER::_draw; 2436 $self->SUPER::_draw;
2443} 2437}
2444 2438
2445############################################################################# 2439#############################################################################
2446 2440
2464} 2458}
2465 2459
2466sub _draw { 2460sub _draw {
2467 my ($self) = @_; 2461 my ($self) = @_;
2468 2462
2463 return unless $::CONN;#d# manage and cache textures differently
2469 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2464 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2470 2465
2471 # TODO animation 2466 # TODO animation
2472 if ($tex) { 2467 if ($tex) {
2473 glEnable GL_BLEND;
2474 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2475 glEnable GL_TEXTURE_2D; 2468 glEnable GL_TEXTURE_2D;
2476 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2469 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2477 glColor 1, 1, 1, 1; 2470 glColor 1, 1, 1, 1;
2478 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2471 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2479 glDisable GL_TEXTURE_2D; 2472 glDisable GL_TEXTURE_2D;
2480 glDisable GL_BLEND;
2481 } 2473 }
2482} 2474}
2483 2475
2484############################################################################# 2476#############################################################################
2485 2477
2486package CFClient::UI::Menu; 2478package CFClient::UI::InventoryItem;
2487 2479
2488our @ISA = CFClient::UI::FancyFrame::; 2480our @ISA = CFClient::UI::HBox::;
2489
2490use CFClient::OpenGL;
2491 2481
2492sub new { 2482sub new {
2493 my $class = shift; 2483 my $class = shift;
2494 2484
2495 my $self = $class->SUPER::new (
2496 items => [],
2497 z => 100,
2498 @_,
2499 );
2500
2501 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2502
2503 for my $item (@{ $self->{items} }) {
2504 my ($widget, $cb) = @$item;
2505
2506 # handle various types of items, only text for now
2507 if (!ref $widget) {
2508 $widget = new CFClient::UI::Label
2509 can_hover => 1,
2510 can_events => 1,
2511 text => $widget;
2512 }
2513
2514 $self->{item}{$widget} = $item;
2515
2516 $self->{vbox}->add ($widget);
2517 }
2518
2519 $self
2520}
2521
2522# popup given the event (must be a mouse button down event currently)
2523sub popup {
2524 my ($self, $ev) = @_;
2525
2526 $self->emit ("popdown");
2527
2528 # maybe save $GRAB? must be careful about events...
2529 $GRAB = $self;
2530 $self->{button} = $ev->{button};
2531
2532 $self->show;
2533 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2534}
2535
2536sub mouse_motion {
2537 my ($self, $ev, $x, $y) = @_;
2538
2539 # TODO: should use vbox->find_widget or so
2540 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2541 $self->{hover} = $self->{item}{$HOVER};
2542}
2543
2544sub button_up {
2545 my ($self, $ev, $x, $y) = @_;
2546
2547 if ($ev->{button} == $self->{button}) {
2548 undef $GRAB;
2549 $self->hide;
2550
2551 $self->emit ("popdown");
2552 $self->{hover}[1]->() if $self->{hover};
2553 }
2554}
2555
2556#############################################################################
2557
2558package CFClient::UI::Root;
2559
2560our @ISA = CFClient::UI::Container::;
2561
2562use CFClient::OpenGL;
2563
2564sub check_size {
2565 my ($self) = @_;
2566
2567 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2568}
2569
2570sub size_request {
2571 ($::WIDTH, $::HEIGHT)
2572}
2573
2574sub configure {
2575 my ($self, $x, $y, $w, $h) = @_;
2576
2577 $self->SUPER::configure ($x, $y, $w, $h);
2578
2579 for my $child (@{$self->{children}}) {
2580 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2581
2582 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2583 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2584 $child->configure ($X, $Y, $W,$H);
2585 }
2586}
2587
2588sub _topleft {
2589 my ($self, $x, $y) = @_;
2590
2591 ($x, $y)
2592}
2593
2594sub update {
2595 my ($self) = @_;
2596
2597 $self->check_size;
2598 ::refresh ();
2599}
2600
2601sub add {
2602 my ($self, $child) = @_;
2603
2604 # integerize window positions
2605 $child->{x} = int $child->{x};
2606 $child->{y} = int $child->{y};
2607
2608 $self->SUPER::add ($child);
2609}
2610
2611sub on_refresh {
2612 my ($self, $id, $cb) = @_;
2613
2614 $self->{refresh_hook}{$id} = $cb;
2615}
2616
2617sub draw {
2618 my ($self) = @_;
2619
2620 while (my $rcb = delete $self->{refresh_hook}) {
2621 $_->() for values %$rcb;
2622 }
2623
2624 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2625 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2626 glClear GL_COLOR_BUFFER_BIT;
2627
2628 glMatrixMode GL_PROJECTION;
2629 glLoadIdentity;
2630 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2631 glMatrixMode GL_MODELVIEW;
2632 glLoadIdentity;
2633
2634 $self->_draw;
2635}
2636
2637#############################################################################
2638
2639package CFClient::UI::InventoryItem;
2640
2641our @ISA = CFClient::UI::HBox::;
2642
2643sub new {
2644 my $class = shift;
2645
2646 my %args = @_; 2485 my %args = @_;
2647 2486
2648 my $item = $args{item}; 2487 my $item = delete $args{item};
2649 2488
2650 my $desc = $item->{nrof} < 2 2489 my $desc = $item->{nrof} < 2
2651 ? $item->{name} 2490 ? $item->{name}
2652 : "$item->{nrof} $item->{name_pl}"; 2491 : "$item->{nrof} $item->{name_pl}";
2653 2492
2682 2521
2683 1 2522 1
2684 }, 2523 },
2685 %args 2524 %args
2686 ); 2525 );
2526
2687 $self->add(new CFClient::UI::Face 2527 $self->add (new CFClient::UI::Face
2688 can_events => 0, 2528 can_events => 0,
2689 face => $item->{face}, 2529 face => $item->{face},
2690 anim => $item->{anim}, 2530 anim => $item->{anim},
2691 animspeed => $item->{animspeed}); 2531 animspeed => $item->{animspeed},
2532 );
2533
2692 $self->add(new CFClient::UI::Label 2534 $self->add (new CFClient::UI::Label
2693 can_events => 0, 2535 can_events => 0,
2694 text => $desc); 2536 text => $desc,
2537 );
2695 2538
2696 $self 2539 $self
2697} 2540}
2698 2541
2699############################################################################# 2542#############################################################################
2717 my ($self, $items) = @_; 2560 my ($self, $items) = @_;
2718 2561
2719 $self->{scrolled}->clear; 2562 $self->{scrolled}->clear;
2720 return unless $items; 2563 return unless $items;
2721 2564
2722 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2565 my @items = sort {
2566 ($a->{type} <=> $b->{type})
2567 or ($a->{name} cmp $b->{name})
2568 } @$items;
2723 2569
2724 $self->{real_items} = \@items; 2570 $self->{real_items} = \@items;
2725 2571
2726 for my $item (@items) { 2572 for my $item (@items) {
2727 my $desc = $item->{nrof} < 2 2573 my $desc = $item->{nrof} < 2
2728 ? $item->{name} 2574 ? $item->{name}
2729 : "$item->{nrof} $item->{name_pl}"; 2575 : "$item->{nrof} $item->{name_pl}";
2730 2576
2731 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2577 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2732 } 2578 }
2579
2580 $self->{scrolled}->add (@items);
2733 2581
2734# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2582# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2735} 2583}
2736 2584
2737sub size_request { 2585sub size_request {
2738 my ($self) = @_; 2586 my ($self) = @_;
2739 ($self->{req_w}, $self->{req_h}); 2587 ($self->{req_w}, $self->{req_h});
2588}
2589
2590#############################################################################
2591
2592package CFClient::UI::Menu;
2593
2594our @ISA = CFClient::UI::FancyFrame::;
2595
2596use CFClient::OpenGL;
2597
2598sub new {
2599 my $class = shift;
2600
2601 my $self = $class->SUPER::new (
2602 items => [],
2603 z => 100,
2604 @_,
2605 );
2606
2607 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2608
2609 for my $item (@{ $self->{items} }) {
2610 my ($widget, $cb) = @$item;
2611
2612 # handle various types of items, only text for now
2613 if (!ref $widget) {
2614 $widget = new CFClient::UI::Label
2615 can_hover => 1,
2616 can_events => 1,
2617 text => $widget;
2618 }
2619
2620 $self->{item}{$widget} = $item;
2621
2622 $self->{vbox}->add ($widget);
2623 }
2624
2625 $self
2626}
2627
2628# popup given the event (must be a mouse button down event currently)
2629sub popup {
2630 my ($self, $ev) = @_;
2631
2632 $self->emit ("popdown");
2633
2634 # maybe save $GRAB? must be careful about events...
2635 $GRAB = $self;
2636 $self->{button} = $ev->{button};
2637
2638 $self->show;
2639 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2640}
2641
2642sub mouse_motion {
2643 my ($self, $ev, $x, $y) = @_;
2644
2645 # TODO: should use vbox->find_widget or so
2646 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2647 $self->{hover} = $self->{item}{$HOVER};
2648}
2649
2650sub button_up {
2651 my ($self, $ev, $x, $y) = @_;
2652
2653 if ($ev->{button} == $self->{button}) {
2654 undef $GRAB;
2655 $self->hide;
2656
2657 $self->emit ("popdown");
2658 $self->{hover}[1]->() if $self->{hover};
2659 }
2660}
2661
2662#############################################################################
2663
2664package CFClient::UI::Statusbox;
2665
2666our @ISA = CFClient::UI::VBox::;
2667
2668sub reorder {
2669 my ($self) = @_;
2670 my $NOW = time;
2671
2672 while (my ($k, $v) = each %{ $self->{item} }) {
2673 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2674 }
2675
2676 my @widgets;
2677
2678 my @items = sort {
2679 $a->{pri} <=> $b->{pri}
2680 or $b->{id} <=> $a->{id}
2681 } values %{ $self->{item} };
2682
2683 my $count = 10 + 1;
2684 for my $item (@items) {
2685 last unless --$count;
2686
2687 push @widgets, $item->{label} ||= do {
2688 # TODO: doesn't handle markup well (read as: at all)
2689 my $short = $item->{count} > 1
2690 ? "<b>$item->{count} ×</b> $item->{text}"
2691 : $item->{text};
2692
2693 for ($short) {
2694 s/^\s+//;
2695 s/\012.*/…/s;
2696 my $len = int 40 / $item->{fontsize};
2697 substr $_, $len, length, "…" if $len < length;
2698 }
2699
2700 new CFClient::UI::Label
2701 markup => $short,
2702 tooltip => $item->{tooltip},
2703 tooltip_font => $::FONT_PROP,
2704 tooltip_width => 0.67,
2705 fontsize => $item->{fontsize},
2706 color => $item->{color},
2707 can_events => 1,
2708 can_hover => 1
2709 };
2710 }
2711
2712 $self->clear;
2713 $self->SUPER::add (reverse @widgets);
2714}
2715
2716sub add {
2717 my ($self, $text, %arg) = @_;
2718
2719 $text =~ s/^\s+//;
2720 $text =~ s/\s+$//;
2721
2722 my $timeout = time + ((delete $arg{timeout}) || 60);
2723
2724 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2725
2726 if (my $item = $self->{item}{$group}) {
2727 if ($item->{text} eq $text) {
2728 $item->{count}++;
2729 } else {
2730 $item->{count} = 1;
2731 $item->{text} = $item->{tooltip} = $text;
2732 }
2733 $item->{id} = ++$self->{id};
2734 $item->{timeout} = $timeout;
2735 delete $item->{label};
2736 } else {
2737 $self->{item}{$group} = {
2738 id => ++$self->{id},
2739 text => $text,
2740 timeout => $timeout,
2741 tooltip => $text,
2742 fontsize => 0.8,
2743 color => [0.8, 0.8, 0.8, 0.8],
2744 pri => 0,
2745 count => 1,
2746 %arg,
2747 };
2748 }
2749
2750 $self->reorder;
2751}
2752
2753#############################################################################
2754
2755package CFClient::UI::Root;
2756
2757our @ISA = CFClient::UI::Container::;
2758
2759use CFClient::OpenGL;
2760
2761sub new {
2762 my $class = shift;
2763
2764 $class->SUPER::new (
2765 @_,
2766 )
2767}
2768
2769sub configure {
2770 my ($self, $x, $y, $w, $h) = @_;
2771
2772 $self->{w} = $w;
2773 $self->{h} = $h;
2774}
2775
2776sub check_size {
2777 my ($self) = @_;
2778
2779 $self->size_allocate ($self->{w}, $self->{h})
2780 if $self->{w};
2781}
2782
2783sub size_request {
2784 my ($self) = @_;
2785
2786 ($self->{w}, $self->{h})
2787}
2788
2789sub size_allocate {
2790 my ($self, $w, $h) = @_;
2791
2792 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2793 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2794
2795 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2796 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2797
2798 for my $child ($self->children) {
2799 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2800
2801 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2802 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2803 $child->configure ($X, $Y, $W, $H);
2804 }
2805}
2806
2807sub coord2local {
2808 my ($self, $x, $y) = @_;
2809
2810 ($x, $y)
2811}
2812
2813sub coord2global {
2814 my ($self, $x, $y) = @_;
2815
2816 ($x, $y)
2817}
2818
2819sub update {
2820 my ($self) = @_;
2821
2822 $self->check_size;
2823 $::WANT_REFRESH++;
2824}
2825
2826sub add {
2827 my ($self, @children) = @_;
2828
2829 for my $child (@children) {
2830 $child->{toplevel} = 1;
2831
2832 # integerise window positions
2833 $child->{x} = int $child->{x};
2834 $child->{y} = int $child->{y};
2835 }
2836
2837 $self->SUPER::add (@children);
2838}
2839
2840sub on_refresh {
2841 my ($self, $id, $cb) = @_;
2842
2843 $self->{refresh_hook}{$id} = $cb;
2844}
2845
2846sub on_post_alloc {
2847 my ($self, $id, $cb) = @_;
2848
2849 $self->{post_alloc_hook}{$id} = $cb;
2850}
2851
2852sub draw {
2853 my ($self) = @_;
2854
2855 while ($self->{refresh_hook}) {
2856 $_->()
2857 for values %{delete $self->{refresh_hook}};
2858 }
2859
2860 if ($self->{check_size}) {
2861 my @queue = ([], []);
2862
2863 for (;;) {
2864 if ($self->{check_size}) {
2865 # heuristic: check containers last
2866 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2867 for values %{delete $self->{check_size}}
2868 }
2869
2870 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2871
2872 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2873 ? @$widget{qw(user_w user_h)}
2874 : $widget->size_request;
2875
2876 if (delete $widget->{force_alloc}
2877 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2878 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2879
2880 $widget->{req_w} = $w;
2881 $widget->{req_h} = $h;
2882
2883 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2884
2885 $widget->{parent}->check_size
2886 if $widget->{parent};
2887 }
2888 }
2889 }
2890
2891 while ($self->{size_alloc}) {
2892 for (values %{delete $self->{size_alloc}}) {
2893 my ($widget, $w, $h) = @$_;
2894
2895 $w = 0 if $w < 0;
2896 $h = 0 if $h < 0;
2897
2898 $widget->{w} = $w;
2899 $widget->{h} = $h;
2900 $widget->size_allocate ($w, $h);
2901 $widget->emit (size_allocate => $w, $h);
2902 }
2903 }
2904
2905 while ($self->{post_alloc_hook}) {
2906 $_->()
2907 for values %{delete $self->{post_alloc_hook}};
2908 }
2909
2910 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2911 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2912 glClear GL_COLOR_BUFFER_BIT;
2913
2914 glMatrixMode GL_PROJECTION;
2915 glLoadIdentity;
2916 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2917 glMatrixMode GL_MODELVIEW;
2918 glLoadIdentity;
2919
2920 $self->_draw;
2740} 2921}
2741 2922
2742############################################################################# 2923#############################################################################
2743 2924
2744package CFClient::UI; 2925package CFClient::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines