… | |
… | |
50 | delete $TOOLTIP->{owner}; |
50 | delete $TOOLTIP->{owner}; |
51 | } |
51 | } |
52 | |
52 | |
53 | # class methods for events |
53 | # class methods for events |
54 | sub feed_sdl_key_down_event { |
54 | sub feed_sdl_key_down_event { |
55 | $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) |
55 | $FOCUS->emit (key_down => $_[0]) |
56 | if $FOCUS; |
56 | if $FOCUS; |
57 | } |
57 | } |
58 | |
58 | |
59 | sub feed_sdl_key_up_event { |
59 | sub feed_sdl_key_up_event { |
60 | $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) |
60 | $FOCUS->emit (key_up => $_[0]) |
61 | if $FOCUS; |
61 | if $FOCUS; |
62 | } |
62 | } |
63 | |
63 | |
64 | sub feed_sdl_button_down_event { |
64 | sub feed_sdl_button_down_event { |
65 | my ($ev) = @_; |
65 | my ($ev) = @_; |
… | |
… | |
74 | check_tooltip; |
74 | check_tooltip; |
75 | } |
75 | } |
76 | |
76 | |
77 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
77 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
78 | |
78 | |
79 | if ($GRAB) { |
79 | $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) |
80 | ($x, $y) = $GRAB->coord2local ($x, $y); |
80 | if $GRAB; |
81 | $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y); |
|
|
82 | } |
|
|
83 | } |
81 | } |
84 | |
82 | |
85 | sub feed_sdl_button_up_event { |
83 | sub feed_sdl_button_up_event { |
86 | my ($ev) = @_; |
84 | my ($ev) = @_; |
87 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
85 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
88 | |
86 | |
89 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
87 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
90 | |
88 | |
91 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
89 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
92 | |
90 | |
93 | if ($GRAB) { |
91 | $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) |
94 | ($x, $y) = $GRAB->coord2local ($x, $y); |
92 | if $GRAB; |
95 | $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y); |
|
|
96 | } |
|
|
97 | |
93 | |
98 | if (!$BUTTON_STATE) { |
94 | if (!$BUTTON_STATE) { |
99 | my $grab = $GRAB; undef $GRAB; |
95 | my $grab = $GRAB; undef $GRAB; |
100 | $grab->update if $grab; |
96 | $grab->update if $grab; |
101 | $GRAB->update if $GRAB; |
97 | $GRAB->update if $GRAB; |
… | |
… | |
117 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
113 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
118 | |
114 | |
119 | check_tooltip; |
115 | check_tooltip; |
120 | } |
116 | } |
121 | |
117 | |
122 | if ($HOVER) { |
|
|
123 | ($x, $y) = $HOVER->coord2local ($x, $y); |
|
|
124 | $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); |
118 | $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) |
125 | } |
119 | if $HOVER; |
126 | } |
120 | } |
127 | |
121 | |
128 | # convert position array to integers |
122 | # convert position array to integers |
129 | sub harmonize { |
123 | sub harmonize { |
130 | my ($vals) = @_; |
124 | my ($vals) = @_; |
… | |
… | |
144 | |
138 | |
145 | $_->update |
139 | $_->update |
146 | for @widgets; |
140 | for @widgets; |
147 | } |
141 | } |
148 | |
142 | |
|
|
143 | sub reconfigure_widgets { |
|
|
144 | # make a copy, otherwise C<for> complains about freed values. |
|
|
145 | my @widgets = values %WIDGET; |
|
|
146 | |
|
|
147 | $_->reconfigure |
|
|
148 | for @widgets; |
|
|
149 | } |
|
|
150 | |
149 | # call when resolution changes etc. |
151 | # call when resolution changes etc. |
150 | sub rescale_widgets { |
152 | sub rescale_widgets { |
151 | my ($sx, $sy) = @_; |
153 | my ($sx, $sy) = @_; |
152 | |
154 | |
153 | # make a copy, otherwise for complains about freed values. |
|
|
154 | my @widgets = values %WIDGET; |
155 | for my $widget (values %WIDGET) { |
155 | |
|
|
156 | for my $widget (@widgets) { |
|
|
157 | if ($widget->{toplevel}) { |
156 | if ($widget->{is_toplevel}) { |
158 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
157 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
159 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
158 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
160 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
159 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
161 | $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; |
160 | $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; |
162 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
161 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
163 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
162 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
164 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
163 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
165 | $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; |
164 | $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; |
166 | } |
165 | } |
167 | |
|
|
168 | $widget->reconfigure; |
|
|
169 | } |
166 | } |
|
|
167 | |
|
|
168 | reconfigure_widgets; |
170 | } |
169 | } |
171 | |
170 | |
172 | ############################################################################# |
171 | ############################################################################# |
173 | |
172 | |
174 | package CFClient::UI::Base; |
173 | package CFClient::UI::Base; |
… | |
… | |
224 | $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); |
223 | $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); |
225 | }, |
224 | }, |
226 | ); |
225 | ); |
227 | } |
226 | } |
228 | |
227 | |
229 | sub hide { |
228 | sub set_invisible { |
230 | my ($self) = @_; |
229 | my ($self) = @_; |
|
|
230 | |
|
|
231 | # broken show/hide model |
|
|
232 | |
|
|
233 | delete $self->{visible}; |
231 | |
234 | |
232 | undef $GRAB if $GRAB == $self; |
235 | undef $GRAB if $GRAB == $self; |
233 | undef $HOVER if $HOVER == $self; |
236 | undef $HOVER if $HOVER == $self; |
|
|
237 | |
|
|
238 | CFClient::UI::check_tooltip |
|
|
239 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
|
|
240 | |
|
|
241 | $self->focus_out; |
|
|
242 | } |
|
|
243 | |
|
|
244 | sub hide { |
|
|
245 | my ($self) = @_; |
|
|
246 | |
|
|
247 | $self->set_invisible; |
234 | |
248 | |
235 | $self->{parent}->remove ($self) |
249 | $self->{parent}->remove ($self) |
236 | if $self->{parent}; |
250 | if $self->{parent}; |
237 | } |
251 | } |
238 | |
252 | |
… | |
… | |
308 | } |
322 | } |
309 | |
323 | |
310 | sub set_tooltip { |
324 | sub set_tooltip { |
311 | my ($self, $tooltip) = @_; |
325 | my ($self, $tooltip) = @_; |
312 | |
326 | |
|
|
327 | $tooltip =~ s/^\s+//; |
|
|
328 | $tooltip =~ s/\s+$//; |
|
|
329 | |
|
|
330 | return if $self->{tooltip} eq $tooltip; |
|
|
331 | |
313 | $self->{tooltip} = $tooltip; |
332 | $self->{tooltip} = $tooltip; |
314 | |
333 | |
315 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
334 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
316 | delete $CFClient::UI::TOOLTIP->{owner}; |
335 | delete $CFClient::UI::TOOLTIP->{owner}; |
317 | CFClient::UI::check_tooltip; |
336 | CFClient::UI::check_tooltip; |
… | |
… | |
338 | return if $FOCUS == $self; |
357 | return if $FOCUS == $self; |
339 | return unless $self->{can_focus}; |
358 | return unless $self->{can_focus}; |
340 | |
359 | |
341 | my $focus = $FOCUS; $FOCUS = $self; |
360 | my $focus = $FOCUS; $FOCUS = $self; |
342 | |
361 | |
343 | $self->emit (focus_in => $focus); |
362 | $self->_emit (focus_in => $focus); |
344 | |
363 | |
345 | $focus->update if $focus; |
364 | $focus->update if $focus; |
346 | $FOCUS->update; |
365 | $FOCUS->update; |
347 | } |
366 | } |
348 | |
367 | |
… | |
… | |
351 | |
370 | |
352 | return unless $FOCUS == $self; |
371 | return unless $FOCUS == $self; |
353 | |
372 | |
354 | my $focus = $FOCUS; undef $FOCUS; |
373 | my $focus = $FOCUS; undef $FOCUS; |
355 | |
374 | |
356 | $self->emit (focus_out => $focus); |
375 | $self->_emit (focus_out => $focus); |
357 | |
376 | |
358 | $focus->update if $focus; #? |
377 | $focus->update if $focus; #? |
|
|
378 | |
|
|
379 | $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus |
|
|
380 | unless $FOCUS; |
359 | } |
381 | } |
360 | |
382 | |
361 | sub mouse_motion { } |
383 | sub mouse_motion { } |
362 | sub button_up { } |
384 | sub button_up { } |
363 | sub key_down { } |
385 | sub key_down { } |
364 | sub key_up { } |
386 | sub key_up { } |
365 | |
387 | |
366 | sub button_down { |
388 | sub button_down { |
367 | my ($self, $ev, $x, $y) = @_; |
389 | my ($self, $ev, $x, $y) = @_; |
368 | |
390 | |
369 | $self->focus_in; |
391 | $self->focus_in; |
… | |
… | |
403 | if ($ENV{PCLIENT_DEBUG}) { |
425 | if ($ENV{PCLIENT_DEBUG}) { |
404 | glPushMatrix; |
426 | glPushMatrix; |
405 | glColor 1, 1, 0, 1; |
427 | glColor 1, 1, 0, 1; |
406 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
428 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
407 | glBegin GL_LINE_LOOP; |
429 | glBegin GL_LINE_LOOP; |
408 | glVertex 0 , 0; |
430 | glVertex 0 , 0; |
409 | glVertex $self->{w}, 0; |
431 | glVertex $self->{w} - 1, 0; |
410 | glVertex $self->{w}, $self->{h}; |
432 | glVertex $self->{w} - 1, $self->{h} - 1; |
411 | glVertex 0 , $self->{h}; |
433 | glVertex 0 , $self->{h} - 1; |
412 | glEnd; |
434 | glEnd; |
413 | glPopMatrix; |
435 | glPopMatrix; |
414 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
436 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
415 | } |
437 | } |
416 | } |
438 | } |
… | |
… | |
463 | my ($self, $signal, $cb) = @_; |
485 | my ($self, $signal, $cb) = @_; |
464 | |
486 | |
465 | push @{ $self->{signal_cb}{$signal} }, $cb; |
487 | push @{ $self->{signal_cb}{$signal} }, $cb; |
466 | } |
488 | } |
467 | |
489 | |
|
|
490 | sub _emit { |
|
|
491 | my ($self, $signal, @args) = @_; |
|
|
492 | |
|
|
493 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
|
|
494 | } |
|
|
495 | |
468 | sub emit { |
496 | sub emit { |
469 | my ($self, $signal, @args) = @_; |
497 | my ($self, $signal, @args) = @_; |
470 | |
498 | |
471 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
499 | $self->_emit ($signal, @args) |
|
|
500 | || $self->$signal (@args); |
472 | } |
501 | } |
473 | |
502 | |
474 | sub DESTROY { |
503 | sub DESTROY { |
475 | my ($self) = @_; |
504 | my ($self) = @_; |
476 | |
505 | |
… | |
… | |
739 | |
768 | |
740 | package CFClient::UI::ViewPort; |
769 | package CFClient::UI::ViewPort; |
741 | |
770 | |
742 | our @ISA = CFClient::UI::Window::; |
771 | our @ISA = CFClient::UI::Window::; |
743 | |
772 | |
|
|
773 | sub new { |
|
|
774 | my $class = shift; |
|
|
775 | |
|
|
776 | $class->SUPER::new ( |
|
|
777 | scroll_x => 0, |
|
|
778 | scroll_y => 1, |
|
|
779 | @_, |
|
|
780 | ) |
|
|
781 | } |
|
|
782 | |
744 | sub size_request { |
783 | sub size_request { |
745 | my ($self) = @_; |
784 | my ($self) = @_; |
746 | |
785 | |
747 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
786 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
748 | $self->child->configure (0, 0, @$self{qw(child_w child_h)}); |
|
|
749 | |
787 | |
750 | @$self{qw(child_w child_h)} |
788 | @$self{qw(child_w child_h)} |
751 | } |
789 | } |
752 | |
790 | |
753 | sub size_allocate { |
791 | sub size_allocate { |
754 | my ($self, $w, $h) = @_; |
792 | my ($self, $w, $h) = @_; |
755 | |
793 | |
|
|
794 | $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; |
|
|
795 | $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; |
|
|
796 | |
|
|
797 | $self->child->configure (0, 0, $w, $h); |
756 | $self->update; |
798 | $self->update; |
757 | } |
799 | } |
758 | |
800 | |
759 | sub set_offset { |
801 | sub set_offset { |
760 | my ($self, $x, $y) = @_; |
802 | my ($self, $x, $y) = @_; |
… | |
… | |
814 | |
856 | |
815 | my $slider = new CFClient::UI::Slider |
857 | my $slider = new CFClient::UI::Slider |
816 | vertical => 1, |
858 | vertical => 1, |
817 | range => [0, 0, 1, 0.01], # HACK fix |
859 | range => [0, 0, 1, 0.01], # HACK fix |
818 | connect_changed => sub { |
860 | connect_changed => sub { |
819 | $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); |
861 | $self->{vp}->set_offset (0, $_[1]); |
820 | }, |
862 | }, |
821 | ; |
863 | ; |
822 | |
864 | |
823 | $self = $class->SUPER::new ( |
865 | $self = $class->SUPER::new ( |
824 | vp => (new CFClient::UI::ViewPort expand => 1), |
866 | vp => (new CFClient::UI::ViewPort expand => 1), |
… | |
… | |
829 | $self->{vp}->add ($self->{scrolled}); |
871 | $self->{vp}->add ($self->{scrolled}); |
830 | $self->add ($self->{vp}); |
872 | $self->add ($self->{vp}); |
831 | $self->add ($self->{slider}); |
873 | $self->add ($self->{slider}); |
832 | |
874 | |
833 | $self |
875 | $self |
|
|
876 | } |
|
|
877 | |
|
|
878 | sub size_allocate { |
|
|
879 | my ($self, $w, $h) = @_; |
|
|
880 | |
|
|
881 | $self->SUPER::size_allocate ($w, $h); |
|
|
882 | |
|
|
883 | my $child = $self->{vp}->child; |
|
|
884 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
834 | } |
885 | } |
835 | |
886 | |
836 | #TODO# update range on size_allocate depending on child |
887 | #TODO# update range on size_allocate depending on child |
837 | # update viewport offset on scroll |
888 | # update viewport offset on scroll |
838 | |
889 | |
… | |
… | |
892 | my $class = shift; |
943 | my $class = shift; |
893 | |
944 | |
894 | # TODO: user_x, user_y, overwrite moveto? |
945 | # TODO: user_x, user_y, overwrite moveto? |
895 | |
946 | |
896 | my $self = $class->SUPER::new ( |
947 | my $self = $class->SUPER::new ( |
897 | bg => [1, 1, 1, 1], |
948 | bg => [1, 1, 1, 1], |
898 | border_bg => [1, 1, 1, 1], |
949 | border_bg => [1, 1, 1, 1], |
899 | border => 0.6, |
950 | border => 0.6, |
900 | toplevel => 1, |
951 | is_toplevel => 1, |
901 | can_events => 1, |
952 | can_events => 1, |
902 | @_ |
953 | @_ |
903 | ); |
954 | ); |
904 | |
955 | |
905 | $self->{title} &&= new CFClient::UI::Label |
956 | $self->{title} &&= new CFClient::UI::Label |
906 | align => 0, |
957 | align => 0, |
… | |
… | |
1046 | sub new { |
1097 | sub new { |
1047 | my $class = shift; |
1098 | my $class = shift; |
1048 | |
1099 | |
1049 | $class->SUPER::new ( |
1100 | $class->SUPER::new ( |
1050 | col_expand => [], |
1101 | col_expand => [], |
1051 | @_ |
1102 | @_, |
1052 | ) |
1103 | ) |
1053 | } |
1104 | } |
1054 | |
1105 | |
1055 | sub add { |
1106 | sub add { |
1056 | my ($self, $x, $y, $child) = @_; |
1107 | my ($self, $x, $y, $child) = @_; |
… | |
… | |
1488 | active_fg => [0, 0, 0], |
1539 | active_fg => [0, 0, 0], |
1489 | can_hover => 1, |
1540 | can_hover => 1, |
1490 | can_focus => 1, |
1541 | can_focus => 1, |
1491 | valign => 0, |
1542 | valign => 0, |
1492 | can_events => 1, |
1543 | can_events => 1, |
|
|
1544 | #text => ... |
1493 | @_ |
1545 | @_ |
1494 | ) |
1546 | ) |
1495 | } |
1547 | } |
1496 | |
1548 | |
1497 | sub _set_text { |
1549 | sub _set_text { |
… | |
… | |
1507 | $self->{text} = $text; |
1559 | $self->{text} = $text; |
1508 | |
1560 | |
1509 | $text =~ s/./*/g if $self->{hidden}; |
1561 | $text =~ s/./*/g if $self->{hidden}; |
1510 | $self->{layout}->set_text ("$text "); |
1562 | $self->{layout}->set_text ("$text "); |
1511 | |
1563 | |
1512 | $self->emit (changed => $self->{text}); |
1564 | $self->_emit (changed => $self->{text}); |
1513 | } |
1565 | } |
1514 | |
1566 | |
1515 | sub set_text { |
1567 | sub set_text { |
1516 | my ($self, $text) = @_; |
1568 | my ($self, $text) = @_; |
1517 | |
1569 | |
… | |
… | |
1529 | my ($self) = @_; |
1581 | my ($self) = @_; |
1530 | |
1582 | |
1531 | my ($w, $h) = $self->SUPER::size_request; |
1583 | my ($w, $h) = $self->SUPER::size_request; |
1532 | |
1584 | |
1533 | ($w + 1, $h) # add 1 for cursor |
1585 | ($w + 1, $h) # add 1 for cursor |
1534 | } |
|
|
1535 | |
|
|
1536 | sub size_allocate { |
|
|
1537 | my ($self, $w, $h) = @_; |
|
|
1538 | |
|
|
1539 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
|
|
1540 | } |
1586 | } |
1541 | |
1587 | |
1542 | sub key_down { |
1588 | sub key_down { |
1543 | my ($self, $ev) = @_; |
1589 | my ($self, $ev) = @_; |
1544 | |
1590 | |
… | |
… | |
1559 | } elsif ($sym == CFClient::SDLK_HOME) { |
1605 | } elsif ($sym == CFClient::SDLK_HOME) { |
1560 | $self->{cursor} = 0; |
1606 | $self->{cursor} = 0; |
1561 | } elsif ($sym == CFClient::SDLK_END) { |
1607 | } elsif ($sym == CFClient::SDLK_END) { |
1562 | $self->{cursor} = length $text; |
1608 | $self->{cursor} = length $text; |
1563 | } elsif ($uni == 27) { |
1609 | } elsif ($uni == 27) { |
1564 | $self->emit ('escape'); |
1610 | $self->_emit ('escape'); |
1565 | } elsif ($uni) { |
1611 | } elsif ($uni) { |
1566 | substr $text, $self->{cursor}++, 0, chr $uni; |
1612 | substr $text, $self->{cursor}++, 0, chr $uni; |
1567 | } |
1613 | } |
1568 | |
1614 | |
1569 | $self->_set_text ($text); |
1615 | $self->_set_text ($text); |
… | |
… | |
1656 | if ($sym == 13) { |
1702 | if ($sym == 13) { |
1657 | unshift @{$self->{history}}, |
1703 | unshift @{$self->{history}}, |
1658 | my $txt = $self->get_text; |
1704 | my $txt = $self->get_text; |
1659 | $self->{history_pointer} = -1; |
1705 | $self->{history_pointer} = -1; |
1660 | $self->{history_saveback} = ''; |
1706 | $self->{history_saveback} = ''; |
1661 | $self->emit (activate => $txt); |
1707 | $self->_emit (activate => $txt); |
1662 | $self->update; |
1708 | $self->update; |
1663 | |
1709 | |
1664 | } elsif ($sym == CFClient::SDLK_UP) { |
1710 | } elsif ($sym == CFClient::SDLK_UP) { |
1665 | if ($self->{history_pointer} < 0) { |
1711 | if ($self->{history_pointer} < 0) { |
1666 | $self->{history_saveback} = $self->get_text; |
1712 | $self->{history_saveback} = $self->get_text; |
… | |
… | |
1714 | can_events => 1, |
1760 | can_events => 1, |
1715 | @_ |
1761 | @_ |
1716 | ) |
1762 | ) |
1717 | } |
1763 | } |
1718 | |
1764 | |
|
|
1765 | sub activate { } |
|
|
1766 | |
1719 | sub button_up { |
1767 | sub button_up { |
1720 | my ($self, $ev, $x, $y) = @_; |
1768 | my ($self, $ev, $x, $y) = @_; |
1721 | |
1769 | |
|
|
1770 | $self->emit ("activate") |
1722 | if ($x >= 0 && $x < $self->{w} |
1771 | if $x >= 0 && $x < $self->{w} |
1723 | && $y >= 0 && $y < $self->{h}) { |
1772 | && $y >= 0 && $y < $self->{h}; |
1724 | $self->emit ("activate"); |
|
|
1725 | } |
|
|
1726 | } |
1773 | } |
1727 | |
1774 | |
1728 | sub _draw { |
1775 | sub _draw { |
1729 | my ($self) = @_; |
1776 | my ($self) = @_; |
1730 | |
1777 | |
… | |
… | |
1782 | my ($self, $ev, $x, $y) = @_; |
1829 | my ($self, $ev, $x, $y) = @_; |
1783 | |
1830 | |
1784 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1831 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1785 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1832 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1786 | $self->{state} = !$self->{state}; |
1833 | $self->{state} = !$self->{state}; |
1787 | $self->emit (changed => $self->{state}); |
1834 | $self->_emit (changed => $self->{state}); |
1788 | } |
1835 | } |
1789 | } |
1836 | } |
1790 | |
1837 | |
1791 | sub _draw { |
1838 | sub _draw { |
1792 | my ($self) = @_; |
1839 | my ($self) = @_; |
… | |
… | |
2067 | my $self = $class->SUPER::new ( |
2114 | my $self = $class->SUPER::new ( |
2068 | fg => [1, 1, 1], |
2115 | fg => [1, 1, 1], |
2069 | active_fg => [0, 0, 0], |
2116 | active_fg => [0, 0, 0], |
2070 | bg => [0, 0, 0, 0.2], |
2117 | bg => [0, 0, 0, 0.2], |
2071 | active_bg => [1, 1, 1, 0.5], |
2118 | active_bg => [1, 1, 1, 0.5], |
2072 | range => [0, 0, 100, 10], |
2119 | range => [0, 0, 100, 10, 0], |
2073 | req_w => $::WIDTH / 80, |
2120 | req_w => $::WIDTH / 80, |
2074 | req_h => $::WIDTH / 80, |
2121 | req_h => $::WIDTH / 80, |
2075 | vertical => 0, |
2122 | vertical => 0, |
2076 | can_hover => 1, |
2123 | can_hover => 1, |
2077 | inner_pad => 0.02, |
2124 | inner_pad => 0.02, |
… | |
… | |
2082 | $self->update; |
2129 | $self->update; |
2083 | |
2130 | |
2084 | $self |
2131 | $self |
2085 | } |
2132 | } |
2086 | |
2133 | |
|
|
2134 | sub set_range { |
|
|
2135 | my ($self, $range) = @_; |
|
|
2136 | |
|
|
2137 | $self->{range} = $range; |
|
|
2138 | |
|
|
2139 | $self->update; |
|
|
2140 | } |
|
|
2141 | |
2087 | sub set_value { |
2142 | sub set_value { |
2088 | my ($self, $value) = @_; |
2143 | my ($self, $value) = @_; |
2089 | |
2144 | |
2090 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
2145 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
2091 | |
2146 | |
2092 | $hi = $lo + 1 if $hi <= $lo; |
2147 | $hi = $lo + 1 if $hi <= $lo; |
2093 | |
2148 | |
|
|
2149 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2150 | |
2094 | $value = $lo if $value < $lo; |
2151 | $value = $lo if $value < $lo; |
2095 | $value = $hi if $value > $hi; |
2152 | $value = $hi - $page if $value > $hi - $page; |
2096 | |
2153 | |
2097 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
2154 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
2098 | if $unit; |
2155 | if $unit; |
2099 | |
2156 | |
2100 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2101 | |
|
|
2102 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
2157 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
2103 | |
2158 | |
2104 | if ($value != $old_value) { |
2159 | if ($value != $old_value) { |
2105 | $self->emit (changed => $value); |
2160 | $self->_emit (changed => $value); |
2106 | $self->update; |
2161 | $self->update; |
2107 | } |
2162 | } |
2108 | } |
2163 | } |
2109 | |
2164 | |
2110 | sub size_request { |
2165 | sub size_request { |
… | |
… | |
2118 | |
2173 | |
2119 | sub button_down { |
2174 | sub button_down { |
2120 | my ($self, $ev, $x, $y) = @_; |
2175 | my ($self, $ev, $x, $y) = @_; |
2121 | |
2176 | |
2122 | $self->SUPER::button_down ($ev, $x, $y); |
2177 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
2178 | |
|
|
2179 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
|
|
2180 | |
2123 | $self->mouse_motion ($ev, $x, $y); |
2181 | $self->mouse_motion ($ev, $x, $y); |
2124 | } |
2182 | } |
2125 | |
2183 | |
2126 | sub mouse_motion { |
2184 | sub mouse_motion { |
2127 | my ($self, $ev, $x, $y) = @_; |
2185 | my ($self, $ev, $x, $y) = @_; |
… | |
… | |
2129 | if ($GRAB == $self) { |
2187 | if ($GRAB == $self) { |
2130 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2188 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2131 | |
2189 | |
2132 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2190 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2133 | |
2191 | |
2134 | $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; |
2192 | $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); |
2135 | |
2193 | |
2136 | $self->set_value ($x * ($hi - $lo) + $lo); |
2194 | $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); |
2137 | } |
2195 | } |
2138 | } |
2196 | } |
2139 | |
2197 | |
2140 | sub update { |
2198 | sub update { |
2141 | my ($self) = @_; |
2199 | my ($self) = @_; |
2142 | |
2200 | |
2143 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
2201 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
2144 | $self->set_value ($self->{range}[0]); |
2202 | $self->set_value ($self->{range}[0]); |
2145 | |
2203 | |
2146 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2204 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2205 | my $range = ($hi - $page - $lo) || 1e-100; |
2147 | |
2206 | |
2148 | my $inner_w = 1 - 2 * $self->{inner_pad}; |
2207 | my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; |
2149 | |
2208 | |
2150 | $self->{scale} = ($inner_w / ($hi - $lo)) || 1; |
2209 | $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; |
|
|
2210 | $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; |
2151 | |
2211 | |
2152 | $page = $self->{scale} * $page || 10 / ($self->{w} || 1); |
2212 | $value = ($value - $lo) / $range; |
2153 | $value = $self->{scale} * ($value - $lo); |
2213 | $value = $value * $self->{scale} + $self->{offset}; |
2154 | |
2214 | |
2155 | $value = $self->{inner_pad} + ($value - $page * 0.5); |
|
|
2156 | |
|
|
2157 | $value = 0 if $value < 0; |
|
|
2158 | $page = 1 - $value if $value + $page > 1; |
|
|
2159 | |
|
|
2160 | $self->{knob_x} = $value; |
2215 | $self->{knob_x} = $value - $knob_w * 0.5; |
2161 | $self->{knob_w} = $page; |
2216 | $self->{knob_w} = $knob_w; |
2162 | }); |
2217 | }); |
2163 | |
2218 | |
2164 | $self->SUPER::update; |
2219 | $self->SUPER::update; |
2165 | } |
2220 | } |
2166 | |
2221 | |
… | |
… | |
2190 | # draw handle |
2245 | # draw handle |
2191 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2246 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2192 | |
2247 | |
2193 | glDisable GL_TEXTURE_2D; |
2248 | glDisable GL_TEXTURE_2D; |
2194 | } |
2249 | } |
|
|
2250 | |
|
|
2251 | ############################################################################# |
|
|
2252 | |
|
|
2253 | package CFClient::UI::ValSlider; |
|
|
2254 | |
|
|
2255 | our @ISA = CFClient::UI::HBox::; |
|
|
2256 | |
|
|
2257 | sub new { |
|
|
2258 | my ($class, %arg) = @_; |
|
|
2259 | |
|
|
2260 | my $range = delete $arg{range}; |
|
|
2261 | |
|
|
2262 | my $self = $class->SUPER::new ( |
|
|
2263 | slider => (new CFClient::UI::Slider expand => 1, range => $range), |
|
|
2264 | entry => (new CFClient::UI::Label text => "", template => delete $arg{template}), |
|
|
2265 | to_value => sub { shift }, |
|
|
2266 | from_value => sub { shift }, |
|
|
2267 | %arg, |
|
|
2268 | ); |
|
|
2269 | |
|
|
2270 | $self->{slider}->connect (changed => sub { |
|
|
2271 | my ($self, $value) = @_; |
|
|
2272 | $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value)); |
|
|
2273 | $self->{parent}->emit (changed => $value); |
|
|
2274 | }); |
|
|
2275 | |
|
|
2276 | # $self->{entry}->connect (changed => sub { |
|
|
2277 | # my ($self, $value) = @_; |
|
|
2278 | # $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value)); |
|
|
2279 | # $self->{parent}->emit (changed => $value); |
|
|
2280 | # }); |
|
|
2281 | |
|
|
2282 | $self->add ($self->{slider}, $self->{entry}); |
|
|
2283 | |
|
|
2284 | $self->{slider}->emit (changed => $self->{slider}{range}[0]); |
|
|
2285 | |
|
|
2286 | $self |
|
|
2287 | } |
|
|
2288 | |
|
|
2289 | sub set_range { shift->{slider}->set_range (@_) } |
|
|
2290 | sub set_value { shift->{slider}->set_value (@_) } |
2195 | |
2291 | |
2196 | ############################################################################# |
2292 | ############################################################################# |
2197 | |
2293 | |
2198 | package CFClient::UI::TextView; |
2294 | package CFClient::UI::TextView; |
2199 | |
2295 | |
… | |
… | |
2241 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2337 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2242 | |
2338 | |
2243 | $self->reflow; |
2339 | $self->reflow; |
2244 | } |
2340 | } |
2245 | |
2341 | |
2246 | sub text_height { |
2342 | sub text_size { |
2247 | my ($self, $text, $indent) = @_; |
2343 | my ($self, $text, $indent) = @_; |
2248 | |
2344 | |
2249 | my $layout = $self->{layout}; |
2345 | my $layout = $self->{layout}; |
2250 | |
2346 | |
2251 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2347 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
2252 | $layout->set_width ($self->{children}[0]{w} - $indent); |
2348 | $layout->set_width ($self->{children}[0]{w} - $indent); |
2253 | $layout->set_markup ($text); |
2349 | $layout->set_markup ($text); |
2254 | |
2350 | |
2255 | ($layout->size)[1] |
2351 | $layout->size |
2256 | } |
2352 | } |
2257 | |
2353 | |
2258 | sub reflow { |
2354 | sub reflow { |
2259 | my ($self) = @_; |
2355 | my ($self) = @_; |
2260 | |
2356 | |
2261 | $self->{need_reflow}++; |
2357 | $self->{need_reflow}++; |
2262 | $self->update; |
2358 | $self->update; |
2263 | } |
2359 | } |
2264 | |
2360 | |
|
|
2361 | sub set_offset { |
|
|
2362 | my ($self, $offset) = @_; |
|
|
2363 | |
|
|
2364 | # todo: base offset on lines or so, not on pixels |
|
|
2365 | $self->{children}[1]->set_value ($offset); |
|
|
2366 | } |
|
|
2367 | |
|
|
2368 | sub clear { |
|
|
2369 | my ($self) = @_; |
|
|
2370 | |
|
|
2371 | $self->{par} = []; |
|
|
2372 | $self->{height} = 0; |
|
|
2373 | $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); |
|
|
2374 | } |
|
|
2375 | |
2265 | sub add_paragraph { |
2376 | sub add_paragraph { |
2266 | my ($self, $color, $text, $indent) = @_; |
2377 | my ($self, $color, $text, $indent) = @_; |
2267 | |
2378 | |
2268 | #TODO: intelligently "reformat" paragraph |
|
|
2269 | |
|
|
2270 | for my $line (split /\n/, $text) { |
2379 | for my $line (split /\n/, $text) { |
2271 | my $height = $self->text_height ($line); |
2380 | my ($w, $h) = $self->text_size ($line); |
2272 | $self->{height} += $height; |
2381 | $self->{height} += $h; |
2273 | push @{$self->{par}}, [$height, $color, $indent, $line]; |
2382 | push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line]; |
2274 | } |
2383 | } |
2275 | |
2384 | |
2276 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
2385 | $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); |
2277 | $self->{children}[1]->update; |
|
|
2278 | } |
2386 | } |
2279 | |
2387 | |
2280 | sub update { |
2388 | sub update { |
2281 | my ($self) = @_; |
2389 | my ($self) = @_; |
2282 | |
2390 | |
… | |
… | |
2285 | return unless $self->{h} > 0; |
2393 | return unless $self->{h} > 0; |
2286 | |
2394 | |
2287 | delete $self->{texture}; |
2395 | delete $self->{texture}; |
2288 | |
2396 | |
2289 | $ROOT->on_post_alloc ($self, sub { |
2397 | $ROOT->on_post_alloc ($self, sub { |
|
|
2398 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
|
|
2399 | |
2290 | if (delete $self->{need_reflow}) { |
2400 | if (delete $self->{need_reflow}) { |
2291 | my $height = 0; |
2401 | my $height = 0; |
2292 | |
2402 | |
2293 | $height += $_->[0] = $self->text_height ($_->[3], $_->[2]) |
2403 | my $layout = $self->{layout}; |
|
|
2404 | |
|
|
2405 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
2406 | |
2294 | for @{$self->{par}}; |
2407 | for (@{$self->{par}}) { |
|
|
2408 | if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support |
|
|
2409 | $layout->set_width ($W - $_->[3]); |
|
|
2410 | $layout->set_markup ($_->[4]); |
|
|
2411 | my ($w, $h) = $layout->size; |
|
|
2412 | $_->[0] = $w + $_->[3]; |
|
|
2413 | $_->[1] = $h; |
|
|
2414 | } |
|
|
2415 | |
|
|
2416 | $height += $_->[1]; |
|
|
2417 | } |
2295 | |
2418 | |
2296 | $self->{height} = $height; |
2419 | $self->{height} = $height; |
2297 | |
2420 | |
2298 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
2421 | $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); |
2299 | $self->{children}[1]->update; |
|
|
2300 | |
2422 | |
2301 | delete $self->{texture}; |
2423 | delete $self->{texture}; |
2302 | } |
2424 | } |
2303 | |
2425 | |
2304 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2426 | $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { |
2305 | glClearColor 0.5, 0.5, 0.5, 0; |
2427 | glClearColor 0.5, 0.5, 0.5, 0; |
2306 | glClear GL_COLOR_BUFFER_BIT; |
2428 | glClear GL_COLOR_BUFFER_BIT; |
2307 | |
2429 | |
2308 | my $top = int $self->{children}[1]{range}[0]; |
2430 | my $top = int $self->{children}[1]{range}[0]; |
2309 | |
2431 | |
2310 | my $y0 = $top; |
2432 | my $y0 = $top; |
2311 | my $y1 = $top + $self->{h}; |
2433 | my $y1 = $top + $H; |
2312 | |
2434 | |
2313 | my $y = 0; |
2435 | my $y = 0; |
2314 | |
2436 | |
2315 | my $layout = $self->{layout}; |
2437 | my $layout = $self->{layout}; |
2316 | |
2438 | |
2317 | $layout->set_font ($self->{font}) if $self->{font}; |
2439 | $layout->set_font ($self->{font}) if $self->{font}; |
2318 | |
2440 | |
2319 | glEnable GL_BLEND; |
2441 | glEnable GL_BLEND; |
|
|
2442 | #TODO# not correct in windows where rgba is forced off |
2320 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2443 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
2321 | |
2444 | |
2322 | for my $par (@{$self->{par}}) { |
2445 | for my $par (@{$self->{par}}) { |
2323 | my $h = $par->[0]; |
2446 | my $h = $par->[1]; |
2324 | |
2447 | |
2325 | if ($y0 < $y + $h && $y < $y1) { |
2448 | if ($y0 < $y + $h && $y < $y1) { |
2326 | $layout->set_foreground (@{ $par->[1] }); |
2449 | $layout->set_foreground (@{ $par->[2] }); |
2327 | $layout->set_width ($self->{w} - $par->[2]); |
2450 | $layout->set_width ($W - $par->[3]); |
2328 | $layout->set_markup ($par->[3]); |
2451 | $layout->set_markup ($par->[4]); |
2329 | |
2452 | |
2330 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
2453 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
2331 | |
2454 | |
2332 | glRasterPos $par->[2], $y - $y0; |
2455 | glRasterPos $par->[3], $y - $y0; |
2333 | glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; |
2456 | glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; |
2334 | } |
2457 | } |
2335 | |
2458 | |
2336 | $y += $h; |
2459 | $y += $h; |
2337 | } |
2460 | } |
… | |
… | |
2424 | |
2547 | |
2425 | # TODO: use animation |
2548 | # TODO: use animation |
2426 | if ($self->{state} = !$self->{state}) { |
2549 | if ($self->{state} = !$self->{state}) { |
2427 | $CFClient::UI::ROOT->add ($self->{other}); |
2550 | $CFClient::UI::ROOT->add ($self->{other}); |
2428 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
2551 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
2429 | $self->emit ("open"); |
2552 | $self->_emit ("open"); |
2430 | } else { |
2553 | } else { |
2431 | $CFClient::UI::ROOT->remove ($self->{other}); |
2554 | $CFClient::UI::ROOT->remove ($self->{other}); |
2432 | $self->emit ("close"); |
2555 | $self->_emit ("close"); |
2433 | } |
2556 | } |
2434 | |
2557 | |
2435 | $self->emit (changed => $self->{state}); |
2558 | $self->_emit (changed => $self->{state}); |
2436 | } |
2559 | } |
2437 | |
2560 | |
2438 | ############################################################################# |
2561 | ############################################################################# |
2439 | |
2562 | |
2440 | package CFClient::UI::Tooltip; |
2563 | package CFClient::UI::Tooltip; |
… | |
… | |
2516 | |
2639 | |
2517 | sub new { |
2640 | sub new { |
2518 | my $class = shift; |
2641 | my $class = shift; |
2519 | |
2642 | |
2520 | my $self = $class->SUPER::new ( |
2643 | my $self = $class->SUPER::new ( |
2521 | aspect => 1, |
2644 | aspect => 1, |
|
|
2645 | can_events => 0, |
2522 | @_, |
2646 | @_, |
2523 | ); |
2647 | ); |
2524 | |
2648 | |
2525 | if ($self->{anim} && $self->{animspeed}) { |
2649 | if ($self->{anim} && $self->{animspeed}) { |
2526 | Scalar::Util::weaken (my $widget = $self); |
2650 | Scalar::Util::weaken (my $widget = $self); |
… | |
… | |
2552 | } |
2676 | } |
2553 | |
2677 | |
2554 | sub _draw { |
2678 | sub _draw { |
2555 | my ($self) = @_; |
2679 | my ($self) = @_; |
2556 | |
2680 | |
2557 | return unless $::CONN;#d# manage and cache textures differently |
2681 | return unless $::CONN; |
2558 | |
2682 | |
2559 | my $face; |
2683 | my $face; |
2560 | |
2684 | |
2561 | if ($self->{frame}) { |
2685 | if ($self->{frame}) { |
2562 | my $anim = $::CONN->{anim}[$self->{anim}]; |
2686 | my $anim = $::CONN->{anim}[$self->{anim}]; |
… | |
… | |
2589 | |
2713 | |
2590 | package CFClient::UI::InventoryItem; |
2714 | package CFClient::UI::InventoryItem; |
2591 | |
2715 | |
2592 | our @ISA = CFClient::UI::HBox::; |
2716 | our @ISA = CFClient::UI::HBox::; |
2593 | |
2717 | |
2594 | sub _item_to_desc { |
|
|
2595 | my ($item) = @_; |
|
|
2596 | |
|
|
2597 | my $desc = |
|
|
2598 | $item->{nrof} < 2 |
|
|
2599 | ? $item->{name} |
|
|
2600 | : "$item->{nrof} × $item->{name_pl}"; |
|
|
2601 | |
|
|
2602 | $item->{flags} & Crossfire::Protocol::F_OPEN |
|
|
2603 | and $desc .= " (open)"; |
|
|
2604 | $item->{flags} & Crossfire::Protocol::F_APPLIED |
|
|
2605 | and $desc .= " (applied)"; |
|
|
2606 | $item->{flags} & Crossfire::Protocol::F_UNPAID |
|
|
2607 | and $desc .= " (unpaid)"; |
|
|
2608 | $item->{flags} & Crossfire::Protocol::F_MAGIC |
|
|
2609 | and $desc .= " (magic)"; |
|
|
2610 | $item->{flags} & Crossfire::Protocol::F_CURSED |
|
|
2611 | and $desc .= " (cursed)"; |
|
|
2612 | $item->{flags} & Crossfire::Protocol::F_DAMNED |
|
|
2613 | and $desc .= " (damned)"; |
|
|
2614 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2615 | and $desc .= " *"; |
|
|
2616 | |
|
|
2617 | $desc |
|
|
2618 | } |
|
|
2619 | |
|
|
2620 | sub new { |
2718 | sub new { |
2621 | my $class = shift; |
2719 | my $class = shift; |
2622 | |
2720 | |
2623 | my %args = @_; |
2721 | my %args = @_; |
2624 | |
2722 | |
2625 | my $item = delete $args{item}; |
2723 | my $item = delete $args{item}; |
2626 | |
2724 | |
2627 | my $desc = _item_to_desc ($item); |
2725 | my $desc = CFClient::Item::desc_string $item; |
2628 | |
2726 | |
2629 | my $self = $class->SUPER::new ( |
2727 | my $self = $class->SUPER::new ( |
2630 | can_hover => 1, |
2728 | can_hover => 1, |
2631 | can_events => 1, |
2729 | can_events => 1, |
2632 | tooltip => ((CFClient::UI::Label::escape $desc) |
2730 | tooltip => ((CFClient::UI::Label::escape $desc) |
2633 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
2731 | . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"), |
2634 | connect_button_down => sub { |
2732 | connect_button_down => sub { |
2635 | my ($self, $ev, $x, $y) = @_; |
2733 | my ($self, $ev, $x, $y) = @_; |
2636 | |
2734 | |
2637 | # todo: maybe put examine on 1? but should just be a tooltip :( |
2735 | # todo: maybe put examine on 1? but should just be a tooltip :( |
2638 | if ($ev->{button} == 1) { |
2736 | if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) { |
2639 | my $targ = $::CONN->{player}{tag}; |
2737 | my $targ = $::CONN->{player}{tag}; |
2640 | |
2738 | |
2641 | if ($item->{container} == $::CONN->{player}{tag}) { |
2739 | if ($item->{container} == $::CONN->{player}{tag}) { |
2642 | $targ = $main::OPENCONT; |
2740 | $targ = $main::OPENCONT; |
2643 | } |
2741 | } |
2644 | |
2742 | |
2645 | $::CONN->send ("move $targ $item->{tag} 0"); |
2743 | $::CONN->send ("move $targ $item->{tag} 0"); |
|
|
2744 | } elsif ($ev->{button} == 1) { |
|
|
2745 | $::CONN->send ("examine $item->{tag}"); |
2646 | } elsif ($ev->{button} == 2) { |
2746 | } elsif ($ev->{button} == 2) { |
2647 | $::CONN->send ("apply $item->{tag}"); |
2747 | $::CONN->send ("apply $item->{tag}"); |
2648 | } elsif ($ev->{button} == 3) { |
2748 | } elsif ($ev->{button} == 3) { |
2649 | my @menu_items = ( |
2749 | my @menu_items = ( |
2650 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
2750 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
… | |
… | |
2688 | } |
2788 | } |
2689 | |
2789 | |
2690 | sub update_item { |
2790 | sub update_item { |
2691 | my ($self) = @_; |
2791 | my ($self) = @_; |
2692 | |
2792 | |
2693 | my $desc = _item_to_desc ($self->{item}); |
2793 | my $desc = CFClient::Item::desc_string ($self->{item}); |
2694 | |
2794 | |
2695 | $self->{name_lbl}->set_text ($desc); |
2795 | $self->{name_lbl}->set_text ($desc); |
2696 | } |
2796 | } |
2697 | |
2797 | |
2698 | ############################################################################# |
2798 | ############################################################################# |
… | |
… | |
2703 | |
2803 | |
2704 | sub new { |
2804 | sub new { |
2705 | my $class = shift; |
2805 | my $class = shift; |
2706 | |
2806 | |
2707 | my $self = $class->SUPER::new ( |
2807 | my $self = $class->SUPER::new ( |
2708 | scrolled => (new CFClient::UI::VBox), |
2808 | scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]), |
2709 | @_, |
2809 | @_, |
2710 | ); |
2810 | ); |
2711 | |
2811 | |
2712 | $self |
2812 | $self |
2713 | } |
2813 | } |
… | |
… | |
2723 | or ($a->{name} cmp $b->{name}) |
2823 | or ($a->{name} cmp $b->{name}) |
2724 | } @$items; |
2824 | } @$items; |
2725 | |
2825 | |
2726 | $self->{real_items} = \@items; |
2826 | $self->{real_items} = \@items; |
2727 | |
2827 | |
|
|
2828 | my $row = 0; |
2728 | for my $item (@items) { |
2829 | for my $item (@items) { |
2729 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2830 | CFClient::Item::update_widgets $item; |
2730 | $item->update_item (); |
|
|
2731 | } |
|
|
2732 | |
2831 | |
2733 | $self->{scrolled}->add (@items); |
2832 | $self->{scrolled}->add (0, $row, $item->{face_widget}); |
|
|
2833 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
|
|
2834 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
|
|
2835 | |
|
|
2836 | $row++; |
|
|
2837 | } |
2734 | |
2838 | |
2735 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2839 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2736 | } |
2840 | } |
2737 | |
2841 | |
2738 | sub size_request { |
2842 | sub size_request { |
… | |
… | |
2780 | |
2884 | |
2781 | # popup given the event (must be a mouse button down event currently) |
2885 | # popup given the event (must be a mouse button down event currently) |
2782 | sub popup { |
2886 | sub popup { |
2783 | my ($self, $ev) = @_; |
2887 | my ($self, $ev) = @_; |
2784 | |
2888 | |
2785 | $self->emit ("popdown"); |
2889 | $self->_emit ("popdown"); |
2786 | |
2890 | |
2787 | # maybe save $GRAB? must be careful about events... |
2891 | # maybe save $GRAB? must be careful about events... |
2788 | $GRAB = $self; |
2892 | $GRAB = $self; |
2789 | $self->{button} = $ev->{button}; |
2893 | $self->{button} = $ev->{button}; |
2790 | |
2894 | |
… | |
… | |
2805 | |
2909 | |
2806 | if ($ev->{button} == $self->{button}) { |
2910 | if ($ev->{button} == $self->{button}) { |
2807 | undef $GRAB; |
2911 | undef $GRAB; |
2808 | $self->hide; |
2912 | $self->hide; |
2809 | |
2913 | |
2810 | $self->emit ("popdown"); |
2914 | $self->_emit ("popdown"); |
2811 | $self->{hover}[1]->() if $self->{hover}; |
2915 | $self->{hover}[1]->() if $self->{hover}; |
2812 | } |
2916 | } |
2813 | } |
2917 | } |
2814 | |
2918 | |
2815 | ############################################################################# |
2919 | ############################################################################# |
… | |
… | |
2877 | sub add { |
2981 | sub add { |
2878 | my ($self, $text, %arg) = @_; |
2982 | my ($self, $text, %arg) = @_; |
2879 | |
2983 | |
2880 | $text =~ s/^\s+//; |
2984 | $text =~ s/^\s+//; |
2881 | $text =~ s/\s+$//; |
2985 | $text =~ s/\s+$//; |
|
|
2986 | |
|
|
2987 | return unless $text; |
2882 | |
2988 | |
2883 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2989 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2884 | |
2990 | |
2885 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2991 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2886 | |
2992 | |
… | |
… | |
2957 | } |
3063 | } |
2958 | |
3064 | |
2959 | sub size_allocate { |
3065 | sub size_allocate { |
2960 | my ($self, $w, $h) = @_; |
3066 | my ($self, $w, $h) = @_; |
2961 | |
3067 | |
2962 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
|
|
2963 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
|
|
2964 | |
|
|
2965 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
|
|
2966 | if $old_w && $old_h && ($old_w != $w || $old_h != $h); |
|
|
2967 | |
|
|
2968 | for my $child ($self->children) { |
3068 | for my $child ($self->children) { |
2969 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
3069 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2970 | |
3070 | |
2971 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
3071 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
2972 | if exists $child->{req_x}; |
3072 | if exists $child->{req_x}; |
… | |
… | |
3001 | } |
3101 | } |
3002 | |
3102 | |
3003 | sub add { |
3103 | sub add { |
3004 | my ($self, @children) = @_; |
3104 | my ($self, @children) = @_; |
3005 | |
3105 | |
|
|
3106 | for (my @widgets = @children; my $w = pop @widgets; ) { |
|
|
3107 | push @widgets, $w->children; |
|
|
3108 | $w->{root} = $self; |
|
|
3109 | $w->{visible} = 1; |
|
|
3110 | } |
|
|
3111 | |
3006 | for my $child (@children) { |
3112 | for my $child (@children) { |
3007 | $child->{toplevel} = 1; |
3113 | $child->{is_toplevel} = 1; |
3008 | |
3114 | |
3009 | # integerise window positions |
3115 | # integerise window positions |
3010 | $child->{x} = int $child->{x}; |
3116 | $child->{x} = int $child->{x}; |
3011 | $child->{y} = int $child->{y}; |
3117 | $child->{y} = int $child->{y}; |
3012 | } |
3118 | } |
3013 | |
3119 | |
3014 | $self->SUPER::add (@children); |
3120 | $self->SUPER::add (@children); |
|
|
3121 | } |
|
|
3122 | |
|
|
3123 | sub remove { |
|
|
3124 | my ($self, @children) = @_; |
|
|
3125 | |
|
|
3126 | $self->SUPER::remove (@children); |
3015 | |
3127 | |
3016 | while (@children) { |
3128 | while (@children) { |
3017 | my $w = pop @children; |
3129 | my $w = pop @children; |
3018 | push @children, $w->children; |
3130 | push @children, $w->children; |
3019 | $w->{visible} = 1; |
3131 | $w->set_invisible; |
3020 | } |
|
|
3021 | } |
|
|
3022 | |
|
|
3023 | sub remove { |
|
|
3024 | my ($self, @children) = @_; |
|
|
3025 | |
|
|
3026 | $self->SUPER::remove (@children); |
|
|
3027 | |
|
|
3028 | while (@children) { |
|
|
3029 | my $w = pop @children; |
|
|
3030 | push @children, $w->children; |
|
|
3031 | delete $w->{visible}; |
|
|
3032 | } |
3132 | } |
3033 | } |
3133 | } |
3034 | |
3134 | |
3035 | sub on_refresh { |
3135 | sub on_refresh { |
3036 | my ($self, $id, $cb) = @_; |
3136 | my ($self, $id, $cb) = @_; |
… | |
… | |
3090 | $w = 0 if $w < 0; |
3190 | $w = 0 if $w < 0; |
3091 | $h = 0 if $h < 0; |
3191 | $h = 0 if $h < 0; |
3092 | |
3192 | |
3093 | $widget->{w} = $w; |
3193 | $widget->{w} = $w; |
3094 | $widget->{h} = $h; |
3194 | $widget->{h} = $h; |
3095 | $widget->size_allocate ($w, $h); |
|
|
3096 | $widget->emit (size_allocate => $w, $h); |
3195 | $widget->emit (size_allocate => $w, $h); |
3097 | } |
3196 | } |
3098 | } |
3197 | } |
3099 | |
3198 | |
3100 | while ($self->{post_alloc_hook}) { |
3199 | while ($self->{post_alloc_hook}) { |