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.125 by root, Mon Apr 17 20:52:15 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) = @_;
185
117 $self->{x} = int $x; 186 $self->{x} = int $x;
118 $self->{y} = int $y; 187 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 188 $self->{z} = $z if defined $z;
120}
121 189
122sub needs_redraw { 190 $self->update;
123 0 191}
192
193sub set_size {
194 my ($self, $w, $h) = @_;
195
196 $self->{user_w} = $w;
197 $self->{user_h} = $h;
198
199 $self->check_size;
124} 200}
125 201
126sub size_request { 202sub size_request {
127 require Carp; 203 require Carp;
128 Carp::confess "size_request is abtract"; 204 Carp::confess "size_request is abstract";
129} 205}
130 206
131sub _size_allocate { 207sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 208 my ($self, $x, $y, $w, $h) = @_;
133 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) {
134 $self->{x} = $x; 223 $self->{x} = $x;
135 $self->{y} = $y; 224 $self->{y} = $y;
225 $self->update;
226 }
136 227
137 return unless $self->{w} != $w || $self->{h} != $h; 228 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 229 $self->{w} = $w;
140 $self->{h} = $h; 230 $self->{h} = $h;
141 231
232 $self->size_allocate ($w, $h);
233 $self->update;
142 1 234 }
143} 235}
144 236
145sub size_allocate { 237sub size_allocate {
238 # nothing to be done
239}
240
241sub children {
242}
243
244sub set_max_size {
146 my ($self, $x, $y, $w, $h) = @_; 245 my ($self, $w, $h) = @_;
147 246
148 $self->_size_allocate ($x, $y, $w, $h); 247 delete $self->{max_w}; $self->{max_w} = $w if $w;
248 delete $self->{max_h}; $self->{max_h} = $h if $h;
149} 249}
150 250
151# return top left coordinates 251# return top left coordinates
152sub _topleft { 252sub _topleft {
153 my ($self, $x, $y) = @_; 253 my ($self, $x, $y) = @_;
254
255 $self->{parent}
256 or Carp::confess "no parent widget in _topleft\n";#d#
154 257
155 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y}); 258 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
156} 259}
157 260
158# translate global coordinates to local coordinate system 261# translate global coordinates to local coordinate system
236 glVertex $x + $self->{w}, $y + $self->{h}; 339 glVertex $x + $self->{w}, $y + $self->{h};
237 glVertex $x , $y + $self->{h}; 340 glVertex $x , $y + $self->{h};
238 glEnd; 341 glEnd;
239 glDisable GL_BLEND; 342 glDisable GL_BLEND;
240 } 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 }
241} 358}
242 359
243sub _draw { 360sub _draw {
244 my ($self) = @_; 361 my ($self) = @_;
245 362
246 warn "no draw defined for $self\n"; 363 warn "no draw defined for $self\n";
247} 364}
248 365
249sub find_widget { 366sub find_widget {
250 my ($self, $x, $y) = @_; 367 my ($self, $x, $y) = @_;
368
369 return () unless $self->{can_events};
251 370
252 return $self 371 return $self
253 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 372 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
254 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 373 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
255 374
263} 382}
264 383
265sub check_size { 384sub check_size {
266 my ($self) = @_; 385 my ($self) = @_;
267 386
268 my ($w, $h) = $self->size_request; 387 $self->{parent}
388 or return 1;
269 389
390 my ($w, $h) = $self->{user_w} && $self->{user_h}
391 ? @$self{qw(user_w user_h)}
392 : $self->size_request;
393
270 if ($w != $self->{req_w} || $h != $self->{req_h}) { 394 if ($w != $self->{req_w} || $h != $self->{req_h}) {
271 $self->{req_w} = $w; 395 $self->{req_w} = $w;
272 $self->{req_h} = $h; 396 $self->{req_h} = $h;
273 397
274 $self->{parent}->check_size 398 $self->{parent}->check_size
275 if $self->{parent}; 399 or $self->size_allocate (
400 (List::Util::max $self->{w}, $w),
401 (List::Util::max $self->{h}, $h),
402 );
403
404 1
405 } else {
406 0
276 } 407 }
277} 408}
278 409
279sub update { 410sub update {
280 my ($self) = @_; 411 my ($self) = @_;
284} 415}
285 416
286sub connect { 417sub connect {
287 my ($self, $signal, $cb) = @_; 418 my ($self, $signal, $cb) = @_;
288 419
289 push @{ $self->{cb}{$signal} }, $cb; 420 push @{ $self->{signal_cb}{$signal} }, $cb;
290} 421}
291 422
292sub emit { 423sub emit {
293 my ($self, $signal, @args) = @_; 424 my ($self, $signal, @args) = @_;
294 425
295 $_->($self, @args) 426 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
296 for @{$self->{cb}{$signal} || []};
297} 427}
298 428
299sub DESTROY { 429sub DESTROY {
300 my ($self) = @_; 430 my ($self) = @_;
301 431
307package CFClient::UI::DrawBG; 437package CFClient::UI::DrawBG;
308 438
309our @ISA = CFClient::UI::Base::; 439our @ISA = CFClient::UI::Base::;
310 440
311use strict; 441use strict;
312use SDL::OpenGL; 442use CFClient::OpenGL;
313 443
314sub new { 444sub new {
315 my $class = shift; 445 my $class = shift;
316 446
317 # range [value, low, high, page] 447 # range [value, low, high, page]
346 476
347package CFClient::UI::Empty; 477package CFClient::UI::Empty;
348 478
349our @ISA = CFClient::UI::Base::; 479our @ISA = CFClient::UI::Base::;
350 480
481sub new {
482 my ($class, %arg) = @_;
483 $class->SUPER::new (can_events => 0, %arg);
484}
485
351sub size_request { 486sub size_request {
352 (0, 0) 487 (0, 0)
353} 488}
354 489
355sub draw { } 490sub draw { }
363sub new { 498sub new {
364 my ($class, %arg) = @_; 499 my ($class, %arg) = @_;
365 500
366 my $children = delete $arg{children} || []; 501 my $children = delete $arg{children} || [];
367 502
368 my $self = $class->SUPER::new (children => [], %arg); 503 my $self = $class->SUPER::new (
504 children => [],
505 can_events => 0,
506 %arg,
507 );
369 $self->add ($_) for @$children; 508 $self->add ($_) for @$children;
370 509
371 $self 510 $self
372} 511}
373 512
381 $self->{children} = [ 520 $self->{children} = [
382 sort { $a->{z} <=> $b->{z} } 521 sort { $a->{z} <=> $b->{z} }
383 @{$self->{children}}, $child 522 @{$self->{children}}, $child
384 ]; 523 ];
385 524
386 $self->{w} = $self->{h} = -1; 525 $child->check_size;
526}
527
528sub children {
529 @{ $_[0]{children} }
530}
531
532sub remove {
533 my ($self, $child) = @_;
534
535 delete $child->{parent};
536 $child->hide;
537
538 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
539
540 $self->check_size;
387 $self->update; 541 $self->update;
388} 542}
389 543
390sub remove { 544sub clear {
391 my ($self, $widget) = @_; 545 my ($self) = @_;
392 546
393 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 547 my $children = delete $self->{children};
548 $self->{children} = [];
394 549
395 $self->check_size; 550 for (@$children) {
551 delete $_->{parent};
552 $_->hide;
553 }
396} 554}
397 555
398sub find_widget { 556sub find_widget {
399 my ($self, $x, $y) = @_; 557 my ($self, $x, $y) = @_;
400 558
430 588
431 $class->SUPER::new (children => [$child], %arg) 589 $class->SUPER::new (children => [$child], %arg)
432} 590}
433 591
434sub add { 592sub add {
435 my ($self, $widget) = @_; 593 my ($self, $child) = @_;
436 594
437 $self->{children} = []; 595 $self->{children} = [];
438 596
439 $self->SUPER::add ($widget); 597 $self->SUPER::add ($child);
440} 598}
441 599
442sub remove { 600sub remove {
443 my ($self, $widget) = @_; 601 my ($self, $widget) = @_;
444 602
453sub size_request { 611sub size_request {
454 $_[0]{children}[0]->size_request 612 $_[0]{children}[0]->size_request
455} 613}
456 614
457sub size_allocate { 615sub size_allocate {
458 my ($self, $x, $y, $w, $h) = @_; 616 my ($self, $w, $h) = @_;
459 617
460 $self->_size_allocate ($x, $y, $w, $h) or return;
461
462 $self->{children}[0]->size_allocate (0, 0, $w, $h); 618 $self->{children}[0]->configure (0, 0, $w, $h);
463} 619}
464 620
465############################################################################# 621#############################################################################
466 622
467package CFClient::UI::Window; 623package CFClient::UI::Window;
468 624
469our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
470 626
471use SDL::OpenGL; 627use CFClient::OpenGL;
472 628
473sub new { 629sub new {
474 my ($class, %arg) = @_; 630 my ($class, %arg) = @_;
475 631
476 my $self = $class->SUPER::new (%arg); 632 my $self = $class->SUPER::new (%arg);
493 $self->child->draw; 649 $self->child->draw;
494 }; 650 };
495} 651}
496 652
497sub size_allocate { 653sub size_allocate {
498 my ($self, $x, $y, $w, $h) = @_; 654 my ($self, $w, $h) = @_;
499 655
500 $self->_size_allocate ($x, $y, $w, $h) or return;
501
502 $self->child->size_allocate (0, 0, $w, $h); 656 $self->child->configure (0, 0, $w, $h);
503 657
504 $self->render_chld; 658 $self->render_chld;
505} 659}
506 660
507sub _draw { 661sub _draw {
532sub new { die } 686sub new { die }
533 687
534sub size_request { 688sub size_request {
535 my ($self) = @_; 689 my ($self) = @_;
536 690
537 @$self{qw(child_w child_h)} = $self->child->size_request; 691 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
538 $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)});
539 693
540 @$self{qw(child_w child_h)} 694 @$self{qw(child_w child_h)}
541} 695}
542 696
543sub size_allocate {
544 my ($self, $x, $y, $w, $h) = @_;
545
546 $self->_size_allocate ($x, $y, $w, $h) or return;
547}
548
549sub _draw { 697sub _draw {
550 my ($self) = @_; 698 my ($self) = @_;
551 699
552 $self->{children}[1]->draw; 700 $self->{children}[1]->draw;
553} 701}
557 705
558package CFClient::UI::Frame; 706package CFClient::UI::Frame;
559 707
560our @ISA = CFClient::UI::Bin::; 708our @ISA = CFClient::UI::Bin::;
561 709
562use SDL::OpenGL; 710use CFClient::OpenGL;
563
564sub size_request {
565 my ($self) = @_;
566 my $chld = $self->child
567 or return (0, 0);
568
569 $chld->move (2, 2);
570
571 map { $_ + 4 } $chld->size_request;
572}
573
574sub size_allocate {
575 my ($self, $x, $y, $w, $h) = @_;
576
577 $self->_size_allocate ($x, $y, $w, $h) or return;
578
579 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
580}
581
582sub _draw {
583 my ($self) = @_;
584
585 my $chld = $self->child;
586
587 my ($w, $h) = $chld->size_request;
588
589 glBegin GL_QUADS;
590 glColor 0, 0, 0;
591 glVertex 0 , 0;
592 glVertex 0 , $h + 4;
593 glVertex $w + 4 , $h + 4;
594 glVertex $w + 4 , 0;
595 glEnd;
596
597 $chld->draw;
598}
599
600#############################################################################
601
602package CFClient::UI::FancyFrame;
603
604our @ISA = CFClient::UI::Bin::;
605
606use SDL::OpenGL;
607
608my @tex =
609 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
610 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
611 711
612sub new { 712sub new {
613 my $class = shift; 713 my $class = shift;
614 714
615 # TODO: user_x, user_y, overwrite moveto?
616
617 $class->SUPER::new ( 715 my $self = $class->SUPER::new (
618 bg => [1, 1, 1, 1], 716 bg => [1, 1, 1, 1],
619 border_bg => [1, 1, 1, 1], 717 border_bg => [1, 1, 1, 1],
620 border => int $::FONTSIZE * 0.8, 718 border => 0.8,
621 @_ 719 @_
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
785}
786
787sub size_request {
788 my ($self) = @_;
789
790 my ($w, $h) = $self->SUPER::size_request;
791
792 (
793 $w + $self->border * 2,
794 $h + $self->border * 2,
622 ) 795 )
623} 796}
624 797
625sub size_request {
626 my ($self) = @_;
627
628 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
629
630 my ($w, $h) = $self->SUPER::size_request;
631
632 (
633 $w + $self->{border} * 2,
634 $h + $self->{border} * 2,
635 )
636}
637
638sub size_allocate { 798sub size_allocate {
639 my ($self, $x, $y, $w, $h) = @_; 799 my ($self, $w, $h) = @_;
640 800
641 $self->_size_allocate ($x, $y, $w, $h) or return;
642
643 $h -= List::Util::max 0, $self->{border} * 2; 801 $h -= List::Util::max 0, $self->border * 2;
644 $w -= List::Util::max 0, $self->{border} * 2; 802 $w -= List::Util::max 0, $self->border * 2;
645 803
804 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
805 if $self->{title};
806
646 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 807 $self->child->configure ($self->border, $self->border, $w, $h);
647} 808}
648 809
649sub button_down { 810sub button_down {
650 my ($self, $ev, $x, $y) = @_; 811 my ($self, $ev, $x, $y) = @_;
651 812
813 my $border = $self->border;
814
652 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 815 if ($x < $self->{w} && $x >= $self->{w} - $border
653 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 816 && $y < $self->{h} && $y >= $self->{h} - $border) {
654 817
655 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 818 my ($ox, $oy) = ($ev->{x}, $ev->{y});
656 my ($bw, $bh) = ($self->{w}, $self->{h}); 819 my ($bw, $bh) = ($self->{w}, $self->{h});
657 820
658 $self->{motion} = sub { 821 $self->{motion} = sub {
659 my ($ev, $x, $y) = @_; 822 my ($ev, $x, $y) = @_;
660 823
661 ($x, $y) = ($ev->motion_x, $ev->motion_y); 824 ($x, $y) = ($ev->{x}, $ev->{y});
662 825
663 $self->{user_w} = $bw + $x - $ox; 826 $self->{user_w} = $bw + $x - $ox;
664 $self->{user_h} = $bh + $y - $oy; 827 $self->{user_h} = $bh + $y - $oy;
665 $self->update; 828 $self->check_size;
666 }; 829 };
667 830
668 } elsif ($x >= 0 && $x < $self->{w} 831 } elsif ($x >= 0 && $x < $self->{w}
669 && $y >= 0 && $y < $self->{border}) { 832 && $y >= 0 && $y < $border) {
670 833
671 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 834 my ($ox, $oy) = ($ev->{x}, $ev->{y});
672 my ($bx, $by) = ($self->{x}, $self->{y}); 835 my ($bx, $by) = ($self->{x}, $self->{y});
673 836
674 $self->{motion} = sub { 837 $self->{motion} = sub {
675 my ($ev, $x, $y) = @_; 838 my ($ev, $x, $y) = @_;
676 839
677 ($x, $y) = ($ev->motion_x, $ev->motion_y); 840 ($x, $y) = ($ev->{x}, $ev->{y});
678 841
679 $self->move ($bx + $x - $ox, $by + $y - $oy); 842 $self->move ($bx + $x - $ox, $by + $y - $oy);
680 $self->update; 843 $self->update;
681 }; 844 };
682 } 845 }
703 glEnable GL_BLEND; 866 glEnable GL_BLEND;
704 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 867 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
705 glEnable GL_TEXTURE_2D; 868 glEnable GL_TEXTURE_2D;
706 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 869 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
707 870
871 my $border = $self->border;
872
708 glColor @{ $self->{border_bg} }; 873 glColor @{ $self->{border_bg} };
709 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 874 $tex[1]->draw_quad (0, 0, $w, $border);
710 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 875 $tex[3]->draw_quad (0, $border, $border, $ch);
711 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 876 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
712 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 877 $tex[4]->draw_quad (0, $h - $border, $w, $border);
713 878
714 my $bg = $tex[0]; 879 my $bg = $tex[0];
715 880
716 # TODO: repeat texture not scale 881 # TODO: repeat texture not scale
717 my $rep_x = $cw / $bg->{w}; 882 my $rep_x = $cw / $bg->{w};
720 glColor @{ $self->{bg} }; 885 glColor @{ $self->{bg} };
721 886
722 $bg->{s} = $rep_x; 887 $bg->{s} = $rep_x;
723 $bg->{t} = $rep_y; 888 $bg->{t} = $rep_y;
724 $bg->{wrap_mode} = 1; 889 $bg->{wrap_mode} = 1;
725 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 890 $bg->draw_quad ($border, $border, $cw, $ch);
726 891
727 glDisable GL_TEXTURE_2D; 892 glDisable GL_TEXTURE_2D;
728 glDisable GL_BLEND; 893 glDisable GL_BLEND;
729 894
895 $self->{title}->draw if $self->{title};
730 $self->child->draw; 896 $self->child->draw;
731} 897}
732 898
733############################################################################# 899#############################################################################
734 900
736 902
737our @ISA = CFClient::UI::Base::; 903our @ISA = CFClient::UI::Base::;
738 904
739use List::Util qw(max sum); 905use List::Util qw(max sum);
740 906
741use SDL::OpenGL; 907use CFClient::OpenGL;
742 908
743sub new { 909sub new {
744 my $class = shift; 910 my $class = shift;
745 911
746 $class->SUPER::new ( 912 $class->SUPER::new (
753 my ($self, $x, $y, $child) = @_; 919 my ($self, $x, $y, $child) = @_;
754 920
755 $child->set_parent ($self); 921 $child->set_parent ($self);
756 $self->{children}[$y][$x] = $child; 922 $self->{children}[$y][$x] = $child;
757 923
758 $self->{w} = $self->{h} = -1; 924 $child->check_size;
759 $self->update; 925}
926
927sub children {
928 grep $_, map @$_, grep $_, @{ $_[0]{children} }
760} 929}
761 930
762# 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?
763sub clear { 932sub clear {
764 my ($self) = @_; 933 my ($self) = @_;
765 934
935 my @children = $self->children;
766 delete $self->{children}; 936 delete $self->{children};
937
938 for (@children) {
939 delete $_->{parent};
940 $_->hide;
941 }
942
767 $self->update; 943 $self->update;
768} 944}
769 945
770sub get_wh { 946sub get_wh {
771 my ($self) = @_; 947 my ($self) = @_;
777 or next; 953 or next;
778 954
779 for my $x (0 .. $#$row) { 955 for my $x (0 .. $#$row) {
780 my $widget = $row->[$x] 956 my $widget = $row->[$x]
781 or next; 957 or next;
782 my ($w, $h) = $widget->size_request; 958 my ($w, $h) = @$widget{qw(req_w req_h)};
783 959
784 $w[$x] = max $w[$x], $w; 960 $w[$x] = max $w[$x], $w;
785 $h[$y] = max $h[$y], $h; 961 $h[$y] = max $h[$y], $h;
786 } 962 }
787 } 963 }
799 (sum @$hs), 975 (sum @$hs),
800 ) 976 )
801} 977}
802 978
803sub size_allocate { 979sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_; 980 my ($self, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h) or return;
807 981
808 my ($ws, $hs) = $self->get_wh; 982 my ($ws, $hs) = $self->get_wh;
809 983
810 my $req_w = sum @$ws; 984 my $req_w = sum @$ws;
811 my $req_h = sum @$hs; 985 my $req_h = sum @$hs;
833 1007
834 for my $c (0 .. $#$row) { 1008 for my $c (0 .. $#$row) {
835 my $col_w = $ws->[$c]; 1009 my $col_w = $ws->[$c];
836 1010
837 if (my $widget = $row->[$c]) { 1011 if (my $widget = $row->[$c]) {
838 $widget->size_allocate ($x, $y, $col_w, $row_h); 1012 $widget->configure ($x, $y, $col_w, $row_h);
839 } 1013 }
840 1014
841 $x += $col_w; 1015 $x += $col_w;
842 } 1016 }
843 1017
888 (List::Util::max map $_->[1], @alloc), 1062 (List::Util::max map $_->[1], @alloc),
889 ) 1063 )
890} 1064}
891 1065
892sub size_allocate { 1066sub size_allocate {
893 my ($self, $x, $y, $w, $h) = @_; 1067 my ($self, $w, $h) = @_;
894
895 $self->_size_allocate ($x, $y, $w, $h) or return;
896 1068
897 ($h, $w) = ($w, $h); 1069 ($h, $w) = ($w, $h);
898 1070
899 my $children = $self->{children}; 1071 my $children = $self->{children};
900 1072
901 my @h = map +($_->size_request)[0], @$children; 1073 my @h = map $_->{req_w}, @$children;
902 1074
903 my $req_h = List::Util::sum @h; 1075 my $req_h = List::Util::sum @h;
904 1076
905 if ($req_h > $h) { 1077 if ($req_h > $h) {
906 # ah well, not enough space 1078 # ah well, not enough space
922 1094
923 my $y = 0; 1095 my $y = 0;
924 for (0 .. $#$children) { 1096 for (0 .. $#$children) {
925 my $child = $children->[$_]; 1097 my $child = $children->[$_];
926 my $h = $h[$_]; 1098 my $h = $h[$_];
927 $child->size_allocate ($y, 0, $h, $w); 1099 $child->configure ($y, 0, $h, $w);
928 1100
929 $y += $h; 1101 $y += $h;
930 } 1102 }
931 1103
932 1 1104 1
950 (List::Util::sum map $_->[1], @alloc), 1122 (List::Util::sum map $_->[1], @alloc),
951 ) 1123 )
952} 1124}
953 1125
954sub size_allocate { 1126sub size_allocate {
955 my ($self, $x, $y, $w, $h) = @_; 1127 my ($self, $w, $h) = @_;
956
957 $self->_size_allocate ($x, $y, $w, $h) or return;
958 1128
959 my $children = $self->{children}; 1129 my $children = $self->{children};
960 1130
961 my @h = map +($_->size_request)[1], @$children; 1131 my @h = map $_->{req_h}, @$children;
962 1132
963 my $req_h = List::Util::sum @h; 1133 my $req_h = List::Util::sum @h;
964 1134
965 if ($req_h > $h) { 1135 if ($req_h > $h) {
966 # ah well, not enough space 1136 # ah well, not enough space
980 1150
981 my $y = 0; 1151 my $y = 0;
982 for (0 .. $#$children) { 1152 for (0 .. $#$children) {
983 my $child = $children->[$_]; 1153 my $child = $children->[$_];
984 my $h = $h[$_]; 1154 my $h = $h[$_];
985 $child->size_allocate (0, $y, $w, $h); 1155 $child->configure (0, $y, $w, $h);
986 1156
987 $y += $h; 1157 $y += $h;
988 } 1158 }
989 1159
990 1 1160 1
994 1164
995package CFClient::UI::Label; 1165package CFClient::UI::Label;
996 1166
997our @ISA = CFClient::UI::Base::; 1167our @ISA = CFClient::UI::Base::;
998 1168
999use SDL::OpenGL; 1169use CFClient::OpenGL;
1000 1170
1001sub new { 1171sub new {
1002 my ($class, %arg) = @_; 1172 my ($class, %arg) = @_;
1003 1173
1004 my $self = $class->SUPER::new ( 1174 my $self = $class->SUPER::new (
1005 fg => [1, 1, 1], 1175 fg => [1, 1, 1],
1006 fontsize => $::FONTSIZE, 1176 #font => default_font
1177 fontsize => 1,
1007 text => "", 1178 text => "",
1008 align => -1, 1179 align => -1,
1009 valign => -1, 1180 valign => -1,
1010 padding => 2, 1181 padding => 2,
1011 layout => new CFClient::Layout, 1182 layout => new CFClient::Layout,
1183 can_events => 0,
1012 %arg 1184 %arg
1013 ); 1185 );
1014 1186
1015 $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 }
1016 1192
1017 $self->set_text (delete $self->{text}) if exists $self->{text}; 1193 $self->set_text (delete $self->{text}) if exists $self->{text};
1018 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1194 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1019 1195
1020 $self 1196 $self
1021} 1197}
1022 1198
1023sub escape_text { 1199sub escape {
1024 local $_ = $_[1]; 1200 local $_ = $_[1];
1025 1201
1026 s/&/&amp;/g; 1202 s/&/&amp;/g;
1027 s/>/&gt;/g; 1203 s/>/&gt;/g;
1028 s/</&lt;/g; 1204 s/</&lt;/g;
1034 my ($self, $text) = @_; 1210 my ($self, $text) = @_;
1035 1211
1036 $self->{layout}->set_text ($text); 1212 $self->{layout}->set_text ($text);
1037 1213
1038 delete $self->{texture}; 1214 delete $self->{texture};
1215 $self->check_size;
1039 $self->update; 1216 $self->update;
1040} 1217}
1041 1218
1042sub set_markup { 1219sub set_markup {
1043 my ($self, $markup) = @_; 1220 my ($self, $markup) = @_;
1044 1221
1045 $self->{layout}->set_markup ($markup); 1222 $self->{layout}->set_markup ($markup);
1046 1223
1047 delete $self->{texture}; 1224 delete $self->{texture};
1225 $self->check_size;
1048 $self->update; 1226 $self->update;
1049} 1227}
1050 1228
1051sub size_request { 1229sub size_request {
1052 my ($self) = @_; 1230 my ($self) = @_;
1053 1231
1054 $self->{layout}->set_width; 1232 $self->{layout}->set_font ($self->{font}) if $self->{font};
1233 $self->{layout}->set_width ($self->{max_w} || -1);
1055 $self->{layout}->set_height ($self->{fontsize}); 1234 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1056 1235
1057 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 }
1058 1247
1059 ( 1248 (
1060 $w + $self->{padding} * 2, 1249 $w + $self->{padding} * 2,
1061 $h + $self->{padding} * 2, 1250 $h + $self->{padding} * 2,
1062 ) 1251 )
1063} 1252}
1064 1253
1065sub size_allocate { 1254sub size_allocate {
1066 my ($self, $x, $y, $w, $h) = @_; 1255 my ($self, $w, $h) = @_;
1067
1068 $self->_size_allocate ($x, $y, $w, $h) or return;
1069 1256
1070 delete $self->{texture}; 1257 delete $self->{texture};
1071} 1258}
1072 1259
1073sub update { 1260sub set_fontsize {
1074 my ($self) = @_; 1261 my ($self, $fontsize) = @_;
1075 1262
1263 $self->{fontsize} = $fontsize;
1076 delete $self->{texture}; 1264 delete $self->{texture};
1265 $self->check_size;
1077 $self->SUPER::update; 1266 $self->update;
1078} 1267}
1079 1268
1080sub _draw { 1269sub _draw {
1081 my ($self) = @_; 1270 my ($self) = @_;
1082 1271
1083 my $tex = $self->{texture} ||= do { 1272 my $tex = $self->{texture} ||= do {
1273 $self->{layout}->set_font ($self->{font}) if $self->{font};
1084 $self->{layout}->set_width ($self->{w}); 1274 $self->{layout}->set_width ($self->{w});
1085 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1275 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1086 new_from_layout CFClient::Texture $self->{layout} 1276 new_from_layout CFClient::Texture $self->{layout}
1087 }; 1277 };
1088 1278
1089 glEnable GL_BLEND; 1279 glEnable GL_BLEND;
1090 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1280 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1115 1305
1116package CFClient::UI::EntryBase; 1306package CFClient::UI::EntryBase;
1117 1307
1118our @ISA = CFClient::UI::Label::; 1308our @ISA = CFClient::UI::Label::;
1119 1309
1120use SDL; 1310use CFClient::OpenGL;
1121use SDL::OpenGL;
1122 1311
1123sub new { 1312sub new {
1124 my $class = shift; 1313 my $class = shift;
1125 1314
1126 $class->SUPER::new ( 1315 $class->SUPER::new (
1127 fg => [1, 1, 1], 1316 fg => [1, 1, 1],
1128 bg => [0, 0, 0, 0.2], 1317 bg => [0, 0, 0, 0.2],
1129 active_bg => [1, 1, 1, 0.5], 1318 active_bg => [1, 1, 1, 0.5],
1130 active_fg => [0, 0, 0], 1319 active_fg => [0, 0, 0],
1131 can_hover => 1, 1320 can_hover => 1,
1132 can_focus => 1, 1321 can_focus => 1,
1133 valign => 0, 1322 valign => 0,
1323 can_events => 1,
1134 @_ 1324 @_
1135 ) 1325 )
1136} 1326}
1137 1327
1138sub _set_text { 1328sub _set_text {
1140 1330
1141 delete $self->{cur_h}; 1331 delete $self->{cur_h};
1142 1332
1143 return if $self->{text} eq $text; 1333 return if $self->{text} eq $text;
1144 1334
1335 delete $self->{texture};
1336
1145 $self->{last_activity} = $::NOW; 1337 $self->{last_activity} = $::NOW;
1146 $self->{text} = $text; 1338 $self->{text} = $text;
1147 1339
1148 $text =~ s/./*/g if $self->{hidden}; 1340 $text =~ s/./*/g if $self->{hidden};
1149 $self->{layout}->set_text ("$text "); 1341 $self->{layout}->set_text ("$text ");
1162 1354
1163 ($w + 1, $h) # add 1 for cursor 1355 ($w + 1, $h) # add 1 for cursor
1164} 1356}
1165 1357
1166sub size_allocate { 1358sub size_allocate {
1167 my ($self, $x, $y, $w, $h) = @_; 1359 my ($self, $w, $h) = @_;
1168 1360
1169 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1361 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1170
1171 $self->_set_text ($self->{text});
1172} 1362}
1173 1363
1174sub set_text { 1364sub set_text {
1175 my ($self, $text) = @_; 1365 my ($self, $text) = @_;
1176 1366
1180} 1370}
1181 1371
1182sub key_down { 1372sub key_down {
1183 my ($self, $ev) = @_; 1373 my ($self, $ev) = @_;
1184 1374
1185 my $mod = $ev->key_mod; 1375 my $mod = $ev->{mod};
1186 my $sym = $ev->key_sym; 1376 my $sym = $ev->{sym};
1187
1188 my $uni = $ev->key_unicode; 1377 my $uni = $ev->{unicode};
1189 1378
1190 my $text = $self->get_text; 1379 my $text = $self->get_text;
1191 1380
1192 if ($sym == SDLK_BACKSPACE) { 1381 if ($sym == 8) {
1193 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1382 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1194 } elsif ($sym == SDLK_DELETE) { 1383 } elsif ($sym == 127) {
1195 substr $text, $self->{cursor}, 1, ""; 1384 substr $text, $self->{cursor}, 1, "";
1196 } elsif ($sym == SDLK_LEFT) { 1385 } elsif ($sym == CFClient::SDLK_LEFT) {
1197 --$self->{cursor} if $self->{cursor}; 1386 --$self->{cursor} if $self->{cursor};
1198 } elsif ($sym == SDLK_RIGHT) { 1387 } elsif ($sym == CFClient::SDLK_RIGHT) {
1199 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1388 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1200 } elsif ($sym == SDLK_HOME) { 1389 } elsif ($sym == CFClient::SDLK_HOME) {
1201 $self->{cursor} = 0; 1390 $self->{cursor} = 0;
1202 } elsif ($sym == SDLK_END) { 1391 } elsif ($sym == CFClient::SDLK_END) {
1203 $self->{cursor} = length $text; 1392 $self->{cursor} = length $text;
1204 } elsif ($sym == SDLK_ESCAPE) { 1393 } elsif ($sym == 27) {
1205 $self->emit ('escape'); 1394 $self->emit ('escape');
1206 } elsif ($uni) { 1395 } elsif ($uni) {
1207 substr $text, $self->{cursor}++, 0, chr $uni; 1396 substr $text, $self->{cursor}++, 0, chr $uni;
1208 } 1397 }
1209 1398
1284 1473
1285package CFClient::UI::Entry; 1474package CFClient::UI::Entry;
1286 1475
1287our @ISA = CFClient::UI::EntryBase::; 1476our @ISA = CFClient::UI::EntryBase::;
1288 1477
1289use SDL; 1478use CFClient::OpenGL;
1290use SDL::OpenGL;
1291 1479
1292sub key_down { 1480sub key_down {
1293 my ($self, $ev) = @_; 1481 my ($self, $ev) = @_;
1294 1482
1295 my $sym = $ev->key_sym; 1483 my $sym = $ev->{sym};
1296 1484
1297 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} = '';
1298 $self->emit (activate => $self->get_text); 1490 $self->emit (activate => $txt);
1299 $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 }
1300 1514
1301 } else { 1515 } else {
1302 $self->SUPER::key_down ($ev); 1516 $self->SUPER::key_down ($ev);
1303 } 1517 }
1304 1518
1308 1522
1309package CFClient::UI::Button; 1523package CFClient::UI::Button;
1310 1524
1311our @ISA = CFClient::UI::Label::; 1525our @ISA = CFClient::UI::Label::;
1312 1526
1313use SDL; 1527use CFClient::OpenGL;
1314use SDL::OpenGL;
1315 1528
1316my @tex = 1529my @tex =
1317 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1530 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1318 qw(b1_button_active.png); 1531 qw(b1_button_active.png);
1319 1532
1320sub new { 1533sub new {
1321 my $class = shift; 1534 my $class = shift;
1322 1535
1323 $class->SUPER::new ( 1536 $class->SUPER::new (
1324 padding => 4, 1537 padding => 4,
1325 fg => [1, 1, 1], 1538 fg => [1, 1, 1],
1326 bg => [1, 1, 1, 0.2], 1539 bg => [1, 1, 1, 0.2],
1327 active_fg => [0, 0, 1], 1540 active_fg => [0, 0, 1],
1328 can_hover => 1, 1541 can_hover => 1,
1542 align => 0,
1543 valign => 0,
1544 can_events => 1,
1329 @_ 1545 @_
1330 ) 1546 )
1331} 1547}
1332 1548
1333sub button_up { 1549sub button_up {
1367package CFClient::UI::CheckBox; 1583package CFClient::UI::CheckBox;
1368 1584
1369our @ISA = CFClient::UI::DrawBG::; 1585our @ISA = CFClient::UI::DrawBG::;
1370 1586
1371my @tex = 1587my @tex =
1372 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1588 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1373 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1589 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1374 1590
1375use SDL; 1591use CFClient::OpenGL;
1376use SDL::OpenGL;
1377 1592
1378sub new { 1593sub new {
1379 my $class = shift; 1594 my $class = shift;
1380 1595
1381 $class->SUPER::new ( 1596 $class->SUPER::new (
1392 my ($self) = @_; 1607 my ($self) = @_;
1393 1608
1394 ($self->{padding} * 2 + 6) x 2 1609 ($self->{padding} * 2 + 6) x 2
1395} 1610}
1396 1611
1397sub size_allocate {
1398 my ($self, $x, $y, $w, $h) = @_;
1399
1400 $self->_size_allocate ($x, $y, $w, $h) or return;
1401}
1402
1403sub button_down { 1612sub button_down {
1404 my ($self, $ev, $x, $y) = @_; 1613 my ($self, $ev, $x, $y) = @_;
1405 1614
1406 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1615 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1407 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1616 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1433 glDisable GL_BLEND; 1642 glDisable GL_BLEND;
1434} 1643}
1435 1644
1436############################################################################# 1645#############################################################################
1437 1646
1438package CFClient::UI::VGauge; 1647package CFClient::UI::Image;
1439 1648
1440our @ISA = CFClient::UI::Base::; 1649our @ISA = CFClient::UI::Base::;
1441 1650
1442use SDL::OpenGL; 1651use CFClient::OpenGL;
1652use Carp qw/confess/;
1443 1653
1444my %tex = ( 1654our %loaded_images;
1445 food => [
1446 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1447 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1448 ],
1449 grace => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1452 ],
1453 hp => [
1454 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1455 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1456 ],
1457 mana => [
1458 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1459 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1460 ],
1461);
1462 1655
1463# eg. VGauge->new (gauge => 'food'), default gauge: food
1464sub new { 1656sub new {
1465 my $class = shift; 1657 my $class = shift;
1466 1658
1467 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};
1468 1671
1469 $self 1672 $self
1470} 1673}
1471 1674
1472sub size_request { 1675sub size_request {
1473 my ($self) = @_; 1676 my ($self) = @_;
1474 1677
1475 ($self->{w}, $self->{h}) 1678 ($self->{tex}->{w}, $self->{tex}->{h})
1476}
1477
1478sub set_max {
1479 my ($self, $max) = @_;
1480 $self->{max_val} = $max;
1481}
1482
1483sub set_value {
1484 my ($self, $val, $max) = @_;
1485
1486 $self->set_max ($max)
1487 if defined $max;
1488
1489 $max = $self->{max_val};
1490 $self->{val} = $val;
1491
1492 $self->update;
1493} 1679}
1494 1680
1495sub _draw { 1681sub _draw {
1496 my ($self) = @_; 1682 my ($self) = @_;
1497 1683
1498 my $tex = $tex{$self->{gauge}}; 1684 my $tex = $self->{tex};
1499 1685
1500 my ($w, $h) = ($self->{w}, $self->{h}); 1686 my ($w, $h) = ($self->{w}, $self->{h});
1501 1687
1502 my $ycut = $self->{val} / ($self->{max_val} || 1); 1688 if ($self->{rot90}) {
1503 $ycut = 1 if $self->{val} > $self->{max_val}; 1689 glRotate 90, 0, 0, 1;
1690 glTranslate 0, -$self->{w}, 0;
1504 1691
1505 my $t1 = $tex->[0]; 1692 ($w, $h) = ($h, $w);
1506 my $t2 = $tex->[1]; 1693 }
1507 1694
1508 glEnable GL_BLEND; 1695 glEnable GL_BLEND;
1509 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1696 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1510 glEnable GL_TEXTURE_2D; 1697 glEnable GL_TEXTURE_2D;
1511 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1698 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1512 1699
1513 my $h1 = $self->{h} - $ycut * $self->{h}; 1700 $tex->draw_quad (0, 0, $w, $h);
1514 my $h2 = $ycut * $self->{h};
1515
1516 my $yp = 0;
1517
1518 glBindTexture (GL_TEXTURE_2D, $t1->{name});
1519 glBegin (GL_QUADS);
1520 glTexCoord (0, 0); glVertex (0 , $yp);
1521 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1);
1522 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1);
1523 glTexCoord (1, 0); glVertex (0 + $w, $yp);
1524 glEnd ();
1525
1526 $yp += $h1;
1527
1528 glBindTexture (GL_TEXTURE_2D, $t2->{name});
1529 glBegin (GL_QUADS);
1530 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp);
1531 glTexCoord (0, 1); glVertex (0 , $yp + $h2);
1532 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2);
1533 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp);
1534 glEnd ();
1535 1701
1536 glDisable GL_BLEND; 1702 glDisable GL_BLEND;
1537 glDisable GL_TEXTURE_2D; 1703 glDisable GL_TEXTURE_2D;
1538} 1704}
1539 1705
1540############################################################################# 1706#############################################################################
1541 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
1542package CFClient::UI::Slider; 1883package CFClient::UI::Slider;
1543 1884
1544use strict; 1885use strict;
1545 1886
1546use SDL::OpenGL; 1887use CFClient::OpenGL;
1547 1888
1548our @ISA = CFClient::UI::DrawBG::; 1889our @ISA = CFClient::UI::DrawBG::;
1549 1890
1550my @tex = 1891my @tex =
1551 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1892 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1561 # TODO: calculations are off 1902 # TODO: calculations are off
1562 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1563 fg => [1, 1, 1], 1904 fg => [1, 1, 1],
1564 active_fg => [0, 0, 0], 1905 active_fg => [0, 0, 0],
1565 range => [0, 0, 100, 10], 1906 range => [0, 0, 100, 10],
1566 req_w => 40, 1907 req_w => 20,
1567 req_h => 13, 1908 req_h => 20,
1568 vertical => 0, 1909 vertical => 0,
1569 can_hover => 1, 1910 can_hover => 1,
1570 inner_pad => 5, 1911 inner_pad => 5,
1571 @_ 1912 @_
1572 ); 1913 );
1676 2017
1677package CFClient::UI::TextView; 2018package CFClient::UI::TextView;
1678 2019
1679our @ISA = CFClient::UI::HBox::; 2020our @ISA = CFClient::UI::HBox::;
1680 2021
1681use SDL::OpenGL; 2022use CFClient::OpenGL;
1682 2023
1683sub new { 2024sub new {
1684 my $class = shift; 2025 my $class = shift;
1685 2026
1686 my $self = $class->SUPER::new ( 2027 my $self = $class->SUPER::new (
1687 req_w => $::WIDTH / 6, 2028 fontsize => 1,
1688 req_h => $::HEIGHT / 6, 2029 can_events => 0,
1689 fontsize => $::FONTSIZE, 2030 #font => default_font
1690 @_, 2031 @_,
1691 2032
1692 layout => (new CFClient::Layout), 2033 layout => (new CFClient::Layout),
1693 par => [], 2034 par => [],
1694 height => 0, 2035 height => 0,
1695 children => [ 2036 children => [
1696 (new CFClient::UI::Empty expand => 1), 2037 (new CFClient::UI::Empty expand => 1),
1697 (new CFClient::UI::Slider vertical => 1), 2038 (new CFClient::UI::Slider vertical => 1),
1698 ], 2039 ],
1699 ); 2040 );
1700 2041
1715sub text_height { 2056sub text_height {
1716 my ($self, $text) = @_; 2057 my ($self, $text) = @_;
1717 2058
1718 my $layout = $self->{layout}; 2059 my $layout = $self->{layout};
1719 2060
1720 $layout->set_height ($self->{fontsize}); 2061 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1721 $layout->set_width ($self->{w}); 2062 $layout->set_width ($self->{w});
1722 $layout->set_text ($text); 2063 $layout->set_text ($text);
1723 2064
1724 ($layout->size)[1] 2065 ($layout->size)[1]
1725} 2066}
1729 2070
1730 $self->{need_reflow}++; 2071 $self->{need_reflow}++;
1731 $self->update; 2072 $self->update;
1732} 2073}
1733 2074
1734sub size_request {
1735 my ($self) = @_;
1736
1737 ($self->{req_w}, $self->{req_h})
1738}
1739
1740sub size_allocate { 2075sub size_allocate {
1741 my ($self, $x, $y, $w, $h) = @_; 2076 my ($self, $w, $h) = @_;
1742 2077
1743 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 2078 $self->SUPER::size_allocate ($w, $h);
1744 2079
2080 $self->{layout}->set_font ($self->{font}) if $self->{font};
1745 $self->{layout}->set_height ($self->{fontsize}); 2081 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1746 $self->{layout}->set_width ($self->{w}); 2082 $self->{layout}->set_width ($self->{children}[0]{w});
1747 2083
1748 $self->reflow; 2084 $self->reflow;
1749 $self->update;
1750} 2085}
1751 2086
1752sub add_paragraph { 2087sub add_paragraph {
1753 my ($self, $color, $text) = @_; 2088 my ($self, $color, $text) = @_;
1754 2089
1803 2138
1804 my $y = 0; 2139 my $y = 0;
1805 2140
1806 my $layout = $self->{layout}; 2141 my $layout = $self->{layout};
1807 2142
2143 $layout->set_font ($self->{font}) if $self->{font};
2144
1808 for my $par (@{$self->{par}}) { 2145 for my $par (@{$self->{par}}) {
1809 my $h = $par->[0]; 2146 my $h = $par->[0];
1810 2147
1811 if ($y0 < $y + $h && $y < $y1) { 2148 if ($y0 < $y + $h && $y < $y1) {
1812 $layout->set_text ($par->[2]); 2149 $layout->set_text ($par->[2]);
1829 my ($self) = @_; 2166 my ($self) = @_;
1830 2167
1831 if ($self->{texture}) { 2168 if ($self->{texture}) {
1832 glEnable GL_TEXTURE_2D; 2169 glEnable GL_TEXTURE_2D;
1833 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;
1834 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2172 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1835 glDisable GL_TEXTURE_2D; 2173 glDisable GL_TEXTURE_2D;
1836 } 2174 }
1837 2175
1838 $self->{children}[1]->draw; 2176 $self->{children}[1]->draw;
1839 2177
1840} 2178}
1841 2179
1842############################################################################# 2180#############################################################################
1843 2181
1844package CFClient::UI::MapWidget;
1845
1846use strict;
1847
1848use List::Util qw(min max);
1849
1850use SDL;
1851use SDL::OpenGL;
1852
1853our @ISA = CFClient::UI::Base::;
1854
1855sub new {
1856 my $class = shift;
1857
1858 $class->SUPER::new (
1859 z => -1,
1860 can_focus => 1,
1861 list => (glGenLists 1),
1862 @_
1863 )
1864}
1865
1866sub key_down {
1867 print "MAPKEYDOWN\n";
1868}
1869
1870sub key_up {
1871}
1872
1873sub button_down {
1874 my ($self, $ev, $x, $y) = @_;
1875
1876 $self->focus_in;
1877
1878 if ($ev->button == 2) {
1879 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1880 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1881
1882 $self->{motion} = sub {
1883 my ($ev, $x, $y) = @_;
1884
1885 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1886
1887 $::CFG->{map_shift_x} = $bw + $x - $ox;
1888 $::CFG->{map_shift_y} = $bh + $y - $oy;
1889
1890 $self->update;
1891 };
1892 }
1893}
1894
1895sub button_up {
1896 my ($self, $ev, $x, $y) = @_;
1897
1898 delete $self->{motion};
1899}
1900
1901sub mouse_motion {
1902 my ($self, $ev, $x, $y) = @_;
1903
1904 $self->{motion}->($ev, $x, $y) if $self->{motion};
1905}
1906
1907sub size_request {
1908 (
1909 1 + 32 * int $::WIDTH / 32,
1910 1 + 32 * int $::HEIGHT / 32,
1911 )
1912}
1913
1914sub update {
1915 my ($self) = @_;
1916
1917 $self->{need_update} = 1;
1918 $self->SUPER::update;
1919}
1920
1921sub draw {
1922 my ($self) = @_;
1923
1924 if (delete $self->{need_update}) {
1925 glNewList $self->{list}, GL_COMPILE;
1926
1927 if ($::MAP) {
1928 my $sw = int $::WIDTH / 32;
1929 my $sh = int $::HEIGHT / 32;
1930
1931 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1932 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1933
1934 glTranslate $sx0 - 32, $sy0 - 32, 0;
1935
1936 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1937
1938 if ($::CFG->{fow_enable}) {
1939 if ($::CFG->{fow_smooth}) { # smooth fog of war
1940 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1941 glConvolutionFilter2D
1942 GL_CONVOLUTION_2D,
1943 GL_ALPHA,
1944 3, 3,
1945 GL_ALPHA, GL_FLOAT,
1946 pack "f*",
1947 0.1, 0.1, 0.1,
1948 0.1, 0.2, 0.1,
1949 0.1, 0.1, 0.1,
1950 ;
1951 glEnable GL_CONVOLUTION_2D;
1952 }
1953
1954 $self->{fow_texture} = new CFClient::Texture
1955 w => $w,
1956 h => $h,
1957 data => $data,
1958 internalformat => GL_ALPHA,
1959 format => GL_ALPHA;
1960
1961 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1962
1963 glEnable GL_BLEND;
1964 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1965 glEnable GL_TEXTURE_2D;
1966 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1967
1968 glColor +($::CFG->{fow_intensity}) x 3, 1;
1969 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1970
1971 glDisable GL_TEXTURE_2D;
1972 glDisable GL_BLEND;
1973 }
1974
1975 # HACK BEGIN
1976 {
1977 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1978 my ($w, $h) = (250, 250);
1979
1980 glEnable GL_BLEND;
1981 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1982 glEnable GL_TEXTURE_2D;
1983 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1984
1985 $self->{mapmap_texture} =
1986 new CFClient::Texture
1987 w => $w,
1988 h => $h,
1989 data => $::MAP->mapmap ($w, $h),
1990 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1991
1992 $self->{mapmap_texture}->draw_quad (100, 100);
1993
1994 glDisable GL_TEXTURE_2D;
1995 glDisable GL_BLEND;
1996 }
1997 # HACK END
1998 }
1999
2000 glEndList;
2001 }
2002
2003 glPushMatrix;
2004 glCallList $self->{list};
2005 glPopMatrix;
2006
2007 if ($FOCUS != $self) {
2008 glColor 64/255, 64/255, 64/255;
2009 glLogicOp GL_AND;
2010 glEnable GL_COLOR_LOGIC_OP;
2011 glBegin GL_QUADS;
2012 glVertex 0, 0;
2013 glVertex 0, $::HEIGHT;
2014 glVertex $::WIDTH, $::HEIGHT;
2015 glVertex $::WIDTH, 0;
2016 glEnd;
2017 glDisable GL_COLOR_LOGIC_OP;
2018 }
2019}
2020
2021my %DIR = (
2022 SDLK_KP8, [1, "north"],
2023 SDLK_KP9, [2, "northeast"],
2024 SDLK_KP6, [3, "east"],
2025 SDLK_KP3, [4, "southeast"],
2026 SDLK_KP2, [5, "south"],
2027 SDLK_KP1, [6, "southwest"],
2028 SDLK_KP4, [7, "west"],
2029 SDLK_KP7, [8, "northwest"],
2030
2031 SDLK_UP, [1, "north"],
2032 SDLK_RIGHT, [3, "east"],
2033 SDLK_DOWN, [5, "south"],
2034 SDLK_LEFT, [7, "west"],
2035);
2036
2037sub key_down {
2038 my ($self, $ev) = @_;
2039
2040 my $mod = $ev->key_mod;
2041 my $sym = $ev->key_sym;
2042
2043 if ($sym == SDLK_KP5) {
2044 $::CONN->user_send ("stay fire");
2045 } elsif ($sym == SDLK_a) {
2046 $::CONN->user_send ("apply");
2047 } elsif ($sym == SDLK_QUOTE) {
2048 $self->emit ('activate_console');
2049 } elsif ($sym == SDLK_SLASH) {
2050 $self->emit ('activate_console' => '/');
2051 } elsif (exists $DIR{$sym}) {
2052 if ($mod & KMOD_SHIFT) {
2053 $self->{shft}++;
2054 $::CONN->user_send ("fire $DIR{$sym}[0]");
2055 } elsif ($mod & KMOD_CTRL) {
2056 $self->{ctrl}++;
2057 $::CONN->user_send ("run $DIR{$sym}[0]");
2058 } else {
2059 $::CONN->user_send ("$DIR{$sym}[1]");
2060 }
2061 }
2062}
2063
2064sub key_up {
2065 my ($self, $ev) = @_;
2066
2067 my $mod = $ev->key_mod;
2068 my $sym = $ev->key_sym;
2069
2070 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2071 $::CONN->user_send ("fire_stop");
2072 }
2073 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2074 $::CONN->user_send ("run_stop");
2075 }
2076}
2077
2078#############################################################################
2079
2080package CFClient::UI::Animator; 2182package CFClient::UI::Animator;
2081 2183
2082use SDL::OpenGL; 2184use CFClient::OpenGL;
2083 2185
2084our @ISA = CFClient::UI::Bin::; 2186our @ISA = CFClient::UI::Bin::;
2085 2187
2086sub moveto { 2188sub moveto {
2087 my ($self, $x, $y) = @_; 2189 my ($self, $x, $y) = @_;
2125 2227
2126sub new { 2228sub new {
2127 my $class = shift; 2229 my $class = shift;
2128 2230
2129 my $self = $class->SUPER::new ( 2231 my $self = $class->SUPER::new (
2130 state => 0, 2232 state => 0,
2131 connect_activate => \&toggle_flopper, 2233 connect_activate => \&toggle_flopper,
2132 @_ 2234 @_
2133 ); 2235 );
2134 2236
2135 if ($self->{state}) { 2237 if ($self->{state}) {
2156 $self->emit (changed => $self->{state}); 2258 $self->emit (changed => $self->{state});
2157} 2259}
2158 2260
2159############################################################################# 2261#############################################################################
2160 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
2161package CFClient::UI::Root; 2372package CFClient::UI::Root;
2162 2373
2163our @ISA = CFClient::UI::Container::; 2374our @ISA = CFClient::UI::Container::;
2164 2375
2165use SDL::OpenGL; 2376use CFClient::OpenGL;
2166 2377
2167sub check_size { 2378sub check_size {
2168 my ($self) = @_; 2379 my ($self) = @_;
2169 2380
2170 $self->size_allocate (0, 0, $::WITH, $::HEIGHT); 2381 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2171} 2382}
2172 2383
2173sub size_request { 2384sub size_request {
2174 ($::WIDTH, $::HEIGHT) 2385 ($::WIDTH, $::HEIGHT)
2175} 2386}
2176 2387
2177sub size_allocate { 2388sub configure {
2178 my ($self, $x, $y, $w, $h) = @_; 2389 my ($self, $x, $y, $w, $h) = @_;
2179 2390
2180 $self->_size_allocate ($x, $y, $w, $h); 2391 $self->SUPER::configure ($x, $y, $w, $h);
2181 2392
2182 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2183 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 }
2184} 2400}
2185 2401
2186sub _topleft { 2402sub _topleft {
2187 my ($self, $x, $y) = @_; 2403 my ($self, $x, $y) = @_;
2188 2404
2195 $self->check_size; 2411 $self->check_size;
2196 ::refresh (); 2412 ::refresh ();
2197} 2413}
2198 2414
2199sub add { 2415sub add {
2200 my ($self, $widget) = @_; 2416 my ($self, $child) = @_;
2201 2417
2418 # integerize window positions
2419 $child->{x} = int $child->{x};
2420 $child->{y} = int $child->{y};
2421
2202 $self->SUPER::add ($widget); 2422 $self->SUPER::add ($child);
2203
2204 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2205} 2423}
2206 2424
2207sub on_refresh { 2425sub on_refresh {
2208 my ($self, $id, $cb) = @_; 2426 my ($self, $id, $cb) = @_;
2209 2427
2233############################################################################# 2451#############################################################################
2234 2452
2235package CFClient::UI; 2453package CFClient::UI;
2236 2454
2237$ROOT = new CFClient::UI::Root; 2455$ROOT = new CFClient::UI::Root;
2456$TOOLTIP = new CFClient::UI::Tooltip;
2238 2457
22391 24581
2240 2459

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines