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.113 by root, Sat Apr 15 12:46:48 2006 UTC vs.
Revision 1.143 by root, Thu Apr 20 21:28:51 2006 UTC

21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
74sub harmonize { 74sub harmonize {
75 my ($vals) = @_; 75 my ($vals) = @_;
76 76
77 my $rem = 0; 77 my $rem = 0;
78 78
79 for ($vals) { 79 for (@$vals) {
80 my $i = int $_ + $rem; 80 my $i = int $_ + $rem;
81 $rem += $_ - $i; 81 $rem += $_ - $i;
82 $_ = $i; 82 $_ = $i;
83 } 83 }
84} 84}
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
151 if ($self->{aspect}) {
152 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154
155 # use alignment to adjust x, y
156
157 $x += ($w - $w2) * 0.5;
158 $y += ($h - $h2) * 0.5;
159
160 ($w, $h) = ($w2, $h2);
161 }
162
163 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 164 $self->{x} = $x;
135 $self->{y} = $y; 165 $self->{y} = $y;
166 $self->update;
167 }
136 168
137 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 170 $self->{w} = $w;
140 $self->{h} = $h; 171 $self->{h} = $h;
141 172
173 $self->size_allocate ($w, $h);
174 $self->update;
142 1 175 }
143} 176}
144 177
145sub size_allocate { 178sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 179 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 180}
150 181
151# return top left coordinates 182# return top left coordinates
152sub _topleft { 183sub _topleft {
153 my ($self, $x, $y) = @_; 184 my ($self, $x, $y) = @_;
176 207
177 return if $FOCUS == $self; 208 return if $FOCUS == $self;
178 return unless $self->{can_focus}; 209 return unless $self->{can_focus};
179 210
180 my $focus = $FOCUS; $FOCUS = $self; 211 my $focus = $FOCUS; $FOCUS = $self;
212
213 $self->emit (focus_in => $focus);
214
181 $focus->update if $focus; 215 $focus->update if $focus;
182 $FOCUS->update; 216 $FOCUS->update;
183} 217}
184 218
185sub focus_out { 219sub focus_out {
186 my ($self) = @_; 220 my ($self) = @_;
187 221
188 return unless $FOCUS == $self; 222 return unless $FOCUS == $self;
189 223
190 my $focus = $FOCUS; undef $FOCUS; 224 my $focus = $FOCUS; undef $FOCUS;
225
226 $self->emit (focus_out => $focus);
227
191 $focus->update if $focus; #? 228 $focus->update if $focus; #?
192} 229}
193 230
194sub mouse_motion { } 231sub mouse_motion { }
195sub button_up { } 232sub button_up { }
219 glPopMatrix; 256 glPopMatrix;
220 257
221 if ($self == $HOVER && $self->{can_hover}) { 258 if ($self == $HOVER && $self->{can_hover}) {
222 my ($x, $y) = @$self{qw(x y)}; 259 my ($x, $y) = @$self{qw(x y)};
223 260
224 glColor 0, 0, 1, 0.2; 261 glColor 1, 0.8, 0.5, 0.2;
225 glEnable GL_BLEND; 262 glEnable GL_BLEND;
226 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 263 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
227 glBegin GL_QUADS; 264 glBegin GL_QUADS;
228 glVertex $x , $y; 265 glVertex $x , $y;
229 glVertex $x + $self->{w}, $y; 266 glVertex $x + $self->{w}, $y;
254 my ($self, $parent) = @_; 291 my ($self, $parent) = @_;
255 292
256 Scalar::Util::weaken ($self->{parent} = $parent); 293 Scalar::Util::weaken ($self->{parent} = $parent);
257} 294}
258 295
296sub check_size {
297 my ($self) = @_;
298
299 my ($w, $h) = $self->size_request;
300
301 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w;
303 $self->{req_h} = $h;
304
305 $self->{parent}->check_size
306 if $self->{parent};
307 }
308}
309
259sub update { 310sub update {
260 my ($self) = @_; 311 my ($self) = @_;
261 312
262 $self->{parent}->update 313 $self->{parent}->update
263 if $self->{parent}; 314 if $self->{parent};
264} 315}
265 316
266sub connect { 317sub connect {
267 my ($self, $signal, $cb) = @_; 318 my ($self, $signal, $cb) = @_;
268 319
269 push @{ $self->{cb}{$signal} }, $cb; 320 push @{ $self->{signal_cb}{$signal} }, $cb;
270} 321}
271 322
272sub emit { 323sub emit {
273 my ($self, $signal, @args) = @_; 324 my ($self, $signal, @args) = @_;
274 325
326 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
275 $_->($self, @args) 327 $cb->($self, @args);
276 for @{$self->{cb}{$signal} || []}; 328 }
277} 329}
278 330
279sub DESTROY { 331sub DESTROY {
280 my ($self) = @_; 332 my ($self) = @_;
281 333
287package CFClient::UI::DrawBG; 339package CFClient::UI::DrawBG;
288 340
289our @ISA = CFClient::UI::Base::; 341our @ISA = CFClient::UI::Base::;
290 342
291use strict; 343use strict;
292use SDL::OpenGL; 344use CFClient::OpenGL;
293 345
294sub new { 346sub new {
295 my $class = shift; 347 my $class = shift;
296 348
297 # range [value, low, high, page] 349 # range [value, low, high, page]
361 $self->{children} = [ 413 $self->{children} = [
362 sort { $a->{z} <=> $b->{z} } 414 sort { $a->{z} <=> $b->{z} }
363 @{$self->{children}}, $child 415 @{$self->{children}}, $child
364 ]; 416 ];
365 417
366 $self->{w} = $self->{h} = -1; 418 $child->check_size;
367 $self->update;
368} 419}
369 420
370sub remove { 421sub remove {
371 my ($self, $widget) = @_; 422 my ($self, $child) = @_;
372 423
424 delete $child->{parent};
425
373 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
374 427
375 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 428 $self->check_size;
376} 429}
377 430
378sub find_widget { 431sub find_widget {
379 my ($self, $x, $y) = @_; 432 my ($self, $x, $y) = @_;
380 433
410 463
411 $class->SUPER::new (children => [$child], %arg) 464 $class->SUPER::new (children => [$child], %arg)
412} 465}
413 466
414sub add { 467sub add {
415 my ($self, $widget) = @_; 468 my ($self, $child) = @_;
416 469
417 $self->{children} = []; 470 $self->{children} = [];
418 471
419 $self->SUPER::add ($widget); 472 $self->SUPER::add ($child);
420} 473}
421 474
422sub remove { 475sub remove {
423 my ($self, $widget) = @_; 476 my ($self, $widget) = @_;
424 477
433sub size_request { 486sub size_request {
434 $_[0]{children}[0]->size_request 487 $_[0]{children}[0]->size_request
435} 488}
436 489
437sub size_allocate { 490sub size_allocate {
438 my ($self, $x, $y, $w, $h) = @_; 491 my ($self, $w, $h) = @_;
439 492
440 $self->_size_allocate ($x, $y, $w, $h) or return;
441
442 $self->{children}[0]->size_allocate (0, 0, $w, $h); 493 $self->{children}[0]->configure (0, 0, $w, $h);
443} 494}
444 495
445############################################################################# 496#############################################################################
446 497
447package CFClient::UI::Window; 498package CFClient::UI::Window;
448 499
449our @ISA = CFClient::UI::Bin::; 500our @ISA = CFClient::UI::Bin::;
450 501
451use SDL::OpenGL; 502use CFClient::OpenGL;
452 503
453sub new { 504sub new {
454 my ($class, %arg) = @_; 505 my ($class, %arg) = @_;
455 506
456 my $self = $class->SUPER::new (%arg); 507 my $self = $class->SUPER::new (%arg);
473 $self->child->draw; 524 $self->child->draw;
474 }; 525 };
475} 526}
476 527
477sub size_allocate { 528sub size_allocate {
478 my ($self, $x, $y, $w, $h) = @_; 529 my ($self, $w, $h) = @_;
479 530
480 $self->_size_allocate ($x, $y, $w, $h) or return;
481
482 $self->child->size_allocate (0, 0, $w, $h); 531 $self->child->configure (0, 0, $w, $h);
483 532
484 $self->render_chld; 533 $self->render_chld;
485} 534}
486 535
487sub _draw { 536sub _draw {
503 glDisable GL_TEXTURE_2D; 552 glDisable GL_TEXTURE_2D;
504} 553}
505 554
506############################################################################# 555#############################################################################
507 556
557package CFClient::UI::ViewPort;
558
559our @ISA = CFClient::UI::Window::;
560
561sub new { die }
562
563sub size_request {
564 my ($self) = @_;
565
566 @$self{qw(child_w child_h)} = $self->child->size_request;
567 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568
569 @$self{qw(child_w child_h)}
570}
571
572sub _draw {
573 my ($self) = @_;
574
575 $self->{children}[1]->draw;
576}
577
578
579#############################################################################
580
508package CFClient::UI::Frame; 581package CFClient::UI::Frame;
509 582
510our @ISA = CFClient::UI::Bin::; 583our @ISA = CFClient::UI::Bin::;
511 584
512use SDL::OpenGL; 585use CFClient::OpenGL;
513 586
514sub size_request { 587sub size_request {
515 my ($self) = @_; 588 my ($self) = @_;
516 my $chld = $self->child 589 my $chld = $self->child
517 or return (0, 0); 590 or return (0, 0);
522} 595}
523 596
524sub size_allocate { 597sub size_allocate {
525 my ($self, $x, $y, $w, $h) = @_; 598 my ($self, $x, $y, $w, $h) = @_;
526 599
527 $self->_size_allocate ($x, $y, $w, $h) or return;
528
529 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 600 $self->child->configure (2, 2, $w - 4, $h - 4);
530} 601}
531 602
532sub _draw { 603sub _draw {
533 my ($self) = @_; 604 my ($self) = @_;
534 605
551 622
552package CFClient::UI::FancyFrame; 623package CFClient::UI::FancyFrame;
553 624
554our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
555 626
556use SDL::OpenGL; 627use CFClient::OpenGL;
557 628
558my @tex = 629my @tex =
559 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
560 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 631 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
561 632
562sub new { 633sub new {
563 my $class = shift; 634 my $class = shift;
564 635
565 # TODO: user_x, user_y, overwrite moveto? 636 # TODO: user_x, user_y, overwrite moveto?
566 637
567 $class->SUPER::new ( 638 my $self = $class->SUPER::new (
568 bg => [1, 1, 1, 1], 639 bg => [1, 1, 1, 1],
569 border_bg => [1, 1, 1, 1], 640 border_bg => [1, 1, 1, 1],
570 border => $::FONTSIZE * 0.8, 641 border => 0.8,
571 @_ 642 @_
643 );
644
645 $self->{title} &&= new CFClient::UI::Label
646 align => 0,
647 valign => 1,
648 text => $self->{title},
649 fontsize => 1;
650
651 $self
652}
653
654sub border {
655 int $_[0]{border} * $::FONTSIZE
656}
657
658sub size_request {
659 my ($self) = @_;
660
661 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
662
663 my ($w, $h) = $self->SUPER::size_request;
664
665 (
666 $w + $self->border * 2,
667 $h + $self->border * 2,
572 ) 668 )
573} 669}
574 670
575sub size_request {
576 my ($self) = @_;
577
578 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
579
580 my ($w, $h) = $self->SUPER::size_request;
581
582 (
583 $w + $self->{border} * 2,
584 $h + $self->{border} * 2,
585 )
586}
587
588sub size_allocate { 671sub size_allocate {
589 my ($self, $x, $y, $w, $h) = @_; 672 my ($self, $w, $h) = @_;
590 673
591 $self->_size_allocate ($x, $y, $w, $h) or return;
592
593 $h -= List::Util::max 0, $self->{border} * 2; 674 $h -= List::Util::max 0, $self->border * 2;
594 $w -= List::Util::max 0, $self->{border} * 2; 675 $w -= List::Util::max 0, $self->border * 2;
595 676
677 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
678 if $self->{title};
679
596 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 680 $self->child->configure ($self->border, $self->border, $w, $h);
597} 681}
598 682
599sub button_down { 683sub button_down {
600 my ($self, $ev, $x, $y) = @_; 684 my ($self, $ev, $x, $y) = @_;
601 685
686 my $border = $self->border;
687
602 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 688 if ($x < $self->{w} && $x >= $self->{w} - $border
603 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 689 && $y < $self->{h} && $y >= $self->{h} - $border) {
604 690
605 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 691 my ($ox, $oy) = ($ev->{x}, $ev->{y});
606 my ($bw, $bh) = ($self->{w}, $self->{h}); 692 my ($bw, $bh) = ($self->{w}, $self->{h});
607 693
608 $self->{motion} = sub { 694 $self->{motion} = sub {
609 my ($ev, $x, $y) = @_; 695 my ($ev, $x, $y) = @_;
610 696
611 ($x, $y) = ($ev->motion_x, $ev->motion_y); 697 ($x, $y) = ($ev->{x}, $ev->{y});
612 698
613 $self->{user_w} = $bw + $x - $ox; 699 $self->{user_w} = $bw + $x - $ox;
614 $self->{user_h} = $bh + $y - $oy; 700 $self->{user_h} = $bh + $y - $oy;
615 $self->update; 701 $self->check_size;
616 }; 702 };
617 703
618 } elsif ($x >= 0 && $x < $self->{w} 704 } elsif ($x >= 0 && $x < $self->{w}
619 && $y >= 0 && $y < $self->{border}) { 705 && $y >= 0 && $y < $border) {
620 706
621 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 707 my ($ox, $oy) = ($ev->{x}, $ev->{y});
622 my ($bx, $by) = ($self->{x}, $self->{y}); 708 my ($bx, $by) = ($self->{x}, $self->{y});
623 709
624 $self->{motion} = sub { 710 $self->{motion} = sub {
625 my ($ev, $x, $y) = @_; 711 my ($ev, $x, $y) = @_;
626 712
627 ($x, $y) = ($ev->motion_x, $ev->motion_y); 713 ($x, $y) = ($ev->{x}, $ev->{y});
628 714
629 $self->move ($bx + $x - $ox, $by + $y - $oy); 715 $self->move ($bx + $x - $ox, $by + $y - $oy);
630 $self->update; 716 $self->update;
631 }; 717 };
632 } 718 }
653 glEnable GL_BLEND; 739 glEnable GL_BLEND;
654 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 740 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
655 glEnable GL_TEXTURE_2D; 741 glEnable GL_TEXTURE_2D;
656 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 742 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
657 743
744 my $border = $self->border;
745
658 glColor @{ $self->{border_bg} }; 746 glColor @{ $self->{border_bg} };
659 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 747 $tex[1]->draw_quad (0, 0, $w, $border);
660 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 748 $tex[3]->draw_quad (0, $border, $border, $ch);
661 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 749 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
662 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 750 $tex[4]->draw_quad (0, $h - $border, $w, $border);
663 751
664 my $bg = $tex[0]; 752 my $bg = $tex[0];
665 753
666 # TODO: repeat texture not scale 754 # TODO: repeat texture not scale
667 my $rep_x = $cw / $bg->{w}; 755 my $rep_x = $cw / $bg->{w};
670 glColor @{ $self->{bg} }; 758 glColor @{ $self->{bg} };
671 759
672 $bg->{s} = $rep_x; 760 $bg->{s} = $rep_x;
673 $bg->{t} = $rep_y; 761 $bg->{t} = $rep_y;
674 $bg->{wrap_mode} = 1; 762 $bg->{wrap_mode} = 1;
675 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 763 $bg->draw_quad ($border, $border, $cw, $ch);
676 764
677 glDisable GL_TEXTURE_2D; 765 glDisable GL_TEXTURE_2D;
678 glDisable GL_BLEND; 766 glDisable GL_BLEND;
679 767
768 $self->{title}->draw if $self->{title};
680 $self->child->draw; 769 $self->child->draw;
681} 770}
682 771
683############################################################################# 772#############################################################################
684 773
686 775
687our @ISA = CFClient::UI::Base::; 776our @ISA = CFClient::UI::Base::;
688 777
689use List::Util qw(max sum); 778use List::Util qw(max sum);
690 779
691use SDL::OpenGL; 780use CFClient::OpenGL;
692 781
693sub new { 782sub new {
694 my $class = shift; 783 my $class = shift;
695 784
696 $class->SUPER::new ( 785 $class->SUPER::new (
703 my ($self, $x, $y, $child) = @_; 792 my ($self, $x, $y, $child) = @_;
704 793
705 $child->set_parent ($self); 794 $child->set_parent ($self);
706 $self->{children}[$y][$x] = $child; 795 $self->{children}[$y][$x] = $child;
707 796
708 $self->{w} = $self->{h} = -1; 797 $child->check_size;
798}
799
800# TODO: move to container class maybe? send childs a signal on removal?
801sub clear {
802 my ($self) = @_;
803
804 delete $self->{children};
709 $self->update; 805 $self->update;
710} 806}
711 807
712sub get_wh { 808sub get_wh {
713 my ($self) = @_; 809 my ($self) = @_;
741 (sum @$hs), 837 (sum @$hs),
742 ) 838 )
743} 839}
744 840
745sub size_allocate { 841sub size_allocate {
746 my ($self, $x, $y, $w, $h) = @_; 842 my ($self, $w, $h) = @_;
747
748 $self->_size_allocate ($x, $y, $w, $h) or return;
749 843
750 my ($ws, $hs) = $self->get_wh; 844 my ($ws, $hs) = $self->get_wh;
751 845
752 my $req_w = sum @$ws; 846 my $req_w = sum @$ws;
753 my $req_h = sum @$hs; 847 my $req_h = sum @$hs;
775 869
776 for my $c (0 .. $#$row) { 870 for my $c (0 .. $#$row) {
777 my $col_w = $ws->[$c]; 871 my $col_w = $ws->[$c];
778 872
779 if (my $widget = $row->[$c]) { 873 if (my $widget = $row->[$c]) {
780 $widget->size_allocate ($x, $y, $col_w, $row_h); 874 $widget->configure ($x, $y, $col_w, $row_h);
781 } 875 }
782 876
783 $x += $col_w; 877 $x += $col_w;
784 } 878 }
785 879
830 (List::Util::max map $_->[1], @alloc), 924 (List::Util::max map $_->[1], @alloc),
831 ) 925 )
832} 926}
833 927
834sub size_allocate { 928sub size_allocate {
835 my ($self, $x, $y, $w, $h) = @_; 929 my ($self, $w, $h) = @_;
836
837 $self->_size_allocate ($x, $y, $w, $h);
838 930
839 ($h, $w) = ($w, $h); 931 ($h, $w) = ($w, $h);
840 932
841 my $children = $self->{children}; 933 my $children = $self->{children};
842 934
864 956
865 my $y = 0; 957 my $y = 0;
866 for (0 .. $#$children) { 958 for (0 .. $#$children) {
867 my $child = $children->[$_]; 959 my $child = $children->[$_];
868 my $h = $h[$_]; 960 my $h = $h[$_];
869 $child->size_allocate ($y, 0, $h, $w); 961 $child->configure ($y, 0, $h, $w);
870 962
871 $y += $h; 963 $y += $h;
872 } 964 }
965
966 1
873} 967}
874 968
875############################################################################# 969#############################################################################
876 970
877package CFClient::UI::VBox; 971package CFClient::UI::VBox;
890 (List::Util::sum map $_->[1], @alloc), 984 (List::Util::sum map $_->[1], @alloc),
891 ) 985 )
892} 986}
893 987
894sub size_allocate { 988sub size_allocate {
895 my ($self, $x, $y, $w, $h) = @_; 989 my ($self, $w, $h) = @_;
896
897 $self->_size_allocate ($x, $y, $w, $h);
898 990
899 my $children = $self->{children}; 991 my $children = $self->{children};
900 992
901 my @h = map +($_->size_request)[1], @$children; 993 my @h = map +($_->size_request)[1], @$children;
902 994
920 1012
921 my $y = 0; 1013 my $y = 0;
922 for (0 .. $#$children) { 1014 for (0 .. $#$children) {
923 my $child = $children->[$_]; 1015 my $child = $children->[$_];
924 my $h = $h[$_]; 1016 my $h = $h[$_];
925 $child->size_allocate (0, $y, $w, $h); 1017 $child->configure (0, $y, $w, $h);
926 1018
927 $y += $h; 1019 $y += $h;
928 } 1020 }
1021
1022 1
929} 1023}
930 1024
931############################################################################# 1025#############################################################################
932 1026
933package CFClient::UI::Label; 1027package CFClient::UI::Label;
934 1028
935our @ISA = CFClient::UI::Base::; 1029our @ISA = CFClient::UI::Base::;
936 1030
937use SDL::OpenGL; 1031use CFClient::OpenGL;
938 1032
939sub new { 1033sub new {
940 my ($class, %arg) = @_; 1034 my ($class, %arg) = @_;
941 1035
942 my $self = $class->SUPER::new ( 1036 my $self = $class->SUPER::new (
943 fg => [1, 1, 1], 1037 fg => [1, 1, 1],
944 fontsize => $::FONTSIZE, 1038 fontsize => 1,
945 text => "", 1039 text => "",
946 align => -1, 1040 align => -1,
1041 valign => -1,
947 padding => 2, 1042 padding => 2,
948 layout => new CFClient::Layout, 1043 layout => new CFClient::Layout,
949 %arg 1044 %arg
950 ); 1045 );
951 1046
952 $self->set_text ($self->{text}); 1047 if (exists $self->{template}) {
1048 my $layout = new CFClient::Layout;
1049 $layout->set_text (delete $self->{template});
1050 $self->{template} = $layout;
1051 }
1052
1053 $self->set_text (delete $self->{text}) if exists $self->{text};
1054 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
953 1055
954 $self 1056 $self
955} 1057}
956 1058
957sub escape_text { 1059sub escape_text {
965} 1067}
966 1068
967sub set_text { 1069sub set_text {
968 my ($self, $text) = @_; 1070 my ($self, $text) = @_;
969 1071
970 $self->{text} = $text;
971 $self->{layout}->set_markup ($text); 1072 $self->{layout}->set_text ($text);
972 1073
973 delete $self->{texture}; 1074 delete $self->{texture};
974# $self->{w} = $self->{h} = -1;
975 $self->update; 1075 $self->update;
976} 1076}
977 1077
978sub get_text { 1078sub set_markup {
979 my ($self, $text) = @_; 1079 my ($self, $markup) = @_;
980 1080
981 $self->{text} 1081 $self->{layout}->set_markup ($markup);
1082
1083 delete $self->{texture};
1084 $self->update;
982} 1085}
983 1086
984sub size_request { 1087sub size_request {
985 my ($self) = @_; 1088 my ($self) = @_;
986 1089
987 $self->{layout}->set_width; 1090 $self->{layout}->set_width;
988 $self->{layout}->set_height ($self->{fontsize}); 1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1092
989 my ($w, $h) = $self->{layout}->size; 1093 my ($w, $h) = $self->{layout}->size;
1094
1095 if (exists $self->{template}) {
1096 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1097
1098 my ($w2, $h2) = $self->{template}->size;
1099
1100 $w = List::Util::max $w, $w2;
1101 $h = List::Util::max $h, $h2;
1102 }
990 1103
991 ( 1104 (
992 $w + $self->{padding} * 2, 1105 $w + $self->{padding} * 2,
993 $h + $self->{padding} * 2, 1106 $h + $self->{padding} * 2,
994 ) 1107 )
995} 1108}
996 1109
997sub size_allocate { 1110sub size_allocate {
998 my ($self, $x, $y, $w, $h) = @_; 1111 my ($self, $w, $h) = @_;
999
1000 $self->_size_allocate ($x, $y, $w, $h) or return;
1001 1112
1002 delete $self->{texture}; 1113 delete $self->{texture};
1003}
1004
1005sub update {
1006 my ($self) = @_;
1007
1008 delete $self->{texture};
1009 $self->SUPER::update;
1010} 1114}
1011 1115
1012sub _draw { 1116sub _draw {
1013 my ($self) = @_; 1117 my ($self) = @_;
1014 1118
1015 my $tex = $self->{texture} ||= do { 1119 my $tex = $self->{texture} ||= do {
1016 $self->{layout}->set_width ($self->{w}); 1120 $self->{layout}->set_width ($self->{w});
1017 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1121 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1018 new_from_layout CFClient::Texture $self->{layout} 1122 new_from_layout CFClient::Texture $self->{layout}
1019 }; 1123 };
1020 1124
1021 glEnable GL_BLEND; 1125 glEnable GL_BLEND;
1022 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1126 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1023 glEnable GL_TEXTURE_2D; 1127 glEnable GL_TEXTURE_2D;
1024 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1128 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1025 1129
1026 glColor @{$self->{fg}}; 1130 glColor @{$self->{fg}};
1027 1131
1028 my $x = 1132 $self->{ox} = int (
1029 $self->{align} < 0 ? $self->{padding} 1133 $self->{align} < 0 ? $self->{padding}
1030 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1134 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1031 : ($self->{w} - $tex->{w}) * 0.5; 1135 : ($self->{w} - $tex->{w}) * 0.5
1136 );
1032 1137
1033 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1138 $self->{oy} = int (
1139 $self->{valign} < 0 ? $self->{padding}
1140 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1141 : ($self->{h} - $tex->{h}) * 0.5
1142 );
1143
1144 $tex->draw_quad ($self->{ox}, $self->{oy});
1034 1145
1035 glDisable GL_TEXTURE_2D; 1146 glDisable GL_TEXTURE_2D;
1036 glDisable GL_BLEND; 1147 glDisable GL_BLEND;
1037} 1148}
1038 1149
1039############################################################################# 1150#############################################################################
1040 1151
1041package CFClient::UI::Entry; 1152package CFClient::UI::EntryBase;
1042 1153
1043our @ISA = CFClient::UI::Label::; 1154our @ISA = CFClient::UI::Label::;
1044 1155
1045use SDL; 1156use CFClient::OpenGL;
1046use SDL::OpenGL;
1047 1157
1048sub new { 1158sub new {
1049 my $class = shift; 1159 my $class = shift;
1050 1160
1051 $class->SUPER::new ( 1161 $class->SUPER::new (
1053 bg => [0, 0, 0, 0.2], 1163 bg => [0, 0, 0, 0.2],
1054 active_bg => [1, 1, 1, 0.5], 1164 active_bg => [1, 1, 1, 0.5],
1055 active_fg => [0, 0, 0], 1165 active_fg => [0, 0, 0],
1056 can_hover => 1, 1166 can_hover => 1,
1057 can_focus => 1, 1167 can_focus => 1,
1168 valign => 0,
1058 @_ 1169 @_
1059 ) 1170 )
1060} 1171}
1061 1172
1062sub _set_text { 1173sub _set_text {
1063 my ($self, $text) = @_; 1174 my ($self, $text) = @_;
1064 1175
1065 my $old_text = $self->{text}; 1176 delete $self->{cur_h};
1177
1178 return if $self->{text} eq $text;
1179
1180 delete $self->{texture};
1066 1181
1067 $self->{last_activity} = $::NOW; 1182 $self->{last_activity} = $::NOW;
1068
1069 $self->{text} = $text; 1183 $self->{text} = $text;
1070 $self->{layout}->set_width ($self->{w});
1071 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1072 1184
1073 $text =~ s/./*/g if $self->{hidden}; 1185 $text =~ s/./*/g if $self->{hidden};
1186 $self->{layout}->set_text ("$text ");
1074 1187
1075 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1188 $self->emit (changed => $self->{text});
1189}
1076 1190
1077 $text = substr $text, 0, $self->{cursor}; 1191sub get_text {
1078 utf8::encode $text; 1192 $_[0]{text}
1079
1080 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1081
1082 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1083 if $old_text ne $self->{text};
1084} 1193}
1085 1194
1086sub size_request { 1195sub size_request {
1087 my ($self) = @_; 1196 my ($self) = @_;
1088 1197
1090 1199
1091 ($w + 1, $h) # add 1 for cursor 1200 ($w + 1, $h) # add 1 for cursor
1092} 1201}
1093 1202
1094sub size_allocate { 1203sub size_allocate {
1095 my ($self, $x, $y, $w, $h) = @_; 1204 my ($self, $w, $h) = @_;
1096
1097 $self->SUPER::size_allocate ($x, $y, $w, $h);
1098 1205
1099 $self->_set_text ($self->{text}); 1206 $self->_set_text ($self->{text});
1100} 1207}
1101 1208
1102sub set_text { 1209sub set_text {
1108} 1215}
1109 1216
1110sub key_down { 1217sub key_down {
1111 my ($self, $ev) = @_; 1218 my ($self, $ev) = @_;
1112 1219
1113 my $mod = $ev->key_mod; 1220 my $mod = $ev->{mod};
1114 my $sym = $ev->key_sym; 1221 my $sym = $ev->{sym};
1115
1116 my $uni = $ev->key_unicode; 1222 my $uni = $ev->{unicode};
1117 1223
1118 my $text = $self->get_text; 1224 my $text = $self->get_text;
1119 1225
1120 if ($sym == SDLK_BACKSPACE) { 1226 if ($sym == 8) {
1121 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1227 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1122 } elsif ($sym == SDLK_DELETE) { 1228 } elsif ($sym == 127) {
1123 substr $text, $self->{cursor}, 1, ""; 1229 substr $text, $self->{cursor}, 1, "";
1124 } elsif ($sym == SDLK_LEFT) { 1230 } elsif ($sym == CFClient::SDLK_LEFT) {
1125 --$self->{cursor} if $self->{cursor}; 1231 --$self->{cursor} if $self->{cursor};
1126 } elsif ($sym == SDLK_RIGHT) { 1232 } elsif ($sym == CFClient::SDLK_RIGHT) {
1127 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1233 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1128 } elsif ($sym == SDLK_HOME) { 1234 } elsif ($sym == CFClient::SDLK_HOME) {
1129 $self->{cursor} = 0; 1235 $self->{cursor} = 0;
1130 } elsif ($sym == SDLK_END) { 1236 } elsif ($sym == CFClient::SDLK_END) {
1131 $self->{cursor} = length $text; 1237 $self->{cursor} = length $text;
1132 } elsif ($sym == SDLK_ESCAPE) { 1238 } elsif ($sym == 27) {
1133 $self->emit ('escape'); 1239 $self->emit ('escape');
1134 } elsif ($uni) { 1240 } elsif ($uni) {
1135 substr $text, $self->{cursor}++, 0, chr $uni; 1241 substr $text, $self->{cursor}++, 0, chr $uni;
1136 } 1242 }
1137 1243
1192 1298
1193 $self->SUPER::_draw; 1299 $self->SUPER::_draw;
1194 1300
1195 #TODO: force update every cursor change :( 1301 #TODO: force update every cursor change :(
1196 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1302 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1303
1304 unless (exists $self->{cur_h}) {
1305 my $text = substr $self->{text}, 0, $self->{cursor};
1306 utf8::encode $text;
1307
1308 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1309 }
1310
1197 glColor @{$self->{fg}}; 1311 glColor @{$self->{fg}};
1198 glBegin GL_LINES; 1312 glBegin GL_LINES;
1199 glVertex $self->{cur_x}, $self->{cur_y};
1200 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1313 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1314 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1201 glEnd; 1315 glEnd;
1202 } 1316 }
1203} 1317}
1204 1318
1205package CFClient::UI::LineEntry; 1319package CFClient::UI::Entry;
1206 1320
1207our @ISA = CFClient::UI::Entry::; 1321our @ISA = CFClient::UI::EntryBase::;
1208 1322
1209use SDL; 1323use CFClient::OpenGL;
1210use SDL::OpenGL;
1211 1324
1212sub key_down { 1325sub key_down {
1213 my ($self, $ev) = @_; 1326 my ($self, $ev) = @_;
1214 1327
1215 my $sym = $ev->key_sym; 1328 my $sym = $ev->{sym};
1216 1329
1217 if ($sym == SDLK_RETURN) { 1330 if ($sym == 13) {
1218 $self->emit (activate => $self->get_text); 1331 $self->emit (activate => $self->get_text);
1219 $self->update; 1332 $self->update;
1220 1333
1221 } else { 1334 } else {
1222 $self->SUPER::key_down ($ev); 1335 $self->SUPER::key_down ($ev);
1228 1341
1229package CFClient::UI::Button; 1342package CFClient::UI::Button;
1230 1343
1231our @ISA = CFClient::UI::Label::; 1344our @ISA = CFClient::UI::Label::;
1232 1345
1233use SDL; 1346use CFClient::OpenGL;
1234use SDL::OpenGL;
1235 1347
1236my @tex = 1348my @tex =
1237 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1238 qw(b1_button_active.png); 1350 qw(b1_button_active.png);
1239 1351
1242 1354
1243 $class->SUPER::new ( 1355 $class->SUPER::new (
1244 padding => 4, 1356 padding => 4,
1245 fg => [1, 1, 1], 1357 fg => [1, 1, 1],
1246 bg => [1, 1, 1, 0.2], 1358 bg => [1, 1, 1, 0.2],
1247 active_fg => [1, 1, 0], 1359 active_fg => [0, 0, 1],
1248 can_hover => 1, 1360 can_hover => 1,
1361 align => 0,
1362 valign => 0,
1249 @_ 1363 @_
1250 ) 1364 )
1251} 1365}
1252 1366
1253sub button_up { 1367sub button_up {
1261 1375
1262sub _draw { 1376sub _draw {
1263 my ($self) = @_; 1377 my ($self) = @_;
1264 1378
1265 local $self->{fg} = $self->{fg}; 1379 local $self->{fg} = $self->{fg};
1266 my $tex = $tex[0];
1267
1268 glEnable GL_BLEND;
1269 glEnable GL_TEXTURE_2D;
1270 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1271 1380
1272 if ($GRAB == $self) { 1381 if ($GRAB == $self) {
1273 $self->{fg} = $self->{active_fg}; 1382 $self->{fg} = $self->{active_fg};
1274 } 1383 }
1275 1384
1276 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1385 glEnable GL_BLEND;
1386 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1387 glEnable GL_TEXTURE_2D;
1277 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1388 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1389 glColor 0, 0, 0, 1;
1278 1390
1279 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1391 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1280 1392
1281 glDisable GL_TEXTURE_2D; 1393 glDisable GL_TEXTURE_2D;
1282 glDisable GL_BLEND; 1394 glDisable GL_BLEND;
1283 1395
1284 $self->SUPER::_draw; 1396 $self->SUPER::_draw;
1292 1404
1293my @tex = 1405my @tex =
1294 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1295 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1407 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1296 1408
1297use SDL; 1409use CFClient::OpenGL;
1298use SDL::OpenGL;
1299 1410
1300sub new { 1411sub new {
1301 my $class = shift; 1412 my $class = shift;
1302 1413
1303 $class->SUPER::new ( 1414 $class->SUPER::new (
1314 my ($self) = @_; 1425 my ($self) = @_;
1315 1426
1316 ($self->{padding} * 2 + 6) x 2 1427 ($self->{padding} * 2 + 6) x 2
1317} 1428}
1318 1429
1319sub size_allocate {
1320 my ($self, $x, $y, $w, $h) = @_;
1321
1322 $self->_size_allocate ($x, $y, $w, $h);
1323}
1324
1325sub button_down { 1430sub button_down {
1326 my ($self, $ev, $x, $y) = @_; 1431 my ($self, $ev, $x, $y) = @_;
1327 1432
1328 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1433 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1329 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1434 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1355 glDisable GL_BLEND; 1460 glDisable GL_BLEND;
1356} 1461}
1357 1462
1358############################################################################# 1463#############################################################################
1359 1464
1465package CFClient::UI::VGauge;
1466
1467our @ISA = CFClient::UI::Base::;
1468
1469use CFClient::OpenGL;
1470
1471my %tex = (
1472 food => [
1473 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1474 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1475 ],
1476 grace => [
1477 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1478 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1479 ],
1480 hp => [
1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1482 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1483 ],
1484 mana => [
1485 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1486 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1487 ],
1488);
1489
1490# eg. VGauge->new (gauge => 'food'), default gauge: food
1491sub new {
1492 my $class = shift;
1493
1494 my $self = $class->SUPER::new (
1495 type => 'food',
1496 @_
1497 );
1498
1499 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1500
1501 $self
1502}
1503
1504sub size_request {
1505 my ($self) = @_;
1506
1507 #my $tex = $tex{$self->{type}}[0];
1508 #@$tex{qw(w h)}
1509 (0, 0)
1510}
1511
1512sub set_max {
1513 my ($self, $max) = @_;
1514
1515 $self->{max_val} = $max;
1516}
1517
1518sub set_value {
1519 my ($self, $val, $max) = @_;
1520
1521 $self->set_max ($max)
1522 if defined $max;
1523
1524 $max = $self->{max_val};
1525 $self->{val} = $val;
1526
1527 $self->update;
1528}
1529
1530sub _draw {
1531 my ($self) = @_;
1532
1533 my $tex = $tex{$self->{type}};
1534
1535 my ($w, $h) = ($self->{w}, $self->{h});
1536
1537 if ($self->{vertical}) {
1538 glRotate 90, 0, 0, 1;
1539 glTranslate 0, -$self->{w}, 0;
1540
1541 ($w, $h) = ($h, $w);
1542 }
1543
1544 my $ycut = $self->{val} / ($self->{max_val} || 1);
1545 $ycut = 1 if $self->{val} > $self->{max_val};
1546
1547 my $t1 = $tex->[0];
1548 my $t2 = $tex->[1];
1549
1550 glEnable GL_BLEND;
1551 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1552 glEnable GL_TEXTURE_2D;
1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1554
1555 my $h1 = $self->{h} - $ycut * $self->{h};
1556 my $h2 = $ycut * $self->{h};
1557
1558 glBindTexture GL_TEXTURE_2D, $t1->{name};
1559 glBegin GL_QUADS;
1560 glTexCoord 0 , 0; glVertex 0 , 0;
1561 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1562 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1563 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1564 glEnd;
1565
1566 glBindTexture GL_TEXTURE_2D, $t2->{name};
1567 glBegin GL_QUADS;
1568 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1569 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1570 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1571 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1572 glEnd;
1573
1574 glDisable GL_BLEND;
1575 glDisable GL_TEXTURE_2D;
1576}
1577
1578#############################################################################
1579
1580package CFClient::UI::Gauge;
1581
1582our @ISA = CFClient::UI::VBox::;
1583
1584sub new {
1585 my ($class, %arg) = shift;
1586
1587 my $self = $class->SUPER::new (
1588 @_,
1589 );
1590
1591 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1592 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1593 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1594
1595 $self
1596}
1597
1598sub set_value {
1599 my ($self, $val, $max) = @_;
1600
1601 $self->set_max ($max)
1602 if defined $max;
1603
1604 $self->{gauge}->set_value ($val, $max);
1605 $self->{value}->set_text ($val);
1606}
1607
1608sub set_max {
1609 my ($self, $max) = @_;
1610
1611 $self->{gauge}->set_max ($max);
1612 $self->{max}->set_text ($max);
1613}
1614
1615#############################################################################
1616
1360package CFClient::UI::Slider; 1617package CFClient::UI::Slider;
1361 1618
1362use strict; 1619use strict;
1363 1620
1364use SDL::OpenGL; 1621use CFClient::OpenGL;
1365 1622
1366our @ISA = CFClient::UI::DrawBG::; 1623our @ISA = CFClient::UI::DrawBG::;
1367 1624
1368my @tex = 1625my @tex =
1369 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1379 # TODO: calculations are off 1636 # TODO: calculations are off
1380 my $self = $class->SUPER::new ( 1637 my $self = $class->SUPER::new (
1381 fg => [1, 1, 1], 1638 fg => [1, 1, 1],
1382 active_fg => [0, 0, 0], 1639 active_fg => [0, 0, 0],
1383 range => [0, 0, 100, 10], 1640 range => [0, 0, 100, 10],
1384 req_w => 40, 1641 req_w => 20,
1385 req_h => 13, 1642 req_h => 20,
1386 vertical => 0, 1643 vertical => 0,
1387 can_hover => 1, 1644 can_hover => 1,
1388 inner_pad => 5, 1645 inner_pad => 5,
1389 @_ 1646 @_
1390 ); 1647 );
1494 1751
1495package CFClient::UI::TextView; 1752package CFClient::UI::TextView;
1496 1753
1497our @ISA = CFClient::UI::HBox::; 1754our @ISA = CFClient::UI::HBox::;
1498 1755
1499use SDL::OpenGL; 1756use CFClient::OpenGL;
1500 1757
1501sub new { 1758sub new {
1502 my $class = shift; 1759 my $class = shift;
1503 1760
1504 my $self = $class->SUPER::new ( 1761 my $self = $class->SUPER::new (
1505 req_w => $::WIDTH / 6,
1506 req_h => $::HEIGHT / 6,
1507 fontsize => $::FONTSIZE, 1762 fontsize => 1,
1508 @_, 1763 @_,
1509 1764
1510 layout => (new CFClient::Layout), 1765 layout => (new CFClient::Layout),
1511 par => [], 1766 par => [],
1512 height => 0, 1767 height => 0,
1533sub text_height { 1788sub text_height {
1534 my ($self, $text) = @_; 1789 my ($self, $text) = @_;
1535 1790
1536 my $layout = $self->{layout}; 1791 my $layout = $self->{layout};
1537 1792
1538 $layout->set_height ($self->{fontsize}); 1793 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1539 $layout->set_width ($self->{w}); 1794 $layout->set_width ($self->{w});
1540 $layout->set_text ($text); 1795 $layout->set_text ($text);
1541 1796
1542 ($layout->size)[1] 1797 ($layout->size)[1]
1543} 1798}
1547 1802
1548 $self->{need_reflow}++; 1803 $self->{need_reflow}++;
1549 $self->update; 1804 $self->update;
1550} 1805}
1551 1806
1552sub size_request {
1553 my ($self) = @_;
1554
1555 ($self->{req_w}, $self->{req_h})
1556}
1557
1558sub size_allocate { 1807sub size_allocate {
1559 my ($self, $x, $y, $w, $h) = @_; 1808 my ($self, $w, $h) = @_;
1560 1809
1561 $self->SUPER::size_allocate ($x, $y, $w, $h); 1810 $self->SUPER::size_allocate ($w, $h);
1562 1811
1563 $self->{layout}->set_height ($self->{fontsize}); 1812 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1564 $self->{layout}->set_width ($self->{w}); 1813 $self->{layout}->set_width ($self->{children}[0]{w});
1565 1814
1566 $self->reflow; 1815 $self->reflow;
1567} 1816}
1568 1817
1569sub add_paragraph { 1818sub add_paragraph {
1656 1905
1657} 1906}
1658 1907
1659############################################################################# 1908#############################################################################
1660 1909
1661package CFClient::UI::MapWidget;
1662
1663use strict;
1664
1665use List::Util qw(min max);
1666
1667use SDL;
1668use SDL::OpenGL;
1669
1670our @ISA = CFClient::UI::Base::;
1671
1672sub new {
1673 my $class = shift;
1674
1675 $class->SUPER::new (
1676 z => -1,
1677 can_focus => 1,
1678 list => (glGenLists 1),
1679 @_
1680 )
1681}
1682
1683sub key_down {
1684 print "MAPKEYDOWN\n";
1685}
1686
1687sub key_up {
1688}
1689
1690sub button_down {
1691 my ($self, $ev, $x, $y) = @_;
1692
1693 $self->focus_in;
1694
1695 if ($ev->button == 2) {
1696 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1697 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1698
1699 $self->{motion} = sub {
1700 my ($ev, $x, $y) = @_;
1701
1702 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1703
1704 $::CFG->{map_shift_x} = $bw + $x - $ox;
1705 $::CFG->{map_shift_y} = $bh + $y - $oy;
1706
1707 $self->update;
1708 };
1709 }
1710}
1711
1712sub button_up {
1713 my ($self, $ev, $x, $y) = @_;
1714
1715 delete $self->{motion};
1716}
1717
1718sub mouse_motion {
1719 my ($self, $ev, $x, $y) = @_;
1720
1721 $self->{motion}->($ev, $x, $y) if $self->{motion};
1722}
1723
1724sub size_request {
1725 (
1726 1 + 32 * int $::WIDTH / 32,
1727 1 + 32 * int $::HEIGHT / 32,
1728 )
1729}
1730
1731sub update {
1732 my ($self) = @_;
1733
1734 $self->{need_update} = 1;
1735 $self->SUPER::update;
1736}
1737
1738sub draw {
1739 my ($self) = @_;
1740
1741 if (delete $self->{need_update}) {
1742 glNewList $self->{list}, GL_COMPILE;
1743
1744 if ($::MAP) {
1745 my $sw = int $::WIDTH / 32;
1746 my $sh = int $::HEIGHT / 32;
1747
1748 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1749 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1750
1751 glTranslate $sx0 - 32, $sy0 - 32, 0;
1752
1753 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1754
1755 if ($::CFG->{fow_enable}) {
1756 if ($::CFG->{fow_smooth}) { # smooth fog of war
1757 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1758 glConvolutionFilter2D
1759 GL_CONVOLUTION_2D,
1760 GL_ALPHA,
1761 3, 3,
1762 GL_ALPHA, GL_FLOAT,
1763 pack "f*",
1764 0.1, 0.1, 0.1,
1765 0.1, 0.2, 0.1,
1766 0.1, 0.1, 0.1,
1767 ;
1768 glEnable GL_CONVOLUTION_2D;
1769 }
1770
1771 my $tex = new CFClient::Texture
1772 w => $w,
1773 h => $h,
1774 data => $data,
1775 internalformat => GL_ALPHA,
1776 format => GL_ALPHA;
1777
1778 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1779
1780 glEnable GL_BLEND;
1781 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1782 glEnable GL_TEXTURE_2D;
1783 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1784
1785 glColor +($::CFG->{fow_intensity}) x 3, 1;
1786 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1787
1788 glDisable GL_TEXTURE_2D;
1789 glDisable GL_BLEND;
1790 }
1791
1792 # HACK BEGIN
1793 {
1794 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1795 my ($w, $h) = (250, 250);
1796
1797 glEnable GL_BLEND;
1798 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1799 glEnable GL_TEXTURE_2D;
1800 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1801
1802 CFClient::Texture->new (
1803 w => $w,
1804 h => $h,
1805 data => $::MAP->mapmap ($w, $h),
1806 type => GL_UNSIGNED_INT_8_8_8_8_REV
1807 )->draw_quad (100, 100);
1808
1809 glDisable GL_TEXTURE_2D;
1810 glDisable GL_BLEND;
1811 }
1812 # HACK END
1813 }
1814
1815 glEndList;
1816 }
1817
1818 glPushMatrix;
1819 glCallList $self->{list};
1820 glPopMatrix;
1821
1822 if ($FOCUS != $self) {
1823 glColor 64/255, 64/255, 64/255;
1824 glLogicOp GL_AND;
1825 glEnable GL_COLOR_LOGIC_OP;
1826 glBegin GL_QUADS;
1827 glVertex 0, 0;
1828 glVertex 0, $::HEIGHT;
1829 glVertex $::WIDTH, $::HEIGHT;
1830 glVertex $::WIDTH, 0;
1831 glEnd;
1832 glDisable GL_COLOR_LOGIC_OP;
1833 }
1834}
1835
1836my %DIR = (
1837 SDLK_KP8, [1, "north"],
1838 SDLK_KP9, [2, "northeast"],
1839 SDLK_KP6, [3, "east"],
1840 SDLK_KP3, [4, "southeast"],
1841 SDLK_KP2, [5, "south"],
1842 SDLK_KP1, [6, "southwest"],
1843 SDLK_KP4, [7, "west"],
1844 SDLK_KP7, [8, "northwest"],
1845
1846 SDLK_UP, [1, "north"],
1847 SDLK_RIGHT, [3, "east"],
1848 SDLK_DOWN, [5, "south"],
1849 SDLK_LEFT, [7, "west"],
1850);
1851
1852sub key_down {
1853 my ($self, $ev) = @_;
1854
1855 my $mod = $ev->key_mod;
1856 my $sym = $ev->key_sym;
1857
1858 if ($sym == SDLK_KP5) {
1859 $::CONN->user_send ("command stay fire");
1860 } elsif ($sym == SDLK_a) {
1861 $::CONN->user_send ("command apply");
1862 } elsif ($sym == SDLK_QUOTE) {
1863 $self->emit ('activate_console');
1864 } elsif ($sym == SDLK_SLASH) {
1865 $self->emit ('activate_console' => '/');
1866 } elsif (exists $DIR{$sym}) {
1867 if ($mod & KMOD_SHIFT) {
1868 $self->{shft}++;
1869 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1870 } elsif ($mod & KMOD_CTRL) {
1871 $self->{ctrl}++;
1872 $::CONN->user_send ("command run $DIR{$sym}[0]");
1873 } else {
1874 $::CONN->user_send ("command $DIR{$sym}[1]");
1875 }
1876 }
1877}
1878
1879sub key_up {
1880 my ($self, $ev) = @_;
1881
1882 my $mod = $ev->key_mod;
1883 my $sym = $ev->key_sym;
1884
1885 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1886 $::CONN->user_send ("command fire_stop");
1887 }
1888 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1889 $::CONN->user_send ("command run_stop");
1890 }
1891}
1892
1893#############################################################################
1894
1895package CFClient::UI::Animator; 1910package CFClient::UI::Animator;
1896 1911
1897use SDL::OpenGL; 1912use CFClient::OpenGL;
1898 1913
1899our @ISA = CFClient::UI::Bin::; 1914our @ISA = CFClient::UI::Bin::;
1900 1915
1901sub moveto { 1916sub moveto {
1902 my ($self, $x, $y) = @_; 1917 my ($self, $x, $y) = @_;
1960 1975
1961 # TODO: use animation 1976 # TODO: use animation
1962 if ($self->{state} = !$self->{state}) { 1977 if ($self->{state} = !$self->{state}) {
1963 $CFClient::UI::ROOT->add ($self->{other}); 1978 $CFClient::UI::ROOT->add ($self->{other});
1964 $self->{other}->move ($self->coord2global (0, $self->{h})); 1979 $self->{other}->move ($self->coord2global (0, $self->{h}));
1980 $self->emit ("open");
1965 } else { 1981 } else {
1966 $CFClient::UI::ROOT->remove ($self->{other}); 1982 $CFClient::UI::ROOT->remove ($self->{other});
1983 $self->emit ("close");
1967 } 1984 }
1985
1986 $self->emit (changed => $self->{state});
1968} 1987}
1969 1988
1970############################################################################# 1989#############################################################################
1971 1990
1972package CFClient::UI::Root; 1991package CFClient::UI::Root;
1973 1992
1974our @ISA = CFClient::UI::Container::; 1993our @ISA = CFClient::UI::Container::;
1975 1994
1976use SDL::OpenGL; 1995use CFClient::OpenGL;
1996
1997sub check_size {
1998 my ($self) = @_;
1999
2000 $self->configure (0, 0, $::WITH, $::HEIGHT);
2001}
1977 2002
1978sub size_request { 2003sub size_request {
1979 ($::WIDTH, $::HEIGHT) 2004 ($::WIDTH, $::HEIGHT)
1980} 2005}
1981 2006
1982sub size_allocate { 2007sub configure {
1983 my ($self, $x, $y, $w, $h) = @_; 2008 my ($self, $x, $y, $w, $h) = @_;
1984 2009
1985 $self->_size_allocate ($x, $y, $w, $h); 2010 $self->SUPER::configure ($x, $y, $w, $h);
1986 2011
1987 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2012 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1988 for @{$self->{children}}; 2013 for @{$self->{children}};
1989} 2014}
1990 2015
1991sub _topleft { 2016sub _topleft {
1992 my ($self, $x, $y) = @_; 2017 my ($self, $x, $y) = @_;
1995} 2020}
1996 2021
1997sub update { 2022sub update {
1998 my ($self) = @_; 2023 my ($self) = @_;
1999 2024
2000 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2025 $self->check_size;
2001 ::refresh (); 2026 ::refresh ();
2002} 2027}
2003 2028
2004sub add { 2029sub add {
2005 my ($self, $widget) = @_; 2030 my ($self, $child) = @_;
2006 2031
2007 $self->SUPER::add ($widget); 2032 $self->SUPER::add ($child);
2008
2009 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2010} 2033}
2011 2034
2012sub on_refresh { 2035sub on_refresh {
2013 my ($self, $id, $cb) = @_; 2036 my ($self, $id, $cb) = @_;
2014 2037

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines