… | |
… | |
6 | |
6 | |
7 | use SDL::OpenGL; |
7 | use SDL::OpenGL; |
8 | use SDL::OpenGL::Constants; |
8 | use SDL::OpenGL::Constants; |
9 | |
9 | |
10 | our $FOCUS; # the widget with current focus |
10 | our $FOCUS; # the widget with current focus |
11 | #our @ACTIVE_WIDGETS; |
|
|
12 | |
11 | |
13 | # class methods for events |
12 | # class methods for events |
14 | 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 } |
15 | 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 } |
16 | sub feed_sdl_button_down_event { } |
15 | sub feed_sdl_button_down_event { } |
… | |
… | |
20 | my $class = shift; |
19 | my $class = shift; |
21 | |
20 | |
22 | bless { @_ }, $class |
21 | bless { @_ }, $class |
23 | } |
22 | } |
24 | |
23 | |
25 | #sub activate { |
|
|
26 | # push @ACTIVE_WIDGETS, $_[0]; |
|
|
27 | # Scalar::Util::weaken $ACTIVE_WIDGETS[-1]; |
|
|
28 | #} |
|
|
29 | |
|
|
30 | #sub deactivate { |
|
|
31 | # @ACTIVE_WIDGETS = |
|
|
32 | # sort { $a->{z} <=> $b->{z} } |
|
|
33 | # grep { $_ && $_ != $_[0] } |
|
|
34 | # @ACTIVE_WIDGETS; |
|
|
35 | #} |
|
|
36 | |
|
|
37 | sub move { |
24 | sub move { |
38 | my ($self, $x, $y, $z) = @_; |
25 | my ($self, $x, $y, $z) = @_; |
39 | $self->{x} = $x; |
26 | $self->{x} = $x; |
40 | $self->{y} = $y; |
27 | $self->{y} = $y; |
41 | $self->{z} = $z if defined $z; |
28 | $self->{z} = $z if defined $z; |
… | |
… | |
44 | sub needs_redraw { |
31 | sub needs_redraw { |
45 | 0 |
32 | 0 |
46 | } |
33 | } |
47 | |
34 | |
48 | sub size_request { |
35 | sub size_request { |
|
|
36 | require Carp; |
49 | die "size_request is abtract"; |
37 | Carp::confess "size_request is abtract"; |
|
|
38 | } |
|
|
39 | |
|
|
40 | sub size_allocate { |
|
|
41 | my ($self, $w, $h) = @_; |
|
|
42 | $self->w ($w); |
|
|
43 | $self->h ($h); |
50 | } |
44 | } |
51 | |
45 | |
52 | sub focus_in { |
46 | sub focus_in { |
53 | my ($widget) = @_; |
47 | my ($widget) = @_; |
54 | $FOCUS = $widget; |
48 | $FOCUS = $widget; |
… | |
… | |
72 | |
66 | |
73 | sub button_up { |
67 | sub button_up { |
74 | my ($widget, $sdlev) = @_; |
68 | my ($widget, $sdlev) = @_; |
75 | } |
69 | } |
76 | |
70 | |
77 | sub w { $_[0]->{w} } |
71 | sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } |
78 | sub h { $_[0]->{h} } |
72 | sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } |
79 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
73 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
80 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
74 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
81 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
75 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
82 | |
76 | |
83 | sub draw { |
77 | sub draw { |
… | |
… | |
128 | my ($self) = @_; |
122 | my ($self) = @_; |
129 | |
123 | |
130 | #$self->deactivate; |
124 | #$self->deactivate; |
131 | } |
125 | } |
132 | |
126 | |
133 | package Crossfire::Client::Widget::Container; |
127 | package Crossfire::Client::Widget::Bin; |
134 | |
128 | |
135 | our @ISA = Crossfire::Client::Widget::; |
129 | our @ISA = Crossfire::Client::Widget::; |
136 | |
130 | |
137 | use SDL::OpenGL; |
|
|
138 | |
|
|
139 | sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->update } |
131 | sub add { $_[0]->{child} = $_[1]; $_[1]->set_parent ($_[0]); $_[1]->{expand} = $_[2] } |
140 | sub get { $_[0]->{child} } |
132 | sub get { $_[0]->{child} } |
|
|
133 | |
141 | sub remove { |
134 | sub remove { |
142 | my ($self, $chld) = @_; |
135 | my ($self, $chld) = @_; |
143 | delete $self->{child} |
136 | delete $self->{child} |
144 | if $self->{child} == $chld; |
137 | if $self->{child} == $chld; |
145 | } |
138 | } |
146 | |
139 | |
|
|
140 | sub size_request { |
147 | sub size_request { $_[0]->{child}->size_request if $_[0]->{child} } |
141 | $_[0]->{child}->size_request if $_[0]->{child} |
|
|
142 | } |
|
|
143 | sub size_allocate { |
|
|
144 | my ($self, $w, $h) = @_; |
|
|
145 | $self->SUPER::size_allocate ($w, $h); |
|
|
146 | $self->{child}->size_allocate ($w, $h) |
|
|
147 | if $self->{child} |
|
|
148 | } |
148 | |
149 | |
149 | sub _draw { die "Containers can't be drawn!" } |
150 | sub _draw { |
|
|
151 | my ($self) = @_; |
|
|
152 | |
|
|
153 | $self->{child}->draw; |
|
|
154 | } |
150 | |
155 | |
151 | package Crossfire::Client::Widget::Toplevel; |
156 | package Crossfire::Client::Widget::Toplevel; |
152 | |
157 | |
153 | our @ISA = Crossfire::Client::Widget::; |
158 | our @ISA = Crossfire::Client::Widget::; |
154 | |
159 | |
… | |
… | |
161 | @{$self->{childs}} = |
166 | @{$self->{childs}} = |
162 | sort { $a->{z} <=> $b->{z} } |
167 | sort { $a->{z} <=> $b->{z} } |
163 | @{$self->{childs}}; |
168 | @{$self->{childs}}; |
164 | |
169 | |
165 | $chld->set_parent ($self); |
170 | $chld->set_parent ($self); |
|
|
171 | $chld->size_allocate ($chld->size_request); |
166 | } |
172 | } |
167 | |
173 | |
168 | sub remove { |
174 | sub remove { |
169 | my ($self, $chld) = @_; |
175 | my ($self, $chld) = @_; |
170 | @{$self->{childs}} = |
176 | @{$self->{childs}} = |
… | |
… | |
184 | $_->draw for @{$self->{childs}}; |
190 | $_->draw for @{$self->{childs}}; |
185 | } |
191 | } |
186 | |
192 | |
187 | package Crossfire::Client::Widget::Window; |
193 | package Crossfire::Client::Widget::Window; |
188 | |
194 | |
189 | our @ISA = Crossfire::Client::Widget::Container::; |
195 | our @ISA = Crossfire::Client::Widget::Bin::; |
190 | |
196 | |
191 | use SDL::OpenGL; |
197 | use SDL::OpenGL; |
192 | |
198 | |
193 | sub add { |
199 | sub add { |
194 | my ($self, $chld) = @_; |
200 | my ($self, $chld) = @_; |
|
|
201 | warn "ADD $chld\n"; |
195 | $self->SUPER::add ($chld); |
202 | $self->SUPER::add ($chld); |
196 | $chld->set_parent ($self); |
203 | $chld->set_parent ($self); |
197 | $self->update; #TODO: Move this to the size_request event propably? |
|
|
198 | } |
204 | } |
199 | |
205 | |
200 | sub remove { |
206 | sub remove { |
201 | my ($self) = @_; |
207 | my ($self) = @_; |
202 | # TODO FIXME: removing a child from a window will crash, see render_chld |
208 | # TODO FIXME: removing a child from a window will crash, see render_chld |
203 | $self->update; |
209 | # $self->update; |
204 | } |
210 | } |
205 | |
211 | |
206 | sub update { |
212 | sub update { |
207 | my ($self) = @_; |
213 | my ($self) = @_; |
208 | $self->render_chld; |
214 | $self->render_chld; |
… | |
… | |
211 | sub render_chld { |
217 | sub render_chld { |
212 | my ($self) = @_; |
218 | my ($self) = @_; |
213 | my $chld = $self->get; |
219 | my $chld = $self->get; |
214 | my ($w, $h) = $self->size_request; |
220 | my ($w, $h) = $self->size_request; |
215 | |
221 | |
|
|
222 | require Carp; |
|
|
223 | Carp::cluck "RENDERCHI $w $h"; |
|
|
224 | warn "RENDERCHI $w $h\n"; |
216 | $self->{texture} = |
225 | $self->{texture} = |
217 | Crossfire::Client::Texture->new_from_opengl ( |
226 | Crossfire::Client::Texture->new_from_opengl ( |
218 | $w, $h, sub { $chld->draw } |
227 | $w, $h, sub { $chld->draw } |
219 | ); |
228 | ); |
220 | $self->{texture}->upload; |
229 | $self->{texture}->upload; |
221 | } |
230 | } |
222 | |
231 | |
223 | sub size_request { |
232 | sub size_request { |
224 | my ($self) = @_; |
233 | my ($self) = @_; |
225 | my $chld = $self->get |
234 | ($self->w, $self->h) |
226 | or return (0, 0); |
|
|
227 | $chld->size_request |
|
|
228 | } |
235 | } |
229 | |
236 | |
230 | sub _draw { |
237 | sub size_allocate { |
231 | my ($self) = @_; |
238 | my ($self, $w, $h) = @_; |
232 | |
239 | |
233 | my ($w, $h) = $self->size_request;#TODO# use width/height of texture |
240 | $self->w ($w); |
|
|
241 | $self->h ($h); |
|
|
242 | $self->get->size_allocate ($w, $h); |
|
|
243 | |
|
|
244 | $self->update; #TODO: Move this to the size_request event propably? |
|
|
245 | } |
|
|
246 | |
|
|
247 | sub _draw { |
|
|
248 | my ($self) = @_; |
|
|
249 | |
|
|
250 | my ($w, $h) = ($self->w, $self->h); |
234 | |
251 | |
235 | my $tex = $self->{texture} |
252 | my $tex = $self->{texture} |
236 | or return; |
253 | or return; |
237 | |
254 | |
238 | glEnable GL_BLEND; |
255 | glEnable GL_BLEND; |
239 | glEnable GL_TEXTURE_2D; |
256 | glEnable GL_TEXTURE_2D; |
240 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
257 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
241 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
258 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
242 | |
|
|
243 | glColor 1, 0, 1; |
|
|
244 | |
259 | |
245 | glBegin GL_QUADS; |
260 | glBegin GL_QUADS; |
246 | glTexCoord 0, 0; glVertex 0, 0; |
261 | glTexCoord 0, 0; glVertex 0, 0; |
247 | glTexCoord 0, 1; glVertex 0, $h; |
262 | glTexCoord 0, 1; glVertex 0, $h; |
248 | glTexCoord 1, 1; glVertex $w, $h; |
263 | glTexCoord 1, 1; glVertex $w, $h; |
… | |
… | |
253 | glDisable GL_TEXTURE_2D; |
268 | glDisable GL_TEXTURE_2D; |
254 | } |
269 | } |
255 | |
270 | |
256 | package Crossfire::Client::Widget::Frame; |
271 | package Crossfire::Client::Widget::Frame; |
257 | |
272 | |
258 | our @ISA = Crossfire::Client::Widget::Container::; |
273 | our @ISA = Crossfire::Client::Widget::Bin::; |
259 | |
274 | |
260 | use SDL::OpenGL; |
275 | use SDL::OpenGL; |
261 | |
276 | |
262 | sub size_request { |
277 | sub size_request { |
263 | my ($self) = @_; |
278 | my ($self) = @_; |
… | |
… | |
265 | or return (0, 0); |
280 | or return (0, 0); |
266 | |
281 | |
267 | $chld->move (2, 2); |
282 | $chld->move (2, 2); |
268 | |
283 | |
269 | map { $_ + 4 } $chld->size_request; |
284 | map { $_ + 4 } $chld->size_request; |
|
|
285 | } |
|
|
286 | |
|
|
287 | sub size_allocate { |
|
|
288 | my ($self, $w, $h) = @_; |
|
|
289 | |
|
|
290 | $self->w ($w); |
|
|
291 | $self->h ($h); |
|
|
292 | |
|
|
293 | $self->get->size_allocate ($w - 4, $h - 4); |
|
|
294 | $self->get->move (2, 2); |
270 | } |
295 | } |
271 | |
296 | |
272 | sub _draw { |
297 | sub _draw { |
273 | my ($self) = @_; |
298 | my ($self) = @_; |
274 | |
299 | |
… | |
… | |
291 | |
316 | |
292 | our @ISA = Crossfire::Client::Widget::Frame::; |
317 | our @ISA = Crossfire::Client::Widget::Frame::; |
293 | |
318 | |
294 | use SDL::OpenGL; |
319 | use SDL::OpenGL; |
295 | |
320 | |
296 | #TODO: implement themed frame |
321 | sub new { |
|
|
322 | my ($self, $theme) = @_; |
|
|
323 | $self = $self->SUPER::new; |
|
|
324 | |
|
|
325 | $self->{txts} = [ |
|
|
326 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
|
|
327 | qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ |
|
|
328 | ]; |
|
|
329 | $self |
|
|
330 | } |
|
|
331 | |
|
|
332 | sub size_request { |
|
|
333 | my ($self) = @_; |
|
|
334 | my ($w, $h) = $self->get->size_request; |
|
|
335 | |
|
|
336 | $h += $self->{txts}->[1]->{height}; |
|
|
337 | $h += $self->{txts}->[4]->{height}; |
|
|
338 | $w += $self->{txts}->[2]->{width}; |
|
|
339 | $w += $self->{txts}->[3]->{width}; |
|
|
340 | |
|
|
341 | ($w, $h) |
|
|
342 | } |
|
|
343 | |
|
|
344 | sub size_allocate { |
|
|
345 | my ($self, $w, $h) = @_; |
|
|
346 | |
|
|
347 | $self->w ($w); |
|
|
348 | $self->h ($h); |
|
|
349 | $h -= $self->{txts}->[1]->{height}; |
|
|
350 | $h -= $self->{txts}->[4]->{height}; |
|
|
351 | $w -= $self->{txts}->[2]->{width}; |
|
|
352 | $w -= $self->{txts}->[3]->{width}; |
|
|
353 | |
|
|
354 | $h = $h < 0 ? 0 : $h; |
|
|
355 | $w = $w < 0 ? 0 : $w; |
|
|
356 | warn "CHILD:$w $h\n"; |
|
|
357 | $self->get->size_allocate ($w, $h); |
|
|
358 | $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
|
|
359 | } |
|
|
360 | |
|
|
361 | sub _draw { |
|
|
362 | my ($self) = @_; |
|
|
363 | |
|
|
364 | my ($w, $h) = ($self->w, $self->h); |
|
|
365 | my ($cw, $ch) = ($self->get->w, $self->get->h); |
|
|
366 | |
|
|
367 | glEnable GL_BLEND; |
|
|
368 | glEnable GL_TEXTURE_2D; |
|
|
369 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
370 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
371 | |
|
|
372 | my $top = $self->{txts}->[1]; |
|
|
373 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
|
|
374 | |
|
|
375 | glBegin GL_QUADS; |
|
|
376 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
377 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
|
|
378 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
|
|
379 | glTexCoord 1, 0; glVertex $w , 0; |
|
|
380 | glEnd; |
|
|
381 | |
|
|
382 | my $left = $self->{txts}->[3]; |
|
|
383 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
|
|
384 | |
|
|
385 | glBegin GL_QUADS; |
|
|
386 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
|
|
387 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
|
|
388 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
|
|
389 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
|
|
390 | glEnd; |
|
|
391 | |
|
|
392 | my $right = $self->{txts}->[2]; |
|
|
393 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
|
|
394 | |
|
|
395 | glBegin GL_QUADS; |
|
|
396 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
|
|
397 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
|
|
398 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
|
|
399 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
|
|
400 | glEnd; |
|
|
401 | |
|
|
402 | my $bottom = $self->{txts}->[4]; |
|
|
403 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
|
|
404 | |
|
|
405 | glBegin GL_QUADS; |
|
|
406 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
|
|
407 | glTexCoord 0, 1; glVertex 0 , $h; |
|
|
408 | glTexCoord 1, 1; glVertex $w , $h; |
|
|
409 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
|
|
410 | glEnd; |
|
|
411 | |
|
|
412 | my $bg = $self->{txts}->[0]; |
|
|
413 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
|
|
414 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
415 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
|
|
416 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
|
|
417 | |
|
|
418 | my $rep_x = $cw / $bg->{width}; |
|
|
419 | my $rep_y = $ch / $bg->{height}; |
|
|
420 | |
|
|
421 | glBegin GL_QUADS; |
|
|
422 | glTexCoord 0, 0; glVertex $left->{width}, $top->{height}; |
|
|
423 | glTexCoord 0, $rep_y; glVertex $left->{width}, $top->{height} + $ch; |
|
|
424 | glTexCoord $rep_x, $rep_y; glVertex $left->{width} + $cw , $top->{height} + $ch; |
|
|
425 | glTexCoord $rep_x, 0; glVertex $left->{width} + $cw , $top->{height}; |
|
|
426 | glEnd; |
|
|
427 | |
|
|
428 | glDisable GL_BLEND; |
|
|
429 | glDisable GL_TEXTURE_2D; |
|
|
430 | |
|
|
431 | $self->get->draw; |
|
|
432 | |
|
|
433 | } |
297 | |
434 | |
298 | package Crossfire::Client::Widget::Table; |
435 | package Crossfire::Client::Widget::Table; |
299 | |
436 | |
300 | our @ISA = Crossfire::Client::Widget::Container::; |
437 | our @ISA = Crossfire::Client::Widget::Bin::; |
301 | |
438 | |
302 | use SDL::OpenGL; |
439 | use SDL::OpenGL; |
303 | |
440 | |
304 | sub add { |
441 | sub add { |
305 | my ($self, $x, $y, $chld) = @_; |
442 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
380 | } |
517 | } |
381 | } |
518 | } |
382 | |
519 | |
383 | package Crossfire::Client::Widget::VBox; |
520 | package Crossfire::Client::Widget::VBox; |
384 | |
521 | |
385 | our @ISA = Crossfire::Client::Widget::Container::; |
522 | our @ISA = Crossfire::Client::Widget::Bin::; |
386 | |
523 | |
387 | use SDL::OpenGL; |
524 | use SDL::OpenGL; |
388 | |
525 | |
389 | sub add { |
526 | sub add { |
390 | my ($self, $chld) = @_; |
527 | my ($self, $chld, $expand) = @_; |
391 | push @{$self->{childs}}, $chld; |
528 | push @{$self->{childs}}, $chld; |
|
|
529 | $chld->{expand} = $expand; |
392 | $chld->set_parent ($self); |
530 | $chld->set_parent ($self); |
393 | $self->update; |
531 | $self->update; |
394 | } |
532 | } |
395 | |
533 | |
396 | sub size_request { |
534 | sub size_request { |
… | |
… | |
404 | } |
542 | } |
405 | |
543 | |
406 | return ($ws, $hs); |
544 | return ($ws, $hs); |
407 | } |
545 | } |
408 | |
546 | |
|
|
547 | sub size_allocate { |
|
|
548 | my ($self, $w, $h) = @_; |
|
|
549 | |
|
|
550 | $self->w ($w); |
|
|
551 | $self->h ($h); |
|
|
552 | |
|
|
553 | my $exp; |
|
|
554 | my @oth; |
|
|
555 | # find expand widget |
|
|
556 | for (@{$self->{childs}}) { |
|
|
557 | if ($_->{expand}) { |
|
|
558 | $exp = $_; |
|
|
559 | last; |
|
|
560 | } |
|
|
561 | push @oth, $_; |
|
|
562 | } |
|
|
563 | |
|
|
564 | my ($ow, $oh); |
|
|
565 | |
|
|
566 | # get sizes of other widgets |
|
|
567 | for (@oth) { |
|
|
568 | my ($w, $h) = $_->size_request; |
|
|
569 | $oh += $h; |
|
|
570 | if ($ow < $w) { $ow = $w } |
|
|
571 | } |
|
|
572 | |
|
|
573 | my $y = 0; |
|
|
574 | for (@{$self->{childs}}) { |
|
|
575 | $_->move (0, $y); |
|
|
576 | |
|
|
577 | if ($_ == $exp) { |
|
|
578 | $_->size_allocate ($w, $h - $oh); |
|
|
579 | $y += $h - $oh; |
|
|
580 | } else { |
|
|
581 | my ($cw, $h) = $_->size_request; |
|
|
582 | $_->size_allocate ($w, $h); |
|
|
583 | $y += $h; |
|
|
584 | } |
|
|
585 | } |
|
|
586 | } |
|
|
587 | |
409 | sub _draw { |
588 | sub _draw { |
410 | my ($self) = @_; |
589 | my ($self) = @_; |
411 | |
590 | |
412 | my ($x, $y); |
591 | my ($x, $y); |
413 | for (@{$self->{childs} || []}) { |
592 | for (@{$self->{childs} || []}) { |
414 | $_->move (0, $y, 0); #TODO: move to size_request |
|
|
415 | $_->draw; |
593 | $_->draw; |
416 | my ($w, $h) = $_->size_request; |
|
|
417 | $y += $h; |
594 | $y += $_->h; |
418 | } |
595 | } |
419 | } |
596 | } |
420 | |
597 | |
421 | package Crossfire::Client::Widget::Label; |
598 | package Crossfire::Client::Widget::Label; |
422 | |
599 | |
… | |
… | |
425 | use SDL::OpenGL; |
602 | use SDL::OpenGL; |
426 | |
603 | |
427 | sub new { |
604 | sub new { |
428 | my ($class, $x, $y, $z, $height, $text) = @_; |
605 | my ($class, $x, $y, $z, $height, $text) = @_; |
429 | |
606 | |
|
|
607 | # TODO: color, and make height, xyz etc. optional |
430 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
608 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
431 | |
609 | |
432 | $self->set_text ($text); |
610 | $self->set_text ($text); |
433 | |
611 | |
434 | $self |
612 | $self |
… | |
… | |
467 | glEnable GL_TEXTURE_2D; |
645 | glEnable GL_TEXTURE_2D; |
468 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
646 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
469 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
647 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
470 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
648 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
471 | |
649 | |
472 | glColor 1, 1, 1, 0.8; |
650 | glColor 1, 0, 0, 1; # TODO color |
473 | |
651 | |
474 | glBegin GL_QUADS; |
652 | glBegin GL_QUADS; |
475 | glTexCoord 0, 0; glVertex 0 , 0; |
653 | glTexCoord 0, 0; glVertex 0 , 0; |
476 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
654 | glTexCoord 0, 1; glVertex 0 , $tex->{height}; |
477 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
655 | glTexCoord 1, 1; glVertex $tex->{width}, $tex->{height}; |
… | |
… | |
507 | $text .= chr $uni; |
685 | $text .= chr $uni; |
508 | } |
686 | } |
509 | $self->set_text ($text); |
687 | $self->set_text ($text); |
510 | } |
688 | } |
511 | |
689 | |
512 | |
|
|
513 | # XXX: TextView isn't neccessary with pango multiline text rendering |
|
|
514 | package Crossfire::Client::Widget::TextView; |
|
|
515 | |
|
|
516 | use strict; |
|
|
517 | |
|
|
518 | our @ISA = qw/Crossfire::Client::Widget/; |
|
|
519 | |
|
|
520 | use SDL::OpenGL; |
|
|
521 | use SDL::OpenGL::Constants; |
|
|
522 | |
|
|
523 | sub new { |
|
|
524 | my ($class, $text, $h) = @_; |
|
|
525 | my $self = $class->SUPER::new (); |
|
|
526 | |
|
|
527 | $self->{txt_height} = $h; |
|
|
528 | @{$self->{lines}} = split /\r?\n/, $text; |
|
|
529 | |
|
|
530 | for (split /\r?\n/, $text) { |
|
|
531 | $self->add_line ($_); |
|
|
532 | } |
|
|
533 | $self |
|
|
534 | } |
|
|
535 | |
|
|
536 | #sub render_lines { |
|
|
537 | # my ($self) = @_; |
|
|
538 | # |
|
|
539 | # $self->{txt_lines} = []; |
|
|
540 | # |
|
|
541 | # for (@{$self->{lines}}) { |
|
|
542 | # push @{$self->{txt_lines}}, |
|
|
543 | # new_from_ttf Crossfire::Client::Texture $self->{ttf}, $_; |
|
|
544 | # } |
|
|
545 | #} |
|
|
546 | |
|
|
547 | sub add_line { |
|
|
548 | my ($self, $line) = @_; |
|
|
549 | push @{$self->{lines}}, $line; |
|
|
550 | |
|
|
551 | push @{$self->{txt_lines}}, |
|
|
552 | new_from_text Crossfire::Client::Texture $line, $self->{txt_height}; |
|
|
553 | } |
|
|
554 | |
|
|
555 | sub size_request { |
|
|
556 | my ($self) = @_; |
|
|
557 | |
|
|
558 | my $w = 0; |
|
|
559 | my $h = 0; |
|
|
560 | |
|
|
561 | for (@{$self->{txt_lines}}) { |
|
|
562 | if ($w < $_->{width}) { $w = $_->{width} } |
|
|
563 | $h += $_->{height}; |
|
|
564 | } |
|
|
565 | |
|
|
566 | return ($w, $h); |
|
|
567 | } |
|
|
568 | |
|
|
569 | sub draw_line { |
|
|
570 | my ($self, $tex, $y) = @_; |
|
|
571 | |
|
|
572 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
573 | |
|
|
574 | glColor 1, 0, 1; |
|
|
575 | |
|
|
576 | glBegin GL_QUADS; |
|
|
577 | glTexCoord 0, 0; glVertex 0 , $y; |
|
|
578 | glTexCoord 0, 1; glVertex 0 , $y + $tex->{height}; |
|
|
579 | glTexCoord 1, 1; glVertex $tex->{width}, $y + $tex->{height}; |
|
|
580 | glTexCoord 1, 0; glVertex $tex->{width}, $y; |
|
|
581 | glEnd; |
|
|
582 | } |
|
|
583 | |
|
|
584 | sub _draw { |
|
|
585 | my ($self) = @_; |
|
|
586 | |
|
|
587 | glEnable GL_BLEND; |
|
|
588 | glEnable GL_TEXTURE_2D; |
|
|
589 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;#DECAL; |
|
|
590 | |
|
|
591 | my $l = 0; |
|
|
592 | for (@{$self->{txt_lines}}) { |
|
|
593 | $self->draw_line ($_, $l); |
|
|
594 | $l += $_->{height}; |
|
|
595 | } |
|
|
596 | |
|
|
597 | glDisable GL_BLEND; |
|
|
598 | glDisable GL_TEXTURE_2D; |
|
|
599 | } |
|
|
600 | |
|
|
601 | package Crossfire::Client::Widget::MapWidget; |
690 | package Crossfire::Client::Widget::MapWidget; |
602 | |
691 | |
603 | use strict; |
692 | use strict; |
604 | |
693 | |
605 | use List::Util qw(min max); |
694 | use List::Util qw(min max); |
… | |
… | |
613 | sub key_down { |
702 | sub key_down { |
614 | print "MAPKEYDOWN\n"; |
703 | print "MAPKEYDOWN\n"; |
615 | } |
704 | } |
616 | |
705 | |
617 | sub key_up { |
706 | sub key_up { |
|
|
707 | } |
|
|
708 | |
|
|
709 | sub size_request { |
|
|
710 | |
|
|
711 | } |
|
|
712 | |
|
|
713 | sub size_allocate { |
618 | } |
714 | } |
619 | |
715 | |
620 | sub _draw { |
716 | sub _draw { |
621 | my ($self) = @_; |
717 | my ($self) = @_; |
622 | |
718 | |
… | |
… | |
642 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
738 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
643 | } |
739 | } |
644 | |
740 | |
645 | glEnable GL_TEXTURE_2D; |
741 | glEnable GL_TEXTURE_2D; |
646 | glEnable GL_BLEND; |
742 | glEnable GL_BLEND; |
647 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
743 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
744 | |
|
|
745 | my $sw4 = ($sw + 3) & ~3; |
|
|
746 | my $lighting = "\x00" x ($sw4 * $sh); |
648 | |
747 | |
649 | for my $x (0 .. $sw - 1) { |
748 | for my $x (0 .. $sw - 1) { |
650 | for my $y (0 .. $sh - 1) { |
749 | for my $y (0 .. $sh - 1) { |
651 | |
750 | |
652 | my $cell = $map->[$x + $xofs][$y + $yofs] |
751 | my $cell = $map->[$x + $xofs][$y + $yofs] |
653 | or next; |
752 | or next; |
654 | |
753 | |
655 | my $darkness = $cell->[0] * (1 / 255); |
754 | my $darkness = $cell->[0] * (1 / 255); |
656 | if ($darkness < 0) { |
755 | if ($darkness < 0) { |
657 | glColor 0.3, 0.3, 0.3; |
756 | $darkness = 0.15; |
658 | } else { |
|
|
659 | glColor $darkness, $darkness, $darkness; |
|
|
660 | } |
757 | } |
|
|
758 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
661 | |
759 | |
662 | for my $num (grep $_, @$cell[1,2,3]) { |
760 | for my $num (grep $_, @$cell[1,2,3]) { |
663 | my $tex = $::CONN->{face}[$num]{texture} || next; |
761 | my $tex = $::CONN->{face}[$num]{texture} || next; |
664 | |
762 | |
665 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
763 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
… | |
… | |
677 | glTexCoord 1, 0; glVertex $px + $w, $py; |
775 | glTexCoord 1, 0; glVertex $px + $w, $py; |
678 | glEnd; |
776 | glEnd; |
679 | } |
777 | } |
680 | } |
778 | } |
681 | } |
779 | } |
|
|
780 | |
|
|
781 | # if (1) { # higher quality darkness |
|
|
782 | # $lighting =~ s/(.)/$1$1$1/gs; |
|
|
783 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
|
|
784 | # |
|
|
785 | # $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear"); |
|
|
786 | # |
|
|
787 | # $lighting = $pb->get_pixels; |
|
|
788 | # $lighting =~ s/(.)../$1/gs; |
|
|
789 | # } |
|
|
790 | |
|
|
791 | $lighting = new Crossfire::Client::Texture |
|
|
792 | width => $sw4, |
|
|
793 | height => $sh, |
|
|
794 | data => $lighting, |
|
|
795 | internalformat => GL_ALPHA4, |
|
|
796 | format => GL_ALPHA; |
|
|
797 | |
|
|
798 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
799 | glColor 0, 0, 0, 0.75; |
|
|
800 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
801 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
|
|
802 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
803 | glBegin GL_QUADS; |
|
|
804 | glTexCoord 0, 0; glVertex 0 , 0; |
|
|
805 | glTexCoord 0, 1; glVertex 0 , $sh * 32; |
|
|
806 | glTexCoord 1, 1; glVertex $sw4 * 32, $sh * 32; |
|
|
807 | glTexCoord 1, 0; glVertex $sw4 * 32, 0; |
|
|
808 | glEnd; |
682 | |
809 | |
683 | glDisable GL_TEXTURE_2D; |
810 | glDisable GL_TEXTURE_2D; |
684 | glDisable GL_BLEND; |
811 | glDisable GL_BLEND; |
685 | } |
812 | } |
686 | |
813 | |
… | |
… | |
733 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
860 | if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { |
734 | $::CONN->send ("command run_stop"); |
861 | $::CONN->send ("command run_stop"); |
735 | } |
862 | } |
736 | } |
863 | } |
737 | |
864 | |
|
|
865 | package Crossfire::Client::Widget::Animator; |
|
|
866 | |
|
|
867 | use SDL::OpenGL; |
|
|
868 | |
|
|
869 | our @ISA = Crossfire::Client::Widget::Bin::; |
|
|
870 | |
|
|
871 | sub moveto { |
|
|
872 | my ($self, $x, $y) = @_; |
|
|
873 | |
|
|
874 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
|
|
875 | $self->{speed} = 0.01; |
|
|
876 | $self->{time} = 1; |
|
|
877 | |
|
|
878 | ::animation_start $self; |
|
|
879 | } |
|
|
880 | |
|
|
881 | sub animate { |
|
|
882 | my ($self, $interval) = @_; |
|
|
883 | |
|
|
884 | $self->{time} -= $interval * $self->{speed}; |
|
|
885 | if ($self->{time} <= 0) { |
|
|
886 | $self->{time} = 0; |
|
|
887 | ::animation_stop $self; |
|
|
888 | } |
|
|
889 | |
|
|
890 | my ($x0, $y0, $x1, $y1) = @{$self->{moveto}}; |
|
|
891 | |
|
|
892 | $self->{x} = $x0 * $self->{time} + $x1 * (1 - $self->{time}); |
|
|
893 | $self->{y} = $y0 * $self->{time} + $y1 * (1 - $self->{time}); |
|
|
894 | } |
|
|
895 | |
|
|
896 | sub _draw { |
|
|
897 | my ($self) = @_; |
|
|
898 | |
|
|
899 | glPushMatrix; |
|
|
900 | glRotate $self->{time} * 10000, 0, 1, 0; |
|
|
901 | $self->{child}->draw; |
|
|
902 | glPopMatrix; |
|
|
903 | } |
|
|
904 | |
738 | 1; |
905 | 1; |
739 | |
906 | |