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.112 by root, Sat Apr 15 12:29:46 2006 UTC vs.
Revision 1.135 by root, Wed Apr 19 06:21:07 2006 UTC

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
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->motion_x, $ev->motion_y);
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->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->translate ($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->motion_x, $ev->motion_y);
43 43
44 my $widget = $GRAB || $TOPLEVEL->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->translate ($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;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
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->motion_x, $ev->motion_y);
60 60
61 my $widget = $GRAB || $TOPLEVEL->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;
65 65
66 $hover->update if $hover && $hover->{can_hover}; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 71}
72 72
73# convert position array to integers 73# convert position array to integers
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 {
164 # nothing to be done
165}
166
167# return top left coordinates
168sub _topleft {
146 my ($self, $x, $y, $w, $h) = @_; 169 my ($self, $x, $y) = @_;
147 170
148 $self->_size_allocate ($x, $y, $w, $h); 171 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
149} 172}
150 173
151# translate global coordinates to local coordinate system 174# translate global coordinates to local coordinate system
152sub translate { 175sub coord2local {
153 my ($self, $x, $y) = @_; 176 my ($self, $x, $y) = @_;
154 177
155 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 178 my ($X, $Y) = $self->_topleft;
179 ($x - $X, $y - $Y)
180}
181
182# translate local coordinates to global coordinate system
183sub coord2global {
184 my ($self, $x, $y) = @_;
185
186 my ($X, $Y) = $self->_topleft;
187 ($x + $X, $y + $Y)
156} 188}
157 189
158sub focus_in { 190sub focus_in {
159 my ($self) = @_; 191 my ($self) = @_;
160 192
161 return if $FOCUS == $self; 193 return if $FOCUS == $self;
162 return unless $self->{can_focus}; 194 return unless $self->{can_focus};
163 195
164 my $focus = $FOCUS; $FOCUS = $self; 196 my $focus = $FOCUS; $FOCUS = $self;
197
198 $self->emit (focus_in => $focus);
199
165 $focus->update if $focus; 200 $focus->update if $focus;
166 $FOCUS->update; 201 $FOCUS->update;
167} 202}
168 203
169sub focus_out { 204sub focus_out {
170 my ($self) = @_; 205 my ($self) = @_;
171 206
172 return unless $FOCUS == $self; 207 return unless $FOCUS == $self;
173 208
174 my $focus = $FOCUS; undef $FOCUS; 209 my $focus = $FOCUS; undef $FOCUS;
210
211 $self->emit (focus_out => $focus);
212
175 $focus->update if $focus; #? 213 $focus->update if $focus; #?
176} 214}
177 215
178sub mouse_motion { } 216sub mouse_motion { }
179sub button_up { } 217sub button_up { }
203 glPopMatrix; 241 glPopMatrix;
204 242
205 if ($self == $HOVER && $self->{can_hover}) { 243 if ($self == $HOVER && $self->{can_hover}) {
206 my ($x, $y) = @$self{qw(x y)}; 244 my ($x, $y) = @$self{qw(x y)};
207 245
208 glColor 0, 0, 1, 0.2; 246 glColor 1, 0.8, 0.5, 0.2;
209 glEnable GL_BLEND; 247 glEnable GL_BLEND;
210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 248 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
211 glBegin GL_QUADS; 249 glBegin GL_QUADS;
212 glVertex $x , $y; 250 glVertex $x , $y;
213 glVertex $x + $self->{w}, $y; 251 glVertex $x + $self->{w}, $y;
233 271
234 () 272 ()
235} 273}
236 274
237sub set_parent { 275sub set_parent {
238 my ($self, $par) = @_; 276 my ($self, $parent) = @_;
239 277
240 $self->{parent} = $par;
241 Scalar::Util::weaken $self->{parent}; 278 Scalar::Util::weaken ($self->{parent} = $parent);
242} 279}
243 280
244sub get_parent { 281sub check_size {
245 $_[0]->{parent} 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 }
246} 293}
247 294
248sub update { 295sub update {
249 my ($self) = @_; 296 my ($self) = @_;
250 297
253} 300}
254 301
255sub connect { 302sub connect {
256 my ($self, $signal, $cb) = @_; 303 my ($self, $signal, $cb) = @_;
257 304
258 push @{ $self->{cb}{$signal} }, $cb; 305 push @{ $self->{signal_cb}{$signal} }, $cb;
259} 306}
260 307
261sub emit { 308sub emit {
262 my ($self, $signal, @args) = @_; 309 my ($self, $signal, @args) = @_;
263 310
311 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
264 $_->($self, @args) 312 $cb->($self, @args);
265 for @{$self->{cb}{$signal} || []}; 313 }
266} 314}
267 315
268sub DESTROY { 316sub DESTROY {
269 my ($self) = @_; 317 my ($self) = @_;
270 318
339 387
340 $self 388 $self
341} 389}
342 390
343sub add { 391sub add {
344 my ($self, $chld) = @_; 392 my ($self, $child) = @_;
345 393
346 $chld->set_parent ($self); 394 $child->set_parent ($self);
395
396 use sort 'stable';
347 397
348 $self->{children} = [ 398 $self->{children} = [
349 sort { $a->{z} <=> $b->{z} } 399 sort { $a->{z} <=> $b->{z} }
350 @{$self->{children}}, $chld 400 @{$self->{children}}, $child
351 ]; 401 ];
352 402
353 $self->{w} = $self->{h} = -1; 403 $child->check_size;
354 $self->update;
355} 404}
356 405
357sub remove { 406sub remove {
358 my ($self, $widget) = @_; 407 my ($self, $child) = @_;
359 408
409 delete $child->{parent};
410
360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 411 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
361 412
362 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 413 $self->check_size;
363} 414}
364 415
365sub find_widget { 416sub find_widget {
366 my ($self, $x, $y) = @_; 417 my ($self, $x, $y) = @_;
367 418
397 448
398 $class->SUPER::new (children => [$child], %arg) 449 $class->SUPER::new (children => [$child], %arg)
399} 450}
400 451
401sub add { 452sub add {
402 my ($self, $widget) = @_; 453 my ($self, $child) = @_;
403 454
404 $self->{children} = []; 455 $self->{children} = [];
405 456
406 $self->SUPER::add ($widget); 457 $self->SUPER::add ($child);
407} 458}
408 459
409sub remove { 460sub remove {
410 my ($self, $widget) = @_; 461 my ($self, $widget) = @_;
411 462
420sub size_request { 471sub size_request {
421 $_[0]{children}[0]->size_request 472 $_[0]{children}[0]->size_request
422} 473}
423 474
424sub size_allocate { 475sub size_allocate {
425 my ($self, $x, $y, $w, $h) = @_; 476 my ($self, $w, $h) = @_;
426 477
427 $self->_size_allocate ($x, $y, $w, $h) or return;
428
429 $self->{children}[0]->size_allocate (0, 0, $w, $h); 478 $self->{children}[0]->configure (0, 0, $w, $h);
430} 479}
431 480
432############################################################################# 481#############################################################################
433 482
434package CFClient::UI::Window; 483package CFClient::UI::Window;
460 $self->child->draw; 509 $self->child->draw;
461 }; 510 };
462} 511}
463 512
464sub size_allocate { 513sub size_allocate {
465 my ($self, $x, $y, $w, $h) = @_; 514 my ($self, $w, $h) = @_;
466 515
467 $self->_size_allocate ($x, $y, $w, $h) or return;
468
469 $self->child->size_allocate (0, 0, $w, $h); 516 $self->child->configure (0, 0, $w, $h);
470 517
471 $self->render_chld; 518 $self->render_chld;
472} 519}
473 520
474sub _draw { 521sub _draw {
490 glDisable GL_TEXTURE_2D; 537 glDisable GL_TEXTURE_2D;
491} 538}
492 539
493############################################################################# 540#############################################################################
494 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
495package CFClient::UI::Frame; 566package CFClient::UI::Frame;
496 567
497our @ISA = CFClient::UI::Bin::; 568our @ISA = CFClient::UI::Bin::;
498 569
499use SDL::OpenGL; 570use SDL::OpenGL;
509} 580}
510 581
511sub size_allocate { 582sub size_allocate {
512 my ($self, $x, $y, $w, $h) = @_; 583 my ($self, $x, $y, $w, $h) = @_;
513 584
514 $self->_size_allocate ($x, $y, $w, $h) or return;
515
516 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 585 $self->child->configure (2, 2, $w - 4, $h - 4);
517} 586}
518 587
519sub _draw { 588sub _draw {
520 my ($self) = @_; 589 my ($self) = @_;
521 590
552 # TODO: user_x, user_y, overwrite moveto? 621 # TODO: user_x, user_y, overwrite moveto?
553 622
554 $class->SUPER::new ( 623 $class->SUPER::new (
555 bg => [1, 1, 1, 1], 624 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 625 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 626 border => 0.8,
558 @_ 627 @_
559 ) 628 )
560} 629}
561 630
631sub border {
632 int $_[0]{border} * $::FONTSIZE
633}
634
562sub size_request { 635sub size_request {
563 my ($self) = @_; 636 my ($self) = @_;
564 637
565 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};
566 639
567 my ($w, $h) = $self->SUPER::size_request; 640 my ($w, $h) = $self->SUPER::size_request;
568 641
569 ( 642 (
570 $w + $self->{border} * 2, 643 $w + $self->border * 2,
571 $h + $self->{border} * 2, 644 $h + $self->border * 2,
572 ) 645 )
573} 646}
574 647
575sub size_allocate { 648sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_; 649 my ($self, $w, $h) = @_;
577 650
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $h -= List::Util::max 0, $self->{border} * 2; 651 $h -= List::Util::max 0, $self->border * 2;
581 $w -= List::Util::max 0, $self->{border} * 2; 652 $w -= List::Util::max 0, $self->border * 2;
582 653
583 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 654 $self->child->configure ($self->border, $self->border, $w, $h);
584} 655}
585 656
586sub button_down { 657sub button_down {
587 my ($self, $ev, $x, $y) = @_; 658 my ($self, $ev, $x, $y) = @_;
588 659
660 my $border = $self->border;
661
589 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 662 if ($x < $self->{w} && $x >= $self->{w} - $border
590 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 663 && $y < $self->{h} && $y >= $self->{h} - $border) {
591 664
592 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 665 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
593 my ($bw, $bh) = ($self->{w}, $self->{h}); 666 my ($bw, $bh) = ($self->{w}, $self->{h});
594 667
595 $self->{motion} = sub { 668 $self->{motion} = sub {
597 670
598 ($x, $y) = ($ev->motion_x, $ev->motion_y); 671 ($x, $y) = ($ev->motion_x, $ev->motion_y);
599 672
600 $self->{user_w} = $bw + $x - $ox; 673 $self->{user_w} = $bw + $x - $ox;
601 $self->{user_h} = $bh + $y - $oy; 674 $self->{user_h} = $bh + $y - $oy;
602 $self->update; 675 $self->check_size;
603 }; 676 };
604 677
605 } elsif ($x >= 0 && $x < $self->{w} 678 } elsif ($x >= 0 && $x < $self->{w}
606 && $y >= 0 && $y < $self->{border}) { 679 && $y >= 0 && $y < $border) {
607 680
608 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 681 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
609 my ($bx, $by) = ($self->{x}, $self->{y}); 682 my ($bx, $by) = ($self->{x}, $self->{y});
610 683
611 $self->{motion} = sub { 684 $self->{motion} = sub {
640 glEnable GL_BLEND; 713 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 714 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D; 715 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 716 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
644 717
718 my $border = $self->border;
719
645 glColor @{ $self->{border_bg} }; 720 glColor @{ $self->{border_bg} };
646 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 721 $tex[1]->draw_quad (0, 0, $w, $border);
647 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 722 $tex[3]->draw_quad (0, $border, $border, $ch);
648 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 723 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
649 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 724 $tex[4]->draw_quad (0, $h - $border, $w, $border);
650 725
651 my $bg = $tex[0]; 726 my $bg = $tex[0];
652 727
653 # TODO: repeat texture not scale 728 # TODO: repeat texture not scale
654 my $rep_x = $cw / $bg->{w}; 729 my $rep_x = $cw / $bg->{w};
657 glColor @{ $self->{bg} }; 732 glColor @{ $self->{bg} };
658 733
659 $bg->{s} = $rep_x; 734 $bg->{s} = $rep_x;
660 $bg->{t} = $rep_y; 735 $bg->{t} = $rep_y;
661 $bg->{wrap_mode} = 1; 736 $bg->{wrap_mode} = 1;
662 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 737 $bg->draw_quad ($border, $border, $cw, $ch);
663 738
664 glDisable GL_TEXTURE_2D; 739 glDisable GL_TEXTURE_2D;
665 glDisable GL_BLEND; 740 glDisable GL_BLEND;
666 741
667 $self->child->draw; 742 $self->child->draw;
685 @_ 760 @_
686 ) 761 )
687} 762}
688 763
689sub add { 764sub add {
690 my ($self, $x, $y, $chld) = @_; 765 my ($self, $x, $y, $child) = @_;
691 766
767 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 768 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 769
695 $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};
696 $self->update; 778 $self->update;
697} 779}
698 780
699sub get_wh { 781sub get_wh {
700 my ($self) = @_; 782 my ($self) = @_;
728 (sum @$hs), 810 (sum @$hs),
729 ) 811 )
730} 812}
731 813
732sub size_allocate { 814sub size_allocate {
733 my ($self, $x, $y, $w, $h) = @_; 815 my ($self, $w, $h) = @_;
734
735 $self->_size_allocate ($x, $y, $w, $h) or return;
736 816
737 my ($ws, $hs) = $self->get_wh; 817 my ($ws, $hs) = $self->get_wh;
738 818
739 my $req_w = sum @$ws; 819 my $req_w = sum @$ws;
740 my $req_h = sum @$hs; 820 my $req_h = sum @$hs;
762 842
763 for my $c (0 .. $#$row) { 843 for my $c (0 .. $#$row) {
764 my $col_w = $ws->[$c]; 844 my $col_w = $ws->[$c];
765 845
766 if (my $widget = $row->[$c]) { 846 if (my $widget = $row->[$c]) {
767 $widget->size_allocate ($x, $y, $col_w, $row_h); 847 $widget->configure ($x, $y, $col_w, $row_h);
768 } 848 }
769 849
770 $x += $col_w; 850 $x += $col_w;
771 } 851 }
772 852
817 (List::Util::max map $_->[1], @alloc), 897 (List::Util::max map $_->[1], @alloc),
818 ) 898 )
819} 899}
820 900
821sub size_allocate { 901sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_; 902 my ($self, $w, $h) = @_;
823
824 $self->_size_allocate ($x, $y, $w, $h);
825 903
826 ($h, $w) = ($w, $h); 904 ($h, $w) = ($w, $h);
827 905
828 my $children = $self->{children}; 906 my $children = $self->{children};
829 907
851 929
852 my $y = 0; 930 my $y = 0;
853 for (0 .. $#$children) { 931 for (0 .. $#$children) {
854 my $child = $children->[$_]; 932 my $child = $children->[$_];
855 my $h = $h[$_]; 933 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w); 934 $child->configure ($y, 0, $h, $w);
857 935
858 $y += $h; 936 $y += $h;
859 } 937 }
938
939 1
860} 940}
861 941
862############################################################################# 942#############################################################################
863 943
864package CFClient::UI::VBox; 944package CFClient::UI::VBox;
877 (List::Util::sum map $_->[1], @alloc), 957 (List::Util::sum map $_->[1], @alloc),
878 ) 958 )
879} 959}
880 960
881sub size_allocate { 961sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_; 962 my ($self, $w, $h) = @_;
883
884 $self->_size_allocate ($x, $y, $w, $h);
885 963
886 my $children = $self->{children}; 964 my $children = $self->{children};
887 965
888 my @h = map +($_->size_request)[1], @$children; 966 my @h = map +($_->size_request)[1], @$children;
889 967
907 985
908 my $y = 0; 986 my $y = 0;
909 for (0 .. $#$children) { 987 for (0 .. $#$children) {
910 my $child = $children->[$_]; 988 my $child = $children->[$_];
911 my $h = $h[$_]; 989 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h); 990 $child->configure (0, $y, $w, $h);
913 991
914 $y += $h; 992 $y += $h;
915 } 993 }
994
995 1
916} 996}
917 997
918############################################################################# 998#############################################################################
919 999
920package CFClient::UI::Label; 1000package CFClient::UI::Label;
926sub new { 1006sub new {
927 my ($class, %arg) = @_; 1007 my ($class, %arg) = @_;
928 1008
929 my $self = $class->SUPER::new ( 1009 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 1010 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE, 1011 fontsize => 1,
932 text => "", 1012 text => "",
933 align => -1, 1013 align => -1,
1014 valign => -1,
934 padding => 2, 1015 padding => 2,
935 layout => new CFClient::Layout, 1016 layout => new CFClient::Layout,
936 %arg 1017 %arg
937 ); 1018 );
938 1019
939 $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};
940 1024
941 $self 1025 $self
942} 1026}
943 1027
944sub escape_text { 1028sub escape_text {
952} 1036}
953 1037
954sub set_text { 1038sub set_text {
955 my ($self, $text) = @_; 1039 my ($self, $text) = @_;
956 1040
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text); 1041 $self->{layout}->set_text ($text);
959 1042
960 delete $self->{texture}; 1043 delete $self->{texture};
961# $self->{w} = $self->{h} = -1;
962 $self->update; 1044 $self->update;
963} 1045}
964 1046
965sub get_text { 1047sub set_markup {
966 my ($self, $text) = @_; 1048 my ($self, $markup) = @_;
967 1049
968 $self->{text} 1050 $self->{layout}->set_markup ($markup);
1051
1052 delete $self->{texture};
1053 $self->update;
969} 1054}
970 1055
971sub size_request { 1056sub size_request {
972 my ($self) = @_; 1057 my ($self) = @_;
973 1058
974 $self->{layout}->set_width; 1059 $self->{layout}->set_width;
975 $self->{layout}->set_height ($self->{fontsize}); 1060 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1061
976 my ($w, $h) = $self->{layout}->size; 1062 my ($w, $h) = $self->{layout}->size;
977 1063
978 ( 1064 (
979 $w + $self->{padding} * 2, 1065 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2, 1066 $h + $self->{padding} * 2,
981 ) 1067 )
982} 1068}
983 1069
984sub size_allocate { 1070sub size_allocate {
985 my ($self, $x, $y, $w, $h) = @_; 1071 my ($self, $w, $h) = @_;
986
987 $self->_size_allocate ($x, $y, $w, $h) or return;
988 1072
989 delete $self->{texture}; 1073 delete $self->{texture};
990}
991
992sub update {
993 my ($self) = @_;
994
995 delete $self->{texture};
996 $self->SUPER::update;
997} 1074}
998 1075
999sub _draw { 1076sub _draw {
1000 my ($self) = @_; 1077 my ($self) = @_;
1001 1078
1002 my $tex = $self->{texture} ||= do { 1079 my $tex = $self->{texture} ||= do {
1003 $self->{layout}->set_width ($self->{w}); 1080 $self->{layout}->set_width ($self->{w});
1004 $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);
1005 new_from_layout CFClient::Texture $self->{layout} 1082 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1083 };
1007 1084
1008 glEnable GL_BLEND; 1085 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1086 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1087 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1088 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1089
1013 glColor @{$self->{fg}}; 1090 glColor @{$self->{fg}};
1014 1091
1015 my $x = 1092 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1093 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1094 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1095 : ($self->{w} - $tex->{w}) * 0.5
1096 );
1019 1097
1020 $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});
1021 1105
1022 glDisable GL_TEXTURE_2D; 1106 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1107 glDisable GL_BLEND;
1024} 1108}
1025 1109
1026############################################################################# 1110#############################################################################
1027 1111
1028package CFClient::UI::Entry; 1112package CFClient::UI::EntryBase;
1029 1113
1030our @ISA = CFClient::UI::Label::; 1114our @ISA = CFClient::UI::Label::;
1031 1115
1032use SDL; 1116use SDL;
1033use SDL::OpenGL; 1117use SDL::OpenGL;
1040 bg => [0, 0, 0, 0.2], 1124 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1125 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1126 active_fg => [0, 0, 0],
1043 can_hover => 1, 1127 can_hover => 1,
1044 can_focus => 1, 1128 can_focus => 1,
1129 valign => 0,
1045 @_ 1130 @_
1046 ) 1131 )
1047} 1132}
1048 1133
1049sub _set_text { 1134sub _set_text {
1050 my ($self, $text) = @_; 1135 my ($self, $text) = @_;
1051 1136
1052 my $old_text = $self->{text}; 1137 delete $self->{cur_h};
1138
1139 return if $self->{text} eq $text;
1140
1141 delete $self->{texture};
1053 1142
1054 $self->{last_activity} = $::NOW; 1143 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1144 $self->{text} = $text;
1057 $self->{layout}->set_width ($self->{w});
1058 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1059 1145
1060 $text =~ s/./*/g if $self->{hidden}; 1146 $text =~ s/./*/g if $self->{hidden};
1147 $self->{layout}->set_text ("$text ");
1061 1148
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1149 $self->emit (changed => $self->{text});
1150}
1063 1151
1064 $text = substr $text, 0, $self->{cursor}; 1152sub get_text {
1065 utf8::encode $text; 1153 $_[0]{text}
1066
1067 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1068
1069 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1070 if $old_text ne $self->{text};
1071} 1154}
1072 1155
1073sub size_request { 1156sub size_request {
1074 my ($self) = @_; 1157 my ($self) = @_;
1075 1158
1077 1160
1078 ($w + 1, $h) # add 1 for cursor 1161 ($w + 1, $h) # add 1 for cursor
1079} 1162}
1080 1163
1081sub size_allocate { 1164sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1165 my ($self, $w, $h) = @_;
1083
1084 $self->SUPER::size_allocate ($x, $y, $w, $h);
1085 1166
1086 $self->_set_text ($self->{text}); 1167 $self->_set_text ($self->{text});
1087} 1168}
1088 1169
1089sub set_text { 1170sub set_text {
1179 1260
1180 $self->SUPER::_draw; 1261 $self->SUPER::_draw;
1181 1262
1182 #TODO: force update every cursor change :( 1263 #TODO: force update every cursor change :(
1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1264 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1265
1266 unless (exists $self->{cur_h}) {
1267 my $text = substr $self->{text}, 0, $self->{cursor};
1268 utf8::encode $text;
1269
1270 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1271 }
1272
1184 glColor @{$self->{fg}}; 1273 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1274 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1275 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1276 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1188 glEnd; 1277 glEnd;
1189 } 1278 }
1190} 1279}
1191 1280
1192package CFClient::UI::LineEntry; 1281package CFClient::UI::Entry;
1193 1282
1194our @ISA = CFClient::UI::Entry::; 1283our @ISA = CFClient::UI::EntryBase::;
1195 1284
1196use SDL; 1285use SDL;
1197use SDL::OpenGL; 1286use SDL::OpenGL;
1198 1287
1199sub key_down { 1288sub key_down {
1229 1318
1230 $class->SUPER::new ( 1319 $class->SUPER::new (
1231 padding => 4, 1320 padding => 4,
1232 fg => [1, 1, 1], 1321 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1322 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1323 active_fg => [0, 0, 1],
1235 can_hover => 1, 1324 can_hover => 1,
1325 align => 0,
1326 valign => 0,
1236 @_ 1327 @_
1237 ) 1328 )
1238} 1329}
1239 1330
1240sub button_up { 1331sub button_up {
1248 1339
1249sub _draw { 1340sub _draw {
1250 my ($self) = @_; 1341 my ($self) = @_;
1251 1342
1252 local $self->{fg} = $self->{fg}; 1343 local $self->{fg} = $self->{fg};
1253 my $tex = $tex[0];
1254
1255 glEnable GL_BLEND;
1256 glEnable GL_TEXTURE_2D;
1257 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1258 1344
1259 if ($GRAB == $self) { 1345 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1346 $self->{fg} = $self->{active_fg};
1261 } 1347 }
1262 1348
1263 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1349 glEnable GL_BLEND;
1350 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1351 glEnable GL_TEXTURE_2D;
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1352 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1353 glColor 0, 0, 0, 1;
1265 1354
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1355 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1356
1268 glDisable GL_TEXTURE_2D; 1357 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1358 glDisable GL_BLEND;
1270 1359
1271 $self->SUPER::_draw; 1360 $self->SUPER::_draw;
1301 my ($self) = @_; 1390 my ($self) = @_;
1302 1391
1303 ($self->{padding} * 2 + 6) x 2 1392 ($self->{padding} * 2 + 6) x 2
1304} 1393}
1305 1394
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down { 1395sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1396 my ($self, $ev, $x, $y) = @_;
1314 1397
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1398 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1399 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1338 1421
1339 $tex->draw_quad (0, 0, $s, $s); 1422 $tex->draw_quad (0, 0, $s, $s);
1340 1423
1341 glDisable GL_TEXTURE_2D; 1424 glDisable GL_TEXTURE_2D;
1342 glDisable GL_BLEND; 1425 glDisable GL_BLEND;
1426}
1427
1428#############################################################################
1429
1430package CFClient::UI::VGauge;
1431
1432our @ISA = CFClient::UI::Base::;
1433
1434use SDL::OpenGL;
1435
1436my %tex = (
1437 food => [
1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1439 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1440 ],
1441 grace => [
1442 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1443 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1444 ],
1445 hp => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1448 ],
1449 mana => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1452 ],
1453);
1454
1455# eg. VGauge->new (gauge => 'food'), default gauge: food
1456sub new {
1457 my $class = shift;
1458
1459 my $self = $class->SUPER::new (gauge => 'food', @_);
1460
1461 $self
1462}
1463
1464sub size_request {
1465 my ($self) = @_;
1466
1467 my $tex = $tex{$self->{gauge}}[0];
1468
1469 @$tex{qw(w h)}
1470}
1471
1472sub set_max {
1473 my ($self, $max) = @_;
1474
1475 $self->{max_val} = $max;
1476}
1477
1478sub set_value {
1479 my ($self, $val, $max) = @_;
1480
1481 $self->set_max ($max)
1482 if defined $max;
1483
1484 $max = $self->{max_val};
1485 $self->{val} = $val;
1486
1487 $self->update;
1488}
1489
1490sub _draw {
1491 my ($self) = @_;
1492
1493 my $tex = $tex{$self->{gauge}};
1494
1495 my ($w, $h) = ($self->{w}, $self->{h});
1496
1497 my $ycut = $self->{val} / ($self->{max_val} || 1);
1498 $ycut = 1 if $self->{val} > $self->{max_val};
1499
1500 my $t1 = $tex->[0];
1501 my $t2 = $tex->[1];
1502
1503 glEnable GL_BLEND;
1504 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1505 glEnable GL_TEXTURE_2D;
1506 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1507
1508 my $h1 = $self->{h} - $ycut * $self->{h};
1509 my $h2 = $ycut * $self->{h};
1510
1511 my $yp = 0;
1512
1513 glBindTexture GL_TEXTURE_2D, $t1->{name};
1514 glBegin GL_QUADS;
1515 glTexCoord 0 , 0; glVertex 0 , $yp;
1516 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1;
1517 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1;
1518 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp;
1519 glEnd;
1520
1521 $yp += $h1;
1522
1523 glBindTexture GL_TEXTURE_2D, $t2->{name};
1524 glBegin GL_QUADS;
1525 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp;
1526 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2;
1527 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1528 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp;
1529 glEnd;
1530
1531 glDisable GL_BLEND;
1532 glDisable GL_TEXTURE_2D;
1343} 1533}
1344 1534
1345############################################################################# 1535#############################################################################
1346 1536
1347package CFClient::UI::Slider; 1537package CFClient::UI::Slider;
1366 # TODO: calculations are off 1556 # TODO: calculations are off
1367 my $self = $class->SUPER::new ( 1557 my $self = $class->SUPER::new (
1368 fg => [1, 1, 1], 1558 fg => [1, 1, 1],
1369 active_fg => [0, 0, 0], 1559 active_fg => [0, 0, 0],
1370 range => [0, 0, 100, 10], 1560 range => [0, 0, 100, 10],
1371 req_w => 40, 1561 req_w => 20,
1372 req_h => 13, 1562 req_h => 20,
1373 vertical => 0, 1563 vertical => 0,
1374 can_hover => 1, 1564 can_hover => 1,
1375 inner_pad => 5, 1565 inner_pad => 5,
1376 @_ 1566 @_
1377 ); 1567 );
1487 1677
1488sub new { 1678sub new {
1489 my $class = shift; 1679 my $class = shift;
1490 1680
1491 my $self = $class->SUPER::new ( 1681 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE, 1682 fontsize => 1,
1495 @_, 1683 @_,
1496 1684
1497 layout => (new CFClient::Layout), 1685 layout => (new CFClient::Layout),
1498 par => [], 1686 par => [],
1499 height => 0, 1687 height => 0,
1520sub text_height { 1708sub text_height {
1521 my ($self, $text) = @_; 1709 my ($self, $text) = @_;
1522 1710
1523 my $layout = $self->{layout}; 1711 my $layout = $self->{layout};
1524 1712
1525 $layout->set_height ($self->{fontsize}); 1713 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1526 $layout->set_width ($self->{w}); 1714 $layout->set_width ($self->{w});
1527 $layout->set_text ($text); 1715 $layout->set_text ($text);
1528 1716
1529 ($layout->size)[1] 1717 ($layout->size)[1]
1530} 1718}
1534 1722
1535 $self->{need_reflow}++; 1723 $self->{need_reflow}++;
1536 $self->update; 1724 $self->update;
1537} 1725}
1538 1726
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate { 1727sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 1728 my ($self, $w, $h) = @_;
1547 1729
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 1730 $self->SUPER::size_allocate ($w, $h);
1549 1731
1550 $self->{layout}->set_height ($self->{fontsize}); 1732 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 $self->{layout}->set_width ($self->{w}); 1733 $self->{layout}->set_width ($self->{children}[0]{w});
1552 1734
1553 $self->reflow; 1735 $self->reflow;
1554} 1736}
1555 1737
1556sub add_paragraph { 1738sub add_paragraph {
1575 1757
1576 return unless $self->{h} > 0; 1758 return unless $self->{h} > 0;
1577 1759
1578 delete $self->{texture}; 1760 delete $self->{texture};
1579 1761
1580 $TOPLEVEL->on_refresh ($self, sub { 1762 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1763 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1764 my $height = 0;
1583 1765
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1766 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1767 for @{$self->{par}};
1643 1825
1644} 1826}
1645 1827
1646############################################################################# 1828#############################################################################
1647 1829
1648package CFClient::UI::MapWidget;
1649
1650use strict;
1651
1652use List::Util qw(min max);
1653
1654use SDL;
1655use SDL::OpenGL;
1656
1657our @ISA = CFClient::UI::Base::;
1658
1659sub new {
1660 my $class = shift;
1661
1662 $class->SUPER::new (
1663 z => -1,
1664 can_focus => 1,
1665 list => (glGenLists 1),
1666 @_
1667 )
1668}
1669
1670sub key_down {
1671 print "MAPKEYDOWN\n";
1672}
1673
1674sub key_up {
1675}
1676
1677sub button_down {
1678 my ($self, $ev, $x, $y) = @_;
1679
1680 $self->focus_in;
1681
1682 if ($ev->button == 2) {
1683 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1684 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1685
1686 $self->{motion} = sub {
1687 my ($ev, $x, $y) = @_;
1688
1689 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1690
1691 $::CFG->{map_shift_x} = $bw + $x - $ox;
1692 $::CFG->{map_shift_y} = $bh + $y - $oy;
1693
1694 $self->update;
1695 };
1696 }
1697}
1698
1699sub button_up {
1700 my ($self, $ev, $x, $y) = @_;
1701
1702 delete $self->{motion};
1703}
1704
1705sub mouse_motion {
1706 my ($self, $ev, $x, $y) = @_;
1707
1708 $self->{motion}->($ev, $x, $y) if $self->{motion};
1709}
1710
1711sub size_request {
1712 (
1713 1 + 32 * int $::WIDTH / 32,
1714 1 + 32 * int $::HEIGHT / 32,
1715 )
1716}
1717
1718sub update {
1719 my ($self) = @_;
1720
1721 $self->{need_update} = 1;
1722 $self->SUPER::update;
1723}
1724
1725sub draw {
1726 my ($self) = @_;
1727
1728 if (delete $self->{need_update}) {
1729 glNewList $self->{list}, GL_COMPILE;
1730
1731 if ($::MAP) {
1732 my $sw = int $::WIDTH / 32;
1733 my $sh = int $::HEIGHT / 32;
1734
1735 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1736 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1737
1738 glTranslate $sx0 - 32, $sy0 - 32, 0;
1739
1740 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1741
1742 if ($::CFG->{fow_enable}) {
1743 if ($::CFG->{fow_smooth}) { # smooth fog of war
1744 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1745 glConvolutionFilter2D
1746 GL_CONVOLUTION_2D,
1747 GL_ALPHA,
1748 3, 3,
1749 GL_ALPHA, GL_FLOAT,
1750 pack "f*",
1751 0.1, 0.1, 0.1,
1752 0.1, 0.2, 0.1,
1753 0.1, 0.1, 0.1,
1754 ;
1755 glEnable GL_CONVOLUTION_2D;
1756 }
1757
1758 my $tex = new CFClient::Texture
1759 w => $w,
1760 h => $h,
1761 data => $data,
1762 internalformat => GL_ALPHA,
1763 format => GL_ALPHA;
1764
1765 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1766
1767 glEnable GL_BLEND;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1769 glEnable GL_TEXTURE_2D;
1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1771
1772 glColor +($::CFG->{fow_intensity}) x 3, 1;
1773 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1774
1775 glDisable GL_TEXTURE_2D;
1776 glDisable GL_BLEND;
1777 }
1778
1779 # HACK BEGIN
1780 {
1781 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1782 my ($w, $h) = (250, 250);
1783
1784 glEnable GL_BLEND;
1785 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1786 glEnable GL_TEXTURE_2D;
1787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1788
1789 CFClient::Texture->new (
1790 w => $w,
1791 h => $h,
1792 data => $::MAP->mapmap ($w, $h),
1793 type => GL_UNSIGNED_INT_8_8_8_8_REV
1794 )->draw_quad (100, 100);
1795
1796 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND;
1798 }
1799 # HACK END
1800 }
1801
1802 glEndList;
1803 }
1804
1805 glPushMatrix;
1806 glCallList $self->{list};
1807 glPopMatrix;
1808
1809 if ($FOCUS != $self) {
1810 glColor 64/255, 64/255, 64/255;
1811 glLogicOp GL_AND;
1812 glEnable GL_COLOR_LOGIC_OP;
1813 glBegin GL_QUADS;
1814 glVertex 0, 0;
1815 glVertex 0, $::HEIGHT;
1816 glVertex $::WIDTH, $::HEIGHT;
1817 glVertex $::WIDTH, 0;
1818 glEnd;
1819 glDisable GL_COLOR_LOGIC_OP;
1820 }
1821}
1822
1823my %DIR = (
1824 SDLK_KP8, [1, "north"],
1825 SDLK_KP9, [2, "northeast"],
1826 SDLK_KP6, [3, "east"],
1827 SDLK_KP3, [4, "southeast"],
1828 SDLK_KP2, [5, "south"],
1829 SDLK_KP1, [6, "southwest"],
1830 SDLK_KP4, [7, "west"],
1831 SDLK_KP7, [8, "northwest"],
1832
1833 SDLK_UP, [1, "north"],
1834 SDLK_RIGHT, [3, "east"],
1835 SDLK_DOWN, [5, "south"],
1836 SDLK_LEFT, [7, "west"],
1837);
1838
1839sub key_down {
1840 my ($self, $ev) = @_;
1841
1842 my $mod = $ev->key_mod;
1843 my $sym = $ev->key_sym;
1844
1845 if ($sym == SDLK_KP5) {
1846 $::CONN->user_send ("command stay fire");
1847 } elsif ($sym == SDLK_a) {
1848 $::CONN->user_send ("command apply");
1849 } elsif ($sym == SDLK_QUOTE) {
1850 $self->emit ('activate_console');
1851 } elsif ($sym == SDLK_SLASH) {
1852 $self->emit ('activate_console' => '/');
1853 } elsif (exists $DIR{$sym}) {
1854 if ($mod & KMOD_SHIFT) {
1855 $self->{shft}++;
1856 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1857 } elsif ($mod & KMOD_CTRL) {
1858 $self->{ctrl}++;
1859 $::CONN->user_send ("command run $DIR{$sym}[0]");
1860 } else {
1861 $::CONN->user_send ("command $DIR{$sym}[1]");
1862 }
1863 }
1864}
1865
1866sub key_up {
1867 my ($self, $ev) = @_;
1868
1869 my $mod = $ev->key_mod;
1870 my $sym = $ev->key_sym;
1871
1872 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1873 $::CONN->user_send ("command fire_stop");
1874 }
1875 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1876 $::CONN->user_send ("command run_stop");
1877 }
1878}
1879
1880#############################################################################
1881
1882package CFClient::UI::Animator; 1830package CFClient::UI::Animator;
1883 1831
1884use SDL::OpenGL; 1832use SDL::OpenGL;
1885 1833
1886our @ISA = CFClient::UI::Bin::; 1834our @ISA = CFClient::UI::Bin::;
1945sub toggle_flopper { 1893sub toggle_flopper {
1946 my ($self) = @_; 1894 my ($self) = @_;
1947 1895
1948 # TODO: use animation 1896 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 1897 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 1898 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 1899 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 1900 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 1901 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 1902 $CFClient::UI::ROOT->remove ($self->{other});
1903 $self->emit ("close");
1957 } 1904 }
1958}
1959 1905
1960############################################################################# 1906 $self->emit (changed => $self->{state});
1907}
1961 1908
1909#############################################################################
1910
1962package CFClient::UI::Toplevel; 1911package CFClient::UI::Root;
1963 1912
1964our @ISA = CFClient::UI::Container::; 1913our @ISA = CFClient::UI::Container::;
1965 1914
1966use SDL::OpenGL; 1915use SDL::OpenGL;
1916
1917sub check_size {
1918 my ($self) = @_;
1919
1920 $self->configure (0, 0, $::WITH, $::HEIGHT);
1921}
1967 1922
1968sub size_request { 1923sub size_request {
1969 ($::WIDTH, $::HEIGHT) 1924 ($::WIDTH, $::HEIGHT)
1970} 1925}
1971 1926
1972sub size_allocate { 1927sub configure {
1973 my ($self, $x, $y, $w, $h) = @_; 1928 my ($self, $x, $y, $w, $h) = @_;
1974 1929
1975 $self->_size_allocate ($x, $y, $w, $h); 1930 $self->SUPER::configure ($x, $y, $w, $h);
1976 1931
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1932 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 1933 for @{$self->{children}};
1979} 1934}
1980 1935
1981sub translate { 1936sub _topleft {
1982 my ($self, $x, $y) = @_; 1937 my ($self, $x, $y) = @_;
1983 1938
1984 ($x, $y) 1939 ($x, $y)
1985} 1940}
1986 1941
1987sub update { 1942sub update {
1988 my ($self) = @_; 1943 my ($self) = @_;
1989 1944
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 1945 $self->check_size;
1991 ::refresh (); 1946 ::refresh ();
1992} 1947}
1993 1948
1994sub add { 1949sub add {
1995 my ($self, $widget) = @_; 1950 my ($self, $child) = @_;
1996 1951
1997 $self->SUPER::add ($widget); 1952 $self->SUPER::add ($child);
1998
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000} 1953}
2001 1954
2002sub on_refresh { 1955sub on_refresh {
2003 my ($self, $id, $cb) = @_; 1956 my ($self, $id, $cb) = @_;
2004 1957
2027 1980
2028############################################################################# 1981#############################################################################
2029 1982
2030package CFClient::UI; 1983package CFClient::UI;
2031 1984
2032$TOPLEVEL = new CFClient::UI::Toplevel; 1985$ROOT = new CFClient::UI::Root;
2033 1986
20341 19871
2035 1988

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines