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.137 by root, Wed Apr 19 21:38:05 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;
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 = $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->{x}, $ev->{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;
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 || $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;
1033use SDL::OpenGL; 1116use SDL::OpenGL;
1034 1117
1035sub new { 1118sub new {
1036 my $class = shift; 1119 my $class = shift;
1037 1120
1040 bg => [0, 0, 0, 0.2], 1123 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1124 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1125 active_fg => [0, 0, 0],
1043 can_hover => 1, 1126 can_hover => 1,
1044 can_focus => 1, 1127 can_focus => 1,
1128 valign => 0,
1045 @_ 1129 @_
1046 ) 1130 )
1047} 1131}
1048 1132
1049sub _set_text { 1133sub _set_text {
1050 my ($self, $text) = @_; 1134 my ($self, $text) = @_;
1051 1135
1052 my $old_text = $self->{text}; 1136 delete $self->{cur_h};
1137
1138 return if $self->{text} eq $text;
1139
1140 delete $self->{texture};
1053 1141
1054 $self->{last_activity} = $::NOW; 1142 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1143 $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 1144
1060 $text =~ s/./*/g if $self->{hidden}; 1145 $text =~ s/./*/g if $self->{hidden};
1146 $self->{layout}->set_text ("$text ");
1061 1147
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1148 $self->emit (changed => $self->{text});
1149}
1063 1150
1064 $text = substr $text, 0, $self->{cursor}; 1151sub get_text {
1065 utf8::encode $text; 1152 $_[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} 1153}
1072 1154
1073sub size_request { 1155sub size_request {
1074 my ($self) = @_; 1156 my ($self) = @_;
1075 1157
1077 1159
1078 ($w + 1, $h) # add 1 for cursor 1160 ($w + 1, $h) # add 1 for cursor
1079} 1161}
1080 1162
1081sub size_allocate { 1163sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1164 my ($self, $w, $h) = @_;
1083
1084 $self->SUPER::size_allocate ($x, $y, $w, $h);
1085 1165
1086 $self->_set_text ($self->{text}); 1166 $self->_set_text ($self->{text});
1087} 1167}
1088 1168
1089sub set_text { 1169sub set_text {
1095} 1175}
1096 1176
1097sub key_down { 1177sub key_down {
1098 my ($self, $ev) = @_; 1178 my ($self, $ev) = @_;
1099 1179
1100 my $mod = $ev->key_mod; 1180 my $mod = $ev->{mod};
1101 my $sym = $ev->key_sym; 1181 my $sym = $ev->{sym};
1102
1103 my $uni = $ev->key_unicode; 1182 my $uni = $ev->{unicode};
1104 1183
1105 my $text = $self->get_text; 1184 my $text = $self->get_text;
1106 1185
1107 if ($sym == SDLK_BACKSPACE) { 1186 if ($sym == 8) {
1108 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1187 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1109 } elsif ($sym == SDLK_DELETE) { 1188 } elsif ($sym == 127) {
1110 substr $text, $self->{cursor}, 1, ""; 1189 substr $text, $self->{cursor}, 1, "";
1111 } elsif ($sym == SDLK_LEFT) { 1190 } elsif ($sym == CFClient::SDLK_LEFT) {
1112 --$self->{cursor} if $self->{cursor}; 1191 --$self->{cursor} if $self->{cursor};
1113 } elsif ($sym == SDLK_RIGHT) { 1192 } elsif ($sym == CFClient::SDLK_RIGHT) {
1114 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1193 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) { 1194 } elsif ($sym == CFClient::SDLK_HOME) {
1116 $self->{cursor} = 0; 1195 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) { 1196 } elsif ($sym == CFClient::SDLK_END) {
1118 $self->{cursor} = length $text; 1197 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) { 1198 } elsif ($sym == 27) {
1120 $self->emit ('escape'); 1199 $self->emit ('escape');
1121 } elsif ($uni) { 1200 } elsif ($uni) {
1122 substr $text, $self->{cursor}++, 0, chr $uni; 1201 substr $text, $self->{cursor}++, 0, chr $uni;
1123 } 1202 }
1124 1203
1179 1258
1180 $self->SUPER::_draw; 1259 $self->SUPER::_draw;
1181 1260
1182 #TODO: force update every cursor change :( 1261 #TODO: force update every cursor change :(
1183 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
1184 glColor @{$self->{fg}}; 1271 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1272 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 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};
1188 glEnd; 1275 glEnd;
1189 } 1276 }
1190} 1277}
1191 1278
1192package CFClient::UI::LineEntry; 1279package CFClient::UI::Entry;
1193 1280
1194our @ISA = CFClient::UI::Entry::; 1281our @ISA = CFClient::UI::EntryBase::;
1195 1282
1196use SDL;
1197use SDL::OpenGL; 1283use SDL::OpenGL;
1198 1284
1199sub key_down { 1285sub key_down {
1200 my ($self, $ev) = @_; 1286 my ($self, $ev) = @_;
1201 1287
1202 my $sym = $ev->key_sym; 1288 my $sym = $ev->{sym};
1203 1289
1204 if ($sym == SDLK_RETURN) { 1290 if ($sym == 13) {
1205 $self->emit (activate => $self->get_text); 1291 $self->emit (activate => $self->get_text);
1206 $self->update; 1292 $self->update;
1207 1293
1208 } else { 1294 } else {
1209 $self->SUPER::key_down ($ev); 1295 $self->SUPER::key_down ($ev);
1215 1301
1216package CFClient::UI::Button; 1302package CFClient::UI::Button;
1217 1303
1218our @ISA = CFClient::UI::Label::; 1304our @ISA = CFClient::UI::Label::;
1219 1305
1220use SDL;
1221use SDL::OpenGL; 1306use SDL::OpenGL;
1222 1307
1223my @tex = 1308my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1309 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1225 qw(b1_button_active.png); 1310 qw(b1_button_active.png);
1229 1314
1230 $class->SUPER::new ( 1315 $class->SUPER::new (
1231 padding => 4, 1316 padding => 4,
1232 fg => [1, 1, 1], 1317 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1318 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1319 active_fg => [0, 0, 1],
1235 can_hover => 1, 1320 can_hover => 1,
1321 align => 0,
1322 valign => 0,
1236 @_ 1323 @_
1237 ) 1324 )
1238} 1325}
1239 1326
1240sub button_up { 1327sub button_up {
1248 1335
1249sub _draw { 1336sub _draw {
1250 my ($self) = @_; 1337 my ($self) = @_;
1251 1338
1252 local $self->{fg} = $self->{fg}; 1339 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 1340
1259 if ($GRAB == $self) { 1341 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1342 $self->{fg} = $self->{active_fg};
1261 } 1343 }
1262 1344
1263 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;
1264 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;
1265 1350
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1351 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1352
1268 glDisable GL_TEXTURE_2D; 1353 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1354 glDisable GL_BLEND;
1270 1355
1271 $self->SUPER::_draw; 1356 $self->SUPER::_draw;
1279 1364
1280my @tex = 1365my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1366 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1367 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283 1368
1284use SDL;
1285use SDL::OpenGL; 1369use SDL::OpenGL;
1286 1370
1287sub new { 1371sub new {
1288 my $class = shift; 1372 my $class = shift;
1289 1373
1301 my ($self) = @_; 1385 my ($self) = @_;
1302 1386
1303 ($self->{padding} * 2 + 6) x 2 1387 ($self->{padding} * 2 + 6) x 2
1304} 1388}
1305 1389
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down { 1390sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1391 my ($self, $ev, $x, $y) = @_;
1314 1392
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1393 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1394 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1338 1416
1339 $tex->draw_quad (0, 0, $s, $s); 1417 $tex->draw_quad (0, 0, $s, $s);
1340 1418
1341 glDisable GL_TEXTURE_2D; 1419 glDisable GL_TEXTURE_2D;
1342 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;
1343} 1528}
1344 1529
1345############################################################################# 1530#############################################################################
1346 1531
1347package CFClient::UI::Slider; 1532package CFClient::UI::Slider;
1366 # TODO: calculations are off 1551 # TODO: calculations are off
1367 my $self = $class->SUPER::new ( 1552 my $self = $class->SUPER::new (
1368 fg => [1, 1, 1], 1553 fg => [1, 1, 1],
1369 active_fg => [0, 0, 0], 1554 active_fg => [0, 0, 0],
1370 range => [0, 0, 100, 10], 1555 range => [0, 0, 100, 10],
1371 req_w => 40, 1556 req_w => 20,
1372 req_h => 13, 1557 req_h => 20,
1373 vertical => 0, 1558 vertical => 0,
1374 can_hover => 1, 1559 can_hover => 1,
1375 inner_pad => 5, 1560 inner_pad => 5,
1376 @_ 1561 @_
1377 ); 1562 );
1487 1672
1488sub new { 1673sub new {
1489 my $class = shift; 1674 my $class = shift;
1490 1675
1491 my $self = $class->SUPER::new ( 1676 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE, 1677 fontsize => 1,
1495 @_, 1678 @_,
1496 1679
1497 layout => (new CFClient::Layout), 1680 layout => (new CFClient::Layout),
1498 par => [], 1681 par => [],
1499 height => 0, 1682 height => 0,
1520sub text_height { 1703sub text_height {
1521 my ($self, $text) = @_; 1704 my ($self, $text) = @_;
1522 1705
1523 my $layout = $self->{layout}; 1706 my $layout = $self->{layout};
1524 1707
1525 $layout->set_height ($self->{fontsize}); 1708 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1526 $layout->set_width ($self->{w}); 1709 $layout->set_width ($self->{w});
1527 $layout->set_text ($text); 1710 $layout->set_text ($text);
1528 1711
1529 ($layout->size)[1] 1712 ($layout->size)[1]
1530} 1713}
1534 1717
1535 $self->{need_reflow}++; 1718 $self->{need_reflow}++;
1536 $self->update; 1719 $self->update;
1537} 1720}
1538 1721
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate { 1722sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 1723 my ($self, $w, $h) = @_;
1547 1724
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 1725 $self->SUPER::size_allocate ($w, $h);
1549 1726
1550 $self->{layout}->set_height ($self->{fontsize}); 1727 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 $self->{layout}->set_width ($self->{w}); 1728 $self->{layout}->set_width ($self->{children}[0]{w});
1552 1729
1553 $self->reflow; 1730 $self->reflow;
1554} 1731}
1555 1732
1556sub add_paragraph { 1733sub add_paragraph {
1575 1752
1576 return unless $self->{h} > 0; 1753 return unless $self->{h} > 0;
1577 1754
1578 delete $self->{texture}; 1755 delete $self->{texture};
1579 1756
1580 $TOPLEVEL->on_refresh ($self, sub { 1757 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1758 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1759 my $height = 0;
1583 1760
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1761 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1762 for @{$self->{par}};
1643 1820
1644} 1821}
1645 1822
1646############################################################################# 1823#############################################################################
1647 1824
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; 1825package CFClient::UI::Animator;
1883 1826
1884use SDL::OpenGL; 1827use SDL::OpenGL;
1885 1828
1886our @ISA = CFClient::UI::Bin::; 1829our @ISA = CFClient::UI::Bin::;
1945sub toggle_flopper { 1888sub toggle_flopper {
1946 my ($self) = @_; 1889 my ($self) = @_;
1947 1890
1948 # TODO: use animation 1891 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 1892 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 1893 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 1894 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 1895 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 1896 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 1897 $CFClient::UI::ROOT->remove ($self->{other});
1898 $self->emit ("close");
1957 } 1899 }
1958}
1959 1900
1960############################################################################# 1901 $self->emit (changed => $self->{state});
1902}
1961 1903
1904#############################################################################
1905
1962package CFClient::UI::Toplevel; 1906package CFClient::UI::Root;
1963 1907
1964our @ISA = CFClient::UI::Container::; 1908our @ISA = CFClient::UI::Container::;
1965 1909
1966use SDL::OpenGL; 1910use SDL::OpenGL;
1911
1912sub check_size {
1913 my ($self) = @_;
1914
1915 $self->configure (0, 0, $::WITH, $::HEIGHT);
1916}
1967 1917
1968sub size_request { 1918sub size_request {
1969 ($::WIDTH, $::HEIGHT) 1919 ($::WIDTH, $::HEIGHT)
1970} 1920}
1971 1921
1972sub size_allocate { 1922sub configure {
1973 my ($self, $x, $y, $w, $h) = @_; 1923 my ($self, $x, $y, $w, $h) = @_;
1974 1924
1975 $self->_size_allocate ($x, $y, $w, $h); 1925 $self->SUPER::configure ($x, $y, $w, $h);
1976 1926
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1927 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 1928 for @{$self->{children}};
1979} 1929}
1980 1930
1981sub translate { 1931sub _topleft {
1982 my ($self, $x, $y) = @_; 1932 my ($self, $x, $y) = @_;
1983 1933
1984 ($x, $y) 1934 ($x, $y)
1985} 1935}
1986 1936
1987sub update { 1937sub update {
1988 my ($self) = @_; 1938 my ($self) = @_;
1989 1939
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 1940 $self->check_size;
1991 ::refresh (); 1941 ::refresh ();
1992} 1942}
1993 1943
1994sub add { 1944sub add {
1995 my ($self, $widget) = @_; 1945 my ($self, $child) = @_;
1996 1946
1997 $self->SUPER::add ($widget); 1947 $self->SUPER::add ($child);
1998
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000} 1948}
2001 1949
2002sub on_refresh { 1950sub on_refresh {
2003 my ($self, $id, $cb) = @_; 1951 my ($self, $id, $cb) = @_;
2004 1952
2027 1975
2028############################################################################# 1976#############################################################################
2029 1977
2030package CFClient::UI; 1978package CFClient::UI;
2031 1979
2032$TOPLEVEL = new CFClient::UI::Toplevel; 1980$ROOT = new CFClient::UI::Root;
2033 1981
20341 19821
2035 1983

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines