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.74 by root, Tue Apr 11 19:31:18 2006 UTC vs.
Revision 1.112 by root, Sat Apr 15 12:29:46 2006 UTC

43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $TOPLEVEL->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_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->translate ($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;
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $TOPLEVEL->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; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $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->translate ($x, $y)) if $HOVER;
71}
72
73# convert position array to integers
74sub harmonize {
75 my ($vals) = @_;
76
77 my $rem = 0;
78
79 for ($vals) {
80 my $i = int $_ + $rem;
81 $rem += $_ - $i;
82 $_ = $i;
83 }
71} 84}
72 85
73############################################################################# 86#############################################################################
74 87
75package CFClient::UI::Base; 88package CFClient::UI::Base;
79use SDL::OpenGL; 92use SDL::OpenGL;
80 93
81sub new { 94sub new {
82 my $class = shift; 95 my $class = shift;
83 96
84 bless { 97 my $self = bless {
85 x => 0, 98 x => 0,
86 y => 0, 99 y => 0,
87 z => 0, 100 z => 0,
101 w => -1,
102 h => -1,
88 @_ 103 @_
89 }, $class 104 }, $class;
105
106 for (keys %$self) {
107 if (/^connect_(.*)$/) {
108 $self->connect ($1 => delete $self->{$_});
109 }
110 }
111
112 $self
90} 113}
91 114
92sub move { 115sub move {
93 my ($self, $x, $y, $z) = @_; 116 my ($self, $x, $y, $z) = @_;
94 $self->{x} = $x; 117 $self->{x} = $x;
103sub size_request { 126sub size_request {
104 require Carp; 127 require Carp;
105 Carp::confess "size_request is abtract"; 128 Carp::confess "size_request is abtract";
106} 129}
107 130
108sub size_allocate { 131sub _size_allocate {
109 my ($self, $w, $h) = @_; 132 my ($self, $x, $y, $w, $h) = @_;
133
134 $self->{x} = $x;
135 $self->{y} = $y;
136
137 return unless $self->{w} != $w || $self->{h} != $h;
110 138
111 $self->{w} = $w; 139 $self->{w} = $w;
112 $self->{h} = $h; 140 $self->{h} = $h;
113}
114 141
142 1
143}
144
145sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_;
147
148 $self->_size_allocate ($x, $y, $w, $h);
149}
150
115# translate global koordinates to local coordinate system 151# translate global coordinates to local coordinate system
116sub translate { 152sub translate {
117 my ($self, $x, $y) = @_; 153 my ($self, $x, $y) = @_;
118 154
119 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 155 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y});
120} 156}
121 157
122sub focus_in { 158sub focus_in {
123 my ($self) = @_; 159 my ($self) = @_;
124 160
125 return if $FOCUS == $self; 161 return if $FOCUS == $self;
162 return unless $self->{can_focus};
126 163
127 my $focus = $FOCUS; $FOCUS = $self; 164 my $focus = $FOCUS; $FOCUS = $self;
128 $focus->update if $focus; 165 $focus->update if $focus;
129 $FOCUS->update; 166 $FOCUS->update;
130} 167}
163 glPushMatrix; 200 glPushMatrix;
164 glTranslate $self->{x}, $self->{y}, 0; 201 glTranslate $self->{x}, $self->{y}, 0;
165 $self->_draw; 202 $self->_draw;
166 glPopMatrix; 203 glPopMatrix;
167 204
168 if ($self == $HOVER) { 205 if ($self == $HOVER && $self->{can_hover}) {
169 my ($x, $y) = @$self{qw(x y)}; 206 my ($x, $y) = @$self{qw(x y)};
170 207
171 glColor 1, 1, 1, 0.1; 208 glColor 0, 0, 1, 0.2;
172 glEnable GL_BLEND; 209 glEnable GL_BLEND;
173 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
174 glBegin GL_QUADS; 211 glBegin GL_QUADS;
175 glVertex $x , $y; 212 glVertex $x , $y;
176 glVertex $x + $self->{w}, $y; 213 glVertex $x + $self->{w}, $y;
185 my ($self) = @_; 222 my ($self) = @_;
186 223
187 warn "no draw defined for $self\n"; 224 warn "no draw defined for $self\n";
188} 225}
189 226
190sub bbox {
191 my ($self) = @_;
192 my ($w, $h) = $self->size_request;
193 (
194 $self->{x},
195 $self->{y},
196 $self->{x} = $w,
197 $self->{y} = $h
198 )
199}
200
201sub find_widget { 227sub find_widget {
202 my ($self, $x, $y) = @_; 228 my ($self, $x, $y) = @_;
203 229
204 return $self 230 return $self
205 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 231 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
206 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 232 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
207 233
208 () 234 ()
209} 235}
210 236
211sub del_parent { $_[0]->{parent} = undef }
212
213sub set_parent { 237sub set_parent {
214 my ($self, $par) = @_; 238 my ($self, $par) = @_;
215 239
216 $self->{parent} = $par; 240 $self->{parent} = $par;
217 Scalar::Util::weaken $self->{parent}; 241 Scalar::Util::weaken $self->{parent};
260 my $class = shift; 284 my $class = shift;
261 285
262 # range [value, low, high, page] 286 # range [value, low, high, page]
263 287
264 $class->SUPER::new ( 288 $class->SUPER::new (
265 bg => [0, 0, 0, 0.4], 289 bg => [0, 0, 0, 0.2],
266 active_bg => [1, 1, 1], 290 active_bg => [1, 1, 1, 0.5],
267 @_ 291 @_
268 ) 292 )
269} 293}
270 294
271sub _draw { 295sub _draw {
272 my ($self) = @_; 296 my ($self) = @_;
273 297
274 my ($w, $h) = @$self{qw(w h)}; 298 my ($w, $h) = @$self{qw(w h)};
275 299
300 glEnable GL_BLEND;
301 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
276 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} }; 302 glColor @{ $FOCUS == $self ? $self->{active_bg} : $self->{bg} };
303
277 glBegin GL_QUADS; 304 glBegin GL_QUADS;
278 glVertex 0 , 0; 305 glVertex 0 , 0;
279 glVertex 0 , $h; 306 glVertex 0 , $h;
280 glVertex $w, $h; 307 glVertex $w, $h;
281 glVertex $w, 0; 308 glVertex $w, 0;
282 glEnd; 309 glEnd;
310
311 glDisable GL_BLEND;
283} 312}
284 313
285############################################################################# 314#############################################################################
286 315
287package CFClient::UI::Empty; 316package CFClient::UI::Empty;
310 339
311 $self 340 $self
312} 341}
313 342
314sub add { 343sub add {
315 my ($self, $chld, $expand) = @_; 344 my ($self, $chld) = @_;
316 345
317 $chld->{expand} = $expand;
318 $chld->set_parent ($self); 346 $chld->set_parent ($self);
319 347
320 $self->{children} = [ 348 $self->{children} = [
321 sort { $a->{z} <=> $b->{z} } 349 sort { $a->{z} <=> $b->{z} }
322 @{$self->{children}}, $chld 350 @{$self->{children}}, $chld
323 ]; 351 ];
324 352
325 $self->size_allocate ($self->{w}, $self->{h}) 353 $self->{w} = $self->{h} = -1;
326 if $self->{w}; #TODO: check for "realised state" 354 $self->update;
327} 355}
328 356
329sub remove { 357sub remove {
330 my ($self, $widget) = @_; 358 my ($self, $widget) = @_;
331 359
332 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
333 361
334 $self->size_allocate ($self->{w}, $self->{h}); 362 $self->size_allocate (0, 0, $self->{w}, $self->{h});
335} 363}
336 364
337sub find_widget { 365sub find_widget {
338 my ($self, $x, $y) = @_; 366 my ($self, $x, $y) = @_;
339 367
392sub size_request { 420sub size_request {
393 $_[0]{children}[0]->size_request 421 $_[0]{children}[0]->size_request
394} 422}
395 423
396sub size_allocate { 424sub size_allocate {
397 my ($self, $w, $h) = @_; 425 my ($self, $x, $y, $w, $h) = @_;
398 426
399 return unless $self->{w} != $w || $self->{h} != $h; 427 $self->_size_allocate ($x, $y, $w, $h) or return;
400 428
401 $self->SUPER::size_allocate ($w, $h);
402 $self->{children}[0]->size_allocate ($w, $h); 429 $self->{children}[0]->size_allocate (0, 0, $w, $h);
403} 430}
404 431
405############################################################################# 432#############################################################################
406 433
407package CFClient::UI::Window; 434package CFClient::UI::Window;
425} 452}
426 453
427sub render_chld { 454sub render_chld {
428 my ($self) = @_; 455 my ($self) = @_;
429 456
430 $self->{texture} = 457 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
431 CFClient::Texture->new_from_opengl ( 458 glClearColor 0, 0, 0, 1;
432 $self->{w}, $self->{h}, sub { $self->child->draw } 459 glClear GL_COLOR_BUFFER_BIT;
433 ); 460 $self->child->draw;
461 };
434} 462}
435 463
436sub size_allocate { 464sub size_allocate {
437 my ($self, $w, $h) = @_; 465 my ($self, $x, $y, $w, $h) = @_;
438 466
439 return unless $self->{w} != $w || $self->{h} != $h; 467 $self->_size_allocate ($x, $y, $w, $h) or return;
440 468
441 $self->{w} = $w;
442 $self->{h} = $h;
443
444 $self->child->size_allocate ($w, $h); 469 $self->child->size_allocate (0, 0, $w, $h);
445 470
446 $self->render_chld; 471 $self->render_chld;
447} 472}
448 473
449sub _draw { 474sub _draw {
482 507
483 map { $_ + 4 } $chld->size_request; 508 map { $_ + 4 } $chld->size_request;
484} 509}
485 510
486sub size_allocate { 511sub size_allocate {
487 my ($self, $w, $h) = @_; 512 my ($self, $x, $y, $w, $h) = @_;
488 513
489 return unless $self->{w} != $w || $self->{h} != $h; 514 $self->_size_allocate ($x, $y, $w, $h) or return;
490 515
491 $self->{w} = $w;
492 $self->{h} = $h;
493
494 $self->child->size_allocate ($w - 4, $h - 4); 516 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
495 $self->child->move (2, 2);
496} 517}
497 518
498sub _draw { 519sub _draw {
499 my ($self) = @_; 520 my ($self) = @_;
500 521
523 544
524my @tex = 545my @tex =
525 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
526 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 547 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
527 548
549sub new {
550 my $class = shift;
551
552 # TODO: user_x, user_y, overwrite moveto?
553
554 $class->SUPER::new (
555 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8,
558 @_
559 )
560}
561
528sub size_request { 562sub size_request {
529 my ($self) = @_; 563 my ($self) = @_;
530 564
565 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
566
531 my ($w, $h) = $self->SUPER::size_request; 567 my ($w, $h) = $self->SUPER::size_request;
532 568
533 $h += $tex[1]->{h}; 569 (
534 $h += $tex[4]->{h}; 570 $w + $self->{border} * 2,
535 $w += $tex[2]->{w}; 571 $h + $self->{border} * 2,
536 $w += $tex[3]->{w}; 572 )
537
538 ($w, $h)
539} 573}
540 574
541sub size_allocate { 575sub size_allocate {
542 my ($self, $w, $h) = @_; 576 my ($self, $x, $y, $w, $h) = @_;
543 577
544 return unless $self->{w} != $w || $self->{h} != $h; 578 $self->_size_allocate ($x, $y, $w, $h) or return;
545 579
546 $self->SUPER::size_allocate ($w, $h); 580 $h -= List::Util::max 0, $self->{border} * 2;
581 $w -= List::Util::max 0, $self->{border} * 2;
547 582
548 $h -= $tex[1]->{h}; 583 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h);
549 $h -= $tex[4]->{h}; 584}
550 $w -= $tex[2]->{w};
551 $w -= $tex[3]->{w};
552 585
553 $h = $h < 0 ? 0 : $h; 586sub button_down {
554 $w = $w < 0 ? 0 : $w; 587 my ($self, $ev, $x, $y) = @_;
555 588
556 my $child = $self->child; 589 if ($x < $self->{w} && $x >= $self->{w} - $self->{border}
590 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) {
557 591
558 $child->size_allocate ($w, $h); 592 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
559 $child->move ($tex[3]->{w}, $tex[1]->{h}); 593 my ($bw, $bh) = ($self->{w}, $self->{h});
594
595 $self->{motion} = sub {
596 my ($ev, $x, $y) = @_;
597
598 ($x, $y) = ($ev->motion_x, $ev->motion_y);
599
600 $self->{user_w} = $bw + $x - $ox;
601 $self->{user_h} = $bh + $y - $oy;
602 $self->update;
603 };
604
605 } elsif ($x >= 0 && $x < $self->{w}
606 && $y >= 0 && $y < $self->{border}) {
607
608 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
609 my ($bx, $by) = ($self->{x}, $self->{y});
610
611 $self->{motion} = sub {
612 my ($ev, $x, $y) = @_;
613
614 ($x, $y) = ($ev->motion_x, $ev->motion_y);
615
616 $self->move ($bx + $x - $ox, $by + $y - $oy);
617 $self->update;
618 };
619 }
620}
621
622sub button_up {
623 my ($self, $ev, $x, $y) = @_;
624
625 delete $self->{motion};
626}
627
628sub mouse_motion {
629 my ($self, $ev, $x, $y) = @_;
630
631 $self->{motion}->($ev, $x, $y) if $self->{motion};
560} 632}
561 633
562sub _draw { 634sub _draw {
563 my ($self) = @_; 635 my ($self) = @_;
564 636
565 my ($w, $h) = ($self->{w}, $self->{h}); 637 my ($w, $h ) = ($self->{w}, $self->{h});
566 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 638 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
639
640 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
644
645 glColor @{ $self->{border_bg} };
646 $tex[1]->draw_quad (0, 0, $w, $self->{border});
647 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch);
648 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch);
649 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border});
650
651 my $bg = $tex[0];
652
653 # TODO: repeat texture not scale
654 my $rep_x = $cw / $bg->{w};
655 my $rep_y = $ch / $bg->{h};
656
657 glColor @{ $self->{bg} };
658
659 $bg->{s} = $rep_x;
660 $bg->{t} = $rep_y;
661 $bg->{wrap_mode} = 1;
662 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch);
663
664 glDisable GL_TEXTURE_2D;
665 glDisable GL_BLEND;
666
667 $self->child->draw;
668}
669
670#############################################################################
671
672package CFClient::UI::Table;
673
674our @ISA = CFClient::UI::Base::;
675
676use List::Util qw(max sum);
677
678use SDL::OpenGL;
679
680sub new {
681 my $class = shift;
682
683 $class->SUPER::new (
684 col_expand => [],
685 @_
686 )
687}
688
689sub add {
690 my ($self, $x, $y, $chld) = @_;
691
692 $self->{children}[$y][$x] = $chld;
693 $chld->set_parent ($self);
694
695 $self->{w} = $self->{h} = -1;
696 $self->update;
697}
698
699sub get_wh {
700 my ($self) = @_;
701
702 my (@w, @h);
703
704 for my $y (0 .. $#{$self->{children}}) {
705 my $row = $self->{children}[$y]
706 or next;
707
708 for my $x (0 .. $#$row) {
709 my $widget = $row->[$x]
710 or next;
711 my ($w, $h) = $widget->size_request;
712
713 $w[$x] = max $w[$x], $w;
714 $h[$y] = max $h[$y], $h;
715 }
716 }
717
718 (\@w, \@h)
719}
720
721sub size_request {
722 my ($self) = @_;
723
724 my ($ws, $hs) = $self->get_wh;
725
726 (
727 (sum @$ws),
728 (sum @$hs),
729 )
730}
731
732sub size_allocate {
733 my ($self, $x, $y, $w, $h) = @_;
734
735 $self->_size_allocate ($x, $y, $w, $h) or return;
736
737 my ($ws, $hs) = $self->get_wh;
738
739 my $req_w = sum @$ws;
740 my $req_h = sum @$hs;
741
742 # TODO: nicer code && do row_expand
743 my @col_expand = @{$self->{col_expand}};
744 @col_expand = (1) x @$ws unless @col_expand;
745 my $col_expand = (sum @col_expand) || 1;
746
747 # linearly scale sizes
748 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
749 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
750
751 CFClient::UI::harmonize $ws;
752 CFClient::UI::harmonize $hs;
753
754 my $y;
755
756 for my $r (0 .. $#{$self->{children}}) {
757 my $row = $self->{children}[$r]
758 or next;
759
760 my $x = 0;
761 my $row_h = $hs->[$r];
762
763 for my $c (0 .. $#$row) {
764 my $col_w = $ws->[$c];
765
766 if (my $widget = $row->[$c]) {
767 $widget->size_allocate ($x, $y, $col_w, $row_h);
768 }
769
770 $x += $col_w;
771 }
772
773 $y += $row_h;
774 }
775
776}
777
778sub find_widget {
779 my ($self, $x, $y) = @_;
780
781 $x -= $self->{x};
782 $y -= $self->{y};
783
784 my $res;
785
786 for (grep $_, map @$_, grep $_, @{ $self->{children} }) {
787 $res = $_->find_widget ($x, $y)
788 and return $res;
789 }
790
791 $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y})
792}
793
794sub _draw {
795 my ($self) = @_;
796
797 for (grep $_, @{$self->{children}}) {
798 $_->draw for grep $_, @$_;
799 }
800}
801
802#############################################################################
803
804package CFClient::UI::HBox;
805
806# TODO: wrap into common Box base class
807
808our @ISA = CFClient::UI::Container::;
809
810sub size_request {
811 my ($self) = @_;
812
813 my @alloc = map [$_->size_request], @{$self->{children}};
814
815 (
816 (List::Util::sum map $_->[0], @alloc),
817 (List::Util::max map $_->[1], @alloc),
818 )
819}
820
821sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_;
823
824 $self->_size_allocate ($x, $y, $w, $h);
825
826 ($h, $w) = ($w, $h);
827
828 my $children = $self->{children};
829
830 my @h = map +($_->size_request)[0], @$children;
831
832 my $req_h = List::Util::sum @h;
833
834 if ($req_h > $h) {
835 # ah well, not enough space
836 $_ *= $h / $req_h for @h;
837 } else {
838 my $exp = List::Util::sum map $_->{expand}, @$children;
839 $exp ||= 1;
840
841 for (0 .. $#$children) {
842 my $child = $children->[$_];
843
844 my $alloc_h = $h[$_];
845 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
846 $h[$_] = $alloc_h;
847 }
848 }
849
850 CFClient::UI::harmonize \@h;
851
852 my $y = 0;
853 for (0 .. $#$children) {
854 my $child = $children->[$_];
855 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w);
857
858 $y += $h;
859 }
860}
861
862#############################################################################
863
864package CFClient::UI::VBox;
865
866# TODO: wrap into common Box base class
867
868our @ISA = CFClient::UI::Container::;
869
870sub size_request {
871 my ($self) = @_;
872
873 my @alloc = map [$_->size_request], @{$self->{children}};
874
875 (
876 (List::Util::max map $_->[0], @alloc),
877 (List::Util::sum map $_->[1], @alloc),
878 )
879}
880
881sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_;
883
884 $self->_size_allocate ($x, $y, $w, $h);
885
886 my $children = $self->{children};
887
888 my @h = map +($_->size_request)[1], @$children;
889
890 my $req_h = List::Util::sum @h;
891
892 if ($req_h > $h) {
893 # ah well, not enough space
894 $_ *= $h / $req_h for @h;
895 } else {
896 my $exp = List::Util::sum map $_->{expand}, @$children;
897 $exp ||= 1;
898
899 for (0 .. $#$children) {
900 my $child = $children->[$_];
901
902 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
903 }
904 }
905
906 CFClient::UI::harmonize \@h;
907
908 my $y = 0;
909 for (0 .. $#$children) {
910 my $child = $children->[$_];
911 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h);
913
914 $y += $h;
915 }
916}
917
918#############################################################################
919
920package CFClient::UI::Label;
921
922our @ISA = CFClient::UI::Base::;
923
924use SDL::OpenGL;
925
926sub new {
927 my ($class, %arg) = @_;
928
929 my $self = $class->SUPER::new (
930 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE,
932 text => "",
933 align => -1,
934 padding => 2,
935 layout => new CFClient::Layout,
936 %arg
937 );
938
939 $self->set_text ($self->{text});
940
941 $self
942}
943
944sub escape_text {
945 local $_ = $_[1];
946
947 s/&/&amp;/g;
948 s/>/&gt;/g;
949 s/</&lt;/g;
950
951 $_[1]
952}
953
954sub set_text {
955 my ($self, $text) = @_;
956
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text);
959
960 delete $self->{texture};
961# $self->{w} = $self->{h} = -1;
962 $self->update;
963}
964
965sub get_text {
966 my ($self, $text) = @_;
967
968 $self->{text}
969}
970
971sub size_request {
972 my ($self) = @_;
973
974 $self->{layout}->set_width;
975 $self->{layout}->set_height ($self->{fontsize});
976 my ($w, $h) = $self->{layout}->size;
977
978 (
979 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2,
981 )
982}
983
984sub size_allocate {
985 my ($self, $x, $y, $w, $h) = @_;
986
987 $self->_size_allocate ($x, $y, $w, $h) or return;
988
989 delete $self->{texture};
990}
991
992sub update {
993 my ($self) = @_;
994
995 delete $self->{texture};
996 $self->SUPER::update;
997}
998
999sub _draw {
1000 my ($self) = @_;
1001
1002 my $tex = $self->{texture} ||= do {
1003 $self->{layout}->set_width ($self->{w});
1004 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1005 new_from_layout CFClient::Texture $self->{layout}
1006 };
567 1007
568 glEnable GL_BLEND; 1008 glEnable GL_BLEND;
569 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
570 glEnable GL_TEXTURE_2D; 1010 glEnable GL_TEXTURE_2D;
571 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
572 1012
573 my $top = $tex[1];
574 $top->draw_quad (0, 0, $w, $top->{h});
575
576 my $left = $tex[3];
577 $left->draw_quad (0, $top->{h}, $left->{w}, $ch);
578
579 my $right = $tex[2];
580 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
581
582 my $bottom = $tex[4];
583 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
584
585 my $bg = $tex[0];
586 glBindTexture GL_TEXTURE_2D, $bg->{name};
587 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
588 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
589 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
590
591 my $rep_x = $cw / $bg->{w};
592 my $rep_y = $ch / $bg->{h};
593
594 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch);
595
596 glDisable GL_BLEND;
597 glDisable GL_TEXTURE_2D;
598
599 $self->child->draw;
600
601}
602
603#############################################################################
604
605package CFClient::UI::Table;
606
607our @ISA = CFClient::UI::Base::;
608
609use SDL::OpenGL;
610
611sub add {
612 my ($self, $x, $y, $chld) = @_;
613 my $old_chld = $self->{children}[$y][$x];
614
615 $self->{children}[$y][$x] = $chld;
616 $chld->set_parent ($self);
617 $self->update;
618}
619
620sub max_row_height {
621 my ($self, $row) = @_;
622
623 my $hs = 0;
624 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) {
625 my $c = $self->{children}->[$row]->[$xi];
626 if ($c) {
627 my ($w, $h) = $c->size_request;
628 if ($hs < $h) { $hs = $h }
629 }
630 }
631 return $hs;
632}
633
634sub max_col_width {
635 my ($self, $col) = @_;
636
637 my $ws = 0;
638 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) {
639 my $c = ($self->{children}->[$yi] || [])->[$col];
640 if ($c) {
641 my ($w, $h) = $c->size_request;
642 if ($ws < $w) { $ws = $w }
643 }
644 }
645 return $ws;
646}
647
648sub size_request {
649 my ($self) = @_;
650
651 my ($hs, $ws) = (0, 0);
652
653 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
654 $hs += $self->max_row_height ($yi);
655 }
656
657 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
658 my $wm = 0;
659 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
660 $wm += $self->max_col_width ($xi)
661 }
662 if ($ws < $wm) { $ws = $wm }
663 }
664
665 return ($ws, $hs);
666}
667
668sub _draw {
669 my ($self) = @_;
670
671 my $y = 0;
672 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
673 my $x = 0;
674
675 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
676
677 my $c = $self->{children}->[$yi]->[$xi];
678 if ($c) {
679 $c->move ($x, $y, 0); #TODO: Move to size_request
680 $c->draw if $c;
681 }
682
683 $x += $self->max_col_width ($xi);
684 }
685
686 $y += $self->max_row_height ($yi);
687 }
688}
689
690#############################################################################
691
692package CFClient::UI::VBox;
693
694our @ISA = CFClient::UI::Container::;
695
696use SDL::OpenGL;
697
698sub size_request {
699 my ($self) = @_;
700
701 my @alloc = map [$_->size_request], @{$self->{children}};
702
703 (
704 (List::Util::max map $_->[0], @alloc),
705 (List::Util::sum map $_->[1], @alloc),
706 )
707}
708
709sub size_allocate {
710 my ($self, $w, $h) = @_;
711
712 return unless $self->{w} != $w || $self->{h} != $h;
713
714 $self->{w} = $w;
715 $self->{h} = $h;
716
717 return unless $self->{h};
718
719 my $children = $self->{children};
720
721 my @h = map +($_->size_request)[1], @$children;
722
723 my $req_h = List::Util::sum @h;
724
725 if ($req_h > $h) {
726 # ah well, not enough space
727 $_ = $h[$_] * $h / $req_h for @h;
728 } else {
729 my @exp = grep $_->{expand}, @$children;
730 @exp = @$children unless @exp;
731
732 my %exp = map +($_ => 1), @exp;
733
734 for (0 .. $#$children) {
735 my $child = $children->[$_];
736
737 my $alloc_h = $h[$_];
738 $alloc_h += ($h - $req_h) / @exp if $exp{$child};
739 $h[$_] = $alloc_h;
740 }
741 }
742
743 my $y = 0;
744 for (0 .. $#$children) {
745 my $child = $children->[$_];
746 my $h = $h[$_];
747 $child->move (0, $y);
748 $child->size_allocate ($w, $h);
749
750 $y += $h;
751 }
752}
753
754#############################################################################
755
756package CFClient::UI::Label;
757
758our @ISA = CFClient::UI::Base::;
759
760use SDL::OpenGL;
761
762sub new {
763 my ($class, %arg) = @_;
764
765 my $self = $class->SUPER::new (
766 fg => [1, 1, 1],
767 height => $::FONTSIZE,
768 text => "",
769 align => -1,
770 layout => new CFClient::Layout,
771 %arg
772 );
773
774 $self->set_text ($self->{text});
775
776 $self
777}
778
779sub escape_text {
780 local $_ = $_[1];
781
782 s/&/&amp;/g;
783 s/>/&gt;/g;
784 s/</&lt;/g;
785
786 $_[1]
787}
788
789sub set_text {
790 my ($self, $text) = @_;
791
792 $self->{text} = $text;
793 $self->{layout}->set_markup ($text);
794
795 delete $self->{texture};
796 $self->update;
797}
798
799sub get_text {
800 my ($self, $text) = @_;
801
802 $self->{text}
803}
804
805sub size_request {
806 my ($self) = @_;
807
808 $self->{layout}->set_width;
809 $self->{layout}->set_height ($self->{height});
810 $self->{layout}->size
811# if ($self->{texture}{w} > 1 && $self->{texture}{height} > 1) { #TODO: hack
812# (
813# $self->{texture}{w},
814# $self->{texture}{h},
815# )
816# } else {
817# my ($w, $h, $data) = CFClient::font_render "Yy", $self->{h};
818#
819# ($w, $h)
820# }
821}
822
823sub size_allocate {
824 my ($self, $w, $h) = @_;
825
826 return unless $self->{w} != $w || $self->{h} != $h;
827
828 $self->SUPER::size_allocate ($w, $h);
829 delete $self->{texture};
830}
831
832sub update {
833 my ($self) = @_;
834
835 delete $self->{texture};
836 $self->SUPER::update;
837}
838
839sub _draw {
840 my ($self) = @_;
841
842 my $tex = $self->{texture} ||= do {
843 $self->{layout}->set_width ($self->{w});
844 new_from_layout CFClient::Texture $self->{layout}
845 };
846
847 glEnable GL_BLEND;
848 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
849 glEnable GL_TEXTURE_2D;
850 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
851
852 glColor @{$self->{fg}}; 1013 glColor @{$self->{fg}};
853 1014
854 my $x = 1015 my $x =
855 $self->{align} < 0 ? 0 1016 $self->{align} < 0 ? $self->{padding}
856 : $self->{align} > 0 ? $self->{w} - $tex->{w} 1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
857 : ($self->{w} - $tex->{w}) * 0.5; 1018 : ($self->{w} - $tex->{w}) * 0.5;
858 1019
859 $tex->draw_quad ($x, 0); 1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5);
860 1021
861 glDisable GL_TEXTURE_2D; 1022 glDisable GL_TEXTURE_2D;
862 glDisable GL_BLEND; 1023 glDisable GL_BLEND;
863 glGetError and die;
864} 1024}
865 1025
866############################################################################# 1026#############################################################################
867 1027
868package CFClient::UI::Entry; 1028package CFClient::UI::Entry;
875sub new { 1035sub new {
876 my $class = shift; 1036 my $class = shift;
877 1037
878 $class->SUPER::new ( 1038 $class->SUPER::new (
879 fg => [1, 1, 1], 1039 fg => [1, 1, 1],
880 bg => [0, 0, 0, 0.4], 1040 bg => [0, 0, 0, 0.2],
881 active_bg => [1, 1, 1], 1041 active_bg => [1, 1, 1, 0.5],
882 active_fg => [0, 0, 0], 1042 active_fg => [0, 0, 0],
1043 can_hover => 1,
1044 can_focus => 1,
883 @_ 1045 @_
884 ) 1046 )
885} 1047}
886 1048
887sub _set_text { 1049sub _set_text {
888 my ($self, $text) = @_; 1050 my ($self, $text) = @_;
889 1051
1052 my $old_text = $self->{text};
1053
890 $self->{last_activity} = $::NOW; 1054 $self->{last_activity} = $::NOW;
891 1055
892 $self->{text} = $text; 1056 $self->{text} = $text;
893 $self->{layout}->set_width ($self->{w}); 1057 $self->{layout}->set_width ($self->{w});
1058 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
894 1059
895 $text =~ s/./*/g if $self->{hidden}; 1060 $text =~ s/./*/g if $self->{hidden};
896 1061
897
898 $self->{layout}->set_markup ($self->escape_text ($text)); 1062 $self->{layout}->set_markup ($self->escape_text ($text) . " ");
899 1063
900 $text = substr $text, 0, $self->{cursor}; 1064 $text = substr $text, 0, $self->{cursor};
901 utf8::encode $text; 1065 utf8::encode $text;
902 1066
903 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 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};
904} 1071}
905 1072
906sub size_request { 1073sub size_request {
907 my ($self) = @_; 1074 my ($self) = @_;
908 1075
910 1077
911 ($w + 1, $h) # add 1 for cursor 1078 ($w + 1, $h) # add 1 for cursor
912} 1079}
913 1080
914sub size_allocate { 1081sub size_allocate {
915 my ($self, $w, $h) = @_; 1082 my ($self, $x, $y, $w, $h) = @_;
916 1083
917 return unless $self->{w} != $w || $self->{h} != $h;
918
919 $self->SUPER::size_allocate ($w, $h); 1084 $self->SUPER::size_allocate ($x, $y, $w, $h);
920 1085
921 $self->_set_text ($self->{text}); 1086 $self->_set_text ($self->{text});
922} 1087}
923 1088
924sub set_text { 1089sub set_text {
945 substr $text, $self->{cursor}, 1, ""; 1110 substr $text, $self->{cursor}, 1, "";
946 } elsif ($sym == SDLK_LEFT) { 1111 } elsif ($sym == SDLK_LEFT) {
947 --$self->{cursor} if $self->{cursor}; 1112 --$self->{cursor} if $self->{cursor};
948 } elsif ($sym == SDLK_RIGHT) { 1113 } elsif ($sym == SDLK_RIGHT) {
949 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1114 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) {
1116 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) {
1118 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) {
1120 $self->emit ('escape');
950 } elsif ($uni) { 1121 } elsif ($uni) {
951 substr $text, $self->{cursor}++, 0, chr $uni; 1122 substr $text, $self->{cursor}++, 0, chr $uni;
952 } 1123 }
953 1124
954 $self->_set_text ($text); 1125 $self->_set_text ($text);
969 $self->SUPER::button_down ($ev, $x, $y); 1140 $self->SUPER::button_down ($ev, $x, $y);
970 1141
971 my $idx = $self->{layout}->xy_to_index ($x, $y); 1142 my $idx = $self->{layout}->xy_to_index ($x, $y);
972 1143
973 # byte-index to char-index 1144 # byte-index to char-index
974 my $text = $self->{layout}; 1145 my $text = $self->{text};
975 utf8::encode $text; 1146 utf8::encode $text;
976 $self->{cursor} = length substr $text, 0, $idx; 1147 $self->{cursor} = length substr $text, 0, $idx;
977 1148
978 $self->_set_text ($self->{text}); 1149 $self->_set_text ($self->{text});
979 $self->update; 1150 $self->update;
994 $self->{fg} = $self->{active_fg}; 1165 $self->{fg} = $self->{active_fg};
995 } else { 1166 } else {
996 glColor @{$self->{bg}}; 1167 glColor @{$self->{bg}};
997 } 1168 }
998 1169
1170 glEnable GL_BLEND;
1171 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
999 glBegin GL_QUADS; 1172 glBegin GL_QUADS;
1000 glVertex 0 , 0; 1173 glVertex 0 , 0;
1001 glVertex 0 , $self->{h}; 1174 glVertex 0 , $self->{h};
1002 glVertex $self->{w}, $self->{h}; 1175 glVertex $self->{w}, $self->{h};
1003 glVertex $self->{w}, 0; 1176 glVertex $self->{w}, 0;
1004 glEnd; 1177 glEnd;
1178 glDisable GL_BLEND;
1005 1179
1006 $self->SUPER::_draw; 1180 $self->SUPER::_draw;
1007 1181
1008 #TODO: force update every cursor change :( 1182 #TODO: force update every cursor change :(
1009 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1013 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h};
1014 glEnd; 1188 glEnd;
1015 } 1189 }
1016} 1190}
1017 1191
1018#############################################################################
1019
1020package CFClient::UI::Slider; 1192package CFClient::UI::LineEntry;
1021 1193
1022use strict; 1194our @ISA = CFClient::UI::Entry::;
1023 1195
1196use SDL;
1024use SDL::OpenGL; 1197use SDL::OpenGL;
1025 1198
1026our @ISA = CFClient::UI::DrawBG::; 1199sub key_down {
1027
1028sub size_request {
1029 my ($self) = @_; 1200 my ($self, $ev) = @_;
1030 1201
1031 my $w = 50; 1202 my $sym = $ev->key_sym;
1032 my $h = 10;
1033 1203
1034 $self->{vertical} ? ($h, $w) : ($w, $h) 1204 if ($sym == SDLK_RETURN) {
1205 $self->emit (activate => $self->get_text);
1206 $self->update;
1207
1208 } else {
1209 $self->SUPER::key_down ($ev);
1210 }
1211
1035} 1212}
1213
1214#############################################################################
1215
1216package CFClient::UI::Button;
1217
1218our @ISA = CFClient::UI::Label::;
1219
1220use SDL;
1221use SDL::OpenGL;
1222
1223my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1225 qw(b1_button_active.png);
1036 1226
1037sub new { 1227sub new {
1038 my $class = shift; 1228 my $class = shift;
1039 1229
1230 $class->SUPER::new (
1231 padding => 4,
1232 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0],
1235 can_hover => 1,
1236 @_
1237 )
1238}
1239
1240sub button_up {
1241 my ($self, $ev, $x, $y) = @_;
1242
1243 if ($x >= 0 && $x < $self->{w}
1244 && $y >= 0 && $y < $self->{h}) {
1245 $self->emit ("activate");
1246 }
1247}
1248
1249sub _draw {
1250 my ($self) = @_;
1251
1252 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
1259 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg};
1261 }
1262
1263 glBindTexture GL_TEXTURE_2D, $tex->{name};
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1265
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
1267
1268 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND;
1270
1271 $self->SUPER::_draw;
1272}
1273
1274#############################################################################
1275
1276package CFClient::UI::CheckBox;
1277
1278our @ISA = CFClient::UI::DrawBG::;
1279
1280my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283
1284use SDL;
1285use SDL::OpenGL;
1286
1287sub new {
1288 my $class = shift;
1289
1290 $class->SUPER::new (
1291 padding => 2,
1292 fg => [1, 1, 1],
1293 active_fg => [1, 1, 0],
1294 state => 0,
1295 can_hover => 1,
1296 @_
1297 )
1298}
1299
1300sub size_request {
1301 my ($self) = @_;
1302
1303 ($self->{padding} * 2 + 6) x 2
1304}
1305
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down {
1313 my ($self, $ev, $x, $y) = @_;
1314
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1317 $self->{state} = !$self->{state};
1318 $self->emit (changed => $self->{state});
1319 }
1320}
1321
1322sub _draw {
1323 my ($self) = @_;
1324
1325 $self->SUPER::_draw;
1326
1327 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1328
1329 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1330
1331 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1332
1333 glEnable GL_BLEND;
1334 glEnable GL_TEXTURE_2D;
1335 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1336
1337 my $tex = $self->{state} ? $tex[1] : $tex[0];
1338
1339 $tex->draw_quad (0, 0, $s, $s);
1340
1341 glDisable GL_TEXTURE_2D;
1342 glDisable GL_BLEND;
1343}
1344
1345#############################################################################
1346
1347package CFClient::UI::Slider;
1348
1349use strict;
1350
1351use SDL::OpenGL;
1352
1353our @ISA = CFClient::UI::DrawBG::;
1354
1355my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1357 qw(s1_slider.png s1_slider_bg.png);
1358
1359sub new {
1360 my $class = shift;
1361
1040 # range [value, low, high, page] 1362 # range [value, low, high, page]
1041 1363
1364 # TODO: 0-width page
1365 # TODO: req_w/h are wrong with vertical
1366 # TODO: calculations are off
1042 $class->SUPER::new ( 1367 my $self = $class->SUPER::new (
1043 fg => [1, 1, 1], 1368 fg => [1, 1, 1],
1044 active_fg => [0, 0, 0], 1369 active_fg => [0, 0, 0],
1045 range => [0, 0, 100, 10], 1370 range => [0, 0, 100, 10],
1371 req_w => 40,
1372 req_h => 13,
1046 vertical => 1, 1373 vertical => 0,
1374 can_hover => 1,
1375 inner_pad => 5,
1047 @_ 1376 @_
1048 ) 1377 );
1378
1379 $self
1380}
1381
1382sub size_request {
1383 my ($self) = @_;
1384
1385 my $w = $self->{req_w};
1386 my $h = $self->{req_h};
1387
1388 $self->{vertical} ? ($h, $w) : ($w, $h)
1049} 1389}
1050 1390
1051sub button_down { 1391sub button_down {
1052 my ($self, $ev, $x, $y) = @_; 1392 my ($self, $ev, $x, $y) = @_;
1053 1393
1061 if ($GRAB == $self) { 1401 if ($GRAB == $self) {
1062 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1402 my ($value, $lo, $hi, $page) = @{$self->{range}};
1063 1403
1064 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1404 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1065 1405
1406 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1407 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1408
1409 $x -= $inner_pad_px; # substract the padding
1066 $x = $x * ($hi - $lo) / $w + $lo; 1410 $x = $x * ($hi - $lo) / $inner_w + $lo;
1067 $x = $lo if $x < $lo; 1411 $x = $lo if $x < $lo;
1068 $x = $hi - $page if $x > $hi - $page; 1412 $x = $hi - $page if $x > $hi - $page;
1069 $self->{range}[0] = $x; 1413 $self->{range}[0] = $x;
1070 1414
1071 $self->emit (changed => $x); 1415 $self->emit (changed => $x);
1072 $self->update; 1416 $self->update;
1073 } 1417 }
1418}
1419
1420# the inner_* stuff is for generating a padding for the slider handle,
1421# so that the handle doesn't leave the texture. This calculation isn't 100%
1422# correct propably, but it does the job for now
1423sub _calc_inner_pad_px {
1424 my ($self, $w) = @_;
1425 ($w / 100) * $self->{inner_pad} # % to pixels
1074} 1426}
1075 1427
1076sub _draw { 1428sub _draw {
1077 my ($self) = @_; 1429 my ($self) = @_;
1078 1430
1092 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1444 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1093 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1445 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1094 1446
1095 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1447 my ($value, $lo, $hi, $page) = @{$self->{range}};
1096 1448
1449 $hi = $value + 1 if $lo == $hi;
1450
1451 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1452 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1453
1097 $page = int $page * $w / ($hi - $lo); 1454 $page = int $page * $inner_w / ($hi - $lo);
1098 $value = int +($value - $lo) * $w / ($hi - $lo); 1455 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1099 1456
1100 $w -= $page; 1457 $w -= $page;
1101 $page &= ~1; 1458 $page &= ~1;
1102 glTranslate $page * 0.5, 0, 0; 1459 glTranslate $page * 0.5, 0, 0;
1460 $page ||= 2;
1103 1461
1104 glColor @$fg;
1105 glBegin GL_LINES;
1106 glVertex 0, 0; glVertex 0, $h;
1107 glVertex $w - 1, 0; glVertex $w - 1, $h;
1108 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1109 glEnd;
1110
1111 my $knob_a = $value - $page * 0.5; 1462 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1112 my $knob_b = $value + $page * 0.5; 1463 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1113 1464
1114 glBegin GL_QUADS; 1465 glEnable GL_BLEND;
1115 glColor @$fg; 1466 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1116 glVertex $knob_a, 0; 1467 glEnable GL_TEXTURE_2D;
1117 glVertex $knob_a, $h; 1468 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1118 glVertex $knob_b, $h;
1119 glVertex $knob_b, 0;
1120 1469
1121 if ($knob_a < $knob_b - 2) { 1470 # draw background
1122 glColor @$bg; 1471 $tex[1]->draw_quad (0, 0, $w, $h);
1123 glVertex $knob_a + 1, 1; 1472
1124 glVertex $knob_a + 1, $h - 1; 1473 # draw handle
1125 glVertex $knob_b - 1, $h - 1; 1474 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1126 glVertex $knob_b - 1, 1; 1475
1476 glDisable GL_BLEND;
1477 glDisable GL_TEXTURE_2D;
1478}
1479
1480#############################################################################
1481
1482package CFClient::UI::TextView;
1483
1484our @ISA = CFClient::UI::HBox::;
1485
1486use SDL::OpenGL;
1487
1488sub new {
1489 my $class = shift;
1490
1491 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE,
1495 @_,
1496
1497 layout => (new CFClient::Layout),
1498 par => [],
1499 height => 0,
1500 children => [
1501 (new CFClient::UI::Empty expand => 1),
1502 (new CFClient::UI::Slider vertical => 1),
1503 ],
1504 );
1505
1506 $self->{children}[1]->connect (changed => sub {
1507 $self->update;
1508 });
1509
1510 $self
1511}
1512
1513sub set_fontsize {
1514 my ($self, $fontsize) = @_;
1515
1516 $self->{fontsize} = $fontsize;
1517 $self->reflow;
1518}
1519
1520sub text_height {
1521 my ($self, $text) = @_;
1522
1523 my $layout = $self->{layout};
1524
1525 $layout->set_height ($self->{fontsize});
1526 $layout->set_width ($self->{w});
1527 $layout->set_text ($text);
1127 } 1528
1128 glEnd; 1529 ($layout->size)[1]
1530}
1531
1532sub reflow {
1533 my ($self) = @_;
1534
1535 $self->{need_reflow}++;
1536 $self->update;
1537}
1538
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_;
1547
1548 $self->SUPER::size_allocate ($x, $y, $w, $h);
1549
1550 $self->{layout}->set_height ($self->{fontsize});
1551 $self->{layout}->set_width ($self->{w});
1552
1553 $self->reflow;
1554}
1555
1556sub add_paragraph {
1557 my ($self, $color, $text) = @_;
1558
1559 #TODO: intelligently "reformat" paragraph
1560
1561 my $height = $self->text_height ($text);
1562
1563 $self->{height} += $height;
1564
1565 push @{$self->{par}}, [$height, $color, $text];
1566
1567 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1568 $self->{children}[1]->update;
1569}
1570
1571sub update {
1572 my ($self) = @_;
1573
1574 $self->SUPER::update;
1575
1576 return unless $self->{h} > 0;
1577
1578 delete $self->{texture};
1579
1580 $TOPLEVEL->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) {
1582 my $height = 0;
1583
1584 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}};
1586
1587 $self->{height} = $height;
1588
1589 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1590
1591 delete $self->{texture};
1592 }
1593
1594 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1595 glClearColor 0, 0, 0, 1;
1596 glClear GL_COLOR_BUFFER_BIT;
1597
1598 glEnable GL_BLEND;
1599 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1600 glEnable GL_TEXTURE_2D;
1601 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1602
1603 my $top = int $self->{children}[1]{range}[0];
1604
1605 my $y0 = $top;
1606 my $y1 = $top + $self->{h};
1607
1608 my $y = 0;
1609
1610 my $layout = $self->{layout};
1611
1612 for my $par (@{$self->{par}}) {
1613 my $h = $par->[0];
1614
1615 if ($y0 < $y + $h && $y < $y1) {
1616 $layout->set_text ($par->[2]);
1617
1618 glColor @{ $par->[1] };
1619 my ($W, $H) = $layout->size;
1620 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1621 }
1622
1623 $y += $h;
1624 }
1625
1626 glDisable GL_TEXTURE_2D;
1627 glDisable GL_BLEND;
1628 };
1629 });
1630}
1631
1632sub _draw {
1633 my ($self) = @_;
1634
1635 if ($self->{texture}) {
1636 glEnable GL_TEXTURE_2D;
1637 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1638 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1639 glDisable GL_TEXTURE_2D;
1640 }
1641
1642 $self->{children}[1]->draw;
1643
1129} 1644}
1130 1645
1131############################################################################# 1646#############################################################################
1132 1647
1133package CFClient::UI::MapWidget; 1648package CFClient::UI::MapWidget;
1143 1658
1144sub new { 1659sub new {
1145 my $class = shift; 1660 my $class = shift;
1146 1661
1147 $class->SUPER::new ( 1662 $class->SUPER::new (
1148 z => -1, 1663 z => -1,
1664 can_focus => 1,
1149 list => (glGenLists 1), 1665 list => (glGenLists 1),
1150 @_ 1666 @_
1151 ) 1667 )
1152} 1668}
1153 1669
1154sub key_down { 1670sub key_down {
1156} 1672}
1157 1673
1158sub key_up { 1674sub key_up {
1159} 1675}
1160 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
1161sub size_request { 1711sub size_request {
1162 ( 1712 (
1163 1 + int $::WIDTH / 32, 1713 1 + 32 * int $::WIDTH / 32,
1164 1 + int $::HEIGHT / 32, 1714 1 + 32 * int $::HEIGHT / 32,
1165 ) 1715 )
1166} 1716}
1167 1717
1168sub update { 1718sub update {
1169 my ($self) = @_; 1719 my ($self) = @_;
1170 1720
1171 $self->{need_update} = 1; 1721 $self->{need_update} = 1;
1172 $self->SUPER::update; 1722 $self->SUPER::update;
1173} 1723}
1174 1724
1175sub _draw { 1725sub draw {
1176 my ($self) = @_; 1726 my ($self) = @_;
1177 1727
1178 if (delete $self->{need_update}) { 1728 if (delete $self->{need_update}) {
1179 glNewList $self->{list}, GL_COMPILE; 1729 glNewList $self->{list}, GL_COMPILE;
1180 1730
1181 my $mx = $::CONN->{mapx}; 1731 if ($::MAP) {
1182 my $my = $::CONN->{mapy};
1183
1184 my $map = $::CONN->{map};
1185
1186 my ($xofs, $yofs);
1187
1188 my $sw = 1 + int $::WIDTH / 32; 1732 my $sw = int $::WIDTH / 32;
1189 my $sh = 1 + int $::HEIGHT / 32; 1733 my $sh = int $::HEIGHT / 32;
1190 1734
1191 if ($::CONN->{mapw} > $sw) { 1735 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1192 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5; 1736 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1193 } else { 1737
1194 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs}; 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
1195 } 1800 }
1196 1801
1197 if ($::CONN->{maph} > $sh) {
1198 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5;
1199 } else {
1200 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1201 }
1202
1203 glEnable GL_TEXTURE_2D;
1204 glEnable GL_BLEND;
1205 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1207
1208 my $sw4 = ($sw + 3) & ~3;
1209 my $darkness = "\x00" x ($sw4 * $sh);
1210
1211 for my $x (0 .. $sw - 1) {
1212 my $row = $map->[$x + $xofs];
1213 for my $y (0 .. $sh - 1) {
1214
1215 my $cell = $row->[$y + $yofs]
1216 or next;
1217
1218 my $dark = $cell->[0];
1219 if ($dark < 0) {
1220 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1221 } else {
1222 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1223 }
1224
1225 for my $num (grep $_, @$cell[1,2,3]) {
1226 my $tex = $::CONN->{face}[$num]{texture} || next;
1227
1228 my ($w, $h) = @$tex{qw(w h)};
1229
1230 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1231 }
1232 }
1233 }
1234
1235# if (1) { # higher quality darkness
1236# $lighting =~ s/(.)/$1$1$1/gs;
1237# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1238#
1239# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1240#
1241# $lighting = $pb->get_pixels;
1242# $lighting =~ s/(.)../$1/gs;
1243# }
1244
1245 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1246
1247 $darkness = new CFClient::Texture
1248 w => $sw4,
1249 h => $sh,
1250 data => $darkness,
1251 internalformat => GL_ALPHA,
1252 format => GL_ALPHA;
1253
1254 glColor 0.45, 0.45, 0.45, 1;
1255 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1256
1257 glDisable GL_TEXTURE_2D;
1258 glDisable GL_BLEND;
1259
1260 glEndList; 1802 glEndList;
1261 } 1803 }
1262 1804
1805 glPushMatrix;
1263 glCallList $self->{list}; 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 }
1264} 1821}
1265 1822
1266my %DIR = ( 1823my %DIR = (
1267 SDLK_KP8, [1, "north"], 1824 SDLK_KP8, [1, "north"],
1268 SDLK_KP9, [2, "northeast"], 1825 SDLK_KP9, [2, "northeast"],
1284 1841
1285 my $mod = $ev->key_mod; 1842 my $mod = $ev->key_mod;
1286 my $sym = $ev->key_sym; 1843 my $sym = $ev->key_sym;
1287 1844
1288 if ($sym == SDLK_KP5) { 1845 if ($sym == SDLK_KP5) {
1289 $::CONN->send ("command stay fire"); 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' => '/');
1290 } elsif (exists $DIR{$sym}) { 1853 } elsif (exists $DIR{$sym}) {
1291 if ($mod & KMOD_SHIFT) { 1854 if ($mod & KMOD_SHIFT) {
1292 $self->{shft}++; 1855 $self->{shft}++;
1293 $::CONN->send ("command fire $DIR{$sym}[0]"); 1856 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1294 } elsif ($mod & KMOD_CTRL) { 1857 } elsif ($mod & KMOD_CTRL) {
1295 $self->{ctrl}++; 1858 $self->{ctrl}++;
1296 $::CONN->send ("command run $DIR{$sym}[0]"); 1859 $::CONN->user_send ("command run $DIR{$sym}[0]");
1297 } else { 1860 } else {
1298 $::CONN->send ("command $DIR{$sym}[1]"); 1861 $::CONN->user_send ("command $DIR{$sym}[1]");
1299 } 1862 }
1300 } 1863 }
1301} 1864}
1302 1865
1303sub key_up { 1866sub key_up {
1305 1868
1306 my $mod = $ev->key_mod; 1869 my $mod = $ev->key_mod;
1307 my $sym = $ev->key_sym; 1870 my $sym = $ev->key_sym;
1308 1871
1309 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) { 1872 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1310 $::CONN->send ("command fire_stop"); 1873 $::CONN->user_send ("command fire_stop");
1311 } 1874 }
1312 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 1875 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1313 $::CONN->send ("command run_stop"); 1876 $::CONN->user_send ("command run_stop");
1314 } 1877 }
1315} 1878}
1316 1879
1317############################################################################# 1880#############################################################################
1318 1881
1356 glPopMatrix; 1919 glPopMatrix;
1357} 1920}
1358 1921
1359############################################################################# 1922#############################################################################
1360 1923
1924package CFClient::UI::Flopper;
1925
1926our @ISA = CFClient::UI::Button::;
1927
1928sub new {
1929 my $class = shift;
1930
1931 my $self = $class->SUPER::new (
1932 state => 0,
1933 connect_activate => \&toggle_flopper,
1934 @_
1935 );
1936
1937 if ($self->{state}) {
1938 $self->{state} = 0;
1939 $self->toggle_flopper;
1940 }
1941
1942 $self
1943}
1944
1945sub toggle_flopper {
1946 my ($self) = @_;
1947
1948 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other});
1951 $self->{other}->move (
1952 ($::WIDTH - $self->{other}{w}) * 0.5,
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other});
1957 }
1958}
1959
1960#############################################################################
1961
1361package CFClient::UI::Toplevel; 1962package CFClient::UI::Toplevel;
1362 1963
1363our @ISA = CFClient::UI::Container::; 1964our @ISA = CFClient::UI::Container::;
1965
1966use SDL::OpenGL;
1364 1967
1365sub size_request { 1968sub size_request {
1366 ($::WIDTH, $::HEIGHT) 1969 ($::WIDTH, $::HEIGHT)
1367} 1970}
1368 1971
1369sub size_allocate { 1972sub size_allocate {
1370 my ($self, $w, $h) = @_; 1973 my ($self, $x, $y, $w, $h) = @_;
1371 1974
1372 $self->SUPER::size_allocate ($w, $h); 1975 $self->_size_allocate ($x, $y, $w, $h);
1373 1976
1374 $_->size_allocate ($_->size_request) 1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1375 for @{$self->{children}}; 1978 for @{$self->{children}};
1376} 1979}
1377 1980
1378sub translate { 1981sub translate {
1379 my ($self, $x, $y) = @_; 1982 my ($self, $x, $y) = @_;
1382} 1985}
1383 1986
1384sub update { 1987sub update {
1385 my ($self) = @_; 1988 my ($self) = @_;
1386 1989
1387 $self->size_allocate ($self->size_request); 1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1388 ::refresh (); 1991 ::refresh ();
1389} 1992}
1390 1993
1391sub add { 1994sub add {
1392 my ($self, $widget) = @_; 1995 my ($self, $widget) = @_;
1393 1996
1394 $self->SUPER::add ($widget); 1997 $self->SUPER::add ($widget);
1395 1998
1396 $widget->size_allocate ($widget->size_request); 1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000}
2001
2002sub on_refresh {
2003 my ($self, $id, $cb) = @_;
2004
2005 $self->{refresh_hook}{$id} = $cb;
1397} 2006}
1398 2007
1399sub draw { 2008sub draw {
1400 my ($self) = @_; 2009 my ($self) = @_;
1401 2010
2011 while (my $rcb = delete $self->{refresh_hook}) {
2012 $_->() for values %$rcb;
2013 }
2014
2015 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2016 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2017 glClear GL_COLOR_BUFFER_BIT;
2018
2019 glMatrixMode GL_PROJECTION;
2020 glLoadIdentity;
2021 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2022 glMatrixMode GL_MODELVIEW;
2023 glLoadIdentity;
2024
1402 $self->_draw; 2025 $self->_draw;
1403} 2026}
1404 2027
1405############################################################################# 2028#############################################################################
1406 2029

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines