… | |
… | |
7 | |
7 | |
8 | use CFClient; |
8 | use CFClient; |
9 | |
9 | |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
11 | |
11 | |
12 | our $TOPLEVEL; |
12 | our $ROOT; |
|
|
13 | our $TOOLTIP; |
13 | our $BUTTON_STATE; |
14 | our $BUTTON_STATE; |
|
|
15 | |
|
|
16 | sub check_tooltip { |
|
|
17 | if (!$GRAB) { |
|
|
18 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
|
|
19 | if (exists $widget->{tooltip}) { |
|
|
20 | |
|
|
21 | if ($TOOLTIP->{owner} != $widget) { |
|
|
22 | $TOOLTIP->{owner} = $widget; |
|
|
23 | |
|
|
24 | my $tip = $widget->{tooltip}; |
|
|
25 | |
|
|
26 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
|
|
27 | |
|
|
28 | $TOOLTIP->set_text ($widget->{tooltip}); |
|
|
29 | $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0)); |
|
|
30 | $TOOLTIP->show; |
|
|
31 | } |
|
|
32 | |
|
|
33 | return; |
|
|
34 | } |
|
|
35 | } |
|
|
36 | } |
|
|
37 | |
|
|
38 | $TOOLTIP->hide; |
|
|
39 | delete $TOOLTIP->{owner}; |
|
|
40 | } |
14 | |
41 | |
15 | # class methods for events |
42 | # class methods for events |
16 | sub feed_sdl_key_down_event { |
43 | sub feed_sdl_key_down_event { |
17 | $FOCUS->key_down ($_[0]) if $FOCUS; |
44 | $FOCUS->key_down ($_[0]) if $FOCUS; |
18 | } |
45 | } |
… | |
… | |
21 | $FOCUS->key_up ($_[0]) if $FOCUS; |
48 | $FOCUS->key_up ($_[0]) if $FOCUS; |
22 | } |
49 | } |
23 | |
50 | |
24 | sub feed_sdl_button_down_event { |
51 | sub feed_sdl_button_down_event { |
25 | my ($ev) = @_; |
52 | my ($ev) = @_; |
26 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
53 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
27 | |
54 | |
28 | if (!$BUTTON_STATE) { |
55 | if (!$BUTTON_STATE) { |
29 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
56 | my $widget = $ROOT->find_widget ($x, $y); |
30 | |
57 | |
31 | $GRAB = $widget; |
58 | $GRAB = $widget; |
32 | $GRAB->update if $GRAB; |
59 | $GRAB->update if $GRAB; |
33 | } |
|
|
34 | |
60 | |
|
|
61 | check_tooltip; |
|
|
62 | } |
|
|
63 | |
35 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
64 | $BUTTON_STATE |= 1 << ($ev->{button} - 1); |
36 | |
65 | |
37 | $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
66 | $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
38 | } |
67 | } |
39 | |
68 | |
40 | sub feed_sdl_button_up_event { |
69 | sub feed_sdl_button_up_event { |
41 | my ($ev) = @_; |
70 | my ($ev) = @_; |
42 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
71 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
43 | |
72 | |
44 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
73 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
45 | |
74 | |
46 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
75 | $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); |
47 | |
76 | |
48 | $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; |
77 | $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; |
49 | |
78 | |
50 | if (!$BUTTON_STATE) { |
79 | if (!$BUTTON_STATE) { |
51 | my $grab = $GRAB; undef $GRAB; |
80 | my $grab = $GRAB; undef $GRAB; |
52 | $grab->update if $grab; |
81 | $grab->update if $grab; |
53 | $GRAB->update if $GRAB; |
82 | $GRAB->update if $GRAB; |
|
|
83 | |
|
|
84 | check_tooltip; |
54 | } |
85 | } |
55 | } |
86 | } |
56 | |
87 | |
57 | sub feed_sdl_motion_event { |
88 | sub feed_sdl_motion_event { |
58 | my ($ev) = @_; |
89 | my ($ev) = @_; |
59 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
90 | my ($x, $y) = ($ev->{x}, $ev->{y}); |
60 | |
91 | |
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
92 | my $widget = $GRAB || $ROOT->find_widget ($x, $y); |
62 | |
93 | |
63 | if ($widget != $HOVER) { |
94 | if ($widget != $HOVER) { |
64 | my $hover = $HOVER; $HOVER = $widget; |
95 | my $hover = $HOVER; $HOVER = $widget; |
65 | |
96 | |
66 | $hover->update if $hover; |
97 | $hover->update if $hover && $hover->{can_hover}; |
67 | $HOVER->update if $HOVER; |
98 | $HOVER->update if $HOVER && $HOVER->{can_hover}; |
68 | } |
|
|
69 | |
99 | |
|
|
100 | check_tooltip; |
|
|
101 | } |
|
|
102 | |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
103 | $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; |
|
|
104 | } |
|
|
105 | |
|
|
106 | # convert position array to integers |
|
|
107 | sub harmonize { |
|
|
108 | my ($vals) = @_; |
|
|
109 | |
|
|
110 | my $rem = 0; |
|
|
111 | |
|
|
112 | for (@$vals) { |
|
|
113 | my $i = int $_ + $rem; |
|
|
114 | $rem += $_ - $i; |
|
|
115 | $_ = $i; |
|
|
116 | } |
71 | } |
117 | } |
72 | |
118 | |
73 | ############################################################################# |
119 | ############################################################################# |
74 | |
120 | |
75 | package CFClient::UI::Base; |
121 | package CFClient::UI::Base; |
76 | |
122 | |
77 | use strict; |
123 | use strict; |
78 | |
124 | |
79 | use SDL::OpenGL; |
125 | use CFClient::OpenGL; |
80 | |
126 | |
81 | sub new { |
127 | sub new { |
82 | my $class = shift; |
128 | my $class = shift; |
83 | |
129 | |
84 | my $self = bless { |
130 | my $self = bless { |
85 | x => 0, |
131 | x => 0, |
86 | y => 0, |
132 | y => 0, |
87 | z => 0, |
133 | z => 0, |
88 | w => -1, |
134 | can_events => 1, |
89 | h => -1, |
|
|
90 | @_ |
135 | @_ |
91 | }, $class; |
136 | }, $class; |
92 | |
137 | |
93 | for (keys %$self) { |
138 | for (keys %$self) { |
94 | if (/^connect_(.*)$/) { |
139 | if (/^connect_(.*)$/) { |
… | |
… | |
97 | } |
142 | } |
98 | |
143 | |
99 | $self |
144 | $self |
100 | } |
145 | } |
101 | |
146 | |
|
|
147 | sub show { |
|
|
148 | my ($self) = @_; |
|
|
149 | |
|
|
150 | return if $self->{parent}; |
|
|
151 | |
|
|
152 | $CFClient::UI::ROOT->add ($self); |
|
|
153 | } |
|
|
154 | |
|
|
155 | sub hide { |
|
|
156 | my ($self) = @_; |
|
|
157 | |
|
|
158 | return unless $self->{parent}; |
|
|
159 | |
|
|
160 | $self->{parent}->remove ($self); |
|
|
161 | } |
|
|
162 | |
102 | sub move { |
163 | sub move { |
103 | my ($self, $x, $y, $z) = @_; |
164 | my ($self, $x, $y, $z) = @_; |
|
|
165 | |
104 | $self->{x} = $x; |
166 | $self->{x} = int $x; |
105 | $self->{y} = $y; |
167 | $self->{y} = int $y; |
106 | $self->{z} = $z if defined $z; |
168 | $self->{z} = $z if defined $z; |
107 | } |
|
|
108 | |
169 | |
109 | sub needs_redraw { |
170 | $self->update; |
110 | 0 |
171 | } |
|
|
172 | |
|
|
173 | sub set_size { |
|
|
174 | my ($self, $w, $h) = @_; |
|
|
175 | |
|
|
176 | $self->{user_w} = $w; |
|
|
177 | $self->{user_h} = $h; |
|
|
178 | |
|
|
179 | $self->check_size; |
111 | } |
180 | } |
112 | |
181 | |
113 | sub size_request { |
182 | sub size_request { |
114 | require Carp; |
183 | require Carp; |
115 | Carp::confess "size_request is abtract"; |
184 | Carp::confess "size_request is abstract"; |
116 | } |
185 | } |
117 | |
186 | |
118 | sub _size_allocate { |
187 | sub configure { |
119 | my ($self, $x, $y, $w, $h) = @_; |
188 | my ($self, $x, $y, $w, $h) = @_; |
120 | |
189 | |
|
|
190 | if ($self->{aspect}) { |
|
|
191 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
|
|
192 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
|
|
193 | |
|
|
194 | # use alignment to adjust x, y |
|
|
195 | |
|
|
196 | $x += int +($w - $w2) * 0.5; |
|
|
197 | $y += int +($h - $h2) * 0.5; |
|
|
198 | |
|
|
199 | ($w, $h) = ($w2, $h2); |
|
|
200 | } |
|
|
201 | |
|
|
202 | if ($self->{x} != $x || $self->{y} != $y) { |
121 | $self->{x} = int $x; |
203 | $self->{x} = $x; |
122 | $self->{y} = int $y; |
204 | $self->{y} = $y; |
|
|
205 | $self->update; |
|
|
206 | } |
123 | |
207 | |
124 | return unless $self->{w} != $w || $self->{h} != $h; |
208 | if ($self->{w} != $w || $self->{h} != $h) { |
125 | |
|
|
126 | $self->{w} = int $w; |
209 | $self->{w} = $w; |
127 | $self->{h} = int $h; |
210 | $self->{h} = $h; |
128 | |
211 | |
|
|
212 | $self->size_allocate ($w, $h); |
|
|
213 | $self->update; |
129 | 1 |
214 | } |
130 | } |
215 | } |
131 | |
216 | |
132 | sub size_allocate { |
217 | sub size_allocate { |
|
|
218 | # nothing to be done |
|
|
219 | } |
|
|
220 | |
|
|
221 | sub set_max_size { |
133 | my ($self, $x, $y, $w, $h) = @_; |
222 | my ($self, $w, $h) = @_; |
134 | |
223 | |
135 | $self->_size_allocate ($x, $y, $w, $h); |
224 | delete $self->{max_w}; $self->{max_w} = $w if $w; |
|
|
225 | delete $self->{max_h}; $self->{max_h} = $h if $h; |
|
|
226 | } |
|
|
227 | |
|
|
228 | # return top left coordinates |
|
|
229 | sub _topleft { |
|
|
230 | my ($self, $x, $y) = @_; |
|
|
231 | |
|
|
232 | $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); |
136 | } |
233 | } |
137 | |
234 | |
138 | # translate global coordinates to local coordinate system |
235 | # translate global coordinates to local coordinate system |
139 | sub translate { |
236 | sub coord2local { |
140 | my ($self, $x, $y) = @_; |
237 | my ($self, $x, $y) = @_; |
141 | |
238 | |
142 | $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); |
239 | my ($X, $Y) = $self->_topleft; |
|
|
240 | ($x - $X, $y - $Y) |
|
|
241 | } |
|
|
242 | |
|
|
243 | # translate local coordinates to global coordinate system |
|
|
244 | sub coord2global { |
|
|
245 | my ($self, $x, $y) = @_; |
|
|
246 | |
|
|
247 | my ($X, $Y) = $self->_topleft; |
|
|
248 | ($x + $X, $y + $Y) |
143 | } |
249 | } |
144 | |
250 | |
145 | sub focus_in { |
251 | sub focus_in { |
146 | my ($self) = @_; |
252 | my ($self) = @_; |
147 | |
253 | |
148 | return if $FOCUS == $self; |
254 | return if $FOCUS == $self; |
149 | return unless $self->{can_focus}; |
255 | return unless $self->{can_focus}; |
150 | |
256 | |
151 | my $focus = $FOCUS; $FOCUS = $self; |
257 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
258 | |
|
|
259 | $self->emit (focus_in => $focus); |
|
|
260 | |
152 | $focus->update if $focus; |
261 | $focus->update if $focus; |
153 | $FOCUS->update; |
262 | $FOCUS->update; |
154 | } |
263 | } |
155 | |
264 | |
156 | sub focus_out { |
265 | sub focus_out { |
157 | my ($self) = @_; |
266 | my ($self) = @_; |
158 | |
267 | |
159 | return unless $FOCUS == $self; |
268 | return unless $FOCUS == $self; |
160 | |
269 | |
161 | my $focus = $FOCUS; undef $FOCUS; |
270 | my $focus = $FOCUS; undef $FOCUS; |
|
|
271 | |
|
|
272 | $self->emit (focus_out => $focus); |
|
|
273 | |
162 | $focus->update if $focus; #? |
274 | $focus->update if $focus; #? |
163 | } |
275 | } |
164 | |
276 | |
165 | sub mouse_motion { } |
277 | sub mouse_motion { } |
166 | sub button_up { } |
278 | sub button_up { } |
… | |
… | |
190 | glPopMatrix; |
302 | glPopMatrix; |
191 | |
303 | |
192 | if ($self == $HOVER && $self->{can_hover}) { |
304 | if ($self == $HOVER && $self->{can_hover}) { |
193 | my ($x, $y) = @$self{qw(x y)}; |
305 | my ($x, $y) = @$self{qw(x y)}; |
194 | |
306 | |
195 | glColor 0, 0, 1, 0.2; |
307 | glColor 1, 0.8, 0.5, 0.2; |
196 | glEnable GL_BLEND; |
308 | glEnable GL_BLEND; |
197 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
309 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
198 | glBegin GL_QUADS; |
310 | glBegin GL_QUADS; |
199 | glVertex $x , $y; |
311 | glVertex $x , $y; |
200 | glVertex $x + $self->{w}, $y; |
312 | glVertex $x + $self->{w}, $y; |
… | |
… | |
212 | } |
324 | } |
213 | |
325 | |
214 | sub find_widget { |
326 | sub find_widget { |
215 | my ($self, $x, $y) = @_; |
327 | my ($self, $x, $y) = @_; |
216 | |
328 | |
|
|
329 | return () unless $self->{can_events}; |
|
|
330 | |
217 | return $self |
331 | return $self |
218 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
332 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
219 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
333 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
220 | |
334 | |
221 | () |
335 | () |
222 | } |
336 | } |
223 | |
337 | |
224 | sub set_parent { |
338 | sub set_parent { |
225 | my ($self, $par) = @_; |
339 | my ($self, $parent) = @_; |
226 | |
340 | |
227 | $self->{parent} = $par; |
|
|
228 | Scalar::Util::weaken $self->{parent}; |
341 | Scalar::Util::weaken ($self->{parent} = $parent); |
229 | } |
342 | } |
230 | |
343 | |
231 | sub get_parent { |
344 | sub check_size { |
|
|
345 | my ($self) = @_; |
|
|
346 | |
232 | $_[0]->{parent} |
347 | $self->{parent} |
|
|
348 | or return 1; |
|
|
349 | |
|
|
350 | my ($w, $h) = $self->{user_w} && $self->{user_h} |
|
|
351 | ? @$self{qw(user_w user_h)} |
|
|
352 | : $self->size_request; |
|
|
353 | |
|
|
354 | if ($w != $self->{req_w} || $h != $self->{req_h}) { |
|
|
355 | $self->{req_w} = $w; |
|
|
356 | $self->{req_h} = $h; |
|
|
357 | |
|
|
358 | $self->{parent}->check_size |
|
|
359 | or $self->size_allocate ( |
|
|
360 | (List::Util::max $self->{w}, $w), |
|
|
361 | (List::Util::max $self->{h}, $h), |
|
|
362 | ); |
|
|
363 | |
|
|
364 | 1 |
|
|
365 | } else { |
|
|
366 | 0 |
|
|
367 | } |
233 | } |
368 | } |
234 | |
369 | |
235 | sub update { |
370 | sub update { |
236 | my ($self) = @_; |
371 | my ($self) = @_; |
237 | |
372 | |
… | |
… | |
240 | } |
375 | } |
241 | |
376 | |
242 | sub connect { |
377 | sub connect { |
243 | my ($self, $signal, $cb) = @_; |
378 | my ($self, $signal, $cb) = @_; |
244 | |
379 | |
245 | push @{ $self->{cb}{$signal} }, $cb; |
380 | push @{ $self->{signal_cb}{$signal} }, $cb; |
246 | } |
381 | } |
247 | |
382 | |
248 | sub emit { |
383 | sub emit { |
249 | my ($self, $signal, @args) = @_; |
384 | my ($self, $signal, @args) = @_; |
250 | |
385 | |
|
|
386 | for my $cb (@{$self->{signal_cb}{$signal} || []}) { |
251 | $_->($self, @args) |
387 | $cb->($self, @args); |
252 | for @{$self->{cb}{$signal} || []}; |
388 | } |
253 | } |
389 | } |
254 | |
390 | |
255 | sub DESTROY { |
391 | sub DESTROY { |
256 | my ($self) = @_; |
392 | my ($self) = @_; |
257 | |
393 | |
… | |
… | |
263 | package CFClient::UI::DrawBG; |
399 | package CFClient::UI::DrawBG; |
264 | |
400 | |
265 | our @ISA = CFClient::UI::Base::; |
401 | our @ISA = CFClient::UI::Base::; |
266 | |
402 | |
267 | use strict; |
403 | use strict; |
268 | use SDL::OpenGL; |
404 | use CFClient::OpenGL; |
269 | |
405 | |
270 | sub new { |
406 | sub new { |
271 | my $class = shift; |
407 | my $class = shift; |
272 | |
408 | |
273 | # range [value, low, high, page] |
409 | # range [value, low, high, page] |
… | |
… | |
302 | |
438 | |
303 | package CFClient::UI::Empty; |
439 | package CFClient::UI::Empty; |
304 | |
440 | |
305 | our @ISA = CFClient::UI::Base::; |
441 | our @ISA = CFClient::UI::Base::; |
306 | |
442 | |
|
|
443 | sub new { |
|
|
444 | my ($class, %arg) = @_; |
|
|
445 | $class->SUPER::new (can_events => 0, %arg); |
|
|
446 | } |
|
|
447 | |
307 | sub size_request { |
448 | sub size_request { |
308 | (0, 0) |
449 | (0, 0) |
309 | } |
450 | } |
310 | |
451 | |
311 | sub draw { } |
452 | sub draw { } |
… | |
… | |
319 | sub new { |
460 | sub new { |
320 | my ($class, %arg) = @_; |
461 | my ($class, %arg) = @_; |
321 | |
462 | |
322 | my $children = delete $arg{children} || []; |
463 | my $children = delete $arg{children} || []; |
323 | |
464 | |
324 | my $self = $class->SUPER::new (children => [], %arg); |
465 | my $self = $class->SUPER::new (children => [], can_events => 0, %arg); |
325 | $self->add ($_) for @$children; |
466 | $self->add ($_) for @$children; |
326 | |
467 | |
327 | $self |
468 | $self |
328 | } |
469 | } |
329 | |
470 | |
330 | sub add { |
471 | sub add { |
331 | my ($self, $chld) = @_; |
472 | my ($self, $child) = @_; |
332 | |
473 | |
333 | $chld->set_parent ($self); |
474 | $child->set_parent ($self); |
|
|
475 | |
|
|
476 | use sort 'stable'; |
334 | |
477 | |
335 | $self->{children} = [ |
478 | $self->{children} = [ |
336 | sort { $a->{z} <=> $b->{z} } |
479 | sort { $a->{z} <=> $b->{z} } |
337 | @{$self->{children}}, $chld |
480 | @{$self->{children}}, $child |
338 | ]; |
481 | ]; |
339 | |
482 | |
340 | $self->{w} = $self->{h} = -1; |
483 | $child->check_size; |
|
|
484 | } |
|
|
485 | |
|
|
486 | sub remove { |
|
|
487 | my ($self, $child) = @_; |
|
|
488 | |
|
|
489 | delete $child->{parent}; |
|
|
490 | |
|
|
491 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
|
|
492 | |
|
|
493 | $self->check_size; |
341 | $self->update; |
494 | $self->update; |
342 | } |
|
|
343 | |
|
|
344 | sub remove { |
|
|
345 | my ($self, $widget) = @_; |
|
|
346 | |
|
|
347 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
|
|
348 | |
|
|
349 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
|
|
350 | } |
495 | } |
351 | |
496 | |
352 | sub find_widget { |
497 | sub find_widget { |
353 | my ($self, $x, $y) = @_; |
498 | my ($self, $x, $y) = @_; |
354 | |
499 | |
… | |
… | |
384 | |
529 | |
385 | $class->SUPER::new (children => [$child], %arg) |
530 | $class->SUPER::new (children => [$child], %arg) |
386 | } |
531 | } |
387 | |
532 | |
388 | sub add { |
533 | sub add { |
389 | my ($self, $widget) = @_; |
534 | my ($self, $child) = @_; |
390 | |
535 | |
391 | $self->{children} = []; |
536 | $self->{children} = []; |
392 | |
537 | |
393 | $self->SUPER::add ($widget); |
538 | $self->SUPER::add ($child); |
394 | } |
539 | } |
395 | |
540 | |
396 | sub remove { |
541 | sub remove { |
397 | my ($self, $widget) = @_; |
542 | my ($self, $widget) = @_; |
398 | |
543 | |
… | |
… | |
407 | sub size_request { |
552 | sub size_request { |
408 | $_[0]{children}[0]->size_request |
553 | $_[0]{children}[0]->size_request |
409 | } |
554 | } |
410 | |
555 | |
411 | sub size_allocate { |
556 | sub size_allocate { |
412 | my ($self, $x, $y, $w, $h) = @_; |
557 | my ($self, $w, $h) = @_; |
413 | |
558 | |
414 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
415 | |
|
|
416 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
559 | $self->{children}[0]->configure (0, 0, $w, $h); |
417 | } |
560 | } |
418 | |
561 | |
419 | ############################################################################# |
562 | ############################################################################# |
420 | |
563 | |
421 | package CFClient::UI::Window; |
564 | package CFClient::UI::Window; |
422 | |
565 | |
423 | our @ISA = CFClient::UI::Bin::; |
566 | our @ISA = CFClient::UI::Bin::; |
424 | |
567 | |
425 | use SDL::OpenGL; |
568 | use CFClient::OpenGL; |
426 | |
569 | |
427 | sub new { |
570 | sub new { |
428 | my ($class, %arg) = @_; |
571 | my ($class, %arg) = @_; |
429 | |
572 | |
430 | my $self = $class->SUPER::new (%arg); |
573 | my $self = $class->SUPER::new (%arg); |
… | |
… | |
439 | } |
582 | } |
440 | |
583 | |
441 | sub render_chld { |
584 | sub render_chld { |
442 | my ($self) = @_; |
585 | my ($self) = @_; |
443 | |
586 | |
444 | $self->{texture} = |
587 | $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
445 | CFClient::Texture->new_from_opengl ( |
588 | glClearColor 0, 0, 0, 1; |
446 | $self->{w}, $self->{h}, sub { $self->child->draw } |
589 | glClear GL_COLOR_BUFFER_BIT; |
447 | ); |
590 | $self->child->draw; |
|
|
591 | }; |
448 | } |
592 | } |
449 | |
593 | |
450 | sub size_allocate { |
594 | sub size_allocate { |
451 | my ($self, $x, $y, $w, $h) = @_; |
595 | my ($self, $w, $h) = @_; |
452 | |
596 | |
453 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
454 | |
|
|
455 | $self->child->size_allocate (0, 0, $w, $h); |
597 | $self->child->configure (0, 0, $w, $h); |
456 | |
598 | |
457 | $self->render_chld; |
599 | $self->render_chld; |
458 | } |
600 | } |
459 | |
601 | |
460 | sub _draw { |
602 | sub _draw { |
… | |
… | |
476 | glDisable GL_TEXTURE_2D; |
618 | glDisable GL_TEXTURE_2D; |
477 | } |
619 | } |
478 | |
620 | |
479 | ############################################################################# |
621 | ############################################################################# |
480 | |
622 | |
|
|
623 | package CFClient::UI::ViewPort; |
|
|
624 | |
|
|
625 | our @ISA = CFClient::UI::Window::; |
|
|
626 | |
|
|
627 | sub new { die } |
|
|
628 | |
|
|
629 | sub size_request { |
|
|
630 | my ($self) = @_; |
|
|
631 | |
|
|
632 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
|
|
633 | $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); |
|
|
634 | |
|
|
635 | @$self{qw(child_w child_h)} |
|
|
636 | } |
|
|
637 | |
|
|
638 | sub _draw { |
|
|
639 | my ($self) = @_; |
|
|
640 | |
|
|
641 | $self->{children}[1]->draw; |
|
|
642 | } |
|
|
643 | |
|
|
644 | |
|
|
645 | ############################################################################# |
|
|
646 | |
481 | package CFClient::UI::Frame; |
647 | package CFClient::UI::Frame; |
482 | |
648 | |
483 | our @ISA = CFClient::UI::Bin::; |
649 | our @ISA = CFClient::UI::Bin::; |
484 | |
650 | |
485 | use SDL::OpenGL; |
651 | use CFClient::OpenGL; |
486 | |
|
|
487 | sub size_request { |
|
|
488 | my ($self) = @_; |
|
|
489 | my $chld = $self->child |
|
|
490 | or return (0, 0); |
|
|
491 | |
|
|
492 | $chld->move (2, 2); |
|
|
493 | |
|
|
494 | map { $_ + 4 } $chld->size_request; |
|
|
495 | } |
|
|
496 | |
|
|
497 | sub size_allocate { |
|
|
498 | my ($self, $x, $y, $w, $h) = @_; |
|
|
499 | |
|
|
500 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
501 | |
|
|
502 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
|
|
503 | } |
|
|
504 | |
|
|
505 | sub _draw { |
|
|
506 | my ($self) = @_; |
|
|
507 | |
|
|
508 | my $chld = $self->child; |
|
|
509 | |
|
|
510 | my ($w, $h) = $chld->size_request; |
|
|
511 | |
|
|
512 | glBegin GL_QUADS; |
|
|
513 | glColor 0, 0, 0; |
|
|
514 | glVertex 0 , 0; |
|
|
515 | glVertex 0 , $h + 4; |
|
|
516 | glVertex $w + 4 , $h + 4; |
|
|
517 | glVertex $w + 4 , 0; |
|
|
518 | glEnd; |
|
|
519 | |
|
|
520 | $chld->draw; |
|
|
521 | } |
|
|
522 | |
|
|
523 | ############################################################################# |
|
|
524 | |
|
|
525 | package CFClient::UI::FancyFrame; |
|
|
526 | |
|
|
527 | our @ISA = CFClient::UI::Bin::; |
|
|
528 | |
|
|
529 | use SDL::OpenGL; |
|
|
530 | |
|
|
531 | my @tex = |
|
|
532 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
533 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
534 | |
652 | |
535 | sub new { |
653 | sub new { |
536 | my $class = shift; |
654 | my $class = shift; |
537 | |
655 | |
538 | # TODO: user_x, user_y, overwrite moveto? |
|
|
539 | |
|
|
540 | $class->SUPER::new ( |
656 | my $self = $class->SUPER::new ( |
541 | bg => [1, 1, 1, 1], |
657 | bg => [1, 1, 1, 1], |
542 | border_bg => [1, 1, 1, 1], |
658 | border_bg => [1, 1, 1, 1], |
543 | border => $::FONTSIZE * 0.8, |
659 | border => 0.8, |
544 | @_ |
660 | @_ |
|
|
661 | ); |
|
|
662 | |
|
|
663 | $self |
|
|
664 | } |
|
|
665 | |
|
|
666 | sub _draw { |
|
|
667 | my ($self) = @_; |
|
|
668 | |
|
|
669 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
670 | |
|
|
671 | glEnable GL_BLEND; |
|
|
672 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
673 | glEnable GL_TEXTURE_2D; |
|
|
674 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
675 | |
|
|
676 | # glBegin GL_QUADS; |
|
|
677 | # glColor 0, 0, 0, 0; |
|
|
678 | # glVertex 0 , 0; |
|
|
679 | # glVertex 0 , $h; |
|
|
680 | # glVertex $w, $h; |
|
|
681 | # glVertex $w, 0; |
|
|
682 | # glEnd; |
|
|
683 | |
|
|
684 | |
|
|
685 | $self->child->draw; |
|
|
686 | glDisable GL_BLEND; |
|
|
687 | glDisable GL_TEXTURE_2D; |
|
|
688 | } |
|
|
689 | |
|
|
690 | ############################################################################# |
|
|
691 | |
|
|
692 | package CFClient::UI::FancyFrame; |
|
|
693 | |
|
|
694 | our @ISA = CFClient::UI::Bin::; |
|
|
695 | |
|
|
696 | use CFClient::OpenGL; |
|
|
697 | |
|
|
698 | my @tex = |
|
|
699 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
700 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
|
|
701 | |
|
|
702 | sub new { |
|
|
703 | my $class = shift; |
|
|
704 | |
|
|
705 | # TODO: user_x, user_y, overwrite moveto? |
|
|
706 | |
|
|
707 | my $self = $class->SUPER::new ( |
|
|
708 | bg => [1, 1, 1, 1], |
|
|
709 | border_bg => [1, 1, 1, 1], |
|
|
710 | border => 0.8, |
|
|
711 | can_events => 1, |
|
|
712 | @_ |
|
|
713 | ); |
|
|
714 | |
|
|
715 | $self->{title} &&= new CFClient::UI::Label |
|
|
716 | align => 0, |
|
|
717 | valign => 1, |
|
|
718 | text => $self->{title}, |
|
|
719 | fontsize => 1; |
|
|
720 | |
|
|
721 | $self |
|
|
722 | } |
|
|
723 | |
|
|
724 | sub border { |
|
|
725 | int $_[0]{border} * $::FONTSIZE |
|
|
726 | } |
|
|
727 | |
|
|
728 | sub size_request { |
|
|
729 | my ($self) = @_; |
|
|
730 | |
|
|
731 | my ($w, $h) = $self->SUPER::size_request; |
|
|
732 | |
|
|
733 | ( |
|
|
734 | $w + $self->border * 2, |
|
|
735 | $h + $self->border * 2, |
545 | ) |
736 | ) |
546 | } |
737 | } |
547 | |
738 | |
548 | sub size_request { |
|
|
549 | my ($self) = @_; |
|
|
550 | |
|
|
551 | return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; |
|
|
552 | |
|
|
553 | my ($w, $h) = $self->SUPER::size_request; |
|
|
554 | |
|
|
555 | ( |
|
|
556 | $w + $self->{border} * 2, |
|
|
557 | $h + $self->{border} * 2, |
|
|
558 | ) |
|
|
559 | } |
|
|
560 | |
|
|
561 | sub size_allocate { |
739 | sub size_allocate { |
562 | my ($self, $x, $y, $w, $h) = @_; |
740 | my ($self, $w, $h) = @_; |
563 | |
741 | |
564 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
565 | |
|
|
566 | $h -= List::Util::max 0, $self->{border} * 2; |
742 | $h -= List::Util::max 0, $self->border * 2; |
567 | $w -= List::Util::max 0, $self->{border} * 2; |
743 | $w -= List::Util::max 0, $self->border * 2; |
568 | |
744 | |
|
|
745 | $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2) |
|
|
746 | if $self->{title}; |
|
|
747 | |
569 | $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); |
748 | $self->child->configure ($self->border, $self->border, $w, $h); |
570 | } |
749 | } |
571 | |
750 | |
572 | sub button_down { |
751 | sub button_down { |
573 | my ($self, $ev, $x, $y) = @_; |
752 | my ($self, $ev, $x, $y) = @_; |
574 | |
753 | |
|
|
754 | my $border = $self->border; |
|
|
755 | |
575 | if ($x < $self->{w} && $x >= $self->{w} - $self->{border} |
756 | if ($x < $self->{w} && $x >= $self->{w} - $border |
576 | && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { |
757 | && $y < $self->{h} && $y >= $self->{h} - $border) { |
577 | |
758 | |
578 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
759 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
579 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
760 | my ($bw, $bh) = ($self->{w}, $self->{h}); |
580 | |
761 | |
581 | $self->{motion} = sub { |
762 | $self->{motion} = sub { |
582 | my ($ev, $x, $y) = @_; |
763 | my ($ev, $x, $y) = @_; |
583 | |
764 | |
584 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
765 | ($x, $y) = ($ev->{x}, $ev->{y}); |
585 | |
766 | |
586 | $self->{user_w} = $bw + $x - $ox; |
767 | $self->{user_w} = $bw + $x - $ox; |
587 | $self->{user_h} = $bh + $y - $oy; |
768 | $self->{user_h} = $bh + $y - $oy; |
588 | $self->update; |
769 | $self->check_size; |
589 | }; |
770 | }; |
590 | |
771 | |
591 | } elsif ($x >= 0 && $x < $self->{w} |
772 | } elsif ($x >= 0 && $x < $self->{w} |
592 | && $y >= 0 && $y < $self->{border}) { |
773 | && $y >= 0 && $y < $border) { |
593 | |
774 | |
594 | my ($ox, $oy) = ($ev->button_x, $ev->button_y); |
775 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
595 | my ($bx, $by) = ($self->{x}, $self->{y}); |
776 | my ($bx, $by) = ($self->{x}, $self->{y}); |
596 | |
777 | |
597 | $self->{motion} = sub { |
778 | $self->{motion} = sub { |
598 | my ($ev, $x, $y) = @_; |
779 | my ($ev, $x, $y) = @_; |
599 | |
780 | |
600 | ($x, $y) = ($ev->motion_x, $ev->motion_y); |
781 | ($x, $y) = ($ev->{x}, $ev->{y}); |
601 | |
782 | |
602 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
783 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
603 | $self->update; |
784 | $self->update; |
604 | }; |
785 | }; |
605 | } |
786 | } |
… | |
… | |
626 | glEnable GL_BLEND; |
807 | glEnable GL_BLEND; |
627 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
808 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
628 | glEnable GL_TEXTURE_2D; |
809 | glEnable GL_TEXTURE_2D; |
629 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
810 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
630 | |
811 | |
|
|
812 | my $border = $self->border; |
|
|
813 | |
631 | glColor @{ $self->{border_bg} }; |
814 | glColor @{ $self->{border_bg} }; |
632 | $tex[1]->draw_quad (0, 0, $w, $self->{border}); |
815 | $tex[1]->draw_quad (0, 0, $w, $border); |
633 | $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); |
816 | $tex[3]->draw_quad (0, $border, $border, $ch); |
634 | $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); |
817 | $tex[2]->draw_quad ($w - $border, $border, $border, $ch); |
635 | $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); |
818 | $tex[4]->draw_quad (0, $h - $border, $w, $border); |
636 | |
819 | |
637 | my $bg = $tex[0]; |
820 | my $bg = $tex[0]; |
638 | |
821 | |
639 | # TODO: repeat texture not scale |
822 | # TODO: repeat texture not scale |
640 | my $rep_x = $cw / $bg->{w}; |
823 | my $rep_x = $cw / $bg->{w}; |
641 | my $rep_y = $ch / $bg->{h}; |
824 | my $rep_y = $ch / $bg->{h}; |
642 | |
825 | |
643 | glColor @{ $self->{bg} }; |
826 | glColor @{ $self->{bg} }; |
|
|
827 | |
|
|
828 | $bg->{s} = $rep_x; |
|
|
829 | $bg->{t} = $rep_y; |
|
|
830 | $bg->{wrap_mode} = 1; |
644 | $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); |
831 | $bg->draw_quad ($border, $border, $cw, $ch); |
645 | |
832 | |
646 | glDisable GL_TEXTURE_2D; |
833 | glDisable GL_TEXTURE_2D; |
647 | glDisable GL_BLEND; |
834 | glDisable GL_BLEND; |
648 | |
835 | |
|
|
836 | $self->{title}->draw if $self->{title}; |
649 | $self->child->draw; |
837 | $self->child->draw; |
650 | } |
838 | } |
651 | |
839 | |
652 | ############################################################################# |
840 | ############################################################################# |
653 | |
841 | |
… | |
… | |
655 | |
843 | |
656 | our @ISA = CFClient::UI::Base::; |
844 | our @ISA = CFClient::UI::Base::; |
657 | |
845 | |
658 | use List::Util qw(max sum); |
846 | use List::Util qw(max sum); |
659 | |
847 | |
660 | use SDL::OpenGL; |
848 | use CFClient::OpenGL; |
661 | |
849 | |
662 | sub new { |
850 | sub new { |
663 | my $class = shift; |
851 | my $class = shift; |
664 | |
852 | |
665 | $class->SUPER::new ( |
853 | $class->SUPER::new ( |
… | |
… | |
667 | @_ |
855 | @_ |
668 | ) |
856 | ) |
669 | } |
857 | } |
670 | |
858 | |
671 | sub add { |
859 | sub add { |
672 | my ($self, $x, $y, $chld) = @_; |
860 | my ($self, $x, $y, $child) = @_; |
673 | |
861 | |
|
|
862 | $child->set_parent ($self); |
674 | $self->{children}[$y][$x] = $chld; |
863 | $self->{children}[$y][$x] = $child; |
675 | $chld->set_parent ($self); |
|
|
676 | |
864 | |
677 | $self->{w} = $self->{h} = -1; |
865 | $child->check_size; |
|
|
866 | } |
|
|
867 | |
|
|
868 | # TODO: move to container class maybe? send childs a signal on removal? |
|
|
869 | sub clear { |
|
|
870 | my ($self) = @_; |
|
|
871 | |
|
|
872 | delete $self->{children}; |
678 | $self->update; |
873 | $self->update; |
679 | } |
874 | } |
680 | |
875 | |
681 | sub get_wh { |
876 | sub get_wh { |
682 | my ($self) = @_; |
877 | my ($self) = @_; |
… | |
… | |
688 | or next; |
883 | or next; |
689 | |
884 | |
690 | for my $x (0 .. $#$row) { |
885 | for my $x (0 .. $#$row) { |
691 | my $widget = $row->[$x] |
886 | my $widget = $row->[$x] |
692 | or next; |
887 | or next; |
693 | my ($w, $h) = $widget->size_request; |
888 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
694 | |
889 | |
695 | $w[$x] = max $w[$x], $w; |
890 | $w[$x] = max $w[$x], $w; |
696 | $h[$y] = max $h[$y], $h; |
891 | $h[$y] = max $h[$y], $h; |
697 | } |
892 | } |
698 | } |
893 | } |
… | |
… | |
710 | (sum @$hs), |
905 | (sum @$hs), |
711 | ) |
906 | ) |
712 | } |
907 | } |
713 | |
908 | |
714 | sub size_allocate { |
909 | sub size_allocate { |
715 | my ($self, $x, $y, $w, $h) = @_; |
910 | my ($self, $w, $h) = @_; |
716 | |
|
|
717 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
718 | |
911 | |
719 | my ($ws, $hs) = $self->get_wh; |
912 | my ($ws, $hs) = $self->get_wh; |
720 | |
913 | |
721 | my $req_w = sum @$ws; |
914 | my $req_w = sum @$ws; |
722 | my $req_h = sum @$hs; |
915 | my $req_h = sum @$hs; |
… | |
… | |
727 | my $col_expand = (sum @col_expand) || 1; |
920 | my $col_expand = (sum @col_expand) || 1; |
728 | |
921 | |
729 | # linearly scale sizes |
922 | # linearly scale sizes |
730 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
923 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
731 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
924 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
925 | |
|
|
926 | CFClient::UI::harmonize $ws; |
|
|
927 | CFClient::UI::harmonize $hs; |
732 | |
928 | |
733 | my $y; |
929 | my $y; |
734 | |
930 | |
735 | for my $r (0 .. $#{$self->{children}}) { |
931 | for my $r (0 .. $#{$self->{children}}) { |
736 | my $row = $self->{children}[$r] |
932 | my $row = $self->{children}[$r] |
… | |
… | |
741 | |
937 | |
742 | for my $c (0 .. $#$row) { |
938 | for my $c (0 .. $#$row) { |
743 | my $col_w = $ws->[$c]; |
939 | my $col_w = $ws->[$c]; |
744 | |
940 | |
745 | if (my $widget = $row->[$c]) { |
941 | if (my $widget = $row->[$c]) { |
746 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
942 | $widget->configure ($x, $y, $col_w, $row_h); |
747 | } |
943 | } |
748 | |
944 | |
749 | $x += $col_w; |
945 | $x += $col_w; |
750 | } |
946 | } |
751 | |
947 | |
… | |
… | |
796 | (List::Util::max map $_->[1], @alloc), |
992 | (List::Util::max map $_->[1], @alloc), |
797 | ) |
993 | ) |
798 | } |
994 | } |
799 | |
995 | |
800 | sub size_allocate { |
996 | sub size_allocate { |
801 | my ($self, $x, $y, $w, $h) = @_; |
997 | my ($self, $w, $h) = @_; |
802 | |
|
|
803 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
804 | |
998 | |
805 | ($h, $w) = ($w, $h); |
999 | ($h, $w) = ($w, $h); |
806 | |
1000 | |
807 | my $children = $self->{children}; |
1001 | my $children = $self->{children}; |
808 | |
1002 | |
809 | my @h = map +($_->size_request)[0], @$children; |
1003 | my @h = map $_->{req_w}, @$children; |
810 | |
1004 | |
811 | my $req_h = List::Util::sum @h; |
1005 | my $req_h = List::Util::sum @h; |
812 | |
1006 | |
813 | if ($req_h > $h) { |
1007 | if ($req_h > $h) { |
814 | # ah well, not enough space |
1008 | # ah well, not enough space |
… | |
… | |
824 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
1018 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
825 | $h[$_] = $alloc_h; |
1019 | $h[$_] = $alloc_h; |
826 | } |
1020 | } |
827 | } |
1021 | } |
828 | |
1022 | |
|
|
1023 | CFClient::UI::harmonize \@h; |
|
|
1024 | |
829 | my $y = 0; |
1025 | my $y = 0; |
830 | for (0 .. $#$children) { |
1026 | for (0 .. $#$children) { |
831 | my $child = $children->[$_]; |
1027 | my $child = $children->[$_]; |
832 | my $h = $h[$_]; |
1028 | my $h = $h[$_]; |
833 | $child->size_allocate ($y, 0, $h, $w); |
1029 | $child->configure ($y, 0, $h, $w); |
834 | |
1030 | |
835 | $y += $h; |
1031 | $y += $h; |
836 | } |
1032 | } |
|
|
1033 | |
|
|
1034 | 1 |
837 | } |
1035 | } |
838 | |
1036 | |
839 | ############################################################################# |
1037 | ############################################################################# |
840 | |
1038 | |
841 | package CFClient::UI::VBox; |
1039 | package CFClient::UI::VBox; |
… | |
… | |
854 | (List::Util::sum map $_->[1], @alloc), |
1052 | (List::Util::sum map $_->[1], @alloc), |
855 | ) |
1053 | ) |
856 | } |
1054 | } |
857 | |
1055 | |
858 | sub size_allocate { |
1056 | sub size_allocate { |
859 | my ($self, $x, $y, $w, $h) = @_; |
1057 | my ($self, $w, $h) = @_; |
860 | |
|
|
861 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
862 | |
1058 | |
863 | my $children = $self->{children}; |
1059 | my $children = $self->{children}; |
864 | |
1060 | |
865 | my @h = map +($_->size_request)[1], @$children; |
1061 | my @h = map $_->{req_h}, @$children; |
866 | |
1062 | |
867 | my $req_h = List::Util::sum @h; |
1063 | my $req_h = List::Util::sum @h; |
868 | |
1064 | |
869 | if ($req_h > $h) { |
1065 | if ($req_h > $h) { |
870 | # ah well, not enough space |
1066 | # ah well, not enough space |
… | |
… | |
878 | |
1074 | |
879 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
1075 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
880 | } |
1076 | } |
881 | } |
1077 | } |
882 | |
1078 | |
|
|
1079 | CFClient::UI::harmonize \@h; |
|
|
1080 | |
883 | my $y = 0; |
1081 | my $y = 0; |
884 | for (0 .. $#$children) { |
1082 | for (0 .. $#$children) { |
885 | my $child = $children->[$_]; |
1083 | my $child = $children->[$_]; |
886 | my $h = $h[$_]; |
1084 | my $h = $h[$_]; |
887 | $child->size_allocate (0, $y, $w, $h); |
1085 | $child->configure (0, $y, $w, $h); |
888 | |
1086 | |
889 | $y += $h; |
1087 | $y += $h; |
890 | } |
1088 | } |
|
|
1089 | |
|
|
1090 | 1 |
891 | } |
1091 | } |
892 | |
1092 | |
893 | ############################################################################# |
1093 | ############################################################################# |
894 | |
1094 | |
895 | package CFClient::UI::Label; |
1095 | package CFClient::UI::Label; |
896 | |
1096 | |
897 | our @ISA = CFClient::UI::Base::; |
1097 | our @ISA = CFClient::UI::Base::; |
898 | |
1098 | |
899 | use SDL::OpenGL; |
1099 | use CFClient::OpenGL; |
900 | |
1100 | |
901 | sub new { |
1101 | sub new { |
902 | my ($class, %arg) = @_; |
1102 | my ($class, %arg) = @_; |
903 | |
1103 | |
904 | my $self = $class->SUPER::new ( |
1104 | my $self = $class->SUPER::new ( |
905 | fg => [1, 1, 1], |
1105 | fg => [1, 1, 1], |
906 | height => $::FONTSIZE, |
1106 | #font => default_font |
|
|
1107 | fontsize => 1, |
907 | text => "", |
1108 | text => "", |
908 | align => -1, |
1109 | align => -1, |
|
|
1110 | valign => -1, |
909 | padding => 2, |
1111 | padding => 2, |
910 | layout => new CFClient::Layout, |
1112 | layout => new CFClient::Layout, |
|
|
1113 | can_events => 0, |
911 | %arg |
1114 | %arg |
912 | ); |
1115 | ); |
913 | |
1116 | |
914 | $self->set_text ($self->{text}); |
1117 | if (exists $self->{template}) { |
|
|
1118 | my $layout = new CFClient::Layout; |
|
|
1119 | $layout->set_text (delete $self->{template}); |
|
|
1120 | $self->{template} = $layout; |
|
|
1121 | } |
|
|
1122 | |
|
|
1123 | $self->set_text (delete $self->{text}) if exists $self->{text}; |
|
|
1124 | $self->set_markup (delete $self->{markup}) if exists $self->{markup}; |
915 | |
1125 | |
916 | $self |
1126 | $self |
917 | } |
1127 | } |
918 | |
1128 | |
919 | sub escape_text { |
1129 | sub escape_text { |
… | |
… | |
927 | } |
1137 | } |
928 | |
1138 | |
929 | sub set_text { |
1139 | sub set_text { |
930 | my ($self, $text) = @_; |
1140 | my ($self, $text) = @_; |
931 | |
1141 | |
932 | $self->{text} = $text; |
|
|
933 | $self->{layout}->set_markup ($text); |
1142 | $self->{layout}->set_text ($text); |
934 | |
1143 | |
935 | delete $self->{texture}; |
1144 | delete $self->{texture}; |
936 | # $self->{w} = $self->{h} = -1; |
1145 | $self->check_size; |
937 | $self->update; |
1146 | $self->update; |
938 | } |
1147 | } |
939 | |
1148 | |
940 | sub get_text { |
1149 | sub set_markup { |
941 | my ($self, $text) = @_; |
1150 | my ($self, $markup) = @_; |
942 | |
1151 | |
943 | $self->{text} |
1152 | $self->{layout}->set_markup ($markup); |
|
|
1153 | |
|
|
1154 | delete $self->{texture}; |
|
|
1155 | $self->check_size; |
|
|
1156 | $self->update; |
944 | } |
1157 | } |
945 | |
1158 | |
946 | sub size_request { |
1159 | sub size_request { |
947 | my ($self) = @_; |
1160 | my ($self) = @_; |
948 | |
1161 | |
949 | $self->{layout}->set_width; |
1162 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1163 | $self->{layout}->set_width ($self->{max_w} || -1); |
950 | $self->{layout}->set_height ($self->{height}); |
1164 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1165 | |
951 | my ($w, $h) = $self->{layout}->size; |
1166 | my ($w, $h) = $self->{layout}->size; |
|
|
1167 | |
|
|
1168 | if (exists $self->{template}) { |
|
|
1169 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
|
|
1170 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1171 | |
|
|
1172 | my ($w2, $h2) = $self->{template}->size; |
|
|
1173 | |
|
|
1174 | $w = List::Util::max $w, $w2; |
|
|
1175 | $h = List::Util::max $h, $h2; |
|
|
1176 | } |
952 | |
1177 | |
953 | ( |
1178 | ( |
954 | $w + $self->{padding} * 2, |
1179 | $w + $self->{padding} * 2, |
955 | $h + $self->{padding} * 2, |
1180 | $h + $self->{padding} * 2, |
956 | ) |
1181 | ) |
957 | } |
1182 | } |
958 | |
1183 | |
959 | sub size_allocate { |
1184 | sub size_allocate { |
960 | my ($self, $x, $y, $w, $h) = @_; |
1185 | my ($self, $w, $h) = @_; |
961 | |
|
|
962 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
963 | |
1186 | |
964 | delete $self->{texture}; |
1187 | delete $self->{texture}; |
965 | } |
1188 | } |
966 | |
1189 | |
967 | sub update { |
1190 | sub set_fontsize { |
968 | my ($self) = @_; |
1191 | my ($self, $fontsize) = @_; |
969 | |
1192 | |
|
|
1193 | $self->{fontsize} = $fontsize; |
970 | delete $self->{texture}; |
1194 | delete $self->{texture}; |
|
|
1195 | $self->check_size; |
971 | $self->SUPER::update; |
1196 | $self->update; |
972 | } |
1197 | } |
973 | |
1198 | |
974 | sub _draw { |
1199 | sub _draw { |
975 | my ($self) = @_; |
1200 | my ($self) = @_; |
976 | |
1201 | |
977 | my $tex = $self->{texture} ||= do { |
1202 | my $tex = $self->{texture} ||= do { |
|
|
1203 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
978 | $self->{layout}->set_width ($self->{w}); |
1204 | $self->{layout}->set_width ($self->{w}); |
979 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
1205 | $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE); |
980 | new_from_layout CFClient::Texture $self->{layout} |
1206 | new_from_layout CFClient::Texture $self->{layout} |
981 | }; |
1207 | }; |
982 | |
1208 | |
983 | glEnable GL_BLEND; |
1209 | glEnable GL_BLEND; |
984 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1210 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
985 | glEnable GL_TEXTURE_2D; |
1211 | glEnable GL_TEXTURE_2D; |
986 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1212 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
987 | |
1213 | |
988 | glColor @{$self->{fg}}; |
1214 | glColor @{$self->{fg}}; |
989 | |
1215 | |
990 | my $x = |
1216 | $self->{ox} = int ( |
991 | $self->{align} < 0 ? $self->{padding} |
1217 | $self->{align} < 0 ? $self->{padding} |
992 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1218 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
993 | : ($self->{w} - $tex->{w}) * 0.5; |
1219 | : ($self->{w} - $tex->{w}) * 0.5 |
|
|
1220 | ); |
994 | |
1221 | |
995 | glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; |
1222 | $self->{oy} = int ( |
996 | $tex->draw_quad (0, 0); |
1223 | $self->{valign} < 0 ? $self->{padding} |
|
|
1224 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
|
|
1225 | : ($self->{h} - $tex->{h}) * 0.5 |
|
|
1226 | ); |
|
|
1227 | |
|
|
1228 | $tex->draw_quad ($self->{ox}, $self->{oy}); |
997 | |
1229 | |
998 | glDisable GL_TEXTURE_2D; |
1230 | glDisable GL_TEXTURE_2D; |
999 | glDisable GL_BLEND; |
1231 | glDisable GL_BLEND; |
1000 | } |
1232 | } |
1001 | |
1233 | |
1002 | ############################################################################# |
1234 | ############################################################################# |
1003 | |
1235 | |
1004 | package CFClient::UI::Entry; |
1236 | package CFClient::UI::EntryBase; |
1005 | |
1237 | |
1006 | our @ISA = CFClient::UI::Label::; |
1238 | our @ISA = CFClient::UI::Label::; |
1007 | |
1239 | |
1008 | use SDL; |
1240 | use CFClient::OpenGL; |
1009 | use SDL::OpenGL; |
|
|
1010 | |
1241 | |
1011 | sub new { |
1242 | sub new { |
1012 | my $class = shift; |
1243 | my $class = shift; |
1013 | |
1244 | |
1014 | $class->SUPER::new ( |
1245 | $class->SUPER::new ( |
… | |
… | |
1016 | bg => [0, 0, 0, 0.2], |
1247 | bg => [0, 0, 0, 0.2], |
1017 | active_bg => [1, 1, 1, 0.5], |
1248 | active_bg => [1, 1, 1, 0.5], |
1018 | active_fg => [0, 0, 0], |
1249 | active_fg => [0, 0, 0], |
1019 | can_hover => 1, |
1250 | can_hover => 1, |
1020 | can_focus => 1, |
1251 | can_focus => 1, |
|
|
1252 | valign => 0, |
|
|
1253 | can_events => 1, |
1021 | @_ |
1254 | @_ |
1022 | ) |
1255 | ) |
1023 | } |
1256 | } |
1024 | |
1257 | |
1025 | sub _set_text { |
1258 | sub _set_text { |
1026 | my ($self, $text) = @_; |
1259 | my ($self, $text) = @_; |
1027 | |
1260 | |
|
|
1261 | delete $self->{cur_h}; |
|
|
1262 | |
|
|
1263 | return if $self->{text} eq $text; |
|
|
1264 | |
|
|
1265 | delete $self->{texture}; |
|
|
1266 | |
1028 | $self->{last_activity} = $::NOW; |
1267 | $self->{last_activity} = $::NOW; |
1029 | |
|
|
1030 | $self->{text} = $text; |
1268 | $self->{text} = $text; |
1031 | $self->{layout}->set_width ($self->{w}); |
|
|
1032 | $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); |
|
|
1033 | |
1269 | |
1034 | $text =~ s/./*/g if $self->{hidden}; |
1270 | $text =~ s/./*/g if $self->{hidden}; |
|
|
1271 | $self->{layout}->set_text ("$text "); |
1035 | |
1272 | |
1036 | $self->{layout}->set_markup ($self->escape_text ($text) . " "); |
1273 | $self->emit (changed => $self->{text}); |
|
|
1274 | } |
1037 | |
1275 | |
1038 | $text = substr $text, 0, $self->{cursor}; |
1276 | sub get_text { |
1039 | utf8::encode $text; |
1277 | $_[0]{text} |
1040 | |
|
|
1041 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
1042 | } |
1278 | } |
1043 | |
1279 | |
1044 | sub size_request { |
1280 | sub size_request { |
1045 | my ($self) = @_; |
1281 | my ($self) = @_; |
1046 | |
1282 | |
… | |
… | |
1048 | |
1284 | |
1049 | ($w + 1, $h) # add 1 for cursor |
1285 | ($w + 1, $h) # add 1 for cursor |
1050 | } |
1286 | } |
1051 | |
1287 | |
1052 | sub size_allocate { |
1288 | sub size_allocate { |
1053 | my ($self, $x, $y, $w, $h) = @_; |
1289 | my ($self, $w, $h) = @_; |
1054 | |
1290 | |
1055 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
1291 | $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text |
1056 | |
|
|
1057 | $self->_set_text ($self->{text}); |
|
|
1058 | } |
1292 | } |
1059 | |
1293 | |
1060 | sub set_text { |
1294 | sub set_text { |
1061 | my ($self, $text) = @_; |
1295 | my ($self, $text) = @_; |
1062 | |
1296 | |
… | |
… | |
1066 | } |
1300 | } |
1067 | |
1301 | |
1068 | sub key_down { |
1302 | sub key_down { |
1069 | my ($self, $ev) = @_; |
1303 | my ($self, $ev) = @_; |
1070 | |
1304 | |
1071 | my $mod = $ev->key_mod; |
1305 | my $mod = $ev->{mod}; |
1072 | my $sym = $ev->key_sym; |
1306 | my $sym = $ev->{sym}; |
1073 | |
|
|
1074 | my $uni = $ev->key_unicode; |
1307 | my $uni = $ev->{unicode}; |
1075 | |
1308 | |
1076 | my $text = $self->get_text; |
1309 | my $text = $self->get_text; |
1077 | |
1310 | |
1078 | if ($sym == SDLK_BACKSPACE) { |
1311 | if ($sym == 8) { |
1079 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1312 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1080 | } elsif ($sym == SDLK_DELETE) { |
1313 | } elsif ($sym == 127) { |
1081 | substr $text, $self->{cursor}, 1, ""; |
1314 | substr $text, $self->{cursor}, 1, ""; |
1082 | } elsif ($sym == SDLK_LEFT) { |
1315 | } elsif ($sym == CFClient::SDLK_LEFT) { |
1083 | --$self->{cursor} if $self->{cursor}; |
1316 | --$self->{cursor} if $self->{cursor}; |
1084 | } elsif ($sym == SDLK_RIGHT) { |
1317 | } elsif ($sym == CFClient::SDLK_RIGHT) { |
1085 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1318 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1086 | } elsif ($sym == SDLK_HOME) { |
1319 | } elsif ($sym == CFClient::SDLK_HOME) { |
1087 | $self->{cursor} = 0; |
1320 | $self->{cursor} = 0; |
1088 | } elsif ($sym == SDLK_END) { |
1321 | } elsif ($sym == CFClient::SDLK_END) { |
1089 | $self->{cursor} = length $text; |
1322 | $self->{cursor} = length $text; |
|
|
1323 | } elsif ($sym == 27) { |
|
|
1324 | $self->emit ('escape'); |
1090 | } elsif ($uni) { |
1325 | } elsif ($uni) { |
1091 | substr $text, $self->{cursor}++, 0, chr $uni; |
1326 | substr $text, $self->{cursor}++, 0, chr $uni; |
1092 | } |
1327 | } |
1093 | |
1328 | |
1094 | $self->_set_text ($text); |
1329 | $self->_set_text ($text); |
… | |
… | |
1148 | |
1383 | |
1149 | $self->SUPER::_draw; |
1384 | $self->SUPER::_draw; |
1150 | |
1385 | |
1151 | #TODO: force update every cursor change :( |
1386 | #TODO: force update every cursor change :( |
1152 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
1387 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1388 | |
|
|
1389 | unless (exists $self->{cur_h}) { |
|
|
1390 | my $text = substr $self->{text}, 0, $self->{cursor}; |
|
|
1391 | utf8::encode $text; |
|
|
1392 | |
|
|
1393 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) |
|
|
1394 | } |
|
|
1395 | |
1153 | glColor @{$self->{fg}}; |
1396 | glColor @{$self->{fg}}; |
1154 | glBegin GL_LINES; |
1397 | glBegin GL_LINES; |
1155 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1156 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
1398 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; |
|
|
1399 | glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
1157 | glEnd; |
1400 | glEnd; |
1158 | } |
1401 | } |
1159 | } |
1402 | } |
1160 | |
1403 | |
|
|
1404 | package CFClient::UI::Entry; |
|
|
1405 | |
|
|
1406 | our @ISA = CFClient::UI::EntryBase::; |
|
|
1407 | |
|
|
1408 | use CFClient::OpenGL; |
|
|
1409 | |
|
|
1410 | sub key_down { |
|
|
1411 | my ($self, $ev) = @_; |
|
|
1412 | |
|
|
1413 | my $sym = $ev->{sym}; |
|
|
1414 | |
|
|
1415 | if ($sym == 13) { |
|
|
1416 | $self->emit (activate => $self->get_text); |
|
|
1417 | $self->update; |
|
|
1418 | |
|
|
1419 | } else { |
|
|
1420 | $self->SUPER::key_down ($ev); |
|
|
1421 | } |
|
|
1422 | |
|
|
1423 | } |
|
|
1424 | |
1161 | ############################################################################# |
1425 | ############################################################################# |
1162 | |
1426 | |
1163 | package CFClient::UI::Button; |
1427 | package CFClient::UI::Button; |
1164 | |
1428 | |
1165 | our @ISA = CFClient::UI::Label::; |
1429 | our @ISA = CFClient::UI::Label::; |
1166 | |
1430 | |
1167 | use SDL; |
1431 | use CFClient::OpenGL; |
1168 | use SDL::OpenGL; |
|
|
1169 | |
1432 | |
1170 | my @tex = |
1433 | my @tex = |
1171 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
1434 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1172 | qw(b1_button_active.png); |
1435 | qw(b1_button_active.png); |
1173 | |
1436 | |
1174 | sub new { |
1437 | sub new { |
1175 | my $class = shift; |
1438 | my $class = shift; |
1176 | |
1439 | |
1177 | $class->SUPER::new ( |
1440 | $class->SUPER::new ( |
1178 | padding => 4, |
1441 | padding => 4, |
1179 | fg => [1, 1, 1], |
1442 | fg => [1, 1, 1], |
1180 | bg => [1, 1, 1, 0.2], |
1443 | bg => [1, 1, 1, 0.2], |
1181 | active_fg => [1, 1, 0], |
1444 | active_fg => [0, 0, 1], |
1182 | can_hover => 1, |
1445 | can_hover => 1, |
|
|
1446 | align => 0, |
|
|
1447 | valign => 0, |
|
|
1448 | can_events => 1, |
1183 | @_ |
1449 | @_ |
1184 | ) |
1450 | ) |
1185 | } |
1451 | } |
1186 | |
1452 | |
1187 | sub button_up { |
1453 | sub button_up { |
… | |
… | |
1195 | |
1461 | |
1196 | sub _draw { |
1462 | sub _draw { |
1197 | my ($self) = @_; |
1463 | my ($self) = @_; |
1198 | |
1464 | |
1199 | local $self->{fg} = $self->{fg}; |
1465 | local $self->{fg} = $self->{fg}; |
1200 | my $tex = $tex[0]; |
|
|
1201 | |
|
|
1202 | glEnable GL_BLEND; |
|
|
1203 | glEnable GL_TEXTURE_2D; |
|
|
1204 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1205 | |
1466 | |
1206 | if ($GRAB == $self) { |
1467 | if ($GRAB == $self) { |
1207 | $self->{fg} = $self->{active_fg}; |
1468 | $self->{fg} = $self->{active_fg}; |
1208 | } |
1469 | } |
1209 | |
1470 | |
1210 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
1471 | glEnable GL_BLEND; |
|
|
1472 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1473 | glEnable GL_TEXTURE_2D; |
1211 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1474 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1475 | glColor 0, 0, 0, 1; |
1212 | |
1476 | |
1213 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
1477 | $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h}); |
1214 | |
1478 | |
1215 | glDisable GL_TEXTURE_2D; |
1479 | glDisable GL_TEXTURE_2D; |
1216 | glDisable GL_BLEND; |
1480 | glDisable GL_BLEND; |
1217 | |
1481 | |
1218 | $self->SUPER::_draw; |
1482 | $self->SUPER::_draw; |
… | |
… | |
1222 | |
1486 | |
1223 | package CFClient::UI::CheckBox; |
1487 | package CFClient::UI::CheckBox; |
1224 | |
1488 | |
1225 | our @ISA = CFClient::UI::DrawBG::; |
1489 | our @ISA = CFClient::UI::DrawBG::; |
1226 | |
1490 | |
1227 | use SDL; |
1491 | my @tex = |
1228 | use SDL::OpenGL; |
1492 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1493 | qw(c1_checkbox_bg.png c1_checkbox_active.png); |
|
|
1494 | |
|
|
1495 | use CFClient::OpenGL; |
1229 | |
1496 | |
1230 | sub new { |
1497 | sub new { |
1231 | my $class = shift; |
1498 | my $class = shift; |
1232 | |
1499 | |
1233 | $class->SUPER::new ( |
1500 | $class->SUPER::new ( |
… | |
… | |
1244 | my ($self) = @_; |
1511 | my ($self) = @_; |
1245 | |
1512 | |
1246 | ($self->{padding} * 2 + 6) x 2 |
1513 | ($self->{padding} * 2 + 6) x 2 |
1247 | } |
1514 | } |
1248 | |
1515 | |
1249 | sub size_allocate { |
|
|
1250 | my ($self, $x, $y, $w, $h) = @_; |
|
|
1251 | |
|
|
1252 | $self->_size_allocate ($x, $y, $w, $h); |
|
|
1253 | } |
|
|
1254 | |
|
|
1255 | sub button_down { |
1516 | sub button_down { |
1256 | my ($self, $ev, $x, $y) = @_; |
1517 | my ($self, $ev, $x, $y) = @_; |
1257 | |
1518 | |
1258 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1519 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1259 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1520 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
… | |
… | |
1268 | $self->SUPER::_draw; |
1529 | $self->SUPER::_draw; |
1269 | |
1530 | |
1270 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1531 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1271 | |
1532 | |
1272 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1533 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1273 | |
1534 | |
1274 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1535 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1275 | |
1536 | |
1276 | glBegin GL_LINE_LOOP; |
1537 | glEnable GL_BLEND; |
1277 | glVertex 0 , 0; |
1538 | glEnable GL_TEXTURE_2D; |
1278 | glVertex 0 , $s; |
1539 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1279 | glVertex $s, $s; |
1540 | |
1280 | glVertex $s, 0; |
1541 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
|
|
1542 | |
|
|
1543 | $tex->draw_quad (0, 0, $s, $s); |
|
|
1544 | |
|
|
1545 | glDisable GL_TEXTURE_2D; |
|
|
1546 | glDisable GL_BLEND; |
|
|
1547 | } |
|
|
1548 | |
|
|
1549 | ############################################################################# |
|
|
1550 | |
|
|
1551 | package CFClient::UI::Image; |
|
|
1552 | |
|
|
1553 | our @ISA = CFClient::UI::Base::; |
|
|
1554 | |
|
|
1555 | use CFClient::OpenGL; |
|
|
1556 | use Carp qw/confess/; |
|
|
1557 | |
|
|
1558 | our %loaded_images; |
|
|
1559 | |
|
|
1560 | sub new { |
|
|
1561 | my $class = shift; |
|
|
1562 | |
|
|
1563 | my $self = $class->SUPER::new (can_events => 0, @_); |
|
|
1564 | |
|
|
1565 | $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; |
|
|
1566 | |
|
|
1567 | $loaded_images{$self->{image}} ||= |
|
|
1568 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; |
|
|
1569 | |
|
|
1570 | my $tex = $self->{tex} = $loaded_images{$self->{image}}; |
|
|
1571 | |
|
|
1572 | Scalar::Util::weaken $loaded_images{$self->{image}}; |
|
|
1573 | |
|
|
1574 | $self->{aspect} = $tex->{w} / $tex->{h}; |
|
|
1575 | |
|
|
1576 | $self |
|
|
1577 | } |
|
|
1578 | |
|
|
1579 | sub size_request { |
|
|
1580 | my ($self) = @_; |
|
|
1581 | |
|
|
1582 | ($self->{tex}->{w}, $self->{tex}->{h}) |
|
|
1583 | } |
|
|
1584 | |
|
|
1585 | sub _draw { |
|
|
1586 | my ($self) = @_; |
|
|
1587 | |
|
|
1588 | my $tex = $self->{tex}; |
|
|
1589 | |
|
|
1590 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1591 | |
|
|
1592 | if ($self->{rot90}) { |
|
|
1593 | glRotate 90, 0, 0, 1; |
|
|
1594 | glTranslate 0, -$self->{w}, 0; |
|
|
1595 | |
|
|
1596 | ($w, $h) = ($h, $w); |
|
|
1597 | } |
|
|
1598 | |
|
|
1599 | glEnable GL_BLEND; |
|
|
1600 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1601 | glEnable GL_TEXTURE_2D; |
|
|
1602 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1603 | |
|
|
1604 | $tex->draw_quad (0, 0, $w, $h); |
|
|
1605 | |
|
|
1606 | glDisable GL_BLEND; |
|
|
1607 | glDisable GL_TEXTURE_2D; |
|
|
1608 | } |
|
|
1609 | |
|
|
1610 | ############################################################################# |
|
|
1611 | |
|
|
1612 | package CFClient::UI::VGauge; |
|
|
1613 | |
|
|
1614 | our @ISA = CFClient::UI::Base::; |
|
|
1615 | |
|
|
1616 | use List::Util qw(min max); |
|
|
1617 | |
|
|
1618 | use CFClient::OpenGL; |
|
|
1619 | |
|
|
1620 | my %tex = ( |
|
|
1621 | food => [ |
|
|
1622 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1623 | qw/g1_food_gauge_empty.png g1_food_gauge_full.png/ |
|
|
1624 | ], |
|
|
1625 | grace => [ |
|
|
1626 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1627 | qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/ |
|
|
1628 | ], |
|
|
1629 | hp => [ |
|
|
1630 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1631 | qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/ |
|
|
1632 | ], |
|
|
1633 | mana => [ |
|
|
1634 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
|
|
1635 | qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/ |
|
|
1636 | ], |
|
|
1637 | ); |
|
|
1638 | |
|
|
1639 | # eg. VGauge->new (gauge => 'food'), default gauge: food |
|
|
1640 | sub new { |
|
|
1641 | my $class = shift; |
|
|
1642 | |
|
|
1643 | my $self = $class->SUPER::new ( |
|
|
1644 | type => 'food', |
|
|
1645 | @_ |
|
|
1646 | ); |
|
|
1647 | |
|
|
1648 | $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h}; |
|
|
1649 | |
|
|
1650 | $self |
|
|
1651 | } |
|
|
1652 | |
|
|
1653 | sub size_request { |
|
|
1654 | my ($self) = @_; |
|
|
1655 | |
|
|
1656 | #my $tex = $tex{$self->{type}}[0]; |
|
|
1657 | #@$tex{qw(w h)} |
|
|
1658 | (0, 0) |
|
|
1659 | } |
|
|
1660 | |
|
|
1661 | sub set_max { |
|
|
1662 | my ($self, $max) = @_; |
|
|
1663 | |
|
|
1664 | $self->{max_val} = $max; |
|
|
1665 | } |
|
|
1666 | |
|
|
1667 | sub set_value { |
|
|
1668 | my ($self, $val, $max) = @_; |
|
|
1669 | |
|
|
1670 | $self->set_max ($max) |
|
|
1671 | if defined $max; |
|
|
1672 | |
|
|
1673 | $max = $self->{max_val}; |
|
|
1674 | $self->{val} = $val; |
|
|
1675 | |
|
|
1676 | $self->update; |
|
|
1677 | } |
|
|
1678 | |
|
|
1679 | sub _draw { |
|
|
1680 | my ($self) = @_; |
|
|
1681 | |
|
|
1682 | my $tex = $tex{$self->{type}}; |
|
|
1683 | my ($t1, $t2, $t3) = @$tex; |
|
|
1684 | |
|
|
1685 | my ($w, $h) = ($self->{w}, $self->{h}); |
|
|
1686 | |
|
|
1687 | if ($self->{vertical}) { |
|
|
1688 | glRotate 90, 0, 0, 1; |
|
|
1689 | glTranslate 0, -$self->{w}, 0; |
|
|
1690 | |
|
|
1691 | ($w, $h) = ($h, $w); |
|
|
1692 | } |
|
|
1693 | |
|
|
1694 | my $ycut = $self->{val} / ($self->{max_val} || 1); |
|
|
1695 | |
|
|
1696 | my $ycut1 = max 0, min 1, $ycut; |
|
|
1697 | my $ycut2 = max 0, min 1, $ycut - 1; |
|
|
1698 | |
|
|
1699 | my $h1 = $self->{h} * (1 - $ycut1); |
|
|
1700 | my $h2 = $self->{h} * (1 - $ycut2); |
|
|
1701 | |
|
|
1702 | glEnable GL_BLEND; |
|
|
1703 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1704 | glEnable GL_TEXTURE_2D; |
|
|
1705 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
1706 | |
|
|
1707 | glBindTexture GL_TEXTURE_2D, $t1->{name}; |
|
|
1708 | glBegin GL_QUADS; |
|
|
1709 | glTexCoord 0 , 0; glVertex 0 , 0; |
|
|
1710 | glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1711 | glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1712 | glTexCoord $t1->{s}, 0; glVertex $w, 0; |
1281 | glEnd; |
1713 | glEnd; |
1282 | |
1714 | |
1283 | if ($self->{state}) { |
1715 | my $ycut1 = List::Util::min 1, $ycut; |
|
|
1716 | glBindTexture GL_TEXTURE_2D, $t2->{name}; |
|
|
1717 | glBegin GL_QUADS; |
|
|
1718 | glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1; |
|
|
1719 | glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2; |
|
|
1720 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2; |
|
|
1721 | glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1; |
|
|
1722 | glEnd; |
|
|
1723 | |
|
|
1724 | if ($t3) { |
|
|
1725 | glBindTexture GL_TEXTURE_2D, $t3->{name}; |
1284 | glBegin GL_LINES; |
1726 | glBegin GL_QUADS; |
1285 | glVertex 0 , 0; |
1727 | glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2; |
1286 | glVertex $s, $s; |
1728 | glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h}; |
1287 | glVertex $s, 0; |
1729 | glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h}; |
1288 | glVertex 0 , $s; |
1730 | glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2; |
1289 | glEnd; |
1731 | glEnd; |
1290 | } |
1732 | } |
|
|
1733 | |
|
|
1734 | glDisable GL_BLEND; |
|
|
1735 | glDisable GL_TEXTURE_2D; |
|
|
1736 | } |
|
|
1737 | |
|
|
1738 | ############################################################################# |
|
|
1739 | |
|
|
1740 | package CFClient::UI::Gauge; |
|
|
1741 | |
|
|
1742 | our @ISA = CFClient::UI::VBox::; |
|
|
1743 | |
|
|
1744 | sub new { |
|
|
1745 | my ($class, %arg) = @_; |
|
|
1746 | |
|
|
1747 | my $self = $class->SUPER::new ( |
|
|
1748 | tooltip => $arg{type}, |
|
|
1749 | %arg, |
|
|
1750 | ); |
|
|
1751 | |
|
|
1752 | $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1); |
|
|
1753 | $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1); |
|
|
1754 | $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1); |
|
|
1755 | |
|
|
1756 | $self |
|
|
1757 | } |
|
|
1758 | |
|
|
1759 | sub set_fontsize { |
|
|
1760 | my ($self, $fsize) = @_; |
|
|
1761 | |
|
|
1762 | $self->{value}->set_fontsize ($fsize); |
|
|
1763 | $self->{max} ->set_fontsize ($fsize); |
|
|
1764 | } |
|
|
1765 | |
|
|
1766 | sub set_value { |
|
|
1767 | my ($self, $val, $max) = @_; |
|
|
1768 | |
|
|
1769 | $self->set_max ($max) |
|
|
1770 | if defined $max; |
|
|
1771 | |
|
|
1772 | $self->{gauge}->set_value ($val, $max); |
|
|
1773 | $self->{value}->set_text ($val); |
|
|
1774 | } |
|
|
1775 | |
|
|
1776 | sub set_max { |
|
|
1777 | my ($self, $max) = @_; |
|
|
1778 | |
|
|
1779 | $self->{gauge}->set_max ($max); |
|
|
1780 | $self->{max}->set_text ($max); |
1291 | } |
1781 | } |
1292 | |
1782 | |
1293 | ############################################################################# |
1783 | ############################################################################# |
1294 | |
1784 | |
1295 | package CFClient::UI::Slider; |
1785 | package CFClient::UI::Slider; |
1296 | |
1786 | |
1297 | use strict; |
1787 | use strict; |
1298 | |
1788 | |
1299 | use SDL::OpenGL; |
1789 | use CFClient::OpenGL; |
1300 | |
1790 | |
1301 | our @ISA = CFClient::UI::DrawBG::; |
1791 | our @ISA = CFClient::UI::DrawBG::; |
|
|
1792 | |
|
|
1793 | my @tex = |
|
|
1794 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
|
|
1795 | qw(s1_slider.png s1_slider_bg.png); |
1302 | |
1796 | |
1303 | sub new { |
1797 | sub new { |
1304 | my $class = shift; |
1798 | my $class = shift; |
1305 | |
1799 | |
1306 | # range [value, low, high, page] |
1800 | # range [value, low, high, page] |
… | |
… | |
1310 | # TODO: calculations are off |
1804 | # TODO: calculations are off |
1311 | my $self = $class->SUPER::new ( |
1805 | my $self = $class->SUPER::new ( |
1312 | fg => [1, 1, 1], |
1806 | fg => [1, 1, 1], |
1313 | active_fg => [0, 0, 0], |
1807 | active_fg => [0, 0, 0], |
1314 | range => [0, 0, 100, 10], |
1808 | range => [0, 0, 100, 10], |
1315 | req_w => 40, |
1809 | req_w => 20, |
1316 | req_h => 13, |
1810 | req_h => 20, |
1317 | vertical => 0, |
1811 | vertical => 0, |
1318 | can_hover => 1, |
1812 | can_hover => 1, |
|
|
1813 | inner_pad => 5, |
1319 | @_ |
1814 | @_ |
1320 | ); |
1815 | ); |
1321 | |
1816 | |
1322 | $self |
1817 | $self |
1323 | } |
1818 | } |
… | |
… | |
1344 | if ($GRAB == $self) { |
1839 | if ($GRAB == $self) { |
1345 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1840 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1346 | |
1841 | |
1347 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1842 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
1348 | |
1843 | |
|
|
1844 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1845 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1846 | |
|
|
1847 | $x -= $inner_pad_px; # substract the padding |
1349 | $x = $x * ($hi - $lo) / $w + $lo; |
1848 | $x = $x * ($hi - $lo) / $inner_w + $lo; |
1350 | $x = $lo if $x < $lo; |
1849 | $x = $lo if $x < $lo; |
1351 | $x = $hi - $page if $x > $hi - $page; |
1850 | $x = $hi - $page if $x > $hi - $page; |
1352 | $self->{range}[0] = $x; |
1851 | $self->{range}[0] = $x; |
1353 | |
1852 | |
1354 | $self->emit (changed => $x); |
1853 | $self->emit (changed => $x); |
1355 | $self->update; |
1854 | $self->update; |
1356 | } |
1855 | } |
|
|
1856 | } |
|
|
1857 | |
|
|
1858 | # the inner_* stuff is for generating a padding for the slider handle, |
|
|
1859 | # so that the handle doesn't leave the texture. This calculation isn't 100% |
|
|
1860 | # correct propably, but it does the job for now |
|
|
1861 | sub _calc_inner_pad_px { |
|
|
1862 | my ($self, $w) = @_; |
|
|
1863 | ($w / 100) * $self->{inner_pad} # % to pixels |
1357 | } |
1864 | } |
1358 | |
1865 | |
1359 | sub _draw { |
1866 | sub _draw { |
1360 | my ($self) = @_; |
1867 | my ($self) = @_; |
1361 | |
1868 | |
… | |
… | |
1375 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1882 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
1376 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1883 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
1377 | |
1884 | |
1378 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1885 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
1379 | |
1886 | |
|
|
1887 | $hi = $value + 1 if $lo == $hi; |
|
|
1888 | |
|
|
1889 | my $inner_pad_px = $self->_calc_inner_pad_px ($w); |
|
|
1890 | my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right |
|
|
1891 | |
1380 | $page = int $page * $w / ($hi - $lo); |
1892 | $page = int $page * $inner_w / ($hi - $lo); |
1381 | $value = int +($value - $lo) * $w / ($hi - $lo); |
1893 | $value = int +($value - $lo) * $inner_w / ($hi - $lo); |
1382 | |
1894 | |
1383 | $w -= $page; |
1895 | $w -= $page; |
1384 | $page &= ~1; |
1896 | $page &= ~1; |
1385 | glTranslate $page * 0.5, 0, 0; |
1897 | glTranslate $page * 0.5, 0, 0; |
1386 | $page ||= 2; |
1898 | $page ||= 2; |
1387 | |
1899 | |
1388 | glColor @$fg; |
|
|
1389 | glBegin GL_LINES; |
|
|
1390 | glVertex 0, 0; glVertex 0, $h; |
|
|
1391 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1392 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1393 | glEnd; |
|
|
1394 | |
|
|
1395 | my $knob_a = $value - $page * 0.5; |
1900 | my $knob_a = $inner_pad_px + ($value - $page * 0.5); |
1396 | my $knob_b = $value + $page * 0.5; |
1901 | my $knob_b = $inner_pad_px + ($value + $page * 0.5); |
1397 | |
1902 | |
1398 | glBegin GL_QUADS; |
1903 | glEnable GL_BLEND; |
1399 | glColor @$fg; |
1904 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1400 | glVertex $knob_a, 0; |
1905 | glEnable GL_TEXTURE_2D; |
1401 | glVertex $knob_a, $h; |
1906 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1402 | glVertex $knob_b, $h; |
|
|
1403 | glVertex $knob_b, 0; |
|
|
1404 | |
1907 | |
1405 | if ($knob_a < $knob_b - 2) { |
1908 | # draw background |
1406 | glColor @$bg; |
1909 | $tex[1]->draw_quad (0, 0, $w, $h); |
1407 | glVertex $knob_a + 1, 1; |
1910 | |
1408 | glVertex $knob_a + 1, $h - 1; |
1911 | # draw handle |
1409 | glVertex $knob_b - 1, $h - 1; |
1912 | $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h); |
1410 | glVertex $knob_b - 1, 1; |
1913 | |
1411 | } |
1914 | glDisable GL_BLEND; |
1412 | glEnd; |
1915 | glDisable GL_TEXTURE_2D; |
1413 | } |
1916 | } |
1414 | |
1917 | |
1415 | ############################################################################# |
1918 | ############################################################################# |
1416 | |
1919 | |
1417 | package CFClient::UI::TextView; |
1920 | package CFClient::UI::TextView; |
1418 | |
1921 | |
1419 | our @ISA = CFClient::UI::HBox::; |
1922 | our @ISA = CFClient::UI::HBox::; |
1420 | |
1923 | |
1421 | use SDL::OpenGL; |
1924 | use CFClient::OpenGL; |
1422 | |
1925 | |
1423 | sub new { |
1926 | sub new { |
1424 | my $class = shift; |
1927 | my $class = shift; |
1425 | |
1928 | |
1426 | my $self = $class->SUPER::new ( |
1929 | my $self = $class->SUPER::new ( |
|
|
1930 | fontsize => 1, |
|
|
1931 | #font => default_font |
|
|
1932 | @_, |
|
|
1933 | |
|
|
1934 | layout => (new CFClient::Layout), |
1427 | par => [], |
1935 | par => [], |
1428 | @_, |
1936 | height => 0, |
1429 | children => [ |
1937 | children => [ |
1430 | (new CFClient::UI::Empty expand => 1), |
1938 | (new CFClient::UI::Empty expand => 1), |
1431 | (new CFClient::UI::Slider vertical => 1), |
1939 | (new CFClient::UI::Slider vertical => 1), |
1432 | ], |
1940 | ], |
1433 | ); |
1941 | ); |
1434 | |
1942 | |
|
|
1943 | $self->{children}[1]->connect (changed => sub { |
|
|
1944 | $self->update; |
|
|
1945 | }); |
|
|
1946 | |
1435 | $self |
1947 | $self |
|
|
1948 | } |
|
|
1949 | |
|
|
1950 | sub set_fontsize { |
|
|
1951 | my ($self, $fontsize) = @_; |
|
|
1952 | |
|
|
1953 | $self->{fontsize} = $fontsize; |
|
|
1954 | $self->reflow; |
|
|
1955 | } |
|
|
1956 | |
|
|
1957 | sub text_height { |
|
|
1958 | my ($self, $text) = @_; |
|
|
1959 | |
|
|
1960 | my $layout = $self->{layout}; |
|
|
1961 | |
|
|
1962 | $layout->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1963 | $layout->set_width ($self->{w}); |
|
|
1964 | $layout->set_text ($text); |
|
|
1965 | |
|
|
1966 | ($layout->size)[1] |
|
|
1967 | } |
|
|
1968 | |
|
|
1969 | sub reflow { |
|
|
1970 | my ($self) = @_; |
|
|
1971 | |
|
|
1972 | $self->{need_reflow}++; |
|
|
1973 | $self->update; |
|
|
1974 | } |
|
|
1975 | |
|
|
1976 | sub size_allocate { |
|
|
1977 | my ($self, $w, $h) = @_; |
|
|
1978 | |
|
|
1979 | $self->SUPER::size_allocate ($w, $h); |
|
|
1980 | |
|
|
1981 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
|
|
1982 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
|
|
1983 | $self->{layout}->set_width ($self->{children}[0]{w}); |
|
|
1984 | |
|
|
1985 | $self->reflow; |
1436 | } |
1986 | } |
1437 | |
1987 | |
1438 | sub add_paragraph { |
1988 | sub add_paragraph { |
1439 | my ($self, $color, $text) = @_; |
1989 | my ($self, $color, $text) = @_; |
1440 | |
1990 | |
|
|
1991 | #TODO: intelligently "reformat" paragraph |
|
|
1992 | |
|
|
1993 | my $height = $self->text_height ($text); |
|
|
1994 | |
|
|
1995 | $self->{height} += $height; |
|
|
1996 | |
1441 | push @{$self->{par}}, [$color, $text]; |
1997 | push @{$self->{par}}, [$height, $color, $text]; |
1442 | my $count = scalar @{$self->{par}}; |
1998 | |
1443 | $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; |
1999 | $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}]; |
1444 | $self->{children}[1]->update; |
2000 | $self->{children}[1]->update; |
1445 | } |
2001 | } |
1446 | |
2002 | |
|
|
2003 | sub update { |
|
|
2004 | my ($self) = @_; |
|
|
2005 | |
|
|
2006 | $self->SUPER::update; |
|
|
2007 | |
|
|
2008 | return unless $self->{h} > 0; |
|
|
2009 | |
|
|
2010 | delete $self->{texture}; |
|
|
2011 | |
|
|
2012 | $ROOT->on_refresh ($self, sub { |
|
|
2013 | if (delete $self->{need_reflow}) { |
|
|
2014 | my $height = 0; |
|
|
2015 | |
|
|
2016 | $height += $_->[0] = $self->text_height ($_->[2]) |
|
|
2017 | for @{$self->{par}}; |
|
|
2018 | |
|
|
2019 | $self->{height} = $height; |
|
|
2020 | |
|
|
2021 | $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}]; |
|
|
2022 | |
|
|
2023 | delete $self->{texture}; |
|
|
2024 | } |
|
|
2025 | |
|
|
2026 | $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { |
|
|
2027 | glClearColor 0, 0, 0, 1; |
|
|
2028 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2029 | |
|
|
2030 | glEnable GL_BLEND; |
|
|
2031 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
2032 | glEnable GL_TEXTURE_2D; |
|
|
2033 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
2034 | |
|
|
2035 | my $top = int $self->{children}[1]{range}[0]; |
|
|
2036 | |
|
|
2037 | my $y0 = $top; |
|
|
2038 | my $y1 = $top + $self->{h}; |
|
|
2039 | |
|
|
2040 | my $y = 0; |
|
|
2041 | |
|
|
2042 | my $layout = $self->{layout}; |
|
|
2043 | |
|
|
2044 | $layout->set_font ($self->{font}) if $self->{font}; |
|
|
2045 | |
|
|
2046 | for my $par (@{$self->{par}}) { |
|
|
2047 | my $h = $par->[0]; |
|
|
2048 | |
|
|
2049 | if ($y0 < $y + $h && $y < $y1) { |
|
|
2050 | $layout->set_text ($par->[2]); |
|
|
2051 | |
|
|
2052 | glColor @{ $par->[1] }; |
|
|
2053 | my ($W, $H) = $layout->size; |
|
|
2054 | CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); |
|
|
2055 | } |
|
|
2056 | |
|
|
2057 | $y += $h; |
|
|
2058 | } |
|
|
2059 | |
|
|
2060 | glDisable GL_TEXTURE_2D; |
|
|
2061 | glDisable GL_BLEND; |
|
|
2062 | }; |
|
|
2063 | }); |
|
|
2064 | } |
|
|
2065 | |
1447 | sub _draw { |
2066 | sub _draw { |
1448 | my ($self) = @_; |
2067 | my ($self) = @_; |
1449 | |
2068 | |
1450 | my $par = $self->{par}; |
2069 | if ($self->{texture}) { |
1451 | |
|
|
1452 | my $bottom = (scalar @$par) - 1; |
|
|
1453 | |
|
|
1454 | my $w = $self->{children}[0]{w}; |
|
|
1455 | my $y = $self->{h}; |
|
|
1456 | |
|
|
1457 | glEnable GL_BLEND; |
|
|
1458 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1459 | glEnable GL_TEXTURE_2D; |
2070 | glEnable GL_TEXTURE_2D; |
1460 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
2071 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1461 | |
2072 | $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); |
1462 | # TODO: everything! |
|
|
1463 | while ($y > 0 && $bottom >= 0) { |
|
|
1464 | my $par = $par->[$bottom--]; |
|
|
1465 | |
|
|
1466 | my $layout = new CFClient::Layout; |
|
|
1467 | |
|
|
1468 | $layout->set_height ($::FONTSIZE); |
|
|
1469 | $layout->set_width ($w); |
|
|
1470 | $layout->set_text ($par->[1]); |
|
|
1471 | my $tex = new_from_layout CFClient::Texture $layout; |
|
|
1472 | $y -= $tex->{h}; |
|
|
1473 | |
|
|
1474 | glColor @{ $par->[0] }; |
|
|
1475 | $tex->draw_quad (0, $y); |
|
|
1476 | } |
|
|
1477 | |
|
|
1478 | glDisable GL_TEXTURE_2D; |
2073 | glDisable GL_TEXTURE_2D; |
1479 | glDisable GL_BLEND; |
|
|
1480 | } |
|
|
1481 | |
|
|
1482 | ############################################################################# |
|
|
1483 | |
|
|
1484 | package CFClient::UI::MapWidget; |
|
|
1485 | |
|
|
1486 | use strict; |
|
|
1487 | |
|
|
1488 | use List::Util qw(min max); |
|
|
1489 | |
|
|
1490 | use SDL; |
|
|
1491 | use SDL::OpenGL; |
|
|
1492 | |
|
|
1493 | our @ISA = CFClient::UI::Base::; |
|
|
1494 | |
|
|
1495 | sub new { |
|
|
1496 | my $class = shift; |
|
|
1497 | |
|
|
1498 | $class->SUPER::new ( |
|
|
1499 | z => -1, |
|
|
1500 | can_focus => 1, |
|
|
1501 | list => (glGenLists 1), |
|
|
1502 | @_ |
|
|
1503 | ) |
2074 | } |
1504 | } |
|
|
1505 | |
2075 | |
1506 | sub key_down { |
2076 | $self->{children}[1]->draw; |
1507 | print "MAPKEYDOWN\n"; |
|
|
1508 | } |
|
|
1509 | |
2077 | |
1510 | sub key_up { |
|
|
1511 | } |
|
|
1512 | |
|
|
1513 | sub size_request { |
|
|
1514 | ( |
|
|
1515 | 1 + 32 * int $::WIDTH / 32, |
|
|
1516 | 1 + 32 * int $::HEIGHT / 32, |
|
|
1517 | ) |
|
|
1518 | } |
|
|
1519 | |
|
|
1520 | sub update { |
|
|
1521 | my ($self) = @_; |
|
|
1522 | |
|
|
1523 | $self->{need_update} = 1; |
|
|
1524 | $self->SUPER::update; |
|
|
1525 | } |
|
|
1526 | |
|
|
1527 | sub draw { |
|
|
1528 | my ($self) = @_; |
|
|
1529 | |
|
|
1530 | $self->{need_update}++;#d# |
|
|
1531 | if (delete $self->{need_update}) { |
|
|
1532 | glNewList $self->{list}, GL_COMPILE; |
|
|
1533 | |
|
|
1534 | my $sw = int $::WIDTH / 32; |
|
|
1535 | my $sh = int $::HEIGHT / 32; |
|
|
1536 | |
|
|
1537 | if ($::MAP) { |
|
|
1538 | my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh); |
|
|
1539 | |
|
|
1540 | if ($::CFG->{fow_enable}) { |
|
|
1541 | if ($::CFG->{fow_smooth}) { # smooth fog of war |
|
|
1542 | glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER; |
|
|
1543 | glConvolutionFilter2D |
|
|
1544 | GL_CONVOLUTION_2D, |
|
|
1545 | GL_ALPHA, |
|
|
1546 | 3, 3, |
|
|
1547 | GL_ALPHA, GL_FLOAT, |
|
|
1548 | pack "f*", |
|
|
1549 | 0.1, 0.1, 0.1, |
|
|
1550 | 0.1, 0.2, 0.1, |
|
|
1551 | 0.1, 0.1, 0.1, |
|
|
1552 | ; |
|
|
1553 | glEnable GL_CONVOLUTION_2D; |
|
|
1554 | } |
|
|
1555 | |
|
|
1556 | my $tex = new CFClient::Texture |
|
|
1557 | w => $w, |
|
|
1558 | h => $h, |
|
|
1559 | data => $data, |
|
|
1560 | internalformat => GL_ALPHA, |
|
|
1561 | format => GL_ALPHA; |
|
|
1562 | |
|
|
1563 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
1564 | |
|
|
1565 | glEnable GL_BLEND; |
|
|
1566 | glEnable GL_TEXTURE_2D; |
|
|
1567 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
1568 | |
|
|
1569 | glColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
1570 | $tex->draw_quad (0, 0, $w * 32, $h * 32); |
|
|
1571 | |
|
|
1572 | glDisable GL_TEXTURE_2D; |
|
|
1573 | glDisable GL_BLEND; |
|
|
1574 | } |
|
|
1575 | } |
|
|
1576 | |
|
|
1577 | glEndList; |
|
|
1578 | } |
|
|
1579 | |
|
|
1580 | glCallList $self->{list}; |
|
|
1581 | |
|
|
1582 | if ($FOCUS != $self) { |
|
|
1583 | glColor 64/255, 64/255, 64/255; |
|
|
1584 | glLogicOp GL_AND; |
|
|
1585 | glEnable GL_COLOR_LOGIC_OP; |
|
|
1586 | glBegin GL_QUADS; |
|
|
1587 | glVertex 0, 0; |
|
|
1588 | glVertex 0, $::HEIGHT; |
|
|
1589 | glVertex $::WIDTH, $::HEIGHT; |
|
|
1590 | glVertex $::WIDTH, 0; |
|
|
1591 | glEnd; |
|
|
1592 | glDisable GL_COLOR_LOGIC_OP; |
|
|
1593 | } |
|
|
1594 | } |
|
|
1595 | |
|
|
1596 | my %DIR = ( |
|
|
1597 | SDLK_KP8, [1, "north"], |
|
|
1598 | SDLK_KP9, [2, "northeast"], |
|
|
1599 | SDLK_KP6, [3, "east"], |
|
|
1600 | SDLK_KP3, [4, "southeast"], |
|
|
1601 | SDLK_KP2, [5, "south"], |
|
|
1602 | SDLK_KP1, [6, "southwest"], |
|
|
1603 | SDLK_KP4, [7, "west"], |
|
|
1604 | SDLK_KP7, [8, "northwest"], |
|
|
1605 | |
|
|
1606 | SDLK_UP, [1, "north"], |
|
|
1607 | SDLK_RIGHT, [3, "east"], |
|
|
1608 | SDLK_DOWN, [5, "south"], |
|
|
1609 | SDLK_LEFT, [7, "west"], |
|
|
1610 | ); |
|
|
1611 | |
|
|
1612 | sub key_down { |
|
|
1613 | my ($self, $ev) = @_; |
|
|
1614 | |
|
|
1615 | my $mod = $ev->key_mod; |
|
|
1616 | my $sym = $ev->key_sym; |
|
|
1617 | |
|
|
1618 | if ($sym == SDLK_KP5) { |
|
|
1619 | $::CONN->user_send ("command stay fire"); |
|
|
1620 | } elsif ($sym == SDLK_a) { |
|
|
1621 | $::CONN->user_send ("command apply"); |
|
|
1622 | } elsif (exists $DIR{$sym}) { |
|
|
1623 | if ($mod & KMOD_SHIFT) { |
|
|
1624 | $self->{shft}++; |
|
|
1625 | $::CONN->user_send ("command fire $DIR{$sym}[0]"); |
|
|
1626 | } elsif ($mod & KMOD_CTRL) { |
|
|
1627 | $self->{ctrl}++; |
|
|
1628 | $::CONN->user_send ("command run $DIR{$sym}[0]"); |
|
|
1629 | } else { |
|
|
1630 | $::CONN->user_send ("command $DIR{$sym}[1]"); |
|
|
1631 | } |
|
|
1632 | } |
|
|
1633 | } |
|
|
1634 | |
|
|
1635 | sub key_up { |
|
|
1636 | my ($self, $ev) = @_; |
|
|
1637 | |
|
|
1638 | my $mod = $ev->key_mod; |
|
|
1639 | my $sym = $ev->key_sym; |
|
|
1640 | |
|
|
1641 | if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { |
|
|
1642 | $::CONN->user_send ("command fire_stop"); |
|
|
1643 | } |
|
|
1644 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
|
|
1645 | $::CONN->user_send ("command run_stop"); |
|
|
1646 | } |
|
|
1647 | } |
2078 | } |
1648 | |
2079 | |
1649 | ############################################################################# |
2080 | ############################################################################# |
1650 | |
2081 | |
1651 | package CFClient::UI::Animator; |
2082 | package CFClient::UI::Animator; |
1652 | |
2083 | |
1653 | use SDL::OpenGL; |
2084 | use CFClient::OpenGL; |
1654 | |
2085 | |
1655 | our @ISA = CFClient::UI::Bin::; |
2086 | our @ISA = CFClient::UI::Bin::; |
1656 | |
2087 | |
1657 | sub moveto { |
2088 | sub moveto { |
1658 | my ($self, $x, $y) = @_; |
2089 | my ($self, $x, $y) = @_; |
… | |
… | |
1698 | my $class = shift; |
2129 | my $class = shift; |
1699 | |
2130 | |
1700 | my $self = $class->SUPER::new ( |
2131 | my $self = $class->SUPER::new ( |
1701 | state => 0, |
2132 | state => 0, |
1702 | connect_activate => \&toggle_flopper, |
2133 | connect_activate => \&toggle_flopper, |
|
|
2134 | can_events => 1, |
1703 | @_ |
2135 | @_ |
1704 | ); |
2136 | ); |
1705 | |
2137 | |
1706 | if ($self->{state}) { |
2138 | if ($self->{state}) { |
1707 | $self->{state} = 0; |
2139 | $self->{state} = 0; |
… | |
… | |
1714 | sub toggle_flopper { |
2146 | sub toggle_flopper { |
1715 | my ($self) = @_; |
2147 | my ($self) = @_; |
1716 | |
2148 | |
1717 | # TODO: use animation |
2149 | # TODO: use animation |
1718 | if ($self->{state} = !$self->{state}) { |
2150 | if ($self->{state} = !$self->{state}) { |
1719 | $CFClient::UI::TOPLEVEL->add ($self->{other}); |
2151 | $CFClient::UI::ROOT->add ($self->{other}); |
1720 | $self->{other}->move ( |
2152 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
1721 | ($::WIDTH - $self->{other}{w}) * 0.5, |
2153 | $self->emit ("open"); |
1722 | ($::HEIGHT - $self->{other}{h}) * 0.5, |
|
|
1723 | ); |
|
|
1724 | } else { |
2154 | } else { |
1725 | $CFClient::UI::TOPLEVEL->remove ($self->{other}); |
2155 | $CFClient::UI::ROOT->remove ($self->{other}); |
|
|
2156 | $self->emit ("close"); |
|
|
2157 | } |
|
|
2158 | |
|
|
2159 | $self->emit (changed => $self->{state}); |
|
|
2160 | } |
|
|
2161 | |
|
|
2162 | ############################################################################# |
|
|
2163 | |
|
|
2164 | package CFClient::UI::Tooltip; |
|
|
2165 | |
|
|
2166 | our @ISA = CFClient::UI::Bin::; |
|
|
2167 | |
|
|
2168 | use CFClient::OpenGL; |
|
|
2169 | |
|
|
2170 | sub new { |
|
|
2171 | my $class = shift; |
|
|
2172 | |
|
|
2173 | $class->SUPER::new ( |
|
|
2174 | @_, |
|
|
2175 | can_events => 0, |
|
|
2176 | ) |
|
|
2177 | } |
|
|
2178 | |
|
|
2179 | sub set_text { |
|
|
2180 | my ($self, $text) = @_; |
|
|
2181 | |
|
|
2182 | $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0]; |
|
|
2183 | $self->{label}->set_text ($text); |
|
|
2184 | $self->add ($self->{label}); |
|
|
2185 | } |
|
|
2186 | |
|
|
2187 | sub size_request { |
|
|
2188 | my ($self) = @_; |
|
|
2189 | |
|
|
2190 | $self->child->set_max_size ($::WIDTH * 0.2); |
|
|
2191 | |
|
|
2192 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
|
|
2193 | |
|
|
2194 | ($w + 4, $h + 4) |
|
|
2195 | } |
|
|
2196 | |
|
|
2197 | sub _draw { |
|
|
2198 | my ($self) = @_; |
|
|
2199 | |
|
|
2200 | glPushMatrix; |
|
|
2201 | glTranslate 0.375, 0.375; |
|
|
2202 | |
|
|
2203 | my ($w, $h) = @$self{qw(w h)}; |
|
|
2204 | |
|
|
2205 | glColor 1, 0.8, 0.4; |
|
|
2206 | glBegin GL_QUADS; |
|
|
2207 | glVertex 0 , 0; |
|
|
2208 | glVertex 0 , $h; |
|
|
2209 | glVertex $w, $h; |
|
|
2210 | glVertex $w, 0; |
|
|
2211 | glEnd; |
1726 | } |
2212 | |
1727 | } |
2213 | glColor 0, 0, 0; |
|
|
2214 | glBegin GL_LINE_LOOP; |
|
|
2215 | glVertex 0 , 0; |
|
|
2216 | glVertex 0 , $h; |
|
|
2217 | glVertex $w, $h; |
|
|
2218 | glVertex $w, 0; |
|
|
2219 | glEnd; |
|
|
2220 | |
|
|
2221 | glPopMatrix; |
1728 | |
2222 | |
1729 | ############################################################################# |
2223 | glTranslate 2, 2; |
|
|
2224 | $self->SUPER::_draw; |
|
|
2225 | } |
1730 | |
2226 | |
|
|
2227 | ############################################################################# |
|
|
2228 | |
1731 | package CFClient::UI::Toplevel; |
2229 | package CFClient::UI::Root; |
1732 | |
2230 | |
1733 | our @ISA = CFClient::UI::Container::; |
2231 | our @ISA = CFClient::UI::Container::; |
|
|
2232 | |
|
|
2233 | use CFClient::OpenGL; |
|
|
2234 | |
|
|
2235 | sub check_size { |
|
|
2236 | my ($self) = @_; |
|
|
2237 | |
|
|
2238 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
|
|
2239 | } |
1734 | |
2240 | |
1735 | sub size_request { |
2241 | sub size_request { |
1736 | ($::WIDTH, $::HEIGHT) |
2242 | ($::WIDTH, $::HEIGHT) |
1737 | } |
2243 | } |
1738 | |
2244 | |
1739 | sub size_allocate { |
2245 | sub configure { |
1740 | my ($self, $x, $y, $w, $h) = @_; |
2246 | my ($self, $x, $y, $w, $h) = @_; |
1741 | |
2247 | |
1742 | $self->_size_allocate ($x, $y, $w, $h); |
2248 | $self->SUPER::configure ($x, $y, $w, $h); |
1743 | |
2249 | |
1744 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
|
|
1745 | for @{$self->{children}}; |
2250 | for my $child (@{$self->{children}}) { |
1746 | } |
2251 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
1747 | |
2252 | |
1748 | sub translate { |
2253 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2254 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2255 | $child->configure ($X, $Y, $W,$H); |
|
|
2256 | } |
|
|
2257 | } |
|
|
2258 | |
|
|
2259 | sub _topleft { |
1749 | my ($self, $x, $y) = @_; |
2260 | my ($self, $x, $y) = @_; |
1750 | |
2261 | |
1751 | ($x, $y) |
2262 | ($x, $y) |
1752 | } |
2263 | } |
1753 | |
2264 | |
1754 | sub update { |
2265 | sub update { |
1755 | my ($self) = @_; |
2266 | my ($self) = @_; |
1756 | |
2267 | |
1757 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
2268 | $self->check_size; |
1758 | ::refresh (); |
2269 | ::refresh (); |
1759 | } |
2270 | } |
1760 | |
2271 | |
1761 | sub add { |
2272 | sub add { |
1762 | my ($self, $widget) = @_; |
2273 | my ($self, $child) = @_; |
1763 | |
2274 | |
|
|
2275 | # integerize window positions |
|
|
2276 | $child->{x} = int $child->{x}; |
|
|
2277 | $child->{y} = int $child->{y}; |
|
|
2278 | |
1764 | $self->SUPER::add ($widget); |
2279 | $self->SUPER::add ($child); |
|
|
2280 | } |
1765 | |
2281 | |
1766 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
2282 | sub on_refresh { |
|
|
2283 | my ($self, $id, $cb) = @_; |
|
|
2284 | |
|
|
2285 | $self->{refresh_hook}{$id} = $cb; |
1767 | } |
2286 | } |
1768 | |
2287 | |
1769 | sub draw { |
2288 | sub draw { |
1770 | my ($self) = @_; |
2289 | my ($self) = @_; |
1771 | |
2290 | |
|
|
2291 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2292 | $_->() for values %$rcb; |
|
|
2293 | } |
|
|
2294 | |
|
|
2295 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2296 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2297 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2298 | |
|
|
2299 | glMatrixMode GL_PROJECTION; |
|
|
2300 | glLoadIdentity; |
|
|
2301 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2302 | glMatrixMode GL_MODELVIEW; |
|
|
2303 | glLoadIdentity; |
|
|
2304 | |
1772 | $self->_draw; |
2305 | $self->_draw; |
1773 | } |
2306 | } |
1774 | |
2307 | |
1775 | ############################################################################# |
2308 | ############################################################################# |
1776 | |
2309 | |
1777 | package CFClient::UI; |
2310 | package CFClient::UI; |
1778 | |
2311 | |
|
|
2312 | $ROOT = new CFClient::UI::Root; |
1779 | $TOPLEVEL = new CFClient::UI::Toplevel; |
2313 | $TOOLTIP = new CFClient::UI::Tooltip; |
1780 | |
2314 | |
1781 | 1 |
2315 | 1 |
1782 | |
2316 | |