1 | package CFClient::UI; |
1 | package CFClient::UI; |
2 | |
2 | |
|
|
3 | use utf8; |
3 | use strict; |
4 | use strict; |
4 | |
5 | |
5 | use Scalar::Util (); |
6 | use Scalar::Util (); |
6 | use List::Util (); |
7 | use List::Util (); |
7 | |
8 | |
… | |
… | |
23 | |
24 | |
24 | my $tip = $widget->{tooltip}; |
25 | my $tip = $widget->{tooltip}; |
25 | |
26 | |
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
27 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
27 | |
28 | |
28 | $TOOLTIP->set_markup ($widget->{tooltip}); |
29 | $TOOLTIP->set_tooltip_from ($widget); |
29 | |
|
|
30 | $TOOLTIP->show; |
30 | $TOOLTIP->show; |
31 | |
31 | |
32 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
32 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
33 | |
33 | |
34 | if ($x + $TOOLTIP->{w} > $::WIDTH) { |
34 | if ($x + $TOOLTIP->{w} > $::WIDTH) { |
35 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); |
35 | ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0); |
36 | } |
36 | } |
37 | |
37 | |
38 | $TOOLTIP->move ($x, $y); |
38 | $TOOLTIP->move ($x, $y); |
|
|
39 | $TOOLTIP->check_size; |
|
|
40 | $TOOLTIP->update; |
39 | } |
41 | } |
40 | |
42 | |
41 | return; |
43 | return; |
42 | } |
44 | } |
43 | } |
45 | } |
… | |
… | |
232 | $self->{y} = $y; |
234 | $self->{y} = $y; |
233 | $self->update; |
235 | $self->update; |
234 | } |
236 | } |
235 | |
237 | |
236 | if ($self->{w} != $w || $self->{h} != $h) { |
238 | if ($self->{w} != $w || $self->{h} != $h) { |
237 | $self->{w} = $w; |
239 | $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 | } |
240 | } |
244 | } |
241 | } |
245 | |
242 | |
246 | sub size_allocate { |
243 | sub size_allocate { |
247 | # nothing to be done |
244 | # nothing to be done |
… | |
… | |
255 | my ($self) = @_; |
252 | my ($self) = @_; |
256 | |
253 | |
257 | $_->reconfigure |
254 | $_->reconfigure |
258 | for $self->children; |
255 | for $self->children; |
259 | |
256 | |
260 | $_->check_size; |
257 | $self->check_size (1); |
|
|
258 | $self->update; |
261 | } |
259 | } |
262 | |
260 | |
263 | sub set_max_size { |
261 | sub set_max_size { |
264 | my ($self, $w, $h) = @_; |
262 | my ($self, $w, $h) = @_; |
265 | |
263 | |
266 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
264 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
267 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
265 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
268 | } |
|
|
269 | |
|
|
270 | # return top left coordinates |
|
|
271 | sub _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 | } |
266 | } |
279 | |
267 | |
280 | # translate global coordinates to local coordinate system |
268 | # translate global coordinates to local coordinate system |
281 | sub coord2local { |
269 | sub coord2local { |
282 | my ($self, $x, $y) = @_; |
270 | my ($self, $x, $y) = @_; |
283 | |
271 | |
284 | my ($X, $Y) = $self->_topleft; |
272 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
285 | ($x - $X, $y - $Y) |
|
|
286 | } |
273 | } |
287 | |
274 | |
288 | # translate local coordinates to global coordinate system |
275 | # translate local coordinates to global coordinate system |
289 | sub coord2global { |
276 | sub coord2global { |
290 | my ($self, $x, $y) = @_; |
277 | my ($self, $x, $y) = @_; |
291 | |
278 | |
292 | my ($X, $Y) = $self->_topleft; |
279 | $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) |
293 | ($x + $X, $y + $Y) |
|
|
294 | } |
280 | } |
295 | |
281 | |
296 | sub focus_in { |
282 | sub focus_in { |
297 | my ($self) = @_; |
283 | my ($self) = @_; |
298 | |
284 | |
… | |
… | |
396 | |
382 | |
397 | sub set_parent { |
383 | sub set_parent { |
398 | my ($self, $parent) = @_; |
384 | my ($self, $parent) = @_; |
399 | |
385 | |
400 | Scalar::Util::weaken ($self->{parent} = $parent); |
386 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
387 | |
|
|
388 | |
|
|
389 | # TODO: req_w _does_change after ->reconfigure |
|
|
390 | $self->check_size |
|
|
391 | unless exists $self->{req_w}; |
401 | } |
392 | } |
402 | |
393 | |
403 | sub check_size { |
394 | sub check_size { |
404 | my ($self) = @_; |
395 | my ($self, $forced) = @_; |
405 | |
396 | |
406 | $self->{parent} |
397 | $self->{force_alloc} = 1 if $forced; |
407 | or return 1; |
398 | $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 | } |
399 | } |
428 | |
400 | |
429 | sub update { |
401 | sub update { |
430 | my ($self) = @_; |
402 | my ($self) = @_; |
431 | |
403 | |
… | |
… | |
528 | |
500 | |
529 | $self |
501 | $self |
530 | } |
502 | } |
531 | |
503 | |
532 | sub add { |
504 | sub add { |
533 | my ($self, $child) = @_; |
505 | my ($self, @widgets) = @_; |
534 | |
506 | |
535 | $child->set_parent ($self); |
507 | $_->set_parent ($self) |
|
|
508 | for @widgets; |
536 | |
509 | |
537 | use sort 'stable'; |
510 | use sort 'stable'; |
538 | |
511 | |
539 | $self->{children} = [ |
512 | $self->{children} = [ |
540 | sort { $a->{z} <=> $b->{z} } |
513 | sort { $a->{z} <=> $b->{z} } |
541 | @{$self->{children}}, $child |
514 | @{$self->{children}}, @widgets |
542 | ]; |
515 | ]; |
543 | |
516 | |
544 | $child->check_size; |
517 | $self->check_size (1); |
545 | $self->update; |
518 | $self->update; |
546 | } |
519 | } |
547 | |
520 | |
548 | sub children { |
521 | sub children { |
549 | @{ $_[0]{children} } |
522 | @{ $_[0]{children} } |
… | |
… | |
656 | } |
629 | } |
657 | |
630 | |
658 | sub update { |
631 | sub update { |
659 | my ($self) = @_; |
632 | my ($self) = @_; |
660 | |
633 | |
661 | $ROOT->on_refresh ($self => sub { $self->render_child }); |
634 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
662 | $self->SUPER::update; |
635 | $self->SUPER::update; |
663 | } |
636 | } |
664 | |
637 | |
665 | sub size_allocate { |
638 | sub size_allocate { |
666 | my ($self, $w, $h) = @_; |
639 | my ($self, $w, $h) = @_; |
… | |
… | |
697 | my ($w, $h) = ($self->w, $self->h); |
670 | my ($w, $h) = ($self->w, $self->h); |
698 | |
671 | |
699 | my $tex = $self->{texture} |
672 | my $tex = $self->{texture} |
700 | or return; |
673 | or return; |
701 | |
674 | |
702 | glEnable GL_BLEND; |
|
|
703 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
704 | glEnable GL_TEXTURE_2D; |
675 | glEnable GL_TEXTURE_2D; |
705 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
676 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
706 | glColor 0, 0, 0, 1; |
677 | glColor 0, 0, 0, 1; |
707 | |
678 | |
708 | $tex->draw_quad (0, 0, $w, $h); |
679 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
709 | |
680 | |
710 | glDisable GL_BLEND; |
|
|
711 | glDisable GL_TEXTURE_2D; |
681 | glDisable GL_TEXTURE_2D; |
712 | } |
682 | } |
713 | |
683 | |
714 | ############################################################################# |
684 | ############################################################################# |
715 | |
685 | |
… | |
… | |
737 | |
707 | |
738 | $self->{view_x} = int $x; |
708 | $self->{view_x} = int $x; |
739 | $self->{view_y} = int $y; |
709 | $self->{view_y} = int $y; |
740 | |
710 | |
741 | $self->update; |
711 | $self->update; |
|
|
712 | } |
|
|
713 | |
|
|
714 | # hmm, this does not work for topleft of $self... but we should not ask for that |
|
|
715 | sub coord2local { |
|
|
716 | my ($self, $x, $y) = @_; |
|
|
717 | |
|
|
718 | $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
719 | } |
|
|
720 | |
|
|
721 | sub coord2global { |
|
|
722 | my ($self, $x, $y) = @_; |
|
|
723 | |
|
|
724 | $x = List::Util::min $self->{w}, $x - $self->{view_x}; |
|
|
725 | $y = List::Util::min $self->{h}, $y - $self->{view_y}; |
|
|
726 | |
|
|
727 | $self->SUPER::coord2global ($x, $y) |
|
|
728 | } |
|
|
729 | |
|
|
730 | sub find_widget { |
|
|
731 | my ($self, $x, $y) = @_; |
|
|
732 | |
|
|
733 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
734 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
|
|
735 | ) { |
|
|
736 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
737 | } else { |
|
|
738 | $self->CFClient::UI::Base::find_widget ($x, $y) |
|
|
739 | } |
742 | } |
740 | } |
743 | |
741 | |
744 | sub _render { |
742 | sub _render { |
745 | my ($self) = @_; |
743 | my ($self) = @_; |
746 | |
744 | |
… | |
… | |
790 | |
788 | |
791 | our @ISA = CFClient::UI::Bin::; |
789 | our @ISA = CFClient::UI::Bin::; |
792 | |
790 | |
793 | use CFClient::OpenGL; |
791 | use CFClient::OpenGL; |
794 | |
792 | |
795 | sub new { |
|
|
796 | my $class = shift; |
|
|
797 | |
|
|
798 | my $self = $class->SUPER::new ( |
|
|
799 | bg => [1, 1, 1, 1], |
|
|
800 | border_bg => [1, 1, 1, 1], |
|
|
801 | border => 0.8, |
|
|
802 | @_ |
|
|
803 | ); |
|
|
804 | |
|
|
805 | $self |
|
|
806 | } |
|
|
807 | |
|
|
808 | sub _draw { |
|
|
809 | my ($self) = @_; |
|
|
810 | |
|
|
811 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
812 | |
|
|
813 | glEnable GL_BLEND; |
|
|
814 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
815 | glEnable GL_TEXTURE_2D; |
|
|
816 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
817 | |
|
|
818 | # glBegin GL_QUADS; |
|
|
819 | # glColor 0, 0, 0, 0; |
|
|
820 | # glVertex 0 , 0; |
|
|
821 | # glVertex 0 , $h; |
|
|
822 | # glVertex $w, $h; |
|
|
823 | # glVertex $w, 0; |
|
|
824 | # glEnd; |
|
|
825 | |
|
|
826 | |
|
|
827 | $self->child->draw; |
|
|
828 | glDisable GL_BLEND; |
|
|
829 | glDisable GL_TEXTURE_2D; |
|
|
830 | } |
|
|
831 | |
|
|
832 | ############################################################################# |
793 | ############################################################################# |
833 | |
794 | |
834 | package CFClient::UI::FancyFrame; |
795 | package CFClient::UI::FancyFrame; |
835 | |
796 | |
836 | our @ISA = CFClient::UI::Bin::; |
797 | our @ISA = CFClient::UI::Bin::; |
… | |
… | |
950 | my ($self) = @_; |
911 | my ($self) = @_; |
951 | |
912 | |
952 | my ($w, $h ) = ($self->{w}, $self->{h}); |
913 | my ($w, $h ) = ($self->{w}, $self->{h}); |
953 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
914 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
954 | |
915 | |
955 | glEnable GL_BLEND; |
|
|
956 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
957 | glEnable GL_TEXTURE_2D; |
916 | glEnable GL_TEXTURE_2D; |
958 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
917 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
959 | |
918 | |
960 | my $border = $self->border; |
919 | my $border = $self->border; |
961 | |
920 | |
962 | glColor @{ $self->{border_bg} }; |
921 | glColor @{ $self->{border_bg} }; |
963 | $tex[1]->draw_quad (0, 0, $w, $border); |
922 | $tex[1]->draw_quad_alpha (0, 0, $w, $border); |
964 | $tex[3]->draw_quad (0, $border, $border, $ch); |
923 | $tex[3]->draw_quad_alpha (0, $border, $border, $ch); |
965 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
924 | $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); |
966 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
925 | $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); |
967 | |
926 | |
968 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
927 | if (@{$self->{bg}} < 4 || $self->{bg}[3]) { |
969 | my $bg = $tex[0]; |
928 | my $bg = $tex[0]; |
970 | |
929 | |
971 | # TODO: repeat texture not scale |
930 | # TODO: repeat texture not scale |
… | |
… | |
975 | glColor @{ $self->{bg} }; |
934 | glColor @{ $self->{bg} }; |
976 | |
935 | |
977 | $bg->{s} = $rep_x; |
936 | $bg->{s} = $rep_x; |
978 | $bg->{t} = $rep_y; |
937 | $bg->{t} = $rep_y; |
979 | $bg->{wrap_mode} = 1; |
938 | $bg->{wrap_mode} = 1; |
980 | $bg->draw_quad ($border, $border, $cw, $ch); |
939 | $bg->draw_quad_alpha ($border, $border, $cw, $ch); |
|
|
940 | } |
981 | |
941 | |
982 | glDisable GL_TEXTURE_2D; |
942 | glDisable GL_TEXTURE_2D; |
983 | glDisable GL_BLEND; |
|
|
984 | } |
|
|
985 | |
943 | |
986 | $self->{title}->draw if $self->{title}; |
944 | $self->{title}->draw if $self->{title}; |
987 | |
945 | |
988 | $self->child->draw; |
946 | $self->child->draw; |
989 | } |
947 | } |
… | |
… | |
1216 | } |
1174 | } |
1217 | |
1175 | |
1218 | sub size_allocate { |
1176 | sub size_allocate { |
1219 | my ($self, $w, $h) = @_; |
1177 | my ($self, $w, $h) = @_; |
1220 | |
1178 | |
|
|
1179 | Carp::confess "negative size" if $w < 0 || $h < 0;#d# |
|
|
1180 | |
1221 | my $children = $self->{children}; |
1181 | my $children = $self->{children}; |
1222 | |
1182 | |
1223 | my @h = map $_->{req_h}, @$children; |
1183 | my @h = map $_->{req_h}, @$children; |
1224 | |
1184 | |
1225 | my $req_h = List::Util::sum @h; |
1185 | my $req_h = List::Util::sum @h; |
… | |
… | |
1264 | my ($class, %arg) = @_; |
1224 | my ($class, %arg) = @_; |
1265 | |
1225 | |
1266 | my $self = $class->SUPER::new ( |
1226 | my $self = $class->SUPER::new ( |
1267 | fg => [1, 1, 1], |
1227 | fg => [1, 1, 1], |
1268 | #font => default_font |
1228 | #font => default_font |
|
|
1229 | #text => initial text |
|
|
1230 | #markup => initial narkup |
|
|
1231 | layout => (new CFClient::Layout), |
1269 | fontsize => 1, |
1232 | fontsize => 1, |
1270 | text => "", |
|
|
1271 | align => -1, |
1233 | align => -1, |
1272 | valign => -1, |
1234 | valign => -1, |
1273 | padding => 2, |
1235 | padding => 2, |
1274 | layout => new CFClient::Layout, |
|
|
1275 | can_events => 0, |
1236 | can_events => 0, |
1276 | %arg |
1237 | %arg |
1277 | ); |
1238 | ); |
1278 | |
1239 | |
1279 | if (exists $self->{template}) { |
1240 | if (exists $self->{template}) { |
1280 | my $layout = new CFClient::Layout; |
1241 | my $layout = new CFClient::Layout; |
1281 | $layout->set_text (delete $self->{template}); |
1242 | $layout->set_text (delete $self->{template}); |
1282 | $self->{template} = $layout; |
1243 | $self->{template} = $layout; |
1283 | } |
1244 | } |
1284 | |
1245 | |
1285 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
1246 | if (exists $self->{markup}) { |
1286 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
1247 | $self->set_markup (delete $self->{markup}); |
|
|
1248 | } else { |
|
|
1249 | $self->set_text (delete $self->{text}); |
|
|
1250 | } |
1287 | |
1251 | |
1288 | $self |
1252 | $self |
1289 | } |
1253 | } |
1290 | |
1254 | |
1291 | sub escape { |
1255 | sub escape { |
… | |
… | |
1303 | |
1267 | |
1304 | delete $self->{texture}; |
1268 | delete $self->{texture}; |
1305 | $self->SUPER::update; |
1269 | $self->SUPER::update; |
1306 | } |
1270 | } |
1307 | |
1271 | |
1308 | sub reconfigure { |
|
|
1309 | my ($self) = @_; |
|
|
1310 | |
|
|
1311 | delete $self->{texture}; |
|
|
1312 | } |
|
|
1313 | |
|
|
1314 | sub set_text { |
1272 | sub set_text { |
1315 | my ($self, $text) = @_; |
1273 | my ($self, $text) = @_; |
1316 | |
1274 | |
1317 | return if $self->{text} eq "T$text"; |
1275 | return if $self->{text} eq "T$text"; |
1318 | $self->{text} = "T$text"; |
1276 | $self->{text} = "T$text"; |
1319 | |
1277 | |
|
|
1278 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1320 | $self->{layout}->set_text ($text); |
1279 | $self->{layout}->set_text ($text); |
1321 | |
1280 | |
1322 | delete $self->{texture}; |
|
|
1323 | $self->update; |
1281 | $self->update; |
1324 | $self->check_size; |
1282 | $self->check_size; |
1325 | } |
1283 | } |
1326 | |
1284 | |
1327 | sub set_markup { |
1285 | sub set_markup { |
1328 | my ($self, $markup) = @_; |
1286 | my ($self, $markup) = @_; |
1329 | |
1287 | |
1330 | return if $self->{text} eq "M$markup"; |
1288 | return if $self->{text} eq "M$markup"; |
1331 | $self->{text} = "M$markup"; |
1289 | $self->{text} = "M$markup"; |
1332 | |
1290 | |
|
|
1291 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
|
|
1292 | |
|
|
1293 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1333 | $self->{layout}->set_markup ($markup); |
1294 | $self->{layout}->set_markup ($markup); |
1334 | |
1295 | |
1335 | delete $self->{texture}; |
|
|
1336 | $self->update; |
1296 | $self->update; |
1337 | $self->check_size; |
1297 | $self->check_size; |
1338 | } |
1298 | } |
1339 | |
1299 | |
1340 | sub size_request { |
1300 | sub size_request { |
… | |
… | |
1371 | sub set_fontsize { |
1331 | sub set_fontsize { |
1372 | my ($self, $fontsize) = @_; |
1332 | my ($self, $fontsize) = @_; |
1373 | |
1333 | |
1374 | $self->{fontsize} = $fontsize; |
1334 | $self->{fontsize} = $fontsize; |
1375 | delete $self->{texture}; |
1335 | delete $self->{texture}; |
|
|
1336 | |
|
|
1337 | $self->update; |
1376 | $self->check_size; |
1338 | $self->check_size; |
1377 | $self->update; |
|
|
1378 | } |
1339 | } |
1379 | |
1340 | |
1380 | sub _draw { |
1341 | sub _draw { |
1381 | my ($self) = @_; |
1342 | my ($self) = @_; |
1382 | |
1343 | |
1383 | my $tex = $self->{texture} ||= do { |
1344 | my $tex = $self->{texture} ||= do { |
|
|
1345 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1384 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1346 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1385 | $self->{layout}->set_width ($self->{w}); |
1347 | $self->{layout}->set_width ($self->{w}); |
1386 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1348 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
|
|
1349 | |
1387 | new_from_layout CFClient::Texture $self->{layout} |
1350 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
|
|
1351 | |
|
|
1352 | $self->{ox} = int $self->{align} < 0 ? $self->{padding} |
|
|
1353 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
1354 | : ($self->{w} - $tex->{w}) * 0.5; |
|
|
1355 | |
|
|
1356 | $self->{oy} = int $self->{valign} < 0 ? $self->{padding} |
|
|
1357 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1358 | : ($self->{h} - $tex->{h}) * 0.5; |
|
|
1359 | |
|
|
1360 | $tex |
1388 | }; |
1361 | }; |
1389 | |
1362 | |
1390 | glEnable GL_BLEND; |
|
|
1391 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1392 | glEnable GL_TEXTURE_2D; |
1363 | glEnable GL_TEXTURE_2D; |
1393 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1364 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1394 | |
1365 | |
|
|
1366 | if ($tex->{format} == GL_ALPHA) { |
1395 | glColor @{$self->{fg}}; |
1367 | glColor @{$self->{fg}}; |
1396 | |
|
|
1397 | $self->{ox} = int ( |
|
|
1398 | $self->{align} < 0 ? $self->{padding} |
|
|
1399 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
|
|
1400 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1401 | ); |
|
|
1402 | |
|
|
1403 | $self->{oy} = int ( |
|
|
1404 | $self->{valign} < 0 ? $self->{padding} |
|
|
1405 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1406 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1407 | ); |
|
|
1408 | |
|
|
1409 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
1368 | $tex->draw_quad_alpha ($self->{ox}, $self->{oy}); |
|
|
1369 | } else { |
|
|
1370 | $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}); |
|
|
1371 | } |
1410 | |
1372 | |
1411 | glDisable GL_TEXTURE_2D; |
1373 | glDisable GL_TEXTURE_2D; |
1412 | glDisable GL_BLEND; |
|
|
1413 | } |
1374 | } |
1414 | |
1375 | |
1415 | ############################################################################# |
1376 | ############################################################################# |
1416 | |
1377 | |
1417 | package CFClient::UI::EntryBase; |
1378 | package CFClient::UI::EntryBase; |
… | |
… | |
1452 | $self->{layout}->set_text ("$text "); |
1413 | $self->{layout}->set_text ("$text "); |
1453 | |
1414 | |
1454 | $self->emit (changed => $self->{text}); |
1415 | $self->emit (changed => $self->{text}); |
1455 | } |
1416 | } |
1456 | |
1417 | |
|
|
1418 | sub set_text { |
|
|
1419 | my ($self, $text) = @_; |
|
|
1420 | |
|
|
1421 | $self->{cursor} = length $text; |
|
|
1422 | $self->_set_text ($text); |
|
|
1423 | $self->check_size; |
|
|
1424 | $self->update; |
|
|
1425 | } |
|
|
1426 | |
1457 | sub get_text { |
1427 | sub get_text { |
1458 | $_[0]{text} |
1428 | $_[0]{text} |
1459 | } |
1429 | } |
1460 | |
1430 | |
1461 | sub size_request { |
1431 | sub size_request { |
… | |
… | |
1468 | |
1438 | |
1469 | sub size_allocate { |
1439 | sub size_allocate { |
1470 | my ($self, $w, $h) = @_; |
1440 | my ($self, $w, $h) = @_; |
1471 | |
1441 | |
1472 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1442 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1473 | } |
|
|
1474 | |
|
|
1475 | sub set_text { |
|
|
1476 | my ($self, $text) = @_; |
|
|
1477 | |
|
|
1478 | $self->{cursor} = length $text; |
|
|
1479 | $self->_set_text ($text); |
|
|
1480 | $self->update; |
|
|
1481 | } |
1443 | } |
1482 | |
1444 | |
1483 | sub key_down { |
1445 | sub key_down { |
1484 | my ($self, $ev) = @_; |
1446 | my ($self, $ev) = @_; |
1485 | |
1447 | |
… | |
… | |
1673 | |
1635 | |
1674 | if ($GRAB == $self) { |
1636 | if ($GRAB == $self) { |
1675 | $self->{fg} = $self->{active_fg}; |
1637 | $self->{fg} = $self->{active_fg}; |
1676 | } |
1638 | } |
1677 | |
1639 | |
1678 | glEnable GL_BLEND; |
|
|
1679 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1680 | glEnable GL_TEXTURE_2D; |
1640 | glEnable GL_TEXTURE_2D; |
1681 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1641 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1682 | glColor 0, 0, 0, 1; |
1642 | glColor 0, 0, 0, 1; |
1683 | |
1643 | |
1684 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1644 | $tex[0]->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
1685 | |
1645 | |
1686 | glDisable GL_TEXTURE_2D; |
1646 | glDisable GL_TEXTURE_2D; |
1687 | glDisable GL_BLEND; |
|
|
1688 | |
1647 | |
1689 | $self->SUPER::_draw; |
1648 | $self->SUPER::_draw; |
1690 | } |
1649 | } |
1691 | |
1650 | |
1692 | ############################################################################# |
1651 | ############################################################################# |
… | |
… | |
1739 | |
1698 | |
1740 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1699 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1741 | |
1700 | |
1742 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1701 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1743 | |
1702 | |
1744 | glEnable GL_BLEND; |
1703 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1704 | |
1745 | glEnable GL_TEXTURE_2D; |
1705 | glEnable GL_TEXTURE_2D; |
1746 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1747 | |
|
|
1748 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1749 | |
|
|
1750 | $tex->draw_quad (0, 0, $s, $s); |
1706 | $tex->draw_quad_alpha (0, 0, $s, $s); |
1751 | |
|
|
1752 | glDisable GL_TEXTURE_2D; |
1707 | glDisable GL_TEXTURE_2D; |
1753 | glDisable GL_BLEND; |
|
|
1754 | } |
1708 | } |
1755 | |
1709 | |
1756 | ############################################################################# |
1710 | ############################################################################# |
1757 | |
1711 | |
1758 | package CFClient::UI::Image; |
1712 | package CFClient::UI::Image; |
… | |
… | |
1801 | glTranslate 0, -$self->{w}, 0; |
1755 | glTranslate 0, -$self->{w}, 0; |
1802 | |
1756 | |
1803 | ($w, $h) = ($h, $w); |
1757 | ($w, $h) = ($h, $w); |
1804 | } |
1758 | } |
1805 | |
1759 | |
1806 | glEnable GL_BLEND; |
|
|
1807 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1808 | glEnable GL_TEXTURE_2D; |
1760 | glEnable GL_TEXTURE_2D; |
1809 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1761 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1810 | |
1762 | |
1811 | $tex->draw_quad (0, 0, $w, $h); |
1763 | $tex->draw_quad_alpha (0, 0, $w, $h); |
1812 | |
1764 | |
1813 | glDisable GL_BLEND; |
|
|
1814 | glDisable GL_TEXTURE_2D; |
1765 | glDisable GL_TEXTURE_2D; |
1815 | } |
1766 | } |
1816 | |
1767 | |
1817 | ############################################################################# |
1768 | ############################################################################# |
1818 | |
1769 | |
… | |
… | |
2110 | $page ||= 2; |
2061 | $page ||= 2; |
2111 | |
2062 | |
2112 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
2063 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
2113 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
2064 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
2114 | |
2065 | |
2115 | glEnable GL_BLEND; |
|
|
2116 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2117 | glEnable GL_TEXTURE_2D; |
2066 | glEnable GL_TEXTURE_2D; |
2118 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2067 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2119 | |
2068 | |
2120 | # draw background |
2069 | # draw background |
2121 | $tex[1]->draw_quad (0, 0, $w, $h); |
2070 | $tex[1]->draw_quad_alpha (0, 0, $w, $h); |
2122 | |
2071 | |
2123 | # draw handle |
2072 | # draw handle |
2124 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
2073 | $tex[0]->draw_quad_alpha ($knob_a, 0, $knob_b - $knob_a, $h); |
2125 | |
2074 | |
2126 | glDisable GL_BLEND; |
|
|
2127 | glDisable GL_TEXTURE_2D; |
2075 | glDisable GL_TEXTURE_2D; |
2128 | } |
2076 | } |
2129 | |
2077 | |
2130 | ############################################################################# |
2078 | ############################################################################# |
2131 | |
2079 | |
… | |
… | |
2142 | fontsize => 1, |
2090 | fontsize => 1, |
2143 | can_events => 0, |
2091 | can_events => 0, |
2144 | #font => default_font |
2092 | #font => default_font |
2145 | @_, |
2093 | @_, |
2146 | |
2094 | |
2147 | layout => (new CFClient::Layout), |
2095 | layout => (new CFClient::Layout 1), |
2148 | par => [], |
2096 | par => [], |
2149 | height => 0, |
2097 | height => 0, |
2150 | children => [ |
2098 | children => [ |
2151 | (new CFClient::UI::Empty expand => 1), |
2099 | (new CFClient::UI::Empty expand => 1), |
2152 | (new CFClient::UI::Slider vertical => 1), |
2100 | (new CFClient::UI::Slider vertical => 1), |
… | |
… | |
2170 | |
2118 | |
2171 | my $layout = $self->{layout}; |
2119 | my $layout = $self->{layout}; |
2172 | |
2120 | |
2173 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2121 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2174 | $layout->set_width ($self->{children}[0]{w}); |
2122 | $layout->set_width ($self->{children}[0]{w}); |
2175 | $layout->set_text ($text); |
2123 | $layout->set_markup ($text); |
2176 | |
2124 | |
2177 | ($layout->size)[1] |
2125 | ($layout->size)[1] |
2178 | } |
2126 | } |
2179 | |
2127 | |
2180 | sub reflow { |
2128 | sub reflow { |
… | |
… | |
2218 | |
2166 | |
2219 | return unless $self->{h} > 0; |
2167 | return unless $self->{h} > 0; |
2220 | |
2168 | |
2221 | delete $self->{texture}; |
2169 | delete $self->{texture}; |
2222 | |
2170 | |
2223 | $ROOT->on_refresh ($self, sub { |
2171 | $ROOT->on_post_alloc ($self, sub { |
2224 | if (delete $self->{need_reflow}) { |
2172 | if (delete $self->{need_reflow}) { |
2225 | my $height = 0; |
2173 | my $height = 0; |
2226 | |
2174 | |
2227 | $height += $_->[0] = $self->text_height ($_->[2]) |
2175 | $height += $_->[0] = $self->text_height ($_->[2]) |
2228 | for @{$self->{par}}; |
2176 | for @{$self->{par}}; |
… | |
… | |
2236 | |
2184 | |
2237 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2185 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2238 | glClearColor 0, 0, 0, 0; |
2186 | glClearColor 0, 0, 0, 0; |
2239 | glClear GL_COLOR_BUFFER_BIT; |
2187 | glClear GL_COLOR_BUFFER_BIT; |
2240 | |
2188 | |
2241 | glEnable GL_BLEND; |
|
|
2242 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2243 | glEnable GL_TEXTURE_2D; |
2189 | glEnable GL_TEXTURE_2D; |
2244 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2190 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2245 | |
2191 | |
2246 | my $top = int $self->{children}[1]{range}[0]; |
2192 | my $top = int $self->{children}[1]{range}[0]; |
2247 | |
2193 | |
… | |
… | |
2256 | |
2202 | |
2257 | for my $par (@{$self->{par}}) { |
2203 | for my $par (@{$self->{par}}) { |
2258 | my $h = $par->[0]; |
2204 | my $h = $par->[0]; |
2259 | |
2205 | |
2260 | if ($y0 < $y + $h && $y < $y1) { |
2206 | if ($y0 < $y + $h && $y < $y1) { |
|
|
2207 | $layout->set_foreground (@{ $par->[1] }); |
2261 | $layout->set_text ($par->[2]); |
2208 | $layout->set_markup ($par->[2]); |
2262 | |
2209 | |
2263 | glColor @{ $par->[1] }; |
|
|
2264 | my ($W, $H) = $layout->size; |
2210 | my ($W, $H) = $layout->size; |
2265 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
2211 | CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); |
2266 | } |
2212 | } |
2267 | |
2213 | |
2268 | $y += $h; |
2214 | $y += $h; |
2269 | } |
2215 | } |
2270 | |
2216 | |
2271 | glDisable GL_TEXTURE_2D; |
2217 | glDisable GL_TEXTURE_2D; |
2272 | glDisable GL_BLEND; |
|
|
2273 | }; |
2218 | }; |
2274 | }); |
2219 | }); |
2275 | } |
2220 | } |
2276 | |
2221 | |
2277 | sub _draw { |
2222 | sub _draw { |
2278 | my ($self) = @_; |
2223 | my ($self) = @_; |
2279 | |
2224 | |
2280 | glEnable GL_BLEND; |
|
|
2281 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2282 | glEnable GL_TEXTURE_2D; |
2225 | glEnable GL_TEXTURE_2D; |
2283 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2226 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2284 | glColor 1, 1, 1, 1; |
2227 | glColor 1, 1, 1, 1; |
2285 | $self->{texture}->draw_quad (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2228 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2286 | glDisable GL_TEXTURE_2D; |
2229 | glDisable GL_TEXTURE_2D; |
2287 | glDisable GL_BLEND; |
|
|
2288 | |
2230 | |
2289 | $self->{children}[1]->draw; |
2231 | $self->{children}[1]->draw; |
2290 | |
2232 | |
2291 | } |
2233 | } |
2292 | |
2234 | |
… | |
… | |
2386 | @_, |
2328 | @_, |
2387 | can_events => 0, |
2329 | can_events => 0, |
2388 | ) |
2330 | ) |
2389 | } |
2331 | } |
2390 | |
2332 | |
2391 | sub set_markup { |
2333 | sub set_tooltip_from { |
2392 | my ($self, $text) = @_; |
2334 | my ($self, $widget) = @_; |
2393 | |
2335 | |
2394 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
2336 | $self->add (new CFClient::UI::Label |
2395 | $self->{label}->set_markup ($text); |
2337 | markup => $widget->{tooltip}, |
2396 | $self->add ($self->{label}); |
2338 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
|
|
2339 | fontsize => 0.8, |
|
|
2340 | fg => [0, 0, 0, 1], |
|
|
2341 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
|
|
2342 | ); |
2397 | } |
2343 | } |
2398 | |
2344 | |
2399 | sub size_request { |
2345 | sub size_request { |
2400 | my ($self) = @_; |
2346 | my ($self) = @_; |
2401 | |
|
|
2402 | $self->child->set_max_size ($::WIDTH * 0.3); |
|
|
2403 | |
2347 | |
2404 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2348 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
2405 | |
2349 | |
2406 | ($w + 4, $h + 4) |
2350 | ($w + 4, $h + 4) |
2407 | } |
2351 | } |
… | |
… | |
2413 | } |
2357 | } |
2414 | |
2358 | |
2415 | sub _draw { |
2359 | sub _draw { |
2416 | my ($self) = @_; |
2360 | my ($self) = @_; |
2417 | |
2361 | |
2418 | glPushMatrix; |
|
|
2419 | glTranslate 0.375, 0.375; |
2362 | glTranslate 0.375, 0.375; |
2420 | |
2363 | |
2421 | my ($w, $h) = @$self{qw(w h)}; |
2364 | my ($w, $h) = @$self{qw(w h)}; |
2422 | |
2365 | |
2423 | glColor 1, 0.8, 0.4; |
2366 | glColor 1, 0.8, 0.4; |
… | |
… | |
2434 | glVertex 0 , $h; |
2377 | glVertex 0 , $h; |
2435 | glVertex $w, $h; |
2378 | glVertex $w, $h; |
2436 | glVertex $w, 0; |
2379 | glVertex $w, 0; |
2437 | glEnd; |
2380 | glEnd; |
2438 | |
2381 | |
2439 | glPopMatrix; |
2382 | glTranslate 2 - 0.375, 2 - 0.375; |
2440 | |
|
|
2441 | glTranslate 2, 2; |
|
|
2442 | $self->SUPER::_draw; |
2383 | $self->SUPER::_draw; |
2443 | } |
2384 | } |
2444 | |
2385 | |
2445 | ############################################################################# |
2386 | ############################################################################# |
2446 | |
2387 | |
… | |
… | |
2464 | } |
2405 | } |
2465 | |
2406 | |
2466 | sub _draw { |
2407 | sub _draw { |
2467 | my ($self) = @_; |
2408 | my ($self) = @_; |
2468 | |
2409 | |
|
|
2410 | return unless $::CONN;#d# manage and cache textures differently |
2469 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2411 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2470 | |
2412 | |
2471 | # TODO animation |
2413 | # TODO animation |
2472 | if ($tex) { |
2414 | if ($tex) { |
2473 | glEnable GL_BLEND; |
|
|
2474 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2475 | glEnable GL_TEXTURE_2D; |
2415 | glEnable GL_TEXTURE_2D; |
2476 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2416 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2477 | glColor 1, 1, 1, 1; |
2417 | glColor 1, 1, 1, 1; |
2478 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2418 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2479 | glDisable GL_TEXTURE_2D; |
2419 | glDisable GL_TEXTURE_2D; |
2480 | glDisable GL_BLEND; |
|
|
2481 | } |
2420 | } |
2482 | } |
2421 | } |
2483 | |
2422 | |
2484 | ############################################################################# |
2423 | ############################################################################# |
2485 | |
2424 | |
2486 | package CFClient::UI::Menu; |
2425 | package CFClient::UI::InventoryItem; |
2487 | |
2426 | |
2488 | our @ISA = CFClient::UI::FancyFrame::; |
2427 | our @ISA = CFClient::UI::HBox::; |
2489 | |
|
|
2490 | use CFClient::OpenGL; |
|
|
2491 | |
2428 | |
2492 | sub new { |
2429 | sub new { |
2493 | my $class = shift; |
2430 | my $class = shift; |
2494 | |
2431 | |
2495 | my $self = $class->SUPER::new ( |
|
|
2496 | items => [], |
|
|
2497 | z => 100, |
|
|
2498 | @_, |
|
|
2499 | ); |
|
|
2500 | |
|
|
2501 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
2502 | |
|
|
2503 | for my $item (@{ $self->{items} }) { |
|
|
2504 | my ($widget, $cb) = @$item; |
|
|
2505 | |
|
|
2506 | # handle various types of items, only text for now |
|
|
2507 | if (!ref $widget) { |
|
|
2508 | $widget = new CFClient::UI::Label |
|
|
2509 | can_hover => 1, |
|
|
2510 | can_events => 1, |
|
|
2511 | text => $widget; |
|
|
2512 | } |
|
|
2513 | |
|
|
2514 | $self->{item}{$widget} = $item; |
|
|
2515 | |
|
|
2516 | $self->{vbox}->add ($widget); |
|
|
2517 | } |
|
|
2518 | |
|
|
2519 | $self |
|
|
2520 | } |
|
|
2521 | |
|
|
2522 | # popup given the event (must be a mouse button down event currently) |
|
|
2523 | sub popup { |
|
|
2524 | my ($self, $ev) = @_; |
|
|
2525 | |
|
|
2526 | $self->emit ("popdown"); |
|
|
2527 | |
|
|
2528 | # maybe save $GRAB? must be careful about events... |
|
|
2529 | $GRAB = $self; |
|
|
2530 | $self->{button} = $ev->{button}; |
|
|
2531 | |
|
|
2532 | $self->show; |
|
|
2533 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
|
|
2534 | } |
|
|
2535 | |
|
|
2536 | sub mouse_motion { |
|
|
2537 | my ($self, $ev, $x, $y) = @_; |
|
|
2538 | |
|
|
2539 | # TODO: should use vbox->find_widget or so |
|
|
2540 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
|
|
2541 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2542 | } |
|
|
2543 | |
|
|
2544 | sub button_up { |
|
|
2545 | my ($self, $ev, $x, $y) = @_; |
|
|
2546 | |
|
|
2547 | if ($ev->{button} == $self->{button}) { |
|
|
2548 | undef $GRAB; |
|
|
2549 | $self->hide; |
|
|
2550 | |
|
|
2551 | $self->emit ("popdown"); |
|
|
2552 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2553 | } |
|
|
2554 | } |
|
|
2555 | |
|
|
2556 | ############################################################################# |
|
|
2557 | |
|
|
2558 | package CFClient::UI::Root; |
|
|
2559 | |
|
|
2560 | our @ISA = CFClient::UI::Container::; |
|
|
2561 | |
|
|
2562 | use CFClient::OpenGL; |
|
|
2563 | |
|
|
2564 | sub check_size { |
|
|
2565 | my ($self) = @_; |
|
|
2566 | |
|
|
2567 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
|
|
2568 | } |
|
|
2569 | |
|
|
2570 | sub size_request { |
|
|
2571 | ($::WIDTH, $::HEIGHT) |
|
|
2572 | } |
|
|
2573 | |
|
|
2574 | sub configure { |
|
|
2575 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2576 | |
|
|
2577 | $self->SUPER::configure ($x, $y, $w, $h); |
|
|
2578 | |
|
|
2579 | for my $child (@{$self->{children}}) { |
|
|
2580 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2581 | |
|
|
2582 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2583 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2584 | $child->configure ($X, $Y, $W,$H); |
|
|
2585 | } |
|
|
2586 | } |
|
|
2587 | |
|
|
2588 | sub _topleft { |
|
|
2589 | my ($self, $x, $y) = @_; |
|
|
2590 | |
|
|
2591 | ($x, $y) |
|
|
2592 | } |
|
|
2593 | |
|
|
2594 | sub update { |
|
|
2595 | my ($self) = @_; |
|
|
2596 | |
|
|
2597 | $self->check_size; |
|
|
2598 | ::refresh (); |
|
|
2599 | } |
|
|
2600 | |
|
|
2601 | sub add { |
|
|
2602 | my ($self, $child) = @_; |
|
|
2603 | |
|
|
2604 | # integerize window positions |
|
|
2605 | $child->{x} = int $child->{x}; |
|
|
2606 | $child->{y} = int $child->{y}; |
|
|
2607 | |
|
|
2608 | $self->SUPER::add ($child); |
|
|
2609 | } |
|
|
2610 | |
|
|
2611 | sub on_refresh { |
|
|
2612 | my ($self, $id, $cb) = @_; |
|
|
2613 | |
|
|
2614 | $self->{refresh_hook}{$id} = $cb; |
|
|
2615 | } |
|
|
2616 | |
|
|
2617 | sub draw { |
|
|
2618 | my ($self) = @_; |
|
|
2619 | |
|
|
2620 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2621 | $_->() for values %$rcb; |
|
|
2622 | } |
|
|
2623 | |
|
|
2624 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2625 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2626 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2627 | |
|
|
2628 | glMatrixMode GL_PROJECTION; |
|
|
2629 | glLoadIdentity; |
|
|
2630 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2631 | glMatrixMode GL_MODELVIEW; |
|
|
2632 | glLoadIdentity; |
|
|
2633 | |
|
|
2634 | $self->_draw; |
|
|
2635 | } |
|
|
2636 | |
|
|
2637 | ############################################################################# |
|
|
2638 | |
|
|
2639 | package CFClient::UI::InventoryItem; |
|
|
2640 | |
|
|
2641 | our @ISA = CFClient::UI::HBox::; |
|
|
2642 | |
|
|
2643 | sub new { |
|
|
2644 | my $class = shift; |
|
|
2645 | |
|
|
2646 | my %args = @_; |
2432 | my %args = @_; |
2647 | |
2433 | |
2648 | my $item = $args{item}; |
2434 | my $item = delete $args{item}; |
2649 | |
2435 | |
2650 | my $desc = $item->{nrof} < 2 |
2436 | my $desc = $item->{nrof} < 2 |
2651 | ? $item->{name} |
2437 | ? $item->{name} |
2652 | : "$item->{nrof} $item->{name_pl}"; |
2438 | : "$item->{nrof} $item->{name_pl}"; |
2653 | |
2439 | |
… | |
… | |
2682 | |
2468 | |
2683 | 1 |
2469 | 1 |
2684 | }, |
2470 | }, |
2685 | %args |
2471 | %args |
2686 | ); |
2472 | ); |
|
|
2473 | |
2687 | $self->add(new CFClient::UI::Face |
2474 | $self->add (new CFClient::UI::Face |
2688 | can_events => 0, |
2475 | can_events => 0, |
2689 | face => $item->{face}, |
2476 | face => $item->{face}, |
2690 | anim => $item->{anim}, |
2477 | anim => $item->{anim}, |
2691 | animspeed => $item->{animspeed}); |
2478 | animspeed => $item->{animspeed}, |
|
|
2479 | ); |
|
|
2480 | |
2692 | $self->add(new CFClient::UI::Label |
2481 | $self->add (new CFClient::UI::Label |
2693 | can_events => 0, |
2482 | can_events => 0, |
2694 | text => $desc); |
2483 | text => $desc, |
|
|
2484 | ); |
2695 | |
2485 | |
2696 | $self |
2486 | $self |
2697 | } |
2487 | } |
2698 | |
2488 | |
2699 | ############################################################################# |
2489 | ############################################################################# |
… | |
… | |
2726 | for my $item (@items) { |
2516 | for my $item (@items) { |
2727 | my $desc = $item->{nrof} < 2 |
2517 | my $desc = $item->{nrof} < 2 |
2728 | ? $item->{name} |
2518 | ? $item->{name} |
2729 | : "$item->{nrof} $item->{name_pl}"; |
2519 | : "$item->{nrof} $item->{name_pl}"; |
2730 | |
2520 | |
2731 | $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); |
2521 | $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item); |
2732 | } |
2522 | } |
2733 | |
2523 | |
2734 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2524 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2735 | } |
2525 | } |
2736 | |
2526 | |
2737 | sub size_request { |
2527 | sub size_request { |
2738 | my ($self) = @_; |
2528 | my ($self) = @_; |
2739 | ($self->{req_w}, $self->{req_h}); |
2529 | ($self->{req_w}, $self->{req_h}); |
|
|
2530 | } |
|
|
2531 | |
|
|
2532 | ############################################################################# |
|
|
2533 | |
|
|
2534 | package CFClient::UI::Menu; |
|
|
2535 | |
|
|
2536 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
2537 | |
|
|
2538 | use CFClient::OpenGL; |
|
|
2539 | |
|
|
2540 | sub new { |
|
|
2541 | my $class = shift; |
|
|
2542 | |
|
|
2543 | my $self = $class->SUPER::new ( |
|
|
2544 | items => [], |
|
|
2545 | z => 100, |
|
|
2546 | @_, |
|
|
2547 | ); |
|
|
2548 | |
|
|
2549 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
2550 | |
|
|
2551 | for my $item (@{ $self->{items} }) { |
|
|
2552 | my ($widget, $cb) = @$item; |
|
|
2553 | |
|
|
2554 | # handle various types of items, only text for now |
|
|
2555 | if (!ref $widget) { |
|
|
2556 | $widget = new CFClient::UI::Label |
|
|
2557 | can_hover => 1, |
|
|
2558 | can_events => 1, |
|
|
2559 | text => $widget; |
|
|
2560 | } |
|
|
2561 | |
|
|
2562 | $self->{item}{$widget} = $item; |
|
|
2563 | |
|
|
2564 | $self->{vbox}->add ($widget); |
|
|
2565 | } |
|
|
2566 | |
|
|
2567 | $self |
|
|
2568 | } |
|
|
2569 | |
|
|
2570 | # popup given the event (must be a mouse button down event currently) |
|
|
2571 | sub popup { |
|
|
2572 | my ($self, $ev) = @_; |
|
|
2573 | |
|
|
2574 | $self->emit ("popdown"); |
|
|
2575 | |
|
|
2576 | # maybe save $GRAB? must be careful about events... |
|
|
2577 | $GRAB = $self; |
|
|
2578 | $self->{button} = $ev->{button}; |
|
|
2579 | |
|
|
2580 | $self->show; |
|
|
2581 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
|
|
2582 | } |
|
|
2583 | |
|
|
2584 | sub mouse_motion { |
|
|
2585 | my ($self, $ev, $x, $y) = @_; |
|
|
2586 | |
|
|
2587 | # TODO: should use vbox->find_widget or so |
|
|
2588 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
|
|
2589 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2590 | } |
|
|
2591 | |
|
|
2592 | sub button_up { |
|
|
2593 | my ($self, $ev, $x, $y) = @_; |
|
|
2594 | |
|
|
2595 | if ($ev->{button} == $self->{button}) { |
|
|
2596 | undef $GRAB; |
|
|
2597 | $self->hide; |
|
|
2598 | |
|
|
2599 | $self->emit ("popdown"); |
|
|
2600 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2601 | } |
|
|
2602 | } |
|
|
2603 | |
|
|
2604 | ############################################################################# |
|
|
2605 | |
|
|
2606 | package CFClient::UI::Statusbox; |
|
|
2607 | |
|
|
2608 | our @ISA = CFClient::UI::VBox::; |
|
|
2609 | |
|
|
2610 | sub reorder { |
|
|
2611 | my ($self) = @_; |
|
|
2612 | my $NOW = time; |
|
|
2613 | |
|
|
2614 | while (my ($k, $v) = each %{ $self->{item} }) { |
|
|
2615 | delete $self->{item}{$k} if $v->{timeout} < $NOW; |
|
|
2616 | } |
|
|
2617 | |
|
|
2618 | my @widgets; |
|
|
2619 | |
|
|
2620 | my @items = sort { |
|
|
2621 | $a->{pri} <=> $b->{pri} |
|
|
2622 | or $b->{id} <=> $a->{id} |
|
|
2623 | } values %{ $self->{item} }; |
|
|
2624 | |
|
|
2625 | my $count = 10 + 1; |
|
|
2626 | for my $item (@items) { |
|
|
2627 | last unless --$count; |
|
|
2628 | |
|
|
2629 | push @widgets, $item->{label} ||= do { |
|
|
2630 | # TODO: doesn't handle markup well (read as: at all) |
|
|
2631 | my $short = $item->{count} > 1 |
|
|
2632 | ? "<b>$item->{count} ×</b> $item->{text}" |
|
|
2633 | : $item->{text}; |
|
|
2634 | |
|
|
2635 | for ($short) { |
|
|
2636 | s/^\s+//; |
|
|
2637 | s/\012.*/…/s; |
|
|
2638 | my $len = int 40 / $item->{fontsize}; |
|
|
2639 | substr $_, $len, length, "…" if $len < length; |
|
|
2640 | } |
|
|
2641 | |
|
|
2642 | new CFClient::UI::Label |
|
|
2643 | markup => $short, |
|
|
2644 | tooltip => $item->{tooltip}, |
|
|
2645 | tooltip_font => $::FONT_PROP, |
|
|
2646 | tooltip_width => 0.67, |
|
|
2647 | fontsize => $item->{fontsize}, |
|
|
2648 | color => $item->{color}, |
|
|
2649 | can_events => 1, |
|
|
2650 | can_hover => 1 |
|
|
2651 | }; |
|
|
2652 | } |
|
|
2653 | |
|
|
2654 | $self->clear; |
|
|
2655 | $self->SUPER::add (reverse @widgets); |
|
|
2656 | } |
|
|
2657 | |
|
|
2658 | sub add { |
|
|
2659 | my ($self, $text, %arg) = @_; |
|
|
2660 | |
|
|
2661 | $text =~ s/^\s+//; |
|
|
2662 | $text =~ s/\s+$//; |
|
|
2663 | |
|
|
2664 | my $timeout = time + ((delete $arg{timeout}) || 60); |
|
|
2665 | |
|
|
2666 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
|
|
2667 | |
|
|
2668 | if (my $item = $self->{item}{$group}) { |
|
|
2669 | if ($item->{text} eq $text) { |
|
|
2670 | $item->{count}++; |
|
|
2671 | } else { |
|
|
2672 | $item->{count} = 1; |
|
|
2673 | $item->{text} = $item->{tooltip} = $text; |
|
|
2674 | } |
|
|
2675 | $item->{id} = ++$self->{id}; |
|
|
2676 | $item->{timeout} = $timeout; |
|
|
2677 | delete $item->{label}; |
|
|
2678 | } else { |
|
|
2679 | $self->{item}{$group} = { |
|
|
2680 | id => ++$self->{id}, |
|
|
2681 | text => $text, |
|
|
2682 | timeout => $timeout, |
|
|
2683 | tooltip => $text, |
|
|
2684 | fontsize => 0.8, |
|
|
2685 | color => [0.8, 0.8, 0.8, 0.8], |
|
|
2686 | pri => 0, |
|
|
2687 | count => 1, |
|
|
2688 | %arg, |
|
|
2689 | }; |
|
|
2690 | } |
|
|
2691 | |
|
|
2692 | $self->reorder; |
|
|
2693 | } |
|
|
2694 | |
|
|
2695 | ############################################################################# |
|
|
2696 | |
|
|
2697 | package CFClient::UI::Root; |
|
|
2698 | |
|
|
2699 | our @ISA = CFClient::UI::Container::; |
|
|
2700 | |
|
|
2701 | use CFClient::OpenGL; |
|
|
2702 | |
|
|
2703 | sub new { |
|
|
2704 | my $class = shift; |
|
|
2705 | |
|
|
2706 | $class->SUPER::new ( |
|
|
2707 | @_, |
|
|
2708 | ) |
|
|
2709 | } |
|
|
2710 | |
|
|
2711 | sub configure { |
|
|
2712 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2713 | |
|
|
2714 | $self->{w} = $w; |
|
|
2715 | $self->{h} = $h; |
|
|
2716 | } |
|
|
2717 | |
|
|
2718 | sub check_size { |
|
|
2719 | my ($self) = @_; |
|
|
2720 | |
|
|
2721 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
2722 | if $self->{w}; |
|
|
2723 | } |
|
|
2724 | |
|
|
2725 | sub size_request { |
|
|
2726 | my ($self) = @_; |
|
|
2727 | |
|
|
2728 | ($self->{w}, $self->{h}) |
|
|
2729 | } |
|
|
2730 | |
|
|
2731 | sub size_allocate { |
|
|
2732 | my ($self, $w, $h) = @_; |
|
|
2733 | |
|
|
2734 | my $old_w = $self->{old_w}; |
|
|
2735 | my $old_h = $self->{old_h}; |
|
|
2736 | |
|
|
2737 | if ($old_w && $old_h) { |
|
|
2738 | for my $child ($self->children) { |
|
|
2739 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2740 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2741 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2742 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2743 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2744 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2745 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2746 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2747 | } |
|
|
2748 | } |
|
|
2749 | |
|
|
2750 | for my $child ($self->children) { |
|
|
2751 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2752 | |
|
|
2753 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2754 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2755 | $child->configure ($X, $Y, $W, $H); |
|
|
2756 | } |
|
|
2757 | |
|
|
2758 | $self->{old_w} = $w; |
|
|
2759 | $self->{old_h} = $h; |
|
|
2760 | } |
|
|
2761 | |
|
|
2762 | sub coord2local { |
|
|
2763 | my ($self, $x, $y) = @_; |
|
|
2764 | |
|
|
2765 | ($x, $y) |
|
|
2766 | } |
|
|
2767 | |
|
|
2768 | sub coord2global { |
|
|
2769 | my ($self, $x, $y) = @_; |
|
|
2770 | |
|
|
2771 | ($x, $y) |
|
|
2772 | } |
|
|
2773 | |
|
|
2774 | sub update { |
|
|
2775 | my ($self) = @_; |
|
|
2776 | |
|
|
2777 | $self->check_size; |
|
|
2778 | $::WANT_REFRESH++; |
|
|
2779 | } |
|
|
2780 | |
|
|
2781 | sub add { |
|
|
2782 | my ($self, $child) = @_; |
|
|
2783 | |
|
|
2784 | # integerise window positions |
|
|
2785 | $child->{x} = int $child->{x}; |
|
|
2786 | $child->{y} = int $child->{y}; |
|
|
2787 | |
|
|
2788 | $self->SUPER::add ($child); |
|
|
2789 | } |
|
|
2790 | |
|
|
2791 | sub on_refresh { |
|
|
2792 | my ($self, $id, $cb) = @_; |
|
|
2793 | |
|
|
2794 | $self->{refresh_hook}{$id} = $cb; |
|
|
2795 | } |
|
|
2796 | |
|
|
2797 | sub on_post_alloc { |
|
|
2798 | my ($self, $id, $cb) = @_; |
|
|
2799 | |
|
|
2800 | $self->{post_alloc_hook}{$id} = $cb; |
|
|
2801 | } |
|
|
2802 | |
|
|
2803 | sub draw { |
|
|
2804 | my ($self) = @_; |
|
|
2805 | |
|
|
2806 | while ($self->{refresh_hook}) { |
|
|
2807 | $_->() |
|
|
2808 | for values %{delete $self->{refresh_hook}}; |
|
|
2809 | } |
|
|
2810 | |
|
|
2811 | if ($self->{check_size}) { |
|
|
2812 | my @queue = ([], []); |
|
|
2813 | |
|
|
2814 | for (;;) { |
|
|
2815 | if ($self->{check_size}) { |
|
|
2816 | # heuristic: check containers last |
|
|
2817 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
|
|
2818 | for values %{delete $self->{check_size}} |
|
|
2819 | } |
|
|
2820 | |
|
|
2821 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
|
|
2822 | |
|
|
2823 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
|
|
2824 | ? @$widget{qw(user_w user_h)} |
|
|
2825 | : $widget->size_request; |
|
|
2826 | |
|
|
2827 | if (delete $widget->{force_alloc} |
|
|
2828 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
|
|
2829 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
|
|
2830 | |
|
|
2831 | $widget->{req_w} = $w; |
|
|
2832 | $widget->{req_h} = $h; |
|
|
2833 | |
|
|
2834 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
|
|
2835 | |
|
|
2836 | $widget->{parent}->check_size |
|
|
2837 | if $widget->{parent}; |
|
|
2838 | } |
|
|
2839 | } |
|
|
2840 | } |
|
|
2841 | |
|
|
2842 | while ($self->{size_alloc}) { |
|
|
2843 | for (values %{delete $self->{size_alloc}}) { |
|
|
2844 | my ($widget, $w, $h) = @$_; |
|
|
2845 | |
|
|
2846 | $w = 0 if $w < 0; |
|
|
2847 | $h = 0 if $h < 0; |
|
|
2848 | |
|
|
2849 | $widget->{w} = $w; |
|
|
2850 | $widget->{h} = $h; |
|
|
2851 | $widget->size_allocate ($w, $h); |
|
|
2852 | $widget->emit (size_allocate => $w, $h); |
|
|
2853 | } |
|
|
2854 | } |
|
|
2855 | |
|
|
2856 | while ($self->{post_alloc_hook}) { |
|
|
2857 | $_->() |
|
|
2858 | for values %{delete $self->{post_alloc_hook}}; |
|
|
2859 | } |
|
|
2860 | |
|
|
2861 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2862 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2863 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2864 | |
|
|
2865 | glMatrixMode GL_PROJECTION; |
|
|
2866 | glLoadIdentity; |
|
|
2867 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2868 | glMatrixMode GL_MODELVIEW; |
|
|
2869 | glLoadIdentity; |
|
|
2870 | |
|
|
2871 | $self->_draw; |
2740 | } |
2872 | } |
2741 | |
2873 | |
2742 | ############################################################################# |
2874 | ############################################################################# |
2743 | |
2875 | |
2744 | package CFClient::UI; |
2876 | package CFClient::UI; |