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