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.120 by elmex, Sun Apr 16 17:43:17 2006 UTC vs.
Revision 1.139 by root, Wed Apr 19 23:55:58 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}
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 abtract";
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
134 $self->{x} = $x; 151 $self->{x} = $x;
135 $self->{y} = $y; 152 $self->{y} = $y;
136 153
137 return unless $self->{w} != $w || $self->{h} != $h; 154 return unless $self->{w} != $w || $self->{h} != $h;
138 155
139 $self->{w} = $w; 156 $self->{w} = $w;
140 $self->{h} = $h; 157 $self->{h} = $h;
141 158
142 1 159 $self->size_allocate ($w, $h);
160 $self->update;
143} 161}
144 162
145sub size_allocate { 163sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 164 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 165}
150 166
151# return top left coordinates 167# return top left coordinates
152sub _topleft { 168sub _topleft {
153 my ($self, $x, $y) = @_; 169 my ($self, $x, $y) = @_;
225 glPopMatrix; 241 glPopMatrix;
226 242
227 if ($self == $HOVER && $self->{can_hover}) { 243 if ($self == $HOVER && $self->{can_hover}) {
228 my ($x, $y) = @$self{qw(x y)}; 244 my ($x, $y) = @$self{qw(x y)};
229 245
230 glColor 0, 0, 1, 0.2; 246 glColor 1, 0.8, 0.5, 0.2;
231 glEnable GL_BLEND; 247 glEnable GL_BLEND;
232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 248 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
233 glBegin GL_QUADS; 249 glBegin GL_QUADS;
234 glVertex $x , $y; 250 glVertex $x , $y;
235 glVertex $x + $self->{w}, $y; 251 glVertex $x + $self->{w}, $y;
260 my ($self, $parent) = @_; 276 my ($self, $parent) = @_;
261 277
262 Scalar::Util::weaken ($self->{parent} = $parent); 278 Scalar::Util::weaken ($self->{parent} = $parent);
263} 279}
264 280
281sub check_size {
282 my ($self) = @_;
283
284 my ($w, $h) = $self->size_request;
285
286 if ($w != $self->{req_w} || $h != $self->{req_h}) {
287 $self->{req_w} = $w;
288 $self->{req_h} = $h;
289
290 $self->{parent}->check_size
291 if $self->{parent};
292 }
293}
294
265sub update { 295sub update {
266 my ($self) = @_; 296 my ($self) = @_;
267 297
268 $self->{parent}->update 298 $self->{parent}->update
269 if $self->{parent}; 299 if $self->{parent};
270} 300}
271 301
272sub connect { 302sub connect {
273 my ($self, $signal, $cb) = @_; 303 my ($self, $signal, $cb) = @_;
274 304
275 push @{ $self->{cb}{$signal} }, $cb; 305 push @{ $self->{signal_cb}{$signal} }, $cb;
276} 306}
277 307
278sub emit { 308sub emit {
279 my ($self, $signal, @args) = @_; 309 my ($self, $signal, @args) = @_;
280 310
311 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
281 $_->($self, @args) 312 $cb->($self, @args);
282 for @{$self->{cb}{$signal} || []}; 313 }
283} 314}
284 315
285sub DESTROY { 316sub DESTROY {
286 my ($self) = @_; 317 my ($self) = @_;
287 318
293package CFClient::UI::DrawBG; 324package CFClient::UI::DrawBG;
294 325
295our @ISA = CFClient::UI::Base::; 326our @ISA = CFClient::UI::Base::;
296 327
297use strict; 328use strict;
298use SDL::OpenGL; 329use CFClient::OpenGL;
299 330
300sub new { 331sub new {
301 my $class = shift; 332 my $class = shift;
302 333
303 # range [value, low, high, page] 334 # range [value, low, high, page]
367 $self->{children} = [ 398 $self->{children} = [
368 sort { $a->{z} <=> $b->{z} } 399 sort { $a->{z} <=> $b->{z} }
369 @{$self->{children}}, $child 400 @{$self->{children}}, $child
370 ]; 401 ];
371 402
372 $self->{w} = $self->{h} = -1; 403 $child->check_size;
373 $self->update;
374} 404}
375 405
376sub remove { 406sub remove {
377 my ($self, $widget) = @_; 407 my ($self, $child) = @_;
378 408
409 delete $child->{parent};
410
379 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
380 412
381 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 413 $self->check_size;
382} 414}
383 415
384sub find_widget { 416sub find_widget {
385 my ($self, $x, $y) = @_; 417 my ($self, $x, $y) = @_;
386 418
416 448
417 $class->SUPER::new (children => [$child], %arg) 449 $class->SUPER::new (children => [$child], %arg)
418} 450}
419 451
420sub add { 452sub add {
421 my ($self, $widget) = @_; 453 my ($self, $child) = @_;
422 454
423 $self->{children} = []; 455 $self->{children} = [];
424 456
425 $self->SUPER::add ($widget); 457 $self->SUPER::add ($child);
426} 458}
427 459
428sub remove { 460sub remove {
429 my ($self, $widget) = @_; 461 my ($self, $widget) = @_;
430 462
439sub size_request { 471sub size_request {
440 $_[0]{children}[0]->size_request 472 $_[0]{children}[0]->size_request
441} 473}
442 474
443sub size_allocate { 475sub size_allocate {
444 my ($self, $x, $y, $w, $h) = @_; 476 my ($self, $w, $h) = @_;
445 477
446 $self->_size_allocate ($x, $y, $w, $h) or return;
447
448 $self->{children}[0]->size_allocate (0, 0, $w, $h); 478 $self->{children}[0]->configure (0, 0, $w, $h);
449} 479}
450 480
451############################################################################# 481#############################################################################
452 482
453package CFClient::UI::Window; 483package CFClient::UI::Window;
454 484
455our @ISA = CFClient::UI::Bin::; 485our @ISA = CFClient::UI::Bin::;
456 486
457use SDL::OpenGL; 487use CFClient::OpenGL;
458 488
459sub new { 489sub new {
460 my ($class, %arg) = @_; 490 my ($class, %arg) = @_;
461 491
462 my $self = $class->SUPER::new (%arg); 492 my $self = $class->SUPER::new (%arg);
479 $self->child->draw; 509 $self->child->draw;
480 }; 510 };
481} 511}
482 512
483sub size_allocate { 513sub size_allocate {
484 my ($self, $x, $y, $w, $h) = @_; 514 my ($self, $w, $h) = @_;
485 515
486 $self->_size_allocate ($x, $y, $w, $h) or return;
487
488 $self->child->size_allocate (0, 0, $w, $h); 516 $self->child->configure (0, 0, $w, $h);
489 517
490 $self->render_chld; 518 $self->render_chld;
491} 519}
492 520
493sub _draw { 521sub _draw {
509 glDisable GL_TEXTURE_2D; 537 glDisable GL_TEXTURE_2D;
510} 538}
511 539
512############################################################################# 540#############################################################################
513 541
542package CFClient::UI::ViewPort;
543
544our @ISA = CFClient::UI::Window::;
545
546sub new { die }
547
548sub size_request {
549 my ($self) = @_;
550
551 @$self{qw(child_w child_h)} = $self->child->size_request;
552 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
553
554 @$self{qw(child_w child_h)}
555}
556
557sub _draw {
558 my ($self) = @_;
559
560 $self->{children}[1]->draw;
561}
562
563
564#############################################################################
565
514package CFClient::UI::Frame; 566package CFClient::UI::Frame;
515 567
516our @ISA = CFClient::UI::Bin::; 568our @ISA = CFClient::UI::Bin::;
517 569
518use SDL::OpenGL; 570use CFClient::OpenGL;
519 571
520sub size_request { 572sub size_request {
521 my ($self) = @_; 573 my ($self) = @_;
522 my $chld = $self->child 574 my $chld = $self->child
523 or return (0, 0); 575 or return (0, 0);
528} 580}
529 581
530sub size_allocate { 582sub size_allocate {
531 my ($self, $x, $y, $w, $h) = @_; 583 my ($self, $x, $y, $w, $h) = @_;
532 584
533 $self->_size_allocate ($x, $y, $w, $h) or return;
534
535 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 585 $self->child->configure (2, 2, $w - 4, $h - 4);
536} 586}
537 587
538sub _draw { 588sub _draw {
539 my ($self) = @_; 589 my ($self) = @_;
540 590
557 607
558package CFClient::UI::FancyFrame; 608package CFClient::UI::FancyFrame;
559 609
560our @ISA = CFClient::UI::Bin::; 610our @ISA = CFClient::UI::Bin::;
561 611
562use SDL::OpenGL; 612use CFClient::OpenGL;
563 613
564my @tex = 614my @tex =
565 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 615 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
566 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 616 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
567 617
571 # TODO: user_x, user_y, overwrite moveto? 621 # TODO: user_x, user_y, overwrite moveto?
572 622
573 $class->SUPER::new ( 623 $class->SUPER::new (
574 bg => [1, 1, 1, 1], 624 bg => [1, 1, 1, 1],
575 border_bg => [1, 1, 1, 1], 625 border_bg => [1, 1, 1, 1],
576 border => int $::FONTSIZE * 0.8, 626 border => 0.8,
577 @_ 627 @_
578 ) 628 )
579} 629}
580 630
631sub border {
632 int $_[0]{border} * $::FONTSIZE
633}
634
581sub size_request { 635sub size_request {
582 my ($self) = @_; 636 my ($self) = @_;
583 637
584 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; 638 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
585 639
586 my ($w, $h) = $self->SUPER::size_request; 640 my ($w, $h) = $self->SUPER::size_request;
587 641
588 ( 642 (
589 $w + $self->{border} * 2, 643 $w + $self->border * 2,
590 $h + $self->{border} * 2, 644 $h + $self->border * 2,
591 ) 645 )
592} 646}
593 647
594sub size_allocate { 648sub size_allocate {
595 my ($self, $x, $y, $w, $h) = @_; 649 my ($self, $w, $h) = @_;
596 650
597 $self->_size_allocate ($x, $y, $w, $h) or return;
598
599 $h -= List::Util::max 0, $self->{border} * 2; 651 $h -= List::Util::max 0, $self->border * 2;
600 $w -= List::Util::max 0, $self->{border} * 2; 652 $w -= List::Util::max 0, $self->border * 2;
601 653
602 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 654 $self->child->configure ($self->border, $self->border, $w, $h);
603} 655}
604 656
605sub button_down { 657sub button_down {
606 my ($self, $ev, $x, $y) = @_; 658 my ($self, $ev, $x, $y) = @_;
607 659
660 my $border = $self->border;
661
608 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 662 if ($x < $self->{w} && $x >= $self->{w} - $border
609 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 663 && $y < $self->{h} && $y >= $self->{h} - $border) {
610 664
611 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 665 my ($ox, $oy) = ($ev->{x}, $ev->{y});
612 my ($bw, $bh) = ($self->{w}, $self->{h}); 666 my ($bw, $bh) = ($self->{w}, $self->{h});
613 667
614 $self->{motion} = sub { 668 $self->{motion} = sub {
615 my ($ev, $x, $y) = @_; 669 my ($ev, $x, $y) = @_;
616 670
617 ($x, $y) = ($ev->motion_x, $ev->motion_y); 671 ($x, $y) = ($ev->{x}, $ev->{y});
618 672
619 $self->{user_w} = $bw + $x - $ox; 673 $self->{user_w} = $bw + $x - $ox;
620 $self->{user_h} = $bh + $y - $oy; 674 $self->{user_h} = $bh + $y - $oy;
621 $self->update; 675 $self->check_size;
622 }; 676 };
623 677
624 } elsif ($x >= 0 && $x < $self->{w} 678 } elsif ($x >= 0 && $x < $self->{w}
625 && $y >= 0 && $y < $self->{border}) { 679 && $y >= 0 && $y < $border) {
626 680
627 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 681 my ($ox, $oy) = ($ev->{x}, $ev->{y});
628 my ($bx, $by) = ($self->{x}, $self->{y}); 682 my ($bx, $by) = ($self->{x}, $self->{y});
629 683
630 $self->{motion} = sub { 684 $self->{motion} = sub {
631 my ($ev, $x, $y) = @_; 685 my ($ev, $x, $y) = @_;
632 686
633 ($x, $y) = ($ev->motion_x, $ev->motion_y); 687 ($x, $y) = ($ev->{x}, $ev->{y});
634 688
635 $self->move ($bx + $x - $ox, $by + $y - $oy); 689 $self->move ($bx + $x - $ox, $by + $y - $oy);
636 $self->update; 690 $self->update;
637 }; 691 };
638 } 692 }
659 glEnable GL_BLEND; 713 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 714 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 715 glEnable GL_TEXTURE_2D;
662 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
663 717
718 my $border = $self->border;
719
664 glColor @{ $self->{border_bg} }; 720 glColor @{ $self->{border_bg} };
665 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 721 $tex[1]->draw_quad (0, 0, $w, $border);
666 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 722 $tex[3]->draw_quad (0, $border, $border, $ch);
667 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 723 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
668 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 724 $tex[4]->draw_quad (0, $h - $border, $w, $border);
669 725
670 my $bg = $tex[0]; 726 my $bg = $tex[0];
671 727
672 # TODO: repeat texture not scale 728 # TODO: repeat texture not scale
673 my $rep_x = $cw / $bg->{w}; 729 my $rep_x = $cw / $bg->{w};
676 glColor @{ $self->{bg} }; 732 glColor @{ $self->{bg} };
677 733
678 $bg->{s} = $rep_x; 734 $bg->{s} = $rep_x;
679 $bg->{t} = $rep_y; 735 $bg->{t} = $rep_y;
680 $bg->{wrap_mode} = 1; 736 $bg->{wrap_mode} = 1;
681 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 737 $bg->draw_quad ($border, $border, $cw, $ch);
682 738
683 glDisable GL_TEXTURE_2D; 739 glDisable GL_TEXTURE_2D;
684 glDisable GL_BLEND; 740 glDisable GL_BLEND;
685 741
686 $self->child->draw; 742 $self->child->draw;
692 748
693our @ISA = CFClient::UI::Base::; 749our @ISA = CFClient::UI::Base::;
694 750
695use List::Util qw(max sum); 751use List::Util qw(max sum);
696 752
697use SDL::OpenGL; 753use CFClient::OpenGL;
698 754
699sub new { 755sub new {
700 my $class = shift; 756 my $class = shift;
701 757
702 $class->SUPER::new ( 758 $class->SUPER::new (
709 my ($self, $x, $y, $child) = @_; 765 my ($self, $x, $y, $child) = @_;
710 766
711 $child->set_parent ($self); 767 $child->set_parent ($self);
712 $self->{children}[$y][$x] = $child; 768 $self->{children}[$y][$x] = $child;
713 769
714 $self->{w} = $self->{h} = -1; 770 $child->check_size;
715 $self->update;
716} 771}
717 772
718# TODO: move to container class maybe? send childs a signal on removal? 773# TODO: move to container class maybe? send childs a signal on removal?
719sub clear { 774sub clear {
720 my ($self) = @_; 775 my ($self) = @_;
755 (sum @$hs), 810 (sum @$hs),
756 ) 811 )
757} 812}
758 813
759sub size_allocate { 814sub size_allocate {
760 my ($self, $x, $y, $w, $h) = @_; 815 my ($self, $w, $h) = @_;
761
762 $self->_size_allocate ($x, $y, $w, $h) or return;
763 816
764 my ($ws, $hs) = $self->get_wh; 817 my ($ws, $hs) = $self->get_wh;
765 818
766 my $req_w = sum @$ws; 819 my $req_w = sum @$ws;
767 my $req_h = sum @$hs; 820 my $req_h = sum @$hs;
789 842
790 for my $c (0 .. $#$row) { 843 for my $c (0 .. $#$row) {
791 my $col_w = $ws->[$c]; 844 my $col_w = $ws->[$c];
792 845
793 if (my $widget = $row->[$c]) { 846 if (my $widget = $row->[$c]) {
794 $widget->size_allocate ($x, $y, $col_w, $row_h); 847 $widget->configure ($x, $y, $col_w, $row_h);
795 } 848 }
796 849
797 $x += $col_w; 850 $x += $col_w;
798 } 851 }
799 852
844 (List::Util::max map $_->[1], @alloc), 897 (List::Util::max map $_->[1], @alloc),
845 ) 898 )
846} 899}
847 900
848sub size_allocate { 901sub size_allocate {
849 my ($self, $x, $y, $w, $h) = @_; 902 my ($self, $w, $h) = @_;
850
851 $self->_size_allocate ($x, $y, $w, $h);
852 903
853 ($h, $w) = ($w, $h); 904 ($h, $w) = ($w, $h);
854 905
855 my $children = $self->{children}; 906 my $children = $self->{children};
856 907
878 929
879 my $y = 0; 930 my $y = 0;
880 for (0 .. $#$children) { 931 for (0 .. $#$children) {
881 my $child = $children->[$_]; 932 my $child = $children->[$_];
882 my $h = $h[$_]; 933 my $h = $h[$_];
883 $child->size_allocate ($y, 0, $h, $w); 934 $child->configure ($y, 0, $h, $w);
884 935
885 $y += $h; 936 $y += $h;
886 } 937 }
938
939 1
887} 940}
888 941
889############################################################################# 942#############################################################################
890 943
891package CFClient::UI::VBox; 944package CFClient::UI::VBox;
904 (List::Util::sum map $_->[1], @alloc), 957 (List::Util::sum map $_->[1], @alloc),
905 ) 958 )
906} 959}
907 960
908sub size_allocate { 961sub size_allocate {
909 my ($self, $x, $y, $w, $h) = @_; 962 my ($self, $w, $h) = @_;
910
911 $self->_size_allocate ($x, $y, $w, $h);
912 963
913 my $children = $self->{children}; 964 my $children = $self->{children};
914 965
915 my @h = map +($_->size_request)[1], @$children; 966 my @h = map +($_->size_request)[1], @$children;
916 967
934 985
935 my $y = 0; 986 my $y = 0;
936 for (0 .. $#$children) { 987 for (0 .. $#$children) {
937 my $child = $children->[$_]; 988 my $child = $children->[$_];
938 my $h = $h[$_]; 989 my $h = $h[$_];
939 $child->size_allocate (0, $y, $w, $h); 990 $child->configure (0, $y, $w, $h);
940 991
941 $y += $h; 992 $y += $h;
942 } 993 }
994
995 1
943} 996}
944 997
945############################################################################# 998#############################################################################
946 999
947package CFClient::UI::Label; 1000package CFClient::UI::Label;
948 1001
949our @ISA = CFClient::UI::Base::; 1002our @ISA = CFClient::UI::Base::;
950 1003
951use SDL::OpenGL; 1004use CFClient::OpenGL;
952 1005
953sub new { 1006sub new {
954 my ($class, %arg) = @_; 1007 my ($class, %arg) = @_;
955 1008
956 my $self = $class->SUPER::new ( 1009 my $self = $class->SUPER::new (
957 fg => [1, 1, 1], 1010 fg => [1, 1, 1],
958 fontsize => $::FONTSIZE, 1011 fontsize => 1,
959 text => "", 1012 text => "",
960 align => -1, 1013 align => -1,
961 valign => -1, 1014 valign => -1,
962 padding => 2, 1015 padding => 2,
963 layout => new CFClient::Layout, 1016 layout => new CFClient::Layout,
964 %arg 1017 %arg
965 ); 1018 );
966 1019
967 $self->set_text ($self->{text}); 1020 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d#
1021
1022 $self->set_text (delete $self->{text}) if exists $self->{text};
1023 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
968 1024
969 $self 1025 $self
970} 1026}
971 1027
972sub escape_text { 1028sub escape_text {
980} 1036}
981 1037
982sub set_text { 1038sub set_text {
983 my ($self, $text) = @_; 1039 my ($self, $text) = @_;
984 1040
985 $self->{text} = $text;
986 $self->{layout}->set_markup ($text); 1041 $self->{layout}->set_text ($text);
987 1042
988 delete $self->{texture}; 1043 delete $self->{texture};
989# $self->{w} = $self->{h} = -1;
990 $self->update; 1044 $self->update;
991} 1045}
992 1046
993sub get_text { 1047sub set_markup {
994 my ($self, $text) = @_; 1048 my ($self, $markup) = @_;
995 1049
996 $self->{text} 1050 $self->{layout}->set_markup ($markup);
1051
1052 delete $self->{texture};
1053 $self->update;
997} 1054}
998 1055
999sub size_request { 1056sub size_request {
1000 my ($self) = @_; 1057 my ($self) = @_;
1001 1058
1002 $self->{layout}->set_width; 1059 $self->{layout}->set_width;
1003 $self->{layout}->set_height ($self->{fontsize}); 1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061
1004 my ($w, $h) = $self->{layout}->size; 1062 my ($w, $h) = $self->{layout}->size;
1005 1063
1006 ( 1064 (
1007 $w + $self->{padding} * 2, 1065 $w + $self->{padding} * 2,
1008 $h + $self->{padding} * 2, 1066 $h + $self->{padding} * 2,
1009 ) 1067 )
1010} 1068}
1011 1069
1012sub size_allocate { 1070sub size_allocate {
1013 my ($self, $x, $y, $w, $h) = @_; 1071 my ($self, $w, $h) = @_;
1014
1015 $self->_size_allocate ($x, $y, $w, $h) or return;
1016 1072
1017 delete $self->{texture}; 1073 delete $self->{texture};
1018}
1019
1020sub update {
1021 my ($self) = @_;
1022
1023 delete $self->{texture};
1024 $self->SUPER::update;
1025} 1074}
1026 1075
1027sub _draw { 1076sub _draw {
1028 my ($self) = @_; 1077 my ($self) = @_;
1029 1078
1030 my $tex = $self->{texture} ||= do { 1079 my $tex = $self->{texture} ||= do {
1031 $self->{layout}->set_width ($self->{w}); 1080 $self->{layout}->set_width ($self->{w});
1032 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1081 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1033 new_from_layout CFClient::Texture $self->{layout} 1082 new_from_layout CFClient::Texture $self->{layout}
1034 }; 1083 };
1035 1084
1036 glEnable GL_BLEND; 1085 glEnable GL_BLEND;
1037 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1086 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1038 glEnable GL_TEXTURE_2D; 1087 glEnable GL_TEXTURE_2D;
1039 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1088 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1040 1089
1041 glColor @{$self->{fg}}; 1090 glColor @{$self->{fg}};
1042 1091
1043 my $x = 1092 $self->{ox} = int (
1044 $self->{align} < 0 ? $self->{padding} 1093 $self->{align} < 0 ? $self->{padding}
1045 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1094 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1046 : ($self->{w} - $tex->{w}) * 0.5; 1095 : ($self->{w} - $tex->{w}) * 0.5
1096 );
1047 1097
1048 my $y = 1098 $self->{oy} = int (
1049 $self->{valign} < 0 ? $self->{padding} 1099 $self->{valign} < 0 ? $self->{padding}
1050 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1100 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1051 : ($self->{h} - $tex->{h}) * 0.5; 1101 : ($self->{h} - $tex->{h}) * 0.5
1102 );
1052 1103
1053 $tex->draw_quad (int $x, int $y); 1104 $tex->draw_quad ($self->{ox}, $self->{oy});
1054 1105
1055 glDisable GL_TEXTURE_2D; 1106 glDisable GL_TEXTURE_2D;
1056 glDisable GL_BLEND; 1107 glDisable GL_BLEND;
1057} 1108}
1058 1109
1059############################################################################# 1110#############################################################################
1060 1111
1061package CFClient::UI::Entry; 1112package CFClient::UI::EntryBase;
1062 1113
1063our @ISA = CFClient::UI::Label::; 1114our @ISA = CFClient::UI::Label::;
1064 1115
1065use SDL; 1116use CFClient::OpenGL;
1066use SDL::OpenGL;
1067 1117
1068sub new { 1118sub new {
1069 my $class = shift; 1119 my $class = shift;
1070 1120
1071 $class->SUPER::new ( 1121 $class->SUPER::new (
1081} 1131}
1082 1132
1083sub _set_text { 1133sub _set_text {
1084 my ($self, $text) = @_; 1134 my ($self, $text) = @_;
1085 1135
1086 my $old_text = $self->{text}; 1136 delete $self->{cur_h};
1137
1138 return if $self->{text} eq $text;
1139
1140 delete $self->{texture};
1087 1141
1088 $self->{last_activity} = $::NOW; 1142 $self->{last_activity} = $::NOW;
1089
1090 $self->{text} = $text; 1143 $self->{text} = $text;
1091 $self->{layout}->set_width ($self->{w});
1092 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1093 1144
1094 $text =~ s/./*/g if $self->{hidden}; 1145 $text =~ s/./*/g if $self->{hidden};
1146 $self->{layout}->set_text ("$text ");
1095 1147
1096 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1148 $self->emit (changed => $self->{text});
1149}
1097 1150
1098 $text = substr $text, 0, $self->{cursor}; 1151sub get_text {
1099 utf8::encode $text; 1152 $_[0]{text}
1100
1101 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1102
1103 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1104 if $old_text ne $self->{text};
1105} 1153}
1106 1154
1107sub size_request { 1155sub size_request {
1108 my ($self) = @_; 1156 my ($self) = @_;
1109 1157
1111 1159
1112 ($w + 1, $h) # add 1 for cursor 1160 ($w + 1, $h) # add 1 for cursor
1113} 1161}
1114 1162
1115sub size_allocate { 1163sub size_allocate {
1116 my ($self, $x, $y, $w, $h) = @_; 1164 my ($self, $w, $h) = @_;
1117
1118 $self->SUPER::size_allocate ($x, $y, $w, $h);
1119 1165
1120 $self->_set_text ($self->{text}); 1166 $self->_set_text ($self->{text});
1121} 1167}
1122 1168
1123sub set_text { 1169sub set_text {
1129} 1175}
1130 1176
1131sub key_down { 1177sub key_down {
1132 my ($self, $ev) = @_; 1178 my ($self, $ev) = @_;
1133 1179
1134 my $mod = $ev->key_mod; 1180 my $mod = $ev->{mod};
1135 my $sym = $ev->key_sym; 1181 my $sym = $ev->{sym};
1136
1137 my $uni = $ev->key_unicode; 1182 my $uni = $ev->{unicode};
1138 1183
1139 my $text = $self->get_text; 1184 my $text = $self->get_text;
1140 1185
1141 if ($sym == SDLK_BACKSPACE) { 1186 if ($sym == 8) {
1142 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1187 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1143 } elsif ($sym == SDLK_DELETE) { 1188 } elsif ($sym == 127) {
1144 substr $text, $self->{cursor}, 1, ""; 1189 substr $text, $self->{cursor}, 1, "";
1145 } elsif ($sym == SDLK_LEFT) { 1190 } elsif ($sym == CFClient::SDLK_LEFT) {
1146 --$self->{cursor} if $self->{cursor}; 1191 --$self->{cursor} if $self->{cursor};
1147 } elsif ($sym == SDLK_RIGHT) { 1192 } elsif ($sym == CFClient::SDLK_RIGHT) {
1148 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1193 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1149 } elsif ($sym == SDLK_HOME) { 1194 } elsif ($sym == CFClient::SDLK_HOME) {
1150 $self->{cursor} = 0; 1195 $self->{cursor} = 0;
1151 } elsif ($sym == SDLK_END) { 1196 } elsif ($sym == CFClient::SDLK_END) {
1152 $self->{cursor} = length $text; 1197 $self->{cursor} = length $text;
1153 } elsif ($sym == SDLK_ESCAPE) { 1198 } elsif ($sym == 27) {
1154 $self->emit ('escape'); 1199 $self->emit ('escape');
1155 } elsif ($uni) { 1200 } elsif ($uni) {
1156 substr $text, $self->{cursor}++, 0, chr $uni; 1201 substr $text, $self->{cursor}++, 0, chr $uni;
1157 } 1202 }
1158 1203
1213 1258
1214 $self->SUPER::_draw; 1259 $self->SUPER::_draw;
1215 1260
1216 #TODO: force update every cursor change :( 1261 #TODO: force update every cursor change :(
1217 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1262 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1263
1264 unless (exists $self->{cur_h}) {
1265 my $text = substr $self->{text}, 0, $self->{cursor};
1266 utf8::encode $text;
1267
1268 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1269 }
1270
1218 glColor @{$self->{fg}}; 1271 glColor @{$self->{fg}};
1219 glBegin GL_LINES; 1272 glBegin GL_LINES;
1220 glVertex $self->{cur_x}, $self->{cur_y};
1221 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1273 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1274 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1222 glEnd; 1275 glEnd;
1223 } 1276 }
1224} 1277}
1225 1278
1226package CFClient::UI::LineEntry; 1279package CFClient::UI::Entry;
1227 1280
1228our @ISA = CFClient::UI::Entry::; 1281our @ISA = CFClient::UI::EntryBase::;
1229 1282
1230use SDL; 1283use CFClient::OpenGL;
1231use SDL::OpenGL;
1232 1284
1233sub key_down { 1285sub key_down {
1234 my ($self, $ev) = @_; 1286 my ($self, $ev) = @_;
1235 1287
1236 my $sym = $ev->key_sym; 1288 my $sym = $ev->{sym};
1237 1289
1238 if ($sym == SDLK_RETURN) { 1290 if ($sym == 13) {
1239 $self->emit (activate => $self->get_text); 1291 $self->emit (activate => $self->get_text);
1240 $self->update; 1292 $self->update;
1241 1293
1242 } else { 1294 } else {
1243 $self->SUPER::key_down ($ev); 1295 $self->SUPER::key_down ($ev);
1249 1301
1250package CFClient::UI::Button; 1302package CFClient::UI::Button;
1251 1303
1252our @ISA = CFClient::UI::Label::; 1304our @ISA = CFClient::UI::Label::;
1253 1305
1254use SDL; 1306use CFClient::OpenGL;
1255use SDL::OpenGL;
1256 1307
1257my @tex = 1308my @tex =
1258 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1259 qw(b1_button_active.png); 1310 qw(b1_button_active.png);
1260 1311
1263 1314
1264 $class->SUPER::new ( 1315 $class->SUPER::new (
1265 padding => 4, 1316 padding => 4,
1266 fg => [1, 1, 1], 1317 fg => [1, 1, 1],
1267 bg => [1, 1, 1, 0.2], 1318 bg => [1, 1, 1, 0.2],
1268 active_fg => [1, 1, 0], 1319 active_fg => [0, 0, 1],
1269 can_hover => 1, 1320 can_hover => 1,
1321 align => 0,
1322 valign => 0,
1270 @_ 1323 @_
1271 ) 1324 )
1272} 1325}
1273 1326
1274sub button_up { 1327sub button_up {
1311 1364
1312my @tex = 1365my @tex =
1313 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1314 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1367 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1315 1368
1316use SDL; 1369use CFClient::OpenGL;
1317use SDL::OpenGL;
1318 1370
1319sub new { 1371sub new {
1320 my $class = shift; 1372 my $class = shift;
1321 1373
1322 $class->SUPER::new ( 1374 $class->SUPER::new (
1333 my ($self) = @_; 1385 my ($self) = @_;
1334 1386
1335 ($self->{padding} * 2 + 6) x 2 1387 ($self->{padding} * 2 + 6) x 2
1336} 1388}
1337 1389
1338sub size_allocate {
1339 my ($self, $x, $y, $w, $h) = @_;
1340
1341 $self->_size_allocate ($x, $y, $w, $h);
1342}
1343
1344sub button_down { 1390sub button_down {
1345 my ($self, $ev, $x, $y) = @_; 1391 my ($self, $ev, $x, $y) = @_;
1346 1392
1347 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1393 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1348 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1394 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1374 glDisable GL_BLEND; 1420 glDisable GL_BLEND;
1375} 1421}
1376 1422
1377############################################################################# 1423#############################################################################
1378 1424
1425package CFClient::UI::VGauge;
1426
1427our @ISA = CFClient::UI::Base::;
1428
1429use CFClient::OpenGL;
1430
1431my %tex = (
1432 food => [
1433 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1434 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1435 ],
1436 grace => [
1437 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1438 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1439 ],
1440 hp => [
1441 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1442 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1443 ],
1444 mana => [
1445 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1446 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1447 ],
1448);
1449
1450# eg. VGauge->new (gauge => 'food'), default gauge: food
1451sub new {
1452 my $class = shift;
1453
1454 my $self = $class->SUPER::new (gauge => 'food', @_);
1455
1456 $self
1457}
1458
1459sub size_request {
1460 my ($self) = @_;
1461
1462 my $tex = $tex{$self->{gauge}}[0];
1463
1464 @$tex{qw(w h)}
1465}
1466
1467sub set_max {
1468 my ($self, $max) = @_;
1469
1470 $self->{max_val} = $max;
1471}
1472
1473sub set_value {
1474 my ($self, $val, $max) = @_;
1475
1476 $self->set_max ($max)
1477 if defined $max;
1478
1479 $max = $self->{max_val};
1480 $self->{val} = $val;
1481
1482 $self->update;
1483}
1484
1485sub _draw {
1486 my ($self) = @_;
1487
1488 my $tex = $tex{$self->{gauge}};
1489
1490 my ($w, $h) = ($self->{w}, $self->{h});
1491
1492 my $ycut = $self->{val} / ($self->{max_val} || 1);
1493 $ycut = 1 if $self->{val} > $self->{max_val};
1494
1495 my $t1 = $tex->[0];
1496 my $t2 = $tex->[1];
1497
1498 glEnable GL_BLEND;
1499 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1500 glEnable GL_TEXTURE_2D;
1501 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1502
1503 my $h1 = $self->{h} - $ycut * $self->{h};
1504 my $h2 = $ycut * $self->{h};
1505
1506 my $yp = 0;
1507
1508 glBindTexture GL_TEXTURE_2D, $t1->{name};
1509 glBegin GL_QUADS;
1510 glTexCoord 0 , 0; glVertex 0 , $yp;
1511 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1;
1512 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1;
1513 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp;
1514 glEnd;
1515
1516 $yp += $h1;
1517
1518 glBindTexture GL_TEXTURE_2D, $t2->{name};
1519 glBegin GL_QUADS;
1520 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp;
1521 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2;
1522 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1523 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp;
1524 glEnd;
1525
1526 glDisable GL_BLEND;
1527 glDisable GL_TEXTURE_2D;
1528}
1529
1530#############################################################################
1531
1379package CFClient::UI::Slider; 1532package CFClient::UI::Slider;
1380 1533
1381use strict; 1534use strict;
1382 1535
1383use SDL::OpenGL; 1536use CFClient::OpenGL;
1384 1537
1385our @ISA = CFClient::UI::DrawBG::; 1538our @ISA = CFClient::UI::DrawBG::;
1386 1539
1387my @tex = 1540my @tex =
1388 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1541 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1398 # TODO: calculations are off 1551 # TODO: calculations are off
1399 my $self = $class->SUPER::new ( 1552 my $self = $class->SUPER::new (
1400 fg => [1, 1, 1], 1553 fg => [1, 1, 1],
1401 active_fg => [0, 0, 0], 1554 active_fg => [0, 0, 0],
1402 range => [0, 0, 100, 10], 1555 range => [0, 0, 100, 10],
1403 req_w => 40, 1556 req_w => 20,
1404 req_h => 13, 1557 req_h => 20,
1405 vertical => 0, 1558 vertical => 0,
1406 can_hover => 1, 1559 can_hover => 1,
1407 inner_pad => 5, 1560 inner_pad => 5,
1408 @_ 1561 @_
1409 ); 1562 );
1513 1666
1514package CFClient::UI::TextView; 1667package CFClient::UI::TextView;
1515 1668
1516our @ISA = CFClient::UI::HBox::; 1669our @ISA = CFClient::UI::HBox::;
1517 1670
1518use SDL::OpenGL; 1671use CFClient::OpenGL;
1519 1672
1520sub new { 1673sub new {
1521 my $class = shift; 1674 my $class = shift;
1522 1675
1523 my $self = $class->SUPER::new ( 1676 my $self = $class->SUPER::new (
1524 req_w => $::WIDTH / 6,
1525 req_h => $::HEIGHT / 6,
1526 fontsize => $::FONTSIZE, 1677 fontsize => 1,
1527 @_, 1678 @_,
1528 1679
1529 layout => (new CFClient::Layout), 1680 layout => (new CFClient::Layout),
1530 par => [], 1681 par => [],
1531 height => 0, 1682 height => 0,
1552sub text_height { 1703sub text_height {
1553 my ($self, $text) = @_; 1704 my ($self, $text) = @_;
1554 1705
1555 my $layout = $self->{layout}; 1706 my $layout = $self->{layout};
1556 1707
1557 $layout->set_height ($self->{fontsize}); 1708 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1558 $layout->set_width ($self->{w}); 1709 $layout->set_width ($self->{w});
1559 $layout->set_text ($text); 1710 $layout->set_text ($text);
1560 1711
1561 ($layout->size)[1] 1712 ($layout->size)[1]
1562} 1713}
1566 1717
1567 $self->{need_reflow}++; 1718 $self->{need_reflow}++;
1568 $self->update; 1719 $self->update;
1569} 1720}
1570 1721
1571sub size_request {
1572 my ($self) = @_;
1573
1574 ($self->{req_w}, $self->{req_h})
1575}
1576
1577sub size_allocate { 1722sub size_allocate {
1578 my ($self, $x, $y, $w, $h) = @_; 1723 my ($self, $w, $h) = @_;
1579 1724
1580 $self->SUPER::size_allocate ($x, $y, $w, $h); 1725 $self->SUPER::size_allocate ($w, $h);
1581 1726
1582 $self->{layout}->set_height ($self->{fontsize}); 1727 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1583 $self->{layout}->set_width ($self->{w}); 1728 $self->{layout}->set_width ($self->{children}[0]{w});
1584 1729
1585 $self->reflow; 1730 $self->reflow;
1586} 1731}
1587 1732
1588sub add_paragraph { 1733sub add_paragraph {
1675 1820
1676} 1821}
1677 1822
1678############################################################################# 1823#############################################################################
1679 1824
1680package CFClient::UI::MapWidget;
1681
1682use strict;
1683
1684use List::Util qw(min max);
1685
1686use SDL;
1687use SDL::OpenGL;
1688
1689our @ISA = CFClient::UI::Base::;
1690
1691sub new {
1692 my $class = shift;
1693
1694 $class->SUPER::new (
1695 z => -1,
1696 can_focus => 1,
1697 list => (glGenLists 1),
1698 @_
1699 )
1700}
1701
1702sub key_down {
1703 print "MAPKEYDOWN\n";
1704}
1705
1706sub key_up {
1707}
1708
1709sub button_down {
1710 my ($self, $ev, $x, $y) = @_;
1711
1712 $self->focus_in;
1713
1714 if ($ev->button == 2) {
1715 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1716 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1717
1718 $self->{motion} = sub {
1719 my ($ev, $x, $y) = @_;
1720
1721 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1722
1723 $::CFG->{map_shift_x} = $bw + $x - $ox;
1724 $::CFG->{map_shift_y} = $bh + $y - $oy;
1725
1726 $self->update;
1727 };
1728 }
1729}
1730
1731sub button_up {
1732 my ($self, $ev, $x, $y) = @_;
1733
1734 delete $self->{motion};
1735}
1736
1737sub mouse_motion {
1738 my ($self, $ev, $x, $y) = @_;
1739
1740 $self->{motion}->($ev, $x, $y) if $self->{motion};
1741}
1742
1743sub size_request {
1744 (
1745 1 + 32 * int $::WIDTH / 32,
1746 1 + 32 * int $::HEIGHT / 32,
1747 )
1748}
1749
1750sub update {
1751 my ($self) = @_;
1752
1753 $self->{need_update} = 1;
1754 $self->SUPER::update;
1755}
1756
1757sub draw {
1758 my ($self) = @_;
1759
1760 if (delete $self->{need_update}) {
1761 glNewList $self->{list}, GL_COMPILE;
1762
1763 if ($::MAP) {
1764 my $sw = int $::WIDTH / 32;
1765 my $sh = int $::HEIGHT / 32;
1766
1767 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1768 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1769
1770 glTranslate $sx0 - 32, $sy0 - 32, 0;
1771
1772 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1773
1774 if ($::CFG->{fow_enable}) {
1775 if ($::CFG->{fow_smooth}) { # smooth fog of war
1776 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1777 glConvolutionFilter2D
1778 GL_CONVOLUTION_2D,
1779 GL_ALPHA,
1780 3, 3,
1781 GL_ALPHA, GL_FLOAT,
1782 pack "f*",
1783 0.1, 0.1, 0.1,
1784 0.1, 0.2, 0.1,
1785 0.1, 0.1, 0.1,
1786 ;
1787 glEnable GL_CONVOLUTION_2D;
1788 }
1789
1790 $self->{fow_texture} = new CFClient::Texture
1791 w => $w,
1792 h => $h,
1793 data => $data,
1794 internalformat => GL_ALPHA,
1795 format => GL_ALPHA;
1796
1797 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1798
1799 glEnable GL_BLEND;
1800 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1801 glEnable GL_TEXTURE_2D;
1802 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1803
1804 glColor +($::CFG->{fow_intensity}) x 3, 1;
1805 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1806
1807 glDisable GL_TEXTURE_2D;
1808 glDisable GL_BLEND;
1809 }
1810
1811 # HACK BEGIN
1812 {
1813 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1814 my ($w, $h) = (250, 250);
1815
1816 glEnable GL_BLEND;
1817 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1818 glEnable GL_TEXTURE_2D;
1819 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1820
1821 $self->{mapmap_texture} =
1822 new CFClient::Texture
1823 w => $w,
1824 h => $h,
1825 data => $::MAP->mapmap ($w, $h),
1826 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1827
1828 $self->{mapmap_texture}->draw_quad (100, 100);
1829
1830 glDisable GL_TEXTURE_2D;
1831 glDisable GL_BLEND;
1832 }
1833 # HACK END
1834 }
1835
1836 glEndList;
1837 }
1838
1839 glPushMatrix;
1840 glCallList $self->{list};
1841 glPopMatrix;
1842
1843 if ($FOCUS != $self) {
1844 glColor 64/255, 64/255, 64/255;
1845 glLogicOp GL_AND;
1846 glEnable GL_COLOR_LOGIC_OP;
1847 glBegin GL_QUADS;
1848 glVertex 0, 0;
1849 glVertex 0, $::HEIGHT;
1850 glVertex $::WIDTH, $::HEIGHT;
1851 glVertex $::WIDTH, 0;
1852 glEnd;
1853 glDisable GL_COLOR_LOGIC_OP;
1854 }
1855}
1856
1857my %DIR = (
1858 SDLK_KP8, [1, "north"],
1859 SDLK_KP9, [2, "northeast"],
1860 SDLK_KP6, [3, "east"],
1861 SDLK_KP3, [4, "southeast"],
1862 SDLK_KP2, [5, "south"],
1863 SDLK_KP1, [6, "southwest"],
1864 SDLK_KP4, [7, "west"],
1865 SDLK_KP7, [8, "northwest"],
1866
1867 SDLK_UP, [1, "north"],
1868 SDLK_RIGHT, [3, "east"],
1869 SDLK_DOWN, [5, "south"],
1870 SDLK_LEFT, [7, "west"],
1871);
1872
1873sub key_down {
1874 my ($self, $ev) = @_;
1875
1876 my $mod = $ev->key_mod;
1877 my $sym = $ev->key_sym;
1878
1879 if ($sym == SDLK_KP5) {
1880 $::CONN->user_send ("command stay fire");
1881 } elsif ($sym == SDLK_a) {
1882 $::CONN->user_send ("command apply");
1883 } elsif ($sym == SDLK_QUOTE) {
1884 $self->emit ('activate_console');
1885 } elsif ($sym == SDLK_SLASH) {
1886 $self->emit ('activate_console' => '/');
1887 } elsif (exists $DIR{$sym}) {
1888 if ($mod & KMOD_SHIFT) {
1889 $self->{shft}++;
1890 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1891 } elsif ($mod & KMOD_CTRL) {
1892 $self->{ctrl}++;
1893 $::CONN->user_send ("command run $DIR{$sym}[0]");
1894 } else {
1895 $::CONN->user_send ("command $DIR{$sym}[1]");
1896 }
1897 }
1898}
1899
1900sub key_up {
1901 my ($self, $ev) = @_;
1902
1903 my $mod = $ev->key_mod;
1904 my $sym = $ev->key_sym;
1905
1906 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1907 $::CONN->user_send ("command fire_stop");
1908 }
1909 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1910 $::CONN->user_send ("command run_stop");
1911 }
1912}
1913
1914#############################################################################
1915
1916package CFClient::UI::Animator; 1825package CFClient::UI::Animator;
1917 1826
1918use SDL::OpenGL; 1827use CFClient::OpenGL;
1919 1828
1920our @ISA = CFClient::UI::Bin::; 1829our @ISA = CFClient::UI::Bin::;
1921 1830
1922sub moveto { 1831sub moveto {
1923 my ($self, $x, $y) = @_; 1832 my ($self, $x, $y) = @_;
1996 1905
1997package CFClient::UI::Root; 1906package CFClient::UI::Root;
1998 1907
1999our @ISA = CFClient::UI::Container::; 1908our @ISA = CFClient::UI::Container::;
2000 1909
2001use SDL::OpenGL; 1910use CFClient::OpenGL;
1911
1912sub check_size {
1913 my ($self) = @_;
1914
1915 $self->configure (0, 0, $::WITH, $::HEIGHT);
1916}
2002 1917
2003sub size_request { 1918sub size_request {
2004 ($::WIDTH, $::HEIGHT) 1919 ($::WIDTH, $::HEIGHT)
2005} 1920}
2006 1921
2007sub size_allocate { 1922sub configure {
2008 my ($self, $x, $y, $w, $h) = @_; 1923 my ($self, $x, $y, $w, $h) = @_;
2009 1924
2010 $self->_size_allocate ($x, $y, $w, $h); 1925 $self->SUPER::configure ($x, $y, $w, $h);
2011 1926
2012 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1927 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2013 for @{$self->{children}}; 1928 for @{$self->{children}};
2014} 1929}
2015 1930
2016sub _topleft { 1931sub _topleft {
2017 my ($self, $x, $y) = @_; 1932 my ($self, $x, $y) = @_;
2020} 1935}
2021 1936
2022sub update { 1937sub update {
2023 my ($self) = @_; 1938 my ($self) = @_;
2024 1939
2025 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 1940 $self->check_size;
2026 ::refresh (); 1941 ::refresh ();
2027} 1942}
2028 1943
2029sub add { 1944sub add {
2030 my ($self, $widget) = @_; 1945 my ($self, $child) = @_;
2031 1946
2032 $self->SUPER::add ($widget); 1947 $self->SUPER::add ($child);
2033
2034 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2035} 1948}
2036 1949
2037sub on_refresh { 1950sub on_refresh {
2038 my ($self, $id, $cb) = @_; 1951 my ($self, $id, $cb) = @_;
2039 1952

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines