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