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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines