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.107 by root, Fri Apr 14 20:27:35 2006 UTC vs.
Revision 1.158 by root, Mon Apr 24 02:41:48 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;
104}
105
106# convert position array to integers
107sub harmonize {
108 my ($vals) = @_;
109
110 my $rem = 0;
111
112 for (@$vals) {
113 my $i = int $_ + $rem;
114 $rem += $_ - $i;
115 $_ = $i;
116 }
71} 117}
72 118
73############################################################################# 119#############################################################################
74 120
75package CFClient::UI::Base; 121package CFClient::UI::Base;
76 122
77use strict; 123use strict;
78 124
79use SDL::OpenGL; 125use CFClient::OpenGL;
80 126
81sub new { 127sub new {
82 my $class = shift; 128 my $class = shift;
83 129
84 my $self = bless { 130 my $self = bless {
85 x => 0, 131 x => 0,
86 y => 0, 132 y => 0,
87 z => 0, 133 z => 0,
88 w => -1, 134 can_events => 1,
89 h => -1,
90 @_ 135 @_
91 }, $class; 136 }, $class;
92 137
93 for (keys %$self) { 138 for (keys %$self) {
94 if (/^connect_(.*)$/) { 139 if (/^connect_(.*)$/) {
97 } 142 }
98 143
99 $self 144 $self
100} 145}
101 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
102sub move { 163sub move {
103 my ($self, $x, $y, $z) = @_; 164 my ($self, $x, $y, $z) = @_;
165
104 $self->{x} = $x; 166 $self->{x} = int $x;
105 $self->{y} = $y; 167 $self->{y} = int $y;
106 $self->{z} = $z if defined $z; 168 $self->{z} = $z if defined $z;
107}
108 169
109sub needs_redraw { 170 $self->update;
110 0 171}
172
173sub set_size {
174 my ($self, $w, $h) = @_;
175
176 $self->{user_w} = $w;
177 $self->{user_h} = $h;
178
179 $self->check_size;
111} 180}
112 181
113sub size_request { 182sub size_request {
114 require Carp; 183 require Carp;
115 Carp::confess "size_request is abtract"; 184 Carp::confess "size_request is abstract";
116} 185}
117 186
118sub _size_allocate { 187sub configure {
119 my ($self, $x, $y, $w, $h) = @_; 188 my ($self, $x, $y, $w, $h) = @_;
120 189
190 if ($self->{aspect}) {
191 my $w2 = List::Util::min $w, int $h * $self->{aspect};
192 my $h2 = List::Util::min $h, int $w / $self->{aspect};
193
194 # use alignment to adjust x, y
195
196 $x += int +($w - $w2) * 0.5;
197 $y += int +($h - $h2) * 0.5;
198
199 ($w, $h) = ($w2, $h2);
200 }
201
202 if ($self->{x} != $x || $self->{y} != $y) {
121 $self->{x} = $x; 203 $self->{x} = $x;
122 $self->{y} = $y; 204 $self->{y} = $y;
205 $self->update;
206 }
123 207
124 return unless $self->{w} != $w || $self->{h} != $h; 208 if ($self->{w} != $w || $self->{h} != $h) {
125
126 $self->{w} = $w; 209 $self->{w} = $w;
127 $self->{h} = $h; 210 $self->{h} = $h;
128 211
212 $self->size_allocate ($w, $h);
213 $self->update;
129 1 214 }
130} 215}
131 216
132sub size_allocate { 217sub size_allocate {
218 # nothing to be done
219}
220
221sub set_max_size {
133 my ($self, $x, $y, $w, $h) = @_; 222 my ($self, $w, $h) = @_;
134 223
135 $self->_size_allocate ($x, $y, $w, $h); 224 delete $self->{max_w}; $self->{max_w} = $w if $w;
225 delete $self->{max_h}; $self->{max_h} = $h if $h;
226}
227
228# return top left coordinates
229sub _topleft {
230 my ($self, $x, $y) = @_;
231
232 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
136} 233}
137 234
138# translate global coordinates to local coordinate system 235# translate global coordinates to local coordinate system
139sub translate { 236sub coord2local {
140 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
141 238
142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 239 my ($X, $Y) = $self->_topleft;
240 ($x - $X, $y - $Y)
241}
242
243# translate local coordinates to global coordinate system
244sub coord2global {
245 my ($self, $x, $y) = @_;
246
247 my ($X, $Y) = $self->_topleft;
248 ($x + $X, $y + $Y)
143} 249}
144 250
145sub focus_in { 251sub focus_in {
146 my ($self) = @_; 252 my ($self) = @_;
147 253
148 return if $FOCUS == $self; 254 return if $FOCUS == $self;
149 return unless $self->{can_focus}; 255 return unless $self->{can_focus};
150 256
151 my $focus = $FOCUS; $FOCUS = $self; 257 my $focus = $FOCUS; $FOCUS = $self;
258
259 $self->emit (focus_in => $focus);
260
152 $focus->update if $focus; 261 $focus->update if $focus;
153 $FOCUS->update; 262 $FOCUS->update;
154} 263}
155 264
156sub focus_out { 265sub focus_out {
157 my ($self) = @_; 266 my ($self) = @_;
158 267
159 return unless $FOCUS == $self; 268 return unless $FOCUS == $self;
160 269
161 my $focus = $FOCUS; undef $FOCUS; 270 my $focus = $FOCUS; undef $FOCUS;
271
272 $self->emit (focus_out => $focus);
273
162 $focus->update if $focus; #? 274 $focus->update if $focus; #?
163} 275}
164 276
165sub mouse_motion { } 277sub mouse_motion { }
166sub button_up { } 278sub button_up { }
190 glPopMatrix; 302 glPopMatrix;
191 303
192 if ($self == $HOVER && $self->{can_hover}) { 304 if ($self == $HOVER && $self->{can_hover}) {
193 my ($x, $y) = @$self{qw(x y)}; 305 my ($x, $y) = @$self{qw(x y)};
194 306
195 glColor 0, 0, 1, 0.2; 307 glColor 1, 0.8, 0.5, 0.2;
196 glEnable GL_BLEND; 308 glEnable GL_BLEND;
197 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 309 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
198 glBegin GL_QUADS; 310 glBegin GL_QUADS;
199 glVertex $x , $y; 311 glVertex $x , $y;
200 glVertex $x + $self->{w}, $y; 312 glVertex $x + $self->{w}, $y;
212} 324}
213 325
214sub find_widget { 326sub find_widget {
215 my ($self, $x, $y) = @_; 327 my ($self, $x, $y) = @_;
216 328
329 return () unless $self->{can_events};
330
217 return $self 331 return $self
218 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 332 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
219 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 333 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
220 334
221 () 335 ()
222} 336}
223 337
224sub set_parent { 338sub set_parent {
225 my ($self, $par) = @_; 339 my ($self, $parent) = @_;
226 340
227 $self->{parent} = $par;
228 Scalar::Util::weaken $self->{parent}; 341 Scalar::Util::weaken ($self->{parent} = $parent);
229} 342}
230 343
231sub get_parent { 344sub check_size {
345 my ($self) = @_;
346
232 $_[0]->{parent} 347 $self->{parent}
348 or return 1;
349
350 my ($w, $h) = $self->{user_w} && $self->{user_h}
351 ? @$self{qw(user_w user_h)}
352 : $self->size_request;
353
354 if ($w != $self->{req_w} || $h != $self->{req_h}) {
355 $self->{req_w} = $w;
356 $self->{req_h} = $h;
357
358 $self->{parent}->check_size
359 or $self->size_allocate (
360 (List::Util::max $self->{w}, $w),
361 (List::Util::max $self->{h}, $h),
362 );
363
364 1
365 } else {
366 0
367 }
233} 368}
234 369
235sub update { 370sub update {
236 my ($self) = @_; 371 my ($self) = @_;
237 372
240} 375}
241 376
242sub connect { 377sub connect {
243 my ($self, $signal, $cb) = @_; 378 my ($self, $signal, $cb) = @_;
244 379
245 push @{ $self->{cb}{$signal} }, $cb; 380 push @{ $self->{signal_cb}{$signal} }, $cb;
246} 381}
247 382
248sub emit { 383sub emit {
249 my ($self, $signal, @args) = @_; 384 my ($self, $signal, @args) = @_;
250 385
386 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
251 $_->($self, @args) 387 $cb->($self, @args);
252 for @{$self->{cb}{$signal} || []}; 388 }
253} 389}
254 390
255sub DESTROY { 391sub DESTROY {
256 my ($self) = @_; 392 my ($self) = @_;
257 393
263package CFClient::UI::DrawBG; 399package CFClient::UI::DrawBG;
264 400
265our @ISA = CFClient::UI::Base::; 401our @ISA = CFClient::UI::Base::;
266 402
267use strict; 403use strict;
268use SDL::OpenGL; 404use CFClient::OpenGL;
269 405
270sub new { 406sub new {
271 my $class = shift; 407 my $class = shift;
272 408
273 # range [value, low, high, page] 409 # range [value, low, high, page]
302 438
303package CFClient::UI::Empty; 439package CFClient::UI::Empty;
304 440
305our @ISA = CFClient::UI::Base::; 441our @ISA = CFClient::UI::Base::;
306 442
443sub new {
444 my ($class, %arg) = @_;
445 $class->SUPER::new (can_events => 0, %arg);
446}
447
307sub size_request { 448sub size_request {
308 (0, 0) 449 (0, 0)
309} 450}
310 451
311sub draw { } 452sub draw { }
319sub new { 460sub new {
320 my ($class, %arg) = @_; 461 my ($class, %arg) = @_;
321 462
322 my $children = delete $arg{children} || []; 463 my $children = delete $arg{children} || [];
323 464
324 my $self = $class->SUPER::new (children => [], %arg); 465 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
325 $self->add ($_) for @$children; 466 $self->add ($_) for @$children;
326 467
327 $self 468 $self
328} 469}
329 470
330sub add { 471sub add {
331 my ($self, $chld) = @_; 472 my ($self, $child) = @_;
332 473
333 $chld->set_parent ($self); 474 $child->set_parent ($self);
475
476 use sort 'stable';
334 477
335 $self->{children} = [ 478 $self->{children} = [
336 sort { $a->{z} <=> $b->{z} } 479 sort { $a->{z} <=> $b->{z} }
337 @{$self->{children}}, $chld 480 @{$self->{children}}, $child
338 ]; 481 ];
339 482
340 $self->{w} = $self->{h} = -1; 483 $child->check_size;
484}
485
486sub remove {
487 my ($self, $child) = @_;
488
489 delete $child->{parent};
490
491 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
492
493 $self->check_size;
341 $self->update; 494 $self->update;
342}
343
344sub remove {
345 my ($self, $widget) = @_;
346
347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
348
349 $self->size_allocate (0, 0, $self->{w}, $self->{h});
350} 495}
351 496
352sub find_widget { 497sub find_widget {
353 my ($self, $x, $y) = @_; 498 my ($self, $x, $y) = @_;
354 499
384 529
385 $class->SUPER::new (children => [$child], %arg) 530 $class->SUPER::new (children => [$child], %arg)
386} 531}
387 532
388sub add { 533sub add {
389 my ($self, $widget) = @_; 534 my ($self, $child) = @_;
390 535
391 $self->{children} = []; 536 $self->{children} = [];
392 537
393 $self->SUPER::add ($widget); 538 $self->SUPER::add ($child);
394} 539}
395 540
396sub remove { 541sub remove {
397 my ($self, $widget) = @_; 542 my ($self, $widget) = @_;
398 543
407sub size_request { 552sub size_request {
408 $_[0]{children}[0]->size_request 553 $_[0]{children}[0]->size_request
409} 554}
410 555
411sub size_allocate { 556sub size_allocate {
412 my ($self, $x, $y, $w, $h) = @_; 557 my ($self, $w, $h) = @_;
413 558
414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
416 $self->{children}[0]->size_allocate (0, 0, $w, $h); 559 $self->{children}[0]->configure (0, 0, $w, $h);
417} 560}
418 561
419############################################################################# 562#############################################################################
420 563
421package CFClient::UI::Window; 564package CFClient::UI::Window;
422 565
423our @ISA = CFClient::UI::Bin::; 566our @ISA = CFClient::UI::Bin::;
424 567
425use SDL::OpenGL; 568use CFClient::OpenGL;
426 569
427sub new { 570sub new {
428 my ($class, %arg) = @_; 571 my ($class, %arg) = @_;
429 572
430 my $self = $class->SUPER::new (%arg); 573 my $self = $class->SUPER::new (%arg);
447 $self->child->draw; 590 $self->child->draw;
448 }; 591 };
449} 592}
450 593
451sub size_allocate { 594sub size_allocate {
452 my ($self, $x, $y, $w, $h) = @_; 595 my ($self, $w, $h) = @_;
453 596
454 $self->_size_allocate ($x, $y, $w, $h) or return;
455
456 $self->child->size_allocate (0, 0, $w, $h); 597 $self->child->configure (0, 0, $w, $h);
457 598
458 $self->render_chld; 599 $self->render_chld;
459} 600}
460 601
461sub _draw { 602sub _draw {
477 glDisable GL_TEXTURE_2D; 618 glDisable GL_TEXTURE_2D;
478} 619}
479 620
480############################################################################# 621#############################################################################
481 622
623package CFClient::UI::ViewPort;
624
625our @ISA = CFClient::UI::Window::;
626
627sub new { die }
628
629sub size_request {
630 my ($self) = @_;
631
632 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
633 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
634
635 @$self{qw(child_w child_h)}
636}
637
638sub _draw {
639 my ($self) = @_;
640
641 $self->{children}[1]->draw;
642}
643
644
645#############################################################################
646
482package CFClient::UI::Frame; 647package CFClient::UI::Frame;
483 648
484our @ISA = CFClient::UI::Bin::; 649our @ISA = CFClient::UI::Bin::;
485 650
486use SDL::OpenGL; 651use CFClient::OpenGL;
487
488sub size_request {
489 my ($self) = @_;
490 my $chld = $self->child
491 or return (0, 0);
492
493 $chld->move (2, 2);
494
495 map { $_ + 4 } $chld->size_request;
496}
497
498sub size_allocate {
499 my ($self, $x, $y, $w, $h) = @_;
500
501 $self->_size_allocate ($x, $y, $w, $h) or return;
502
503 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
504}
505
506sub _draw {
507 my ($self) = @_;
508
509 my $chld = $self->child;
510
511 my ($w, $h) = $chld->size_request;
512
513 glBegin GL_QUADS;
514 glColor 0, 0, 0;
515 glVertex 0 , 0;
516 glVertex 0 , $h + 4;
517 glVertex $w + 4 , $h + 4;
518 glVertex $w + 4 , 0;
519 glEnd;
520
521 $chld->draw;
522}
523
524#############################################################################
525
526package CFClient::UI::FancyFrame;
527
528our @ISA = CFClient::UI::Bin::;
529
530use SDL::OpenGL;
531
532my @tex =
533 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
534 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
535 652
536sub new { 653sub new {
537 my $class = shift; 654 my $class = shift;
538 655
539 # TODO: user_x, user_y, overwrite moveto?
540
541 $class->SUPER::new ( 656 my $self = $class->SUPER::new (
542 bg => [1, 1, 1, 1], 657 bg => [1, 1, 1, 1],
543 border_bg => [1, 1, 1, 1], 658 border_bg => [1, 1, 1, 1],
544 border => $::FONTSIZE * 0.8, 659 border => 0.8,
545 @_ 660 @_
661 );
662
663 $self
664}
665
666sub _draw {
667 my ($self) = @_;
668
669 my ($w, $h) = ($self->{w}, $self->{h});
670
671 glEnable GL_BLEND;
672 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
673 glEnable GL_TEXTURE_2D;
674 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
675
676# glBegin GL_QUADS;
677# glColor 0, 0, 0, 0;
678# glVertex 0 , 0;
679# glVertex 0 , $h;
680# glVertex $w, $h;
681# glVertex $w, 0;
682# glEnd;
683
684
685 $self->child->draw;
686 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D;
688}
689
690#############################################################################
691
692package CFClient::UI::FancyFrame;
693
694our @ISA = CFClient::UI::Bin::;
695
696use CFClient::OpenGL;
697
698my @tex =
699 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
700 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
701
702sub new {
703 my $class = shift;
704
705 # TODO: user_x, user_y, overwrite moveto?
706
707 my $self = $class->SUPER::new (
708 bg => [1, 1, 1, 1],
709 border_bg => [1, 1, 1, 1],
710 border => 0.8,
711 can_events => 1,
712 @_
713 );
714
715 $self->{title} &&= new CFClient::UI::Label
716 align => 0,
717 valign => 1,
718 text => $self->{title},
719 fontsize => 1;
720
721 $self
722}
723
724sub border {
725 int $_[0]{border} * $::FONTSIZE
726}
727
728sub size_request {
729 my ($self) = @_;
730
731 my ($w, $h) = $self->SUPER::size_request;
732
733 (
734 $w + $self->border * 2,
735 $h + $self->border * 2,
546 ) 736 )
547} 737}
548 738
549sub size_request {
550 my ($self) = @_;
551
552 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
553
554 my ($w, $h) = $self->SUPER::size_request;
555
556 (
557 $w + $self->{border} * 2,
558 $h + $self->{border} * 2,
559 )
560}
561
562sub size_allocate { 739sub size_allocate {
563 my ($self, $x, $y, $w, $h) = @_; 740 my ($self, $w, $h) = @_;
564 741
565 $self->_size_allocate ($x, $y, $w, $h) or return;
566
567 $h -= List::Util::max 0, $self->{border} * 2; 742 $h -= List::Util::max 0, $self->border * 2;
568 $w -= List::Util::max 0, $self->{border} * 2; 743 $w -= List::Util::max 0, $self->border * 2;
569 744
745 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
746 if $self->{title};
747
570 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 748 $self->child->configure ($self->border, $self->border, $w, $h);
571} 749}
572 750
573sub button_down { 751sub button_down {
574 my ($self, $ev, $x, $y) = @_; 752 my ($self, $ev, $x, $y) = @_;
575 753
754 my $border = $self->border;
755
576 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 756 if ($x < $self->{w} && $x >= $self->{w} - $border
577 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 757 && $y < $self->{h} && $y >= $self->{h} - $border) {
578 758
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 759 my ($ox, $oy) = ($ev->{x}, $ev->{y});
580 my ($bw, $bh) = ($self->{w}, $self->{h}); 760 my ($bw, $bh) = ($self->{w}, $self->{h});
581 761
582 $self->{motion} = sub { 762 $self->{motion} = sub {
583 my ($ev, $x, $y) = @_; 763 my ($ev, $x, $y) = @_;
584 764
585 ($x, $y) = ($ev->motion_x, $ev->motion_y); 765 ($x, $y) = ($ev->{x}, $ev->{y});
586 766
587 $self->{user_w} = $bw + $x - $ox; 767 $self->{user_w} = $bw + $x - $ox;
588 $self->{user_h} = $bh + $y - $oy; 768 $self->{user_h} = $bh + $y - $oy;
589 $self->update; 769 $self->check_size;
590 }; 770 };
591 771
592 } elsif ($x >= 0 && $x < $self->{w} 772 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $self->{border}) { 773 && $y >= 0 && $y < $border) {
594 774
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 775 my ($ox, $oy) = ($ev->{x}, $ev->{y});
596 my ($bx, $by) = ($self->{x}, $self->{y}); 776 my ($bx, $by) = ($self->{x}, $self->{y});
597 777
598 $self->{motion} = sub { 778 $self->{motion} = sub {
599 my ($ev, $x, $y) = @_; 779 my ($ev, $x, $y) = @_;
600 780
601 ($x, $y) = ($ev->motion_x, $ev->motion_y); 781 ($x, $y) = ($ev->{x}, $ev->{y});
602 782
603 $self->move ($bx + $x - $ox, $by + $y - $oy); 783 $self->move ($bx + $x - $ox, $by + $y - $oy);
604 $self->update; 784 $self->update;
605 }; 785 };
606 } 786 }
627 glEnable GL_BLEND; 807 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
629 glEnable GL_TEXTURE_2D; 809 glEnable GL_TEXTURE_2D;
630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 810 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
631 811
812 my $border = $self->border;
813
632 glColor @{ $self->{border_bg} }; 814 glColor @{ $self->{border_bg} };
633 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 815 $tex[1]->draw_quad (0, 0, $w, $border);
634 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 816 $tex[3]->draw_quad (0, $border, $border, $ch);
635 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 817 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
636 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 818 $tex[4]->draw_quad (0, $h - $border, $w, $border);
637 819
638 my $bg = $tex[0]; 820 my $bg = $tex[0];
639 821
640 # TODO: repeat texture not scale 822 # TODO: repeat texture not scale
641 my $rep_x = $cw / $bg->{w}; 823 my $rep_x = $cw / $bg->{w};
644 glColor @{ $self->{bg} }; 826 glColor @{ $self->{bg} };
645 827
646 $bg->{s} = $rep_x; 828 $bg->{s} = $rep_x;
647 $bg->{t} = $rep_y; 829 $bg->{t} = $rep_y;
648 $bg->{wrap_mode} = 1; 830 $bg->{wrap_mode} = 1;
649 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 831 $bg->draw_quad ($border, $border, $cw, $ch);
650 832
651 glDisable GL_TEXTURE_2D; 833 glDisable GL_TEXTURE_2D;
652 glDisable GL_BLEND; 834 glDisable GL_BLEND;
653 835
836 $self->{title}->draw if $self->{title};
654 $self->child->draw; 837 $self->child->draw;
655} 838}
656 839
657############################################################################# 840#############################################################################
658 841
660 843
661our @ISA = CFClient::UI::Base::; 844our @ISA = CFClient::UI::Base::;
662 845
663use List::Util qw(max sum); 846use List::Util qw(max sum);
664 847
665use SDL::OpenGL; 848use CFClient::OpenGL;
666 849
667sub new { 850sub new {
668 my $class = shift; 851 my $class = shift;
669 852
670 $class->SUPER::new ( 853 $class->SUPER::new (
672 @_ 855 @_
673 ) 856 )
674} 857}
675 858
676sub add { 859sub add {
677 my ($self, $x, $y, $chld) = @_; 860 my ($self, $x, $y, $child) = @_;
678 861
862 $child->set_parent ($self);
679 $self->{children}[$y][$x] = $chld; 863 $self->{children}[$y][$x] = $child;
680 $chld->set_parent ($self);
681 864
682 $self->{w} = $self->{h} = -1; 865 $child->check_size;
866}
867
868# TODO: move to container class maybe? send childs a signal on removal?
869sub clear {
870 my ($self) = @_;
871
872 delete $self->{children};
683 $self->update; 873 $self->update;
684} 874}
685 875
686sub get_wh { 876sub get_wh {
687 my ($self) = @_; 877 my ($self) = @_;
693 or next; 883 or next;
694 884
695 for my $x (0 .. $#$row) { 885 for my $x (0 .. $#$row) {
696 my $widget = $row->[$x] 886 my $widget = $row->[$x]
697 or next; 887 or next;
698 my ($w, $h) = $widget->size_request; 888 my ($w, $h) = @$widget{qw(req_w req_h)};
699 889
700 $w[$x] = max $w[$x], $w; 890 $w[$x] = max $w[$x], $w;
701 $h[$y] = max $h[$y], $h; 891 $h[$y] = max $h[$y], $h;
702 } 892 }
703 } 893 }
715 (sum @$hs), 905 (sum @$hs),
716 ) 906 )
717} 907}
718 908
719sub size_allocate { 909sub size_allocate {
720 my ($self, $x, $y, $w, $h) = @_; 910 my ($self, $w, $h) = @_;
721
722 $self->_size_allocate ($x, $y, $w, $h) or return;
723 911
724 my ($ws, $hs) = $self->get_wh; 912 my ($ws, $hs) = $self->get_wh;
725 913
726 my $req_w = sum @$ws; 914 my $req_w = sum @$ws;
727 my $req_h = sum @$hs; 915 my $req_h = sum @$hs;
733 921
734 # linearly scale sizes 922 # linearly scale sizes
735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 923 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
736 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 924 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
737 925
738 $_ = int $_ for @$ws; #TODO: avoid rounding problems 926 CFClient::UI::harmonize $ws;
739 $_ = int $_ for @$hs; #TODO: avoid rounding problems 927 CFClient::UI::harmonize $hs;
740 928
741 my $y; 929 my $y;
742 930
743 for my $r (0 .. $#{$self->{children}}) { 931 for my $r (0 .. $#{$self->{children}}) {
744 my $row = $self->{children}[$r] 932 my $row = $self->{children}[$r]
749 937
750 for my $c (0 .. $#$row) { 938 for my $c (0 .. $#$row) {
751 my $col_w = $ws->[$c]; 939 my $col_w = $ws->[$c];
752 940
753 if (my $widget = $row->[$c]) { 941 if (my $widget = $row->[$c]) {
754 $widget->size_allocate ($x, $y, $col_w, $row_h); 942 $widget->configure ($x, $y, $col_w, $row_h);
755 } 943 }
756 944
757 $x += $col_w; 945 $x += $col_w;
758 } 946 }
759 947
804 (List::Util::max map $_->[1], @alloc), 992 (List::Util::max map $_->[1], @alloc),
805 ) 993 )
806} 994}
807 995
808sub size_allocate { 996sub size_allocate {
809 my ($self, $x, $y, $w, $h) = @_; 997 my ($self, $w, $h) = @_;
810
811 $self->_size_allocate ($x, $y, $w, $h);
812 998
813 ($h, $w) = ($w, $h); 999 ($h, $w) = ($w, $h);
814 1000
815 my $children = $self->{children}; 1001 my $children = $self->{children};
816 1002
817 my @h = map +($_->size_request)[0], @$children; 1003 my @h = map $_->{req_w}, @$children;
818 1004
819 my $req_h = List::Util::sum @h; 1005 my $req_h = List::Util::sum @h;
820 1006
821 if ($req_h > $h) { 1007 if ($req_h > $h) {
822 # ah well, not enough space 1008 # ah well, not enough space
832 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 1018 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
833 $h[$_] = $alloc_h; 1019 $h[$_] = $alloc_h;
834 } 1020 }
835 } 1021 }
836 1022
1023 CFClient::UI::harmonize \@h;
1024
837 my $y = 0; 1025 my $y = 0;
838 for (0 .. $#$children) { 1026 for (0 .. $#$children) {
839 my $child = $children->[$_]; 1027 my $child = $children->[$_];
840 my $h = int $h[$_]; 1028 my $h = $h[$_];
841 $child->size_allocate ($y, 0, $h, $w); 1029 $child->configure ($y, 0, $h, $w);
842 1030
843 $y += $h; 1031 $y += $h;
844 } 1032 }
1033
1034 1
845} 1035}
846 1036
847############################################################################# 1037#############################################################################
848 1038
849package CFClient::UI::VBox; 1039package CFClient::UI::VBox;
862 (List::Util::sum map $_->[1], @alloc), 1052 (List::Util::sum map $_->[1], @alloc),
863 ) 1053 )
864} 1054}
865 1055
866sub size_allocate { 1056sub size_allocate {
867 my ($self, $x, $y, $w, $h) = @_; 1057 my ($self, $w, $h) = @_;
868
869 $self->_size_allocate ($x, $y, $w, $h);
870 1058
871 my $children = $self->{children}; 1059 my $children = $self->{children};
872 1060
873 my @h = map +($_->size_request)[1], @$children; 1061 my @h = map $_->{req_h}, @$children;
874 1062
875 my $req_h = List::Util::sum @h; 1063 my $req_h = List::Util::sum @h;
876 1064
877 if ($req_h > $h) { 1065 if ($req_h > $h) {
878 # ah well, not enough space 1066 # ah well, not enough space
886 1074
887 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1075 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
888 } 1076 }
889 } 1077 }
890 1078
1079 CFClient::UI::harmonize \@h;
1080
891 my $y = 0; 1081 my $y = 0;
892 for (0 .. $#$children) { 1082 for (0 .. $#$children) {
893 my $child = $children->[$_]; 1083 my $child = $children->[$_];
894 my $h = int $h[$_]; 1084 my $h = $h[$_];
895 $child->size_allocate (0, $y, $w, $h); 1085 $child->configure (0, $y, $w, $h);
896 1086
897 $y += $h; 1087 $y += $h;
898 } 1088 }
1089
1090 1
899} 1091}
900 1092
901############################################################################# 1093#############################################################################
902 1094
903package CFClient::UI::Label; 1095package CFClient::UI::Label;
904 1096
905our @ISA = CFClient::UI::Base::; 1097our @ISA = CFClient::UI::Base::;
906 1098
907use SDL::OpenGL; 1099use CFClient::OpenGL;
908 1100
909sub new { 1101sub new {
910 my ($class, %arg) = @_; 1102 my ($class, %arg) = @_;
911 1103
912 my $self = $class->SUPER::new ( 1104 my $self = $class->SUPER::new (
913 fg => [1, 1, 1], 1105 fg => [1, 1, 1],
1106 #font => default_font
914 fontsize => $::FONTSIZE, 1107 fontsize => 1,
915 text => "", 1108 text => "",
916 align => -1, 1109 align => -1,
1110 valign => -1,
917 padding => 2, 1111 padding => 2,
918 layout => new CFClient::Layout, 1112 layout => new CFClient::Layout,
1113 can_events => 0,
919 %arg 1114 %arg
920 ); 1115 );
921 1116
922 $self->set_text ($self->{text}); 1117 if (exists $self->{template}) {
1118 my $layout = new CFClient::Layout;
1119 $layout->set_text (delete $self->{template});
1120 $self->{template} = $layout;
1121 }
1122
1123 $self->set_text (delete $self->{text}) if exists $self->{text};
1124 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
923 1125
924 $self 1126 $self
925} 1127}
926 1128
927sub escape_text { 1129sub escape_text {
935} 1137}
936 1138
937sub set_text { 1139sub set_text {
938 my ($self, $text) = @_; 1140 my ($self, $text) = @_;
939 1141
940 $self->{text} = $text;
941 $self->{layout}->set_markup ($text); 1142 $self->{layout}->set_text ($text);
942 1143
943 delete $self->{texture}; 1144 delete $self->{texture};
944# $self->{w} = $self->{h} = -1; 1145 $self->check_size;
945 $self->update; 1146 $self->update;
946} 1147}
947 1148
948sub get_text { 1149sub set_markup {
949 my ($self, $text) = @_; 1150 my ($self, $markup) = @_;
950 1151
951 $self->{text} 1152 $self->{layout}->set_markup ($markup);
1153
1154 delete $self->{texture};
1155 $self->check_size;
1156 $self->update;
952} 1157}
953 1158
954sub size_request { 1159sub size_request {
955 my ($self) = @_; 1160 my ($self) = @_;
956 1161
957 $self->{layout}->set_width; 1162 $self->{layout}->set_font ($self->{font}) if $self->{font};
1163 $self->{layout}->set_width ($self->{max_w} || -1);
958 $self->{layout}->set_height ($self->{fontsize}); 1164 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1165
959 my ($w, $h) = $self->{layout}->size; 1166 my ($w, $h) = $self->{layout}->size;
1167
1168 if (exists $self->{template}) {
1169 $self->{template}->set_font ($self->{font}) if $self->{font};
1170 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1171
1172 my ($w2, $h2) = $self->{template}->size;
1173
1174 $w = List::Util::max $w, $w2;
1175 $h = List::Util::max $h, $h2;
1176 }
960 1177
961 ( 1178 (
962 $w + $self->{padding} * 2, 1179 $w + $self->{padding} * 2,
963 $h + $self->{padding} * 2, 1180 $h + $self->{padding} * 2,
964 ) 1181 )
965} 1182}
966 1183
967sub size_allocate { 1184sub size_allocate {
968 my ($self, $x, $y, $w, $h) = @_; 1185 my ($self, $w, $h) = @_;
969
970 $self->_size_allocate ($x, $y, $w, $h) or return;
971 1186
972 delete $self->{texture}; 1187 delete $self->{texture};
973} 1188}
974 1189
975sub update { 1190sub set_fontsize {
976 my ($self) = @_; 1191 my ($self, $fontsize) = @_;
977 1192
1193 $self->{fontsize} = $fontsize;
978 delete $self->{texture}; 1194 delete $self->{texture};
1195 $self->check_size;
979 $self->SUPER::update; 1196 $self->update;
980} 1197}
981 1198
982sub _draw { 1199sub _draw {
983 my ($self) = @_; 1200 my ($self) = @_;
984 1201
985 my $tex = $self->{texture} ||= do { 1202 my $tex = $self->{texture} ||= do {
1203 $self->{layout}->set_font ($self->{font}) if $self->{font};
986 $self->{layout}->set_width ($self->{w}); 1204 $self->{layout}->set_width ($self->{w});
987 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1205 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
988 new_from_layout CFClient::Texture $self->{layout} 1206 new_from_layout CFClient::Texture $self->{layout}
989 }; 1207 };
990 1208
991 glEnable GL_BLEND; 1209 glEnable GL_BLEND;
992 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
993 glEnable GL_TEXTURE_2D; 1211 glEnable GL_TEXTURE_2D;
994 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1212 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
995 1213
996 glColor @{$self->{fg}}; 1214 glColor @{$self->{fg}};
997 1215
998 my $x = 1216 $self->{ox} = int (
999 $self->{align} < 0 ? $self->{padding} 1217 $self->{align} < 0 ? $self->{padding}
1000 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1218 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1001 : ($self->{w} - $tex->{w}) * 0.5; 1219 : ($self->{w} - $tex->{w}) * 0.5
1220 );
1002 1221
1003 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1222 $self->{oy} = int (
1223 $self->{valign} < 0 ? $self->{padding}
1224 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1225 : ($self->{h} - $tex->{h}) * 0.5
1226 );
1227
1228 $tex->draw_quad ($self->{ox}, $self->{oy});
1004 1229
1005 glDisable GL_TEXTURE_2D; 1230 glDisable GL_TEXTURE_2D;
1006 glDisable GL_BLEND; 1231 glDisable GL_BLEND;
1007} 1232}
1008 1233
1009############################################################################# 1234#############################################################################
1010 1235
1011package CFClient::UI::Entry; 1236package CFClient::UI::EntryBase;
1012 1237
1013our @ISA = CFClient::UI::Label::; 1238our @ISA = CFClient::UI::Label::;
1014 1239
1015use SDL; 1240use CFClient::OpenGL;
1016use SDL::OpenGL;
1017 1241
1018sub new { 1242sub new {
1019 my $class = shift; 1243 my $class = shift;
1020 1244
1021 $class->SUPER::new ( 1245 $class->SUPER::new (
1023 bg => [0, 0, 0, 0.2], 1247 bg => [0, 0, 0, 0.2],
1024 active_bg => [1, 1, 1, 0.5], 1248 active_bg => [1, 1, 1, 0.5],
1025 active_fg => [0, 0, 0], 1249 active_fg => [0, 0, 0],
1026 can_hover => 1, 1250 can_hover => 1,
1027 can_focus => 1, 1251 can_focus => 1,
1252 valign => 0,
1253 can_events => 1,
1028 @_ 1254 @_
1029 ) 1255 )
1030} 1256}
1031 1257
1032sub _set_text { 1258sub _set_text {
1033 my ($self, $text) = @_; 1259 my ($self, $text) = @_;
1034 1260
1035 my $old_text = $self->{text}; 1261 delete $self->{cur_h};
1262
1263 return if $self->{text} eq $text;
1264
1265 delete $self->{texture};
1036 1266
1037 $self->{last_activity} = $::NOW; 1267 $self->{last_activity} = $::NOW;
1038
1039 $self->{text} = $text; 1268 $self->{text} = $text;
1040 $self->{layout}->set_width ($self->{w});
1041 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1042 1269
1043 $text =~ s/./*/g if $self->{hidden}; 1270 $text =~ s/./*/g if $self->{hidden};
1271 $self->{layout}->set_text ("$text ");
1044 1272
1045 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1273 $self->emit (changed => $self->{text});
1274}
1046 1275
1047 $text = substr $text, 0, $self->{cursor}; 1276sub get_text {
1048 utf8::encode $text; 1277 $_[0]{text}
1049
1050 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1051
1052 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1053 if $old_text ne $self->{text};
1054} 1278}
1055 1279
1056sub size_request { 1280sub size_request {
1057 my ($self) = @_; 1281 my ($self) = @_;
1058 1282
1060 1284
1061 ($w + 1, $h) # add 1 for cursor 1285 ($w + 1, $h) # add 1 for cursor
1062} 1286}
1063 1287
1064sub size_allocate { 1288sub size_allocate {
1065 my ($self, $x, $y, $w, $h) = @_; 1289 my ($self, $w, $h) = @_;
1066 1290
1067 $self->SUPER::size_allocate ($x, $y, $w, $h); 1291 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1068
1069 $self->_set_text ($self->{text});
1070} 1292}
1071 1293
1072sub set_text { 1294sub set_text {
1073 my ($self, $text) = @_; 1295 my ($self, $text) = @_;
1074 1296
1078} 1300}
1079 1301
1080sub key_down { 1302sub key_down {
1081 my ($self, $ev) = @_; 1303 my ($self, $ev) = @_;
1082 1304
1083 my $mod = $ev->key_mod; 1305 my $mod = $ev->{mod};
1084 my $sym = $ev->key_sym; 1306 my $sym = $ev->{sym};
1085
1086 my $uni = $ev->key_unicode; 1307 my $uni = $ev->{unicode};
1087 1308
1088 my $text = $self->get_text; 1309 my $text = $self->get_text;
1089 1310
1090 if ($sym == SDLK_BACKSPACE) { 1311 if ($sym == 8) {
1091 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1312 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1092 } elsif ($sym == SDLK_DELETE) { 1313 } elsif ($sym == 127) {
1093 substr $text, $self->{cursor}, 1, ""; 1314 substr $text, $self->{cursor}, 1, "";
1094 } elsif ($sym == SDLK_LEFT) { 1315 } elsif ($sym == CFClient::SDLK_LEFT) {
1095 --$self->{cursor} if $self->{cursor}; 1316 --$self->{cursor} if $self->{cursor};
1096 } elsif ($sym == SDLK_RIGHT) { 1317 } elsif ($sym == CFClient::SDLK_RIGHT) {
1097 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1318 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1098 } elsif ($sym == SDLK_HOME) { 1319 } elsif ($sym == CFClient::SDLK_HOME) {
1099 $self->{cursor} = 0; 1320 $self->{cursor} = 0;
1100 } elsif ($sym == SDLK_END) { 1321 } elsif ($sym == CFClient::SDLK_END) {
1101 $self->{cursor} = length $text; 1322 $self->{cursor} = length $text;
1102 } elsif ($sym == SDLK_ESCAPE) { 1323 } elsif ($sym == 27) {
1103 $self->emit ('escape'); 1324 $self->emit ('escape');
1104 } elsif ($uni) { 1325 } elsif ($uni) {
1105 substr $text, $self->{cursor}++, 0, chr $uni; 1326 substr $text, $self->{cursor}++, 0, chr $uni;
1106 } 1327 }
1107 1328
1162 1383
1163 $self->SUPER::_draw; 1384 $self->SUPER::_draw;
1164 1385
1165 #TODO: force update every cursor change :( 1386 #TODO: force update every cursor change :(
1166 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1387 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1388
1389 unless (exists $self->{cur_h}) {
1390 my $text = substr $self->{text}, 0, $self->{cursor};
1391 utf8::encode $text;
1392
1393 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1394 }
1395
1167 glColor @{$self->{fg}}; 1396 glColor @{$self->{fg}};
1168 glBegin GL_LINES; 1397 glBegin GL_LINES;
1169 glVertex $self->{cur_x}, $self->{cur_y};
1170 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1398 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1399 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1171 glEnd; 1400 glEnd;
1172 } 1401 }
1173} 1402}
1174 1403
1175package CFClient::UI::LineEntry; 1404package CFClient::UI::Entry;
1176 1405
1177our @ISA = CFClient::UI::Entry::; 1406our @ISA = CFClient::UI::EntryBase::;
1178 1407
1179use SDL; 1408use CFClient::OpenGL;
1180use SDL::OpenGL;
1181 1409
1182sub key_down { 1410sub key_down {
1183 my ($self, $ev) = @_; 1411 my ($self, $ev) = @_;
1184 1412
1185 my $sym = $ev->key_sym; 1413 my $sym = $ev->{sym};
1186 1414
1187 if ($sym == SDLK_RETURN) { 1415 if ($sym == 13) {
1188 $self->emit (activate => $self->get_text); 1416 $self->emit (activate => $self->get_text);
1189 $self->update; 1417 $self->update;
1190 1418
1191 } else { 1419 } else {
1192 $self->SUPER::key_down ($ev); 1420 $self->SUPER::key_down ($ev);
1198 1426
1199package CFClient::UI::Button; 1427package CFClient::UI::Button;
1200 1428
1201our @ISA = CFClient::UI::Label::; 1429our @ISA = CFClient::UI::Label::;
1202 1430
1203use SDL; 1431use CFClient::OpenGL;
1204use SDL::OpenGL;
1205 1432
1206my @tex = 1433my @tex =
1207 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1434 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1208 qw(b1_button_active.png); 1435 qw(b1_button_active.png);
1209 1436
1210sub new { 1437sub new {
1211 my $class = shift; 1438 my $class = shift;
1212 1439
1213 $class->SUPER::new ( 1440 $class->SUPER::new (
1214 padding => 4, 1441 padding => 4,
1215 fg => [1, 1, 1], 1442 fg => [1, 1, 1],
1216 bg => [1, 1, 1, 0.2], 1443 bg => [1, 1, 1, 0.2],
1217 active_fg => [1, 1, 0], 1444 active_fg => [0, 0, 1],
1218 can_hover => 1, 1445 can_hover => 1,
1446 align => 0,
1447 valign => 0,
1448 can_events => 1,
1219 @_ 1449 @_
1220 ) 1450 )
1221} 1451}
1222 1452
1223sub button_up { 1453sub button_up {
1231 1461
1232sub _draw { 1462sub _draw {
1233 my ($self) = @_; 1463 my ($self) = @_;
1234 1464
1235 local $self->{fg} = $self->{fg}; 1465 local $self->{fg} = $self->{fg};
1236 my $tex = $tex[0];
1237
1238 glEnable GL_BLEND;
1239 glEnable GL_TEXTURE_2D;
1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1241 1466
1242 if ($GRAB == $self) { 1467 if ($GRAB == $self) {
1243 $self->{fg} = $self->{active_fg}; 1468 $self->{fg} = $self->{active_fg};
1244 } 1469 }
1245 1470
1246 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1471 glEnable GL_BLEND;
1472 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1473 glEnable GL_TEXTURE_2D;
1247 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1474 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1475 glColor 0, 0, 0, 1;
1248 1476
1249 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1477 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1250 1478
1251 glDisable GL_TEXTURE_2D; 1479 glDisable GL_TEXTURE_2D;
1252 glDisable GL_BLEND; 1480 glDisable GL_BLEND;
1253 1481
1254 $self->SUPER::_draw; 1482 $self->SUPER::_draw;
1259package CFClient::UI::CheckBox; 1487package CFClient::UI::CheckBox;
1260 1488
1261our @ISA = CFClient::UI::DrawBG::; 1489our @ISA = CFClient::UI::DrawBG::;
1262 1490
1263my @tex = 1491my @tex =
1264 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1492 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1265 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1493 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1266 1494
1267use SDL; 1495use CFClient::OpenGL;
1268use SDL::OpenGL;
1269 1496
1270sub new { 1497sub new {
1271 my $class = shift; 1498 my $class = shift;
1272 1499
1273 $class->SUPER::new ( 1500 $class->SUPER::new (
1284 my ($self) = @_; 1511 my ($self) = @_;
1285 1512
1286 ($self->{padding} * 2 + 6) x 2 1513 ($self->{padding} * 2 + 6) x 2
1287} 1514}
1288 1515
1289sub size_allocate {
1290 my ($self, $x, $y, $w, $h) = @_;
1291
1292 $self->_size_allocate ($x, $y, $w, $h);
1293}
1294
1295sub button_down { 1516sub button_down {
1296 my ($self, $ev, $x, $y) = @_; 1517 my ($self, $ev, $x, $y) = @_;
1297 1518
1298 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1519 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1299 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1520 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1325 glDisable GL_BLEND; 1546 glDisable GL_BLEND;
1326} 1547}
1327 1548
1328############################################################################# 1549#############################################################################
1329 1550
1551package CFClient::UI::Image;
1552
1553our @ISA = CFClient::UI::Base::;
1554
1555use CFClient::OpenGL;
1556use Carp qw/confess/;
1557
1558our %loaded_images;
1559
1560sub new {
1561 my $class = shift;
1562
1563 my $self = $class->SUPER::new (can_events => 0, @_);
1564
1565 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1566
1567 $loaded_images{$self->{image}} ||=
1568 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1569
1570 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1571
1572 Scalar::Util::weaken $loaded_images{$self->{image}};
1573
1574 $self->{aspect} = $tex->{w} / $tex->{h};
1575
1576 $self
1577}
1578
1579sub size_request {
1580 my ($self) = @_;
1581
1582 ($self->{tex}->{w}, $self->{tex}->{h})
1583}
1584
1585sub _draw {
1586 my ($self) = @_;
1587
1588 my $tex = $self->{tex};
1589
1590 my ($w, $h) = ($self->{w}, $self->{h});
1591
1592 if ($self->{rot90}) {
1593 glRotate 90, 0, 0, 1;
1594 glTranslate 0, -$self->{w}, 0;
1595
1596 ($w, $h) = ($h, $w);
1597 }
1598
1599 glEnable GL_BLEND;
1600 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1601 glEnable GL_TEXTURE_2D;
1602 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1603
1604 $tex->draw_quad (0, 0, $w, $h);
1605
1606 glDisable GL_BLEND;
1607 glDisable GL_TEXTURE_2D;
1608}
1609
1610#############################################################################
1611
1612package CFClient::UI::VGauge;
1613
1614our @ISA = CFClient::UI::Base::;
1615
1616use List::Util qw(min max);
1617
1618use CFClient::OpenGL;
1619
1620my %tex = (
1621 food => [
1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1623 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1624 ],
1625 grace => [
1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1627 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1628 ],
1629 hp => [
1630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1631 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1632 ],
1633 mana => [
1634 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1635 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1636 ],
1637);
1638
1639# eg. VGauge->new (gauge => 'food'), default gauge: food
1640sub new {
1641 my $class = shift;
1642
1643 my $self = $class->SUPER::new (
1644 type => 'food',
1645 @_
1646 );
1647
1648 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1649
1650 $self
1651}
1652
1653sub size_request {
1654 my ($self) = @_;
1655
1656 #my $tex = $tex{$self->{type}}[0];
1657 #@$tex{qw(w h)}
1658 (0, 0)
1659}
1660
1661sub set_max {
1662 my ($self, $max) = @_;
1663
1664 $self->{max_val} = $max;
1665}
1666
1667sub set_value {
1668 my ($self, $val, $max) = @_;
1669
1670 $self->set_max ($max)
1671 if defined $max;
1672
1673 $max = $self->{max_val};
1674 $self->{val} = $val;
1675
1676 $self->update;
1677}
1678
1679sub _draw {
1680 my ($self) = @_;
1681
1682 my $tex = $tex{$self->{type}};
1683 my ($t1, $t2, $t3) = @$tex;
1684
1685 my ($w, $h) = ($self->{w}, $self->{h});
1686
1687 if ($self->{vertical}) {
1688 glRotate 90, 0, 0, 1;
1689 glTranslate 0, -$self->{w}, 0;
1690
1691 ($w, $h) = ($h, $w);
1692 }
1693
1694 my $ycut = $self->{val} / ($self->{max_val} || 1);
1695
1696 my $ycut1 = max 0, min 1, $ycut;
1697 my $ycut2 = max 0, min 1, $ycut - 1;
1698
1699 my $h1 = $self->{h} * (1 - $ycut1);
1700 my $h2 = $self->{h} * (1 - $ycut2);
1701
1702 glEnable GL_BLEND;
1703 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1704 glEnable GL_TEXTURE_2D;
1705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1706
1707 glBindTexture GL_TEXTURE_2D, $t1->{name};
1708 glBegin GL_QUADS;
1709 glTexCoord 0 , 0; glVertex 0 , 0;
1710 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1711 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1712 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1713 glEnd;
1714
1715 my $ycut1 = List::Util::min 1, $ycut;
1716 glBindTexture GL_TEXTURE_2D, $t2->{name};
1717 glBegin GL_QUADS;
1718 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1719 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1720 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1721 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1722 glEnd;
1723
1724 if ($t3) {
1725 glBindTexture GL_TEXTURE_2D, $t3->{name};
1726 glBegin GL_QUADS;
1727 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1728 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1729 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1730 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1731 glEnd;
1732 }
1733
1734 glDisable GL_BLEND;
1735 glDisable GL_TEXTURE_2D;
1736}
1737
1738#############################################################################
1739
1740package CFClient::UI::Gauge;
1741
1742our @ISA = CFClient::UI::VBox::;
1743
1744sub new {
1745 my ($class, %arg) = @_;
1746
1747 my $self = $class->SUPER::new (
1748 tooltip => $arg{type},
1749 %arg,
1750 );
1751
1752 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1);
1753 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1754 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1755
1756 $self
1757}
1758
1759sub set_fontsize {
1760 my ($self, $fsize) = @_;
1761
1762 $self->{value}->set_fontsize ($fsize);
1763 $self->{max} ->set_fontsize ($fsize);
1764}
1765
1766sub set_value {
1767 my ($self, $val, $max) = @_;
1768
1769 $self->set_max ($max)
1770 if defined $max;
1771
1772 $self->{gauge}->set_value ($val, $max);
1773 $self->{value}->set_text ($val);
1774}
1775
1776sub set_max {
1777 my ($self, $max) = @_;
1778
1779 $self->{gauge}->set_max ($max);
1780 $self->{max}->set_text ($max);
1781}
1782
1783#############################################################################
1784
1330package CFClient::UI::Slider; 1785package CFClient::UI::Slider;
1331 1786
1332use strict; 1787use strict;
1333 1788
1334use SDL::OpenGL; 1789use CFClient::OpenGL;
1335 1790
1336our @ISA = CFClient::UI::DrawBG::; 1791our @ISA = CFClient::UI::DrawBG::;
1337 1792
1338my @tex = 1793my @tex =
1339 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1794 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1349 # TODO: calculations are off 1804 # TODO: calculations are off
1350 my $self = $class->SUPER::new ( 1805 my $self = $class->SUPER::new (
1351 fg => [1, 1, 1], 1806 fg => [1, 1, 1],
1352 active_fg => [0, 0, 0], 1807 active_fg => [0, 0, 0],
1353 range => [0, 0, 100, 10], 1808 range => [0, 0, 100, 10],
1354 req_w => 40, 1809 req_w => 20,
1355 req_h => 13, 1810 req_h => 20,
1356 vertical => 0, 1811 vertical => 0,
1357 can_hover => 1, 1812 can_hover => 1,
1358 inner_pad => 5, 1813 inner_pad => 5,
1359 @_ 1814 @_
1360 ); 1815 );
1464 1919
1465package CFClient::UI::TextView; 1920package CFClient::UI::TextView;
1466 1921
1467our @ISA = CFClient::UI::HBox::; 1922our @ISA = CFClient::UI::HBox::;
1468 1923
1469use SDL::OpenGL; 1924use CFClient::OpenGL;
1470 1925
1471sub new { 1926sub new {
1472 my $class = shift; 1927 my $class = shift;
1473 1928
1474 my $self = $class->SUPER::new ( 1929 my $self = $class->SUPER::new (
1475 req_w => $::WIDTH / 6,
1476 req_h => $::HEIGHT / 6,
1477 fontsize => $::FONTSIZE, 1930 fontsize => 1,
1931 #font => default_font
1478 @_, 1932 @_,
1479 1933
1480 layout => (new CFClient::Layout), 1934 layout => (new CFClient::Layout),
1481 par => [], 1935 par => [],
1482 height => 0, 1936 height => 0,
1503sub text_height { 1957sub text_height {
1504 my ($self, $text) = @_; 1958 my ($self, $text) = @_;
1505 1959
1506 my $layout = $self->{layout}; 1960 my $layout = $self->{layout};
1507 1961
1508 $layout->set_height ($self->{fontsize}); 1962 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1509 $layout->set_width ($self->{w}); 1963 $layout->set_width ($self->{w});
1510 $layout->set_text ($text); 1964 $layout->set_text ($text);
1511 1965
1512 ($layout->size)[1] 1966 ($layout->size)[1]
1513} 1967}
1517 1971
1518 $self->{need_reflow}++; 1972 $self->{need_reflow}++;
1519 $self->update; 1973 $self->update;
1520} 1974}
1521 1975
1522sub size_request {
1523 my ($self) = @_;
1524
1525 ($self->{req_w}, $self->{req_h})
1526}
1527
1528sub size_allocate { 1976sub size_allocate {
1529 my ($self, $x, $y, $w, $h) = @_; 1977 my ($self, $w, $h) = @_;
1530 1978
1531 $self->SUPER::size_allocate ($x, $y, $w, $h); 1979 $self->SUPER::size_allocate ($w, $h);
1532 1980
1981 $self->{layout}->set_font ($self->{font}) if $self->{font};
1533 $self->{layout}->set_height ($self->{fontsize}); 1982 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1534 $self->{layout}->set_width ($self->{w}); 1983 $self->{layout}->set_width ($self->{children}[0]{w});
1535 1984
1536 $self->reflow; 1985 $self->reflow;
1537} 1986}
1538 1987
1539sub add_paragraph { 1988sub add_paragraph {
1558 2007
1559 return unless $self->{h} > 0; 2008 return unless $self->{h} > 0;
1560 2009
1561 delete $self->{texture}; 2010 delete $self->{texture};
1562 2011
1563 $TOPLEVEL->on_refresh ($self, sub { 2012 $ROOT->on_refresh ($self, sub {
1564 if (delete $self->{need_reflow}) { 2013 if (delete $self->{need_reflow}) {
1565 my $height = 0; 2014 my $height = 0;
1566 2015
1567 $height += $_->[0] = $self->text_height ($_->[2]) 2016 $height += $_->[0] = $self->text_height ($_->[2])
1568 for @{$self->{par}}; 2017 for @{$self->{par}};
1589 my $y1 = $top + $self->{h}; 2038 my $y1 = $top + $self->{h};
1590 2039
1591 my $y = 0; 2040 my $y = 0;
1592 2041
1593 my $layout = $self->{layout}; 2042 my $layout = $self->{layout};
2043
2044 $layout->set_font ($self->{font}) if $self->{font};
1594 2045
1595 for my $par (@{$self->{par}}) { 2046 for my $par (@{$self->{par}}) {
1596 my $h = $par->[0]; 2047 my $h = $par->[0];
1597 2048
1598 if ($y0 < $y + $h && $y < $y1) { 2049 if ($y0 < $y + $h && $y < $y1) {
1626 2077
1627} 2078}
1628 2079
1629############################################################################# 2080#############################################################################
1630 2081
1631package CFClient::UI::MapWidget;
1632
1633use strict;
1634
1635use List::Util qw(min max);
1636
1637use SDL;
1638use SDL::OpenGL;
1639
1640our @ISA = CFClient::UI::Base::;
1641
1642sub new {
1643 my $class = shift;
1644
1645 $class->SUPER::new (
1646 z => -1,
1647 can_focus => 1,
1648 list => (glGenLists 1),
1649 @_
1650 )
1651}
1652
1653sub key_down {
1654 print "MAPKEYDOWN\n";
1655}
1656
1657sub key_up {
1658}
1659
1660sub size_request {
1661 (
1662 1 + 32 * int $::WIDTH / 32,
1663 1 + 32 * int $::HEIGHT / 32,
1664 )
1665}
1666
1667sub update {
1668 my ($self) = @_;
1669
1670 $self->{need_update} = 1;
1671 $self->SUPER::update;
1672}
1673
1674sub draw {
1675 my ($self) = @_;
1676
1677 $self->{need_update}++;#d#
1678 if (delete $self->{need_update}) {
1679 glNewList $self->{list}, GL_COMPILE;
1680
1681 my $sw = int $::WIDTH / 32;
1682 my $sh = int $::HEIGHT / 32;
1683
1684 if ($::MAP) {
1685 my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh);
1686
1687 if ($::CFG->{fow_enable}) {
1688 if ($::CFG->{fow_smooth}) { # smooth fog of war
1689 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1690 glConvolutionFilter2D
1691 GL_CONVOLUTION_2D,
1692 GL_ALPHA,
1693 3, 3,
1694 GL_ALPHA, GL_FLOAT,
1695 pack "f*",
1696 0.1, 0.1, 0.1,
1697 0.1, 0.2, 0.1,
1698 0.1, 0.1, 0.1,
1699 ;
1700 glEnable GL_CONVOLUTION_2D;
1701 }
1702
1703 my $tex = new CFClient::Texture
1704 w => $w,
1705 h => $h,
1706 data => $data,
1707 internalformat => GL_ALPHA,
1708 format => GL_ALPHA;
1709
1710 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1711
1712 glEnable GL_BLEND;
1713 glEnable GL_TEXTURE_2D;
1714 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1715
1716 glColor +($::CFG->{fow_intensity}) x 3, 1;
1717 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1718
1719 glDisable GL_TEXTURE_2D;
1720 glDisable GL_BLEND;
1721 }
1722 }
1723
1724 glEndList;
1725 }
1726
1727 glCallList $self->{list};
1728
1729 if ($FOCUS != $self) {
1730 glColor 64/255, 64/255, 64/255;
1731 glLogicOp GL_AND;
1732 glEnable GL_COLOR_LOGIC_OP;
1733 glBegin GL_QUADS;
1734 glVertex 0, 0;
1735 glVertex 0, $::HEIGHT;
1736 glVertex $::WIDTH, $::HEIGHT;
1737 glVertex $::WIDTH, 0;
1738 glEnd;
1739 glDisable GL_COLOR_LOGIC_OP;
1740 }
1741}
1742
1743my %DIR = (
1744 SDLK_KP8, [1, "north"],
1745 SDLK_KP9, [2, "northeast"],
1746 SDLK_KP6, [3, "east"],
1747 SDLK_KP3, [4, "southeast"],
1748 SDLK_KP2, [5, "south"],
1749 SDLK_KP1, [6, "southwest"],
1750 SDLK_KP4, [7, "west"],
1751 SDLK_KP7, [8, "northwest"],
1752
1753 SDLK_UP, [1, "north"],
1754 SDLK_RIGHT, [3, "east"],
1755 SDLK_DOWN, [5, "south"],
1756 SDLK_LEFT, [7, "west"],
1757);
1758
1759sub key_down {
1760 my ($self, $ev) = @_;
1761
1762 my $mod = $ev->key_mod;
1763 my $sym = $ev->key_sym;
1764
1765 if ($sym == SDLK_KP5) {
1766 $::CONN->user_send ("command stay fire");
1767 } elsif ($sym == SDLK_a) {
1768 $::CONN->user_send ("command apply");
1769 } elsif ($sym == SDLK_QUOTE) {
1770 $self->emit ('activate_console');
1771 } elsif ($sym == SDLK_SLASH) {
1772 $self->emit ('activate_console' => '/');
1773 } elsif (exists $DIR{$sym}) {
1774 if ($mod & KMOD_SHIFT) {
1775 $self->{shft}++;
1776 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1777 } elsif ($mod & KMOD_CTRL) {
1778 $self->{ctrl}++;
1779 $::CONN->user_send ("command run $DIR{$sym}[0]");
1780 } else {
1781 $::CONN->user_send ("command $DIR{$sym}[1]");
1782 }
1783 }
1784}
1785
1786sub key_up {
1787 my ($self, $ev) = @_;
1788
1789 my $mod = $ev->key_mod;
1790 my $sym = $ev->key_sym;
1791
1792 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1793 $::CONN->user_send ("command fire_stop");
1794 }
1795 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1796 $::CONN->user_send ("command run_stop");
1797 }
1798}
1799
1800#############################################################################
1801
1802package CFClient::UI::Animator; 2082package CFClient::UI::Animator;
1803 2083
1804use SDL::OpenGL; 2084use CFClient::OpenGL;
1805 2085
1806our @ISA = CFClient::UI::Bin::; 2086our @ISA = CFClient::UI::Bin::;
1807 2087
1808sub moveto { 2088sub moveto {
1809 my ($self, $x, $y) = @_; 2089 my ($self, $x, $y) = @_;
1849 my $class = shift; 2129 my $class = shift;
1850 2130
1851 my $self = $class->SUPER::new ( 2131 my $self = $class->SUPER::new (
1852 state => 0, 2132 state => 0,
1853 connect_activate => \&toggle_flopper, 2133 connect_activate => \&toggle_flopper,
2134 can_events => 1,
1854 @_ 2135 @_
1855 ); 2136 );
1856 2137
1857 if ($self->{state}) { 2138 if ($self->{state}) {
1858 $self->{state} = 0; 2139 $self->{state} = 0;
1865sub toggle_flopper { 2146sub toggle_flopper {
1866 my ($self) = @_; 2147 my ($self) = @_;
1867 2148
1868 # TODO: use animation 2149 # TODO: use animation
1869 if ($self->{state} = !$self->{state}) { 2150 if ($self->{state} = !$self->{state}) {
1870 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2151 $CFClient::UI::ROOT->add ($self->{other});
1871 $self->{other}->move ( 2152 $self->{other}->move ($self->coord2global (0, $self->{h}));
1872 ($::WIDTH - $self->{other}{w}) * 0.5, 2153 $self->emit ("open");
1873 ($::HEIGHT - $self->{other}{h}) * 0.5,
1874 );
1875 } else { 2154 } else {
1876 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2155 $CFClient::UI::ROOT->remove ($self->{other});
2156 $self->emit ("close");
2157 }
2158
2159 $self->emit (changed => $self->{state});
2160}
2161
2162#############################################################################
2163
2164package CFClient::UI::Tooltip;
2165
2166our @ISA = CFClient::UI::Bin::;
2167
2168use CFClient::OpenGL;
2169
2170sub new {
2171 my $class = shift;
2172
2173 $class->SUPER::new (
2174 @_,
2175 can_events => 0,
2176 )
2177}
2178
2179sub set_text {
2180 my ($self, $text) = @_;
2181
2182 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2183 $self->{label}->set_text ($text);
2184 $self->add ($self->{label});
2185}
2186
2187sub size_request {
2188 my ($self) = @_;
2189
2190 $self->child->set_max_size ($::WIDTH * 0.2);
2191
2192 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2193
2194 ($w + 4, $h + 4)
2195}
2196
2197sub _draw {
2198 my ($self) = @_;
2199
2200 glPushMatrix;
2201 glTranslate 0.375, 0.375;
2202
2203 my ($w, $h) = @$self{qw(w h)};
2204
2205 glColor 1, 0.8, 0.4;
2206 glBegin GL_QUADS;
2207 glVertex 0 , 0;
2208 glVertex 0 , $h;
2209 glVertex $w, $h;
2210 glVertex $w, 0;
2211 glEnd;
1877 } 2212
1878} 2213 glColor 0, 0, 0;
2214 glBegin GL_LINE_LOOP;
2215 glVertex 0 , 0;
2216 glVertex 0 , $h;
2217 glVertex $w, $h;
2218 glVertex $w, 0;
2219 glEnd;
2220
2221 glPopMatrix;
1879 2222
1880############################################################################# 2223 glTranslate 2, 2;
2224 $self->SUPER::_draw;
2225}
1881 2226
2227#############################################################################
2228
1882package CFClient::UI::Toplevel; 2229package CFClient::UI::Root;
1883 2230
1884our @ISA = CFClient::UI::Container::; 2231our @ISA = CFClient::UI::Container::;
1885 2232
1886use SDL::OpenGL; 2233use CFClient::OpenGL;
2234
2235sub check_size {
2236 my ($self) = @_;
2237
2238 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2239}
1887 2240
1888sub size_request { 2241sub size_request {
1889 ($::WIDTH, $::HEIGHT) 2242 ($::WIDTH, $::HEIGHT)
1890} 2243}
1891 2244
1892sub size_allocate { 2245sub configure {
1893 my ($self, $x, $y, $w, $h) = @_; 2246 my ($self, $x, $y, $w, $h) = @_;
1894 2247
1895 $self->_size_allocate ($x, $y, $w, $h); 2248 $self->SUPER::configure ($x, $y, $w, $h);
1896 2249
1897 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1898 for @{$self->{children}}; 2250 for my $child (@{$self->{children}}) {
1899} 2251 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1900 2252
1901sub translate { 2253 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2254 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2255 $child->configure ($X, $Y, $W,$H);
2256 }
2257}
2258
2259sub _topleft {
1902 my ($self, $x, $y) = @_; 2260 my ($self, $x, $y) = @_;
1903 2261
1904 ($x, $y) 2262 ($x, $y)
1905} 2263}
1906 2264
1907sub update { 2265sub update {
1908 my ($self) = @_; 2266 my ($self) = @_;
1909 2267
1910 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2268 $self->check_size;
1911 ::refresh (); 2269 ::refresh ();
1912} 2270}
1913 2271
1914sub add { 2272sub add {
1915 my ($self, $widget) = @_; 2273 my ($self, $child) = @_;
1916 2274
2275 # integerize window positions
2276 $child->{x} = int $child->{x};
2277 $child->{y} = int $child->{y};
2278
1917 $self->SUPER::add ($widget); 2279 $self->SUPER::add ($child);
1918
1919 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1920} 2280}
1921 2281
1922sub on_refresh { 2282sub on_refresh {
1923 my ($self, $id, $cb) = @_; 2283 my ($self, $id, $cb) = @_;
1924 2284
1947 2307
1948############################################################################# 2308#############################################################################
1949 2309
1950package CFClient::UI; 2310package CFClient::UI;
1951 2311
2312$ROOT = new CFClient::UI::Root;
1952$TOPLEVEL = new CFClient::UI::Toplevel; 2313$TOOLTIP = new CFClient::UI::Tooltip;
1953 2314
19541 23151
1955 2316

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines