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.150 by elmex, Sun Apr 23 00:08:29 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 $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover && $hover->{can_hover}; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71} 71}
72 72
73# convert position array to integers 73# convert position array to integers
74sub harmonize { 74sub harmonize {
75 my ($vals) = @_; 75 my ($vals) = @_;
76 76
77 my $rem = 0; 77 my $rem = 0;
78 78
79 for ($vals) { 79 for (@$vals) {
80 my $i = int $_ + $rem; 80 my $i = int $_ + $rem;
81 $rem += $_ - $i; 81 $rem += $_ - $i;
82 $_ = $i; 82 $_ = $i;
83 } 83 }
84} 84}
87 87
88package CFClient::UI::Base; 88package CFClient::UI::Base;
89 89
90use strict; 90use strict;
91 91
92use SDL::OpenGL; 92use CFClient::OpenGL;
93 93
94sub new { 94sub new {
95 my $class = shift; 95 my $class = shift;
96 96
97 my $self = bless { 97 my $self = bless {
98 x => 0, 98 x => 0,
99 y => 0, 99 y => 0,
100 z => 0, 100 z => 0,
101 w => -1, 101 can_events => 1,
102 h => -1,
103 @_ 102 @_
104 }, $class; 103 }, $class;
105 104
106 for (keys %$self) { 105 for (keys %$self) {
107 if (/^connect_(.*)$/) { 106 if (/^connect_(.*)$/) {
110 } 109 }
111 110
112 $self 111 $self
113} 112}
114 113
114sub show {
115 my ($self) = @_;
116
117 return if $self->{parent};
118
119 $CFClient::UI::ROOT->add ($self);
120}
121
122sub hide {
123 my ($self) = @_;
124
125 return unless $self->{parent};
126
127 $self->{parent}->remove ($self);
128}
129
115sub move { 130sub move {
116 my ($self, $x, $y, $z) = @_; 131 my ($self, $x, $y, $z) = @_;
132
117 $self->{x} = $x; 133 $self->{x} = int $x;
118 $self->{y} = $y; 134 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 135 $self->{z} = $z if defined $z;
136
137 $self->update;
120} 138}
121 139
122sub needs_redraw { 140sub needs_redraw {
123 0 141 0
124} 142}
125 143
126sub size_request { 144sub size_request {
127 require Carp; 145 require Carp;
128 Carp::confess "size_request is abtract"; 146 Carp::confess "size_request is abstract";
129} 147}
130 148
131sub _size_allocate { 149sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 150 my ($self, $x, $y, $w, $h) = @_;
133 151
152 if ($self->{aspect}) {
153 my $w2 = List::Util::min $w, int $h * $self->{aspect};
154 my $h2 = List::Util::min $h, int $w / $self->{aspect};
155
156 # use alignment to adjust x, y
157
158 $x += int +($w - $w2) * 0.5;
159 $y += int +($h - $h2) * 0.5;
160
161 ($w, $h) = ($w2, $h2);
162 }
163
164 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 165 $self->{x} = $x;
135 $self->{y} = $y; 166 $self->{y} = $y;
167 $self->update;
168 }
136 169
137 return unless $self->{w} != $w || $self->{h} != $h; 170 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 171 $self->{w} = $w;
140 $self->{h} = $h; 172 $self->{h} = $h;
141 173
174 $self->size_allocate ($w, $h);
175 $self->update;
142 1 176 }
143} 177}
144 178
145sub size_allocate { 179sub size_allocate {
180 # nothing to be done
181}
182
183# return top left coordinates
184sub _topleft {
146 my ($self, $x, $y, $w, $h) = @_; 185 my ($self, $x, $y) = @_;
147 186
148 $self->_size_allocate ($x, $y, $w, $h); 187 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
149} 188}
150 189
151# translate global coordinates to local coordinate system 190# translate global coordinates to local coordinate system
152sub translate { 191sub coord2local {
153 my ($self, $x, $y) = @_; 192 my ($self, $x, $y) = @_;
154 193
155 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 194 my ($X, $Y) = $self->_topleft;
195 ($x - $X, $y - $Y)
196}
197
198# translate local coordinates to global coordinate system
199sub coord2global {
200 my ($self, $x, $y) = @_;
201
202 my ($X, $Y) = $self->_topleft;
203 ($x + $X, $y + $Y)
156} 204}
157 205
158sub focus_in { 206sub focus_in {
159 my ($self) = @_; 207 my ($self) = @_;
160 208
161 return if $FOCUS == $self; 209 return if $FOCUS == $self;
162 return unless $self->{can_focus}; 210 return unless $self->{can_focus};
163 211
164 my $focus = $FOCUS; $FOCUS = $self; 212 my $focus = $FOCUS; $FOCUS = $self;
213
214 $self->emit (focus_in => $focus);
215
165 $focus->update if $focus; 216 $focus->update if $focus;
166 $FOCUS->update; 217 $FOCUS->update;
167} 218}
168 219
169sub focus_out { 220sub focus_out {
170 my ($self) = @_; 221 my ($self) = @_;
171 222
172 return unless $FOCUS == $self; 223 return unless $FOCUS == $self;
173 224
174 my $focus = $FOCUS; undef $FOCUS; 225 my $focus = $FOCUS; undef $FOCUS;
226
227 $self->emit (focus_out => $focus);
228
175 $focus->update if $focus; #? 229 $focus->update if $focus; #?
176} 230}
177 231
178sub mouse_motion { } 232sub mouse_motion { }
179sub button_up { } 233sub button_up { }
203 glPopMatrix; 257 glPopMatrix;
204 258
205 if ($self == $HOVER && $self->{can_hover}) { 259 if ($self == $HOVER && $self->{can_hover}) {
206 my ($x, $y) = @$self{qw(x y)}; 260 my ($x, $y) = @$self{qw(x y)};
207 261
208 glColor 0, 0, 1, 0.2; 262 glColor 1, 0.8, 0.5, 0.2;
209 glEnable GL_BLEND; 263 glEnable GL_BLEND;
210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 264 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
211 glBegin GL_QUADS; 265 glBegin GL_QUADS;
212 glVertex $x , $y; 266 glVertex $x , $y;
213 glVertex $x + $self->{w}, $y; 267 glVertex $x + $self->{w}, $y;
225} 279}
226 280
227sub find_widget { 281sub find_widget {
228 my ($self, $x, $y) = @_; 282 my ($self, $x, $y) = @_;
229 283
284 return () unless $self->{can_events};
285
230 return $self 286 return $self
231 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 287 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
232 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 288 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
233 289
234 () 290 ()
235} 291}
236 292
237sub set_parent { 293sub set_parent {
238 my ($self, $par) = @_; 294 my ($self, $parent) = @_;
239 295
240 $self->{parent} = $par;
241 Scalar::Util::weaken $self->{parent}; 296 Scalar::Util::weaken ($self->{parent} = $parent);
242} 297}
243 298
244sub get_parent { 299sub check_size {
245 $_[0]->{parent} 300 my ($self) = @_;
301
302 return unless $self->{parent};
303
304 my ($w, $h) = $self->size_request;
305
306 if ($w != $self->{req_w} || $h != $self->{req_h}) {
307 $self->{req_w} = $w;
308 $self->{req_h} = $h;
309
310 $self->{parent}->check_size;
311 }
246} 312}
247 313
248sub update { 314sub update {
249 my ($self) = @_; 315 my ($self) = @_;
250 316
253} 319}
254 320
255sub connect { 321sub connect {
256 my ($self, $signal, $cb) = @_; 322 my ($self, $signal, $cb) = @_;
257 323
258 push @{ $self->{cb}{$signal} }, $cb; 324 push @{ $self->{signal_cb}{$signal} }, $cb;
259} 325}
260 326
261sub emit { 327sub emit {
262 my ($self, $signal, @args) = @_; 328 my ($self, $signal, @args) = @_;
263 329
330 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
264 $_->($self, @args) 331 $cb->($self, @args);
265 for @{$self->{cb}{$signal} || []}; 332 }
266} 333}
267 334
268sub DESTROY { 335sub DESTROY {
269 my ($self) = @_; 336 my ($self) = @_;
270 337
276package CFClient::UI::DrawBG; 343package CFClient::UI::DrawBG;
277 344
278our @ISA = CFClient::UI::Base::; 345our @ISA = CFClient::UI::Base::;
279 346
280use strict; 347use strict;
281use SDL::OpenGL; 348use CFClient::OpenGL;
282 349
283sub new { 350sub new {
284 my $class = shift; 351 my $class = shift;
285 352
286 # range [value, low, high, page] 353 # range [value, low, high, page]
315 382
316package CFClient::UI::Empty; 383package CFClient::UI::Empty;
317 384
318our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
319 386
387sub new {
388 my ($class, %arg) = @_;
389 $class->SUPER::new (can_events => 0, %arg);
390}
391
320sub size_request { 392sub size_request {
321 (0, 0) 393 (0, 0)
322} 394}
323 395
324sub draw { } 396sub draw { }
332sub new { 404sub new {
333 my ($class, %arg) = @_; 405 my ($class, %arg) = @_;
334 406
335 my $children = delete $arg{children} || []; 407 my $children = delete $arg{children} || [];
336 408
337 my $self = $class->SUPER::new (children => [], %arg); 409 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
338 $self->add ($_) for @$children; 410 $self->add ($_) for @$children;
339 411
340 $self 412 $self
341} 413}
342 414
343sub add { 415sub add {
344 my ($self, $chld) = @_; 416 my ($self, $child) = @_;
345 417
346 $chld->set_parent ($self); 418 $child->set_parent ($self);
419
420 use sort 'stable';
347 421
348 $self->{children} = [ 422 $self->{children} = [
349 sort { $a->{z} <=> $b->{z} } 423 sort { $a->{z} <=> $b->{z} }
350 @{$self->{children}}, $chld 424 @{$self->{children}}, $child
351 ]; 425 ];
352 426
353 $self->{w} = $self->{h} = -1; 427 $child->check_size;
354 $self->update;
355} 428}
356 429
357sub remove { 430sub remove {
358 my ($self, $widget) = @_; 431 my ($self, $child) = @_;
359 432
433 delete $child->{parent};
434
360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 435 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
361 436
362 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 437 $self->check_size;
363} 438}
364 439
365sub find_widget { 440sub find_widget {
366 my ($self, $x, $y) = @_; 441 my ($self, $x, $y) = @_;
367 442
397 472
398 $class->SUPER::new (children => [$child], %arg) 473 $class->SUPER::new (children => [$child], %arg)
399} 474}
400 475
401sub add { 476sub add {
402 my ($self, $widget) = @_; 477 my ($self, $child) = @_;
403 478
404 $self->{children} = []; 479 $self->{children} = [];
405 480
406 $self->SUPER::add ($widget); 481 $self->SUPER::add ($child);
407} 482}
408 483
409sub remove { 484sub remove {
410 my ($self, $widget) = @_; 485 my ($self, $widget) = @_;
411 486
420sub size_request { 495sub size_request {
421 $_[0]{children}[0]->size_request 496 $_[0]{children}[0]->size_request
422} 497}
423 498
424sub size_allocate { 499sub size_allocate {
425 my ($self, $x, $y, $w, $h) = @_; 500 my ($self, $w, $h) = @_;
426 501
427 $self->_size_allocate ($x, $y, $w, $h) or return;
428
429 $self->{children}[0]->size_allocate (0, 0, $w, $h); 502 $self->{children}[0]->configure (0, 0, $w, $h);
430} 503}
431 504
432############################################################################# 505#############################################################################
433 506
434package CFClient::UI::Window; 507package CFClient::UI::Window;
435 508
436our @ISA = CFClient::UI::Bin::; 509our @ISA = CFClient::UI::Bin::;
437 510
438use SDL::OpenGL; 511use CFClient::OpenGL;
439 512
440sub new { 513sub new {
441 my ($class, %arg) = @_; 514 my ($class, %arg) = @_;
442 515
443 my $self = $class->SUPER::new (%arg); 516 my $self = $class->SUPER::new (%arg);
460 $self->child->draw; 533 $self->child->draw;
461 }; 534 };
462} 535}
463 536
464sub size_allocate { 537sub size_allocate {
465 my ($self, $x, $y, $w, $h) = @_; 538 my ($self, $w, $h) = @_;
466 539
467 $self->_size_allocate ($x, $y, $w, $h) or return;
468
469 $self->child->size_allocate (0, 0, $w, $h); 540 $self->child->configure (0, 0, $w, $h);
470 541
471 $self->render_chld; 542 $self->render_chld;
472} 543}
473 544
474sub _draw { 545sub _draw {
490 glDisable GL_TEXTURE_2D; 561 glDisable GL_TEXTURE_2D;
491} 562}
492 563
493############################################################################# 564#############################################################################
494 565
566package CFClient::UI::ViewPort;
567
568our @ISA = CFClient::UI::Window::;
569
570sub new { die }
571
572sub size_request {
573 my ($self) = @_;
574
575 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
576 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
577
578 @$self{qw(child_w child_h)}
579}
580
581sub _draw {
582 my ($self) = @_;
583
584 $self->{children}[1]->draw;
585}
586
587
588#############################################################################
589
495package CFClient::UI::Frame; 590package CFClient::UI::Frame;
496 591
497our @ISA = CFClient::UI::Bin::; 592our @ISA = CFClient::UI::Bin::;
498 593
499use SDL::OpenGL; 594use 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 595
549sub new { 596sub new {
550 my $class = shift; 597 my $class = shift;
551 598
552 # TODO: user_x, user_y, overwrite moveto?
553
554 $class->SUPER::new ( 599 my $self = $class->SUPER::new (
555 bg => [1, 1, 1, 1], 600 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 601 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 602 border => 0.8,
558 @_ 603 @_
604 );
605
606 $self
607}
608
609sub set_size {
610 my ($self, $w, $h) = @_;
611 $self->{req_w} = $w;
612 $self->{req_h} = $h;
613 $self->check_size;
614}
615
616sub size_request {
617 my ($self) = @_;
618 ($self->{req_w}, $self->{req_h})
619}
620
621sub size_allocate {
622 my ($self, $w, $h) = @_;
623 $self->{w} = $w;
624 $self->{h} = $h;
625 $self->child->configure (0, 0, $w, $h);
626}
627
628sub _draw {
629 my ($self) = @_;
630
631 my ($w, $h) = ($self->{w}, $self->{h});
632
633 glEnable GL_BLEND;
634 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
635 glEnable GL_TEXTURE_2D;
636 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
637
638# glBegin GL_QUADS;
639# glColor 0, 0, 0, 0;
640# glVertex 0 , 0;
641# glVertex 0 , $h;
642# glVertex $w, $h;
643# glVertex $w, 0;
644# glEnd;
645
646
647 $self->child->draw;
648 glDisable GL_BLEND;
649 glDisable GL_TEXTURE_2D;
650}
651
652#############################################################################
653
654package CFClient::UI::FancyFrame;
655
656our @ISA = CFClient::UI::Bin::;
657
658use CFClient::OpenGL;
659
660my @tex =
661 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
662 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
663
664sub new {
665 my $class = shift;
666
667 # TODO: user_x, user_y, overwrite moveto?
668
669 my $self = $class->SUPER::new (
670 bg => [1, 1, 1, 1],
671 border_bg => [1, 1, 1, 1],
672 border => 0.8,
673 can_events => 1,
674 @_
675 );
676
677 $self->{title} &&= new CFClient::UI::Label
678 align => 0,
679 valign => 1,
680 text => $self->{title},
681 fontsize => 1;
682
683 $self
684}
685
686sub border {
687 int $_[0]{border} * $::FONTSIZE
688}
689
690sub size_request {
691 my ($self) = @_;
692
693 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
694
695 my ($w, $h) = $self->SUPER::size_request;
696
697 (
698 $w + $self->border * 2,
699 $h + $self->border * 2,
559 ) 700 )
560} 701}
561 702
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 { 703sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_; 704 my ($self, $w, $h) = @_;
577 705
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $h -= List::Util::max 0, $self->{border} * 2; 706 $h -= List::Util::max 0, $self->border * 2;
581 $w -= List::Util::max 0, $self->{border} * 2; 707 $w -= List::Util::max 0, $self->border * 2;
582 708
709 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
710 if $self->{title};
711
583 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 712 $self->child->configure ($self->border, $self->border, $w, $h);
584} 713}
585 714
586sub button_down { 715sub button_down {
587 my ($self, $ev, $x, $y) = @_; 716 my ($self, $ev, $x, $y) = @_;
588 717
718 my $border = $self->border;
719
589 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 720 if ($x < $self->{w} && $x >= $self->{w} - $border
590 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 721 && $y < $self->{h} && $y >= $self->{h} - $border) {
591 722
592 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 723 my ($ox, $oy) = ($ev->{x}, $ev->{y});
593 my ($bw, $bh) = ($self->{w}, $self->{h}); 724 my ($bw, $bh) = ($self->{w}, $self->{h});
594 725
595 $self->{motion} = sub { 726 $self->{motion} = sub {
596 my ($ev, $x, $y) = @_; 727 my ($ev, $x, $y) = @_;
597 728
598 ($x, $y) = ($ev->motion_x, $ev->motion_y); 729 ($x, $y) = ($ev->{x}, $ev->{y});
599 730
600 $self->{user_w} = $bw + $x - $ox; 731 $self->{user_w} = $bw + $x - $ox;
601 $self->{user_h} = $bh + $y - $oy; 732 $self->{user_h} = $bh + $y - $oy;
602 $self->update; 733 $self->check_size;
603 }; 734 };
604 735
605 } elsif ($x >= 0 && $x < $self->{w} 736 } elsif ($x >= 0 && $x < $self->{w}
606 && $y >= 0 && $y < $self->{border}) { 737 && $y >= 0 && $y < $border) {
607 738
608 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 739 my ($ox, $oy) = ($ev->{x}, $ev->{y});
609 my ($bx, $by) = ($self->{x}, $self->{y}); 740 my ($bx, $by) = ($self->{x}, $self->{y});
610 741
611 $self->{motion} = sub { 742 $self->{motion} = sub {
612 my ($ev, $x, $y) = @_; 743 my ($ev, $x, $y) = @_;
613 744
614 ($x, $y) = ($ev->motion_x, $ev->motion_y); 745 ($x, $y) = ($ev->{x}, $ev->{y});
615 746
616 $self->move ($bx + $x - $ox, $by + $y - $oy); 747 $self->move ($bx + $x - $ox, $by + $y - $oy);
617 $self->update; 748 $self->update;
618 }; 749 };
619 } 750 }
640 glEnable GL_BLEND; 771 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 772 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D; 773 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 774 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
644 775
776 my $border = $self->border;
777
645 glColor @{ $self->{border_bg} }; 778 glColor @{ $self->{border_bg} };
646 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 779 $tex[1]->draw_quad (0, 0, $w, $border);
647 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 780 $tex[3]->draw_quad (0, $border, $border, $ch);
648 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 781 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
649 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 782 $tex[4]->draw_quad (0, $h - $border, $w, $border);
650 783
651 my $bg = $tex[0]; 784 my $bg = $tex[0];
652 785
653 # TODO: repeat texture not scale 786 # TODO: repeat texture not scale
654 my $rep_x = $cw / $bg->{w}; 787 my $rep_x = $cw / $bg->{w};
657 glColor @{ $self->{bg} }; 790 glColor @{ $self->{bg} };
658 791
659 $bg->{s} = $rep_x; 792 $bg->{s} = $rep_x;
660 $bg->{t} = $rep_y; 793 $bg->{t} = $rep_y;
661 $bg->{wrap_mode} = 1; 794 $bg->{wrap_mode} = 1;
662 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 795 $bg->draw_quad ($border, $border, $cw, $ch);
663 796
664 glDisable GL_TEXTURE_2D; 797 glDisable GL_TEXTURE_2D;
665 glDisable GL_BLEND; 798 glDisable GL_BLEND;
666 799
800 $self->{title}->draw if $self->{title};
667 $self->child->draw; 801 $self->child->draw;
668} 802}
669 803
670############################################################################# 804#############################################################################
671 805
673 807
674our @ISA = CFClient::UI::Base::; 808our @ISA = CFClient::UI::Base::;
675 809
676use List::Util qw(max sum); 810use List::Util qw(max sum);
677 811
678use SDL::OpenGL; 812use CFClient::OpenGL;
679 813
680sub new { 814sub new {
681 my $class = shift; 815 my $class = shift;
682 816
683 $class->SUPER::new ( 817 $class->SUPER::new (
685 @_ 819 @_
686 ) 820 )
687} 821}
688 822
689sub add { 823sub add {
690 my ($self, $x, $y, $chld) = @_; 824 my ($self, $x, $y, $child) = @_;
691 825
826 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 827 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 828
695 $self->{w} = $self->{h} = -1; 829 $child->check_size;
830}
831
832# TODO: move to container class maybe? send childs a signal on removal?
833sub clear {
834 my ($self) = @_;
835
836 delete $self->{children};
696 $self->update; 837 $self->update;
697} 838}
698 839
699sub get_wh { 840sub get_wh {
700 my ($self) = @_; 841 my ($self) = @_;
706 or next; 847 or next;
707 848
708 for my $x (0 .. $#$row) { 849 for my $x (0 .. $#$row) {
709 my $widget = $row->[$x] 850 my $widget = $row->[$x]
710 or next; 851 or next;
711 my ($w, $h) = $widget->size_request; 852 my ($w, $h) = @$widget{qw(req_w req_h)};
712 853
713 $w[$x] = max $w[$x], $w; 854 $w[$x] = max $w[$x], $w;
714 $h[$y] = max $h[$y], $h; 855 $h[$y] = max $h[$y], $h;
715 } 856 }
716 } 857 }
728 (sum @$hs), 869 (sum @$hs),
729 ) 870 )
730} 871}
731 872
732sub size_allocate { 873sub size_allocate {
733 my ($self, $x, $y, $w, $h) = @_; 874 my ($self, $w, $h) = @_;
734
735 $self->_size_allocate ($x, $y, $w, $h) or return;
736 875
737 my ($ws, $hs) = $self->get_wh; 876 my ($ws, $hs) = $self->get_wh;
738 877
739 my $req_w = sum @$ws; 878 my $req_w = sum @$ws;
740 my $req_h = sum @$hs; 879 my $req_h = sum @$hs;
762 901
763 for my $c (0 .. $#$row) { 902 for my $c (0 .. $#$row) {
764 my $col_w = $ws->[$c]; 903 my $col_w = $ws->[$c];
765 904
766 if (my $widget = $row->[$c]) { 905 if (my $widget = $row->[$c]) {
767 $widget->size_allocate ($x, $y, $col_w, $row_h); 906 $widget->configure ($x, $y, $col_w, $row_h);
768 } 907 }
769 908
770 $x += $col_w; 909 $x += $col_w;
771 } 910 }
772 911
817 (List::Util::max map $_->[1], @alloc), 956 (List::Util::max map $_->[1], @alloc),
818 ) 957 )
819} 958}
820 959
821sub size_allocate { 960sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_; 961 my ($self, $w, $h) = @_;
823
824 $self->_size_allocate ($x, $y, $w, $h);
825 962
826 ($h, $w) = ($w, $h); 963 ($h, $w) = ($w, $h);
827 964
828 my $children = $self->{children}; 965 my $children = $self->{children};
829 966
830 my @h = map +($_->size_request)[0], @$children; 967 my @h = map $_->{req_w}, @$children;
831 968
832 my $req_h = List::Util::sum @h; 969 my $req_h = List::Util::sum @h;
833 970
834 if ($req_h > $h) { 971 if ($req_h > $h) {
835 # ah well, not enough space 972 # ah well, not enough space
851 988
852 my $y = 0; 989 my $y = 0;
853 for (0 .. $#$children) { 990 for (0 .. $#$children) {
854 my $child = $children->[$_]; 991 my $child = $children->[$_];
855 my $h = $h[$_]; 992 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w); 993 $child->configure ($y, 0, $h, $w);
857 994
858 $y += $h; 995 $y += $h;
859 } 996 }
997
998 1
860} 999}
861 1000
862############################################################################# 1001#############################################################################
863 1002
864package CFClient::UI::VBox; 1003package CFClient::UI::VBox;
877 (List::Util::sum map $_->[1], @alloc), 1016 (List::Util::sum map $_->[1], @alloc),
878 ) 1017 )
879} 1018}
880 1019
881sub size_allocate { 1020sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_; 1021 my ($self, $w, $h) = @_;
883
884 $self->_size_allocate ($x, $y, $w, $h);
885 1022
886 my $children = $self->{children}; 1023 my $children = $self->{children};
887 1024
888 my @h = map +($_->size_request)[1], @$children; 1025 my @h = map $_->{req_h}, @$children;
889 1026
890 my $req_h = List::Util::sum @h; 1027 my $req_h = List::Util::sum @h;
891 1028
892 if ($req_h > $h) { 1029 if ($req_h > $h) {
893 # ah well, not enough space 1030 # ah well, not enough space
907 1044
908 my $y = 0; 1045 my $y = 0;
909 for (0 .. $#$children) { 1046 for (0 .. $#$children) {
910 my $child = $children->[$_]; 1047 my $child = $children->[$_];
911 my $h = $h[$_]; 1048 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h); 1049 $child->configure (0, $y, $w, $h);
913 1050
914 $y += $h; 1051 $y += $h;
915 } 1052 }
1053
1054 1
916} 1055}
917 1056
918############################################################################# 1057#############################################################################
919 1058
920package CFClient::UI::Label; 1059package CFClient::UI::Label;
921 1060
922our @ISA = CFClient::UI::Base::; 1061our @ISA = CFClient::UI::Base::;
923 1062
924use SDL::OpenGL; 1063use CFClient::OpenGL;
925 1064
926sub new { 1065sub new {
927 my ($class, %arg) = @_; 1066 my ($class, %arg) = @_;
928 1067
929 my $self = $class->SUPER::new ( 1068 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 1069 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE, 1070 fontsize => 1,
932 text => "", 1071 text => "",
933 align => -1, 1072 align => -1,
1073 valign => -1,
934 padding => 2, 1074 padding => 2,
935 layout => new CFClient::Layout, 1075 layout => new CFClient::Layout,
1076 can_events => 0,
936 %arg 1077 %arg
937 ); 1078 );
938 1079
939 $self->set_text ($self->{text}); 1080 if (exists $self->{template}) {
1081 my $layout = new CFClient::Layout;
1082 $layout->set_text (delete $self->{template});
1083 $self->{template} = $layout;
1084 }
1085
1086 $self->set_text (delete $self->{text}) if exists $self->{text};
1087 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
940 1088
941 $self 1089 $self
942} 1090}
943 1091
944sub escape_text { 1092sub escape_text {
952} 1100}
953 1101
954sub set_text { 1102sub set_text {
955 my ($self, $text) = @_; 1103 my ($self, $text) = @_;
956 1104
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text); 1105 $self->{layout}->set_text ($text);
959 1106
960 delete $self->{texture}; 1107 delete $self->{texture};
961# $self->{w} = $self->{h} = -1; 1108 $self->check_size;
962 $self->update; 1109 $self->update;
963} 1110}
964 1111
965sub get_text { 1112sub set_markup {
966 my ($self, $text) = @_; 1113 my ($self, $markup) = @_;
967 1114
968 $self->{text} 1115 $self->{layout}->set_markup ($markup);
1116
1117 delete $self->{texture};
1118 $self->check_size;
1119 $self->update;
969} 1120}
970 1121
971sub size_request { 1122sub size_request {
972 my ($self) = @_; 1123 my ($self) = @_;
973 1124
974 $self->{layout}->set_width; 1125 $self->{layout}->set_width;
975 $self->{layout}->set_height ($self->{fontsize}); 1126 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1127
976 my ($w, $h) = $self->{layout}->size; 1128 my ($w, $h) = $self->{layout}->size;
1129
1130 if (exists $self->{template}) {
1131 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1132
1133 my ($w2, $h2) = $self->{template}->size;
1134
1135 $w = List::Util::max $w, $w2;
1136 $h = List::Util::max $h, $h2;
1137 }
977 1138
978 ( 1139 (
979 $w + $self->{padding} * 2, 1140 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2, 1141 $h + $self->{padding} * 2,
981 ) 1142 )
982} 1143}
983 1144
984sub size_allocate { 1145sub size_allocate {
985 my ($self, $x, $y, $w, $h) = @_; 1146 my ($self, $w, $h) = @_;
986
987 $self->_size_allocate ($x, $y, $w, $h) or return;
988 1147
989 delete $self->{texture}; 1148 delete $self->{texture};
990} 1149}
991 1150
992sub update { 1151sub set_fontsize {
993 my ($self) = @_; 1152 my ($self, $fontsize) = @_;
994 1153
995 delete $self->{texture}; 1154 $self->{fontsize} = $fontsize;
996 $self->SUPER::update; 1155 $self->check_size;
997} 1156}
998 1157
999sub _draw { 1158sub _draw {
1000 my ($self) = @_; 1159 my ($self) = @_;
1001 1160
1002 my $tex = $self->{texture} ||= do { 1161 my $tex = $self->{texture} ||= do {
1003 $self->{layout}->set_width ($self->{w}); 1162 $self->{layout}->set_width ($self->{w});
1004 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1163 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1005 new_from_layout CFClient::Texture $self->{layout} 1164 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1165 };
1007 1166
1008 glEnable GL_BLEND; 1167 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1168 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1169 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1171
1013 glColor @{$self->{fg}}; 1172 glColor @{$self->{fg}};
1014 1173
1015 my $x = 1174 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1175 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1176 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1177 : ($self->{w} - $tex->{w}) * 0.5
1178 );
1019 1179
1020 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1180 $self->{oy} = int (
1181 $self->{valign} < 0 ? $self->{padding}
1182 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1183 : ($self->{h} - $tex->{h}) * 0.5
1184 );
1185
1186 $tex->draw_quad ($self->{ox}, $self->{oy});
1021 1187
1022 glDisable GL_TEXTURE_2D; 1188 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1189 glDisable GL_BLEND;
1024} 1190}
1025 1191
1026############################################################################# 1192#############################################################################
1027 1193
1028package CFClient::UI::Entry; 1194package CFClient::UI::EntryBase;
1029 1195
1030our @ISA = CFClient::UI::Label::; 1196our @ISA = CFClient::UI::Label::;
1031 1197
1032use SDL; 1198use CFClient::OpenGL;
1033use SDL::OpenGL;
1034 1199
1035sub new { 1200sub new {
1036 my $class = shift; 1201 my $class = shift;
1037 1202
1038 $class->SUPER::new ( 1203 $class->SUPER::new (
1040 bg => [0, 0, 0, 0.2], 1205 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1206 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1207 active_fg => [0, 0, 0],
1043 can_hover => 1, 1208 can_hover => 1,
1044 can_focus => 1, 1209 can_focus => 1,
1210 valign => 0,
1211 can_events => 1,
1045 @_ 1212 @_
1046 ) 1213 )
1047} 1214}
1048 1215
1049sub _set_text { 1216sub _set_text {
1050 my ($self, $text) = @_; 1217 my ($self, $text) = @_;
1051 1218
1052 my $old_text = $self->{text}; 1219 delete $self->{cur_h};
1220
1221 return if $self->{text} eq $text;
1222
1223 delete $self->{texture};
1053 1224
1054 $self->{last_activity} = $::NOW; 1225 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1226 $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 1227
1060 $text =~ s/./*/g if $self->{hidden}; 1228 $text =~ s/./*/g if $self->{hidden};
1229 $self->{layout}->set_text ("$text ");
1061 1230
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1231 $self->emit (changed => $self->{text});
1232}
1063 1233
1064 $text = substr $text, 0, $self->{cursor}; 1234sub get_text {
1065 utf8::encode $text; 1235 $_[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} 1236}
1072 1237
1073sub size_request { 1238sub size_request {
1074 my ($self) = @_; 1239 my ($self) = @_;
1075 1240
1077 1242
1078 ($w + 1, $h) # add 1 for cursor 1243 ($w + 1, $h) # add 1 for cursor
1079} 1244}
1080 1245
1081sub size_allocate { 1246sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1247 my ($self, $w, $h) = @_;
1083 1248
1084 $self->SUPER::size_allocate ($x, $y, $w, $h); 1249 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1085
1086 $self->_set_text ($self->{text});
1087} 1250}
1088 1251
1089sub set_text { 1252sub set_text {
1090 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1091 1254
1095} 1258}
1096 1259
1097sub key_down { 1260sub key_down {
1098 my ($self, $ev) = @_; 1261 my ($self, $ev) = @_;
1099 1262
1100 my $mod = $ev->key_mod; 1263 my $mod = $ev->{mod};
1101 my $sym = $ev->key_sym; 1264 my $sym = $ev->{sym};
1102
1103 my $uni = $ev->key_unicode; 1265 my $uni = $ev->{unicode};
1104 1266
1105 my $text = $self->get_text; 1267 my $text = $self->get_text;
1106 1268
1107 if ($sym == SDLK_BACKSPACE) { 1269 if ($sym == 8) {
1108 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1270 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1109 } elsif ($sym == SDLK_DELETE) { 1271 } elsif ($sym == 127) {
1110 substr $text, $self->{cursor}, 1, ""; 1272 substr $text, $self->{cursor}, 1, "";
1111 } elsif ($sym == SDLK_LEFT) { 1273 } elsif ($sym == CFClient::SDLK_LEFT) {
1112 --$self->{cursor} if $self->{cursor}; 1274 --$self->{cursor} if $self->{cursor};
1113 } elsif ($sym == SDLK_RIGHT) { 1275 } elsif ($sym == CFClient::SDLK_RIGHT) {
1114 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1276 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) { 1277 } elsif ($sym == CFClient::SDLK_HOME) {
1116 $self->{cursor} = 0; 1278 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) { 1279 } elsif ($sym == CFClient::SDLK_END) {
1118 $self->{cursor} = length $text; 1280 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) { 1281 } elsif ($sym == 27) {
1120 $self->emit ('escape'); 1282 $self->emit ('escape');
1121 } elsif ($uni) { 1283 } elsif ($uni) {
1122 substr $text, $self->{cursor}++, 0, chr $uni; 1284 substr $text, $self->{cursor}++, 0, chr $uni;
1123 } 1285 }
1124 1286
1179 1341
1180 $self->SUPER::_draw; 1342 $self->SUPER::_draw;
1181 1343
1182 #TODO: force update every cursor change :( 1344 #TODO: force update every cursor change :(
1183 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1345 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1346
1347 unless (exists $self->{cur_h}) {
1348 my $text = substr $self->{text}, 0, $self->{cursor};
1349 utf8::encode $text;
1350
1351 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1352 }
1353
1184 glColor @{$self->{fg}}; 1354 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1355 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1356 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1357 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1188 glEnd; 1358 glEnd;
1189 } 1359 }
1190} 1360}
1191 1361
1192package CFClient::UI::LineEntry; 1362package CFClient::UI::Entry;
1193 1363
1194our @ISA = CFClient::UI::Entry::; 1364our @ISA = CFClient::UI::EntryBase::;
1195 1365
1196use SDL; 1366use CFClient::OpenGL;
1197use SDL::OpenGL;
1198 1367
1199sub key_down { 1368sub key_down {
1200 my ($self, $ev) = @_; 1369 my ($self, $ev) = @_;
1201 1370
1202 my $sym = $ev->key_sym; 1371 my $sym = $ev->{sym};
1203 1372
1204 if ($sym == SDLK_RETURN) { 1373 if ($sym == 13) {
1205 $self->emit (activate => $self->get_text); 1374 $self->emit (activate => $self->get_text);
1206 $self->update; 1375 $self->update;
1207 1376
1208 } else { 1377 } else {
1209 $self->SUPER::key_down ($ev); 1378 $self->SUPER::key_down ($ev);
1215 1384
1216package CFClient::UI::Button; 1385package CFClient::UI::Button;
1217 1386
1218our @ISA = CFClient::UI::Label::; 1387our @ISA = CFClient::UI::Label::;
1219 1388
1220use SDL; 1389use CFClient::OpenGL;
1221use SDL::OpenGL;
1222 1390
1223my @tex = 1391my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1392 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1225 qw(b1_button_active.png); 1393 qw(b1_button_active.png);
1226 1394
1227sub new { 1395sub new {
1228 my $class = shift; 1396 my $class = shift;
1229 1397
1230 $class->SUPER::new ( 1398 $class->SUPER::new (
1231 padding => 4, 1399 padding => 4,
1232 fg => [1, 1, 1], 1400 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1401 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1402 active_fg => [0, 0, 1],
1235 can_hover => 1, 1403 can_hover => 1,
1404 align => 0,
1405 valign => 0,
1406 can_events => 1,
1236 @_ 1407 @_
1237 ) 1408 )
1238} 1409}
1239 1410
1240sub button_up { 1411sub button_up {
1248 1419
1249sub _draw { 1420sub _draw {
1250 my ($self) = @_; 1421 my ($self) = @_;
1251 1422
1252 local $self->{fg} = $self->{fg}; 1423 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 1424
1259 if ($GRAB == $self) { 1425 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1426 $self->{fg} = $self->{active_fg};
1261 } 1427 }
1262 1428
1263 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1429 glEnable GL_BLEND;
1430 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1431 glEnable GL_TEXTURE_2D;
1264 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1432 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1433 glColor 0, 0, 0, 1;
1265 1434
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1435 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1436
1268 glDisable GL_TEXTURE_2D; 1437 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1438 glDisable GL_BLEND;
1270 1439
1271 $self->SUPER::_draw; 1440 $self->SUPER::_draw;
1276package CFClient::UI::CheckBox; 1445package CFClient::UI::CheckBox;
1277 1446
1278our @ISA = CFClient::UI::DrawBG::; 1447our @ISA = CFClient::UI::DrawBG::;
1279 1448
1280my @tex = 1449my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1451 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283 1452
1284use SDL; 1453use CFClient::OpenGL;
1285use SDL::OpenGL;
1286 1454
1287sub new { 1455sub new {
1288 my $class = shift; 1456 my $class = shift;
1289 1457
1290 $class->SUPER::new ( 1458 $class->SUPER::new (
1301 my ($self) = @_; 1469 my ($self) = @_;
1302 1470
1303 ($self->{padding} * 2 + 6) x 2 1471 ($self->{padding} * 2 + 6) x 2
1304} 1472}
1305 1473
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down { 1474sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1475 my ($self, $ev, $x, $y) = @_;
1314 1476
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1477 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1478 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1342 glDisable GL_BLEND; 1504 glDisable GL_BLEND;
1343} 1505}
1344 1506
1345############################################################################# 1507#############################################################################
1346 1508
1509package CFClient::UI::Image;
1510
1511our @ISA = CFClient::UI::Base::;
1512
1513use CFClient::OpenGL;
1514use Carp qw/confess/;
1515
1516our %loaded_images;
1517
1518sub new {
1519 my $class = shift;
1520
1521 my $self = $class->SUPER::new (can_events => 0, @_);
1522
1523 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1524
1525 $loaded_images{$self->{image}} ||=
1526 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1527
1528 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1529
1530 Scalar::Util::weaken $loaded_images{$self->{image}};
1531
1532 $self->{aspect} = $tex->{w} / $tex->{h};
1533
1534 $self
1535}
1536
1537sub size_request {
1538 my ($self) = @_;
1539
1540 ($self->{tex}->{w}, $self->{tex}->{h})
1541}
1542
1543sub _draw {
1544 my ($self) = @_;
1545
1546 my $tex = $self->{tex};
1547
1548 my ($w, $h) = ($self->{w}, $self->{h});
1549
1550 if ($self->{rot90}) {
1551 glRotate 90, 0, 0, 1;
1552 glTranslate 0, -$self->{w}, 0;
1553
1554 ($w, $h) = ($h, $w);
1555 }
1556
1557 glEnable GL_BLEND;
1558 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1559 glEnable GL_TEXTURE_2D;
1560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1561
1562 $tex->draw_quad (0, 0, $w, $h);
1563
1564 glDisable GL_BLEND;
1565 glDisable GL_TEXTURE_2D;
1566}
1567
1568#############################################################################
1569
1570package CFClient::UI::VGauge;
1571
1572our @ISA = CFClient::UI::Base::;
1573
1574use CFClient::OpenGL;
1575
1576my %tex = (
1577 food => [
1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1579 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1580 ],
1581 grace => [
1582 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1583 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1584 ],
1585 hp => [
1586 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1587 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1588 ],
1589 mana => [
1590 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1591 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1592 ],
1593);
1594
1595# eg. VGauge->new (gauge => 'food'), default gauge: food
1596sub new {
1597 my $class = shift;
1598
1599 my $self = $class->SUPER::new (
1600 type => 'food',
1601 @_
1602 );
1603
1604 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1605
1606 $self
1607}
1608
1609sub size_request {
1610 my ($self) = @_;
1611
1612 #my $tex = $tex{$self->{type}}[0];
1613 #@$tex{qw(w h)}
1614 (0, 0)
1615}
1616
1617sub set_max {
1618 my ($self, $max) = @_;
1619
1620 $self->{max_val} = $max;
1621}
1622
1623sub set_value {
1624 my ($self, $val, $max) = @_;
1625
1626 $self->set_max ($max)
1627 if defined $max;
1628
1629 $max = $self->{max_val};
1630 $self->{val} = $val;
1631
1632 $self->update;
1633}
1634
1635sub _draw {
1636 my ($self) = @_;
1637
1638 my $tex = $tex{$self->{type}};
1639
1640 my ($w, $h) = ($self->{w}, $self->{h});
1641
1642 if ($self->{vertical}) {
1643 glRotate 90, 0, 0, 1;
1644 glTranslate 0, -$self->{w}, 0;
1645
1646 ($w, $h) = ($h, $w);
1647 }
1648
1649 my $ycut = $self->{val} / ($self->{max_val} || 1);
1650 $ycut = 1 if $self->{val} > $self->{max_val};
1651
1652 my $t1 = $tex->[0];
1653 my $t2 = $tex->[1];
1654
1655 glEnable GL_BLEND;
1656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1657 glEnable GL_TEXTURE_2D;
1658 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1659
1660 my $h1 = $self->{h} - $ycut * $self->{h};
1661 my $h2 = $ycut * $self->{h};
1662
1663 glBindTexture GL_TEXTURE_2D, $t1->{name};
1664 glBegin GL_QUADS;
1665 glTexCoord 0 , 0; glVertex 0 , 0;
1666 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1667 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1668 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1669 glEnd;
1670
1671 glBindTexture GL_TEXTURE_2D, $t2->{name};
1672 glBegin GL_QUADS;
1673 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1674 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1675 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1676 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1677 glEnd;
1678
1679 glDisable GL_BLEND;
1680 glDisable GL_TEXTURE_2D;
1681}
1682
1683#############################################################################
1684
1685package CFClient::UI::Gauge;
1686
1687our @ISA = CFClient::UI::VBox::;
1688
1689sub new {
1690 my ($class, %arg) = shift;
1691
1692 my $self = $class->SUPER::new (
1693 @_,
1694 );
1695
1696 $self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1697 $self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1698 $self->add ($self->{value});
1699 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1700 $self->add ($self->{max});
1701
1702 $self
1703}
1704
1705sub size_request {
1706 my ($self) = @_;
1707 (($self->{max}->size_request)[0], 0)
1708}
1709
1710sub set_fontsize {
1711 my ($self, $fsize) = @_;
1712
1713 $self->{value}->set_fontsize ($fsize);
1714 $self->{max} ->set_fontsize ($fsize);
1715}
1716
1717sub set_value {
1718 my ($self, $val, $max) = @_;
1719
1720 $self->set_max ($max)
1721 if defined $max;
1722
1723 $self->{gauge}->set_value ($val, $max);
1724 $self->{value}->set_text ($val);
1725}
1726
1727sub set_max {
1728 my ($self, $max) = @_;
1729
1730 $self->{gauge}->set_max ($max);
1731 $self->{max}->set_text ($max);
1732}
1733
1734#############################################################################
1735
1347package CFClient::UI::Slider; 1736package CFClient::UI::Slider;
1348 1737
1349use strict; 1738use strict;
1350 1739
1351use SDL::OpenGL; 1740use CFClient::OpenGL;
1352 1741
1353our @ISA = CFClient::UI::DrawBG::; 1742our @ISA = CFClient::UI::DrawBG::;
1354 1743
1355my @tex = 1744my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1745 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1366 # TODO: calculations are off 1755 # TODO: calculations are off
1367 my $self = $class->SUPER::new ( 1756 my $self = $class->SUPER::new (
1368 fg => [1, 1, 1], 1757 fg => [1, 1, 1],
1369 active_fg => [0, 0, 0], 1758 active_fg => [0, 0, 0],
1370 range => [0, 0, 100, 10], 1759 range => [0, 0, 100, 10],
1371 req_w => 40, 1760 req_w => 20,
1372 req_h => 13, 1761 req_h => 20,
1373 vertical => 0, 1762 vertical => 0,
1374 can_hover => 1, 1763 can_hover => 1,
1375 inner_pad => 5, 1764 inner_pad => 5,
1376 @_ 1765 @_
1377 ); 1766 );
1481 1870
1482package CFClient::UI::TextView; 1871package CFClient::UI::TextView;
1483 1872
1484our @ISA = CFClient::UI::HBox::; 1873our @ISA = CFClient::UI::HBox::;
1485 1874
1486use SDL::OpenGL; 1875use CFClient::OpenGL;
1487 1876
1488sub new { 1877sub new {
1489 my $class = shift; 1878 my $class = shift;
1490 1879
1491 my $self = $class->SUPER::new ( 1880 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE, 1881 fontsize => 1,
1495 @_, 1882 @_,
1496 1883
1497 layout => (new CFClient::Layout), 1884 layout => (new CFClient::Layout),
1498 par => [], 1885 par => [],
1499 height => 0, 1886 height => 0,
1520sub text_height { 1907sub text_height {
1521 my ($self, $text) = @_; 1908 my ($self, $text) = @_;
1522 1909
1523 my $layout = $self->{layout}; 1910 my $layout = $self->{layout};
1524 1911
1525 $layout->set_height ($self->{fontsize}); 1912 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1526 $layout->set_width ($self->{w}); 1913 $layout->set_width ($self->{w});
1527 $layout->set_text ($text); 1914 $layout->set_text ($text);
1528 1915
1529 ($layout->size)[1] 1916 ($layout->size)[1]
1530} 1917}
1534 1921
1535 $self->{need_reflow}++; 1922 $self->{need_reflow}++;
1536 $self->update; 1923 $self->update;
1537} 1924}
1538 1925
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate { 1926sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 1927 my ($self, $w, $h) = @_;
1547 1928
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 1929 $self->SUPER::size_allocate ($w, $h);
1549 1930
1550 $self->{layout}->set_height ($self->{fontsize}); 1931 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 $self->{layout}->set_width ($self->{w}); 1932 $self->{layout}->set_width ($self->{children}[0]{w});
1552 1933
1553 $self->reflow; 1934 $self->reflow;
1554} 1935}
1555 1936
1556sub add_paragraph { 1937sub add_paragraph {
1575 1956
1576 return unless $self->{h} > 0; 1957 return unless $self->{h} > 0;
1577 1958
1578 delete $self->{texture}; 1959 delete $self->{texture};
1579 1960
1580 $TOPLEVEL->on_refresh ($self, sub { 1961 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1962 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1963 my $height = 0;
1583 1964
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1965 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1966 for @{$self->{par}};
1643 2024
1644} 2025}
1645 2026
1646############################################################################# 2027#############################################################################
1647 2028
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; 2029package CFClient::UI::Animator;
1883 2030
1884use SDL::OpenGL; 2031use CFClient::OpenGL;
1885 2032
1886our @ISA = CFClient::UI::Bin::; 2033our @ISA = CFClient::UI::Bin::;
1887 2034
1888sub moveto { 2035sub moveto {
1889 my ($self, $x, $y) = @_; 2036 my ($self, $x, $y) = @_;
1929 my $class = shift; 2076 my $class = shift;
1930 2077
1931 my $self = $class->SUPER::new ( 2078 my $self = $class->SUPER::new (
1932 state => 0, 2079 state => 0,
1933 connect_activate => \&toggle_flopper, 2080 connect_activate => \&toggle_flopper,
2081 can_events => 1,
1934 @_ 2082 @_
1935 ); 2083 );
1936 2084
1937 if ($self->{state}) { 2085 if ($self->{state}) {
1938 $self->{state} = 0; 2086 $self->{state} = 0;
1945sub toggle_flopper { 2093sub toggle_flopper {
1946 my ($self) = @_; 2094 my ($self) = @_;
1947 2095
1948 # TODO: use animation 2096 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 2097 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2098 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 2099 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 2100 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 2101 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2102 $CFClient::UI::ROOT->remove ($self->{other});
2103 $self->emit ("close");
1957 } 2104 }
1958}
1959 2105
1960############################################################################# 2106 $self->emit (changed => $self->{state});
2107}
1961 2108
2109#############################################################################
2110
1962package CFClient::UI::Toplevel; 2111package CFClient::UI::Root;
1963 2112
1964our @ISA = CFClient::UI::Container::; 2113our @ISA = CFClient::UI::Container::;
1965 2114
1966use SDL::OpenGL; 2115use CFClient::OpenGL;
2116
2117sub check_size {
2118 my ($self) = @_;
2119
2120 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2121}
1967 2122
1968sub size_request { 2123sub size_request {
1969 ($::WIDTH, $::HEIGHT) 2124 ($::WIDTH, $::HEIGHT)
1970} 2125}
1971 2126
1972sub size_allocate { 2127sub configure {
1973 my ($self, $x, $y, $w, $h) = @_; 2128 my ($self, $x, $y, $w, $h) = @_;
1974 2129
1975 $self->_size_allocate ($x, $y, $w, $h); 2130 $self->SUPER::configure ($x, $y, $w, $h);
1976 2131
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 2132 for my $child (@{$self->{children}}) {
1979} 2133 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1980 2134
1981sub translate { 2135 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2136 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2137 $child->configure ($X, $Y, $W,$H);
2138 }
2139}
2140
2141sub _topleft {
1982 my ($self, $x, $y) = @_; 2142 my ($self, $x, $y) = @_;
1983 2143
1984 ($x, $y) 2144 ($x, $y)
1985} 2145}
1986 2146
1987sub update { 2147sub update {
1988 my ($self) = @_; 2148 my ($self) = @_;
1989 2149
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2150 $self->check_size;
1991 ::refresh (); 2151 ::refresh ();
1992} 2152}
1993 2153
1994sub add { 2154sub add {
1995 my ($self, $widget) = @_; 2155 my ($self, $child) = @_;
1996 2156
2157 # integerize window positions
2158 $child->{x} = int $child->{x};
2159 $child->{y} = int $child->{y};
2160
1997 $self->SUPER::add ($widget); 2161 $self->SUPER::add ($child);
1998
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000} 2162}
2001 2163
2002sub on_refresh { 2164sub on_refresh {
2003 my ($self, $id, $cb) = @_; 2165 my ($self, $id, $cb) = @_;
2004 2166
2027 2189
2028############################################################################# 2190#############################################################################
2029 2191
2030package CFClient::UI; 2192package CFClient::UI;
2031 2193
2032$TOPLEVEL = new CFClient::UI::Toplevel; 2194$ROOT = new CFClient::UI::Root;
2033 2195
20341 21961
2035 2197

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines