ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.121 by root, Mon Apr 17 19:21:00 2006 UTC vs.
Revision 1.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) = @_;
225 glPopMatrix; 257 glPopMatrix;
226 258
227 if ($self == $HOVER && $self->{can_hover}) { 259 if ($self == $HOVER && $self->{can_hover}) {
228 my ($x, $y) = @$self{qw(x y)}; 260 my ($x, $y) = @$self{qw(x y)};
229 261
230 glColor 0, 0, 1, 0.2; 262 glColor 1, 0.8, 0.5, 0.2;
231 glEnable GL_BLEND; 263 glEnable GL_BLEND;
232 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 264 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
233 glBegin GL_QUADS; 265 glBegin GL_QUADS;
234 glVertex $x , $y; 266 glVertex $x , $y;
235 glVertex $x + $self->{w}, $y; 267 glVertex $x + $self->{w}, $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 ()
260 my ($self, $parent) = @_; 294 my ($self, $parent) = @_;
261 295
262 Scalar::Util::weaken ($self->{parent} = $parent); 296 Scalar::Util::weaken ($self->{parent} = $parent);
263} 297}
264 298
299sub check_size {
300 my ($self) = @_;
301
302 return unless $self->{parent};
303
304 my ($w, $h) = $self->size_request;
305
306 if ($w != $self->{req_w} || $h != $self->{req_h}) {
307 $self->{req_w} = $w;
308 $self->{req_h} = $h;
309
310 $self->{parent}->check_size;
311 }
312}
313
265sub update { 314sub update {
266 my ($self) = @_; 315 my ($self) = @_;
267 316
268 $self->{parent}->update 317 $self->{parent}->update
269 if $self->{parent}; 318 if $self->{parent};
270} 319}
271 320
272sub connect { 321sub connect {
273 my ($self, $signal, $cb) = @_; 322 my ($self, $signal, $cb) = @_;
274 323
275 push @{ $self->{cb}{$signal} }, $cb; 324 push @{ $self->{signal_cb}{$signal} }, $cb;
276} 325}
277 326
278sub emit { 327sub emit {
279 my ($self, $signal, @args) = @_; 328 my ($self, $signal, @args) = @_;
280 329
330 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
281 $_->($self, @args) 331 $cb->($self, @args);
282 for @{$self->{cb}{$signal} || []}; 332 }
283} 333}
284 334
285sub DESTROY { 335sub DESTROY {
286 my ($self) = @_; 336 my ($self) = @_;
287 337
293package CFClient::UI::DrawBG; 343package CFClient::UI::DrawBG;
294 344
295our @ISA = CFClient::UI::Base::; 345our @ISA = CFClient::UI::Base::;
296 346
297use strict; 347use strict;
298use SDL::OpenGL; 348use CFClient::OpenGL;
299 349
300sub new { 350sub new {
301 my $class = shift; 351 my $class = shift;
302 352
303 # range [value, low, high, page] 353 # range [value, low, high, page]
332 382
333package CFClient::UI::Empty; 383package CFClient::UI::Empty;
334 384
335our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
336 386
387sub new {
388 my ($class, %arg) = @_;
389 $class->SUPER::new (can_events => 0, %arg);
390}
391
337sub size_request { 392sub size_request {
338 (0, 0) 393 (0, 0)
339} 394}
340 395
341sub draw { } 396sub draw { }
349sub new { 404sub new {
350 my ($class, %arg) = @_; 405 my ($class, %arg) = @_;
351 406
352 my $children = delete $arg{children} || []; 407 my $children = delete $arg{children} || [];
353 408
354 my $self = $class->SUPER::new (children => [], %arg); 409 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
355 $self->add ($_) for @$children; 410 $self->add ($_) for @$children;
356 411
357 $self 412 $self
358} 413}
359 414
367 $self->{children} = [ 422 $self->{children} = [
368 sort { $a->{z} <=> $b->{z} } 423 sort { $a->{z} <=> $b->{z} }
369 @{$self->{children}}, $child 424 @{$self->{children}}, $child
370 ]; 425 ];
371 426
372 $self->{w} = $self->{h} = -1; 427 $child->check_size;
373 $self->update;
374} 428}
375 429
376sub remove { 430sub remove {
377 my ($self, $widget) = @_; 431 my ($self, $child) = @_;
378 432
433 delete $child->{parent};
434
379 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 435 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
380 436
381 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 437 $self->check_size;
382} 438}
383 439
384sub find_widget { 440sub find_widget {
385 my ($self, $x, $y) = @_; 441 my ($self, $x, $y) = @_;
386 442
416 472
417 $class->SUPER::new (children => [$child], %arg) 473 $class->SUPER::new (children => [$child], %arg)
418} 474}
419 475
420sub add { 476sub add {
421 my ($self, $widget) = @_; 477 my ($self, $child) = @_;
422 478
423 $self->{children} = []; 479 $self->{children} = [];
424 480
425 $self->SUPER::add ($widget); 481 $self->SUPER::add ($child);
426} 482}
427 483
428sub remove { 484sub remove {
429 my ($self, $widget) = @_; 485 my ($self, $widget) = @_;
430 486
439sub size_request { 495sub size_request {
440 $_[0]{children}[0]->size_request 496 $_[0]{children}[0]->size_request
441} 497}
442 498
443sub size_allocate { 499sub size_allocate {
444 my ($self, $x, $y, $w, $h) = @_; 500 my ($self, $w, $h) = @_;
445 501
446 $self->_size_allocate ($x, $y, $w, $h) or return;
447
448 $self->{children}[0]->size_allocate (0, 0, $w, $h); 502 $self->{children}[0]->configure (0, 0, $w, $h);
449} 503}
450 504
451############################################################################# 505#############################################################################
452 506
453package CFClient::UI::Window; 507package CFClient::UI::Window;
454 508
455our @ISA = CFClient::UI::Bin::; 509our @ISA = CFClient::UI::Bin::;
456 510
457use SDL::OpenGL; 511use CFClient::OpenGL;
458 512
459sub new { 513sub new {
460 my ($class, %arg) = @_; 514 my ($class, %arg) = @_;
461 515
462 my $self = $class->SUPER::new (%arg); 516 my $self = $class->SUPER::new (%arg);
479 $self->child->draw; 533 $self->child->draw;
480 }; 534 };
481} 535}
482 536
483sub size_allocate { 537sub size_allocate {
484 my ($self, $x, $y, $w, $h) = @_; 538 my ($self, $w, $h) = @_;
485 539
486 $self->_size_allocate ($x, $y, $w, $h) or return;
487
488 $self->child->size_allocate (0, 0, $w, $h); 540 $self->child->configure (0, 0, $w, $h);
489 541
490 $self->render_chld; 542 $self->render_chld;
491} 543}
492 544
493sub _draw { 545sub _draw {
509 glDisable GL_TEXTURE_2D; 561 glDisable GL_TEXTURE_2D;
510} 562}
511 563
512############################################################################# 564#############################################################################
513 565
566package CFClient::UI::ViewPort;
567
568our @ISA = CFClient::UI::Window::;
569
570sub new { die }
571
572sub size_request {
573 my ($self) = @_;
574
575 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
576 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
577
578 @$self{qw(child_w child_h)}
579}
580
581sub _draw {
582 my ($self) = @_;
583
584 $self->{children}[1]->draw;
585}
586
587
588#############################################################################
589
514package CFClient::UI::Frame; 590package CFClient::UI::Frame;
515 591
516our @ISA = CFClient::UI::Bin::; 592our @ISA = CFClient::UI::Bin::;
517 593
518use SDL::OpenGL; 594use 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 595
568sub new { 596sub new {
569 my $class = shift; 597 my $class = shift;
570 598
571 # TODO: user_x, user_y, overwrite moveto?
572
573 $class->SUPER::new ( 599 my $self = $class->SUPER::new (
574 bg => [1, 1, 1, 1], 600 bg => [1, 1, 1, 1],
575 border_bg => [1, 1, 1, 1], 601 border_bg => [1, 1, 1, 1],
576 border => int $::FONTSIZE * 0.8, 602 border => 0.8,
577 @_ 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,
578 ) 700 )
579} 701}
580 702
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 { 703sub size_allocate {
595 my ($self, $x, $y, $w, $h) = @_; 704 my ($self, $w, $h) = @_;
596 705
597 $self->_size_allocate ($x, $y, $w, $h) or return;
598
599 $h -= List::Util::max 0, $self->{border} * 2; 706 $h -= List::Util::max 0, $self->border * 2;
600 $w -= List::Util::max 0, $self->{border} * 2; 707 $w -= List::Util::max 0, $self->border * 2;
601 708
709 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
710 if $self->{title};
711
602 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 712 $self->child->configure ($self->border, $self->border, $w, $h);
603} 713}
604 714
605sub button_down { 715sub button_down {
606 my ($self, $ev, $x, $y) = @_; 716 my ($self, $ev, $x, $y) = @_;
607 717
718 my $border = $self->border;
719
608 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 720 if ($x < $self->{w} && $x >= $self->{w} - $border
609 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 721 && $y < $self->{h} && $y >= $self->{h} - $border) {
610 722
611 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 723 my ($ox, $oy) = ($ev->{x}, $ev->{y});
612 my ($bw, $bh) = ($self->{w}, $self->{h}); 724 my ($bw, $bh) = ($self->{w}, $self->{h});
613 725
614 $self->{motion} = sub { 726 $self->{motion} = sub {
615 my ($ev, $x, $y) = @_; 727 my ($ev, $x, $y) = @_;
616 728
617 ($x, $y) = ($ev->motion_x, $ev->motion_y); 729 ($x, $y) = ($ev->{x}, $ev->{y});
618 730
619 $self->{user_w} = $bw + $x - $ox; 731 $self->{user_w} = $bw + $x - $ox;
620 $self->{user_h} = $bh + $y - $oy; 732 $self->{user_h} = $bh + $y - $oy;
621 $self->update; 733 $self->check_size;
622 }; 734 };
623 735
624 } elsif ($x >= 0 && $x < $self->{w} 736 } elsif ($x >= 0 && $x < $self->{w}
625 && $y >= 0 && $y < $self->{border}) { 737 && $y >= 0 && $y < $border) {
626 738
627 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 739 my ($ox, $oy) = ($ev->{x}, $ev->{y});
628 my ($bx, $by) = ($self->{x}, $self->{y}); 740 my ($bx, $by) = ($self->{x}, $self->{y});
629 741
630 $self->{motion} = sub { 742 $self->{motion} = sub {
631 my ($ev, $x, $y) = @_; 743 my ($ev, $x, $y) = @_;
632 744
633 ($x, $y) = ($ev->motion_x, $ev->motion_y); 745 ($x, $y) = ($ev->{x}, $ev->{y});
634 746
635 $self->move ($bx + $x - $ox, $by + $y - $oy); 747 $self->move ($bx + $x - $ox, $by + $y - $oy);
636 $self->update; 748 $self->update;
637 }; 749 };
638 } 750 }
659 glEnable GL_BLEND; 771 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 772 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 773 glEnable GL_TEXTURE_2D;
662 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 774 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
663 775
776 my $border = $self->border;
777
664 glColor @{ $self->{border_bg} }; 778 glColor @{ $self->{border_bg} };
665 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 779 $tex[1]->draw_quad (0, 0, $w, $border);
666 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 780 $tex[3]->draw_quad (0, $border, $border, $ch);
667 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 781 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
668 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 782 $tex[4]->draw_quad (0, $h - $border, $w, $border);
669 783
670 my $bg = $tex[0]; 784 my $bg = $tex[0];
671 785
672 # TODO: repeat texture not scale 786 # TODO: repeat texture not scale
673 my $rep_x = $cw / $bg->{w}; 787 my $rep_x = $cw / $bg->{w};
676 glColor @{ $self->{bg} }; 790 glColor @{ $self->{bg} };
677 791
678 $bg->{s} = $rep_x; 792 $bg->{s} = $rep_x;
679 $bg->{t} = $rep_y; 793 $bg->{t} = $rep_y;
680 $bg->{wrap_mode} = 1; 794 $bg->{wrap_mode} = 1;
681 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 795 $bg->draw_quad ($border, $border, $cw, $ch);
682 796
683 glDisable GL_TEXTURE_2D; 797 glDisable GL_TEXTURE_2D;
684 glDisable GL_BLEND; 798 glDisable GL_BLEND;
685 799
800 $self->{title}->draw if $self->{title};
686 $self->child->draw; 801 $self->child->draw;
687} 802}
688 803
689############################################################################# 804#############################################################################
690 805
692 807
693our @ISA = CFClient::UI::Base::; 808our @ISA = CFClient::UI::Base::;
694 809
695use List::Util qw(max sum); 810use List::Util qw(max sum);
696 811
697use SDL::OpenGL; 812use CFClient::OpenGL;
698 813
699sub new { 814sub new {
700 my $class = shift; 815 my $class = shift;
701 816
702 $class->SUPER::new ( 817 $class->SUPER::new (
709 my ($self, $x, $y, $child) = @_; 824 my ($self, $x, $y, $child) = @_;
710 825
711 $child->set_parent ($self); 826 $child->set_parent ($self);
712 $self->{children}[$y][$x] = $child; 827 $self->{children}[$y][$x] = $child;
713 828
714 $self->{w} = $self->{h} = -1; 829 $child->check_size;
715 $self->update;
716} 830}
717 831
718# 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?
719sub clear { 833sub clear {
720 my ($self) = @_; 834 my ($self) = @_;
733 or next; 847 or next;
734 848
735 for my $x (0 .. $#$row) { 849 for my $x (0 .. $#$row) {
736 my $widget = $row->[$x] 850 my $widget = $row->[$x]
737 or next; 851 or next;
738 my ($w, $h) = $widget->size_request; 852 my ($w, $h) = @$widget{qw(req_w req_h)};
739 853
740 $w[$x] = max $w[$x], $w; 854 $w[$x] = max $w[$x], $w;
741 $h[$y] = max $h[$y], $h; 855 $h[$y] = max $h[$y], $h;
742 } 856 }
743 } 857 }
755 (sum @$hs), 869 (sum @$hs),
756 ) 870 )
757} 871}
758 872
759sub size_allocate { 873sub size_allocate {
760 my ($self, $x, $y, $w, $h) = @_; 874 my ($self, $w, $h) = @_;
761
762 $self->_size_allocate ($x, $y, $w, $h) or return;
763 875
764 my ($ws, $hs) = $self->get_wh; 876 my ($ws, $hs) = $self->get_wh;
765 877
766 my $req_w = sum @$ws; 878 my $req_w = sum @$ws;
767 my $req_h = sum @$hs; 879 my $req_h = sum @$hs;
789 901
790 for my $c (0 .. $#$row) { 902 for my $c (0 .. $#$row) {
791 my $col_w = $ws->[$c]; 903 my $col_w = $ws->[$c];
792 904
793 if (my $widget = $row->[$c]) { 905 if (my $widget = $row->[$c]) {
794 $widget->size_allocate ($x, $y, $col_w, $row_h); 906 $widget->configure ($x, $y, $col_w, $row_h);
795 } 907 }
796 908
797 $x += $col_w; 909 $x += $col_w;
798 } 910 }
799 911
844 (List::Util::max map $_->[1], @alloc), 956 (List::Util::max map $_->[1], @alloc),
845 ) 957 )
846} 958}
847 959
848sub size_allocate { 960sub size_allocate {
849 my ($self, $x, $y, $w, $h) = @_; 961 my ($self, $w, $h) = @_;
850
851 $self->_size_allocate ($x, $y, $w, $h);
852 962
853 ($h, $w) = ($w, $h); 963 ($h, $w) = ($w, $h);
854 964
855 my $children = $self->{children}; 965 my $children = $self->{children};
856 966
857 my @h = map +($_->size_request)[0], @$children; 967 my @h = map $_->{req_w}, @$children;
858 968
859 my $req_h = List::Util::sum @h; 969 my $req_h = List::Util::sum @h;
860 970
861 if ($req_h > $h) { 971 if ($req_h > $h) {
862 # ah well, not enough space 972 # ah well, not enough space
878 988
879 my $y = 0; 989 my $y = 0;
880 for (0 .. $#$children) { 990 for (0 .. $#$children) {
881 my $child = $children->[$_]; 991 my $child = $children->[$_];
882 my $h = $h[$_]; 992 my $h = $h[$_];
883 $child->size_allocate ($y, 0, $h, $w); 993 $child->configure ($y, 0, $h, $w);
884 994
885 $y += $h; 995 $y += $h;
886 } 996 }
997
998 1
887} 999}
888 1000
889############################################################################# 1001#############################################################################
890 1002
891package CFClient::UI::VBox; 1003package CFClient::UI::VBox;
904 (List::Util::sum map $_->[1], @alloc), 1016 (List::Util::sum map $_->[1], @alloc),
905 ) 1017 )
906} 1018}
907 1019
908sub size_allocate { 1020sub size_allocate {
909 my ($self, $x, $y, $w, $h) = @_; 1021 my ($self, $w, $h) = @_;
910
911 $self->_size_allocate ($x, $y, $w, $h);
912 1022
913 my $children = $self->{children}; 1023 my $children = $self->{children};
914 1024
915 my @h = map +($_->size_request)[1], @$children; 1025 my @h = map $_->{req_h}, @$children;
916 1026
917 my $req_h = List::Util::sum @h; 1027 my $req_h = List::Util::sum @h;
918 1028
919 if ($req_h > $h) { 1029 if ($req_h > $h) {
920 # ah well, not enough space 1030 # ah well, not enough space
934 1044
935 my $y = 0; 1045 my $y = 0;
936 for (0 .. $#$children) { 1046 for (0 .. $#$children) {
937 my $child = $children->[$_]; 1047 my $child = $children->[$_];
938 my $h = $h[$_]; 1048 my $h = $h[$_];
939 $child->size_allocate (0, $y, $w, $h); 1049 $child->configure (0, $y, $w, $h);
940 1050
941 $y += $h; 1051 $y += $h;
942 } 1052 }
1053
1054 1
943} 1055}
944 1056
945############################################################################# 1057#############################################################################
946 1058
947package CFClient::UI::Label; 1059package CFClient::UI::Label;
948 1060
949our @ISA = CFClient::UI::Base::; 1061our @ISA = CFClient::UI::Base::;
950 1062
951use SDL::OpenGL; 1063use CFClient::OpenGL;
952 1064
953sub new { 1065sub new {
954 my ($class, %arg) = @_; 1066 my ($class, %arg) = @_;
955 1067
956 my $self = $class->SUPER::new ( 1068 my $self = $class->SUPER::new (
957 fg => [1, 1, 1], 1069 fg => [1, 1, 1],
958 fontsize => $::FONTSIZE, 1070 fontsize => 1,
959 text => "", 1071 text => "",
960 align => -1, 1072 align => -1,
961 valign => -1, 1073 valign => -1,
962 padding => 2, 1074 padding => 2,
963 layout => new CFClient::Layout, 1075 layout => new CFClient::Layout,
1076 can_events => 0,
964 %arg 1077 %arg
965 ); 1078 );
966 1079
967 $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 }
968 1085
969 $self->set_text (delete $self->{text}) if exists $self->{text}; 1086 $self->set_text (delete $self->{text}) if exists $self->{text};
970 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1087 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
971 1088
972 $self 1089 $self
986 my ($self, $text) = @_; 1103 my ($self, $text) = @_;
987 1104
988 $self->{layout}->set_text ($text); 1105 $self->{layout}->set_text ($text);
989 1106
990 delete $self->{texture}; 1107 delete $self->{texture};
1108 $self->check_size;
991 $self->update; 1109 $self->update;
992} 1110}
993 1111
994sub set_markup { 1112sub set_markup {
995 my ($self, $markup) = @_; 1113 my ($self, $markup) = @_;
996 1114
997 $self->{layout}->set_markup ($markup); 1115 $self->{layout}->set_markup ($markup);
998 1116
999 delete $self->{texture}; 1117 delete $self->{texture};
1118 $self->check_size;
1000 $self->update; 1119 $self->update;
1001} 1120}
1002 1121
1003sub size_request { 1122sub size_request {
1004 my ($self) = @_; 1123 my ($self) = @_;
1005 1124
1006 $self->{layout}->set_width; 1125 $self->{layout}->set_width;
1007 $self->{layout}->set_height ($self->{fontsize}); 1126 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1008 1127
1009 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 }
1010 1138
1011 ( 1139 (
1012 $w + $self->{padding} * 2, 1140 $w + $self->{padding} * 2,
1013 $h + $self->{padding} * 2, 1141 $h + $self->{padding} * 2,
1014 ) 1142 )
1015} 1143}
1016 1144
1017sub size_allocate { 1145sub size_allocate {
1018 my ($self, $x, $y, $w, $h) = @_; 1146 my ($self, $w, $h) = @_;
1019
1020 $self->_size_allocate ($x, $y, $w, $h) or return;
1021 1147
1022 delete $self->{texture}; 1148 delete $self->{texture};
1023} 1149}
1024 1150
1025sub update { 1151sub set_fontsize {
1026 my ($self) = @_; 1152 my ($self, $fontsize) = @_;
1027 1153
1028 delete $self->{texture}; 1154 $self->{fontsize} = $fontsize;
1029 $self->SUPER::update; 1155 $self->check_size;
1030} 1156}
1031 1157
1032sub _draw { 1158sub _draw {
1033 my ($self) = @_; 1159 my ($self) = @_;
1034 1160
1035 my $tex = $self->{texture} ||= do { 1161 my $tex = $self->{texture} ||= do {
1036 $self->{layout}->set_width ($self->{w}); 1162 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1163 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1038 new_from_layout CFClient::Texture $self->{layout} 1164 new_from_layout CFClient::Texture $self->{layout}
1039 }; 1165 };
1040 1166
1041 glEnable GL_BLEND; 1167 glEnable GL_BLEND;
1042 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1168 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1043 glEnable GL_TEXTURE_2D; 1169 glEnable GL_TEXTURE_2D;
1044 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1045 1171
1046 glColor @{$self->{fg}}; 1172 glColor @{$self->{fg}};
1047 1173
1048 my $x = 1174 $self->{ox} = int (
1049 $self->{align} < 0 ? $self->{padding} 1175 $self->{align} < 0 ? $self->{padding}
1050 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1176 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1051 : ($self->{w} - $tex->{w}) * 0.5; 1177 : ($self->{w} - $tex->{w}) * 0.5
1178 );
1052 1179
1053 my $y = 1180 $self->{oy} = int (
1054 $self->{valign} < 0 ? $self->{padding} 1181 $self->{valign} < 0 ? $self->{padding}
1055 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1182 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1056 : ($self->{h} - $tex->{h}) * 0.5; 1183 : ($self->{h} - $tex->{h}) * 0.5
1184 );
1057 1185
1058 $tex->draw_quad (int $x, int $y); 1186 $tex->draw_quad ($self->{ox}, $self->{oy});
1059 1187
1060 glDisable GL_TEXTURE_2D; 1188 glDisable GL_TEXTURE_2D;
1061 glDisable GL_BLEND; 1189 glDisable GL_BLEND;
1062} 1190}
1063 1191
1065 1193
1066package CFClient::UI::EntryBase; 1194package CFClient::UI::EntryBase;
1067 1195
1068our @ISA = CFClient::UI::Label::; 1196our @ISA = CFClient::UI::Label::;
1069 1197
1070use SDL; 1198use CFClient::OpenGL;
1071use SDL::OpenGL;
1072 1199
1073sub new { 1200sub new {
1074 my $class = shift; 1201 my $class = shift;
1075 1202
1076 $class->SUPER::new ( 1203 $class->SUPER::new (
1079 active_bg => [1, 1, 1, 0.5], 1206 active_bg => [1, 1, 1, 0.5],
1080 active_fg => [0, 0, 0], 1207 active_fg => [0, 0, 0],
1081 can_hover => 1, 1208 can_hover => 1,
1082 can_focus => 1, 1209 can_focus => 1,
1083 valign => 0, 1210 valign => 0,
1211 can_events => 1,
1084 @_ 1212 @_
1085 ) 1213 )
1086} 1214}
1087 1215
1088sub _set_text { 1216sub _set_text {
1090 1218
1091 delete $self->{cur_h}; 1219 delete $self->{cur_h};
1092 1220
1093 return if $self->{text} eq $text; 1221 return if $self->{text} eq $text;
1094 1222
1223 delete $self->{texture};
1224
1095 $self->{last_activity} = $::NOW; 1225 $self->{last_activity} = $::NOW;
1096 $self->{text} = $text; 1226 $self->{text} = $text;
1097 1227
1098 $text =~ s/./*/g if $self->{hidden}; 1228 $text =~ s/./*/g if $self->{hidden};
1099 $self->{layout}->set_text ("$text "); 1229 $self->{layout}->set_text ("$text ");
1112 1242
1113 ($w + 1, $h) # add 1 for cursor 1243 ($w + 1, $h) # add 1 for cursor
1114} 1244}
1115 1245
1116sub size_allocate { 1246sub size_allocate {
1117 my ($self, $x, $y, $w, $h) = @_; 1247 my ($self, $w, $h) = @_;
1118 1248
1119 $self->SUPER::size_allocate ($x, $y, $w, $h); 1249 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1120
1121 $self->_set_text ($self->{text});
1122} 1250}
1123 1251
1124sub set_text { 1252sub set_text {
1125 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1126 1254
1130} 1258}
1131 1259
1132sub key_down { 1260sub key_down {
1133 my ($self, $ev) = @_; 1261 my ($self, $ev) = @_;
1134 1262
1135 my $mod = $ev->key_mod; 1263 my $mod = $ev->{mod};
1136 my $sym = $ev->key_sym; 1264 my $sym = $ev->{sym};
1137
1138 my $uni = $ev->key_unicode; 1265 my $uni = $ev->{unicode};
1139 1266
1140 my $text = $self->get_text; 1267 my $text = $self->get_text;
1141 1268
1142 if ($sym == SDLK_BACKSPACE) { 1269 if ($sym == 8) {
1143 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1270 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1144 } elsif ($sym == SDLK_DELETE) { 1271 } elsif ($sym == 127) {
1145 substr $text, $self->{cursor}, 1, ""; 1272 substr $text, $self->{cursor}, 1, "";
1146 } elsif ($sym == SDLK_LEFT) { 1273 } elsif ($sym == CFClient::SDLK_LEFT) {
1147 --$self->{cursor} if $self->{cursor}; 1274 --$self->{cursor} if $self->{cursor};
1148 } elsif ($sym == SDLK_RIGHT) { 1275 } elsif ($sym == CFClient::SDLK_RIGHT) {
1149 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1276 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1150 } elsif ($sym == SDLK_HOME) { 1277 } elsif ($sym == CFClient::SDLK_HOME) {
1151 $self->{cursor} = 0; 1278 $self->{cursor} = 0;
1152 } elsif ($sym == SDLK_END) { 1279 } elsif ($sym == CFClient::SDLK_END) {
1153 $self->{cursor} = length $text; 1280 $self->{cursor} = length $text;
1154 } elsif ($sym == SDLK_ESCAPE) { 1281 } elsif ($sym == 27) {
1155 $self->emit ('escape'); 1282 $self->emit ('escape');
1156 } elsif ($uni) { 1283 } elsif ($uni) {
1157 substr $text, $self->{cursor}++, 0, chr $uni; 1284 substr $text, $self->{cursor}++, 0, chr $uni;
1158 } 1285 }
1159 1286
1224 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text) 1351 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1225 } 1352 }
1226 1353
1227 glColor @{$self->{fg}}; 1354 glColor @{$self->{fg}};
1228 glBegin GL_LINES; 1355 glBegin GL_LINES;
1229 glVertex $self->{cur_x}, $self->{cur_y};
1230 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1356 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1357 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1231 glEnd; 1358 glEnd;
1232 } 1359 }
1233} 1360}
1234 1361
1235package CFClient::UI::Entry; 1362package CFClient::UI::Entry;
1236 1363
1237our @ISA = CFClient::UI::EntryBase::; 1364our @ISA = CFClient::UI::EntryBase::;
1238 1365
1239use SDL; 1366use CFClient::OpenGL;
1240use SDL::OpenGL;
1241 1367
1242sub key_down { 1368sub key_down {
1243 my ($self, $ev) = @_; 1369 my ($self, $ev) = @_;
1244 1370
1245 my $sym = $ev->key_sym; 1371 my $sym = $ev->{sym};
1246 1372
1247 if ($sym == SDLK_RETURN) { 1373 if ($sym == 13) {
1248 $self->emit (activate => $self->get_text); 1374 $self->emit (activate => $self->get_text);
1249 $self->update; 1375 $self->update;
1250 1376
1251 } else { 1377 } else {
1252 $self->SUPER::key_down ($ev); 1378 $self->SUPER::key_down ($ev);
1258 1384
1259package CFClient::UI::Button; 1385package CFClient::UI::Button;
1260 1386
1261our @ISA = CFClient::UI::Label::; 1387our @ISA = CFClient::UI::Label::;
1262 1388
1263use SDL; 1389use CFClient::OpenGL;
1264use SDL::OpenGL;
1265 1390
1266my @tex = 1391my @tex =
1267 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1392 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1268 qw(b1_button_active.png); 1393 qw(b1_button_active.png);
1269 1394
1270sub new { 1395sub new {
1271 my $class = shift; 1396 my $class = shift;
1272 1397
1273 $class->SUPER::new ( 1398 $class->SUPER::new (
1274 padding => 4, 1399 padding => 4,
1275 fg => [1, 1, 1], 1400 fg => [1, 1, 1],
1276 bg => [1, 1, 1, 0.2], 1401 bg => [1, 1, 1, 0.2],
1277 active_fg => [1, 1, 0], 1402 active_fg => [0, 0, 1],
1278 can_hover => 1, 1403 can_hover => 1,
1404 align => 0,
1405 valign => 0,
1406 can_events => 1,
1279 @_ 1407 @_
1280 ) 1408 )
1281} 1409}
1282 1410
1283sub button_up { 1411sub button_up {
1317package CFClient::UI::CheckBox; 1445package CFClient::UI::CheckBox;
1318 1446
1319our @ISA = CFClient::UI::DrawBG::; 1447our @ISA = CFClient::UI::DrawBG::;
1320 1448
1321my @tex = 1449my @tex =
1322 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1323 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1451 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1324 1452
1325use SDL; 1453use CFClient::OpenGL;
1326use SDL::OpenGL;
1327 1454
1328sub new { 1455sub new {
1329 my $class = shift; 1456 my $class = shift;
1330 1457
1331 $class->SUPER::new ( 1458 $class->SUPER::new (
1342 my ($self) = @_; 1469 my ($self) = @_;
1343 1470
1344 ($self->{padding} * 2 + 6) x 2 1471 ($self->{padding} * 2 + 6) x 2
1345} 1472}
1346 1473
1347sub size_allocate {
1348 my ($self, $x, $y, $w, $h) = @_;
1349
1350 $self->_size_allocate ($x, $y, $w, $h);
1351}
1352
1353sub button_down { 1474sub button_down {
1354 my ($self, $ev, $x, $y) = @_; 1475 my ($self, $ev, $x, $y) = @_;
1355 1476
1356 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1477 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1357 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1478 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1383 glDisable GL_BLEND; 1504 glDisable GL_BLEND;
1384} 1505}
1385 1506
1386############################################################################# 1507#############################################################################
1387 1508
1509package CFClient::UI::Image;
1510
1511our @ISA = CFClient::UI::Base::;
1512
1513use CFClient::OpenGL;
1514use Carp qw/confess/;
1515
1516our %loaded_images;
1517
1518sub new {
1519 my $class = shift;
1520
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};
1533
1534 $self
1535}
1536
1537sub size_request {
1538 my ($self) = @_;
1539
1540 ($self->{tex}->{w}, $self->{tex}->{h})
1541}
1542
1543sub _draw {
1544 my ($self) = @_;
1545
1546 my $tex = $self->{tex};
1547
1548 my ($w, $h) = ($self->{w}, $self->{h});
1549
1550 if ($self->{rot90}) {
1551 glRotate 90, 0, 0, 1;
1552 glTranslate 0, -$self->{w}, 0;
1553
1554 ($w, $h) = ($h, $w);
1555 }
1556
1557 glEnable GL_BLEND;
1558 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1559 glEnable GL_TEXTURE_2D;
1560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
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
1660 my $h1 = $self->{h} - $ycut * $self->{h};
1661 my $h2 = $ycut * $self->{h};
1662
1663 glBindTexture GL_TEXTURE_2D, $t1->{name};
1664 glBegin GL_QUADS;
1665 glTexCoord 0 , 0; glVertex 0 , 0;
1666 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1667 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1668 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1669 glEnd;
1670
1671 glBindTexture GL_TEXTURE_2D, $t2->{name};
1672 glBegin GL_QUADS;
1673 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1674 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1675 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1676 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1677 glEnd;
1678
1679 glDisable GL_BLEND;
1680 glDisable GL_TEXTURE_2D;
1681}
1682
1683#############################################################################
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
1388package CFClient::UI::Slider; 1736package CFClient::UI::Slider;
1389 1737
1390use strict; 1738use strict;
1391 1739
1392use SDL::OpenGL; 1740use CFClient::OpenGL;
1393 1741
1394our @ISA = CFClient::UI::DrawBG::; 1742our @ISA = CFClient::UI::DrawBG::;
1395 1743
1396my @tex = 1744my @tex =
1397 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1745 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1407 # TODO: calculations are off 1755 # TODO: calculations are off
1408 my $self = $class->SUPER::new ( 1756 my $self = $class->SUPER::new (
1409 fg => [1, 1, 1], 1757 fg => [1, 1, 1],
1410 active_fg => [0, 0, 0], 1758 active_fg => [0, 0, 0],
1411 range => [0, 0, 100, 10], 1759 range => [0, 0, 100, 10],
1412 req_w => 40, 1760 req_w => 20,
1413 req_h => 13, 1761 req_h => 20,
1414 vertical => 0, 1762 vertical => 0,
1415 can_hover => 1, 1763 can_hover => 1,
1416 inner_pad => 5, 1764 inner_pad => 5,
1417 @_ 1765 @_
1418 ); 1766 );
1522 1870
1523package CFClient::UI::TextView; 1871package CFClient::UI::TextView;
1524 1872
1525our @ISA = CFClient::UI::HBox::; 1873our @ISA = CFClient::UI::HBox::;
1526 1874
1527use SDL::OpenGL; 1875use CFClient::OpenGL;
1528 1876
1529sub new { 1877sub new {
1530 my $class = shift; 1878 my $class = shift;
1531 1879
1532 my $self = $class->SUPER::new ( 1880 my $self = $class->SUPER::new (
1533 req_w => $::WIDTH / 6,
1534 req_h => $::HEIGHT / 6,
1535 fontsize => $::FONTSIZE, 1881 fontsize => 1,
1536 @_, 1882 @_,
1537 1883
1538 layout => (new CFClient::Layout), 1884 layout => (new CFClient::Layout),
1539 par => [], 1885 par => [],
1540 height => 0, 1886 height => 0,
1561sub text_height { 1907sub text_height {
1562 my ($self, $text) = @_; 1908 my ($self, $text) = @_;
1563 1909
1564 my $layout = $self->{layout}; 1910 my $layout = $self->{layout};
1565 1911
1566 $layout->set_height ($self->{fontsize}); 1912 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1567 $layout->set_width ($self->{w}); 1913 $layout->set_width ($self->{w});
1568 $layout->set_text ($text); 1914 $layout->set_text ($text);
1569 1915
1570 ($layout->size)[1] 1916 ($layout->size)[1]
1571} 1917}
1575 1921
1576 $self->{need_reflow}++; 1922 $self->{need_reflow}++;
1577 $self->update; 1923 $self->update;
1578} 1924}
1579 1925
1580sub size_request {
1581 my ($self) = @_;
1582
1583 ($self->{req_w}, $self->{req_h})
1584}
1585
1586sub size_allocate { 1926sub size_allocate {
1587 my ($self, $x, $y, $w, $h) = @_; 1927 my ($self, $w, $h) = @_;
1588 1928
1589 $self->SUPER::size_allocate ($x, $y, $w, $h); 1929 $self->SUPER::size_allocate ($w, $h);
1590 1930
1591 $self->{layout}->set_height ($self->{fontsize}); 1931 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1592 $self->{layout}->set_width ($self->{w}); 1932 $self->{layout}->set_width ($self->{children}[0]{w});
1593 1933
1594 $self->reflow; 1934 $self->reflow;
1595} 1935}
1596 1936
1597sub add_paragraph { 1937sub add_paragraph {
1684 2024
1685} 2025}
1686 2026
1687############################################################################# 2027#############################################################################
1688 2028
1689package CFClient::UI::MapWidget;
1690
1691use strict;
1692
1693use List::Util qw(min max);
1694
1695use SDL;
1696use SDL::OpenGL;
1697
1698our @ISA = CFClient::UI::Base::;
1699
1700sub new {
1701 my $class = shift;
1702
1703 $class->SUPER::new (
1704 z => -1,
1705 can_focus => 1,
1706 list => (glGenLists 1),
1707 @_
1708 )
1709}
1710
1711sub key_down {
1712 print "MAPKEYDOWN\n";
1713}
1714
1715sub key_up {
1716}
1717
1718sub button_down {
1719 my ($self, $ev, $x, $y) = @_;
1720
1721 $self->focus_in;
1722
1723 if ($ev->button == 2) {
1724 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1725 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1726
1727 $self->{motion} = sub {
1728 my ($ev, $x, $y) = @_;
1729
1730 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1731
1732 $::CFG->{map_shift_x} = $bw + $x - $ox;
1733 $::CFG->{map_shift_y} = $bh + $y - $oy;
1734
1735 $self->update;
1736 };
1737 }
1738}
1739
1740sub button_up {
1741 my ($self, $ev, $x, $y) = @_;
1742
1743 delete $self->{motion};
1744}
1745
1746sub mouse_motion {
1747 my ($self, $ev, $x, $y) = @_;
1748
1749 $self->{motion}->($ev, $x, $y) if $self->{motion};
1750}
1751
1752sub size_request {
1753 (
1754 1 + 32 * int $::WIDTH / 32,
1755 1 + 32 * int $::HEIGHT / 32,
1756 )
1757}
1758
1759sub update {
1760 my ($self) = @_;
1761
1762 $self->{need_update} = 1;
1763 $self->SUPER::update;
1764}
1765
1766sub draw {
1767 my ($self) = @_;
1768
1769 if (delete $self->{need_update}) {
1770 glNewList $self->{list}, GL_COMPILE;
1771
1772 if ($::MAP) {
1773 my $sw = int $::WIDTH / 32;
1774 my $sh = int $::HEIGHT / 32;
1775
1776 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1777 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1778
1779 glTranslate $sx0 - 32, $sy0 - 32, 0;
1780
1781 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1782
1783 if ($::CFG->{fow_enable}) {
1784 if ($::CFG->{fow_smooth}) { # smooth fog of war
1785 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1786 glConvolutionFilter2D
1787 GL_CONVOLUTION_2D,
1788 GL_ALPHA,
1789 3, 3,
1790 GL_ALPHA, GL_FLOAT,
1791 pack "f*",
1792 0.1, 0.1, 0.1,
1793 0.1, 0.2, 0.1,
1794 0.1, 0.1, 0.1,
1795 ;
1796 glEnable GL_CONVOLUTION_2D;
1797 }
1798
1799 $self->{fow_texture} = new CFClient::Texture
1800 w => $w,
1801 h => $h,
1802 data => $data,
1803 internalformat => GL_ALPHA,
1804 format => GL_ALPHA;
1805
1806 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1807
1808 glEnable GL_BLEND;
1809 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1810 glEnable GL_TEXTURE_2D;
1811 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1812
1813 glColor +($::CFG->{fow_intensity}) x 3, 1;
1814 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1815
1816 glDisable GL_TEXTURE_2D;
1817 glDisable GL_BLEND;
1818 }
1819
1820 # HACK BEGIN
1821 {
1822 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1823 my ($w, $h) = (250, 250);
1824
1825 glEnable GL_BLEND;
1826 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1827 glEnable GL_TEXTURE_2D;
1828 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1829
1830 $self->{mapmap_texture} =
1831 new CFClient::Texture
1832 w => $w,
1833 h => $h,
1834 data => $::MAP->mapmap ($w, $h),
1835 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1836
1837 $self->{mapmap_texture}->draw_quad (100, 100);
1838
1839 glDisable GL_TEXTURE_2D;
1840 glDisable GL_BLEND;
1841 }
1842 # HACK END
1843 }
1844
1845 glEndList;
1846 }
1847
1848 glPushMatrix;
1849 glCallList $self->{list};
1850 glPopMatrix;
1851
1852 if ($FOCUS != $self) {
1853 glColor 64/255, 64/255, 64/255;
1854 glLogicOp GL_AND;
1855 glEnable GL_COLOR_LOGIC_OP;
1856 glBegin GL_QUADS;
1857 glVertex 0, 0;
1858 glVertex 0, $::HEIGHT;
1859 glVertex $::WIDTH, $::HEIGHT;
1860 glVertex $::WIDTH, 0;
1861 glEnd;
1862 glDisable GL_COLOR_LOGIC_OP;
1863 }
1864}
1865
1866my %DIR = (
1867 SDLK_KP8, [1, "north"],
1868 SDLK_KP9, [2, "northeast"],
1869 SDLK_KP6, [3, "east"],
1870 SDLK_KP3, [4, "southeast"],
1871 SDLK_KP2, [5, "south"],
1872 SDLK_KP1, [6, "southwest"],
1873 SDLK_KP4, [7, "west"],
1874 SDLK_KP7, [8, "northwest"],
1875
1876 SDLK_UP, [1, "north"],
1877 SDLK_RIGHT, [3, "east"],
1878 SDLK_DOWN, [5, "south"],
1879 SDLK_LEFT, [7, "west"],
1880);
1881
1882sub key_down {
1883 my ($self, $ev) = @_;
1884
1885 my $mod = $ev->key_mod;
1886 my $sym = $ev->key_sym;
1887
1888 if ($sym == SDLK_KP5) {
1889 $::CONN->user_send ("command stay fire");
1890 } elsif ($sym == SDLK_a) {
1891 $::CONN->user_send ("command apply");
1892 } elsif ($sym == SDLK_QUOTE) {
1893 $self->emit ('activate_console');
1894 } elsif ($sym == SDLK_SLASH) {
1895 $self->emit ('activate_console' => '/');
1896 } elsif (exists $DIR{$sym}) {
1897 if ($mod & KMOD_SHIFT) {
1898 $self->{shft}++;
1899 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1900 } elsif ($mod & KMOD_CTRL) {
1901 $self->{ctrl}++;
1902 $::CONN->user_send ("command run $DIR{$sym}[0]");
1903 } else {
1904 $::CONN->user_send ("command $DIR{$sym}[1]");
1905 }
1906 }
1907}
1908
1909sub key_up {
1910 my ($self, $ev) = @_;
1911
1912 my $mod = $ev->key_mod;
1913 my $sym = $ev->key_sym;
1914
1915 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1916 $::CONN->user_send ("command fire_stop");
1917 }
1918 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1919 $::CONN->user_send ("command run_stop");
1920 }
1921}
1922
1923#############################################################################
1924
1925package CFClient::UI::Animator; 2029package CFClient::UI::Animator;
1926 2030
1927use SDL::OpenGL; 2031use CFClient::OpenGL;
1928 2032
1929our @ISA = CFClient::UI::Bin::; 2033our @ISA = CFClient::UI::Bin::;
1930 2034
1931sub moveto { 2035sub moveto {
1932 my ($self, $x, $y) = @_; 2036 my ($self, $x, $y) = @_;
1972 my $class = shift; 2076 my $class = shift;
1973 2077
1974 my $self = $class->SUPER::new ( 2078 my $self = $class->SUPER::new (
1975 state => 0, 2079 state => 0,
1976 connect_activate => \&toggle_flopper, 2080 connect_activate => \&toggle_flopper,
2081 can_events => 1,
1977 @_ 2082 @_
1978 ); 2083 );
1979 2084
1980 if ($self->{state}) { 2085 if ($self->{state}) {
1981 $self->{state} = 0; 2086 $self->{state} = 0;
2005 2110
2006package CFClient::UI::Root; 2111package CFClient::UI::Root;
2007 2112
2008our @ISA = CFClient::UI::Container::; 2113our @ISA = CFClient::UI::Container::;
2009 2114
2010use SDL::OpenGL; 2115use CFClient::OpenGL;
2116
2117sub check_size {
2118 my ($self) = @_;
2119
2120 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2121}
2011 2122
2012sub size_request { 2123sub size_request {
2013 ($::WIDTH, $::HEIGHT) 2124 ($::WIDTH, $::HEIGHT)
2014} 2125}
2015 2126
2016sub size_allocate { 2127sub configure {
2017 my ($self, $x, $y, $w, $h) = @_; 2128 my ($self, $x, $y, $w, $h) = @_;
2018 2129
2019 $self->_size_allocate ($x, $y, $w, $h); 2130 $self->SUPER::configure ($x, $y, $w, $h);
2020 2131
2021 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2022 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 }
2023} 2139}
2024 2140
2025sub _topleft { 2141sub _topleft {
2026 my ($self, $x, $y) = @_; 2142 my ($self, $x, $y) = @_;
2027 2143
2029} 2145}
2030 2146
2031sub update { 2147sub update {
2032 my ($self) = @_; 2148 my ($self) = @_;
2033 2149
2034 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2150 $self->check_size;
2035 ::refresh (); 2151 ::refresh ();
2036} 2152}
2037 2153
2038sub add { 2154sub add {
2039 my ($self, $widget) = @_; 2155 my ($self, $child) = @_;
2040 2156
2157 # integerize window positions
2158 $child->{x} = int $child->{x};
2159 $child->{y} = int $child->{y};
2160
2041 $self->SUPER::add ($widget); 2161 $self->SUPER::add ($child);
2042
2043 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2044} 2162}
2045 2163
2046sub on_refresh { 2164sub on_refresh {
2047 my ($self, $id, $cb) = @_; 2165 my ($self, $id, $cb) = @_;
2048 2166

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines