… | |
… | |
5 | |
5 | |
6 | use Scalar::Util (); |
6 | use Scalar::Util (); |
7 | use List::Util (); |
7 | use List::Util (); |
8 | |
8 | |
9 | use CFClient; |
9 | use CFClient; |
|
|
10 | use CFClient::Texture; |
10 | |
11 | |
11 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | |
13 | |
|
|
14 | our $LAYOUT; |
13 | our $ROOT; |
15 | our $ROOT; |
14 | our $TOOLTIP; |
16 | our $TOOLTIP; |
15 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
16 | |
18 | |
17 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
|
|
20 | |
|
|
21 | sub get_layout { |
|
|
22 | for (grep { $_->{name} } values %WIDGET) { |
|
|
23 | $LAYOUT->{$_->{name}} = { |
|
|
24 | x => $_->{x} / $::WIDTH, |
|
|
25 | y => $_->{y} / $::HEIGHT, |
|
|
26 | w => $_->{w} / $::WIDTH, |
|
|
27 | h => $_->{h} / $::HEIGHT |
|
|
28 | }; |
|
|
29 | } |
|
|
30 | |
|
|
31 | return $LAYOUT; |
|
|
32 | } |
|
|
33 | |
|
|
34 | sub set_layout { |
|
|
35 | my ($layout) = @_; |
|
|
36 | $LAYOUT = $layout; |
|
|
37 | } |
18 | |
38 | |
19 | sub check_tooltip { |
39 | sub check_tooltip { |
20 | if (!$GRAB) { |
40 | if (!$GRAB) { |
21 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
41 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
22 | if (length $widget->{tooltip}) { |
42 | if (length $widget->{tooltip}) { |
… | |
… | |
50 | delete $TOOLTIP->{owner}; |
70 | delete $TOOLTIP->{owner}; |
51 | } |
71 | } |
52 | |
72 | |
53 | # class methods for events |
73 | # class methods for events |
54 | sub feed_sdl_key_down_event { |
74 | sub feed_sdl_key_down_event { |
55 | $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) |
75 | $FOCUS->emit (key_down => $_[0]) |
56 | if $FOCUS; |
76 | if $FOCUS; |
57 | } |
77 | } |
58 | |
78 | |
59 | sub feed_sdl_key_up_event { |
79 | sub feed_sdl_key_up_event { |
60 | $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) |
80 | $FOCUS->emit (key_up => $_[0]) |
61 | if $FOCUS; |
81 | if $FOCUS; |
62 | } |
82 | } |
63 | |
83 | |
64 | sub feed_sdl_button_down_event { |
84 | sub feed_sdl_button_down_event { |
65 | my ($ev) = @_; |
85 | my ($ev) = @_; |
… | |
… | |
74 | check_tooltip; |
94 | check_tooltip; |
75 | } |
95 | } |
76 | |
96 | |
77 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
97 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
78 | |
98 | |
79 | if ($GRAB) { |
99 | $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y)) |
80 | ($x, $y) = $GRAB->coord2local ($x, $y); |
100 | if $GRAB; |
81 | $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y); |
|
|
82 | } |
|
|
83 | } |
101 | } |
84 | |
102 | |
85 | sub feed_sdl_button_up_event { |
103 | sub feed_sdl_button_up_event { |
86 | my ($ev) = @_; |
104 | my ($ev) = @_; |
87 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
105 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
88 | |
106 | |
89 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
107 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
90 | |
108 | |
91 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
109 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
92 | |
110 | |
93 | if ($GRAB) { |
111 | $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y)) |
94 | ($x, $y) = $GRAB->coord2local ($x, $y); |
112 | if $GRAB; |
95 | $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y); |
|
|
96 | } |
|
|
97 | |
113 | |
98 | if (!$BUTTON_STATE) { |
114 | if (!$BUTTON_STATE) { |
99 | my $grab = $GRAB; undef $GRAB; |
115 | my $grab = $GRAB; undef $GRAB; |
100 | $grab->update if $grab; |
116 | $grab->update if $grab; |
101 | $GRAB->update if $GRAB; |
117 | $GRAB->update if $GRAB; |
… | |
… | |
117 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
133 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
118 | |
134 | |
119 | check_tooltip; |
135 | check_tooltip; |
120 | } |
136 | } |
121 | |
137 | |
122 | if ($HOVER) { |
|
|
123 | ($x, $y) = $HOVER->coord2local ($x, $y); |
|
|
124 | $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); |
138 | $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y)) |
125 | } |
139 | if $HOVER; |
126 | } |
140 | } |
127 | |
141 | |
128 | # convert position array to integers |
142 | # convert position array to integers |
129 | sub harmonize { |
143 | sub harmonize { |
130 | my ($vals) = @_; |
144 | my ($vals) = @_; |
… | |
… | |
136 | $rem += $_ - $i; |
150 | $rem += $_ - $i; |
137 | $_ = $i; |
151 | $_ = $i; |
138 | } |
152 | } |
139 | } |
153 | } |
140 | |
154 | |
|
|
155 | sub full_refresh { |
|
|
156 | # make a copy, otherwise for complains about freed values. |
|
|
157 | my @widgets = values %WIDGET; |
|
|
158 | |
|
|
159 | $_->update |
|
|
160 | for @widgets; |
|
|
161 | } |
|
|
162 | |
|
|
163 | sub reconfigure_widgets { |
|
|
164 | # make a copy, otherwise C<for> complains about freed values. |
|
|
165 | my @widgets = values %WIDGET; |
|
|
166 | |
|
|
167 | $_->reconfigure |
|
|
168 | for @widgets; |
|
|
169 | } |
|
|
170 | |
141 | # call when resolution changes etc. |
171 | # call when resolution changes etc. |
142 | sub rescale_widgets { |
172 | sub rescale_widgets { |
143 | my ($sx, $sy) = @_; |
173 | my ($sx, $sy) = @_; |
144 | |
174 | |
145 | # make a copy, otherwise for complains about freed values. |
|
|
146 | my @widgets = values %WIDGET; |
175 | for my $widget (values %WIDGET) { |
147 | |
|
|
148 | for my $widget (@widgets) { |
|
|
149 | if ($widget->{toplevel}) { |
176 | if ($widget->{is_toplevel}) { |
150 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
177 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
151 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
178 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
152 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
179 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
153 | $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; |
180 | $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; |
154 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
181 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
155 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
182 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
156 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
183 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
157 | $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; |
184 | $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; |
158 | } |
185 | } |
159 | |
|
|
160 | $widget->reconfigure; |
|
|
161 | } |
186 | } |
|
|
187 | |
|
|
188 | reconfigure_widgets; |
162 | } |
189 | } |
163 | |
190 | |
164 | ############################################################################# |
191 | ############################################################################# |
165 | |
192 | |
166 | package CFClient::UI::Base; |
193 | package CFClient::UI::Base; |
… | |
… | |
179 | can_events => 1, |
206 | can_events => 1, |
180 | @_ |
207 | @_ |
181 | }, $class; |
208 | }, $class; |
182 | |
209 | |
183 | for (keys %$self) { |
210 | for (keys %$self) { |
184 | if (/^connect_(.*)$/) { |
211 | if (/^on_(.*)$/) { |
185 | $self->connect ($1 => delete $self->{$_}); |
212 | $self->connect ($1 => delete $self->{$_}); |
186 | } |
213 | } |
187 | } |
214 | } |
188 | |
215 | |
189 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
216 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
190 | |
217 | |
|
|
218 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
|
|
219 | $self->{user_x} = $layout->{x} * $::WIDTH; |
|
|
220 | $self->{user_y} = $layout->{y} * $::HEIGHT; |
|
|
221 | $self->{user_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; |
|
|
222 | $self->{user_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; |
|
|
223 | } |
|
|
224 | |
191 | $self |
225 | $self |
192 | } |
226 | } |
193 | |
227 | |
194 | sub destroy { |
228 | sub destroy { |
195 | my ($self) = @_; |
229 | my ($self) = @_; |
… | |
… | |
198 | %$self = (); |
232 | %$self = (); |
199 | } |
233 | } |
200 | |
234 | |
201 | sub show { |
235 | sub show { |
202 | my ($self) = @_; |
236 | my ($self) = @_; |
203 | |
|
|
204 | return if $self->{parent}; |
237 | return if $self->{parent}; |
205 | |
238 | |
206 | $CFClient::UI::ROOT->add ($self); |
239 | $CFClient::UI::ROOT->add ($self); |
207 | } |
240 | } |
208 | |
241 | |
209 | sub hide { |
242 | sub show_centered { |
210 | my ($self) = @_; |
243 | my ($self) = @_; |
|
|
244 | return if $self->{parent}; |
|
|
245 | |
|
|
246 | $self->show; |
|
|
247 | |
|
|
248 | $CFClient::UI::ROOT->on_post_alloc ( |
|
|
249 | "centered $self" => sub { |
|
|
250 | $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); |
|
|
251 | }, |
|
|
252 | ); |
|
|
253 | } |
|
|
254 | |
|
|
255 | sub set_invisible { |
|
|
256 | my ($self) = @_; |
|
|
257 | |
|
|
258 | # broken show/hide model |
|
|
259 | |
|
|
260 | delete $self->{root}; |
|
|
261 | delete $self->{visible}; |
211 | |
262 | |
212 | undef $GRAB if $GRAB == $self; |
263 | undef $GRAB if $GRAB == $self; |
213 | undef $HOVER if $HOVER == $self; |
264 | undef $HOVER if $HOVER == $self; |
|
|
265 | |
|
|
266 | CFClient::UI::check_tooltip |
|
|
267 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
|
|
268 | |
|
|
269 | $self->focus_out; |
|
|
270 | } |
|
|
271 | |
|
|
272 | sub hide { |
|
|
273 | my ($self) = @_; |
|
|
274 | |
|
|
275 | $self->set_invisible; |
214 | |
276 | |
215 | $self->{parent}->remove ($self) |
277 | $self->{parent}->remove ($self) |
216 | if $self->{parent}; |
278 | if $self->{parent}; |
217 | } |
279 | } |
218 | |
280 | |
… | |
… | |
288 | } |
350 | } |
289 | |
351 | |
290 | sub set_tooltip { |
352 | sub set_tooltip { |
291 | my ($self, $tooltip) = @_; |
353 | my ($self, $tooltip) = @_; |
292 | |
354 | |
|
|
355 | $tooltip =~ s/^\s+//; |
|
|
356 | $tooltip =~ s/\s+$//; |
|
|
357 | |
|
|
358 | return if $self->{tooltip} eq $tooltip; |
|
|
359 | |
293 | $self->{tooltip} = $tooltip; |
360 | $self->{tooltip} = $tooltip; |
294 | |
361 | |
295 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
362 | if ($CFClient::UI::TOOLTIP->{owner} == $self) { |
296 | delete $CFClient::UI::TOOLTIP->{owner}; |
363 | delete $CFClient::UI::TOOLTIP->{owner}; |
297 | CFClient::UI::check_tooltip; |
364 | CFClient::UI::check_tooltip; |
… | |
… | |
318 | return if $FOCUS == $self; |
385 | return if $FOCUS == $self; |
319 | return unless $self->{can_focus}; |
386 | return unless $self->{can_focus}; |
320 | |
387 | |
321 | my $focus = $FOCUS; $FOCUS = $self; |
388 | my $focus = $FOCUS; $FOCUS = $self; |
322 | |
389 | |
323 | $self->emit (focus_in => $focus); |
390 | $self->_emit (focus_in => $focus); |
324 | |
391 | |
325 | $focus->update if $focus; |
392 | $focus->update if $focus; |
326 | $FOCUS->update; |
393 | $FOCUS->update; |
327 | } |
394 | } |
328 | |
395 | |
… | |
… | |
331 | |
398 | |
332 | return unless $FOCUS == $self; |
399 | return unless $FOCUS == $self; |
333 | |
400 | |
334 | my $focus = $FOCUS; undef $FOCUS; |
401 | my $focus = $FOCUS; undef $FOCUS; |
335 | |
402 | |
336 | $self->emit (focus_out => $focus); |
403 | $self->_emit (focus_out => $focus); |
337 | |
404 | |
338 | $focus->update if $focus; #? |
405 | $focus->update if $focus; #? |
|
|
406 | |
|
|
407 | $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus |
|
|
408 | unless $FOCUS; |
339 | } |
409 | } |
340 | |
410 | |
341 | sub mouse_motion { } |
411 | sub mouse_motion { } |
342 | sub button_up { } |
412 | sub button_up { } |
343 | sub key_down { } |
413 | sub key_down { } |
344 | sub key_up { } |
414 | sub key_up { } |
345 | |
415 | |
346 | sub button_down { |
416 | sub button_down { |
347 | my ($self, $ev, $x, $y) = @_; |
417 | my ($self, $ev, $x, $y) = @_; |
348 | |
418 | |
349 | $self->focus_in; |
419 | $self->focus_in; |
… | |
… | |
383 | if ($ENV{PCLIENT_DEBUG}) { |
453 | if ($ENV{PCLIENT_DEBUG}) { |
384 | glPushMatrix; |
454 | glPushMatrix; |
385 | glColor 1, 1, 0, 1; |
455 | glColor 1, 1, 0, 1; |
386 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
456 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
387 | glBegin GL_LINE_LOOP; |
457 | glBegin GL_LINE_LOOP; |
388 | glVertex 0 , 0; |
458 | glVertex 0 , 0; |
389 | glVertex $self->{w}, 0; |
459 | glVertex $self->{w} - 1, 0; |
390 | glVertex $self->{w}, $self->{h}; |
460 | glVertex $self->{w} - 1, $self->{h} - 1; |
391 | glVertex 0 , $self->{h}; |
461 | glVertex 0 , $self->{h} - 1; |
392 | glEnd; |
462 | glEnd; |
393 | glPopMatrix; |
463 | glPopMatrix; |
394 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
464 | #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; |
395 | } |
465 | } |
396 | } |
466 | } |
… | |
… | |
416 | sub set_parent { |
486 | sub set_parent { |
417 | my ($self, $parent) = @_; |
487 | my ($self, $parent) = @_; |
418 | |
488 | |
419 | Scalar::Util::weaken ($self->{parent} = $parent); |
489 | Scalar::Util::weaken ($self->{parent} = $parent); |
420 | |
490 | |
|
|
491 | $self->{root} = $parent->{root}; |
|
|
492 | $self->{visible} = $parent->{visible} + 1; |
|
|
493 | |
421 | # TODO: req_w _does_change after ->reconfigure |
494 | # TODO: req_w _does_change after ->reconfigure |
422 | $self->check_size |
495 | $self->check_size |
423 | unless exists $self->{req_w}; |
496 | unless exists $self->{req_w}; |
|
|
497 | |
|
|
498 | $self->show; |
424 | } |
499 | } |
425 | |
500 | |
426 | sub check_size { |
501 | sub check_size { |
427 | my ($self, $forced) = @_; |
502 | my ($self, $forced) = @_; |
428 | |
503 | |
… | |
… | |
441 | my ($self, $signal, $cb) = @_; |
516 | my ($self, $signal, $cb) = @_; |
442 | |
517 | |
443 | push @{ $self->{signal_cb}{$signal} }, $cb; |
518 | push @{ $self->{signal_cb}{$signal} }, $cb; |
444 | } |
519 | } |
445 | |
520 | |
|
|
521 | sub _emit { |
|
|
522 | my ($self, $signal, @args) = @_; |
|
|
523 | |
|
|
524 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
|
|
525 | } |
|
|
526 | |
446 | sub emit { |
527 | sub emit { |
447 | my ($self, $signal, @args) = @_; |
528 | my ($self, $signal, @args) = @_; |
448 | |
529 | |
449 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
530 | $self->_emit ($signal, @args) |
|
|
531 | || $self->$signal (@args); |
450 | } |
532 | } |
451 | |
533 | |
452 | sub DESTROY { |
534 | sub DESTROY { |
453 | my ($self) = @_; |
535 | my ($self) = @_; |
454 | |
536 | |
… | |
… | |
567 | delete $child->{parent}; |
649 | delete $child->{parent}; |
568 | $child->hide; |
650 | $child->hide; |
569 | |
651 | |
570 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
652 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
571 | |
653 | |
572 | $self->check_size; |
654 | $self->check_size (1); |
573 | $self->update; |
655 | $self->update; |
574 | } |
656 | } |
575 | |
657 | |
576 | sub clear { |
658 | sub clear { |
577 | my ($self) = @_; |
659 | my ($self) = @_; |
… | |
… | |
691 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
773 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
692 | glClearColor 0, 0, 0, 0; |
774 | glClearColor 0, 0, 0, 0; |
693 | glClear GL_COLOR_BUFFER_BIT; |
775 | glClear GL_COLOR_BUFFER_BIT; |
694 | |
776 | |
695 | $self->_render; |
777 | $self->_render; |
696 | # glColorMask 1, 1, 1, 0; |
|
|
697 | # glEnable GL_BLEND; |
|
|
698 | # glBlendFunc GL_SRC_ALPHA, GL_ZERO; |
|
|
699 | # glRasterPos 0, 0; |
|
|
700 | # glCopyPixels 0, 0, $self->{w}, $self->{h}; |
|
|
701 | # glDisable GL_BLEND; |
|
|
702 | # glColorMask 1, 1, 1, 1; |
|
|
703 | }; |
778 | }; |
704 | } |
779 | } |
705 | |
780 | |
706 | sub _draw { |
781 | sub _draw { |
707 | my ($self) = @_; |
782 | my ($self) = @_; |
… | |
… | |
711 | my $tex = $self->{texture} |
786 | my $tex = $self->{texture} |
712 | or return; |
787 | or return; |
713 | |
788 | |
714 | glEnable GL_TEXTURE_2D; |
789 | glEnable GL_TEXTURE_2D; |
715 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
790 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
716 | glColor 0, 0, 0, 1; |
791 | glColor 1, 1, 1, 1; |
717 | |
792 | |
718 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
793 | $tex->draw_quad_alpha_premultiplied (0, 0, $w, $h); |
719 | |
794 | |
720 | glDisable GL_TEXTURE_2D; |
795 | glDisable GL_TEXTURE_2D; |
721 | } |
796 | } |
… | |
… | |
724 | |
799 | |
725 | package CFClient::UI::ViewPort; |
800 | package CFClient::UI::ViewPort; |
726 | |
801 | |
727 | our @ISA = CFClient::UI::Window::; |
802 | our @ISA = CFClient::UI::Window::; |
728 | |
803 | |
|
|
804 | sub new { |
|
|
805 | my $class = shift; |
|
|
806 | |
|
|
807 | $class->SUPER::new ( |
|
|
808 | scroll_x => 0, |
|
|
809 | scroll_y => 1, |
|
|
810 | @_, |
|
|
811 | ) |
|
|
812 | } |
|
|
813 | |
729 | sub size_request { |
814 | sub size_request { |
730 | my ($self) = @_; |
815 | my ($self) = @_; |
731 | |
816 | |
732 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
817 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
733 | $self->child->configure (0, 0, @$self{qw(child_w child_h)}); |
|
|
734 | |
818 | |
735 | @$self{qw(child_w child_h)} |
819 | @$self{qw(child_w child_h)} |
736 | } |
820 | } |
737 | |
821 | |
738 | sub size_allocate { |
822 | sub size_allocate { |
739 | my ($self, $w, $h) = @_; |
823 | my ($self, $w, $h) = @_; |
740 | |
824 | |
|
|
825 | $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; |
|
|
826 | $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; |
|
|
827 | |
|
|
828 | $self->child->configure (0, 0, $w, $h); |
741 | $self->update; |
829 | $self->update; |
742 | } |
830 | } |
743 | |
831 | |
744 | sub set_offset { |
832 | sub set_offset { |
745 | my ($self, $x, $y) = @_; |
833 | my ($self, $x, $y) = @_; |
… | |
… | |
796 | my $class = shift; |
884 | my $class = shift; |
797 | |
885 | |
798 | my $self; |
886 | my $self; |
799 | |
887 | |
800 | my $slider = new CFClient::UI::Slider |
888 | my $slider = new CFClient::UI::Slider |
801 | vertical => 1, |
889 | vertical => 1, |
802 | range => [0, 0, 1, 0.01], # HACK fix |
890 | range => [0, 0, 1, 0.01], # HACK fix |
803 | connect_changed => sub { |
891 | on_changed => sub { |
804 | $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h})); |
892 | $self->{vp}->set_offset (0, $_[1]); |
805 | }, |
893 | }, |
806 | ; |
894 | ; |
807 | |
895 | |
808 | $self = $class->SUPER::new ( |
896 | $self = $class->SUPER::new ( |
809 | vp => (new CFClient::UI::ViewPort), |
897 | vp => (new CFClient::UI::ViewPort expand => 1), |
810 | slider => $slider, |
898 | slider => $slider, |
811 | @_, |
899 | @_, |
812 | ); |
900 | ); |
813 | |
901 | |
814 | $self->{vp}->add ($self->{scrolled}); |
902 | $self->{vp}->add ($self->{scrolled}); |
815 | $self->add ($self->{vp}); |
903 | $self->add ($self->{vp}); |
816 | $self->add ($self->{slider}); |
904 | $self->add ($self->{slider}); |
817 | |
905 | |
818 | $self |
906 | $self |
|
|
907 | } |
|
|
908 | |
|
|
909 | sub update { |
|
|
910 | my ($self) = @_; |
|
|
911 | |
|
|
912 | $self->SUPER::update; |
|
|
913 | |
|
|
914 | # todo: overwrite size_allocate of child |
|
|
915 | my $child = $self->{vp}->child; |
|
|
916 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
|
|
917 | } |
|
|
918 | |
|
|
919 | sub size_allocate { |
|
|
920 | my ($self, $w, $h) = @_; |
|
|
921 | |
|
|
922 | $self->SUPER::size_allocate ($w, $h); |
|
|
923 | |
|
|
924 | my $child = $self->{vp}->child; |
|
|
925 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
819 | } |
926 | } |
820 | |
927 | |
821 | #TODO# update range on size_allocate depending on child |
928 | #TODO# update range on size_allocate depending on child |
822 | # update viewport offset on scroll |
929 | # update viewport offset on scroll |
823 | |
930 | |
… | |
… | |
877 | my $class = shift; |
984 | my $class = shift; |
878 | |
985 | |
879 | # TODO: user_x, user_y, overwrite moveto? |
986 | # TODO: user_x, user_y, overwrite moveto? |
880 | |
987 | |
881 | my $self = $class->SUPER::new ( |
988 | my $self = $class->SUPER::new ( |
882 | bg => [1, 1, 1, 1], |
989 | bg => [1, 1, 1, 1], |
883 | border_bg => [1, 1, 1, 1], |
990 | border_bg => [1, 1, 1, 1], |
884 | border => 0.6, |
991 | border => 0.6, |
885 | toplevel => 1, |
992 | is_toplevel => 1, |
886 | can_events => 1, |
993 | can_events => 1, |
887 | @_ |
994 | @_ |
888 | ); |
995 | ); |
889 | |
996 | |
890 | $self->{title} &&= new CFClient::UI::Label |
997 | $self->{title} &&= new CFClient::UI::Label |
891 | align => 0, |
998 | align => 0, |
… | |
… | |
944 | my ($ev, $x, $y) = @_; |
1051 | my ($ev, $x, $y) = @_; |
945 | |
1052 | |
946 | my $dx = $ev->{x} - $ox; |
1053 | my $dx = $ev->{x} - $ox; |
947 | my $dy = $ev->{y} - $oy; |
1054 | my $dy = $ev->{y} - $oy; |
948 | |
1055 | |
|
|
1056 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1057 | $self->{user_y} = $wy + $dy * $my; |
949 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1058 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
950 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1059 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
951 | $self->move ($wx + $dx * $mx, $wy + $dy * $my); |
1060 | $self->move ($self->{user_x}, $self->{user_y}); |
952 | $self->check_size; |
1061 | $self->check_size; |
953 | }; |
1062 | }; |
954 | |
1063 | |
955 | } elsif ($lr ^ $td) { |
1064 | } elsif ($lr ^ $td) { |
956 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1065 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
… | |
… | |
959 | $self->{motion} = sub { |
1068 | $self->{motion} = sub { |
960 | my ($ev, $x, $y) = @_; |
1069 | my ($ev, $x, $y) = @_; |
961 | |
1070 | |
962 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1071 | ($x, $y) = ($ev->{x}, $ev->{y}); |
963 | |
1072 | |
964 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
1073 | $self->{user_x} = $bx + $x - $ox; |
|
|
1074 | $self->{user_y} = $by + $y - $oy; |
|
|
1075 | $self->move ($self->{user_x}, $self->{user_y}); |
965 | $self->update; |
1076 | $self->update; |
966 | }; |
1077 | }; |
967 | } |
1078 | } |
968 | } |
1079 | } |
969 | |
1080 | |
… | |
… | |
1031 | sub new { |
1142 | sub new { |
1032 | my $class = shift; |
1143 | my $class = shift; |
1033 | |
1144 | |
1034 | $class->SUPER::new ( |
1145 | $class->SUPER::new ( |
1035 | col_expand => [], |
1146 | col_expand => [], |
1036 | @_ |
1147 | @_, |
1037 | ) |
1148 | ) |
|
|
1149 | } |
|
|
1150 | |
|
|
1151 | sub children { |
|
|
1152 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
1038 | } |
1153 | } |
1039 | |
1154 | |
1040 | sub add { |
1155 | sub add { |
1041 | my ($self, $x, $y, $child) = @_; |
1156 | my ($self, $x, $y, $child) = @_; |
1042 | |
1157 | |
1043 | $child->set_parent ($self); |
1158 | $child->set_parent ($self); |
1044 | $self->{children}[$y][$x] = $child; |
1159 | $self->{children}[$y][$x] = $child; |
1045 | |
1160 | |
1046 | $child->check_size; |
1161 | $self->check_size (1); |
1047 | } |
1162 | } |
1048 | |
1163 | |
1049 | sub children { |
|
|
1050 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1051 | } |
|
|
1052 | |
|
|
1053 | # TODO: move to container class maybe? send childs a signal on removal? |
1164 | # TODO: move to container class maybe? send children a signal on removal? |
1054 | sub clear { |
1165 | sub clear { |
1055 | my ($self) = @_; |
1166 | my ($self) = @_; |
1056 | |
1167 | |
1057 | my @children = $self->children; |
1168 | my @children = $self->children; |
1058 | delete $self->{children}; |
1169 | delete $self->{children}; |
… | |
… | |
1060 | for (@children) { |
1171 | for (@children) { |
1061 | delete $_->{parent}; |
1172 | delete $_->{parent}; |
1062 | $_->hide; |
1173 | $_->hide; |
1063 | } |
1174 | } |
1064 | |
1175 | |
|
|
1176 | $self->check_size (1); |
1065 | $self->update; |
1177 | $self->update; |
1066 | } |
1178 | } |
1067 | |
1179 | |
1068 | sub get_wh { |
1180 | sub get_wh { |
1069 | my ($self) = @_; |
1181 | my ($self) = @_; |
… | |
… | |
1101 | sub size_allocate { |
1213 | sub size_allocate { |
1102 | my ($self, $w, $h) = @_; |
1214 | my ($self, $w, $h) = @_; |
1103 | |
1215 | |
1104 | my ($ws, $hs) = $self->get_wh; |
1216 | my ($ws, $hs) = $self->get_wh; |
1105 | |
1217 | |
1106 | my $req_w = sum @$ws; |
1218 | my $req_w = (sum @$ws) || 1; |
1107 | my $req_h = sum @$hs; |
1219 | my $req_h = (sum @$hs) || 1; |
1108 | |
1220 | |
1109 | # TODO: nicer code && do row_expand |
1221 | # TODO: nicer code && do row_expand |
1110 | my @col_expand = @{$self->{col_expand}}; |
1222 | my @col_expand = @{$self->{col_expand}}; |
1111 | @col_expand = (1) x @$ws unless @col_expand; |
1223 | @col_expand = (1) x @$ws unless @col_expand; |
1112 | my $col_expand = (sum @col_expand) || 1; |
1224 | my $col_expand = (sum @col_expand) || 1; |
… | |
… | |
1473 | active_fg => [0, 0, 0], |
1585 | active_fg => [0, 0, 0], |
1474 | can_hover => 1, |
1586 | can_hover => 1, |
1475 | can_focus => 1, |
1587 | can_focus => 1, |
1476 | valign => 0, |
1588 | valign => 0, |
1477 | can_events => 1, |
1589 | can_events => 1, |
|
|
1590 | #text => ... |
1478 | @_ |
1591 | @_ |
1479 | ) |
1592 | ) |
1480 | } |
1593 | } |
1481 | |
1594 | |
1482 | sub _set_text { |
1595 | sub _set_text { |
… | |
… | |
1492 | $self->{text} = $text; |
1605 | $self->{text} = $text; |
1493 | |
1606 | |
1494 | $text =~ s/./*/g if $self->{hidden}; |
1607 | $text =~ s/./*/g if $self->{hidden}; |
1495 | $self->{layout}->set_text ("$text "); |
1608 | $self->{layout}->set_text ("$text "); |
1496 | |
1609 | |
1497 | $self->emit (changed => $self->{text}); |
1610 | $self->_emit (changed => $self->{text}); |
1498 | } |
1611 | } |
1499 | |
1612 | |
1500 | sub set_text { |
1613 | sub set_text { |
1501 | my ($self, $text) = @_; |
1614 | my ($self, $text) = @_; |
1502 | |
1615 | |
… | |
… | |
1514 | my ($self) = @_; |
1627 | my ($self) = @_; |
1515 | |
1628 | |
1516 | my ($w, $h) = $self->SUPER::size_request; |
1629 | my ($w, $h) = $self->SUPER::size_request; |
1517 | |
1630 | |
1518 | ($w + 1, $h) # add 1 for cursor |
1631 | ($w + 1, $h) # add 1 for cursor |
1519 | } |
|
|
1520 | |
|
|
1521 | sub size_allocate { |
|
|
1522 | my ($self, $w, $h) = @_; |
|
|
1523 | |
|
|
1524 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
|
|
1525 | } |
1632 | } |
1526 | |
1633 | |
1527 | sub key_down { |
1634 | sub key_down { |
1528 | my ($self, $ev) = @_; |
1635 | my ($self, $ev) = @_; |
1529 | |
1636 | |
… | |
… | |
1544 | } elsif ($sym == CFClient::SDLK_HOME) { |
1651 | } elsif ($sym == CFClient::SDLK_HOME) { |
1545 | $self->{cursor} = 0; |
1652 | $self->{cursor} = 0; |
1546 | } elsif ($sym == CFClient::SDLK_END) { |
1653 | } elsif ($sym == CFClient::SDLK_END) { |
1547 | $self->{cursor} = length $text; |
1654 | $self->{cursor} = length $text; |
1548 | } elsif ($uni == 27) { |
1655 | } elsif ($uni == 27) { |
1549 | $self->emit ('escape'); |
1656 | $self->_emit ('escape'); |
1550 | } elsif ($uni) { |
1657 | } elsif ($uni) { |
1551 | substr $text, $self->{cursor}++, 0, chr $uni; |
1658 | substr $text, $self->{cursor}++, 0, chr $uni; |
1552 | } |
1659 | } |
1553 | |
1660 | |
1554 | $self->_set_text ($text); |
1661 | $self->_set_text ($text); |
… | |
… | |
1641 | if ($sym == 13) { |
1748 | if ($sym == 13) { |
1642 | unshift @{$self->{history}}, |
1749 | unshift @{$self->{history}}, |
1643 | my $txt = $self->get_text; |
1750 | my $txt = $self->get_text; |
1644 | $self->{history_pointer} = -1; |
1751 | $self->{history_pointer} = -1; |
1645 | $self->{history_saveback} = ''; |
1752 | $self->{history_saveback} = ''; |
1646 | $self->emit (activate => $txt); |
1753 | $self->_emit (activate => $txt); |
1647 | $self->update; |
1754 | $self->update; |
1648 | |
1755 | |
1649 | } elsif ($sym == CFClient::SDLK_UP) { |
1756 | } elsif ($sym == CFClient::SDLK_UP) { |
1650 | if ($self->{history_pointer} < 0) { |
1757 | if ($self->{history_pointer} < 0) { |
1651 | $self->{history_saveback} = $self->get_text; |
1758 | $self->{history_saveback} = $self->get_text; |
… | |
… | |
1699 | can_events => 1, |
1806 | can_events => 1, |
1700 | @_ |
1807 | @_ |
1701 | ) |
1808 | ) |
1702 | } |
1809 | } |
1703 | |
1810 | |
|
|
1811 | sub activate { } |
|
|
1812 | |
1704 | sub button_up { |
1813 | sub button_up { |
1705 | my ($self, $ev, $x, $y) = @_; |
1814 | my ($self, $ev, $x, $y) = @_; |
1706 | |
1815 | |
|
|
1816 | $self->emit ("activate") |
1707 | if ($x >= 0 && $x < $self->{w} |
1817 | if $x >= 0 && $x < $self->{w} |
1708 | && $y >= 0 && $y < $self->{h}) { |
1818 | && $y >= 0 && $y < $self->{h}; |
1709 | $self->emit ("activate"); |
|
|
1710 | } |
|
|
1711 | } |
1819 | } |
1712 | |
1820 | |
1713 | sub _draw { |
1821 | sub _draw { |
1714 | my ($self) = @_; |
1822 | my ($self) = @_; |
1715 | |
1823 | |
… | |
… | |
1767 | my ($self, $ev, $x, $y) = @_; |
1875 | my ($self, $ev, $x, $y) = @_; |
1768 | |
1876 | |
1769 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1877 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1770 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1878 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1771 | $self->{state} = !$self->{state}; |
1879 | $self->{state} = !$self->{state}; |
1772 | $self->emit (changed => $self->{state}); |
1880 | $self->_emit (changed => $self->{state}); |
1773 | } |
1881 | } |
1774 | } |
1882 | } |
1775 | |
1883 | |
1776 | sub _draw { |
1884 | sub _draw { |
1777 | my ($self) = @_; |
1885 | my ($self) = @_; |
… | |
… | |
2052 | my $self = $class->SUPER::new ( |
2160 | my $self = $class->SUPER::new ( |
2053 | fg => [1, 1, 1], |
2161 | fg => [1, 1, 1], |
2054 | active_fg => [0, 0, 0], |
2162 | active_fg => [0, 0, 0], |
2055 | bg => [0, 0, 0, 0.2], |
2163 | bg => [0, 0, 0, 0.2], |
2056 | active_bg => [1, 1, 1, 0.5], |
2164 | active_bg => [1, 1, 1, 0.5], |
2057 | range => [0, 0, 100, 10], |
2165 | range => [0, 0, 100, 10, 0], |
2058 | req_w => $::WIDTH / 80, |
2166 | req_w => $::WIDTH / 80, |
2059 | req_h => $::WIDTH / 80, |
2167 | req_h => $::WIDTH / 80, |
2060 | vertical => 0, |
2168 | vertical => 0, |
2061 | can_hover => 1, |
2169 | can_hover => 1, |
2062 | inner_pad => .05, |
2170 | inner_pad => 0.02, |
2063 | @_ |
2171 | @_ |
2064 | ); |
2172 | ); |
2065 | |
2173 | |
2066 | $self->set_value ($self->{range}[0]); |
2174 | $self->set_value ($self->{range}[0]); |
2067 | $self->update; |
2175 | $self->update; |
2068 | |
2176 | |
2069 | $self |
2177 | $self |
2070 | } |
2178 | } |
2071 | |
2179 | |
|
|
2180 | sub set_range { |
|
|
2181 | my ($self, $range) = @_; |
|
|
2182 | |
|
|
2183 | ($range, $self->{range}) = ($self->{range}, $range); |
|
|
2184 | |
|
|
2185 | $self->update |
|
|
2186 | if "@$range" ne "@{$self->{range}}"; |
|
|
2187 | } |
|
|
2188 | |
2072 | sub set_value { |
2189 | sub set_value { |
2073 | my ($self, $value) = @_; |
2190 | my ($self, $value) = @_; |
2074 | |
2191 | |
2075 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
2192 | my ($old_value, $lo, $hi, $page, $unit) = @{$self->{range}}; |
2076 | |
2193 | |
2077 | $hi = $lo + 1 if $hi <= $lo; |
2194 | $hi = $lo + 1 if $hi <= $lo; |
2078 | |
2195 | |
|
|
2196 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2197 | |
2079 | $value = $lo if $value < $lo; |
2198 | $value = $lo if $value < $lo; |
2080 | $value = $hi if $value > $hi; |
2199 | $value = $hi - $page if $value > $hi - $page; |
2081 | |
2200 | |
2082 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
2201 | $value = $lo + $unit * int +($value - $lo + $unit * 0.5) / $unit |
2083 | if $unit; |
2202 | if $unit; |
2084 | |
2203 | |
2085 | $page = $hi - $lo if $page > $hi - $lo; |
|
|
2086 | |
|
|
2087 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
2204 | @{$self->{range}} = ($value, $lo, $hi, $page, $unit); |
2088 | |
2205 | |
2089 | if ($value != $old_value) { |
2206 | if ($value != $old_value) { |
2090 | $self->emit (changed => $value); |
2207 | $self->_emit (changed => $value); |
2091 | $self->update; |
2208 | $self->update; |
2092 | } |
2209 | } |
2093 | } |
2210 | } |
2094 | |
2211 | |
2095 | sub size_request { |
2212 | sub size_request { |
… | |
… | |
2103 | |
2220 | |
2104 | sub button_down { |
2221 | sub button_down { |
2105 | my ($self, $ev, $x, $y) = @_; |
2222 | my ($self, $ev, $x, $y) = @_; |
2106 | |
2223 | |
2107 | $self->SUPER::button_down ($ev, $x, $y); |
2224 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
2225 | |
|
|
2226 | $self->{click} = [$self->{range}[0], $self->{vertical} ? $y : $x]; |
|
|
2227 | |
2108 | $self->mouse_motion ($ev, $x, $y); |
2228 | $self->mouse_motion ($ev, $x, $y); |
2109 | } |
2229 | } |
2110 | |
2230 | |
2111 | sub mouse_motion { |
2231 | sub mouse_motion { |
2112 | my ($self, $ev, $x, $y) = @_; |
2232 | my ($self, $ev, $x, $y) = @_; |
… | |
… | |
2114 | if ($GRAB == $self) { |
2234 | if ($GRAB == $self) { |
2115 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2235 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
2116 | |
2236 | |
2117 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2237 | my (undef, $lo, $hi, $page) = @{$self->{range}}; |
2118 | |
2238 | |
2119 | $x = $x / ($w * (1 - 2 * $self->{inner_pad})) - $self->{inner_pad}; |
2239 | $x = ($x - $self->{click}[1]) / ($w * $self->{scale}); |
2120 | |
2240 | |
2121 | $self->set_value ($x * ($hi - $lo) + $lo); |
2241 | $self->set_value ($self->{click}[0] + $x * ($hi - $page - $lo)); |
2122 | } |
2242 | } |
2123 | } |
2243 | } |
2124 | |
2244 | |
2125 | sub update { |
2245 | sub update { |
2126 | my ($self) = @_; |
2246 | my ($self) = @_; |
2127 | |
2247 | |
2128 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
2248 | $CFClient::UI::ROOT->on_post_alloc ($self => sub { |
2129 | $self->set_value ($self->{range}[0]); |
2249 | $self->set_value ($self->{range}[0]); |
2130 | |
2250 | |
2131 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
2251 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
2252 | my $range = ($hi - $page - $lo) || 1e-100; |
2132 | |
2253 | |
2133 | my $inner_w = 1 - 2 * $self->{inner_pad}; |
2254 | my $knob_w = List::Util::min 1, $page / ($hi - $lo) || 0.1; |
2134 | |
2255 | |
2135 | $self->{scale} = ($inner_w / ($hi - $lo)) || 1; |
2256 | $self->{offset} = List::Util::max $self->{inner_pad}, $knob_w * 0.5; |
|
|
2257 | $self->{scale} = 1 - 2 * $self->{offset} || 1e-100; |
2136 | |
2258 | |
2137 | $page = $self->{scale} * $page || 10 / ($self->{w} || 1); |
2259 | $value = ($value - $lo) / $range; |
2138 | $value = $self->{scale} * ($value - $lo); |
2260 | $value = $value * $self->{scale} + $self->{offset}; |
2139 | |
2261 | |
2140 | $value = $self->{inner_pad} + ($value - $page * 0.5); |
|
|
2141 | |
|
|
2142 | $value = 0 if $value < 0; |
|
|
2143 | $page = 1 - $value if $value + $page > 1; |
|
|
2144 | |
|
|
2145 | $self->{knob_x} = $value; |
2262 | $self->{knob_x} = $value - $knob_w * 0.5; |
2146 | $self->{knob_w} = $page; |
2263 | $self->{knob_w} = $knob_w; |
2147 | }); |
2264 | }); |
2148 | |
2265 | |
2149 | $self->SUPER::update; |
2266 | $self->SUPER::update; |
2150 | } |
2267 | } |
2151 | |
2268 | |
… | |
… | |
2175 | # draw handle |
2292 | # draw handle |
2176 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2293 | $tex[0]->draw_quad_alpha ($self->{knob_x}, 0, $self->{knob_w}, 1); |
2177 | |
2294 | |
2178 | glDisable GL_TEXTURE_2D; |
2295 | glDisable GL_TEXTURE_2D; |
2179 | } |
2296 | } |
|
|
2297 | |
|
|
2298 | ############################################################################# |
|
|
2299 | |
|
|
2300 | package CFClient::UI::ValSlider; |
|
|
2301 | |
|
|
2302 | our @ISA = CFClient::UI::HBox::; |
|
|
2303 | |
|
|
2304 | sub new { |
|
|
2305 | my ($class, %arg) = @_; |
|
|
2306 | |
|
|
2307 | my $range = delete $arg{range}; |
|
|
2308 | |
|
|
2309 | my $self = $class->SUPER::new ( |
|
|
2310 | slider => (new CFClient::UI::Slider expand => 1, range => $range), |
|
|
2311 | entry => (new CFClient::UI::Label text => "", template => delete $arg{template}), |
|
|
2312 | to_value => sub { shift }, |
|
|
2313 | from_value => sub { shift }, |
|
|
2314 | %arg, |
|
|
2315 | ); |
|
|
2316 | |
|
|
2317 | $self->{slider}->connect (changed => sub { |
|
|
2318 | my ($self, $value) = @_; |
|
|
2319 | $self->{parent}{entry}->set_text ($self->{parent}{to_value}->($value)); |
|
|
2320 | $self->{parent}->emit (changed => $value); |
|
|
2321 | }); |
|
|
2322 | |
|
|
2323 | # $self->{entry}->connect (changed => sub { |
|
|
2324 | # my ($self, $value) = @_; |
|
|
2325 | # $self->{parent}{slider}->set_value ($self->{parent}{from_value}->($value)); |
|
|
2326 | # $self->{parent}->emit (changed => $value); |
|
|
2327 | # }); |
|
|
2328 | |
|
|
2329 | $self->add ($self->{slider}, $self->{entry}); |
|
|
2330 | |
|
|
2331 | $self->{slider}->emit (changed => $self->{slider}{range}[0]); |
|
|
2332 | |
|
|
2333 | $self |
|
|
2334 | } |
|
|
2335 | |
|
|
2336 | sub set_range { shift->{slider}->set_range (@_) } |
|
|
2337 | sub set_value { shift->{slider}->set_value (@_) } |
2180 | |
2338 | |
2181 | ############################################################################# |
2339 | ############################################################################# |
2182 | |
2340 | |
2183 | package CFClient::UI::TextView; |
2341 | package CFClient::UI::TextView; |
2184 | |
2342 | |
… | |
… | |
2214 | |
2372 | |
2215 | $self->{fontsize} = $fontsize; |
2373 | $self->{fontsize} = $fontsize; |
2216 | $self->reflow; |
2374 | $self->reflow; |
2217 | } |
2375 | } |
2218 | |
2376 | |
2219 | sub text_height { |
|
|
2220 | my ($self, $text) = @_; |
|
|
2221 | |
|
|
2222 | my $layout = $self->{layout}; |
|
|
2223 | |
|
|
2224 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
2225 | $layout->set_width ($self->{children}[0]{w}); |
|
|
2226 | $layout->set_markup ($text); |
|
|
2227 | |
|
|
2228 | ($layout->size)[1] |
|
|
2229 | } |
|
|
2230 | |
|
|
2231 | sub reflow { |
|
|
2232 | my ($self) = @_; |
|
|
2233 | |
|
|
2234 | $self->{need_reflow}++; |
|
|
2235 | $self->update; |
|
|
2236 | } |
|
|
2237 | |
|
|
2238 | sub size_allocate { |
2377 | sub size_allocate { |
2239 | my ($self, $w, $h) = @_; |
2378 | my ($self, $w, $h) = @_; |
2240 | |
2379 | |
2241 | $self->SUPER::size_allocate ($w, $h); |
2380 | $self->SUPER::size_allocate ($w, $h); |
2242 | |
2381 | |
… | |
… | |
2245 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2384 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2246 | |
2385 | |
2247 | $self->reflow; |
2386 | $self->reflow; |
2248 | } |
2387 | } |
2249 | |
2388 | |
|
|
2389 | sub text_size { |
|
|
2390 | my ($self, $text, $indent) = @_; |
|
|
2391 | |
|
|
2392 | my $layout = $self->{layout}; |
|
|
2393 | |
|
|
2394 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
2395 | $layout->set_width ($self->{children}[0]{w} - $indent); |
|
|
2396 | $layout->set_markup ($text); |
|
|
2397 | |
|
|
2398 | $layout->size |
|
|
2399 | } |
|
|
2400 | |
|
|
2401 | sub reflow { |
|
|
2402 | my ($self) = @_; |
|
|
2403 | |
|
|
2404 | $self->{need_reflow}++; |
|
|
2405 | $self->update; |
|
|
2406 | } |
|
|
2407 | |
|
|
2408 | sub set_offset { |
|
|
2409 | my ($self, $offset) = @_; |
|
|
2410 | |
|
|
2411 | # todo: base offset on lines or so, not on pixels |
|
|
2412 | $self->{children}[1]->set_value ($offset); |
|
|
2413 | } |
|
|
2414 | |
|
|
2415 | sub clear { |
|
|
2416 | my ($self) = @_; |
|
|
2417 | |
|
|
2418 | $self->{par} = []; |
|
|
2419 | $self->{height} = 0; |
|
|
2420 | $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); |
|
|
2421 | } |
|
|
2422 | |
2250 | sub add_paragraph { |
2423 | sub add_paragraph { |
2251 | my ($self, $color, $text) = @_; |
2424 | my ($self, $color, $text, $indent) = @_; |
2252 | |
2425 | |
2253 | #TODO: intelligently "reformat" paragraph |
2426 | for my $line (split /\n/, $text) { |
2254 | |
2427 | my ($w, $h) = $self->text_size ($line); |
2255 | my $height = $self->text_height ($text); |
|
|
2256 | |
|
|
2257 | $self->{height} += $height; |
2428 | $self->{height} += $h; |
|
|
2429 | push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line]; |
|
|
2430 | } |
2258 | |
2431 | |
2259 | push @{$self->{par}}, [$height, $color, $text]; |
|
|
2260 | |
|
|
2261 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
2432 | $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); |
2262 | $self->{children}[1]->update; |
|
|
2263 | } |
2433 | } |
2264 | |
2434 | |
2265 | sub update { |
2435 | sub update { |
2266 | my ($self) = @_; |
2436 | my ($self) = @_; |
2267 | |
2437 | |
… | |
… | |
2270 | return unless $self->{h} > 0; |
2440 | return unless $self->{h} > 0; |
2271 | |
2441 | |
2272 | delete $self->{texture}; |
2442 | delete $self->{texture}; |
2273 | |
2443 | |
2274 | $ROOT->on_post_alloc ($self, sub { |
2444 | $ROOT->on_post_alloc ($self, sub { |
|
|
2445 | my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; |
|
|
2446 | |
2275 | if (delete $self->{need_reflow}) { |
2447 | if (delete $self->{need_reflow}) { |
2276 | my $height = 0; |
2448 | my $height = 0; |
2277 | |
2449 | |
2278 | $height += $_->[0] = $self->text_height ($_->[2]) |
2450 | my $layout = $self->{layout}; |
|
|
2451 | |
|
|
2452 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
2453 | |
2279 | for @{$self->{par}}; |
2454 | for (@{$self->{par}}) { |
|
|
2455 | if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support |
|
|
2456 | $layout->set_width ($W - $_->[3]); |
|
|
2457 | $layout->set_markup ($_->[4]); |
|
|
2458 | my ($w, $h) = $layout->size; |
|
|
2459 | $_->[0] = $w + $_->[3]; |
|
|
2460 | $_->[1] = $h; |
|
|
2461 | } |
|
|
2462 | |
|
|
2463 | $height += $_->[1]; |
|
|
2464 | } |
2280 | |
2465 | |
2281 | $self->{height} = $height; |
2466 | $self->{height} = $height; |
2282 | |
2467 | |
2283 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
2468 | $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); |
2284 | |
2469 | |
2285 | delete $self->{texture}; |
2470 | delete $self->{texture}; |
2286 | } |
2471 | } |
2287 | |
2472 | |
2288 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{children}[0]{w}, $self->{children}[0]{h}, sub { |
2473 | $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { |
2289 | glClearColor 0, 0, 0, 0; |
2474 | glClearColor 0.5, 0.5, 0.5, 0; |
2290 | glClear GL_COLOR_BUFFER_BIT; |
2475 | glClear GL_COLOR_BUFFER_BIT; |
2291 | |
2476 | |
2292 | glEnable GL_TEXTURE_2D; |
|
|
2293 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2294 | |
|
|
2295 | my $top = int $self->{children}[1]{range}[0]; |
2477 | my $top = int $self->{children}[1]{range}[0]; |
2296 | |
2478 | |
2297 | my $y0 = $top; |
2479 | my $y0 = $top; |
2298 | my $y1 = $top + $self->{h}; |
2480 | my $y1 = $top + $H; |
2299 | |
2481 | |
2300 | my $y = 0; |
2482 | my $y = 0; |
2301 | |
2483 | |
2302 | my $layout = $self->{layout}; |
2484 | my $layout = $self->{layout}; |
2303 | |
2485 | |
2304 | $layout->set_font ($self->{font}) if $self->{font}; |
2486 | $layout->set_font ($self->{font}) if $self->{font}; |
2305 | |
2487 | |
|
|
2488 | glEnable GL_BLEND; |
|
|
2489 | #TODO# not correct in windows where rgba is forced off |
|
|
2490 | glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2491 | |
2306 | for my $par (@{$self->{par}}) { |
2492 | for my $par (@{$self->{par}}) { |
2307 | my $h = $par->[0]; |
2493 | my $h = $par->[1]; |
2308 | |
2494 | |
2309 | if ($y0 < $y + $h && $y < $y1) { |
2495 | if ($y0 < $y + $h && $y < $y1) { |
2310 | $layout->set_foreground (@{ $par->[1] }); |
2496 | $layout->set_foreground (@{ $par->[2] }); |
|
|
2497 | $layout->set_width ($W - $par->[3]); |
2311 | $layout->set_markup ($par->[2]); |
2498 | $layout->set_markup ($par->[4]); |
2312 | |
2499 | |
2313 | my ($W, $H) = $layout->size; |
2500 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
2314 | CFClient::Texture->new_from_layout ($layout)->draw_quad_alpha_premultiplied (0, $y - $y0); |
2501 | |
|
|
2502 | glRasterPos $par->[3], $y - $y0; |
|
|
2503 | glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; |
2315 | } |
2504 | } |
2316 | |
2505 | |
2317 | $y += $h; |
2506 | $y += $h; |
2318 | } |
2507 | } |
2319 | |
2508 | |
2320 | glDisable GL_TEXTURE_2D; |
2509 | glDisable GL_BLEND; |
2321 | }; |
2510 | }; |
2322 | }); |
2511 | }); |
2323 | } |
2512 | } |
2324 | |
2513 | |
2325 | sub _draw { |
2514 | sub _draw { |
2326 | my ($self) = @_; |
2515 | my ($self) = @_; |
2327 | |
2516 | |
2328 | glEnable GL_TEXTURE_2D; |
2517 | glEnable GL_TEXTURE_2D; |
2329 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2518 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2330 | glColor 1, 1, 1, 1; |
2519 | glColor 1, 1, 1, 1; |
2331 | $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2520 | $self->{texture}->draw_quad_alpha (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); |
2332 | glDisable GL_TEXTURE_2D; |
2521 | glDisable GL_TEXTURE_2D; |
2333 | |
2522 | |
2334 | $self->{children}[1]->draw; |
2523 | $self->{children}[1]->draw; |
2335 | |
2524 | |
2336 | } |
2525 | } |
… | |
… | |
2385 | |
2574 | |
2386 | sub new { |
2575 | sub new { |
2387 | my $class = shift; |
2576 | my $class = shift; |
2388 | |
2577 | |
2389 | my $self = $class->SUPER::new ( |
2578 | my $self = $class->SUPER::new ( |
2390 | state => 0, |
2579 | state => 0, |
2391 | connect_activate => \&toggle_flopper, |
2580 | on_activate => \&toggle_flopper, |
2392 | @_ |
2581 | @_ |
2393 | ); |
2582 | ); |
2394 | |
2583 | |
2395 | if ($self->{state}) { |
2584 | if ($self->{state}) { |
2396 | $self->{state} = 0; |
2585 | $self->{state} = 0; |
… | |
… | |
2405 | |
2594 | |
2406 | # TODO: use animation |
2595 | # TODO: use animation |
2407 | if ($self->{state} = !$self->{state}) { |
2596 | if ($self->{state} = !$self->{state}) { |
2408 | $CFClient::UI::ROOT->add ($self->{other}); |
2597 | $CFClient::UI::ROOT->add ($self->{other}); |
2409 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
2598 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
2410 | $self->emit ("open"); |
2599 | $self->_emit ("open"); |
2411 | } else { |
2600 | } else { |
2412 | $CFClient::UI::ROOT->remove ($self->{other}); |
2601 | $CFClient::UI::ROOT->remove ($self->{other}); |
2413 | $self->emit ("close"); |
2602 | $self->_emit ("close"); |
2414 | } |
2603 | } |
2415 | |
2604 | |
2416 | $self->emit (changed => $self->{state}); |
2605 | $self->_emit (changed => $self->{state}); |
2417 | } |
2606 | } |
2418 | |
2607 | |
2419 | ############################################################################# |
2608 | ############################################################################# |
2420 | |
2609 | |
2421 | package CFClient::UI::Tooltip; |
2610 | package CFClient::UI::Tooltip; |
… | |
… | |
2496 | use CFClient::OpenGL; |
2685 | use CFClient::OpenGL; |
2497 | |
2686 | |
2498 | sub new { |
2687 | sub new { |
2499 | my $class = shift; |
2688 | my $class = shift; |
2500 | |
2689 | |
2501 | $class->SUPER::new ( |
2690 | my $self = $class->SUPER::new ( |
2502 | aspect => 1, |
2691 | aspect => 1, |
|
|
2692 | can_events => 0, |
2503 | @_, |
2693 | @_, |
2504 | ) |
2694 | ); |
|
|
2695 | |
|
|
2696 | if ($self->{anim} && $self->{animspeed}) { |
|
|
2697 | Scalar::Util::weaken (my $widget = $self); |
|
|
2698 | |
|
|
2699 | $self->{timer} = Event->timer ( |
|
|
2700 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
|
|
2701 | hard => 1, |
|
|
2702 | interval => $self->{animspeed}, |
|
|
2703 | cb => sub { |
|
|
2704 | ++$widget->{frame}; |
|
|
2705 | $widget->update; |
|
|
2706 | }, |
|
|
2707 | ); |
|
|
2708 | } |
|
|
2709 | |
|
|
2710 | $self |
2505 | } |
2711 | } |
2506 | |
2712 | |
2507 | sub size_request { |
2713 | sub size_request { |
2508 | (32, 8) |
2714 | (32, 8) |
2509 | } |
2715 | } |
2510 | |
2716 | |
|
|
2717 | sub update { |
|
|
2718 | my ($self) = @_; |
|
|
2719 | |
|
|
2720 | return unless $self->{visible}; |
|
|
2721 | |
|
|
2722 | $self->SUPER::update; |
|
|
2723 | } |
|
|
2724 | |
2511 | sub _draw { |
2725 | sub _draw { |
2512 | my ($self) = @_; |
2726 | my ($self) = @_; |
2513 | |
2727 | |
2514 | return unless $::CONN;#d# manage and cache textures differently |
2728 | return unless $::CONN; |
|
|
2729 | |
|
|
2730 | my $face; |
|
|
2731 | |
|
|
2732 | if ($self->{frame}) { |
|
|
2733 | my $anim = $::CONN->{anim}[$self->{anim}]; |
|
|
2734 | |
|
|
2735 | $face = $anim->[ $self->{frame} % @$anim ] |
|
|
2736 | if $anim && @$anim; |
|
|
2737 | } |
|
|
2738 | |
2515 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2739 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; |
2516 | |
2740 | |
2517 | # TODO animation |
|
|
2518 | if ($tex) { |
2741 | if ($tex) { |
2519 | glEnable GL_TEXTURE_2D; |
2742 | glEnable GL_TEXTURE_2D; |
2520 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2743 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
2521 | glColor 1, 1, 1, 1; |
2744 | glColor 1, 1, 1, 1; |
2522 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2745 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
2523 | glDisable GL_TEXTURE_2D; |
2746 | glDisable GL_TEXTURE_2D; |
2524 | } |
2747 | } |
2525 | } |
2748 | } |
2526 | |
2749 | |
|
|
2750 | sub DESTROY { |
|
|
2751 | my ($self) = @_; |
|
|
2752 | |
|
|
2753 | $self->{timer}->cancel |
|
|
2754 | if $self->{timer}; |
|
|
2755 | |
|
|
2756 | $self->SUPER::DESTROY; |
|
|
2757 | } |
|
|
2758 | |
2527 | ############################################################################# |
2759 | ############################################################################# |
2528 | |
2760 | |
2529 | package CFClient::UI::InventoryItem; |
2761 | package CFClient::UI::Inventory; |
2530 | |
2762 | |
2531 | our @ISA = CFClient::UI::HBox::; |
2763 | our @ISA = CFClient::UI::ScrolledWindow::; |
2532 | |
|
|
2533 | sub _item_to_desc { |
|
|
2534 | my ($item) = @_; |
|
|
2535 | |
|
|
2536 | my $desc = |
|
|
2537 | $item->{nrof} < 2 |
|
|
2538 | ? $item->{name} |
|
|
2539 | : "$item->{nrof} $item->{name_pl}"; |
|
|
2540 | |
|
|
2541 | $item->{flags} & Crossfire::Protocol::F_OPEN |
|
|
2542 | and $desc .= " (open)"; |
|
|
2543 | $item->{flags} & Crossfire::Protocol::F_APPLIED |
|
|
2544 | and $desc .= " (applied)"; |
|
|
2545 | $item->{flags} & Crossfire::Protocol::F_UNPAID |
|
|
2546 | and $desc .= " (unpaid)"; |
|
|
2547 | $item->{flags} & Crossfire::Protocol::F_MAGIC |
|
|
2548 | and $desc .= " (magic)"; |
|
|
2549 | $item->{flags} & Crossfire::Protocol::F_CURSED |
|
|
2550 | and $desc .= " (cursed)"; |
|
|
2551 | $item->{flags} & Crossfire::Protocol::F_DAMNED |
|
|
2552 | and $desc .= " (damned)"; |
|
|
2553 | $item->{flags} & Crossfire::Protocol::F_LOCKED |
|
|
2554 | and $desc .= " *"; |
|
|
2555 | |
|
|
2556 | $desc |
|
|
2557 | } |
|
|
2558 | |
2764 | |
2559 | sub new { |
2765 | sub new { |
2560 | my $class = shift; |
2766 | my $class = shift; |
2561 | |
2767 | |
2562 | my %args = @_; |
|
|
2563 | |
|
|
2564 | my $item = delete $args{item}; |
|
|
2565 | |
|
|
2566 | my $desc = _item_to_desc ($item); |
|
|
2567 | |
|
|
2568 | my $self = $class->SUPER::new ( |
2768 | my $self = $class->SUPER::new ( |
2569 | can_hover => 1, |
2769 | scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]), |
2570 | can_events => 1, |
|
|
2571 | tooltip => ((CFClient::UI::Label::escape $desc) |
|
|
2572 | . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"), |
|
|
2573 | connect_button_down => sub { |
|
|
2574 | my ($self, $ev, $x, $y) = @_; |
|
|
2575 | |
|
|
2576 | # todo: maybe put examine on 1? but should just be a tooltip :( |
|
|
2577 | if ($ev->{button} == 1) { |
|
|
2578 | my $targ = $::CONN->{player}{tag}; |
|
|
2579 | |
|
|
2580 | if ($item->{container} == $::CONN->{player}{tag}) { |
|
|
2581 | $targ = $main::OPENCONT; |
|
|
2582 | } |
|
|
2583 | |
|
|
2584 | $::CONN->send ("move $targ $item->{tag} 0"); |
|
|
2585 | } elsif ($ev->{button} == 2) { |
|
|
2586 | $::CONN->send ("apply $item->{tag}"); |
|
|
2587 | } elsif ($ev->{button} == 3) { |
|
|
2588 | my @menu_items = ( |
|
|
2589 | ["examine", sub { $::CONN->send ("examine $item->{tag}") }], |
|
|
2590 | ["mark", sub { $::CONN->send ("mark $item->{tag}") }], |
|
|
2591 | ["apply", sub { $::CONN->send ("apply $item->{tag}") }], |
|
|
2592 | ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }], |
|
|
2593 | [ |
|
|
2594 | $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock", |
|
|
2595 | sub { $::CONN->send ("lock $item->{tag}") }, |
|
|
2596 | ], |
|
|
2597 | ); |
|
|
2598 | |
|
|
2599 | CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev); |
|
|
2600 | } |
|
|
2601 | |
|
|
2602 | 1 |
|
|
2603 | }, |
|
|
2604 | %args |
|
|
2605 | ); |
|
|
2606 | |
|
|
2607 | |
|
|
2608 | $self->add (new CFClient::UI::Face |
|
|
2609 | can_events => 0, |
|
|
2610 | face => $item->{face}, |
|
|
2611 | anim => $item->{anim}, |
|
|
2612 | animspeed => $item->{animspeed}, |
|
|
2613 | ); |
|
|
2614 | |
|
|
2615 | $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0); |
|
|
2616 | |
|
|
2617 | $self->{item} = $item; |
|
|
2618 | |
|
|
2619 | $self->update_item; |
|
|
2620 | |
|
|
2621 | $self |
|
|
2622 | } |
|
|
2623 | |
|
|
2624 | sub update_item { |
|
|
2625 | my ($self) = @_; |
|
|
2626 | |
|
|
2627 | my $desc = _item_to_desc ($self->{item}); |
|
|
2628 | |
|
|
2629 | $self->{name_lbl}->set_text ($desc); |
|
|
2630 | } |
|
|
2631 | |
|
|
2632 | ############################################################################# |
|
|
2633 | |
|
|
2634 | package CFClient::UI::Inventory; |
|
|
2635 | |
|
|
2636 | our @ISA = CFClient::UI::ScrolledWindow::; |
|
|
2637 | |
|
|
2638 | sub new { |
|
|
2639 | my $class = shift; |
|
|
2640 | |
|
|
2641 | my $self = $class->SUPER::new ( |
|
|
2642 | scrolled => (new CFClient::UI::VBox), |
|
|
2643 | @_, |
2770 | @_, |
2644 | ); |
2771 | ); |
2645 | |
2772 | |
2646 | $self |
2773 | $self |
2647 | } |
2774 | } |
… | |
… | |
2657 | or ($a->{name} cmp $b->{name}) |
2784 | or ($a->{name} cmp $b->{name}) |
2658 | } @$items; |
2785 | } @$items; |
2659 | |
2786 | |
2660 | $self->{real_items} = \@items; |
2787 | $self->{real_items} = \@items; |
2661 | |
2788 | |
|
|
2789 | my $row = 0; |
2662 | for my $item (@items) { |
2790 | for my $item (@items) { |
2663 | $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item; |
2791 | CFClient::Item::update_widgets $item; |
2664 | $item->update_item (); |
|
|
2665 | } |
|
|
2666 | |
2792 | |
2667 | $self->{scrolled}->add (@items); |
2793 | $self->{scrolled}->add (0, $row, $item->{face_widget}); |
|
|
2794 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
|
|
2795 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2668 | |
2796 | |
2669 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
2797 | $row++; |
|
|
2798 | } |
2670 | } |
2799 | } |
2671 | |
2800 | |
2672 | sub size_request { |
2801 | sub size_request { |
2673 | my ($self) = @_; |
2802 | my ($self) = @_; |
2674 | ($self->{req_w}, $self->{req_h}); |
2803 | ($self->{req_w}, $self->{req_h}); |
… | |
… | |
2714 | |
2843 | |
2715 | # popup given the event (must be a mouse button down event currently) |
2844 | # popup given the event (must be a mouse button down event currently) |
2716 | sub popup { |
2845 | sub popup { |
2717 | my ($self, $ev) = @_; |
2846 | my ($self, $ev) = @_; |
2718 | |
2847 | |
2719 | $self->emit ("popdown"); |
2848 | $self->_emit ("popdown"); |
2720 | |
2849 | |
2721 | # maybe save $GRAB? must be careful about events... |
2850 | # maybe save $GRAB? must be careful about events... |
2722 | $GRAB = $self; |
2851 | $GRAB = $self; |
2723 | $self->{button} = $ev->{button}; |
2852 | $self->{button} = $ev->{button}; |
2724 | |
2853 | |
… | |
… | |
2739 | |
2868 | |
2740 | if ($ev->{button} == $self->{button}) { |
2869 | if ($ev->{button} == $self->{button}) { |
2741 | undef $GRAB; |
2870 | undef $GRAB; |
2742 | $self->hide; |
2871 | $self->hide; |
2743 | |
2872 | |
2744 | $self->emit ("popdown"); |
2873 | $self->_emit ("popdown"); |
2745 | $self->{hover}[1]->() if $self->{hover}; |
2874 | $self->{hover}[1]->() if $self->{hover}; |
2746 | } |
2875 | } |
2747 | } |
2876 | } |
2748 | |
2877 | |
2749 | ############################################################################# |
2878 | ############################################################################# |
… | |
… | |
2811 | sub add { |
2940 | sub add { |
2812 | my ($self, $text, %arg) = @_; |
2941 | my ($self, $text, %arg) = @_; |
2813 | |
2942 | |
2814 | $text =~ s/^\s+//; |
2943 | $text =~ s/^\s+//; |
2815 | $text =~ s/\s+$//; |
2944 | $text =~ s/\s+$//; |
|
|
2945 | |
|
|
2946 | return unless $text; |
2816 | |
2947 | |
2817 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2948 | my $timeout = time + ((delete $arg{timeout}) || 60); |
2818 | |
2949 | |
2819 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2950 | my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; |
2820 | |
2951 | |
… | |
… | |
2864 | |
2995 | |
2865 | sub new { |
2996 | sub new { |
2866 | my $class = shift; |
2997 | my $class = shift; |
2867 | |
2998 | |
2868 | $class->SUPER::new ( |
2999 | $class->SUPER::new ( |
|
|
3000 | visible => 1, |
2869 | @_, |
3001 | @_, |
2870 | ) |
3002 | ) |
2871 | } |
3003 | } |
2872 | |
3004 | |
2873 | sub configure { |
3005 | sub configure { |
… | |
… | |
2891 | } |
3023 | } |
2892 | |
3024 | |
2893 | sub size_allocate { |
3025 | sub size_allocate { |
2894 | my ($self, $w, $h) = @_; |
3026 | my ($self, $w, $h) = @_; |
2895 | |
3027 | |
2896 | my $old_w = $self->{old_w}; $self->{old_w} = $w; |
|
|
2897 | my $old_h = $self->{old_h}; $self->{old_h} = $h; |
|
|
2898 | |
|
|
2899 | CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h |
|
|
2900 | if $old_w && $old_h && ($old_w != $w || $old_h != $h); |
|
|
2901 | |
|
|
2902 | for my $child ($self->children) { |
3028 | for my $child ($self->children) { |
2903 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
3029 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2904 | |
3030 | |
2905 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
3031 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
2906 | if exists $child->{req_x}; |
3032 | if exists $child->{req_x}; |
… | |
… | |
2935 | } |
3061 | } |
2936 | |
3062 | |
2937 | sub add { |
3063 | sub add { |
2938 | my ($self, @children) = @_; |
3064 | my ($self, @children) = @_; |
2939 | |
3065 | |
|
|
3066 | for (my @widgets = @children; my $w = pop @widgets; ) { |
|
|
3067 | push @widgets, $w->children; |
|
|
3068 | $w->{root} = $self; |
|
|
3069 | $w->{visible} = $self->{visible} + 1; |
|
|
3070 | } |
|
|
3071 | |
2940 | for my $child (@children) { |
3072 | for my $child (@children) { |
2941 | $child->{toplevel} = 1; |
3073 | $child->{is_toplevel} = 1; |
2942 | |
3074 | |
2943 | # integerise window positions |
3075 | # integerise window positions |
2944 | $child->{x} = int $child->{x}; |
3076 | $child->{x} = int $child->{x}; |
2945 | $child->{y} = int $child->{y}; |
3077 | $child->{y} = int $child->{y}; |
2946 | } |
3078 | } |
2947 | |
3079 | |
2948 | $self->SUPER::add (@children); |
3080 | $self->SUPER::add (@children); |
2949 | } |
3081 | } |
2950 | |
3082 | |
|
|
3083 | sub remove { |
|
|
3084 | my ($self, @children) = @_; |
|
|
3085 | |
|
|
3086 | $self->SUPER::remove (@children); |
|
|
3087 | |
|
|
3088 | while (@children) { |
|
|
3089 | my $w = pop @children; |
|
|
3090 | push @children, $w->children; |
|
|
3091 | $w->set_invisible; |
|
|
3092 | } |
|
|
3093 | } |
|
|
3094 | |
2951 | sub on_refresh { |
3095 | sub on_refresh { |
2952 | my ($self, $id, $cb) = @_; |
3096 | my ($self, $id, $cb) = @_; |
2953 | |
3097 | |
2954 | $self->{refresh_hook}{$id} = $cb; |
3098 | $self->{refresh_hook}{$id} = $cb; |
2955 | } |
3099 | } |
… | |
… | |
2967 | $_->() |
3111 | $_->() |
2968 | for values %{delete $self->{refresh_hook}}; |
3112 | for values %{delete $self->{refresh_hook}}; |
2969 | } |
3113 | } |
2970 | |
3114 | |
2971 | if ($self->{check_size}) { |
3115 | if ($self->{check_size}) { |
2972 | my @queue = ([], []); |
3116 | my @queue; |
2973 | |
3117 | |
2974 | for (;;) { |
3118 | for (;;) { |
2975 | if ($self->{check_size}) { |
3119 | if ($self->{check_size}) { |
2976 | # heuristic: check containers last |
3120 | #TODO use array-of-depth approach |
2977 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
3121 | |
|
|
3122 | @queue = sort { $a->{visible} <=> $b->{visible} } |
2978 | for values %{delete $self->{check_size}} |
3123 | @queue, values %{delete $self->{check_size}}; |
2979 | } |
3124 | } |
2980 | |
3125 | |
2981 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
3126 | my $widget = pop @queue || last; |
|
|
3127 | |
|
|
3128 | defined $widget->{visible} or last; # do not resize invisible widgets |
2982 | |
3129 | |
2983 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3130 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
2984 | ? @$widget{qw(user_w user_h)} |
3131 | ? @$widget{qw(user_w user_h)} |
2985 | : $widget->size_request; |
3132 | : $widget->size_request; |
2986 | |
3133 | |
… | |
… | |
3006 | $w = 0 if $w < 0; |
3153 | $w = 0 if $w < 0; |
3007 | $h = 0 if $h < 0; |
3154 | $h = 0 if $h < 0; |
3008 | |
3155 | |
3009 | $widget->{w} = $w; |
3156 | $widget->{w} = $w; |
3010 | $widget->{h} = $h; |
3157 | $widget->{h} = $h; |
3011 | $widget->size_allocate ($w, $h); |
|
|
3012 | $widget->emit (size_allocate => $w, $h); |
3158 | $widget->emit (size_allocate => $w, $h); |
3013 | } |
3159 | } |
3014 | } |
3160 | } |
3015 | |
3161 | |
3016 | while ($self->{post_alloc_hook}) { |
3162 | while ($self->{post_alloc_hook}) { |
… | |
… | |
3022 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
3168 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
3023 | glClear GL_COLOR_BUFFER_BIT; |
3169 | glClear GL_COLOR_BUFFER_BIT; |
3024 | |
3170 | |
3025 | glMatrixMode GL_PROJECTION; |
3171 | glMatrixMode GL_PROJECTION; |
3026 | glLoadIdentity; |
3172 | glLoadIdentity; |
3027 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
3173 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000, 10000; |
3028 | glMatrixMode GL_MODELVIEW; |
3174 | glMatrixMode GL_MODELVIEW; |
3029 | glLoadIdentity; |
3175 | glLoadIdentity; |
3030 | |
3176 | |
3031 | $self->_draw; |
3177 | $self->_draw; |
3032 | } |
3178 | } |