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.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) = @_;
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) = @_;
263} 294}
264 295
265sub check_size { 296sub check_size {
266 my ($self) = @_; 297 my ($self) = @_;
267 298
299 return unless $self->{parent};
300
268 my ($w, $h) = $self->size_request; 301 my ($w, $h) = $self->size_request;
269 302
270 if ($w != $self->{req_w} || $h != $self->{req_h}) { 303 if ($w != $self->{req_w} || $h != $self->{req_h}) {
271 $self->{req_w} = $w; 304 $self->{req_w} = $w;
272 $self->{req_h} = $h; 305 $self->{req_h} = $h;
273 306
274 $self->{parent}->check_size 307 $self->{parent}->check_size;
275 if $self->{parent};
276 } 308 }
277} 309}
278 310
279sub update { 311sub update {
280 my ($self) = @_; 312 my ($self) = @_;
284} 316}
285 317
286sub connect { 318sub connect {
287 my ($self, $signal, $cb) = @_; 319 my ($self, $signal, $cb) = @_;
288 320
289 push @{ $self->{cb}{$signal} }, $cb; 321 push @{ $self->{signal_cb}{$signal} }, $cb;
290} 322}
291 323
292sub emit { 324sub emit {
293 my ($self, $signal, @args) = @_; 325 my ($self, $signal, @args) = @_;
294 326
327 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
295 $_->($self, @args) 328 $cb->($self, @args);
296 for @{$self->{cb}{$signal} || []}; 329 }
297} 330}
298 331
299sub DESTROY { 332sub DESTROY {
300 my ($self) = @_; 333 my ($self) = @_;
301 334
307package CFClient::UI::DrawBG; 340package CFClient::UI::DrawBG;
308 341
309our @ISA = CFClient::UI::Base::; 342our @ISA = CFClient::UI::Base::;
310 343
311use strict; 344use strict;
312use SDL::OpenGL; 345use CFClient::OpenGL;
313 346
314sub new { 347sub new {
315 my $class = shift; 348 my $class = shift;
316 349
317 # range [value, low, high, page] 350 # range [value, low, high, page]
381 $self->{children} = [ 414 $self->{children} = [
382 sort { $a->{z} <=> $b->{z} } 415 sort { $a->{z} <=> $b->{z} }
383 @{$self->{children}}, $child 416 @{$self->{children}}, $child
384 ]; 417 ];
385 418
386 $self->{w} = $self->{h} = -1; 419 $child->check_size;
387 $self->update;
388} 420}
389 421
390sub remove { 422sub remove {
391 my ($self, $widget) = @_; 423 my ($self, $child) = @_;
392 424
425 delete $child->{parent};
426
393 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 427 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
394 428
395 $self->check_size; 429 $self->check_size;
396} 430}
397 431
398sub find_widget { 432sub find_widget {
430 464
431 $class->SUPER::new (children => [$child], %arg) 465 $class->SUPER::new (children => [$child], %arg)
432} 466}
433 467
434sub add { 468sub add {
435 my ($self, $widget) = @_; 469 my ($self, $child) = @_;
436 470
437 $self->{children} = []; 471 $self->{children} = [];
438 472
439 $self->SUPER::add ($widget); 473 $self->SUPER::add ($child);
440} 474}
441 475
442sub remove { 476sub remove {
443 my ($self, $widget) = @_; 477 my ($self, $widget) = @_;
444 478
453sub size_request { 487sub size_request {
454 $_[0]{children}[0]->size_request 488 $_[0]{children}[0]->size_request
455} 489}
456 490
457sub size_allocate { 491sub size_allocate {
458 my ($self, $x, $y, $w, $h) = @_; 492 my ($self, $w, $h) = @_;
459 493
460 $self->_size_allocate ($x, $y, $w, $h) or return;
461
462 $self->{children}[0]->size_allocate (0, 0, $w, $h); 494 $self->{children}[0]->configure (0, 0, $w, $h);
463} 495}
464 496
465############################################################################# 497#############################################################################
466 498
467package CFClient::UI::Window; 499package CFClient::UI::Window;
468 500
469our @ISA = CFClient::UI::Bin::; 501our @ISA = CFClient::UI::Bin::;
470 502
471use SDL::OpenGL; 503use CFClient::OpenGL;
472 504
473sub new { 505sub new {
474 my ($class, %arg) = @_; 506 my ($class, %arg) = @_;
475 507
476 my $self = $class->SUPER::new (%arg); 508 my $self = $class->SUPER::new (%arg);
493 $self->child->draw; 525 $self->child->draw;
494 }; 526 };
495} 527}
496 528
497sub size_allocate { 529sub size_allocate {
498 my ($self, $x, $y, $w, $h) = @_; 530 my ($self, $w, $h) = @_;
499 531
500 $self->_size_allocate ($x, $y, $w, $h) or return;
501
502 $self->child->size_allocate (0, 0, $w, $h); 532 $self->child->configure (0, 0, $w, $h);
503 533
504 $self->render_chld; 534 $self->render_chld;
505} 535}
506 536
507sub _draw { 537sub _draw {
532sub new { die } 562sub new { die }
533 563
534sub size_request { 564sub size_request {
535 my ($self) = @_; 565 my ($self) = @_;
536 566
537 @$self{qw(child_w child_h)} = $self->child->size_request; 567 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
538 $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)});
539 569
540 @$self{qw(child_w child_h)} 570 @$self{qw(child_w child_h)}
541} 571}
542 572
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 { 573sub _draw {
550 my ($self) = @_; 574 my ($self) = @_;
551 575
552 $self->{children}[1]->draw; 576 $self->{children}[1]->draw;
553} 577}
557 581
558package CFClient::UI::Frame; 582package CFClient::UI::Frame;
559 583
560our @ISA = CFClient::UI::Bin::; 584our @ISA = CFClient::UI::Bin::;
561 585
562use SDL::OpenGL; 586use 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 587
612sub new { 588sub new {
613 my $class = shift; 589 my $class = shift;
614 590
615 # TODO: user_x, user_y, overwrite moveto?
616
617 $class->SUPER::new ( 591 my $self = $class->SUPER::new (
618 bg => [1, 1, 1, 1], 592 bg => [1, 1, 1, 1],
619 border_bg => [1, 1, 1, 1], 593 border_bg => [1, 1, 1, 1],
620 border => int $::FONTSIZE * 0.8, 594 border => 0.8,
621 @_ 595 @_
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;
606}
607
608sub size_request {
609 my ($self) = @_;
610 ($self->{req_w}, $self->{req_h})
611}
612
613sub size_allocate {
614 my ($self, $w, $h) = @_;
615 $self->{w} = $w;
616 $self->{h} = $h;
617 $self->child->configure (0, 0, $w, $h);
618}
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,
622 ) 691 )
623} 692}
624 693
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 { 694sub size_allocate {
639 my ($self, $x, $y, $w, $h) = @_; 695 my ($self, $w, $h) = @_;
640 696
641 $self->_size_allocate ($x, $y, $w, $h) or return;
642
643 $h -= List::Util::max 0, $self->{border} * 2; 697 $h -= List::Util::max 0, $self->border * 2;
644 $w -= List::Util::max 0, $self->{border} * 2; 698 $w -= List::Util::max 0, $self->border * 2;
645 699
700 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
701 if $self->{title};
702
646 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 703 $self->child->configure ($self->border, $self->border, $w, $h);
647} 704}
648 705
649sub button_down { 706sub button_down {
650 my ($self, $ev, $x, $y) = @_; 707 my ($self, $ev, $x, $y) = @_;
651 708
709 my $border = $self->border;
710
652 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 711 if ($x < $self->{w} && $x >= $self->{w} - $border
653 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 712 && $y < $self->{h} && $y >= $self->{h} - $border) {
654 713
655 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 714 my ($ox, $oy) = ($ev->{x}, $ev->{y});
656 my ($bw, $bh) = ($self->{w}, $self->{h}); 715 my ($bw, $bh) = ($self->{w}, $self->{h});
657 716
658 $self->{motion} = sub { 717 $self->{motion} = sub {
659 my ($ev, $x, $y) = @_; 718 my ($ev, $x, $y) = @_;
660 719
661 ($x, $y) = ($ev->motion_x, $ev->motion_y); 720 ($x, $y) = ($ev->{x}, $ev->{y});
662 721
663 $self->{user_w} = $bw + $x - $ox; 722 $self->{user_w} = $bw + $x - $ox;
664 $self->{user_h} = $bh + $y - $oy; 723 $self->{user_h} = $bh + $y - $oy;
665 $self->update; 724 $self->check_size;
666 }; 725 };
667 726
668 } elsif ($x >= 0 && $x < $self->{w} 727 } elsif ($x >= 0 && $x < $self->{w}
669 && $y >= 0 && $y < $self->{border}) { 728 && $y >= 0 && $y < $border) {
670 729
671 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 730 my ($ox, $oy) = ($ev->{x}, $ev->{y});
672 my ($bx, $by) = ($self->{x}, $self->{y}); 731 my ($bx, $by) = ($self->{x}, $self->{y});
673 732
674 $self->{motion} = sub { 733 $self->{motion} = sub {
675 my ($ev, $x, $y) = @_; 734 my ($ev, $x, $y) = @_;
676 735
677 ($x, $y) = ($ev->motion_x, $ev->motion_y); 736 ($x, $y) = ($ev->{x}, $ev->{y});
678 737
679 $self->move ($bx + $x - $ox, $by + $y - $oy); 738 $self->move ($bx + $x - $ox, $by + $y - $oy);
680 $self->update; 739 $self->update;
681 }; 740 };
682 } 741 }
703 glEnable GL_BLEND; 762 glEnable GL_BLEND;
704 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 763 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
705 glEnable GL_TEXTURE_2D; 764 glEnable GL_TEXTURE_2D;
706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 765 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
707 766
767 my $border = $self->border;
768
708 glColor @{ $self->{border_bg} }; 769 glColor @{ $self->{border_bg} };
709 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 770 $tex[1]->draw_quad (0, 0, $w, $border);
710 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 771 $tex[3]->draw_quad (0, $border, $border, $ch);
711 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 772 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
712 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 773 $tex[4]->draw_quad (0, $h - $border, $w, $border);
713 774
714 my $bg = $tex[0]; 775 my $bg = $tex[0];
715 776
716 # TODO: repeat texture not scale 777 # TODO: repeat texture not scale
717 my $rep_x = $cw / $bg->{w}; 778 my $rep_x = $cw / $bg->{w};
720 glColor @{ $self->{bg} }; 781 glColor @{ $self->{bg} };
721 782
722 $bg->{s} = $rep_x; 783 $bg->{s} = $rep_x;
723 $bg->{t} = $rep_y; 784 $bg->{t} = $rep_y;
724 $bg->{wrap_mode} = 1; 785 $bg->{wrap_mode} = 1;
725 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 786 $bg->draw_quad ($border, $border, $cw, $ch);
726 787
727 glDisable GL_TEXTURE_2D; 788 glDisable GL_TEXTURE_2D;
728 glDisable GL_BLEND; 789 glDisable GL_BLEND;
729 790
791 $self->{title}->draw if $self->{title};
730 $self->child->draw; 792 $self->child->draw;
731} 793}
732 794
733############################################################################# 795#############################################################################
734 796
736 798
737our @ISA = CFClient::UI::Base::; 799our @ISA = CFClient::UI::Base::;
738 800
739use List::Util qw(max sum); 801use List::Util qw(max sum);
740 802
741use SDL::OpenGL; 803use CFClient::OpenGL;
742 804
743sub new { 805sub new {
744 my $class = shift; 806 my $class = shift;
745 807
746 $class->SUPER::new ( 808 $class->SUPER::new (
753 my ($self, $x, $y, $child) = @_; 815 my ($self, $x, $y, $child) = @_;
754 816
755 $child->set_parent ($self); 817 $child->set_parent ($self);
756 $self->{children}[$y][$x] = $child; 818 $self->{children}[$y][$x] = $child;
757 819
758 $self->{w} = $self->{h} = -1; 820 $child->check_size;
759 $self->update;
760} 821}
761 822
762# 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?
763sub clear { 824sub clear {
764 my ($self) = @_; 825 my ($self) = @_;
777 or next; 838 or next;
778 839
779 for my $x (0 .. $#$row) { 840 for my $x (0 .. $#$row) {
780 my $widget = $row->[$x] 841 my $widget = $row->[$x]
781 or next; 842 or next;
782 my ($w, $h) = $widget->size_request; 843 my ($w, $h) = @$widget{qw(req_w req_h)};
783 844
784 $w[$x] = max $w[$x], $w; 845 $w[$x] = max $w[$x], $w;
785 $h[$y] = max $h[$y], $h; 846 $h[$y] = max $h[$y], $h;
786 } 847 }
787 } 848 }
799 (sum @$hs), 860 (sum @$hs),
800 ) 861 )
801} 862}
802 863
803sub size_allocate { 864sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_; 865 my ($self, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h) or return;
807 866
808 my ($ws, $hs) = $self->get_wh; 867 my ($ws, $hs) = $self->get_wh;
809 868
810 my $req_w = sum @$ws; 869 my $req_w = sum @$ws;
811 my $req_h = sum @$hs; 870 my $req_h = sum @$hs;
833 892
834 for my $c (0 .. $#$row) { 893 for my $c (0 .. $#$row) {
835 my $col_w = $ws->[$c]; 894 my $col_w = $ws->[$c];
836 895
837 if (my $widget = $row->[$c]) { 896 if (my $widget = $row->[$c]) {
838 $widget->size_allocate ($x, $y, $col_w, $row_h); 897 $widget->configure ($x, $y, $col_w, $row_h);
839 } 898 }
840 899
841 $x += $col_w; 900 $x += $col_w;
842 } 901 }
843 902
888 (List::Util::max map $_->[1], @alloc), 947 (List::Util::max map $_->[1], @alloc),
889 ) 948 )
890} 949}
891 950
892sub size_allocate { 951sub size_allocate {
893 my ($self, $x, $y, $w, $h) = @_; 952 my ($self, $w, $h) = @_;
894
895 $self->_size_allocate ($x, $y, $w, $h) or return;
896 953
897 ($h, $w) = ($w, $h); 954 ($h, $w) = ($w, $h);
898 955
899 my $children = $self->{children}; 956 my $children = $self->{children};
900 957
901 my @h = map +($_->size_request)[0], @$children; 958 my @h = map $_->{req_w}, @$children;
902 959
903 my $req_h = List::Util::sum @h; 960 my $req_h = List::Util::sum @h;
904 961
905 if ($req_h > $h) { 962 if ($req_h > $h) {
906 # ah well, not enough space 963 # ah well, not enough space
922 979
923 my $y = 0; 980 my $y = 0;
924 for (0 .. $#$children) { 981 for (0 .. $#$children) {
925 my $child = $children->[$_]; 982 my $child = $children->[$_];
926 my $h = $h[$_]; 983 my $h = $h[$_];
927 $child->size_allocate ($y, 0, $h, $w); 984 $child->configure ($y, 0, $h, $w);
928 985
929 $y += $h; 986 $y += $h;
930 } 987 }
931 988
932 1 989 1
950 (List::Util::sum map $_->[1], @alloc), 1007 (List::Util::sum map $_->[1], @alloc),
951 ) 1008 )
952} 1009}
953 1010
954sub size_allocate { 1011sub size_allocate {
955 my ($self, $x, $y, $w, $h) = @_; 1012 my ($self, $w, $h) = @_;
956
957 $self->_size_allocate ($x, $y, $w, $h) or return;
958 1013
959 my $children = $self->{children}; 1014 my $children = $self->{children};
960 1015
961 my @h = map +($_->size_request)[1], @$children; 1016 my @h = map $_->{req_h}, @$children;
962 1017
963 my $req_h = List::Util::sum @h; 1018 my $req_h = List::Util::sum @h;
964 1019
965 if ($req_h > $h) { 1020 if ($req_h > $h) {
966 # ah well, not enough space 1021 # ah well, not enough space
980 1035
981 my $y = 0; 1036 my $y = 0;
982 for (0 .. $#$children) { 1037 for (0 .. $#$children) {
983 my $child = $children->[$_]; 1038 my $child = $children->[$_];
984 my $h = $h[$_]; 1039 my $h = $h[$_];
985 $child->size_allocate (0, $y, $w, $h); 1040 $child->configure (0, $y, $w, $h);
986 1041
987 $y += $h; 1042 $y += $h;
988 } 1043 }
989 1044
990 1 1045 1
994 1049
995package CFClient::UI::Label; 1050package CFClient::UI::Label;
996 1051
997our @ISA = CFClient::UI::Base::; 1052our @ISA = CFClient::UI::Base::;
998 1053
999use SDL::OpenGL; 1054use CFClient::OpenGL;
1000 1055
1001sub new { 1056sub new {
1002 my ($class, %arg) = @_; 1057 my ($class, %arg) = @_;
1003 1058
1004 my $self = $class->SUPER::new ( 1059 my $self = $class->SUPER::new (
1005 fg => [1, 1, 1], 1060 fg => [1, 1, 1],
1006 fontsize => $::FONTSIZE, 1061 fontsize => 1,
1007 text => "", 1062 text => "",
1008 align => -1, 1063 align => -1,
1009 valign => -1, 1064 valign => -1,
1010 padding => 2, 1065 padding => 2,
1011 layout => new CFClient::Layout, 1066 layout => new CFClient::Layout,
1012 %arg 1067 %arg
1013 ); 1068 );
1014 1069
1015 $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 }
1016 1075
1017 $self->set_text (delete $self->{text}) if exists $self->{text}; 1076 $self->set_text (delete $self->{text}) if exists $self->{text};
1018 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1077 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1019 1078
1020 $self 1079 $self
1034 my ($self, $text) = @_; 1093 my ($self, $text) = @_;
1035 1094
1036 $self->{layout}->set_text ($text); 1095 $self->{layout}->set_text ($text);
1037 1096
1038 delete $self->{texture}; 1097 delete $self->{texture};
1098 $self->check_size;
1039 $self->update; 1099 $self->update;
1040} 1100}
1041 1101
1042sub set_markup { 1102sub set_markup {
1043 my ($self, $markup) = @_; 1103 my ($self, $markup) = @_;
1044 1104
1045 $self->{layout}->set_markup ($markup); 1105 $self->{layout}->set_markup ($markup);
1046 1106
1047 delete $self->{texture}; 1107 delete $self->{texture};
1108 $self->check_size;
1048 $self->update; 1109 $self->update;
1049} 1110}
1050 1111
1051sub size_request { 1112sub size_request {
1052 my ($self) = @_; 1113 my ($self) = @_;
1053 1114
1054 $self->{layout}->set_width; 1115 $self->{layout}->set_width;
1055 $self->{layout}->set_height ($self->{fontsize}); 1116 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1056 1117
1057 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 }
1058 1128
1059 ( 1129 (
1060 $w + $self->{padding} * 2, 1130 $w + $self->{padding} * 2,
1061 $h + $self->{padding} * 2, 1131 $h + $self->{padding} * 2,
1062 ) 1132 )
1063} 1133}
1064 1134
1065sub size_allocate { 1135sub size_allocate {
1066 my ($self, $x, $y, $w, $h) = @_; 1136 my ($self, $w, $h) = @_;
1067
1068 $self->_size_allocate ($x, $y, $w, $h) or return;
1069 1137
1070 delete $self->{texture}; 1138 delete $self->{texture};
1071} 1139}
1072 1140
1073sub update { 1141sub set_fontsize {
1074 my ($self) = @_; 1142 my ($self, $fontsize) = @_;
1075 1143
1076 delete $self->{texture}; 1144 $self->{fontsize} = $fontsize;
1077 $self->SUPER::update; 1145 $self->check_size;
1078} 1146}
1079 1147
1080sub _draw { 1148sub _draw {
1081 my ($self) = @_; 1149 my ($self) = @_;
1082 1150
1083 my $tex = $self->{texture} ||= do { 1151 my $tex = $self->{texture} ||= do {
1084 $self->{layout}->set_width ($self->{w}); 1152 $self->{layout}->set_width ($self->{w});
1085 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1153 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1086 new_from_layout CFClient::Texture $self->{layout} 1154 new_from_layout CFClient::Texture $self->{layout}
1087 }; 1155 };
1088 1156
1089 glEnable GL_BLEND; 1157 glEnable GL_BLEND;
1090 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1158 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1115 1183
1116package CFClient::UI::EntryBase; 1184package CFClient::UI::EntryBase;
1117 1185
1118our @ISA = CFClient::UI::Label::; 1186our @ISA = CFClient::UI::Label::;
1119 1187
1120use SDL; 1188use CFClient::OpenGL;
1121use SDL::OpenGL;
1122 1189
1123sub new { 1190sub new {
1124 my $class = shift; 1191 my $class = shift;
1125 1192
1126 $class->SUPER::new ( 1193 $class->SUPER::new (
1140 1207
1141 delete $self->{cur_h}; 1208 delete $self->{cur_h};
1142 1209
1143 return if $self->{text} eq $text; 1210 return if $self->{text} eq $text;
1144 1211
1212 delete $self->{texture};
1213
1145 $self->{last_activity} = $::NOW; 1214 $self->{last_activity} = $::NOW;
1146 $self->{text} = $text; 1215 $self->{text} = $text;
1147 1216
1148 $text =~ s/./*/g if $self->{hidden}; 1217 $text =~ s/./*/g if $self->{hidden};
1149 $self->{layout}->set_text ("$text "); 1218 $self->{layout}->set_text ("$text ");
1162 1231
1163 ($w + 1, $h) # add 1 for cursor 1232 ($w + 1, $h) # add 1 for cursor
1164} 1233}
1165 1234
1166sub size_allocate { 1235sub size_allocate {
1167 my ($self, $x, $y, $w, $h) = @_; 1236 my ($self, $w, $h) = @_;
1168 1237
1169 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1238 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1170
1171 $self->_set_text ($self->{text});
1172} 1239}
1173 1240
1174sub set_text { 1241sub set_text {
1175 my ($self, $text) = @_; 1242 my ($self, $text) = @_;
1176 1243
1180} 1247}
1181 1248
1182sub key_down { 1249sub key_down {
1183 my ($self, $ev) = @_; 1250 my ($self, $ev) = @_;
1184 1251
1185 my $mod = $ev->key_mod; 1252 my $mod = $ev->{mod};
1186 my $sym = $ev->key_sym; 1253 my $sym = $ev->{sym};
1187
1188 my $uni = $ev->key_unicode; 1254 my $uni = $ev->{unicode};
1189 1255
1190 my $text = $self->get_text; 1256 my $text = $self->get_text;
1191 1257
1192 if ($sym == SDLK_BACKSPACE) { 1258 if ($sym == 8) {
1193 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1259 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1194 } elsif ($sym == SDLK_DELETE) { 1260 } elsif ($sym == 127) {
1195 substr $text, $self->{cursor}, 1, ""; 1261 substr $text, $self->{cursor}, 1, "";
1196 } elsif ($sym == SDLK_LEFT) { 1262 } elsif ($sym == CFClient::SDLK_LEFT) {
1197 --$self->{cursor} if $self->{cursor}; 1263 --$self->{cursor} if $self->{cursor};
1198 } elsif ($sym == SDLK_RIGHT) { 1264 } elsif ($sym == CFClient::SDLK_RIGHT) {
1199 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1265 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1200 } elsif ($sym == SDLK_HOME) { 1266 } elsif ($sym == CFClient::SDLK_HOME) {
1201 $self->{cursor} = 0; 1267 $self->{cursor} = 0;
1202 } elsif ($sym == SDLK_END) { 1268 } elsif ($sym == CFClient::SDLK_END) {
1203 $self->{cursor} = length $text; 1269 $self->{cursor} = length $text;
1204 } elsif ($sym == SDLK_ESCAPE) { 1270 } elsif ($sym == 27) {
1205 $self->emit ('escape'); 1271 $self->emit ('escape');
1206 } elsif ($uni) { 1272 } elsif ($uni) {
1207 substr $text, $self->{cursor}++, 0, chr $uni; 1273 substr $text, $self->{cursor}++, 0, chr $uni;
1208 } 1274 }
1209 1275
1284 1350
1285package CFClient::UI::Entry; 1351package CFClient::UI::Entry;
1286 1352
1287our @ISA = CFClient::UI::EntryBase::; 1353our @ISA = CFClient::UI::EntryBase::;
1288 1354
1289use SDL; 1355use CFClient::OpenGL;
1290use SDL::OpenGL;
1291 1356
1292sub key_down { 1357sub key_down {
1293 my ($self, $ev) = @_; 1358 my ($self, $ev) = @_;
1294 1359
1295 my $sym = $ev->key_sym; 1360 my $sym = $ev->{sym};
1296 1361
1297 if ($sym == SDLK_RETURN) { 1362 if ($sym == 13) {
1298 $self->emit (activate => $self->get_text); 1363 $self->emit (activate => $self->get_text);
1299 $self->update; 1364 $self->update;
1300 1365
1301 } else { 1366 } else {
1302 $self->SUPER::key_down ($ev); 1367 $self->SUPER::key_down ($ev);
1308 1373
1309package CFClient::UI::Button; 1374package CFClient::UI::Button;
1310 1375
1311our @ISA = CFClient::UI::Label::; 1376our @ISA = CFClient::UI::Label::;
1312 1377
1313use SDL; 1378use CFClient::OpenGL;
1314use SDL::OpenGL;
1315 1379
1316my @tex = 1380my @tex =
1317 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1381 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1318 qw(b1_button_active.png); 1382 qw(b1_button_active.png);
1319 1383
1320sub new { 1384sub new {
1321 my $class = shift; 1385 my $class = shift;
1322 1386
1324 padding => 4, 1388 padding => 4,
1325 fg => [1, 1, 1], 1389 fg => [1, 1, 1],
1326 bg => [1, 1, 1, 0.2], 1390 bg => [1, 1, 1, 0.2],
1327 active_fg => [0, 0, 1], 1391 active_fg => [0, 0, 1],
1328 can_hover => 1, 1392 can_hover => 1,
1393 align => 0,
1394 valign => 0,
1329 @_ 1395 @_
1330 ) 1396 )
1331} 1397}
1332 1398
1333sub button_up { 1399sub button_up {
1367package CFClient::UI::CheckBox; 1433package CFClient::UI::CheckBox;
1368 1434
1369our @ISA = CFClient::UI::DrawBG::; 1435our @ISA = CFClient::UI::DrawBG::;
1370 1436
1371my @tex = 1437my @tex =
1372 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1373 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1439 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1374 1440
1375use SDL; 1441use CFClient::OpenGL;
1376use SDL::OpenGL;
1377 1442
1378sub new { 1443sub new {
1379 my $class = shift; 1444 my $class = shift;
1380 1445
1381 $class->SUPER::new ( 1446 $class->SUPER::new (
1392 my ($self) = @_; 1457 my ($self) = @_;
1393 1458
1394 ($self->{padding} * 2 + 6) x 2 1459 ($self->{padding} * 2 + 6) x 2
1395} 1460}
1396 1461
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 { 1462sub button_down {
1404 my ($self, $ev, $x, $y) = @_; 1463 my ($self, $ev, $x, $y) = @_;
1405 1464
1406 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1465 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1407 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1466 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1433 glDisable GL_BLEND; 1492 glDisable GL_BLEND;
1434} 1493}
1435 1494
1436############################################################################# 1495#############################################################################
1437 1496
1438package CFClient::UI::VGauge; 1497package CFClient::UI::Image;
1439 1498
1440our @ISA = CFClient::UI::Base::; 1499our @ISA = CFClient::UI::Base::;
1441 1500
1442use SDL::OpenGL; 1501use CFClient::OpenGL;
1502use Carp qw/confess/;
1443 1503
1444my %tex = ( 1504our %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 1505
1463# eg. VGauge->new (gauge => 'food'), default gauge: food
1464sub new { 1506sub new {
1465 my $class = shift; 1507 my $class = shift;
1466 1508
1467 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};
1468 1521
1469 $self 1522 $self
1470} 1523}
1471 1524
1472sub size_request { 1525sub size_request {
1473 my ($self) = @_; 1526 my ($self) = @_;
1474 1527
1475 ($self->{w}, $self->{h}) 1528 ($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} 1529}
1494 1530
1495sub _draw { 1531sub _draw {
1496 my ($self) = @_; 1532 my ($self) = @_;
1497 1533
1498 my $tex = $tex{$self->{gauge}}; 1534 my $tex = $self->{tex};
1499 1535
1500 my ($w, $h) = ($self->{w}, $self->{h}); 1536 my ($w, $h) = ($self->{w}, $self->{h});
1501 1537
1502 my $ycut = $self->{val} / ($self->{max_val} || 1); 1538 if ($self->{rot90}) {
1503 $ycut = 1 if $self->{val} > $self->{max_val}; 1539 glRotate 90, 0, 0, 1;
1540 glTranslate 0, -$self->{w}, 0;
1504 1541
1505 my $t1 = $tex->[0]; 1542 ($w, $h) = ($h, $w);
1506 my $t2 = $tex->[1]; 1543 }
1507 1544
1508 glEnable GL_BLEND; 1545 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1546 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D; 1547 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1548 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 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
1513 my $h1 = $self->{h} - $ycut * $self->{h}; 1648 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h}; 1649 my $h2 = $ycut * $self->{h};
1515 1650
1516 my $yp = 0;
1517
1518 glBindTexture (GL_TEXTURE_2D, $t1->{name}); 1651 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin (GL_QUADS); 1652 glBegin GL_QUADS;
1520 glTexCoord (0, 0); glVertex (0 , $yp); 1653 glTexCoord 0 , 0; glVertex 0 , 0;
1521 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); 1654 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1522 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); 1655 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1523 glTexCoord (1, 0); glVertex (0 + $w, $yp); 1656 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1524 glEnd (); 1657 glEnd;
1525 1658
1526 $yp += $h1;
1527
1528 glBindTexture (GL_TEXTURE_2D, $t2->{name}); 1659 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin (GL_QUADS); 1660 glBegin GL_QUADS;
1530 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); 1661 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1531 glTexCoord (0, 1); glVertex (0 , $yp + $h2); 1662 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1532 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); 1663 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1533 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); 1664 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1534 glEnd (); 1665 glEnd;
1535 1666
1536 glDisable GL_BLEND; 1667 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D; 1668 glDisable GL_TEXTURE_2D;
1538} 1669}
1539 1670
1540############################################################################# 1671#############################################################################
1541 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
1542package CFClient::UI::Slider; 1717package CFClient::UI::Slider;
1543 1718
1544use strict; 1719use strict;
1545 1720
1546use SDL::OpenGL; 1721use CFClient::OpenGL;
1547 1722
1548our @ISA = CFClient::UI::DrawBG::; 1723our @ISA = CFClient::UI::DrawBG::;
1549 1724
1550my @tex = 1725my @tex =
1551 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1726 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1561 # TODO: calculations are off 1736 # TODO: calculations are off
1562 my $self = $class->SUPER::new ( 1737 my $self = $class->SUPER::new (
1563 fg => [1, 1, 1], 1738 fg => [1, 1, 1],
1564 active_fg => [0, 0, 0], 1739 active_fg => [0, 0, 0],
1565 range => [0, 0, 100, 10], 1740 range => [0, 0, 100, 10],
1566 req_w => 40, 1741 req_w => 20,
1567 req_h => 13, 1742 req_h => 20,
1568 vertical => 0, 1743 vertical => 0,
1569 can_hover => 1, 1744 can_hover => 1,
1570 inner_pad => 5, 1745 inner_pad => 5,
1571 @_ 1746 @_
1572 ); 1747 );
1676 1851
1677package CFClient::UI::TextView; 1852package CFClient::UI::TextView;
1678 1853
1679our @ISA = CFClient::UI::HBox::; 1854our @ISA = CFClient::UI::HBox::;
1680 1855
1681use SDL::OpenGL; 1856use CFClient::OpenGL;
1682 1857
1683sub new { 1858sub new {
1684 my $class = shift; 1859 my $class = shift;
1685 1860
1686 my $self = $class->SUPER::new ( 1861 my $self = $class->SUPER::new (
1687 req_w => $::WIDTH / 6,
1688 req_h => $::HEIGHT / 6,
1689 fontsize => $::FONTSIZE, 1862 fontsize => 1,
1690 @_, 1863 @_,
1691 1864
1692 layout => (new CFClient::Layout), 1865 layout => (new CFClient::Layout),
1693 par => [], 1866 par => [],
1694 height => 0, 1867 height => 0,
1715sub text_height { 1888sub text_height {
1716 my ($self, $text) = @_; 1889 my ($self, $text) = @_;
1717 1890
1718 my $layout = $self->{layout}; 1891 my $layout = $self->{layout};
1719 1892
1720 $layout->set_height ($self->{fontsize}); 1893 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1721 $layout->set_width ($self->{w}); 1894 $layout->set_width ($self->{w});
1722 $layout->set_text ($text); 1895 $layout->set_text ($text);
1723 1896
1724 ($layout->size)[1] 1897 ($layout->size)[1]
1725} 1898}
1729 1902
1730 $self->{need_reflow}++; 1903 $self->{need_reflow}++;
1731 $self->update; 1904 $self->update;
1732} 1905}
1733 1906
1734sub size_request {
1735 my ($self) = @_;
1736
1737 ($self->{req_w}, $self->{req_h})
1738}
1739
1740sub size_allocate { 1907sub size_allocate {
1741 my ($self, $x, $y, $w, $h) = @_; 1908 my ($self, $w, $h) = @_;
1742 1909
1743 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1910 $self->SUPER::size_allocate ($w, $h);
1744 1911
1745 $self->{layout}->set_height ($self->{fontsize}); 1912 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1746 $self->{layout}->set_width ($self->{w}); 1913 $self->{layout}->set_width ($self->{children}[0]{w});
1747 1914
1748 $self->reflow; 1915 $self->reflow;
1749 $self->update;
1750} 1916}
1751 1917
1752sub add_paragraph { 1918sub add_paragraph {
1753 my ($self, $color, $text) = @_; 1919 my ($self, $color, $text) = @_;
1754 1920
1839 2005
1840} 2006}
1841 2007
1842############################################################################# 2008#############################################################################
1843 2009
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; 2010package CFClient::UI::Animator;
2081 2011
2082use SDL::OpenGL; 2012use CFClient::OpenGL;
2083 2013
2084our @ISA = CFClient::UI::Bin::; 2014our @ISA = CFClient::UI::Bin::;
2085 2015
2086sub moveto { 2016sub moveto {
2087 my ($self, $x, $y) = @_; 2017 my ($self, $x, $y) = @_;
2160 2090
2161package CFClient::UI::Root; 2091package CFClient::UI::Root;
2162 2092
2163our @ISA = CFClient::UI::Container::; 2093our @ISA = CFClient::UI::Container::;
2164 2094
2165use SDL::OpenGL; 2095use CFClient::OpenGL;
2166 2096
2167sub check_size { 2097sub check_size {
2168 my ($self) = @_; 2098 my ($self) = @_;
2169 2099
2170 $self->size_allocate (0, 0, $::WITH, $::HEIGHT); 2100 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2171} 2101}
2172 2102
2173sub size_request { 2103sub size_request {
2174 ($::WIDTH, $::HEIGHT) 2104 ($::WIDTH, $::HEIGHT)
2175} 2105}
2176 2106
2177sub size_allocate { 2107sub configure {
2178 my ($self, $x, $y, $w, $h) = @_; 2108 my ($self, $x, $y, $w, $h) = @_;
2179 2109
2180 $self->_size_allocate ($x, $y, $w, $h); 2110 $self->SUPER::configure ($x, $y, $w, $h);
2181 2111
2182 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2183 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 }
2184} 2119}
2185 2120
2186sub _topleft { 2121sub _topleft {
2187 my ($self, $x, $y) = @_; 2122 my ($self, $x, $y) = @_;
2188 2123
2195 $self->check_size; 2130 $self->check_size;
2196 ::refresh (); 2131 ::refresh ();
2197} 2132}
2198 2133
2199sub add { 2134sub add {
2200 my ($self, $widget) = @_; 2135 my ($self, $child) = @_;
2201 2136
2137 # integerize window positions
2138 $child->{x} = int $child->{x};
2139 $child->{y} = int $child->{y};
2140
2202 $self->SUPER::add ($widget); 2141 $self->SUPER::add ($child);
2203
2204 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2205} 2142}
2206 2143
2207sub on_refresh { 2144sub on_refresh {
2208 my ($self, $id, $cb) = @_; 2145 my ($self, $id, $cb) = @_;
2209 2146

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines