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.140 by root, Thu Apr 20 04:20:52 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}
87 87
88package CFClient::UI::Base; 88package CFClient::UI::Base;
89 89
90use strict; 90use strict;
91 91
92use SDL::OpenGL; 92use CFClient::OpenGL;
93 93
94sub new { 94sub new {
95 my $class = shift; 95 my $class = shift;
96 96
97 my $self = bless { 97 my $self = bless {
98 x => 0, 98 x => 0,
99 y => 0, 99 y => 0,
100 z => 0, 100 z => 0,
101 w => -1,
102 h => -1,
103 @_ 101 @_
104 }, $class; 102 }, $class;
105 103
106 for (keys %$self) { 104 for (keys %$self) {
107 if (/^connect_(.*)$/) { 105 if (/^connect_(.*)$/) {
110 } 108 }
111 109
112 $self 110 $self
113} 111}
114 112
113sub show {
114 my ($self) = @_;
115
116 return if $self->{parent};
117
118 $CFClient::UI::ROOT->add ($self);
119}
120
121sub hide {
122 my ($self) = @_;
123
124 return unless $self->{parent};
125
126 $self->{parent}->remove ($self);
127}
128
115sub move { 129sub move {
116 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
117 $self->{x} = $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
154 if ($self->{aspect}) {
155 $w = List::Util::min $w, int $h * $self->{aspect};
156 $h = List::Util::min $h, int $w / $self->{aspect};
157 }
158
137 return unless $self->{w} != $w || $self->{h} != $h; 159 return unless $self->{w} != $w || $self->{h} != $h;
138 160
139 $self->{w} = $w; 161 $self->{w} = $w;
140 $self->{h} = $h; 162 $self->{h} = $h;
141 163
142 1 164 $self->size_allocate ($w, $h);
165 $self->update;
143} 166}
144 167
145sub size_allocate { 168sub size_allocate {
169 # nothing to be done
170}
171
172# return top left coordinates
173sub _topleft {
146 my ($self, $x, $y, $w, $h) = @_; 174 my ($self, $x, $y) = @_;
147 175
148 $self->_size_allocate ($x, $y, $w, $h); 176 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
149} 177}
150 178
151# translate global coordinates to local coordinate system 179# translate global coordinates to local coordinate system
152sub translate { 180sub coord2local {
153 my ($self, $x, $y) = @_; 181 my ($self, $x, $y) = @_;
154 182
155 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 183 my ($X, $Y) = $self->_topleft;
184 ($x - $X, $y - $Y)
185}
186
187# translate local coordinates to global coordinate system
188sub coord2global {
189 my ($self, $x, $y) = @_;
190
191 my ($X, $Y) = $self->_topleft;
192 ($x + $X, $y + $Y)
156} 193}
157 194
158sub focus_in { 195sub focus_in {
159 my ($self) = @_; 196 my ($self) = @_;
160 197
161 return if $FOCUS == $self; 198 return if $FOCUS == $self;
162 return unless $self->{can_focus}; 199 return unless $self->{can_focus};
163 200
164 my $focus = $FOCUS; $FOCUS = $self; 201 my $focus = $FOCUS; $FOCUS = $self;
202
203 $self->emit (focus_in => $focus);
204
165 $focus->update if $focus; 205 $focus->update if $focus;
166 $FOCUS->update; 206 $FOCUS->update;
167} 207}
168 208
169sub focus_out { 209sub focus_out {
170 my ($self) = @_; 210 my ($self) = @_;
171 211
172 return unless $FOCUS == $self; 212 return unless $FOCUS == $self;
173 213
174 my $focus = $FOCUS; undef $FOCUS; 214 my $focus = $FOCUS; undef $FOCUS;
215
216 $self->emit (focus_out => $focus);
217
175 $focus->update if $focus; #? 218 $focus->update if $focus; #?
176} 219}
177 220
178sub mouse_motion { } 221sub mouse_motion { }
179sub button_up { } 222sub button_up { }
203 glPopMatrix; 246 glPopMatrix;
204 247
205 if ($self == $HOVER && $self->{can_hover}) { 248 if ($self == $HOVER && $self->{can_hover}) {
206 my ($x, $y) = @$self{qw(x y)}; 249 my ($x, $y) = @$self{qw(x y)};
207 250
208 glColor 0, 0, 1, 0.2; 251 glColor 1, 0.8, 0.5, 0.2;
209 glEnable GL_BLEND; 252 glEnable GL_BLEND;
210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 253 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
211 glBegin GL_QUADS; 254 glBegin GL_QUADS;
212 glVertex $x , $y; 255 glVertex $x , $y;
213 glVertex $x + $self->{w}, $y; 256 glVertex $x + $self->{w}, $y;
233 276
234 () 277 ()
235} 278}
236 279
237sub set_parent { 280sub set_parent {
238 my ($self, $par) = @_; 281 my ($self, $parent) = @_;
239 282
240 $self->{parent} = $par;
241 Scalar::Util::weaken $self->{parent}; 283 Scalar::Util::weaken ($self->{parent} = $parent);
242} 284}
243 285
244sub get_parent { 286sub check_size {
245 $_[0]->{parent} 287 my ($self) = @_;
288
289 my ($w, $h) = $self->size_request;
290
291 if ($w != $self->{req_w} || $h != $self->{req_h}) {
292 $self->{req_w} = $w;
293 $self->{req_h} = $h;
294
295 $self->{parent}->check_size
296 if $self->{parent};
297 }
246} 298}
247 299
248sub update { 300sub update {
249 my ($self) = @_; 301 my ($self) = @_;
250 302
253} 305}
254 306
255sub connect { 307sub connect {
256 my ($self, $signal, $cb) = @_; 308 my ($self, $signal, $cb) = @_;
257 309
258 push @{ $self->{cb}{$signal} }, $cb; 310 push @{ $self->{signal_cb}{$signal} }, $cb;
259} 311}
260 312
261sub emit { 313sub emit {
262 my ($self, $signal, @args) = @_; 314 my ($self, $signal, @args) = @_;
263 315
316 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
264 $_->($self, @args) 317 $cb->($self, @args);
265 for @{$self->{cb}{$signal} || []}; 318 }
266} 319}
267 320
268sub DESTROY { 321sub DESTROY {
269 my ($self) = @_; 322 my ($self) = @_;
270 323
276package CFClient::UI::DrawBG; 329package CFClient::UI::DrawBG;
277 330
278our @ISA = CFClient::UI::Base::; 331our @ISA = CFClient::UI::Base::;
279 332
280use strict; 333use strict;
281use SDL::OpenGL; 334use CFClient::OpenGL;
282 335
283sub new { 336sub new {
284 my $class = shift; 337 my $class = shift;
285 338
286 # range [value, low, high, page] 339 # range [value, low, high, page]
339 392
340 $self 393 $self
341} 394}
342 395
343sub add { 396sub add {
344 my ($self, $chld) = @_; 397 my ($self, $child) = @_;
345 398
346 $chld->set_parent ($self); 399 $child->set_parent ($self);
400
401 use sort 'stable';
347 402
348 $self->{children} = [ 403 $self->{children} = [
349 sort { $a->{z} <=> $b->{z} } 404 sort { $a->{z} <=> $b->{z} }
350 @{$self->{children}}, $chld 405 @{$self->{children}}, $child
351 ]; 406 ];
352 407
353 $self->{w} = $self->{h} = -1; 408 $child->check_size;
354 $self->update;
355} 409}
356 410
357sub remove { 411sub remove {
358 my ($self, $widget) = @_; 412 my ($self, $child) = @_;
359 413
414 delete $child->{parent};
415
360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 416 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
361 417
362 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 418 $self->check_size;
363} 419}
364 420
365sub find_widget { 421sub find_widget {
366 my ($self, $x, $y) = @_; 422 my ($self, $x, $y) = @_;
367 423
397 453
398 $class->SUPER::new (children => [$child], %arg) 454 $class->SUPER::new (children => [$child], %arg)
399} 455}
400 456
401sub add { 457sub add {
402 my ($self, $widget) = @_; 458 my ($self, $child) = @_;
403 459
404 $self->{children} = []; 460 $self->{children} = [];
405 461
406 $self->SUPER::add ($widget); 462 $self->SUPER::add ($child);
407} 463}
408 464
409sub remove { 465sub remove {
410 my ($self, $widget) = @_; 466 my ($self, $widget) = @_;
411 467
420sub size_request { 476sub size_request {
421 $_[0]{children}[0]->size_request 477 $_[0]{children}[0]->size_request
422} 478}
423 479
424sub size_allocate { 480sub size_allocate {
425 my ($self, $x, $y, $w, $h) = @_; 481 my ($self, $w, $h) = @_;
426 482
427 $self->_size_allocate ($x, $y, $w, $h) or return;
428
429 $self->{children}[0]->size_allocate (0, 0, $w, $h); 483 $self->{children}[0]->configure (0, 0, $w, $h);
430} 484}
431 485
432############################################################################# 486#############################################################################
433 487
434package CFClient::UI::Window; 488package CFClient::UI::Window;
435 489
436our @ISA = CFClient::UI::Bin::; 490our @ISA = CFClient::UI::Bin::;
437 491
438use SDL::OpenGL; 492use CFClient::OpenGL;
439 493
440sub new { 494sub new {
441 my ($class, %arg) = @_; 495 my ($class, %arg) = @_;
442 496
443 my $self = $class->SUPER::new (%arg); 497 my $self = $class->SUPER::new (%arg);
460 $self->child->draw; 514 $self->child->draw;
461 }; 515 };
462} 516}
463 517
464sub size_allocate { 518sub size_allocate {
465 my ($self, $x, $y, $w, $h) = @_; 519 my ($self, $w, $h) = @_;
466 520
467 $self->_size_allocate ($x, $y, $w, $h) or return;
468
469 $self->child->size_allocate (0, 0, $w, $h); 521 $self->child->configure (0, 0, $w, $h);
470 522
471 $self->render_chld; 523 $self->render_chld;
472} 524}
473 525
474sub _draw { 526sub _draw {
490 glDisable GL_TEXTURE_2D; 542 glDisable GL_TEXTURE_2D;
491} 543}
492 544
493############################################################################# 545#############################################################################
494 546
547package CFClient::UI::ViewPort;
548
549our @ISA = CFClient::UI::Window::;
550
551sub new { die }
552
553sub size_request {
554 my ($self) = @_;
555
556 @$self{qw(child_w child_h)} = $self->child->size_request;
557 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
558
559 @$self{qw(child_w child_h)}
560}
561
562sub _draw {
563 my ($self) = @_;
564
565 $self->{children}[1]->draw;
566}
567
568
569#############################################################################
570
495package CFClient::UI::Frame; 571package CFClient::UI::Frame;
496 572
497our @ISA = CFClient::UI::Bin::; 573our @ISA = CFClient::UI::Bin::;
498 574
499use SDL::OpenGL; 575use CFClient::OpenGL;
500 576
501sub size_request { 577sub size_request {
502 my ($self) = @_; 578 my ($self) = @_;
503 my $chld = $self->child 579 my $chld = $self->child
504 or return (0, 0); 580 or return (0, 0);
509} 585}
510 586
511sub size_allocate { 587sub size_allocate {
512 my ($self, $x, $y, $w, $h) = @_; 588 my ($self, $x, $y, $w, $h) = @_;
513 589
514 $self->_size_allocate ($x, $y, $w, $h) or return;
515
516 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 590 $self->child->configure (2, 2, $w - 4, $h - 4);
517} 591}
518 592
519sub _draw { 593sub _draw {
520 my ($self) = @_; 594 my ($self) = @_;
521 595
538 612
539package CFClient::UI::FancyFrame; 613package CFClient::UI::FancyFrame;
540 614
541our @ISA = CFClient::UI::Bin::; 615our @ISA = CFClient::UI::Bin::;
542 616
543use SDL::OpenGL; 617use CFClient::OpenGL;
544 618
545my @tex = 619my @tex =
546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 620 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
547 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 621 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
548 622
552 # TODO: user_x, user_y, overwrite moveto? 626 # TODO: user_x, user_y, overwrite moveto?
553 627
554 $class->SUPER::new ( 628 $class->SUPER::new (
555 bg => [1, 1, 1, 1], 629 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 630 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 631 border => 0.8,
558 @_ 632 @_
559 ) 633 )
560} 634}
561 635
636sub border {
637 int $_[0]{border} * $::FONTSIZE
638}
639
562sub size_request { 640sub size_request {
563 my ($self) = @_; 641 my ($self) = @_;
564 642
565 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h}; 643 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
566 644
567 my ($w, $h) = $self->SUPER::size_request; 645 my ($w, $h) = $self->SUPER::size_request;
568 646
569 ( 647 (
570 $w + $self->{border} * 2, 648 $w + $self->border * 2,
571 $h + $self->{border} * 2, 649 $h + $self->border * 2,
572 ) 650 )
573} 651}
574 652
575sub size_allocate { 653sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_; 654 my ($self, $w, $h) = @_;
577 655
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $h -= List::Util::max 0, $self->{border} * 2; 656 $h -= List::Util::max 0, $self->border * 2;
581 $w -= List::Util::max 0, $self->{border} * 2; 657 $w -= List::Util::max 0, $self->border * 2;
582 658
583 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 659 $self->child->configure ($self->border, $self->border, $w, $h);
584} 660}
585 661
586sub button_down { 662sub button_down {
587 my ($self, $ev, $x, $y) = @_; 663 my ($self, $ev, $x, $y) = @_;
588 664
665 my $border = $self->border;
666
589 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 667 if ($x < $self->{w} && $x >= $self->{w} - $border
590 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 668 && $y < $self->{h} && $y >= $self->{h} - $border) {
591 669
592 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 670 my ($ox, $oy) = ($ev->{x}, $ev->{y});
593 my ($bw, $bh) = ($self->{w}, $self->{h}); 671 my ($bw, $bh) = ($self->{w}, $self->{h});
594 672
595 $self->{motion} = sub { 673 $self->{motion} = sub {
596 my ($ev, $x, $y) = @_; 674 my ($ev, $x, $y) = @_;
597 675
598 ($x, $y) = ($ev->motion_x, $ev->motion_y); 676 ($x, $y) = ($ev->{x}, $ev->{y});
599 677
600 $self->{user_w} = $bw + $x - $ox; 678 $self->{user_w} = $bw + $x - $ox;
601 $self->{user_h} = $bh + $y - $oy; 679 $self->{user_h} = $bh + $y - $oy;
602 $self->update; 680 $self->check_size;
603 }; 681 };
604 682
605 } elsif ($x >= 0 && $x < $self->{w} 683 } elsif ($x >= 0 && $x < $self->{w}
606 && $y >= 0 && $y < $self->{border}) { 684 && $y >= 0 && $y < $border) {
607 685
608 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 686 my ($ox, $oy) = ($ev->{x}, $ev->{y});
609 my ($bx, $by) = ($self->{x}, $self->{y}); 687 my ($bx, $by) = ($self->{x}, $self->{y});
610 688
611 $self->{motion} = sub { 689 $self->{motion} = sub {
612 my ($ev, $x, $y) = @_; 690 my ($ev, $x, $y) = @_;
613 691
614 ($x, $y) = ($ev->motion_x, $ev->motion_y); 692 ($x, $y) = ($ev->{x}, $ev->{y});
615 693
616 $self->move ($bx + $x - $ox, $by + $y - $oy); 694 $self->move ($bx + $x - $ox, $by + $y - $oy);
617 $self->update; 695 $self->update;
618 }; 696 };
619 } 697 }
640 glEnable GL_BLEND; 718 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 719 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D; 720 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 721 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
644 722
723 my $border = $self->border;
724
645 glColor @{ $self->{border_bg} }; 725 glColor @{ $self->{border_bg} };
646 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 726 $tex[1]->draw_quad (0, 0, $w, $border);
647 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 727 $tex[3]->draw_quad (0, $border, $border, $ch);
648 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 728 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
649 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 729 $tex[4]->draw_quad (0, $h - $border, $w, $border);
650 730
651 my $bg = $tex[0]; 731 my $bg = $tex[0];
652 732
653 # TODO: repeat texture not scale 733 # TODO: repeat texture not scale
654 my $rep_x = $cw / $bg->{w}; 734 my $rep_x = $cw / $bg->{w};
657 glColor @{ $self->{bg} }; 737 glColor @{ $self->{bg} };
658 738
659 $bg->{s} = $rep_x; 739 $bg->{s} = $rep_x;
660 $bg->{t} = $rep_y; 740 $bg->{t} = $rep_y;
661 $bg->{wrap_mode} = 1; 741 $bg->{wrap_mode} = 1;
662 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 742 $bg->draw_quad ($border, $border, $cw, $ch);
663 743
664 glDisable GL_TEXTURE_2D; 744 glDisable GL_TEXTURE_2D;
665 glDisable GL_BLEND; 745 glDisable GL_BLEND;
666 746
667 $self->child->draw; 747 $self->child->draw;
673 753
674our @ISA = CFClient::UI::Base::; 754our @ISA = CFClient::UI::Base::;
675 755
676use List::Util qw(max sum); 756use List::Util qw(max sum);
677 757
678use SDL::OpenGL; 758use CFClient::OpenGL;
679 759
680sub new { 760sub new {
681 my $class = shift; 761 my $class = shift;
682 762
683 $class->SUPER::new ( 763 $class->SUPER::new (
685 @_ 765 @_
686 ) 766 )
687} 767}
688 768
689sub add { 769sub add {
690 my ($self, $x, $y, $chld) = @_; 770 my ($self, $x, $y, $child) = @_;
691 771
772 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 773 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 774
695 $self->{w} = $self->{h} = -1; 775 $child->check_size;
776}
777
778# TODO: move to container class maybe? send childs a signal on removal?
779sub clear {
780 my ($self) = @_;
781
782 delete $self->{children};
696 $self->update; 783 $self->update;
697} 784}
698 785
699sub get_wh { 786sub get_wh {
700 my ($self) = @_; 787 my ($self) = @_;
728 (sum @$hs), 815 (sum @$hs),
729 ) 816 )
730} 817}
731 818
732sub size_allocate { 819sub size_allocate {
733 my ($self, $x, $y, $w, $h) = @_; 820 my ($self, $w, $h) = @_;
734
735 $self->_size_allocate ($x, $y, $w, $h) or return;
736 821
737 my ($ws, $hs) = $self->get_wh; 822 my ($ws, $hs) = $self->get_wh;
738 823
739 my $req_w = sum @$ws; 824 my $req_w = sum @$ws;
740 my $req_h = sum @$hs; 825 my $req_h = sum @$hs;
762 847
763 for my $c (0 .. $#$row) { 848 for my $c (0 .. $#$row) {
764 my $col_w = $ws->[$c]; 849 my $col_w = $ws->[$c];
765 850
766 if (my $widget = $row->[$c]) { 851 if (my $widget = $row->[$c]) {
767 $widget->size_allocate ($x, $y, $col_w, $row_h); 852 $widget->configure ($x, $y, $col_w, $row_h);
768 } 853 }
769 854
770 $x += $col_w; 855 $x += $col_w;
771 } 856 }
772 857
817 (List::Util::max map $_->[1], @alloc), 902 (List::Util::max map $_->[1], @alloc),
818 ) 903 )
819} 904}
820 905
821sub size_allocate { 906sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_; 907 my ($self, $w, $h) = @_;
823
824 $self->_size_allocate ($x, $y, $w, $h);
825 908
826 ($h, $w) = ($w, $h); 909 ($h, $w) = ($w, $h);
827 910
828 my $children = $self->{children}; 911 my $children = $self->{children};
829 912
851 934
852 my $y = 0; 935 my $y = 0;
853 for (0 .. $#$children) { 936 for (0 .. $#$children) {
854 my $child = $children->[$_]; 937 my $child = $children->[$_];
855 my $h = $h[$_]; 938 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w); 939 $child->configure ($y, 0, $h, $w);
857 940
858 $y += $h; 941 $y += $h;
859 } 942 }
943
944 1
860} 945}
861 946
862############################################################################# 947#############################################################################
863 948
864package CFClient::UI::VBox; 949package CFClient::UI::VBox;
877 (List::Util::sum map $_->[1], @alloc), 962 (List::Util::sum map $_->[1], @alloc),
878 ) 963 )
879} 964}
880 965
881sub size_allocate { 966sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_; 967 my ($self, $w, $h) = @_;
883
884 $self->_size_allocate ($x, $y, $w, $h);
885 968
886 my $children = $self->{children}; 969 my $children = $self->{children};
887 970
888 my @h = map +($_->size_request)[1], @$children; 971 my @h = map +($_->size_request)[1], @$children;
889 972
907 990
908 my $y = 0; 991 my $y = 0;
909 for (0 .. $#$children) { 992 for (0 .. $#$children) {
910 my $child = $children->[$_]; 993 my $child = $children->[$_];
911 my $h = $h[$_]; 994 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h); 995 $child->configure (0, $y, $w, $h);
913 996
914 $y += $h; 997 $y += $h;
915 } 998 }
999
1000 1
916} 1001}
917 1002
918############################################################################# 1003#############################################################################
919 1004
920package CFClient::UI::Label; 1005package CFClient::UI::Label;
921 1006
922our @ISA = CFClient::UI::Base::; 1007our @ISA = CFClient::UI::Base::;
923 1008
924use SDL::OpenGL; 1009use CFClient::OpenGL;
925 1010
926sub new { 1011sub new {
927 my ($class, %arg) = @_; 1012 my ($class, %arg) = @_;
928 1013
929 my $self = $class->SUPER::new ( 1014 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 1015 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE, 1016 fontsize => 1,
932 text => "", 1017 text => "",
933 align => -1, 1018 align => -1,
1019 valign => -1,
934 padding => 2, 1020 padding => 2,
935 layout => new CFClient::Layout, 1021 layout => new CFClient::Layout,
936 %arg 1022 %arg
937 ); 1023 );
938 1024
939 $self->set_text ($self->{text}); 1025 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d#
1026
1027 $self->set_text (delete $self->{text}) if exists $self->{text};
1028 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
940 1029
941 $self 1030 $self
942} 1031}
943 1032
944sub escape_text { 1033sub escape_text {
952} 1041}
953 1042
954sub set_text { 1043sub set_text {
955 my ($self, $text) = @_; 1044 my ($self, $text) = @_;
956 1045
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text); 1046 $self->{layout}->set_text ($text);
959 1047
960 delete $self->{texture}; 1048 delete $self->{texture};
961# $self->{w} = $self->{h} = -1;
962 $self->update; 1049 $self->update;
963} 1050}
964 1051
965sub get_text { 1052sub set_markup {
966 my ($self, $text) = @_; 1053 my ($self, $markup) = @_;
967 1054
968 $self->{text} 1055 $self->{layout}->set_markup ($markup);
1056
1057 delete $self->{texture};
1058 $self->update;
969} 1059}
970 1060
971sub size_request { 1061sub size_request {
972 my ($self) = @_; 1062 my ($self) = @_;
973 1063
974 $self->{layout}->set_width; 1064 $self->{layout}->set_width;
975 $self->{layout}->set_height ($self->{fontsize}); 1065 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1066
976 my ($w, $h) = $self->{layout}->size; 1067 my ($w, $h) = $self->{layout}->size;
977 1068
978 ( 1069 (
979 $w + $self->{padding} * 2, 1070 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2, 1071 $h + $self->{padding} * 2,
981 ) 1072 )
982} 1073}
983 1074
984sub size_allocate { 1075sub size_allocate {
985 my ($self, $x, $y, $w, $h) = @_; 1076 my ($self, $w, $h) = @_;
986
987 $self->_size_allocate ($x, $y, $w, $h) or return;
988 1077
989 delete $self->{texture}; 1078 delete $self->{texture};
990}
991
992sub update {
993 my ($self) = @_;
994
995 delete $self->{texture};
996 $self->SUPER::update;
997} 1079}
998 1080
999sub _draw { 1081sub _draw {
1000 my ($self) = @_; 1082 my ($self) = @_;
1001 1083
1002 my $tex = $self->{texture} ||= do { 1084 my $tex = $self->{texture} ||= do {
1003 $self->{layout}->set_width ($self->{w}); 1085 $self->{layout}->set_width ($self->{w});
1004 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1086 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1005 new_from_layout CFClient::Texture $self->{layout} 1087 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1088 };
1007 1089
1008 glEnable GL_BLEND; 1090 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1091 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1092 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1093 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1094
1013 glColor @{$self->{fg}}; 1095 glColor @{$self->{fg}};
1014 1096
1015 my $x = 1097 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1098 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1099 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1100 : ($self->{w} - $tex->{w}) * 0.5
1101 );
1019 1102
1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1103 $self->{oy} = int (
1104 $self->{valign} < 0 ? $self->{padding}
1105 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1106 : ($self->{h} - $tex->{h}) * 0.5
1107 );
1108
1109 $tex->draw_quad ($self->{ox}, $self->{oy});
1021 1110
1022 glDisable GL_TEXTURE_2D; 1111 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1112 glDisable GL_BLEND;
1024} 1113}
1025 1114
1026############################################################################# 1115#############################################################################
1027 1116
1028package CFClient::UI::Entry; 1117package CFClient::UI::EntryBase;
1029 1118
1030our @ISA = CFClient::UI::Label::; 1119our @ISA = CFClient::UI::Label::;
1031 1120
1032use SDL; 1121use CFClient::OpenGL;
1033use SDL::OpenGL;
1034 1122
1035sub new { 1123sub new {
1036 my $class = shift; 1124 my $class = shift;
1037 1125
1038 $class->SUPER::new ( 1126 $class->SUPER::new (
1040 bg => [0, 0, 0, 0.2], 1128 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1129 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1130 active_fg => [0, 0, 0],
1043 can_hover => 1, 1131 can_hover => 1,
1044 can_focus => 1, 1132 can_focus => 1,
1133 valign => 0,
1045 @_ 1134 @_
1046 ) 1135 )
1047} 1136}
1048 1137
1049sub _set_text { 1138sub _set_text {
1050 my ($self, $text) = @_; 1139 my ($self, $text) = @_;
1051 1140
1052 my $old_text = $self->{text}; 1141 delete $self->{cur_h};
1142
1143 return if $self->{text} eq $text;
1144
1145 delete $self->{texture};
1053 1146
1054 $self->{last_activity} = $::NOW; 1147 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1148 $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 1149
1060 $text =~ s/./*/g if $self->{hidden}; 1150 $text =~ s/./*/g if $self->{hidden};
1151 $self->{layout}->set_text ("$text ");
1061 1152
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1153 $self->emit (changed => $self->{text});
1154}
1063 1155
1064 $text = substr $text, 0, $self->{cursor}; 1156sub get_text {
1065 utf8::encode $text; 1157 $_[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} 1158}
1072 1159
1073sub size_request { 1160sub size_request {
1074 my ($self) = @_; 1161 my ($self) = @_;
1075 1162
1077 1164
1078 ($w + 1, $h) # add 1 for cursor 1165 ($w + 1, $h) # add 1 for cursor
1079} 1166}
1080 1167
1081sub size_allocate { 1168sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1169 my ($self, $w, $h) = @_;
1083
1084 $self->SUPER::size_allocate ($x, $y, $w, $h);
1085 1170
1086 $self->_set_text ($self->{text}); 1171 $self->_set_text ($self->{text});
1087} 1172}
1088 1173
1089sub set_text { 1174sub set_text {
1095} 1180}
1096 1181
1097sub key_down { 1182sub key_down {
1098 my ($self, $ev) = @_; 1183 my ($self, $ev) = @_;
1099 1184
1100 my $mod = $ev->key_mod; 1185 my $mod = $ev->{mod};
1101 my $sym = $ev->key_sym; 1186 my $sym = $ev->{sym};
1102
1103 my $uni = $ev->key_unicode; 1187 my $uni = $ev->{unicode};
1104 1188
1105 my $text = $self->get_text; 1189 my $text = $self->get_text;
1106 1190
1107 if ($sym == SDLK_BACKSPACE) { 1191 if ($sym == 8) {
1108 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1192 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1109 } elsif ($sym == SDLK_DELETE) { 1193 } elsif ($sym == 127) {
1110 substr $text, $self->{cursor}, 1, ""; 1194 substr $text, $self->{cursor}, 1, "";
1111 } elsif ($sym == SDLK_LEFT) { 1195 } elsif ($sym == CFClient::SDLK_LEFT) {
1112 --$self->{cursor} if $self->{cursor}; 1196 --$self->{cursor} if $self->{cursor};
1113 } elsif ($sym == SDLK_RIGHT) { 1197 } elsif ($sym == CFClient::SDLK_RIGHT) {
1114 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1198 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) { 1199 } elsif ($sym == CFClient::SDLK_HOME) {
1116 $self->{cursor} = 0; 1200 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) { 1201 } elsif ($sym == CFClient::SDLK_END) {
1118 $self->{cursor} = length $text; 1202 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) { 1203 } elsif ($sym == 27) {
1120 $self->emit ('escape'); 1204 $self->emit ('escape');
1121 } elsif ($uni) { 1205 } elsif ($uni) {
1122 substr $text, $self->{cursor}++, 0, chr $uni; 1206 substr $text, $self->{cursor}++, 0, chr $uni;
1123 } 1207 }
1124 1208
1179 1263
1180 $self->SUPER::_draw; 1264 $self->SUPER::_draw;
1181 1265
1182 #TODO: force update every cursor change :( 1266 #TODO: force update every cursor change :(
1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1267 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1268
1269 unless (exists $self->{cur_h}) {
1270 my $text = substr $self->{text}, 0, $self->{cursor};
1271 utf8::encode $text;
1272
1273 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1274 }
1275
1184 glColor @{$self->{fg}}; 1276 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1277 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1278 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1279 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1188 glEnd; 1280 glEnd;
1189 } 1281 }
1190} 1282}
1191 1283
1192package CFClient::UI::LineEntry; 1284package CFClient::UI::Entry;
1193 1285
1194our @ISA = CFClient::UI::Entry::; 1286our @ISA = CFClient::UI::EntryBase::;
1195 1287
1196use SDL; 1288use CFClient::OpenGL;
1197use SDL::OpenGL;
1198 1289
1199sub key_down { 1290sub key_down {
1200 my ($self, $ev) = @_; 1291 my ($self, $ev) = @_;
1201 1292
1202 my $sym = $ev->key_sym; 1293 my $sym = $ev->{sym};
1203 1294
1204 if ($sym == SDLK_RETURN) { 1295 if ($sym == 13) {
1205 $self->emit (activate => $self->get_text); 1296 $self->emit (activate => $self->get_text);
1206 $self->update; 1297 $self->update;
1207 1298
1208 } else { 1299 } else {
1209 $self->SUPER::key_down ($ev); 1300 $self->SUPER::key_down ($ev);
1215 1306
1216package CFClient::UI::Button; 1307package CFClient::UI::Button;
1217 1308
1218our @ISA = CFClient::UI::Label::; 1309our @ISA = CFClient::UI::Label::;
1219 1310
1220use SDL; 1311use CFClient::OpenGL;
1221use SDL::OpenGL;
1222 1312
1223my @tex = 1313my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1314 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1225 qw(b1_button_active.png); 1315 qw(b1_button_active.png);
1226 1316
1229 1319
1230 $class->SUPER::new ( 1320 $class->SUPER::new (
1231 padding => 4, 1321 padding => 4,
1232 fg => [1, 1, 1], 1322 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1323 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1324 active_fg => [0, 0, 1],
1235 can_hover => 1, 1325 can_hover => 1,
1326 align => 0,
1327 valign => 0,
1236 @_ 1328 @_
1237 ) 1329 )
1238} 1330}
1239 1331
1240sub button_up { 1332sub button_up {
1248 1340
1249sub _draw { 1341sub _draw {
1250 my ($self) = @_; 1342 my ($self) = @_;
1251 1343
1252 local $self->{fg} = $self->{fg}; 1344 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 1345
1259 if ($GRAB == $self) { 1346 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1347 $self->{fg} = $self->{active_fg};
1261 } 1348 }
1262 1349
1263 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1350 glEnable GL_BLEND;
1351 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1352 glEnable GL_TEXTURE_2D;
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1353 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1354 glColor 0, 0, 0, 1;
1265 1355
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1356 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1357
1268 glDisable GL_TEXTURE_2D; 1358 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1359 glDisable GL_BLEND;
1270 1360
1271 $self->SUPER::_draw; 1361 $self->SUPER::_draw;
1279 1369
1280my @tex = 1370my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1371 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1372 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283 1373
1284use SDL; 1374use CFClient::OpenGL;
1285use SDL::OpenGL;
1286 1375
1287sub new { 1376sub new {
1288 my $class = shift; 1377 my $class = shift;
1289 1378
1290 $class->SUPER::new ( 1379 $class->SUPER::new (
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}) {
1342 glDisable GL_BLEND; 1425 glDisable GL_BLEND;
1343} 1426}
1344 1427
1345############################################################################# 1428#############################################################################
1346 1429
1430package CFClient::UI::VGauge;
1431
1432our @ISA = CFClient::UI::Base::;
1433
1434use CFClient::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 (
1460 gauge => 'food',
1461 @_
1462 );
1463
1464 $self->{aspect} = $tex{$self->{gauge}}[0]{w} / $tex{$self->{gauge}}[0]{h};
1465
1466 $self
1467}
1468
1469sub size_request {
1470 my ($self) = @_;
1471
1472 my $tex = $tex{$self->{gauge}}[0];
1473
1474 @$tex{qw(w h)}
1475}
1476
1477sub set_max {
1478 my ($self, $max) = @_;
1479
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493}
1494
1495sub _draw {
1496 my ($self) = @_;
1497
1498 my $tex = $tex{$self->{gauge}};
1499
1500 my ($w, $h) = ($self->{w}, $self->{h});
1501
1502 my $ycut = $self->{val} / ($self->{max_val} || 1);
1503 $ycut = 1 if $self->{val} > $self->{max_val};
1504
1505 my $t1 = $tex->[0];
1506 my $t2 = $tex->[1];
1507
1508 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512
1513 my $h1 = $self->{h} - $ycut * $self->{h};
1514 my $h2 = $ycut * $self->{h};
1515
1516 my $yp = 0;
1517
1518 glBindTexture GL_TEXTURE_2D, $t1->{name};
1519 glBegin GL_QUADS;
1520 glTexCoord 0 , 0; glVertex 0 , $yp;
1521 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $yp + $h1;
1522 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex 0 + $w, $yp + $h1;
1523 glTexCoord $t1->{s}, 0; glVertex 0 + $w, $yp;
1524 glEnd;
1525
1526 $yp += $h1;
1527
1528 glBindTexture GL_TEXTURE_2D, $t2->{name};
1529 glBegin GL_QUADS;
1530 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $yp;
1531 glTexCoord 0 , $t2->{t}; glVertex 0 , $yp + $h2;
1532 glTexCoord $t2->{s}, $t2->{t}; glVertex 0 + $w, $yp + $h2;
1533 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex 0 + $w, $yp;
1534 glEnd;
1535
1536 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D;
1538}
1539
1540#############################################################################
1541
1347package CFClient::UI::Slider; 1542package CFClient::UI::Slider;
1348 1543
1349use strict; 1544use strict;
1350 1545
1351use SDL::OpenGL; 1546use CFClient::OpenGL;
1352 1547
1353our @ISA = CFClient::UI::DrawBG::; 1548our @ISA = CFClient::UI::DrawBG::;
1354 1549
1355my @tex = 1550my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1551 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1366 # TODO: calculations are off 1561 # TODO: calculations are off
1367 my $self = $class->SUPER::new ( 1562 my $self = $class->SUPER::new (
1368 fg => [1, 1, 1], 1563 fg => [1, 1, 1],
1369 active_fg => [0, 0, 0], 1564 active_fg => [0, 0, 0],
1370 range => [0, 0, 100, 10], 1565 range => [0, 0, 100, 10],
1371 req_w => 40, 1566 req_w => 20,
1372 req_h => 13, 1567 req_h => 20,
1373 vertical => 0, 1568 vertical => 0,
1374 can_hover => 1, 1569 can_hover => 1,
1375 inner_pad => 5, 1570 inner_pad => 5,
1376 @_ 1571 @_
1377 ); 1572 );
1481 1676
1482package CFClient::UI::TextView; 1677package CFClient::UI::TextView;
1483 1678
1484our @ISA = CFClient::UI::HBox::; 1679our @ISA = CFClient::UI::HBox::;
1485 1680
1486use SDL::OpenGL; 1681use CFClient::OpenGL;
1487 1682
1488sub new { 1683sub new {
1489 my $class = shift; 1684 my $class = shift;
1490 1685
1491 my $self = $class->SUPER::new ( 1686 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE, 1687 fontsize => 1,
1495 @_, 1688 @_,
1496 1689
1497 layout => (new CFClient::Layout), 1690 layout => (new CFClient::Layout),
1498 par => [], 1691 par => [],
1499 height => 0, 1692 height => 0,
1520sub text_height { 1713sub text_height {
1521 my ($self, $text) = @_; 1714 my ($self, $text) = @_;
1522 1715
1523 my $layout = $self->{layout}; 1716 my $layout = $self->{layout};
1524 1717
1525 $layout->set_height ($self->{fontsize}); 1718 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1526 $layout->set_width ($self->{w}); 1719 $layout->set_width ($self->{w});
1527 $layout->set_text ($text); 1720 $layout->set_text ($text);
1528 1721
1529 ($layout->size)[1] 1722 ($layout->size)[1]
1530} 1723}
1534 1727
1535 $self->{need_reflow}++; 1728 $self->{need_reflow}++;
1536 $self->update; 1729 $self->update;
1537} 1730}
1538 1731
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate { 1732sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 1733 my ($self, $w, $h) = @_;
1547 1734
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 1735 $self->SUPER::size_allocate ($w, $h);
1549 1736
1550 $self->{layout}->set_height ($self->{fontsize}); 1737 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 $self->{layout}->set_width ($self->{w}); 1738 $self->{layout}->set_width ($self->{children}[0]{w});
1552 1739
1553 $self->reflow; 1740 $self->reflow;
1554} 1741}
1555 1742
1556sub add_paragraph { 1743sub add_paragraph {
1575 1762
1576 return unless $self->{h} > 0; 1763 return unless $self->{h} > 0;
1577 1764
1578 delete $self->{texture}; 1765 delete $self->{texture};
1579 1766
1580 $TOPLEVEL->on_refresh ($self, sub { 1767 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1768 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1769 my $height = 0;
1583 1770
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1771 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1772 for @{$self->{par}};
1643 1830
1644} 1831}
1645 1832
1646############################################################################# 1833#############################################################################
1647 1834
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; 1835package CFClient::UI::Animator;
1883 1836
1884use SDL::OpenGL; 1837use CFClient::OpenGL;
1885 1838
1886our @ISA = CFClient::UI::Bin::; 1839our @ISA = CFClient::UI::Bin::;
1887 1840
1888sub moveto { 1841sub moveto {
1889 my ($self, $x, $y) = @_; 1842 my ($self, $x, $y) = @_;
1945sub toggle_flopper { 1898sub toggle_flopper {
1946 my ($self) = @_; 1899 my ($self) = @_;
1947 1900
1948 # TODO: use animation 1901 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 1902 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 1903 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 1904 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 1905 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 1906 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 1907 $CFClient::UI::ROOT->remove ($self->{other});
1908 $self->emit ("close");
1957 } 1909 }
1958}
1959 1910
1960############################################################################# 1911 $self->emit (changed => $self->{state});
1912}
1961 1913
1914#############################################################################
1915
1962package CFClient::UI::Toplevel; 1916package CFClient::UI::Root;
1963 1917
1964our @ISA = CFClient::UI::Container::; 1918our @ISA = CFClient::UI::Container::;
1965 1919
1966use SDL::OpenGL; 1920use CFClient::OpenGL;
1921
1922sub check_size {
1923 my ($self) = @_;
1924
1925 $self->configure (0, 0, $::WITH, $::HEIGHT);
1926}
1967 1927
1968sub size_request { 1928sub size_request {
1969 ($::WIDTH, $::HEIGHT) 1929 ($::WIDTH, $::HEIGHT)
1970} 1930}
1971 1931
1972sub size_allocate { 1932sub configure {
1973 my ($self, $x, $y, $w, $h) = @_; 1933 my ($self, $x, $y, $w, $h) = @_;
1974 1934
1975 $self->_size_allocate ($x, $y, $w, $h); 1935 $self->SUPER::configure ($x, $y, $w, $h);
1976 1936
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 1937 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 1938 for @{$self->{children}};
1979} 1939}
1980 1940
1981sub translate { 1941sub _topleft {
1982 my ($self, $x, $y) = @_; 1942 my ($self, $x, $y) = @_;
1983 1943
1984 ($x, $y) 1944 ($x, $y)
1985} 1945}
1986 1946
1987sub update { 1947sub update {
1988 my ($self) = @_; 1948 my ($self) = @_;
1989 1949
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 1950 $self->check_size;
1991 ::refresh (); 1951 ::refresh ();
1992} 1952}
1993 1953
1994sub add { 1954sub add {
1995 my ($self, $widget) = @_; 1955 my ($self, $child) = @_;
1996 1956
1997 $self->SUPER::add ($widget); 1957 $self->SUPER::add ($child);
1998
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000} 1958}
2001 1959
2002sub on_refresh { 1960sub on_refresh {
2003 my ($self, $id, $cb) = @_; 1961 my ($self, $id, $cb) = @_;
2004 1962
2027 1985
2028############################################################################# 1986#############################################################################
2029 1987
2030package CFClient::UI; 1988package CFClient::UI;
2031 1989
2032$TOPLEVEL = new CFClient::UI::Toplevel; 1990$ROOT = new CFClient::UI::Root;
2033 1991
20341 19921
2035 1993

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines