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.151 by root, Sun Apr 23 00:57:39 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
995 delete $self->{texture}; 1183 $self->{fontsize} = $fontsize;
996 $self->SUPER::update; 1184 $self->check_size;
997} 1185}
998 1186
999sub _draw { 1187sub _draw {
1000 my ($self) = @_; 1188 my ($self) = @_;
1001 1189
1002 my $tex = $self->{texture} ||= do { 1190 my $tex = $self->{texture} ||= do {
1003 $self->{layout}->set_width ($self->{w}); 1191 $self->{layout}->set_width ($self->{w});
1004 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1192 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1005 new_from_layout CFClient::Texture $self->{layout} 1193 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1194 };
1007 1195
1008 glEnable GL_BLEND; 1196 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1197 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1198 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1199 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1200
1013 glColor @{$self->{fg}}; 1201 glColor @{$self->{fg}};
1014 1202
1015 my $x = 1203 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1204 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1205 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1206 : ($self->{w} - $tex->{w}) * 0.5
1207 );
1019 1208
1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1209 $self->{oy} = int (
1210 $self->{valign} < 0 ? $self->{padding}
1211 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1212 : ($self->{h} - $tex->{h}) * 0.5
1213 );
1214
1215 $tex->draw_quad ($self->{ox}, $self->{oy});
1021 1216
1022 glDisable GL_TEXTURE_2D; 1217 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1218 glDisable GL_BLEND;
1024} 1219}
1025 1220
1026############################################################################# 1221#############################################################################
1027 1222
1028package CFClient::UI::Entry; 1223package CFClient::UI::EntryBase;
1029 1224
1030our @ISA = CFClient::UI::Label::; 1225our @ISA = CFClient::UI::Label::;
1031 1226
1032use SDL; 1227use CFClient::OpenGL;
1033use SDL::OpenGL;
1034 1228
1035sub new { 1229sub new {
1036 my $class = shift; 1230 my $class = shift;
1037 1231
1038 $class->SUPER::new ( 1232 $class->SUPER::new (
1040 bg => [0, 0, 0, 0.2], 1234 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1235 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1236 active_fg => [0, 0, 0],
1043 can_hover => 1, 1237 can_hover => 1,
1044 can_focus => 1, 1238 can_focus => 1,
1239 valign => 0,
1240 can_events => 1,
1045 @_ 1241 @_
1046 ) 1242 )
1047} 1243}
1048 1244
1049sub _set_text { 1245sub _set_text {
1050 my ($self, $text) = @_; 1246 my ($self, $text) = @_;
1051 1247
1052 my $old_text = $self->{text}; 1248 delete $self->{cur_h};
1249
1250 return if $self->{text} eq $text;
1251
1252 delete $self->{texture};
1053 1253
1054 $self->{last_activity} = $::NOW; 1254 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1255 $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 1256
1060 $text =~ s/./*/g if $self->{hidden}; 1257 $text =~ s/./*/g if $self->{hidden};
1258 $self->{layout}->set_text ("$text ");
1061 1259
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1260 $self->emit (changed => $self->{text});
1261}
1063 1262
1064 $text = substr $text, 0, $self->{cursor}; 1263sub get_text {
1065 utf8::encode $text; 1264 $_[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} 1265}
1072 1266
1073sub size_request { 1267sub size_request {
1074 my ($self) = @_; 1268 my ($self) = @_;
1075 1269
1077 1271
1078 ($w + 1, $h) # add 1 for cursor 1272 ($w + 1, $h) # add 1 for cursor
1079} 1273}
1080 1274
1081sub size_allocate { 1275sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1276 my ($self, $w, $h) = @_;
1083 1277
1084 $self->SUPER::size_allocate ($x, $y, $w, $h); 1278 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1085
1086 $self->_set_text ($self->{text});
1087} 1279}
1088 1280
1089sub set_text { 1281sub set_text {
1090 my ($self, $text) = @_; 1282 my ($self, $text) = @_;
1091 1283
1095} 1287}
1096 1288
1097sub key_down { 1289sub key_down {
1098 my ($self, $ev) = @_; 1290 my ($self, $ev) = @_;
1099 1291
1100 my $mod = $ev->key_mod; 1292 my $mod = $ev->{mod};
1101 my $sym = $ev->key_sym; 1293 my $sym = $ev->{sym};
1102
1103 my $uni = $ev->key_unicode; 1294 my $uni = $ev->{unicode};
1104 1295
1105 my $text = $self->get_text; 1296 my $text = $self->get_text;
1106 1297
1107 if ($sym == SDLK_BACKSPACE) { 1298 if ($sym == 8) {
1108 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1299 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1109 } elsif ($sym == SDLK_DELETE) { 1300 } elsif ($sym == 127) {
1110 substr $text, $self->{cursor}, 1, ""; 1301 substr $text, $self->{cursor}, 1, "";
1111 } elsif ($sym == SDLK_LEFT) { 1302 } elsif ($sym == CFClient::SDLK_LEFT) {
1112 --$self->{cursor} if $self->{cursor}; 1303 --$self->{cursor} if $self->{cursor};
1113 } elsif ($sym == SDLK_RIGHT) { 1304 } elsif ($sym == CFClient::SDLK_RIGHT) {
1114 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1305 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) { 1306 } elsif ($sym == CFClient::SDLK_HOME) {
1116 $self->{cursor} = 0; 1307 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) { 1308 } elsif ($sym == CFClient::SDLK_END) {
1118 $self->{cursor} = length $text; 1309 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) { 1310 } elsif ($sym == 27) {
1120 $self->emit ('escape'); 1311 $self->emit ('escape');
1121 } elsif ($uni) { 1312 } elsif ($uni) {
1122 substr $text, $self->{cursor}++, 0, chr $uni; 1313 substr $text, $self->{cursor}++, 0, chr $uni;
1123 } 1314 }
1124 1315
1179 1370
1180 $self->SUPER::_draw; 1371 $self->SUPER::_draw;
1181 1372
1182 #TODO: force update every cursor change :( 1373 #TODO: force update every cursor change :(
1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1374 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1375
1376 unless (exists $self->{cur_h}) {
1377 my $text = substr $self->{text}, 0, $self->{cursor};
1378 utf8::encode $text;
1379
1380 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1381 }
1382
1184 glColor @{$self->{fg}}; 1383 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1384 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1385 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1386 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1188 glEnd; 1387 glEnd;
1189 } 1388 }
1190} 1389}
1191 1390
1192package CFClient::UI::LineEntry; 1391package CFClient::UI::Entry;
1193 1392
1194our @ISA = CFClient::UI::Entry::; 1393our @ISA = CFClient::UI::EntryBase::;
1195 1394
1196use SDL; 1395use CFClient::OpenGL;
1197use SDL::OpenGL;
1198 1396
1199sub key_down { 1397sub key_down {
1200 my ($self, $ev) = @_; 1398 my ($self, $ev) = @_;
1201 1399
1202 my $sym = $ev->key_sym; 1400 my $sym = $ev->{sym};
1203 1401
1204 if ($sym == SDLK_RETURN) { 1402 if ($sym == 13) {
1205 $self->emit (activate => $self->get_text); 1403 $self->emit (activate => $self->get_text);
1206 $self->update; 1404 $self->update;
1207 1405
1208 } else { 1406 } else {
1209 $self->SUPER::key_down ($ev); 1407 $self->SUPER::key_down ($ev);
1215 1413
1216package CFClient::UI::Button; 1414package CFClient::UI::Button;
1217 1415
1218our @ISA = CFClient::UI::Label::; 1416our @ISA = CFClient::UI::Label::;
1219 1417
1220use SDL; 1418use CFClient::OpenGL;
1221use SDL::OpenGL;
1222 1419
1223my @tex = 1420my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1421 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1225 qw(b1_button_active.png); 1422 qw(b1_button_active.png);
1226 1423
1227sub new { 1424sub new {
1228 my $class = shift; 1425 my $class = shift;
1229 1426
1230 $class->SUPER::new ( 1427 $class->SUPER::new (
1231 padding => 4, 1428 padding => 4,
1232 fg => [1, 1, 1], 1429 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1430 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1431 active_fg => [0, 0, 1],
1235 can_hover => 1, 1432 can_hover => 1,
1433 align => 0,
1434 valign => 0,
1435 can_events => 1,
1236 @_ 1436 @_
1237 ) 1437 )
1238} 1438}
1239 1439
1240sub button_up { 1440sub button_up {
1248 1448
1249sub _draw { 1449sub _draw {
1250 my ($self) = @_; 1450 my ($self) = @_;
1251 1451
1252 local $self->{fg} = $self->{fg}; 1452 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 1453
1259 if ($GRAB == $self) { 1454 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1455 $self->{fg} = $self->{active_fg};
1261 } 1456 }
1262 1457
1263 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1458 glEnable GL_BLEND;
1459 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1460 glEnable GL_TEXTURE_2D;
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1461 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1462 glColor 0, 0, 0, 1;
1265 1463
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1464 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1465
1268 glDisable GL_TEXTURE_2D; 1466 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1467 glDisable GL_BLEND;
1270 1468
1271 $self->SUPER::_draw; 1469 $self->SUPER::_draw;
1276package CFClient::UI::CheckBox; 1474package CFClient::UI::CheckBox;
1277 1475
1278our @ISA = CFClient::UI::DrawBG::; 1476our @ISA = CFClient::UI::DrawBG::;
1279 1477
1280my @tex = 1478my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1479 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1480 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283 1481
1284use SDL; 1482use CFClient::OpenGL;
1285use SDL::OpenGL;
1286 1483
1287sub new { 1484sub new {
1288 my $class = shift; 1485 my $class = shift;
1289 1486
1290 $class->SUPER::new ( 1487 $class->SUPER::new (
1301 my ($self) = @_; 1498 my ($self) = @_;
1302 1499
1303 ($self->{padding} * 2 + 6) x 2 1500 ($self->{padding} * 2 + 6) x 2
1304} 1501}
1305 1502
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down { 1503sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1504 my ($self, $ev, $x, $y) = @_;
1314 1505
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1506 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1507 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1342 glDisable GL_BLEND; 1533 glDisable GL_BLEND;
1343} 1534}
1344 1535
1345############################################################################# 1536#############################################################################
1346 1537
1538package CFClient::UI::Image;
1539
1540our @ISA = CFClient::UI::Base::;
1541
1542use CFClient::OpenGL;
1543use Carp qw/confess/;
1544
1545our %loaded_images;
1546
1547sub new {
1548 my $class = shift;
1549
1550 my $self = $class->SUPER::new (can_events => 0, @_);
1551
1552 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1553
1554 $loaded_images{$self->{image}} ||=
1555 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1556
1557 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1558
1559 Scalar::Util::weaken $loaded_images{$self->{image}};
1560
1561 $self->{aspect} = $tex->{w} / $tex->{h};
1562
1563 $self
1564}
1565
1566sub size_request {
1567 my ($self) = @_;
1568
1569 ($self->{tex}->{w}, $self->{tex}->{h})
1570}
1571
1572sub _draw {
1573 my ($self) = @_;
1574
1575 my $tex = $self->{tex};
1576
1577 my ($w, $h) = ($self->{w}, $self->{h});
1578
1579 if ($self->{rot90}) {
1580 glRotate 90, 0, 0, 1;
1581 glTranslate 0, -$self->{w}, 0;
1582
1583 ($w, $h) = ($h, $w);
1584 }
1585
1586 glEnable GL_BLEND;
1587 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1588 glEnable GL_TEXTURE_2D;
1589 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1590
1591 $tex->draw_quad (0, 0, $w, $h);
1592
1593 glDisable GL_BLEND;
1594 glDisable GL_TEXTURE_2D;
1595}
1596
1597#############################################################################
1598
1599package CFClient::UI::VGauge;
1600
1601our @ISA = CFClient::UI::Base::;
1602
1603use CFClient::OpenGL;
1604
1605my %tex = (
1606 food => [
1607 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1608 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1609 ],
1610 grace => [
1611 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1612 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1613 ],
1614 hp => [
1615 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1616 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1617 ],
1618 mana => [
1619 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1620 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1621 ],
1622);
1623
1624# eg. VGauge->new (gauge => 'food'), default gauge: food
1625sub new {
1626 my $class = shift;
1627
1628 my $self = $class->SUPER::new (
1629 type => 'food',
1630 @_
1631 );
1632
1633 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1634
1635 $self
1636}
1637
1638sub size_request {
1639 my ($self) = @_;
1640
1641 #my $tex = $tex{$self->{type}}[0];
1642 #@$tex{qw(w h)}
1643 (0, 0)
1644}
1645
1646sub set_max {
1647 my ($self, $max) = @_;
1648
1649 $self->{max_val} = $max;
1650}
1651
1652sub set_value {
1653 my ($self, $val, $max) = @_;
1654
1655 $self->set_max ($max)
1656 if defined $max;
1657
1658 $max = $self->{max_val};
1659 $self->{val} = $val;
1660
1661 $self->update;
1662}
1663
1664sub _draw {
1665 my ($self) = @_;
1666
1667 my $tex = $tex{$self->{type}};
1668
1669 my ($w, $h) = ($self->{w}, $self->{h});
1670
1671 if ($self->{vertical}) {
1672 glRotate 90, 0, 0, 1;
1673 glTranslate 0, -$self->{w}, 0;
1674
1675 ($w, $h) = ($h, $w);
1676 }
1677
1678 my $ycut = $self->{val} / ($self->{max_val} || 1);
1679 $ycut = 1 if $self->{val} > $self->{max_val};
1680
1681 my $t1 = $tex->[0];
1682 my $t2 = $tex->[1];
1683
1684 glEnable GL_BLEND;
1685 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1686 glEnable GL_TEXTURE_2D;
1687 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1688
1689 my $h1 = $self->{h} - $ycut * $self->{h};
1690 my $h2 = $ycut * $self->{h};
1691
1692 glBindTexture GL_TEXTURE_2D, $t1->{name};
1693 glBegin GL_QUADS;
1694 glTexCoord 0 , 0; glVertex 0 , 0;
1695 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1696 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1697 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1698 glEnd;
1699
1700 glBindTexture GL_TEXTURE_2D, $t2->{name};
1701 glBegin GL_QUADS;
1702 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1703 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1704 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1705 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1706 glEnd;
1707
1708 glDisable GL_BLEND;
1709 glDisable GL_TEXTURE_2D;
1710}
1711
1712#############################################################################
1713
1714package CFClient::UI::Gauge;
1715
1716our @ISA = CFClient::UI::VBox::;
1717
1718sub new {
1719 my ($class, %arg) = @_;
1720
1721 my $self = $class->SUPER::new (
1722 tooltip => $arg{type},
1723 %arg,
1724 );
1725
1726 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1727 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1728 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1729
1730 $self
1731}
1732
1733sub set_fontsize {
1734 my ($self, $fsize) = @_;
1735
1736 $self->{value}->set_fontsize ($fsize);
1737 $self->{max} ->set_fontsize ($fsize);
1738}
1739
1740sub set_value {
1741 my ($self, $val, $max) = @_;
1742
1743 $self->set_max ($max)
1744 if defined $max;
1745
1746 $self->{gauge}->set_value ($val, $max);
1747 $self->{value}->set_text ($val);
1748}
1749
1750sub set_max {
1751 my ($self, $max) = @_;
1752
1753 $self->{gauge}->set_max ($max);
1754 $self->{max}->set_text ($max);
1755}
1756
1757#############################################################################
1758
1347package CFClient::UI::Slider; 1759package CFClient::UI::Slider;
1348 1760
1349use strict; 1761use strict;
1350 1762
1351use SDL::OpenGL; 1763use CFClient::OpenGL;
1352 1764
1353our @ISA = CFClient::UI::DrawBG::; 1765our @ISA = CFClient::UI::DrawBG::;
1354 1766
1355my @tex = 1767my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1768 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1366 # TODO: calculations are off 1778 # TODO: calculations are off
1367 my $self = $class->SUPER::new ( 1779 my $self = $class->SUPER::new (
1368 fg => [1, 1, 1], 1780 fg => [1, 1, 1],
1369 active_fg => [0, 0, 0], 1781 active_fg => [0, 0, 0],
1370 range => [0, 0, 100, 10], 1782 range => [0, 0, 100, 10],
1371 req_w => 40, 1783 req_w => 20,
1372 req_h => 13, 1784 req_h => 20,
1373 vertical => 0, 1785 vertical => 0,
1374 can_hover => 1, 1786 can_hover => 1,
1375 inner_pad => 5, 1787 inner_pad => 5,
1376 @_ 1788 @_
1377 ); 1789 );
1481 1893
1482package CFClient::UI::TextView; 1894package CFClient::UI::TextView;
1483 1895
1484our @ISA = CFClient::UI::HBox::; 1896our @ISA = CFClient::UI::HBox::;
1485 1897
1486use SDL::OpenGL; 1898use CFClient::OpenGL;
1487 1899
1488sub new { 1900sub new {
1489 my $class = shift; 1901 my $class = shift;
1490 1902
1491 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE, 1904 fontsize => 1,
1495 @_, 1905 @_,
1496 1906
1497 layout => (new CFClient::Layout), 1907 layout => (new CFClient::Layout),
1498 par => [], 1908 par => [],
1499 height => 0, 1909 height => 0,
1520sub text_height { 1930sub text_height {
1521 my ($self, $text) = @_; 1931 my ($self, $text) = @_;
1522 1932
1523 my $layout = $self->{layout}; 1933 my $layout = $self->{layout};
1524 1934
1525 $layout->set_height ($self->{fontsize}); 1935 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1526 $layout->set_width ($self->{w}); 1936 $layout->set_width ($self->{w});
1527 $layout->set_text ($text); 1937 $layout->set_text ($text);
1528 1938
1529 ($layout->size)[1] 1939 ($layout->size)[1]
1530} 1940}
1534 1944
1535 $self->{need_reflow}++; 1945 $self->{need_reflow}++;
1536 $self->update; 1946 $self->update;
1537} 1947}
1538 1948
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate { 1949sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 1950 my ($self, $w, $h) = @_;
1547 1951
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 1952 $self->SUPER::size_allocate ($w, $h);
1549 1953
1550 $self->{layout}->set_height ($self->{fontsize}); 1954 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 $self->{layout}->set_width ($self->{w}); 1955 $self->{layout}->set_width ($self->{children}[0]{w});
1552 1956
1553 $self->reflow; 1957 $self->reflow;
1554} 1958}
1555 1959
1556sub add_paragraph { 1960sub add_paragraph {
1575 1979
1576 return unless $self->{h} > 0; 1980 return unless $self->{h} > 0;
1577 1981
1578 delete $self->{texture}; 1982 delete $self->{texture};
1579 1983
1580 $TOPLEVEL->on_refresh ($self, sub { 1984 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1985 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1986 my $height = 0;
1583 1987
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1988 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1989 for @{$self->{par}};
1643 2047
1644} 2048}
1645 2049
1646############################################################################# 2050#############################################################################
1647 2051
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; 2052package CFClient::UI::Animator;
1883 2053
1884use SDL::OpenGL; 2054use CFClient::OpenGL;
1885 2055
1886our @ISA = CFClient::UI::Bin::; 2056our @ISA = CFClient::UI::Bin::;
1887 2057
1888sub moveto { 2058sub moveto {
1889 my ($self, $x, $y) = @_; 2059 my ($self, $x, $y) = @_;
1929 my $class = shift; 2099 my $class = shift;
1930 2100
1931 my $self = $class->SUPER::new ( 2101 my $self = $class->SUPER::new (
1932 state => 0, 2102 state => 0,
1933 connect_activate => \&toggle_flopper, 2103 connect_activate => \&toggle_flopper,
2104 can_events => 1,
1934 @_ 2105 @_
1935 ); 2106 );
1936 2107
1937 if ($self->{state}) { 2108 if ($self->{state}) {
1938 $self->{state} = 0; 2109 $self->{state} = 0;
1945sub toggle_flopper { 2116sub toggle_flopper {
1946 my ($self) = @_; 2117 my ($self) = @_;
1947 2118
1948 # TODO: use animation 2119 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 2120 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2121 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 2122 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 2123 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 2124 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2125 $CFClient::UI::ROOT->remove ($self->{other});
2126 $self->emit ("close");
1957 } 2127 }
1958}
1959 2128
1960############################################################################# 2129 $self->emit (changed => $self->{state});
2130}
1961 2131
2132#############################################################################
2133
1962package CFClient::UI::Toplevel; 2134package CFClient::UI::Root;
1963 2135
1964our @ISA = CFClient::UI::Container::; 2136our @ISA = CFClient::UI::Container::;
1965 2137
1966use SDL::OpenGL; 2138use CFClient::OpenGL;
2139
2140sub check_size {
2141 my ($self) = @_;
2142
2143 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2144}
1967 2145
1968sub size_request { 2146sub size_request {
1969 ($::WIDTH, $::HEIGHT) 2147 ($::WIDTH, $::HEIGHT)
1970} 2148}
1971 2149
1972sub size_allocate { 2150sub configure {
1973 my ($self, $x, $y, $w, $h) = @_; 2151 my ($self, $x, $y, $w, $h) = @_;
1974 2152
1975 $self->_size_allocate ($x, $y, $w, $h); 2153 $self->SUPER::configure ($x, $y, $w, $h);
1976 2154
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 2155 for my $child (@{$self->{children}}) {
1979} 2156 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1980 2157
1981sub translate { 2158 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2159 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2160 $child->configure ($X, $Y, $W,$H);
2161 }
2162}
2163
2164sub _topleft {
1982 my ($self, $x, $y) = @_; 2165 my ($self, $x, $y) = @_;
1983 2166
1984 ($x, $y) 2167 ($x, $y)
1985} 2168}
1986 2169
1987sub update { 2170sub update {
1988 my ($self) = @_; 2171 my ($self) = @_;
1989 2172
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2173 $self->check_size;
1991 ::refresh (); 2174 ::refresh ();
1992} 2175}
1993 2176
1994sub add { 2177sub add {
1995 my ($self, $widget) = @_; 2178 my ($self, $child) = @_;
1996 2179
2180 # integerize window positions
2181 $child->{x} = int $child->{x};
2182 $child->{y} = int $child->{y};
2183
1997 $self->SUPER::add ($widget); 2184 $self->SUPER::add ($child);
1998
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000} 2185}
2001 2186
2002sub on_refresh { 2187sub on_refresh {
2003 my ($self, $id, $cb) = @_; 2188 my ($self, $id, $cb) = @_;
2004 2189
2027 2212
2028############################################################################# 2213#############################################################################
2029 2214
2030package CFClient::UI; 2215package CFClient::UI;
2031 2216
2032$TOPLEVEL = new CFClient::UI::Toplevel; 2217$ROOT = new CFClient::UI::Root;
2218$TOOLTIP = new CFClient::UI::Label fontsize => 0.8, can_events => 0;
2033 2219
20341 22201
2035 2221

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines