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.130 by root, Tue Apr 18 01:51:06 2006 UTC vs.
Revision 1.172 by root, Tue Apr 25 09:52:04 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_markup ($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->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0])
45 if $FOCUS;
18} 46}
19 47
20sub feed_sdl_key_up_event { 48sub feed_sdl_key_up_event {
21 $FOCUS->key_up ($_[0]) if $FOCUS; 49 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0])
50 if $FOCUS;
22} 51}
23 52
24sub feed_sdl_button_down_event { 53sub feed_sdl_button_down_event {
25 my ($ev) = @_; 54 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 55 my ($x, $y) = ($ev->{x}, $ev->{y});
27 56
28 if (!$BUTTON_STATE) { 57 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 58 my $widget = $ROOT->find_widget ($x, $y);
30 59
31 $GRAB = $widget; 60 $GRAB = $widget;
32 $GRAB->update if $GRAB; 61 $GRAB->update if $GRAB;
33 }
34 62
63 check_tooltip;
64 }
65
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 66 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 67
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 68 if ($GRAB) {
69 ($x, $y) = $GRAB->coord2local ($x, $y);
70 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
71 }
38} 72}
39 73
40sub feed_sdl_button_up_event { 74sub feed_sdl_button_up_event {
41 my ($ev) = @_; 75 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 76 my ($x, $y) = ($ev->{x}, $ev->{y});
43 77
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 78 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 79
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 80 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 81
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 82 if ($GRAB) {
83 ($x, $y) = $GRAB->coord2local ($x, $y);
84 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
85 }
49 86
50 if (!$BUTTON_STATE) { 87 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 88 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 89 $grab->update if $grab;
53 $GRAB->update if $GRAB; 90 $GRAB->update if $GRAB;
91
92 check_tooltip;
54 } 93 }
55} 94}
56 95
57sub feed_sdl_motion_event { 96sub feed_sdl_motion_event {
58 my ($ev) = @_; 97 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 98 my ($x, $y) = ($ev->{x}, $ev->{y});
60 99
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 100 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 101
63 if ($widget != $HOVER) { 102 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 103 my $hover = $HOVER; $HOVER = $widget;
65 104
66 $hover->update if $hover && $hover->{can_hover}; 105 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 106 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 }
69 107
70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER; 108 check_tooltip;
109 }
110
111 if ($HOVER) {
112 ($x, $y) = $HOVER->coord2local ($x, $y);
113 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y);
114 }
71} 115}
72 116
73# convert position array to integers 117# convert position array to integers
74sub harmonize { 118sub harmonize {
75 my ($vals) = @_; 119 my ($vals) = @_;
87 131
88package CFClient::UI::Base; 132package CFClient::UI::Base;
89 133
90use strict; 134use strict;
91 135
92use SDL::OpenGL; 136use CFClient::OpenGL;
93 137
94sub new { 138sub new {
95 my $class = shift; 139 my $class = shift;
96 140
97 my $self = bless { 141 my $self = bless {
98 x => 0, 142 x => 0,
99 y => 0, 143 y => 0,
100 z => 0, 144 z => 0,
101 w => -1, 145 can_events => 1,
102 h => -1,
103 @_ 146 @_
104 }, $class; 147 }, $class;
105 148
106 for (keys %$self) { 149 for (keys %$self) {
107 if (/^connect_(.*)$/) { 150 if (/^connect_(.*)$/) {
110 } 153 }
111 154
112 $self 155 $self
113} 156}
114 157
158sub destroy {
159 my ($self) = @_;
160
161 $self->hide;
162 %$self = ();
163}
164
165sub show {
166 my ($self) = @_;
167
168 return if $self->{parent};
169
170 $CFClient::UI::ROOT->add ($self);
171}
172
173sub hide {
174 my ($self) = @_;
175
176 undef $GRAB if $GRAB == $self;
177 undef $HOVER if $HOVER == $self;
178
179 $self->{parent}->remove ($self)
180 if $self->{parent};
181}
182
115sub move { 183sub move {
116 my ($self, $x, $y, $z) = @_; 184 my ($self, $x, $y, $z) = @_;
117 185
118 $self->{x} = int $x; 186 $self->{x} = int $x;
119 $self->{y} = int $y; 187 $self->{y} = int $y;
120 $self->{z} = $z if defined $z; 188 $self->{z} = $z if defined $z;
121 189
122 $self->update; 190 $self->update;
123} 191}
124 192
125sub needs_redraw { 193sub set_size {
126 0 194 my ($self, $w, $h) = @_;
195
196 $self->{user_w} = $w;
197 $self->{user_h} = $h;
198
199 $self->check_size;
127} 200}
128 201
129sub size_request { 202sub size_request {
130 require Carp; 203 require Carp;
131 Carp::confess "size_request is abtract"; 204 Carp::confess "size_request is abstract";
132} 205}
133 206
134sub configure { 207sub configure {
135 my ($self, $x, $y, $w, $h) = @_; 208 my ($self, $x, $y, $w, $h) = @_;
136 209
210 if ($self->{aspect}) {
211 my $w2 = List::Util::min $w, int $h * $self->{aspect};
212 my $h2 = List::Util::min $h, int $w / $self->{aspect};
213
214 # use alignment to adjust x, y
215
216 $x += int +($w - $w2) * 0.5;
217 $y += int +($h - $h2) * 0.5;
218
219 ($w, $h) = ($w2, $h2);
220 }
221
222 if ($self->{x} != $x || $self->{y} != $y) {
137 $self->{x} = $x; 223 $self->{x} = $x;
138 $self->{y} = $y; 224 $self->{y} = $y;
225 $self->update;
226 }
139 227
140 return unless $self->{w} != $w || $self->{h} != $h; 228 if ($self->{w} != $w || $self->{h} != $h) {
141
142 $self->{w} = $w; 229 $self->{w} = $w;
143 $self->{h} = $h; 230 $self->{h} = $h;
144 231
145 $self->size_allocate ($w, $h); 232 $self->size_allocate ($w, $h);
233 $self->update;
234 }
146} 235}
147 236
148sub size_allocate { 237sub size_allocate {
149 # nothing to be done 238 # nothing to be done
239}
240
241sub children {
242}
243
244sub set_max_size {
245 my ($self, $w, $h) = @_;
246
247 delete $self->{max_w}; $self->{max_w} = $w if $w;
248 delete $self->{max_h}; $self->{max_h} = $h if $h;
150} 249}
151 250
152# return top left coordinates 251# return top left coordinates
153sub _topleft { 252sub _topleft {
154 my ($self, $x, $y) = @_; 253 my ($self, $x, $y) = @_;
254
255 $self->{parent}
256 or Carp::confess "no parent widget in _topleft\n";#d#
155 257
156 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 258 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
157} 259}
158 260
159# translate global coordinates to local coordinate system 261# translate global coordinates to local coordinate system
237 glVertex $x + $self->{w}, $y + $self->{h}; 339 glVertex $x + $self->{w}, $y + $self->{h};
238 glVertex $x , $y + $self->{h}; 340 glVertex $x , $y + $self->{h};
239 glEnd; 341 glEnd;
240 glDisable GL_BLEND; 342 glDisable GL_BLEND;
241 } 343 }
344
345 if ($ENV{PCLIENT_DEBUG}) {
346 glPushMatrix;
347 glColor 1, 1, 0, 1;
348 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
349 glBegin GL_LINE_LOOP;
350 glVertex 0 , 0;
351 glVertex $self->{w}, 0;
352 glVertex $self->{w}, $self->{h};
353 glVertex 0 , $self->{h};
354 glEnd;
355 glPopMatrix;
356 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
357 }
242} 358}
243 359
244sub _draw { 360sub _draw {
245 my ($self) = @_; 361 my ($self) = @_;
246 362
247 warn "no draw defined for $self\n"; 363 warn "no draw defined for $self\n";
248} 364}
249 365
250sub find_widget { 366sub find_widget {
251 my ($self, $x, $y) = @_; 367 my ($self, $x, $y) = @_;
368
369 return () unless $self->{can_events};
252 370
253 return $self 371 return $self
254 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 372 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
255 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 373 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
256 374
264} 382}
265 383
266sub check_size { 384sub check_size {
267 my ($self) = @_; 385 my ($self) = @_;
268 386
269 my ($w, $h) = $self->size_request; 387 $self->{parent}
388 or return 1;
270 389
390 my ($w, $h) = $self->{user_w} && $self->{user_h}
391 ? @$self{qw(user_w user_h)}
392 : $self->size_request;
393
271 if ($w != $self->{req_w} || $h != $self->{req_h}) { 394 if ($w != $self->{req_w} || $h != $self->{req_h}) {
272 $self->{req_w} = $w; 395 $self->{req_w} = $w;
273 $self->{req_h} = $h; 396 $self->{req_h} = $h;
274 397
275 $self->{parent}->check_size 398 $self->{parent}->check_size
276 if $self->{parent};
277
278 $self->size_allocate ($w, $h); 399 or $self->size_allocate (
279 $self->update; 400 (List::Util::max $self->{w}, $w),
401 (List::Util::max $self->{h}, $h),
402 );
403
404 1
405 } else {
406 0
280 } 407 }
281} 408}
282 409
283sub update { 410sub update {
284 my ($self) = @_; 411 my ($self) = @_;
288} 415}
289 416
290sub connect { 417sub connect {
291 my ($self, $signal, $cb) = @_; 418 my ($self, $signal, $cb) = @_;
292 419
293 push @{ $self->{cb}{$signal} }, $cb; 420 push @{ $self->{signal_cb}{$signal} }, $cb;
294} 421}
295 422
296sub emit { 423sub emit {
297 my ($self, $signal, @args) = @_; 424 my ($self, $signal, @args) = @_;
298 425
299 $_->($self, @args) 426 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
300 for @{$self->{cb}{$signal} || []};
301} 427}
302 428
303sub DESTROY { 429sub DESTROY {
304 my ($self) = @_; 430 my ($self) = @_;
305 431
311package CFClient::UI::DrawBG; 437package CFClient::UI::DrawBG;
312 438
313our @ISA = CFClient::UI::Base::; 439our @ISA = CFClient::UI::Base::;
314 440
315use strict; 441use strict;
316use SDL::OpenGL; 442use CFClient::OpenGL;
317 443
318sub new { 444sub new {
319 my $class = shift; 445 my $class = shift;
320 446
321 # range [value, low, high, page] 447 # range [value, low, high, page]
350 476
351package CFClient::UI::Empty; 477package CFClient::UI::Empty;
352 478
353our @ISA = CFClient::UI::Base::; 479our @ISA = CFClient::UI::Base::;
354 480
481sub new {
482 my ($class, %arg) = @_;
483 $class->SUPER::new (can_events => 0, %arg);
484}
485
355sub size_request { 486sub size_request {
356 (0, 0) 487 (0, 0)
357} 488}
358 489
359sub draw { } 490sub draw { }
367sub new { 498sub new {
368 my ($class, %arg) = @_; 499 my ($class, %arg) = @_;
369 500
370 my $children = delete $arg{children} || []; 501 my $children = delete $arg{children} || [];
371 502
372 my $self = $class->SUPER::new (children => [], %arg); 503 my $self = $class->SUPER::new (
504 children => [],
505 can_events => 0,
506 %arg,
507 );
373 $self->add ($_) for @$children; 508 $self->add ($_) for @$children;
374 509
375 $self 510 $self
376} 511}
377 512
385 $self->{children} = [ 520 $self->{children} = [
386 sort { $a->{z} <=> $b->{z} } 521 sort { $a->{z} <=> $b->{z} }
387 @{$self->{children}}, $child 522 @{$self->{children}}, $child
388 ]; 523 ];
389 524
390 $self->{w} = $self->{h} = -1;
391
392 $child->check_size; 525 $child->check_size;
393} 526}
394 527
528sub children {
529 @{ $_[0]{children} }
530}
531
395sub remove { 532sub remove {
396 my ($self, $widget) = @_; 533 my ($self, $child) = @_;
397 534
535 delete $child->{parent};
536 $child->hide;
537
398 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 538 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
399 539
400 $self->check_size; 540 $self->check_size;
541 $self->update;
542}
543
544sub clear {
545 my ($self) = @_;
546
547 my $children = delete $self->{children};
548 $self->{children} = [];
549
550 for (@$children) {
551 delete $_->{parent};
552 $_->hide;
553 }
401} 554}
402 555
403sub find_widget { 556sub find_widget {
404 my ($self, $x, $y) = @_; 557 my ($self, $x, $y) = @_;
405 558
435 588
436 $class->SUPER::new (children => [$child], %arg) 589 $class->SUPER::new (children => [$child], %arg)
437} 590}
438 591
439sub add { 592sub add {
440 my ($self, $widget) = @_; 593 my ($self, $child) = @_;
441 594
442 $self->{children} = []; 595 $self->{children} = [];
443 596
444 $self->SUPER::add ($widget); 597 $self->SUPER::add ($child);
445} 598}
446 599
447sub remove { 600sub remove {
448 my ($self, $widget) = @_; 601 my ($self, $widget) = @_;
449 602
469 622
470package CFClient::UI::Window; 623package CFClient::UI::Window;
471 624
472our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
473 626
474use SDL::OpenGL; 627use CFClient::OpenGL;
475 628
476sub new { 629sub new {
477 my ($class, %arg) = @_; 630 my ($class, %arg) = @_;
478 631
479 my $self = $class->SUPER::new (%arg); 632 my $self = $class->SUPER::new (%arg);
533sub new { die } 686sub new { die }
534 687
535sub size_request { 688sub size_request {
536 my ($self) = @_; 689 my ($self) = @_;
537 690
538 @$self{qw(child_w child_h)} = $self->child->size_request; 691 @$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)}); 692 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
540 693
541 @$self{qw(child_w child_h)} 694 @$self{qw(child_w child_h)}
542} 695}
543 696
552 705
553package CFClient::UI::Frame; 706package CFClient::UI::Frame;
554 707
555our @ISA = CFClient::UI::Bin::; 708our @ISA = CFClient::UI::Bin::;
556 709
557use SDL::OpenGL; 710use CFClient::OpenGL;
558
559sub size_request {
560 my ($self) = @_;
561 my $chld = $self->child
562 or return (0, 0);
563
564 $chld->move (2, 2);
565
566 map { $_ + 4 } $chld->size_request;
567}
568
569sub size_allocate {
570 my ($self, $x, $y, $w, $h) = @_;
571
572 $self->child->configure (2, 2, $w - 4, $h - 4);
573}
574
575sub _draw {
576 my ($self) = @_;
577
578 my $chld = $self->child;
579
580 my ($w, $h) = $chld->size_request;
581
582 glBegin GL_QUADS;
583 glColor 0, 0, 0;
584 glVertex 0 , 0;
585 glVertex 0 , $h + 4;
586 glVertex $w + 4 , $h + 4;
587 glVertex $w + 4 , 0;
588 glEnd;
589
590 $chld->draw;
591}
592
593#############################################################################
594
595package CFClient::UI::FancyFrame;
596
597our @ISA = CFClient::UI::Bin::;
598
599use SDL::OpenGL;
600
601my @tex =
602 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
603 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
604 711
605sub new { 712sub new {
606 my $class = shift; 713 my $class = shift;
607 714
608 # TODO: user_x, user_y, overwrite moveto?
609
610 $class->SUPER::new ( 715 my $self = $class->SUPER::new (
611 bg => [1, 1, 1, 1], 716 bg => [1, 1, 1, 1],
612 border_bg => [1, 1, 1, 1], 717 border_bg => [1, 1, 1, 1],
613 border => int $::FONTSIZE * 0.8, 718 border => 0.8,
614 @_ 719 @_
615 ) 720 );
721
722 $self
723}
724
725sub _draw {
726 my ($self) = @_;
727
728 my ($w, $h) = ($self->{w}, $self->{h});
729
730 glEnable GL_BLEND;
731 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
732 glEnable GL_TEXTURE_2D;
733 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
734
735# glBegin GL_QUADS;
736# glColor 0, 0, 0, 0;
737# glVertex 0 , 0;
738# glVertex 0 , $h;
739# glVertex $w, $h;
740# glVertex $w, 0;
741# glEnd;
742
743
744 $self->child->draw;
745 glDisable GL_BLEND;
746 glDisable GL_TEXTURE_2D;
747}
748
749#############################################################################
750
751package CFClient::UI::FancyFrame;
752
753our @ISA = CFClient::UI::Bin::;
754
755use CFClient::OpenGL;
756
757my @tex =
758 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
759 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
760
761sub new {
762 my $class = shift;
763
764 # TODO: user_x, user_y, overwrite moveto?
765
766 my $self = $class->SUPER::new (
767 bg => [1, 1, 1, 1],
768 border_bg => [1, 1, 1, 1],
769 border => 0.8,
770 can_events => 1,
771 @_
772 );
773
774 $self->{title} &&= new CFClient::UI::Label
775 align => 0,
776 valign => 1,
777 text => $self->{title},
778 fontsize => 1;
779
780 $self
781}
782
783sub border {
784 int $_[0]{border} * $::FONTSIZE
616} 785}
617 786
618sub size_request { 787sub size_request {
619 my ($self) = @_; 788 my ($self) = @_;
620 789
621 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
622
623 my ($w, $h) = $self->SUPER::size_request; 790 my ($w, $h) = $self->SUPER::size_request;
624 791
625 ( 792 (
626 $w + $self->{border} * 2, 793 $w + $self->border * 2,
627 $h + $self->{border} * 2, 794 $h + $self->border * 2,
628 ) 795 )
629} 796}
630 797
631sub size_allocate { 798sub size_allocate {
632 my ($self, $w, $h) = @_; 799 my ($self, $w, $h) = @_;
633 800
634 $h -= List::Util::max 0, $self->{border} * 2; 801 $h -= List::Util::max 0, $self->border * 2;
635 $w -= List::Util::max 0, $self->{border} * 2; 802 $w -= List::Util::max 0, $self->border * 2;
636 803
804 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
805 if $self->{title};
806
637 $self->child->configure ($self->{border}, $self->{border}, $w, $h); 807 $self->child->configure ($self->border, $self->border, $w, $h);
638} 808}
639 809
640sub button_down { 810sub button_down {
641 my ($self, $ev, $x, $y) = @_; 811 my ($self, $ev, $x, $y) = @_;
642 812
813 my $border = $self->border;
814
643 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 815 if ($x < $self->{w} && $x >= $self->{w} - $border
644 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 816 && $y < $self->{h} && $y >= $self->{h} - $border) {
645 817
646 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 818 my ($ox, $oy) = ($ev->{x}, $ev->{y});
647 my ($bw, $bh) = ($self->{w}, $self->{h}); 819 my ($bw, $bh) = ($self->{w}, $self->{h});
648 820
649 $self->{motion} = sub { 821 $self->{motion} = sub {
650 my ($ev, $x, $y) = @_; 822 my ($ev, $x, $y) = @_;
651 823
652 ($x, $y) = ($ev->motion_x, $ev->motion_y); 824 ($x, $y) = ($ev->{x}, $ev->{y});
653 825
654 $self->{user_w} = $bw + $x - $ox; 826 $self->{user_w} = $bw + $x - $ox;
655 $self->{user_h} = $bh + $y - $oy; 827 $self->{user_h} = $bh + $y - $oy;
656 $self->check_size; 828 $self->check_size;
657 }; 829 };
658 830
659 } elsif ($x >= 0 && $x < $self->{w} 831 } elsif ($x >= 0 && $x < $self->{w}
660 && $y >= 0 && $y < $self->{border}) { 832 && $y >= 0 && $y < $border) {
661 833
662 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 834 my ($ox, $oy) = ($ev->{x}, $ev->{y});
663 my ($bx, $by) = ($self->{x}, $self->{y}); 835 my ($bx, $by) = ($self->{x}, $self->{y});
664 836
665 $self->{motion} = sub { 837 $self->{motion} = sub {
666 my ($ev, $x, $y) = @_; 838 my ($ev, $x, $y) = @_;
667 839
668 ($x, $y) = ($ev->motion_x, $ev->motion_y); 840 ($x, $y) = ($ev->{x}, $ev->{y});
669 841
670 $self->move ($bx + $x - $ox, $by + $y - $oy); 842 $self->move ($bx + $x - $ox, $by + $y - $oy);
671 $self->update; 843 $self->update;
672 }; 844 };
673 } 845 }
694 glEnable GL_BLEND; 866 glEnable GL_BLEND;
695 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 867 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
696 glEnable GL_TEXTURE_2D; 868 glEnable GL_TEXTURE_2D;
697 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 869 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
698 870
871 my $border = $self->border;
872
699 glColor @{ $self->{border_bg} }; 873 glColor @{ $self->{border_bg} };
700 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 874 $tex[1]->draw_quad (0, 0, $w, $border);
701 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 875 $tex[3]->draw_quad (0, $border, $border, $ch);
702 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 876 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
703 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 877 $tex[4]->draw_quad (0, $h - $border, $w, $border);
704 878
705 my $bg = $tex[0]; 879 my $bg = $tex[0];
706 880
707 # TODO: repeat texture not scale 881 # TODO: repeat texture not scale
708 my $rep_x = $cw / $bg->{w}; 882 my $rep_x = $cw / $bg->{w};
711 glColor @{ $self->{bg} }; 885 glColor @{ $self->{bg} };
712 886
713 $bg->{s} = $rep_x; 887 $bg->{s} = $rep_x;
714 $bg->{t} = $rep_y; 888 $bg->{t} = $rep_y;
715 $bg->{wrap_mode} = 1; 889 $bg->{wrap_mode} = 1;
716 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 890 $bg->draw_quad ($border, $border, $cw, $ch);
717 891
718 glDisable GL_TEXTURE_2D; 892 glDisable GL_TEXTURE_2D;
719 glDisable GL_BLEND; 893 glDisable GL_BLEND;
720 894
895 $self->{title}->draw if $self->{title};
721 $self->child->draw; 896 $self->child->draw;
722} 897}
723 898
724############################################################################# 899#############################################################################
725 900
727 902
728our @ISA = CFClient::UI::Base::; 903our @ISA = CFClient::UI::Base::;
729 904
730use List::Util qw(max sum); 905use List::Util qw(max sum);
731 906
732use SDL::OpenGL; 907use CFClient::OpenGL;
733 908
734sub new { 909sub new {
735 my $class = shift; 910 my $class = shift;
736 911
737 $class->SUPER::new ( 912 $class->SUPER::new (
744 my ($self, $x, $y, $child) = @_; 919 my ($self, $x, $y, $child) = @_;
745 920
746 $child->set_parent ($self); 921 $child->set_parent ($self);
747 $self->{children}[$y][$x] = $child; 922 $self->{children}[$y][$x] = $child;
748 923
749 $self->{w} = $self->{h} = -1;
750
751 $child->check_size; 924 $child->check_size;
925}
926
927sub children {
928 grep $_, map @$_, grep $_, @{ $_[0]{children} }
752} 929}
753 930
754# TODO: move to container class maybe? send childs a signal on removal? 931# TODO: move to container class maybe? send childs a signal on removal?
755sub clear { 932sub clear {
756 my ($self) = @_; 933 my ($self) = @_;
757 934
935 my @children = $self->children;
758 delete $self->{children}; 936 delete $self->{children};
937
938 for (@children) {
939 delete $_->{parent};
940 $_->hide;
941 }
942
759 $self->update; 943 $self->update;
760} 944}
761 945
762sub get_wh { 946sub get_wh {
763 my ($self) = @_; 947 my ($self) = @_;
769 or next; 953 or next;
770 954
771 for my $x (0 .. $#$row) { 955 for my $x (0 .. $#$row) {
772 my $widget = $row->[$x] 956 my $widget = $row->[$x]
773 or next; 957 or next;
774 my ($w, $h) = $widget->size_request; 958 my ($w, $h) = @$widget{qw(req_w req_h)};
775 959
776 $w[$x] = max $w[$x], $w; 960 $w[$x] = max $w[$x], $w;
777 $h[$y] = max $h[$y], $h; 961 $h[$y] = max $h[$y], $h;
778 } 962 }
779 } 963 }
884 1068
885 ($h, $w) = ($w, $h); 1069 ($h, $w) = ($w, $h);
886 1070
887 my $children = $self->{children}; 1071 my $children = $self->{children};
888 1072
889 my @h = map +($_->size_request)[0], @$children; 1073 my @h = map $_->{req_w}, @$children;
890 1074
891 my $req_h = List::Util::sum @h; 1075 my $req_h = List::Util::sum @h;
892 1076
893 if ($req_h > $h) { 1077 if ($req_h > $h) {
894 # ah well, not enough space 1078 # ah well, not enough space
942sub size_allocate { 1126sub size_allocate {
943 my ($self, $w, $h) = @_; 1127 my ($self, $w, $h) = @_;
944 1128
945 my $children = $self->{children}; 1129 my $children = $self->{children};
946 1130
947 my @h = map +($_->size_request)[1], @$children; 1131 my @h = map $_->{req_h}, @$children;
948 1132
949 my $req_h = List::Util::sum @h; 1133 my $req_h = List::Util::sum @h;
950 1134
951 if ($req_h > $h) { 1135 if ($req_h > $h) {
952 # ah well, not enough space 1136 # ah well, not enough space
980 1164
981package CFClient::UI::Label; 1165package CFClient::UI::Label;
982 1166
983our @ISA = CFClient::UI::Base::; 1167our @ISA = CFClient::UI::Base::;
984 1168
985use SDL::OpenGL; 1169use CFClient::OpenGL;
986 1170
987sub new { 1171sub new {
988 my ($class, %arg) = @_; 1172 my ($class, %arg) = @_;
989 1173
990 my $self = $class->SUPER::new ( 1174 my $self = $class->SUPER::new (
991 fg => [1, 1, 1], 1175 fg => [1, 1, 1],
992 fontsize => $::FONTSIZE, 1176 #font => default_font
1177 fontsize => 1,
993 text => "", 1178 text => "",
994 align => -1, 1179 align => -1,
995 valign => -1, 1180 valign => -1,
996 padding => 2, 1181 padding => 2,
997 layout => new CFClient::Layout, 1182 layout => new CFClient::Layout,
1183 can_events => 0,
998 %arg 1184 %arg
999 ); 1185 );
1000 1186
1001 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1187 if (exists $self->{template}) {
1188 my $layout = new CFClient::Layout;
1189 $layout->set_text (delete $self->{template});
1190 $self->{template} = $layout;
1191 }
1002 1192
1003 $self->set_text (delete $self->{text}) if exists $self->{text}; 1193 $self->set_text (delete $self->{text}) if exists $self->{text};
1004 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1194 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1005 1195
1006 $self 1196 $self
1007} 1197}
1008 1198
1009sub escape_text { 1199sub escape {
1010 local $_ = $_[1]; 1200 local $_ = $_[1];
1011 1201
1012 s/&/&amp;/g; 1202 s/&/&amp;/g;
1013 s/>/&gt;/g; 1203 s/>/&gt;/g;
1014 s/</&lt;/g; 1204 s/</&lt;/g;
1020 my ($self, $text) = @_; 1210 my ($self, $text) = @_;
1021 1211
1022 $self->{layout}->set_text ($text); 1212 $self->{layout}->set_text ($text);
1023 1213
1024 delete $self->{texture}; 1214 delete $self->{texture};
1215 $self->check_size;
1025 $self->update; 1216 $self->update;
1026} 1217}
1027 1218
1028sub set_markup { 1219sub set_markup {
1029 my ($self, $markup) = @_; 1220 my ($self, $markup) = @_;
1030 1221
1031 $self->{layout}->set_markup ($markup); 1222 $self->{layout}->set_markup ($markup);
1032 1223
1033 delete $self->{texture}; 1224 delete $self->{texture};
1225 $self->check_size;
1034 $self->update; 1226 $self->update;
1035} 1227}
1036 1228
1037sub size_request { 1229sub size_request {
1038 my ($self) = @_; 1230 my ($self) = @_;
1039 1231
1040 $self->{layout}->set_width; 1232 $self->{layout}->set_font ($self->{font}) if $self->{font};
1233 $self->{layout}->set_width ($self->{max_w} || -1);
1041 $self->{layout}->set_height ($self->{fontsize}); 1234 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1042 1235
1043 my ($w, $h) = $self->{layout}->size; 1236 my ($w, $h) = $self->{layout}->size;
1237
1238 if (exists $self->{template}) {
1239 $self->{template}->set_font ($self->{font}) if $self->{font};
1240 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1241
1242 my ($w2, $h2) = $self->{template}->size;
1243
1244 $w = List::Util::max $w, $w2;
1245 $h = List::Util::max $h, $h2;
1246 }
1044 1247
1045 ( 1248 (
1046 $w + $self->{padding} * 2, 1249 $w + $self->{padding} * 2,
1047 $h + $self->{padding} * 2, 1250 $h + $self->{padding} * 2,
1048 ) 1251 )
1052 my ($self, $w, $h) = @_; 1255 my ($self, $w, $h) = @_;
1053 1256
1054 delete $self->{texture}; 1257 delete $self->{texture};
1055} 1258}
1056 1259
1057sub update { 1260sub set_fontsize {
1058 my ($self) = @_; 1261 my ($self, $fontsize) = @_;
1059 1262
1263 $self->{fontsize} = $fontsize;
1060 delete $self->{texture}; 1264 delete $self->{texture};
1265 $self->check_size;
1061 $self->SUPER::update; 1266 $self->update;
1062} 1267}
1063 1268
1064sub _draw { 1269sub _draw {
1065 my ($self) = @_; 1270 my ($self) = @_;
1066 1271
1067 my $tex = $self->{texture} ||= do { 1272 my $tex = $self->{texture} ||= do {
1273 $self->{layout}->set_font ($self->{font}) if $self->{font};
1068 $self->{layout}->set_width ($self->{w}); 1274 $self->{layout}->set_width ($self->{w});
1069 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1275 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1070 new_from_layout CFClient::Texture $self->{layout} 1276 new_from_layout CFClient::Texture $self->{layout}
1071 }; 1277 };
1072 1278
1073 glEnable GL_BLEND; 1279 glEnable GL_BLEND;
1074 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1280 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1099 1305
1100package CFClient::UI::EntryBase; 1306package CFClient::UI::EntryBase;
1101 1307
1102our @ISA = CFClient::UI::Label::; 1308our @ISA = CFClient::UI::Label::;
1103 1309
1104use SDL; 1310use CFClient::OpenGL;
1105use SDL::OpenGL;
1106 1311
1107sub new { 1312sub new {
1108 my $class = shift; 1313 my $class = shift;
1109 1314
1110 $class->SUPER::new ( 1315 $class->SUPER::new (
1111 fg => [1, 1, 1], 1316 fg => [1, 1, 1],
1112 bg => [0, 0, 0, 0.2], 1317 bg => [0, 0, 0, 0.2],
1113 active_bg => [1, 1, 1, 0.5], 1318 active_bg => [1, 1, 1, 0.5],
1114 active_fg => [0, 0, 0], 1319 active_fg => [0, 0, 0],
1115 can_hover => 1, 1320 can_hover => 1,
1116 can_focus => 1, 1321 can_focus => 1,
1117 valign => 0, 1322 valign => 0,
1323 can_events => 1,
1118 @_ 1324 @_
1119 ) 1325 )
1120} 1326}
1121 1327
1122sub _set_text { 1328sub _set_text {
1124 1330
1125 delete $self->{cur_h}; 1331 delete $self->{cur_h};
1126 1332
1127 return if $self->{text} eq $text; 1333 return if $self->{text} eq $text;
1128 1334
1335 delete $self->{texture};
1336
1129 $self->{last_activity} = $::NOW; 1337 $self->{last_activity} = $::NOW;
1130 $self->{text} = $text; 1338 $self->{text} = $text;
1131 1339
1132 $text =~ s/./*/g if $self->{hidden}; 1340 $text =~ s/./*/g if $self->{hidden};
1133 $self->{layout}->set_text ("$text "); 1341 $self->{layout}->set_text ("$text ");
1148} 1356}
1149 1357
1150sub size_allocate { 1358sub size_allocate {
1151 my ($self, $w, $h) = @_; 1359 my ($self, $w, $h) = @_;
1152 1360
1153 $self->_set_text ($self->{text}); 1361 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1154} 1362}
1155 1363
1156sub set_text { 1364sub set_text {
1157 my ($self, $text) = @_; 1365 my ($self, $text) = @_;
1158 1366
1162} 1370}
1163 1371
1164sub key_down { 1372sub key_down {
1165 my ($self, $ev) = @_; 1373 my ($self, $ev) = @_;
1166 1374
1167 my $mod = $ev->key_mod; 1375 my $mod = $ev->{mod};
1168 my $sym = $ev->key_sym; 1376 my $sym = $ev->{sym};
1169
1170 my $uni = $ev->key_unicode; 1377 my $uni = $ev->{unicode};
1171 1378
1172 my $text = $self->get_text; 1379 my $text = $self->get_text;
1173 1380
1174 if ($sym == SDLK_BACKSPACE) { 1381 if ($sym == 8) {
1175 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1382 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1176 } elsif ($sym == SDLK_DELETE) { 1383 } elsif ($sym == 127) {
1177 substr $text, $self->{cursor}, 1, ""; 1384 substr $text, $self->{cursor}, 1, "";
1178 } elsif ($sym == SDLK_LEFT) { 1385 } elsif ($sym == CFClient::SDLK_LEFT) {
1179 --$self->{cursor} if $self->{cursor}; 1386 --$self->{cursor} if $self->{cursor};
1180 } elsif ($sym == SDLK_RIGHT) { 1387 } elsif ($sym == CFClient::SDLK_RIGHT) {
1181 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1388 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1182 } elsif ($sym == SDLK_HOME) { 1389 } elsif ($sym == CFClient::SDLK_HOME) {
1183 $self->{cursor} = 0; 1390 $self->{cursor} = 0;
1184 } elsif ($sym == SDLK_END) { 1391 } elsif ($sym == CFClient::SDLK_END) {
1185 $self->{cursor} = length $text; 1392 $self->{cursor} = length $text;
1186 } elsif ($sym == SDLK_ESCAPE) { 1393 } elsif ($sym == 27) {
1187 $self->emit ('escape'); 1394 $self->emit ('escape');
1188 } elsif ($uni) { 1395 } elsif ($uni) {
1189 substr $text, $self->{cursor}++, 0, chr $uni; 1396 substr $text, $self->{cursor}++, 0, chr $uni;
1190 } 1397 }
1191 1398
1266 1473
1267package CFClient::UI::Entry; 1474package CFClient::UI::Entry;
1268 1475
1269our @ISA = CFClient::UI::EntryBase::; 1476our @ISA = CFClient::UI::EntryBase::;
1270 1477
1271use SDL; 1478use CFClient::OpenGL;
1272use SDL::OpenGL;
1273 1479
1274sub key_down { 1480sub key_down {
1275 my ($self, $ev) = @_; 1481 my ($self, $ev) = @_;
1276 1482
1277 my $sym = $ev->key_sym; 1483 my $sym = $ev->{sym};
1278 1484
1279 if ($sym == SDLK_RETURN) { 1485 if ($sym == 13) {
1486 unshift @{$self->{history}},
1487 my $txt = $self->get_text;
1488 $self->{history_pointer} = -1;
1489 $self->{history_saveback} = '';
1280 $self->emit (activate => $self->get_text); 1490 $self->emit (activate => $txt);
1281 $self->update; 1491 $self->update;
1492
1493 } elsif ($sym == CFClient::SDLK_UP) {
1494 if ($self->{history_pointer} < 0) {
1495 $self->{history_saveback} = $self->get_text;
1496 }
1497 if (@{$self->{history} || []} > 0) {
1498 $self->{history_pointer}++;
1499 if ($self->{history_pointer} >= @{$self->{history} || []}) {
1500 $self->{history_pointer} = @{$self->{history} || []} - 1;
1501 }
1502 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1503 }
1504
1505 } elsif ($sym == CFClient::SDLK_DOWN) {
1506 $self->{history_pointer}--;
1507 $self->{history_pointer} = -1 if $self->{history_pointer} < 0;
1508
1509 if ($self->{history_pointer} >= 0) {
1510 $self->set_text ($self->{history}->[$self->{history_pointer}]);
1511 } else {
1512 $self->set_text ($self->{history_saveback});
1513 }
1282 1514
1283 } else { 1515 } else {
1284 $self->SUPER::key_down ($ev); 1516 $self->SUPER::key_down ($ev);
1285 } 1517 }
1286 1518
1290 1522
1291package CFClient::UI::Button; 1523package CFClient::UI::Button;
1292 1524
1293our @ISA = CFClient::UI::Label::; 1525our @ISA = CFClient::UI::Label::;
1294 1526
1295use SDL; 1527use CFClient::OpenGL;
1296use SDL::OpenGL;
1297 1528
1298my @tex = 1529my @tex =
1299 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1530 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1300 qw(b1_button_active.png); 1531 qw(b1_button_active.png);
1301 1532
1302sub new { 1533sub new {
1303 my $class = shift; 1534 my $class = shift;
1304 1535
1305 $class->SUPER::new ( 1536 $class->SUPER::new (
1306 padding => 4, 1537 padding => 4,
1307 fg => [1, 1, 1], 1538 fg => [1, 1, 1],
1308 bg => [1, 1, 1, 0.2], 1539 bg => [1, 1, 1, 0.2],
1309 active_fg => [0, 0, 1], 1540 active_fg => [0, 0, 1],
1310 can_hover => 1, 1541 can_hover => 1,
1311 align => 0, 1542 align => 0,
1312 valign => 0, 1543 valign => 0,
1544 can_events => 1,
1313 @_ 1545 @_
1314 ) 1546 )
1315} 1547}
1316 1548
1317sub button_up { 1549sub button_up {
1351package CFClient::UI::CheckBox; 1583package CFClient::UI::CheckBox;
1352 1584
1353our @ISA = CFClient::UI::DrawBG::; 1585our @ISA = CFClient::UI::DrawBG::;
1354 1586
1355my @tex = 1587my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1588 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1357 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1589 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1358 1590
1359use SDL; 1591use CFClient::OpenGL;
1360use SDL::OpenGL;
1361 1592
1362sub new { 1593sub new {
1363 my $class = shift; 1594 my $class = shift;
1364 1595
1365 $class->SUPER::new ( 1596 $class->SUPER::new (
1411 glDisable GL_BLEND; 1642 glDisable GL_BLEND;
1412} 1643}
1413 1644
1414############################################################################# 1645#############################################################################
1415 1646
1416package CFClient::UI::VGauge; 1647package CFClient::UI::Image;
1417 1648
1418our @ISA = CFClient::UI::Base::; 1649our @ISA = CFClient::UI::Base::;
1419 1650
1420use SDL::OpenGL; 1651use CFClient::OpenGL;
1652use Carp qw/confess/;
1421 1653
1422my %tex = ( 1654our %loaded_images;
1423 food => [
1424 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1425 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1426 ],
1427 grace => [
1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1429 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1430 ],
1431 hp => [
1432 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1433 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1434 ],
1435 mana => [
1436 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1437 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1438 ],
1439);
1440 1655
1441# eg. VGauge->new (gauge => 'food'), default gauge: food
1442sub new { 1656sub new {
1443 my $class = shift; 1657 my $class = shift;
1444 1658
1445 my $self = $class->SUPER::new (gauge => 'food', @_); 1659 my $self = $class->SUPER::new (can_events => 0, @_);
1660
1661 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1662
1663 $loaded_images{$self->{image}} ||=
1664 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1665
1666 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1667
1668 Scalar::Util::weaken $loaded_images{$self->{image}};
1669
1670 $self->{aspect} = $tex->{w} / $tex->{h};
1446 1671
1447 $self 1672 $self
1448} 1673}
1449 1674
1450sub size_request { 1675sub size_request {
1451 my ($self) = @_; 1676 my ($self) = @_;
1452 1677
1453 my $tex = $tex{$self->{gauge}}[0]; 1678 ($self->{tex}->{w}, $self->{tex}->{h})
1454
1455 @$tex{qw(w h)}
1456}
1457
1458sub set_max {
1459 my ($self, $max) = @_;
1460
1461 $self->{max_val} = $max;
1462}
1463
1464sub set_value {
1465 my ($self, $val, $max) = @_;
1466
1467 $self->set_max ($max)
1468 if defined $max;
1469
1470 $max = $self->{max_val};
1471 $self->{val} = $val;
1472
1473 $self->update;
1474} 1679}
1475 1680
1476sub _draw { 1681sub _draw {
1477 my ($self) = @_; 1682 my ($self) = @_;
1478 1683
1479 my $tex = $tex{$self->{gauge}}; 1684 my $tex = $self->{tex};
1480 1685
1481 my ($w, $h) = ($self->{w}, $self->{h}); 1686 my ($w, $h) = ($self->{w}, $self->{h});
1482 1687
1483 my $ycut = $self->{val} / ($self->{max_val} || 1); 1688 if ($self->{rot90}) {
1484 $ycut = 1 if $self->{val} > $self->{max_val}; 1689 glRotate 90, 0, 0, 1;
1690 glTranslate 0, -$self->{w}, 0;
1485 1691
1486 my $t1 = $tex->[0]; 1692 ($w, $h) = ($h, $w);
1487 my $t2 = $tex->[1]; 1693 }
1488 1694
1489 glEnable GL_BLEND; 1695 glEnable GL_BLEND;
1490 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1696 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1491 glEnable GL_TEXTURE_2D; 1697 glEnable GL_TEXTURE_2D;
1492 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1698 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1493 1699
1494 my $h1 = $self->{h} - $ycut * $self->{h}; 1700 $tex->draw_quad (0, 0, $w, $h);
1495 my $h2 = $ycut * $self->{h};
1496
1497 my $yp = 0;
1498
1499 glBindTexture (GL_TEXTURE_2D, $t1->{name});
1500 glBegin (GL_QUADS);
1501 glTexCoord (0, 0); glVertex (0 , $yp);
1502 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1);
1503 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1);
1504 glTexCoord (1, 0); glVertex (0 + $w, $yp);
1505 glEnd ();
1506
1507 $yp += $h1;
1508
1509 glBindTexture (GL_TEXTURE_2D, $t2->{name});
1510 glBegin (GL_QUADS);
1511 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp);
1512 glTexCoord (0, 1); glVertex (0 , $yp + $h2);
1513 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2);
1514 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp);
1515 glEnd ();
1516 1701
1517 glDisable GL_BLEND; 1702 glDisable GL_BLEND;
1518 glDisable GL_TEXTURE_2D; 1703 glDisable GL_TEXTURE_2D;
1519} 1704}
1520 1705
1521############################################################################# 1706#############################################################################
1522 1707
1708package CFClient::UI::VGauge;
1709
1710our @ISA = CFClient::UI::Base::;
1711
1712use List::Util qw(min max);
1713
1714use CFClient::OpenGL;
1715
1716my %tex = (
1717 food => [
1718 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1719 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1720 ],
1721 grace => [
1722 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1723 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1724 ],
1725 hp => [
1726 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1727 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1728 ],
1729 mana => [
1730 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1731 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1732 ],
1733);
1734
1735# eg. VGauge->new (gauge => 'food'), default gauge: food
1736sub new {
1737 my $class = shift;
1738
1739 my $self = $class->SUPER::new (
1740 type => 'food',
1741 @_
1742 );
1743
1744 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1745
1746 $self
1747}
1748
1749sub size_request {
1750 my ($self) = @_;
1751
1752 #my $tex = $tex{$self->{type}}[0];
1753 #@$tex{qw(w h)}
1754 (0, 0)
1755}
1756
1757sub set_max {
1758 my ($self, $max) = @_;
1759
1760 $self->{max_val} = $max;
1761}
1762
1763sub set_value {
1764 my ($self, $val, $max) = @_;
1765
1766 $self->set_max ($max)
1767 if defined $max;
1768
1769 $max = $self->{max_val};
1770 $self->{val} = $val;
1771
1772 $self->update;
1773}
1774
1775sub _draw {
1776 my ($self) = @_;
1777
1778 my $tex = $tex{$self->{type}};
1779 my ($t1, $t2, $t3) = @$tex;
1780
1781 my ($w, $h) = ($self->{w}, $self->{h});
1782
1783 if ($self->{vertical}) {
1784 glRotate 90, 0, 0, 1;
1785 glTranslate 0, -$self->{w}, 0;
1786
1787 ($w, $h) = ($h, $w);
1788 }
1789
1790 my $ycut = $self->{val} / ($self->{max_val} || 1);
1791
1792 my $ycut1 = max 0, min 1, $ycut;
1793 my $ycut2 = max 0, min 1, $ycut - 1;
1794
1795 my $h1 = $self->{h} * (1 - $ycut1);
1796 my $h2 = $self->{h} * (1 - $ycut2);
1797
1798 glEnable GL_BLEND;
1799 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1800 glEnable GL_TEXTURE_2D;
1801 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1802
1803 glBindTexture GL_TEXTURE_2D, $t1->{name};
1804 glBegin GL_QUADS;
1805 glTexCoord 0 , 0; glVertex 0 , 0;
1806 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1807 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1808 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1809 glEnd;
1810
1811 my $ycut1 = List::Util::min 1, $ycut;
1812 glBindTexture GL_TEXTURE_2D, $t2->{name};
1813 glBegin GL_QUADS;
1814 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1815 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1816 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1817 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1818 glEnd;
1819
1820 if ($t3) {
1821 glBindTexture GL_TEXTURE_2D, $t3->{name};
1822 glBegin GL_QUADS;
1823 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1824 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1825 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1826 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1827 glEnd;
1828 }
1829
1830 glDisable GL_BLEND;
1831 glDisable GL_TEXTURE_2D;
1832}
1833
1834#############################################################################
1835
1836package CFClient::UI::Gauge;
1837
1838our @ISA = CFClient::UI::VBox::;
1839
1840sub new {
1841 my ($class, %arg) = @_;
1842
1843 my $self = $class->SUPER::new (
1844 tooltip => $arg{type},
1845 can_hover => 1,
1846 can_events => 1,
1847 %arg,
1848 );
1849
1850 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1851 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1852 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1853
1854 $self
1855}
1856
1857sub set_fontsize {
1858 my ($self, $fsize) = @_;
1859
1860 $self->{value}->set_fontsize ($fsize);
1861 $self->{max} ->set_fontsize ($fsize);
1862}
1863
1864sub set_value {
1865 my ($self, $val, $max) = @_;
1866
1867 $self->set_max ($max)
1868 if defined $max;
1869
1870 $self->{gauge}->set_value ($val, $max);
1871 $self->{value}->set_text ($val);
1872}
1873
1874sub set_max {
1875 my ($self, $max) = @_;
1876
1877 $self->{gauge}->set_max ($max);
1878 $self->{max}->set_text ($max);
1879}
1880
1881#############################################################################
1882
1523package CFClient::UI::Slider; 1883package CFClient::UI::Slider;
1524 1884
1525use strict; 1885use strict;
1526 1886
1527use SDL::OpenGL; 1887use CFClient::OpenGL;
1528 1888
1529our @ISA = CFClient::UI::DrawBG::; 1889our @ISA = CFClient::UI::DrawBG::;
1530 1890
1531my @tex = 1891my @tex =
1532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1892 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1542 # TODO: calculations are off 1902 # TODO: calculations are off
1543 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1544 fg => [1, 1, 1], 1904 fg => [1, 1, 1],
1545 active_fg => [0, 0, 0], 1905 active_fg => [0, 0, 0],
1546 range => [0, 0, 100, 10], 1906 range => [0, 0, 100, 10],
1547 req_w => 40, 1907 req_w => 20,
1548 req_h => 13, 1908 req_h => 20,
1549 vertical => 0, 1909 vertical => 0,
1550 can_hover => 1, 1910 can_hover => 1,
1551 inner_pad => 5, 1911 inner_pad => 5,
1552 @_ 1912 @_
1553 ); 1913 );
1657 2017
1658package CFClient::UI::TextView; 2018package CFClient::UI::TextView;
1659 2019
1660our @ISA = CFClient::UI::HBox::; 2020our @ISA = CFClient::UI::HBox::;
1661 2021
1662use SDL::OpenGL; 2022use CFClient::OpenGL;
1663 2023
1664sub new { 2024sub new {
1665 my $class = shift; 2025 my $class = shift;
1666 2026
1667 my $self = $class->SUPER::new ( 2027 my $self = $class->SUPER::new (
1668 req_w => $::WIDTH / 6, 2028 fontsize => 1,
1669 req_h => $::HEIGHT / 6, 2029 can_events => 0,
1670 fontsize => $::FONTSIZE, 2030 #font => default_font
1671 @_, 2031 @_,
1672 2032
1673 layout => (new CFClient::Layout), 2033 layout => (new CFClient::Layout),
1674 par => [], 2034 par => [],
1675 height => 0, 2035 height => 0,
1676 children => [ 2036 children => [
1677 (new CFClient::UI::Empty expand => 1), 2037 (new CFClient::UI::Empty expand => 1),
1678 (new CFClient::UI::Slider vertical => 1), 2038 (new CFClient::UI::Slider vertical => 1),
1679 ], 2039 ],
1680 ); 2040 );
1681 2041
1696sub text_height { 2056sub text_height {
1697 my ($self, $text) = @_; 2057 my ($self, $text) = @_;
1698 2058
1699 my $layout = $self->{layout}; 2059 my $layout = $self->{layout};
1700 2060
1701 $layout->set_height ($self->{fontsize}); 2061 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1702 $layout->set_width ($self->{w}); 2062 $layout->set_width ($self->{w});
1703 $layout->set_text ($text); 2063 $layout->set_text ($text);
1704 2064
1705 ($layout->size)[1] 2065 ($layout->size)[1]
1706} 2066}
1710 2070
1711 $self->{need_reflow}++; 2071 $self->{need_reflow}++;
1712 $self->update; 2072 $self->update;
1713} 2073}
1714 2074
1715sub size_request {
1716 my ($self) = @_;
1717
1718 ($self->{req_w}, $self->{req_h})
1719}
1720
1721sub size_allocate { 2075sub size_allocate {
1722 my ($self, $w, $h) = @_; 2076 my ($self, $w, $h) = @_;
1723 2077
2078 $self->SUPER::size_allocate ($w, $h);
2079
2080 $self->{layout}->set_font ($self->{font}) if $self->{font};
1724 $self->{layout}->set_height ($self->{fontsize}); 2081 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1725 $self->{layout}->set_width ($self->{w}); 2082 $self->{layout}->set_width ($self->{children}[0]{w});
1726 2083
1727 $self->reflow; 2084 $self->reflow;
1728 $self->update;
1729} 2085}
1730 2086
1731sub add_paragraph { 2087sub add_paragraph {
1732 my ($self, $color, $text) = @_; 2088 my ($self, $color, $text) = @_;
1733 2089
1782 2138
1783 my $y = 0; 2139 my $y = 0;
1784 2140
1785 my $layout = $self->{layout}; 2141 my $layout = $self->{layout};
1786 2142
2143 $layout->set_font ($self->{font}) if $self->{font};
2144
1787 for my $par (@{$self->{par}}) { 2145 for my $par (@{$self->{par}}) {
1788 my $h = $par->[0]; 2146 my $h = $par->[0];
1789 2147
1790 if ($y0 < $y + $h && $y < $y1) { 2148 if ($y0 < $y + $h && $y < $y1) {
1791 $layout->set_text ($par->[2]); 2149 $layout->set_text ($par->[2]);
1808 my ($self) = @_; 2166 my ($self) = @_;
1809 2167
1810 if ($self->{texture}) { 2168 if ($self->{texture}) {
1811 glEnable GL_TEXTURE_2D; 2169 glEnable GL_TEXTURE_2D;
1812 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2171 glColor 1, 1, 1, 1;
1813 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2172 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1814 glDisable GL_TEXTURE_2D; 2173 glDisable GL_TEXTURE_2D;
1815 } 2174 }
1816 2175
1817 $self->{children}[1]->draw; 2176 $self->{children}[1]->draw;
1818 2177
1819} 2178}
1820 2179
1821############################################################################# 2180#############################################################################
1822 2181
1823package CFClient::UI::MapWidget;
1824
1825use strict;
1826
1827use List::Util qw(min max);
1828
1829use SDL;
1830use SDL::OpenGL;
1831
1832our @ISA = CFClient::UI::Base::;
1833
1834sub new {
1835 my $class = shift;
1836
1837 $class->SUPER::new (
1838 z => -1,
1839 can_focus => 1,
1840 list => (glGenLists 1),
1841 @_
1842 )
1843}
1844
1845sub key_down {
1846 print "MAPKEYDOWN\n";
1847}
1848
1849sub key_up {
1850}
1851
1852sub button_down {
1853 my ($self, $ev, $x, $y) = @_;
1854
1855 $self->focus_in;
1856
1857 if ($ev->button == 2) {
1858 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1859 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1860
1861 $self->{motion} = sub {
1862 my ($ev, $x, $y) = @_;
1863
1864 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1865
1866 $::CFG->{map_shift_x} = $bw + $x - $ox;
1867 $::CFG->{map_shift_y} = $bh + $y - $oy;
1868
1869 $self->update;
1870 };
1871 }
1872}
1873
1874sub button_up {
1875 my ($self, $ev, $x, $y) = @_;
1876
1877 delete $self->{motion};
1878}
1879
1880sub mouse_motion {
1881 my ($self, $ev, $x, $y) = @_;
1882
1883 $self->{motion}->($ev, $x, $y) if $self->{motion};
1884}
1885
1886sub size_request {
1887 (
1888 1 + 32 * int $::WIDTH / 32,
1889 1 + 32 * int $::HEIGHT / 32,
1890 )
1891}
1892
1893sub update {
1894 my ($self) = @_;
1895
1896 $self->{need_update} = 1;
1897 $self->SUPER::update;
1898}
1899
1900sub draw {
1901 my ($self) = @_;
1902
1903 if (delete $self->{need_update}) {
1904 glNewList $self->{list}, GL_COMPILE;
1905
1906 if ($::MAP) {
1907 my $sw = int $::WIDTH / 32;
1908 my $sh = int $::HEIGHT / 32;
1909
1910 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1911 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1912
1913 glTranslate $sx0 - 32, $sy0 - 32, 0;
1914
1915 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1916
1917 if ($::CFG->{fow_enable}) {
1918 if ($::CFG->{fow_smooth}) { # smooth fog of war
1919 glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER);
1920 glConvolutionFilter2D (
1921 GL_CONVOLUTION_2D,
1922 GL_ALPHA,
1923 3, 3,
1924 GL_ALPHA, GL_FLOAT,
1925 pack "f*",
1926 0.1, 0.1, 0.1,
1927 0.1, 0.2, 0.1,
1928 0.1, 0.1, 0.1,
1929 );
1930 glEnable GL_CONVOLUTION_2D;
1931 }
1932
1933 $self->{fow_texture} = new CFClient::Texture
1934 w => $w,
1935 h => $h,
1936 data => $data,
1937 internalformat => GL_ALPHA,
1938 format => GL_ALPHA;
1939
1940 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1941
1942 glEnable GL_BLEND;
1943 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1944 glEnable GL_TEXTURE_2D;
1945 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1946
1947 glColor +($::CFG->{fow_intensity}) x 3, 1;
1948 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1949
1950 glDisable GL_TEXTURE_2D;
1951 glDisable GL_BLEND;
1952 }
1953
1954 # HACK BEGIN
1955 {
1956 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1957 my ($w, $h) = (250, 250);
1958
1959 glEnable GL_BLEND;
1960 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1961 glEnable GL_TEXTURE_2D;
1962 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1963
1964 $self->{mapmap_texture} =
1965 new CFClient::Texture
1966 w => $w,
1967 h => $h,
1968 data => $::MAP->mapmap ($w, $h),
1969 type => $CFClient::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE;
1970
1971 $self->{mapmap_texture}->draw_quad (100, 100);
1972
1973 glDisable GL_TEXTURE_2D;
1974 glDisable GL_BLEND;
1975 }
1976 # HACK END
1977 }
1978
1979 glEndList;
1980 }
1981
1982 glPushMatrix;
1983 glCallList $self->{list};
1984 glPopMatrix;
1985
1986 if ($FOCUS != $self) {
1987 glColor 64/255, 64/255, 64/255;
1988 glLogicOp GL_AND;
1989 glEnable GL_COLOR_LOGIC_OP;
1990 glBegin GL_QUADS;
1991 glVertex 0, 0;
1992 glVertex 0, $::HEIGHT;
1993 glVertex $::WIDTH, $::HEIGHT;
1994 glVertex $::WIDTH, 0;
1995 glEnd;
1996 glDisable GL_COLOR_LOGIC_OP;
1997 }
1998}
1999
2000my %DIR = (
2001 SDLK_KP8, [1, "north"],
2002 SDLK_KP9, [2, "northeast"],
2003 SDLK_KP6, [3, "east"],
2004 SDLK_KP3, [4, "southeast"],
2005 SDLK_KP2, [5, "south"],
2006 SDLK_KP1, [6, "southwest"],
2007 SDLK_KP4, [7, "west"],
2008 SDLK_KP7, [8, "northwest"],
2009
2010 SDLK_UP, [1, "north"],
2011 SDLK_RIGHT, [3, "east"],
2012 SDLK_DOWN, [5, "south"],
2013 SDLK_LEFT, [7, "west"],
2014);
2015
2016sub key_down {
2017 my ($self, $ev) = @_;
2018
2019 my $mod = $ev->key_mod;
2020 my $sym = $ev->key_sym;
2021
2022 if ($sym == SDLK_KP5) {
2023 $::CONN->user_send ("stay fire");
2024 } elsif ($sym == SDLK_a) {
2025 $::CONN->user_send ("apply");
2026 } elsif ($sym == SDLK_QUOTE) {
2027 $self->emit ('activate_console');
2028 } elsif ($sym == SDLK_SLASH) {
2029 $self->emit ('activate_console' => '/');
2030 } elsif (exists $DIR{$sym}) {
2031 if ($mod & KMOD_SHIFT) {
2032 $self->{shft}++;
2033 $::CONN->user_send ("fire $DIR{$sym}[0]");
2034 } elsif ($mod & KMOD_CTRL) {
2035 $self->{ctrl}++;
2036 $::CONN->user_send ("run $DIR{$sym}[0]");
2037 } else {
2038 $::CONN->user_send ("$DIR{$sym}[1]");
2039 }
2040 }
2041}
2042
2043sub key_up {
2044 my ($self, $ev) = @_;
2045
2046 my $mod = $ev->key_mod;
2047 my $sym = $ev->key_sym;
2048
2049 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2050 $::CONN->user_send ("fire_stop");
2051 }
2052 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2053 $::CONN->user_send ("run_stop");
2054 }
2055}
2056
2057#############################################################################
2058
2059package CFClient::UI::Animator; 2182package CFClient::UI::Animator;
2060 2183
2061use SDL::OpenGL; 2184use CFClient::OpenGL;
2062 2185
2063our @ISA = CFClient::UI::Bin::; 2186our @ISA = CFClient::UI::Bin::;
2064 2187
2065sub moveto { 2188sub moveto {
2066 my ($self, $x, $y) = @_; 2189 my ($self, $x, $y) = @_;
2104 2227
2105sub new { 2228sub new {
2106 my $class = shift; 2229 my $class = shift;
2107 2230
2108 my $self = $class->SUPER::new ( 2231 my $self = $class->SUPER::new (
2109 state => 0, 2232 state => 0,
2110 connect_activate => \&toggle_flopper, 2233 connect_activate => \&toggle_flopper,
2111 @_ 2234 @_
2112 ); 2235 );
2113 2236
2114 if ($self->{state}) { 2237 if ($self->{state}) {
2135 $self->emit (changed => $self->{state}); 2258 $self->emit (changed => $self->{state});
2136} 2259}
2137 2260
2138############################################################################# 2261#############################################################################
2139 2262
2263package CFClient::UI::Tooltip;
2264
2265our @ISA = CFClient::UI::Bin::;
2266
2267use CFClient::OpenGL;
2268
2269sub new {
2270 my $class = shift;
2271
2272 $class->SUPER::new (
2273 @_,
2274 can_events => 0,
2275 )
2276}
2277
2278sub set_markup {
2279 my ($self, $text) = @_;
2280
2281 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2282 $self->{label}->set_markup ($text);
2283 $self->add ($self->{label});
2284}
2285
2286sub size_request {
2287 my ($self) = @_;
2288
2289 $self->child->set_max_size ($::WIDTH * 0.3);
2290
2291 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2292
2293 ($w + 4, $h + 4)
2294}
2295
2296sub size_allocate {
2297 my ($self, $w, $h) = @_;
2298
2299 $self->SUPER::size_allocate ($w - 4, $h - 4);
2300}
2301
2302sub _draw {
2303 my ($self) = @_;
2304
2305 glPushMatrix;
2306 glTranslate 0.375, 0.375;
2307
2308 my ($w, $h) = @$self{qw(w h)};
2309
2310 glColor 1, 0.8, 0.4;
2311 glBegin GL_QUADS;
2312 glVertex 0 , 0;
2313 glVertex 0 , $h;
2314 glVertex $w, $h;
2315 glVertex $w, 0;
2316 glEnd;
2317
2318 glColor 0, 0, 0;
2319 glBegin GL_LINE_LOOP;
2320 glVertex 0 , 0;
2321 glVertex 0 , $h;
2322 glVertex $w, $h;
2323 glVertex $w, 0;
2324 glEnd;
2325
2326 glPopMatrix;
2327
2328 glTranslate 2, 2;
2329 $self->SUPER::_draw;
2330}
2331
2332#############################################################################
2333
2334package CFClient::UI::Face;
2335
2336our @ISA = CFClient::UI::Base::;
2337
2338use CFClient::OpenGL;
2339
2340sub new {
2341 my $class = shift;
2342
2343 $class->SUPER::new (
2344 aspect => 1,
2345 @_,
2346 )
2347}
2348
2349sub size_request {
2350 (32, 8)
2351}
2352
2353sub draw {
2354 my ($self) = @_;
2355
2356 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2357
2358 if ($tex) {
2359 glEnable GL_BLEND;
2360 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2361 glEnable GL_TEXTURE_2D;
2362 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2363 glColor 1, 1, 1, 1;
2364 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2365 glDisable GL_TEXTURE_2D;
2366 glDisable GL_BLEND;
2367 }
2368}
2369
2370#############################################################################
2371
2140package CFClient::UI::Root; 2372package CFClient::UI::Root;
2141 2373
2142our @ISA = CFClient::UI::Container::; 2374our @ISA = CFClient::UI::Container::;
2143 2375
2144use SDL::OpenGL; 2376use CFClient::OpenGL;
2145 2377
2146sub check_size { 2378sub check_size {
2147 my ($self) = @_; 2379 my ($self) = @_;
2148 2380
2149 $self->configure (0, 0, $::WITH, $::HEIGHT); 2381 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2150} 2382}
2151 2383
2152sub size_request { 2384sub size_request {
2153 ($::WIDTH, $::HEIGHT) 2385 ($::WIDTH, $::HEIGHT)
2154} 2386}
2156sub configure { 2388sub configure {
2157 my ($self, $x, $y, $w, $h) = @_; 2389 my ($self, $x, $y, $w, $h) = @_;
2158 2390
2159 $self->SUPER::configure ($x, $y, $w, $h); 2391 $self->SUPER::configure ($x, $y, $w, $h);
2160 2392
2161 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2162 for @{$self->{children}}; 2393 for my $child (@{$self->{children}}) {
2394 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2395
2396 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2397 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2398 $child->configure ($X, $Y, $W,$H);
2399 }
2163} 2400}
2164 2401
2165sub _topleft { 2402sub _topleft {
2166 my ($self, $x, $y) = @_; 2403 my ($self, $x, $y) = @_;
2167 2404
2175 ::refresh (); 2412 ::refresh ();
2176} 2413}
2177 2414
2178sub add { 2415sub add {
2179 my ($self, $child) = @_; 2416 my ($self, $child) = @_;
2417
2418 # integerize window positions
2419 $child->{x} = int $child->{x};
2420 $child->{y} = int $child->{y};
2180 2421
2181 $self->SUPER::add ($child); 2422 $self->SUPER::add ($child);
2182} 2423}
2183 2424
2184sub on_refresh { 2425sub on_refresh {
2210############################################################################# 2451#############################################################################
2211 2452
2212package CFClient::UI; 2453package CFClient::UI;
2213 2454
2214$ROOT = new CFClient::UI::Root; 2455$ROOT = new CFClient::UI::Root;
2456$TOOLTIP = new CFClient::UI::Tooltip;
2215 2457
22161 24581
2217 2459

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines