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.114 by root, Sat Apr 15 13:56:25 2006 UTC vs.
Revision 1.137 by root, Wed Apr 19 21:38:05 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;
74sub harmonize { 74sub harmonize {
75 my ($vals) = @_; 75 my ($vals) = @_;
76 76
77 my $rem = 0; 77 my $rem = 0;
78 78
79 for ($vals) { 79 for (@$vals) {
80 my $i = int $_ + $rem; 80 my $i = int $_ + $rem;
81 $rem += $_ - $i; 81 $rem += $_ - $i;
82 $_ = $i; 82 $_ = $i;
83 } 83 }
84} 84}
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} = $x; 132 $self->{x} = int $x;
118 $self->{y} = $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) = @_;
176 192
177 return if $FOCUS == $self; 193 return if $FOCUS == $self;
178 return unless $self->{can_focus}; 194 return unless $self->{can_focus};
179 195
180 my $focus = $FOCUS; $FOCUS = $self; 196 my $focus = $FOCUS; $FOCUS = $self;
197
198 $self->emit (focus_in => $focus);
199
181 $focus->update if $focus; 200 $focus->update if $focus;
182 $FOCUS->update; 201 $FOCUS->update;
183} 202}
184 203
185sub focus_out { 204sub focus_out {
186 my ($self) = @_; 205 my ($self) = @_;
187 206
188 return unless $FOCUS == $self; 207 return unless $FOCUS == $self;
189 208
190 my $focus = $FOCUS; undef $FOCUS; 209 my $focus = $FOCUS; undef $FOCUS;
210
211 $self->emit (focus_out => $focus);
212
191 $focus->update if $focus; #? 213 $focus->update if $focus; #?
192} 214}
193 215
194sub mouse_motion { } 216sub mouse_motion { }
195sub button_up { } 217sub button_up { }
219 glPopMatrix; 241 glPopMatrix;
220 242
221 if ($self == $HOVER && $self->{can_hover}) { 243 if ($self == $HOVER && $self->{can_hover}) {
222 my ($x, $y) = @$self{qw(x y)}; 244 my ($x, $y) = @$self{qw(x y)};
223 245
224 glColor 0, 0, 1, 0.2; 246 glColor 1, 0.8, 0.5, 0.2;
225 glEnable GL_BLEND; 247 glEnable GL_BLEND;
226 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 248 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
227 glBegin GL_QUADS; 249 glBegin GL_QUADS;
228 glVertex $x , $y; 250 glVertex $x , $y;
229 glVertex $x + $self->{w}, $y; 251 glVertex $x + $self->{w}, $y;
254 my ($self, $parent) = @_; 276 my ($self, $parent) = @_;
255 277
256 Scalar::Util::weaken ($self->{parent} = $parent); 278 Scalar::Util::weaken ($self->{parent} = $parent);
257} 279}
258 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
259sub update { 295sub update {
260 my ($self) = @_; 296 my ($self) = @_;
261 297
262 $self->{parent}->update 298 $self->{parent}->update
263 if $self->{parent}; 299 if $self->{parent};
264} 300}
265 301
266sub connect { 302sub connect {
267 my ($self, $signal, $cb) = @_; 303 my ($self, $signal, $cb) = @_;
268 304
269 push @{ $self->{cb}{$signal} }, $cb; 305 push @{ $self->{signal_cb}{$signal} }, $cb;
270} 306}
271 307
272sub emit { 308sub emit {
273 my ($self, $signal, @args) = @_; 309 my ($self, $signal, @args) = @_;
274 310
311 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
275 $_->($self, @args) 312 $cb->($self, @args);
276 for @{$self->{cb}{$signal} || []}; 313 }
277} 314}
278 315
279sub DESTROY { 316sub DESTROY {
280 my ($self) = @_; 317 my ($self) = @_;
281 318
361 $self->{children} = [ 398 $self->{children} = [
362 sort { $a->{z} <=> $b->{z} } 399 sort { $a->{z} <=> $b->{z} }
363 @{$self->{children}}, $child 400 @{$self->{children}}, $child
364 ]; 401 ];
365 402
366 $self->{w} = $self->{h} = -1; 403 $child->check_size;
367 $self->update;
368} 404}
369 405
370sub remove { 406sub remove {
371 my ($self, $widget) = @_; 407 my ($self, $child) = @_;
372 408
409 delete $child->{parent};
410
373 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
374 412
375 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 413 $self->check_size;
376} 414}
377 415
378sub find_widget { 416sub find_widget {
379 my ($self, $x, $y) = @_; 417 my ($self, $x, $y) = @_;
380 418
410 448
411 $class->SUPER::new (children => [$child], %arg) 449 $class->SUPER::new (children => [$child], %arg)
412} 450}
413 451
414sub add { 452sub add {
415 my ($self, $widget) = @_; 453 my ($self, $child) = @_;
416 454
417 $self->{children} = []; 455 $self->{children} = [];
418 456
419 $self->SUPER::add ($widget); 457 $self->SUPER::add ($child);
420} 458}
421 459
422sub remove { 460sub remove {
423 my ($self, $widget) = @_; 461 my ($self, $widget) = @_;
424 462
433sub size_request { 471sub size_request {
434 $_[0]{children}[0]->size_request 472 $_[0]{children}[0]->size_request
435} 473}
436 474
437sub size_allocate { 475sub size_allocate {
438 my ($self, $x, $y, $w, $h) = @_; 476 my ($self, $w, $h) = @_;
439 477
440 $self->_size_allocate ($x, $y, $w, $h) or return;
441
442 $self->{children}[0]->size_allocate (0, 0, $w, $h); 478 $self->{children}[0]->configure (0, 0, $w, $h);
443} 479}
444 480
445############################################################################# 481#############################################################################
446 482
447package CFClient::UI::Window; 483package CFClient::UI::Window;
473 $self->child->draw; 509 $self->child->draw;
474 }; 510 };
475} 511}
476 512
477sub size_allocate { 513sub size_allocate {
478 my ($self, $x, $y, $w, $h) = @_; 514 my ($self, $w, $h) = @_;
479 515
480 $self->_size_allocate ($x, $y, $w, $h) or return;
481
482 $self->child->size_allocate (0, 0, $w, $h); 516 $self->child->configure (0, 0, $w, $h);
483 517
484 $self->render_chld; 518 $self->render_chld;
485} 519}
486 520
487sub _draw { 521sub _draw {
503 glDisable GL_TEXTURE_2D; 537 glDisable GL_TEXTURE_2D;
504} 538}
505 539
506############################################################################# 540#############################################################################
507 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
508package CFClient::UI::Frame; 566package CFClient::UI::Frame;
509 567
510our @ISA = CFClient::UI::Bin::; 568our @ISA = CFClient::UI::Bin::;
511 569
512use SDL::OpenGL; 570use SDL::OpenGL;
522} 580}
523 581
524sub size_allocate { 582sub size_allocate {
525 my ($self, $x, $y, $w, $h) = @_; 583 my ($self, $x, $y, $w, $h) = @_;
526 584
527 $self->_size_allocate ($x, $y, $w, $h) or return;
528
529 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 585 $self->child->configure (2, 2, $w - 4, $h - 4);
530} 586}
531 587
532sub _draw { 588sub _draw {
533 my ($self) = @_; 589 my ($self) = @_;
534 590
565 # TODO: user_x, user_y, overwrite moveto? 621 # TODO: user_x, user_y, overwrite moveto?
566 622
567 $class->SUPER::new ( 623 $class->SUPER::new (
568 bg => [1, 1, 1, 1], 624 bg => [1, 1, 1, 1],
569 border_bg => [1, 1, 1, 1], 625 border_bg => [1, 1, 1, 1],
570 border => $::FONTSIZE * 0.8, 626 border => 0.8,
571 @_ 627 @_
572 ) 628 )
573} 629}
574 630
631sub border {
632 int $_[0]{border} * $::FONTSIZE
633}
634
575sub size_request { 635sub size_request {
576 my ($self) = @_; 636 my ($self) = @_;
577 637
578 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};
579 639
580 my ($w, $h) = $self->SUPER::size_request; 640 my ($w, $h) = $self->SUPER::size_request;
581 641
582 ( 642 (
583 $w + $self->{border} * 2, 643 $w + $self->border * 2,
584 $h + $self->{border} * 2, 644 $h + $self->border * 2,
585 ) 645 )
586} 646}
587 647
588sub size_allocate { 648sub size_allocate {
589 my ($self, $x, $y, $w, $h) = @_; 649 my ($self, $w, $h) = @_;
590 650
591 $self->_size_allocate ($x, $y, $w, $h) or return;
592
593 $h -= List::Util::max 0, $self->{border} * 2; 651 $h -= List::Util::max 0, $self->border * 2;
594 $w -= List::Util::max 0, $self->{border} * 2; 652 $w -= List::Util::max 0, $self->border * 2;
595 653
596 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 654 $self->child->configure ($self->border, $self->border, $w, $h);
597} 655}
598 656
599sub button_down { 657sub button_down {
600 my ($self, $ev, $x, $y) = @_; 658 my ($self, $ev, $x, $y) = @_;
601 659
660 my $border = $self->border;
661
602 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 662 if ($x < $self->{w} && $x >= $self->{w} - $border
603 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 663 && $y < $self->{h} && $y >= $self->{h} - $border) {
604 664
605 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 665 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
606 my ($bw, $bh) = ($self->{w}, $self->{h}); 666 my ($bw, $bh) = ($self->{w}, $self->{h});
607 667
608 $self->{motion} = sub { 668 $self->{motion} = sub {
610 670
611 ($x, $y) = ($ev->motion_x, $ev->motion_y); 671 ($x, $y) = ($ev->motion_x, $ev->motion_y);
612 672
613 $self->{user_w} = $bw + $x - $ox; 673 $self->{user_w} = $bw + $x - $ox;
614 $self->{user_h} = $bh + $y - $oy; 674 $self->{user_h} = $bh + $y - $oy;
615 $self->update; 675 $self->check_size;
616 }; 676 };
617 677
618 } elsif ($x >= 0 && $x < $self->{w} 678 } elsif ($x >= 0 && $x < $self->{w}
619 && $y >= 0 && $y < $self->{border}) { 679 && $y >= 0 && $y < $border) {
620 680
621 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 681 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
622 my ($bx, $by) = ($self->{x}, $self->{y}); 682 my ($bx, $by) = ($self->{x}, $self->{y});
623 683
624 $self->{motion} = sub { 684 $self->{motion} = sub {
653 glEnable GL_BLEND; 713 glEnable GL_BLEND;
654 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 714 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
655 glEnable GL_TEXTURE_2D; 715 glEnable GL_TEXTURE_2D;
656 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
657 717
718 my $border = $self->border;
719
658 glColor @{ $self->{border_bg} }; 720 glColor @{ $self->{border_bg} };
659 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 721 $tex[1]->draw_quad (0, 0, $w, $border);
660 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 722 $tex[3]->draw_quad (0, $border, $border, $ch);
661 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 723 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
662 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 724 $tex[4]->draw_quad (0, $h - $border, $w, $border);
663 725
664 my $bg = $tex[0]; 726 my $bg = $tex[0];
665 727
666 # TODO: repeat texture not scale 728 # TODO: repeat texture not scale
667 my $rep_x = $cw / $bg->{w}; 729 my $rep_x = $cw / $bg->{w};
670 glColor @{ $self->{bg} }; 732 glColor @{ $self->{bg} };
671 733
672 $bg->{s} = $rep_x; 734 $bg->{s} = $rep_x;
673 $bg->{t} = $rep_y; 735 $bg->{t} = $rep_y;
674 $bg->{wrap_mode} = 1; 736 $bg->{wrap_mode} = 1;
675 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 737 $bg->draw_quad ($border, $border, $cw, $ch);
676 738
677 glDisable GL_TEXTURE_2D; 739 glDisable GL_TEXTURE_2D;
678 glDisable GL_BLEND; 740 glDisable GL_BLEND;
679 741
680 $self->child->draw; 742 $self->child->draw;
703 my ($self, $x, $y, $child) = @_; 765 my ($self, $x, $y, $child) = @_;
704 766
705 $child->set_parent ($self); 767 $child->set_parent ($self);
706 $self->{children}[$y][$x] = $child; 768 $self->{children}[$y][$x] = $child;
707 769
708 $self->{w} = $self->{h} = -1; 770 $child->check_size;
771}
772
773# TODO: move to container class maybe? send childs a signal on removal?
774sub clear {
775 my ($self) = @_;
776
777 delete $self->{children};
709 $self->update; 778 $self->update;
710} 779}
711 780
712sub get_wh { 781sub get_wh {
713 my ($self) = @_; 782 my ($self) = @_;
741 (sum @$hs), 810 (sum @$hs),
742 ) 811 )
743} 812}
744 813
745sub size_allocate { 814sub size_allocate {
746 my ($self, $x, $y, $w, $h) = @_; 815 my ($self, $w, $h) = @_;
747
748 $self->_size_allocate ($x, $y, $w, $h) or return;
749 816
750 my ($ws, $hs) = $self->get_wh; 817 my ($ws, $hs) = $self->get_wh;
751 818
752 my $req_w = sum @$ws; 819 my $req_w = sum @$ws;
753 my $req_h = sum @$hs; 820 my $req_h = sum @$hs;
775 842
776 for my $c (0 .. $#$row) { 843 for my $c (0 .. $#$row) {
777 my $col_w = $ws->[$c]; 844 my $col_w = $ws->[$c];
778 845
779 if (my $widget = $row->[$c]) { 846 if (my $widget = $row->[$c]) {
780 $widget->size_allocate ($x, $y, $col_w, $row_h); 847 $widget->configure ($x, $y, $col_w, $row_h);
781 } 848 }
782 849
783 $x += $col_w; 850 $x += $col_w;
784 } 851 }
785 852
830 (List::Util::max map $_->[1], @alloc), 897 (List::Util::max map $_->[1], @alloc),
831 ) 898 )
832} 899}
833 900
834sub size_allocate { 901sub size_allocate {
835 my ($self, $x, $y, $w, $h) = @_; 902 my ($self, $w, $h) = @_;
836
837 $self->_size_allocate ($x, $y, $w, $h);
838 903
839 ($h, $w) = ($w, $h); 904 ($h, $w) = ($w, $h);
840 905
841 my $children = $self->{children}; 906 my $children = $self->{children};
842 907
864 929
865 my $y = 0; 930 my $y = 0;
866 for (0 .. $#$children) { 931 for (0 .. $#$children) {
867 my $child = $children->[$_]; 932 my $child = $children->[$_];
868 my $h = $h[$_]; 933 my $h = $h[$_];
869 $child->size_allocate ($y, 0, $h, $w); 934 $child->configure ($y, 0, $h, $w);
870 935
871 $y += $h; 936 $y += $h;
872 } 937 }
938
939 1
873} 940}
874 941
875############################################################################# 942#############################################################################
876 943
877package CFClient::UI::VBox; 944package CFClient::UI::VBox;
890 (List::Util::sum map $_->[1], @alloc), 957 (List::Util::sum map $_->[1], @alloc),
891 ) 958 )
892} 959}
893 960
894sub size_allocate { 961sub size_allocate {
895 my ($self, $x, $y, $w, $h) = @_; 962 my ($self, $w, $h) = @_;
896
897 $self->_size_allocate ($x, $y, $w, $h);
898 963
899 my $children = $self->{children}; 964 my $children = $self->{children};
900 965
901 my @h = map +($_->size_request)[1], @$children; 966 my @h = map +($_->size_request)[1], @$children;
902 967
920 985
921 my $y = 0; 986 my $y = 0;
922 for (0 .. $#$children) { 987 for (0 .. $#$children) {
923 my $child = $children->[$_]; 988 my $child = $children->[$_];
924 my $h = $h[$_]; 989 my $h = $h[$_];
925 $child->size_allocate (0, $y, $w, $h); 990 $child->configure (0, $y, $w, $h);
926 991
927 $y += $h; 992 $y += $h;
928 } 993 }
994
995 1
929} 996}
930 997
931############################################################################# 998#############################################################################
932 999
933package CFClient::UI::Label; 1000package CFClient::UI::Label;
939sub new { 1006sub new {
940 my ($class, %arg) = @_; 1007 my ($class, %arg) = @_;
941 1008
942 my $self = $class->SUPER::new ( 1009 my $self = $class->SUPER::new (
943 fg => [1, 1, 1], 1010 fg => [1, 1, 1],
944 fontsize => $::FONTSIZE, 1011 fontsize => 1,
945 text => "", 1012 text => "",
946 align => -1, 1013 align => -1,
1014 valign => -1,
947 padding => 2, 1015 padding => 2,
948 layout => new CFClient::Layout, 1016 layout => new CFClient::Layout,
949 %arg 1017 %arg
950 ); 1018 );
951 1019
952 $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};
953 1024
954 $self 1025 $self
955} 1026}
956 1027
957sub escape_text { 1028sub escape_text {
965} 1036}
966 1037
967sub set_text { 1038sub set_text {
968 my ($self, $text) = @_; 1039 my ($self, $text) = @_;
969 1040
970 $self->{text} = $text;
971 $self->{layout}->set_markup ($text); 1041 $self->{layout}->set_text ($text);
972 1042
973 delete $self->{texture}; 1043 delete $self->{texture};
974# $self->{w} = $self->{h} = -1;
975 $self->update; 1044 $self->update;
976} 1045}
977 1046
978sub get_text { 1047sub set_markup {
979 my ($self, $text) = @_; 1048 my ($self, $markup) = @_;
980 1049
981 $self->{text} 1050 $self->{layout}->set_markup ($markup);
1051
1052 delete $self->{texture};
1053 $self->update;
982} 1054}
983 1055
984sub size_request { 1056sub size_request {
985 my ($self) = @_; 1057 my ($self) = @_;
986 1058
987 $self->{layout}->set_width; 1059 $self->{layout}->set_width;
988 $self->{layout}->set_height ($self->{fontsize}); 1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061
989 my ($w, $h) = $self->{layout}->size; 1062 my ($w, $h) = $self->{layout}->size;
990 1063
991 ( 1064 (
992 $w + $self->{padding} * 2, 1065 $w + $self->{padding} * 2,
993 $h + $self->{padding} * 2, 1066 $h + $self->{padding} * 2,
994 ) 1067 )
995} 1068}
996 1069
997sub size_allocate { 1070sub size_allocate {
998 my ($self, $x, $y, $w, $h) = @_; 1071 my ($self, $w, $h) = @_;
999
1000 $self->_size_allocate ($x, $y, $w, $h) or return;
1001 1072
1002 delete $self->{texture}; 1073 delete $self->{texture};
1003}
1004
1005sub update {
1006 my ($self) = @_;
1007
1008 delete $self->{texture};
1009 $self->SUPER::update;
1010} 1074}
1011 1075
1012sub _draw { 1076sub _draw {
1013 my ($self) = @_; 1077 my ($self) = @_;
1014 1078
1015 my $tex = $self->{texture} ||= do { 1079 my $tex = $self->{texture} ||= do {
1016 $self->{layout}->set_width ($self->{w}); 1080 $self->{layout}->set_width ($self->{w});
1017 $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);
1018 new_from_layout CFClient::Texture $self->{layout} 1082 new_from_layout CFClient::Texture $self->{layout}
1019 }; 1083 };
1020 1084
1021 glEnable GL_BLEND; 1085 glEnable GL_BLEND;
1022 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1086 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1023 glEnable GL_TEXTURE_2D; 1087 glEnable GL_TEXTURE_2D;
1024 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1088 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1025 1089
1026 glColor @{$self->{fg}}; 1090 glColor @{$self->{fg}};
1027 1091
1028 my $x = 1092 $self->{ox} = int (
1029 $self->{align} < 0 ? $self->{padding} 1093 $self->{align} < 0 ? $self->{padding}
1030 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1094 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1031 : ($self->{w} - $tex->{w}) * 0.5; 1095 : ($self->{w} - $tex->{w}) * 0.5
1096 );
1032 1097
1033 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1098 $self->{oy} = int (
1099 $self->{valign} < 0 ? $self->{padding}
1100 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1101 : ($self->{h} - $tex->{h}) * 0.5
1102 );
1103
1104 $tex->draw_quad ($self->{ox}, $self->{oy});
1034 1105
1035 glDisable GL_TEXTURE_2D; 1106 glDisable GL_TEXTURE_2D;
1036 glDisable GL_BLEND; 1107 glDisable GL_BLEND;
1037} 1108}
1038 1109
1039############################################################################# 1110#############################################################################
1040 1111
1041package CFClient::UI::Entry; 1112package CFClient::UI::EntryBase;
1042 1113
1043our @ISA = CFClient::UI::Label::; 1114our @ISA = CFClient::UI::Label::;
1044 1115
1045use SDL;
1046use SDL::OpenGL; 1116use SDL::OpenGL;
1047 1117
1048sub new { 1118sub new {
1049 my $class = shift; 1119 my $class = shift;
1050 1120
1053 bg => [0, 0, 0, 0.2], 1123 bg => [0, 0, 0, 0.2],
1054 active_bg => [1, 1, 1, 0.5], 1124 active_bg => [1, 1, 1, 0.5],
1055 active_fg => [0, 0, 0], 1125 active_fg => [0, 0, 0],
1056 can_hover => 1, 1126 can_hover => 1,
1057 can_focus => 1, 1127 can_focus => 1,
1128 valign => 0,
1058 @_ 1129 @_
1059 ) 1130 )
1060} 1131}
1061 1132
1062sub _set_text { 1133sub _set_text {
1063 my ($self, $text) = @_; 1134 my ($self, $text) = @_;
1064 1135
1065 my $old_text = $self->{text}; 1136 delete $self->{cur_h};
1137
1138 return if $self->{text} eq $text;
1139
1140 delete $self->{texture};
1066 1141
1067 $self->{last_activity} = $::NOW; 1142 $self->{last_activity} = $::NOW;
1068
1069 $self->{text} = $text; 1143 $self->{text} = $text;
1070 $self->{layout}->set_width ($self->{w});
1071 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1072 1144
1073 $text =~ s/./*/g if $self->{hidden}; 1145 $text =~ s/./*/g if $self->{hidden};
1146 $self->{layout}->set_text ("$text ");
1074 1147
1075 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1148 $self->emit (changed => $self->{text});
1149}
1076 1150
1077 $text = substr $text, 0, $self->{cursor}; 1151sub get_text {
1078 utf8::encode $text; 1152 $_[0]{text}
1079
1080 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1081
1082 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1083 if $old_text ne $self->{text};
1084} 1153}
1085 1154
1086sub size_request { 1155sub size_request {
1087 my ($self) = @_; 1156 my ($self) = @_;
1088 1157
1090 1159
1091 ($w + 1, $h) # add 1 for cursor 1160 ($w + 1, $h) # add 1 for cursor
1092} 1161}
1093 1162
1094sub size_allocate { 1163sub size_allocate {
1095 my ($self, $x, $y, $w, $h) = @_; 1164 my ($self, $w, $h) = @_;
1096
1097 $self->SUPER::size_allocate ($x, $y, $w, $h);
1098 1165
1099 $self->_set_text ($self->{text}); 1166 $self->_set_text ($self->{text});
1100} 1167}
1101 1168
1102sub set_text { 1169sub set_text {
1108} 1175}
1109 1176
1110sub key_down { 1177sub key_down {
1111 my ($self, $ev) = @_; 1178 my ($self, $ev) = @_;
1112 1179
1113 my $mod = $ev->key_mod; 1180 my $mod = $ev->{mod};
1114 my $sym = $ev->key_sym; 1181 my $sym = $ev->{sym};
1115
1116 my $uni = $ev->key_unicode; 1182 my $uni = $ev->{unicode};
1117 1183
1118 my $text = $self->get_text; 1184 my $text = $self->get_text;
1119 1185
1120 if ($sym == SDLK_BACKSPACE) { 1186 if ($sym == 8) {
1121 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1187 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1122 } elsif ($sym == SDLK_DELETE) { 1188 } elsif ($sym == 127) {
1123 substr $text, $self->{cursor}, 1, ""; 1189 substr $text, $self->{cursor}, 1, "";
1124 } elsif ($sym == SDLK_LEFT) { 1190 } elsif ($sym == CFClient::SDLK_LEFT) {
1125 --$self->{cursor} if $self->{cursor}; 1191 --$self->{cursor} if $self->{cursor};
1126 } elsif ($sym == SDLK_RIGHT) { 1192 } elsif ($sym == CFClient::SDLK_RIGHT) {
1127 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1193 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1128 } elsif ($sym == SDLK_HOME) { 1194 } elsif ($sym == CFClient::SDLK_HOME) {
1129 $self->{cursor} = 0; 1195 $self->{cursor} = 0;
1130 } elsif ($sym == SDLK_END) { 1196 } elsif ($sym == CFClient::SDLK_END) {
1131 $self->{cursor} = length $text; 1197 $self->{cursor} = length $text;
1132 } elsif ($sym == SDLK_ESCAPE) { 1198 } elsif ($sym == 27) {
1133 $self->emit ('escape'); 1199 $self->emit ('escape');
1134 } elsif ($uni) { 1200 } elsif ($uni) {
1135 substr $text, $self->{cursor}++, 0, chr $uni; 1201 substr $text, $self->{cursor}++, 0, chr $uni;
1136 } 1202 }
1137 1203
1192 1258
1193 $self->SUPER::_draw; 1259 $self->SUPER::_draw;
1194 1260
1195 #TODO: force update every cursor change :( 1261 #TODO: force update every cursor change :(
1196 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
1197 glColor @{$self->{fg}}; 1271 glColor @{$self->{fg}};
1198 glBegin GL_LINES; 1272 glBegin GL_LINES;
1199 glVertex $self->{cur_x}, $self->{cur_y};
1200 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};
1201 glEnd; 1275 glEnd;
1202 } 1276 }
1203} 1277}
1204 1278
1205package CFClient::UI::LineEntry; 1279package CFClient::UI::Entry;
1206 1280
1207our @ISA = CFClient::UI::Entry::; 1281our @ISA = CFClient::UI::EntryBase::;
1208 1282
1209use SDL;
1210use SDL::OpenGL; 1283use SDL::OpenGL;
1211 1284
1212sub key_down { 1285sub key_down {
1213 my ($self, $ev) = @_; 1286 my ($self, $ev) = @_;
1214 1287
1215 my $sym = $ev->key_sym; 1288 my $sym = $ev->{sym};
1216 1289
1217 if ($sym == SDLK_RETURN) { 1290 if ($sym == 13) {
1218 $self->emit (activate => $self->get_text); 1291 $self->emit (activate => $self->get_text);
1219 $self->update; 1292 $self->update;
1220 1293
1221 } else { 1294 } else {
1222 $self->SUPER::key_down ($ev); 1295 $self->SUPER::key_down ($ev);
1228 1301
1229package CFClient::UI::Button; 1302package CFClient::UI::Button;
1230 1303
1231our @ISA = CFClient::UI::Label::; 1304our @ISA = CFClient::UI::Label::;
1232 1305
1233use SDL;
1234use SDL::OpenGL; 1306use SDL::OpenGL;
1235 1307
1236my @tex = 1308my @tex =
1237 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1238 qw(b1_button_active.png); 1310 qw(b1_button_active.png);
1242 1314
1243 $class->SUPER::new ( 1315 $class->SUPER::new (
1244 padding => 4, 1316 padding => 4,
1245 fg => [1, 1, 1], 1317 fg => [1, 1, 1],
1246 bg => [1, 1, 1, 0.2], 1318 bg => [1, 1, 1, 0.2],
1247 active_fg => [1, 1, 0], 1319 active_fg => [0, 0, 1],
1248 can_hover => 1, 1320 can_hover => 1,
1321 align => 0,
1322 valign => 0,
1249 @_ 1323 @_
1250 ) 1324 )
1251} 1325}
1252 1326
1253sub button_up { 1327sub button_up {
1261 1335
1262sub _draw { 1336sub _draw {
1263 my ($self) = @_; 1337 my ($self) = @_;
1264 1338
1265 local $self->{fg} = $self->{fg}; 1339 local $self->{fg} = $self->{fg};
1266 my $tex = $tex[0];
1267
1268 glEnable GL_BLEND;
1269 glEnable GL_TEXTURE_2D;
1270 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1271 1340
1272 if ($GRAB == $self) { 1341 if ($GRAB == $self) {
1273 $self->{fg} = $self->{active_fg}; 1342 $self->{fg} = $self->{active_fg};
1274 } 1343 }
1275 1344
1276 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1345 glEnable GL_BLEND;
1346 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1347 glEnable GL_TEXTURE_2D;
1277 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1348 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1349 glColor 0, 0, 0, 1;
1278 1350
1279 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1351 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1280 1352
1281 glDisable GL_TEXTURE_2D; 1353 glDisable GL_TEXTURE_2D;
1282 glDisable GL_BLEND; 1354 glDisable GL_BLEND;
1283 1355
1284 $self->SUPER::_draw; 1356 $self->SUPER::_draw;
1292 1364
1293my @tex = 1365my @tex =
1294 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1295 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1367 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1296 1368
1297use SDL;
1298use SDL::OpenGL; 1369use SDL::OpenGL;
1299 1370
1300sub new { 1371sub new {
1301 my $class = shift; 1372 my $class = shift;
1302 1373
1314 my ($self) = @_; 1385 my ($self) = @_;
1315 1386
1316 ($self->{padding} * 2 + 6) x 2 1387 ($self->{padding} * 2 + 6) x 2
1317} 1388}
1318 1389
1319sub size_allocate {
1320 my ($self, $x, $y, $w, $h) = @_;
1321
1322 $self->_size_allocate ($x, $y, $w, $h);
1323}
1324
1325sub button_down { 1390sub button_down {
1326 my ($self, $ev, $x, $y) = @_; 1391 my ($self, $ev, $x, $y) = @_;
1327 1392
1328 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1393 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1329 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1394 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1351 1416
1352 $tex->draw_quad (0, 0, $s, $s); 1417 $tex->draw_quad (0, 0, $s, $s);
1353 1418
1354 glDisable GL_TEXTURE_2D; 1419 glDisable GL_TEXTURE_2D;
1355 glDisable GL_BLEND; 1420 glDisable GL_BLEND;
1421}
1422
1423#############################################################################
1424
1425package CFClient::UI::VGauge;
1426
1427our @ISA = CFClient::UI::Base::;
1428
1429use SDL::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;
1356} 1528}
1357 1529
1358############################################################################# 1530#############################################################################
1359 1531
1360package CFClient::UI::Slider; 1532package CFClient::UI::Slider;
1379 # TODO: calculations are off 1551 # TODO: calculations are off
1380 my $self = $class->SUPER::new ( 1552 my $self = $class->SUPER::new (
1381 fg => [1, 1, 1], 1553 fg => [1, 1, 1],
1382 active_fg => [0, 0, 0], 1554 active_fg => [0, 0, 0],
1383 range => [0, 0, 100, 10], 1555 range => [0, 0, 100, 10],
1384 req_w => 40, 1556 req_w => 20,
1385 req_h => 13, 1557 req_h => 20,
1386 vertical => 0, 1558 vertical => 0,
1387 can_hover => 1, 1559 can_hover => 1,
1388 inner_pad => 5, 1560 inner_pad => 5,
1389 @_ 1561 @_
1390 ); 1562 );
1500 1672
1501sub new { 1673sub new {
1502 my $class = shift; 1674 my $class = shift;
1503 1675
1504 my $self = $class->SUPER::new ( 1676 my $self = $class->SUPER::new (
1505 req_w => $::WIDTH / 6,
1506 req_h => $::HEIGHT / 6,
1507 fontsize => $::FONTSIZE, 1677 fontsize => 1,
1508 @_, 1678 @_,
1509 1679
1510 layout => (new CFClient::Layout), 1680 layout => (new CFClient::Layout),
1511 par => [], 1681 par => [],
1512 height => 0, 1682 height => 0,
1533sub text_height { 1703sub text_height {
1534 my ($self, $text) = @_; 1704 my ($self, $text) = @_;
1535 1705
1536 my $layout = $self->{layout}; 1706 my $layout = $self->{layout};
1537 1707
1538 $layout->set_height ($self->{fontsize}); 1708 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1539 $layout->set_width ($self->{w}); 1709 $layout->set_width ($self->{w});
1540 $layout->set_text ($text); 1710 $layout->set_text ($text);
1541 1711
1542 ($layout->size)[1] 1712 ($layout->size)[1]
1543} 1713}
1547 1717
1548 $self->{need_reflow}++; 1718 $self->{need_reflow}++;
1549 $self->update; 1719 $self->update;
1550} 1720}
1551 1721
1552sub size_request {
1553 my ($self) = @_;
1554
1555 ($self->{req_w}, $self->{req_h})
1556}
1557
1558sub size_allocate { 1722sub size_allocate {
1559 my ($self, $x, $y, $w, $h) = @_; 1723 my ($self, $w, $h) = @_;
1560 1724
1561 $self->SUPER::size_allocate ($x, $y, $w, $h); 1725 $self->SUPER::size_allocate ($w, $h);
1562 1726
1563 $self->{layout}->set_height ($self->{fontsize}); 1727 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1564 $self->{layout}->set_width ($self->{w}); 1728 $self->{layout}->set_width ($self->{children}[0]{w});
1565 1729
1566 $self->reflow; 1730 $self->reflow;
1567} 1731}
1568 1732
1569sub add_paragraph { 1733sub add_paragraph {
1652 glDisable GL_TEXTURE_2D; 1816 glDisable GL_TEXTURE_2D;
1653 } 1817 }
1654 1818
1655 $self->{children}[1]->draw; 1819 $self->{children}[1]->draw;
1656 1820
1657}
1658
1659#############################################################################
1660
1661package CFClient::UI::MapWidget;
1662
1663use strict;
1664
1665use List::Util qw(min max);
1666
1667use SDL;
1668use SDL::OpenGL;
1669
1670our @ISA = CFClient::UI::Base::;
1671
1672sub new {
1673 my $class = shift;
1674
1675 $class->SUPER::new (
1676 z => -1,
1677 can_focus => 1,
1678 list => (glGenLists 1),
1679 @_
1680 )
1681}
1682
1683sub key_down {
1684 print "MAPKEYDOWN\n";
1685}
1686
1687sub key_up {
1688}
1689
1690sub button_down {
1691 my ($self, $ev, $x, $y) = @_;
1692
1693 $self->focus_in;
1694
1695 if ($ev->button == 2) {
1696 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1697 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1698
1699 $self->{motion} = sub {
1700 my ($ev, $x, $y) = @_;
1701
1702 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1703
1704 $::CFG->{map_shift_x} = $bw + $x - $ox;
1705 $::CFG->{map_shift_y} = $bh + $y - $oy;
1706
1707 $self->update;
1708 };
1709 }
1710}
1711
1712sub button_up {
1713 my ($self, $ev, $x, $y) = @_;
1714
1715 delete $self->{motion};
1716}
1717
1718sub mouse_motion {
1719 my ($self, $ev, $x, $y) = @_;
1720
1721 $self->{motion}->($ev, $x, $y) if $self->{motion};
1722}
1723
1724sub size_request {
1725 (
1726 1 + 32 * int $::WIDTH / 32,
1727 1 + 32 * int $::HEIGHT / 32,
1728 )
1729}
1730
1731sub update {
1732 my ($self) = @_;
1733
1734 $self->{need_update} = 1;
1735 $self->SUPER::update;
1736}
1737
1738sub draw {
1739 my ($self) = @_;
1740
1741 if (delete $self->{need_update}) {
1742 glNewList $self->{list}, GL_COMPILE;
1743
1744 if ($::MAP) {
1745 my $sw = int $::WIDTH / 32;
1746 my $sh = int $::HEIGHT / 32;
1747
1748 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1749 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1750
1751 glTranslate $sx0 - 32, $sy0 - 32, 0;
1752
1753 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1754
1755 if ($::CFG->{fow_enable}) {
1756 if ($::CFG->{fow_smooth}) { # smooth fog of war
1757 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1758 glConvolutionFilter2D
1759 GL_CONVOLUTION_2D,
1760 GL_ALPHA,
1761 3, 3,
1762 GL_ALPHA, GL_FLOAT,
1763 pack "f*",
1764 0.1, 0.1, 0.1,
1765 0.1, 0.2, 0.1,
1766 0.1, 0.1, 0.1,
1767 ;
1768 glEnable GL_CONVOLUTION_2D;
1769 }
1770
1771 my $tex = new CFClient::Texture
1772 w => $w,
1773 h => $h,
1774 data => $data,
1775 internalformat => GL_ALPHA,
1776 format => GL_ALPHA;
1777
1778 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1779
1780 glEnable GL_BLEND;
1781 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1782 glEnable GL_TEXTURE_2D;
1783 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1784
1785 glColor +($::CFG->{fow_intensity}) x 3, 1;
1786 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1787
1788 glDisable GL_TEXTURE_2D;
1789 glDisable GL_BLEND;
1790 }
1791
1792 # HACK BEGIN
1793 {
1794 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1795 my ($w, $h) = (250, 250);
1796
1797 glEnable GL_BLEND;
1798 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1799 glEnable GL_TEXTURE_2D;
1800 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1801
1802 CFClient::Texture->new (
1803 w => $w,
1804 h => $h,
1805 data => $::MAP->mapmap ($w, $h),
1806 type => GL_UNSIGNED_INT_8_8_8_8_REV
1807 )->draw_quad (100, 100);
1808
1809 glDisable GL_TEXTURE_2D;
1810 glDisable GL_BLEND;
1811 }
1812 # HACK END
1813 }
1814
1815 glEndList;
1816 }
1817
1818 glPushMatrix;
1819 glCallList $self->{list};
1820 glPopMatrix;
1821
1822 if ($FOCUS != $self) {
1823 glColor 64/255, 64/255, 64/255;
1824 glLogicOp GL_AND;
1825 glEnable GL_COLOR_LOGIC_OP;
1826 glBegin GL_QUADS;
1827 glVertex 0, 0;
1828 glVertex 0, $::HEIGHT;
1829 glVertex $::WIDTH, $::HEIGHT;
1830 glVertex $::WIDTH, 0;
1831 glEnd;
1832 glDisable GL_COLOR_LOGIC_OP;
1833 }
1834}
1835
1836my %DIR = (
1837 SDLK_KP8, [1, "north"],
1838 SDLK_KP9, [2, "northeast"],
1839 SDLK_KP6, [3, "east"],
1840 SDLK_KP3, [4, "southeast"],
1841 SDLK_KP2, [5, "south"],
1842 SDLK_KP1, [6, "southwest"],
1843 SDLK_KP4, [7, "west"],
1844 SDLK_KP7, [8, "northwest"],
1845
1846 SDLK_UP, [1, "north"],
1847 SDLK_RIGHT, [3, "east"],
1848 SDLK_DOWN, [5, "south"],
1849 SDLK_LEFT, [7, "west"],
1850);
1851
1852sub key_down {
1853 my ($self, $ev) = @_;
1854
1855 my $mod = $ev->key_mod;
1856 my $sym = $ev->key_sym;
1857
1858 if ($sym == SDLK_KP5) {
1859 $::CONN->user_send ("command stay fire");
1860 } elsif ($sym == SDLK_a) {
1861 $::CONN->user_send ("command apply");
1862 } elsif ($sym == SDLK_QUOTE) {
1863 $self->emit ('activate_console');
1864 } elsif ($sym == SDLK_SLASH) {
1865 $self->emit ('activate_console' => '/');
1866 } elsif (exists $DIR{$sym}) {
1867 if ($mod & KMOD_SHIFT) {
1868 $self->{shft}++;
1869 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1870 } elsif ($mod & KMOD_CTRL) {
1871 $self->{ctrl}++;
1872 $::CONN->user_send ("command run $DIR{$sym}[0]");
1873 } else {
1874 $::CONN->user_send ("command $DIR{$sym}[1]");
1875 }
1876 }
1877}
1878
1879sub key_up {
1880 my ($self, $ev) = @_;
1881
1882 my $mod = $ev->key_mod;
1883 my $sym = $ev->key_sym;
1884
1885 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1886 $::CONN->user_send ("command fire_stop");
1887 }
1888 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1889 $::CONN->user_send ("command run_stop");
1890 }
1891} 1821}
1892 1822
1893############################################################################# 1823#############################################################################
1894 1824
1895package CFClient::UI::Animator; 1825package CFClient::UI::Animator;
1977 1907
1978our @ISA = CFClient::UI::Container::; 1908our @ISA = CFClient::UI::Container::;
1979 1909
1980use SDL::OpenGL; 1910use SDL::OpenGL;
1981 1911
1912sub check_size {
1913 my ($self) = @_;
1914
1915 $self->configure (0, 0, $::WITH, $::HEIGHT);
1916}
1917
1982sub size_request { 1918sub size_request {
1983 ($::WIDTH, $::HEIGHT) 1919 ($::WIDTH, $::HEIGHT)
1984} 1920}
1985 1921
1986sub size_allocate { 1922sub configure {
1987 my ($self, $x, $y, $w, $h) = @_; 1923 my ($self, $x, $y, $w, $h) = @_;
1988 1924
1989 $self->_size_allocate ($x, $y, $w, $h); 1925 $self->SUPER::configure ($x, $y, $w, $h);
1990 1926
1991 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1927 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1992 for @{$self->{children}}; 1928 for @{$self->{children}};
1993} 1929}
1994 1930
1995sub _topleft { 1931sub _topleft {
1996 my ($self, $x, $y) = @_; 1932 my ($self, $x, $y) = @_;
1999} 1935}
2000 1936
2001sub update { 1937sub update {
2002 my ($self) = @_; 1938 my ($self) = @_;
2003 1939
2004 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 1940 $self->check_size;
2005 ::refresh (); 1941 ::refresh ();
2006} 1942}
2007 1943
2008sub add { 1944sub add {
2009 my ($self, $widget) = @_; 1945 my ($self, $child) = @_;
2010 1946
2011 $self->SUPER::add ($widget); 1947 $self->SUPER::add ($child);
2012
2013 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2014} 1948}
2015 1949
2016sub on_refresh { 1950sub on_refresh {
2017 my ($self, $id, $cb) = @_; 1951 my ($self, $id, $cb) = @_;
2018 1952

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines