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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines