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.185 by root, Mon May 8 17:23:08 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
739 $self->{view_y} = int $y; 729 $self->{view_y} = int $y;
740 730
741 $self->update; 731 $self->update;
742} 732}
743 733
744# hmm, this does not work for topleft of $self... but we should not aks for that 734# hmm, this does not work for topleft of $self... but we should not ask for that
745sub _topleft { 735sub coord2local {
746 my ($self, $x, $y) = @_; 736 my ($self, $x, $y) = @_;
747 737
748 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) 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)
749} 748}
750 749
751sub find_widget { 750sub find_widget {
752 my ($self, $x, $y) = @_; 751 my ($self, $x, $y) = @_;
753 752
798 $self->add ($self->{slider}); 797 $self->add ($self->{slider});
799 798
800 $self 799 $self
801} 800}
802 801
803#TODO# update range on size_allocate depeneing on child 802#TODO# update range on size_allocate depending on child
804# update viewport offset on scroll 803# update viewport offset on scroll
805 804
806############################################################################# 805#############################################################################
807 806
808package CFClient::UI::Frame; 807package CFClient::UI::Frame;
812use CFClient::OpenGL; 811use CFClient::OpenGL;
813 812
814sub new { 813sub new {
815 my $class = shift; 814 my $class = shift;
816 815
817 my $self = $class->SUPER::new ( 816 $class->SUPER::new (
818 bg => [1, 1, 1, 1], 817 bg => undef,
819 border_bg => [1, 1, 1, 1],
820 border => 0.8,
821 @_ 818 @_,
822 ); 819 )
823
824 $self
825} 820}
826 821
827sub _draw { 822sub _draw {
828 my ($self) = @_; 823 my ($self) = @_;
829 824
830 my ($w, $h) = ($self->{w}, $self->{h}); 825 if ($self->{bg}) {
826 my ($w, $h) = @$self{qw(w h)};
831 827
832 glEnable GL_BLEND; 828 glEnable GL_BLEND;
833 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 829 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
834 glEnable GL_TEXTURE_2D; 830 glColor @{ $self->{bg} };
835 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
836 831
837# glBegin GL_QUADS; 832 glBegin GL_QUADS;
838# glColor 0, 0, 0, 0;
839# glVertex 0 , 0; 833 glVertex 0 , 0;
840# glVertex 0 , $h; 834 glVertex 0 , $h;
841# glVertex $w, $h; 835 glVertex $w, $h;
842# glVertex $w, 0; 836 glVertex $w, 0;
843# glEnd; 837 glEnd;
844 838
845
846 $self->child->draw;
847 glDisable GL_BLEND; 839 glDisable GL_BLEND;
848 glDisable GL_TEXTURE_2D; 840 }
841
842 $self->SUPER::_draw;
849} 843}
850 844
851############################################################################# 845#############################################################################
852 846
853package CFClient::UI::FancyFrame; 847package CFClient::UI::FancyFrame;
867 861
868 my $self = $class->SUPER::new ( 862 my $self = $class->SUPER::new (
869 bg => [1, 1, 1, 1], 863 bg => [1, 1, 1, 1],
870 border_bg => [1, 1, 1, 1], 864 border_bg => [1, 1, 1, 1],
871 border => 0.6, 865 border => 0.6,
866 toplevel => 1,
872 can_events => 1, 867 can_events => 1,
873 @_ 868 @_
874 ); 869 );
875 870
876 $self->{title} &&= new CFClient::UI::Label 871 $self->{title} &&= new CFClient::UI::Label
969 my ($self) = @_; 964 my ($self) = @_;
970 965
971 my ($w, $h ) = ($self->{w}, $self->{h}); 966 my ($w, $h ) = ($self->{w}, $self->{h});
972 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 967 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
973 968
974 glEnable GL_BLEND;
975 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
976 glEnable GL_TEXTURE_2D; 969 glEnable GL_TEXTURE_2D;
977 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 970 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
978 971
979 my $border = $self->border; 972 my $border = $self->border;
980 973
981 glColor @{ $self->{border_bg} }; 974 glColor @{ $self->{border_bg} };
982 $tex[1]->draw_quad (0, 0, $w, $border); 975 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
983 $tex[3]->draw_quad (0, $border, $border, $ch); 976 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
984 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 977 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
985 $tex[4]->draw_quad (0, $h - $border, $w, $border); 978 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
986 979
987 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 980 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
988 my $bg = $tex[0]; 981 my $bg = $tex[0];
989 982
990 # TODO: repeat texture not scale 983 # TODO: repeat texture not scale
994 glColor @{ $self->{bg} }; 987 glColor @{ $self->{bg} };
995 988
996 $bg->{s} = $rep_x; 989 $bg->{s} = $rep_x;
997 $bg->{t} = $rep_y; 990 $bg->{t} = $rep_y;
998 $bg->{wrap_mode} = 1; 991 $bg->{wrap_mode} = 1;
999 $bg->draw_quad ($border, $border, $cw, $ch); 992 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
993 }
1000 994
1001 glDisable GL_TEXTURE_2D; 995 glDisable GL_TEXTURE_2D;
1002 glDisable GL_BLEND;
1003 }
1004 996
1005 $self->{title}->draw if $self->{title}; 997 $self->{title}->draw if $self->{title};
1006 998
1007 $self->child->draw; 999 $self->child->draw;
1008} 1000}
1235} 1227}
1236 1228
1237sub size_allocate { 1229sub size_allocate {
1238 my ($self, $w, $h) = @_; 1230 my ($self, $w, $h) = @_;
1239 1231
1232 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1233
1240 my $children = $self->{children}; 1234 my $children = $self->{children};
1241 1235
1242 my @h = map $_->{req_h}, @$children; 1236 my @h = map $_->{req_h}, @$children;
1243 1237
1244 my $req_h = List::Util::sum @h; 1238 my $req_h = List::Util::sum @h;
1283 my ($class, %arg) = @_; 1277 my ($class, %arg) = @_;
1284 1278
1285 my $self = $class->SUPER::new ( 1279 my $self = $class->SUPER::new (
1286 fg => [1, 1, 1], 1280 fg => [1, 1, 1],
1287 #font => default_font 1281 #font => default_font
1282 #text => initial text
1283 #markup => initial narkup
1284 layout => (new CFClient::Layout),
1288 fontsize => 1, 1285 fontsize => 1,
1289 text => "",
1290 align => -1, 1286 align => -1,
1291 valign => -1, 1287 valign => -1,
1292 padding => 2, 1288 padding => 2,
1293 layout => new CFClient::Layout,
1294 can_events => 0, 1289 can_events => 0,
1295 %arg 1290 %arg
1296 ); 1291 );
1297 1292
1298 if (exists $self->{template}) { 1293 if (exists $self->{template}) {
1299 my $layout = new CFClient::Layout; 1294 my $layout = new CFClient::Layout;
1300 $layout->set_text (delete $self->{template}); 1295 $layout->set_text (delete $self->{template});
1301 $self->{template} = $layout; 1296 $self->{template} = $layout;
1302 } 1297 }
1303 1298
1304 $self->set_text (delete $self->{text}) if exists $self->{text}; 1299 if (exists $self->{markup}) {
1305 $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 }
1306 1304
1307 $self 1305 $self
1308} 1306}
1309 1307
1310sub escape { 1308sub escape {
1322 1320
1323 delete $self->{texture}; 1321 delete $self->{texture};
1324 $self->SUPER::update; 1322 $self->SUPER::update;
1325} 1323}
1326 1324
1327sub reconfigure {
1328 my ($self) = @_;
1329
1330 delete $self->{texture};
1331}
1332
1333sub set_text { 1325sub set_text {
1334 my ($self, $text) = @_; 1326 my ($self, $text) = @_;
1335 1327
1336 return if $self->{text} eq "T$text"; 1328 return if $self->{text} eq "T$text";
1337 $self->{text} = "T$text"; 1329 $self->{text} = "T$text";
1338 1330
1331 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1339 $self->{layout}->set_text ($text); 1332 $self->{layout}->set_text ($text);
1340 1333
1341 delete $self->{texture};
1342 $self->update; 1334 $self->update;
1343 $self->check_size; 1335 $self->check_size;
1344} 1336}
1345 1337
1346sub set_markup { 1338sub set_markup {
1347 my ($self, $markup) = @_; 1339 my ($self, $markup) = @_;
1348 1340
1349 return if $self->{text} eq "M$markup"; 1341 return if $self->{text} eq "M$markup";
1350 $self->{text} = "M$markup"; 1342 $self->{text} = "M$markup";
1351 1343
1344 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1345
1346 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1352 $self->{layout}->set_markup ($markup); 1347 $self->{layout}->set_markup ($markup);
1353 1348
1354 delete $self->{texture};
1355 $self->update; 1349 $self->update;
1356 $self->check_size; 1350 $self->check_size;
1357} 1351}
1358 1352
1359sub size_request { 1353sub size_request {
1390sub set_fontsize { 1384sub set_fontsize {
1391 my ($self, $fontsize) = @_; 1385 my ($self, $fontsize) = @_;
1392 1386
1393 $self->{fontsize} = $fontsize; 1387 $self->{fontsize} = $fontsize;
1394 delete $self->{texture}; 1388 delete $self->{texture};
1389
1390 $self->update;
1395 $self->check_size; 1391 $self->check_size;
1396 $self->update;
1397} 1392}
1398 1393
1399sub _draw { 1394sub _draw {
1400 my ($self) = @_; 1395 my ($self) = @_;
1401 1396
1402 my $tex = $self->{texture} ||= do { 1397 my $tex = $self->{texture} ||= do {
1398 $self->{layout}->set_foreground (@{$self->{fg}});
1403 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1399 $self->{layout}->set_font ($self->{font}) if $self->{font};
1404 $self->{layout}->set_width ($self->{w}); 1400 $self->{layout}->set_width ($self->{w});
1405 $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
1406 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
1407 }; 1414 };
1408 1415
1409 glEnable GL_BLEND;
1410 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1411 glEnable GL_TEXTURE_2D; 1416 glEnable GL_TEXTURE_2D;
1412 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1413 1418
1419 if ($tex->{format} == GL_ALPHA) {
1414 glColor @{$self->{fg}}; 1420 glColor @{$self->{fg}};
1415
1416 $self->{ox} = int (
1417 $self->{align} < 0 ? $self->{padding}
1418 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1419 : ($self->{w} - $tex->{w}) * 0.5
1420 );
1421
1422 $self->{oy} = int (
1423 $self->{valign} < 0 ? $self->{padding}
1424 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1425 : ($self->{h} - $tex->{h}) * 0.5
1426 );
1427
1428 $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 }
1429 1425
1430 glDisable GL_TEXTURE_2D; 1426 glDisable GL_TEXTURE_2D;
1431 glDisable GL_BLEND;
1432} 1427}
1433 1428
1434############################################################################# 1429#############################################################################
1435 1430
1436package CFClient::UI::EntryBase; 1431package CFClient::UI::EntryBase;
1471 $self->{layout}->set_text ("$text "); 1466 $self->{layout}->set_text ("$text ");
1472 1467
1473 $self->emit (changed => $self->{text}); 1468 $self->emit (changed => $self->{text});
1474} 1469}
1475 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
1476sub get_text { 1480sub get_text {
1477 $_[0]{text} 1481 $_[0]{text}
1478} 1482}
1479 1483
1480sub size_request { 1484sub size_request {
1487 1491
1488sub size_allocate { 1492sub size_allocate {
1489 my ($self, $w, $h) = @_; 1493 my ($self, $w, $h) = @_;
1490 1494
1491 $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
1492}
1493
1494sub set_text {
1495 my ($self, $text) = @_;
1496
1497 $self->{cursor} = length $text;
1498 $self->_set_text ($text);
1499 $self->update;
1500} 1496}
1501 1497
1502sub key_down { 1498sub key_down {
1503 my ($self, $ev) = @_; 1499 my ($self, $ev) = @_;
1504 1500
1506 my $sym = $ev->{sym}; 1502 my $sym = $ev->{sym};
1507 my $uni = $ev->{unicode}; 1503 my $uni = $ev->{unicode};
1508 1504
1509 my $text = $self->get_text; 1505 my $text = $self->get_text;
1510 1506
1511 if ($sym == 8) { 1507 if ($uni == 8) {
1512 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1508 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1513 } elsif ($sym == 127) { 1509 } elsif ($uni == 127) {
1514 substr $text, $self->{cursor}, 1, ""; 1510 substr $text, $self->{cursor}, 1, "";
1515 } elsif ($sym == CFClient::SDLK_LEFT) { 1511 } elsif ($sym == CFClient::SDLK_LEFT) {
1516 --$self->{cursor} if $self->{cursor}; 1512 --$self->{cursor} if $self->{cursor};
1517 } elsif ($sym == CFClient::SDLK_RIGHT) { 1513 } elsif ($sym == CFClient::SDLK_RIGHT) {
1518 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1514 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1519 } elsif ($sym == CFClient::SDLK_HOME) { 1515 } elsif ($sym == CFClient::SDLK_HOME) {
1520 $self->{cursor} = 0; 1516 $self->{cursor} = 0;
1521 } elsif ($sym == CFClient::SDLK_END) { 1517 } elsif ($sym == CFClient::SDLK_END) {
1522 $self->{cursor} = length $text; 1518 $self->{cursor} = length $text;
1523 } elsif ($sym == 27) { 1519 } elsif ($uni == 27) {
1524 $self->emit ('escape'); 1520 $self->emit ('escape');
1525 } elsif ($uni) { 1521 } elsif ($uni) {
1526 substr $text, $self->{cursor}++, 0, chr $uni; 1522 substr $text, $self->{cursor}++, 0, chr $uni;
1527 } 1523 }
1528 1524
1692 1688
1693 if ($GRAB == $self) { 1689 if ($GRAB == $self) {
1694 $self->{fg} = $self->{active_fg}; 1690 $self->{fg} = $self->{active_fg};
1695 } 1691 }
1696 1692
1697 glEnable GL_BLEND;
1698 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1699 glEnable GL_TEXTURE_2D; 1693 glEnable GL_TEXTURE_2D;
1700 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1701 glColor 0, 0, 0, 1; 1695 glColor 0, 0, 0, 1;
1702 1696
1703 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1697 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1704 1698
1705 glDisable GL_TEXTURE_2D; 1699 glDisable GL_TEXTURE_2D;
1706 glDisable GL_BLEND;
1707 1700
1708 $self->SUPER::_draw; 1701 $self->SUPER::_draw;
1709} 1702}
1710 1703
1711############################################################################# 1704#############################################################################
1758 1751
1759 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;
1760 1753
1761 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1754 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1762 1755
1763 glEnable GL_BLEND; 1756 my $tex = $self->{state} ? $tex[1] : $tex[0];
1757
1764 glEnable GL_TEXTURE_2D; 1758 glEnable GL_TEXTURE_2D;
1765 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1766
1767 my $tex = $self->{state} ? $tex[1] : $tex[0];
1768
1769 $tex->draw_quad (0, 0, $s, $s); 1759 $tex->draw_quad_alpha (0, 0, $s, $s);
1770
1771 glDisable GL_TEXTURE_2D; 1760 glDisable GL_TEXTURE_2D;
1772 glDisable GL_BLEND;
1773} 1761}
1774 1762
1775############################################################################# 1763#############################################################################
1776 1764
1777package CFClient::UI::Image; 1765package CFClient::UI::Image;
1820 glTranslate 0, -$self->{w}, 0; 1808 glTranslate 0, -$self->{w}, 0;
1821 1809
1822 ($w, $h) = ($h, $w); 1810 ($w, $h) = ($h, $w);
1823 } 1811 }
1824 1812
1825 glEnable GL_BLEND;
1826 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1827 glEnable GL_TEXTURE_2D; 1813 glEnable GL_TEXTURE_2D;
1828 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1814 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1829 1815
1830 $tex->draw_quad (0, 0, $w, $h); 1816 $tex->draw_quad_alpha (0, 0, $w, $h);
1831 1817
1832 glDisable GL_BLEND;
1833 glDisable GL_TEXTURE_2D; 1818 glDisable GL_TEXTURE_2D;
1834} 1819}
1835 1820
1836############################################################################# 1821#############################################################################
1837 1822
2129 $page ||= 2; 2114 $page ||= 2;
2130 2115
2131 my $knob_a = $inner_pad_px + ($value - $page * 0.5); 2116 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2132 my $knob_b = $inner_pad_px + ($value + $page * 0.5); 2117 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2133 2118
2134 glEnable GL_BLEND;
2135 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2136 glEnable GL_TEXTURE_2D; 2119 glEnable GL_TEXTURE_2D;
2137 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2120 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2138 2121
2139 # draw background 2122 # draw background
2140 $tex[1]->draw_quad (0, 0, $w, $h); 2123 $tex[1]->draw_quad_alpha (0, 0, $w, $h);
2141 2124
2142 # draw handle 2125 # draw handle
2143 $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);
2144 2127
2145 glDisable GL_BLEND;
2146 glDisable GL_TEXTURE_2D; 2128 glDisable GL_TEXTURE_2D;
2147} 2129}
2148 2130
2149############################################################################# 2131#############################################################################
2150 2132
2161 fontsize => 1, 2143 fontsize => 1,
2162 can_events => 0, 2144 can_events => 0,
2163 #font => default_font 2145 #font => default_font
2164 @_, 2146 @_,
2165 2147
2166 layout => (new CFClient::Layout), 2148 layout => (new CFClient::Layout 1),
2167 par => [], 2149 par => [],
2168 height => 0, 2150 height => 0,
2169 children => [ 2151 children => [
2170 (new CFClient::UI::Empty expand => 1), 2152 (new CFClient::UI::Empty expand => 1),
2171 (new CFClient::UI::Slider vertical => 1), 2153 (new CFClient::UI::Slider vertical => 1),
2189 2171
2190 my $layout = $self->{layout}; 2172 my $layout = $self->{layout};
2191 2173
2192 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2174 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2193 $layout->set_width ($self->{children}[0]{w}); 2175 $layout->set_width ($self->{children}[0]{w});
2194 $layout->set_text ($text); 2176 $layout->set_markup ($text);
2195 2177
2196 ($layout->size)[1] 2178 ($layout->size)[1]
2197} 2179}
2198 2180
2199sub reflow { 2181sub reflow {
2237 2219
2238 return unless $self->{h} > 0; 2220 return unless $self->{h} > 0;
2239 2221
2240 delete $self->{texture}; 2222 delete $self->{texture};
2241 2223
2242 $ROOT->on_refresh ($self, sub { 2224 $ROOT->on_post_alloc ($self, sub {
2243 if (delete $self->{need_reflow}) { 2225 if (delete $self->{need_reflow}) {
2244 my $height = 0; 2226 my $height = 0;
2245 2227
2246 $height += $_->[0] = $self->text_height ($_->[2]) 2228 $height += $_->[0] = $self->text_height ($_->[2])
2247 for @{$self->{par}}; 2229 for @{$self->{par}};
2255 2237
2256 $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 {
2257 glClearColor 0, 0, 0, 0; 2239 glClearColor 0, 0, 0, 0;
2258 glClear GL_COLOR_BUFFER_BIT; 2240 glClear GL_COLOR_BUFFER_BIT;
2259 2241
2260 glEnable GL_BLEND;
2261 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2262 glEnable GL_TEXTURE_2D; 2242 glEnable GL_TEXTURE_2D;
2263 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2243 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2264 2244
2265 my $top = int $self->{children}[1]{range}[0]; 2245 my $top = int $self->{children}[1]{range}[0];
2266 2246
2275 2255
2276 for my $par (@{$self->{par}}) { 2256 for my $par (@{$self->{par}}) {
2277 my $h = $par->[0]; 2257 my $h = $par->[0];
2278 2258
2279 if ($y0 < $y + $h && $y < $y1) { 2259 if ($y0 < $y + $h && $y < $y1) {
2260 $layout->set_foreground (@{ $par->[1] });
2280 $layout->set_text ($par->[2]); 2261 $layout->set_markup ($par->[2]);
2281 2262
2282 glColor @{ $par->[1] };
2283 my ($W, $H) = $layout->size; 2263 my ($W, $H) = $layout->size;
2284 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);
2285 } 2265 }
2286 2266
2287 $y += $h; 2267 $y += $h;
2288 } 2268 }
2289 2269
2290 glDisable GL_TEXTURE_2D; 2270 glDisable GL_TEXTURE_2D;
2291 glDisable GL_BLEND;
2292 }; 2271 };
2293 }); 2272 });
2294} 2273}
2295 2274
2296sub _draw { 2275sub _draw {
2297 my ($self) = @_; 2276 my ($self) = @_;
2298 2277
2299 glEnable GL_BLEND;
2300 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2301 glEnable GL_TEXTURE_2D; 2278 glEnable GL_TEXTURE_2D;
2302 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2279 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2303 glColor 1, 1, 1, 1; 2280 glColor 1, 1, 1, 1;
2304 $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});
2305 glDisable GL_TEXTURE_2D; 2282 glDisable GL_TEXTURE_2D;
2306 glDisable GL_BLEND;
2307 2283
2308 $self->{children}[1]->draw; 2284 $self->{children}[1]->draw;
2309 2285
2310} 2286}
2311 2287
2405 @_, 2381 @_,
2406 can_events => 0, 2382 can_events => 0,
2407 ) 2383 )
2408} 2384}
2409 2385
2410sub set_markup { 2386sub set_tooltip_from {
2411 my ($self, $text) = @_; 2387 my ($self, $widget) = @_;
2412 2388
2413 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2389 $self->add (new CFClient::UI::Label
2414 $self->{label}->set_markup ($text); 2390 markup => $widget->{tooltip},
2415 $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 );
2416} 2396}
2417 2397
2418sub size_request { 2398sub size_request {
2419 my ($self) = @_; 2399 my ($self) = @_;
2420
2421 $self->child->set_max_size ($::WIDTH * 0.3);
2422 2400
2423 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2401 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2424 2402
2425 ($w + 4, $h + 4) 2403 ($w + 4, $h + 4)
2426} 2404}
2432} 2410}
2433 2411
2434sub _draw { 2412sub _draw {
2435 my ($self) = @_; 2413 my ($self) = @_;
2436 2414
2437 glPushMatrix;
2438 glTranslate 0.375, 0.375; 2415 glTranslate 0.375, 0.375;
2439 2416
2440 my ($w, $h) = @$self{qw(w h)}; 2417 my ($w, $h) = @$self{qw(w h)};
2441 2418
2442 glColor 1, 0.8, 0.4; 2419 glColor 1, 0.8, 0.4;
2453 glVertex 0 , $h; 2430 glVertex 0 , $h;
2454 glVertex $w, $h; 2431 glVertex $w, $h;
2455 glVertex $w, 0; 2432 glVertex $w, 0;
2456 glEnd; 2433 glEnd;
2457 2434
2458 glPopMatrix; 2435 glTranslate 2 - 0.375, 2 - 0.375;
2459
2460 glTranslate 2, 2;
2461 $self->SUPER::_draw; 2436 $self->SUPER::_draw;
2462} 2437}
2463 2438
2464############################################################################# 2439#############################################################################
2465 2440
2488 return unless $::CONN;#d# manage and cache textures differently 2463 return unless $::CONN;#d# manage and cache textures differently
2489 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2464 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2490 2465
2491 # TODO animation 2466 # TODO animation
2492 if ($tex) { 2467 if ($tex) {
2493 glEnable GL_BLEND;
2494 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2495 glEnable GL_TEXTURE_2D; 2468 glEnable GL_TEXTURE_2D;
2496 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2469 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2497 glColor 1, 1, 1, 1; 2470 glColor 1, 1, 1, 1;
2498 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2471 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2499 glDisable GL_TEXTURE_2D; 2472 glDisable GL_TEXTURE_2D;
2500 glDisable GL_BLEND;
2501 } 2473 }
2502} 2474}
2503 2475
2504############################################################################# 2476#############################################################################
2505 2477
2506package CFClient::UI::Menu; 2478package CFClient::UI::InventoryItem;
2507 2479
2508our @ISA = CFClient::UI::FancyFrame::; 2480our @ISA = CFClient::UI::HBox::;
2509
2510use CFClient::OpenGL;
2511 2481
2512sub new { 2482sub new {
2513 my $class = shift; 2483 my $class = shift;
2514 2484
2515 my $self = $class->SUPER::new (
2516 items => [],
2517 z => 100,
2518 @_,
2519 );
2520
2521 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2522
2523 for my $item (@{ $self->{items} }) {
2524 my ($widget, $cb) = @$item;
2525
2526 # handle various types of items, only text for now
2527 if (!ref $widget) {
2528 $widget = new CFClient::UI::Label
2529 can_hover => 1,
2530 can_events => 1,
2531 text => $widget;
2532 }
2533
2534 $self->{item}{$widget} = $item;
2535
2536 $self->{vbox}->add ($widget);
2537 }
2538
2539 $self
2540}
2541
2542# popup given the event (must be a mouse button down event currently)
2543sub popup {
2544 my ($self, $ev) = @_;
2545
2546 $self->emit ("popdown");
2547
2548 # maybe save $GRAB? must be careful about events...
2549 $GRAB = $self;
2550 $self->{button} = $ev->{button};
2551
2552 $self->show;
2553 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2554}
2555
2556sub mouse_motion {
2557 my ($self, $ev, $x, $y) = @_;
2558
2559 # TODO: should use vbox->find_widget or so
2560 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2561 $self->{hover} = $self->{item}{$HOVER};
2562}
2563
2564sub button_up {
2565 my ($self, $ev, $x, $y) = @_;
2566
2567 if ($ev->{button} == $self->{button}) {
2568 undef $GRAB;
2569 $self->hide;
2570
2571 $self->emit ("popdown");
2572 $self->{hover}[1]->() if $self->{hover};
2573 }
2574}
2575
2576#############################################################################
2577
2578package CFClient::UI::Root;
2579
2580our @ISA = CFClient::UI::Container::;
2581
2582use CFClient::OpenGL;
2583
2584sub check_size {
2585 my ($self) = @_;
2586
2587 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2588}
2589
2590sub size_request {
2591 ($::WIDTH, $::HEIGHT)
2592}
2593
2594sub configure {
2595 my ($self, $x, $y, $w, $h) = @_;
2596
2597 $self->SUPER::configure ($x, $y, $w, $h);
2598
2599 for my $child (@{$self->{children}}) {
2600 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2601
2602 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2603 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2604 $child->configure ($X, $Y, $W,$H);
2605 }
2606}
2607
2608sub _topleft {
2609 my ($self, $x, $y) = @_;
2610
2611 ($x, $y)
2612}
2613
2614sub update {
2615 my ($self) = @_;
2616
2617 $self->check_size;
2618 ::refresh ();
2619}
2620
2621sub add {
2622 my ($self, $child) = @_;
2623
2624 # integerize window positions
2625 $child->{x} = int $child->{x};
2626 $child->{y} = int $child->{y};
2627
2628 $self->SUPER::add ($child);
2629}
2630
2631sub on_refresh {
2632 my ($self, $id, $cb) = @_;
2633
2634 $self->{refresh_hook}{$id} = $cb;
2635}
2636
2637sub draw {
2638 my ($self) = @_;
2639
2640 while (my $rcb = delete $self->{refresh_hook}) {
2641 $_->() for values %$rcb;
2642 }
2643
2644 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2645 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2646 glClear GL_COLOR_BUFFER_BIT;
2647
2648 glMatrixMode GL_PROJECTION;
2649 glLoadIdentity;
2650 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2651 glMatrixMode GL_MODELVIEW;
2652 glLoadIdentity;
2653
2654 $self->_draw;
2655}
2656
2657#############################################################################
2658
2659package CFClient::UI::InventoryItem;
2660
2661our @ISA = CFClient::UI::HBox::;
2662
2663sub new {
2664 my $class = shift;
2665
2666 my %args = @_; 2485 my %args = @_;
2667 2486
2668 my $item = $args{item}; 2487 my $item = delete $args{item};
2669 2488
2670 my $desc = $item->{nrof} < 2 2489 my $desc = $item->{nrof} < 2
2671 ? $item->{name} 2490 ? $item->{name}
2672 : "$item->{nrof} $item->{name_pl}"; 2491 : "$item->{nrof} $item->{name_pl}";
2673 2492
2702 2521
2703 1 2522 1
2704 }, 2523 },
2705 %args 2524 %args
2706 ); 2525 );
2526
2707 $self->add(new CFClient::UI::Face 2527 $self->add (new CFClient::UI::Face
2708 can_events => 0, 2528 can_events => 0,
2709 face => $item->{face}, 2529 face => $item->{face},
2710 anim => $item->{anim}, 2530 anim => $item->{anim},
2711 animspeed => $item->{animspeed}); 2531 animspeed => $item->{animspeed},
2532 );
2533
2712 $self->add(new CFClient::UI::Label 2534 $self->add (new CFClient::UI::Label
2713 can_events => 0, 2535 can_events => 0,
2714 text => $desc); 2536 text => $desc,
2537 );
2715 2538
2716 $self 2539 $self
2717} 2540}
2718 2541
2719############################################################################# 2542#############################################################################
2737 my ($self, $items) = @_; 2560 my ($self, $items) = @_;
2738 2561
2739 $self->{scrolled}->clear; 2562 $self->{scrolled}->clear;
2740 return unless $items; 2563 return unless $items;
2741 2564
2742 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;
2743 2569
2744 $self->{real_items} = \@items; 2570 $self->{real_items} = \@items;
2745 2571
2746 for my $item (@items) { 2572 for my $item (@items) {
2747 my $desc = $item->{nrof} < 2 2573 my $desc = $item->{nrof} < 2
2748 ? $item->{name} 2574 ? $item->{name}
2749 : "$item->{nrof} $item->{name_pl}"; 2575 : "$item->{nrof} $item->{name_pl}";
2750 2576
2751 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2577 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2752 } 2578 }
2579
2580 $self->{scrolled}->add (@items);
2753 2581
2754# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2582# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2755} 2583}
2756 2584
2757sub size_request { 2585sub size_request {
2758 my ($self) = @_; 2586 my ($self) = @_;
2759 ($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;
2760} 2921}
2761 2922
2762############################################################################# 2923#############################################################################
2763 2924
2764package CFClient::UI; 2925package CFClient::UI;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines