1 | package Crossfire::Client::Widget; |
1 | package CFClient::Widget; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
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 | use Crossfire::Client; |
10 | use CFClient; |
11 | |
11 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
13 | |
14 | our $TOPLEVEL; |
14 | our $TOPLEVEL; |
15 | our $BUTTON_STATE; |
15 | our $BUTTON_STATE; |
… | |
… | |
73 | } |
73 | } |
74 | |
74 | |
75 | sub new { |
75 | sub new { |
76 | my $class = shift; |
76 | my $class = shift; |
77 | |
77 | |
78 | bless { @_ }, $class |
78 | bless { |
|
|
79 | x => 0, |
|
|
80 | y => 0, |
|
|
81 | z => 0, |
|
|
82 | @_ |
|
|
83 | }, $class |
79 | } |
84 | } |
80 | |
85 | |
81 | sub move { |
86 | sub move { |
82 | my ($self, $x, $y, $z) = @_; |
87 | my ($self, $x, $y, $z) = @_; |
83 | $self->{x} = $x; |
88 | $self->{x} = $x; |
… | |
… | |
109 | } |
114 | } |
110 | |
115 | |
111 | sub focus_in { |
116 | sub focus_in { |
112 | my ($self) = @_; |
117 | my ($self) = @_; |
113 | |
118 | |
|
|
119 | return if $FOCUS == $self; |
|
|
120 | |
114 | my $focus = $FOCUS; $FOCUS = $self; |
121 | my $focus = $FOCUS; $FOCUS = $self; |
115 | $focus->update if $focus; |
122 | $focus->update if $focus; |
116 | $FOCUS->update; |
123 | $FOCUS->update; |
117 | } |
124 | } |
118 | |
125 | |
… | |
… | |
125 | $focus->update if $focus; #? |
132 | $focus->update if $focus; #? |
126 | } |
133 | } |
127 | |
134 | |
128 | sub mouse_motion { } |
135 | sub mouse_motion { } |
129 | sub button_up { } |
136 | sub button_up { } |
130 | sub button_down { } |
|
|
131 | sub key_down { } |
137 | sub key_down { } |
132 | sub key_up { } |
138 | sub key_up { } |
|
|
139 | |
|
|
140 | sub button_down { |
|
|
141 | my ($self, $ev, $x, $y) = @_; |
|
|
142 | |
|
|
143 | $self->focus_in; |
|
|
144 | } |
133 | |
145 | |
134 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
146 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
135 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
147 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
136 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
148 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
137 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
149 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
138 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
150 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
139 | |
151 | |
140 | sub draw { |
152 | sub draw { |
141 | my ($self) = @_; |
153 | my ($self) = @_; |
142 | |
154 | |
|
|
155 | return unless $self->{h} && $self->{w}; |
|
|
156 | |
143 | glPushMatrix; |
157 | glPushMatrix; |
144 | glTranslate $self->{x}, $self->{y}, 0; |
158 | glTranslate $self->{x}, $self->{y}, 0; |
145 | $self->_draw; |
159 | $self->_draw; |
|
|
160 | glPopMatrix; |
|
|
161 | |
146 | if ($self == $HOVER) { |
162 | if ($self == $HOVER) { |
|
|
163 | my ($x, $y) = @$self->{qw(x y)}; |
|
|
164 | |
147 | glColor 1, 1, 1, 0.4; |
165 | glColor 1, 1, 1, 0.1; |
148 | glEnable GL_BLEND; |
166 | glEnable GL_BLEND; |
149 | glBegin GL_QUADS; |
167 | glBegin GL_QUADS; |
150 | glVertex 0 , 0; |
168 | glVertex $x , $y; |
151 | glVertex $self->{w}, 0; |
169 | glVertex $x + $self->{w}, $y; |
152 | glVertex $self->{w}, $self->{h}; |
170 | glVertex $x + $self->{w}, $y + $self->{h}; |
153 | glVertex 0 , $self->{h}; |
171 | glVertex $x , $y + $self->{h}; |
154 | glEnd; |
172 | glEnd; |
155 | glDisable GL_BLEND; |
173 | glDisable GL_BLEND; |
156 | } |
174 | } |
157 | glPopMatrix; |
|
|
158 | } |
175 | } |
159 | |
176 | |
160 | sub _draw { |
177 | sub _draw { |
161 | my ($self) = @_; |
178 | my ($self) = @_; |
162 | |
179 | |
… | |
… | |
202 | |
219 | |
203 | $self->{parent}->update |
220 | $self->{parent}->update |
204 | if $self->{parent}; |
221 | if $self->{parent}; |
205 | } |
222 | } |
206 | |
223 | |
|
|
224 | sub connect { |
|
|
225 | my ($self, $signal, $cb) = @_; |
|
|
226 | |
|
|
227 | push @{ $self->{cb}{$signal} }, $cb; |
|
|
228 | } |
|
|
229 | |
|
|
230 | sub emit { |
|
|
231 | my ($self, $signal, @args) = @_; |
|
|
232 | |
|
|
233 | $_->($self, @args) |
|
|
234 | for @{$self->{cb}{$signal} || []}; |
|
|
235 | } |
|
|
236 | |
207 | sub DESTROY { |
237 | sub DESTROY { |
208 | my ($self) = @_; |
238 | my ($self) = @_; |
209 | |
239 | |
210 | #$self->deactivate; |
240 | #$self->deactivate; |
211 | } |
241 | } |
212 | |
242 | |
213 | ############################################################################# |
243 | ############################################################################# |
214 | |
244 | |
215 | package Crossfire::Client::Widget::Container; |
245 | package CFClient::Widget::DrawBG; |
216 | |
246 | |
217 | our @ISA = Crossfire::Client::Widget::; |
247 | our @ISA = CFClient::Widget::; |
|
|
248 | |
|
|
249 | use strict; |
|
|
250 | use SDL::OpenGL; |
218 | |
251 | |
219 | sub new { |
252 | sub new { |
|
|
253 | my $class = shift; |
|
|
254 | |
|
|
255 | # range [value, low, high, page] |
|
|
256 | |
|
|
257 | $class->SUPER::new ( |
|
|
258 | bg => [0, 0, 0, 0.4], |
|
|
259 | active_bg => [1, 1, 1], |
|
|
260 | @_ |
|
|
261 | ) |
|
|
262 | } |
|
|
263 | |
|
|
264 | sub _draw { |
|
|
265 | my ($self) = @_; |
|
|
266 | |
|
|
267 | my ($w, $h) = @$self{qw(w h)}; |
|
|
268 | |
|
|
269 | glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; |
|
|
270 | glBegin GL_QUADS; |
|
|
271 | glVertex 0 , 0; |
|
|
272 | glVertex 0 , $h; |
|
|
273 | glVertex $w, $h; |
|
|
274 | glVertex $w, 0; |
|
|
275 | glEnd; |
|
|
276 | } |
|
|
277 | |
|
|
278 | ############################################################################# |
|
|
279 | |
|
|
280 | package CFClient::Widget::Empty; |
|
|
281 | |
|
|
282 | our @ISA = CFClient::Widget::; |
|
|
283 | |
|
|
284 | sub size_request { |
|
|
285 | (0, 0) |
|
|
286 | } |
|
|
287 | |
|
|
288 | sub draw { } |
|
|
289 | |
|
|
290 | ############################################################################# |
|
|
291 | |
|
|
292 | package CFClient::Widget::Container; |
|
|
293 | |
|
|
294 | our @ISA = CFClient::Widget::; |
|
|
295 | |
|
|
296 | sub new { |
220 | my ($class, @widgets) = @_; |
297 | my ($class, %arg) = @_; |
221 | |
298 | |
|
|
299 | my $children = delete $arg{children} || []; |
|
|
300 | |
222 | my $self = $class->SUPER::new (children => []); |
301 | my $self = $class->SUPER::new (children => [], %arg); |
223 | $self->add ($_) for @widgets; |
302 | $self->add ($_) for @$children; |
224 | |
303 | |
225 | $self |
304 | $self |
226 | } |
305 | } |
227 | |
306 | |
228 | sub add { |
307 | sub add { |
229 | my ($self, $chld, $expand) = @_; |
308 | my ($self, $chld, $expand) = @_; |
230 | |
309 | |
231 | $chld->{expand} = $expand; |
310 | $chld->{expand} = $expand; |
232 | $chld->set_parent ($self); |
311 | $chld->set_parent ($self); |
233 | |
312 | |
234 | @{$self->{children}} = |
313 | $self->{children} = [ |
235 | sort { $a->{z} <=> $b->{z} } |
314 | sort { $a->{z} <=> $b->{z} } |
236 | @{$self->{children}}, $chld; |
315 | @{$self->{children}}, $chld |
|
|
316 | ]; |
237 | |
317 | |
238 | $self->size_allocate ($self->{w}, $self->{h}) |
318 | $self->size_allocate ($self->{w}, $self->{h}) |
239 | if $self->{w}; #TODO: check for "realised state" |
319 | if $self->{w}; #TODO: check for "realised state" |
240 | } |
320 | } |
241 | |
321 | |
… | |
… | |
269 | $_->draw for @{$self->{children}}; |
349 | $_->draw for @{$self->{children}}; |
270 | } |
350 | } |
271 | |
351 | |
272 | ############################################################################# |
352 | ############################################################################# |
273 | |
353 | |
274 | package Crossfire::Client::Widget::Bin; |
354 | package CFClient::Widget::Bin; |
275 | |
355 | |
276 | our @ISA = Crossfire::Client::Widget::Container::; |
356 | our @ISA = CFClient::Widget::Container::; |
|
|
357 | |
|
|
358 | sub new { |
|
|
359 | my ($class, %arg) = @_; |
|
|
360 | |
|
|
361 | my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; |
|
|
362 | |
|
|
363 | $class->SUPER::new (children => [$child], %arg) |
|
|
364 | } |
|
|
365 | |
|
|
366 | sub add { |
|
|
367 | my ($self, $widget) = @_; |
|
|
368 | |
|
|
369 | $self->{children} = []; |
|
|
370 | |
|
|
371 | $self->SUPER::add ($widget); |
|
|
372 | } |
|
|
373 | |
|
|
374 | sub remove { |
|
|
375 | my ($self, $widget) = @_; |
|
|
376 | |
|
|
377 | $self->SUPER::remove ($widget); |
|
|
378 | |
|
|
379 | $self->{children} = [new CFClient::Widget::Empty] |
|
|
380 | unless @{$self->{children}}; |
|
|
381 | } |
277 | |
382 | |
278 | sub child { $_[0]->{children}[0] } |
383 | sub child { $_[0]->{children}[0] } |
279 | |
384 | |
280 | sub size_request { |
385 | sub size_request { |
281 | $_[0]{children}[0]->size_request if $_[0]{children}[0]; |
386 | $_[0]{children}[0]->size_request |
282 | } |
387 | } |
283 | |
388 | |
284 | sub size_allocate { |
389 | sub size_allocate { |
285 | my ($self, $w, $h) = @_; |
390 | my ($self, $w, $h) = @_; |
286 | |
391 | |
|
|
392 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
393 | |
287 | $self->SUPER::size_allocate ($w, $h); |
394 | $self->SUPER::size_allocate ($w, $h); |
288 | $self->{children}[0]->size_allocate ($w, $h) |
395 | $self->{children}[0]->size_allocate ($w, $h); |
289 | if $self->{children}[0] |
|
|
290 | } |
396 | } |
291 | |
397 | |
292 | ############################################################################# |
398 | ############################################################################# |
293 | |
399 | |
294 | package Crossfire::Client::Widget::Window; |
400 | package CFClient::Widget::Window; |
295 | |
401 | |
296 | our @ISA = Crossfire::Client::Widget::Bin::; |
402 | our @ISA = CFClient::Widget::Bin::; |
297 | |
403 | |
298 | use SDL::OpenGL; |
404 | use SDL::OpenGL; |
299 | |
405 | |
300 | sub new { |
406 | sub new { |
301 | my ($class, $x, $y, $z, $w, $h) = @_; |
407 | my ($class, %arg) = @_; |
302 | |
408 | |
303 | my $self = $class->SUPER::new; |
409 | my $self = $class->SUPER::new (%arg); |
304 | |
|
|
305 | @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h); |
|
|
306 | } |
410 | } |
307 | |
411 | |
308 | sub update { |
412 | sub update { |
309 | my ($self) = @_; |
413 | my ($self) = @_; |
310 | |
414 | |
|
|
415 | # we want to do this delayed... |
311 | $self->render_chld; |
416 | $self->render_chld; |
312 | $self->SUPER::update; |
417 | $self->SUPER::update; |
313 | } |
418 | } |
314 | |
419 | |
315 | sub render_chld { |
420 | sub render_chld { |
316 | my ($self) = @_; |
421 | my ($self) = @_; |
317 | |
422 | |
318 | $self->{texture} = |
423 | $self->{texture} = |
319 | Crossfire::Client::Texture->new_from_opengl ( |
424 | CFClient::Texture->new_from_opengl ( |
320 | $self->{w}, $self->{h}, sub { $self->child->draw } |
425 | $self->{w}, $self->{h}, sub { $self->child->draw } |
321 | ); |
426 | ); |
322 | } |
427 | } |
323 | |
428 | |
324 | sub size_allocate { |
429 | sub size_allocate { |
325 | my ($self, $w, $h) = @_; |
430 | my ($self, $w, $h) = @_; |
|
|
431 | |
|
|
432 | return unless $self->{w} != $w || $self->{h} != $h; |
326 | |
433 | |
327 | $self->{w} = $w; |
434 | $self->{w} = $w; |
328 | $self->{h} = $h; |
435 | $self->{h} = $h; |
329 | |
436 | |
330 | $self->child->size_allocate ($w, $h); |
437 | $self->child->size_allocate ($w, $h); |
… | |
… | |
350 | glDisable GL_TEXTURE_2D; |
457 | glDisable GL_TEXTURE_2D; |
351 | } |
458 | } |
352 | |
459 | |
353 | ############################################################################# |
460 | ############################################################################# |
354 | |
461 | |
355 | package Crossfire::Client::Widget::Frame; |
462 | package CFClient::Widget::Frame; |
356 | |
463 | |
357 | our @ISA = Crossfire::Client::Widget::Bin::; |
464 | our @ISA = CFClient::Widget::Bin::; |
358 | |
465 | |
359 | use SDL::OpenGL; |
466 | use SDL::OpenGL; |
360 | |
467 | |
361 | sub size_request { |
468 | sub size_request { |
362 | my ($self) = @_; |
469 | my ($self) = @_; |
… | |
… | |
369 | } |
476 | } |
370 | |
477 | |
371 | sub size_allocate { |
478 | sub size_allocate { |
372 | my ($self, $w, $h) = @_; |
479 | my ($self, $w, $h) = @_; |
373 | |
480 | |
|
|
481 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
482 | |
374 | $self->{w} = $w; |
483 | $self->{w} = $w; |
375 | $self->{h} = $h; |
484 | $self->{h} = $h; |
376 | |
485 | |
377 | $self->child->size_allocate ($w - 4, $h - 4); |
486 | $self->child->size_allocate ($w - 4, $h - 4); |
378 | $self->child->move (2, 2); |
487 | $self->child->move (2, 2); |
… | |
… | |
396 | $chld->draw; |
505 | $chld->draw; |
397 | } |
506 | } |
398 | |
507 | |
399 | ############################################################################# |
508 | ############################################################################# |
400 | |
509 | |
401 | package Crossfire::Client::Widget::FancyFrame; |
510 | package CFClient::Widget::FancyFrame; |
402 | |
511 | |
403 | our @ISA = Crossfire::Client::Widget::Bin::; |
512 | our @ISA = CFClient::Widget::Bin::; |
404 | |
513 | |
405 | use SDL::OpenGL; |
514 | use SDL::OpenGL; |
406 | |
515 | |
407 | my @tex = |
516 | my @tex = |
408 | map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } |
517 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
409 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
518 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
410 | |
519 | |
411 | sub size_request { |
520 | sub size_request { |
412 | my ($self) = @_; |
521 | my ($self) = @_; |
413 | |
522 | |
414 | my ($w, $h) = $self->SUPER::size_request; |
523 | my ($w, $h) = $self->SUPER::size_request; |
415 | |
524 | |
416 | $h += $tex[1]->{height}; |
525 | $h += $tex[1]->{h}; |
417 | $h += $tex[4]->{height}; |
526 | $h += $tex[4]->{h}; |
418 | $w += $tex[2]->{width}; |
527 | $w += $tex[2]->{w}; |
419 | $w += $tex[3]->{width}; |
528 | $w += $tex[3]->{w}; |
420 | |
529 | |
421 | ($w, $h) |
530 | ($w, $h) |
422 | } |
531 | } |
423 | |
532 | |
424 | sub size_allocate { |
533 | sub size_allocate { |
425 | my ($self, $w, $h) = @_; |
534 | my ($self, $w, $h) = @_; |
426 | |
535 | |
|
|
536 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
537 | |
427 | $self->SUPER::size_allocate ($w, $h); |
538 | $self->SUPER::size_allocate ($w, $h); |
428 | |
539 | |
429 | $h -= $tex[1]->{height}; |
540 | $h -= $tex[1]->{h}; |
430 | $h -= $tex[4]->{height}; |
541 | $h -= $tex[4]->{h}; |
431 | $w -= $tex[2]->{width}; |
542 | $w -= $tex[2]->{w}; |
432 | $w -= $tex[3]->{width}; |
543 | $w -= $tex[3]->{w}; |
433 | |
544 | |
434 | $h = $h < 0 ? 0 : $h; |
545 | $h = $h < 0 ? 0 : $h; |
435 | $w = $w < 0 ? 0 : $w; |
546 | $w = $w < 0 ? 0 : $w; |
436 | |
547 | |
|
|
548 | my $child = $self->child; |
|
|
549 | |
437 | $self->child->size_allocate ($w, $h); |
550 | $child->size_allocate ($w, $h); |
438 | $self->child->move ($tex[3]->{width}, $tex[1]->{height}); |
551 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
439 | } |
552 | } |
440 | |
553 | |
441 | sub _draw { |
554 | sub _draw { |
442 | my ($self) = @_; |
555 | my ($self) = @_; |
443 | |
556 | |
… | |
… | |
448 | glEnable GL_TEXTURE_2D; |
561 | glEnable GL_TEXTURE_2D; |
449 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
562 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
450 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
563 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
451 | |
564 | |
452 | my $top = $tex[1]; |
565 | my $top = $tex[1]; |
453 | $top->draw_quad (0, 0, $w, $top->{height}); |
566 | $top->draw_quad (0, 0, $w, $top->{h}); |
454 | |
567 | |
455 | my $left = $tex[3]; |
568 | my $left = $tex[3]; |
456 | $left->draw_quad (0, $top->{height}, $left->{width}, $ch); |
569 | $left->draw_quad (0, $top->{h}, $left->{w}, $ch); |
457 | |
570 | |
458 | my $right = $tex[2]; |
571 | my $right = $tex[2]; |
459 | $right->draw_quad ($w - $right->{width}, $top->{height}, $right->{width}, $ch); |
572 | $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch); |
460 | |
573 | |
461 | my $bottom = $tex[4]; |
574 | my $bottom = $tex[4]; |
462 | $bottom->draw_quad (0, $h - $bottom->{height}, $w, $bottom->{height}); |
575 | $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h}); |
463 | |
576 | |
464 | my $bg = $tex[0]; |
577 | my $bg = $tex[0]; |
465 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
578 | glBindTexture GL_TEXTURE_2D, $bg->{name}; |
466 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
579 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
467 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
580 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; |
468 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
581 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; |
469 | |
582 | |
470 | my $rep_x = $cw / $bg->{width}; |
583 | my $rep_x = $cw / $bg->{w}; |
471 | my $rep_y = $ch / $bg->{height}; |
584 | my $rep_y = $ch / $bg->{h}; |
472 | |
585 | |
473 | $bg->draw_quad ($left->{width}, $top->{height}, $cw, $ch); |
586 | $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); |
474 | |
587 | |
475 | glDisable GL_BLEND; |
588 | glDisable GL_BLEND; |
476 | glDisable GL_TEXTURE_2D; |
589 | glDisable GL_TEXTURE_2D; |
477 | |
590 | |
478 | $self->child->draw; |
591 | $self->child->draw; |
479 | |
592 | |
480 | } |
593 | } |
481 | |
594 | |
482 | ############################################################################# |
595 | ############################################################################# |
483 | |
596 | |
484 | package Crossfire::Client::Widget::Table; |
597 | package CFClient::Widget::Table; |
485 | |
598 | |
486 | our @ISA = Crossfire::Client::Widget::Bin::; |
599 | our @ISA = CFClient::Widget::; |
487 | |
600 | |
488 | use SDL::OpenGL; |
601 | use SDL::OpenGL; |
489 | |
602 | |
490 | sub add { |
603 | sub add { |
491 | my ($self, $x, $y, $chld) = @_; |
604 | my ($self, $x, $y, $chld) = @_; |
… | |
… | |
566 | } |
679 | } |
567 | } |
680 | } |
568 | |
681 | |
569 | ############################################################################# |
682 | ############################################################################# |
570 | |
683 | |
571 | package Crossfire::Client::Widget::VBox; |
684 | package CFClient::Widget::VBox; |
572 | |
685 | |
573 | our @ISA = Crossfire::Client::Widget::Container::; |
686 | our @ISA = CFClient::Widget::Container::; |
574 | |
687 | |
575 | use SDL::OpenGL; |
688 | use SDL::OpenGL; |
576 | |
689 | |
577 | sub size_request { |
690 | sub size_request { |
578 | my ($self) = @_; |
691 | my ($self) = @_; |
… | |
… | |
586 | } |
699 | } |
587 | |
700 | |
588 | sub size_allocate { |
701 | sub size_allocate { |
589 | my ($self, $w, $h) = @_; |
702 | my ($self, $w, $h) = @_; |
590 | |
703 | |
|
|
704 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
705 | |
591 | $self->w ($w); |
706 | $self->{w} = $w; |
592 | $self->h ($h); |
707 | $self->{h} = $h; |
593 | |
708 | |
594 | my $exp; |
709 | return unless $self->{h}; |
595 | my @oth; |
710 | |
596 | # find expand widget |
711 | my $children = $self->{children}; |
597 | for (@{$self->{children}}) { |
712 | |
598 | if ($_->{expand}) { |
713 | my @h = map +($_->size_request)[1], @$children; |
599 | $exp = $_; |
714 | |
600 | last; |
715 | my $req_h = List::Util::sum @h; |
|
|
716 | |
|
|
717 | if ($req_h > $h) { |
|
|
718 | # ah well, not enough space |
|
|
719 | $_ = $h[$_] * $h / $req_h for @h; |
|
|
720 | } else { |
|
|
721 | my @exp = grep $_->{expand}, @$children; |
|
|
722 | @exp = @$children unless @exp; |
|
|
723 | |
|
|
724 | my %exp = map +($_ => 1), @exp; |
|
|
725 | |
|
|
726 | for (0 .. $#$children) { |
|
|
727 | my $child = $children->[$_]; |
|
|
728 | |
|
|
729 | my $alloc_h = $h[$_]; |
|
|
730 | $alloc_h += ($h - $req_h) / @exp if $exp{$child}; |
|
|
731 | $h[$_] = $alloc_h; |
601 | } |
732 | } |
602 | push @oth, $_; |
|
|
603 | } |
|
|
604 | |
|
|
605 | my ($ow, $oh); |
|
|
606 | |
|
|
607 | # get sizes of other widgets |
|
|
608 | for (@oth) { |
|
|
609 | my ($w, $h) = $_->size_request; |
|
|
610 | $oh += $h; |
|
|
611 | if ($ow < $w) { $ow = $w } |
|
|
612 | } |
733 | } |
613 | |
734 | |
614 | my $y = 0; |
735 | my $y = 0; |
615 | for (@{$self->{children}}) { |
736 | for (0 .. $#$children) { |
|
|
737 | my $child = $children->[$_]; |
|
|
738 | my $h = $h[$_]; |
616 | $_->move (0, $y); |
739 | $child->move (0, $y); |
617 | |
|
|
618 | if ($_ == $exp) { |
|
|
619 | $_->size_allocate ($w, $h - $oh); |
|
|
620 | $y += $h - $oh; |
|
|
621 | } else { |
|
|
622 | my ($cw, $h) = $_->size_request; |
|
|
623 | $_->size_allocate ($w, $h); |
740 | $child->size_allocate ($w, $h); |
|
|
741 | |
624 | $y += $h; |
742 | $y += $h; |
625 | } |
|
|
626 | } |
743 | } |
627 | } |
744 | } |
628 | |
745 | |
629 | ############################################################################# |
746 | ############################################################################# |
630 | |
747 | |
631 | package Crossfire::Client::Widget::Label; |
748 | package CFClient::Widget::Label; |
632 | |
749 | |
633 | our @ISA = Crossfire::Client::Widget::; |
750 | our @ISA = CFClient::Widget::; |
634 | |
751 | |
635 | use SDL::OpenGL; |
752 | use SDL::OpenGL; |
636 | |
753 | |
637 | sub new { |
754 | sub new { |
638 | my ($class, $x, $y, $z, $height, $text) = @_; |
755 | my ($class, %arg) = @_; |
639 | |
756 | |
|
|
757 | my $self = $class->SUPER::new ( |
|
|
758 | fg => [1, 1, 1], |
640 | $height ||= $::FONTSIZE; |
759 | height => $::FONTSIZE, |
|
|
760 | text => "", |
|
|
761 | align => -1, |
|
|
762 | layout => new CFClient::Layout, |
|
|
763 | %arg |
|
|
764 | ); |
641 | |
765 | |
642 | # TODO: color, and make height, xyz etc. optional |
|
|
643 | my $self = $class->SUPER::new (x => $x, y => $y, z => $z, height => $height); |
|
|
644 | |
|
|
645 | $self->set_text ($text); |
766 | $self->set_text ($self->{text}); |
646 | |
767 | |
647 | $self |
768 | $self |
|
|
769 | } |
|
|
770 | |
|
|
771 | sub escape_text { |
|
|
772 | local $_ = $_[1]; |
|
|
773 | |
|
|
774 | s/&/&/g; |
|
|
775 | s/>/>/g; |
|
|
776 | s/</</g; |
|
|
777 | |
|
|
778 | $_[1] |
648 | } |
779 | } |
649 | |
780 | |
650 | sub set_text { |
781 | sub set_text { |
651 | my ($self, $text) = @_; |
782 | my ($self, $text) = @_; |
652 | |
783 | |
653 | $self->{text} = $text; |
784 | $self->{text} = $text; |
654 | $self->{texture} = new_from_text Crossfire::Client::Texture $text, $self->{height}; |
785 | $self->{layout}->set_markup ($text); |
655 | |
786 | |
|
|
787 | delete $self->{texture}; |
656 | $self->update; |
788 | $self->update; |
657 | } |
789 | } |
658 | |
790 | |
659 | sub get_text { |
791 | sub get_text { |
660 | my ($self, $text) = @_; |
792 | my ($self, $text) = @_; |
… | |
… | |
663 | } |
795 | } |
664 | |
796 | |
665 | sub size_request { |
797 | sub size_request { |
666 | my ($self) = @_; |
798 | my ($self) = @_; |
667 | |
799 | |
|
|
800 | $self->{layout}->set_width; |
|
|
801 | $self->{layout}->set_height ($self->{height}); |
|
|
802 | $self->{layout}->size |
668 | if ($self->{texture}{width} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
803 | # if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack |
669 | ( |
804 | # ( |
670 | $self->{texture}{width}, |
805 | # $self->{texture}{w}, |
671 | $self->{texture}{height}, |
806 | # $self->{texture}{h}, |
672 | ) |
807 | # ) |
673 | } else { |
808 | # } else { |
674 | my ($w, $h, $data) = Crossfire::Client::font_render "Yy", $self->{height}; |
809 | # my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h}; |
675 | |
810 | # |
676 | ($w, $h) |
811 | # ($w, $h) |
677 | } |
812 | # } |
|
|
813 | } |
678 | |
814 | |
|
|
815 | sub size_allocate { |
|
|
816 | my ($self, $w, $h) = @_; |
|
|
817 | |
|
|
818 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
819 | |
|
|
820 | $self->SUPER::size_allocate ($w, $h); |
|
|
821 | delete $self->{texture}; |
|
|
822 | } |
|
|
823 | |
|
|
824 | sub update { |
|
|
825 | my ($self) = @_; |
|
|
826 | |
|
|
827 | delete $self->{texture}; |
|
|
828 | $self->SUPER::update; |
679 | } |
829 | } |
680 | |
830 | |
681 | sub _draw { |
831 | sub _draw { |
682 | my ($self) = @_; |
832 | my ($self) = @_; |
683 | |
833 | |
684 | my $tex = $self->{texture}; |
834 | my $tex = $self->{texture} ||= do { |
|
|
835 | $self->{layout}->set_width ($self->{w}); |
|
|
836 | new_from_layout CFClient::Texture $self->{layout}; |
|
|
837 | }; |
685 | |
838 | |
686 | glEnable GL_BLEND; |
839 | glEnable GL_BLEND; |
687 | glEnable GL_TEXTURE_2D; |
840 | glEnable GL_TEXTURE_2D; |
688 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
841 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
689 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
842 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
690 | |
843 | |
691 | glColor 1, 0, 0, 1; # TODO color |
844 | glColor @{$self->{fg}}; |
692 | |
845 | |
|
|
846 | my $x = |
|
|
847 | $self->{align} < 0 ? 0 |
|
|
848 | : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} |
|
|
849 | : ($self->{w} + $self->{texture}{w}) * 0.5; |
|
|
850 | |
693 | $tex->draw_quad (0, 0); |
851 | $tex->draw_quad ($x, 0); |
694 | |
852 | |
695 | glDisable GL_BLEND; |
853 | glDisable GL_BLEND; |
696 | glDisable GL_TEXTURE_2D; |
854 | glDisable GL_TEXTURE_2D; |
697 | } |
855 | } |
698 | |
856 | |
699 | ############################################################################# |
857 | ############################################################################# |
700 | |
858 | |
701 | package Crossfire::Client::Widget::Entry; |
859 | package CFClient::Widget::Entry; |
702 | |
860 | |
703 | our @ISA = Crossfire::Client::Widget::Label::; |
861 | our @ISA = CFClient::Widget::Label::; |
704 | |
862 | |
705 | use SDL; |
863 | use SDL; |
706 | use SDL::OpenGL; |
864 | use SDL::OpenGL; |
707 | |
865 | |
|
|
866 | sub new { |
|
|
867 | my $class = shift; |
|
|
868 | |
|
|
869 | $class->SUPER::new ( |
|
|
870 | fg => [1, 1, 1], |
|
|
871 | bg => [0, 0, 0, 0.4], |
|
|
872 | active_bg => [1, 1, 1], |
|
|
873 | active_fg => [0, 0, 0], |
|
|
874 | @_ |
|
|
875 | ) |
|
|
876 | } |
|
|
877 | |
|
|
878 | sub _set_text { |
|
|
879 | my ($self, $text) = @_; |
|
|
880 | |
|
|
881 | $self->{last_activity} = $::NOW; |
|
|
882 | |
|
|
883 | $self->{text} = $text; |
|
|
884 | $self->{layout}->set_width ($self->{w}); |
|
|
885 | |
|
|
886 | $text =~ s/./*/g if $self->{hidden}; |
|
|
887 | |
|
|
888 | |
|
|
889 | $self->{layout}->set_markup ($self->escape_text ($text)); |
|
|
890 | |
|
|
891 | $text = substr $text, 0, $self->{cursor}; |
|
|
892 | utf8::encode $text; |
|
|
893 | |
|
|
894 | @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); |
|
|
895 | } |
|
|
896 | |
|
|
897 | sub size_request { |
|
|
898 | my ($self) = @_; |
|
|
899 | |
|
|
900 | my ($w, $h) = $self->SUPER::size_request; |
|
|
901 | |
|
|
902 | ($w + 1, $h) # add 1 for cursor |
|
|
903 | } |
|
|
904 | |
|
|
905 | sub size_allocate { |
|
|
906 | my ($self, $w, $h) = @_; |
|
|
907 | |
|
|
908 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
909 | |
|
|
910 | $self->SUPER::size_allocate ($w, $h); |
|
|
911 | |
|
|
912 | $self->_set_text ($self->{text}); |
|
|
913 | } |
|
|
914 | |
|
|
915 | sub set_text { |
|
|
916 | my ($self, $text) = @_; |
|
|
917 | |
|
|
918 | $self->{cursor} = length $text; |
|
|
919 | $self->_set_text ($text); |
|
|
920 | $self->update; |
|
|
921 | } |
|
|
922 | |
708 | sub key_down { |
923 | sub key_down { |
709 | my ($self, $ev) = @_; |
924 | my ($self, $ev) = @_; |
710 | |
925 | |
711 | my $mod = $ev->key_mod; |
926 | my $mod = $ev->key_mod; |
712 | my $sym = $ev->key_sym; |
927 | my $sym = $ev->key_sym; |
… | |
… | |
714 | my $uni = $ev->key_unicode; |
929 | my $uni = $ev->key_unicode; |
715 | |
930 | |
716 | my $text = $self->get_text; |
931 | my $text = $self->get_text; |
717 | |
932 | |
718 | if ($sym == SDLK_BACKSPACE) { |
933 | if ($sym == SDLK_BACKSPACE) { |
719 | substr $text, -1, 1, ''; |
934 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
|
|
935 | } elsif ($sym == SDLK_DELETE) { |
|
|
936 | substr $text, $self->{cursor}, 1, ""; |
|
|
937 | } elsif ($sym == SDLK_LEFT) { |
|
|
938 | --$self->{cursor} if $self->{cursor}; |
|
|
939 | } elsif ($sym == SDLK_RIGHT) { |
|
|
940 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
720 | } elsif ($uni) { |
941 | } elsif ($uni) { |
721 | $text .= chr $uni; |
942 | substr $text, $self->{cursor}++, 0, chr $uni; |
722 | } |
943 | } |
723 | |
944 | |
724 | $self->set_text ($text); |
945 | $self->_set_text ($text); |
|
|
946 | $self->update; |
|
|
947 | } |
|
|
948 | |
|
|
949 | sub focus_in { |
|
|
950 | my ($self) = @_; |
|
|
951 | |
|
|
952 | $self->{last_activity} = $::NOW; |
|
|
953 | |
|
|
954 | $self->SUPER::focus_in; |
725 | } |
955 | } |
726 | |
956 | |
727 | sub button_down { |
957 | sub button_down { |
728 | my ($self, $ev) = @_; |
958 | my ($self, $ev, $x, $y) = @_; |
729 | |
959 | |
730 | $self->focus_in; |
960 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
961 | |
|
|
962 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
|
|
963 | |
|
|
964 | # byte-index to char-index |
|
|
965 | my $text = $self->{layout}; |
|
|
966 | utf8::encode $text; |
|
|
967 | $self->{cursor} = length substr $text, 0, $idx; |
|
|
968 | |
|
|
969 | $self->_set_text ($self->{text}); |
|
|
970 | $self->update; |
731 | } |
971 | } |
732 | |
972 | |
733 | sub mouse_motion { |
973 | sub mouse_motion { |
734 | my ($self, $ev, $x, $y) = @_; |
974 | my ($self, $ev, $x, $y) = @_; |
735 | printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
975 | # printf "M %d,%d %d,%d\n", $ev->motion_x, $ev->motion_y, $x, $y;#d# |
736 | } |
976 | } |
737 | |
977 | |
738 | sub _draw { |
978 | sub _draw { |
739 | my ($self) = @_; |
979 | my ($self) = @_; |
740 | |
980 | |
|
|
981 | local $self->{fg} = $self->{fg}; |
|
|
982 | |
741 | if ($FOCUS == $self) { |
983 | if ($FOCUS == $self) { |
742 | glColor 1, 1, 1; |
984 | glColor @{$self->{active_bg}}; |
|
|
985 | $self->{fg} = $self->{active_fg}; |
743 | } else { |
986 | } else { |
744 | glColor 0.7, 0.7, 0.7; |
987 | glColor @{$self->{bg}}; |
745 | } |
988 | } |
746 | |
989 | |
747 | glBegin GL_QUADS; |
990 | glBegin GL_QUADS; |
748 | glVertex 0 , 0; |
991 | glVertex 0 , 0; |
749 | glVertex 0 , $self->{h} - 1; |
992 | glVertex 0 , $self->{h}; |
750 | glVertex $self->{w} - 1, $self->{h} - 1; |
993 | glVertex $self->{w}, $self->{h}; |
751 | glVertex $self->{w} - 1, 0; |
994 | glVertex $self->{w}, 0; |
752 | glEnd; |
995 | glEnd; |
753 | |
996 | |
754 | $self->SUPER::_draw; |
997 | $self->SUPER::_draw; |
755 | } |
|
|
756 | |
998 | |
757 | ############################################################################# |
999 | #TODO: force update every cursor change :( |
|
|
1000 | if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { |
|
|
1001 | glColor @{$self->{fg}}; |
|
|
1002 | glBegin GL_LINES; |
|
|
1003 | glVertex $self->{cur_x}, $self->{cur_y}; |
|
|
1004 | glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; |
|
|
1005 | glEnd; |
|
|
1006 | } |
|
|
1007 | } |
758 | |
1008 | |
|
|
1009 | ############################################################################# |
|
|
1010 | |
|
|
1011 | package CFClient::Widget::Slider; |
|
|
1012 | |
|
|
1013 | use strict; |
|
|
1014 | |
|
|
1015 | use SDL::OpenGL; |
|
|
1016 | use SDL::OpenGL::Constants; |
|
|
1017 | |
|
|
1018 | our @ISA = CFClient::Widget::DrawBG::; |
|
|
1019 | |
|
|
1020 | sub size_request { |
|
|
1021 | my ($self) = @_; |
|
|
1022 | |
|
|
1023 | my $w = 50; |
|
|
1024 | my $h = 10; |
|
|
1025 | |
|
|
1026 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
1027 | } |
|
|
1028 | |
|
|
1029 | sub new { |
|
|
1030 | my $class = shift; |
|
|
1031 | |
|
|
1032 | # range [value, low, high, page] |
|
|
1033 | |
|
|
1034 | $class->SUPER::new ( |
|
|
1035 | fg => [1, 1, 1], |
|
|
1036 | active_fg => [0, 0, 0], |
|
|
1037 | range => [0, 0, 100, 10], |
|
|
1038 | vertical => 1, |
|
|
1039 | @_ |
|
|
1040 | ) |
|
|
1041 | } |
|
|
1042 | |
|
|
1043 | sub button_down { |
|
|
1044 | my ($self, $ev, $x, $y) = @_; |
|
|
1045 | |
|
|
1046 | $self->SUPER::button_down ($ev, $x, $y); |
|
|
1047 | $self->mouse_motion ($ev, $x, $y); |
|
|
1048 | } |
|
|
1049 | |
|
|
1050 | sub mouse_motion { |
|
|
1051 | my ($self, $ev, $x, $y) = @_; |
|
|
1052 | |
|
|
1053 | if ($GRAB == $self) { |
|
|
1054 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1055 | |
|
|
1056 | my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); |
|
|
1057 | |
|
|
1058 | $x = $x * ($hi - $lo) / $w + $lo; |
|
|
1059 | $x = $lo if $x < $lo; |
|
|
1060 | $x = $hi - $page if $x > $hi - $page; |
|
|
1061 | $self->{range}[0] = $x; |
|
|
1062 | |
|
|
1063 | $self->emit (changed => $x); |
|
|
1064 | $self->update; |
|
|
1065 | } |
|
|
1066 | } |
|
|
1067 | |
|
|
1068 | sub _draw { |
|
|
1069 | my ($self) = @_; |
|
|
1070 | |
|
|
1071 | $self->SUPER::_draw (); |
|
|
1072 | |
|
|
1073 | my ($w, $h) = @$self{qw(w h)}; |
|
|
1074 | |
|
|
1075 | if ($self->{vertical}) { |
|
|
1076 | # draw a vertical slider like a rotated horizontal slider |
|
|
1077 | |
|
|
1078 | glRotate 90, 0, 0, 1; |
|
|
1079 | glTranslate 0, -$self->{w}, 0; |
|
|
1080 | |
|
|
1081 | ($w, $h) = ($h, $w); |
|
|
1082 | } |
|
|
1083 | |
|
|
1084 | my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; |
|
|
1085 | my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; |
|
|
1086 | |
|
|
1087 | my ($value, $lo, $hi, $page) = @{$self->{range}}; |
|
|
1088 | |
|
|
1089 | $page = int $page * $w / ($hi - $lo); |
|
|
1090 | $value = int +($value - $lo) * $w / ($hi - $lo); |
|
|
1091 | |
|
|
1092 | $w -= $page; |
|
|
1093 | $page &= ~1; |
|
|
1094 | glTranslate $page * 0.5, 0, 0; |
|
|
1095 | |
|
|
1096 | glColor @$fg; |
|
|
1097 | glBegin GL_LINES; |
|
|
1098 | glVertex 0, 0; glVertex 0, $h; |
|
|
1099 | glVertex $w - 1, 0; glVertex $w - 1, $h; |
|
|
1100 | glVertex 0, $h * 0.5; glVertex $w, $h * 0.5; |
|
|
1101 | glEnd; |
|
|
1102 | |
|
|
1103 | my $knob_a = $value - $page * 0.5; |
|
|
1104 | my $knob_b = $value + $page * 0.5; |
|
|
1105 | |
|
|
1106 | glBegin GL_QUADS; |
|
|
1107 | glColor @$fg; |
|
|
1108 | glVertex $knob_a, 0; |
|
|
1109 | glVertex $knob_a, $h; |
|
|
1110 | glVertex $knob_b, $h; |
|
|
1111 | glVertex $knob_b, 0; |
|
|
1112 | |
|
|
1113 | if ($knob_a < $knob_b - 2) { |
|
|
1114 | glColor @$bg; |
|
|
1115 | glVertex $knob_a + 1, 1; |
|
|
1116 | glVertex $knob_a + 1, $h - 1; |
|
|
1117 | glVertex $knob_b - 1, $h - 1; |
|
|
1118 | glVertex $knob_b - 1, 1; |
|
|
1119 | } |
|
|
1120 | glEnd; |
|
|
1121 | } |
|
|
1122 | |
|
|
1123 | ############################################################################# |
|
|
1124 | |
759 | package Crossfire::Client::Widget::MapWidget; |
1125 | package CFClient::Widget::MapWidget; |
760 | |
1126 | |
761 | use strict; |
1127 | use strict; |
762 | |
1128 | |
763 | use List::Util qw(min max); |
1129 | use List::Util qw(min max); |
764 | |
1130 | |
765 | use SDL; |
1131 | use SDL; |
766 | use SDL::OpenGL; |
1132 | use SDL::OpenGL; |
767 | use SDL::OpenGL::Constants; |
1133 | use SDL::OpenGL::Constants; |
768 | |
1134 | |
769 | our @ISA = Crossfire::Client::Widget::; |
1135 | our @ISA = CFClient::Widget::; |
|
|
1136 | |
|
|
1137 | sub new { |
|
|
1138 | my $class = shift; |
|
|
1139 | |
|
|
1140 | $class->SUPER::new ( |
|
|
1141 | z => -1, |
|
|
1142 | list => (glGenLists 1), |
|
|
1143 | @_ |
|
|
1144 | ) |
|
|
1145 | } |
770 | |
1146 | |
771 | sub key_down { |
1147 | sub key_down { |
772 | print "MAPKEYDOWN\n"; |
1148 | print "MAPKEYDOWN\n"; |
773 | } |
1149 | } |
774 | |
1150 | |
… | |
… | |
780 | 1 + int $::WIDTH / 32, |
1156 | 1 + int $::WIDTH / 32, |
781 | 1 + int $::HEIGHT / 32, |
1157 | 1 + int $::HEIGHT / 32, |
782 | ) |
1158 | ) |
783 | } |
1159 | } |
784 | |
1160 | |
|
|
1161 | sub update { |
|
|
1162 | my ($self) = @_; |
|
|
1163 | |
|
|
1164 | $self->{need_update} = 1; |
|
|
1165 | } |
|
|
1166 | |
785 | sub _draw { |
1167 | sub _draw { |
786 | my ($self) = @_; |
1168 | my ($self) = @_; |
787 | |
1169 | |
|
|
1170 | if (delete $self->{need_update}) { |
|
|
1171 | glNewList $self->{list}, GL_COMPILE; |
|
|
1172 | |
788 | my $mx = $::CONN->{mapx}; |
1173 | my $mx = $::CONN->{mapx}; |
789 | my $my = $::CONN->{mapy}; |
1174 | my $my = $::CONN->{mapy}; |
790 | |
1175 | |
791 | my $map = $::CONN->{map}; |
1176 | my $map = $::CONN->{map}; |
792 | |
1177 | |
793 | my ($xofs, $yofs); |
1178 | my ($xofs, $yofs); |
794 | |
1179 | |
795 | my $sw = 1 + int $::WIDTH / 32; |
1180 | my $sw = 1 + int $::WIDTH / 32; |
796 | my $sh = 1 + int $::HEIGHT / 32; |
1181 | my $sh = 1 + int $::HEIGHT / 32; |
797 | |
1182 | |
798 | if ($::CONN->{mapw} > $sw) { |
1183 | if ($::CONN->{mapw} > $sw) { |
799 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
1184 | $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; |
800 | } else { |
1185 | } else { |
801 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
1186 | $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; |
802 | } |
1187 | } |
803 | |
1188 | |
804 | if ($::CONN->{maph} > $sh) { |
1189 | if ($::CONN->{maph} > $sh) { |
805 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
1190 | $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; |
806 | } else { |
1191 | } else { |
807 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1192 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
808 | } |
1193 | } |
809 | |
1194 | |
810 | glEnable GL_TEXTURE_2D; |
1195 | glEnable GL_TEXTURE_2D; |
811 | glEnable GL_BLEND; |
1196 | glEnable GL_BLEND; |
812 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1197 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
813 | |
1198 | |
814 | my $sw4 = ($sw + 3) & ~3; |
1199 | my $sw4 = ($sw + 3) & ~3; |
815 | my $darkness = "\x00" x ($sw4 * $sh); |
1200 | my $darkness = "\x00" x ($sw4 * $sh); |
816 | |
1201 | |
817 | for my $x (0 .. $sw - 1) { |
1202 | for my $x (0 .. $sw - 1) { |
818 | my $row = $map->[$x + $xofs]; |
1203 | my $row = $map->[$x + $xofs]; |
819 | for my $y (0 .. $sh - 1) { |
1204 | for my $y (0 .. $sh - 1) { |
820 | |
1205 | |
821 | my $cell = $row->[$y + $yofs] |
1206 | my $cell = $row->[$y + $yofs] |
822 | or next; |
1207 | or next; |
823 | |
1208 | |
824 | my $dark = $cell->[0]; |
1209 | my $dark = $cell->[0]; |
825 | if ($dark < 0) { |
1210 | if ($dark < 0) { |
826 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
1211 | substr $darkness, $y * $sw4 + $x, 1, chr 224; |
827 | } else { |
1212 | } else { |
828 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
1213 | substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark; |
829 | } |
1214 | } |
830 | |
1215 | |
831 | for my $num (grep $_, @$cell[1,2,3]) { |
1216 | for my $num (grep $_, @$cell[1,2,3]) { |
832 | my $tex = $::CONN->{face}[$num]{texture} || next; |
1217 | my $tex = $::CONN->{face}[$num]{texture} || next; |
833 | |
1218 | |
834 | my $w = $tex->{width}; |
1219 | my ($w, $h) = @$tex{qw(w h)}; |
835 | my $h = $tex->{height}; |
|
|
836 | |
1220 | |
837 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
1221 | $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h); |
|
|
1222 | } |
838 | } |
1223 | } |
839 | } |
1224 | } |
840 | } |
|
|
841 | |
1225 | |
842 | # if (1) { # higher quality darkness |
1226 | # if (1) { # higher quality darkness |
843 | # $lighting =~ s/(.)/$1$1$1/gs; |
1227 | # $lighting =~ s/(.)/$1$1$1/gs; |
844 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
1228 | # my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3; |
845 | # |
1229 | # |
… | |
… | |
847 | # |
1231 | # |
848 | # $lighting = $pb->get_pixels; |
1232 | # $lighting = $pb->get_pixels; |
849 | # $lighting =~ s/(.)../$1/gs; |
1233 | # $lighting =~ s/(.)../$1/gs; |
850 | # } |
1234 | # } |
851 | |
1235 | |
852 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1236 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
853 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1237 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
854 | |
1238 | |
855 | $darkness = new Crossfire::Client::Texture |
1239 | $darkness = new CFClient::Texture |
856 | width => $sw4, |
1240 | width => $sw4, |
857 | height => $sh, |
1241 | height => $sh, |
858 | data => $darkness, |
1242 | data => $darkness, |
859 | internalformat => GL_ALPHA, |
1243 | internalformat => GL_ALPHA, |
860 | format => GL_ALPHA; |
1244 | format => GL_ALPHA; |
861 | |
1245 | |
862 | glColor 0.45, 0.45, 0.45, 1; |
1246 | glColor 0.45, 0.45, 0.45, 1; |
863 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
1247 | $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32); |
864 | |
1248 | |
865 | glDisable GL_TEXTURE_2D; |
1249 | glDisable GL_TEXTURE_2D; |
866 | glDisable GL_BLEND; |
1250 | glDisable GL_BLEND; |
|
|
1251 | |
|
|
1252 | glEndList; |
|
|
1253 | } |
|
|
1254 | |
|
|
1255 | glCallList $self->{list}; |
867 | } |
1256 | } |
868 | |
1257 | |
869 | my %DIR = ( |
1258 | my %DIR = ( |
870 | SDLK_KP8, [1, "north"], |
1259 | SDLK_KP8, [1, "north"], |
871 | SDLK_KP9, [2, "northeast"], |
1260 | SDLK_KP9, [2, "northeast"], |
… | |
… | |
917 | } |
1306 | } |
918 | } |
1307 | } |
919 | |
1308 | |
920 | ############################################################################# |
1309 | ############################################################################# |
921 | |
1310 | |
922 | package Crossfire::Client::Widget::Animator; |
1311 | package CFClient::Widget::Animator; |
923 | |
1312 | |
924 | use SDL::OpenGL; |
1313 | use SDL::OpenGL; |
925 | |
1314 | |
926 | our @ISA = Crossfire::Client::Widget::Bin::; |
1315 | our @ISA = CFClient::Widget::Bin::; |
927 | |
1316 | |
928 | sub moveto { |
1317 | sub moveto { |
929 | my ($self, $x, $y) = @_; |
1318 | my ($self, $x, $y) = @_; |
930 | |
1319 | |
931 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
1320 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
959 | glPopMatrix; |
1348 | glPopMatrix; |
960 | } |
1349 | } |
961 | |
1350 | |
962 | ############################################################################# |
1351 | ############################################################################# |
963 | |
1352 | |
964 | package Crossfire::Client::Widget::Toplevel; |
1353 | package CFClient::Widget::Toplevel; |
965 | |
1354 | |
966 | our @ISA = Crossfire::Client::Widget::Container::; |
1355 | our @ISA = CFClient::Widget::Container::; |
967 | |
1356 | |
968 | sub size_request { |
1357 | sub size_request { |
969 | ($::WIDTH, $::HEIGHT) |
1358 | ($::WIDTH, $::HEIGHT) |
970 | } |
1359 | } |
971 | |
1360 | |
… | |
… | |
1005 | $self->_draw; |
1394 | $self->_draw; |
1006 | } |
1395 | } |
1007 | |
1396 | |
1008 | ############################################################################# |
1397 | ############################################################################# |
1009 | |
1398 | |
1010 | package Crossfire::Client::Widget; |
1399 | package CFClient::Widget; |
1011 | |
1400 | |
1012 | $TOPLEVEL = new Crossfire::Client::Widget::Toplevel; |
1401 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
1013 | |
1402 | |
1014 | 1 |
1403 | 1 |
1015 | |
1404 | |