1 | package CFClient::Widget; |
1 | package CFClient::UI; |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
4 | |
4 | |
5 | use Scalar::Util; |
5 | use Scalar::Util (); |
6 | |
6 | use List::Util (); |
7 | use SDL::OpenGL; |
|
|
8 | use SDL::OpenGL::Constants; |
|
|
9 | |
7 | |
10 | use CFClient; |
8 | use CFClient; |
11 | |
9 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
10 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
11 | |
… | |
… | |
70 | } |
68 | } |
71 | |
69 | |
72 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
70 | $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; |
73 | } |
71 | } |
74 | |
72 | |
|
|
73 | ############################################################################# |
|
|
74 | |
|
|
75 | package CFClient::UI::Base; |
|
|
76 | |
|
|
77 | use strict; |
|
|
78 | |
|
|
79 | use SDL::OpenGL; |
|
|
80 | |
75 | sub new { |
81 | sub new { |
76 | my $class = shift; |
82 | my $class = shift; |
77 | |
83 | |
78 | bless { |
84 | bless { |
79 | x => 0, |
85 | x => 0, |
80 | y => 0, |
86 | y => 0, |
81 | z => 0, |
87 | z => 0, |
|
|
88 | w => -1, |
|
|
89 | h => -1, |
82 | @_ |
90 | @_ |
83 | }, $class |
91 | }, $class |
84 | } |
92 | } |
85 | |
93 | |
86 | sub move { |
94 | sub move { |
… | |
… | |
97 | sub size_request { |
105 | sub size_request { |
98 | require Carp; |
106 | require Carp; |
99 | Carp::confess "size_request is abtract"; |
107 | Carp::confess "size_request is abtract"; |
100 | } |
108 | } |
101 | |
109 | |
102 | sub size_allocate { |
110 | sub _size_allocate { |
103 | my ($self, $w, $h) = @_; |
111 | my ($self, $x, $y, $w, $h) = @_; |
|
|
112 | |
|
|
113 | $self->{x} = $x; |
|
|
114 | $self->{y} = $y; |
|
|
115 | |
|
|
116 | return unless $self->{w} != $w || $self->{h} != $h; |
104 | |
117 | |
105 | $self->{w} = $w; |
118 | $self->{w} = $w; |
106 | $self->{h} = $h; |
119 | $self->{h} = $h; |
|
|
120 | |
|
|
121 | 1 |
|
|
122 | } |
|
|
123 | |
|
|
124 | sub size_allocate { |
|
|
125 | my ($self, $x, $y, $w, $h) = @_; |
|
|
126 | |
|
|
127 | $self->_size_allocate ($x, $y, $w, $h); |
107 | } |
128 | } |
108 | |
129 | |
109 | # translate global koordinates to local coordinate system |
130 | # translate global koordinates to local coordinate system |
110 | sub translate { |
131 | sub translate { |
111 | my ($self, $x, $y) = @_; |
132 | my ($self, $x, $y) = @_; |
… | |
… | |
158 | glTranslate $self->{x}, $self->{y}, 0; |
179 | glTranslate $self->{x}, $self->{y}, 0; |
159 | $self->_draw; |
180 | $self->_draw; |
160 | glPopMatrix; |
181 | glPopMatrix; |
161 | |
182 | |
162 | if ($self == $HOVER) { |
183 | if ($self == $HOVER) { |
163 | my ($x, $y) = @$self->{qw(x y)}; |
184 | my ($x, $y) = @$self{qw(x y)}; |
164 | |
185 | |
165 | glColor 1, 1, 1, 0.1; |
186 | glColor 1, 1, 1, 0.1; |
166 | glEnable GL_BLEND; |
187 | glEnable GL_BLEND; |
|
|
188 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
167 | glBegin GL_QUADS; |
189 | glBegin GL_QUADS; |
168 | glVertex $x , $y; |
190 | glVertex $x , $y; |
169 | glVertex $x + $self->{w}, $y; |
191 | glVertex $x + $self->{w}, $y; |
170 | glVertex $x + $self->{w}, $y + $self->{h}; |
192 | glVertex $x + $self->{w}, $y + $self->{h}; |
171 | glVertex $x , $y + $self->{h}; |
193 | glVertex $x , $y + $self->{h}; |
… | |
… | |
240 | #$self->deactivate; |
262 | #$self->deactivate; |
241 | } |
263 | } |
242 | |
264 | |
243 | ############################################################################# |
265 | ############################################################################# |
244 | |
266 | |
245 | package CFClient::Widget::DrawBG; |
267 | package CFClient::UI::DrawBG; |
246 | |
268 | |
247 | our @ISA = CFClient::Widget::; |
269 | our @ISA = CFClient::UI::Base::; |
248 | |
270 | |
249 | use strict; |
271 | use strict; |
250 | use SDL::OpenGL; |
272 | use SDL::OpenGL; |
251 | |
273 | |
252 | sub new { |
274 | sub new { |
… | |
… | |
275 | glEnd; |
297 | glEnd; |
276 | } |
298 | } |
277 | |
299 | |
278 | ############################################################################# |
300 | ############################################################################# |
279 | |
301 | |
280 | package CFClient::Widget::Empty; |
302 | package CFClient::UI::Empty; |
281 | |
303 | |
282 | our @ISA = CFClient::Widget::; |
304 | our @ISA = CFClient::UI::Base::; |
283 | |
305 | |
284 | sub size_request { |
306 | sub size_request { |
285 | (0, 0) |
307 | (0, 0) |
286 | } |
308 | } |
287 | |
309 | |
288 | sub draw { } |
310 | sub draw { } |
289 | |
311 | |
290 | ############################################################################# |
312 | ############################################################################# |
291 | |
313 | |
292 | package CFClient::Widget::Container; |
314 | package CFClient::UI::Container; |
293 | |
315 | |
294 | our @ISA = CFClient::Widget::; |
316 | our @ISA = CFClient::UI::Base::; |
295 | |
317 | |
296 | sub new { |
318 | sub new { |
297 | my ($class, %arg) = @_; |
319 | my ($class, %arg) = @_; |
298 | |
320 | |
299 | my $children = delete $arg{children} || []; |
321 | my $children = delete $arg{children} || []; |
… | |
… | |
313 | $self->{children} = [ |
335 | $self->{children} = [ |
314 | sort { $a->{z} <=> $b->{z} } |
336 | sort { $a->{z} <=> $b->{z} } |
315 | @{$self->{children}}, $chld |
337 | @{$self->{children}}, $chld |
316 | ]; |
338 | ]; |
317 | |
339 | |
318 | $self->size_allocate ($self->{w}, $self->{h}) |
340 | $self->{w} = $self->{h} = -1; |
319 | if $self->{w}; #TODO: check for "realised state" |
341 | $self->update; |
320 | } |
342 | } |
321 | |
343 | |
322 | sub remove { |
344 | sub remove { |
323 | my ($self, $widget) = @_; |
345 | my ($self, $widget) = @_; |
324 | |
346 | |
325 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
347 | $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; |
326 | |
348 | |
327 | $self->size_allocate ($self->{w}, $self->{h}); |
349 | $self->size_allocate (0, 0, $self->{w}, $self->{h}); |
328 | } |
350 | } |
329 | |
351 | |
330 | sub find_widget { |
352 | sub find_widget { |
331 | my ($self, $x, $y) = @_; |
353 | my ($self, $x, $y) = @_; |
332 | |
354 | |
… | |
… | |
349 | $_->draw for @{$self->{children}}; |
371 | $_->draw for @{$self->{children}}; |
350 | } |
372 | } |
351 | |
373 | |
352 | ############################################################################# |
374 | ############################################################################# |
353 | |
375 | |
354 | package CFClient::Widget::Bin; |
376 | package CFClient::UI::Bin; |
355 | |
377 | |
356 | our @ISA = CFClient::Widget::Container::; |
378 | our @ISA = CFClient::UI::Container::; |
357 | |
379 | |
358 | sub new { |
380 | sub new { |
359 | my ($class, %arg) = @_; |
381 | my ($class, %arg) = @_; |
360 | |
382 | |
361 | my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; |
383 | my $child = (delete $arg{child}) || new CFClient::UI::Empty::; |
362 | |
384 | |
363 | $class->SUPER::new (children => [$child], %arg) |
385 | $class->SUPER::new (children => [$child], %arg) |
364 | } |
386 | } |
365 | |
387 | |
366 | sub add { |
388 | sub add { |
… | |
… | |
374 | sub remove { |
396 | sub remove { |
375 | my ($self, $widget) = @_; |
397 | my ($self, $widget) = @_; |
376 | |
398 | |
377 | $self->SUPER::remove ($widget); |
399 | $self->SUPER::remove ($widget); |
378 | |
400 | |
379 | $self->{children} = [new CFClient::Widget::Empty] |
401 | $self->{children} = [new CFClient::UI::Empty] |
380 | unless @{$self->{children}}; |
402 | unless @{$self->{children}}; |
381 | } |
403 | } |
382 | |
404 | |
383 | sub child { $_[0]->{children}[0] } |
405 | sub child { $_[0]->{children}[0] } |
384 | |
406 | |
385 | sub size_request { |
407 | sub size_request { |
386 | $_[0]{children}[0]->size_request |
408 | $_[0]{children}[0]->size_request |
387 | } |
409 | } |
388 | |
410 | |
389 | sub size_allocate { |
411 | sub size_allocate { |
390 | my ($self, $w, $h) = @_; |
412 | my ($self, $x, $y, $w, $h) = @_; |
391 | |
413 | |
392 | return unless $self->{w} != $w || $self->{h} != $h; |
414 | $self->_size_allocate ($x, $y, $w, $h) or return; |
393 | |
415 | |
394 | $self->SUPER::size_allocate ($w, $h); |
|
|
395 | $self->{children}[0]->size_allocate ($w, $h); |
416 | $self->{children}[0]->size_allocate (0, 0, $w, $h); |
396 | } |
417 | } |
397 | |
418 | |
398 | ############################################################################# |
419 | ############################################################################# |
399 | |
420 | |
400 | package CFClient::Widget::Window; |
421 | package CFClient::UI::Window; |
401 | |
422 | |
402 | our @ISA = CFClient::Widget::Bin::; |
423 | our @ISA = CFClient::UI::Bin::; |
403 | |
424 | |
404 | use SDL::OpenGL; |
425 | use SDL::OpenGL; |
405 | |
426 | |
406 | sub new { |
427 | sub new { |
407 | my ($class, %arg) = @_; |
428 | my ($class, %arg) = @_; |
… | |
… | |
425 | $self->{w}, $self->{h}, sub { $self->child->draw } |
446 | $self->{w}, $self->{h}, sub { $self->child->draw } |
426 | ); |
447 | ); |
427 | } |
448 | } |
428 | |
449 | |
429 | sub size_allocate { |
450 | sub size_allocate { |
430 | my ($self, $w, $h) = @_; |
451 | my ($self, $x, $y, $w, $h) = @_; |
431 | |
452 | |
432 | return unless $self->{w} != $w || $self->{h} != $h; |
453 | $self->_size_allocate ($x, $y, $w, $h) or return; |
433 | |
454 | |
434 | $self->{w} = $w; |
|
|
435 | $self->{h} = $h; |
|
|
436 | |
|
|
437 | $self->child->size_allocate ($w, $h); |
455 | $self->child->size_allocate (0, 0, $w, $h); |
438 | |
456 | |
439 | $self->render_chld; |
457 | $self->render_chld; |
440 | } |
458 | } |
441 | |
459 | |
442 | sub _draw { |
460 | sub _draw { |
… | |
… | |
446 | |
464 | |
447 | my $tex = $self->{texture} |
465 | my $tex = $self->{texture} |
448 | or return; |
466 | or return; |
449 | |
467 | |
450 | glEnable GL_BLEND; |
468 | glEnable GL_BLEND; |
|
|
469 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
451 | glEnable GL_TEXTURE_2D; |
470 | glEnable GL_TEXTURE_2D; |
452 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
471 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
453 | |
472 | |
454 | $tex->draw_quad (0, 0, $w, $h); |
473 | $tex->draw_quad (0, 0, $w, $h); |
455 | |
474 | |
… | |
… | |
457 | glDisable GL_TEXTURE_2D; |
476 | glDisable GL_TEXTURE_2D; |
458 | } |
477 | } |
459 | |
478 | |
460 | ############################################################################# |
479 | ############################################################################# |
461 | |
480 | |
462 | package CFClient::Widget::Frame; |
481 | package CFClient::UI::Frame; |
463 | |
482 | |
464 | our @ISA = CFClient::Widget::Bin::; |
483 | our @ISA = CFClient::UI::Bin::; |
465 | |
484 | |
466 | use SDL::OpenGL; |
485 | use SDL::OpenGL; |
467 | |
486 | |
468 | sub size_request { |
487 | sub size_request { |
469 | my ($self) = @_; |
488 | my ($self) = @_; |
… | |
… | |
474 | |
493 | |
475 | map { $_ + 4 } $chld->size_request; |
494 | map { $_ + 4 } $chld->size_request; |
476 | } |
495 | } |
477 | |
496 | |
478 | sub size_allocate { |
497 | sub size_allocate { |
479 | my ($self, $w, $h) = @_; |
498 | my ($self, $x, $y, $w, $h) = @_; |
480 | |
499 | |
481 | return unless $self->{w} != $w || $self->{h} != $h; |
500 | $self->_size_allocate ($x, $y, $w, $h) or return; |
482 | |
501 | |
483 | $self->{w} = $w; |
|
|
484 | $self->{h} = $h; |
|
|
485 | |
|
|
486 | $self->child->size_allocate ($w - 4, $h - 4); |
502 | $self->child->size_allocate (2, 2, $w - 4, $h - 4); |
487 | $self->child->move (2, 2); |
|
|
488 | } |
503 | } |
489 | |
504 | |
490 | sub _draw { |
505 | sub _draw { |
491 | my ($self) = @_; |
506 | my ($self) = @_; |
492 | |
507 | |
… | |
… | |
505 | $chld->draw; |
520 | $chld->draw; |
506 | } |
521 | } |
507 | |
522 | |
508 | ############################################################################# |
523 | ############################################################################# |
509 | |
524 | |
510 | package CFClient::Widget::FancyFrame; |
525 | package CFClient::UI::FancyFrame; |
511 | |
526 | |
512 | our @ISA = CFClient::Widget::Bin::; |
527 | our @ISA = CFClient::UI::Bin::; |
513 | |
528 | |
514 | use SDL::OpenGL; |
529 | use SDL::OpenGL; |
515 | |
530 | |
516 | my @tex = |
531 | my @tex = |
517 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
532 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } |
… | |
… | |
529 | |
544 | |
530 | ($w, $h) |
545 | ($w, $h) |
531 | } |
546 | } |
532 | |
547 | |
533 | sub size_allocate { |
548 | sub size_allocate { |
534 | my ($self, $w, $h) = @_; |
549 | my ($self, $x, $y, $w, $h) = @_; |
535 | |
550 | |
536 | return unless $self->{w} != $w || $self->{h} != $h; |
551 | $self->_size_allocate ($x, $y, $w, $h) or return; |
537 | |
|
|
538 | $self->SUPER::size_allocate ($w, $h); |
|
|
539 | |
552 | |
540 | $h -= $tex[1]->{h}; |
553 | $h -= $tex[1]->{h}; |
541 | $h -= $tex[4]->{h}; |
554 | $h -= $tex[4]->{h}; |
542 | $w -= $tex[2]->{w}; |
555 | $w -= $tex[2]->{w}; |
543 | $w -= $tex[3]->{w}; |
556 | $w -= $tex[3]->{w}; |
… | |
… | |
545 | $h = $h < 0 ? 0 : $h; |
558 | $h = $h < 0 ? 0 : $h; |
546 | $w = $w < 0 ? 0 : $w; |
559 | $w = $w < 0 ? 0 : $w; |
547 | |
560 | |
548 | my $child = $self->child; |
561 | my $child = $self->child; |
549 | |
562 | |
550 | $child->size_allocate ($w, $h); |
|
|
551 | $child->move ($tex[3]->{w}, $tex[1]->{h}); |
563 | $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h); |
552 | } |
564 | } |
553 | |
565 | |
554 | sub _draw { |
566 | sub _draw { |
555 | my ($self) = @_; |
567 | my ($self) = @_; |
556 | |
568 | |
557 | my ($w, $h) = ($self->{w}, $self->{h}); |
569 | my ($w, $h) = ($self->{w}, $self->{h}); |
558 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
570 | my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); |
559 | |
571 | |
560 | glEnable GL_BLEND; |
572 | glEnable GL_BLEND; |
|
|
573 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
561 | glEnable GL_TEXTURE_2D; |
574 | glEnable GL_TEXTURE_2D; |
562 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
563 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
575 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
564 | |
576 | |
565 | my $top = $tex[1]; |
577 | my $top = $tex[1]; |
566 | $top->draw_quad (0, 0, $w, $top->{h}); |
578 | $top->draw_quad (0, 0, $w, $top->{h}); |
567 | |
579 | |
… | |
… | |
592 | |
604 | |
593 | } |
605 | } |
594 | |
606 | |
595 | ############################################################################# |
607 | ############################################################################# |
596 | |
608 | |
597 | package CFClient::Widget::Table; |
609 | package CFClient::UI::Table; |
598 | |
610 | |
599 | our @ISA = CFClient::Widget::; |
611 | our @ISA = CFClient::UI::Base::; |
|
|
612 | |
|
|
613 | use List::Util qw(max sum); |
600 | |
614 | |
601 | use SDL::OpenGL; |
615 | use SDL::OpenGL; |
602 | |
616 | |
603 | sub add { |
617 | sub add { |
604 | my ($self, $x, $y, $chld) = @_; |
618 | my ($self, $x, $y, $chld) = @_; |
605 | my $old_chld = $self->{children}[$y][$x]; |
|
|
606 | |
619 | |
607 | $self->{children}[$y][$x] = $chld; |
620 | $self->{children}[$y][$x] = $chld; |
608 | $chld->set_parent ($self); |
621 | $chld->set_parent ($self); |
|
|
622 | |
|
|
623 | $self->{w} = $self->{h} = -1; |
609 | $self->update; |
624 | $self->update; |
610 | } |
625 | } |
611 | |
626 | |
612 | sub max_row_height { |
627 | sub get_wh { |
613 | my ($self, $row) = @_; |
628 | my ($self) = @_; |
614 | |
629 | |
615 | my $hs = 0; |
630 | my (@w, @h); |
616 | for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { |
631 | |
|
|
632 | for my $y (0 .. $#{$self->{children}}) { |
617 | my $c = $self->{children}->[$row]->[$xi]; |
633 | my $row = $self->{children}[$y] |
618 | if ($c) { |
634 | or next; |
|
|
635 | |
|
|
636 | for my $x (0 .. $#$row) { |
|
|
637 | my $widget = $row->[$x] |
|
|
638 | or next; |
619 | my ($w, $h) = $c->size_request; |
639 | my ($w, $h) = $widget->size_request; |
620 | if ($hs < $h) { $hs = $h } |
640 | |
|
|
641 | $w[$x] = max $w[$x], $w; |
|
|
642 | $h[$y] = max $h[$y], $h; |
621 | } |
643 | } |
622 | } |
644 | } |
623 | return $hs; |
|
|
624 | } |
|
|
625 | |
645 | |
626 | sub max_col_width { |
646 | (\@w, \@h) |
|
|
647 | } |
|
|
648 | |
|
|
649 | sub size_request { |
627 | my ($self, $col) = @_; |
650 | my ($self) = @_; |
628 | |
651 | |
|
|
652 | my ($ws, $hs) = $self->get_wh; |
|
|
653 | |
|
|
654 | ( |
|
|
655 | (List::Util::sum @$ws), |
|
|
656 | (List::Util::sum @$hs), |
|
|
657 | ) |
|
|
658 | } |
|
|
659 | |
|
|
660 | sub size_allocate { |
|
|
661 | my ($self, $x, $y, $w, $h) = @_; |
|
|
662 | |
|
|
663 | $self->_size_allocate ($x, $y, $w, $h) or return; |
|
|
664 | |
|
|
665 | my ($ws, $hs) = $self->get_wh; |
|
|
666 | |
|
|
667 | my $req_w = List::Util::sum @$ws; |
|
|
668 | my $req_h = List::Util::sum @$hs; |
|
|
669 | |
|
|
670 | # linearly scale sizes |
|
|
671 | $_ *= $req_w / $w for @$ws; |
|
|
672 | $_ *= $req_h / $h for @$hs; |
|
|
673 | |
|
|
674 | my $y; |
|
|
675 | |
|
|
676 | for my $r (0 .. $#{$self->{children}}) { |
|
|
677 | my $row = $self->{children}[$r] |
|
|
678 | or next; |
|
|
679 | |
629 | my $ws = 0; |
680 | my $x = 0; |
630 | for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { |
681 | my $row_h = $hs->[$r]; |
631 | my $c = ($self->{children}->[$yi] || [])->[$col]; |
682 | |
632 | if ($c) { |
683 | for my $c (0 .. $#$row) { |
633 | my ($w, $h) = $c->size_request; |
684 | my $widget = $row->[$c] |
634 | if ($ws < $w) { $ws = $w } |
685 | or next; |
|
|
686 | |
|
|
687 | my $col_w = $ws->[$c]; |
|
|
688 | |
|
|
689 | $widget->size_allocate ($x, $y, $col_w, $row_h); |
|
|
690 | |
|
|
691 | $x += $col_w; |
635 | } |
692 | } |
636 | } |
|
|
637 | return $ws; |
|
|
638 | } |
|
|
639 | |
693 | |
640 | sub size_request { |
694 | $y += $row_h; |
641 | my ($self) = @_; |
|
|
642 | |
|
|
643 | my ($hs, $ws) = (0, 0); |
|
|
644 | |
|
|
645 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
646 | $hs += $self->max_row_height ($yi); |
|
|
647 | } |
695 | } |
648 | |
696 | |
649 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
|
|
650 | my $wm = 0; |
|
|
651 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
|
|
652 | $wm += $self->max_col_width ($xi) |
|
|
653 | } |
|
|
654 | if ($ws < $wm) { $ws = $wm } |
|
|
655 | } |
|
|
656 | |
|
|
657 | return ($ws, $hs); |
|
|
658 | } |
697 | } |
659 | |
698 | |
660 | sub _draw { |
699 | sub _draw { |
661 | my ($self) = @_; |
700 | my ($self) = @_; |
662 | |
701 | |
663 | my $y = 0; |
702 | for (grep $_, @{$self->{children}}) { |
664 | for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { |
703 | $_->draw for grep $_, @$_; |
665 | my $x = 0; |
|
|
666 | |
|
|
667 | for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) { |
|
|
668 | |
|
|
669 | my $c = $self->{children}->[$yi]->[$xi]; |
|
|
670 | if ($c) { |
|
|
671 | $c->move ($x, $y, 0); #TODO: Move to size_request |
|
|
672 | $c->draw if $c; |
|
|
673 | } |
|
|
674 | |
|
|
675 | $x += $self->max_col_width ($xi); |
|
|
676 | } |
|
|
677 | |
|
|
678 | $y += $self->max_row_height ($yi); |
|
|
679 | } |
704 | } |
680 | } |
705 | } |
681 | |
706 | |
682 | ############################################################################# |
707 | ############################################################################# |
683 | |
708 | |
684 | package CFClient::Widget::VBox; |
709 | package CFClient::UI::VBox; |
685 | |
710 | |
686 | our @ISA = CFClient::Widget::Container::; |
711 | our @ISA = CFClient::UI::Container::; |
687 | |
712 | |
688 | use SDL::OpenGL; |
713 | use SDL::OpenGL; |
689 | |
714 | |
690 | sub size_request { |
715 | sub size_request { |
691 | my ($self) = @_; |
716 | my ($self) = @_; |
… | |
… | |
697 | (List::Util::sum map $_->[1], @alloc), |
722 | (List::Util::sum map $_->[1], @alloc), |
698 | ) |
723 | ) |
699 | } |
724 | } |
700 | |
725 | |
701 | sub size_allocate { |
726 | sub size_allocate { |
702 | my ($self, $w, $h) = @_; |
727 | my ($self, $x, $y, $w, $h) = @_; |
703 | |
728 | |
704 | return unless $self->{w} != $w || $self->{h} != $h; |
729 | $self->_size_allocate ($x, $y, $w, $h) or return; |
705 | |
|
|
706 | $self->{w} = $w; |
|
|
707 | $self->{h} = $h; |
|
|
708 | |
730 | |
709 | return unless $self->{h}; |
731 | return unless $self->{h}; |
710 | |
732 | |
711 | my $children = $self->{children}; |
733 | my $children = $self->{children}; |
712 | |
734 | |
… | |
… | |
734 | |
756 | |
735 | my $y = 0; |
757 | my $y = 0; |
736 | for (0 .. $#$children) { |
758 | for (0 .. $#$children) { |
737 | my $child = $children->[$_]; |
759 | my $child = $children->[$_]; |
738 | my $h = $h[$_]; |
760 | my $h = $h[$_]; |
739 | $child->move (0, $y); |
|
|
740 | $child->size_allocate ($w, $h); |
761 | $child->size_allocate (0, $y, $w, $h); |
741 | |
762 | |
742 | $y += $h; |
763 | $y += $h; |
743 | } |
764 | } |
744 | } |
765 | } |
745 | |
766 | |
746 | ############################################################################# |
767 | ############################################################################# |
747 | |
768 | |
748 | package CFClient::Widget::Label; |
769 | package CFClient::UI::Label; |
749 | |
770 | |
750 | our @ISA = CFClient::Widget::; |
771 | our @ISA = CFClient::UI::Base::; |
751 | |
772 | |
752 | use SDL::OpenGL; |
773 | use SDL::OpenGL; |
753 | |
774 | |
754 | sub new { |
775 | sub new { |
755 | my ($class, %arg) = @_; |
776 | my ($class, %arg) = @_; |
… | |
… | |
811 | # ($w, $h) |
832 | # ($w, $h) |
812 | # } |
833 | # } |
813 | } |
834 | } |
814 | |
835 | |
815 | sub size_allocate { |
836 | sub size_allocate { |
816 | my ($self, $w, $h) = @_; |
837 | my ($self, $x, $y, $w, $h) = @_; |
817 | |
838 | |
818 | return unless $self->{w} != $w || $self->{h} != $h; |
839 | $self->_size_allocate ($x, $y, $w, $h) or return; |
819 | |
840 | |
820 | $self->SUPER::size_allocate ($w, $h); |
|
|
821 | delete $self->{texture}; |
841 | delete $self->{texture}; |
822 | } |
842 | } |
823 | |
843 | |
824 | sub update { |
844 | sub update { |
825 | my ($self) = @_; |
845 | my ($self) = @_; |
… | |
… | |
831 | sub _draw { |
851 | sub _draw { |
832 | my ($self) = @_; |
852 | my ($self) = @_; |
833 | |
853 | |
834 | my $tex = $self->{texture} ||= do { |
854 | my $tex = $self->{texture} ||= do { |
835 | $self->{layout}->set_width ($self->{w}); |
855 | $self->{layout}->set_width ($self->{w}); |
836 | new_from_layout CFClient::Texture $self->{layout}; |
856 | new_from_layout CFClient::Texture $self->{layout} |
837 | }; |
857 | }; |
838 | |
858 | |
839 | glEnable GL_BLEND; |
859 | glEnable GL_BLEND; |
|
|
860 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
840 | glEnable GL_TEXTURE_2D; |
861 | glEnable GL_TEXTURE_2D; |
841 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
842 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
862 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
843 | |
863 | |
844 | glColor @{$self->{fg}}; |
864 | glColor @{$self->{fg}}; |
845 | |
865 | |
846 | my $x = |
866 | my $x = |
847 | $self->{align} < 0 ? 0 |
867 | $self->{align} < 0 ? 0 |
848 | : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} |
868 | : $self->{align} > 0 ? $self->{w} - $tex->{w} |
849 | : ($self->{w} + $self->{texture}{w}) * 0.5; |
869 | : ($self->{w} - $tex->{w}) * 0.5; |
850 | |
870 | |
851 | $tex->draw_quad ($x, 0); |
871 | $tex->draw_quad ($x, 0); |
852 | |
872 | |
|
|
873 | glDisable GL_TEXTURE_2D; |
853 | glDisable GL_BLEND; |
874 | glDisable GL_BLEND; |
854 | glDisable GL_TEXTURE_2D; |
|
|
855 | } |
875 | } |
856 | |
876 | |
857 | ############################################################################# |
877 | ############################################################################# |
858 | |
878 | |
859 | package CFClient::Widget::Entry; |
879 | package CFClient::UI::Entry; |
860 | |
880 | |
861 | our @ISA = CFClient::Widget::Label::; |
881 | our @ISA = CFClient::UI::Label::; |
862 | |
882 | |
863 | use SDL; |
883 | use SDL; |
864 | use SDL::OpenGL; |
884 | use SDL::OpenGL; |
865 | |
885 | |
866 | sub new { |
886 | sub new { |
… | |
… | |
901 | |
921 | |
902 | ($w + 1, $h) # add 1 for cursor |
922 | ($w + 1, $h) # add 1 for cursor |
903 | } |
923 | } |
904 | |
924 | |
905 | sub size_allocate { |
925 | sub size_allocate { |
906 | my ($self, $w, $h) = @_; |
926 | my ($self, $x, $y, $w, $h) = @_; |
907 | |
927 | |
908 | return unless $self->{w} != $w || $self->{h} != $h; |
|
|
909 | |
|
|
910 | $self->SUPER::size_allocate ($w, $h); |
928 | $self->SUPER::size_allocate ($x, $y, $w, $h); |
911 | |
929 | |
912 | $self->_set_text ($self->{text}); |
930 | $self->_set_text ($self->{text}); |
913 | } |
931 | } |
914 | |
932 | |
915 | sub set_text { |
933 | sub set_text { |
… | |
… | |
1006 | } |
1024 | } |
1007 | } |
1025 | } |
1008 | |
1026 | |
1009 | ############################################################################# |
1027 | ############################################################################# |
1010 | |
1028 | |
1011 | package CFClient::Widget::Slider; |
1029 | package CFClient::UI::Slider; |
1012 | |
1030 | |
1013 | use strict; |
1031 | use strict; |
1014 | |
1032 | |
1015 | use SDL::OpenGL; |
1033 | use SDL::OpenGL; |
1016 | use SDL::OpenGL::Constants; |
|
|
1017 | |
1034 | |
1018 | our @ISA = CFClient::Widget::DrawBG::; |
1035 | our @ISA = CFClient::UI::DrawBG::; |
1019 | |
1036 | |
1020 | sub size_request { |
1037 | sub size_request { |
1021 | my ($self) = @_; |
1038 | my ($self) = @_; |
1022 | |
1039 | |
1023 | my $w = 50; |
1040 | my $w = 50; |
… | |
… | |
1120 | glEnd; |
1137 | glEnd; |
1121 | } |
1138 | } |
1122 | |
1139 | |
1123 | ############################################################################# |
1140 | ############################################################################# |
1124 | |
1141 | |
1125 | package CFClient::Widget::MapWidget; |
1142 | package CFClient::UI::MapWidget; |
1126 | |
1143 | |
1127 | use strict; |
1144 | use strict; |
1128 | |
1145 | |
1129 | use List::Util qw(min max); |
1146 | use List::Util qw(min max); |
1130 | |
1147 | |
1131 | use SDL; |
1148 | use SDL; |
1132 | use SDL::OpenGL; |
1149 | use SDL::OpenGL; |
1133 | use SDL::OpenGL::Constants; |
|
|
1134 | |
1150 | |
1135 | our @ISA = CFClient::Widget::; |
1151 | our @ISA = CFClient::UI::Base::; |
1136 | |
1152 | |
1137 | sub new { |
1153 | sub new { |
1138 | my $class = shift; |
1154 | my $class = shift; |
1139 | |
1155 | |
1140 | $class->SUPER::new ( |
1156 | $class->SUPER::new ( |
… | |
… | |
1160 | |
1176 | |
1161 | sub update { |
1177 | sub update { |
1162 | my ($self) = @_; |
1178 | my ($self) = @_; |
1163 | |
1179 | |
1164 | $self->{need_update} = 1; |
1180 | $self->{need_update} = 1; |
|
|
1181 | $self->SUPER::update; |
1165 | } |
1182 | } |
1166 | |
1183 | |
1167 | sub _draw { |
1184 | sub _draw { |
1168 | my ($self) = @_; |
1185 | my ($self) = @_; |
1169 | |
1186 | |
… | |
… | |
1192 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1209 | $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; |
1193 | } |
1210 | } |
1194 | |
1211 | |
1195 | glEnable GL_TEXTURE_2D; |
1212 | glEnable GL_TEXTURE_2D; |
1196 | glEnable GL_BLEND; |
1213 | glEnable GL_BLEND; |
|
|
1214 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
1197 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1215 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
1198 | |
1216 | |
1199 | my $sw4 = ($sw + 3) & ~3; |
1217 | my $sw4 = ($sw + 3) & ~3; |
1200 | my $darkness = "\x00" x ($sw4 * $sh); |
1218 | my $darkness = "\x00" x ($sw4 * $sh); |
1201 | |
1219 | |
… | |
… | |
1231 | # |
1249 | # |
1232 | # $lighting = $pb->get_pixels; |
1250 | # $lighting = $pb->get_pixels; |
1233 | # $lighting =~ s/(.)../$1/gs; |
1251 | # $lighting =~ s/(.)../$1/gs; |
1234 | # } |
1252 | # } |
1235 | |
1253 | |
1236 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
1237 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1254 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
1238 | |
1255 | |
1239 | $darkness = new CFClient::Texture |
1256 | $darkness = new CFClient::Texture |
1240 | width => $sw4, |
1257 | w => $sw4, |
1241 | height => $sh, |
1258 | h => $sh, |
1242 | data => $darkness, |
1259 | data => $darkness, |
1243 | internalformat => GL_ALPHA, |
1260 | internalformat => GL_ALPHA, |
1244 | format => GL_ALPHA; |
1261 | format => GL_ALPHA; |
1245 | |
1262 | |
1246 | glColor 0.45, 0.45, 0.45, 1; |
1263 | glColor 0.45, 0.45, 0.45, 1; |
… | |
… | |
1306 | } |
1323 | } |
1307 | } |
1324 | } |
1308 | |
1325 | |
1309 | ############################################################################# |
1326 | ############################################################################# |
1310 | |
1327 | |
1311 | package CFClient::Widget::Animator; |
1328 | package CFClient::UI::Animator; |
1312 | |
1329 | |
1313 | use SDL::OpenGL; |
1330 | use SDL::OpenGL; |
1314 | |
1331 | |
1315 | our @ISA = CFClient::Widget::Bin::; |
1332 | our @ISA = CFClient::UI::Bin::; |
1316 | |
1333 | |
1317 | sub moveto { |
1334 | sub moveto { |
1318 | my ($self, $x, $y) = @_; |
1335 | my ($self, $x, $y) = @_; |
1319 | |
1336 | |
1320 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
1337 | $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; |
… | |
… | |
1348 | glPopMatrix; |
1365 | glPopMatrix; |
1349 | } |
1366 | } |
1350 | |
1367 | |
1351 | ############################################################################# |
1368 | ############################################################################# |
1352 | |
1369 | |
1353 | package CFClient::Widget::Toplevel; |
1370 | package CFClient::UI::Toplevel; |
1354 | |
1371 | |
1355 | our @ISA = CFClient::Widget::Container::; |
1372 | our @ISA = CFClient::UI::Container::; |
1356 | |
1373 | |
1357 | sub size_request { |
1374 | sub size_request { |
1358 | ($::WIDTH, $::HEIGHT) |
1375 | ($::WIDTH, $::HEIGHT) |
1359 | } |
1376 | } |
1360 | |
1377 | |
1361 | sub size_allocate { |
1378 | sub size_allocate { |
1362 | my ($self, $w, $h) = @_; |
1379 | my ($self, $x, $y, $w, $h) = @_; |
1363 | |
1380 | |
1364 | $self->SUPER::size_allocate ($w, $h); |
1381 | $self->_size_allocate ($x, $y, $w, $h); |
1365 | |
1382 | |
1366 | $_->size_allocate ($_->size_request) |
1383 | $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) |
1367 | for @{$self->{children}}; |
1384 | for @{$self->{children}}; |
1368 | } |
1385 | } |
1369 | |
1386 | |
1370 | sub translate { |
1387 | sub translate { |
1371 | my ($self, $x, $y) = @_; |
1388 | my ($self, $x, $y) = @_; |
… | |
… | |
1374 | } |
1391 | } |
1375 | |
1392 | |
1376 | sub update { |
1393 | sub update { |
1377 | my ($self) = @_; |
1394 | my ($self) = @_; |
1378 | |
1395 | |
1379 | $self->size_allocate ($self->size_request); |
1396 | $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); |
1380 | ::refresh (); |
1397 | ::refresh (); |
1381 | } |
1398 | } |
1382 | |
1399 | |
1383 | sub add { |
1400 | sub add { |
1384 | my ($self, $widget) = @_; |
1401 | my ($self, $widget) = @_; |
1385 | |
1402 | |
1386 | $self->SUPER::add ($widget); |
1403 | $self->SUPER::add ($widget); |
1387 | |
1404 | |
1388 | $widget->size_allocate ($widget->size_request); |
1405 | $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); |
1389 | } |
1406 | } |
1390 | |
1407 | |
1391 | sub draw { |
1408 | sub draw { |
1392 | my ($self) = @_; |
1409 | my ($self) = @_; |
1393 | |
1410 | |
1394 | $self->_draw; |
1411 | $self->_draw; |
1395 | } |
1412 | } |
1396 | |
1413 | |
1397 | ############################################################################# |
1414 | ############################################################################# |
1398 | |
1415 | |
1399 | package CFClient::Widget; |
1416 | package CFClient::UI; |
1400 | |
1417 | |
1401 | $TOPLEVEL = new CFClient::Widget::Toplevel; |
1418 | $TOPLEVEL = new CFClient::UI::Toplevel; |
1402 | |
1419 | |
1403 | 1 |
1420 | 1 |
1404 | |
1421 | |