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.130 by root, Tue Apr 18 01:51:06 2006 UTC vs.
Revision 1.149 by root, Sat Apr 22 23:11:32 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) = @_;
117 131
118 $self->{x} = int $x; 132 $self->{x} = int $x;
119 $self->{y} = int $y; 133 $self->{y} = int $y;
126 0 140 0
127} 141}
128 142
129sub size_request { 143sub size_request {
130 require Carp; 144 require Carp;
131 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abstract";
132} 146}
133 147
134sub configure { 148sub configure {
135 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
136 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) {
137 $self->{x} = $x; 164 $self->{x} = $x;
138 $self->{y} = $y; 165 $self->{y} = $y;
166 $self->update;
167 }
139 168
140 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
141
142 $self->{w} = $w; 170 $self->{w} = $w;
143 $self->{h} = $h; 171 $self->{h} = $h;
144 172
145 $self->size_allocate ($w, $h); 173 $self->size_allocate ($w, $h);
174 $self->update;
175 }
146} 176}
147 177
148sub size_allocate { 178sub size_allocate {
149 # nothing to be done 179 # nothing to be done
150} 180}
264} 294}
265 295
266sub check_size { 296sub check_size {
267 my ($self) = @_; 297 my ($self) = @_;
268 298
299 return unless $self->{parent};
300
269 my ($w, $h) = $self->size_request; 301 my ($w, $h) = $self->size_request;
270 302
271 if ($w != $self->{req_w} || $h != $self->{req_h}) { 303 if ($w != $self->{req_w} || $h != $self->{req_h}) {
272 $self->{req_w} = $w; 304 $self->{req_w} = $w;
273 $self->{req_h} = $h; 305 $self->{req_h} = $h;
274 306
275 $self->{parent}->check_size 307 $self->{parent}->check_size;
276 if $self->{parent};
277
278 $self->size_allocate ($w, $h);
279 $self->update;
280 } 308 }
281} 309}
282 310
283sub update { 311sub update {
284 my ($self) = @_; 312 my ($self) = @_;
288} 316}
289 317
290sub connect { 318sub connect {
291 my ($self, $signal, $cb) = @_; 319 my ($self, $signal, $cb) = @_;
292 320
293 push @{ $self->{cb}{$signal} }, $cb; 321 push @{ $self->{signal_cb}{$signal} }, $cb;
294} 322}
295 323
296sub emit { 324sub emit {
297 my ($self, $signal, @args) = @_; 325 my ($self, $signal, @args) = @_;
298 326
327 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
299 $_->($self, @args) 328 $cb->($self, @args);
300 for @{$self->{cb}{$signal} || []}; 329 }
301} 330}
302 331
303sub DESTROY { 332sub DESTROY {
304 my ($self) = @_; 333 my ($self) = @_;
305 334
311package CFClient::UI::DrawBG; 340package CFClient::UI::DrawBG;
312 341
313our @ISA = CFClient::UI::Base::; 342our @ISA = CFClient::UI::Base::;
314 343
315use strict; 344use strict;
316use SDL::OpenGL; 345use CFClient::OpenGL;
317 346
318sub new { 347sub new {
319 my $class = shift; 348 my $class = shift;
320 349
321 # range [value, low, high, page] 350 # range [value, low, high, page]
385 $self->{children} = [ 414 $self->{children} = [
386 sort { $a->{z} <=> $b->{z} } 415 sort { $a->{z} <=> $b->{z} }
387 @{$self->{children}}, $child 416 @{$self->{children}}, $child
388 ]; 417 ];
389 418
390 $self->{w} = $self->{h} = -1;
391
392 $child->check_size; 419 $child->check_size;
393} 420}
394 421
395sub remove { 422sub remove {
396 my ($self, $widget) = @_; 423 my ($self, $child) = @_;
397 424
425 delete $child->{parent};
426
398 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 427 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
399 428
400 $self->check_size; 429 $self->check_size;
401} 430}
402 431
403sub find_widget { 432sub find_widget {
435 464
436 $class->SUPER::new (children => [$child], %arg) 465 $class->SUPER::new (children => [$child], %arg)
437} 466}
438 467
439sub add { 468sub add {
440 my ($self, $widget) = @_; 469 my ($self, $child) = @_;
441 470
442 $self->{children} = []; 471 $self->{children} = [];
443 472
444 $self->SUPER::add ($widget); 473 $self->SUPER::add ($child);
445} 474}
446 475
447sub remove { 476sub remove {
448 my ($self, $widget) = @_; 477 my ($self, $widget) = @_;
449 478
469 498
470package CFClient::UI::Window; 499package CFClient::UI::Window;
471 500
472our @ISA = CFClient::UI::Bin::; 501our @ISA = CFClient::UI::Bin::;
473 502
474use SDL::OpenGL; 503use CFClient::OpenGL;
475 504
476sub new { 505sub new {
477 my ($class, %arg) = @_; 506 my ($class, %arg) = @_;
478 507
479 my $self = $class->SUPER::new (%arg); 508 my $self = $class->SUPER::new (%arg);
533sub new { die } 562sub new { die }
534 563
535sub size_request { 564sub size_request {
536 my ($self) = @_; 565 my ($self) = @_;
537 566
538 @$self{qw(child_w child_h)} = $self->child->size_request; 567 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
539 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 568 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
540 569
541 @$self{qw(child_w child_h)} 570 @$self{qw(child_w child_h)}
542} 571}
543 572
552 581
553package CFClient::UI::Frame; 582package CFClient::UI::Frame;
554 583
555our @ISA = CFClient::UI::Bin::; 584our @ISA = CFClient::UI::Bin::;
556 585
557use SDL::OpenGL; 586use CFClient::OpenGL;
558
559sub size_request {
560 my ($self) = @_;
561 my $chld = $self->child
562 or return (0, 0);
563
564 $chld->move (2, 2);
565
566 map { $_ + 4 } $chld->size_request;
567}
568
569sub size_allocate {
570 my ($self, $x, $y, $w, $h) = @_;
571
572 $self->child->configure (2, 2, $w - 4, $h - 4);
573}
574
575sub _draw {
576 my ($self) = @_;
577
578 my $chld = $self->child;
579
580 my ($w, $h) = $chld->size_request;
581
582 glBegin GL_QUADS;
583 glColor 0, 0, 0;
584 glVertex 0 , 0;
585 glVertex 0 , $h + 4;
586 glVertex $w + 4 , $h + 4;
587 glVertex $w + 4 , 0;
588 glEnd;
589
590 $chld->draw;
591}
592
593#############################################################################
594
595package CFClient::UI::FancyFrame;
596
597our @ISA = CFClient::UI::Bin::;
598
599use SDL::OpenGL;
600
601my @tex =
602 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
603 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
604 587
605sub new { 588sub new {
606 my $class = shift; 589 my $class = shift;
607 590
608 # TODO: user_x, user_y, overwrite moveto?
609
610 $class->SUPER::new ( 591 my $self = $class->SUPER::new (
611 bg => [1, 1, 1, 1], 592 bg => [1, 1, 1, 1],
612 border_bg => [1, 1, 1, 1], 593 border_bg => [1, 1, 1, 1],
613 border => int $::FONTSIZE * 0.8, 594 border => 0.8,
614 @_ 595 @_
615 ) 596 );
597
598 $self
599}
600
601sub set_size {
602 my ($self, $w, $h) = @_;
603 $self->{req_w} = $w;
604 $self->{req_h} = $h;
605 $self->check_size;
616} 606}
617 607
618sub size_request { 608sub size_request {
619 my ($self) = @_; 609 my ($self) = @_;
620 610 ($self->{req_w}, $self->{req_h})
621 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
622
623 my ($w, $h) = $self->SUPER::size_request;
624
625 (
626 $w + $self->{border} * 2,
627 $h + $self->{border} * 2,
628 )
629} 611}
630 612
631sub size_allocate { 613sub size_allocate {
632 my ($self, $w, $h) = @_; 614 my ($self, $w, $h) = @_;
615 $self->{w} = $w;
616 $self->{h} = $h;
617 $self->child->configure (0, 0, $w, $h);
618}
633 619
620sub _draw {
621 my ($self) = @_;
622
623 my ($w, $h) = ($self->{w}, $self->{h});
624
625 glEnable GL_BLEND;
626 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
627 glEnable GL_TEXTURE_2D;
628 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
629
630# glBegin GL_QUADS;
631# glColor 0, 0, 0, 0;
632# glVertex 0 , 0;
633# glVertex 0 , $h;
634# glVertex $w, $h;
635# glVertex $w, 0;
636# glEnd;
637
638
639 $self->child->draw;
640 glDisable GL_BLEND;
641 glDisable GL_TEXTURE_2D;
642}
643
644#############################################################################
645
646package CFClient::UI::FancyFrame;
647
648our @ISA = CFClient::UI::Bin::;
649
650use CFClient::OpenGL;
651
652my @tex =
653 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
654 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
655
656sub new {
657 my $class = shift;
658
659 # TODO: user_x, user_y, overwrite moveto?
660
661 my $self = $class->SUPER::new (
662 bg => [1, 1, 1, 1],
663 border_bg => [1, 1, 1, 1],
664 border => 0.8,
665 @_
666 );
667
668 $self->{title} &&= new CFClient::UI::Label
669 align => 0,
670 valign => 1,
671 text => $self->{title},
672 fontsize => 1;
673
674 $self
675}
676
677sub border {
678 int $_[0]{border} * $::FONTSIZE
679}
680
681sub size_request {
682 my ($self) = @_;
683
684 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
685
686 my ($w, $h) = $self->SUPER::size_request;
687
688 (
689 $w + $self->border * 2,
690 $h + $self->border * 2,
691 )
692}
693
694sub size_allocate {
695 my ($self, $w, $h) = @_;
696
634 $h -= List::Util::max 0, $self->{border} * 2; 697 $h -= List::Util::max 0, $self->border * 2;
635 $w -= List::Util::max 0, $self->{border} * 2; 698 $w -= List::Util::max 0, $self->border * 2;
636 699
700 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
701 if $self->{title};
702
637 $self->child->configure ($self->{border}, $self->{border}, $w, $h); 703 $self->child->configure ($self->border, $self->border, $w, $h);
638} 704}
639 705
640sub button_down { 706sub button_down {
641 my ($self, $ev, $x, $y) = @_; 707 my ($self, $ev, $x, $y) = @_;
642 708
709 my $border = $self->border;
710
643 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 711 if ($x < $self->{w} && $x >= $self->{w} - $border
644 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 712 && $y < $self->{h} && $y >= $self->{h} - $border) {
645 713
646 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 714 my ($ox, $oy) = ($ev->{x}, $ev->{y});
647 my ($bw, $bh) = ($self->{w}, $self->{h}); 715 my ($bw, $bh) = ($self->{w}, $self->{h});
648 716
649 $self->{motion} = sub { 717 $self->{motion} = sub {
650 my ($ev, $x, $y) = @_; 718 my ($ev, $x, $y) = @_;
651 719
652 ($x, $y) = ($ev->motion_x, $ev->motion_y); 720 ($x, $y) = ($ev->{x}, $ev->{y});
653 721
654 $self->{user_w} = $bw + $x - $ox; 722 $self->{user_w} = $bw + $x - $ox;
655 $self->{user_h} = $bh + $y - $oy; 723 $self->{user_h} = $bh + $y - $oy;
656 $self->check_size; 724 $self->check_size;
657 }; 725 };
658 726
659 } elsif ($x >= 0 && $x < $self->{w} 727 } elsif ($x >= 0 && $x < $self->{w}
660 && $y >= 0 && $y < $self->{border}) { 728 && $y >= 0 && $y < $border) {
661 729
662 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 730 my ($ox, $oy) = ($ev->{x}, $ev->{y});
663 my ($bx, $by) = ($self->{x}, $self->{y}); 731 my ($bx, $by) = ($self->{x}, $self->{y});
664 732
665 $self->{motion} = sub { 733 $self->{motion} = sub {
666 my ($ev, $x, $y) = @_; 734 my ($ev, $x, $y) = @_;
667 735
668 ($x, $y) = ($ev->motion_x, $ev->motion_y); 736 ($x, $y) = ($ev->{x}, $ev->{y});
669 737
670 $self->move ($bx + $x - $ox, $by + $y - $oy); 738 $self->move ($bx + $x - $ox, $by + $y - $oy);
671 $self->update; 739 $self->update;
672 }; 740 };
673 } 741 }
694 glEnable GL_BLEND; 762 glEnable GL_BLEND;
695 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 763 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
696 glEnable GL_TEXTURE_2D; 764 glEnable GL_TEXTURE_2D;
697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 765 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
698 766
767 my $border = $self->border;
768
699 glColor @{ $self->{border_bg} }; 769 glColor @{ $self->{border_bg} };
700 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 770 $tex[1]->draw_quad (0, 0, $w, $border);
701 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 771 $tex[3]->draw_quad (0, $border, $border, $ch);
702 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 772 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
703 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 773 $tex[4]->draw_quad (0, $h - $border, $w, $border);
704 774
705 my $bg = $tex[0]; 775 my $bg = $tex[0];
706 776
707 # TODO: repeat texture not scale 777 # TODO: repeat texture not scale
708 my $rep_x = $cw / $bg->{w}; 778 my $rep_x = $cw / $bg->{w};
711 glColor @{ $self->{bg} }; 781 glColor @{ $self->{bg} };
712 782
713 $bg->{s} = $rep_x; 783 $bg->{s} = $rep_x;
714 $bg->{t} = $rep_y; 784 $bg->{t} = $rep_y;
715 $bg->{wrap_mode} = 1; 785 $bg->{wrap_mode} = 1;
716 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 786 $bg->draw_quad ($border, $border, $cw, $ch);
717 787
718 glDisable GL_TEXTURE_2D; 788 glDisable GL_TEXTURE_2D;
719 glDisable GL_BLEND; 789 glDisable GL_BLEND;
720 790
791 $self->{title}->draw if $self->{title};
721 $self->child->draw; 792 $self->child->draw;
722} 793}
723 794
724############################################################################# 795#############################################################################
725 796
727 798
728our @ISA = CFClient::UI::Base::; 799our @ISA = CFClient::UI::Base::;
729 800
730use List::Util qw(max sum); 801use List::Util qw(max sum);
731 802
732use SDL::OpenGL; 803use CFClient::OpenGL;
733 804
734sub new { 805sub new {
735 my $class = shift; 806 my $class = shift;
736 807
737 $class->SUPER::new ( 808 $class->SUPER::new (
744 my ($self, $x, $y, $child) = @_; 815 my ($self, $x, $y, $child) = @_;
745 816
746 $child->set_parent ($self); 817 $child->set_parent ($self);
747 $self->{children}[$y][$x] = $child; 818 $self->{children}[$y][$x] = $child;
748 819
749 $self->{w} = $self->{h} = -1;
750
751 $child->check_size; 820 $child->check_size;
752} 821}
753 822
754# TODO: move to container class maybe? send childs a signal on removal? 823# TODO: move to container class maybe? send childs a signal on removal?
755sub clear { 824sub clear {
769 or next; 838 or next;
770 839
771 for my $x (0 .. $#$row) { 840 for my $x (0 .. $#$row) {
772 my $widget = $row->[$x] 841 my $widget = $row->[$x]
773 or next; 842 or next;
774 my ($w, $h) = $widget->size_request; 843 my ($w, $h) = @$widget{qw(req_w req_h)};
775 844
776 $w[$x] = max $w[$x], $w; 845 $w[$x] = max $w[$x], $w;
777 $h[$y] = max $h[$y], $h; 846 $h[$y] = max $h[$y], $h;
778 } 847 }
779 } 848 }
884 953
885 ($h, $w) = ($w, $h); 954 ($h, $w) = ($w, $h);
886 955
887 my $children = $self->{children}; 956 my $children = $self->{children};
888 957
889 my @h = map +($_->size_request)[0], @$children; 958 my @h = map $_->{req_w}, @$children;
890 959
891 my $req_h = List::Util::sum @h; 960 my $req_h = List::Util::sum @h;
892 961
893 if ($req_h > $h) { 962 if ($req_h > $h) {
894 # ah well, not enough space 963 # ah well, not enough space
942sub size_allocate { 1011sub size_allocate {
943 my ($self, $w, $h) = @_; 1012 my ($self, $w, $h) = @_;
944 1013
945 my $children = $self->{children}; 1014 my $children = $self->{children};
946 1015
947 my @h = map +($_->size_request)[1], @$children; 1016 my @h = map $_->{req_h}, @$children;
948 1017
949 my $req_h = List::Util::sum @h; 1018 my $req_h = List::Util::sum @h;
950 1019
951 if ($req_h > $h) { 1020 if ($req_h > $h) {
952 # ah well, not enough space 1021 # ah well, not enough space
980 1049
981package CFClient::UI::Label; 1050package CFClient::UI::Label;
982 1051
983our @ISA = CFClient::UI::Base::; 1052our @ISA = CFClient::UI::Base::;
984 1053
985use SDL::OpenGL; 1054use CFClient::OpenGL;
986 1055
987sub new { 1056sub new {
988 my ($class, %arg) = @_; 1057 my ($class, %arg) = @_;
989 1058
990 my $self = $class->SUPER::new ( 1059 my $self = $class->SUPER::new (
991 fg => [1, 1, 1], 1060 fg => [1, 1, 1],
992 fontsize => $::FONTSIZE, 1061 fontsize => 1,
993 text => "", 1062 text => "",
994 align => -1, 1063 align => -1,
995 valign => -1, 1064 valign => -1,
996 padding => 2, 1065 padding => 2,
997 layout => new CFClient::Layout, 1066 layout => new CFClient::Layout,
998 %arg 1067 %arg
999 ); 1068 );
1000 1069
1001 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1070 if (exists $self->{template}) {
1071 my $layout = new CFClient::Layout;
1072 $layout->set_text (delete $self->{template});
1073 $self->{template} = $layout;
1074 }
1002 1075
1003 $self->set_text (delete $self->{text}) if exists $self->{text}; 1076 $self->set_text (delete $self->{text}) if exists $self->{text};
1004 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1077 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1005 1078
1006 $self 1079 $self
1020 my ($self, $text) = @_; 1093 my ($self, $text) = @_;
1021 1094
1022 $self->{layout}->set_text ($text); 1095 $self->{layout}->set_text ($text);
1023 1096
1024 delete $self->{texture}; 1097 delete $self->{texture};
1098 $self->check_size;
1025 $self->update; 1099 $self->update;
1026} 1100}
1027 1101
1028sub set_markup { 1102sub set_markup {
1029 my ($self, $markup) = @_; 1103 my ($self, $markup) = @_;
1030 1104
1031 $self->{layout}->set_markup ($markup); 1105 $self->{layout}->set_markup ($markup);
1032 1106
1033 delete $self->{texture}; 1107 delete $self->{texture};
1108 $self->check_size;
1034 $self->update; 1109 $self->update;
1035} 1110}
1036 1111
1037sub size_request { 1112sub size_request {
1038 my ($self) = @_; 1113 my ($self) = @_;
1039 1114
1040 $self->{layout}->set_width; 1115 $self->{layout}->set_width;
1041 $self->{layout}->set_height ($self->{fontsize}); 1116 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1042 1117
1043 my ($w, $h) = $self->{layout}->size; 1118 my ($w, $h) = $self->{layout}->size;
1119
1120 if (exists $self->{template}) {
1121 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1122
1123 my ($w2, $h2) = $self->{template}->size;
1124
1125 $w = List::Util::max $w, $w2;
1126 $h = List::Util::max $h, $h2;
1127 }
1044 1128
1045 ( 1129 (
1046 $w + $self->{padding} * 2, 1130 $w + $self->{padding} * 2,
1047 $h + $self->{padding} * 2, 1131 $h + $self->{padding} * 2,
1048 ) 1132 )
1052 my ($self, $w, $h) = @_; 1136 my ($self, $w, $h) = @_;
1053 1137
1054 delete $self->{texture}; 1138 delete $self->{texture};
1055} 1139}
1056 1140
1057sub update { 1141sub set_fontsize {
1058 my ($self) = @_; 1142 my ($self, $fontsize) = @_;
1059 1143
1060 delete $self->{texture}; 1144 $self->{fontsize} = $fontsize;
1061 $self->SUPER::update; 1145 $self->check_size;
1062} 1146}
1063 1147
1064sub _draw { 1148sub _draw {
1065 my ($self) = @_; 1149 my ($self) = @_;
1066 1150
1067 my $tex = $self->{texture} ||= do { 1151 my $tex = $self->{texture} ||= do {
1068 $self->{layout}->set_width ($self->{w}); 1152 $self->{layout}->set_width ($self->{w});
1069 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1153 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1070 new_from_layout CFClient::Texture $self->{layout} 1154 new_from_layout CFClient::Texture $self->{layout}
1071 }; 1155 };
1072 1156
1073 glEnable GL_BLEND; 1157 glEnable GL_BLEND;
1074 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1158 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1099 1183
1100package CFClient::UI::EntryBase; 1184package CFClient::UI::EntryBase;
1101 1185
1102our @ISA = CFClient::UI::Label::; 1186our @ISA = CFClient::UI::Label::;
1103 1187
1104use SDL; 1188use CFClient::OpenGL;
1105use SDL::OpenGL;
1106 1189
1107sub new { 1190sub new {
1108 my $class = shift; 1191 my $class = shift;
1109 1192
1110 $class->SUPER::new ( 1193 $class->SUPER::new (
1124 1207
1125 delete $self->{cur_h}; 1208 delete $self->{cur_h};
1126 1209
1127 return if $self->{text} eq $text; 1210 return if $self->{text} eq $text;
1128 1211
1212 delete $self->{texture};
1213
1129 $self->{last_activity} = $::NOW; 1214 $self->{last_activity} = $::NOW;
1130 $self->{text} = $text; 1215 $self->{text} = $text;
1131 1216
1132 $text =~ s/./*/g if $self->{hidden}; 1217 $text =~ s/./*/g if $self->{hidden};
1133 $self->{layout}->set_text ("$text "); 1218 $self->{layout}->set_text ("$text ");
1148} 1233}
1149 1234
1150sub size_allocate { 1235sub size_allocate {
1151 my ($self, $w, $h) = @_; 1236 my ($self, $w, $h) = @_;
1152 1237
1153 $self->_set_text ($self->{text}); 1238 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1154} 1239}
1155 1240
1156sub set_text { 1241sub set_text {
1157 my ($self, $text) = @_; 1242 my ($self, $text) = @_;
1158 1243
1162} 1247}
1163 1248
1164sub key_down { 1249sub key_down {
1165 my ($self, $ev) = @_; 1250 my ($self, $ev) = @_;
1166 1251
1167 my $mod = $ev->key_mod; 1252 my $mod = $ev->{mod};
1168 my $sym = $ev->key_sym; 1253 my $sym = $ev->{sym};
1169
1170 my $uni = $ev->key_unicode; 1254 my $uni = $ev->{unicode};
1171 1255
1172 my $text = $self->get_text; 1256 my $text = $self->get_text;
1173 1257
1174 if ($sym == SDLK_BACKSPACE) { 1258 if ($sym == 8) {
1175 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1259 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1176 } elsif ($sym == SDLK_DELETE) { 1260 } elsif ($sym == 127) {
1177 substr $text, $self->{cursor}, 1, ""; 1261 substr $text, $self->{cursor}, 1, "";
1178 } elsif ($sym == SDLK_LEFT) { 1262 } elsif ($sym == CFClient::SDLK_LEFT) {
1179 --$self->{cursor} if $self->{cursor}; 1263 --$self->{cursor} if $self->{cursor};
1180 } elsif ($sym == SDLK_RIGHT) { 1264 } elsif ($sym == CFClient::SDLK_RIGHT) {
1181 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1265 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1182 } elsif ($sym == SDLK_HOME) { 1266 } elsif ($sym == CFClient::SDLK_HOME) {
1183 $self->{cursor} = 0; 1267 $self->{cursor} = 0;
1184 } elsif ($sym == SDLK_END) { 1268 } elsif ($sym == CFClient::SDLK_END) {
1185 $self->{cursor} = length $text; 1269 $self->{cursor} = length $text;
1186 } elsif ($sym == SDLK_ESCAPE) { 1270 } elsif ($sym == 27) {
1187 $self->emit ('escape'); 1271 $self->emit ('escape');
1188 } elsif ($uni) { 1272 } elsif ($uni) {
1189 substr $text, $self->{cursor}++, 0, chr $uni; 1273 substr $text, $self->{cursor}++, 0, chr $uni;
1190 } 1274 }
1191 1275
1266 1350
1267package CFClient::UI::Entry; 1351package CFClient::UI::Entry;
1268 1352
1269our @ISA = CFClient::UI::EntryBase::; 1353our @ISA = CFClient::UI::EntryBase::;
1270 1354
1271use SDL; 1355use CFClient::OpenGL;
1272use SDL::OpenGL;
1273 1356
1274sub key_down { 1357sub key_down {
1275 my ($self, $ev) = @_; 1358 my ($self, $ev) = @_;
1276 1359
1277 my $sym = $ev->key_sym; 1360 my $sym = $ev->{sym};
1278 1361
1279 if ($sym == SDLK_RETURN) { 1362 if ($sym == 13) {
1280 $self->emit (activate => $self->get_text); 1363 $self->emit (activate => $self->get_text);
1281 $self->update; 1364 $self->update;
1282 1365
1283 } else { 1366 } else {
1284 $self->SUPER::key_down ($ev); 1367 $self->SUPER::key_down ($ev);
1290 1373
1291package CFClient::UI::Button; 1374package CFClient::UI::Button;
1292 1375
1293our @ISA = CFClient::UI::Label::; 1376our @ISA = CFClient::UI::Label::;
1294 1377
1295use SDL; 1378use CFClient::OpenGL;
1296use SDL::OpenGL;
1297 1379
1298my @tex = 1380my @tex =
1299 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1381 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1300 qw(b1_button_active.png); 1382 qw(b1_button_active.png);
1301 1383
1302sub new { 1384sub new {
1303 my $class = shift; 1385 my $class = shift;
1304 1386
1351package CFClient::UI::CheckBox; 1433package CFClient::UI::CheckBox;
1352 1434
1353our @ISA = CFClient::UI::DrawBG::; 1435our @ISA = CFClient::UI::DrawBG::;
1354 1436
1355my @tex = 1437my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1357 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1439 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1358 1440
1359use SDL; 1441use CFClient::OpenGL;
1360use SDL::OpenGL;
1361 1442
1362sub new { 1443sub new {
1363 my $class = shift; 1444 my $class = shift;
1364 1445
1365 $class->SUPER::new ( 1446 $class->SUPER::new (
1411 glDisable GL_BLEND; 1492 glDisable GL_BLEND;
1412} 1493}
1413 1494
1414############################################################################# 1495#############################################################################
1415 1496
1416package CFClient::UI::VGauge; 1497package CFClient::UI::Image;
1417 1498
1418our @ISA = CFClient::UI::Base::; 1499our @ISA = CFClient::UI::Base::;
1419 1500
1420use SDL::OpenGL; 1501use CFClient::OpenGL;
1502use Carp qw/confess/;
1421 1503
1422my %tex = ( 1504our %loaded_images;
1423 food => [
1424 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1425 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1426 ],
1427 grace => [
1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1429 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1430 ],
1431 hp => [
1432 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1433 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1434 ],
1435 mana => [
1436 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1437 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1438 ],
1439);
1440 1505
1441# eg. VGauge->new (gauge => 'food'), default gauge: food
1442sub new { 1506sub new {
1443 my $class = shift; 1507 my $class = shift;
1444 1508
1445 my $self = $class->SUPER::new (gauge => 'food', @_); 1509 my $self = $class->SUPER::new (@_);
1510
1511 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1512
1513 $loaded_images{$self->{image}} ||=
1514 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1515
1516 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1517
1518 Scalar::Util::weaken $loaded_images{$self->{image}};
1519
1520 $self->{aspect} = $tex->{w} / $tex->{h};
1446 1521
1447 $self 1522 $self
1448} 1523}
1449 1524
1450sub size_request { 1525sub size_request {
1451 my ($self) = @_; 1526 my ($self) = @_;
1452 1527
1453 my $tex = $tex{$self->{gauge}}[0]; 1528 ($self->{tex}->{w}, $self->{tex}->{h})
1454
1455 @$tex{qw(w h)}
1456}
1457
1458sub set_max {
1459 my ($self, $max) = @_;
1460
1461 $self->{max_val} = $max;
1462}
1463
1464sub set_value {
1465 my ($self, $val, $max) = @_;
1466
1467 $self->set_max ($max)
1468 if defined $max;
1469
1470 $max = $self->{max_val};
1471 $self->{val} = $val;
1472
1473 $self->update;
1474} 1529}
1475 1530
1476sub _draw { 1531sub _draw {
1477 my ($self) = @_; 1532 my ($self) = @_;
1478 1533
1479 my $tex = $tex{$self->{gauge}}; 1534 my $tex = $self->{tex};
1480 1535
1481 my ($w, $h) = ($self->{w}, $self->{h}); 1536 my ($w, $h) = ($self->{w}, $self->{h});
1482 1537
1483 my $ycut = $self->{val} / ($self->{max_val} || 1); 1538 if ($self->{rot90}) {
1484 $ycut = 1 if $self->{val} > $self->{max_val}; 1539 glRotate 90, 0, 0, 1;
1540 glTranslate 0, -$self->{w}, 0;
1485 1541
1486 my $t1 = $tex->[0]; 1542 ($w, $h) = ($h, $w);
1487 my $t2 = $tex->[1]; 1543 }
1488 1544
1489 glEnable GL_BLEND; 1545 glEnable GL_BLEND;
1490 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1546 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1491 glEnable GL_TEXTURE_2D; 1547 glEnable GL_TEXTURE_2D;
1492 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1548 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1493 1549
1550 $tex->draw_quad (0, 0, $w, $h);
1551
1552 glDisable GL_BLEND;
1553 glDisable GL_TEXTURE_2D;
1554}
1555
1556#############################################################################
1557
1558package CFClient::UI::VGauge;
1559
1560our @ISA = CFClient::UI::Base::;
1561
1562use CFClient::OpenGL;
1563
1564my %tex = (
1565 food => [
1566 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1567 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1568 ],
1569 grace => [
1570 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1571 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1572 ],
1573 hp => [
1574 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1575 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1576 ],
1577 mana => [
1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1579 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1580 ],
1581);
1582
1583# eg. VGauge->new (gauge => 'food'), default gauge: food
1584sub new {
1585 my $class = shift;
1586
1587 my $self = $class->SUPER::new (
1588 type => 'food',
1589 @_
1590 );
1591
1592 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1593
1594 $self
1595}
1596
1597sub size_request {
1598 my ($self) = @_;
1599
1600 #my $tex = $tex{$self->{type}}[0];
1601 #@$tex{qw(w h)}
1602 (0, 0)
1603}
1604
1605sub set_max {
1606 my ($self, $max) = @_;
1607
1608 $self->{max_val} = $max;
1609}
1610
1611sub set_value {
1612 my ($self, $val, $max) = @_;
1613
1614 $self->set_max ($max)
1615 if defined $max;
1616
1617 $max = $self->{max_val};
1618 $self->{val} = $val;
1619
1620 $self->update;
1621}
1622
1623sub _draw {
1624 my ($self) = @_;
1625
1626 my $tex = $tex{$self->{type}};
1627
1628 my ($w, $h) = ($self->{w}, $self->{h});
1629
1630 if ($self->{vertical}) {
1631 glRotate 90, 0, 0, 1;
1632 glTranslate 0, -$self->{w}, 0;
1633
1634 ($w, $h) = ($h, $w);
1635 }
1636
1637 my $ycut = $self->{val} / ($self->{max_val} || 1);
1638 $ycut = 1 if $self->{val} > $self->{max_val};
1639
1640 my $t1 = $tex->[0];
1641 my $t2 = $tex->[1];
1642
1643 glEnable GL_BLEND;
1644 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1645 glEnable GL_TEXTURE_2D;
1646 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1647
1494 my $h1 = $self->{h} - $ycut * $self->{h}; 1648 my $h1 = $self->{h} - $ycut * $self->{h};
1495 my $h2 = $ycut * $self->{h}; 1649 my $h2 = $ycut * $self->{h};
1496 1650
1497 my $yp = 0;
1498
1499 glBindTexture (GL_TEXTURE_2D, $t1->{name}); 1651 glBindTexture GL_TEXTURE_2D, $t1->{name};
1500 glBegin (GL_QUADS); 1652 glBegin GL_QUADS;
1501 glTexCoord (0, 0); glVertex (0 , $yp); 1653 glTexCoord 0 , 0; glVertex 0 , 0;
1502 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); 1654 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1503 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); 1655 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1504 glTexCoord (1, 0); glVertex (0 + $w, $yp); 1656 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1505 glEnd (); 1657 glEnd;
1506 1658
1507 $yp += $h1;
1508
1509 glBindTexture (GL_TEXTURE_2D, $t2->{name}); 1659 glBindTexture GL_TEXTURE_2D, $t2->{name};
1510 glBegin (GL_QUADS); 1660 glBegin GL_QUADS;
1511 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); 1661 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1512 glTexCoord (0, 1); glVertex (0 , $yp + $h2); 1662 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1513 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); 1663 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1514 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); 1664 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1515 glEnd (); 1665 glEnd;
1516 1666
1517 glDisable GL_BLEND; 1667 glDisable GL_BLEND;
1518 glDisable GL_TEXTURE_2D; 1668 glDisable GL_TEXTURE_2D;
1519} 1669}
1520 1670
1521############################################################################# 1671#############################################################################
1522 1672
1673package CFClient::UI::Gauge;
1674
1675our @ISA = CFClient::UI::VBox::;
1676
1677sub new {
1678 my ($class, %arg) = shift;
1679
1680 my $self = $class->SUPER::new (
1681 @_,
1682 );
1683
1684 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1685 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1686 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1687
1688 $self
1689}
1690
1691sub set_fontsize {
1692 my ($self, $fsize) = @_;
1693
1694 $self->{value}->set_fontsize ($fsize);
1695 $self->{max} ->set_fontsize ($fsize);
1696}
1697
1698sub set_value {
1699 my ($self, $val, $max) = @_;
1700
1701 $self->set_max ($max)
1702 if defined $max;
1703
1704 $self->{gauge}->set_value ($val, $max);
1705 $self->{value}->set_text ($val);
1706}
1707
1708sub set_max {
1709 my ($self, $max) = @_;
1710
1711 $self->{gauge}->set_max ($max);
1712 $self->{max}->set_text ($max);
1713}
1714
1715#############################################################################
1716
1523package CFClient::UI::Slider; 1717package CFClient::UI::Slider;
1524 1718
1525use strict; 1719use strict;
1526 1720
1527use SDL::OpenGL; 1721use CFClient::OpenGL;
1528 1722
1529our @ISA = CFClient::UI::DrawBG::; 1723our @ISA = CFClient::UI::DrawBG::;
1530 1724
1531my @tex = 1725my @tex =
1532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1726 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1542 # TODO: calculations are off 1736 # TODO: calculations are off
1543 my $self = $class->SUPER::new ( 1737 my $self = $class->SUPER::new (
1544 fg => [1, 1, 1], 1738 fg => [1, 1, 1],
1545 active_fg => [0, 0, 0], 1739 active_fg => [0, 0, 0],
1546 range => [0, 0, 100, 10], 1740 range => [0, 0, 100, 10],
1547 req_w => 40, 1741 req_w => 20,
1548 req_h => 13, 1742 req_h => 20,
1549 vertical => 0, 1743 vertical => 0,
1550 can_hover => 1, 1744 can_hover => 1,
1551 inner_pad => 5, 1745 inner_pad => 5,
1552 @_ 1746 @_
1553 ); 1747 );
1657 1851
1658package CFClient::UI::TextView; 1852package CFClient::UI::TextView;
1659 1853
1660our @ISA = CFClient::UI::HBox::; 1854our @ISA = CFClient::UI::HBox::;
1661 1855
1662use SDL::OpenGL; 1856use CFClient::OpenGL;
1663 1857
1664sub new { 1858sub new {
1665 my $class = shift; 1859 my $class = shift;
1666 1860
1667 my $self = $class->SUPER::new ( 1861 my $self = $class->SUPER::new (
1668 req_w => $::WIDTH / 6,
1669 req_h => $::HEIGHT / 6,
1670 fontsize => $::FONTSIZE, 1862 fontsize => 1,
1671 @_, 1863 @_,
1672 1864
1673 layout => (new CFClient::Layout), 1865 layout => (new CFClient::Layout),
1674 par => [], 1866 par => [],
1675 height => 0, 1867 height => 0,
1696sub text_height { 1888sub text_height {
1697 my ($self, $text) = @_; 1889 my ($self, $text) = @_;
1698 1890
1699 my $layout = $self->{layout}; 1891 my $layout = $self->{layout};
1700 1892
1701 $layout->set_height ($self->{fontsize}); 1893 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1702 $layout->set_width ($self->{w}); 1894 $layout->set_width ($self->{w});
1703 $layout->set_text ($text); 1895 $layout->set_text ($text);
1704 1896
1705 ($layout->size)[1] 1897 ($layout->size)[1]
1706} 1898}
1710 1902
1711 $self->{need_reflow}++; 1903 $self->{need_reflow}++;
1712 $self->update; 1904 $self->update;
1713} 1905}
1714 1906
1715sub size_request {
1716 my ($self) = @_;
1717
1718 ($self->{req_w}, $self->{req_h})
1719}
1720
1721sub size_allocate { 1907sub size_allocate {
1722 my ($self, $w, $h) = @_; 1908 my ($self, $w, $h) = @_;
1723 1909
1910 $self->SUPER::size_allocate ($w, $h);
1911
1724 $self->{layout}->set_height ($self->{fontsize}); 1912 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1725 $self->{layout}->set_width ($self->{w}); 1913 $self->{layout}->set_width ($self->{children}[0]{w});
1726 1914
1727 $self->reflow; 1915 $self->reflow;
1728 $self->update;
1729} 1916}
1730 1917
1731sub add_paragraph { 1918sub add_paragraph {
1732 my ($self, $color, $text) = @_; 1919 my ($self, $color, $text) = @_;
1733 1920
1818 2005
1819} 2006}
1820 2007
1821############################################################################# 2008#############################################################################
1822 2009
1823package CFClient::UI::MapWidget;
1824
1825use strict;
1826
1827use List::Util qw(min max);
1828
1829use SDL;
1830use SDL::OpenGL;
1831
1832our @ISA = CFClient::UI::Base::;
1833
1834sub new {
1835 my $class = shift;
1836
1837 $class->SUPER::new (
1838 z => -1,
1839 can_focus => 1,
1840 list => (glGenLists 1),
1841 @_
1842 )
1843}
1844
1845sub key_down {
1846 print "MAPKEYDOWN\n";
1847}
1848
1849sub key_up {
1850}
1851
1852sub button_down {
1853 my ($self, $ev, $x, $y) = @_;
1854
1855 $self->focus_in;
1856
1857 if ($ev->button == 2) {
1858 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1859 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1860
1861 $self->{motion} = sub {
1862 my ($ev, $x, $y) = @_;
1863
1864 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1865
1866 $::CFG->{map_shift_x} = $bw + $x - $ox;
1867 $::CFG->{map_shift_y} = $bh + $y - $oy;
1868
1869 $self->update;
1870 };
1871 }
1872}
1873
1874sub button_up {
1875 my ($self, $ev, $x, $y) = @_;
1876
1877 delete $self->{motion};
1878}
1879
1880sub mouse_motion {
1881 my ($self, $ev, $x, $y) = @_;
1882
1883 $self->{motion}->($ev, $x, $y) if $self->{motion};
1884}
1885
1886sub size_request {
1887 (
1888 1 + 32 * int $::WIDTH / 32,
1889 1 + 32 * int $::HEIGHT / 32,
1890 )
1891}
1892
1893sub update {
1894 my ($self) = @_;
1895
1896 $self->{need_update} = 1;
1897 $self->SUPER::update;
1898}
1899
1900sub draw {
1901 my ($self) = @_;
1902
1903 if (delete $self->{need_update}) {
1904 glNewList $self->{list}, GL_COMPILE;
1905
1906 if ($::MAP) {
1907 my $sw = int $::WIDTH / 32;
1908 my $sh = int $::HEIGHT / 32;
1909
1910 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1911 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1912
1913 glTranslate $sx0 - 32, $sy0 - 32, 0;
1914
1915 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1916
1917 if ($::CFG->{fow_enable}) {
1918 if ($::CFG->{fow_smooth}) { # smooth fog of war
1919 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
1920 glConvolutionFilter2D (
1921 GL_CONVOLUTION_2D,
1922 GL_ALPHA,
1923 3, 3,
1924 GL_ALPHA, GL_FLOAT,
1925 pack "f*",
1926 0.1, 0.1, 0.1,
1927 0.1, 0.2, 0.1,
1928 0.1, 0.1, 0.1,
1929 );
1930 glEnable GL_CONVOLUTION_2D;
1931 }
1932
1933 $self->{fow_texture} = new CFClient::Texture
1934 w => $w,
1935 h => $h,
1936 data => $data,
1937 internalformat => GL_ALPHA,
1938 format => GL_ALPHA;
1939
1940 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1941
1942 glEnable GL_BLEND;
1943 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1944 glEnable GL_TEXTURE_2D;
1945 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1946
1947 glColor +($::CFG->{fow_intensity}) x 3, 1;
1948 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1949
1950 glDisable GL_TEXTURE_2D;
1951 glDisable GL_BLEND;
1952 }
1953
1954 # HACK BEGIN
1955 {
1956 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1957 my ($w, $h) = (250, 250);
1958
1959 glEnable GL_BLEND;
1960 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1961 glEnable GL_TEXTURE_2D;
1962 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1963
1964 $self->{mapmap_texture} =
1965 new CFClient::Texture
1966 w => $w,
1967 h => $h,
1968 data => $::MAP->mapmap ($w, $h),
1969 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
1970
1971 $self->{mapmap_texture}->draw_quad (100, 100);
1972
1973 glDisable GL_TEXTURE_2D;
1974 glDisable GL_BLEND;
1975 }
1976 # HACK END
1977 }
1978
1979 glEndList;
1980 }
1981
1982 glPushMatrix;
1983 glCallList $self->{list};
1984 glPopMatrix;
1985
1986 if ($FOCUS != $self) {
1987 glColor 64/255, 64/255, 64/255;
1988 glLogicOp GL_AND;
1989 glEnable GL_COLOR_LOGIC_OP;
1990 glBegin GL_QUADS;
1991 glVertex 0, 0;
1992 glVertex 0, $::HEIGHT;
1993 glVertex $::WIDTH, $::HEIGHT;
1994 glVertex $::WIDTH, 0;
1995 glEnd;
1996 glDisable GL_COLOR_LOGIC_OP;
1997 }
1998}
1999
2000my %DIR = (
2001 SDLK_KP8, [1, "north"],
2002 SDLK_KP9, [2, "northeast"],
2003 SDLK_KP6, [3, "east"],
2004 SDLK_KP3, [4, "southeast"],
2005 SDLK_KP2, [5, "south"],
2006 SDLK_KP1, [6, "southwest"],
2007 SDLK_KP4, [7, "west"],
2008 SDLK_KP7, [8, "northwest"],
2009
2010 SDLK_UP, [1, "north"],
2011 SDLK_RIGHT, [3, "east"],
2012 SDLK_DOWN, [5, "south"],
2013 SDLK_LEFT, [7, "west"],
2014);
2015
2016sub key_down {
2017 my ($self, $ev) = @_;
2018
2019 my $mod = $ev->key_mod;
2020 my $sym = $ev->key_sym;
2021
2022 if ($sym == SDLK_KP5) {
2023 $::CONN->user_send ("stay fire");
2024 } elsif ($sym == SDLK_a) {
2025 $::CONN->user_send ("apply");
2026 } elsif ($sym == SDLK_QUOTE) {
2027 $self->emit ('activate_console');
2028 } elsif ($sym == SDLK_SLASH) {
2029 $self->emit ('activate_console' => '/');
2030 } elsif (exists $DIR{$sym}) {
2031 if ($mod & KMOD_SHIFT) {
2032 $self->{shft}++;
2033 $::CONN->user_send ("fire $DIR{$sym}[0]");
2034 } elsif ($mod & KMOD_CTRL) {
2035 $self->{ctrl}++;
2036 $::CONN->user_send ("run $DIR{$sym}[0]");
2037 } else {
2038 $::CONN->user_send ("$DIR{$sym}[1]");
2039 }
2040 }
2041}
2042
2043sub key_up {
2044 my ($self, $ev) = @_;
2045
2046 my $mod = $ev->key_mod;
2047 my $sym = $ev->key_sym;
2048
2049 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2050 $::CONN->user_send ("fire_stop");
2051 }
2052 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2053 $::CONN->user_send ("run_stop");
2054 }
2055}
2056
2057#############################################################################
2058
2059package CFClient::UI::Animator; 2010package CFClient::UI::Animator;
2060 2011
2061use SDL::OpenGL; 2012use CFClient::OpenGL;
2062 2013
2063our @ISA = CFClient::UI::Bin::; 2014our @ISA = CFClient::UI::Bin::;
2064 2015
2065sub moveto { 2016sub moveto {
2066 my ($self, $x, $y) = @_; 2017 my ($self, $x, $y) = @_;
2139 2090
2140package CFClient::UI::Root; 2091package CFClient::UI::Root;
2141 2092
2142our @ISA = CFClient::UI::Container::; 2093our @ISA = CFClient::UI::Container::;
2143 2094
2144use SDL::OpenGL; 2095use CFClient::OpenGL;
2145 2096
2146sub check_size { 2097sub check_size {
2147 my ($self) = @_; 2098 my ($self) = @_;
2148 2099
2149 $self->configure (0, 0, $::WITH, $::HEIGHT); 2100 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2150} 2101}
2151 2102
2152sub size_request { 2103sub size_request {
2153 ($::WIDTH, $::HEIGHT) 2104 ($::WIDTH, $::HEIGHT)
2154} 2105}
2156sub configure { 2107sub configure {
2157 my ($self, $x, $y, $w, $h) = @_; 2108 my ($self, $x, $y, $w, $h) = @_;
2158 2109
2159 $self->SUPER::configure ($x, $y, $w, $h); 2110 $self->SUPER::configure ($x, $y, $w, $h);
2160 2111
2161 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2162 for @{$self->{children}}; 2112 for my $child (@{$self->{children}}) {
2113 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2114
2115 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2116 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2117 $child->configure ($X, $Y, $W,$H);
2118 }
2163} 2119}
2164 2120
2165sub _topleft { 2121sub _topleft {
2166 my ($self, $x, $y) = @_; 2122 my ($self, $x, $y) = @_;
2167 2123
2175 ::refresh (); 2131 ::refresh ();
2176} 2132}
2177 2133
2178sub add { 2134sub add {
2179 my ($self, $child) = @_; 2135 my ($self, $child) = @_;
2136
2137 # integerize window positions
2138 $child->{x} = int $child->{x};
2139 $child->{y} = int $child->{y};
2180 2140
2181 $self->SUPER::add ($child); 2141 $self->SUPER::add ($child);
2182} 2142}
2183 2143
2184sub on_refresh { 2144sub on_refresh {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines