… | |
… | |
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) = @_; |
… | |
… | |
136 | $rem += $_ - $i; |
130 | $rem += $_ - $i; |
137 | $_ = $i; |
131 | $_ = $i; |
138 | } |
132 | } |
139 | } |
133 | } |
140 | |
134 | |
|
|
135 | sub full_refresh { |
|
|
136 | # make a copy, otherwise for complains about freed values. |
|
|
137 | my @widgets = values %WIDGET; |
|
|
138 | |
|
|
139 | $_->update |
|
|
140 | for @widgets; |
|
|
141 | } |
|
|
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 | |
141 | # call when resolution changes etc. |
151 | # call when resolution changes etc. |
142 | sub rescale_widgets { |
152 | sub rescale_widgets { |
143 | my ($sx, $sy) = @_; |
153 | my ($sx, $sy) = @_; |
144 | |
154 | |
145 | for my $widget (values %WIDGET) { |
155 | for my $widget (values %WIDGET) { |
146 | if ($widget->{toplevel}) { |
156 | if ($widget->{is_toplevel}) { |
147 | $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}; |
148 | $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}; |
149 | $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}; |
150 | $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}; |
151 | $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}; |
152 | $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}; |
153 | $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}; |
154 | $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}; |
155 | } |
165 | } |
156 | |
|
|
157 | $widget->reconfigure; |
|
|
158 | } |
166 | } |
|
|
167 | |
|
|
168 | reconfigure_widgets; |
159 | } |
169 | } |
160 | |
170 | |
161 | ############################################################################# |
171 | ############################################################################# |
162 | |
172 | |
163 | package CFClient::UI::Base; |
173 | package CFClient::UI::Base; |
… | |
… | |
195 | %$self = (); |
205 | %$self = (); |
196 | } |
206 | } |
197 | |
207 | |
198 | sub show { |
208 | sub show { |
199 | my ($self) = @_; |
209 | my ($self) = @_; |
200 | |
|
|
201 | return if $self->{parent}; |
210 | return if $self->{parent}; |
202 | |
211 | |
203 | $CFClient::UI::ROOT->add ($self); |
212 | $CFClient::UI::ROOT->add ($self); |
204 | } |
213 | } |
205 | |
214 | |
206 | sub hide { |
215 | sub show_centered { |
207 | my ($self) = @_; |
216 | my ($self) = @_; |
|
|
217 | return if $self->{parent}; |
|
|
218 | |
|
|
219 | $self->show; |
|
|
220 | |
|
|
221 | $CFClient::UI::ROOT->on_post_alloc ( |
|
|
222 | "centered $self" => sub { |
|
|
223 | $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); |
|
|
224 | }, |
|
|
225 | ); |
|
|
226 | } |
|
|
227 | |
|
|
228 | sub set_invisible { |
|
|
229 | my ($self) = @_; |
|
|
230 | |
|
|
231 | # broken show/hide model |
|
|
232 | |
|
|
233 | delete $self->{visible}; |
208 | |
234 | |
209 | undef $GRAB if $GRAB == $self; |
235 | undef $GRAB if $GRAB == $self; |
210 | 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; |
211 | |
248 | |
212 | $self->{parent}->remove ($self) |
249 | $self->{parent}->remove ($self) |
213 | if $self->{parent}; |
250 | if $self->{parent}; |
214 | } |
251 | } |
215 | |
252 | |
… | |
… | |
282 | |
319 | |
283 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
320 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
284 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
321 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
285 | } |
322 | } |
286 | |
323 | |
|
|
324 | sub set_tooltip { |
|
|
325 | my ($self, $tooltip) = @_; |
|
|
326 | |
|
|
327 | $tooltip =~ s/^\s+//; |
|
|
328 | $tooltip =~ s/\s+$//; |
|
|
329 | |
|
|
330 | return if $self->{tooltip} eq $tooltip; |
|
|
331 | |
|
|
332 | $self->{tooltip} = $tooltip; |
|
|
333 | |
|
|
334 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
|
|
335 | delete $CFClient::UI::TOOLTIP->{owner}; |
|
|
336 | CFClient::UI::check_tooltip; |
|
|
337 | } |
|
|
338 | } |
|
|
339 | |
287 | # translate global coordinates to local coordinate system |
340 | # translate global coordinates to local coordinate system |
288 | sub coord2local { |
341 | sub coord2local { |
289 | my ($self, $x, $y) = @_; |
342 | my ($self, $x, $y) = @_; |
290 | |
343 | |
291 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
344 | $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) |
… | |
… | |
304 | return if $FOCUS == $self; |
357 | return if $FOCUS == $self; |
305 | return unless $self->{can_focus}; |
358 | return unless $self->{can_focus}; |
306 | |
359 | |
307 | my $focus = $FOCUS; $FOCUS = $self; |
360 | my $focus = $FOCUS; $FOCUS = $self; |
308 | |
361 | |
309 | $self->emit (focus_in => $focus); |
362 | $self->_emit (focus_in => $focus); |
310 | |
363 | |
311 | $focus->update if $focus; |
364 | $focus->update if $focus; |
312 | $FOCUS->update; |
365 | $FOCUS->update; |
313 | } |
366 | } |
314 | |
367 | |
… | |
… | |
317 | |
370 | |
318 | return unless $FOCUS == $self; |
371 | return unless $FOCUS == $self; |
319 | |
372 | |
320 | my $focus = $FOCUS; undef $FOCUS; |
373 | my $focus = $FOCUS; undef $FOCUS; |
321 | |
374 | |
322 | $self->emit (focus_out => $focus); |
375 | $self->_emit (focus_out => $focus); |
323 | |
376 | |
324 | $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; |
325 | } |
381 | } |
326 | |
382 | |
327 | sub mouse_motion { } |
383 | sub mouse_motion { } |
328 | sub button_up { } |
384 | sub button_up { } |
329 | sub key_down { } |
385 | sub key_down { } |
330 | sub key_up { } |
386 | sub key_up { } |
331 | |
387 | |
332 | sub button_down { |
388 | sub button_down { |
333 | my ($self, $ev, $x, $y) = @_; |
389 | my ($self, $ev, $x, $y) = @_; |
334 | |
390 | |
335 | $self->focus_in; |
391 | $self->focus_in; |
… | |
… | |
369 | if ($ENV{PCLIENT_DEBUG}) { |
425 | if ($ENV{PCLIENT_DEBUG}) { |
370 | glPushMatrix; |
426 | glPushMatrix; |
371 | glColor 1, 1, 0, 1; |
427 | glColor 1, 1, 0, 1; |
372 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
428 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
373 | glBegin GL_LINE_LOOP; |
429 | glBegin GL_LINE_LOOP; |
374 | glVertex 0 , 0; |
430 | glVertex 0 , 0; |
375 | glVertex $self->{w}, 0; |
431 | glVertex $self->{w} - 1, 0; |
376 | glVertex $self->{w}, $self->{h}; |
432 | glVertex $self->{w} - 1, $self->{h} - 1; |
377 | glVertex 0 , $self->{h}; |
433 | glVertex 0 , $self->{h} - 1; |
378 | glEnd; |
434 | glEnd; |
379 | glPopMatrix; |
435 | glPopMatrix; |
380 | #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; |
381 | } |
437 | } |
382 | } |
438 | } |
… | |
… | |
402 | sub set_parent { |
458 | sub set_parent { |
403 | my ($self, $parent) = @_; |
459 | my ($self, $parent) = @_; |
404 | |
460 | |
405 | Scalar::Util::weaken ($self->{parent} = $parent); |
461 | Scalar::Util::weaken ($self->{parent} = $parent); |
406 | |
462 | |
|
|
463 | $self->{root} = $parent->{root}; |
|
|
464 | $self->{visible} = $parent->{visible}; |
|
|
465 | |
407 | # TODO: req_w _does_change after ->reconfigure |
466 | # TODO: req_w _does_change after ->reconfigure |
408 | $self->check_size |
467 | $self->check_size |
409 | unless exists $self->{req_w}; |
468 | unless exists $self->{req_w}; |
|
|
469 | |
|
|
470 | $self->show; |
410 | } |
471 | } |
411 | |
472 | |
412 | sub check_size { |
473 | sub check_size { |
413 | my ($self, $forced) = @_; |
474 | my ($self, $forced) = @_; |
414 | |
475 | |
… | |
… | |
427 | my ($self, $signal, $cb) = @_; |
488 | my ($self, $signal, $cb) = @_; |
428 | |
489 | |
429 | push @{ $self->{signal_cb}{$signal} }, $cb; |
490 | push @{ $self->{signal_cb}{$signal} }, $cb; |
430 | } |
491 | } |
431 | |
492 | |
|
|
493 | sub _emit { |
|
|
494 | my ($self, $signal, @args) = @_; |
|
|
495 | |
|
|
496 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
|
|
497 | } |
|
|
498 | |
432 | sub emit { |
499 | sub emit { |
433 | my ($self, $signal, @args) = @_; |
500 | my ($self, $signal, @args) = @_; |
434 | |
501 | |
435 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
502 | $self->_emit ($signal, @args) |
|
|
503 | || $self->$signal (@args); |
436 | } |
504 | } |
437 | |
505 | |
438 | sub DESTROY { |
506 | sub DESTROY { |
439 | my ($self) = @_; |
507 | my ($self) = @_; |
440 | |
508 | |
… | |
… | |
455 | my $class = shift; |
523 | my $class = shift; |
456 | |
524 | |
457 | # range [value, low, high, page] |
525 | # range [value, low, high, page] |
458 | |
526 | |
459 | $class->SUPER::new ( |
527 | $class->SUPER::new ( |
460 | bg => [0, 0, 0, 0.2], |
528 | #bg => [0, 0, 0, 0.2], |
461 | active_bg => [1, 1, 1, 0.5], |
529 | #active_bg => [1, 1, 1, 0.5], |
462 | @_ |
530 | @_ |
463 | ) |
531 | ) |
464 | } |
532 | } |
465 | |
533 | |
466 | sub _draw { |
534 | sub _draw { |
467 | my ($self) = @_; |
535 | my ($self) = @_; |
468 | |
536 | |
|
|
537 | my $color = $FOCUS == $self && $self->{active_bg} |
|
|
538 | ? $self->{active_bg} |
|
|
539 | : $self->{bg}; |
|
|
540 | |
|
|
541 | if ($color && (@$color < 4 || $color->[3])) { |
469 | my ($w, $h) = @$self{qw(w h)}; |
542 | my ($w, $h) = @$self{qw(w h)}; |
470 | |
543 | |
471 | glEnable GL_BLEND; |
544 | glEnable GL_BLEND; |
472 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
545 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
473 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
546 | glColor @$color; |
474 | |
547 | |
475 | glBegin GL_QUADS; |
548 | glBegin GL_QUADS; |
476 | glVertex 0 , 0; |
549 | glVertex 0 , 0; |
477 | glVertex 0 , $h; |
550 | glVertex 0 , $h; |
478 | glVertex $w, $h; |
551 | glVertex $w, $h; |
479 | glVertex $w, 0; |
552 | glVertex $w, 0; |
480 | glEnd; |
553 | glEnd; |
481 | |
554 | |
482 | glDisable GL_BLEND; |
555 | glDisable GL_BLEND; |
|
|
556 | } |
483 | } |
557 | } |
484 | |
558 | |
485 | ############################################################################# |
559 | ############################################################################# |
486 | |
560 | |
487 | package CFClient::UI::Empty; |
561 | package CFClient::UI::Empty; |
… | |
… | |
547 | delete $child->{parent}; |
621 | delete $child->{parent}; |
548 | $child->hide; |
622 | $child->hide; |
549 | |
623 | |
550 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
624 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
551 | |
625 | |
552 | $self->check_size; |
626 | $self->check_size (1); |
553 | $self->update; |
627 | $self->update; |
554 | } |
628 | } |
555 | |
629 | |
556 | sub clear { |
630 | sub clear { |
557 | my ($self) = @_; |
631 | my ($self) = @_; |
… | |
… | |
671 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
745 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
672 | glClearColor 0, 0, 0, 0; |
746 | glClearColor 0, 0, 0, 0; |
673 | glClear GL_COLOR_BUFFER_BIT; |
747 | glClear GL_COLOR_BUFFER_BIT; |
674 | |
748 | |
675 | $self->_render; |
749 | $self->_render; |
676 | # glColorMask 1, 1, 1, 0; |
|
|
677 | # glEnable GL_BLEND; |
|
|
678 | # glBlendFunc GL_SRC_ALPHA, GL_ZERO; |
|
|
679 | # glRasterPos 0, 0; |
|
|
680 | # glCopyPixels 0, 0, $self->{w}, $self->{h}; |
|
|
681 | # glDisable GL_BLEND; |
|
|
682 | # glColorMask 1, 1, 1, 1; |
|
|
683 | }; |
750 | }; |
684 | } |
751 | } |
685 | |
752 | |
686 | sub _draw { |
753 | sub _draw { |
687 | my ($self) = @_; |
754 | my ($self) = @_; |
… | |
… | |
691 | my $tex = $self->{texture} |
758 | my $tex = $self->{texture} |
692 | or return; |
759 | or return; |
693 | |
760 | |
694 | glEnable GL_TEXTURE_2D; |
761 | glEnable GL_TEXTURE_2D; |
695 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
762 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
696 | glColor 0, 0, 0, 1; |
763 | glColor 1, 1, 1, 1; |
697 | |
764 | |
698 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
765 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
699 | |
766 | |
700 | glDisable GL_TEXTURE_2D; |
767 | glDisable GL_TEXTURE_2D; |
701 | } |
768 | } |
… | |
… | |
704 | |
771 | |
705 | package CFClient::UI::ViewPort; |
772 | package CFClient::UI::ViewPort; |
706 | |
773 | |
707 | our @ISA = CFClient::UI::Window::; |
774 | our @ISA = CFClient::UI::Window::; |
708 | |
775 | |
|
|
776 | sub new { |
|
|
777 | my $class = shift; |
|
|
778 | |
|
|
779 | $class->SUPER::new ( |
|
|
780 | scroll_x => 0, |
|
|
781 | scroll_y => 1, |
|
|
782 | @_, |
|
|
783 | ) |
|
|
784 | } |
|
|
785 | |
709 | sub size_request { |
786 | sub size_request { |
710 | my ($self) = @_; |
787 | my ($self) = @_; |
711 | |
788 | |
712 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
789 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
713 | $self->child->configure (0, 0, @$self{qw(child_w child_h)}); |
|
|
714 | |
790 | |
715 | @$self{qw(child_w child_h)} |
791 | @$self{qw(child_w child_h)} |
716 | } |
792 | } |
717 | |
793 | |
718 | sub size_allocate { |
794 | sub size_allocate { |
719 | my ($self, $w, $h) = @_; |
795 | my ($self, $w, $h) = @_; |
720 | |
796 | |
|
|
797 | $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; |
|
|
798 | $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; |
|
|
799 | |
|
|
800 | $self->child->configure (0, 0, $w, $h); |
721 | $self->update; |
801 | $self->update; |
722 | } |
802 | } |
723 | |
803 | |
724 | sub set_offset { |
804 | sub set_offset { |
725 | my ($self, $x, $y) = @_; |
805 | my ($self, $x, $y) = @_; |
… | |
… | |
779 | |
859 | |
780 | my $slider = new CFClient::UI::Slider |
860 | my $slider = new CFClient::UI::Slider |
781 | vertical => 1, |
861 | vertical => 1, |
782 | range => [0, 0, 1, 0.01], # HACK fix |
862 | range => [0, 0, 1, 0.01], # HACK fix |
783 | connect_changed => sub { |
863 | connect_changed => sub { |
784 | $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); |
864 | $self->{vp}->set_offset (0, $_[1]); |
785 | }, |
865 | }, |
786 | ; |
866 | ; |
787 | |
867 | |
788 | $self = $class->SUPER::new ( |
868 | $self = $class->SUPER::new ( |
789 | vp => (new CFClient::UI::ViewPort), |
869 | vp => (new CFClient::UI::ViewPort expand => 1), |
790 | slider => $slider, |
870 | slider => $slider, |
791 | @_, |
871 | @_, |
792 | ); |
872 | ); |
793 | |
873 | |
794 | $self->{vp}->add ($self->{scrolled}); |
874 | $self->{vp}->add ($self->{scrolled}); |
795 | $self->add ($self->{vp}); |
875 | $self->add ($self->{vp}); |
796 | $self->add ($self->{slider}); |
876 | $self->add ($self->{slider}); |
797 | |
877 | |
798 | $self |
878 | $self |
|
|
879 | } |
|
|
880 | |
|
|
881 | sub update { |
|
|
882 | my ($self) = @_; |
|
|
883 | |
|
|
884 | $self->SUPER::update; |
|
|
885 | |
|
|
886 | # todo: overwrite size_allocate of child |
|
|
887 | my $child = $self->{vp}->child; |
|
|
888 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
|
|
889 | } |
|
|
890 | |
|
|
891 | sub size_allocate { |
|
|
892 | my ($self, $w, $h) = @_; |
|
|
893 | |
|
|
894 | $self->SUPER::size_allocate ($w, $h); |
|
|
895 | |
|
|
896 | my $child = $self->{vp}->child; |
|
|
897 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
799 | } |
898 | } |
800 | |
899 | |
801 | #TODO# update range on size_allocate depending on child |
900 | #TODO# update range on size_allocate depending on child |
802 | # update viewport offset on scroll |
901 | # update viewport offset on scroll |
803 | |
902 | |
… | |
… | |
857 | my $class = shift; |
956 | my $class = shift; |
858 | |
957 | |
859 | # TODO: user_x, user_y, overwrite moveto? |
958 | # TODO: user_x, user_y, overwrite moveto? |
860 | |
959 | |
861 | my $self = $class->SUPER::new ( |
960 | my $self = $class->SUPER::new ( |
862 | bg => [1, 1, 1, 1], |
961 | bg => [1, 1, 1, 1], |
863 | border_bg => [1, 1, 1, 1], |
962 | border_bg => [1, 1, 1, 1], |
864 | border => 0.6, |
963 | border => 0.6, |
865 | toplevel => 1, |
964 | is_toplevel => 1, |
866 | can_events => 1, |
965 | can_events => 1, |
867 | @_ |
966 | @_ |
868 | ); |
967 | ); |
869 | |
968 | |
870 | $self->{title} &&= new CFClient::UI::Label |
969 | $self->{title} &&= new CFClient::UI::Label |
871 | align => 0, |
970 | align => 0, |
… | |
… | |
895 | my ($self, $w, $h) = @_; |
994 | my ($self, $w, $h) = @_; |
896 | |
995 | |
897 | $h -= List::Util::max 0, $self->border * 2; |
996 | $h -= List::Util::max 0, $self->border * 2; |
898 | $w -= List::Util::max 0, $self->border * 2; |
997 | $w -= List::Util::max 0, $self->border * 2; |
899 | |
998 | |
900 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
999 | $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) |
901 | if $self->{title}; |
1000 | if $self->{title}; |
902 | |
1001 | |
903 | $self->child->configure ($self->border, $self->border, $w, $h); |
1002 | $self->child->configure ($self->border, $self->border, $w, $h); |
904 | } |
1003 | } |
905 | |
1004 | |
… | |
… | |
1011 | sub new { |
1110 | sub new { |
1012 | my $class = shift; |
1111 | my $class = shift; |
1013 | |
1112 | |
1014 | $class->SUPER::new ( |
1113 | $class->SUPER::new ( |
1015 | col_expand => [], |
1114 | col_expand => [], |
1016 | @_ |
1115 | @_, |
1017 | ) |
1116 | ) |
|
|
1117 | } |
|
|
1118 | |
|
|
1119 | sub children { |
|
|
1120 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
1018 | } |
1121 | } |
1019 | |
1122 | |
1020 | sub add { |
1123 | sub add { |
1021 | my ($self, $x, $y, $child) = @_; |
1124 | my ($self, $x, $y, $child) = @_; |
1022 | |
1125 | |
1023 | $child->set_parent ($self); |
1126 | $child->set_parent ($self); |
1024 | $self->{children}[$y][$x] = $child; |
1127 | $self->{children}[$y][$x] = $child; |
1025 | |
1128 | |
1026 | $child->check_size; |
1129 | $self->check_size (1); |
1027 | } |
1130 | } |
1028 | |
1131 | |
1029 | sub children { |
|
|
1030 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1031 | } |
|
|
1032 | |
|
|
1033 | # TODO: move to container class maybe? send childs a signal on removal? |
1132 | # TODO: move to container class maybe? send children a signal on removal? |
1034 | sub clear { |
1133 | sub clear { |
1035 | my ($self) = @_; |
1134 | my ($self) = @_; |
1036 | |
1135 | |
1037 | my @children = $self->children; |
1136 | my @children = $self->children; |
1038 | delete $self->{children}; |
1137 | delete $self->{children}; |
… | |
… | |
1040 | for (@children) { |
1139 | for (@children) { |
1041 | delete $_->{parent}; |
1140 | delete $_->{parent}; |
1042 | $_->hide; |
1141 | $_->hide; |
1043 | } |
1142 | } |
1044 | |
1143 | |
|
|
1144 | $self->check_size (1); |
1045 | $self->update; |
1145 | $self->update; |
1046 | } |
1146 | } |
1047 | |
1147 | |
1048 | sub get_wh { |
1148 | sub get_wh { |
1049 | my ($self) = @_; |
1149 | my ($self) = @_; |
… | |
… | |
1081 | sub size_allocate { |
1181 | sub size_allocate { |
1082 | my ($self, $w, $h) = @_; |
1182 | my ($self, $w, $h) = @_; |
1083 | |
1183 | |
1084 | my ($ws, $hs) = $self->get_wh; |
1184 | my ($ws, $hs) = $self->get_wh; |
1085 | |
1185 | |
1086 | my $req_w = sum @$ws; |
1186 | my $req_w = (sum @$ws) || 1; |
1087 | my $req_h = sum @$hs; |
1187 | my $req_h = (sum @$hs) || 1; |
1088 | |
1188 | |
1089 | # TODO: nicer code && do row_expand |
1189 | # TODO: nicer code && do row_expand |
1090 | my @col_expand = @{$self->{col_expand}}; |
1190 | my @col_expand = @{$self->{col_expand}}; |
1091 | @col_expand = (1) x @$ws unless @col_expand; |
1191 | @col_expand = (1) x @$ws unless @col_expand; |
1092 | my $col_expand = (sum @col_expand) || 1; |
1192 | my $col_expand = (sum @col_expand) || 1; |
… | |
… | |
1266 | |
1366 | |
1267 | ############################################################################# |
1367 | ############################################################################# |
1268 | |
1368 | |
1269 | package CFClient::UI::Label; |
1369 | package CFClient::UI::Label; |
1270 | |
1370 | |
1271 | our @ISA = CFClient::UI::Base::; |
1371 | our @ISA = CFClient::UI::DrawBG::; |
1272 | |
1372 | |
1273 | use CFClient::OpenGL; |
1373 | use CFClient::OpenGL; |
1274 | |
1374 | |
1275 | sub new { |
1375 | sub new { |
1276 | my ($class, %arg) = @_; |
1376 | my ($class, %arg) = @_; |
1277 | |
1377 | |
1278 | my $self = $class->SUPER::new ( |
1378 | my $self = $class->SUPER::new ( |
1279 | fg => [1, 1, 1], |
1379 | fg => [1, 1, 1], |
|
|
1380 | #bg => none |
|
|
1381 | #active_bg => none |
1280 | #font => default_font |
1382 | #font => default_font |
1281 | #text => initial text |
1383 | #text => initial text |
1282 | #markup => initial narkup |
1384 | #markup => initial narkup |
|
|
1385 | #max_w => maximum pixel width |
|
|
1386 | ellipsise => 3, # end |
1283 | layout => (new CFClient::Layout), |
1387 | layout => (new CFClient::Layout), |
1284 | fontsize => 1, |
1388 | fontsize => 1, |
1285 | align => -1, |
1389 | align => -1, |
1286 | valign => -1, |
1390 | valign => -1, |
1287 | padding => 2, |
1391 | padding => 2, |
… | |
… | |
1302 | } |
1406 | } |
1303 | |
1407 | |
1304 | $self |
1408 | $self |
1305 | } |
1409 | } |
1306 | |
1410 | |
1307 | sub escape { |
1411 | sub escape($) { |
1308 | local $_ = $_[1]; |
1412 | local $_ = $_[0]; |
1309 | |
1413 | |
1310 | s/&/&/g; |
1414 | s/&/&/g; |
1311 | s/>/>/g; |
1415 | s/>/>/g; |
1312 | s/</</g; |
1416 | s/</</g; |
1313 | |
1417 | |
1314 | $_[1] |
1418 | $_ |
1315 | } |
1419 | } |
1316 | |
1420 | |
1317 | sub update { |
1421 | sub update { |
1318 | my ($self) = @_; |
1422 | my ($self) = @_; |
1319 | |
1423 | |
… | |
… | |
1352 | sub size_request { |
1456 | sub size_request { |
1353 | my ($self) = @_; |
1457 | my ($self) = @_; |
1354 | |
1458 | |
1355 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1459 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1356 | $self->{layout}->set_width ($self->{max_w} || -1); |
1460 | $self->{layout}->set_width ($self->{max_w} || -1); |
|
|
1461 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
|
|
1462 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1357 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1463 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1358 | |
1464 | |
1359 | my ($w, $h) = $self->{layout}->size; |
1465 | my ($w, $h) = $self->{layout}->size; |
1360 | |
1466 | |
1361 | if (exists $self->{template}) { |
1467 | if (exists $self->{template}) { |
… | |
… | |
1391 | } |
1497 | } |
1392 | |
1498 | |
1393 | sub _draw { |
1499 | sub _draw { |
1394 | my ($self) = @_; |
1500 | my ($self) = @_; |
1395 | |
1501 | |
|
|
1502 | $self->SUPER::_draw; # draw background, if applicable |
|
|
1503 | |
1396 | my $tex = $self->{texture} ||= do { |
1504 | my $tex = $self->{texture} ||= do { |
1397 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1505 | $self->{layout}->set_foreground (@{$self->{fg}}); |
1398 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1506 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
1399 | $self->{layout}->set_width ($self->{w}); |
1507 | $self->{layout}->set_width ($self->{w}); |
|
|
1508 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
|
|
1509 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1400 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
1510 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1401 | |
1511 | |
1402 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1512 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1403 | |
1513 | |
1404 | $self->{ox} = int $self->{align} < 0 ? $self->{padding} |
1514 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding} |
1405 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1515 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1406 | : ($self->{w} - $tex->{w}) * 0.5; |
1516 | : ($self->{w} - $tex->{w}) * 0.5); |
1407 | |
1517 | |
1408 | $self->{oy} = int $self->{valign} < 0 ? $self->{padding} |
1518 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} |
1409 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1519 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1410 | : ($self->{h} - $tex->{h}) * 0.5; |
1520 | : ($self->{h} - $tex->{h}) * 0.5); |
1411 | |
1521 | |
1412 | $tex |
1522 | $tex |
1413 | }; |
1523 | }; |
1414 | |
1524 | |
1415 | glEnable GL_TEXTURE_2D; |
1525 | glEnable GL_TEXTURE_2D; |
… | |
… | |
1443 | active_fg => [0, 0, 0], |
1553 | active_fg => [0, 0, 0], |
1444 | can_hover => 1, |
1554 | can_hover => 1, |
1445 | can_focus => 1, |
1555 | can_focus => 1, |
1446 | valign => 0, |
1556 | valign => 0, |
1447 | can_events => 1, |
1557 | can_events => 1, |
|
|
1558 | #text => ... |
1448 | @_ |
1559 | @_ |
1449 | ) |
1560 | ) |
1450 | } |
1561 | } |
1451 | |
1562 | |
1452 | sub _set_text { |
1563 | sub _set_text { |
… | |
… | |
1462 | $self->{text} = $text; |
1573 | $self->{text} = $text; |
1463 | |
1574 | |
1464 | $text =~ s/./*/g if $self->{hidden}; |
1575 | $text =~ s/./*/g if $self->{hidden}; |
1465 | $self->{layout}->set_text ("$text "); |
1576 | $self->{layout}->set_text ("$text "); |
1466 | |
1577 | |
1467 | $self->emit (changed => $self->{text}); |
1578 | $self->_emit (changed => $self->{text}); |
1468 | } |
1579 | } |
1469 | |
1580 | |
1470 | sub set_text { |
1581 | sub set_text { |
1471 | my ($self, $text) = @_; |
1582 | my ($self, $text) = @_; |
1472 | |
1583 | |
1473 | $self->{cursor} = length $text; |
1584 | $self->{cursor} = length $text; |
1474 | $self->_set_text ($text); |
1585 | $self->_set_text ($text); |
|
|
1586 | $self->update; |
1475 | $self->check_size; |
1587 | $self->check_size; |
1476 | $self->update; |
|
|
1477 | } |
1588 | } |
1478 | |
1589 | |
1479 | sub get_text { |
1590 | sub get_text { |
1480 | $_[0]{text} |
1591 | $_[0]{text} |
1481 | } |
1592 | } |
… | |
… | |
1484 | my ($self) = @_; |
1595 | my ($self) = @_; |
1485 | |
1596 | |
1486 | my ($w, $h) = $self->SUPER::size_request; |
1597 | my ($w, $h) = $self->SUPER::size_request; |
1487 | |
1598 | |
1488 | ($w + 1, $h) # add 1 for cursor |
1599 | ($w + 1, $h) # add 1 for cursor |
1489 | } |
|
|
1490 | |
|
|
1491 | sub size_allocate { |
|
|
1492 | my ($self, $w, $h) = @_; |
|
|
1493 | |
|
|
1494 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
|
|
1495 | } |
1600 | } |
1496 | |
1601 | |
1497 | sub key_down { |
1602 | sub key_down { |
1498 | my ($self, $ev) = @_; |
1603 | my ($self, $ev) = @_; |
1499 | |
1604 | |
… | |
… | |
1514 | } elsif ($sym == CFClient::SDLK_HOME) { |
1619 | } elsif ($sym == CFClient::SDLK_HOME) { |
1515 | $self->{cursor} = 0; |
1620 | $self->{cursor} = 0; |
1516 | } elsif ($sym == CFClient::SDLK_END) { |
1621 | } elsif ($sym == CFClient::SDLK_END) { |
1517 | $self->{cursor} = length $text; |
1622 | $self->{cursor} = length $text; |
1518 | } elsif ($uni == 27) { |
1623 | } elsif ($uni == 27) { |
1519 | $self->emit ('escape'); |
1624 | $self->_emit ('escape'); |
1520 | } elsif ($uni) { |
1625 | } elsif ($uni) { |
1521 | substr $text, $self->{cursor}++, 0, chr $uni; |
1626 | substr $text, $self->{cursor}++, 0, chr $uni; |
1522 | } |
1627 | } |
1523 | |
1628 | |
1524 | $self->_set_text ($text); |
1629 | $self->_set_text ($text); |
1525 | $self->update; |
1630 | $self->update; |
|
|
1631 | $self->check_size; |
1526 | } |
1632 | } |
1527 | |
1633 | |
1528 | sub focus_in { |
1634 | sub focus_in { |
1529 | my ($self) = @_; |
1635 | my ($self) = @_; |
1530 | |
1636 | |
… | |
… | |
1610 | if ($sym == 13) { |
1716 | if ($sym == 13) { |
1611 | unshift @{$self->{history}}, |
1717 | unshift @{$self->{history}}, |
1612 | my $txt = $self->get_text; |
1718 | my $txt = $self->get_text; |
1613 | $self->{history_pointer} = -1; |
1719 | $self->{history_pointer} = -1; |
1614 | $self->{history_saveback} = ''; |
1720 | $self->{history_saveback} = ''; |
1615 | $self->emit (activate => $txt); |
1721 | $self->_emit (activate => $txt); |
1616 | $self->update; |
1722 | $self->update; |
1617 | |
1723 | |
1618 | } elsif ($sym == CFClient::SDLK_UP) { |
1724 | } elsif ($sym == CFClient::SDLK_UP) { |
1619 | if ($self->{history_pointer} < 0) { |
1725 | if ($self->{history_pointer} < 0) { |
1620 | $self->{history_saveback} = $self->get_text; |
1726 | $self->{history_saveback} = $self->get_text; |
… | |
… | |
1659 | my $class = shift; |
1765 | my $class = shift; |
1660 | |
1766 | |
1661 | $class->SUPER::new ( |
1767 | $class->SUPER::new ( |
1662 | padding => 4, |
1768 | padding => 4, |
1663 | fg => [1, 1, 1], |
1769 | fg => [1, 1, 1], |
1664 | bg => [1, 1, 1, 0.2], |
|
|
1665 | active_fg => [0, 0, 1], |
1770 | active_fg => [0, 0, 1], |
1666 | can_hover => 1, |
1771 | can_hover => 1, |
1667 | align => 0, |
1772 | align => 0, |
1668 | valign => 0, |
1773 | valign => 0, |
1669 | can_events => 1, |
1774 | can_events => 1, |
1670 | @_ |
1775 | @_ |
1671 | ) |
1776 | ) |
1672 | } |
1777 | } |
1673 | |
1778 | |
|
|
1779 | sub activate { } |
|
|
1780 | |
1674 | sub button_up { |
1781 | sub button_up { |
1675 | my ($self, $ev, $x, $y) = @_; |
1782 | my ($self, $ev, $x, $y) = @_; |
1676 | |
1783 | |
|
|
1784 | $self->emit ("activate") |
1677 | if ($x >= 0 && $x < $self->{w} |
1785 | if $x >= 0 && $x < $self->{w} |
1678 | && $y >= 0 && $y < $self->{h}) { |
1786 | && $y >= 0 && $y < $self->{h}; |
1679 | $self->emit ("activate"); |
|
|
1680 | } |
|
|
1681 | } |
1787 | } |
1682 | |
1788 | |
1683 | sub _draw { |
1789 | sub _draw { |
1684 | my ($self) = @_; |
1790 | my ($self) = @_; |
1685 | |
1791 | |
… | |
… | |
1717 | |
1823 | |
1718 | $class->SUPER::new ( |
1824 | $class->SUPER::new ( |
1719 | padding => 2, |
1825 | padding => 2, |
1720 | fg => [1, 1, 1], |
1826 | fg => [1, 1, 1], |
1721 | active_fg => [1, 1, 0], |
1827 | active_fg => [1, 1, 0], |
|
|
1828 | bg => [0, 0, 0, 0.2], |
|
|
1829 | active_bg => [1, 1, 1, 0.5], |
1722 | state => 0, |
1830 | state => 0, |
1723 | can_hover => 1, |
1831 | can_hover => 1, |
1724 | @_ |
1832 | @_ |
1725 | ) |
1833 | ) |
1726 | } |
1834 | } |
… | |
… | |
1735 | my ($self, $ev, $x, $y) = @_; |
1843 | my ($self, $ev, $x, $y) = @_; |
1736 | |
1844 | |
1737 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1845 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1738 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1846 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1739 | $self->{state} = !$self->{state}; |
1847 | $self->{state} = !$self->{state}; |
1740 | $self->emit (changed => $self->{state}); |
1848 | $self->_emit (changed => $self->{state}); |
1741 | } |
1849 | } |
1742 | } |
1850 | } |
1743 | |
1851 | |
1744 | sub _draw { |
1852 | sub _draw { |
1745 | my ($self) = @_; |
1853 | my ($self) = @_; |
… | |
… | |
2018 | # TODO: req_w/h are wrong with vertical |
2126 | # TODO: req_w/h are wrong with vertical |
2019 | # TODO: calculations are off |
2127 | # TODO: calculations are off |
2020 | my $self = $class->SUPER::new ( |
2128 | my $self = $class->SUPER::new ( |
2021 | fg => [1, 1, 1], |
2129 | fg => [1, 1, 1], |
2022 | active_fg => [0, 0, 0], |
2130 | active_fg => [0, 0, 0], |
|
|
2131 | bg => [0, 0, 0, 0.2], |
|
|
2132 | active_bg => [1, 1, 1, 0.5], |
2023 | range => [0, 0, 100, 10], |
2133 | range => [0, 0, 100, 10, 0], |
2024 | req_w => $::WIDTH / 80, |
2134 | req_w => $::WIDTH / 80, |
2025 | req_h => $::WIDTH / 80, |
2135 | req_h => $::WIDTH / 80, |
2026 | vertical => 0, |
2136 | vertical => 0, |
2027 | can_hover => 1, |
2137 | can_hover => 1, |
2028 | inner_pad => .05, |
2138 | inner_pad => 0.02, |
2029 | @_ |
2139 | @_ |
2030 | ); |
2140 | ); |
2031 | |
2141 | |
2032 | $self->set_value ($self->{range}[0]); |
2142 | $self->set_value ($self->{range}[0]); |
2033 | $self->update; |
2143 | $self->update; |
2034 | |
2144 | |
2035 | $self |
2145 | $self |
2036 | } |
2146 | } |
2037 | |
2147 | |
|
|
2148 | sub set_range { |
|
|
2149 | my ($self, $range) = @_; |
|
|
2150 | |
|
|
2151 | ($range, $self->{range}) = ($self->{range}, $range); |
|
|
2152 | |
|
|
2153 | $self->update |
|
|
2154 | if "@$range" ne "@{$self->{range}}"; |
|
|
2155 | } |
|
|
2156 | |
2038 | sub set_value { |
2157 | sub set_value { |
2039 | my ($self, $value) = @_; |
2158 | my ($self, $value) = @_; |
2040 | |
2159 | |
2041 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
2160 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
2042 | |
2161 | |
2043 | $hi = $lo + 1 if $hi <= $lo; |
2162 | $hi = $lo + 1 if $hi <= $lo; |
2044 | |
2163 | |
|
|
2164 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2165 | |
2045 | $value = $lo if $value < $lo; |
2166 | $value = $lo if $value < $lo; |
2046 | $value = $hi if $value > $hi; |
2167 | $value = $hi - $page if $value > $hi - $page; |
2047 | |
2168 | |
2048 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
2169 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
2049 | if $unit; |
2170 | if $unit; |
2050 | |
2171 | |
2051 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2052 | |
|
|
2053 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
2172 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
2054 | |
2173 | |
2055 | if ($value != $old_value) { |
2174 | if ($value != $old_value) { |
2056 | $self->emit (changed => $value); |
2175 | $self->_emit (changed => $value); |
2057 | $self->update; |
2176 | $self->update; |
2058 | } |
2177 | } |
2059 | } |
2178 | } |
2060 | |
2179 | |
2061 | sub size_request { |
2180 | sub size_request { |
… | |
… | |
2069 | |
2188 | |
2070 | sub button_down { |
2189 | sub button_down { |
2071 | my ($self, $ev, $x, $y) = @_; |
2190 | my ($self, $ev, $x, $y) = @_; |
2072 | |
2191 | |
2073 | $self->SUPER::button_down ($ev, $x, $y); |
2192 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
2193 | |
|
|
2194 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
|
|
2195 | |
2074 | $self->mouse_motion ($ev, $x, $y); |
2196 | $self->mouse_motion ($ev, $x, $y); |
2075 | } |
2197 | } |
2076 | |
2198 | |
2077 | sub mouse_motion { |
2199 | sub mouse_motion { |
2078 | my ($self, $ev, $x, $y) = @_; |
2200 | my ($self, $ev, $x, $y) = @_; |
… | |
… | |
2080 | if ($GRAB == $self) { |
2202 | if ($GRAB == $self) { |
2081 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2203 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2082 | |
2204 | |
2083 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2205 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2084 | |
2206 | |
2085 | $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; |
2207 | $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); |
2086 | |
2208 | |
2087 | $self->set_value ($x * ($hi - $lo) + $lo); |
2209 | $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); |
2088 | } |
2210 | } |
2089 | } |
2211 | } |
2090 | |
2212 | |
2091 | sub update { |
2213 | sub update { |
2092 | my ($self) = @_; |
2214 | my ($self) = @_; |
2093 | |
2215 | |
2094 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
2216 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
2095 | $self->set_value ($self->{range}[0]); |
2217 | $self->set_value ($self->{range}[0]); |
2096 | |
2218 | |
2097 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2219 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2220 | my $range = ($hi - $page - $lo) || 1e-100; |
2098 | |
2221 | |
2099 | my $inner_w = 1 - 2 * $self->{inner_pad}; |
2222 | my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; |
2100 | |
2223 | |
2101 | $self->{scale} = ($inner_w / ($hi - $lo)) || 1; |
2224 | $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; |
|
|
2225 | $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; |
2102 | |
2226 | |
2103 | $page = $self->{scale} * $page || 10 / ($self->{w} || 1); |
2227 | $value = ($value - $lo) / $range; |
2104 | $value = $self->{scale} * ($value - $lo); |
2228 | $value = $value * $self->{scale} + $self->{offset}; |
2105 | |
2229 | |
2106 | $value = $self->{inner_pad} + ($value - $page * 0.5); |
|
|
2107 | |
|
|
2108 | $value = 0 if $value < 0; |
|
|
2109 | $page = 1 - $value if $value + $page > 1; |
|
|
2110 | |
|
|
2111 | $self->{knob_x} = $value; |
2230 | $self->{knob_x} = $value - $knob_w * 0.5; |
2112 | $self->{knob_w} = $page; |
2231 | $self->{knob_w} = $knob_w; |
2113 | }); |
2232 | }); |
2114 | |
2233 | |
2115 | $self->SUPER::update; |
2234 | $self->SUPER::update; |
2116 | } |
2235 | } |
2117 | |
2236 | |
… | |
… | |
2123 | glScale $self->{w}, $self->{h}; |
2242 | glScale $self->{w}, $self->{h}; |
2124 | |
2243 | |
2125 | if ($self->{vertical}) { |
2244 | if ($self->{vertical}) { |
2126 | # draw a vertical slider like a rotated horizontal slider |
2245 | # draw a vertical slider like a rotated horizontal slider |
2127 | |
2246 | |
|
|
2247 | glTranslate 1, 0, 0; |
2128 | glRotate 90, 0, 0, 1; |
2248 | glRotate 90, 0, 0, 1; |
2129 | glTranslate 0, 1, 0; |
|
|
2130 | } |
2249 | } |
2131 | |
2250 | |
2132 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2251 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
2133 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2252 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
2134 | |
2253 | |
… | |
… | |
2141 | # draw handle |
2260 | # draw handle |
2142 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2261 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2143 | |
2262 | |
2144 | glDisable GL_TEXTURE_2D; |
2263 | glDisable GL_TEXTURE_2D; |
2145 | } |
2264 | } |
|
|
2265 | |
|
|
2266 | ############################################################################# |
|
|
2267 | |
|
|
2268 | package CFClient::UI::ValSlider; |
|
|
2269 | |
|
|
2270 | our @ISA = CFClient::UI::HBox::; |
|
|
2271 | |
|
|
2272 | sub new { |
|
|
2273 | my ($class, %arg) = @_; |
|
|
2274 | |
|
|
2275 | my $range = delete $arg{range}; |
|
|
2276 | |
|
|
2277 | my $self = $class->SUPER::new ( |
|
|
2278 | slider => (new CFClient::UI::Slider expand => 1, range => $range), |
|
|
2279 | entry => (new CFClient::UI::Label text => "", template => delete $arg{template}), |
|
|
2280 | to_value => sub { shift }, |
|
|
2281 | from_value => sub { shift }, |
|
|
2282 | %arg, |
|
|
2283 | ); |
|
|
2284 | |
|
|
2285 | $self->{slider}->connect (changed => sub { |
|
|
2286 | my ($self, $value) = @_; |
|
|
2287 | $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value)); |
|
|
2288 | $self->{parent}->emit (changed => $value); |
|
|
2289 | }); |
|
|
2290 | |
|
|
2291 | # $self->{entry}->connect (changed => sub { |
|
|
2292 | # my ($self, $value) = @_; |
|
|
2293 | # $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value)); |
|
|
2294 | # $self->{parent}->emit (changed => $value); |
|
|
2295 | # }); |
|
|
2296 | |
|
|
2297 | $self->add ($self->{slider}, $self->{entry}); |
|
|
2298 | |
|
|
2299 | $self->{slider}->emit (changed => $self->{slider}{range}[0]); |
|
|
2300 | |
|
|
2301 | $self |
|
|
2302 | } |
|
|
2303 | |
|
|
2304 | sub set_range { shift->{slider}->set_range (@_) } |
|
|
2305 | sub set_value { shift->{slider}->set_value (@_) } |
2146 | |
2306 | |
2147 | ############################################################################# |
2307 | ############################################################################# |
2148 | |
2308 | |
2149 | package CFClient::UI::TextView; |
2309 | package CFClient::UI::TextView; |
2150 | |
2310 | |
… | |
… | |
2180 | |
2340 | |
2181 | $self->{fontsize} = $fontsize; |
2341 | $self->{fontsize} = $fontsize; |
2182 | $self->reflow; |
2342 | $self->reflow; |
2183 | } |
2343 | } |
2184 | |
2344 | |
2185 | sub text_height { |
|
|
2186 | my ($self, $text) = @_; |
|
|
2187 | |
|
|
2188 | my $layout = $self->{layout}; |
|
|
2189 | |
|
|
2190 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
2191 | $layout->set_width ($self->{children}[0]{w}); |
|
|
2192 | $layout->set_markup ($text); |
|
|
2193 | |
|
|
2194 | ($layout->size)[1] |
|
|
2195 | } |
|
|
2196 | |
|
|
2197 | sub reflow { |
|
|
2198 | my ($self) = @_; |
|
|
2199 | |
|
|
2200 | $self->{need_reflow}++; |
|
|
2201 | $self->update; |
|
|
2202 | } |
|
|
2203 | |
|
|
2204 | sub size_allocate { |
2345 | sub size_allocate { |
2205 | my ($self, $w, $h) = @_; |
2346 | my ($self, $w, $h) = @_; |
2206 | |
2347 | |
2207 | $self->SUPER::size_allocate ($w, $h); |
2348 | $self->SUPER::size_allocate ($w, $h); |
2208 | |
2349 | |
… | |
… | |
2211 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2352 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2212 | |
2353 | |
2213 | $self->reflow; |
2354 | $self->reflow; |
2214 | } |
2355 | } |
2215 | |
2356 | |
|
|
2357 | sub text_size { |
|
|
2358 | my ($self, $text, $indent) = @_; |
|
|
2359 | |
|
|
2360 | my $layout = $self->{layout}; |
|
|
2361 | |
|
|
2362 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
2363 | $layout->set_width ($self->{children}[0]{w} - $indent); |
|
|
2364 | $layout->set_markup ($text); |
|
|
2365 | |
|
|
2366 | $layout->size |
|
|
2367 | } |
|
|
2368 | |
|
|
2369 | sub reflow { |
|
|
2370 | my ($self) = @_; |
|
|
2371 | |
|
|
2372 | $self->{need_reflow}++; |
|
|
2373 | $self->update; |
|
|
2374 | } |
|
|
2375 | |
|
|
2376 | sub set_offset { |
|
|
2377 | my ($self, $offset) = @_; |
|
|
2378 | |
|
|
2379 | # todo: base offset on lines or so, not on pixels |
|
|
2380 | $self->{children}[1]->set_value ($offset); |
|
|
2381 | } |
|
|
2382 | |
|
|
2383 | sub clear { |
|
|
2384 | my ($self) = @_; |
|
|
2385 | |
|
|
2386 | $self->{par} = []; |
|
|
2387 | $self->{height} = 0; |
|
|
2388 | $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); |
|
|
2389 | } |
|
|
2390 | |
2216 | sub add_paragraph { |
2391 | sub add_paragraph { |
2217 | my ($self, $color, $text) = @_; |
2392 | my ($self, $color, $text, $indent) = @_; |
2218 | |
2393 | |
2219 | #TODO: intelligently "reformat" paragraph |
2394 | for my $line (split /\n/, $text) { |
2220 | |
2395 | my ($w, $h) = $self->text_size ($line); |
2221 | my $height = $self->text_height ($text); |
|
|
2222 | |
|
|
2223 | $self->{height} += $height; |
2396 | $self->{height} += $h; |
|
|
2397 | push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line]; |
|
|
2398 | } |
2224 | |
2399 | |
2225 | push @{$self->{par}}, [$height, $color, $text]; |
|
|
2226 | |
|
|
2227 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
2400 | $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); |
2228 | $self->{children}[1]->update; |
|
|
2229 | } |
2401 | } |
2230 | |
2402 | |
2231 | sub update { |
2403 | sub update { |
2232 | my ($self) = @_; |
2404 | my ($self) = @_; |
2233 | |
2405 | |
… | |
… | |
2236 | return unless $self->{h} > 0; |
2408 | return unless $self->{h} > 0; |
2237 | |
2409 | |
2238 | delete $self->{texture}; |
2410 | delete $self->{texture}; |
2239 | |
2411 | |
2240 | $ROOT->on_post_alloc ($self, sub { |
2412 | $ROOT->on_post_alloc ($self, sub { |
|
|
2413 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
|
|
2414 | |
2241 | if (delete $self->{need_reflow}) { |
2415 | if (delete $self->{need_reflow}) { |
2242 | my $height = 0; |
2416 | my $height = 0; |
2243 | |
2417 | |
2244 | $height += $_->[0] = $self->text_height ($_->[2]) |
2418 | my $layout = $self->{layout}; |
|
|
2419 | |
|
|
2420 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
2421 | |
2245 | for @{$self->{par}}; |
2422 | for (@{$self->{par}}) { |
|
|
2423 | if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support |
|
|
2424 | $layout->set_width ($W - $_->[3]); |
|
|
2425 | $layout->set_markup ($_->[4]); |
|
|
2426 | my ($w, $h) = $layout->size; |
|
|
2427 | $_->[0] = $w + $_->[3]; |
|
|
2428 | $_->[1] = $h; |
|
|
2429 | } |
|
|
2430 | |
|
|
2431 | $height += $_->[1]; |
|
|
2432 | } |
2246 | |
2433 | |
2247 | $self->{height} = $height; |
2434 | $self->{height} = $height; |
2248 | |
2435 | |
2249 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
2436 | $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); |
2250 | |
2437 | |
2251 | delete $self->{texture}; |
2438 | delete $self->{texture}; |
2252 | } |
2439 | } |
2253 | |
2440 | |
2254 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2441 | $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { |
2255 | glClearColor 0, 0, 0, 0; |
2442 | glClearColor 0.5, 0.5, 0.5, 0; |
2256 | glClear GL_COLOR_BUFFER_BIT; |
2443 | glClear GL_COLOR_BUFFER_BIT; |
2257 | |
2444 | |
2258 | glEnable GL_TEXTURE_2D; |
|
|
2259 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2260 | |
|
|
2261 | my $top = int $self->{children}[1]{range}[0]; |
2445 | my $top = int $self->{children}[1]{range}[0]; |
2262 | |
2446 | |
2263 | my $y0 = $top; |
2447 | my $y0 = $top; |
2264 | my $y1 = $top + $self->{h}; |
2448 | my $y1 = $top + $H; |
2265 | |
2449 | |
2266 | my $y = 0; |
2450 | my $y = 0; |
2267 | |
2451 | |
2268 | my $layout = $self->{layout}; |
2452 | my $layout = $self->{layout}; |
2269 | |
2453 | |
2270 | $layout->set_font ($self->{font}) if $self->{font}; |
2454 | $layout->set_font ($self->{font}) if $self->{font}; |
2271 | |
2455 | |
|
|
2456 | glEnable GL_BLEND; |
|
|
2457 | #TODO# not correct in windows where rgba is forced off |
|
|
2458 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2459 | |
2272 | for my $par (@{$self->{par}}) { |
2460 | for my $par (@{$self->{par}}) { |
2273 | my $h = $par->[0]; |
2461 | my $h = $par->[1]; |
2274 | |
2462 | |
2275 | if ($y0 < $y + $h && $y < $y1) { |
2463 | if ($y0 < $y + $h && $y < $y1) { |
2276 | $layout->set_foreground (@{ $par->[1] }); |
2464 | $layout->set_foreground (@{ $par->[2] }); |
|
|
2465 | $layout->set_width ($W - $par->[3]); |
2277 | $layout->set_markup ($par->[2]); |
2466 | $layout->set_markup ($par->[4]); |
2278 | |
2467 | |
2279 | my ($W, $H) = $layout->size; |
2468 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
2280 | CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); |
2469 | |
|
|
2470 | glRasterPos $par->[3], $y - $y0; |
|
|
2471 | glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; |
2281 | } |
2472 | } |
2282 | |
2473 | |
2283 | $y += $h; |
2474 | $y += $h; |
2284 | } |
2475 | } |
2285 | |
2476 | |
2286 | glDisable GL_TEXTURE_2D; |
2477 | glDisable GL_BLEND; |
2287 | }; |
2478 | }; |
2288 | }); |
2479 | }); |
2289 | } |
2480 | } |
2290 | |
2481 | |
2291 | sub _draw { |
2482 | sub _draw { |
2292 | my ($self) = @_; |
2483 | my ($self) = @_; |
2293 | |
2484 | |
2294 | glEnable GL_TEXTURE_2D; |
2485 | glEnable GL_TEXTURE_2D; |
2295 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2486 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2296 | glColor 1, 1, 1, 1; |
2487 | glColor 1, 1, 1, 1; |
2297 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2488 | $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2298 | glDisable GL_TEXTURE_2D; |
2489 | glDisable GL_TEXTURE_2D; |
2299 | |
2490 | |
2300 | $self->{children}[1]->draw; |
2491 | $self->{children}[1]->draw; |
2301 | |
2492 | |
2302 | } |
2493 | } |
… | |
… | |
2371 | |
2562 | |
2372 | # TODO: use animation |
2563 | # TODO: use animation |
2373 | if ($self->{state} = !$self->{state}) { |
2564 | if ($self->{state} = !$self->{state}) { |
2374 | $CFClient::UI::ROOT->add ($self->{other}); |
2565 | $CFClient::UI::ROOT->add ($self->{other}); |
2375 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
2566 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
2376 | $self->emit ("open"); |
2567 | $self->_emit ("open"); |
2377 | } else { |
2568 | } else { |
2378 | $CFClient::UI::ROOT->remove ($self->{other}); |
2569 | $CFClient::UI::ROOT->remove ($self->{other}); |
2379 | $self->emit ("close"); |
2570 | $self->_emit ("close"); |
2380 | } |
2571 | } |
2381 | |
2572 | |
2382 | $self->emit (changed => $self->{state}); |
2573 | $self->_emit (changed => $self->{state}); |
2383 | } |
2574 | } |
2384 | |
2575 | |
2385 | ############################################################################# |
2576 | ############################################################################# |
2386 | |
2577 | |
2387 | package CFClient::UI::Tooltip; |
2578 | package CFClient::UI::Tooltip; |
… | |
… | |
2401 | |
2592 | |
2402 | sub set_tooltip_from { |
2593 | sub set_tooltip_from { |
2403 | my ($self, $widget) = @_; |
2594 | my ($self, $widget) = @_; |
2404 | |
2595 | |
2405 | $self->add (new CFClient::UI::Label |
2596 | $self->add (new CFClient::UI::Label |
2406 | markup => $widget->{tooltip}, |
2597 | markup => $widget->{tooltip}, |
2407 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
2598 | max_w => ($widget->{tooltip_width} || 0.25) * $::WIDTH, |
2408 | fontsize => 0.8, |
2599 | fontsize => 0.8, |
2409 | fg => [0, 0, 0, 1], |
2600 | fg => [0, 0, 0, 1], |
|
|
2601 | ellipsise => 0, |
2410 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
2602 | font => ($widget->{tooltip_font} || $::FONT_PROP), |
2411 | ); |
2603 | ); |
2412 | } |
2604 | } |
2413 | |
2605 | |
2414 | sub size_request { |
2606 | sub size_request { |
2415 | my ($self) = @_; |
2607 | my ($self) = @_; |
… | |
… | |
2461 | use CFClient::OpenGL; |
2653 | use CFClient::OpenGL; |
2462 | |
2654 | |
2463 | sub new { |
2655 | sub new { |
2464 | my $class = shift; |
2656 | my $class = shift; |
2465 | |
2657 | |
2466 | $class->SUPER::new ( |
2658 | my $self = $class->SUPER::new ( |
2467 | aspect => 1, |
2659 | aspect => 1, |
|
|
2660 | can_events => 0, |
2468 | @_, |
2661 | @_, |
2469 | ) |
2662 | ); |
|
|
2663 | |
|
|
2664 | if ($self->{anim} && $self->{animspeed}) { |
|
|
2665 | Scalar::Util::weaken (my $widget = $self); |
|
|
2666 | |
|
|
2667 | $self->{timer} = Event->timer ( |
|
|
2668 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
|
|
2669 | hard => 1, |
|
|
2670 | interval => $self->{animspeed}, |
|
|
2671 | cb => sub { |
|
|
2672 | ++$widget->{frame}; |
|
|
2673 | $widget->update; |
|
|
2674 | }, |
|
|
2675 | ); |
|
|
2676 | } |
|
|
2677 | |
|
|
2678 | $self |
2470 | } |
2679 | } |
2471 | |
2680 | |
2472 | sub size_request { |
2681 | sub size_request { |
2473 | (32, 8) |
2682 | (32, 8) |
2474 | } |
2683 | } |
2475 | |
2684 | |
|
|
2685 | sub update { |
|
|
2686 | my ($self) = @_; |
|
|
2687 | |
|
|
2688 | return unless $self->{visible}; |
|
|
2689 | |
|
|
2690 | $self->SUPER::update; |
|
|
2691 | } |
|
|
2692 | |
2476 | sub _draw { |
2693 | sub _draw { |
2477 | my ($self) = @_; |
2694 | my ($self) = @_; |
2478 | |
2695 | |
2479 | return unless $::CONN;#d# manage and cache textures differently |
2696 | return unless $::CONN; |
|
|
2697 | |
|
|
2698 | my $face; |
|
|
2699 | |
|
|
2700 | if ($self->{frame}) { |
|
|
2701 | my $anim = $::CONN->{anim}[$self->{anim}]; |
|
|
2702 | |
|
|
2703 | $face = $anim->[ $self->{frame} % @$anim ] |
|
|
2704 | if $anim && @$anim; |
|
|
2705 | } |
|
|
2706 | |
2480 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2707 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; |
2481 | |
2708 | |
2482 | # TODO animation |
|
|
2483 | if ($tex) { |
2709 | if ($tex) { |
2484 | glEnable GL_TEXTURE_2D; |
2710 | glEnable GL_TEXTURE_2D; |
2485 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2711 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2486 | glColor 1, 1, 1, 1; |
2712 | glColor 1, 1, 1, 1; |
2487 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2713 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2488 | glDisable GL_TEXTURE_2D; |
2714 | glDisable GL_TEXTURE_2D; |
2489 | } |
2715 | } |
2490 | } |
2716 | } |
2491 | |
2717 | |
|
|
2718 | sub DESTROY { |
|
|
2719 | my ($self) = @_; |
|
|
2720 | |
|
|
2721 | $self->{timer}->cancel |
|
|
2722 | if $self->{timer}; |
|
|
2723 | |
|
|
2724 | $self->SUPER::DESTROY; |
|
|
2725 | } |
|
|
2726 | |
2492 | ############################################################################# |
2727 | ############################################################################# |
2493 | |
2728 | |
2494 | package CFClient::UI::InventoryItem; |
2729 | package CFClient::UI::Inventory; |
2495 | |
2730 | |
2496 | our @ISA = CFClient::UI::HBox::; |
2731 | our @ISA = CFClient::UI::ScrolledWindow::; |
2497 | |
2732 | |
2498 | sub new { |
2733 | sub new { |
2499 | my $class = shift; |
2734 | my $class = shift; |
2500 | |
2735 | |
2501 | my %args = @_; |
|
|
2502 | |
|
|
2503 | my $item = delete $args{item}; |
|
|
2504 | |
|
|
2505 | my $desc = $item->{nrof} < 2 |
|
|
2506 | ? $item->{name} |
|
|
2507 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2508 | |
|
|
2509 | $item->{flags} & Crossfire::Protocol::F_OPEN |
|
|
2510 | and $desc .= " (open)"; |
|
|
2511 | $item->{flags} & Crossfire::Protocol::F_APPLIED |
|
|
2512 | and $desc .= " (applied)"; |
|
|
2513 | $item->{flags} & Crossfire::Protocol::F_UNPAID |
|
|
2514 | and $desc .= " (unpaid)"; |
|
|
2515 | $item->{flags} & Crossfire::Protocol::F_MAGIC |
|
|
2516 | and $desc .= " (magic)"; |
|
|
2517 | $item->{flags} & Crossfire::Protocol::F_CURSED |
|
|
2518 | and $desc .= " (cursed)"; |
|
|
2519 | $item->{flags} & Crossfire::Protocol::F_DAMNED |
|
|
2520 | and $desc .= " (damned)"; |
|
|
2521 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2522 | and $desc .= " *"; |
|
|
2523 | |
|
|
2524 | my $self = $class->SUPER::new ( |
2736 | my $self = $class->SUPER::new ( |
2525 | can_hover => 1, |
2737 | scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]), |
2526 | can_events => 1, |
|
|
2527 | tooltip => (CFClient::UI::Label->escape ($desc) |
|
|
2528 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
|
|
2529 | connect_button_down => sub { |
|
|
2530 | my ($self, $ev, $x, $y) = @_; |
|
|
2531 | |
|
|
2532 | # todo: maybe put examine on 1? but should just be a tooltip :( |
|
|
2533 | if ($ev->{button} == 1) { |
|
|
2534 | $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0"); |
|
|
2535 | } elsif ($ev->{button} == 2) { |
|
|
2536 | $::CONN->send ("apply $item->{tag}"); |
|
|
2537 | } elsif ($ev->{button} == 3) { |
|
|
2538 | my @menu_items = ( |
|
|
2539 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2540 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2541 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2542 | ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], |
|
|
2543 | [ |
|
|
2544 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
|
|
2545 | sub { $::CONN->send ("lock ". () ."$item->{tag}") }, |
|
|
2546 | ], |
|
|
2547 | ); |
|
|
2548 | |
|
|
2549 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
|
|
2550 | } |
|
|
2551 | |
|
|
2552 | 1 |
|
|
2553 | }, |
|
|
2554 | %args |
|
|
2555 | ); |
|
|
2556 | |
|
|
2557 | $self->add (new CFClient::UI::Face |
|
|
2558 | can_events => 0, |
|
|
2559 | face => $item->{face}, |
|
|
2560 | anim => $item->{anim}, |
|
|
2561 | animspeed => $item->{animspeed}, |
|
|
2562 | ); |
|
|
2563 | |
|
|
2564 | $self->add (new CFClient::UI::Label |
|
|
2565 | can_events => 0, |
|
|
2566 | text => $desc, |
|
|
2567 | ); |
|
|
2568 | |
|
|
2569 | $self |
|
|
2570 | } |
|
|
2571 | |
|
|
2572 | ############################################################################# |
|
|
2573 | |
|
|
2574 | package CFClient::UI::Inventory; |
|
|
2575 | |
|
|
2576 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
2577 | |
|
|
2578 | sub new { |
|
|
2579 | my $class = shift; |
|
|
2580 | |
|
|
2581 | my $self = $class->SUPER::new ( |
|
|
2582 | scrolled => (new CFClient::UI::VBox), |
|
|
2583 | @_, |
2738 | @_, |
2584 | ); |
2739 | ); |
2585 | |
2740 | |
2586 | $self |
2741 | $self |
2587 | } |
2742 | } |
… | |
… | |
2597 | or ($a->{name} cmp $b->{name}) |
2752 | or ($a->{name} cmp $b->{name}) |
2598 | } @$items; |
2753 | } @$items; |
2599 | |
2754 | |
2600 | $self->{real_items} = \@items; |
2755 | $self->{real_items} = \@items; |
2601 | |
2756 | |
|
|
2757 | my $row = 0; |
2602 | for my $item (@items) { |
2758 | for my $item (@items) { |
2603 | my $desc = $item->{nrof} < 2 |
2759 | CFClient::Item::update_widgets $item; |
2604 | ? $item->{name} |
|
|
2605 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2606 | |
2760 | |
2607 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2761 | $self->{scrolled}->add (0, $row, $item->{face_widget}); |
2608 | } |
2762 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
|
|
2763 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2609 | |
2764 | |
2610 | $self->{scrolled}->add (@items); |
2765 | $row++; |
2611 | |
2766 | } |
2612 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2613 | } |
2767 | } |
2614 | |
2768 | |
2615 | sub size_request { |
2769 | sub size_request { |
2616 | my ($self) = @_; |
2770 | my ($self) = @_; |
2617 | ($self->{req_w}, $self->{req_h}); |
2771 | ($self->{req_w}, $self->{req_h}); |
… | |
… | |
2657 | |
2811 | |
2658 | # popup given the event (must be a mouse button down event currently) |
2812 | # popup given the event (must be a mouse button down event currently) |
2659 | sub popup { |
2813 | sub popup { |
2660 | my ($self, $ev) = @_; |
2814 | my ($self, $ev) = @_; |
2661 | |
2815 | |
2662 | $self->emit ("popdown"); |
2816 | $self->_emit ("popdown"); |
2663 | |
2817 | |
2664 | # maybe save $GRAB? must be careful about events... |
2818 | # maybe save $GRAB? must be careful about events... |
2665 | $GRAB = $self; |
2819 | $GRAB = $self; |
2666 | $self->{button} = $ev->{button}; |
2820 | $self->{button} = $ev->{button}; |
2667 | |
2821 | |
… | |
… | |
2682 | |
2836 | |
2683 | if ($ev->{button} == $self->{button}) { |
2837 | if ($ev->{button} == $self->{button}) { |
2684 | undef $GRAB; |
2838 | undef $GRAB; |
2685 | $self->hide; |
2839 | $self->hide; |
2686 | |
2840 | |
2687 | $self->emit ("popdown"); |
2841 | $self->_emit ("popdown"); |
2688 | $self->{hover}[1]->() if $self->{hover}; |
2842 | $self->{hover}[1]->() if $self->{hover}; |
2689 | } |
2843 | } |
2690 | } |
2844 | } |
2691 | |
2845 | |
2692 | ############################################################################# |
2846 | ############################################################################# |
2693 | |
2847 | |
2694 | package CFClient::UI::Statusbox; |
2848 | package CFClient::UI::Statusbox; |
2695 | |
2849 | |
2696 | our @ISA = CFClient::UI::VBox::; |
2850 | our @ISA = CFClient::UI::VBox::; |
|
|
2851 | |
|
|
2852 | sub new { |
|
|
2853 | my $class = shift; |
|
|
2854 | |
|
|
2855 | $class->SUPER::new ( |
|
|
2856 | fontsize => 0.8, |
|
|
2857 | @_, |
|
|
2858 | ) |
|
|
2859 | } |
2697 | |
2860 | |
2698 | sub reorder { |
2861 | sub reorder { |
2699 | my ($self) = @_; |
2862 | my ($self) = @_; |
2700 | my $NOW = time; |
2863 | my $NOW = time; |
2701 | |
2864 | |
… | |
… | |
2721 | : $item->{text}; |
2884 | : $item->{text}; |
2722 | |
2885 | |
2723 | for ($short) { |
2886 | for ($short) { |
2724 | s/^\s+//; |
2887 | s/^\s+//; |
2725 | s/\s+/ /g; |
2888 | s/\s+/ /g; |
2726 | my $len = int 40 / $item->{fontsize}; |
|
|
2727 | substr $_, $len, length, "…" if $len < length; |
|
|
2728 | } |
2889 | } |
2729 | |
2890 | |
2730 | new CFClient::UI::Label |
2891 | new CFClient::UI::Label |
2731 | markup => $short, |
2892 | markup => $short, |
2732 | tooltip => $item->{tooltip}, |
2893 | tooltip => $item->{tooltip}, |
2733 | tooltip_font => $::FONT_PROP, |
2894 | tooltip_font => $::FONT_PROP, |
2734 | tooltip_width => 0.67, |
2895 | tooltip_width => 0.67, |
2735 | fontsize => $item->{fontsize}, |
2896 | fontsize => $item->{fontsize} || $self->{fontsize}, |
|
|
2897 | max_w => $::WIDTH * 0.44, |
2736 | fg => $item->{fg}, |
2898 | fg => $item->{fg}, |
2737 | can_events => 1, |
2899 | can_events => 1, |
2738 | can_hover => 1 |
2900 | can_hover => 1 |
2739 | }; |
2901 | }; |
2740 | } |
2902 | } |
… | |
… | |
2746 | sub add { |
2908 | sub add { |
2747 | my ($self, $text, %arg) = @_; |
2909 | my ($self, $text, %arg) = @_; |
2748 | |
2910 | |
2749 | $text =~ s/^\s+//; |
2911 | $text =~ s/^\s+//; |
2750 | $text =~ s/\s+$//; |
2912 | $text =~ s/\s+$//; |
|
|
2913 | |
|
|
2914 | return unless $text; |
2751 | |
2915 | |
2752 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2916 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2753 | |
2917 | |
2754 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2918 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2755 | |
2919 | |
… | |
… | |
2767 | $self->{item}{$group} = { |
2931 | $self->{item}{$group} = { |
2768 | id => ++$self->{id}, |
2932 | id => ++$self->{id}, |
2769 | text => $text, |
2933 | text => $text, |
2770 | timeout => $timeout, |
2934 | timeout => $timeout, |
2771 | tooltip => $text, |
2935 | tooltip => $text, |
2772 | fontsize => 0.8, |
|
|
2773 | fg => [0.8, 0.8, 0.8, 0.8], |
2936 | fg => [0.8, 0.8, 0.8, 0.8], |
2774 | pri => 0, |
2937 | pri => 0, |
2775 | count => 1, |
2938 | count => 1, |
2776 | %arg, |
2939 | %arg, |
2777 | }; |
2940 | }; |
2778 | } |
2941 | } |
2779 | |
2942 | |
2780 | $self->reorder; |
2943 | $self->reorder; |
2781 | } |
2944 | } |
2782 | |
2945 | |
|
|
2946 | sub reconfigure { |
|
|
2947 | my ($self) = @_; |
|
|
2948 | |
|
|
2949 | delete $_->{label} |
|
|
2950 | for values %{ $self->{item} || {} }; |
|
|
2951 | |
|
|
2952 | $self->reorder; |
|
|
2953 | $self->SUPER::reconfigure; |
|
|
2954 | } |
|
|
2955 | |
2783 | ############################################################################# |
2956 | ############################################################################# |
2784 | |
2957 | |
2785 | package CFClient::UI::Root; |
2958 | package CFClient::UI::Root; |
2786 | |
2959 | |
2787 | our @ISA = CFClient::UI::Container::; |
2960 | our @ISA = CFClient::UI::Container::; |
… | |
… | |
2817 | } |
2990 | } |
2818 | |
2991 | |
2819 | sub size_allocate { |
2992 | sub size_allocate { |
2820 | my ($self, $w, $h) = @_; |
2993 | my ($self, $w, $h) = @_; |
2821 | |
2994 | |
2822 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
|
|
2823 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
|
|
2824 | |
|
|
2825 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
|
|
2826 | if $old_w && $old_h && ($old_w != $w || $old_h != $h); |
|
|
2827 | |
|
|
2828 | for my $child ($self->children) { |
2995 | for my $child ($self->children) { |
2829 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2996 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2830 | |
2997 | |
2831 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
2998 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
2832 | if exists $child->{req_x}; |
2999 | if exists $child->{req_x}; |
… | |
… | |
2861 | } |
3028 | } |
2862 | |
3029 | |
2863 | sub add { |
3030 | sub add { |
2864 | my ($self, @children) = @_; |
3031 | my ($self, @children) = @_; |
2865 | |
3032 | |
|
|
3033 | for (my @widgets = @children; my $w = pop @widgets; ) { |
|
|
3034 | push @widgets, $w->children; |
|
|
3035 | $w->{root} = $self; |
|
|
3036 | $w->{visible} = 1; |
|
|
3037 | } |
|
|
3038 | |
2866 | for my $child (@children) { |
3039 | for my $child (@children) { |
2867 | $child->{toplevel} = 1; |
3040 | $child->{is_toplevel} = 1; |
2868 | |
3041 | |
2869 | # integerise window positions |
3042 | # integerise window positions |
2870 | $child->{x} = int $child->{x}; |
3043 | $child->{x} = int $child->{x}; |
2871 | $child->{y} = int $child->{y}; |
3044 | $child->{y} = int $child->{y}; |
2872 | } |
3045 | } |
2873 | |
3046 | |
2874 | $self->SUPER::add (@children); |
3047 | $self->SUPER::add (@children); |
|
|
3048 | } |
|
|
3049 | |
|
|
3050 | sub remove { |
|
|
3051 | my ($self, @children) = @_; |
|
|
3052 | |
|
|
3053 | $self->SUPER::remove (@children); |
|
|
3054 | |
|
|
3055 | while (@children) { |
|
|
3056 | my $w = pop @children; |
|
|
3057 | push @children, $w->children; |
|
|
3058 | $w->set_invisible; |
|
|
3059 | } |
2875 | } |
3060 | } |
2876 | |
3061 | |
2877 | sub on_refresh { |
3062 | sub on_refresh { |
2878 | my ($self, $id, $cb) = @_; |
3063 | my ($self, $id, $cb) = @_; |
2879 | |
3064 | |
… | |
… | |
2932 | $w = 0 if $w < 0; |
3117 | $w = 0 if $w < 0; |
2933 | $h = 0 if $h < 0; |
3118 | $h = 0 if $h < 0; |
2934 | |
3119 | |
2935 | $widget->{w} = $w; |
3120 | $widget->{w} = $w; |
2936 | $widget->{h} = $h; |
3121 | $widget->{h} = $h; |
2937 | $widget->size_allocate ($w, $h); |
|
|
2938 | $widget->emit (size_allocate => $w, $h); |
3122 | $widget->emit (size_allocate => $w, $h); |
2939 | } |
3123 | } |
2940 | } |
3124 | } |
2941 | |
3125 | |
2942 | while ($self->{post_alloc_hook}) { |
3126 | while ($self->{post_alloc_hook}) { |
… | |
… | |
2948 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
3132 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
2949 | glClear GL_COLOR_BUFFER_BIT; |
3133 | glClear GL_COLOR_BUFFER_BIT; |
2950 | |
3134 | |
2951 | glMatrixMode GL_PROJECTION; |
3135 | glMatrixMode GL_PROJECTION; |
2952 | glLoadIdentity; |
3136 | glLoadIdentity; |
2953 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
3137 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
2954 | glMatrixMode GL_MODELVIEW; |
3138 | glMatrixMode GL_MODELVIEW; |
2955 | glLoadIdentity; |
3139 | glLoadIdentity; |
2956 | |
3140 | |
2957 | $self->_draw; |
3141 | $self->_draw; |
2958 | } |
3142 | } |
… | |
… | |
2960 | ############################################################################# |
3144 | ############################################################################# |
2961 | |
3145 | |
2962 | package CFClient::UI; |
3146 | package CFClient::UI; |
2963 | |
3147 | |
2964 | $ROOT = new CFClient::UI::Root; |
3148 | $ROOT = new CFClient::UI::Root; |
2965 | $TOOLTIP = new CFClient::UI::Tooltip; |
3149 | $TOOLTIP = new CFClient::UI::Tooltip z => 900; |
2966 | |
3150 | |
2967 | 1 |
3151 | 1 |
2968 | |
3152 | |