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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines