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.123 by root, Mon Apr 17 19:50:22 2006 UTC vs.
Revision 1.155 by root, Sun Apr 23 02:22:54 2006 UTC

8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $ROOT; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_text ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 48 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 49}
23 50
24sub feed_sdl_button_down_event { 51sub feed_sdl_button_down_event {
25 my ($ev) = @_; 52 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 53 my ($x, $y) = ($ev->{x}, $ev->{y});
27 54
28 if (!$BUTTON_STATE) { 55 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 56 my $widget = $ROOT->find_widget ($x, $y);
30 57
31 $GRAB = $widget; 58 $GRAB = $widget;
32 $GRAB->update if $GRAB; 59 $GRAB->update if $GRAB;
33 }
34 60
61 check_tooltip;
62 }
63
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 65
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 67}
39 68
40sub feed_sdl_button_up_event { 69sub feed_sdl_button_up_event {
41 my ($ev) = @_; 70 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 71 my ($x, $y) = ($ev->{x}, $ev->{y});
43 72
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 73 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 74
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 76
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 78
50 if (!$BUTTON_STATE) { 79 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 80 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 81 $grab->update if $grab;
53 $GRAB->update if $GRAB; 82 $GRAB->update if $GRAB;
83
84 check_tooltip;
54 } 85 }
55} 86}
56 87
57sub feed_sdl_motion_event { 88sub feed_sdl_motion_event {
58 my ($ev) = @_; 89 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 90 my ($x, $y) = ($ev->{x}, $ev->{y});
60 91
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 92 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 93
63 if ($widget != $HOVER) { 94 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 95 my $hover = $HOVER; $HOVER = $widget;
65 96
66 $hover->update if $hover && $hover->{can_hover}; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 98 $HOVER->update if $HOVER && $HOVER->{can_hover};
99
100 check_tooltip;
68 } 101 }
69 102
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 104}
72 105
87 120
88package CFClient::UI::Base; 121package CFClient::UI::Base;
89 122
90use strict; 123use strict;
91 124
92use SDL::OpenGL; 125use CFClient::OpenGL;
93 126
94sub new { 127sub new {
95 my $class = shift; 128 my $class = shift;
96 129
97 my $self = bless { 130 my $self = bless {
98 x => 0, 131 x => 0,
99 y => 0, 132 y => 0,
100 z => 0, 133 z => 0,
101 w => -1, 134 can_events => 1,
102 h => -1,
103 @_ 135 @_
104 }, $class; 136 }, $class;
105 137
106 for (keys %$self) { 138 for (keys %$self) {
107 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
110 } 142 }
111 143
112 $self 144 $self
113} 145}
114 146
147sub show {
148 my ($self) = @_;
149
150 return if $self->{parent};
151
152 $CFClient::UI::ROOT->add ($self);
153}
154
155sub hide {
156 my ($self) = @_;
157
158 return unless $self->{parent};
159
160 $self->{parent}->remove ($self);
161}
162
115sub move { 163sub move {
116 my ($self, $x, $y, $z) = @_; 164 my ($self, $x, $y, $z) = @_;
165
117 $self->{x} = int $x; 166 $self->{x} = int $x;
118 $self->{y} = int $y; 167 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 168 $self->{z} = $z if defined $z;
169
170 $self->update;
120} 171}
121 172
122sub needs_redraw { 173sub needs_redraw {
123 0 174 0
124} 175}
125 176
126sub size_request { 177sub size_request {
127 require Carp; 178 require Carp;
128 Carp::confess "size_request is abtract"; 179 Carp::confess "size_request is abstract";
129} 180}
130 181
131sub _size_allocate { 182sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 183 my ($self, $x, $y, $w, $h) = @_;
133 184
185 if ($self->{aspect}) {
186 my $w2 = List::Util::min $w, int $h * $self->{aspect};
187 my $h2 = List::Util::min $h, int $w / $self->{aspect};
188
189 # use alignment to adjust x, y
190
191 $x += int +($w - $w2) * 0.5;
192 $y += int +($h - $h2) * 0.5;
193
194 ($w, $h) = ($w2, $h2);
195 }
196
197 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 198 $self->{x} = $x;
135 $self->{y} = $y; 199 $self->{y} = $y;
200 $self->update;
201 }
136 202
137 return unless $self->{w} != $w || $self->{h} != $h; 203 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 204 $self->{w} = $w;
140 $self->{h} = $h; 205 $self->{h} = $h;
141 206
207 $self->size_allocate ($w, $h);
208 $self->update;
142 1 209 }
143} 210}
144 211
145sub size_allocate { 212sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 213 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 214}
150 215
151# return top left coordinates 216# return top left coordinates
152sub _topleft { 217sub _topleft {
153 my ($self, $x, $y) = @_; 218 my ($self, $x, $y) = @_;
247} 312}
248 313
249sub find_widget { 314sub find_widget {
250 my ($self, $x, $y) = @_; 315 my ($self, $x, $y) = @_;
251 316
317 return () unless $self->{can_events};
318
252 return $self 319 return $self
253 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 320 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
254 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 321 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
255 322
256 () 323 ()
260 my ($self, $parent) = @_; 327 my ($self, $parent) = @_;
261 328
262 Scalar::Util::weaken ($self->{parent} = $parent); 329 Scalar::Util::weaken ($self->{parent} = $parent);
263} 330}
264 331
332sub check_size {
333 my ($self) = @_;
334
335 return unless $self->{parent};
336
337 my ($w, $h) = $self->size_request;
338
339 if ($w != $self->{req_w} || $h != $self->{req_h}) {
340 $self->{req_w} = $w;
341 $self->{req_h} = $h;
342
343 $self->{parent}->check_size;
344 }
345}
346
265sub update { 347sub update {
266 my ($self) = @_; 348 my ($self) = @_;
267 349
268 $self->{parent}->update 350 $self->{parent}->update
269 if $self->{parent}; 351 if $self->{parent};
270} 352}
271 353
272sub connect { 354sub connect {
273 my ($self, $signal, $cb) = @_; 355 my ($self, $signal, $cb) = @_;
274 356
275 push @{ $self->{cb}{$signal} }, $cb; 357 push @{ $self->{signal_cb}{$signal} }, $cb;
276} 358}
277 359
278sub emit { 360sub emit {
279 my ($self, $signal, @args) = @_; 361 my ($self, $signal, @args) = @_;
280 362
363 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
281 $_->($self, @args) 364 $cb->($self, @args);
282 for @{$self->{cb}{$signal} || []}; 365 }
283} 366}
284 367
285sub DESTROY { 368sub DESTROY {
286 my ($self) = @_; 369 my ($self) = @_;
287 370
293package CFClient::UI::DrawBG; 376package CFClient::UI::DrawBG;
294 377
295our @ISA = CFClient::UI::Base::; 378our @ISA = CFClient::UI::Base::;
296 379
297use strict; 380use strict;
298use SDL::OpenGL; 381use CFClient::OpenGL;
299 382
300sub new { 383sub new {
301 my $class = shift; 384 my $class = shift;
302 385
303 # range [value, low, high, page] 386 # range [value, low, high, page]
332 415
333package CFClient::UI::Empty; 416package CFClient::UI::Empty;
334 417
335our @ISA = CFClient::UI::Base::; 418our @ISA = CFClient::UI::Base::;
336 419
420sub new {
421 my ($class, %arg) = @_;
422 $class->SUPER::new (can_events => 0, %arg);
423}
424
337sub size_request { 425sub size_request {
338 (0, 0) 426 (0, 0)
339} 427}
340 428
341sub draw { } 429sub draw { }
349sub new { 437sub new {
350 my ($class, %arg) = @_; 438 my ($class, %arg) = @_;
351 439
352 my $children = delete $arg{children} || []; 440 my $children = delete $arg{children} || [];
353 441
354 my $self = $class->SUPER::new (children => [], %arg); 442 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
355 $self->add ($_) for @$children; 443 $self->add ($_) for @$children;
356 444
357 $self 445 $self
358} 446}
359 447
367 $self->{children} = [ 455 $self->{children} = [
368 sort { $a->{z} <=> $b->{z} } 456 sort { $a->{z} <=> $b->{z} }
369 @{$self->{children}}, $child 457 @{$self->{children}}, $child
370 ]; 458 ];
371 459
372 $self->{w} = $self->{h} = -1; 460 $child->check_size;
461}
462
463sub remove {
464 my ($self, $child) = @_;
465
466 delete $child->{parent};
467
468 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
469
470 $self->check_size;
373 $self->update; 471 $self->update;
374}
375
376sub remove {
377 my ($self, $widget) = @_;
378
379 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
380
381 $self->size_allocate (0, 0, $self->{w}, $self->{h});
382} 472}
383 473
384sub find_widget { 474sub find_widget {
385 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
386 476
416 506
417 $class->SUPER::new (children => [$child], %arg) 507 $class->SUPER::new (children => [$child], %arg)
418} 508}
419 509
420sub add { 510sub add {
421 my ($self, $widget) = @_; 511 my ($self, $child) = @_;
422 512
423 $self->{children} = []; 513 $self->{children} = [];
424 514
425 $self->SUPER::add ($widget); 515 $self->SUPER::add ($child);
426} 516}
427 517
428sub remove { 518sub remove {
429 my ($self, $widget) = @_; 519 my ($self, $widget) = @_;
430 520
439sub size_request { 529sub size_request {
440 $_[0]{children}[0]->size_request 530 $_[0]{children}[0]->size_request
441} 531}
442 532
443sub size_allocate { 533sub size_allocate {
444 my ($self, $x, $y, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
445 535
446 $self->_size_allocate ($x, $y, $w, $h) or return;
447
448 $self->{children}[0]->size_allocate (0, 0, $w, $h); 536 $self->{children}[0]->configure (0, 0, $w, $h);
449} 537}
450 538
451############################################################################# 539#############################################################################
452 540
453package CFClient::UI::Window; 541package CFClient::UI::Window;
454 542
455our @ISA = CFClient::UI::Bin::; 543our @ISA = CFClient::UI::Bin::;
456 544
457use SDL::OpenGL; 545use CFClient::OpenGL;
458 546
459sub new { 547sub new {
460 my ($class, %arg) = @_; 548 my ($class, %arg) = @_;
461 549
462 my $self = $class->SUPER::new (%arg); 550 my $self = $class->SUPER::new (%arg);
479 $self->child->draw; 567 $self->child->draw;
480 }; 568 };
481} 569}
482 570
483sub size_allocate { 571sub size_allocate {
484 my ($self, $x, $y, $w, $h) = @_; 572 my ($self, $w, $h) = @_;
485 573
486 $self->_size_allocate ($x, $y, $w, $h) or return;
487
488 $self->child->size_allocate (0, 0, $w, $h); 574 $self->child->configure (0, 0, $w, $h);
489 575
490 $self->render_chld; 576 $self->render_chld;
491} 577}
492 578
493sub _draw { 579sub _draw {
509 glDisable GL_TEXTURE_2D; 595 glDisable GL_TEXTURE_2D;
510} 596}
511 597
512############################################################################# 598#############################################################################
513 599
600package CFClient::UI::ViewPort;
601
602our @ISA = CFClient::UI::Window::;
603
604sub new { die }
605
606sub size_request {
607 my ($self) = @_;
608
609 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
610 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
611
612 @$self{qw(child_w child_h)}
613}
614
615sub _draw {
616 my ($self) = @_;
617
618 $self->{children}[1]->draw;
619}
620
621
622#############################################################################
623
514package CFClient::UI::Frame; 624package CFClient::UI::Frame;
515 625
516our @ISA = CFClient::UI::Bin::; 626our @ISA = CFClient::UI::Bin::;
517 627
518use SDL::OpenGL; 628use 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 629
568sub new { 630sub new {
569 my $class = shift; 631 my $class = shift;
570 632
571 # TODO: user_x, user_y, overwrite moveto?
572
573 $class->SUPER::new ( 633 my $self = $class->SUPER::new (
574 bg => [1, 1, 1, 1], 634 bg => [1, 1, 1, 1],
575 border_bg => [1, 1, 1, 1], 635 border_bg => [1, 1, 1, 1],
576 border => int $::FONTSIZE * 0.8, 636 border => 0.8,
577 @_ 637 @_
638 );
639
640 $self
641}
642
643sub set_size {
644 my ($self, $w, $h) = @_;
645 $self->{req_w} = $w;
646 $self->{req_h} = $h;
647 $self->check_size;
648}
649
650sub size_request {
651 my ($self) = @_;
652 ($self->{req_w}, $self->{req_h})
653}
654
655sub size_allocate {
656 my ($self, $w, $h) = @_;
657 $self->{w} = $w;
658 $self->{h} = $h;
659 $self->child->configure (0, 0, $w, $h);
660}
661
662sub _draw {
663 my ($self) = @_;
664
665 my ($w, $h) = ($self->{w}, $self->{h});
666
667 glEnable GL_BLEND;
668 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
669 glEnable GL_TEXTURE_2D;
670 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
671
672# glBegin GL_QUADS;
673# glColor 0, 0, 0, 0;
674# glVertex 0 , 0;
675# glVertex 0 , $h;
676# glVertex $w, $h;
677# glVertex $w, 0;
678# glEnd;
679
680
681 $self->child->draw;
682 glDisable GL_BLEND;
683 glDisable GL_TEXTURE_2D;
684}
685
686#############################################################################
687
688package CFClient::UI::FancyFrame;
689
690our @ISA = CFClient::UI::Bin::;
691
692use CFClient::OpenGL;
693
694my @tex =
695 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
696 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
697
698sub new {
699 my $class = shift;
700
701 # TODO: user_x, user_y, overwrite moveto?
702
703 my $self = $class->SUPER::new (
704 bg => [1, 1, 1, 1],
705 border_bg => [1, 1, 1, 1],
706 border => 0.8,
707 can_events => 1,
708 @_
709 );
710
711 $self->{title} &&= new CFClient::UI::Label
712 align => 0,
713 valign => 1,
714 text => $self->{title},
715 fontsize => 1;
716
717 $self
718}
719
720sub border {
721 int $_[0]{border} * $::FONTSIZE
722}
723
724sub size_request {
725 my ($self) = @_;
726
727 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
728
729 my ($w, $h) = $self->SUPER::size_request;
730
731 (
732 $w + $self->border * 2,
733 $h + $self->border * 2,
578 ) 734 )
579} 735}
580 736
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 { 737sub size_allocate {
595 my ($self, $x, $y, $w, $h) = @_; 738 my ($self, $w, $h) = @_;
596 739
597 $self->_size_allocate ($x, $y, $w, $h) or return;
598
599 $h -= List::Util::max 0, $self->{border} * 2; 740 $h -= List::Util::max 0, $self->border * 2;
600 $w -= List::Util::max 0, $self->{border} * 2; 741 $w -= List::Util::max 0, $self->border * 2;
601 742
743 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
744 if $self->{title};
745
602 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 746 $self->child->configure ($self->border, $self->border, $w, $h);
603} 747}
604 748
605sub button_down { 749sub button_down {
606 my ($self, $ev, $x, $y) = @_; 750 my ($self, $ev, $x, $y) = @_;
607 751
752 my $border = $self->border;
753
608 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 754 if ($x < $self->{w} && $x >= $self->{w} - $border
609 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 755 && $y < $self->{h} && $y >= $self->{h} - $border) {
610 756
611 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 757 my ($ox, $oy) = ($ev->{x}, $ev->{y});
612 my ($bw, $bh) = ($self->{w}, $self->{h}); 758 my ($bw, $bh) = ($self->{w}, $self->{h});
613 759
614 $self->{motion} = sub { 760 $self->{motion} = sub {
615 my ($ev, $x, $y) = @_; 761 my ($ev, $x, $y) = @_;
616 762
617 ($x, $y) = ($ev->motion_x, $ev->motion_y); 763 ($x, $y) = ($ev->{x}, $ev->{y});
618 764
619 $self->{user_w} = $bw + $x - $ox; 765 $self->{user_w} = $bw + $x - $ox;
620 $self->{user_h} = $bh + $y - $oy; 766 $self->{user_h} = $bh + $y - $oy;
621 $self->update; 767 $self->check_size;
622 }; 768 };
623 769
624 } elsif ($x >= 0 && $x < $self->{w} 770 } elsif ($x >= 0 && $x < $self->{w}
625 && $y >= 0 && $y < $self->{border}) { 771 && $y >= 0 && $y < $border) {
626 772
627 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 773 my ($ox, $oy) = ($ev->{x}, $ev->{y});
628 my ($bx, $by) = ($self->{x}, $self->{y}); 774 my ($bx, $by) = ($self->{x}, $self->{y});
629 775
630 $self->{motion} = sub { 776 $self->{motion} = sub {
631 my ($ev, $x, $y) = @_; 777 my ($ev, $x, $y) = @_;
632 778
633 ($x, $y) = ($ev->motion_x, $ev->motion_y); 779 ($x, $y) = ($ev->{x}, $ev->{y});
634 780
635 $self->move ($bx + $x - $ox, $by + $y - $oy); 781 $self->move ($bx + $x - $ox, $by + $y - $oy);
636 $self->update; 782 $self->update;
637 }; 783 };
638 } 784 }
659 glEnable GL_BLEND; 805 glEnable GL_BLEND;
660 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
661 glEnable GL_TEXTURE_2D; 807 glEnable GL_TEXTURE_2D;
662 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
663 809
810 my $border = $self->border;
811
664 glColor @{ $self->{border_bg} }; 812 glColor @{ $self->{border_bg} };
665 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 813 $tex[1]->draw_quad (0, 0, $w, $border);
666 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 814 $tex[3]->draw_quad (0, $border, $border, $ch);
667 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 815 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
668 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 816 $tex[4]->draw_quad (0, $h - $border, $w, $border);
669 817
670 my $bg = $tex[0]; 818 my $bg = $tex[0];
671 819
672 # TODO: repeat texture not scale 820 # TODO: repeat texture not scale
673 my $rep_x = $cw / $bg->{w}; 821 my $rep_x = $cw / $bg->{w};
676 glColor @{ $self->{bg} }; 824 glColor @{ $self->{bg} };
677 825
678 $bg->{s} = $rep_x; 826 $bg->{s} = $rep_x;
679 $bg->{t} = $rep_y; 827 $bg->{t} = $rep_y;
680 $bg->{wrap_mode} = 1; 828 $bg->{wrap_mode} = 1;
681 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 829 $bg->draw_quad ($border, $border, $cw, $ch);
682 830
683 glDisable GL_TEXTURE_2D; 831 glDisable GL_TEXTURE_2D;
684 glDisable GL_BLEND; 832 glDisable GL_BLEND;
685 833
834 $self->{title}->draw if $self->{title};
686 $self->child->draw; 835 $self->child->draw;
687} 836}
688 837
689############################################################################# 838#############################################################################
690 839
692 841
693our @ISA = CFClient::UI::Base::; 842our @ISA = CFClient::UI::Base::;
694 843
695use List::Util qw(max sum); 844use List::Util qw(max sum);
696 845
697use SDL::OpenGL; 846use CFClient::OpenGL;
698 847
699sub new { 848sub new {
700 my $class = shift; 849 my $class = shift;
701 850
702 $class->SUPER::new ( 851 $class->SUPER::new (
709 my ($self, $x, $y, $child) = @_; 858 my ($self, $x, $y, $child) = @_;
710 859
711 $child->set_parent ($self); 860 $child->set_parent ($self);
712 $self->{children}[$y][$x] = $child; 861 $self->{children}[$y][$x] = $child;
713 862
714 $self->{w} = $self->{h} = -1; 863 $child->check_size;
715 $self->update;
716} 864}
717 865
718# TODO: move to container class maybe? send childs a signal on removal? 866# TODO: move to container class maybe? send childs a signal on removal?
719sub clear { 867sub clear {
720 my ($self) = @_; 868 my ($self) = @_;
733 or next; 881 or next;
734 882
735 for my $x (0 .. $#$row) { 883 for my $x (0 .. $#$row) {
736 my $widget = $row->[$x] 884 my $widget = $row->[$x]
737 or next; 885 or next;
738 my ($w, $h) = $widget->size_request; 886 my ($w, $h) = @$widget{qw(req_w req_h)};
739 887
740 $w[$x] = max $w[$x], $w; 888 $w[$x] = max $w[$x], $w;
741 $h[$y] = max $h[$y], $h; 889 $h[$y] = max $h[$y], $h;
742 } 890 }
743 } 891 }
755 (sum @$hs), 903 (sum @$hs),
756 ) 904 )
757} 905}
758 906
759sub size_allocate { 907sub size_allocate {
760 my ($self, $x, $y, $w, $h) = @_; 908 my ($self, $w, $h) = @_;
761
762 $self->_size_allocate ($x, $y, $w, $h) or return;
763 909
764 my ($ws, $hs) = $self->get_wh; 910 my ($ws, $hs) = $self->get_wh;
765 911
766 my $req_w = sum @$ws; 912 my $req_w = sum @$ws;
767 my $req_h = sum @$hs; 913 my $req_h = sum @$hs;
789 935
790 for my $c (0 .. $#$row) { 936 for my $c (0 .. $#$row) {
791 my $col_w = $ws->[$c]; 937 my $col_w = $ws->[$c];
792 938
793 if (my $widget = $row->[$c]) { 939 if (my $widget = $row->[$c]) {
794 $widget->size_allocate ($x, $y, $col_w, $row_h); 940 $widget->configure ($x, $y, $col_w, $row_h);
795 } 941 }
796 942
797 $x += $col_w; 943 $x += $col_w;
798 } 944 }
799 945
844 (List::Util::max map $_->[1], @alloc), 990 (List::Util::max map $_->[1], @alloc),
845 ) 991 )
846} 992}
847 993
848sub size_allocate { 994sub size_allocate {
849 my ($self, $x, $y, $w, $h) = @_; 995 my ($self, $w, $h) = @_;
850
851 $self->_size_allocate ($x, $y, $w, $h);
852 996
853 ($h, $w) = ($w, $h); 997 ($h, $w) = ($w, $h);
854 998
855 my $children = $self->{children}; 999 my $children = $self->{children};
856 1000
857 my @h = map +($_->size_request)[0], @$children; 1001 my @h = map $_->{req_w}, @$children;
858 1002
859 my $req_h = List::Util::sum @h; 1003 my $req_h = List::Util::sum @h;
860 1004
861 if ($req_h > $h) { 1005 if ($req_h > $h) {
862 # ah well, not enough space 1006 # ah well, not enough space
878 1022
879 my $y = 0; 1023 my $y = 0;
880 for (0 .. $#$children) { 1024 for (0 .. $#$children) {
881 my $child = $children->[$_]; 1025 my $child = $children->[$_];
882 my $h = $h[$_]; 1026 my $h = $h[$_];
883 $child->size_allocate ($y, 0, $h, $w); 1027 $child->configure ($y, 0, $h, $w);
884 1028
885 $y += $h; 1029 $y += $h;
886 } 1030 }
1031
1032 1
887} 1033}
888 1034
889############################################################################# 1035#############################################################################
890 1036
891package CFClient::UI::VBox; 1037package CFClient::UI::VBox;
904 (List::Util::sum map $_->[1], @alloc), 1050 (List::Util::sum map $_->[1], @alloc),
905 ) 1051 )
906} 1052}
907 1053
908sub size_allocate { 1054sub size_allocate {
909 my ($self, $x, $y, $w, $h) = @_; 1055 my ($self, $w, $h) = @_;
910
911 $self->_size_allocate ($x, $y, $w, $h);
912 1056
913 my $children = $self->{children}; 1057 my $children = $self->{children};
914 1058
915 my @h = map +($_->size_request)[1], @$children; 1059 my @h = map $_->{req_h}, @$children;
916 1060
917 my $req_h = List::Util::sum @h; 1061 my $req_h = List::Util::sum @h;
918 1062
919 if ($req_h > $h) { 1063 if ($req_h > $h) {
920 # ah well, not enough space 1064 # ah well, not enough space
934 1078
935 my $y = 0; 1079 my $y = 0;
936 for (0 .. $#$children) { 1080 for (0 .. $#$children) {
937 my $child = $children->[$_]; 1081 my $child = $children->[$_];
938 my $h = $h[$_]; 1082 my $h = $h[$_];
939 $child->size_allocate (0, $y, $w, $h); 1083 $child->configure (0, $y, $w, $h);
940 1084
941 $y += $h; 1085 $y += $h;
942 } 1086 }
1087
1088 1
943} 1089}
944 1090
945############################################################################# 1091#############################################################################
946 1092
947package CFClient::UI::Label; 1093package CFClient::UI::Label;
948 1094
949our @ISA = CFClient::UI::Base::; 1095our @ISA = CFClient::UI::Base::;
950 1096
951use SDL::OpenGL; 1097use CFClient::OpenGL;
952 1098
953sub new { 1099sub new {
954 my ($class, %arg) = @_; 1100 my ($class, %arg) = @_;
955 1101
956 my $self = $class->SUPER::new ( 1102 my $self = $class->SUPER::new (
957 fg => [1, 1, 1], 1103 fg => [1, 1, 1],
958 fontsize => $::FONTSIZE, 1104 fontsize => 1,
959 text => "", 1105 text => "",
960 align => -1, 1106 align => -1,
961 valign => -1, 1107 valign => -1,
962 padding => 2, 1108 padding => 2,
963 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
964 %arg 1111 %arg
965 ); 1112 );
966 1113
967 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1114 if (exists $self->{template}) {
1115 my $layout = new CFClient::Layout;
1116 $layout->set_text (delete $self->{template});
1117 $self->{template} = $layout;
1118 }
968 1119
969 $self->set_text (delete $self->{text}) if exists $self->{text}; 1120 $self->set_text (delete $self->{text}) if exists $self->{text};
970 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1121 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
971 1122
972 $self 1123 $self
986 my ($self, $text) = @_; 1137 my ($self, $text) = @_;
987 1138
988 $self->{layout}->set_text ($text); 1139 $self->{layout}->set_text ($text);
989 1140
990 delete $self->{texture}; 1141 delete $self->{texture};
1142 $self->check_size;
991 $self->update; 1143 $self->update;
992} 1144}
993 1145
994sub set_markup { 1146sub set_markup {
995 my ($self, $markup) = @_; 1147 my ($self, $markup) = @_;
996 1148
997 $self->{layout}->set_markup ($markup); 1149 $self->{layout}->set_markup ($markup);
998 1150
999 delete $self->{texture}; 1151 delete $self->{texture};
1152 $self->check_size;
1000 $self->update; 1153 $self->update;
1001} 1154}
1002 1155
1003sub size_request { 1156sub size_request {
1004 my ($self) = @_; 1157 my ($self) = @_;
1005 1158
1006 $self->{layout}->set_width; 1159 $self->{layout}->set_width;
1007 $self->{layout}->set_height ($self->{fontsize}); 1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1008 1161
1009 my ($w, $h) = $self->{layout}->size; 1162 my ($w, $h) = $self->{layout}->size;
1163
1164 if (exists $self->{template}) {
1165 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1166
1167 my ($w2, $h2) = $self->{template}->size;
1168
1169 $w = List::Util::max $w, $w2;
1170 $h = List::Util::max $h, $h2;
1171 }
1010 1172
1011 ( 1173 (
1012 $w + $self->{padding} * 2, 1174 $w + $self->{padding} * 2,
1013 $h + $self->{padding} * 2, 1175 $h + $self->{padding} * 2,
1014 ) 1176 )
1015} 1177}
1016 1178
1017sub size_allocate { 1179sub size_allocate {
1018 my ($self, $x, $y, $w, $h) = @_; 1180 my ($self, $w, $h) = @_;
1019
1020 $self->_size_allocate ($x, $y, $w, $h) or return;
1021 1181
1022 delete $self->{texture}; 1182 delete $self->{texture};
1023} 1183}
1024 1184
1025sub update { 1185sub set_fontsize {
1026 my ($self) = @_; 1186 my ($self, $fontsize) = @_;
1027 1187
1188 $self->{fontsize} = $fontsize;
1028 delete $self->{texture}; 1189 delete $self->{texture};
1190 $self->check_size;
1029 $self->SUPER::update; 1191 $self->update;
1030} 1192}
1031 1193
1032sub _draw { 1194sub _draw {
1033 my ($self) = @_; 1195 my ($self) = @_;
1034 1196
1035 my $tex = $self->{texture} ||= do { 1197 my $tex = $self->{texture} ||= do {
1036 $self->{layout}->set_width ($self->{w}); 1198 $self->{layout}->set_width ($self->{w});
1037 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1199 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1038 new_from_layout CFClient::Texture $self->{layout} 1200 new_from_layout CFClient::Texture $self->{layout}
1039 }; 1201 };
1040 1202
1041 glEnable GL_BLEND; 1203 glEnable GL_BLEND;
1042 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1067 1229
1068package CFClient::UI::EntryBase; 1230package CFClient::UI::EntryBase;
1069 1231
1070our @ISA = CFClient::UI::Label::; 1232our @ISA = CFClient::UI::Label::;
1071 1233
1072use SDL; 1234use CFClient::OpenGL;
1073use SDL::OpenGL;
1074 1235
1075sub new { 1236sub new {
1076 my $class = shift; 1237 my $class = shift;
1077 1238
1078 $class->SUPER::new ( 1239 $class->SUPER::new (
1081 active_bg => [1, 1, 1, 0.5], 1242 active_bg => [1, 1, 1, 0.5],
1082 active_fg => [0, 0, 0], 1243 active_fg => [0, 0, 0],
1083 can_hover => 1, 1244 can_hover => 1,
1084 can_focus => 1, 1245 can_focus => 1,
1085 valign => 0, 1246 valign => 0,
1247 can_events => 1,
1086 @_ 1248 @_
1087 ) 1249 )
1088} 1250}
1089 1251
1090sub _set_text { 1252sub _set_text {
1092 1254
1093 delete $self->{cur_h}; 1255 delete $self->{cur_h};
1094 1256
1095 return if $self->{text} eq $text; 1257 return if $self->{text} eq $text;
1096 1258
1259 delete $self->{texture};
1260
1097 $self->{last_activity} = $::NOW; 1261 $self->{last_activity} = $::NOW;
1098 $self->{text} = $text; 1262 $self->{text} = $text;
1099 1263
1100 $text =~ s/./*/g if $self->{hidden}; 1264 $text =~ s/./*/g if $self->{hidden};
1101 $self->{layout}->set_text ("$text "); 1265 $self->{layout}->set_text ("$text ");
1114 1278
1115 ($w + 1, $h) # add 1 for cursor 1279 ($w + 1, $h) # add 1 for cursor
1116} 1280}
1117 1281
1118sub size_allocate { 1282sub size_allocate {
1119 my ($self, $x, $y, $w, $h) = @_; 1283 my ($self, $w, $h) = @_;
1120 1284
1121 $self->SUPER::size_allocate ($x, $y, $w, $h); 1285 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1122
1123 $self->_set_text ($self->{text});
1124} 1286}
1125 1287
1126sub set_text { 1288sub set_text {
1127 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1128 1290
1132} 1294}
1133 1295
1134sub key_down { 1296sub key_down {
1135 my ($self, $ev) = @_; 1297 my ($self, $ev) = @_;
1136 1298
1137 my $mod = $ev->key_mod; 1299 my $mod = $ev->{mod};
1138 my $sym = $ev->key_sym; 1300 my $sym = $ev->{sym};
1139
1140 my $uni = $ev->key_unicode; 1301 my $uni = $ev->{unicode};
1141 1302
1142 my $text = $self->get_text; 1303 my $text = $self->get_text;
1143 1304
1144 if ($sym == SDLK_BACKSPACE) { 1305 if ($sym == 8) {
1145 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1306 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1146 } elsif ($sym == SDLK_DELETE) { 1307 } elsif ($sym == 127) {
1147 substr $text, $self->{cursor}, 1, ""; 1308 substr $text, $self->{cursor}, 1, "";
1148 } elsif ($sym == SDLK_LEFT) { 1309 } elsif ($sym == CFClient::SDLK_LEFT) {
1149 --$self->{cursor} if $self->{cursor}; 1310 --$self->{cursor} if $self->{cursor};
1150 } elsif ($sym == SDLK_RIGHT) { 1311 } elsif ($sym == CFClient::SDLK_RIGHT) {
1151 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1312 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1152 } elsif ($sym == SDLK_HOME) { 1313 } elsif ($sym == CFClient::SDLK_HOME) {
1153 $self->{cursor} = 0; 1314 $self->{cursor} = 0;
1154 } elsif ($sym == SDLK_END) { 1315 } elsif ($sym == CFClient::SDLK_END) {
1155 $self->{cursor} = length $text; 1316 $self->{cursor} = length $text;
1156 } elsif ($sym == SDLK_ESCAPE) { 1317 } elsif ($sym == 27) {
1157 $self->emit ('escape'); 1318 $self->emit ('escape');
1158 } elsif ($uni) { 1319 } elsif ($uni) {
1159 substr $text, $self->{cursor}++, 0, chr $uni; 1320 substr $text, $self->{cursor}++, 0, chr $uni;
1160 } 1321 }
1161 1322
1236 1397
1237package CFClient::UI::Entry; 1398package CFClient::UI::Entry;
1238 1399
1239our @ISA = CFClient::UI::EntryBase::; 1400our @ISA = CFClient::UI::EntryBase::;
1240 1401
1241use SDL; 1402use CFClient::OpenGL;
1242use SDL::OpenGL;
1243 1403
1244sub key_down { 1404sub key_down {
1245 my ($self, $ev) = @_; 1405 my ($self, $ev) = @_;
1246 1406
1247 my $sym = $ev->key_sym; 1407 my $sym = $ev->{sym};
1248 1408
1249 if ($sym == SDLK_RETURN) { 1409 if ($sym == 13) {
1250 $self->emit (activate => $self->get_text); 1410 $self->emit (activate => $self->get_text);
1251 $self->update; 1411 $self->update;
1252 1412
1253 } else { 1413 } else {
1254 $self->SUPER::key_down ($ev); 1414 $self->SUPER::key_down ($ev);
1260 1420
1261package CFClient::UI::Button; 1421package CFClient::UI::Button;
1262 1422
1263our @ISA = CFClient::UI::Label::; 1423our @ISA = CFClient::UI::Label::;
1264 1424
1265use SDL; 1425use CFClient::OpenGL;
1266use SDL::OpenGL;
1267 1426
1268my @tex = 1427my @tex =
1269 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1270 qw(b1_button_active.png); 1429 qw(b1_button_active.png);
1271 1430
1272sub new { 1431sub new {
1273 my $class = shift; 1432 my $class = shift;
1274 1433
1276 padding => 4, 1435 padding => 4,
1277 fg => [1, 1, 1], 1436 fg => [1, 1, 1],
1278 bg => [1, 1, 1, 0.2], 1437 bg => [1, 1, 1, 0.2],
1279 active_fg => [0, 0, 1], 1438 active_fg => [0, 0, 1],
1280 can_hover => 1, 1439 can_hover => 1,
1440 align => 0,
1441 valign => 0,
1442 can_events => 1,
1281 @_ 1443 @_
1282 ) 1444 )
1283} 1445}
1284 1446
1285sub button_up { 1447sub button_up {
1319package CFClient::UI::CheckBox; 1481package CFClient::UI::CheckBox;
1320 1482
1321our @ISA = CFClient::UI::DrawBG::; 1483our @ISA = CFClient::UI::DrawBG::;
1322 1484
1323my @tex = 1485my @tex =
1324 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1486 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1325 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1487 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1326 1488
1327use SDL; 1489use CFClient::OpenGL;
1328use SDL::OpenGL;
1329 1490
1330sub new { 1491sub new {
1331 my $class = shift; 1492 my $class = shift;
1332 1493
1333 $class->SUPER::new ( 1494 $class->SUPER::new (
1344 my ($self) = @_; 1505 my ($self) = @_;
1345 1506
1346 ($self->{padding} * 2 + 6) x 2 1507 ($self->{padding} * 2 + 6) x 2
1347} 1508}
1348 1509
1349sub size_allocate {
1350 my ($self, $x, $y, $w, $h) = @_;
1351
1352 $self->_size_allocate ($x, $y, $w, $h);
1353}
1354
1355sub button_down { 1510sub button_down {
1356 my ($self, $ev, $x, $y) = @_; 1511 my ($self, $ev, $x, $y) = @_;
1357 1512
1358 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1513 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1359 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1514 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1385 glDisable GL_BLEND; 1540 glDisable GL_BLEND;
1386} 1541}
1387 1542
1388############################################################################# 1543#############################################################################
1389 1544
1545package CFClient::UI::Image;
1546
1547our @ISA = CFClient::UI::Base::;
1548
1549use CFClient::OpenGL;
1550use Carp qw/confess/;
1551
1552our %loaded_images;
1553
1554sub new {
1555 my $class = shift;
1556
1557 my $self = $class->SUPER::new (can_events => 0, @_);
1558
1559 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1560
1561 $loaded_images{$self->{image}} ||=
1562 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1563
1564 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1565
1566 Scalar::Util::weaken $loaded_images{$self->{image}};
1567
1568 $self->{aspect} = $tex->{w} / $tex->{h};
1569
1570 $self
1571}
1572
1573sub size_request {
1574 my ($self) = @_;
1575
1576 ($self->{tex}->{w}, $self->{tex}->{h})
1577}
1578
1579sub _draw {
1580 my ($self) = @_;
1581
1582 my $tex = $self->{tex};
1583
1584 my ($w, $h) = ($self->{w}, $self->{h});
1585
1586 if ($self->{rot90}) {
1587 glRotate 90, 0, 0, 1;
1588 glTranslate 0, -$self->{w}, 0;
1589
1590 ($w, $h) = ($h, $w);
1591 }
1592
1593 glEnable GL_BLEND;
1594 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1595 glEnable GL_TEXTURE_2D;
1596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1597
1598 $tex->draw_quad (0, 0, $w, $h);
1599
1600 glDisable GL_BLEND;
1601 glDisable GL_TEXTURE_2D;
1602}
1603
1604#############################################################################
1605
1606package CFClient::UI::VGauge;
1607
1608our @ISA = CFClient::UI::Base::;
1609
1610use CFClient::OpenGL;
1611
1612my %tex = (
1613 food => [
1614 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1615 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1616 ],
1617 grace => [
1618 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1619 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1620 ],
1621 hp => [
1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1623 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1624 ],
1625 mana => [
1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1627 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1628 ],
1629);
1630
1631# eg. VGauge->new (gauge => 'food'), default gauge: food
1632sub new {
1633 my $class = shift;
1634
1635 my $self = $class->SUPER::new (
1636 type => 'food',
1637 @_
1638 );
1639
1640 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1641
1642 $self
1643}
1644
1645sub size_request {
1646 my ($self) = @_;
1647
1648 #my $tex = $tex{$self->{type}}[0];
1649 #@$tex{qw(w h)}
1650 (0, 0)
1651}
1652
1653sub set_max {
1654 my ($self, $max) = @_;
1655
1656 $self->{max_val} = $max;
1657}
1658
1659sub set_value {
1660 my ($self, $val, $max) = @_;
1661
1662 $self->set_max ($max)
1663 if defined $max;
1664
1665 $max = $self->{max_val};
1666 $self->{val} = $val;
1667
1668 $self->update;
1669}
1670
1671sub _draw {
1672 my ($self) = @_;
1673
1674 my $tex = $tex{$self->{type}};
1675
1676 my ($w, $h) = ($self->{w}, $self->{h});
1677
1678 if ($self->{vertical}) {
1679 glRotate 90, 0, 0, 1;
1680 glTranslate 0, -$self->{w}, 0;
1681
1682 ($w, $h) = ($h, $w);
1683 }
1684
1685 my $ycut = $self->{val} / ($self->{max_val} || 1);
1686 $ycut = 1 if $self->{val} > $self->{max_val};
1687
1688 my $t1 = $tex->[0];
1689 my $t2 = $tex->[1];
1690
1691 glEnable GL_BLEND;
1692 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1693 glEnable GL_TEXTURE_2D;
1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1695
1696 my $h1 = $self->{h} - $ycut * $self->{h};
1697 my $h2 = $ycut * $self->{h};
1698
1699 glBindTexture GL_TEXTURE_2D, $t1->{name};
1700 glBegin GL_QUADS;
1701 glTexCoord 0 , 0; glVertex 0 , 0;
1702 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1703 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1704 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1705 glEnd;
1706
1707 glBindTexture GL_TEXTURE_2D, $t2->{name};
1708 glBegin GL_QUADS;
1709 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1710 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1711 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1712 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1713 glEnd;
1714
1715 glDisable GL_BLEND;
1716 glDisable GL_TEXTURE_2D;
1717}
1718
1719#############################################################################
1720
1721package CFClient::UI::Gauge;
1722
1723our @ISA = CFClient::UI::VBox::;
1724
1725sub new {
1726 my ($class, %arg) = @_;
1727
1728 my $self = $class->SUPER::new (
1729 tooltip => $arg{type},
1730 %arg,
1731 );
1732
1733 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1734 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1735 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1736
1737 $self
1738}
1739
1740sub set_fontsize {
1741 my ($self, $fsize) = @_;
1742
1743 $self->{value}->set_fontsize ($fsize);
1744 $self->{max} ->set_fontsize ($fsize);
1745}
1746
1747sub set_value {
1748 my ($self, $val, $max) = @_;
1749
1750 $self->set_max ($max)
1751 if defined $max;
1752
1753 $self->{gauge}->set_value ($val, $max);
1754 $self->{value}->set_text ($val);
1755}
1756
1757sub set_max {
1758 my ($self, $max) = @_;
1759
1760 $self->{gauge}->set_max ($max);
1761 $self->{max}->set_text ($max);
1762}
1763
1764#############################################################################
1765
1390package CFClient::UI::Slider; 1766package CFClient::UI::Slider;
1391 1767
1392use strict; 1768use strict;
1393 1769
1394use SDL::OpenGL; 1770use CFClient::OpenGL;
1395 1771
1396our @ISA = CFClient::UI::DrawBG::; 1772our @ISA = CFClient::UI::DrawBG::;
1397 1773
1398my @tex = 1774my @tex =
1399 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1775 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1409 # TODO: calculations are off 1785 # TODO: calculations are off
1410 my $self = $class->SUPER::new ( 1786 my $self = $class->SUPER::new (
1411 fg => [1, 1, 1], 1787 fg => [1, 1, 1],
1412 active_fg => [0, 0, 0], 1788 active_fg => [0, 0, 0],
1413 range => [0, 0, 100, 10], 1789 range => [0, 0, 100, 10],
1414 req_w => 40, 1790 req_w => 20,
1415 req_h => 13, 1791 req_h => 20,
1416 vertical => 0, 1792 vertical => 0,
1417 can_hover => 1, 1793 can_hover => 1,
1418 inner_pad => 5, 1794 inner_pad => 5,
1419 @_ 1795 @_
1420 ); 1796 );
1524 1900
1525package CFClient::UI::TextView; 1901package CFClient::UI::TextView;
1526 1902
1527our @ISA = CFClient::UI::HBox::; 1903our @ISA = CFClient::UI::HBox::;
1528 1904
1529use SDL::OpenGL; 1905use CFClient::OpenGL;
1530 1906
1531sub new { 1907sub new {
1532 my $class = shift; 1908 my $class = shift;
1533 1909
1534 my $self = $class->SUPER::new ( 1910 my $self = $class->SUPER::new (
1535 req_w => $::WIDTH / 6,
1536 req_h => $::HEIGHT / 6,
1537 fontsize => $::FONTSIZE, 1911 fontsize => 1,
1538 @_, 1912 @_,
1539 1913
1540 layout => (new CFClient::Layout), 1914 layout => (new CFClient::Layout),
1541 par => [], 1915 par => [],
1542 height => 0, 1916 height => 0,
1563sub text_height { 1937sub text_height {
1564 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1565 1939
1566 my $layout = $self->{layout}; 1940 my $layout = $self->{layout};
1567 1941
1568 $layout->set_height ($self->{fontsize}); 1942 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1569 $layout->set_width ($self->{w}); 1943 $layout->set_width ($self->{w});
1570 $layout->set_text ($text); 1944 $layout->set_text ($text);
1571 1945
1572 ($layout->size)[1] 1946 ($layout->size)[1]
1573} 1947}
1577 1951
1578 $self->{need_reflow}++; 1952 $self->{need_reflow}++;
1579 $self->update; 1953 $self->update;
1580} 1954}
1581 1955
1582sub size_request {
1583 my ($self) = @_;
1584
1585 ($self->{req_w}, $self->{req_h})
1586}
1587
1588sub size_allocate { 1956sub size_allocate {
1589 my ($self, $x, $y, $w, $h) = @_; 1957 my ($self, $w, $h) = @_;
1590 1958
1591 $self->SUPER::size_allocate ($x, $y, $w, $h); 1959 $self->SUPER::size_allocate ($w, $h);
1592 1960
1593 $self->{layout}->set_height ($self->{fontsize}); 1961 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1594 $self->{layout}->set_width ($self->{w}); 1962 $self->{layout}->set_width ($self->{children}[0]{w});
1595 1963
1596 $self->reflow; 1964 $self->reflow;
1597} 1965}
1598 1966
1599sub add_paragraph { 1967sub add_paragraph {
1686 2054
1687} 2055}
1688 2056
1689############################################################################# 2057#############################################################################
1690 2058
1691package CFClient::UI::MapWidget;
1692
1693use strict;
1694
1695use List::Util qw(min max);
1696
1697use SDL;
1698use SDL::OpenGL;
1699
1700our @ISA = CFClient::UI::Base::;
1701
1702sub new {
1703 my $class = shift;
1704
1705 $class->SUPER::new (
1706 z => -1,
1707 can_focus => 1,
1708 list => (glGenLists 1),
1709 @_
1710 )
1711}
1712
1713sub key_down {
1714 print "MAPKEYDOWN\n";
1715}
1716
1717sub key_up {
1718}
1719
1720sub button_down {
1721 my ($self, $ev, $x, $y) = @_;
1722
1723 $self->focus_in;
1724
1725 if ($ev->button == 2) {
1726 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1727 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1728
1729 $self->{motion} = sub {
1730 my ($ev, $x, $y) = @_;
1731
1732 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1733
1734 $::CFG->{map_shift_x} = $bw + $x - $ox;
1735 $::CFG->{map_shift_y} = $bh + $y - $oy;
1736
1737 $self->update;
1738 };
1739 }
1740}
1741
1742sub button_up {
1743 my ($self, $ev, $x, $y) = @_;
1744
1745 delete $self->{motion};
1746}
1747
1748sub mouse_motion {
1749 my ($self, $ev, $x, $y) = @_;
1750
1751 $self->{motion}->($ev, $x, $y) if $self->{motion};
1752}
1753
1754sub size_request {
1755 (
1756 1 + 32 * int $::WIDTH / 32,
1757 1 + 32 * int $::HEIGHT / 32,
1758 )
1759}
1760
1761sub update {
1762 my ($self) = @_;
1763
1764 $self->{need_update} = 1;
1765 $self->SUPER::update;
1766}
1767
1768sub draw {
1769 my ($self) = @_;
1770
1771 if (delete $self->{need_update}) {
1772 glNewList $self->{list}, GL_COMPILE;
1773
1774 if ($::MAP) {
1775 my $sw = int $::WIDTH / 32;
1776 my $sh = int $::HEIGHT / 32;
1777
1778 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1779 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1780
1781 glTranslate $sx0 - 32, $sy0 - 32, 0;
1782
1783 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1784
1785 if ($::CFG->{fow_enable}) {
1786 if ($::CFG->{fow_smooth}) { # smooth fog of war
1787 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1788 glConvolutionFilter2D
1789 GL_CONVOLUTION_2D,
1790 GL_ALPHA,
1791 3, 3,
1792 GL_ALPHA, GL_FLOAT,
1793 pack "f*",
1794 0.1, 0.1, 0.1,
1795 0.1, 0.2, 0.1,
1796 0.1, 0.1, 0.1,
1797 ;
1798 glEnable GL_CONVOLUTION_2D;
1799 }
1800
1801 $self->{fow_texture} = new CFClient::Texture
1802 w => $w,
1803 h => $h,
1804 data => $data,
1805 internalformat => GL_ALPHA,
1806 format => GL_ALPHA;
1807
1808 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1809
1810 glEnable GL_BLEND;
1811 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1812 glEnable GL_TEXTURE_2D;
1813 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1814
1815 glColor +($::CFG->{fow_intensity}) x 3, 1;
1816 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1817
1818 glDisable GL_TEXTURE_2D;
1819 glDisable GL_BLEND;
1820 }
1821
1822 # HACK BEGIN
1823 {
1824 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1825 my ($w, $h) = (250, 250);
1826
1827 glEnable GL_BLEND;
1828 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1829 glEnable GL_TEXTURE_2D;
1830 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1831
1832 $self->{mapmap_texture} =
1833 new CFClient::Texture
1834 w => $w,
1835 h => $h,
1836 data => $::MAP->mapmap ($w, $h),
1837 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1838
1839 $self->{mapmap_texture}->draw_quad (100, 100);
1840
1841 glDisable GL_TEXTURE_2D;
1842 glDisable GL_BLEND;
1843 }
1844 # HACK END
1845 }
1846
1847 glEndList;
1848 }
1849
1850 glPushMatrix;
1851 glCallList $self->{list};
1852 glPopMatrix;
1853
1854 if ($FOCUS != $self) {
1855 glColor 64/255, 64/255, 64/255;
1856 glLogicOp GL_AND;
1857 glEnable GL_COLOR_LOGIC_OP;
1858 glBegin GL_QUADS;
1859 glVertex 0, 0;
1860 glVertex 0, $::HEIGHT;
1861 glVertex $::WIDTH, $::HEIGHT;
1862 glVertex $::WIDTH, 0;
1863 glEnd;
1864 glDisable GL_COLOR_LOGIC_OP;
1865 }
1866}
1867
1868my %DIR = (
1869 SDLK_KP8, [1, "north"],
1870 SDLK_KP9, [2, "northeast"],
1871 SDLK_KP6, [3, "east"],
1872 SDLK_KP3, [4, "southeast"],
1873 SDLK_KP2, [5, "south"],
1874 SDLK_KP1, [6, "southwest"],
1875 SDLK_KP4, [7, "west"],
1876 SDLK_KP7, [8, "northwest"],
1877
1878 SDLK_UP, [1, "north"],
1879 SDLK_RIGHT, [3, "east"],
1880 SDLK_DOWN, [5, "south"],
1881 SDLK_LEFT, [7, "west"],
1882);
1883
1884sub key_down {
1885 my ($self, $ev) = @_;
1886
1887 my $mod = $ev->key_mod;
1888 my $sym = $ev->key_sym;
1889
1890 if ($sym == SDLK_KP5) {
1891 $::CONN->user_send ("stay fire");
1892 } elsif ($sym == SDLK_a) {
1893 $::CONN->user_send ("apply");
1894 } elsif ($sym == SDLK_QUOTE) {
1895 $self->emit ('activate_console');
1896 } elsif ($sym == SDLK_SLASH) {
1897 $self->emit ('activate_console' => '/');
1898 } elsif (exists $DIR{$sym}) {
1899 if ($mod & KMOD_SHIFT) {
1900 $self->{shft}++;
1901 $::CONN->user_send ("fire $DIR{$sym}[0]");
1902 } elsif ($mod & KMOD_CTRL) {
1903 $self->{ctrl}++;
1904 $::CONN->user_send ("run $DIR{$sym}[0]");
1905 } else {
1906 $::CONN->user_send ("$DIR{$sym}[1]");
1907 }
1908 }
1909}
1910
1911sub key_up {
1912 my ($self, $ev) = @_;
1913
1914 my $mod = $ev->key_mod;
1915 my $sym = $ev->key_sym;
1916
1917 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1918 $::CONN->user_send ("fire_stop");
1919 }
1920 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1921 $::CONN->user_send ("run_stop");
1922 }
1923}
1924
1925#############################################################################
1926
1927package CFClient::UI::Animator; 2059package CFClient::UI::Animator;
1928 2060
1929use SDL::OpenGL; 2061use CFClient::OpenGL;
1930 2062
1931our @ISA = CFClient::UI::Bin::; 2063our @ISA = CFClient::UI::Bin::;
1932 2064
1933sub moveto { 2065sub moveto {
1934 my ($self, $x, $y) = @_; 2066 my ($self, $x, $y) = @_;
1974 my $class = shift; 2106 my $class = shift;
1975 2107
1976 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
1977 state => 0, 2109 state => 0,
1978 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
1979 @_ 2112 @_
1980 ); 2113 );
1981 2114
1982 if ($self->{state}) { 2115 if ($self->{state}) {
1983 $self->{state} = 0; 2116 $self->{state} = 0;
2003 $self->emit (changed => $self->{state}); 2136 $self->emit (changed => $self->{state});
2004} 2137}
2005 2138
2006############################################################################# 2139#############################################################################
2007 2140
2141package CFClient::UI::Tooltip;
2142
2143our @ISA = CFClient::UI::Bin::;
2144
2145use CFClient::OpenGL;
2146
2147sub new {
2148 my $class = shift;
2149
2150 $class->SUPER::new (
2151 @_,
2152 can_events => 0,
2153 )
2154}
2155
2156sub set_text {
2157 my ($self, $text) = @_;
2158
2159 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2160 $self->{label}->set_text ($text);
2161 $self->add ($self->{label});
2162}
2163
2164sub size_request {
2165 my ($self) = @_;
2166
2167 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2168
2169 $w = List::Util::min $::WIDTH * 0.2, $w;
2170 $h = List::Util::max $::HEIGHT * 0.2, $h;
2171
2172 ($w + 4, $h + 4)
2173}
2174
2175sub _draw {
2176 my ($self) = @_;
2177
2178 glPushMatrix;
2179 glTranslate 0.375, 0.375;
2180
2181 my ($w, $h) = @$self{qw(w h)};
2182
2183 glColor 1, 0.8, 0.4;
2184 glBegin GL_QUADS;
2185 glVertex 0 , 0;
2186 glVertex 0 , $h;
2187 glVertex $w, $h;
2188 glVertex $w, 0;
2189 glEnd;
2190
2191 glColor 0, 0, 0;
2192 glBegin GL_LINE_LOOP;
2193 glVertex 0 , 0;
2194 glVertex 0 , $h;
2195 glVertex $w, $h;
2196 glVertex $w, 0;
2197 glEnd;
2198
2199 glPopMatrix;
2200
2201 glTranslate 2, 2;
2202 $self->SUPER::_draw;
2203}
2204
2205#############################################################################
2206
2008package CFClient::UI::Root; 2207package CFClient::UI::Root;
2009 2208
2010our @ISA = CFClient::UI::Container::; 2209our @ISA = CFClient::UI::Container::;
2011 2210
2012use SDL::OpenGL; 2211use CFClient::OpenGL;
2212
2213sub check_size {
2214 my ($self) = @_;
2215
2216 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2217}
2013 2218
2014sub size_request { 2219sub size_request {
2015 ($::WIDTH, $::HEIGHT) 2220 ($::WIDTH, $::HEIGHT)
2016} 2221}
2017 2222
2018sub size_allocate { 2223sub configure {
2019 my ($self, $x, $y, $w, $h) = @_; 2224 my ($self, $x, $y, $w, $h) = @_;
2020 2225
2021 $self->_size_allocate ($x, $y, $w, $h); 2226 $self->SUPER::configure ($x, $y, $w, $h);
2022 2227
2023 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2024 for @{$self->{children}}; 2228 for my $child (@{$self->{children}}) {
2229 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2230
2231 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2232 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2233 $child->configure ($X, $Y, $W,$H);
2234 }
2025} 2235}
2026 2236
2027sub _topleft { 2237sub _topleft {
2028 my ($self, $x, $y) = @_; 2238 my ($self, $x, $y) = @_;
2029 2239
2031} 2241}
2032 2242
2033sub update { 2243sub update {
2034 my ($self) = @_; 2244 my ($self) = @_;
2035 2245
2036 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2246 $self->check_size;
2037 ::refresh (); 2247 ::refresh ();
2038} 2248}
2039 2249
2040sub add { 2250sub add {
2041 my ($self, $widget) = @_; 2251 my ($self, $child) = @_;
2042 2252
2253 # integerize window positions
2254 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y};
2256
2043 $self->SUPER::add ($widget); 2257 $self->SUPER::add ($child);
2044
2045 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2046} 2258}
2047 2259
2048sub on_refresh { 2260sub on_refresh {
2049 my ($self, $id, $cb) = @_; 2261 my ($self, $id, $cb) = @_;
2050 2262
2074############################################################################# 2286#############################################################################
2075 2287
2076package CFClient::UI; 2288package CFClient::UI;
2077 2289
2078$ROOT = new CFClient::UI::Root; 2290$ROOT = new CFClient::UI::Root;
2291$TOOLTIP = new CFClient::UI::Tooltip;
2079 2292
20801 22931
2081 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines