… | |
… | |
7 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
9 | |
9 | |
10 | use Crossfire::Client; |
10 | use Crossfire::Client; |
11 | |
11 | |
12 | our $FOCUS; # the widget with current focus |
|
|
13 | |
|
|
14 | # class methods for events |
12 | # class methods for events |
15 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
13 | sub feed_sdl_key_down_event { $::FOCUS->key_down ($_[0]) if $::FOCUS } |
16 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
14 | sub feed_sdl_key_up_event { $::FOCUS->key_up ($_[0]) if $::FOCUS } |
17 | sub feed_sdl_button_down_event { } |
15 | sub feed_sdl_button_down_event { } |
18 | sub feed_sdl_button_up_event { } |
16 | sub feed_sdl_button_up_event { } |
19 | |
17 | |
20 | sub new { |
18 | sub new { |
21 | my $class = shift; |
19 | my $class = shift; |
… | |
… | |
46 | $self->{h} = $h; |
44 | $self->{h} = $h; |
47 | } |
45 | } |
48 | |
46 | |
49 | sub focus_in { |
47 | sub focus_in { |
50 | my ($widget) = @_; |
48 | my ($widget) = @_; |
51 | $FOCUS = $widget; |
49 | $::FOCUS = $widget; |
52 | } |
50 | } |
53 | |
51 | |
54 | sub focus_out { |
52 | sub focus_out { |
55 | my ($widget) = @_; |
53 | my ($widget) = @_; |
56 | } |
54 | } |
… | |
… | |
81 | my ($self) = @_; |
79 | my ($self) = @_; |
82 | |
80 | |
83 | glPushMatrix; |
81 | glPushMatrix; |
84 | glTranslate $self->{x}, $self->{y}, 0; |
82 | glTranslate $self->{x}, $self->{y}, 0; |
85 | $self->_draw; |
83 | $self->_draw; |
|
|
84 | if ($self == $::HOVER) { |
|
|
85 | glColor 1, 1, 1, 1; |
|
|
86 | glBegin GL_LINES; |
|
|
87 | glVertex 0, 0; |
|
|
88 | glVertex $self->{w} - 1, 0; |
|
|
89 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
90 | glVertex 0, $self->{h} - 1; |
|
|
91 | glVertex 0, 0; |
|
|
92 | glEnd; |
|
|
93 | } |
86 | glPopMatrix; |
94 | glPopMatrix; |
87 | } |
95 | } |
88 | |
96 | |
89 | sub _draw { |
97 | sub _draw { |
90 | my ($self) = @_; |
98 | my ($self) = @_; |
… | |
… | |
162 | |
170 | |
163 | @{$self->{children}} = |
171 | @{$self->{children}} = |
164 | sort { $a->{z} <=> $b->{z} } |
172 | sort { $a->{z} <=> $b->{z} } |
165 | @{$self->{children}}, $chld; |
173 | @{$self->{children}}, $chld; |
166 | |
174 | |
167 | $self->size_allocate ($self->{w}, $self->{h}); |
175 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
176 | if $self->{w}; #TODO: check for "realised state" |
168 | } |
177 | } |
169 | |
178 | |
170 | sub remove { |
179 | sub remove { |
171 | my ($self, $widget) = @_; |
180 | my ($self, $widget) = @_; |
172 | |
181 | |
… | |
… | |
176 | } |
185 | } |
177 | |
186 | |
178 | sub find_widget { |
187 | sub find_widget { |
179 | my ($self, $x, $y) = @_; |
188 | my ($self, $x, $y) = @_; |
180 | |
189 | |
|
|
190 | $x -= $self->{x}; |
|
|
191 | $y -= $self->{y}; |
|
|
192 | |
181 | my $res; |
193 | my $res; |
182 | |
194 | |
183 | for (@{ $self->{children} }) { |
195 | for (reverse @{ $self->{children} }) { |
184 | $res = $_->find_widget ($x, $y) |
196 | $res = $_->find_widget ($x, $y) |
185 | and return $res; |
197 | and return $res; |
186 | } |
198 | } |
187 | |
199 | |
188 | () |
200 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
189 | } |
|
|
190 | |
|
|
191 | sub size_request { |
|
|
192 | my ($self) = @_; |
|
|
193 | |
|
|
194 | my ($hs, $ws) = (0, 0); |
|
|
195 | for (@{$self->{children} || []}) { |
|
|
196 | my ($w, $h) = $_->size_request; |
|
|
197 | $hs += $h; |
|
|
198 | if ($ws < $w) { $ws = $w } |
|
|
199 | } |
|
|
200 | |
|
|
201 | return ($ws, $hs); |
|
|
202 | } |
201 | } |
203 | |
202 | |
204 | sub _draw { |
203 | sub _draw { |
205 | my ($self) = @_; |
204 | my ($self) = @_; |
206 | |
205 | |
… | |
… | |
219 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
218 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
220 | } |
219 | } |
221 | |
220 | |
222 | sub size_allocate { |
221 | sub size_allocate { |
223 | my ($self, $w, $h) = @_; |
222 | my ($self, $w, $h) = @_; |
|
|
223 | |
224 | $self->SUPER::size_allocate ($w, $h); |
224 | $self->SUPER::size_allocate ($w, $h); |
225 | $self->{children}[0]->size_allocate ($w, $h) |
225 | $self->{children}[0]->size_allocate ($w, $h) |
226 | if $self->{children}[0] |
226 | if $self->{children}[0] |
227 | } |
227 | } |
228 | |
228 | |
… | |
… | |
236 | my ($self) = @_; |
236 | my ($self) = @_; |
237 | |
237 | |
238 | ::refresh (); |
238 | ::refresh (); |
239 | } |
239 | } |
240 | |
240 | |
|
|
241 | sub add { |
|
|
242 | my ($self, $widget) = @_; |
|
|
243 | |
|
|
244 | $self->SUPER::add ($widget); |
|
|
245 | |
|
|
246 | $widget->size_allocate ($widget->size_request); |
|
|
247 | } |
|
|
248 | |
241 | ############################################################################# |
249 | ############################################################################# |
242 | |
250 | |
243 | package Crossfire::Client::Widget::Window; |
251 | package Crossfire::Client::Widget::Window; |
244 | |
252 | |
245 | our @ISA = Crossfire::Client::Widget::Bin::; |
253 | our @ISA = Crossfire::Client::Widget::Bin::; |
246 | |
254 | |
247 | use SDL::OpenGL; |
255 | use SDL::OpenGL; |
248 | |
256 | |
249 | sub add { |
257 | sub new { |
250 | my ($self, $chld) = @_; |
258 | my ($class, $x, $y, $z, $w, $h) = @_; |
251 | warn "ADD $chld\n"; |
|
|
252 | $self->SUPER::add ($chld); |
|
|
253 | $chld->set_parent ($self); |
|
|
254 | } |
|
|
255 | |
259 | |
256 | sub remove { |
260 | my $self = $class->SUPER::new; |
257 | my ($self) = @_; |
261 | |
258 | # TODO FIXME: removing a child from a window will crash, see render_chld |
262 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
259 | # $self->update; |
|
|
260 | } |
263 | } |
261 | |
264 | |
262 | sub update { |
265 | sub update { |
263 | my ($self) = @_; |
266 | my ($self) = @_; |
|
|
267 | |
264 | $self->render_chld; |
268 | $self->render_chld; |
|
|
269 | $self->SUPER::update; |
265 | } |
270 | } |
266 | |
271 | |
267 | sub render_chld { |
272 | sub render_chld { |
268 | my ($self) = @_; |
273 | my ($self) = @_; |
269 | my $chld = $self->get; |
|
|
270 | my ($w, $h) = $self->size_request; |
|
|
271 | |
274 | |
272 | require Carp; |
|
|
273 | Carp::cluck "RENDERCHI $w $h"; |
|
|
274 | warn "RENDERCHI $w $h\n"; |
|
|
275 | $self->{texture} = |
275 | $self->{texture} = |
276 | Crossfire::Client::Texture->new_from_opengl ( |
276 | Crossfire::Client::Texture->new_from_opengl ( |
277 | $w, $h, sub { $chld->draw } |
277 | $self->{w}, $self->{h}, sub { $self->child->draw } |
278 | ); |
278 | ); |
279 | $self->{texture}->upload; |
|
|
280 | } |
|
|
281 | |
|
|
282 | sub size_request { |
|
|
283 | my ($self) = @_; |
|
|
284 | ($self->w, $self->h) |
|
|
285 | } |
279 | } |
286 | |
280 | |
287 | sub size_allocate { |
281 | sub size_allocate { |
288 | my ($self, $w, $h) = @_; |
282 | my ($self, $w, $h) = @_; |
289 | |
283 | |
290 | $self->w ($w); |
284 | $self->{w} = $w; |
291 | $self->h ($h); |
285 | $self->{h} = $h; |
|
|
286 | |
292 | $self->get->size_allocate ($w, $h); |
287 | $self->child->size_allocate ($w, $h); |
293 | |
288 | |
294 | $self->update; #TODO: Move this to the size_request event propably? |
289 | $self->render_chld; |
295 | } |
290 | } |
296 | |
291 | |
297 | sub _draw { |
292 | sub _draw { |
298 | my ($self) = @_; |
293 | my ($self) = @_; |
299 | |
294 | |
… | |
… | |
336 | map { $_ + 4 } $chld->size_request; |
331 | map { $_ + 4 } $chld->size_request; |
337 | } |
332 | } |
338 | |
333 | |
339 | sub size_allocate { |
334 | sub size_allocate { |
340 | my ($self, $w, $h) = @_; |
335 | my ($self, $w, $h) = @_; |
341 | |
336 | |
342 | $self->SUPER::size_allocate ($w, $h); |
337 | $self->{w} = $w; |
|
|
338 | $self->{h} = $h; |
343 | |
339 | |
344 | $self->child->size_allocate ($w - 4, $h - 4); |
340 | $self->child->size_allocate ($w - 4, $h - 4); |
345 | $self->child->move (2, 2); |
341 | $self->child->move (2, 2); |
346 | } |
342 | } |
347 | |
343 | |
… | |
… | |
398 | $w -= $tex[2]->{width}; |
394 | $w -= $tex[2]->{width}; |
399 | $w -= $tex[3]->{width}; |
395 | $w -= $tex[3]->{width}; |
400 | |
396 | |
401 | $h = $h < 0 ? 0 : $h; |
397 | $h = $h < 0 ? 0 : $h; |
402 | $w = $w < 0 ? 0 : $w; |
398 | $w = $w < 0 ? 0 : $w; |
403 | warn "CHILD:$w $h\n"; |
399 | |
404 | $self->child->size_allocate ($w, $h); |
400 | $self->child->size_allocate ($w, $h); |
405 | $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
401 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
406 | } |
402 | } |
407 | |
403 | |
408 | sub _draw { |
404 | sub _draw { |
409 | my ($self) = @_; |
405 | my ($self) = @_; |
410 | |
406 | |
411 | my ($w, $h) = ($self->w, $self->h); |
407 | my ($w, $h) = ($self->{w}, $self->{h}); |
412 | my ($cw, $ch) = ($self->child->w, $self->child->h); |
408 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
413 | |
409 | |
414 | glEnable GL_BLEND; |
410 | glEnable GL_BLEND; |
415 | glEnable GL_TEXTURE_2D; |
411 | glEnable GL_TEXTURE_2D; |
416 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
412 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
417 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
413 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
… | |
… | |
571 | package Crossfire::Client::Widget::VBox; |
567 | package Crossfire::Client::Widget::VBox; |
572 | |
568 | |
573 | our @ISA = Crossfire::Client::Widget::Container::; |
569 | our @ISA = Crossfire::Client::Widget::Container::; |
574 | |
570 | |
575 | use SDL::OpenGL; |
571 | use SDL::OpenGL; |
|
|
572 | |
|
|
573 | sub size_request { |
|
|
574 | my ($self) = @_; |
|
|
575 | |
|
|
576 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
577 | |
|
|
578 | ( |
|
|
579 | (List::Util::max map $_->[0], @alloc), |
|
|
580 | (List::Util::sum map $_->[1], @alloc), |
|
|
581 | ) |
|
|
582 | } |
576 | |
583 | |
577 | sub size_allocate { |
584 | sub size_allocate { |
578 | my ($self, $w, $h) = @_; |
585 | my ($self, $w, $h) = @_; |
579 | |
586 | |
580 | $self->w ($w); |
587 | $self->w ($w); |
… | |
… | |
610 | } else { |
617 | } else { |
611 | my ($cw, $h) = $_->size_request; |
618 | my ($cw, $h) = $_->size_request; |
612 | $_->size_allocate ($w, $h); |
619 | $_->size_allocate ($w, $h); |
613 | $y += $h; |
620 | $y += $h; |
614 | } |
621 | } |
615 | } |
|
|
616 | } |
|
|
617 | |
|
|
618 | sub _draw { |
|
|
619 | my ($self) = @_; |
|
|
620 | |
|
|
621 | my ($x, $y); |
|
|
622 | for (@{$self->{children} || []}) { |
|
|
623 | $_->draw; |
|
|
624 | $y += $_->h; |
|
|
625 | } |
622 | } |
626 | } |
623 | } |
627 | |
624 | |
628 | ############################################################################# |
625 | ############################################################################# |
629 | |
626 | |