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.112 by root, Sat Apr 15 12:29:46 2006 UTC vs.
Revision 1.154 by root, Sun Apr 23 02:20:57 2006 UTC

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23 $TOOLTIP->set_text ($widget->{tooltip});
24 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
25 $TOOLTIP->show;
26 }
27
28 return;
29 }
30 }
31 }
32
33 $TOOLTIP->hide;
34 delete $TOOLTIP->{owner};
35}
14 36
15# class methods for events 37# class methods for events
16sub feed_sdl_key_down_event { 38sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 39 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 40}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 43 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 44}
23 45
24sub feed_sdl_button_down_event { 46sub feed_sdl_button_down_event {
25 my ($ev) = @_; 47 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 48 my ($x, $y) = ($ev->{x}, $ev->{y});
27 49
28 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 51 my $widget = $ROOT->find_widget ($x, $y);
30 52
31 $GRAB = $widget; 53 $GRAB = $widget;
32 $GRAB->update if $GRAB; 54 $GRAB->update if $GRAB;
33 }
34 55
56 check_tooltip;
57 }
58
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 59 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 60
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 61 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 62}
39 63
40sub feed_sdl_button_up_event { 64sub feed_sdl_button_up_event {
41 my ($ev) = @_; 65 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 66 my ($x, $y) = ($ev->{x}, $ev->{y});
43 67
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 68 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 69
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 70 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 71
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 72 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 73
50 if (!$BUTTON_STATE) { 74 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 75 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 76 $grab->update if $grab;
53 $GRAB->update if $GRAB; 77 $GRAB->update if $GRAB;
78
79 check_tooltip;
54 } 80 }
55} 81}
56 82
57sub feed_sdl_motion_event { 83sub feed_sdl_motion_event {
58 my ($ev) = @_; 84 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 85 my ($x, $y) = ($ev->{x}, $ev->{y});
60 86
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 87 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 88
63 if ($widget != $HOVER) { 89 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 90 my $hover = $HOVER; $HOVER = $widget;
65 91
66 $hover->update if $hover && $hover->{can_hover}; 92 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 93 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 }
69 94
95 check_tooltip;
96 }
97
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 98 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 99}
72 100
73# convert position array to integers 101# convert position array to integers
74sub harmonize { 102sub harmonize {
75 my ($vals) = @_; 103 my ($vals) = @_;
76 104
77 my $rem = 0; 105 my $rem = 0;
78 106
79 for ($vals) { 107 for (@$vals) {
80 my $i = int $_ + $rem; 108 my $i = int $_ + $rem;
81 $rem += $_ - $i; 109 $rem += $_ - $i;
82 $_ = $i; 110 $_ = $i;
83 } 111 }
84} 112}
87 115
88package CFClient::UI::Base; 116package CFClient::UI::Base;
89 117
90use strict; 118use strict;
91 119
92use SDL::OpenGL; 120use CFClient::OpenGL;
93 121
94sub new { 122sub new {
95 my $class = shift; 123 my $class = shift;
96 124
97 my $self = bless { 125 my $self = bless {
98 x => 0, 126 x => 0,
99 y => 0, 127 y => 0,
100 z => 0, 128 z => 0,
101 w => -1, 129 can_events => 1,
102 h => -1,
103 @_ 130 @_
104 }, $class; 131 }, $class;
105 132
106 for (keys %$self) { 133 for (keys %$self) {
107 if (/^connect_(.*)$/) { 134 if (/^connect_(.*)$/) {
110 } 137 }
111 138
112 $self 139 $self
113} 140}
114 141
142sub show {
143 my ($self) = @_;
144
145 return if $self->{parent};
146
147 $CFClient::UI::ROOT->add ($self);
148}
149
150sub hide {
151 my ($self) = @_;
152
153 return unless $self->{parent};
154
155 $self->{parent}->remove ($self);
156}
157
115sub move { 158sub move {
116 my ($self, $x, $y, $z) = @_; 159 my ($self, $x, $y, $z) = @_;
160
117 $self->{x} = $x; 161 $self->{x} = int $x;
118 $self->{y} = $y; 162 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 163 $self->{z} = $z if defined $z;
164
165 $self->update;
120} 166}
121 167
122sub needs_redraw { 168sub needs_redraw {
123 0 169 0
124} 170}
125 171
126sub size_request { 172sub size_request {
127 require Carp; 173 require Carp;
128 Carp::confess "size_request is abtract"; 174 Carp::confess "size_request is abstract";
129} 175}
130 176
131sub _size_allocate { 177sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 178 my ($self, $x, $y, $w, $h) = @_;
133 179
180 if ($self->{aspect}) {
181 my $w2 = List::Util::min $w, int $h * $self->{aspect};
182 my $h2 = List::Util::min $h, int $w / $self->{aspect};
183
184 # use alignment to adjust x, y
185
186 $x += int +($w - $w2) * 0.5;
187 $y += int +($h - $h2) * 0.5;
188
189 ($w, $h) = ($w2, $h2);
190 }
191
192 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 193 $self->{x} = $x;
135 $self->{y} = $y; 194 $self->{y} = $y;
195 $self->update;
196 }
136 197
137 return unless $self->{w} != $w || $self->{h} != $h; 198 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 199 $self->{w} = $w;
140 $self->{h} = $h; 200 $self->{h} = $h;
141 201
202 $self->size_allocate ($w, $h);
203 $self->update;
142 1 204 }
143} 205}
144 206
145sub size_allocate { 207sub size_allocate {
208 # nothing to be done
209}
210
211# return top left coordinates
212sub _topleft {
146 my ($self, $x, $y, $w, $h) = @_; 213 my ($self, $x, $y) = @_;
147 214
148 $self->_size_allocate ($x, $y, $w, $h); 215 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
149} 216}
150 217
151# translate global coordinates to local coordinate system 218# translate global coordinates to local coordinate system
152sub translate { 219sub coord2local {
153 my ($self, $x, $y) = @_; 220 my ($self, $x, $y) = @_;
154 221
155 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 222 my ($X, $Y) = $self->_topleft;
223 ($x - $X, $y - $Y)
224}
225
226# translate local coordinates to global coordinate system
227sub coord2global {
228 my ($self, $x, $y) = @_;
229
230 my ($X, $Y) = $self->_topleft;
231 ($x + $X, $y + $Y)
156} 232}
157 233
158sub focus_in { 234sub focus_in {
159 my ($self) = @_; 235 my ($self) = @_;
160 236
161 return if $FOCUS == $self; 237 return if $FOCUS == $self;
162 return unless $self->{can_focus}; 238 return unless $self->{can_focus};
163 239
164 my $focus = $FOCUS; $FOCUS = $self; 240 my $focus = $FOCUS; $FOCUS = $self;
241
242 $self->emit (focus_in => $focus);
243
165 $focus->update if $focus; 244 $focus->update if $focus;
166 $FOCUS->update; 245 $FOCUS->update;
167} 246}
168 247
169sub focus_out { 248sub focus_out {
170 my ($self) = @_; 249 my ($self) = @_;
171 250
172 return unless $FOCUS == $self; 251 return unless $FOCUS == $self;
173 252
174 my $focus = $FOCUS; undef $FOCUS; 253 my $focus = $FOCUS; undef $FOCUS;
254
255 $self->emit (focus_out => $focus);
256
175 $focus->update if $focus; #? 257 $focus->update if $focus; #?
176} 258}
177 259
178sub mouse_motion { } 260sub mouse_motion { }
179sub button_up { } 261sub button_up { }
203 glPopMatrix; 285 glPopMatrix;
204 286
205 if ($self == $HOVER && $self->{can_hover}) { 287 if ($self == $HOVER && $self->{can_hover}) {
206 my ($x, $y) = @$self{qw(x y)}; 288 my ($x, $y) = @$self{qw(x y)};
207 289
208 glColor 0, 0, 1, 0.2; 290 glColor 1, 0.8, 0.5, 0.2;
209 glEnable GL_BLEND; 291 glEnable GL_BLEND;
210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 292 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
211 glBegin GL_QUADS; 293 glBegin GL_QUADS;
212 glVertex $x , $y; 294 glVertex $x , $y;
213 glVertex $x + $self->{w}, $y; 295 glVertex $x + $self->{w}, $y;
225} 307}
226 308
227sub find_widget { 309sub find_widget {
228 my ($self, $x, $y) = @_; 310 my ($self, $x, $y) = @_;
229 311
312 return () unless $self->{can_events};
313
230 return $self 314 return $self
231 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 315 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
232 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 316 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
233 317
234 () 318 ()
235} 319}
236 320
237sub set_parent { 321sub set_parent {
238 my ($self, $par) = @_; 322 my ($self, $parent) = @_;
239 323
240 $self->{parent} = $par;
241 Scalar::Util::weaken $self->{parent}; 324 Scalar::Util::weaken ($self->{parent} = $parent);
242} 325}
243 326
244sub get_parent { 327sub check_size {
245 $_[0]->{parent} 328 my ($self) = @_;
329
330 return unless $self->{parent};
331
332 my ($w, $h) = $self->size_request;
333
334 if ($w != $self->{req_w} || $h != $self->{req_h}) {
335 $self->{req_w} = $w;
336 $self->{req_h} = $h;
337
338 $self->{parent}->check_size;
339 }
246} 340}
247 341
248sub update { 342sub update {
249 my ($self) = @_; 343 my ($self) = @_;
250 344
253} 347}
254 348
255sub connect { 349sub connect {
256 my ($self, $signal, $cb) = @_; 350 my ($self, $signal, $cb) = @_;
257 351
258 push @{ $self->{cb}{$signal} }, $cb; 352 push @{ $self->{signal_cb}{$signal} }, $cb;
259} 353}
260 354
261sub emit { 355sub emit {
262 my ($self, $signal, @args) = @_; 356 my ($self, $signal, @args) = @_;
263 357
358 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
264 $_->($self, @args) 359 $cb->($self, @args);
265 for @{$self->{cb}{$signal} || []}; 360 }
266} 361}
267 362
268sub DESTROY { 363sub DESTROY {
269 my ($self) = @_; 364 my ($self) = @_;
270 365
276package CFClient::UI::DrawBG; 371package CFClient::UI::DrawBG;
277 372
278our @ISA = CFClient::UI::Base::; 373our @ISA = CFClient::UI::Base::;
279 374
280use strict; 375use strict;
281use SDL::OpenGL; 376use CFClient::OpenGL;
282 377
283sub new { 378sub new {
284 my $class = shift; 379 my $class = shift;
285 380
286 # range [value, low, high, page] 381 # range [value, low, high, page]
315 410
316package CFClient::UI::Empty; 411package CFClient::UI::Empty;
317 412
318our @ISA = CFClient::UI::Base::; 413our @ISA = CFClient::UI::Base::;
319 414
415sub new {
416 my ($class, %arg) = @_;
417 $class->SUPER::new (can_events => 0, %arg);
418}
419
320sub size_request { 420sub size_request {
321 (0, 0) 421 (0, 0)
322} 422}
323 423
324sub draw { } 424sub draw { }
332sub new { 432sub new {
333 my ($class, %arg) = @_; 433 my ($class, %arg) = @_;
334 434
335 my $children = delete $arg{children} || []; 435 my $children = delete $arg{children} || [];
336 436
337 my $self = $class->SUPER::new (children => [], %arg); 437 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
338 $self->add ($_) for @$children; 438 $self->add ($_) for @$children;
339 439
340 $self 440 $self
341} 441}
342 442
343sub add { 443sub add {
344 my ($self, $chld) = @_; 444 my ($self, $child) = @_;
345 445
346 $chld->set_parent ($self); 446 $child->set_parent ($self);
447
448 use sort 'stable';
347 449
348 $self->{children} = [ 450 $self->{children} = [
349 sort { $a->{z} <=> $b->{z} } 451 sort { $a->{z} <=> $b->{z} }
350 @{$self->{children}}, $chld 452 @{$self->{children}}, $child
351 ]; 453 ];
352 454
353 $self->{w} = $self->{h} = -1; 455 $child->check_size;
456}
457
458sub remove {
459 my ($self, $child) = @_;
460
461 delete $child->{parent};
462
463 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
464
465 $self->check_size;
354 $self->update; 466 $self->update;
355}
356
357sub remove {
358 my ($self, $widget) = @_;
359
360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
361
362 $self->size_allocate (0, 0, $self->{w}, $self->{h});
363} 467}
364 468
365sub find_widget { 469sub find_widget {
366 my ($self, $x, $y) = @_; 470 my ($self, $x, $y) = @_;
367 471
397 501
398 $class->SUPER::new (children => [$child], %arg) 502 $class->SUPER::new (children => [$child], %arg)
399} 503}
400 504
401sub add { 505sub add {
402 my ($self, $widget) = @_; 506 my ($self, $child) = @_;
403 507
404 $self->{children} = []; 508 $self->{children} = [];
405 509
406 $self->SUPER::add ($widget); 510 $self->SUPER::add ($child);
407} 511}
408 512
409sub remove { 513sub remove {
410 my ($self, $widget) = @_; 514 my ($self, $widget) = @_;
411 515
420sub size_request { 524sub size_request {
421 $_[0]{children}[0]->size_request 525 $_[0]{children}[0]->size_request
422} 526}
423 527
424sub size_allocate { 528sub size_allocate {
425 my ($self, $x, $y, $w, $h) = @_; 529 my ($self, $w, $h) = @_;
426 530
427 $self->_size_allocate ($x, $y, $w, $h) or return;
428
429 $self->{children}[0]->size_allocate (0, 0, $w, $h); 531 $self->{children}[0]->configure (0, 0, $w, $h);
430} 532}
431 533
432############################################################################# 534#############################################################################
433 535
434package CFClient::UI::Window; 536package CFClient::UI::Window;
435 537
436our @ISA = CFClient::UI::Bin::; 538our @ISA = CFClient::UI::Bin::;
437 539
438use SDL::OpenGL; 540use CFClient::OpenGL;
439 541
440sub new { 542sub new {
441 my ($class, %arg) = @_; 543 my ($class, %arg) = @_;
442 544
443 my $self = $class->SUPER::new (%arg); 545 my $self = $class->SUPER::new (%arg);
460 $self->child->draw; 562 $self->child->draw;
461 }; 563 };
462} 564}
463 565
464sub size_allocate { 566sub size_allocate {
465 my ($self, $x, $y, $w, $h) = @_; 567 my ($self, $w, $h) = @_;
466 568
467 $self->_size_allocate ($x, $y, $w, $h) or return;
468
469 $self->child->size_allocate (0, 0, $w, $h); 569 $self->child->configure (0, 0, $w, $h);
470 570
471 $self->render_chld; 571 $self->render_chld;
472} 572}
473 573
474sub _draw { 574sub _draw {
490 glDisable GL_TEXTURE_2D; 590 glDisable GL_TEXTURE_2D;
491} 591}
492 592
493############################################################################# 593#############################################################################
494 594
595package CFClient::UI::ViewPort;
596
597our @ISA = CFClient::UI::Window::;
598
599sub new { die }
600
601sub size_request {
602 my ($self) = @_;
603
604 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
605 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
606
607 @$self{qw(child_w child_h)}
608}
609
610sub _draw {
611 my ($self) = @_;
612
613 $self->{children}[1]->draw;
614}
615
616
617#############################################################################
618
495package CFClient::UI::Frame; 619package CFClient::UI::Frame;
496 620
497our @ISA = CFClient::UI::Bin::; 621our @ISA = CFClient::UI::Bin::;
498 622
499use SDL::OpenGL; 623use CFClient::OpenGL;
500
501sub size_request {
502 my ($self) = @_;
503 my $chld = $self->child
504 or return (0, 0);
505
506 $chld->move (2, 2);
507
508 map { $_ + 4 } $chld->size_request;
509}
510
511sub size_allocate {
512 my ($self, $x, $y, $w, $h) = @_;
513
514 $self->_size_allocate ($x, $y, $w, $h) or return;
515
516 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
517}
518
519sub _draw {
520 my ($self) = @_;
521
522 my $chld = $self->child;
523
524 my ($w, $h) = $chld->size_request;
525
526 glBegin GL_QUADS;
527 glColor 0, 0, 0;
528 glVertex 0 , 0;
529 glVertex 0 , $h + 4;
530 glVertex $w + 4 , $h + 4;
531 glVertex $w + 4 , 0;
532 glEnd;
533
534 $chld->draw;
535}
536
537#############################################################################
538
539package CFClient::UI::FancyFrame;
540
541our @ISA = CFClient::UI::Bin::;
542
543use SDL::OpenGL;
544
545my @tex =
546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
547 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
548 624
549sub new { 625sub new {
550 my $class = shift; 626 my $class = shift;
551 627
552 # TODO: user_x, user_y, overwrite moveto?
553
554 $class->SUPER::new ( 628 my $self = $class->SUPER::new (
555 bg => [1, 1, 1, 1], 629 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 630 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 631 border => 0.8,
558 @_ 632 @_
633 );
634
635 $self
636}
637
638sub set_size {
639 my ($self, $w, $h) = @_;
640 $self->{req_w} = $w;
641 $self->{req_h} = $h;
642 $self->check_size;
643}
644
645sub size_request {
646 my ($self) = @_;
647 ($self->{req_w}, $self->{req_h})
648}
649
650sub size_allocate {
651 my ($self, $w, $h) = @_;
652 $self->{w} = $w;
653 $self->{h} = $h;
654 $self->child->configure (0, 0, $w, $h);
655}
656
657sub _draw {
658 my ($self) = @_;
659
660 my ($w, $h) = ($self->{w}, $self->{h});
661
662 glEnable GL_BLEND;
663 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
664 glEnable GL_TEXTURE_2D;
665 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
666
667# glBegin GL_QUADS;
668# glColor 0, 0, 0, 0;
669# glVertex 0 , 0;
670# glVertex 0 , $h;
671# glVertex $w, $h;
672# glVertex $w, 0;
673# glEnd;
674
675
676 $self->child->draw;
677 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D;
679}
680
681#############################################################################
682
683package CFClient::UI::FancyFrame;
684
685our @ISA = CFClient::UI::Bin::;
686
687use CFClient::OpenGL;
688
689my @tex =
690 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
691 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
692
693sub new {
694 my $class = shift;
695
696 # TODO: user_x, user_y, overwrite moveto?
697
698 my $self = $class->SUPER::new (
699 bg => [1, 1, 1, 1],
700 border_bg => [1, 1, 1, 1],
701 border => 0.8,
702 can_events => 1,
703 @_
704 );
705
706 $self->{title} &&= new CFClient::UI::Label
707 align => 0,
708 valign => 1,
709 text => $self->{title},
710 fontsize => 1;
711
712 $self
713}
714
715sub border {
716 int $_[0]{border} * $::FONTSIZE
717}
718
719sub size_request {
720 my ($self) = @_;
721
722 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
723
724 my ($w, $h) = $self->SUPER::size_request;
725
726 (
727 $w + $self->border * 2,
728 $h + $self->border * 2,
559 ) 729 )
560} 730}
561 731
562sub size_request {
563 my ($self) = @_;
564
565 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
566
567 my ($w, $h) = $self->SUPER::size_request;
568
569 (
570 $w + $self->{border} * 2,
571 $h + $self->{border} * 2,
572 )
573}
574
575sub size_allocate { 732sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_; 733 my ($self, $w, $h) = @_;
577 734
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $h -= List::Util::max 0, $self->{border} * 2; 735 $h -= List::Util::max 0, $self->border * 2;
581 $w -= List::Util::max 0, $self->{border} * 2; 736 $w -= List::Util::max 0, $self->border * 2;
582 737
738 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
739 if $self->{title};
740
583 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 741 $self->child->configure ($self->border, $self->border, $w, $h);
584} 742}
585 743
586sub button_down { 744sub button_down {
587 my ($self, $ev, $x, $y) = @_; 745 my ($self, $ev, $x, $y) = @_;
588 746
747 my $border = $self->border;
748
589 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 749 if ($x < $self->{w} && $x >= $self->{w} - $border
590 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 750 && $y < $self->{h} && $y >= $self->{h} - $border) {
591 751
592 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 752 my ($ox, $oy) = ($ev->{x}, $ev->{y});
593 my ($bw, $bh) = ($self->{w}, $self->{h}); 753 my ($bw, $bh) = ($self->{w}, $self->{h});
594 754
595 $self->{motion} = sub { 755 $self->{motion} = sub {
596 my ($ev, $x, $y) = @_; 756 my ($ev, $x, $y) = @_;
597 757
598 ($x, $y) = ($ev->motion_x, $ev->motion_y); 758 ($x, $y) = ($ev->{x}, $ev->{y});
599 759
600 $self->{user_w} = $bw + $x - $ox; 760 $self->{user_w} = $bw + $x - $ox;
601 $self->{user_h} = $bh + $y - $oy; 761 $self->{user_h} = $bh + $y - $oy;
602 $self->update; 762 $self->check_size;
603 }; 763 };
604 764
605 } elsif ($x >= 0 && $x < $self->{w} 765 } elsif ($x >= 0 && $x < $self->{w}
606 && $y >= 0 && $y < $self->{border}) { 766 && $y >= 0 && $y < $border) {
607 767
608 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 768 my ($ox, $oy) = ($ev->{x}, $ev->{y});
609 my ($bx, $by) = ($self->{x}, $self->{y}); 769 my ($bx, $by) = ($self->{x}, $self->{y});
610 770
611 $self->{motion} = sub { 771 $self->{motion} = sub {
612 my ($ev, $x, $y) = @_; 772 my ($ev, $x, $y) = @_;
613 773
614 ($x, $y) = ($ev->motion_x, $ev->motion_y); 774 ($x, $y) = ($ev->{x}, $ev->{y});
615 775
616 $self->move ($bx + $x - $ox, $by + $y - $oy); 776 $self->move ($bx + $x - $ox, $by + $y - $oy);
617 $self->update; 777 $self->update;
618 }; 778 };
619 } 779 }
640 glEnable GL_BLEND; 800 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 801 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D; 802 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 803 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
644 804
805 my $border = $self->border;
806
645 glColor @{ $self->{border_bg} }; 807 glColor @{ $self->{border_bg} };
646 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 808 $tex[1]->draw_quad (0, 0, $w, $border);
647 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 809 $tex[3]->draw_quad (0, $border, $border, $ch);
648 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 810 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
649 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 811 $tex[4]->draw_quad (0, $h - $border, $w, $border);
650 812
651 my $bg = $tex[0]; 813 my $bg = $tex[0];
652 814
653 # TODO: repeat texture not scale 815 # TODO: repeat texture not scale
654 my $rep_x = $cw / $bg->{w}; 816 my $rep_x = $cw / $bg->{w};
657 glColor @{ $self->{bg} }; 819 glColor @{ $self->{bg} };
658 820
659 $bg->{s} = $rep_x; 821 $bg->{s} = $rep_x;
660 $bg->{t} = $rep_y; 822 $bg->{t} = $rep_y;
661 $bg->{wrap_mode} = 1; 823 $bg->{wrap_mode} = 1;
662 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 824 $bg->draw_quad ($border, $border, $cw, $ch);
663 825
664 glDisable GL_TEXTURE_2D; 826 glDisable GL_TEXTURE_2D;
665 glDisable GL_BLEND; 827 glDisable GL_BLEND;
666 828
829 $self->{title}->draw if $self->{title};
667 $self->child->draw; 830 $self->child->draw;
668} 831}
669 832
670############################################################################# 833#############################################################################
671 834
673 836
674our @ISA = CFClient::UI::Base::; 837our @ISA = CFClient::UI::Base::;
675 838
676use List::Util qw(max sum); 839use List::Util qw(max sum);
677 840
678use SDL::OpenGL; 841use CFClient::OpenGL;
679 842
680sub new { 843sub new {
681 my $class = shift; 844 my $class = shift;
682 845
683 $class->SUPER::new ( 846 $class->SUPER::new (
685 @_ 848 @_
686 ) 849 )
687} 850}
688 851
689sub add { 852sub add {
690 my ($self, $x, $y, $chld) = @_; 853 my ($self, $x, $y, $child) = @_;
691 854
855 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 856 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 857
695 $self->{w} = $self->{h} = -1; 858 $child->check_size;
859}
860
861# TODO: move to container class maybe? send childs a signal on removal?
862sub clear {
863 my ($self) = @_;
864
865 delete $self->{children};
696 $self->update; 866 $self->update;
697} 867}
698 868
699sub get_wh { 869sub get_wh {
700 my ($self) = @_; 870 my ($self) = @_;
706 or next; 876 or next;
707 877
708 for my $x (0 .. $#$row) { 878 for my $x (0 .. $#$row) {
709 my $widget = $row->[$x] 879 my $widget = $row->[$x]
710 or next; 880 or next;
711 my ($w, $h) = $widget->size_request; 881 my ($w, $h) = @$widget{qw(req_w req_h)};
712 882
713 $w[$x] = max $w[$x], $w; 883 $w[$x] = max $w[$x], $w;
714 $h[$y] = max $h[$y], $h; 884 $h[$y] = max $h[$y], $h;
715 } 885 }
716 } 886 }
728 (sum @$hs), 898 (sum @$hs),
729 ) 899 )
730} 900}
731 901
732sub size_allocate { 902sub size_allocate {
733 my ($self, $x, $y, $w, $h) = @_; 903 my ($self, $w, $h) = @_;
734
735 $self->_size_allocate ($x, $y, $w, $h) or return;
736 904
737 my ($ws, $hs) = $self->get_wh; 905 my ($ws, $hs) = $self->get_wh;
738 906
739 my $req_w = sum @$ws; 907 my $req_w = sum @$ws;
740 my $req_h = sum @$hs; 908 my $req_h = sum @$hs;
762 930
763 for my $c (0 .. $#$row) { 931 for my $c (0 .. $#$row) {
764 my $col_w = $ws->[$c]; 932 my $col_w = $ws->[$c];
765 933
766 if (my $widget = $row->[$c]) { 934 if (my $widget = $row->[$c]) {
767 $widget->size_allocate ($x, $y, $col_w, $row_h); 935 $widget->configure ($x, $y, $col_w, $row_h);
768 } 936 }
769 937
770 $x += $col_w; 938 $x += $col_w;
771 } 939 }
772 940
817 (List::Util::max map $_->[1], @alloc), 985 (List::Util::max map $_->[1], @alloc),
818 ) 986 )
819} 987}
820 988
821sub size_allocate { 989sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_; 990 my ($self, $w, $h) = @_;
823
824 $self->_size_allocate ($x, $y, $w, $h);
825 991
826 ($h, $w) = ($w, $h); 992 ($h, $w) = ($w, $h);
827 993
828 my $children = $self->{children}; 994 my $children = $self->{children};
829 995
830 my @h = map +($_->size_request)[0], @$children; 996 my @h = map $_->{req_w}, @$children;
831 997
832 my $req_h = List::Util::sum @h; 998 my $req_h = List::Util::sum @h;
833 999
834 if ($req_h > $h) { 1000 if ($req_h > $h) {
835 # ah well, not enough space 1001 # ah well, not enough space
851 1017
852 my $y = 0; 1018 my $y = 0;
853 for (0 .. $#$children) { 1019 for (0 .. $#$children) {
854 my $child = $children->[$_]; 1020 my $child = $children->[$_];
855 my $h = $h[$_]; 1021 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w); 1022 $child->configure ($y, 0, $h, $w);
857 1023
858 $y += $h; 1024 $y += $h;
859 } 1025 }
1026
1027 1
860} 1028}
861 1029
862############################################################################# 1030#############################################################################
863 1031
864package CFClient::UI::VBox; 1032package CFClient::UI::VBox;
877 (List::Util::sum map $_->[1], @alloc), 1045 (List::Util::sum map $_->[1], @alloc),
878 ) 1046 )
879} 1047}
880 1048
881sub size_allocate { 1049sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_; 1050 my ($self, $w, $h) = @_;
883
884 $self->_size_allocate ($x, $y, $w, $h);
885 1051
886 my $children = $self->{children}; 1052 my $children = $self->{children};
887 1053
888 my @h = map +($_->size_request)[1], @$children; 1054 my @h = map $_->{req_h}, @$children;
889 1055
890 my $req_h = List::Util::sum @h; 1056 my $req_h = List::Util::sum @h;
891 1057
892 if ($req_h > $h) { 1058 if ($req_h > $h) {
893 # ah well, not enough space 1059 # ah well, not enough space
907 1073
908 my $y = 0; 1074 my $y = 0;
909 for (0 .. $#$children) { 1075 for (0 .. $#$children) {
910 my $child = $children->[$_]; 1076 my $child = $children->[$_];
911 my $h = $h[$_]; 1077 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h); 1078 $child->configure (0, $y, $w, $h);
913 1079
914 $y += $h; 1080 $y += $h;
915 } 1081 }
1082
1083 1
916} 1084}
917 1085
918############################################################################# 1086#############################################################################
919 1087
920package CFClient::UI::Label; 1088package CFClient::UI::Label;
921 1089
922our @ISA = CFClient::UI::Base::; 1090our @ISA = CFClient::UI::Base::;
923 1091
924use SDL::OpenGL; 1092use CFClient::OpenGL;
925 1093
926sub new { 1094sub new {
927 my ($class, %arg) = @_; 1095 my ($class, %arg) = @_;
928 1096
929 my $self = $class->SUPER::new ( 1097 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 1098 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE, 1099 fontsize => 1,
932 text => "", 1100 text => "",
933 align => -1, 1101 align => -1,
1102 valign => -1,
934 padding => 2, 1103 padding => 2,
935 layout => new CFClient::Layout, 1104 layout => new CFClient::Layout,
1105 can_events => 0,
936 %arg 1106 %arg
937 ); 1107 );
938 1108
939 $self->set_text ($self->{text}); 1109 if (exists $self->{template}) {
1110 my $layout = new CFClient::Layout;
1111 $layout->set_text (delete $self->{template});
1112 $self->{template} = $layout;
1113 }
1114
1115 $self->set_text (delete $self->{text}) if exists $self->{text};
1116 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
940 1117
941 $self 1118 $self
942} 1119}
943 1120
944sub escape_text { 1121sub escape_text {
952} 1129}
953 1130
954sub set_text { 1131sub set_text {
955 my ($self, $text) = @_; 1132 my ($self, $text) = @_;
956 1133
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text); 1134 $self->{layout}->set_text ($text);
959 1135
960 delete $self->{texture}; 1136 delete $self->{texture};
961# $self->{w} = $self->{h} = -1; 1137 $self->check_size;
962 $self->update; 1138 $self->update;
963} 1139}
964 1140
965sub get_text { 1141sub set_markup {
966 my ($self, $text) = @_; 1142 my ($self, $markup) = @_;
967 1143
968 $self->{text} 1144 $self->{layout}->set_markup ($markup);
1145
1146 delete $self->{texture};
1147 $self->check_size;
1148 $self->update;
969} 1149}
970 1150
971sub size_request { 1151sub size_request {
972 my ($self) = @_; 1152 my ($self) = @_;
973 1153
974 $self->{layout}->set_width; 1154 $self->{layout}->set_width;
975 $self->{layout}->set_height ($self->{fontsize}); 1155 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1156
976 my ($w, $h) = $self->{layout}->size; 1157 my ($w, $h) = $self->{layout}->size;
1158
1159 if (exists $self->{template}) {
1160 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1161
1162 my ($w2, $h2) = $self->{template}->size;
1163
1164 $w = List::Util::max $w, $w2;
1165 $h = List::Util::max $h, $h2;
1166 }
977 1167
978 ( 1168 (
979 $w + $self->{padding} * 2, 1169 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2, 1170 $h + $self->{padding} * 2,
981 ) 1171 )
982} 1172}
983 1173
984sub size_allocate { 1174sub size_allocate {
985 my ($self, $x, $y, $w, $h) = @_; 1175 my ($self, $w, $h) = @_;
986
987 $self->_size_allocate ($x, $y, $w, $h) or return;
988 1176
989 delete $self->{texture}; 1177 delete $self->{texture};
990} 1178}
991 1179
992sub update { 1180sub set_fontsize {
993 my ($self) = @_; 1181 my ($self, $fontsize) = @_;
994 1182
1183 $self->{fontsize} = $fontsize;
995 delete $self->{texture}; 1184 delete $self->{texture};
1185 $self->check_size;
996 $self->SUPER::update; 1186 $self->update;
997} 1187}
998 1188
999sub _draw { 1189sub _draw {
1000 my ($self) = @_; 1190 my ($self) = @_;
1001 1191
1002 my $tex = $self->{texture} ||= do { 1192 my $tex = $self->{texture} ||= do {
1003 $self->{layout}->set_width ($self->{w}); 1193 $self->{layout}->set_width ($self->{w});
1004 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1194 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1005 new_from_layout CFClient::Texture $self->{layout} 1195 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1196 };
1007 1197
1008 glEnable GL_BLEND; 1198 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1199 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1200 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1201 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1202
1013 glColor @{$self->{fg}}; 1203 glColor @{$self->{fg}};
1014 1204
1015 my $x = 1205 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1206 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1207 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1208 : ($self->{w} - $tex->{w}) * 0.5
1209 );
1019 1210
1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1211 $self->{oy} = int (
1212 $self->{valign} < 0 ? $self->{padding}
1213 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1214 : ($self->{h} - $tex->{h}) * 0.5
1215 );
1216
1217 $tex->draw_quad ($self->{ox}, $self->{oy});
1021 1218
1022 glDisable GL_TEXTURE_2D; 1219 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1220 glDisable GL_BLEND;
1024} 1221}
1025 1222
1026############################################################################# 1223#############################################################################
1027 1224
1028package CFClient::UI::Entry; 1225package CFClient::UI::EntryBase;
1029 1226
1030our @ISA = CFClient::UI::Label::; 1227our @ISA = CFClient::UI::Label::;
1031 1228
1032use SDL; 1229use CFClient::OpenGL;
1033use SDL::OpenGL;
1034 1230
1035sub new { 1231sub new {
1036 my $class = shift; 1232 my $class = shift;
1037 1233
1038 $class->SUPER::new ( 1234 $class->SUPER::new (
1040 bg => [0, 0, 0, 0.2], 1236 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1237 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1238 active_fg => [0, 0, 0],
1043 can_hover => 1, 1239 can_hover => 1,
1044 can_focus => 1, 1240 can_focus => 1,
1241 valign => 0,
1242 can_events => 1,
1045 @_ 1243 @_
1046 ) 1244 )
1047} 1245}
1048 1246
1049sub _set_text { 1247sub _set_text {
1050 my ($self, $text) = @_; 1248 my ($self, $text) = @_;
1051 1249
1052 my $old_text = $self->{text}; 1250 delete $self->{cur_h};
1251
1252 return if $self->{text} eq $text;
1253
1254 delete $self->{texture};
1053 1255
1054 $self->{last_activity} = $::NOW; 1256 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1257 $self->{text} = $text;
1057 $self->{layout}->set_width ($self->{w});
1058 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1059 1258
1060 $text =~ s/./*/g if $self->{hidden}; 1259 $text =~ s/./*/g if $self->{hidden};
1260 $self->{layout}->set_text ("$text ");
1061 1261
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1262 $self->emit (changed => $self->{text});
1263}
1063 1264
1064 $text = substr $text, 0, $self->{cursor}; 1265sub get_text {
1065 utf8::encode $text; 1266 $_[0]{text}
1066
1067 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1068
1069 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1070 if $old_text ne $self->{text};
1071} 1267}
1072 1268
1073sub size_request { 1269sub size_request {
1074 my ($self) = @_; 1270 my ($self) = @_;
1075 1271
1077 1273
1078 ($w + 1, $h) # add 1 for cursor 1274 ($w + 1, $h) # add 1 for cursor
1079} 1275}
1080 1276
1081sub size_allocate { 1277sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1278 my ($self, $w, $h) = @_;
1083 1279
1084 $self->SUPER::size_allocate ($x, $y, $w, $h); 1280 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1085
1086 $self->_set_text ($self->{text});
1087} 1281}
1088 1282
1089sub set_text { 1283sub set_text {
1090 my ($self, $text) = @_; 1284 my ($self, $text) = @_;
1091 1285
1095} 1289}
1096 1290
1097sub key_down { 1291sub key_down {
1098 my ($self, $ev) = @_; 1292 my ($self, $ev) = @_;
1099 1293
1100 my $mod = $ev->key_mod; 1294 my $mod = $ev->{mod};
1101 my $sym = $ev->key_sym; 1295 my $sym = $ev->{sym};
1102
1103 my $uni = $ev->key_unicode; 1296 my $uni = $ev->{unicode};
1104 1297
1105 my $text = $self->get_text; 1298 my $text = $self->get_text;
1106 1299
1107 if ($sym == SDLK_BACKSPACE) { 1300 if ($sym == 8) {
1108 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1301 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1109 } elsif ($sym == SDLK_DELETE) { 1302 } elsif ($sym == 127) {
1110 substr $text, $self->{cursor}, 1, ""; 1303 substr $text, $self->{cursor}, 1, "";
1111 } elsif ($sym == SDLK_LEFT) { 1304 } elsif ($sym == CFClient::SDLK_LEFT) {
1112 --$self->{cursor} if $self->{cursor}; 1305 --$self->{cursor} if $self->{cursor};
1113 } elsif ($sym == SDLK_RIGHT) { 1306 } elsif ($sym == CFClient::SDLK_RIGHT) {
1114 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1307 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) { 1308 } elsif ($sym == CFClient::SDLK_HOME) {
1116 $self->{cursor} = 0; 1309 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) { 1310 } elsif ($sym == CFClient::SDLK_END) {
1118 $self->{cursor} = length $text; 1311 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) { 1312 } elsif ($sym == 27) {
1120 $self->emit ('escape'); 1313 $self->emit ('escape');
1121 } elsif ($uni) { 1314 } elsif ($uni) {
1122 substr $text, $self->{cursor}++, 0, chr $uni; 1315 substr $text, $self->{cursor}++, 0, chr $uni;
1123 } 1316 }
1124 1317
1179 1372
1180 $self->SUPER::_draw; 1373 $self->SUPER::_draw;
1181 1374
1182 #TODO: force update every cursor change :( 1375 #TODO: force update every cursor change :(
1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1376 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1377
1378 unless (exists $self->{cur_h}) {
1379 my $text = substr $self->{text}, 0, $self->{cursor};
1380 utf8::encode $text;
1381
1382 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1383 }
1384
1184 glColor @{$self->{fg}}; 1385 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1386 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1387 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1388 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1188 glEnd; 1389 glEnd;
1189 } 1390 }
1190} 1391}
1191 1392
1192package CFClient::UI::LineEntry; 1393package CFClient::UI::Entry;
1193 1394
1194our @ISA = CFClient::UI::Entry::; 1395our @ISA = CFClient::UI::EntryBase::;
1195 1396
1196use SDL; 1397use CFClient::OpenGL;
1197use SDL::OpenGL;
1198 1398
1199sub key_down { 1399sub key_down {
1200 my ($self, $ev) = @_; 1400 my ($self, $ev) = @_;
1201 1401
1202 my $sym = $ev->key_sym; 1402 my $sym = $ev->{sym};
1203 1403
1204 if ($sym == SDLK_RETURN) { 1404 if ($sym == 13) {
1205 $self->emit (activate => $self->get_text); 1405 $self->emit (activate => $self->get_text);
1206 $self->update; 1406 $self->update;
1207 1407
1208 } else { 1408 } else {
1209 $self->SUPER::key_down ($ev); 1409 $self->SUPER::key_down ($ev);
1215 1415
1216package CFClient::UI::Button; 1416package CFClient::UI::Button;
1217 1417
1218our @ISA = CFClient::UI::Label::; 1418our @ISA = CFClient::UI::Label::;
1219 1419
1220use SDL; 1420use CFClient::OpenGL;
1221use SDL::OpenGL;
1222 1421
1223my @tex = 1422my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1423 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1225 qw(b1_button_active.png); 1424 qw(b1_button_active.png);
1226 1425
1227sub new { 1426sub new {
1228 my $class = shift; 1427 my $class = shift;
1229 1428
1230 $class->SUPER::new ( 1429 $class->SUPER::new (
1231 padding => 4, 1430 padding => 4,
1232 fg => [1, 1, 1], 1431 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1432 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1433 active_fg => [0, 0, 1],
1235 can_hover => 1, 1434 can_hover => 1,
1435 align => 0,
1436 valign => 0,
1437 can_events => 1,
1236 @_ 1438 @_
1237 ) 1439 )
1238} 1440}
1239 1441
1240sub button_up { 1442sub button_up {
1248 1450
1249sub _draw { 1451sub _draw {
1250 my ($self) = @_; 1452 my ($self) = @_;
1251 1453
1252 local $self->{fg} = $self->{fg}; 1454 local $self->{fg} = $self->{fg};
1253 my $tex = $tex[0];
1254
1255 glEnable GL_BLEND;
1256 glEnable GL_TEXTURE_2D;
1257 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1258 1455
1259 if ($GRAB == $self) { 1456 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1457 $self->{fg} = $self->{active_fg};
1261 } 1458 }
1262 1459
1263 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1460 glEnable GL_BLEND;
1461 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1462 glEnable GL_TEXTURE_2D;
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1463 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1464 glColor 0, 0, 0, 1;
1265 1465
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1466 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1467
1268 glDisable GL_TEXTURE_2D; 1468 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1469 glDisable GL_BLEND;
1270 1470
1271 $self->SUPER::_draw; 1471 $self->SUPER::_draw;
1276package CFClient::UI::CheckBox; 1476package CFClient::UI::CheckBox;
1277 1477
1278our @ISA = CFClient::UI::DrawBG::; 1478our @ISA = CFClient::UI::DrawBG::;
1279 1479
1280my @tex = 1480my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1482 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283 1483
1284use SDL; 1484use CFClient::OpenGL;
1285use SDL::OpenGL;
1286 1485
1287sub new { 1486sub new {
1288 my $class = shift; 1487 my $class = shift;
1289 1488
1290 $class->SUPER::new ( 1489 $class->SUPER::new (
1301 my ($self) = @_; 1500 my ($self) = @_;
1302 1501
1303 ($self->{padding} * 2 + 6) x 2 1502 ($self->{padding} * 2 + 6) x 2
1304} 1503}
1305 1504
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down { 1505sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1506 my ($self, $ev, $x, $y) = @_;
1314 1507
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1508 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1509 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1342 glDisable GL_BLEND; 1535 glDisable GL_BLEND;
1343} 1536}
1344 1537
1345############################################################################# 1538#############################################################################
1346 1539
1540package CFClient::UI::Image;
1541
1542our @ISA = CFClient::UI::Base::;
1543
1544use CFClient::OpenGL;
1545use Carp qw/confess/;
1546
1547our %loaded_images;
1548
1549sub new {
1550 my $class = shift;
1551
1552 my $self = $class->SUPER::new (can_events => 0, @_);
1553
1554 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1555
1556 $loaded_images{$self->{image}} ||=
1557 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1558
1559 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1560
1561 Scalar::Util::weaken $loaded_images{$self->{image}};
1562
1563 $self->{aspect} = $tex->{w} / $tex->{h};
1564
1565 $self
1566}
1567
1568sub size_request {
1569 my ($self) = @_;
1570
1571 ($self->{tex}->{w}, $self->{tex}->{h})
1572}
1573
1574sub _draw {
1575 my ($self) = @_;
1576
1577 my $tex = $self->{tex};
1578
1579 my ($w, $h) = ($self->{w}, $self->{h});
1580
1581 if ($self->{rot90}) {
1582 glRotate 90, 0, 0, 1;
1583 glTranslate 0, -$self->{w}, 0;
1584
1585 ($w, $h) = ($h, $w);
1586 }
1587
1588 glEnable GL_BLEND;
1589 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1590 glEnable GL_TEXTURE_2D;
1591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1592
1593 $tex->draw_quad (0, 0, $w, $h);
1594
1595 glDisable GL_BLEND;
1596 glDisable GL_TEXTURE_2D;
1597}
1598
1599#############################################################################
1600
1601package CFClient::UI::VGauge;
1602
1603our @ISA = CFClient::UI::Base::;
1604
1605use CFClient::OpenGL;
1606
1607my %tex = (
1608 food => [
1609 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1610 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1611 ],
1612 grace => [
1613 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1614 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1615 ],
1616 hp => [
1617 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1618 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1619 ],
1620 mana => [
1621 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1622 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1623 ],
1624);
1625
1626# eg. VGauge->new (gauge => 'food'), default gauge: food
1627sub new {
1628 my $class = shift;
1629
1630 my $self = $class->SUPER::new (
1631 type => 'food',
1632 @_
1633 );
1634
1635 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1636
1637 $self
1638}
1639
1640sub size_request {
1641 my ($self) = @_;
1642
1643 #my $tex = $tex{$self->{type}}[0];
1644 #@$tex{qw(w h)}
1645 (0, 0)
1646}
1647
1648sub set_max {
1649 my ($self, $max) = @_;
1650
1651 $self->{max_val} = $max;
1652}
1653
1654sub set_value {
1655 my ($self, $val, $max) = @_;
1656
1657 $self->set_max ($max)
1658 if defined $max;
1659
1660 $max = $self->{max_val};
1661 $self->{val} = $val;
1662
1663 $self->update;
1664}
1665
1666sub _draw {
1667 my ($self) = @_;
1668
1669 my $tex = $tex{$self->{type}};
1670
1671 my ($w, $h) = ($self->{w}, $self->{h});
1672
1673 if ($self->{vertical}) {
1674 glRotate 90, 0, 0, 1;
1675 glTranslate 0, -$self->{w}, 0;
1676
1677 ($w, $h) = ($h, $w);
1678 }
1679
1680 my $ycut = $self->{val} / ($self->{max_val} || 1);
1681 $ycut = 1 if $self->{val} > $self->{max_val};
1682
1683 my $t1 = $tex->[0];
1684 my $t2 = $tex->[1];
1685
1686 glEnable GL_BLEND;
1687 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1688 glEnable GL_TEXTURE_2D;
1689 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1690
1691 my $h1 = $self->{h} - $ycut * $self->{h};
1692 my $h2 = $ycut * $self->{h};
1693
1694 glBindTexture GL_TEXTURE_2D, $t1->{name};
1695 glBegin GL_QUADS;
1696 glTexCoord 0 , 0; glVertex 0 , 0;
1697 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1698 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1699 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1700 glEnd;
1701
1702 glBindTexture GL_TEXTURE_2D, $t2->{name};
1703 glBegin GL_QUADS;
1704 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1705 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1706 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1707 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1708 glEnd;
1709
1710 glDisable GL_BLEND;
1711 glDisable GL_TEXTURE_2D;
1712}
1713
1714#############################################################################
1715
1716package CFClient::UI::Gauge;
1717
1718our @ISA = CFClient::UI::VBox::;
1719
1720sub new {
1721 my ($class, %arg) = @_;
1722
1723 my $self = $class->SUPER::new (
1724 tooltip => $arg{type},
1725 %arg,
1726 );
1727
1728 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1729 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1730 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1731
1732 $self
1733}
1734
1735sub set_fontsize {
1736 my ($self, $fsize) = @_;
1737
1738 $self->{value}->set_fontsize ($fsize);
1739 $self->{max} ->set_fontsize ($fsize);
1740}
1741
1742sub set_value {
1743 my ($self, $val, $max) = @_;
1744
1745 $self->set_max ($max)
1746 if defined $max;
1747
1748 $self->{gauge}->set_value ($val, $max);
1749 $self->{value}->set_text ($val);
1750}
1751
1752sub set_max {
1753 my ($self, $max) = @_;
1754
1755 $self->{gauge}->set_max ($max);
1756 $self->{max}->set_text ($max);
1757}
1758
1759#############################################################################
1760
1347package CFClient::UI::Slider; 1761package CFClient::UI::Slider;
1348 1762
1349use strict; 1763use strict;
1350 1764
1351use SDL::OpenGL; 1765use CFClient::OpenGL;
1352 1766
1353our @ISA = CFClient::UI::DrawBG::; 1767our @ISA = CFClient::UI::DrawBG::;
1354 1768
1355my @tex = 1769my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1770 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1366 # TODO: calculations are off 1780 # TODO: calculations are off
1367 my $self = $class->SUPER::new ( 1781 my $self = $class->SUPER::new (
1368 fg => [1, 1, 1], 1782 fg => [1, 1, 1],
1369 active_fg => [0, 0, 0], 1783 active_fg => [0, 0, 0],
1370 range => [0, 0, 100, 10], 1784 range => [0, 0, 100, 10],
1371 req_w => 40, 1785 req_w => 20,
1372 req_h => 13, 1786 req_h => 20,
1373 vertical => 0, 1787 vertical => 0,
1374 can_hover => 1, 1788 can_hover => 1,
1375 inner_pad => 5, 1789 inner_pad => 5,
1376 @_ 1790 @_
1377 ); 1791 );
1481 1895
1482package CFClient::UI::TextView; 1896package CFClient::UI::TextView;
1483 1897
1484our @ISA = CFClient::UI::HBox::; 1898our @ISA = CFClient::UI::HBox::;
1485 1899
1486use SDL::OpenGL; 1900use CFClient::OpenGL;
1487 1901
1488sub new { 1902sub new {
1489 my $class = shift; 1903 my $class = shift;
1490 1904
1491 my $self = $class->SUPER::new ( 1905 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE, 1906 fontsize => 1,
1495 @_, 1907 @_,
1496 1908
1497 layout => (new CFClient::Layout), 1909 layout => (new CFClient::Layout),
1498 par => [], 1910 par => [],
1499 height => 0, 1911 height => 0,
1520sub text_height { 1932sub text_height {
1521 my ($self, $text) = @_; 1933 my ($self, $text) = @_;
1522 1934
1523 my $layout = $self->{layout}; 1935 my $layout = $self->{layout};
1524 1936
1525 $layout->set_height ($self->{fontsize}); 1937 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1526 $layout->set_width ($self->{w}); 1938 $layout->set_width ($self->{w});
1527 $layout->set_text ($text); 1939 $layout->set_text ($text);
1528 1940
1529 ($layout->size)[1] 1941 ($layout->size)[1]
1530} 1942}
1534 1946
1535 $self->{need_reflow}++; 1947 $self->{need_reflow}++;
1536 $self->update; 1948 $self->update;
1537} 1949}
1538 1950
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate { 1951sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 1952 my ($self, $w, $h) = @_;
1547 1953
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 1954 $self->SUPER::size_allocate ($w, $h);
1549 1955
1550 $self->{layout}->set_height ($self->{fontsize}); 1956 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 $self->{layout}->set_width ($self->{w}); 1957 $self->{layout}->set_width ($self->{children}[0]{w});
1552 1958
1553 $self->reflow; 1959 $self->reflow;
1554} 1960}
1555 1961
1556sub add_paragraph { 1962sub add_paragraph {
1575 1981
1576 return unless $self->{h} > 0; 1982 return unless $self->{h} > 0;
1577 1983
1578 delete $self->{texture}; 1984 delete $self->{texture};
1579 1985
1580 $TOPLEVEL->on_refresh ($self, sub { 1986 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1987 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1988 my $height = 0;
1583 1989
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1990 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1991 for @{$self->{par}};
1643 2049
1644} 2050}
1645 2051
1646############################################################################# 2052#############################################################################
1647 2053
1648package CFClient::UI::MapWidget;
1649
1650use strict;
1651
1652use List::Util qw(min max);
1653
1654use SDL;
1655use SDL::OpenGL;
1656
1657our @ISA = CFClient::UI::Base::;
1658
1659sub new {
1660 my $class = shift;
1661
1662 $class->SUPER::new (
1663 z => -1,
1664 can_focus => 1,
1665 list => (glGenLists 1),
1666 @_
1667 )
1668}
1669
1670sub key_down {
1671 print "MAPKEYDOWN\n";
1672}
1673
1674sub key_up {
1675}
1676
1677sub button_down {
1678 my ($self, $ev, $x, $y) = @_;
1679
1680 $self->focus_in;
1681
1682 if ($ev->button == 2) {
1683 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1684 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1685
1686 $self->{motion} = sub {
1687 my ($ev, $x, $y) = @_;
1688
1689 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1690
1691 $::CFG->{map_shift_x} = $bw + $x - $ox;
1692 $::CFG->{map_shift_y} = $bh + $y - $oy;
1693
1694 $self->update;
1695 };
1696 }
1697}
1698
1699sub button_up {
1700 my ($self, $ev, $x, $y) = @_;
1701
1702 delete $self->{motion};
1703}
1704
1705sub mouse_motion {
1706 my ($self, $ev, $x, $y) = @_;
1707
1708 $self->{motion}->($ev, $x, $y) if $self->{motion};
1709}
1710
1711sub size_request {
1712 (
1713 1 + 32 * int $::WIDTH / 32,
1714 1 + 32 * int $::HEIGHT / 32,
1715 )
1716}
1717
1718sub update {
1719 my ($self) = @_;
1720
1721 $self->{need_update} = 1;
1722 $self->SUPER::update;
1723}
1724
1725sub draw {
1726 my ($self) = @_;
1727
1728 if (delete $self->{need_update}) {
1729 glNewList $self->{list}, GL_COMPILE;
1730
1731 if ($::MAP) {
1732 my $sw = int $::WIDTH / 32;
1733 my $sh = int $::HEIGHT / 32;
1734
1735 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1736 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1737
1738 glTranslate $sx0 - 32, $sy0 - 32, 0;
1739
1740 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1741
1742 if ($::CFG->{fow_enable}) {
1743 if ($::CFG->{fow_smooth}) { # smooth fog of war
1744 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1745 glConvolutionFilter2D
1746 GL_CONVOLUTION_2D,
1747 GL_ALPHA,
1748 3, 3,
1749 GL_ALPHA, GL_FLOAT,
1750 pack "f*",
1751 0.1, 0.1, 0.1,
1752 0.1, 0.2, 0.1,
1753 0.1, 0.1, 0.1,
1754 ;
1755 glEnable GL_CONVOLUTION_2D;
1756 }
1757
1758 my $tex = new CFClient::Texture
1759 w => $w,
1760 h => $h,
1761 data => $data,
1762 internalformat => GL_ALPHA,
1763 format => GL_ALPHA;
1764
1765 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1766
1767 glEnable GL_BLEND;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1769 glEnable GL_TEXTURE_2D;
1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1771
1772 glColor +($::CFG->{fow_intensity}) x 3, 1;
1773 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1774
1775 glDisable GL_TEXTURE_2D;
1776 glDisable GL_BLEND;
1777 }
1778
1779 # HACK BEGIN
1780 {
1781 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1782 my ($w, $h) = (250, 250);
1783
1784 glEnable GL_BLEND;
1785 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1786 glEnable GL_TEXTURE_2D;
1787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1788
1789 CFClient::Texture->new (
1790 w => $w,
1791 h => $h,
1792 data => $::MAP->mapmap ($w, $h),
1793 type => GL_UNSIGNED_INT_8_8_8_8_REV
1794 )->draw_quad (100, 100);
1795
1796 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND;
1798 }
1799 # HACK END
1800 }
1801
1802 glEndList;
1803 }
1804
1805 glPushMatrix;
1806 glCallList $self->{list};
1807 glPopMatrix;
1808
1809 if ($FOCUS != $self) {
1810 glColor 64/255, 64/255, 64/255;
1811 glLogicOp GL_AND;
1812 glEnable GL_COLOR_LOGIC_OP;
1813 glBegin GL_QUADS;
1814 glVertex 0, 0;
1815 glVertex 0, $::HEIGHT;
1816 glVertex $::WIDTH, $::HEIGHT;
1817 glVertex $::WIDTH, 0;
1818 glEnd;
1819 glDisable GL_COLOR_LOGIC_OP;
1820 }
1821}
1822
1823my %DIR = (
1824 SDLK_KP8, [1, "north"],
1825 SDLK_KP9, [2, "northeast"],
1826 SDLK_KP6, [3, "east"],
1827 SDLK_KP3, [4, "southeast"],
1828 SDLK_KP2, [5, "south"],
1829 SDLK_KP1, [6, "southwest"],
1830 SDLK_KP4, [7, "west"],
1831 SDLK_KP7, [8, "northwest"],
1832
1833 SDLK_UP, [1, "north"],
1834 SDLK_RIGHT, [3, "east"],
1835 SDLK_DOWN, [5, "south"],
1836 SDLK_LEFT, [7, "west"],
1837);
1838
1839sub key_down {
1840 my ($self, $ev) = @_;
1841
1842 my $mod = $ev->key_mod;
1843 my $sym = $ev->key_sym;
1844
1845 if ($sym == SDLK_KP5) {
1846 $::CONN->user_send ("command stay fire");
1847 } elsif ($sym == SDLK_a) {
1848 $::CONN->user_send ("command apply");
1849 } elsif ($sym == SDLK_QUOTE) {
1850 $self->emit ('activate_console');
1851 } elsif ($sym == SDLK_SLASH) {
1852 $self->emit ('activate_console' => '/');
1853 } elsif (exists $DIR{$sym}) {
1854 if ($mod & KMOD_SHIFT) {
1855 $self->{shft}++;
1856 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1857 } elsif ($mod & KMOD_CTRL) {
1858 $self->{ctrl}++;
1859 $::CONN->user_send ("command run $DIR{$sym}[0]");
1860 } else {
1861 $::CONN->user_send ("command $DIR{$sym}[1]");
1862 }
1863 }
1864}
1865
1866sub key_up {
1867 my ($self, $ev) = @_;
1868
1869 my $mod = $ev->key_mod;
1870 my $sym = $ev->key_sym;
1871
1872 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1873 $::CONN->user_send ("command fire_stop");
1874 }
1875 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1876 $::CONN->user_send ("command run_stop");
1877 }
1878}
1879
1880#############################################################################
1881
1882package CFClient::UI::Animator; 2054package CFClient::UI::Animator;
1883 2055
1884use SDL::OpenGL; 2056use CFClient::OpenGL;
1885 2057
1886our @ISA = CFClient::UI::Bin::; 2058our @ISA = CFClient::UI::Bin::;
1887 2059
1888sub moveto { 2060sub moveto {
1889 my ($self, $x, $y) = @_; 2061 my ($self, $x, $y) = @_;
1929 my $class = shift; 2101 my $class = shift;
1930 2102
1931 my $self = $class->SUPER::new ( 2103 my $self = $class->SUPER::new (
1932 state => 0, 2104 state => 0,
1933 connect_activate => \&toggle_flopper, 2105 connect_activate => \&toggle_flopper,
2106 can_events => 1,
1934 @_ 2107 @_
1935 ); 2108 );
1936 2109
1937 if ($self->{state}) { 2110 if ($self->{state}) {
1938 $self->{state} = 0; 2111 $self->{state} = 0;
1945sub toggle_flopper { 2118sub toggle_flopper {
1946 my ($self) = @_; 2119 my ($self) = @_;
1947 2120
1948 # TODO: use animation 2121 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 2122 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2123 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 2124 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 2125 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 2126 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2127 $CFClient::UI::ROOT->remove ($self->{other});
2128 $self->emit ("close");
2129 }
2130
2131 $self->emit (changed => $self->{state});
2132}
2133
2134#############################################################################
2135
2136package CFClient::UI::Tooltip;
2137
2138our @ISA = CFClient::UI::Bin::;
2139
2140use CFClient::OpenGL;
2141
2142sub new {
2143 my $class = shift;
2144
2145 $class->SUPER::new (
2146 @_,
2147 can_events => 0,
2148 )
2149}
2150
2151sub set_text {
2152 my ($self, $text) = @_;
2153
2154 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2155 $self->{label}->set_text ($text);
2156 $self->add ($self->{label});
2157}
2158
2159sub size_request {
2160 my ($self) = @_;
2161
2162 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2163
2164 $w = List::Util::min $::WIDTH * 0.2, $w;
2165 $h = List::Util::max $::HEIGHT * 0.2, $h;
2166
2167 ($w + 4, $h + 4)
2168}
2169
2170sub _draw {
2171 my ($self) = @_;
2172
2173 glPushMatrix;
2174 glTranslate 0.375, 0.375;
2175
2176 my ($w, $h) = @$self{qw(w h)};
2177
2178 glColor 1, 0.8, 0.4;
2179 glBegin GL_QUADS;
2180 glVertex 0 , 0;
2181 glVertex 0 , $h;
2182 glVertex $w, $h;
2183 glVertex $w, 0;
2184 glEnd;
1957 } 2185
1958} 2186 glColor 0, 0, 0;
2187 glBegin GL_LINE_LOOP;
2188 glVertex 0 , 0;
2189 glVertex 0 , $h;
2190 glVertex $w, $h;
2191 glVertex $w, 0;
2192 glEnd;
2193
2194 glPopMatrix;
1959 2195
1960############################################################################# 2196 glTranslate 2, 2;
2197 $self->SUPER::_draw;
2198}
1961 2199
2200#############################################################################
2201
1962package CFClient::UI::Toplevel; 2202package CFClient::UI::Root;
1963 2203
1964our @ISA = CFClient::UI::Container::; 2204our @ISA = CFClient::UI::Container::;
1965 2205
1966use SDL::OpenGL; 2206use CFClient::OpenGL;
2207
2208sub check_size {
2209 my ($self) = @_;
2210
2211 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2212}
1967 2213
1968sub size_request { 2214sub size_request {
1969 ($::WIDTH, $::HEIGHT) 2215 ($::WIDTH, $::HEIGHT)
1970} 2216}
1971 2217
1972sub size_allocate { 2218sub configure {
1973 my ($self, $x, $y, $w, $h) = @_; 2219 my ($self, $x, $y, $w, $h) = @_;
1974 2220
1975 $self->_size_allocate ($x, $y, $w, $h); 2221 $self->SUPER::configure ($x, $y, $w, $h);
1976 2222
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 2223 for my $child (@{$self->{children}}) {
1979} 2224 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1980 2225
1981sub translate { 2226 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2227 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2228 $child->configure ($X, $Y, $W,$H);
2229 }
2230}
2231
2232sub _topleft {
1982 my ($self, $x, $y) = @_; 2233 my ($self, $x, $y) = @_;
1983 2234
1984 ($x, $y) 2235 ($x, $y)
1985} 2236}
1986 2237
1987sub update { 2238sub update {
1988 my ($self) = @_; 2239 my ($self) = @_;
1989 2240
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2241 $self->check_size;
1991 ::refresh (); 2242 ::refresh ();
1992} 2243}
1993 2244
1994sub add { 2245sub add {
1995 my ($self, $widget) = @_; 2246 my ($self, $child) = @_;
1996 2247
2248 # integerize window positions
2249 $child->{x} = int $child->{x};
2250 $child->{y} = int $child->{y};
2251
1997 $self->SUPER::add ($widget); 2252 $self->SUPER::add ($child);
1998
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000} 2253}
2001 2254
2002sub on_refresh { 2255sub on_refresh {
2003 my ($self, $id, $cb) = @_; 2256 my ($self, $id, $cb) = @_;
2004 2257
2027 2280
2028############################################################################# 2281#############################################################################
2029 2282
2030package CFClient::UI; 2283package CFClient::UI;
2031 2284
2285$ROOT = new CFClient::UI::Root;
2032$TOPLEVEL = new CFClient::UI::Toplevel; 2286$TOOLTIP = new CFClient::UI::Tooltip;
2033 2287
20341 22881
2035 2289

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines