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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines