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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines