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.191 by root, Tue May 9 22:27:53 2006 UTC vs.
Revision 1.209 by root, Wed May 17 00:26:55 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) {
35 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); 36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
36 } 37 if $x + $TOOLTIP->{w} > $::WIDTH;
37 38
38 $TOOLTIP->move ($x, $y); 39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
39 } 42 }
40 43
41 return; 44 return;
42 } 45 }
43 } 46 }
130 133
131 for (@$vals) { 134 for (@$vals) {
132 my $i = int $_ + $rem; 135 my $i = int $_ + $rem;
133 $rem += $_ - $i; 136 $rem += $_ - $i;
134 $_ = $i; 137 $_ = $i;
138 }
139}
140
141# call when resolution changes etc.
142sub rescale_widgets {
143 my ($sx, $sy) = @_;
144
145 for my $widget (values %WIDGET) {
146 if ($widget->{toplevel}) {
147 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
148 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
149 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w};
150 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
151 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
152 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
153 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
154 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
155 }
156
157 $widget->reconfigure;
135 } 158 }
136} 159}
137 160
138############################################################################# 161#############################################################################
139 162
158 if (/^connect_(.*)$/) { 181 if (/^connect_(.*)$/) {
159 $self->connect ($1 => delete $self->{$_}); 182 $self->connect ($1 => delete $self->{$_});
160 } 183 }
161 } 184 }
162 185
186 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
187
163 $self 188 $self
164} 189}
165 190
166sub destroy { 191sub destroy {
167 my ($self) = @_; 192 my ($self) = @_;
240 265
241sub size_allocate { 266sub size_allocate {
242 # nothing to be done 267 # nothing to be done
243} 268}
244 269
270sub reconfigure {
271 my ($self) = @_;
272
273 $self->check_size (1);
274 $self->update;
275}
276
245sub children { 277sub children {
246}
247
248# call when resolution changes etc.
249sub reconfigure {
250 my ($self) = @_;
251
252 $_->reconfigure
253 for $self->children;
254
255 $self->check_size;
256 $self->size_allocate ($self->{w}, $self->{h});
257 $self->update;
258} 278}
259 279
260sub set_max_size { 280sub set_max_size {
261 my ($self, $w, $h) = @_; 281 my ($self, $w, $h) = @_;
262 282
263 delete $self->{max_w}; $self->{max_w} = $w if $w; 283 delete $self->{max_w}; $self->{max_w} = $w if $w;
264 delete $self->{max_h}; $self->{max_h} = $h if $h; 284 delete $self->{max_h}; $self->{max_h} = $h if $h;
285}
286
287sub set_tooltip {
288 my ($self, $tooltip) = @_;
289
290 $self->{tooltip} = $tooltip;
291
292 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
293 delete $CFClient::UI::TOOLTIP->{owner};
294 CFClient::UI::check_tooltip;
295 }
265} 296}
266 297
267# translate global coordinates to local coordinate system 298# translate global coordinates to local coordinate system
268sub coord2local { 299sub coord2local {
269 my ($self, $x, $y) = @_; 300 my ($self, $x, $y) = @_;
355 glVertex $self->{w}, 0; 386 glVertex $self->{w}, 0;
356 glVertex $self->{w}, $self->{h}; 387 glVertex $self->{w}, $self->{h};
357 glVertex 0 , $self->{h}; 388 glVertex 0 , $self->{h};
358 glEnd; 389 glEnd;
359 glPopMatrix; 390 glPopMatrix;
360 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 391 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
361 } 392 }
362} 393}
363 394
364sub _draw { 395sub _draw {
365 my ($self) = @_; 396 my ($self) = @_;
380} 411}
381 412
382sub set_parent { 413sub set_parent {
383 my ($self, $parent) = @_; 414 my ($self, $parent) = @_;
384 415
385 $self->{_tree_depth} = $parent->{_tree_depth} + 1;
386 Scalar::Util::weaken ($self->{parent} = $parent); 416 Scalar::Util::weaken ($self->{parent} = $parent);
417
418 # TODO: req_w _does_change after ->reconfigure
419 $self->check_size
420 unless exists $self->{req_w};
387} 421}
388 422
389sub check_size { 423sub check_size {
390 my ($self) = @_; 424 my ($self, $forced) = @_;
391 425
426 $self->{force_alloc} = 1 if $forced;
392 $CFClient::UI::ROOT->{check_size}{$self} = $self; 427 $CFClient::UI::ROOT->{check_size}{$self} = $self;
393} 428}
394 429
395sub update { 430sub update {
396 my ($self) = @_; 431 my ($self) = @_;
412} 447}
413 448
414sub DESTROY { 449sub DESTROY {
415 my ($self) = @_; 450 my ($self) = @_;
416 451
452 delete $WIDGET{$self+0};
417 #$self->deactivate; 453 #$self->deactivate;
418} 454}
419 455
420############################################################################# 456#############################################################################
421 457
430 my $class = shift; 466 my $class = shift;
431 467
432 # range [value, low, high, page] 468 # range [value, low, high, page]
433 469
434 $class->SUPER::new ( 470 $class->SUPER::new (
435 bg => [0, 0, 0, 0.2], 471 #bg => [0, 0, 0, 0.2],
436 active_bg => [1, 1, 1, 0.5], 472 #active_bg => [1, 1, 1, 0.5],
437 @_ 473 @_
438 ) 474 )
439} 475}
440 476
441sub _draw { 477sub _draw {
442 my ($self) = @_; 478 my ($self) = @_;
443 479
480 my $color = $FOCUS == $self && $self->{active_bg}
481 ? $self->{active_bg}
482 : $self->{bg};
483
484 if ($color && (@$color < 4 || $color->[3])) {
444 my ($w, $h) = @$self{qw(w h)}; 485 my ($w, $h) = @$self{qw(w h)};
445 486
446 glEnable GL_BLEND; 487 glEnable GL_BLEND;
447 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 488 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
448 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 489 glColor @$color;
449 490
450 glBegin GL_QUADS; 491 glBegin GL_QUADS;
451 glVertex 0 , 0; 492 glVertex 0 , 0;
452 glVertex 0 , $h; 493 glVertex 0 , $h;
453 glVertex $w, $h; 494 glVertex $w, $h;
454 glVertex $w, 0; 495 glVertex $w, 0;
455 glEnd; 496 glEnd;
456 497
457 glDisable GL_BLEND; 498 glDisable GL_BLEND;
499 }
458} 500}
459 501
460############################################################################# 502#############################################################################
461 503
462package CFClient::UI::Empty; 504package CFClient::UI::Empty;
506 $self->{children} = [ 548 $self->{children} = [
507 sort { $a->{z} <=> $b->{z} } 549 sort { $a->{z} <=> $b->{z} }
508 @{$self->{children}}, @widgets 550 @{$self->{children}}, @widgets
509 ]; 551 ];
510 552
511 $_->check_size 553 $self->check_size (1);
512 for @widgets;
513
514 $self->update; 554 $self->update;
515} 555}
516 556
517sub children { 557sub children {
518 @{ $_[0]{children} } 558 @{ $_[0]{children} }
625} 665}
626 666
627sub update { 667sub update {
628 my ($self) = @_; 668 my ($self) = @_;
629 669
630 $ROOT->on_refresh ($self => sub { $self->render_child }); 670 $ROOT->on_post_alloc ($self => sub { $self->render_child });
631 $self->SUPER::update; 671 $self->SUPER::update;
632} 672}
633 673
634sub size_allocate { 674sub size_allocate {
635 my ($self, $w, $h) = @_; 675 my ($self, $w, $h) = @_;
666 my ($w, $h) = ($self->w, $self->h); 706 my ($w, $h) = ($self->w, $self->h);
667 707
668 my $tex = $self->{texture} 708 my $tex = $self->{texture}
669 or return; 709 or return;
670 710
671 glEnable GL_BLEND;
672 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
673 glEnable GL_TEXTURE_2D; 711 glEnable GL_TEXTURE_2D;
674 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 712 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
675 glColor 0, 0, 0, 1; 713 glColor 0, 0, 0, 1;
676 714
677 $tex->draw_quad (0, 0, $w, $h); 715 $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h);
678 716
679 glDisable GL_BLEND;
680 glDisable GL_TEXTURE_2D; 717 glDisable GL_TEXTURE_2D;
681} 718}
682 719
683############################################################################# 720#############################################################################
684 721
776 $self->add ($self->{slider}); 813 $self->add ($self->{slider});
777 814
778 $self 815 $self
779} 816}
780 817
781#TODO# update range on size_allocate depeneing on child 818#TODO# update range on size_allocate depending on child
782# update viewport offset on scroll 819# update viewport offset on scroll
783 820
784############################################################################# 821#############################################################################
785 822
786package CFClient::UI::Frame; 823package CFClient::UI::Frame;
790use CFClient::OpenGL; 827use CFClient::OpenGL;
791 828
792sub new { 829sub new {
793 my $class = shift; 830 my $class = shift;
794 831
795 my $self = $class->SUPER::new ( 832 $class->SUPER::new (
796 bg => [1, 1, 1, 1], 833 bg => undef,
797 border_bg => [1, 1, 1, 1],
798 border => 0.8,
799 @_ 834 @_,
800 ); 835 )
801
802 $self
803} 836}
804 837
805sub _draw { 838sub _draw {
806 my ($self) = @_; 839 my ($self) = @_;
807 840
808 my ($w, $h) = ($self->{w}, $self->{h}); 841 if ($self->{bg}) {
842 my ($w, $h) = @$self{qw(w h)};
809 843
810 glEnable GL_BLEND; 844 glEnable GL_BLEND;
811 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 845 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
812 glEnable GL_TEXTURE_2D; 846 glColor @{ $self->{bg} };
813 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
814 847
815# glBegin GL_QUADS; 848 glBegin GL_QUADS;
816# glColor 0, 0, 0, 0;
817# glVertex 0 , 0; 849 glVertex 0 , 0;
818# glVertex 0 , $h; 850 glVertex 0 , $h;
819# glVertex $w, $h; 851 glVertex $w, $h;
820# glVertex $w, 0; 852 glVertex $w, 0;
821# glEnd; 853 glEnd;
822 854
823
824 $self->child->draw;
825 glDisable GL_BLEND; 855 glDisable GL_BLEND;
826 glDisable GL_TEXTURE_2D; 856 }
857
858 $self->SUPER::_draw;
827} 859}
828 860
829############################################################################# 861#############################################################################
830 862
831package CFClient::UI::FancyFrame; 863package CFClient::UI::FancyFrame;
845 877
846 my $self = $class->SUPER::new ( 878 my $self = $class->SUPER::new (
847 bg => [1, 1, 1, 1], 879 bg => [1, 1, 1, 1],
848 border_bg => [1, 1, 1, 1], 880 border_bg => [1, 1, 1, 1],
849 border => 0.6, 881 border => 0.6,
882 toplevel => 1,
850 can_events => 1, 883 can_events => 1,
851 @_ 884 @_
852 ); 885 );
853 886
854 $self->{title} &&= new CFClient::UI::Label 887 $self->{title} &&= new CFClient::UI::Label
879 my ($self, $w, $h) = @_; 912 my ($self, $w, $h) = @_;
880 913
881 $h -= List::Util::max 0, $self->border * 2; 914 $h -= List::Util::max 0, $self->border * 2;
882 $w -= List::Util::max 0, $self->border * 2; 915 $w -= List::Util::max 0, $self->border * 2;
883 916
884 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) 917 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
885 if $self->{title}; 918 if $self->{title};
886 919
887 $self->child->configure ($self->border, $self->border, $w, $h); 920 $self->child->configure ($self->border, $self->border, $w, $h);
888} 921}
889 922
947 my ($self) = @_; 980 my ($self) = @_;
948 981
949 my ($w, $h ) = ($self->{w}, $self->{h}); 982 my ($w, $h ) = ($self->{w}, $self->{h});
950 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 983 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
951 984
952 glEnable GL_BLEND;
953 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
954 glEnable GL_TEXTURE_2D; 985 glEnable GL_TEXTURE_2D;
955 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 986 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
956 987
957 my $border = $self->border; 988 my $border = $self->border;
958 989
959 glColor @{ $self->{border_bg} }; 990 glColor @{ $self->{border_bg} };
960 $tex[1]->draw_quad (0, 0, $w, $border); 991 $tex[1]->draw_quad_alpha (0, 0, $w, $border);
961 $tex[3]->draw_quad (0, $border, $border, $ch); 992 $tex[3]->draw_quad_alpha (0, $border, $border, $ch);
962 $tex[2]->draw_quad ($w - $border, $border, $border, $ch); 993 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
963 $tex[4]->draw_quad (0, $h - $border, $w, $border); 994 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border);
964 995
965 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 996 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
966 my $bg = $tex[0]; 997 my $bg = $tex[0];
967 998
968 # TODO: repeat texture not scale 999 # TODO: repeat texture not scale
972 glColor @{ $self->{bg} }; 1003 glColor @{ $self->{bg} };
973 1004
974 $bg->{s} = $rep_x; 1005 $bg->{s} = $rep_x;
975 $bg->{t} = $rep_y; 1006 $bg->{t} = $rep_y;
976 $bg->{wrap_mode} = 1; 1007 $bg->{wrap_mode} = 1;
977 $bg->draw_quad ($border, $border, $cw, $ch); 1008 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1009 }
978 1010
979 glDisable GL_TEXTURE_2D; 1011 glDisable GL_TEXTURE_2D;
980 glDisable GL_BLEND;
981 }
982 1012
983 $self->{title}->draw if $self->{title}; 1013 $self->{title}->draw if $self->{title};
984 1014
985 $self->child->draw; 1015 $self->child->draw;
986} 1016}
1213} 1243}
1214 1244
1215sub size_allocate { 1245sub size_allocate {
1216 my ($self, $w, $h) = @_; 1246 my ($self, $w, $h) = @_;
1217 1247
1248 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1249
1218 my $children = $self->{children}; 1250 my $children = $self->{children};
1219 1251
1220 my @h = map $_->{req_h}, @$children; 1252 my @h = map $_->{req_h}, @$children;
1221 1253
1222 my $req_h = List::Util::sum @h; 1254 my $req_h = List::Util::sum @h;
1251 1283
1252############################################################################# 1284#############################################################################
1253 1285
1254package CFClient::UI::Label; 1286package CFClient::UI::Label;
1255 1287
1256our @ISA = CFClient::UI::Base::; 1288our @ISA = CFClient::UI::DrawBG::;
1257 1289
1258use CFClient::OpenGL; 1290use CFClient::OpenGL;
1259 1291
1260sub new { 1292sub new {
1261 my ($class, %arg) = @_; 1293 my ($class, %arg) = @_;
1262 1294
1263 my $self = $class->SUPER::new ( 1295 my $self = $class->SUPER::new (
1264 fg => [1, 1, 1], 1296 fg => [1, 1, 1],
1297 #bg => none
1298 #active_bg => none
1265 #font => default_font 1299 #font => default_font
1300 #text => initial text
1301 #markup => initial narkup
1302 layout => (new CFClient::Layout),
1266 fontsize => 1, 1303 fontsize => 1,
1267 text => "",
1268 align => -1, 1304 align => -1,
1269 valign => -1, 1305 valign => -1,
1270 padding => 2, 1306 padding => 2,
1271 layout => new CFClient::Layout,
1272 can_events => 0, 1307 can_events => 0,
1273 %arg 1308 %arg
1274 ); 1309 );
1275 1310
1276 if (exists $self->{template}) { 1311 if (exists $self->{template}) {
1277 my $layout = new CFClient::Layout; 1312 my $layout = new CFClient::Layout;
1278 $layout->set_text (delete $self->{template}); 1313 $layout->set_text (delete $self->{template});
1279 $self->{template} = $layout; 1314 $self->{template} = $layout;
1280 } 1315 }
1281 1316
1282 $self->set_text (delete $self->{text}) if exists $self->{text}; 1317 if (exists $self->{markup}) {
1283 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1318 $self->set_markup (delete $self->{markup});
1319 } else {
1320 $self->set_text (delete $self->{text});
1321 }
1284 1322
1285 $self 1323 $self
1286} 1324}
1287 1325
1288sub escape { 1326sub escape($) {
1289 local $_ = $_[1]; 1327 local $_ = $_[0];
1290 1328
1291 s/&/&amp;/g; 1329 s/&/&amp;/g;
1292 s/>/&gt;/g; 1330 s/>/&gt;/g;
1293 s/</&lt;/g; 1331 s/</&lt;/g;
1294 1332
1295 $_[1] 1333 $_
1296} 1334}
1297 1335
1298sub update { 1336sub update {
1299 my ($self) = @_; 1337 my ($self) = @_;
1300 1338
1306 my ($self, $text) = @_; 1344 my ($self, $text) = @_;
1307 1345
1308 return if $self->{text} eq "T$text"; 1346 return if $self->{text} eq "T$text";
1309 $self->{text} = "T$text"; 1347 $self->{text} = "T$text";
1310 1348
1349 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1311 $self->{layout}->set_text ($text); 1350 $self->{layout}->set_text ($text);
1312 1351
1313 $self->update; 1352 $self->update;
1314 $self->check_size; 1353 $self->check_size;
1315} 1354}
1318 my ($self, $markup) = @_; 1357 my ($self, $markup) = @_;
1319 1358
1320 return if $self->{text} eq "M$markup"; 1359 return if $self->{text} eq "M$markup";
1321 $self->{text} = "M$markup"; 1360 $self->{text} = "M$markup";
1322 1361
1362 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1363
1364 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1323 $self->{layout}->set_markup ($markup); 1365 $self->{layout}->set_markup ($markup);
1324 1366
1325 $self->update; 1367 $self->update;
1326 $self->check_size; 1368 $self->check_size;
1327} 1369}
1368} 1410}
1369 1411
1370sub _draw { 1412sub _draw {
1371 my ($self) = @_; 1413 my ($self) = @_;
1372 1414
1415 $self->SUPER::_draw; # draw background, if applicable
1416
1373 my $tex = $self->{texture} ||= do { 1417 my $tex = $self->{texture} ||= do {
1418 $self->{layout}->set_foreground (@{$self->{fg}});
1374 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1419 $self->{layout}->set_font ($self->{font}) if $self->{font};
1375 $self->{layout}->set_width ($self->{w}); 1420 $self->{layout}->set_width ($self->{w});
1376 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); 1421 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1422
1377 new_from_layout CFClient::Texture $self->{layout} 1423 my $tex = new_from_layout CFClient::Texture $self->{layout};
1424
1425 $self->{ox} = int ($self->{align} < 0 ? $self->{padding}
1426 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1427 : ($self->{w} - $tex->{w}) * 0.5);
1428
1429 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding}
1430 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1431 : ($self->{h} - $tex->{h}) * 0.5);
1432
1433 $tex
1378 }; 1434 };
1379 1435
1380 glEnable GL_BLEND;
1381 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1382 glEnable GL_TEXTURE_2D; 1436 glEnable GL_TEXTURE_2D;
1383 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1437 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1384 1438
1439 if ($tex->{format} == GL_ALPHA) {
1385 glColor @{$self->{fg}}; 1440 glColor @{$self->{fg}};
1386
1387 $self->{ox} = int (
1388 $self->{align} < 0 ? $self->{padding}
1389 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1390 : ($self->{w} - $tex->{w}) * 0.5
1391 );
1392
1393 $self->{oy} = int (
1394 $self->{valign} < 0 ? $self->{padding}
1395 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1396 : ($self->{h} - $tex->{h}) * 0.5
1397 );
1398
1399 $tex->draw_quad ($self->{ox}, $self->{oy}); 1441 $tex->draw_quad_alpha ($self->{ox}, $self->{oy});
1442 } else {
1443 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy});
1444 }
1400 1445
1401 glDisable GL_TEXTURE_2D; 1446 glDisable GL_TEXTURE_2D;
1402 glDisable GL_BLEND;
1403} 1447}
1404 1448
1405############################################################################# 1449#############################################################################
1406 1450
1407package CFClient::UI::EntryBase; 1451package CFClient::UI::EntryBase;
1442 $self->{layout}->set_text ("$text "); 1486 $self->{layout}->set_text ("$text ");
1443 1487
1444 $self->emit (changed => $self->{text}); 1488 $self->emit (changed => $self->{text});
1445} 1489}
1446 1490
1447sub get_text {
1448 $_[0]{text}
1449}
1450
1451sub size_request {
1452 my ($self) = @_;
1453
1454 my ($w, $h) = $self->SUPER::size_request;
1455
1456 ($w + 1, $h) # add 1 for cursor
1457}
1458
1459sub size_allocate {
1460 my ($self, $w, $h) = @_;
1461
1462 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1463}
1464
1465sub set_text { 1491sub set_text {
1466 my ($self, $text) = @_; 1492 my ($self, $text) = @_;
1467 1493
1468 $self->{cursor} = length $text; 1494 $self->{cursor} = length $text;
1469 $self->_set_text ($text); 1495 $self->_set_text ($text);
1470 $self->update; 1496 $self->update;
1497 $self->check_size;
1498}
1499
1500sub get_text {
1501 $_[0]{text}
1502}
1503
1504sub size_request {
1505 my ($self) = @_;
1506
1507 my ($w, $h) = $self->SUPER::size_request;
1508
1509 ($w + 1, $h) # add 1 for cursor
1510}
1511
1512sub size_allocate {
1513 my ($self, $w, $h) = @_;
1514
1515 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1471} 1516}
1472 1517
1473sub key_down { 1518sub key_down {
1474 my ($self, $ev) = @_; 1519 my ($self, $ev) = @_;
1475 1520
1477 my $sym = $ev->{sym}; 1522 my $sym = $ev->{sym};
1478 my $uni = $ev->{unicode}; 1523 my $uni = $ev->{unicode};
1479 1524
1480 my $text = $self->get_text; 1525 my $text = $self->get_text;
1481 1526
1482 if ($sym == 8) { 1527 if ($uni == 8) {
1483 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1528 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1484 } elsif ($sym == 127) { 1529 } elsif ($uni == 127) {
1485 substr $text, $self->{cursor}, 1, ""; 1530 substr $text, $self->{cursor}, 1, "";
1486 } elsif ($sym == CFClient::SDLK_LEFT) { 1531 } elsif ($sym == CFClient::SDLK_LEFT) {
1487 --$self->{cursor} if $self->{cursor}; 1532 --$self->{cursor} if $self->{cursor};
1488 } elsif ($sym == CFClient::SDLK_RIGHT) { 1533 } elsif ($sym == CFClient::SDLK_RIGHT) {
1489 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1534 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1490 } elsif ($sym == CFClient::SDLK_HOME) { 1535 } elsif ($sym == CFClient::SDLK_HOME) {
1491 $self->{cursor} = 0; 1536 $self->{cursor} = 0;
1492 } elsif ($sym == CFClient::SDLK_END) { 1537 } elsif ($sym == CFClient::SDLK_END) {
1493 $self->{cursor} = length $text; 1538 $self->{cursor} = length $text;
1494 } elsif ($sym == 27) { 1539 } elsif ($uni == 27) {
1495 $self->emit ('escape'); 1540 $self->emit ('escape');
1496 } elsif ($uni) { 1541 } elsif ($uni) {
1497 substr $text, $self->{cursor}++, 0, chr $uni; 1542 substr $text, $self->{cursor}++, 0, chr $uni;
1498 } 1543 }
1499 1544
1500 $self->_set_text ($text); 1545 $self->_set_text ($text);
1501 $self->update; 1546 $self->update;
1547 $self->check_size;
1502} 1548}
1503 1549
1504sub focus_in { 1550sub focus_in {
1505 my ($self) = @_; 1551 my ($self) = @_;
1506 1552
1635 my $class = shift; 1681 my $class = shift;
1636 1682
1637 $class->SUPER::new ( 1683 $class->SUPER::new (
1638 padding => 4, 1684 padding => 4,
1639 fg => [1, 1, 1], 1685 fg => [1, 1, 1],
1640 bg => [1, 1, 1, 0.2],
1641 active_fg => [0, 0, 1], 1686 active_fg => [0, 0, 1],
1642 can_hover => 1, 1687 can_hover => 1,
1643 align => 0, 1688 align => 0,
1644 valign => 0, 1689 valign => 0,
1645 can_events => 1, 1690 can_events => 1,
1663 1708
1664 if ($GRAB == $self) { 1709 if ($GRAB == $self) {
1665 $self->{fg} = $self->{active_fg}; 1710 $self->{fg} = $self->{active_fg};
1666 } 1711 }
1667 1712
1668 glEnable GL_BLEND;
1669 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1670 glEnable GL_TEXTURE_2D; 1713 glEnable GL_TEXTURE_2D;
1671 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1714 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1672 glColor 0, 0, 0, 1; 1715 glColor 0, 0, 0, 1;
1673 1716
1674 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); 1717 $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
1675 1718
1676 glDisable GL_TEXTURE_2D; 1719 glDisable GL_TEXTURE_2D;
1677 glDisable GL_BLEND;
1678 1720
1679 $self->SUPER::_draw; 1721 $self->SUPER::_draw;
1680} 1722}
1681 1723
1682############################################################################# 1724#############################################################################
1696 1738
1697 $class->SUPER::new ( 1739 $class->SUPER::new (
1698 padding => 2, 1740 padding => 2,
1699 fg => [1, 1, 1], 1741 fg => [1, 1, 1],
1700 active_fg => [1, 1, 0], 1742 active_fg => [1, 1, 0],
1743 bg => [0, 0, 0, 0.2],
1744 active_bg => [1, 1, 1, 0.5],
1701 state => 0, 1745 state => 0,
1702 can_hover => 1, 1746 can_hover => 1,
1703 @_ 1747 @_
1704 ) 1748 )
1705} 1749}
1729 1773
1730 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1774 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1731 1775
1732 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1776 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1733 1777
1734 glEnable GL_BLEND; 1778 my $tex = $self->{state} ? $tex[1] : $tex[0];
1779
1735 glEnable GL_TEXTURE_2D; 1780 glEnable GL_TEXTURE_2D;
1736 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1737
1738 my $tex = $self->{state} ? $tex[1] : $tex[0];
1739
1740 $tex->draw_quad (0, 0, $s, $s); 1781 $tex->draw_quad_alpha (0, 0, $s, $s);
1741
1742 glDisable GL_TEXTURE_2D; 1782 glDisable GL_TEXTURE_2D;
1743 glDisable GL_BLEND;
1744} 1783}
1745 1784
1746############################################################################# 1785#############################################################################
1747 1786
1748package CFClient::UI::Image; 1787package CFClient::UI::Image;
1791 glTranslate 0, -$self->{w}, 0; 1830 glTranslate 0, -$self->{w}, 0;
1792 1831
1793 ($w, $h) = ($h, $w); 1832 ($w, $h) = ($h, $w);
1794 } 1833 }
1795 1834
1796 glEnable GL_BLEND;
1797 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1798 glEnable GL_TEXTURE_2D; 1835 glEnable GL_TEXTURE_2D;
1799 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1836 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1800 1837
1801 $tex->draw_quad (0, 0, $w, $h); 1838 $tex->draw_quad_alpha (0, 0, $w, $h);
1802 1839
1803 glDisable GL_BLEND;
1804 glDisable GL_TEXTURE_2D; 1840 glDisable GL_TEXTURE_2D;
1805} 1841}
1806 1842
1807############################################################################# 1843#############################################################################
1808 1844
1997 qw(s1_slider.png s1_slider_bg.png); 2033 qw(s1_slider.png s1_slider_bg.png);
1998 2034
1999sub new { 2035sub new {
2000 my $class = shift; 2036 my $class = shift;
2001 2037
2002 # range [value, low, high, page] 2038 # range [value, low, high, page, unit]
2003 2039
2004 # TODO: 0-width page 2040 # TODO: 0-width page
2005 # TODO: req_w/h are wrong with vertical 2041 # TODO: req_w/h are wrong with vertical
2006 # TODO: calculations are off 2042 # TODO: calculations are off
2007 my $self = $class->SUPER::new ( 2043 my $self = $class->SUPER::new (
2008 fg => [1, 1, 1], 2044 fg => [1, 1, 1],
2009 active_fg => [0, 0, 0], 2045 active_fg => [0, 0, 0],
2046 bg => [0, 0, 0, 0.2],
2047 active_bg => [1, 1, 1, 0.5],
2010 range => [0, 0, 100, 10], 2048 range => [0, 0, 100, 10],
2011 req_w => $::WIDTH / 80, 2049 req_w => $::WIDTH / 80,
2012 req_h => $::WIDTH / 80, 2050 req_h => $::WIDTH / 80,
2013 vertical => 0, 2051 vertical => 0,
2014 can_hover => 1, 2052 can_hover => 1,
2015 inner_pad => 5, 2053 inner_pad => .05,
2016 @_ 2054 @_
2017 ); 2055 );
2018 2056
2057 $self->set_value ($self->{range}[0]);
2058 $self->update;
2059
2019 $self 2060 $self
2061}
2062
2063sub set_value {
2064 my ($self, $value) = @_;
2065
2066 my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}};
2067
2068 $hi = $lo + 1 if $hi <= $lo;
2069
2070 $value = $lo if $value < $lo;
2071 $value = $hi if $value > $hi;
2072
2073 $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit
2074 if $unit;
2075
2076 $page = $hi - $lo if $page > $hi - $lo;
2077
2078 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2079
2080 if ($value != $old_value) {
2081 $self->emit (changed => $value);
2082 $self->update;
2083 }
2020} 2084}
2021 2085
2022sub size_request { 2086sub size_request {
2023 my ($self) = @_; 2087 my ($self) = @_;
2024 2088
2037 2101
2038sub mouse_motion { 2102sub mouse_motion {
2039 my ($self, $ev, $x, $y) = @_; 2103 my ($self, $ev, $x, $y) = @_;
2040 2104
2041 if ($GRAB == $self) { 2105 if ($GRAB == $self) {
2106 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
2107
2108 my (undef, $lo, $hi, $page) = @{$self->{range}};
2109
2110 $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad};
2111
2112 $self->set_value ($x * ($hi - $lo) + $lo);
2113 }
2114}
2115
2116sub update {
2117 my ($self) = @_;
2118
2119 $CFClient::UI::ROOT->on_post_alloc ($self => sub {
2120 $self->set_value ($self->{range}[0]);
2121
2042 my ($value, $lo, $hi, $page) = @{$self->{range}}; 2122 my ($value, $lo, $hi, $page) = @{$self->{range}};
2043 2123
2044 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 2124 my $inner_w = 1 - 2 * $self->{inner_pad};
2045 2125
2046 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 2126 $self->{scale} = ($inner_w / ($hi - $lo)) || 1;
2047 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2048 2127
2049 $x -= $inner_pad_px; # substract the padding 2128 $page = $self->{scale} * $page || 10 / ($self->{w} || 1);
2050 $x = $x * ($hi - $lo) / $inner_w + $lo; 2129 $value = $self->{scale} * ($value - $lo);
2051 $x = $lo if $x < $lo;
2052 $x = $hi - $page if $x > $hi - $page;
2053 $self->{range}[0] = $x;
2054 2130
2055 $self->emit (changed => $x); 2131 $value = $self->{inner_pad} + ($value - $page * 0.5);
2056 $self->update; 2132
2133 $value = 0 if $value < 0;
2134 $page = 1 - $value if $value + $page > 1;
2135
2136 $self->{knob_x} = $value;
2137 $self->{knob_w} = $page;
2057 } 2138 });
2058}
2059 2139
2060# the inner_* stuff is for generating a padding for the slider handle, 2140 $self->SUPER::update;
2061# so that the handle doesn't leave the texture. This calculation isn't 100%
2062# correct propably, but it does the job for now
2063sub _calc_inner_pad_px {
2064 my ($self, $w) = @_;
2065 ($w / 100) * $self->{inner_pad} # % to pixels
2066} 2141}
2067 2142
2068sub _draw { 2143sub _draw {
2069 my ($self) = @_; 2144 my ($self) = @_;
2070 2145
2071 $self->SUPER::_draw (); 2146 $self->SUPER::_draw ();
2072 2147
2073 my ($w, $h) = @$self{qw(w h)}; 2148 glScale $self->{w}, $self->{h};
2074 2149
2075 if ($self->{vertical}) { 2150 if ($self->{vertical}) {
2076 # draw a vertical slider like a rotated horizontal slider 2151 # draw a vertical slider like a rotated horizontal slider
2077 2152
2078 glRotate 90, 0, 0, 1; 2153 glRotate 90, 0, 0, 1;
2079 glTranslate 0, -$self->{w}, 0; 2154 glTranslate 0, 1, 0;
2080
2081 ($w, $h) = ($h, $w);
2082 } 2155 }
2083 2156
2084 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 2157 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
2085 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 2158 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
2086 2159
2087 my ($value, $lo, $hi, $page) = @{$self->{range}};
2088
2089 $hi = $value + 1 if $lo == $hi;
2090
2091 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
2092 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
2093
2094 $page = int $page * $inner_w / ($hi - $lo);
2095 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
2096
2097 $w -= $page;
2098 $page &= ~1;
2099 glTranslate $page * 0.5, 0, 0;
2100 $page ||= 2;
2101
2102 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
2103 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
2104
2105 glEnable GL_BLEND;
2106 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2107 glEnable GL_TEXTURE_2D; 2160 glEnable GL_TEXTURE_2D;
2108 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2161 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2109 2162
2110 # draw background 2163 # draw background
2111 $tex[1]->draw_quad (0, 0, $w, $h); 2164 $tex[1]->draw_quad_alpha (0, 0, 1, 1);
2112 2165
2113 # draw handle 2166 # draw handle
2114 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); 2167 $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1);
2115 2168
2116 glDisable GL_BLEND;
2117 glDisable GL_TEXTURE_2D; 2169 glDisable GL_TEXTURE_2D;
2118} 2170}
2119 2171
2120############################################################################# 2172#############################################################################
2121 2173
2132 fontsize => 1, 2184 fontsize => 1,
2133 can_events => 0, 2185 can_events => 0,
2134 #font => default_font 2186 #font => default_font
2135 @_, 2187 @_,
2136 2188
2137 layout => (new CFClient::Layout), 2189 layout => (new CFClient::Layout 1),
2138 par => [], 2190 par => [],
2139 height => 0, 2191 height => 0,
2140 children => [ 2192 children => [
2141 (new CFClient::UI::Empty expand => 1), 2193 (new CFClient::UI::Empty expand => 1),
2142 (new CFClient::UI::Slider vertical => 1), 2194 (new CFClient::UI::Slider vertical => 1),
2160 2212
2161 my $layout = $self->{layout}; 2213 my $layout = $self->{layout};
2162 2214
2163 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2215 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2164 $layout->set_width ($self->{children}[0]{w}); 2216 $layout->set_width ($self->{children}[0]{w});
2165 $layout->set_text ($text); 2217 $layout->set_markup ($text);
2166 2218
2167 ($layout->size)[1] 2219 ($layout->size)[1]
2168} 2220}
2169 2221
2170sub reflow { 2222sub reflow {
2208 2260
2209 return unless $self->{h} > 0; 2261 return unless $self->{h} > 0;
2210 2262
2211 delete $self->{texture}; 2263 delete $self->{texture};
2212 2264
2213 $ROOT->on_refresh ($self, sub { 2265 $ROOT->on_post_alloc ($self, sub {
2214 if (delete $self->{need_reflow}) { 2266 if (delete $self->{need_reflow}) {
2215 my $height = 0; 2267 my $height = 0;
2216 2268
2217 $height += $_->[0] = $self->text_height ($_->[2]) 2269 $height += $_->[0] = $self->text_height ($_->[2])
2218 for @{$self->{par}}; 2270 for @{$self->{par}};
2226 2278
2227 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { 2279 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub {
2228 glClearColor 0, 0, 0, 0; 2280 glClearColor 0, 0, 0, 0;
2229 glClear GL_COLOR_BUFFER_BIT; 2281 glClear GL_COLOR_BUFFER_BIT;
2230 2282
2231 glEnable GL_BLEND;
2232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2233 glEnable GL_TEXTURE_2D; 2283 glEnable GL_TEXTURE_2D;
2234 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2284 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2235 2285
2236 my $top = int $self->{children}[1]{range}[0]; 2286 my $top = int $self->{children}[1]{range}[0];
2237 2287
2246 2296
2247 for my $par (@{$self->{par}}) { 2297 for my $par (@{$self->{par}}) {
2248 my $h = $par->[0]; 2298 my $h = $par->[0];
2249 2299
2250 if ($y0 < $y + $h && $y < $y1) { 2300 if ($y0 < $y + $h && $y < $y1) {
2301 $layout->set_foreground (@{ $par->[1] });
2251 $layout->set_text ($par->[2]); 2302 $layout->set_markup ($par->[2]);
2252 2303
2253 glColor @{ $par->[1] };
2254 my ($W, $H) = $layout->size; 2304 my ($W, $H) = $layout->size;
2255 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2305 CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0);
2256 } 2306 }
2257 2307
2258 $y += $h; 2308 $y += $h;
2259 } 2309 }
2260 2310
2261 glDisable GL_TEXTURE_2D; 2311 glDisable GL_TEXTURE_2D;
2262 glDisable GL_BLEND;
2263 }; 2312 };
2264 }); 2313 });
2265} 2314}
2266 2315
2267sub _draw { 2316sub _draw {
2268 my ($self) = @_; 2317 my ($self) = @_;
2269 2318
2270 glEnable GL_BLEND;
2271 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2272 glEnable GL_TEXTURE_2D; 2319 glEnable GL_TEXTURE_2D;
2273 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2320 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2274 glColor 1, 1, 1, 1; 2321 glColor 1, 1, 1, 1;
2275 $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2322 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2276 glDisable GL_TEXTURE_2D; 2323 glDisable GL_TEXTURE_2D;
2277 glDisable GL_BLEND;
2278 2324
2279 $self->{children}[1]->draw; 2325 $self->{children}[1]->draw;
2280 2326
2281} 2327}
2282 2328
2376 @_, 2422 @_,
2377 can_events => 0, 2423 can_events => 0,
2378 ) 2424 )
2379} 2425}
2380 2426
2381sub set_markup { 2427sub set_tooltip_from {
2382 my ($self, $text) = @_; 2428 my ($self, $widget) = @_;
2383 2429
2384 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; 2430 $self->add (new CFClient::UI::Label
2385 $self->{label}->set_markup ($text); 2431 markup => $widget->{tooltip},
2386 $self->add ($self->{label}); 2432 max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH,
2433 fontsize => 0.8,
2434 fg => [0, 0, 0, 1],
2435 font => ($widget->{tooltip_font} || $::FONT_PROP),
2436 );
2387} 2437}
2388 2438
2389sub size_request { 2439sub size_request {
2390 my ($self) = @_; 2440 my ($self) = @_;
2391
2392 $self->child->set_max_size ($::WIDTH * 0.3);
2393 2441
2394 my ($w, $h) = @{$self->child}{qw(req_w req_h)}; 2442 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2395 2443
2396 ($w + 4, $h + 4) 2444 ($w + 4, $h + 4)
2397} 2445}
2403} 2451}
2404 2452
2405sub _draw { 2453sub _draw {
2406 my ($self) = @_; 2454 my ($self) = @_;
2407 2455
2408 glPushMatrix;
2409 glTranslate 0.375, 0.375; 2456 glTranslate 0.375, 0.375;
2410 2457
2411 my ($w, $h) = @$self{qw(w h)}; 2458 my ($w, $h) = @$self{qw(w h)};
2412 2459
2413 glColor 1, 0.8, 0.4; 2460 glColor 1, 0.8, 0.4;
2424 glVertex 0 , $h; 2471 glVertex 0 , $h;
2425 glVertex $w, $h; 2472 glVertex $w, $h;
2426 glVertex $w, 0; 2473 glVertex $w, 0;
2427 glEnd; 2474 glEnd;
2428 2475
2429 glPopMatrix; 2476 glTranslate 2 - 0.375, 2 - 0.375;
2430
2431 glTranslate 2, 2;
2432 $self->SUPER::_draw; 2477 $self->SUPER::_draw;
2433} 2478}
2434 2479
2435############################################################################# 2480#############################################################################
2436 2481
2459 return unless $::CONN;#d# manage and cache textures differently 2504 return unless $::CONN;#d# manage and cache textures differently
2460 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2505 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2461 2506
2462 # TODO animation 2507 # TODO animation
2463 if ($tex) { 2508 if ($tex) {
2464 glEnable GL_BLEND;
2465 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2466 glEnable GL_TEXTURE_2D; 2509 glEnable GL_TEXTURE_2D;
2467 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2510 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2468 glColor 1, 1, 1, 1; 2511 glColor 1, 1, 1, 1;
2469 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2512 $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h});
2470 glDisable GL_TEXTURE_2D; 2513 glDisable GL_TEXTURE_2D;
2471 glDisable GL_BLEND;
2472 } 2514 }
2473} 2515}
2474 2516
2475############################################################################# 2517#############################################################################
2476 2518
2481sub new { 2523sub new {
2482 my $class = shift; 2524 my $class = shift;
2483 2525
2484 my %args = @_; 2526 my %args = @_;
2485 2527
2486 my $item = $args{item}; 2528 my $item = delete $args{item};
2487 2529
2488 my $desc = $item->{nrof} < 2 2530 my $desc = $item->{nrof} < 2
2489 ? $item->{name} 2531 ? $item->{name}
2490 : "$item->{nrof} $item->{name_pl}"; 2532 : "$item->{nrof} $item->{name_pl}";
2491 2533
2534 $item->{flags} & Crossfire::Protocol::F_OPEN
2535 and $desc .= " (open)";
2536 $item->{flags} & Crossfire::Protocol::F_APPLIED
2537 and $desc .= " (applied)";
2538 $item->{flags} & Crossfire::Protocol::F_UNPAID
2539 and $desc .= " (unpaid)";
2540 $item->{flags} & Crossfire::Protocol::F_MAGIC
2541 and $desc .= " (magic)";
2542 $item->{flags} & Crossfire::Protocol::F_CURSED
2543 and $desc .= " (cursed)";
2544 $item->{flags} & Crossfire::Protocol::F_DAMNED
2545 and $desc .= " (damned)";
2546 $item->{flags} & Crossfire::Protocol::F_LOCKED
2547 and $desc .= " *";
2492 2548
2493 my $self = $class->SUPER::new ( 2549 my $self = $class->SUPER::new (
2494 can_hover => 1, 2550 can_hover => 1,
2495 can_events => 1, 2551 can_events => 1,
2496 tooltip => (CFClient::UI::Label->escape ($desc) 2552 tooltip => ((CFClient::UI::Label::escape $desc)
2497 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), 2553 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2498 connect_button_down => sub { 2554 connect_button_down => sub {
2499 my ($self, $ev, $x, $y) = @_; 2555 my ($self, $ev, $x, $y) = @_;
2500 2556
2501 # todo: maybe put examine on 1? but should just be a tooltip :( 2557 # todo: maybe put examine on 1? but should just be a tooltip :(
2502 if ($ev->{button} == 1) { 2558 if ($ev->{button} == 1) {
2503 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); 2559 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2504 } elsif ($ev->{button} == 2) { 2560 } elsif ($ev->{button} == 2) {
2505 $::CONN->send ("apply $item->{tag}"); 2561 $::CONN->send ("apply $item->{tag}");
2506 } elsif ($ev->{button} == 3) { 2562 } elsif ($ev->{button} == 3) {
2507 CFClient::UI::Menu->new ( 2563 my @menu_items = (
2508 items => [
2509 ["examine", sub { $::CONN->send ("examine $item->{tag}") }], 2564 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2565 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2566 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2567 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2510 [ 2568 [
2511 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", 2569 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2512 sub { $::CONN->send ("lock $item->{tag}") }, 2570 sub { $::CONN->send ("lock ". () ."$item->{tag}") },
2513 ],
2514 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2515 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2516 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2517 ], 2571 ],
2518 )->popup ($ev); 2572 );
2573
2574 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2519 } 2575 }
2520 2576
2521 1 2577 1
2522 }, 2578 },
2523 %args 2579 %args
2524 ); 2580 );
2581
2525 $self->add(new CFClient::UI::Face 2582 $self->add (new CFClient::UI::Face
2526 can_events => 0, 2583 can_events => 0,
2527 face => $item->{face}, 2584 face => $item->{face},
2528 anim => $item->{anim}, 2585 anim => $item->{anim},
2529 animspeed => $item->{animspeed}); 2586 animspeed => $item->{animspeed},
2587 );
2588
2530 $self->add(new CFClient::UI::Label 2589 $self->add (new CFClient::UI::Label
2531 can_events => 0, 2590 can_events => 0,
2532 text => $desc); 2591 text => $desc,
2592 );
2533 2593
2534 $self 2594 $self
2535} 2595}
2536 2596
2537############################################################################# 2597#############################################################################
2555 my ($self, $items) = @_; 2615 my ($self, $items) = @_;
2556 2616
2557 $self->{scrolled}->clear; 2617 $self->{scrolled}->clear;
2558 return unless $items; 2618 return unless $items;
2559 2619
2560 my @items = sort { $a->{type} <=> $b->{type} } @$items; 2620 my @items = sort {
2621 ($a->{type} <=> $b->{type})
2622 or ($a->{name} cmp $b->{name})
2623 } @$items;
2561 2624
2562 $self->{real_items} = \@items; 2625 $self->{real_items} = \@items;
2563 2626
2564 for my $item (@items) { 2627 for my $item (@items) {
2565 my $desc = $item->{nrof} < 2 2628 my $desc = $item->{nrof} < 2
2566 ? $item->{name} 2629 ? $item->{name}
2567 : "$item->{nrof} $item->{name_pl}"; 2630 : "$item->{nrof} $item->{name_pl}";
2568 2631
2569 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2632 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2570 } 2633 }
2634
2635 $self->{scrolled}->add (@items);
2571 2636
2572# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2637# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2573} 2638}
2574 2639
2575sub size_request { 2640sub size_request {
2649 } 2714 }
2650} 2715}
2651 2716
2652############################################################################# 2717#############################################################################
2653 2718
2719package CFClient::UI::Statusbox;
2720
2721our @ISA = CFClient::UI::VBox::;
2722
2723sub reorder {
2724 my ($self) = @_;
2725 my $NOW = time;
2726
2727 while (my ($k, $v) = each %{ $self->{item} }) {
2728 delete $self->{item}{$k} if $v->{timeout} < $NOW;
2729 }
2730
2731 my @widgets;
2732
2733 my @items = sort {
2734 $a->{pri} <=> $b->{pri}
2735 or $b->{id} <=> $a->{id}
2736 } values %{ $self->{item} };
2737
2738 my $count = 10 + 1;
2739 for my $item (@items) {
2740 last unless --$count;
2741
2742 push @widgets, $item->{label} ||= do {
2743 # TODO: doesn't handle markup well (read as: at all)
2744 my $short = $item->{count} > 1
2745 ? "<b>$item->{count} ×</b> $item->{text}"
2746 : $item->{text};
2747
2748 for ($short) {
2749 s/^\s+//;
2750 s/\s+/ /g;
2751 my $len = int 40 / $item->{fontsize};
2752 substr $_, $len, length, "…" if $len < length;
2753 }
2754
2755 new CFClient::UI::Label
2756 markup => $short,
2757 tooltip => $item->{tooltip},
2758 tooltip_font => $::FONT_PROP,
2759 tooltip_width => 0.67,
2760 fontsize => $item->{fontsize},
2761 fg => $item->{fg},
2762 can_events => 1,
2763 can_hover => 1
2764 };
2765 }
2766
2767 $self->clear;
2768 $self->SUPER::add (reverse @widgets);
2769}
2770
2771sub add {
2772 my ($self, $text, %arg) = @_;
2773
2774 $text =~ s/^\s+//;
2775 $text =~ s/\s+$//;
2776
2777 my $timeout = time + ((delete $arg{timeout}) || 60);
2778
2779 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2780
2781 if (my $item = $self->{item}{$group}) {
2782 if ($item->{text} eq $text) {
2783 $item->{count}++;
2784 } else {
2785 $item->{count} = 1;
2786 $item->{text} = $item->{tooltip} = $text;
2787 }
2788 $item->{id} = ++$self->{id};
2789 $item->{timeout} = $timeout;
2790 delete $item->{label};
2791 } else {
2792 $self->{item}{$group} = {
2793 id => ++$self->{id},
2794 text => $text,
2795 timeout => $timeout,
2796 tooltip => $text,
2797 fontsize => 0.8,
2798 fg => [0.8, 0.8, 0.8, 0.8],
2799 pri => 0,
2800 count => 1,
2801 %arg,
2802 };
2803 }
2804
2805 $self->reorder;
2806}
2807
2808#############################################################################
2809
2654package CFClient::UI::Root; 2810package CFClient::UI::Root;
2655 2811
2656our @ISA = CFClient::UI::Container::; 2812our @ISA = CFClient::UI::Container::;
2657 2813
2658use CFClient::OpenGL; 2814use CFClient::OpenGL;
2686} 2842}
2687 2843
2688sub size_allocate { 2844sub size_allocate {
2689 my ($self, $w, $h) = @_; 2845 my ($self, $w, $h) = @_;
2690 2846
2691 my $old_w = $self->{old_w}; 2847 my $old_w = $self->{old_w}; $self->{old_w} = $w;
2692 my $old_h = $self->{old_h}; 2848 my $old_h = $self->{old_h}; $self->{old_h} = $h;
2693 2849
2694 if ($old_w && $old_h) { 2850 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
2695 for my $child ($self->children) { 2851 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
2696 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2697 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2698 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2699 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2700 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2701 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2702 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2703 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2704 }
2705 }
2706 2852
2707 for my $child ($self->children) { 2853 for my $child ($self->children) {
2708 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2854 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2709 2855
2856 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
2857 if exists $child->{req_x};
2858
2859 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
2860 if exists $child->{req_y};
2861
2710 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2862 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
2711 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2863 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
2864
2712 $child->configure ($X, $Y, $W, $H); 2865 $child->configure ($X, $Y, $W, $H);
2713 } 2866 }
2714
2715 $self->{old_w} = $w;
2716 $self->{old_h} = $h;
2717} 2867}
2718 2868
2719sub coord2local { 2869sub coord2local {
2720 my ($self, $x, $y) = @_; 2870 my ($self, $x, $y) = @_;
2721 2871
2734 $self->check_size; 2884 $self->check_size;
2735 $::WANT_REFRESH++; 2885 $::WANT_REFRESH++;
2736} 2886}
2737 2887
2738sub add { 2888sub add {
2739 my ($self, $child) = @_; 2889 my ($self, @children) = @_;
2740 2890
2891 for my $child (@children) {
2892 $child->{toplevel} = 1;
2893
2741 # integerise window positions 2894 # integerise window positions
2742 $child->{x} = int $child->{x}; 2895 $child->{x} = int $child->{x};
2743 $child->{y} = int $child->{y}; 2896 $child->{y} = int $child->{y};
2897 }
2744 2898
2745 $self->SUPER::add ($child); 2899 $self->SUPER::add (@children);
2746} 2900}
2747 2901
2748sub on_refresh { 2902sub on_refresh {
2749 my ($self, $id, $cb) = @_; 2903 my ($self, $id, $cb) = @_;
2750 2904
2751 $self->{refresh_hook}{$id} = $cb; 2905 $self->{refresh_hook}{$id} = $cb;
2752} 2906}
2753 2907
2908sub on_post_alloc {
2909 my ($self, $id, $cb) = @_;
2910
2911 $self->{post_alloc_hook}{$id} = $cb;
2912}
2913
2754sub draw { 2914sub draw {
2755 my ($self) = @_; 2915 my ($self) = @_;
2916
2917 while ($self->{refresh_hook}) {
2918 $_->()
2919 for values %{delete $self->{refresh_hook}};
2920 }
2756 2921
2757 if ($self->{check_size}) { 2922 if ($self->{check_size}) {
2758 my @queue = ([], []); 2923 my @queue = ([], []);
2759 2924
2760 for (;;) { 2925 for (;;) {
2768 2933
2769 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 2934 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2770 ? @$widget{qw(user_w user_h)} 2935 ? @$widget{qw(user_w user_h)}
2771 : $widget->size_request; 2936 : $widget->size_request;
2772 2937
2938 if (delete $widget->{force_alloc}
2773 if ($w != $widget->{req_w} || $h != $widget->{req_h}) { 2939 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
2940 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
2941
2774 $widget->{req_w} = $w; 2942 $widget->{req_w} = $w;
2775 $widget->{req_h} = $h; 2943 $widget->{req_h} = $h;
2776 2944
2777 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}]; 2945 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
2778 2946
2779 $widget->{parent}->check_size 2947 $widget->{parent}->check_size
2780 if $widget->{parent}; 2948 if $widget->{parent};
2781 } 2949 }
2782 } 2950 }
2783 } 2951 }
2784 2952
2785 while ($self->{size_alloc}) { 2953 while ($self->{size_alloc}) {
2786 for (values %{delete $self->{size_alloc}}) { 2954 for (values %{delete $self->{size_alloc}}) {
2787 my ($widget, $w, $h) = @$_; 2955 my ($widget, $w, $h) = @$_;
2956
2957 $w = 0 if $w < 0;
2958 $h = 0 if $h < 0;
2788 2959
2789 $widget->{w} = $w; 2960 $widget->{w} = $w;
2790 $widget->{h} = $h; 2961 $widget->{h} = $h;
2791 $widget->size_allocate ($w, $h); 2962 $widget->size_allocate ($w, $h);
2792 $widget->emit (size_allocate => $w, $h); 2963 $widget->emit (size_allocate => $w, $h);
2793 } 2964 }
2794 } 2965 }
2795 2966
2796 while ($self->{refresh_hook}) { 2967 while ($self->{post_alloc_hook}) {
2797 $_->() 2968 $_->()
2798 for values %{delete $self->{refresh_hook}}; 2969 for values %{delete $self->{post_alloc_hook}};
2799 } 2970 }
2800 2971
2801 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2972 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2802 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2973 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2803 glClear GL_COLOR_BUFFER_BIT; 2974 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines