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.121 by root, Mon Apr 17 19:21:00 2006 UTC vs.
Revision 1.148 by elmex, Sat Apr 22 12:14:45 2006 UTC

21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
87 87
88package CFClient::UI::Base; 88package CFClient::UI::Base;
89 89
90use strict; 90use strict;
91 91
92use SDL::OpenGL; 92use CFClient::OpenGL;
93 93
94sub new { 94sub new {
95 my $class = shift; 95 my $class = shift;
96 96
97 my $self = bless { 97 my $self = bless {
98 x => 0, 98 x => 0,
99 y => 0, 99 y => 0,
100 z => 0, 100 z => 0,
101 w => -1,
102 h => -1,
103 @_ 101 @_
104 }, $class; 102 }, $class;
105 103
106 for (keys %$self) { 104 for (keys %$self) {
107 if (/^connect_(.*)$/) { 105 if (/^connect_(.*)$/) {
110 } 108 }
111 109
112 $self 110 $self
113} 111}
114 112
113sub show {
114 my ($self) = @_;
115
116 return if $self->{parent};
117
118 $CFClient::UI::ROOT->add ($self);
119}
120
121sub hide {
122 my ($self) = @_;
123
124 return unless $self->{parent};
125
126 $self->{parent}->remove ($self);
127}
128
115sub move { 129sub move {
116 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
117 $self->{x} = int $x; 132 $self->{x} = int $x;
118 $self->{y} = int $y; 133 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 134 $self->{z} = $z if defined $z;
135
136 $self->update;
120} 137}
121 138
122sub needs_redraw { 139sub needs_redraw {
123 0 140 0
124} 141}
125 142
126sub size_request { 143sub size_request {
127 require Carp; 144 require Carp;
128 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abstract";
129} 146}
130 147
131sub _size_allocate { 148sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
133 150
151 if ($self->{aspect}) {
152 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154
155 # use alignment to adjust x, y
156
157 $x += int +($w - $w2) * 0.5;
158 $y += int +($h - $h2) * 0.5;
159
160 ($w, $h) = ($w2, $h2);
161 }
162
163 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 164 $self->{x} = $x;
135 $self->{y} = $y; 165 $self->{y} = $y;
166 $self->update;
167 }
136 168
137 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 170 $self->{w} = $w;
140 $self->{h} = $h; 171 $self->{h} = $h;
141 172
173 $self->size_allocate ($w, $h);
174 $self->update;
142 1 175 }
143} 176}
144 177
145sub size_allocate { 178sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 179 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 180}
150 181
151# return top left coordinates 182# return top left coordinates
152sub _topleft { 183sub _topleft {
153 my ($self, $x, $y) = @_; 184 my ($self, $x, $y) = @_;
225 glPopMatrix; 256 glPopMatrix;
226 257
227 if ($self == $HOVER && $self->{can_hover}) { 258 if ($self == $HOVER && $self->{can_hover}) {
228 my ($x, $y) = @$self{qw(x y)}; 259 my ($x, $y) = @$self{qw(x y)};
229 260
230 glColor 0, 0, 1, 0.2; 261 glColor 1, 0.8, 0.5, 0.2;
231 glEnable GL_BLEND; 262 glEnable GL_BLEND;
232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 263 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
233 glBegin GL_QUADS; 264 glBegin GL_QUADS;
234 glVertex $x , $y; 265 glVertex $x , $y;
235 glVertex $x + $self->{w}, $y; 266 glVertex $x + $self->{w}, $y;
260 my ($self, $parent) = @_; 291 my ($self, $parent) = @_;
261 292
262 Scalar::Util::weaken ($self->{parent} = $parent); 293 Scalar::Util::weaken ($self->{parent} = $parent);
263} 294}
264 295
296sub check_size {
297 my ($self) = @_;
298
299 my ($w, $h) = $self->size_request;
300
301 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w;
303 $self->{req_h} = $h;
304
305 $self->{parent}->check_size
306 if $self->{parent};
307 }
308}
309
265sub update { 310sub update {
266 my ($self) = @_; 311 my ($self) = @_;
267 312
268 $self->{parent}->update 313 $self->{parent}->update
269 if $self->{parent}; 314 if $self->{parent};
270} 315}
271 316
272sub connect { 317sub connect {
273 my ($self, $signal, $cb) = @_; 318 my ($self, $signal, $cb) = @_;
274 319
275 push @{ $self->{cb}{$signal} }, $cb; 320 push @{ $self->{signal_cb}{$signal} }, $cb;
276} 321}
277 322
278sub emit { 323sub emit {
279 my ($self, $signal, @args) = @_; 324 my ($self, $signal, @args) = @_;
280 325
326 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
281 $_->($self, @args) 327 $cb->($self, @args);
282 for @{$self->{cb}{$signal} || []}; 328 }
283} 329}
284 330
285sub DESTROY { 331sub DESTROY {
286 my ($self) = @_; 332 my ($self) = @_;
287 333
293package CFClient::UI::DrawBG; 339package CFClient::UI::DrawBG;
294 340
295our @ISA = CFClient::UI::Base::; 341our @ISA = CFClient::UI::Base::;
296 342
297use strict; 343use strict;
298use SDL::OpenGL; 344use CFClient::OpenGL;
299 345
300sub new { 346sub new {
301 my $class = shift; 347 my $class = shift;
302 348
303 # range [value, low, high, page] 349 # range [value, low, high, page]
367 $self->{children} = [ 413 $self->{children} = [
368 sort { $a->{z} <=> $b->{z} } 414 sort { $a->{z} <=> $b->{z} }
369 @{$self->{children}}, $child 415 @{$self->{children}}, $child
370 ]; 416 ];
371 417
372 $self->{w} = $self->{h} = -1; 418 $child->check_size;
373 $self->update;
374} 419}
375 420
376sub remove { 421sub remove {
377 my ($self, $widget) = @_; 422 my ($self, $child) = @_;
378 423
424 delete $child->{parent};
425
379 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
380 427
381 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 428 $self->check_size;
382} 429}
383 430
384sub find_widget { 431sub find_widget {
385 my ($self, $x, $y) = @_; 432 my ($self, $x, $y) = @_;
386 433
416 463
417 $class->SUPER::new (children => [$child], %arg) 464 $class->SUPER::new (children => [$child], %arg)
418} 465}
419 466
420sub add { 467sub add {
421 my ($self, $widget) = @_; 468 my ($self, $child) = @_;
422 469
423 $self->{children} = []; 470 $self->{children} = [];
424 471
425 $self->SUPER::add ($widget); 472 $self->SUPER::add ($child);
426} 473}
427 474
428sub remove { 475sub remove {
429 my ($self, $widget) = @_; 476 my ($self, $widget) = @_;
430 477
439sub size_request { 486sub size_request {
440 $_[0]{children}[0]->size_request 487 $_[0]{children}[0]->size_request
441} 488}
442 489
443sub size_allocate { 490sub size_allocate {
444 my ($self, $x, $y, $w, $h) = @_; 491 my ($self, $w, $h) = @_;
445 492
446 $self->_size_allocate ($x, $y, $w, $h) or return;
447
448 $self->{children}[0]->size_allocate (0, 0, $w, $h); 493 $self->{children}[0]->configure (0, 0, $w, $h);
449} 494}
450 495
451############################################################################# 496#############################################################################
452 497
453package CFClient::UI::Window; 498package CFClient::UI::Window;
454 499
455our @ISA = CFClient::UI::Bin::; 500our @ISA = CFClient::UI::Bin::;
456 501
457use SDL::OpenGL; 502use CFClient::OpenGL;
458 503
459sub new { 504sub new {
460 my ($class, %arg) = @_; 505 my ($class, %arg) = @_;
461 506
462 my $self = $class->SUPER::new (%arg); 507 my $self = $class->SUPER::new (%arg);
479 $self->child->draw; 524 $self->child->draw;
480 }; 525 };
481} 526}
482 527
483sub size_allocate { 528sub size_allocate {
484 my ($self, $x, $y, $w, $h) = @_; 529 my ($self, $w, $h) = @_;
485 530
486 $self->_size_allocate ($x, $y, $w, $h) or return;
487
488 $self->child->size_allocate (0, 0, $w, $h); 531 $self->child->configure (0, 0, $w, $h);
489 532
490 $self->render_chld; 533 $self->render_chld;
491} 534}
492 535
493sub _draw { 536sub _draw {
509 glDisable GL_TEXTURE_2D; 552 glDisable GL_TEXTURE_2D;
510} 553}
511 554
512############################################################################# 555#############################################################################
513 556
557package CFClient::UI::ViewPort;
558
559our @ISA = CFClient::UI::Window::;
560
561sub new { die }
562
563sub size_request {
564 my ($self) = @_;
565
566 @$self{qw(child_w child_h)} = $self->child->size_request;
567 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568
569 @$self{qw(child_w child_h)}
570}
571
572sub _draw {
573 my ($self) = @_;
574
575 $self->{children}[1]->draw;
576}
577
578
579#############################################################################
580
514package CFClient::UI::Frame; 581package CFClient::UI::Frame;
515 582
516our @ISA = CFClient::UI::Bin::; 583our @ISA = CFClient::UI::Bin::;
517 584
518use SDL::OpenGL; 585use CFClient::OpenGL;
519
520sub size_request {
521 my ($self) = @_;
522 my $chld = $self->child
523 or return (0, 0);
524
525 $chld->move (2, 2);
526
527 map { $_ + 4 } $chld->size_request;
528}
529
530sub size_allocate {
531 my ($self, $x, $y, $w, $h) = @_;
532
533 $self->_size_allocate ($x, $y, $w, $h) or return;
534
535 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
536}
537
538sub _draw {
539 my ($self) = @_;
540
541 my $chld = $self->child;
542
543 my ($w, $h) = $chld->size_request;
544
545 glBegin GL_QUADS;
546 glColor 0, 0, 0;
547 glVertex 0 , 0;
548 glVertex 0 , $h + 4;
549 glVertex $w + 4 , $h + 4;
550 glVertex $w + 4 , 0;
551 glEnd;
552
553 $chld->draw;
554}
555
556#############################################################################
557
558package CFClient::UI::FancyFrame;
559
560our @ISA = CFClient::UI::Bin::;
561
562use SDL::OpenGL;
563
564my @tex =
565 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
566 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
567 586
568sub new { 587sub new {
569 my $class = shift; 588 my $class = shift;
570 589
571 # TODO: user_x, user_y, overwrite moveto?
572
573 $class->SUPER::new ( 590 my $self = $class->SUPER::new (
574 bg => [1, 1, 1, 1], 591 bg => [1, 1, 1, 1],
575 border_bg => [1, 1, 1, 1], 592 border_bg => [1, 1, 1, 1],
576 border => int $::FONTSIZE * 0.8, 593 border => 0.8,
577 @_ 594 @_
595 );
596
597 $self
598}
599
600sub set_size {
601 my ($self, $w, $h) = @_;
602 $self->{req_w} = $w;
603 $self->{req_h} = $h;
604 $self->check_size;
605}
606
607sub size_request {
608 my ($self) = @_;
609 ($self->{req_w}, $self->{req_h})
610}
611
612sub size_allocate {
613 my ($self, $w, $h) = @_;
614 $self->{w} = $w;
615 $self->{h} = $h;
616 $self->child->configure (0, 0, $w, $h);
617}
618
619sub _draw {
620 my ($self) = @_;
621
622 my ($w, $h) = ($self->{w}, $self->{h});
623
624 glEnable GL_BLEND;
625 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
626 glEnable GL_TEXTURE_2D;
627 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
628
629# glBegin GL_QUADS;
630# glColor 0, 0, 0, 0;
631# glVertex 0 , 0;
632# glVertex 0 , $h;
633# glVertex $w, $h;
634# glVertex $w, 0;
635# glEnd;
636
637
638 $self->child->draw;
639 glDisable GL_BLEND;
640 glDisable GL_TEXTURE_2D;
641}
642
643#############################################################################
644
645package CFClient::UI::FancyFrame;
646
647our @ISA = CFClient::UI::Bin::;
648
649use CFClient::OpenGL;
650
651my @tex =
652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
653 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
654
655sub new {
656 my $class = shift;
657
658 # TODO: user_x, user_y, overwrite moveto?
659
660 my $self = $class->SUPER::new (
661 bg => [1, 1, 1, 1],
662 border_bg => [1, 1, 1, 1],
663 border => 0.8,
664 @_
665 );
666
667 $self->{title} &&= new CFClient::UI::Label
668 align => 0,
669 valign => 1,
670 text => $self->{title},
671 fontsize => 1;
672
673 $self
674}
675
676sub border {
677 int $_[0]{border} * $::FONTSIZE
678}
679
680sub size_request {
681 my ($self) = @_;
682
683 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
684
685 my ($w, $h) = $self->SUPER::size_request;
686
687 (
688 $w + $self->border * 2,
689 $h + $self->border * 2,
578 ) 690 )
579} 691}
580 692
581sub size_request {
582 my ($self) = @_;
583
584 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
585
586 my ($w, $h) = $self->SUPER::size_request;
587
588 (
589 $w + $self->{border} * 2,
590 $h + $self->{border} * 2,
591 )
592}
593
594sub size_allocate { 693sub size_allocate {
595 my ($self, $x, $y, $w, $h) = @_; 694 my ($self, $w, $h) = @_;
596 695
597 $self->_size_allocate ($x, $y, $w, $h) or return;
598
599 $h -= List::Util::max 0, $self->{border} * 2; 696 $h -= List::Util::max 0, $self->border * 2;
600 $w -= List::Util::max 0, $self->{border} * 2; 697 $w -= List::Util::max 0, $self->border * 2;
601 698
699 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
700 if $self->{title};
701
602 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 702 $self->child->configure ($self->border, $self->border, $w, $h);
603} 703}
604 704
605sub button_down { 705sub button_down {
606 my ($self, $ev, $x, $y) = @_; 706 my ($self, $ev, $x, $y) = @_;
607 707
708 my $border = $self->border;
709
608 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 710 if ($x < $self->{w} && $x >= $self->{w} - $border
609 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 711 && $y < $self->{h} && $y >= $self->{h} - $border) {
610 712
611 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 713 my ($ox, $oy) = ($ev->{x}, $ev->{y});
612 my ($bw, $bh) = ($self->{w}, $self->{h}); 714 my ($bw, $bh) = ($self->{w}, $self->{h});
613 715
614 $self->{motion} = sub { 716 $self->{motion} = sub {
615 my ($ev, $x, $y) = @_; 717 my ($ev, $x, $y) = @_;
616 718
617 ($x, $y) = ($ev->motion_x, $ev->motion_y); 719 ($x, $y) = ($ev->{x}, $ev->{y});
618 720
619 $self->{user_w} = $bw + $x - $ox; 721 $self->{user_w} = $bw + $x - $ox;
620 $self->{user_h} = $bh + $y - $oy; 722 $self->{user_h} = $bh + $y - $oy;
621 $self->update; 723 $self->check_size;
622 }; 724 };
623 725
624 } elsif ($x >= 0 && $x < $self->{w} 726 } elsif ($x >= 0 && $x < $self->{w}
625 && $y >= 0 && $y < $self->{border}) { 727 && $y >= 0 && $y < $border) {
626 728
627 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 729 my ($ox, $oy) = ($ev->{x}, $ev->{y});
628 my ($bx, $by) = ($self->{x}, $self->{y}); 730 my ($bx, $by) = ($self->{x}, $self->{y});
629 731
630 $self->{motion} = sub { 732 $self->{motion} = sub {
631 my ($ev, $x, $y) = @_; 733 my ($ev, $x, $y) = @_;
632 734
633 ($x, $y) = ($ev->motion_x, $ev->motion_y); 735 ($x, $y) = ($ev->{x}, $ev->{y});
634 736
635 $self->move ($bx + $x - $ox, $by + $y - $oy); 737 $self->move ($bx + $x - $ox, $by + $y - $oy);
636 $self->update; 738 $self->update;
637 }; 739 };
638 } 740 }
659 glEnable GL_BLEND; 761 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 762 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 763 glEnable GL_TEXTURE_2D;
662 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 764 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
663 765
766 my $border = $self->border;
767
664 glColor @{ $self->{border_bg} }; 768 glColor @{ $self->{border_bg} };
665 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 769 $tex[1]->draw_quad (0, 0, $w, $border);
666 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 770 $tex[3]->draw_quad (0, $border, $border, $ch);
667 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 771 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
668 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 772 $tex[4]->draw_quad (0, $h - $border, $w, $border);
669 773
670 my $bg = $tex[0]; 774 my $bg = $tex[0];
671 775
672 # TODO: repeat texture not scale 776 # TODO: repeat texture not scale
673 my $rep_x = $cw / $bg->{w}; 777 my $rep_x = $cw / $bg->{w};
676 glColor @{ $self->{bg} }; 780 glColor @{ $self->{bg} };
677 781
678 $bg->{s} = $rep_x; 782 $bg->{s} = $rep_x;
679 $bg->{t} = $rep_y; 783 $bg->{t} = $rep_y;
680 $bg->{wrap_mode} = 1; 784 $bg->{wrap_mode} = 1;
681 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 785 $bg->draw_quad ($border, $border, $cw, $ch);
682 786
683 glDisable GL_TEXTURE_2D; 787 glDisable GL_TEXTURE_2D;
684 glDisable GL_BLEND; 788 glDisable GL_BLEND;
685 789
790 $self->{title}->draw if $self->{title};
686 $self->child->draw; 791 $self->child->draw;
687} 792}
688 793
689############################################################################# 794#############################################################################
690 795
692 797
693our @ISA = CFClient::UI::Base::; 798our @ISA = CFClient::UI::Base::;
694 799
695use List::Util qw(max sum); 800use List::Util qw(max sum);
696 801
697use SDL::OpenGL; 802use CFClient::OpenGL;
698 803
699sub new { 804sub new {
700 my $class = shift; 805 my $class = shift;
701 806
702 $class->SUPER::new ( 807 $class->SUPER::new (
709 my ($self, $x, $y, $child) = @_; 814 my ($self, $x, $y, $child) = @_;
710 815
711 $child->set_parent ($self); 816 $child->set_parent ($self);
712 $self->{children}[$y][$x] = $child; 817 $self->{children}[$y][$x] = $child;
713 818
714 $self->{w} = $self->{h} = -1; 819 $child->check_size;
715 $self->update;
716} 820}
717 821
718# TODO: move to container class maybe? send childs a signal on removal? 822# TODO: move to container class maybe? send childs a signal on removal?
719sub clear { 823sub clear {
720 my ($self) = @_; 824 my ($self) = @_;
755 (sum @$hs), 859 (sum @$hs),
756 ) 860 )
757} 861}
758 862
759sub size_allocate { 863sub size_allocate {
760 my ($self, $x, $y, $w, $h) = @_; 864 my ($self, $w, $h) = @_;
761
762 $self->_size_allocate ($x, $y, $w, $h) or return;
763 865
764 my ($ws, $hs) = $self->get_wh; 866 my ($ws, $hs) = $self->get_wh;
765 867
766 my $req_w = sum @$ws; 868 my $req_w = sum @$ws;
767 my $req_h = sum @$hs; 869 my $req_h = sum @$hs;
789 891
790 for my $c (0 .. $#$row) { 892 for my $c (0 .. $#$row) {
791 my $col_w = $ws->[$c]; 893 my $col_w = $ws->[$c];
792 894
793 if (my $widget = $row->[$c]) { 895 if (my $widget = $row->[$c]) {
794 $widget->size_allocate ($x, $y, $col_w, $row_h); 896 $widget->configure ($x, $y, $col_w, $row_h);
795 } 897 }
796 898
797 $x += $col_w; 899 $x += $col_w;
798 } 900 }
799 901
844 (List::Util::max map $_->[1], @alloc), 946 (List::Util::max map $_->[1], @alloc),
845 ) 947 )
846} 948}
847 949
848sub size_allocate { 950sub size_allocate {
849 my ($self, $x, $y, $w, $h) = @_; 951 my ($self, $w, $h) = @_;
850
851 $self->_size_allocate ($x, $y, $w, $h);
852 952
853 ($h, $w) = ($w, $h); 953 ($h, $w) = ($w, $h);
854 954
855 my $children = $self->{children}; 955 my $children = $self->{children};
856 956
878 978
879 my $y = 0; 979 my $y = 0;
880 for (0 .. $#$children) { 980 for (0 .. $#$children) {
881 my $child = $children->[$_]; 981 my $child = $children->[$_];
882 my $h = $h[$_]; 982 my $h = $h[$_];
883 $child->size_allocate ($y, 0, $h, $w); 983 $child->configure ($y, 0, $h, $w);
884 984
885 $y += $h; 985 $y += $h;
886 } 986 }
987
988 1
887} 989}
888 990
889############################################################################# 991#############################################################################
890 992
891package CFClient::UI::VBox; 993package CFClient::UI::VBox;
904 (List::Util::sum map $_->[1], @alloc), 1006 (List::Util::sum map $_->[1], @alloc),
905 ) 1007 )
906} 1008}
907 1009
908sub size_allocate { 1010sub size_allocate {
909 my ($self, $x, $y, $w, $h) = @_; 1011 my ($self, $w, $h) = @_;
910
911 $self->_size_allocate ($x, $y, $w, $h);
912 1012
913 my $children = $self->{children}; 1013 my $children = $self->{children};
914 1014
915 my @h = map +($_->size_request)[1], @$children; 1015 my @h = map +($_->size_request)[1], @$children;
916 1016
934 1034
935 my $y = 0; 1035 my $y = 0;
936 for (0 .. $#$children) { 1036 for (0 .. $#$children) {
937 my $child = $children->[$_]; 1037 my $child = $children->[$_];
938 my $h = $h[$_]; 1038 my $h = $h[$_];
939 $child->size_allocate (0, $y, $w, $h); 1039 $child->configure (0, $y, $w, $h);
940 1040
941 $y += $h; 1041 $y += $h;
942 } 1042 }
1043
1044 1
943} 1045}
944 1046
945############################################################################# 1047#############################################################################
946 1048
947package CFClient::UI::Label; 1049package CFClient::UI::Label;
948 1050
949our @ISA = CFClient::UI::Base::; 1051our @ISA = CFClient::UI::Base::;
950 1052
951use SDL::OpenGL; 1053use CFClient::OpenGL;
952 1054
953sub new { 1055sub new {
954 my ($class, %arg) = @_; 1056 my ($class, %arg) = @_;
955 1057
956 my $self = $class->SUPER::new ( 1058 my $self = $class->SUPER::new (
957 fg => [1, 1, 1], 1059 fg => [1, 1, 1],
958 fontsize => $::FONTSIZE, 1060 fontsize => 1,
959 text => "", 1061 text => "",
960 align => -1, 1062 align => -1,
961 valign => -1, 1063 valign => -1,
962 padding => 2, 1064 padding => 2,
963 layout => new CFClient::Layout, 1065 layout => new CFClient::Layout,
964 %arg 1066 %arg
965 ); 1067 );
966 1068
967 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1069 if (exists $self->{template}) {
1070 my $layout = new CFClient::Layout;
1071 $layout->set_text (delete $self->{template});
1072 $self->{template} = $layout;
1073 }
968 1074
969 $self->set_text (delete $self->{text}) if exists $self->{text}; 1075 $self->set_text (delete $self->{text}) if exists $self->{text};
970 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1076 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
971 1077
972 $self 1078 $self
1002 1108
1003sub size_request { 1109sub size_request {
1004 my ($self) = @_; 1110 my ($self) = @_;
1005 1111
1006 $self->{layout}->set_width; 1112 $self->{layout}->set_width;
1007 $self->{layout}->set_height ($self->{fontsize}); 1113 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1008 1114
1009 my ($w, $h) = $self->{layout}->size; 1115 my ($w, $h) = $self->{layout}->size;
1116
1117 if (exists $self->{template}) {
1118 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1119
1120 my ($w2, $h2) = $self->{template}->size;
1121
1122 $w = List::Util::max $w, $w2;
1123 $h = List::Util::max $h, $h2;
1124 }
1010 1125
1011 ( 1126 (
1012 $w + $self->{padding} * 2, 1127 $w + $self->{padding} * 2,
1013 $h + $self->{padding} * 2, 1128 $h + $self->{padding} * 2,
1014 ) 1129 )
1015} 1130}
1016 1131
1017sub size_allocate { 1132sub size_allocate {
1018 my ($self, $x, $y, $w, $h) = @_; 1133 my ($self, $w, $h) = @_;
1019
1020 $self->_size_allocate ($x, $y, $w, $h) or return;
1021 1134
1022 delete $self->{texture}; 1135 delete $self->{texture};
1023} 1136}
1024 1137
1025sub update { 1138sub set_fontsize {
1026 my ($self) = @_; 1139 my ($self, $fontsize) = @_;
1027 1140
1028 delete $self->{texture}; 1141 $self->{fontsize} = $fontsize;
1029 $self->SUPER::update; 1142 $self->check_size;
1030} 1143}
1031 1144
1032sub _draw { 1145sub _draw {
1033 my ($self) = @_; 1146 my ($self) = @_;
1034 1147
1035 my $tex = $self->{texture} ||= do { 1148 my $tex = $self->{texture} ||= do {
1036 $self->{layout}->set_width ($self->{w}); 1149 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1150 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1038 new_from_layout CFClient::Texture $self->{layout} 1151 new_from_layout CFClient::Texture $self->{layout}
1039 }; 1152 };
1040 1153
1041 glEnable GL_BLEND; 1154 glEnable GL_BLEND;
1042 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1155 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1043 glEnable GL_TEXTURE_2D; 1156 glEnable GL_TEXTURE_2D;
1044 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1157 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1045 1158
1046 glColor @{$self->{fg}}; 1159 glColor @{$self->{fg}};
1047 1160
1048 my $x = 1161 $self->{ox} = int (
1049 $self->{align} < 0 ? $self->{padding} 1162 $self->{align} < 0 ? $self->{padding}
1050 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1163 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1051 : ($self->{w} - $tex->{w}) * 0.5; 1164 : ($self->{w} - $tex->{w}) * 0.5
1165 );
1052 1166
1053 my $y = 1167 $self->{oy} = int (
1054 $self->{valign} < 0 ? $self->{padding} 1168 $self->{valign} < 0 ? $self->{padding}
1055 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1169 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1056 : ($self->{h} - $tex->{h}) * 0.5; 1170 : ($self->{h} - $tex->{h}) * 0.5
1171 );
1057 1172
1058 $tex->draw_quad (int $x, int $y); 1173 $tex->draw_quad ($self->{ox}, $self->{oy});
1059 1174
1060 glDisable GL_TEXTURE_2D; 1175 glDisable GL_TEXTURE_2D;
1061 glDisable GL_BLEND; 1176 glDisable GL_BLEND;
1062} 1177}
1063 1178
1065 1180
1066package CFClient::UI::EntryBase; 1181package CFClient::UI::EntryBase;
1067 1182
1068our @ISA = CFClient::UI::Label::; 1183our @ISA = CFClient::UI::Label::;
1069 1184
1070use SDL; 1185use CFClient::OpenGL;
1071use SDL::OpenGL;
1072 1186
1073sub new { 1187sub new {
1074 my $class = shift; 1188 my $class = shift;
1075 1189
1076 $class->SUPER::new ( 1190 $class->SUPER::new (
1090 1204
1091 delete $self->{cur_h}; 1205 delete $self->{cur_h};
1092 1206
1093 return if $self->{text} eq $text; 1207 return if $self->{text} eq $text;
1094 1208
1209 delete $self->{texture};
1210
1095 $self->{last_activity} = $::NOW; 1211 $self->{last_activity} = $::NOW;
1096 $self->{text} = $text; 1212 $self->{text} = $text;
1097 1213
1098 $text =~ s/./*/g if $self->{hidden}; 1214 $text =~ s/./*/g if $self->{hidden};
1099 $self->{layout}->set_text ("$text "); 1215 $self->{layout}->set_text ("$text ");
1112 1228
1113 ($w + 1, $h) # add 1 for cursor 1229 ($w + 1, $h) # add 1 for cursor
1114} 1230}
1115 1231
1116sub size_allocate { 1232sub size_allocate {
1117 my ($self, $x, $y, $w, $h) = @_; 1233 my ($self, $w, $h) = @_;
1118
1119 $self->SUPER::size_allocate ($x, $y, $w, $h);
1120 1234
1121 $self->_set_text ($self->{text}); 1235 $self->_set_text ($self->{text});
1122} 1236}
1123 1237
1124sub set_text { 1238sub set_text {
1130} 1244}
1131 1245
1132sub key_down { 1246sub key_down {
1133 my ($self, $ev) = @_; 1247 my ($self, $ev) = @_;
1134 1248
1135 my $mod = $ev->key_mod; 1249 my $mod = $ev->{mod};
1136 my $sym = $ev->key_sym; 1250 my $sym = $ev->{sym};
1137
1138 my $uni = $ev->key_unicode; 1251 my $uni = $ev->{unicode};
1139 1252
1140 my $text = $self->get_text; 1253 my $text = $self->get_text;
1141 1254
1142 if ($sym == SDLK_BACKSPACE) { 1255 if ($sym == 8) {
1143 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1256 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1144 } elsif ($sym == SDLK_DELETE) { 1257 } elsif ($sym == 127) {
1145 substr $text, $self->{cursor}, 1, ""; 1258 substr $text, $self->{cursor}, 1, "";
1146 } elsif ($sym == SDLK_LEFT) { 1259 } elsif ($sym == CFClient::SDLK_LEFT) {
1147 --$self->{cursor} if $self->{cursor}; 1260 --$self->{cursor} if $self->{cursor};
1148 } elsif ($sym == SDLK_RIGHT) { 1261 } elsif ($sym == CFClient::SDLK_RIGHT) {
1149 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1262 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1150 } elsif ($sym == SDLK_HOME) { 1263 } elsif ($sym == CFClient::SDLK_HOME) {
1151 $self->{cursor} = 0; 1264 $self->{cursor} = 0;
1152 } elsif ($sym == SDLK_END) { 1265 } elsif ($sym == CFClient::SDLK_END) {
1153 $self->{cursor} = length $text; 1266 $self->{cursor} = length $text;
1154 } elsif ($sym == SDLK_ESCAPE) { 1267 } elsif ($sym == 27) {
1155 $self->emit ('escape'); 1268 $self->emit ('escape');
1156 } elsif ($uni) { 1269 } elsif ($uni) {
1157 substr $text, $self->{cursor}++, 0, chr $uni; 1270 substr $text, $self->{cursor}++, 0, chr $uni;
1158 } 1271 }
1159 1272
1224 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 1337 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1225 } 1338 }
1226 1339
1227 glColor @{$self->{fg}}; 1340 glColor @{$self->{fg}};
1228 glBegin GL_LINES; 1341 glBegin GL_LINES;
1229 glVertex $self->{cur_x}, $self->{cur_y};
1230 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1342 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1343 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1231 glEnd; 1344 glEnd;
1232 } 1345 }
1233} 1346}
1234 1347
1235package CFClient::UI::Entry; 1348package CFClient::UI::Entry;
1236 1349
1237our @ISA = CFClient::UI::EntryBase::; 1350our @ISA = CFClient::UI::EntryBase::;
1238 1351
1239use SDL; 1352use CFClient::OpenGL;
1240use SDL::OpenGL;
1241 1353
1242sub key_down { 1354sub key_down {
1243 my ($self, $ev) = @_; 1355 my ($self, $ev) = @_;
1244 1356
1245 my $sym = $ev->key_sym; 1357 my $sym = $ev->{sym};
1246 1358
1247 if ($sym == SDLK_RETURN) { 1359 if ($sym == 13) {
1248 $self->emit (activate => $self->get_text); 1360 $self->emit (activate => $self->get_text);
1249 $self->update; 1361 $self->update;
1250 1362
1251 } else { 1363 } else {
1252 $self->SUPER::key_down ($ev); 1364 $self->SUPER::key_down ($ev);
1258 1370
1259package CFClient::UI::Button; 1371package CFClient::UI::Button;
1260 1372
1261our @ISA = CFClient::UI::Label::; 1373our @ISA = CFClient::UI::Label::;
1262 1374
1263use SDL; 1375use CFClient::OpenGL;
1264use SDL::OpenGL;
1265 1376
1266my @tex = 1377my @tex =
1267 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1378 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1268 qw(b1_button_active.png); 1379 qw(b1_button_active.png);
1269 1380
1270sub new { 1381sub new {
1271 my $class = shift; 1382 my $class = shift;
1272 1383
1273 $class->SUPER::new ( 1384 $class->SUPER::new (
1274 padding => 4, 1385 padding => 4,
1275 fg => [1, 1, 1], 1386 fg => [1, 1, 1],
1276 bg => [1, 1, 1, 0.2], 1387 bg => [1, 1, 1, 0.2],
1277 active_fg => [1, 1, 0], 1388 active_fg => [0, 0, 1],
1278 can_hover => 1, 1389 can_hover => 1,
1390 align => 0,
1391 valign => 0,
1279 @_ 1392 @_
1280 ) 1393 )
1281} 1394}
1282 1395
1283sub button_up { 1396sub button_up {
1317package CFClient::UI::CheckBox; 1430package CFClient::UI::CheckBox;
1318 1431
1319our @ISA = CFClient::UI::DrawBG::; 1432our @ISA = CFClient::UI::DrawBG::;
1320 1433
1321my @tex = 1434my @tex =
1322 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1435 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1323 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1436 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1324 1437
1325use SDL; 1438use CFClient::OpenGL;
1326use SDL::OpenGL;
1327 1439
1328sub new { 1440sub new {
1329 my $class = shift; 1441 my $class = shift;
1330 1442
1331 $class->SUPER::new ( 1443 $class->SUPER::new (
1342 my ($self) = @_; 1454 my ($self) = @_;
1343 1455
1344 ($self->{padding} * 2 + 6) x 2 1456 ($self->{padding} * 2 + 6) x 2
1345} 1457}
1346 1458
1347sub size_allocate {
1348 my ($self, $x, $y, $w, $h) = @_;
1349
1350 $self->_size_allocate ($x, $y, $w, $h);
1351}
1352
1353sub button_down { 1459sub button_down {
1354 my ($self, $ev, $x, $y) = @_; 1460 my ($self, $ev, $x, $y) = @_;
1355 1461
1356 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1462 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1357 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1463 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1383 glDisable GL_BLEND; 1489 glDisable GL_BLEND;
1384} 1490}
1385 1491
1386############################################################################# 1492#############################################################################
1387 1493
1494package CFClient::UI::Image;
1495
1496our @ISA = CFClient::UI::Base::;
1497
1498use CFClient::OpenGL;
1499use Carp qw/confess/;
1500
1501our %loaded_images;
1502
1503sub new {
1504 my $class = shift;
1505
1506 my $self = $class->SUPER::new (@_);
1507
1508 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1509
1510 $loaded_images{$self->{image}} ||=
1511 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1512
1513 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1514
1515 Scalar::Util::weaken $loaded_images{$self->{image}};
1516
1517 $self->{aspect} = $tex->{w} / $tex->{h};
1518
1519 $self
1520}
1521
1522sub size_request {
1523 my ($self) = @_;
1524
1525 ($self->{tex}->{w}, $self->{tex}->{h})
1526}
1527
1528sub _draw {
1529 my ($self) = @_;
1530
1531 my $tex = $self->{tex};
1532
1533 my ($w, $h) = ($self->{w}, $self->{h});
1534
1535 if ($self->{rot90}) {
1536 glRotate 90, 0, 0, 1;
1537 glTranslate 0, -$self->{w}, 0;
1538
1539 ($w, $h) = ($h, $w);
1540 }
1541
1542 glEnable GL_BLEND;
1543 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1544 glEnable GL_TEXTURE_2D;
1545 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1546
1547 $tex->draw_quad (0, 0, $w, $h);
1548
1549 glDisable GL_BLEND;
1550 glDisable GL_TEXTURE_2D;
1551}
1552
1553#############################################################################
1554
1555package CFClient::UI::VGauge;
1556
1557our @ISA = CFClient::UI::Base::;
1558
1559use CFClient::OpenGL;
1560
1561my %tex = (
1562 food => [
1563 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1564 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1565 ],
1566 grace => [
1567 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1568 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1569 ],
1570 hp => [
1571 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1572 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1573 ],
1574 mana => [
1575 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1576 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1577 ],
1578);
1579
1580# eg. VGauge->new (gauge => 'food'), default gauge: food
1581sub new {
1582 my $class = shift;
1583
1584 my $self = $class->SUPER::new (
1585 type => 'food',
1586 @_
1587 );
1588
1589 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1590
1591 $self
1592}
1593
1594sub size_request {
1595 my ($self) = @_;
1596
1597 #my $tex = $tex{$self->{type}}[0];
1598 #@$tex{qw(w h)}
1599 (0, 0)
1600}
1601
1602sub set_max {
1603 my ($self, $max) = @_;
1604
1605 $self->{max_val} = $max;
1606}
1607
1608sub set_value {
1609 my ($self, $val, $max) = @_;
1610
1611 $self->set_max ($max)
1612 if defined $max;
1613
1614 $max = $self->{max_val};
1615 $self->{val} = $val;
1616
1617 $self->update;
1618}
1619
1620sub _draw {
1621 my ($self) = @_;
1622
1623 my $tex = $tex{$self->{type}};
1624
1625 my ($w, $h) = ($self->{w}, $self->{h});
1626
1627 if ($self->{vertical}) {
1628 glRotate 90, 0, 0, 1;
1629 glTranslate 0, -$self->{w}, 0;
1630
1631 ($w, $h) = ($h, $w);
1632 }
1633
1634 my $ycut = $self->{val} / ($self->{max_val} || 1);
1635 $ycut = 1 if $self->{val} > $self->{max_val};
1636
1637 my $t1 = $tex->[0];
1638 my $t2 = $tex->[1];
1639
1640 glEnable GL_BLEND;
1641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1642 glEnable GL_TEXTURE_2D;
1643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1644
1645 my $h1 = $self->{h} - $ycut * $self->{h};
1646 my $h2 = $ycut * $self->{h};
1647
1648 glBindTexture GL_TEXTURE_2D, $t1->{name};
1649 glBegin GL_QUADS;
1650 glTexCoord 0 , 0; glVertex 0 , 0;
1651 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1652 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1653 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1654 glEnd;
1655
1656 glBindTexture GL_TEXTURE_2D, $t2->{name};
1657 glBegin GL_QUADS;
1658 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1659 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1660 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1661 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1662 glEnd;
1663
1664 glDisable GL_BLEND;
1665 glDisable GL_TEXTURE_2D;
1666}
1667
1668#############################################################################
1669
1670package CFClient::UI::Gauge;
1671
1672our @ISA = CFClient::UI::VBox::;
1673
1674sub new {
1675 my ($class, %arg) = shift;
1676
1677 my $self = $class->SUPER::new (
1678 @_,
1679 );
1680
1681 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1682 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1683 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1684
1685 $self
1686}
1687
1688sub set_fontsize {
1689 my ($self, $fsize) = @_;
1690
1691 $self->{value}->set_fontsize ($fsize);
1692 $self->{max} ->set_fontsize ($fsize);
1693}
1694
1695sub set_value {
1696 my ($self, $val, $max) = @_;
1697
1698 $self->set_max ($max)
1699 if defined $max;
1700
1701 $self->{gauge}->set_value ($val, $max);
1702 $self->{value}->set_text ($val);
1703}
1704
1705sub set_max {
1706 my ($self, $max) = @_;
1707
1708 $self->{gauge}->set_max ($max);
1709 $self->{max}->set_text ($max);
1710}
1711
1712#############################################################################
1713
1388package CFClient::UI::Slider; 1714package CFClient::UI::Slider;
1389 1715
1390use strict; 1716use strict;
1391 1717
1392use SDL::OpenGL; 1718use CFClient::OpenGL;
1393 1719
1394our @ISA = CFClient::UI::DrawBG::; 1720our @ISA = CFClient::UI::DrawBG::;
1395 1721
1396my @tex = 1722my @tex =
1397 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1723 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1407 # TODO: calculations are off 1733 # TODO: calculations are off
1408 my $self = $class->SUPER::new ( 1734 my $self = $class->SUPER::new (
1409 fg => [1, 1, 1], 1735 fg => [1, 1, 1],
1410 active_fg => [0, 0, 0], 1736 active_fg => [0, 0, 0],
1411 range => [0, 0, 100, 10], 1737 range => [0, 0, 100, 10],
1412 req_w => 40, 1738 req_w => 20,
1413 req_h => 13, 1739 req_h => 20,
1414 vertical => 0, 1740 vertical => 0,
1415 can_hover => 1, 1741 can_hover => 1,
1416 inner_pad => 5, 1742 inner_pad => 5,
1417 @_ 1743 @_
1418 ); 1744 );
1522 1848
1523package CFClient::UI::TextView; 1849package CFClient::UI::TextView;
1524 1850
1525our @ISA = CFClient::UI::HBox::; 1851our @ISA = CFClient::UI::HBox::;
1526 1852
1527use SDL::OpenGL; 1853use CFClient::OpenGL;
1528 1854
1529sub new { 1855sub new {
1530 my $class = shift; 1856 my $class = shift;
1531 1857
1532 my $self = $class->SUPER::new ( 1858 my $self = $class->SUPER::new (
1533 req_w => $::WIDTH / 6,
1534 req_h => $::HEIGHT / 6,
1535 fontsize => $::FONTSIZE, 1859 fontsize => 1,
1536 @_, 1860 @_,
1537 1861
1538 layout => (new CFClient::Layout), 1862 layout => (new CFClient::Layout),
1539 par => [], 1863 par => [],
1540 height => 0, 1864 height => 0,
1561sub text_height { 1885sub text_height {
1562 my ($self, $text) = @_; 1886 my ($self, $text) = @_;
1563 1887
1564 my $layout = $self->{layout}; 1888 my $layout = $self->{layout};
1565 1889
1566 $layout->set_height ($self->{fontsize}); 1890 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1567 $layout->set_width ($self->{w}); 1891 $layout->set_width ($self->{w});
1568 $layout->set_text ($text); 1892 $layout->set_text ($text);
1569 1893
1570 ($layout->size)[1] 1894 ($layout->size)[1]
1571} 1895}
1575 1899
1576 $self->{need_reflow}++; 1900 $self->{need_reflow}++;
1577 $self->update; 1901 $self->update;
1578} 1902}
1579 1903
1580sub size_request {
1581 my ($self) = @_;
1582
1583 ($self->{req_w}, $self->{req_h})
1584}
1585
1586sub size_allocate { 1904sub size_allocate {
1587 my ($self, $x, $y, $w, $h) = @_; 1905 my ($self, $w, $h) = @_;
1588 1906
1589 $self->SUPER::size_allocate ($x, $y, $w, $h); 1907 $self->SUPER::size_allocate ($w, $h);
1590 1908
1591 $self->{layout}->set_height ($self->{fontsize}); 1909 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1592 $self->{layout}->set_width ($self->{w}); 1910 $self->{layout}->set_width ($self->{children}[0]{w});
1593 1911
1594 $self->reflow; 1912 $self->reflow;
1595} 1913}
1596 1914
1597sub add_paragraph { 1915sub add_paragraph {
1684 2002
1685} 2003}
1686 2004
1687############################################################################# 2005#############################################################################
1688 2006
1689package CFClient::UI::MapWidget;
1690
1691use strict;
1692
1693use List::Util qw(min max);
1694
1695use SDL;
1696use SDL::OpenGL;
1697
1698our @ISA = CFClient::UI::Base::;
1699
1700sub new {
1701 my $class = shift;
1702
1703 $class->SUPER::new (
1704 z => -1,
1705 can_focus => 1,
1706 list => (glGenLists 1),
1707 @_
1708 )
1709}
1710
1711sub key_down {
1712 print "MAPKEYDOWN\n";
1713}
1714
1715sub key_up {
1716}
1717
1718sub button_down {
1719 my ($self, $ev, $x, $y) = @_;
1720
1721 $self->focus_in;
1722
1723 if ($ev->button == 2) {
1724 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1725 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1726
1727 $self->{motion} = sub {
1728 my ($ev, $x, $y) = @_;
1729
1730 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1731
1732 $::CFG->{map_shift_x} = $bw + $x - $ox;
1733 $::CFG->{map_shift_y} = $bh + $y - $oy;
1734
1735 $self->update;
1736 };
1737 }
1738}
1739
1740sub button_up {
1741 my ($self, $ev, $x, $y) = @_;
1742
1743 delete $self->{motion};
1744}
1745
1746sub mouse_motion {
1747 my ($self, $ev, $x, $y) = @_;
1748
1749 $self->{motion}->($ev, $x, $y) if $self->{motion};
1750}
1751
1752sub size_request {
1753 (
1754 1 + 32 * int $::WIDTH / 32,
1755 1 + 32 * int $::HEIGHT / 32,
1756 )
1757}
1758
1759sub update {
1760 my ($self) = @_;
1761
1762 $self->{need_update} = 1;
1763 $self->SUPER::update;
1764}
1765
1766sub draw {
1767 my ($self) = @_;
1768
1769 if (delete $self->{need_update}) {
1770 glNewList $self->{list}, GL_COMPILE;
1771
1772 if ($::MAP) {
1773 my $sw = int $::WIDTH / 32;
1774 my $sh = int $::HEIGHT / 32;
1775
1776 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1777 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1778
1779 glTranslate $sx0 - 32, $sy0 - 32, 0;
1780
1781 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1782
1783 if ($::CFG->{fow_enable}) {
1784 if ($::CFG->{fow_smooth}) { # smooth fog of war
1785 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1786 glConvolutionFilter2D
1787 GL_CONVOLUTION_2D,
1788 GL_ALPHA,
1789 3, 3,
1790 GL_ALPHA, GL_FLOAT,
1791 pack "f*",
1792 0.1, 0.1, 0.1,
1793 0.1, 0.2, 0.1,
1794 0.1, 0.1, 0.1,
1795 ;
1796 glEnable GL_CONVOLUTION_2D;
1797 }
1798
1799 $self->{fow_texture} = new CFClient::Texture
1800 w => $w,
1801 h => $h,
1802 data => $data,
1803 internalformat => GL_ALPHA,
1804 format => GL_ALPHA;
1805
1806 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1807
1808 glEnable GL_BLEND;
1809 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1810 glEnable GL_TEXTURE_2D;
1811 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1812
1813 glColor +($::CFG->{fow_intensity}) x 3, 1;
1814 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1815
1816 glDisable GL_TEXTURE_2D;
1817 glDisable GL_BLEND;
1818 }
1819
1820 # HACK BEGIN
1821 {
1822 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1823 my ($w, $h) = (250, 250);
1824
1825 glEnable GL_BLEND;
1826 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1827 glEnable GL_TEXTURE_2D;
1828 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1829
1830 $self->{mapmap_texture} =
1831 new CFClient::Texture
1832 w => $w,
1833 h => $h,
1834 data => $::MAP->mapmap ($w, $h),
1835 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1836
1837 $self->{mapmap_texture}->draw_quad (100, 100);
1838
1839 glDisable GL_TEXTURE_2D;
1840 glDisable GL_BLEND;
1841 }
1842 # HACK END
1843 }
1844
1845 glEndList;
1846 }
1847
1848 glPushMatrix;
1849 glCallList $self->{list};
1850 glPopMatrix;
1851
1852 if ($FOCUS != $self) {
1853 glColor 64/255, 64/255, 64/255;
1854 glLogicOp GL_AND;
1855 glEnable GL_COLOR_LOGIC_OP;
1856 glBegin GL_QUADS;
1857 glVertex 0, 0;
1858 glVertex 0, $::HEIGHT;
1859 glVertex $::WIDTH, $::HEIGHT;
1860 glVertex $::WIDTH, 0;
1861 glEnd;
1862 glDisable GL_COLOR_LOGIC_OP;
1863 }
1864}
1865
1866my %DIR = (
1867 SDLK_KP8, [1, "north"],
1868 SDLK_KP9, [2, "northeast"],
1869 SDLK_KP6, [3, "east"],
1870 SDLK_KP3, [4, "southeast"],
1871 SDLK_KP2, [5, "south"],
1872 SDLK_KP1, [6, "southwest"],
1873 SDLK_KP4, [7, "west"],
1874 SDLK_KP7, [8, "northwest"],
1875
1876 SDLK_UP, [1, "north"],
1877 SDLK_RIGHT, [3, "east"],
1878 SDLK_DOWN, [5, "south"],
1879 SDLK_LEFT, [7, "west"],
1880);
1881
1882sub key_down {
1883 my ($self, $ev) = @_;
1884
1885 my $mod = $ev->key_mod;
1886 my $sym = $ev->key_sym;
1887
1888 if ($sym == SDLK_KP5) {
1889 $::CONN->user_send ("command stay fire");
1890 } elsif ($sym == SDLK_a) {
1891 $::CONN->user_send ("command apply");
1892 } elsif ($sym == SDLK_QUOTE) {
1893 $self->emit ('activate_console');
1894 } elsif ($sym == SDLK_SLASH) {
1895 $self->emit ('activate_console' => '/');
1896 } elsif (exists $DIR{$sym}) {
1897 if ($mod & KMOD_SHIFT) {
1898 $self->{shft}++;
1899 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1900 } elsif ($mod & KMOD_CTRL) {
1901 $self->{ctrl}++;
1902 $::CONN->user_send ("command run $DIR{$sym}[0]");
1903 } else {
1904 $::CONN->user_send ("command $DIR{$sym}[1]");
1905 }
1906 }
1907}
1908
1909sub key_up {
1910 my ($self, $ev) = @_;
1911
1912 my $mod = $ev->key_mod;
1913 my $sym = $ev->key_sym;
1914
1915 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1916 $::CONN->user_send ("command fire_stop");
1917 }
1918 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1919 $::CONN->user_send ("command run_stop");
1920 }
1921}
1922
1923#############################################################################
1924
1925package CFClient::UI::Animator; 2007package CFClient::UI::Animator;
1926 2008
1927use SDL::OpenGL; 2009use CFClient::OpenGL;
1928 2010
1929our @ISA = CFClient::UI::Bin::; 2011our @ISA = CFClient::UI::Bin::;
1930 2012
1931sub moveto { 2013sub moveto {
1932 my ($self, $x, $y) = @_; 2014 my ($self, $x, $y) = @_;
2005 2087
2006package CFClient::UI::Root; 2088package CFClient::UI::Root;
2007 2089
2008our @ISA = CFClient::UI::Container::; 2090our @ISA = CFClient::UI::Container::;
2009 2091
2010use SDL::OpenGL; 2092use CFClient::OpenGL;
2093
2094sub check_size {
2095 my ($self) = @_;
2096
2097 $self->configure (0, 0, $::WITH, $::HEIGHT);
2098}
2011 2099
2012sub size_request { 2100sub size_request {
2013 ($::WIDTH, $::HEIGHT) 2101 ($::WIDTH, $::HEIGHT)
2014} 2102}
2015 2103
2016sub size_allocate { 2104sub configure {
2017 my ($self, $x, $y, $w, $h) = @_; 2105 my ($self, $x, $y, $w, $h) = @_;
2018 2106
2019 $self->_size_allocate ($x, $y, $w, $h); 2107 $self->SUPER::configure ($x, $y, $w, $h);
2020 2108
2021 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2109 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2022 for @{$self->{children}}; 2110 for @{$self->{children}};
2023} 2111}
2024 2112
2025sub _topleft { 2113sub _topleft {
2026 my ($self, $x, $y) = @_; 2114 my ($self, $x, $y) = @_;
2029} 2117}
2030 2118
2031sub update { 2119sub update {
2032 my ($self) = @_; 2120 my ($self) = @_;
2033 2121
2034 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2122 $self->check_size;
2035 ::refresh (); 2123 ::refresh ();
2036} 2124}
2037 2125
2038sub add { 2126sub add {
2039 my ($self, $widget) = @_; 2127 my ($self, $child) = @_;
2040 2128
2129 # integerize window positions
2130 $child->{x} = int $child->{x};
2131 $child->{y} = int $child->{y};
2132
2041 $self->SUPER::add ($widget); 2133 $self->SUPER::add ($child);
2042
2043 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2044} 2134}
2045 2135
2046sub on_refresh { 2136sub on_refresh {
2047 my ($self, $id, $cb) = @_; 2137 my ($self, $id, $cb) = @_;
2048 2138

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines