… | |
… | |
5 | use Scalar::Util; |
5 | use Scalar::Util; |
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 | use Crossfire::Client; |
|
|
11 | |
|
|
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
|
|
13 | |
|
|
14 | our $TOPLEVEL; |
|
|
15 | our $BUTTON_STATE; |
11 | |
16 | |
12 | # class methods for events |
17 | # class methods for events |
13 | sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } |
18 | sub feed_sdl_key_down_event { |
14 | sub feed_sdl_key_up_event { $FOCUS->key_up ($_[0]) if $FOCUS } |
19 | $FOCUS->key_down ($_[0]) if $FOCUS; |
|
|
20 | } |
|
|
21 | |
|
|
22 | sub feed_sdl_key_up_event { |
|
|
23 | $FOCUS->key_up ($_[0]) if $FOCUS; |
|
|
24 | } |
|
|
25 | |
15 | sub feed_sdl_button_down_event { } |
26 | sub feed_sdl_button_down_event { |
|
|
27 | my ($ev) = @_; |
|
|
28 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
29 | |
|
|
30 | if (!$BUTTON_STATE) { |
|
|
31 | my $widget = $TOPLEVEL->find_widget ($x, $y); |
|
|
32 | |
|
|
33 | $GRAB = $widget; |
|
|
34 | $GRAB->update if $GRAB; |
|
|
35 | } |
|
|
36 | |
|
|
37 | $BUTTON_STATE |= 1 << ($ev->button - 1); |
|
|
38 | |
|
|
39 | $GRAB->button_down ($ev) if $GRAB; |
|
|
40 | } |
|
|
41 | |
16 | sub feed_sdl_button_up_event { } |
42 | sub feed_sdl_button_up_event { |
|
|
43 | my ($ev) = @_; |
|
|
44 | my ($x, $y) = ($ev->motion_x, $ev->motion_y); |
|
|
45 | |
|
|
46 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
47 | |
|
|
48 | $BUTTON_STATE &= ~(1 << ($ev->button - 1)); |
|
|
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->motion_x, $ev->motion_y); |
|
|
60 | |
|
|
61 | my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); |
|
|
62 | |
|
|
63 | if ($widget != $HOVER) { |
|
|
64 | my $hover = $HOVER; $HOVER = $widget; |
|
|
65 | |
|
|
66 | $hover->update if $hover; |
|
|
67 | $HOVER->update if $HOVER; |
|
|
68 | } |
|
|
69 | |
|
|
70 | $HOVER->mouse_motion ($ev) if $HOVER; |
|
|
71 | } |
17 | |
72 | |
18 | sub new { |
73 | sub new { |
19 | my $class = shift; |
74 | my $class = shift; |
20 | |
75 | |
21 | bless { @_ }, $class |
76 | bless { @_ }, $class |
… | |
… | |
43 | $self->{w} = $w; |
98 | $self->{w} = $w; |
44 | $self->{h} = $h; |
99 | $self->{h} = $h; |
45 | } |
100 | } |
46 | |
101 | |
47 | sub focus_in { |
102 | sub focus_in { |
48 | my ($widget) = @_; |
103 | my ($self) = @_; |
49 | $FOCUS = $widget; |
104 | |
|
|
105 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
106 | $focus->update if $focus; |
|
|
107 | $FOCUS->update; |
50 | } |
108 | } |
51 | |
109 | |
52 | sub focus_out { |
110 | sub focus_out { |
53 | my ($widget) = @_; |
111 | my ($self) = @_; |
54 | } |
|
|
55 | |
112 | |
56 | sub key_down { |
113 | return unless $FOCUS == $self; |
57 | my ($widget, $sdlev) = @_; |
|
|
58 | } |
|
|
59 | |
114 | |
60 | sub key_up { |
115 | my $focus = $FOCUS; undef $FOCUS; |
61 | my ($widget, $sdlev) = @_; |
116 | $focus->update if $focus; #? |
62 | } |
117 | } |
63 | |
118 | |
|
|
119 | sub mouse_motion { } |
|
|
120 | sub button_up { } |
64 | sub button_down { |
121 | sub button_down { } |
65 | my ($widget, $sdlev) = @_; |
122 | sub key_down { } |
66 | } |
123 | sub key_up { } |
67 | |
124 | |
68 | sub button_up { |
|
|
69 | my ($widget, $sdlev) = @_; |
|
|
70 | } |
|
|
71 | |
|
|
72 | sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } |
125 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
73 | sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } |
126 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
74 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
127 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
75 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
128 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
76 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
129 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
77 | |
130 | |
78 | sub draw { |
131 | sub draw { |
79 | my ($self) = @_; |
132 | my ($self) = @_; |
80 | |
133 | |
81 | glPushMatrix; |
134 | glPushMatrix; |
82 | glTranslate $self->{x}, $self->{y}, 0; |
135 | glTranslate $self->{x}, $self->{y}, 0; |
83 | $self->_draw; |
136 | $self->_draw; |
|
|
137 | if ($self == $HOVER) { |
|
|
138 | glColor 1, 1, 1, 0.4; |
|
|
139 | glEnable GL_BLEND; |
|
|
140 | glBegin GL_QUADS; |
|
|
141 | glVertex 0 , 0; |
|
|
142 | glVertex $self->{w}, 0; |
|
|
143 | glVertex $self->{w}, $self->{h}; |
|
|
144 | glVertex 0 , $self->{h}; |
|
|
145 | glEnd; |
|
|
146 | glDisable GL_BLEND; |
|
|
147 | } |
84 | glPopMatrix; |
148 | glPopMatrix; |
85 | } |
149 | } |
86 | |
150 | |
87 | sub _draw { |
151 | sub _draw { |
88 | my ($self) = @_; |
152 | my ($self) = @_; |
… | |
… | |
160 | |
224 | |
161 | @{$self->{children}} = |
225 | @{$self->{children}} = |
162 | sort { $a->{z} <=> $b->{z} } |
226 | sort { $a->{z} <=> $b->{z} } |
163 | @{$self->{children}}, $chld; |
227 | @{$self->{children}}, $chld; |
164 | |
228 | |
165 | $self->size_allocate ($self->{w}, $self->{h}); |
229 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
230 | if $self->{w}; #TODO: check for "realised state" |
166 | } |
231 | } |
167 | |
232 | |
168 | sub remove { |
233 | sub remove { |
169 | my ($self, $widget) = @_; |
234 | my ($self, $widget) = @_; |
170 | |
235 | |
… | |
… | |
174 | } |
239 | } |
175 | |
240 | |
176 | sub find_widget { |
241 | sub find_widget { |
177 | my ($self, $x, $y) = @_; |
242 | my ($self, $x, $y) = @_; |
178 | |
243 | |
|
|
244 | $x -= $self->{x}; |
|
|
245 | $y -= $self->{y}; |
|
|
246 | |
179 | my $res; |
247 | my $res; |
180 | |
248 | |
181 | for (@{ $self->{children} }) { |
249 | for (reverse @{ $self->{children} }) { |
182 | $res = $_->find_widget ($x, $y) |
250 | $res = $_->find_widget ($x, $y) |
183 | and return $res; |
251 | and return $res; |
184 | } |
252 | } |
185 | |
253 | |
186 | () |
254 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
187 | } |
|
|
188 | |
|
|
189 | sub size_request { |
|
|
190 | my ($self) = @_; |
|
|
191 | |
|
|
192 | my ($hs, $ws) = (0, 0); |
|
|
193 | for (@{$self->{children} || []}) { |
|
|
194 | my ($w, $h) = $_->size_request; |
|
|
195 | $hs += $h; |
|
|
196 | if ($ws < $w) { $ws = $w } |
|
|
197 | } |
|
|
198 | |
|
|
199 | return ($ws, $hs); |
|
|
200 | } |
255 | } |
201 | |
256 | |
202 | sub _draw { |
257 | sub _draw { |
203 | my ($self) = @_; |
258 | my ($self) = @_; |
204 | |
259 | |
… | |
… | |
217 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
272 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
218 | } |
273 | } |
219 | |
274 | |
220 | sub size_allocate { |
275 | sub size_allocate { |
221 | my ($self, $w, $h) = @_; |
276 | my ($self, $w, $h) = @_; |
|
|
277 | |
222 | $self->SUPER::size_allocate ($w, $h); |
278 | $self->SUPER::size_allocate ($w, $h); |
223 | $self->{children}[0]->size_allocate ($w, $h) |
279 | $self->{children}[0]->size_allocate ($w, $h) |
224 | if $self->{children}[0] |
280 | if $self->{children}[0] |
225 | } |
281 | } |
226 | |
282 | |
227 | ############################################################################# |
283 | ############################################################################# |
228 | |
284 | |
229 | package Crossfire::Client::Widget::Toplevel; |
285 | package Crossfire::Client::Widget::Window; |
230 | |
286 | |
231 | our @ISA = Crossfire::Client::Widget::Container::; |
287 | our @ISA = Crossfire::Client::Widget::Bin::; |
|
|
288 | |
|
|
289 | use SDL::OpenGL; |
|
|
290 | |
|
|
291 | sub new { |
|
|
292 | my ($class, $x, $y, $z, $w, $h) = @_; |
|
|
293 | |
|
|
294 | my $self = $class->SUPER::new; |
|
|
295 | |
|
|
296 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
297 | } |
232 | |
298 | |
233 | sub update { |
299 | sub update { |
234 | my ($self) = @_; |
300 | my ($self) = @_; |
235 | |
301 | |
236 | ::refresh (); |
|
|
237 | } |
|
|
238 | |
|
|
239 | ############################################################################# |
|
|
240 | |
|
|
241 | package Crossfire::Client::Widget::Window; |
|
|
242 | |
|
|
243 | our @ISA = Crossfire::Client::Widget::Bin::; |
|
|
244 | |
|
|
245 | use SDL::OpenGL; |
|
|
246 | |
|
|
247 | sub add { |
|
|
248 | my ($self, $chld) = @_; |
|
|
249 | warn "ADD $chld\n"; |
|
|
250 | $self->SUPER::add ($chld); |
|
|
251 | $chld->set_parent ($self); |
|
|
252 | } |
|
|
253 | |
|
|
254 | sub remove { |
|
|
255 | my ($self) = @_; |
|
|
256 | # TODO FIXME: removing a child from a window will crash, see render_chld |
|
|
257 | # $self->update; |
|
|
258 | } |
|
|
259 | |
|
|
260 | sub update { |
|
|
261 | my ($self) = @_; |
|
|
262 | $self->render_chld; |
302 | $self->render_chld; |
|
|
303 | $self->SUPER::update; |
263 | } |
304 | } |
264 | |
305 | |
265 | sub render_chld { |
306 | sub render_chld { |
266 | my ($self) = @_; |
307 | my ($self) = @_; |
267 | my $chld = $self->get; |
|
|
268 | my ($w, $h) = $self->size_request; |
|
|
269 | |
308 | |
270 | require Carp; |
|
|
271 | Carp::cluck "RENDERCHI $w $h"; |
|
|
272 | warn "RENDERCHI $w $h\n"; |
|
|
273 | $self->{texture} = |
309 | $self->{texture} = |
274 | Crossfire::Client::Texture->new_from_opengl ( |
310 | Crossfire::Client::Texture->new_from_opengl ( |
275 | $w, $h, sub { $chld->draw } |
311 | $self->{w}, $self->{h}, sub { $self->child->draw } |
276 | ); |
312 | ); |
277 | $self->{texture}->upload; |
|
|
278 | } |
|
|
279 | |
|
|
280 | sub size_request { |
|
|
281 | my ($self) = @_; |
|
|
282 | ($self->w, $self->h) |
|
|
283 | } |
313 | } |
284 | |
314 | |
285 | sub size_allocate { |
315 | sub size_allocate { |
286 | my ($self, $w, $h) = @_; |
316 | my ($self, $w, $h) = @_; |
287 | |
317 | |
288 | $self->w ($w); |
318 | $self->{w} = $w; |
289 | $self->h ($h); |
319 | $self->{h} = $h; |
|
|
320 | |
290 | $self->get->size_allocate ($w, $h); |
321 | $self->child->size_allocate ($w, $h); |
291 | |
322 | |
292 | $self->update; #TODO: Move this to the size_request event propably? |
323 | $self->render_chld; |
293 | } |
324 | } |
294 | |
325 | |
295 | sub _draw { |
326 | sub _draw { |
296 | my ($self) = @_; |
327 | my ($self) = @_; |
297 | |
328 | |
… | |
… | |
334 | map { $_ + 4 } $chld->size_request; |
365 | map { $_ + 4 } $chld->size_request; |
335 | } |
366 | } |
336 | |
367 | |
337 | sub size_allocate { |
368 | sub size_allocate { |
338 | my ($self, $w, $h) = @_; |
369 | my ($self, $w, $h) = @_; |
339 | |
370 | |
340 | $self->w ($w); |
371 | $self->{w} = $w; |
341 | $self->h ($h); |
372 | $self->{h} = $h; |
342 | |
373 | |
343 | $self->child->size_allocate ($w - 4, $h - 4); |
374 | $self->child->size_allocate ($w - 4, $h - 4); |
344 | $self->child->move (2, 2); |
375 | $self->child->move (2, 2); |
345 | } |
376 | } |
346 | |
377 | |
… | |
… | |
364 | |
395 | |
365 | ############################################################################# |
396 | ############################################################################# |
366 | |
397 | |
367 | package Crossfire::Client::Widget::FancyFrame; |
398 | package Crossfire::Client::Widget::FancyFrame; |
368 | |
399 | |
369 | our @ISA = Crossfire::Client::Widget::Frame::; |
400 | our @ISA = Crossfire::Client::Widget::Bin::; |
370 | |
401 | |
371 | use SDL::OpenGL; |
402 | use SDL::OpenGL; |
372 | |
403 | |
373 | sub new { |
404 | my @tex = |
374 | my ($self, $theme) = @_; |
|
|
375 | $self = $self->SUPER::new; |
|
|
376 | |
|
|
377 | $self->{txts} = [ |
|
|
378 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
405 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
379 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png) |
406 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
380 | ]; |
|
|
381 | $self |
|
|
382 | } |
|
|
383 | |
407 | |
384 | sub size_request { |
408 | sub size_request { |
385 | my ($self) = @_; |
409 | my ($self) = @_; |
386 | |
410 | |
387 | my ($w, $h) = $self->SUPER::size_request; |
411 | my ($w, $h) = $self->SUPER::size_request; |
388 | |
412 | |
389 | $h += $self->{txts}->[1]->{height}; |
413 | $h += $tex[1]->{height}; |
390 | $h += $self->{txts}->[4]->{height}; |
414 | $h += $tex[4]->{height}; |
391 | $w += $self->{txts}->[2]->{width}; |
415 | $w += $tex[2]->{width}; |
392 | $w += $self->{txts}->[3]->{width}; |
416 | $w += $tex[3]->{width}; |
393 | |
417 | |
394 | ($w, $h) |
418 | ($w, $h) |
395 | } |
419 | } |
396 | |
420 | |
397 | sub size_allocate { |
421 | sub size_allocate { |
398 | my ($self, $w, $h) = @_; |
422 | my ($self, $w, $h) = @_; |
399 | |
423 | |
400 | $self->SUPER::size_allocate ($w, $h); |
424 | $self->SUPER::size_allocate ($w, $h); |
401 | |
425 | |
402 | $h -= $self->{txts}->[1]->{height}; |
426 | $h -= $tex[1]->{height}; |
403 | $h -= $self->{txts}->[4]->{height}; |
427 | $h -= $tex[4]->{height}; |
404 | $w -= $self->{txts}->[2]->{width}; |
428 | $w -= $tex[2]->{width}; |
405 | $w -= $self->{txts}->[3]->{width}; |
429 | $w -= $tex[3]->{width}; |
406 | |
430 | |
407 | $h = $h < 0 ? 0 : $h; |
431 | $h = $h < 0 ? 0 : $h; |
408 | $w = $w < 0 ? 0 : $w; |
432 | $w = $w < 0 ? 0 : $w; |
409 | warn "CHILD:$w $h\n"; |
433 | |
410 | $self->child->size_allocate ($w, $h); |
434 | $self->child->size_allocate ($w, $h); |
411 | $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
435 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
412 | } |
436 | } |
413 | |
437 | |
414 | sub _draw { |
438 | sub _draw { |
415 | my ($self) = @_; |
439 | my ($self) = @_; |
416 | |
440 | |
417 | my ($w, $h) = ($self->w, $self->h); |
441 | my ($w, $h) = ($self->{w}, $self->{h}); |
418 | my ($cw, $ch) = ($self->child->w, $self->child->h); |
442 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
419 | |
443 | |
420 | glEnable GL_BLEND; |
444 | glEnable GL_BLEND; |
421 | glEnable GL_TEXTURE_2D; |
445 | glEnable GL_TEXTURE_2D; |
422 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
446 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
423 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
447 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
424 | |
448 | |
425 | my $top = $self->{txts}->[1]; |
449 | my $top = $tex[1]; |
426 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
450 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
427 | |
451 | |
428 | glBegin GL_QUADS; |
452 | glBegin GL_QUADS; |
429 | glTexCoord 0, 0; glVertex 0 , 0; |
453 | glTexCoord 0, 0; glVertex 0 , 0; |
430 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
454 | glTexCoord 0, 1; glVertex 0 , $top->{height}; |
431 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
455 | glTexCoord 1, 1; glVertex $w , $top->{height}; |
432 | glTexCoord 1, 0; glVertex $w , 0; |
456 | glTexCoord 1, 0; glVertex $w , 0; |
433 | glEnd; |
457 | glEnd; |
434 | |
458 | |
435 | my $left = $self->{txts}->[3]; |
459 | my $left = $tex[3]; |
436 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
460 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
437 | |
461 | |
438 | glBegin GL_QUADS; |
462 | glBegin GL_QUADS; |
439 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
463 | glTexCoord 0, 0; glVertex 0 , $top->{height}; |
440 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
464 | glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; |
441 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
465 | glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; |
442 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
466 | glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; |
443 | glEnd; |
467 | glEnd; |
444 | |
468 | |
445 | my $right = $self->{txts}->[2]; |
469 | my $right = $tex[2]; |
446 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
470 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
447 | |
471 | |
448 | glBegin GL_QUADS; |
472 | glBegin GL_QUADS; |
449 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
473 | glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; |
450 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
474 | glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; |
451 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
475 | glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; |
452 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
476 | glTexCoord 1, 0; glVertex $w , $top->{height}; |
453 | glEnd; |
477 | glEnd; |
454 | |
478 | |
455 | my $bottom = $self->{txts}->[4]; |
479 | my $bottom = $tex[4]; |
456 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
480 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
457 | |
481 | |
458 | glBegin GL_QUADS; |
482 | glBegin GL_QUADS; |
459 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
483 | glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; |
460 | glTexCoord 0, 1; glVertex 0 , $h; |
484 | glTexCoord 0, 1; glVertex 0 , $h; |
461 | glTexCoord 1, 1; glVertex $w , $h; |
485 | glTexCoord 1, 1; glVertex $w , $h; |
462 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
486 | glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; |
463 | glEnd; |
487 | glEnd; |
464 | |
488 | |
465 | my $bg = $self->{txts}->[0]; |
489 | my $bg = $tex[0]; |
466 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
490 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
467 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
491 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
468 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
492 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
469 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
493 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
470 | |
494 | |
… | |
… | |
577 | package Crossfire::Client::Widget::VBox; |
601 | package Crossfire::Client::Widget::VBox; |
578 | |
602 | |
579 | our @ISA = Crossfire::Client::Widget::Container::; |
603 | our @ISA = Crossfire::Client::Widget::Container::; |
580 | |
604 | |
581 | use SDL::OpenGL; |
605 | use SDL::OpenGL; |
|
|
606 | |
|
|
607 | sub size_request { |
|
|
608 | my ($self) = @_; |
|
|
609 | |
|
|
610 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
611 | |
|
|
612 | ( |
|
|
613 | (List::Util::max map $_->[0], @alloc), |
|
|
614 | (List::Util::sum map $_->[1], @alloc), |
|
|
615 | ) |
|
|
616 | } |
582 | |
617 | |
583 | sub size_allocate { |
618 | sub size_allocate { |
584 | my ($self, $w, $h) = @_; |
619 | my ($self, $w, $h) = @_; |
585 | |
620 | |
586 | $self->w ($w); |
621 | $self->w ($w); |
… | |
… | |
619 | $y += $h; |
654 | $y += $h; |
620 | } |
655 | } |
621 | } |
656 | } |
622 | } |
657 | } |
623 | |
658 | |
624 | sub _draw { |
|
|
625 | my ($self) = @_; |
|
|
626 | |
|
|
627 | my ($x, $y); |
|
|
628 | for (@{$self->{children} || []}) { |
|
|
629 | $_->draw; |
|
|
630 | $y += $_->h; |
|
|
631 | } |
|
|
632 | } |
|
|
633 | |
|
|
634 | ############################################################################# |
659 | ############################################################################# |
635 | |
660 | |
636 | package Crossfire::Client::Widget::Label; |
661 | package Crossfire::Client::Widget::Label; |
637 | |
662 | |
638 | our @ISA = Crossfire::Client::Widget::; |
663 | our @ISA = Crossfire::Client::Widget::; |
… | |
… | |
640 | use SDL::OpenGL; |
665 | use SDL::OpenGL; |
641 | |
666 | |
642 | sub new { |
667 | sub new { |
643 | my ($class, $x, $y, $z, $height, $text) = @_; |
668 | my ($class, $x, $y, $z, $height, $text) = @_; |
644 | |
669 | |
|
|
670 | $height ||= $::FONTSIZE; |
|
|
671 | |
645 | # TODO: color, and make height, xyz etc. optional |
672 | # TODO: color, and make height, xyz etc. optional |
646 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
673 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
647 | |
674 | |
648 | $self->set_text ($text); |
675 | $self->set_text ($text); |
649 | |
676 | |
… | |
… | |
666 | } |
693 | } |
667 | |
694 | |
668 | sub size_request { |
695 | sub size_request { |
669 | my ($self) = @_; |
696 | my ($self) = @_; |
670 | |
697 | |
671 | ( |
698 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
|
|
699 | ( |
672 | $self->{texture}{width}, |
700 | $self->{texture}{width}, |
673 | $self->{texture}{height}, |
701 | $self->{texture}{height}, |
|
|
702 | ) |
|
|
703 | } else { |
|
|
704 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
|
|
705 | |
|
|
706 | ($w, $h) |
674 | ) |
707 | } |
|
|
708 | |
675 | } |
709 | } |
676 | |
710 | |
677 | sub _draw { |
711 | sub _draw { |
678 | my ($self) = @_; |
712 | my ($self) = @_; |
679 | |
713 | |
… | |
… | |
698 | glDisable GL_TEXTURE_2D; |
732 | glDisable GL_TEXTURE_2D; |
699 | } |
733 | } |
700 | |
734 | |
701 | ############################################################################# |
735 | ############################################################################# |
702 | |
736 | |
703 | package Crossfire::Client::Widget::TextEntry; |
737 | package Crossfire::Client::Widget::Entry; |
704 | |
738 | |
705 | our @ISA = Crossfire::Client::Widget::Label::; |
739 | our @ISA = Crossfire::Client::Widget::Label::; |
706 | |
740 | |
707 | use SDL; |
741 | use SDL; |
708 | use SDL::OpenGL; |
742 | use SDL::OpenGL; |
… | |
… | |
711 | my ($self, $ev) = @_; |
745 | my ($self, $ev) = @_; |
712 | |
746 | |
713 | my $mod = $ev->key_mod; |
747 | my $mod = $ev->key_mod; |
714 | my $sym = $ev->key_sym; |
748 | my $sym = $ev->key_sym; |
715 | |
749 | |
716 | $ev->set_unicode (1); |
|
|
717 | my $uni = $ev->key_unicode; |
750 | my $uni = $ev->key_unicode; |
718 | |
751 | |
719 | my $text = $self->get_text; |
752 | my $text = $self->get_text; |
720 | |
753 | |
721 | if ($sym == SDLK_BACKSPACE) { |
754 | if ($sym == SDLK_BACKSPACE) { |
722 | substr $text, -1, 1, ''; |
755 | substr $text, -1, 1, ''; |
723 | |
|
|
724 | } elsif ($uni) { |
756 | } elsif ($uni) { |
725 | $text .= chr $uni; |
757 | $text .= chr $uni; |
|
|
758 | print "$uni <$text>\n";#d# |
726 | } |
759 | } |
|
|
760 | |
727 | $self->set_text ($text); |
761 | $self->set_text ($text); |
|
|
762 | } |
|
|
763 | |
|
|
764 | sub button_down { |
|
|
765 | my ($self, $ev) = @_; |
|
|
766 | |
|
|
767 | $self->focus_in; |
|
|
768 | } |
|
|
769 | |
|
|
770 | sub _draw { |
|
|
771 | my ($self) = @_; |
|
|
772 | |
|
|
773 | if ($FOCUS == $self) { |
|
|
774 | glColor 1, 1, 1; |
|
|
775 | } else { |
|
|
776 | glColor 0.7, 0.7, 0.7; |
|
|
777 | } |
|
|
778 | |
|
|
779 | glBegin GL_QUADS; |
|
|
780 | glVertex 0 , 0; |
|
|
781 | glVertex 0 , $self->{h} - 1; |
|
|
782 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
783 | glVertex $self->{w} - 1, 0; |
|
|
784 | glEnd; |
|
|
785 | |
|
|
786 | $self->SUPER::_draw; |
728 | } |
787 | } |
729 | |
788 | |
730 | ############################################################################# |
789 | ############################################################################# |
731 | |
790 | |
732 | package Crossfire::Client::Widget::MapWidget; |
791 | package Crossfire::Client::Widget::MapWidget; |
… | |
… | |
747 | |
806 | |
748 | sub key_up { |
807 | sub key_up { |
749 | } |
808 | } |
750 | |
809 | |
751 | sub size_request { |
810 | sub size_request { |
752 | |
811 | ( |
753 | } |
812 | 1 + int $::WIDTH / 32, |
754 | |
813 | 1 + int $::HEIGHT / 32, |
755 | sub size_allocate { |
814 | ) |
756 | } |
815 | } |
757 | |
816 | |
758 | sub _draw { |
817 | sub _draw { |
759 | my ($self) = @_; |
818 | my ($self) = @_; |
760 | |
819 | |
… | |
… | |
793 | my $cell = $map->[$x + $xofs][$y + $yofs] |
852 | my $cell = $map->[$x + $xofs][$y + $yofs] |
794 | or next; |
853 | or next; |
795 | |
854 | |
796 | my $darkness = $cell->[0] * (1 / 255); |
855 | my $darkness = $cell->[0] * (1 / 255); |
797 | if ($darkness < 0) { |
856 | if ($darkness < 0) { |
798 | $darkness = 0.15; |
857 | $darkness = $cell->[1] ? 0.1 : 0; |
799 | } |
858 | } |
800 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
859 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
801 | |
860 | |
802 | for my $num (grep $_, @$cell[1,2,3]) { |
861 | for my $num (grep $_, @$cell[1,2,3]) { |
803 | my $tex = $::CONN->{face}[$num]{texture} || next; |
862 | my $tex = $::CONN->{face}[$num]{texture} || next; |
… | |
… | |
832 | |
891 | |
833 | $lighting = new Crossfire::Client::Texture |
892 | $lighting = new Crossfire::Client::Texture |
834 | width => $sw4, |
893 | width => $sw4, |
835 | height => $sh, |
894 | height => $sh, |
836 | data => $lighting, |
895 | data => $lighting, |
837 | internalformat => GL_ALPHA4, |
896 | internalformat => GL_ALPHA, |
838 | format => GL_ALPHA; |
897 | format => GL_ALPHA; |
839 | |
898 | |
840 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
899 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
841 | glColor 0, 0, 0, 0.75; |
900 | glColor 0.45, 0.45, 0.45, 1; |
842 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
901 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
843 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
902 | glBindTexture GL_TEXTURE_2D, $lighting->{name}; |
844 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
903 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
845 | glBegin GL_QUADS; |
904 | glBegin GL_QUADS; |
846 | glTexCoord 0, 0; glVertex 0 , 0; |
905 | glTexCoord 0, 0; glVertex 0 , 0; |
… | |
… | |
914 | |
973 | |
915 | sub moveto { |
974 | sub moveto { |
916 | my ($self, $x, $y) = @_; |
975 | my ($self, $x, $y) = @_; |
917 | |
976 | |
918 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
977 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
919 | $self->{speed} = 0.2; |
978 | $self->{speed} = 2; |
920 | $self->{time} = 1; |
979 | $self->{time} = 1; |
921 | |
980 | |
922 | ::animation_start $self; |
981 | ::animation_start $self; |
923 | } |
982 | } |
924 | |
983 | |
… | |
… | |
939 | |
998 | |
940 | sub _draw { |
999 | sub _draw { |
941 | my ($self) = @_; |
1000 | my ($self) = @_; |
942 | |
1001 | |
943 | glPushMatrix; |
1002 | glPushMatrix; |
944 | glRotate $self->{time} * 10000, 0, 1, 0; |
1003 | glRotate $self->{time} * 1000, 0, 1, 0; |
945 | $self->{children}[0]->draw; |
1004 | $self->{children}[0]->draw; |
946 | glPopMatrix; |
1005 | glPopMatrix; |
947 | } |
1006 | } |
948 | |
1007 | |
949 | 1; |
1008 | ############################################################################# |
950 | |
1009 | |
|
|
1010 | package Crossfire::Client::Widget::Toplevel; |
|
|
1011 | |
|
|
1012 | our @ISA = Crossfire::Client::Widget::Container::; |
|
|
1013 | |
|
|
1014 | sub size_request { |
|
|
1015 | ($::WIDTH, $::HEIGHT) |
|
|
1016 | } |
|
|
1017 | |
|
|
1018 | sub size_allocate { |
|
|
1019 | my ($self, $w, $h) = @_; |
|
|
1020 | |
|
|
1021 | $self->SUPER::size_allocate ($w, $h); |
|
|
1022 | |
|
|
1023 | $_->size_allocate ($_->size_request) |
|
|
1024 | for @{$self->{children}}; |
|
|
1025 | } |
|
|
1026 | |
|
|
1027 | sub update { |
|
|
1028 | my ($self) = @_; |
|
|
1029 | |
|
|
1030 | $self->size_allocate ($self->size_request); |
|
|
1031 | ::refresh (); |
|
|
1032 | } |
|
|
1033 | |
|
|
1034 | sub add { |
|
|
1035 | my ($self, $widget) = @_; |
|
|
1036 | |
|
|
1037 | $self->SUPER::add ($widget); |
|
|
1038 | |
|
|
1039 | $widget->size_allocate ($widget->size_request); |
|
|
1040 | } |
|
|
1041 | |
|
|
1042 | sub draw { |
|
|
1043 | my ($self) = @_; |
|
|
1044 | |
|
|
1045 | $self->_draw; |
|
|
1046 | } |
|
|
1047 | |
|
|
1048 | ############################################################################# |
|
|
1049 | |
|
|
1050 | package Crossfire::Client::Widget; |
|
|
1051 | |
|
|
1052 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
|
|
1053 | |
|
|
1054 | 1 |
|
|
1055 | |