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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines