… | |
… | |
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 |
… | |
… | |
37 | Carp::confess "size_request is abtract"; |
92 | Carp::confess "size_request is abtract"; |
38 | } |
93 | } |
39 | |
94 | |
40 | sub size_allocate { |
95 | sub size_allocate { |
41 | my ($self, $w, $h) = @_; |
96 | my ($self, $w, $h) = @_; |
|
|
97 | |
42 | $self->w ($w); |
98 | $self->{w} = $w; |
43 | $self->h ($h); |
99 | $self->{h} = $h; |
44 | } |
100 | } |
45 | |
101 | |
46 | sub focus_in { |
102 | sub focus_in { |
47 | my ($widget) = @_; |
103 | my ($self) = @_; |
48 | $FOCUS = $widget; |
104 | |
|
|
105 | my $focus = $FOCUS; $FOCUS = $self; |
|
|
106 | $focus->update if $focus; |
|
|
107 | $FOCUS->update; |
49 | } |
108 | } |
50 | |
109 | |
51 | sub focus_out { |
110 | sub focus_out { |
52 | my ($widget) = @_; |
111 | my ($self) = @_; |
53 | } |
|
|
54 | |
112 | |
55 | sub key_down { |
113 | return unless $FOCUS == $self; |
56 | my ($widget, $sdlev) = @_; |
|
|
57 | } |
|
|
58 | |
114 | |
59 | sub key_up { |
115 | my $focus = $FOCUS; undef $FOCUS; |
60 | my ($widget, $sdlev) = @_; |
116 | $focus->update if $focus; #? |
61 | } |
117 | } |
62 | |
118 | |
|
|
119 | sub mouse_motion { } |
|
|
120 | sub button_up { } |
63 | sub button_down { |
121 | sub button_down { } |
64 | my ($widget, $sdlev) = @_; |
122 | sub key_down { } |
65 | } |
123 | sub key_up { } |
66 | |
124 | |
67 | sub button_up { |
|
|
68 | my ($widget, $sdlev) = @_; |
|
|
69 | } |
|
|
70 | |
|
|
71 | sub w { $_[0]->{w} = $_[1] if $_[1]; $_[0]->{w} } |
125 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
72 | sub h { $_[0]->{h} = $_[1] if $_[1]; $_[0]->{h} } |
126 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
73 | sub x { $_[0]->{x} = $_[1] if $_[1]; $_[0]->{x} } |
127 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
74 | sub y { $_[0]->{y} = $_[1] if $_[1]; $_[0]->{y} } |
128 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
75 | sub z { $_[0]->{z} = $_[1] if $_[1]; $_[0]->{z} } |
129 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
76 | |
130 | |
77 | sub draw { |
131 | sub draw { |
78 | my ($self) = @_; |
132 | my ($self) = @_; |
79 | |
133 | |
80 | glPushMatrix; |
134 | glPushMatrix; |
81 | glTranslate $self->{x}, $self->{y}, 0; |
135 | glTranslate $self->{x}, $self->{y}, 0; |
82 | $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 | } |
83 | glPopMatrix; |
148 | glPopMatrix; |
84 | } |
149 | } |
85 | |
150 | |
86 | sub _draw { |
151 | sub _draw { |
87 | my ($self) = @_; |
152 | my ($self) = @_; |
… | |
… | |
159 | |
224 | |
160 | @{$self->{children}} = |
225 | @{$self->{children}} = |
161 | sort { $a->{z} <=> $b->{z} } |
226 | sort { $a->{z} <=> $b->{z} } |
162 | @{$self->{children}}, $chld; |
227 | @{$self->{children}}, $chld; |
163 | |
228 | |
164 | $self->size_allocate ($self->{w}, $self->{h}); |
229 | $self->size_allocate ($self->{w}, $self->{h}) |
|
|
230 | if $self->{w}; #TODO: check for "realised state" |
165 | } |
231 | } |
166 | |
232 | |
167 | sub remove { |
233 | sub remove { |
168 | my ($self, $widget) = @_; |
234 | my ($self, $widget) = @_; |
169 | |
235 | |
… | |
… | |
173 | } |
239 | } |
174 | |
240 | |
175 | sub find_widget { |
241 | sub find_widget { |
176 | my ($self, $x, $y) = @_; |
242 | my ($self, $x, $y) = @_; |
177 | |
243 | |
|
|
244 | $x -= $self->{x}; |
|
|
245 | $y -= $self->{y}; |
|
|
246 | |
178 | my $res; |
247 | my $res; |
179 | |
248 | |
180 | for (@{ $self->{children} }) { |
249 | for (reverse @{ $self->{children} }) { |
181 | $res = $_->find_widget ($x, $y) |
250 | $res = $_->find_widget ($x, $y) |
182 | and return $res; |
251 | and return $res; |
183 | } |
252 | } |
184 | |
253 | |
185 | () |
254 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
186 | } |
|
|
187 | |
|
|
188 | sub size_request { |
|
|
189 | my ($self) = @_; |
|
|
190 | |
|
|
191 | my ($hs, $ws) = (0, 0); |
|
|
192 | for (@{$self->{children} || []}) { |
|
|
193 | my ($w, $h) = $_->size_request; |
|
|
194 | $hs += $h; |
|
|
195 | if ($ws < $w) { $ws = $w } |
|
|
196 | } |
|
|
197 | |
|
|
198 | return ($ws, $hs); |
|
|
199 | } |
255 | } |
200 | |
256 | |
201 | sub _draw { |
257 | sub _draw { |
202 | my ($self) = @_; |
258 | my ($self) = @_; |
203 | |
259 | |
… | |
… | |
216 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
272 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
217 | } |
273 | } |
218 | |
274 | |
219 | sub size_allocate { |
275 | sub size_allocate { |
220 | my ($self, $w, $h) = @_; |
276 | my ($self, $w, $h) = @_; |
|
|
277 | |
221 | $self->SUPER::size_allocate ($w, $h); |
278 | $self->SUPER::size_allocate ($w, $h); |
222 | $self->{children}[0]->size_allocate ($w, $h) |
279 | $self->{children}[0]->size_allocate ($w, $h) |
223 | if $self->{children}[0] |
280 | if $self->{children}[0] |
224 | } |
281 | } |
225 | |
282 | |
226 | ############################################################################# |
283 | ############################################################################# |
227 | |
284 | |
228 | package Crossfire::Client::Widget::Toplevel; |
285 | package Crossfire::Client::Widget::Window; |
229 | |
286 | |
230 | 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 | } |
231 | |
298 | |
232 | sub update { |
299 | sub update { |
233 | my ($self) = @_; |
300 | my ($self) = @_; |
234 | |
301 | |
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; |
302 | $self->render_chld; |
|
|
303 | $self->SUPER::update; |
262 | } |
304 | } |
263 | |
305 | |
264 | sub render_chld { |
306 | sub render_chld { |
265 | my ($self) = @_; |
307 | my ($self) = @_; |
266 | my $chld = $self->get; |
|
|
267 | my ($w, $h) = $self->size_request; |
|
|
268 | |
308 | |
269 | require Carp; |
|
|
270 | Carp::cluck "RENDERCHI $w $h"; |
|
|
271 | warn "RENDERCHI $w $h\n"; |
|
|
272 | $self->{texture} = |
309 | $self->{texture} = |
273 | Crossfire::Client::Texture->new_from_opengl ( |
310 | Crossfire::Client::Texture->new_from_opengl ( |
274 | $w, $h, sub { $chld->draw } |
311 | $self->{w}, $self->{h}, sub { $self->child->draw } |
275 | ); |
312 | ); |
276 | $self->{texture}->upload; |
|
|
277 | } |
|
|
278 | |
|
|
279 | sub size_request { |
|
|
280 | my ($self) = @_; |
|
|
281 | ($self->w, $self->h) |
|
|
282 | } |
313 | } |
283 | |
314 | |
284 | sub size_allocate { |
315 | sub size_allocate { |
285 | my ($self, $w, $h) = @_; |
316 | my ($self, $w, $h) = @_; |
286 | |
317 | |
287 | $self->w ($w); |
318 | $self->{w} = $w; |
288 | $self->h ($h); |
319 | $self->{h} = $h; |
|
|
320 | |
289 | $self->get->size_allocate ($w, $h); |
321 | $self->child->size_allocate ($w, $h); |
290 | |
322 | |
291 | $self->update; #TODO: Move this to the size_request event propably? |
323 | $self->render_chld; |
292 | } |
324 | } |
293 | |
325 | |
294 | sub _draw { |
326 | sub _draw { |
295 | my ($self) = @_; |
327 | my ($self) = @_; |
296 | |
328 | |
… | |
… | |
300 | or return; |
332 | or return; |
301 | |
333 | |
302 | glEnable GL_BLEND; |
334 | glEnable GL_BLEND; |
303 | glEnable GL_TEXTURE_2D; |
335 | glEnable GL_TEXTURE_2D; |
304 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
336 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
305 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
306 | |
337 | |
307 | glBegin GL_QUADS; |
338 | $tex->draw_quad (0, 0, $w, $h); |
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 | |
339 | |
314 | glDisable GL_BLEND; |
340 | glDisable GL_BLEND; |
315 | glDisable GL_TEXTURE_2D; |
341 | glDisable GL_TEXTURE_2D; |
316 | } |
342 | } |
317 | |
343 | |
… | |
… | |
333 | map { $_ + 4 } $chld->size_request; |
359 | map { $_ + 4 } $chld->size_request; |
334 | } |
360 | } |
335 | |
361 | |
336 | sub size_allocate { |
362 | sub size_allocate { |
337 | my ($self, $w, $h) = @_; |
363 | my ($self, $w, $h) = @_; |
338 | |
364 | |
339 | $self->w ($w); |
365 | $self->{w} = $w; |
340 | $self->h ($h); |
366 | $self->{h} = $h; |
341 | |
367 | |
342 | $self->child->size_allocate ($w - 4, $h - 4); |
368 | $self->child->size_allocate ($w - 4, $h - 4); |
343 | $self->child->move (2, 2); |
369 | $self->child->move (2, 2); |
344 | } |
370 | } |
345 | |
371 | |
… | |
… | |
350 | |
376 | |
351 | my ($w, $h) = $chld->size_request; |
377 | my ($w, $h) = $chld->size_request; |
352 | |
378 | |
353 | glBegin GL_QUADS; |
379 | glBegin GL_QUADS; |
354 | glColor 0, 0, 0; |
380 | glColor 0, 0, 0; |
355 | glTexCoord 0, 0; glVertex 0 , 0; |
381 | glVertex 0 , 0; |
356 | glTexCoord 0, 1; glVertex 0 , $h + 4; |
382 | glVertex 0 , $h + 4; |
357 | glTexCoord 1, 1; glVertex $w + 4 , $h + 4; |
383 | glVertex $w + 4 , $h + 4; |
358 | glTexCoord 1, 0; glVertex $w + 4 , 0; |
384 | glVertex $w + 4 , 0; |
359 | glEnd; |
385 | glEnd; |
360 | |
386 | |
361 | $chld->draw; |
387 | $chld->draw; |
362 | } |
388 | } |
363 | |
389 | |
364 | ############################################################################# |
390 | ############################################################################# |
365 | |
391 | |
366 | package Crossfire::Client::Widget::FancyFrame; |
392 | package Crossfire::Client::Widget::FancyFrame; |
367 | |
393 | |
368 | our @ISA = Crossfire::Client::Widget::Frame::; |
394 | our @ISA = Crossfire::Client::Widget::Bin::; |
369 | |
395 | |
370 | use SDL::OpenGL; |
396 | use SDL::OpenGL; |
371 | |
397 | |
372 | sub new { |
398 | my @tex = |
373 | my ($self, $theme) = @_; |
|
|
374 | $self = $self->SUPER::new; |
|
|
375 | |
|
|
376 | $self->{txts} = [ |
|
|
377 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
399 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
378 | qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ |
400 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
379 | ]; |
|
|
380 | $self |
|
|
381 | } |
|
|
382 | |
401 | |
383 | sub size_request { |
402 | sub size_request { |
384 | my ($self) = @_; |
403 | my ($self) = @_; |
385 | |
404 | |
386 | my ($w, $h) = $self->SUPER::size_request; |
405 | my ($w, $h) = $self->SUPER::size_request; |
387 | |
406 | |
388 | $h += $self->{txts}->[1]->{height}; |
407 | $h += $tex[1]->{height}; |
389 | $h += $self->{txts}->[4]->{height}; |
408 | $h += $tex[4]->{height}; |
390 | $w += $self->{txts}->[2]->{width}; |
409 | $w += $tex[2]->{width}; |
391 | $w += $self->{txts}->[3]->{width}; |
410 | $w += $tex[3]->{width}; |
392 | |
411 | |
393 | ($w, $h) |
412 | ($w, $h) |
394 | } |
413 | } |
395 | |
414 | |
396 | sub size_allocate { |
415 | sub size_allocate { |
397 | my ($self, $w, $h) = @_; |
416 | my ($self, $w, $h) = @_; |
398 | |
417 | |
399 | $self->w ($w); |
418 | $self->SUPER::size_allocate ($w, $h); |
400 | $self->h ($h); |
419 | |
401 | $h -= $self->{txts}->[1]->{height}; |
420 | $h -= $tex[1]->{height}; |
402 | $h -= $self->{txts}->[4]->{height}; |
421 | $h -= $tex[4]->{height}; |
403 | $w -= $self->{txts}->[2]->{width}; |
422 | $w -= $tex[2]->{width}; |
404 | $w -= $self->{txts}->[3]->{width}; |
423 | $w -= $tex[3]->{width}; |
405 | |
424 | |
406 | $h = $h < 0 ? 0 : $h; |
425 | $h = $h < 0 ? 0 : $h; |
407 | $w = $w < 0 ? 0 : $w; |
426 | $w = $w < 0 ? 0 : $w; |
408 | warn "CHILD:$w $h\n"; |
427 | |
409 | $self->child->size_allocate ($w, $h); |
428 | $self->child->size_allocate ($w, $h); |
410 | $self->child->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); |
429 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
411 | } |
430 | } |
412 | |
431 | |
413 | sub _draw { |
432 | sub _draw { |
414 | my ($self) = @_; |
433 | my ($self) = @_; |
415 | |
434 | |
416 | my ($w, $h) = ($self->w, $self->h); |
435 | my ($w, $h) = ($self->{w}, $self->{h}); |
417 | my ($cw, $ch) = ($self->child->w, $self->child->h); |
436 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
418 | |
437 | |
419 | glEnable GL_BLEND; |
438 | glEnable GL_BLEND; |
420 | glEnable GL_TEXTURE_2D; |
439 | glEnable GL_TEXTURE_2D; |
421 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
440 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
422 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
441 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
423 | |
442 | |
424 | my $top = $self->{txts}->[1]; |
443 | my $top = $tex[1]; |
425 | glBindTexture GL_TEXTURE_2D, $top->{name}; |
444 | $top->draw_quad (0, 0, $w, $top->{height}); |
426 | |
445 | |
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]; |
446 | my $left = $tex[3]; |
435 | glBindTexture GL_TEXTURE_2D, $left->{name}; |
447 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
436 | |
448 | |
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]; |
449 | my $right = $tex[2]; |
445 | glBindTexture GL_TEXTURE_2D, $right->{name}; |
450 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
446 | |
451 | |
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]; |
452 | my $bottom = $tex[4]; |
455 | glBindTexture GL_TEXTURE_2D, $bottom->{name}; |
453 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
456 | |
454 | |
457 | glBegin GL_QUADS; |
455 | my $bg = $tex[0]; |
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}; |
456 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
466 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
457 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
467 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
458 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
468 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
459 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
469 | |
460 | |
470 | my $rep_x = $cw / $bg->{width}; |
461 | my $rep_x = $cw / $bg->{width}; |
471 | my $rep_y = $ch / $bg->{height}; |
462 | my $rep_y = $ch / $bg->{height}; |
472 | |
463 | |
473 | glBegin GL_QUADS; |
464 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
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 | |
465 | |
480 | glDisable GL_BLEND; |
466 | glDisable GL_BLEND; |
481 | glDisable GL_TEXTURE_2D; |
467 | glDisable GL_TEXTURE_2D; |
482 | |
468 | |
483 | $self->child->draw; |
469 | $self->child->draw; |
… | |
… | |
576 | package Crossfire::Client::Widget::VBox; |
562 | package Crossfire::Client::Widget::VBox; |
577 | |
563 | |
578 | our @ISA = Crossfire::Client::Widget::Container::; |
564 | our @ISA = Crossfire::Client::Widget::Container::; |
579 | |
565 | |
580 | use SDL::OpenGL; |
566 | use SDL::OpenGL; |
|
|
567 | |
|
|
568 | sub size_request { |
|
|
569 | my ($self) = @_; |
|
|
570 | |
|
|
571 | my @alloc = map [$_->size_request], @{$self->{children}}; |
|
|
572 | |
|
|
573 | ( |
|
|
574 | (List::Util::max map $_->[0], @alloc), |
|
|
575 | (List::Util::sum map $_->[1], @alloc), |
|
|
576 | ) |
|
|
577 | } |
581 | |
578 | |
582 | sub size_allocate { |
579 | sub size_allocate { |
583 | my ($self, $w, $h) = @_; |
580 | my ($self, $w, $h) = @_; |
584 | |
581 | |
585 | $self->w ($w); |
582 | $self->w ($w); |
… | |
… | |
618 | $y += $h; |
615 | $y += $h; |
619 | } |
616 | } |
620 | } |
617 | } |
621 | } |
618 | } |
622 | |
619 | |
623 | sub _draw { |
|
|
624 | my ($self) = @_; |
|
|
625 | |
|
|
626 | my ($x, $y); |
|
|
627 | for (@{$self->{children} || []}) { |
|
|
628 | $_->draw; |
|
|
629 | $y += $_->h; |
|
|
630 | } |
|
|
631 | } |
|
|
632 | |
|
|
633 | ############################################################################# |
620 | ############################################################################# |
634 | |
621 | |
635 | package Crossfire::Client::Widget::Label; |
622 | package Crossfire::Client::Widget::Label; |
636 | |
623 | |
637 | our @ISA = Crossfire::Client::Widget::; |
624 | our @ISA = Crossfire::Client::Widget::; |
… | |
… | |
639 | use SDL::OpenGL; |
626 | use SDL::OpenGL; |
640 | |
627 | |
641 | sub new { |
628 | sub new { |
642 | my ($class, $x, $y, $z, $height, $text) = @_; |
629 | my ($class, $x, $y, $z, $height, $text) = @_; |
643 | |
630 | |
|
|
631 | $height ||= $::FONTSIZE; |
|
|
632 | |
644 | # TODO: color, and make height, xyz etc. optional |
633 | # TODO: color, and make height, xyz etc. optional |
645 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
634 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
646 | |
635 | |
647 | $self->set_text ($text); |
636 | $self->set_text ($text); |
648 | |
637 | |
… | |
… | |
665 | } |
654 | } |
666 | |
655 | |
667 | sub size_request { |
656 | sub size_request { |
668 | my ($self) = @_; |
657 | my ($self) = @_; |
669 | |
658 | |
670 | ( |
659 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
|
|
660 | ( |
671 | $self->{texture}{width}, |
661 | $self->{texture}{width}, |
672 | $self->{texture}{height}, |
662 | $self->{texture}{height}, |
|
|
663 | ) |
|
|
664 | } else { |
|
|
665 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
|
|
666 | |
|
|
667 | ($w, $h) |
673 | ) |
668 | } |
|
|
669 | |
674 | } |
670 | } |
675 | |
671 | |
676 | sub _draw { |
672 | sub _draw { |
677 | my ($self) = @_; |
673 | my ($self) = @_; |
678 | |
674 | |
… | |
… | |
680 | |
676 | |
681 | glEnable GL_BLEND; |
677 | glEnable GL_BLEND; |
682 | glEnable GL_TEXTURE_2D; |
678 | glEnable GL_TEXTURE_2D; |
683 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
679 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
684 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
680 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
685 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
686 | |
681 | |
687 | glColor 1, 0, 0, 1; # TODO color |
682 | glColor 1, 0, 0, 1; # TODO color |
688 | |
683 | |
689 | glBegin GL_QUADS; |
684 | $tex->draw_quad (0, 0); |
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 | |
685 | |
696 | glDisable GL_BLEND; |
686 | glDisable GL_BLEND; |
697 | glDisable GL_TEXTURE_2D; |
687 | glDisable GL_TEXTURE_2D; |
698 | } |
688 | } |
699 | |
689 | |
700 | ############################################################################# |
690 | ############################################################################# |
701 | |
691 | |
702 | package Crossfire::Client::Widget::TextEntry; |
692 | package Crossfire::Client::Widget::Entry; |
703 | |
693 | |
704 | our @ISA = Crossfire::Client::Widget::Label::; |
694 | our @ISA = Crossfire::Client::Widget::Label::; |
705 | |
695 | |
706 | use SDL; |
696 | use SDL; |
707 | use SDL::OpenGL; |
697 | use SDL::OpenGL; |
… | |
… | |
710 | my ($self, $ev) = @_; |
700 | my ($self, $ev) = @_; |
711 | |
701 | |
712 | my $mod = $ev->key_mod; |
702 | my $mod = $ev->key_mod; |
713 | my $sym = $ev->key_sym; |
703 | my $sym = $ev->key_sym; |
714 | |
704 | |
715 | $ev->set_unicode (1); |
|
|
716 | my $uni = $ev->key_unicode; |
705 | my $uni = $ev->key_unicode; |
717 | |
706 | |
718 | my $text = $self->get_text; |
707 | my $text = $self->get_text; |
719 | |
708 | |
720 | if ($sym == SDLK_BACKSPACE) { |
709 | if ($sym == SDLK_BACKSPACE) { |
721 | substr $text, -1, 1, ''; |
710 | substr $text, -1, 1, ''; |
722 | |
|
|
723 | } elsif ($uni) { |
711 | } elsif ($uni) { |
724 | $text .= chr $uni; |
712 | $text .= chr $uni; |
|
|
713 | print "$uni <$text>\n";#d# |
725 | } |
714 | } |
|
|
715 | |
726 | $self->set_text ($text); |
716 | $self->set_text ($text); |
|
|
717 | } |
|
|
718 | |
|
|
719 | sub button_down { |
|
|
720 | my ($self, $ev) = @_; |
|
|
721 | |
|
|
722 | $self->focus_in; |
|
|
723 | } |
|
|
724 | |
|
|
725 | sub _draw { |
|
|
726 | my ($self) = @_; |
|
|
727 | |
|
|
728 | if ($FOCUS == $self) { |
|
|
729 | glColor 1, 1, 1; |
|
|
730 | } else { |
|
|
731 | glColor 0.7, 0.7, 0.7; |
|
|
732 | } |
|
|
733 | |
|
|
734 | glBegin GL_QUADS; |
|
|
735 | glVertex 0 , 0; |
|
|
736 | glVertex 0 , $self->{h} - 1; |
|
|
737 | glVertex $self->{w} - 1, $self->{h} - 1; |
|
|
738 | glVertex $self->{w} - 1, 0; |
|
|
739 | glEnd; |
|
|
740 | |
|
|
741 | $self->SUPER::_draw; |
727 | } |
742 | } |
728 | |
743 | |
729 | ############################################################################# |
744 | ############################################################################# |
730 | |
745 | |
731 | package Crossfire::Client::Widget::MapWidget; |
746 | package Crossfire::Client::Widget::MapWidget; |
… | |
… | |
746 | |
761 | |
747 | sub key_up { |
762 | sub key_up { |
748 | } |
763 | } |
749 | |
764 | |
750 | sub size_request { |
765 | sub size_request { |
751 | |
766 | ( |
752 | } |
767 | 1 + int $::WIDTH / 32, |
753 | |
768 | 1 + int $::HEIGHT / 32, |
754 | sub size_allocate { |
769 | ) |
755 | } |
770 | } |
756 | |
771 | |
757 | sub _draw { |
772 | sub _draw { |
758 | my ($self) = @_; |
773 | my ($self) = @_; |
759 | |
774 | |
… | |
… | |
782 | glEnable GL_TEXTURE_2D; |
797 | glEnable GL_TEXTURE_2D; |
783 | glEnable GL_BLEND; |
798 | glEnable GL_BLEND; |
784 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
799 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
785 | |
800 | |
786 | my $sw4 = ($sw + 3) & ~3; |
801 | my $sw4 = ($sw + 3) & ~3; |
787 | my $lighting = "\x00" x ($sw4 * $sh); |
802 | my $darkness = "\x00" x ($sw4 * $sh); |
788 | |
803 | |
789 | for my $x (0 .. $sw - 1) { |
804 | for my $x (0 .. $sw - 1) { |
|
|
805 | my $row = $map->[$x + $xofs]; |
790 | for my $y (0 .. $sh - 1) { |
806 | for my $y (0 .. $sh - 1) { |
791 | |
807 | |
792 | my $cell = $map->[$x + $xofs][$y + $yofs] |
808 | my $cell = $row->[$y + $yofs] |
793 | or next; |
809 | or next; |
794 | |
810 | |
795 | my $darkness = $cell->[0] * (1 / 255); |
811 | my $dark = $cell->[0]; |
796 | if ($darkness < 0) { |
812 | if ($dark < 0) { |
797 | $darkness = 0.15; |
813 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
|
|
814 | } else { |
|
|
815 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
798 | } |
816 | } |
799 | substr $lighting, $y * $sw4 + $x, 1, chr 255 - $darkness * 255; |
|
|
800 | |
817 | |
801 | for my $num (grep $_, @$cell[1,2,3]) { |
818 | for my $num (grep $_, @$cell[1,2,3]) { |
802 | my $tex = $::CONN->{face}[$num]{texture} || next; |
819 | my $tex = $::CONN->{face}[$num]{texture} || next; |
803 | |
820 | |
804 | glBindTexture GL_TEXTURE_2D, $tex->{name}; |
|
|
805 | |
|
|
806 | my $w = $tex->{width}; |
821 | my $w = $tex->{width}; |
807 | my $h = $tex->{height}; |
822 | my $h = $tex->{height}; |
808 | |
823 | |
809 | my $px = ($x + 1) * 32 - $w; |
824 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
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 | } |
825 | } |
819 | } |
826 | } |
820 | } |
827 | } |
821 | |
828 | |
822 | # if (1) { # higher quality darkness |
829 | # if (1) { # higher quality darkness |
… | |
… | |
827 | # |
834 | # |
828 | # $lighting = $pb->get_pixels; |
835 | # $lighting = $pb->get_pixels; |
829 | # $lighting =~ s/(.)../$1/gs; |
836 | # $lighting =~ s/(.)../$1/gs; |
830 | # } |
837 | # } |
831 | |
838 | |
|
|
839 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
840 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
841 | |
832 | $lighting = new Crossfire::Client::Texture |
842 | $darkness = new Crossfire::Client::Texture |
833 | width => $sw4, |
843 | width => $sw4, |
834 | height => $sh, |
844 | height => $sh, |
835 | data => $lighting, |
845 | data => $darkness, |
836 | internalformat => GL_ALPHA4, |
846 | internalformat => GL_ALPHA, |
837 | format => GL_ALPHA; |
847 | format => GL_ALPHA; |
838 | |
848 | |
839 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
849 | glColor 0.45, 0.45, 0.45, 1; |
840 | glColor 0, 0, 0, 0.75; |
850 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
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 | |
851 | glDisable GL_TEXTURE_2D; |
852 | glDisable GL_TEXTURE_2D; |
852 | glDisable GL_BLEND; |
853 | glDisable GL_BLEND; |
853 | } |
854 | } |
854 | |
855 | |
… | |
… | |
913 | |
914 | |
914 | sub moveto { |
915 | sub moveto { |
915 | my ($self, $x, $y) = @_; |
916 | my ($self, $x, $y) = @_; |
916 | |
917 | |
917 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
918 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
918 | $self->{speed} = 0.2; |
919 | $self->{speed} = 0.001; |
919 | $self->{time} = 1; |
920 | $self->{time} = 1; |
920 | |
921 | |
921 | ::animation_start $self; |
922 | ::animation_start $self; |
922 | } |
923 | } |
923 | |
924 | |
924 | sub animate { |
925 | sub animate { |
925 | my ($self, $interval) = @_; |
926 | my ($self, $interval) = @_; |
|
|
927 | |
|
|
928 | printf "%5.2f\n", 1 / $interval if $interval;#d# |
926 | |
929 | |
927 | $self->{time} -= $interval * $self->{speed}; |
930 | $self->{time} -= $interval * $self->{speed}; |
928 | if ($self->{time} <= 0) { |
931 | if ($self->{time} <= 0) { |
929 | $self->{time} = 0; |
932 | $self->{time} = 0; |
930 | ::animation_stop $self; |
933 | ::animation_stop $self; |
… | |
… | |
938 | |
941 | |
939 | sub _draw { |
942 | sub _draw { |
940 | my ($self) = @_; |
943 | my ($self) = @_; |
941 | |
944 | |
942 | glPushMatrix; |
945 | glPushMatrix; |
943 | glRotate $self->{time} * 10000, 0, 1, 0; |
946 | glRotate $self->{time} * 1000, 0, 1, 0; |
944 | $self->{children}[0]->draw; |
947 | $self->{children}[0]->draw; |
945 | glPopMatrix; |
948 | glPopMatrix; |
946 | } |
949 | } |
947 | |
950 | |
948 | 1; |
951 | ############################################################################# |
949 | |
952 | |
|
|
953 | package Crossfire::Client::Widget::Toplevel; |
|
|
954 | |
|
|
955 | our @ISA = Crossfire::Client::Widget::Container::; |
|
|
956 | |
|
|
957 | sub size_request { |
|
|
958 | ($::WIDTH, $::HEIGHT) |
|
|
959 | } |
|
|
960 | |
|
|
961 | sub size_allocate { |
|
|
962 | my ($self, $w, $h) = @_; |
|
|
963 | |
|
|
964 | $self->SUPER::size_allocate ($w, $h); |
|
|
965 | |
|
|
966 | $_->size_allocate ($_->size_request) |
|
|
967 | for @{$self->{children}}; |
|
|
968 | } |
|
|
969 | |
|
|
970 | sub update { |
|
|
971 | my ($self) = @_; |
|
|
972 | |
|
|
973 | $self->size_allocate ($self->size_request); |
|
|
974 | ::refresh (); |
|
|
975 | } |
|
|
976 | |
|
|
977 | sub add { |
|
|
978 | my ($self, $widget) = @_; |
|
|
979 | |
|
|
980 | $self->SUPER::add ($widget); |
|
|
981 | |
|
|
982 | $widget->size_allocate ($widget->size_request); |
|
|
983 | } |
|
|
984 | |
|
|
985 | sub draw { |
|
|
986 | my ($self) = @_; |
|
|
987 | |
|
|
988 | $self->_draw; |
|
|
989 | } |
|
|
990 | |
|
|
991 | ############################################################################# |
|
|
992 | |
|
|
993 | package Crossfire::Client::Widget; |
|
|
994 | |
|
|
995 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
|
|
996 | |
|
|
997 | 1 |
|
|
998 | |