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.127 by root, Mon Apr 17 21:03:31 2006 UTC vs.
Revision 1.150 by elmex, Sun Apr 23 00:08:29 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, 101 can_events => 1,
102 h => -1,
103 @_ 102 @_
104 }, $class; 103 }, $class;
105 104
106 for (keys %$self) { 105 for (keys %$self) {
107 if (/^connect_(.*)$/) { 106 if (/^connect_(.*)$/) {
110 } 109 }
111 110
112 $self 111 $self
113} 112}
114 113
114sub show {
115 my ($self) = @_;
116
117 return if $self->{parent};
118
119 $CFClient::UI::ROOT->add ($self);
120}
121
122sub hide {
123 my ($self) = @_;
124
125 return unless $self->{parent};
126
127 $self->{parent}->remove ($self);
128}
129
115sub move { 130sub move {
116 my ($self, $x, $y, $z) = @_; 131 my ($self, $x, $y, $z) = @_;
132
117 $self->{x} = int $x; 133 $self->{x} = int $x;
118 $self->{y} = int $y; 134 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 135 $self->{z} = $z if defined $z;
136
137 $self->update;
120} 138}
121 139
122sub needs_redraw { 140sub needs_redraw {
123 0 141 0
124} 142}
125 143
126sub size_request { 144sub size_request {
127 require Carp; 145 require Carp;
128 Carp::confess "size_request is abtract"; 146 Carp::confess "size_request is abstract";
129} 147}
130 148
131sub _size_allocate { 149sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 150 my ($self, $x, $y, $w, $h) = @_;
133 151
152 if ($self->{aspect}) {
153 my $w2 = List::Util::min $w, int $h * $self->{aspect};
154 my $h2 = List::Util::min $h, int $w / $self->{aspect};
155
156 # use alignment to adjust x, y
157
158 $x += int +($w - $w2) * 0.5;
159 $y += int +($h - $h2) * 0.5;
160
161 ($w, $h) = ($w2, $h2);
162 }
163
164 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 165 $self->{x} = $x;
135 $self->{y} = $y; 166 $self->{y} = $y;
167 $self->update;
168 }
136 169
137 return unless $self->{w} != $w || $self->{h} != $h; 170 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 171 $self->{w} = $w;
140 $self->{h} = $h; 172 $self->{h} = $h;
141 173
174 $self->size_allocate ($w, $h);
175 $self->update;
142 1 176 }
143} 177}
144 178
145sub size_allocate { 179sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 180 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 181}
150 182
151# return top left coordinates 183# return top left coordinates
152sub _topleft { 184sub _topleft {
153 my ($self, $x, $y) = @_; 185 my ($self, $x, $y) = @_;
247} 279}
248 280
249sub find_widget { 281sub find_widget {
250 my ($self, $x, $y) = @_; 282 my ($self, $x, $y) = @_;
251 283
284 return () unless $self->{can_events};
285
252 return $self 286 return $self
253 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 287 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
254 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 288 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
255 289
256 () 290 ()
263} 297}
264 298
265sub check_size { 299sub check_size {
266 my ($self) = @_; 300 my ($self) = @_;
267 301
302 return unless $self->{parent};
303
268 my ($w, $h) = $self->size_request; 304 my ($w, $h) = $self->size_request;
269 305
270 if ($w != $self->{req_w} || $h != $self->{req_h}) { 306 if ($w != $self->{req_w} || $h != $self->{req_h}) {
271 $self->{req_w} = $w; 307 $self->{req_w} = $w;
272 $self->{req_h} = $h; 308 $self->{req_h} = $h;
273 309
274 $self->{parent}->check_size 310 $self->{parent}->check_size;
275 if $self->{parent};
276 } 311 }
277} 312}
278 313
279sub update { 314sub update {
280 my ($self) = @_; 315 my ($self) = @_;
284} 319}
285 320
286sub connect { 321sub connect {
287 my ($self, $signal, $cb) = @_; 322 my ($self, $signal, $cb) = @_;
288 323
289 push @{ $self->{cb}{$signal} }, $cb; 324 push @{ $self->{signal_cb}{$signal} }, $cb;
290} 325}
291 326
292sub emit { 327sub emit {
293 my ($self, $signal, @args) = @_; 328 my ($self, $signal, @args) = @_;
294 329
330 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
295 $_->($self, @args) 331 $cb->($self, @args);
296 for @{$self->{cb}{$signal} || []}; 332 }
297} 333}
298 334
299sub DESTROY { 335sub DESTROY {
300 my ($self) = @_; 336 my ($self) = @_;
301 337
307package CFClient::UI::DrawBG; 343package CFClient::UI::DrawBG;
308 344
309our @ISA = CFClient::UI::Base::; 345our @ISA = CFClient::UI::Base::;
310 346
311use strict; 347use strict;
312use SDL::OpenGL; 348use CFClient::OpenGL;
313 349
314sub new { 350sub new {
315 my $class = shift; 351 my $class = shift;
316 352
317 # range [value, low, high, page] 353 # range [value, low, high, page]
346 382
347package CFClient::UI::Empty; 383package CFClient::UI::Empty;
348 384
349our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
350 386
387sub new {
388 my ($class, %arg) = @_;
389 $class->SUPER::new (can_events => 0, %arg);
390}
391
351sub size_request { 392sub size_request {
352 (0, 0) 393 (0, 0)
353} 394}
354 395
355sub draw { } 396sub draw { }
363sub new { 404sub new {
364 my ($class, %arg) = @_; 405 my ($class, %arg) = @_;
365 406
366 my $children = delete $arg{children} || []; 407 my $children = delete $arg{children} || [];
367 408
368 my $self = $class->SUPER::new (children => [], %arg); 409 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
369 $self->add ($_) for @$children; 410 $self->add ($_) for @$children;
370 411
371 $self 412 $self
372} 413}
373 414
381 $self->{children} = [ 422 $self->{children} = [
382 sort { $a->{z} <=> $b->{z} } 423 sort { $a->{z} <=> $b->{z} }
383 @{$self->{children}}, $child 424 @{$self->{children}}, $child
384 ]; 425 ];
385 426
386 $self->{w} = $self->{h} = -1;
387
388 $child->check_size; 427 $child->check_size;
389} 428}
390 429
391sub remove { 430sub remove {
392 my ($self, $widget) = @_; 431 my ($self, $child) = @_;
393 432
433 delete $child->{parent};
434
394 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 435 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
395 436
396 $self->check_size; 437 $self->check_size;
397} 438}
398 439
399sub find_widget { 440sub find_widget {
431 472
432 $class->SUPER::new (children => [$child], %arg) 473 $class->SUPER::new (children => [$child], %arg)
433} 474}
434 475
435sub add { 476sub add {
436 my ($self, $widget) = @_; 477 my ($self, $child) = @_;
437 478
438 $self->{children} = []; 479 $self->{children} = [];
439 480
440 $self->SUPER::add ($widget); 481 $self->SUPER::add ($child);
441} 482}
442 483
443sub remove { 484sub remove {
444 my ($self, $widget) = @_; 485 my ($self, $widget) = @_;
445 486
454sub size_request { 495sub size_request {
455 $_[0]{children}[0]->size_request 496 $_[0]{children}[0]->size_request
456} 497}
457 498
458sub size_allocate { 499sub size_allocate {
459 my ($self, $x, $y, $w, $h) = @_; 500 my ($self, $w, $h) = @_;
460 501
461 $self->_size_allocate ($x, $y, $w, $h) or return;
462
463 $self->{children}[0]->size_allocate (0, 0, $w, $h); 502 $self->{children}[0]->configure (0, 0, $w, $h);
464} 503}
465 504
466############################################################################# 505#############################################################################
467 506
468package CFClient::UI::Window; 507package CFClient::UI::Window;
469 508
470our @ISA = CFClient::UI::Bin::; 509our @ISA = CFClient::UI::Bin::;
471 510
472use SDL::OpenGL; 511use CFClient::OpenGL;
473 512
474sub new { 513sub new {
475 my ($class, %arg) = @_; 514 my ($class, %arg) = @_;
476 515
477 my $self = $class->SUPER::new (%arg); 516 my $self = $class->SUPER::new (%arg);
494 $self->child->draw; 533 $self->child->draw;
495 }; 534 };
496} 535}
497 536
498sub size_allocate { 537sub size_allocate {
499 my ($self, $x, $y, $w, $h) = @_; 538 my ($self, $w, $h) = @_;
500 539
501 $self->_size_allocate ($x, $y, $w, $h) or return;
502
503 $self->child->size_allocate (0, 0, $w, $h); 540 $self->child->configure (0, 0, $w, $h);
504 541
505 $self->render_chld; 542 $self->render_chld;
506} 543}
507 544
508sub _draw { 545sub _draw {
533sub new { die } 570sub new { die }
534 571
535sub size_request { 572sub size_request {
536 my ($self) = @_; 573 my ($self) = @_;
537 574
538 @$self{qw(child_w child_h)} = $self->child->size_request; 575 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
539 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 576 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
540 577
541 @$self{qw(child_w child_h)} 578 @$self{qw(child_w child_h)}
542} 579}
543 580
544sub size_allocate {
545 my ($self, $x, $y, $w, $h) = @_;
546
547 $self->_size_allocate ($x, $y, $w, $h) or return;
548}
549
550sub _draw { 581sub _draw {
551 my ($self) = @_; 582 my ($self) = @_;
552 583
553 $self->{children}[1]->draw; 584 $self->{children}[1]->draw;
554} 585}
558 589
559package CFClient::UI::Frame; 590package CFClient::UI::Frame;
560 591
561our @ISA = CFClient::UI::Bin::; 592our @ISA = CFClient::UI::Bin::;
562 593
563use SDL::OpenGL; 594use CFClient::OpenGL;
564
565sub size_request {
566 my ($self) = @_;
567 my $chld = $self->child
568 or return (0, 0);
569
570 $chld->move (2, 2);
571
572 map { $_ + 4 } $chld->size_request;
573}
574
575sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_;
577
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
581}
582
583sub _draw {
584 my ($self) = @_;
585
586 my $chld = $self->child;
587
588 my ($w, $h) = $chld->size_request;
589
590 glBegin GL_QUADS;
591 glColor 0, 0, 0;
592 glVertex 0 , 0;
593 glVertex 0 , $h + 4;
594 glVertex $w + 4 , $h + 4;
595 glVertex $w + 4 , 0;
596 glEnd;
597
598 $chld->draw;
599}
600
601#############################################################################
602
603package CFClient::UI::FancyFrame;
604
605our @ISA = CFClient::UI::Bin::;
606
607use SDL::OpenGL;
608
609my @tex =
610 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
611 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
612 595
613sub new { 596sub new {
614 my $class = shift; 597 my $class = shift;
615 598
616 # TODO: user_x, user_y, overwrite moveto?
617
618 $class->SUPER::new ( 599 my $self = $class->SUPER::new (
619 bg => [1, 1, 1, 1], 600 bg => [1, 1, 1, 1],
620 border_bg => [1, 1, 1, 1], 601 border_bg => [1, 1, 1, 1],
621 border => int $::FONTSIZE * 0.8, 602 border => 0.8,
622 @_ 603 @_
604 );
605
606 $self
607}
608
609sub set_size {
610 my ($self, $w, $h) = @_;
611 $self->{req_w} = $w;
612 $self->{req_h} = $h;
613 $self->check_size;
614}
615
616sub size_request {
617 my ($self) = @_;
618 ($self->{req_w}, $self->{req_h})
619}
620
621sub size_allocate {
622 my ($self, $w, $h) = @_;
623 $self->{w} = $w;
624 $self->{h} = $h;
625 $self->child->configure (0, 0, $w, $h);
626}
627
628sub _draw {
629 my ($self) = @_;
630
631 my ($w, $h) = ($self->{w}, $self->{h});
632
633 glEnable GL_BLEND;
634 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
635 glEnable GL_TEXTURE_2D;
636 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
637
638# glBegin GL_QUADS;
639# glColor 0, 0, 0, 0;
640# glVertex 0 , 0;
641# glVertex 0 , $h;
642# glVertex $w, $h;
643# glVertex $w, 0;
644# glEnd;
645
646
647 $self->child->draw;
648 glDisable GL_BLEND;
649 glDisable GL_TEXTURE_2D;
650}
651
652#############################################################################
653
654package CFClient::UI::FancyFrame;
655
656our @ISA = CFClient::UI::Bin::;
657
658use CFClient::OpenGL;
659
660my @tex =
661 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
662 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
663
664sub new {
665 my $class = shift;
666
667 # TODO: user_x, user_y, overwrite moveto?
668
669 my $self = $class->SUPER::new (
670 bg => [1, 1, 1, 1],
671 border_bg => [1, 1, 1, 1],
672 border => 0.8,
673 can_events => 1,
674 @_
675 );
676
677 $self->{title} &&= new CFClient::UI::Label
678 align => 0,
679 valign => 1,
680 text => $self->{title},
681 fontsize => 1;
682
683 $self
684}
685
686sub border {
687 int $_[0]{border} * $::FONTSIZE
688}
689
690sub size_request {
691 my ($self) = @_;
692
693 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
694
695 my ($w, $h) = $self->SUPER::size_request;
696
697 (
698 $w + $self->border * 2,
699 $h + $self->border * 2,
623 ) 700 )
624} 701}
625 702
626sub size_request {
627 my ($self) = @_;
628
629 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
630
631 my ($w, $h) = $self->SUPER::size_request;
632
633 (
634 $w + $self->{border} * 2,
635 $h + $self->{border} * 2,
636 )
637}
638
639sub size_allocate { 703sub size_allocate {
640 my ($self, $x, $y, $w, $h) = @_; 704 my ($self, $w, $h) = @_;
641 705
642 $self->_size_allocate ($x, $y, $w, $h) or return;
643
644 $h -= List::Util::max 0, $self->{border} * 2; 706 $h -= List::Util::max 0, $self->border * 2;
645 $w -= List::Util::max 0, $self->{border} * 2; 707 $w -= List::Util::max 0, $self->border * 2;
646 708
709 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
710 if $self->{title};
711
647 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 712 $self->child->configure ($self->border, $self->border, $w, $h);
648} 713}
649 714
650sub button_down { 715sub button_down {
651 my ($self, $ev, $x, $y) = @_; 716 my ($self, $ev, $x, $y) = @_;
652 717
718 my $border = $self->border;
719
653 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 720 if ($x < $self->{w} && $x >= $self->{w} - $border
654 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 721 && $y < $self->{h} && $y >= $self->{h} - $border) {
655 722
656 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 723 my ($ox, $oy) = ($ev->{x}, $ev->{y});
657 my ($bw, $bh) = ($self->{w}, $self->{h}); 724 my ($bw, $bh) = ($self->{w}, $self->{h});
658 725
659 $self->{motion} = sub { 726 $self->{motion} = sub {
660 my ($ev, $x, $y) = @_; 727 my ($ev, $x, $y) = @_;
661 728
662 ($x, $y) = ($ev->motion_x, $ev->motion_y); 729 ($x, $y) = ($ev->{x}, $ev->{y});
663 730
664 $self->{user_w} = $bw + $x - $ox; 731 $self->{user_w} = $bw + $x - $ox;
665 $self->{user_h} = $bh + $y - $oy; 732 $self->{user_h} = $bh + $y - $oy;
666 $self->update; 733 $self->check_size;
667 }; 734 };
668 735
669 } elsif ($x >= 0 && $x < $self->{w} 736 } elsif ($x >= 0 && $x < $self->{w}
670 && $y >= 0 && $y < $self->{border}) { 737 && $y >= 0 && $y < $border) {
671 738
672 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 739 my ($ox, $oy) = ($ev->{x}, $ev->{y});
673 my ($bx, $by) = ($self->{x}, $self->{y}); 740 my ($bx, $by) = ($self->{x}, $self->{y});
674 741
675 $self->{motion} = sub { 742 $self->{motion} = sub {
676 my ($ev, $x, $y) = @_; 743 my ($ev, $x, $y) = @_;
677 744
678 ($x, $y) = ($ev->motion_x, $ev->motion_y); 745 ($x, $y) = ($ev->{x}, $ev->{y});
679 746
680 $self->move ($bx + $x - $ox, $by + $y - $oy); 747 $self->move ($bx + $x - $ox, $by + $y - $oy);
681 $self->update; 748 $self->update;
682 }; 749 };
683 } 750 }
704 glEnable GL_BLEND; 771 glEnable GL_BLEND;
705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 772 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
706 glEnable GL_TEXTURE_2D; 773 glEnable GL_TEXTURE_2D;
707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 774 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
708 775
776 my $border = $self->border;
777
709 glColor @{ $self->{border_bg} }; 778 glColor @{ $self->{border_bg} };
710 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 779 $tex[1]->draw_quad (0, 0, $w, $border);
711 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 780 $tex[3]->draw_quad (0, $border, $border, $ch);
712 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 781 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
713 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 782 $tex[4]->draw_quad (0, $h - $border, $w, $border);
714 783
715 my $bg = $tex[0]; 784 my $bg = $tex[0];
716 785
717 # TODO: repeat texture not scale 786 # TODO: repeat texture not scale
718 my $rep_x = $cw / $bg->{w}; 787 my $rep_x = $cw / $bg->{w};
721 glColor @{ $self->{bg} }; 790 glColor @{ $self->{bg} };
722 791
723 $bg->{s} = $rep_x; 792 $bg->{s} = $rep_x;
724 $bg->{t} = $rep_y; 793 $bg->{t} = $rep_y;
725 $bg->{wrap_mode} = 1; 794 $bg->{wrap_mode} = 1;
726 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 795 $bg->draw_quad ($border, $border, $cw, $ch);
727 796
728 glDisable GL_TEXTURE_2D; 797 glDisable GL_TEXTURE_2D;
729 glDisable GL_BLEND; 798 glDisable GL_BLEND;
730 799
800 $self->{title}->draw if $self->{title};
731 $self->child->draw; 801 $self->child->draw;
732} 802}
733 803
734############################################################################# 804#############################################################################
735 805
737 807
738our @ISA = CFClient::UI::Base::; 808our @ISA = CFClient::UI::Base::;
739 809
740use List::Util qw(max sum); 810use List::Util qw(max sum);
741 811
742use SDL::OpenGL; 812use CFClient::OpenGL;
743 813
744sub new { 814sub new {
745 my $class = shift; 815 my $class = shift;
746 816
747 $class->SUPER::new ( 817 $class->SUPER::new (
754 my ($self, $x, $y, $child) = @_; 824 my ($self, $x, $y, $child) = @_;
755 825
756 $child->set_parent ($self); 826 $child->set_parent ($self);
757 $self->{children}[$y][$x] = $child; 827 $self->{children}[$y][$x] = $child;
758 828
759 $self->{w} = $self->{h} = -1;
760
761 $child->check_size; 829 $child->check_size;
762} 830}
763 831
764# TODO: move to container class maybe? send childs a signal on removal? 832# TODO: move to container class maybe? send childs a signal on removal?
765sub clear { 833sub clear {
779 or next; 847 or next;
780 848
781 for my $x (0 .. $#$row) { 849 for my $x (0 .. $#$row) {
782 my $widget = $row->[$x] 850 my $widget = $row->[$x]
783 or next; 851 or next;
784 my ($w, $h) = $widget->size_request; 852 my ($w, $h) = @$widget{qw(req_w req_h)};
785 853
786 $w[$x] = max $w[$x], $w; 854 $w[$x] = max $w[$x], $w;
787 $h[$y] = max $h[$y], $h; 855 $h[$y] = max $h[$y], $h;
788 } 856 }
789 } 857 }
801 (sum @$hs), 869 (sum @$hs),
802 ) 870 )
803} 871}
804 872
805sub size_allocate { 873sub size_allocate {
806 my ($self, $x, $y, $w, $h) = @_; 874 my ($self, $w, $h) = @_;
807
808 $self->_size_allocate ($x, $y, $w, $h) or return;
809 875
810 my ($ws, $hs) = $self->get_wh; 876 my ($ws, $hs) = $self->get_wh;
811 877
812 my $req_w = sum @$ws; 878 my $req_w = sum @$ws;
813 my $req_h = sum @$hs; 879 my $req_h = sum @$hs;
835 901
836 for my $c (0 .. $#$row) { 902 for my $c (0 .. $#$row) {
837 my $col_w = $ws->[$c]; 903 my $col_w = $ws->[$c];
838 904
839 if (my $widget = $row->[$c]) { 905 if (my $widget = $row->[$c]) {
840 $widget->size_allocate ($x, $y, $col_w, $row_h); 906 $widget->configure ($x, $y, $col_w, $row_h);
841 } 907 }
842 908
843 $x += $col_w; 909 $x += $col_w;
844 } 910 }
845 911
890 (List::Util::max map $_->[1], @alloc), 956 (List::Util::max map $_->[1], @alloc),
891 ) 957 )
892} 958}
893 959
894sub size_allocate { 960sub size_allocate {
895 my ($self, $x, $y, $w, $h) = @_; 961 my ($self, $w, $h) = @_;
896
897 $self->_size_allocate ($x, $y, $w, $h) or return;
898 962
899 ($h, $w) = ($w, $h); 963 ($h, $w) = ($w, $h);
900 964
901 my $children = $self->{children}; 965 my $children = $self->{children};
902 966
903 my @h = map +($_->size_request)[0], @$children; 967 my @h = map $_->{req_w}, @$children;
904 968
905 my $req_h = List::Util::sum @h; 969 my $req_h = List::Util::sum @h;
906 970
907 if ($req_h > $h) { 971 if ($req_h > $h) {
908 # ah well, not enough space 972 # ah well, not enough space
924 988
925 my $y = 0; 989 my $y = 0;
926 for (0 .. $#$children) { 990 for (0 .. $#$children) {
927 my $child = $children->[$_]; 991 my $child = $children->[$_];
928 my $h = $h[$_]; 992 my $h = $h[$_];
929 $child->size_allocate ($y, 0, $h, $w); 993 $child->configure ($y, 0, $h, $w);
930 994
931 $y += $h; 995 $y += $h;
932 } 996 }
933 997
934 1 998 1
952 (List::Util::sum map $_->[1], @alloc), 1016 (List::Util::sum map $_->[1], @alloc),
953 ) 1017 )
954} 1018}
955 1019
956sub size_allocate { 1020sub size_allocate {
957 my ($self, $x, $y, $w, $h) = @_; 1021 my ($self, $w, $h) = @_;
958
959 $self->_size_allocate ($x, $y, $w, $h) or return;
960 1022
961 my $children = $self->{children}; 1023 my $children = $self->{children};
962 1024
963 my @h = map +($_->size_request)[1], @$children; 1025 my @h = map $_->{req_h}, @$children;
964 1026
965 my $req_h = List::Util::sum @h; 1027 my $req_h = List::Util::sum @h;
966 1028
967 if ($req_h > $h) { 1029 if ($req_h > $h) {
968 # ah well, not enough space 1030 # ah well, not enough space
982 1044
983 my $y = 0; 1045 my $y = 0;
984 for (0 .. $#$children) { 1046 for (0 .. $#$children) {
985 my $child = $children->[$_]; 1047 my $child = $children->[$_];
986 my $h = $h[$_]; 1048 my $h = $h[$_];
987 $child->size_allocate (0, $y, $w, $h); 1049 $child->configure (0, $y, $w, $h);
988 1050
989 $y += $h; 1051 $y += $h;
990 } 1052 }
991 1053
992 1 1054 1
996 1058
997package CFClient::UI::Label; 1059package CFClient::UI::Label;
998 1060
999our @ISA = CFClient::UI::Base::; 1061our @ISA = CFClient::UI::Base::;
1000 1062
1001use SDL::OpenGL; 1063use CFClient::OpenGL;
1002 1064
1003sub new { 1065sub new {
1004 my ($class, %arg) = @_; 1066 my ($class, %arg) = @_;
1005 1067
1006 my $self = $class->SUPER::new ( 1068 my $self = $class->SUPER::new (
1007 fg => [1, 1, 1], 1069 fg => [1, 1, 1],
1008 fontsize => $::FONTSIZE, 1070 fontsize => 1,
1009 text => "", 1071 text => "",
1010 align => -1, 1072 align => -1,
1011 valign => -1, 1073 valign => -1,
1012 padding => 2, 1074 padding => 2,
1013 layout => new CFClient::Layout, 1075 layout => new CFClient::Layout,
1076 can_events => 0,
1014 %arg 1077 %arg
1015 ); 1078 );
1016 1079
1017 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1080 if (exists $self->{template}) {
1081 my $layout = new CFClient::Layout;
1082 $layout->set_text (delete $self->{template});
1083 $self->{template} = $layout;
1084 }
1018 1085
1019 $self->set_text (delete $self->{text}) if exists $self->{text}; 1086 $self->set_text (delete $self->{text}) if exists $self->{text};
1020 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1087 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1021 1088
1022 $self 1089 $self
1036 my ($self, $text) = @_; 1103 my ($self, $text) = @_;
1037 1104
1038 $self->{layout}->set_text ($text); 1105 $self->{layout}->set_text ($text);
1039 1106
1040 delete $self->{texture}; 1107 delete $self->{texture};
1108 $self->check_size;
1041 $self->update; 1109 $self->update;
1042} 1110}
1043 1111
1044sub set_markup { 1112sub set_markup {
1045 my ($self, $markup) = @_; 1113 my ($self, $markup) = @_;
1046 1114
1047 $self->{layout}->set_markup ($markup); 1115 $self->{layout}->set_markup ($markup);
1048 1116
1049 delete $self->{texture}; 1117 delete $self->{texture};
1118 $self->check_size;
1050 $self->update; 1119 $self->update;
1051} 1120}
1052 1121
1053sub size_request { 1122sub size_request {
1054 my ($self) = @_; 1123 my ($self) = @_;
1055 1124
1056 $self->{layout}->set_width; 1125 $self->{layout}->set_width;
1057 $self->{layout}->set_height ($self->{fontsize}); 1126 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1058 1127
1059 my ($w, $h) = $self->{layout}->size; 1128 my ($w, $h) = $self->{layout}->size;
1129
1130 if (exists $self->{template}) {
1131 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1132
1133 my ($w2, $h2) = $self->{template}->size;
1134
1135 $w = List::Util::max $w, $w2;
1136 $h = List::Util::max $h, $h2;
1137 }
1060 1138
1061 ( 1139 (
1062 $w + $self->{padding} * 2, 1140 $w + $self->{padding} * 2,
1063 $h + $self->{padding} * 2, 1141 $h + $self->{padding} * 2,
1064 ) 1142 )
1065} 1143}
1066 1144
1067sub size_allocate { 1145sub size_allocate {
1068 my ($self, $x, $y, $w, $h) = @_; 1146 my ($self, $w, $h) = @_;
1069
1070 $self->_size_allocate ($x, $y, $w, $h) or return;
1071 1147
1072 delete $self->{texture}; 1148 delete $self->{texture};
1073} 1149}
1074 1150
1075sub update { 1151sub set_fontsize {
1076 my ($self) = @_; 1152 my ($self, $fontsize) = @_;
1077 1153
1078 delete $self->{texture}; 1154 $self->{fontsize} = $fontsize;
1079 $self->SUPER::update; 1155 $self->check_size;
1080} 1156}
1081 1157
1082sub _draw { 1158sub _draw {
1083 my ($self) = @_; 1159 my ($self) = @_;
1084 1160
1085 my $tex = $self->{texture} ||= do { 1161 my $tex = $self->{texture} ||= do {
1086 $self->{layout}->set_width ($self->{w}); 1162 $self->{layout}->set_width ($self->{w});
1087 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1163 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1088 new_from_layout CFClient::Texture $self->{layout} 1164 new_from_layout CFClient::Texture $self->{layout}
1089 }; 1165 };
1090 1166
1091 glEnable GL_BLEND; 1167 glEnable GL_BLEND;
1092 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1168 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1117 1193
1118package CFClient::UI::EntryBase; 1194package CFClient::UI::EntryBase;
1119 1195
1120our @ISA = CFClient::UI::Label::; 1196our @ISA = CFClient::UI::Label::;
1121 1197
1122use SDL; 1198use CFClient::OpenGL;
1123use SDL::OpenGL;
1124 1199
1125sub new { 1200sub new {
1126 my $class = shift; 1201 my $class = shift;
1127 1202
1128 $class->SUPER::new ( 1203 $class->SUPER::new (
1131 active_bg => [1, 1, 1, 0.5], 1206 active_bg => [1, 1, 1, 0.5],
1132 active_fg => [0, 0, 0], 1207 active_fg => [0, 0, 0],
1133 can_hover => 1, 1208 can_hover => 1,
1134 can_focus => 1, 1209 can_focus => 1,
1135 valign => 0, 1210 valign => 0,
1211 can_events => 1,
1136 @_ 1212 @_
1137 ) 1213 )
1138} 1214}
1139 1215
1140sub _set_text { 1216sub _set_text {
1142 1218
1143 delete $self->{cur_h}; 1219 delete $self->{cur_h};
1144 1220
1145 return if $self->{text} eq $text; 1221 return if $self->{text} eq $text;
1146 1222
1223 delete $self->{texture};
1224
1147 $self->{last_activity} = $::NOW; 1225 $self->{last_activity} = $::NOW;
1148 $self->{text} = $text; 1226 $self->{text} = $text;
1149 1227
1150 $text =~ s/./*/g if $self->{hidden}; 1228 $text =~ s/./*/g if $self->{hidden};
1151 $self->{layout}->set_text ("$text "); 1229 $self->{layout}->set_text ("$text ");
1164 1242
1165 ($w + 1, $h) # add 1 for cursor 1243 ($w + 1, $h) # add 1 for cursor
1166} 1244}
1167 1245
1168sub size_allocate { 1246sub size_allocate {
1169 my ($self, $x, $y, $w, $h) = @_; 1247 my ($self, $w, $h) = @_;
1170 1248
1171 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1249 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1172
1173 $self->_set_text ($self->{text});
1174} 1250}
1175 1251
1176sub set_text { 1252sub set_text {
1177 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1178 1254
1182} 1258}
1183 1259
1184sub key_down { 1260sub key_down {
1185 my ($self, $ev) = @_; 1261 my ($self, $ev) = @_;
1186 1262
1187 my $mod = $ev->key_mod; 1263 my $mod = $ev->{mod};
1188 my $sym = $ev->key_sym; 1264 my $sym = $ev->{sym};
1189
1190 my $uni = $ev->key_unicode; 1265 my $uni = $ev->{unicode};
1191 1266
1192 my $text = $self->get_text; 1267 my $text = $self->get_text;
1193 1268
1194 if ($sym == SDLK_BACKSPACE) { 1269 if ($sym == 8) {
1195 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1270 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1196 } elsif ($sym == SDLK_DELETE) { 1271 } elsif ($sym == 127) {
1197 substr $text, $self->{cursor}, 1, ""; 1272 substr $text, $self->{cursor}, 1, "";
1198 } elsif ($sym == SDLK_LEFT) { 1273 } elsif ($sym == CFClient::SDLK_LEFT) {
1199 --$self->{cursor} if $self->{cursor}; 1274 --$self->{cursor} if $self->{cursor};
1200 } elsif ($sym == SDLK_RIGHT) { 1275 } elsif ($sym == CFClient::SDLK_RIGHT) {
1201 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1276 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1202 } elsif ($sym == SDLK_HOME) { 1277 } elsif ($sym == CFClient::SDLK_HOME) {
1203 $self->{cursor} = 0; 1278 $self->{cursor} = 0;
1204 } elsif ($sym == SDLK_END) { 1279 } elsif ($sym == CFClient::SDLK_END) {
1205 $self->{cursor} = length $text; 1280 $self->{cursor} = length $text;
1206 } elsif ($sym == SDLK_ESCAPE) { 1281 } elsif ($sym == 27) {
1207 $self->emit ('escape'); 1282 $self->emit ('escape');
1208 } elsif ($uni) { 1283 } elsif ($uni) {
1209 substr $text, $self->{cursor}++, 0, chr $uni; 1284 substr $text, $self->{cursor}++, 0, chr $uni;
1210 } 1285 }
1211 1286
1286 1361
1287package CFClient::UI::Entry; 1362package CFClient::UI::Entry;
1288 1363
1289our @ISA = CFClient::UI::EntryBase::; 1364our @ISA = CFClient::UI::EntryBase::;
1290 1365
1291use SDL; 1366use CFClient::OpenGL;
1292use SDL::OpenGL;
1293 1367
1294sub key_down { 1368sub key_down {
1295 my ($self, $ev) = @_; 1369 my ($self, $ev) = @_;
1296 1370
1297 my $sym = $ev->key_sym; 1371 my $sym = $ev->{sym};
1298 1372
1299 if ($sym == SDLK_RETURN) { 1373 if ($sym == 13) {
1300 $self->emit (activate => $self->get_text); 1374 $self->emit (activate => $self->get_text);
1301 $self->update; 1375 $self->update;
1302 1376
1303 } else { 1377 } else {
1304 $self->SUPER::key_down ($ev); 1378 $self->SUPER::key_down ($ev);
1310 1384
1311package CFClient::UI::Button; 1385package CFClient::UI::Button;
1312 1386
1313our @ISA = CFClient::UI::Label::; 1387our @ISA = CFClient::UI::Label::;
1314 1388
1315use SDL; 1389use CFClient::OpenGL;
1316use SDL::OpenGL;
1317 1390
1318my @tex = 1391my @tex =
1319 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1392 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1320 qw(b1_button_active.png); 1393 qw(b1_button_active.png);
1321 1394
1322sub new { 1395sub new {
1323 my $class = shift; 1396 my $class = shift;
1324 1397
1328 bg => [1, 1, 1, 0.2], 1401 bg => [1, 1, 1, 0.2],
1329 active_fg => [0, 0, 1], 1402 active_fg => [0, 0, 1],
1330 can_hover => 1, 1403 can_hover => 1,
1331 align => 0, 1404 align => 0,
1332 valign => 0, 1405 valign => 0,
1406 can_events => 1,
1333 @_ 1407 @_
1334 ) 1408 )
1335} 1409}
1336 1410
1337sub button_up { 1411sub button_up {
1371package CFClient::UI::CheckBox; 1445package CFClient::UI::CheckBox;
1372 1446
1373our @ISA = CFClient::UI::DrawBG::; 1447our @ISA = CFClient::UI::DrawBG::;
1374 1448
1375my @tex = 1449my @tex =
1376 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1377 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1451 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1378 1452
1379use SDL; 1453use CFClient::OpenGL;
1380use SDL::OpenGL;
1381 1454
1382sub new { 1455sub new {
1383 my $class = shift; 1456 my $class = shift;
1384 1457
1385 $class->SUPER::new ( 1458 $class->SUPER::new (
1396 my ($self) = @_; 1469 my ($self) = @_;
1397 1470
1398 ($self->{padding} * 2 + 6) x 2 1471 ($self->{padding} * 2 + 6) x 2
1399} 1472}
1400 1473
1401sub size_allocate {
1402 my ($self, $x, $y, $w, $h) = @_;
1403
1404 $self->_size_allocate ($x, $y, $w, $h) or return;
1405}
1406
1407sub button_down { 1474sub button_down {
1408 my ($self, $ev, $x, $y) = @_; 1475 my ($self, $ev, $x, $y) = @_;
1409 1476
1410 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1477 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1411 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1478 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1437 glDisable GL_BLEND; 1504 glDisable GL_BLEND;
1438} 1505}
1439 1506
1440############################################################################# 1507#############################################################################
1441 1508
1442package CFClient::UI::VGauge; 1509package CFClient::UI::Image;
1443 1510
1444our @ISA = CFClient::UI::Base::; 1511our @ISA = CFClient::UI::Base::;
1445 1512
1446use SDL::OpenGL; 1513use CFClient::OpenGL;
1514use Carp qw/confess/;
1447 1515
1448my %tex = ( 1516our %loaded_images;
1449 food => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1452 ],
1453 grace => [
1454 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1455 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1456 ],
1457 hp => [
1458 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1459 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1460 ],
1461 mana => [
1462 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1463 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1464 ],
1465);
1466 1517
1467# eg. VGauge->new (gauge => 'food'), default gauge: food
1468sub new { 1518sub new {
1469 my $class = shift; 1519 my $class = shift;
1470 1520
1471 my $self = $class->SUPER::new (gauge => 'food', @_); 1521 my $self = $class->SUPER::new (can_events => 0, @_);
1522
1523 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1524
1525 $loaded_images{$self->{image}} ||=
1526 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1527
1528 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1529
1530 Scalar::Util::weaken $loaded_images{$self->{image}};
1531
1532 $self->{aspect} = $tex->{w} / $tex->{h};
1472 1533
1473 $self 1534 $self
1474} 1535}
1475 1536
1476sub size_request { 1537sub size_request {
1477 my ($self) = @_; 1538 my ($self) = @_;
1478 1539
1479 my $tex = $tex{$self->{gauge}}[0]; 1540 ($self->{tex}->{w}, $self->{tex}->{h})
1480
1481 @$tex{qw(w h)}
1482}
1483
1484sub set_max {
1485 my ($self, $max) = @_;
1486
1487 $self->{max_val} = $max;
1488}
1489
1490sub set_value {
1491 my ($self, $val, $max) = @_;
1492
1493 $self->set_max ($max)
1494 if defined $max;
1495
1496 $max = $self->{max_val};
1497 $self->{val} = $val;
1498
1499 $self->update;
1500} 1541}
1501 1542
1502sub _draw { 1543sub _draw {
1503 my ($self) = @_; 1544 my ($self) = @_;
1504 1545
1505 my $tex = $tex{$self->{gauge}}; 1546 my $tex = $self->{tex};
1506 1547
1507 my ($w, $h) = ($self->{w}, $self->{h}); 1548 my ($w, $h) = ($self->{w}, $self->{h});
1508 1549
1509 my $ycut = $self->{val} / ($self->{max_val} || 1); 1550 if ($self->{rot90}) {
1510 $ycut = 1 if $self->{val} > $self->{max_val}; 1551 glRotate 90, 0, 0, 1;
1552 glTranslate 0, -$self->{w}, 0;
1511 1553
1512 my $t1 = $tex->[0]; 1554 ($w, $h) = ($h, $w);
1513 my $t2 = $tex->[1]; 1555 }
1514 1556
1515 glEnable GL_BLEND; 1557 glEnable GL_BLEND;
1516 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1558 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1517 glEnable GL_TEXTURE_2D; 1559 glEnable GL_TEXTURE_2D;
1518 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1519 1561
1562 $tex->draw_quad (0, 0, $w, $h);
1563
1564 glDisable GL_BLEND;
1565 glDisable GL_TEXTURE_2D;
1566}
1567
1568#############################################################################
1569
1570package CFClient::UI::VGauge;
1571
1572our @ISA = CFClient::UI::Base::;
1573
1574use CFClient::OpenGL;
1575
1576my %tex = (
1577 food => [
1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1579 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1580 ],
1581 grace => [
1582 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1583 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1584 ],
1585 hp => [
1586 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1587 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1588 ],
1589 mana => [
1590 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1591 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1592 ],
1593);
1594
1595# eg. VGauge->new (gauge => 'food'), default gauge: food
1596sub new {
1597 my $class = shift;
1598
1599 my $self = $class->SUPER::new (
1600 type => 'food',
1601 @_
1602 );
1603
1604 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1605
1606 $self
1607}
1608
1609sub size_request {
1610 my ($self) = @_;
1611
1612 #my $tex = $tex{$self->{type}}[0];
1613 #@$tex{qw(w h)}
1614 (0, 0)
1615}
1616
1617sub set_max {
1618 my ($self, $max) = @_;
1619
1620 $self->{max_val} = $max;
1621}
1622
1623sub set_value {
1624 my ($self, $val, $max) = @_;
1625
1626 $self->set_max ($max)
1627 if defined $max;
1628
1629 $max = $self->{max_val};
1630 $self->{val} = $val;
1631
1632 $self->update;
1633}
1634
1635sub _draw {
1636 my ($self) = @_;
1637
1638 my $tex = $tex{$self->{type}};
1639
1640 my ($w, $h) = ($self->{w}, $self->{h});
1641
1642 if ($self->{vertical}) {
1643 glRotate 90, 0, 0, 1;
1644 glTranslate 0, -$self->{w}, 0;
1645
1646 ($w, $h) = ($h, $w);
1647 }
1648
1649 my $ycut = $self->{val} / ($self->{max_val} || 1);
1650 $ycut = 1 if $self->{val} > $self->{max_val};
1651
1652 my $t1 = $tex->[0];
1653 my $t2 = $tex->[1];
1654
1655 glEnable GL_BLEND;
1656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1657 glEnable GL_TEXTURE_2D;
1658 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1659
1520 my $h1 = $self->{h} - $ycut * $self->{h}; 1660 my $h1 = $self->{h} - $ycut * $self->{h};
1521 my $h2 = $ycut * $self->{h}; 1661 my $h2 = $ycut * $self->{h};
1522 1662
1523 my $yp = 0;
1524
1525 glBindTexture (GL_TEXTURE_2D, $t1->{name}); 1663 glBindTexture GL_TEXTURE_2D, $t1->{name};
1526 glBegin (GL_QUADS); 1664 glBegin GL_QUADS;
1527 glTexCoord (0, 0); glVertex (0 , $yp); 1665 glTexCoord 0 , 0; glVertex 0 , 0;
1528 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1); 1666 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1529 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1); 1667 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1530 glTexCoord (1, 0); glVertex (0 + $w, $yp); 1668 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1531 glEnd (); 1669 glEnd;
1532 1670
1533 $yp += $h1;
1534
1535 glBindTexture (GL_TEXTURE_2D, $t2->{name}); 1671 glBindTexture GL_TEXTURE_2D, $t2->{name};
1536 glBegin (GL_QUADS); 1672 glBegin GL_QUADS;
1537 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp); 1673 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1538 glTexCoord (0, 1); glVertex (0 , $yp + $h2); 1674 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1539 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2); 1675 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1540 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp); 1676 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1541 glEnd (); 1677 glEnd;
1542 1678
1543 glDisable GL_BLEND; 1679 glDisable GL_BLEND;
1544 glDisable GL_TEXTURE_2D; 1680 glDisable GL_TEXTURE_2D;
1545} 1681}
1546 1682
1547############################################################################# 1683#############################################################################
1548 1684
1685package CFClient::UI::Gauge;
1686
1687our @ISA = CFClient::UI::VBox::;
1688
1689sub new {
1690 my ($class, %arg) = shift;
1691
1692 my $self = $class->SUPER::new (
1693 @_,
1694 );
1695
1696 $self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1697 $self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1698 $self->add ($self->{value});
1699 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1700 $self->add ($self->{max});
1701
1702 $self
1703}
1704
1705sub size_request {
1706 my ($self) = @_;
1707 (($self->{max}->size_request)[0], 0)
1708}
1709
1710sub set_fontsize {
1711 my ($self, $fsize) = @_;
1712
1713 $self->{value}->set_fontsize ($fsize);
1714 $self->{max} ->set_fontsize ($fsize);
1715}
1716
1717sub set_value {
1718 my ($self, $val, $max) = @_;
1719
1720 $self->set_max ($max)
1721 if defined $max;
1722
1723 $self->{gauge}->set_value ($val, $max);
1724 $self->{value}->set_text ($val);
1725}
1726
1727sub set_max {
1728 my ($self, $max) = @_;
1729
1730 $self->{gauge}->set_max ($max);
1731 $self->{max}->set_text ($max);
1732}
1733
1734#############################################################################
1735
1549package CFClient::UI::Slider; 1736package CFClient::UI::Slider;
1550 1737
1551use strict; 1738use strict;
1552 1739
1553use SDL::OpenGL; 1740use CFClient::OpenGL;
1554 1741
1555our @ISA = CFClient::UI::DrawBG::; 1742our @ISA = CFClient::UI::DrawBG::;
1556 1743
1557my @tex = 1744my @tex =
1558 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1745 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1568 # TODO: calculations are off 1755 # TODO: calculations are off
1569 my $self = $class->SUPER::new ( 1756 my $self = $class->SUPER::new (
1570 fg => [1, 1, 1], 1757 fg => [1, 1, 1],
1571 active_fg => [0, 0, 0], 1758 active_fg => [0, 0, 0],
1572 range => [0, 0, 100, 10], 1759 range => [0, 0, 100, 10],
1573 req_w => 40, 1760 req_w => 20,
1574 req_h => 13, 1761 req_h => 20,
1575 vertical => 0, 1762 vertical => 0,
1576 can_hover => 1, 1763 can_hover => 1,
1577 inner_pad => 5, 1764 inner_pad => 5,
1578 @_ 1765 @_
1579 ); 1766 );
1683 1870
1684package CFClient::UI::TextView; 1871package CFClient::UI::TextView;
1685 1872
1686our @ISA = CFClient::UI::HBox::; 1873our @ISA = CFClient::UI::HBox::;
1687 1874
1688use SDL::OpenGL; 1875use CFClient::OpenGL;
1689 1876
1690sub new { 1877sub new {
1691 my $class = shift; 1878 my $class = shift;
1692 1879
1693 my $self = $class->SUPER::new ( 1880 my $self = $class->SUPER::new (
1694 req_w => $::WIDTH / 6,
1695 req_h => $::HEIGHT / 6,
1696 fontsize => $::FONTSIZE, 1881 fontsize => 1,
1697 @_, 1882 @_,
1698 1883
1699 layout => (new CFClient::Layout), 1884 layout => (new CFClient::Layout),
1700 par => [], 1885 par => [],
1701 height => 0, 1886 height => 0,
1722sub text_height { 1907sub text_height {
1723 my ($self, $text) = @_; 1908 my ($self, $text) = @_;
1724 1909
1725 my $layout = $self->{layout}; 1910 my $layout = $self->{layout};
1726 1911
1727 $layout->set_height ($self->{fontsize}); 1912 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1728 $layout->set_width ($self->{w}); 1913 $layout->set_width ($self->{w});
1729 $layout->set_text ($text); 1914 $layout->set_text ($text);
1730 1915
1731 ($layout->size)[1] 1916 ($layout->size)[1]
1732} 1917}
1736 1921
1737 $self->{need_reflow}++; 1922 $self->{need_reflow}++;
1738 $self->update; 1923 $self->update;
1739} 1924}
1740 1925
1741sub size_request {
1742 my ($self) = @_;
1743
1744 ($self->{req_w}, $self->{req_h})
1745}
1746
1747sub size_allocate { 1926sub size_allocate {
1748 my ($self, $x, $y, $w, $h) = @_; 1927 my ($self, $w, $h) = @_;
1749 1928
1750 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1929 $self->SUPER::size_allocate ($w, $h);
1751 1930
1752 $self->{layout}->set_height ($self->{fontsize}); 1931 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1753 $self->{layout}->set_width ($self->{w}); 1932 $self->{layout}->set_width ($self->{children}[0]{w});
1754 1933
1755 $self->reflow; 1934 $self->reflow;
1756 $self->update;
1757} 1935}
1758 1936
1759sub add_paragraph { 1937sub add_paragraph {
1760 my ($self, $color, $text) = @_; 1938 my ($self, $color, $text) = @_;
1761 1939
1846 2024
1847} 2025}
1848 2026
1849############################################################################# 2027#############################################################################
1850 2028
1851package CFClient::UI::MapWidget;
1852
1853use strict;
1854
1855use List::Util qw(min max);
1856
1857use SDL;
1858use SDL::OpenGL;
1859
1860our @ISA = CFClient::UI::Base::;
1861
1862sub new {
1863 my $class = shift;
1864
1865 $class->SUPER::new (
1866 z => -1,
1867 can_focus => 1,
1868 list => (glGenLists 1),
1869 @_
1870 )
1871}
1872
1873sub key_down {
1874 print "MAPKEYDOWN\n";
1875}
1876
1877sub key_up {
1878}
1879
1880sub button_down {
1881 my ($self, $ev, $x, $y) = @_;
1882
1883 $self->focus_in;
1884
1885 if ($ev->button == 2) {
1886 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1887 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1888
1889 $self->{motion} = sub {
1890 my ($ev, $x, $y) = @_;
1891
1892 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1893
1894 $::CFG->{map_shift_x} = $bw + $x - $ox;
1895 $::CFG->{map_shift_y} = $bh + $y - $oy;
1896
1897 $self->update;
1898 };
1899 }
1900}
1901
1902sub button_up {
1903 my ($self, $ev, $x, $y) = @_;
1904
1905 delete $self->{motion};
1906}
1907
1908sub mouse_motion {
1909 my ($self, $ev, $x, $y) = @_;
1910
1911 $self->{motion}->($ev, $x, $y) if $self->{motion};
1912}
1913
1914sub size_request {
1915 (
1916 1 + 32 * int $::WIDTH / 32,
1917 1 + 32 * int $::HEIGHT / 32,
1918 )
1919}
1920
1921sub update {
1922 my ($self) = @_;
1923
1924 $self->{need_update} = 1;
1925 $self->SUPER::update;
1926}
1927
1928sub draw {
1929 my ($self) = @_;
1930
1931 if (delete $self->{need_update}) {
1932 glNewList $self->{list}, GL_COMPILE;
1933
1934 if ($::MAP) {
1935 my $sw = int $::WIDTH / 32;
1936 my $sh = int $::HEIGHT / 32;
1937
1938 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1939 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1940
1941 glTranslate $sx0 - 32, $sy0 - 32, 0;
1942
1943 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1944
1945 if ($::CFG->{fow_enable}) {
1946 if ($::CFG->{fow_smooth}) { # smooth fog of war
1947 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1948 glConvolutionFilter2D
1949 GL_CONVOLUTION_2D,
1950 GL_ALPHA,
1951 3, 3,
1952 GL_ALPHA, GL_FLOAT,
1953 pack "f*",
1954 0.1, 0.1, 0.1,
1955 0.1, 0.2, 0.1,
1956 0.1, 0.1, 0.1,
1957 ;
1958 glEnable GL_CONVOLUTION_2D;
1959 }
1960
1961 $self->{fow_texture} = new CFClient::Texture
1962 w => $w,
1963 h => $h,
1964 data => $data,
1965 internalformat => GL_ALPHA,
1966 format => GL_ALPHA;
1967
1968 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1969
1970 glEnable GL_BLEND;
1971 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1972 glEnable GL_TEXTURE_2D;
1973 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1974
1975 glColor +($::CFG->{fow_intensity}) x 3, 1;
1976 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1977
1978 glDisable GL_TEXTURE_2D;
1979 glDisable GL_BLEND;
1980 }
1981
1982 # HACK BEGIN
1983 {
1984 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1985 my ($w, $h) = (250, 250);
1986
1987 glEnable GL_BLEND;
1988 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1989 glEnable GL_TEXTURE_2D;
1990 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1991
1992 $self->{mapmap_texture} =
1993 new CFClient::Texture
1994 w => $w,
1995 h => $h,
1996 data => $::MAP->mapmap ($w, $h),
1997 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1998
1999 $self->{mapmap_texture}->draw_quad (100, 100);
2000
2001 glDisable GL_TEXTURE_2D;
2002 glDisable GL_BLEND;
2003 }
2004 # HACK END
2005 }
2006
2007 glEndList;
2008 }
2009
2010 glPushMatrix;
2011 glCallList $self->{list};
2012 glPopMatrix;
2013
2014 if ($FOCUS != $self) {
2015 glColor 64/255, 64/255, 64/255;
2016 glLogicOp GL_AND;
2017 glEnable GL_COLOR_LOGIC_OP;
2018 glBegin GL_QUADS;
2019 glVertex 0, 0;
2020 glVertex 0, $::HEIGHT;
2021 glVertex $::WIDTH, $::HEIGHT;
2022 glVertex $::WIDTH, 0;
2023 glEnd;
2024 glDisable GL_COLOR_LOGIC_OP;
2025 }
2026}
2027
2028my %DIR = (
2029 SDLK_KP8, [1, "north"],
2030 SDLK_KP9, [2, "northeast"],
2031 SDLK_KP6, [3, "east"],
2032 SDLK_KP3, [4, "southeast"],
2033 SDLK_KP2, [5, "south"],
2034 SDLK_KP1, [6, "southwest"],
2035 SDLK_KP4, [7, "west"],
2036 SDLK_KP7, [8, "northwest"],
2037
2038 SDLK_UP, [1, "north"],
2039 SDLK_RIGHT, [3, "east"],
2040 SDLK_DOWN, [5, "south"],
2041 SDLK_LEFT, [7, "west"],
2042);
2043
2044sub key_down {
2045 my ($self, $ev) = @_;
2046
2047 my $mod = $ev->key_mod;
2048 my $sym = $ev->key_sym;
2049
2050 if ($sym == SDLK_KP5) {
2051 $::CONN->user_send ("stay fire");
2052 } elsif ($sym == SDLK_a) {
2053 $::CONN->user_send ("apply");
2054 } elsif ($sym == SDLK_QUOTE) {
2055 $self->emit ('activate_console');
2056 } elsif ($sym == SDLK_SLASH) {
2057 $self->emit ('activate_console' => '/');
2058 } elsif (exists $DIR{$sym}) {
2059 if ($mod & KMOD_SHIFT) {
2060 $self->{shft}++;
2061 $::CONN->user_send ("fire $DIR{$sym}[0]");
2062 } elsif ($mod & KMOD_CTRL) {
2063 $self->{ctrl}++;
2064 $::CONN->user_send ("run $DIR{$sym}[0]");
2065 } else {
2066 $::CONN->user_send ("$DIR{$sym}[1]");
2067 }
2068 }
2069}
2070
2071sub key_up {
2072 my ($self, $ev) = @_;
2073
2074 my $mod = $ev->key_mod;
2075 my $sym = $ev->key_sym;
2076
2077 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2078 $::CONN->user_send ("fire_stop");
2079 }
2080 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2081 $::CONN->user_send ("run_stop");
2082 }
2083}
2084
2085#############################################################################
2086
2087package CFClient::UI::Animator; 2029package CFClient::UI::Animator;
2088 2030
2089use SDL::OpenGL; 2031use CFClient::OpenGL;
2090 2032
2091our @ISA = CFClient::UI::Bin::; 2033our @ISA = CFClient::UI::Bin::;
2092 2034
2093sub moveto { 2035sub moveto {
2094 my ($self, $x, $y) = @_; 2036 my ($self, $x, $y) = @_;
2134 my $class = shift; 2076 my $class = shift;
2135 2077
2136 my $self = $class->SUPER::new ( 2078 my $self = $class->SUPER::new (
2137 state => 0, 2079 state => 0,
2138 connect_activate => \&toggle_flopper, 2080 connect_activate => \&toggle_flopper,
2081 can_events => 1,
2139 @_ 2082 @_
2140 ); 2083 );
2141 2084
2142 if ($self->{state}) { 2085 if ($self->{state}) {
2143 $self->{state} = 0; 2086 $self->{state} = 0;
2167 2110
2168package CFClient::UI::Root; 2111package CFClient::UI::Root;
2169 2112
2170our @ISA = CFClient::UI::Container::; 2113our @ISA = CFClient::UI::Container::;
2171 2114
2172use SDL::OpenGL; 2115use CFClient::OpenGL;
2173 2116
2174sub check_size { 2117sub check_size {
2175 my ($self) = @_; 2118 my ($self) = @_;
2176 2119
2177 $self->size_allocate (0, 0, $::WITH, $::HEIGHT); 2120 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2178} 2121}
2179 2122
2180sub size_request { 2123sub size_request {
2181 ($::WIDTH, $::HEIGHT) 2124 ($::WIDTH, $::HEIGHT)
2182} 2125}
2183 2126
2184sub size_allocate { 2127sub configure {
2185 my ($self, $x, $y, $w, $h) = @_; 2128 my ($self, $x, $y, $w, $h) = @_;
2186 2129
2187 $self->_size_allocate ($x, $y, $w, $h); 2130 $self->SUPER::configure ($x, $y, $w, $h);
2188 2131
2189 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2190 for @{$self->{children}}; 2132 for my $child (@{$self->{children}}) {
2133 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2134
2135 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2136 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2137 $child->configure ($X, $Y, $W,$H);
2138 }
2191} 2139}
2192 2140
2193sub _topleft { 2141sub _topleft {
2194 my ($self, $x, $y) = @_; 2142 my ($self, $x, $y) = @_;
2195 2143
2202 $self->check_size; 2150 $self->check_size;
2203 ::refresh (); 2151 ::refresh ();
2204} 2152}
2205 2153
2206sub add { 2154sub add {
2207 my ($self, $widget) = @_; 2155 my ($self, $child) = @_;
2208 2156
2157 # integerize window positions
2158 $child->{x} = int $child->{x};
2159 $child->{y} = int $child->{y};
2160
2209 $self->SUPER::add ($widget); 2161 $self->SUPER::add ($child);
2210
2211 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2212} 2162}
2213 2163
2214sub on_refresh { 2164sub on_refresh {
2215 my ($self, $id, $cb) = @_; 2165 my ($self, $id, $cb) = @_;
2216 2166

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines