ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.125 by root, Mon Apr 17 20:52:15 2006 UTC vs.
Revision 1.148 by elmex, Sat Apr 22 12:14:45 2006 UTC

21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
87 87
88package CFClient::UI::Base; 88package CFClient::UI::Base;
89 89
90use strict; 90use strict;
91 91
92use SDL::OpenGL; 92use CFClient::OpenGL;
93 93
94sub new { 94sub new {
95 my $class = shift; 95 my $class = shift;
96 96
97 my $self = bless { 97 my $self = bless {
98 x => 0, 98 x => 0,
99 y => 0, 99 y => 0,
100 z => 0, 100 z => 0,
101 w => -1,
102 h => -1,
103 @_ 101 @_
104 }, $class; 102 }, $class;
105 103
106 for (keys %$self) { 104 for (keys %$self) {
107 if (/^connect_(.*)$/) { 105 if (/^connect_(.*)$/) {
110 } 108 }
111 109
112 $self 110 $self
113} 111}
114 112
113sub show {
114 my ($self) = @_;
115
116 return if $self->{parent};
117
118 $CFClient::UI::ROOT->add ($self);
119}
120
121sub hide {
122 my ($self) = @_;
123
124 return unless $self->{parent};
125
126 $self->{parent}->remove ($self);
127}
128
115sub move { 129sub move {
116 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
117 $self->{x} = int $x; 132 $self->{x} = int $x;
118 $self->{y} = int $y; 133 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 134 $self->{z} = $z if defined $z;
135
136 $self->update;
120} 137}
121 138
122sub needs_redraw { 139sub needs_redraw {
123 0 140 0
124} 141}
125 142
126sub size_request { 143sub size_request {
127 require Carp; 144 require Carp;
128 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abstract";
129} 146}
130 147
131sub _size_allocate { 148sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
133 150
151 if ($self->{aspect}) {
152 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154
155 # use alignment to adjust x, y
156
157 $x += int +($w - $w2) * 0.5;
158 $y += int +($h - $h2) * 0.5;
159
160 ($w, $h) = ($w2, $h2);
161 }
162
163 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 164 $self->{x} = $x;
135 $self->{y} = $y; 165 $self->{y} = $y;
166 $self->update;
167 }
136 168
137 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 170 $self->{w} = $w;
140 $self->{h} = $h; 171 $self->{h} = $h;
141 172
173 $self->size_allocate ($w, $h);
174 $self->update;
142 1 175 }
143} 176}
144 177
145sub size_allocate { 178sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 179 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 180}
150 181
151# return top left coordinates 182# return top left coordinates
152sub _topleft { 183sub _topleft {
153 my ($self, $x, $y) = @_; 184 my ($self, $x, $y) = @_;
284} 315}
285 316
286sub connect { 317sub connect {
287 my ($self, $signal, $cb) = @_; 318 my ($self, $signal, $cb) = @_;
288 319
289 push @{ $self->{cb}{$signal} }, $cb; 320 push @{ $self->{signal_cb}{$signal} }, $cb;
290} 321}
291 322
292sub emit { 323sub emit {
293 my ($self, $signal, @args) = @_; 324 my ($self, $signal, @args) = @_;
294 325
326 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
295 $_->($self, @args) 327 $cb->($self, @args);
296 for @{$self->{cb}{$signal} || []}; 328 }
297} 329}
298 330
299sub DESTROY { 331sub DESTROY {
300 my ($self) = @_; 332 my ($self) = @_;
301 333
307package CFClient::UI::DrawBG; 339package CFClient::UI::DrawBG;
308 340
309our @ISA = CFClient::UI::Base::; 341our @ISA = CFClient::UI::Base::;
310 342
311use strict; 343use strict;
312use SDL::OpenGL; 344use CFClient::OpenGL;
313 345
314sub new { 346sub new {
315 my $class = shift; 347 my $class = shift;
316 348
317 # range [value, low, high, page] 349 # range [value, low, high, page]
381 $self->{children} = [ 413 $self->{children} = [
382 sort { $a->{z} <=> $b->{z} } 414 sort { $a->{z} <=> $b->{z} }
383 @{$self->{children}}, $child 415 @{$self->{children}}, $child
384 ]; 416 ];
385 417
386 $self->{w} = $self->{h} = -1; 418 $child->check_size;
387 $self->update;
388} 419}
389 420
390sub remove { 421sub remove {
391 my ($self, $widget) = @_; 422 my ($self, $child) = @_;
392 423
424 delete $child->{parent};
425
393 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
394 427
395 $self->check_size; 428 $self->check_size;
396} 429}
397 430
398sub find_widget { 431sub find_widget {
430 463
431 $class->SUPER::new (children => [$child], %arg) 464 $class->SUPER::new (children => [$child], %arg)
432} 465}
433 466
434sub add { 467sub add {
435 my ($self, $widget) = @_; 468 my ($self, $child) = @_;
436 469
437 $self->{children} = []; 470 $self->{children} = [];
438 471
439 $self->SUPER::add ($widget); 472 $self->SUPER::add ($child);
440} 473}
441 474
442sub remove { 475sub remove {
443 my ($self, $widget) = @_; 476 my ($self, $widget) = @_;
444 477
453sub size_request { 486sub size_request {
454 $_[0]{children}[0]->size_request 487 $_[0]{children}[0]->size_request
455} 488}
456 489
457sub size_allocate { 490sub size_allocate {
458 my ($self, $x, $y, $w, $h) = @_; 491 my ($self, $w, $h) = @_;
459 492
460 $self->_size_allocate ($x, $y, $w, $h) or return;
461
462 $self->{children}[0]->size_allocate (0, 0, $w, $h); 493 $self->{children}[0]->configure (0, 0, $w, $h);
463} 494}
464 495
465############################################################################# 496#############################################################################
466 497
467package CFClient::UI::Window; 498package CFClient::UI::Window;
468 499
469our @ISA = CFClient::UI::Bin::; 500our @ISA = CFClient::UI::Bin::;
470 501
471use SDL::OpenGL; 502use CFClient::OpenGL;
472 503
473sub new { 504sub new {
474 my ($class, %arg) = @_; 505 my ($class, %arg) = @_;
475 506
476 my $self = $class->SUPER::new (%arg); 507 my $self = $class->SUPER::new (%arg);
493 $self->child->draw; 524 $self->child->draw;
494 }; 525 };
495} 526}
496 527
497sub size_allocate { 528sub size_allocate {
498 my ($self, $x, $y, $w, $h) = @_; 529 my ($self, $w, $h) = @_;
499 530
500 $self->_size_allocate ($x, $y, $w, $h) or return;
501
502 $self->child->size_allocate (0, 0, $w, $h); 531 $self->child->configure (0, 0, $w, $h);
503 532
504 $self->render_chld; 533 $self->render_chld;
505} 534}
506 535
507sub _draw { 536sub _draw {
538 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 567 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
539 568
540 @$self{qw(child_w child_h)} 569 @$self{qw(child_w child_h)}
541} 570}
542 571
543sub size_allocate {
544 my ($self, $x, $y, $w, $h) = @_;
545
546 $self->_size_allocate ($x, $y, $w, $h) or return;
547}
548
549sub _draw { 572sub _draw {
550 my ($self) = @_; 573 my ($self) = @_;
551 574
552 $self->{children}[1]->draw; 575 $self->{children}[1]->draw;
553} 576}
557 580
558package CFClient::UI::Frame; 581package CFClient::UI::Frame;
559 582
560our @ISA = CFClient::UI::Bin::; 583our @ISA = CFClient::UI::Bin::;
561 584
562use SDL::OpenGL; 585use CFClient::OpenGL;
563
564sub size_request {
565 my ($self) = @_;
566 my $chld = $self->child
567 or return (0, 0);
568
569 $chld->move (2, 2);
570
571 map { $_ + 4 } $chld->size_request;
572}
573
574sub size_allocate {
575 my ($self, $x, $y, $w, $h) = @_;
576
577 $self->_size_allocate ($x, $y, $w, $h) or return;
578
579 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
580}
581
582sub _draw {
583 my ($self) = @_;
584
585 my $chld = $self->child;
586
587 my ($w, $h) = $chld->size_request;
588
589 glBegin GL_QUADS;
590 glColor 0, 0, 0;
591 glVertex 0 , 0;
592 glVertex 0 , $h + 4;
593 glVertex $w + 4 , $h + 4;
594 glVertex $w + 4 , 0;
595 glEnd;
596
597 $chld->draw;
598}
599
600#############################################################################
601
602package CFClient::UI::FancyFrame;
603
604our @ISA = CFClient::UI::Bin::;
605
606use SDL::OpenGL;
607
608my @tex =
609 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
610 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
611 586
612sub new { 587sub new {
613 my $class = shift; 588 my $class = shift;
614 589
615 # TODO: user_x, user_y, overwrite moveto?
616
617 $class->SUPER::new ( 590 my $self = $class->SUPER::new (
618 bg => [1, 1, 1, 1], 591 bg => [1, 1, 1, 1],
619 border_bg => [1, 1, 1, 1], 592 border_bg => [1, 1, 1, 1],
620 border => int $::FONTSIZE * 0.8, 593 border => 0.8,
621 @_ 594 @_
595 );
596
597 $self
598}
599
600sub set_size {
601 my ($self, $w, $h) = @_;
602 $self->{req_w} = $w;
603 $self->{req_h} = $h;
604 $self->check_size;
605}
606
607sub size_request {
608 my ($self) = @_;
609 ($self->{req_w}, $self->{req_h})
610}
611
612sub size_allocate {
613 my ($self, $w, $h) = @_;
614 $self->{w} = $w;
615 $self->{h} = $h;
616 $self->child->configure (0, 0, $w, $h);
617}
618
619sub _draw {
620 my ($self) = @_;
621
622 my ($w, $h) = ($self->{w}, $self->{h});
623
624 glEnable GL_BLEND;
625 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
626 glEnable GL_TEXTURE_2D;
627 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
628
629# glBegin GL_QUADS;
630# glColor 0, 0, 0, 0;
631# glVertex 0 , 0;
632# glVertex 0 , $h;
633# glVertex $w, $h;
634# glVertex $w, 0;
635# glEnd;
636
637
638 $self->child->draw;
639 glDisable GL_BLEND;
640 glDisable GL_TEXTURE_2D;
641}
642
643#############################################################################
644
645package CFClient::UI::FancyFrame;
646
647our @ISA = CFClient::UI::Bin::;
648
649use CFClient::OpenGL;
650
651my @tex =
652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
653 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
654
655sub new {
656 my $class = shift;
657
658 # TODO: user_x, user_y, overwrite moveto?
659
660 my $self = $class->SUPER::new (
661 bg => [1, 1, 1, 1],
662 border_bg => [1, 1, 1, 1],
663 border => 0.8,
664 @_
665 );
666
667 $self->{title} &&= new CFClient::UI::Label
668 align => 0,
669 valign => 1,
670 text => $self->{title},
671 fontsize => 1;
672
673 $self
674}
675
676sub border {
677 int $_[0]{border} * $::FONTSIZE
678}
679
680sub size_request {
681 my ($self) = @_;
682
683 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
684
685 my ($w, $h) = $self->SUPER::size_request;
686
687 (
688 $w + $self->border * 2,
689 $h + $self->border * 2,
622 ) 690 )
623} 691}
624 692
625sub size_request {
626 my ($self) = @_;
627
628 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
629
630 my ($w, $h) = $self->SUPER::size_request;
631
632 (
633 $w + $self->{border} * 2,
634 $h + $self->{border} * 2,
635 )
636}
637
638sub size_allocate { 693sub size_allocate {
639 my ($self, $x, $y, $w, $h) = @_; 694 my ($self, $w, $h) = @_;
640 695
641 $self->_size_allocate ($x, $y, $w, $h) or return;
642
643 $h -= List::Util::max 0, $self->{border} * 2; 696 $h -= List::Util::max 0, $self->border * 2;
644 $w -= List::Util::max 0, $self->{border} * 2; 697 $w -= List::Util::max 0, $self->border * 2;
645 698
699 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
700 if $self->{title};
701
646 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 702 $self->child->configure ($self->border, $self->border, $w, $h);
647} 703}
648 704
649sub button_down { 705sub button_down {
650 my ($self, $ev, $x, $y) = @_; 706 my ($self, $ev, $x, $y) = @_;
651 707
708 my $border = $self->border;
709
652 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 710 if ($x < $self->{w} && $x >= $self->{w} - $border
653 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 711 && $y < $self->{h} && $y >= $self->{h} - $border) {
654 712
655 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 713 my ($ox, $oy) = ($ev->{x}, $ev->{y});
656 my ($bw, $bh) = ($self->{w}, $self->{h}); 714 my ($bw, $bh) = ($self->{w}, $self->{h});
657 715
658 $self->{motion} = sub { 716 $self->{motion} = sub {
659 my ($ev, $x, $y) = @_; 717 my ($ev, $x, $y) = @_;
660 718
661 ($x, $y) = ($ev->motion_x, $ev->motion_y); 719 ($x, $y) = ($ev->{x}, $ev->{y});
662 720
663 $self->{user_w} = $bw + $x - $ox; 721 $self->{user_w} = $bw + $x - $ox;
664 $self->{user_h} = $bh + $y - $oy; 722 $self->{user_h} = $bh + $y - $oy;
665 $self->update; 723 $self->check_size;
666 }; 724 };
667 725
668 } elsif ($x >= 0 && $x < $self->{w} 726 } elsif ($x >= 0 && $x < $self->{w}
669 && $y >= 0 && $y < $self->{border}) { 727 && $y >= 0 && $y < $border) {
670 728
671 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 729 my ($ox, $oy) = ($ev->{x}, $ev->{y});
672 my ($bx, $by) = ($self->{x}, $self->{y}); 730 my ($bx, $by) = ($self->{x}, $self->{y});
673 731
674 $self->{motion} = sub { 732 $self->{motion} = sub {
675 my ($ev, $x, $y) = @_; 733 my ($ev, $x, $y) = @_;
676 734
677 ($x, $y) = ($ev->motion_x, $ev->motion_y); 735 ($x, $y) = ($ev->{x}, $ev->{y});
678 736
679 $self->move ($bx + $x - $ox, $by + $y - $oy); 737 $self->move ($bx + $x - $ox, $by + $y - $oy);
680 $self->update; 738 $self->update;
681 }; 739 };
682 } 740 }
703 glEnable GL_BLEND; 761 glEnable GL_BLEND;
704 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 762 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
705 glEnable GL_TEXTURE_2D; 763 glEnable GL_TEXTURE_2D;
706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 764 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
707 765
766 my $border = $self->border;
767
708 glColor @{ $self->{border_bg} }; 768 glColor @{ $self->{border_bg} };
709 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 769 $tex[1]->draw_quad (0, 0, $w, $border);
710 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 770 $tex[3]->draw_quad (0, $border, $border, $ch);
711 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 771 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
712 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 772 $tex[4]->draw_quad (0, $h - $border, $w, $border);
713 773
714 my $bg = $tex[0]; 774 my $bg = $tex[0];
715 775
716 # TODO: repeat texture not scale 776 # TODO: repeat texture not scale
717 my $rep_x = $cw / $bg->{w}; 777 my $rep_x = $cw / $bg->{w};
720 glColor @{ $self->{bg} }; 780 glColor @{ $self->{bg} };
721 781
722 $bg->{s} = $rep_x; 782 $bg->{s} = $rep_x;
723 $bg->{t} = $rep_y; 783 $bg->{t} = $rep_y;
724 $bg->{wrap_mode} = 1; 784 $bg->{wrap_mode} = 1;
725 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 785 $bg->draw_quad ($border, $border, $cw, $ch);
726 786
727 glDisable GL_TEXTURE_2D; 787 glDisable GL_TEXTURE_2D;
728 glDisable GL_BLEND; 788 glDisable GL_BLEND;
729 789
790 $self->{title}->draw if $self->{title};
730 $self->child->draw; 791 $self->child->draw;
731} 792}
732 793
733############################################################################# 794#############################################################################
734 795
736 797
737our @ISA = CFClient::UI::Base::; 798our @ISA = CFClient::UI::Base::;
738 799
739use List::Util qw(max sum); 800use List::Util qw(max sum);
740 801
741use SDL::OpenGL; 802use CFClient::OpenGL;
742 803
743sub new { 804sub new {
744 my $class = shift; 805 my $class = shift;
745 806
746 $class->SUPER::new ( 807 $class->SUPER::new (
753 my ($self, $x, $y, $child) = @_; 814 my ($self, $x, $y, $child) = @_;
754 815
755 $child->set_parent ($self); 816 $child->set_parent ($self);
756 $self->{children}[$y][$x] = $child; 817 $self->{children}[$y][$x] = $child;
757 818
758 $self->{w} = $self->{h} = -1; 819 $child->check_size;
759 $self->update;
760} 820}
761 821
762# TODO: move to container class maybe? send childs a signal on removal? 822# TODO: move to container class maybe? send childs a signal on removal?
763sub clear { 823sub clear {
764 my ($self) = @_; 824 my ($self) = @_;
799 (sum @$hs), 859 (sum @$hs),
800 ) 860 )
801} 861}
802 862
803sub size_allocate { 863sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_; 864 my ($self, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h) or return;
807 865
808 my ($ws, $hs) = $self->get_wh; 866 my ($ws, $hs) = $self->get_wh;
809 867
810 my $req_w = sum @$ws; 868 my $req_w = sum @$ws;
811 my $req_h = sum @$hs; 869 my $req_h = sum @$hs;
833 891
834 for my $c (0 .. $#$row) { 892 for my $c (0 .. $#$row) {
835 my $col_w = $ws->[$c]; 893 my $col_w = $ws->[$c];
836 894
837 if (my $widget = $row->[$c]) { 895 if (my $widget = $row->[$c]) {
838 $widget->size_allocate ($x, $y, $col_w, $row_h); 896 $widget->configure ($x, $y, $col_w, $row_h);
839 } 897 }
840 898
841 $x += $col_w; 899 $x += $col_w;
842 } 900 }
843 901
888 (List::Util::max map $_->[1], @alloc), 946 (List::Util::max map $_->[1], @alloc),
889 ) 947 )
890} 948}
891 949
892sub size_allocate { 950sub size_allocate {
893 my ($self, $x, $y, $w, $h) = @_; 951 my ($self, $w, $h) = @_;
894
895 $self->_size_allocate ($x, $y, $w, $h) or return;
896 952
897 ($h, $w) = ($w, $h); 953 ($h, $w) = ($w, $h);
898 954
899 my $children = $self->{children}; 955 my $children = $self->{children};
900 956
922 978
923 my $y = 0; 979 my $y = 0;
924 for (0 .. $#$children) { 980 for (0 .. $#$children) {
925 my $child = $children->[$_]; 981 my $child = $children->[$_];
926 my $h = $h[$_]; 982 my $h = $h[$_];
927 $child->size_allocate ($y, 0, $h, $w); 983 $child->configure ($y, 0, $h, $w);
928 984
929 $y += $h; 985 $y += $h;
930 } 986 }
931 987
932 1 988 1
950 (List::Util::sum map $_->[1], @alloc), 1006 (List::Util::sum map $_->[1], @alloc),
951 ) 1007 )
952} 1008}
953 1009
954sub size_allocate { 1010sub size_allocate {
955 my ($self, $x, $y, $w, $h) = @_; 1011 my ($self, $w, $h) = @_;
956
957 $self->_size_allocate ($x, $y, $w, $h) or return;
958 1012
959 my $children = $self->{children}; 1013 my $children = $self->{children};
960 1014
961 my @h = map +($_->size_request)[1], @$children; 1015 my @h = map +($_->size_request)[1], @$children;
962 1016
980 1034
981 my $y = 0; 1035 my $y = 0;
982 for (0 .. $#$children) { 1036 for (0 .. $#$children) {
983 my $child = $children->[$_]; 1037 my $child = $children->[$_];
984 my $h = $h[$_]; 1038 my $h = $h[$_];
985 $child->size_allocate (0, $y, $w, $h); 1039 $child->configure (0, $y, $w, $h);
986 1040
987 $y += $h; 1041 $y += $h;
988 } 1042 }
989 1043
990 1 1044 1
994 1048
995package CFClient::UI::Label; 1049package CFClient::UI::Label;
996 1050
997our @ISA = CFClient::UI::Base::; 1051our @ISA = CFClient::UI::Base::;
998 1052
999use SDL::OpenGL; 1053use CFClient::OpenGL;
1000 1054
1001sub new { 1055sub new {
1002 my ($class, %arg) = @_; 1056 my ($class, %arg) = @_;
1003 1057
1004 my $self = $class->SUPER::new ( 1058 my $self = $class->SUPER::new (
1005 fg => [1, 1, 1], 1059 fg => [1, 1, 1],
1006 fontsize => $::FONTSIZE, 1060 fontsize => 1,
1007 text => "", 1061 text => "",
1008 align => -1, 1062 align => -1,
1009 valign => -1, 1063 valign => -1,
1010 padding => 2, 1064 padding => 2,
1011 layout => new CFClient::Layout, 1065 layout => new CFClient::Layout,
1012 %arg 1066 %arg
1013 ); 1067 );
1014 1068
1015 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1069 if (exists $self->{template}) {
1070 my $layout = new CFClient::Layout;
1071 $layout->set_text (delete $self->{template});
1072 $self->{template} = $layout;
1073 }
1016 1074
1017 $self->set_text (delete $self->{text}) if exists $self->{text}; 1075 $self->set_text (delete $self->{text}) if exists $self->{text};
1018 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1076 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1019 1077
1020 $self 1078 $self
1050 1108
1051sub size_request { 1109sub size_request {
1052 my ($self) = @_; 1110 my ($self) = @_;
1053 1111
1054 $self->{layout}->set_width; 1112 $self->{layout}->set_width;
1055 $self->{layout}->set_height ($self->{fontsize}); 1113 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1056 1114
1057 my ($w, $h) = $self->{layout}->size; 1115 my ($w, $h) = $self->{layout}->size;
1116
1117 if (exists $self->{template}) {
1118 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1119
1120 my ($w2, $h2) = $self->{template}->size;
1121
1122 $w = List::Util::max $w, $w2;
1123 $h = List::Util::max $h, $h2;
1124 }
1058 1125
1059 ( 1126 (
1060 $w + $self->{padding} * 2, 1127 $w + $self->{padding} * 2,
1061 $h + $self->{padding} * 2, 1128 $h + $self->{padding} * 2,
1062 ) 1129 )
1063} 1130}
1064 1131
1065sub size_allocate { 1132sub size_allocate {
1066 my ($self, $x, $y, $w, $h) = @_; 1133 my ($self, $w, $h) = @_;
1067
1068 $self->_size_allocate ($x, $y, $w, $h) or return;
1069 1134
1070 delete $self->{texture}; 1135 delete $self->{texture};
1071} 1136}
1072 1137
1073sub update { 1138sub set_fontsize {
1074 my ($self) = @_; 1139 my ($self, $fontsize) = @_;
1075 1140
1076 delete $self->{texture}; 1141 $self->{fontsize} = $fontsize;
1077 $self->SUPER::update; 1142 $self->check_size;
1078} 1143}
1079 1144
1080sub _draw { 1145sub _draw {
1081 my ($self) = @_; 1146 my ($self) = @_;
1082 1147
1083 my $tex = $self->{texture} ||= do { 1148 my $tex = $self->{texture} ||= do {
1084 $self->{layout}->set_width ($self->{w}); 1149 $self->{layout}->set_width ($self->{w});
1085 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1150 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1086 new_from_layout CFClient::Texture $self->{layout} 1151 new_from_layout CFClient::Texture $self->{layout}
1087 }; 1152 };
1088 1153
1089 glEnable GL_BLEND; 1154 glEnable GL_BLEND;
1090 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1155 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1115 1180
1116package CFClient::UI::EntryBase; 1181package CFClient::UI::EntryBase;
1117 1182
1118our @ISA = CFClient::UI::Label::; 1183our @ISA = CFClient::UI::Label::;
1119 1184
1120use SDL; 1185use CFClient::OpenGL;
1121use SDL::OpenGL;
1122 1186
1123sub new { 1187sub new {
1124 my $class = shift; 1188 my $class = shift;
1125 1189
1126 $class->SUPER::new ( 1190 $class->SUPER::new (
1140 1204
1141 delete $self->{cur_h}; 1205 delete $self->{cur_h};
1142 1206
1143 return if $self->{text} eq $text; 1207 return if $self->{text} eq $text;
1144 1208
1209 delete $self->{texture};
1210
1145 $self->{last_activity} = $::NOW; 1211 $self->{last_activity} = $::NOW;
1146 $self->{text} = $text; 1212 $self->{text} = $text;
1147 1213
1148 $text =~ s/./*/g if $self->{hidden}; 1214 $text =~ s/./*/g if $self->{hidden};
1149 $self->{layout}->set_text ("$text "); 1215 $self->{layout}->set_text ("$text ");
1162 1228
1163 ($w + 1, $h) # add 1 for cursor 1229 ($w + 1, $h) # add 1 for cursor
1164} 1230}
1165 1231
1166sub size_allocate { 1232sub size_allocate {
1167 my ($self, $x, $y, $w, $h) = @_; 1233 my ($self, $w, $h) = @_;
1168
1169 $self->SUPER::size_allocate ($x, $y, $w, $h) or return;
1170 1234
1171 $self->_set_text ($self->{text}); 1235 $self->_set_text ($self->{text});
1172} 1236}
1173 1237
1174sub set_text { 1238sub set_text {
1180} 1244}
1181 1245
1182sub key_down { 1246sub key_down {
1183 my ($self, $ev) = @_; 1247 my ($self, $ev) = @_;
1184 1248
1185 my $mod = $ev->key_mod; 1249 my $mod = $ev->{mod};
1186 my $sym = $ev->key_sym; 1250 my $sym = $ev->{sym};
1187
1188 my $uni = $ev->key_unicode; 1251 my $uni = $ev->{unicode};
1189 1252
1190 my $text = $self->get_text; 1253 my $text = $self->get_text;
1191 1254
1192 if ($sym == SDLK_BACKSPACE) { 1255 if ($sym == 8) {
1193 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1256 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1194 } elsif ($sym == SDLK_DELETE) { 1257 } elsif ($sym == 127) {
1195 substr $text, $self->{cursor}, 1, ""; 1258 substr $text, $self->{cursor}, 1, "";
1196 } elsif ($sym == SDLK_LEFT) { 1259 } elsif ($sym == CFClient::SDLK_LEFT) {
1197 --$self->{cursor} if $self->{cursor}; 1260 --$self->{cursor} if $self->{cursor};
1198 } elsif ($sym == SDLK_RIGHT) { 1261 } elsif ($sym == CFClient::SDLK_RIGHT) {
1199 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1262 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1200 } elsif ($sym == SDLK_HOME) { 1263 } elsif ($sym == CFClient::SDLK_HOME) {
1201 $self->{cursor} = 0; 1264 $self->{cursor} = 0;
1202 } elsif ($sym == SDLK_END) { 1265 } elsif ($sym == CFClient::SDLK_END) {
1203 $self->{cursor} = length $text; 1266 $self->{cursor} = length $text;
1204 } elsif ($sym == SDLK_ESCAPE) { 1267 } elsif ($sym == 27) {
1205 $self->emit ('escape'); 1268 $self->emit ('escape');
1206 } elsif ($uni) { 1269 } elsif ($uni) {
1207 substr $text, $self->{cursor}++, 0, chr $uni; 1270 substr $text, $self->{cursor}++, 0, chr $uni;
1208 } 1271 }
1209 1272
1284 1347
1285package CFClient::UI::Entry; 1348package CFClient::UI::Entry;
1286 1349
1287our @ISA = CFClient::UI::EntryBase::; 1350our @ISA = CFClient::UI::EntryBase::;
1288 1351
1289use SDL; 1352use CFClient::OpenGL;
1290use SDL::OpenGL;
1291 1353
1292sub key_down { 1354sub key_down {
1293 my ($self, $ev) = @_; 1355 my ($self, $ev) = @_;
1294 1356
1295 my $sym = $ev->key_sym; 1357 my $sym = $ev->{sym};
1296 1358
1297 if ($sym == SDLK_RETURN) { 1359 if ($sym == 13) {
1298 $self->emit (activate => $self->get_text); 1360 $self->emit (activate => $self->get_text);
1299 $self->update; 1361 $self->update;
1300 1362
1301 } else { 1363 } else {
1302 $self->SUPER::key_down ($ev); 1364 $self->SUPER::key_down ($ev);
1308 1370
1309package CFClient::UI::Button; 1371package CFClient::UI::Button;
1310 1372
1311our @ISA = CFClient::UI::Label::; 1373our @ISA = CFClient::UI::Label::;
1312 1374
1313use SDL; 1375use CFClient::OpenGL;
1314use SDL::OpenGL;
1315 1376
1316my @tex = 1377my @tex =
1317 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1378 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1318 qw(b1_button_active.png); 1379 qw(b1_button_active.png);
1319 1380
1320sub new { 1381sub new {
1321 my $class = shift; 1382 my $class = shift;
1322 1383
1324 padding => 4, 1385 padding => 4,
1325 fg => [1, 1, 1], 1386 fg => [1, 1, 1],
1326 bg => [1, 1, 1, 0.2], 1387 bg => [1, 1, 1, 0.2],
1327 active_fg => [0, 0, 1], 1388 active_fg => [0, 0, 1],
1328 can_hover => 1, 1389 can_hover => 1,
1390 align => 0,
1391 valign => 0,
1329 @_ 1392 @_
1330 ) 1393 )
1331} 1394}
1332 1395
1333sub button_up { 1396sub button_up {
1367package CFClient::UI::CheckBox; 1430package CFClient::UI::CheckBox;
1368 1431
1369our @ISA = CFClient::UI::DrawBG::; 1432our @ISA = CFClient::UI::DrawBG::;
1370 1433
1371my @tex = 1434my @tex =
1372 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1435 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1373 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1436 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1374 1437
1375use SDL; 1438use CFClient::OpenGL;
1376use SDL::OpenGL;
1377 1439
1378sub new { 1440sub new {
1379 my $class = shift; 1441 my $class = shift;
1380 1442
1381 $class->SUPER::new ( 1443 $class->SUPER::new (
1392 my ($self) = @_; 1454 my ($self) = @_;
1393 1455
1394 ($self->{padding} * 2 + 6) x 2 1456 ($self->{padding} * 2 + 6) x 2
1395} 1457}
1396 1458
1397sub size_allocate {
1398 my ($self, $x, $y, $w, $h) = @_;
1399
1400 $self->_size_allocate ($x, $y, $w, $h) or return;
1401}
1402
1403sub button_down { 1459sub button_down {
1404 my ($self, $ev, $x, $y) = @_; 1460 my ($self, $ev, $x, $y) = @_;
1405 1461
1406 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1462 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1407 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1463 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1433 glDisable GL_BLEND; 1489 glDisable GL_BLEND;
1434} 1490}
1435 1491
1436############################################################################# 1492#############################################################################
1437 1493
1438package CFClient::UI::VGauge; 1494package CFClient::UI::Image;
1439 1495
1440our @ISA = CFClient::UI::Base::; 1496our @ISA = CFClient::UI::Base::;
1441 1497
1442use SDL::OpenGL; 1498use CFClient::OpenGL;
1499use Carp qw/confess/;
1443 1500
1444my %tex = ( 1501our %loaded_images;
1445 food => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1448 ],
1449 grace => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1452 ],
1453 hp => [
1454 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1455 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1456 ],
1457 mana => [
1458 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1459 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1460 ],
1461);
1462 1502
1463# eg. VGauge->new (gauge => 'food'), default gauge: food
1464sub new { 1503sub new {
1465 my $class = shift; 1504 my $class = shift;
1466 1505
1467 my $self = $class->SUPER::new (gauge => 'food', @_); 1506 my $self = $class->SUPER::new (@_);
1507
1508 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1509
1510 $loaded_images{$self->{image}} ||=
1511 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1512
1513 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1514
1515 Scalar::Util::weaken $loaded_images{$self->{image}};
1516
1517 $self->{aspect} = $tex->{w} / $tex->{h};
1468 1518
1469 $self 1519 $self
1470} 1520}
1471 1521
1472sub size_request { 1522sub size_request {
1473 my ($self) = @_; 1523 my ($self) = @_;
1474 1524
1475 ($self->{w}, $self->{h}) 1525 ($self->{tex}->{w}, $self->{tex}->{h})
1476}
1477
1478sub set_max {
1479 my ($self, $max) = @_;
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493} 1526}
1494 1527
1495sub _draw { 1528sub _draw {
1496 my ($self) = @_; 1529 my ($self) = @_;
1497 1530
1498 my $tex = $tex{$self->{gauge}}; 1531 my $tex = $self->{tex};
1499 1532
1500 my ($w, $h) = ($self->{w}, $self->{h}); 1533 my ($w, $h) = ($self->{w}, $self->{h});
1501 1534
1502 my $ycut = $self->{val} / ($self->{max_val} || 1); 1535 if ($self->{rot90}) {
1503 $ycut = 1 if $self->{val} > $self->{max_val}; 1536 glRotate 90, 0, 0, 1;
1537 glTranslate 0, -$self->{w}, 0;
1504 1538
1505 my $t1 = $tex->[0]; 1539 ($w, $h) = ($h, $w);
1506 my $t2 = $tex->[1]; 1540 }
1507 1541
1508 glEnable GL_BLEND; 1542 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1543 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D; 1544 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1545 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 1546
1547 $tex->draw_quad (0, 0, $w, $h);
1548
1549 glDisable GL_BLEND;
1550 glDisable GL_TEXTURE_2D;
1551}
1552
1553#############################################################################
1554
1555package CFClient::UI::VGauge;
1556
1557our @ISA = CFClient::UI::Base::;
1558
1559use CFClient::OpenGL;
1560
1561my %tex = (
1562 food => [
1563 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1564 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1565 ],
1566 grace => [
1567 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1568 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1569 ],
1570 hp => [
1571 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1572 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1573 ],
1574 mana => [
1575 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1576 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1577 ],
1578);
1579
1580# eg. VGauge->new (gauge => 'food'), default gauge: food
1581sub new {
1582 my $class = shift;
1583
1584 my $self = $class->SUPER::new (
1585 type => 'food',
1586 @_
1587 );
1588
1589 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1590
1591 $self
1592}
1593
1594sub size_request {
1595 my ($self) = @_;
1596
1597 #my $tex = $tex{$self->{type}}[0];
1598 #@$tex{qw(w h)}
1599 (0, 0)
1600}
1601
1602sub set_max {
1603 my ($self, $max) = @_;
1604
1605 $self->{max_val} = $max;
1606}
1607
1608sub set_value {
1609 my ($self, $val, $max) = @_;
1610
1611 $self->set_max ($max)
1612 if defined $max;
1613
1614 $max = $self->{max_val};
1615 $self->{val} = $val;
1616
1617 $self->update;
1618}
1619
1620sub _draw {
1621 my ($self) = @_;
1622
1623 my $tex = $tex{$self->{type}};
1624
1625 my ($w, $h) = ($self->{w}, $self->{h});
1626
1627 if ($self->{vertical}) {
1628 glRotate 90, 0, 0, 1;
1629 glTranslate 0, -$self->{w}, 0;
1630
1631 ($w, $h) = ($h, $w);
1632 }
1633
1634 my $ycut = $self->{val} / ($self->{max_val} || 1);
1635 $ycut = 1 if $self->{val} > $self->{max_val};
1636
1637 my $t1 = $tex->[0];
1638 my $t2 = $tex->[1];
1639
1640 glEnable GL_BLEND;
1641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1642 glEnable GL_TEXTURE_2D;
1643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1644
1513 my $h1 = $self->{h} - $ycut * $self->{h}; 1645 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h}; 1646 my $h2 = $ycut * $self->{h};
1515 1647
1516 my $yp = 0;
1517
1518 glBindTexture (GL_TEXTURE_2D, $t1->{name}); 1648 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin (GL_QUADS); 1649 glBegin GL_QUADS;
1520 glTexCoord (0, 0); glVertex (0 , $yp); 1650 glTexCoord 0 , 0; glVertex 0 , 0;
1521 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); 1651 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1522 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); 1652 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1523 glTexCoord (1, 0); glVertex (0 + $w, $yp); 1653 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1524 glEnd (); 1654 glEnd;
1525 1655
1526 $yp += $h1;
1527
1528 glBindTexture (GL_TEXTURE_2D, $t2->{name}); 1656 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin (GL_QUADS); 1657 glBegin GL_QUADS;
1530 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); 1658 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1531 glTexCoord (0, 1); glVertex (0 , $yp + $h2); 1659 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1532 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); 1660 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1533 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); 1661 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1534 glEnd (); 1662 glEnd;
1535 1663
1536 glDisable GL_BLEND; 1664 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D; 1665 glDisable GL_TEXTURE_2D;
1538} 1666}
1539 1667
1540############################################################################# 1668#############################################################################
1541 1669
1670package CFClient::UI::Gauge;
1671
1672our @ISA = CFClient::UI::VBox::;
1673
1674sub new {
1675 my ($class, %arg) = shift;
1676
1677 my $self = $class->SUPER::new (
1678 @_,
1679 );
1680
1681 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1682 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1683 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1684
1685 $self
1686}
1687
1688sub set_fontsize {
1689 my ($self, $fsize) = @_;
1690
1691 $self->{value}->set_fontsize ($fsize);
1692 $self->{max} ->set_fontsize ($fsize);
1693}
1694
1695sub set_value {
1696 my ($self, $val, $max) = @_;
1697
1698 $self->set_max ($max)
1699 if defined $max;
1700
1701 $self->{gauge}->set_value ($val, $max);
1702 $self->{value}->set_text ($val);
1703}
1704
1705sub set_max {
1706 my ($self, $max) = @_;
1707
1708 $self->{gauge}->set_max ($max);
1709 $self->{max}->set_text ($max);
1710}
1711
1712#############################################################################
1713
1542package CFClient::UI::Slider; 1714package CFClient::UI::Slider;
1543 1715
1544use strict; 1716use strict;
1545 1717
1546use SDL::OpenGL; 1718use CFClient::OpenGL;
1547 1719
1548our @ISA = CFClient::UI::DrawBG::; 1720our @ISA = CFClient::UI::DrawBG::;
1549 1721
1550my @tex = 1722my @tex =
1551 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1723 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1561 # TODO: calculations are off 1733 # TODO: calculations are off
1562 my $self = $class->SUPER::new ( 1734 my $self = $class->SUPER::new (
1563 fg => [1, 1, 1], 1735 fg => [1, 1, 1],
1564 active_fg => [0, 0, 0], 1736 active_fg => [0, 0, 0],
1565 range => [0, 0, 100, 10], 1737 range => [0, 0, 100, 10],
1566 req_w => 40, 1738 req_w => 20,
1567 req_h => 13, 1739 req_h => 20,
1568 vertical => 0, 1740 vertical => 0,
1569 can_hover => 1, 1741 can_hover => 1,
1570 inner_pad => 5, 1742 inner_pad => 5,
1571 @_ 1743 @_
1572 ); 1744 );
1676 1848
1677package CFClient::UI::TextView; 1849package CFClient::UI::TextView;
1678 1850
1679our @ISA = CFClient::UI::HBox::; 1851our @ISA = CFClient::UI::HBox::;
1680 1852
1681use SDL::OpenGL; 1853use CFClient::OpenGL;
1682 1854
1683sub new { 1855sub new {
1684 my $class = shift; 1856 my $class = shift;
1685 1857
1686 my $self = $class->SUPER::new ( 1858 my $self = $class->SUPER::new (
1687 req_w => $::WIDTH / 6,
1688 req_h => $::HEIGHT / 6,
1689 fontsize => $::FONTSIZE, 1859 fontsize => 1,
1690 @_, 1860 @_,
1691 1861
1692 layout => (new CFClient::Layout), 1862 layout => (new CFClient::Layout),
1693 par => [], 1863 par => [],
1694 height => 0, 1864 height => 0,
1715sub text_height { 1885sub text_height {
1716 my ($self, $text) = @_; 1886 my ($self, $text) = @_;
1717 1887
1718 my $layout = $self->{layout}; 1888 my $layout = $self->{layout};
1719 1889
1720 $layout->set_height ($self->{fontsize}); 1890 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1721 $layout->set_width ($self->{w}); 1891 $layout->set_width ($self->{w});
1722 $layout->set_text ($text); 1892 $layout->set_text ($text);
1723 1893
1724 ($layout->size)[1] 1894 ($layout->size)[1]
1725} 1895}
1729 1899
1730 $self->{need_reflow}++; 1900 $self->{need_reflow}++;
1731 $self->update; 1901 $self->update;
1732} 1902}
1733 1903
1734sub size_request {
1735 my ($self) = @_;
1736
1737 ($self->{req_w}, $self->{req_h})
1738}
1739
1740sub size_allocate { 1904sub size_allocate {
1741 my ($self, $x, $y, $w, $h) = @_; 1905 my ($self, $w, $h) = @_;
1742 1906
1743 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1907 $self->SUPER::size_allocate ($w, $h);
1744 1908
1745 $self->{layout}->set_height ($self->{fontsize}); 1909 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1746 $self->{layout}->set_width ($self->{w}); 1910 $self->{layout}->set_width ($self->{children}[0]{w});
1747 1911
1748 $self->reflow; 1912 $self->reflow;
1749 $self->update;
1750} 1913}
1751 1914
1752sub add_paragraph { 1915sub add_paragraph {
1753 my ($self, $color, $text) = @_; 1916 my ($self, $color, $text) = @_;
1754 1917
1839 2002
1840} 2003}
1841 2004
1842############################################################################# 2005#############################################################################
1843 2006
1844package CFClient::UI::MapWidget;
1845
1846use strict;
1847
1848use List::Util qw(min max);
1849
1850use SDL;
1851use SDL::OpenGL;
1852
1853our @ISA = CFClient::UI::Base::;
1854
1855sub new {
1856 my $class = shift;
1857
1858 $class->SUPER::new (
1859 z => -1,
1860 can_focus => 1,
1861 list => (glGenLists 1),
1862 @_
1863 )
1864}
1865
1866sub key_down {
1867 print "MAPKEYDOWN\n";
1868}
1869
1870sub key_up {
1871}
1872
1873sub button_down {
1874 my ($self, $ev, $x, $y) = @_;
1875
1876 $self->focus_in;
1877
1878 if ($ev->button == 2) {
1879 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1880 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1881
1882 $self->{motion} = sub {
1883 my ($ev, $x, $y) = @_;
1884
1885 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1886
1887 $::CFG->{map_shift_x} = $bw + $x - $ox;
1888 $::CFG->{map_shift_y} = $bh + $y - $oy;
1889
1890 $self->update;
1891 };
1892 }
1893}
1894
1895sub button_up {
1896 my ($self, $ev, $x, $y) = @_;
1897
1898 delete $self->{motion};
1899}
1900
1901sub mouse_motion {
1902 my ($self, $ev, $x, $y) = @_;
1903
1904 $self->{motion}->($ev, $x, $y) if $self->{motion};
1905}
1906
1907sub size_request {
1908 (
1909 1 + 32 * int $::WIDTH / 32,
1910 1 + 32 * int $::HEIGHT / 32,
1911 )
1912}
1913
1914sub update {
1915 my ($self) = @_;
1916
1917 $self->{need_update} = 1;
1918 $self->SUPER::update;
1919}
1920
1921sub draw {
1922 my ($self) = @_;
1923
1924 if (delete $self->{need_update}) {
1925 glNewList $self->{list}, GL_COMPILE;
1926
1927 if ($::MAP) {
1928 my $sw = int $::WIDTH / 32;
1929 my $sh = int $::HEIGHT / 32;
1930
1931 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1932 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1933
1934 glTranslate $sx0 - 32, $sy0 - 32, 0;
1935
1936 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1937
1938 if ($::CFG->{fow_enable}) {
1939 if ($::CFG->{fow_smooth}) { # smooth fog of war
1940 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1941 glConvolutionFilter2D
1942 GL_CONVOLUTION_2D,
1943 GL_ALPHA,
1944 3, 3,
1945 GL_ALPHA, GL_FLOAT,
1946 pack "f*",
1947 0.1, 0.1, 0.1,
1948 0.1, 0.2, 0.1,
1949 0.1, 0.1, 0.1,
1950 ;
1951 glEnable GL_CONVOLUTION_2D;
1952 }
1953
1954 $self->{fow_texture} = new CFClient::Texture
1955 w => $w,
1956 h => $h,
1957 data => $data,
1958 internalformat => GL_ALPHA,
1959 format => GL_ALPHA;
1960
1961 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1962
1963 glEnable GL_BLEND;
1964 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1965 glEnable GL_TEXTURE_2D;
1966 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1967
1968 glColor +($::CFG->{fow_intensity}) x 3, 1;
1969 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1970
1971 glDisable GL_TEXTURE_2D;
1972 glDisable GL_BLEND;
1973 }
1974
1975 # HACK BEGIN
1976 {
1977 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1978 my ($w, $h) = (250, 250);
1979
1980 glEnable GL_BLEND;
1981 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1982 glEnable GL_TEXTURE_2D;
1983 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1984
1985 $self->{mapmap_texture} =
1986 new CFClient::Texture
1987 w => $w,
1988 h => $h,
1989 data => $::MAP->mapmap ($w, $h),
1990 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1991
1992 $self->{mapmap_texture}->draw_quad (100, 100);
1993
1994 glDisable GL_TEXTURE_2D;
1995 glDisable GL_BLEND;
1996 }
1997 # HACK END
1998 }
1999
2000 glEndList;
2001 }
2002
2003 glPushMatrix;
2004 glCallList $self->{list};
2005 glPopMatrix;
2006
2007 if ($FOCUS != $self) {
2008 glColor 64/255, 64/255, 64/255;
2009 glLogicOp GL_AND;
2010 glEnable GL_COLOR_LOGIC_OP;
2011 glBegin GL_QUADS;
2012 glVertex 0, 0;
2013 glVertex 0, $::HEIGHT;
2014 glVertex $::WIDTH, $::HEIGHT;
2015 glVertex $::WIDTH, 0;
2016 glEnd;
2017 glDisable GL_COLOR_LOGIC_OP;
2018 }
2019}
2020
2021my %DIR = (
2022 SDLK_KP8, [1, "north"],
2023 SDLK_KP9, [2, "northeast"],
2024 SDLK_KP6, [3, "east"],
2025 SDLK_KP3, [4, "southeast"],
2026 SDLK_KP2, [5, "south"],
2027 SDLK_KP1, [6, "southwest"],
2028 SDLK_KP4, [7, "west"],
2029 SDLK_KP7, [8, "northwest"],
2030
2031 SDLK_UP, [1, "north"],
2032 SDLK_RIGHT, [3, "east"],
2033 SDLK_DOWN, [5, "south"],
2034 SDLK_LEFT, [7, "west"],
2035);
2036
2037sub key_down {
2038 my ($self, $ev) = @_;
2039
2040 my $mod = $ev->key_mod;
2041 my $sym = $ev->key_sym;
2042
2043 if ($sym == SDLK_KP5) {
2044 $::CONN->user_send ("stay fire");
2045 } elsif ($sym == SDLK_a) {
2046 $::CONN->user_send ("apply");
2047 } elsif ($sym == SDLK_QUOTE) {
2048 $self->emit ('activate_console');
2049 } elsif ($sym == SDLK_SLASH) {
2050 $self->emit ('activate_console' => '/');
2051 } elsif (exists $DIR{$sym}) {
2052 if ($mod & KMOD_SHIFT) {
2053 $self->{shft}++;
2054 $::CONN->user_send ("fire $DIR{$sym}[0]");
2055 } elsif ($mod & KMOD_CTRL) {
2056 $self->{ctrl}++;
2057 $::CONN->user_send ("run $DIR{$sym}[0]");
2058 } else {
2059 $::CONN->user_send ("$DIR{$sym}[1]");
2060 }
2061 }
2062}
2063
2064sub key_up {
2065 my ($self, $ev) = @_;
2066
2067 my $mod = $ev->key_mod;
2068 my $sym = $ev->key_sym;
2069
2070 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2071 $::CONN->user_send ("fire_stop");
2072 }
2073 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2074 $::CONN->user_send ("run_stop");
2075 }
2076}
2077
2078#############################################################################
2079
2080package CFClient::UI::Animator; 2007package CFClient::UI::Animator;
2081 2008
2082use SDL::OpenGL; 2009use CFClient::OpenGL;
2083 2010
2084our @ISA = CFClient::UI::Bin::; 2011our @ISA = CFClient::UI::Bin::;
2085 2012
2086sub moveto { 2013sub moveto {
2087 my ($self, $x, $y) = @_; 2014 my ($self, $x, $y) = @_;
2160 2087
2161package CFClient::UI::Root; 2088package CFClient::UI::Root;
2162 2089
2163our @ISA = CFClient::UI::Container::; 2090our @ISA = CFClient::UI::Container::;
2164 2091
2165use SDL::OpenGL; 2092use CFClient::OpenGL;
2166 2093
2167sub check_size { 2094sub check_size {
2168 my ($self) = @_; 2095 my ($self) = @_;
2169 2096
2170 $self->size_allocate (0, 0, $::WITH, $::HEIGHT); 2097 $self->configure (0, 0, $::WITH, $::HEIGHT);
2171} 2098}
2172 2099
2173sub size_request { 2100sub size_request {
2174 ($::WIDTH, $::HEIGHT) 2101 ($::WIDTH, $::HEIGHT)
2175} 2102}
2176 2103
2177sub size_allocate { 2104sub configure {
2178 my ($self, $x, $y, $w, $h) = @_; 2105 my ($self, $x, $y, $w, $h) = @_;
2179 2106
2180 $self->_size_allocate ($x, $y, $w, $h); 2107 $self->SUPER::configure ($x, $y, $w, $h);
2181 2108
2182 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2109 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2183 for @{$self->{children}}; 2110 for @{$self->{children}};
2184} 2111}
2185 2112
2186sub _topleft { 2113sub _topleft {
2187 my ($self, $x, $y) = @_; 2114 my ($self, $x, $y) = @_;
2195 $self->check_size; 2122 $self->check_size;
2196 ::refresh (); 2123 ::refresh ();
2197} 2124}
2198 2125
2199sub add { 2126sub add {
2200 my ($self, $widget) = @_; 2127 my ($self, $child) = @_;
2201 2128
2129 # integerize window positions
2130 $child->{x} = int $child->{x};
2131 $child->{y} = int $child->{y};
2132
2202 $self->SUPER::add ($widget); 2133 $self->SUPER::add ($child);
2203
2204 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2205} 2134}
2206 2135
2207sub on_refresh { 2136sub on_refresh {
2208 my ($self, $id, $cb) = @_; 2137 my ($self, $id, $cb) = @_;
2209 2138

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines