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.109 by root, Sat Apr 15 01:13:47 2006 UTC vs.
Revision 1.156 by elmex, Sun Apr 23 02:50:42 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;
169
170 $self->update;
107} 171}
108 172
109sub needs_redraw { 173sub needs_redraw {
110 0 174 0
111} 175}
112 176
113sub size_request { 177sub size_request {
114 require Carp; 178 require Carp;
115 Carp::confess "size_request is abtract"; 179 Carp::confess "size_request is abstract";
116} 180}
117 181
118sub _size_allocate { 182sub configure {
119 my ($self, $x, $y, $w, $h) = @_; 183 my ($self, $x, $y, $w, $h) = @_;
120 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) {
121 $self->{x} = $x; 198 $self->{x} = $x;
122 $self->{y} = $y; 199 $self->{y} = $y;
200 $self->update;
201 }
123 202
124 return unless $self->{w} != $w || $self->{h} != $h; 203 if ($self->{w} != $w || $self->{h} != $h) {
125
126 $self->{w} = $w; 204 $self->{w} = $w;
127 $self->{h} = $h; 205 $self->{h} = $h;
128 206
207 $self->size_allocate ($w, $h);
208 $self->update;
129 1 209 }
130} 210}
131 211
132sub size_allocate { 212sub size_allocate {
213 # nothing to be done
214}
215
216# return top left coordinates
217sub _topleft {
133 my ($self, $x, $y, $w, $h) = @_; 218 my ($self, $x, $y) = @_;
134 219
135 $self->_size_allocate ($x, $y, $w, $h); 220 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
136} 221}
137 222
138# translate global coordinates to local coordinate system 223# translate global coordinates to local coordinate system
139sub translate { 224sub coord2local {
140 my ($self, $x, $y) = @_; 225 my ($self, $x, $y) = @_;
141 226
142 $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)
143} 237}
144 238
145sub focus_in { 239sub focus_in {
146 my ($self) = @_; 240 my ($self) = @_;
147 241
148 return if $FOCUS == $self; 242 return if $FOCUS == $self;
149 return unless $self->{can_focus}; 243 return unless $self->{can_focus};
150 244
151 my $focus = $FOCUS; $FOCUS = $self; 245 my $focus = $FOCUS; $FOCUS = $self;
246
247 $self->emit (focus_in => $focus);
248
152 $focus->update if $focus; 249 $focus->update if $focus;
153 $FOCUS->update; 250 $FOCUS->update;
154} 251}
155 252
156sub focus_out { 253sub focus_out {
157 my ($self) = @_; 254 my ($self) = @_;
158 255
159 return unless $FOCUS == $self; 256 return unless $FOCUS == $self;
160 257
161 my $focus = $FOCUS; undef $FOCUS; 258 my $focus = $FOCUS; undef $FOCUS;
259
260 $self->emit (focus_out => $focus);
261
162 $focus->update if $focus; #? 262 $focus->update if $focus; #?
163} 263}
164 264
165sub mouse_motion { } 265sub mouse_motion { }
166sub button_up { } 266sub button_up { }
190 glPopMatrix; 290 glPopMatrix;
191 291
192 if ($self == $HOVER && $self->{can_hover}) { 292 if ($self == $HOVER && $self->{can_hover}) {
193 my ($x, $y) = @$self{qw(x y)}; 293 my ($x, $y) = @$self{qw(x y)};
194 294
195 glColor 0, 0, 1, 0.2; 295 glColor 1, 0.8, 0.5, 0.2;
196 glEnable GL_BLEND; 296 glEnable GL_BLEND;
197 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 297 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
198 glBegin GL_QUADS; 298 glBegin GL_QUADS;
199 glVertex $x , $y; 299 glVertex $x , $y;
200 glVertex $x + $self->{w}, $y; 300 glVertex $x + $self->{w}, $y;
212} 312}
213 313
214sub find_widget { 314sub find_widget {
215 my ($self, $x, $y) = @_; 315 my ($self, $x, $y) = @_;
216 316
317 return () unless $self->{can_events};
318
217 return $self 319 return $self
218 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 320 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
219 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 321 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
220 322
221 () 323 ()
222} 324}
223 325
224sub set_parent { 326sub set_parent {
225 my ($self, $par) = @_; 327 my ($self, $parent) = @_;
226 328
227 $self->{parent} = $par;
228 Scalar::Util::weaken $self->{parent}; 329 Scalar::Util::weaken ($self->{parent} = $parent);
229} 330}
230 331
231sub get_parent { 332sub check_size {
232 $_[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 }
233} 345}
234 346
235sub update { 347sub update {
236 my ($self) = @_; 348 my ($self) = @_;
237 349
240} 352}
241 353
242sub connect { 354sub connect {
243 my ($self, $signal, $cb) = @_; 355 my ($self, $signal, $cb) = @_;
244 356
245 push @{ $self->{cb}{$signal} }, $cb; 357 push @{ $self->{signal_cb}{$signal} }, $cb;
246} 358}
247 359
248sub emit { 360sub emit {
249 my ($self, $signal, @args) = @_; 361 my ($self, $signal, @args) = @_;
250 362
363 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
251 $_->($self, @args) 364 $cb->($self, @args);
252 for @{$self->{cb}{$signal} || []}; 365 }
253} 366}
254 367
255sub DESTROY { 368sub DESTROY {
256 my ($self) = @_; 369 my ($self) = @_;
257 370
263package CFClient::UI::DrawBG; 376package CFClient::UI::DrawBG;
264 377
265our @ISA = CFClient::UI::Base::; 378our @ISA = CFClient::UI::Base::;
266 379
267use strict; 380use strict;
268use SDL::OpenGL; 381use CFClient::OpenGL;
269 382
270sub new { 383sub new {
271 my $class = shift; 384 my $class = shift;
272 385
273 # range [value, low, high, page] 386 # range [value, low, high, page]
302 415
303package CFClient::UI::Empty; 416package CFClient::UI::Empty;
304 417
305our @ISA = CFClient::UI::Base::; 418our @ISA = CFClient::UI::Base::;
306 419
420sub new {
421 my ($class, %arg) = @_;
422 $class->SUPER::new (can_events => 0, %arg);
423}
424
307sub size_request { 425sub size_request {
308 (0, 0) 426 (0, 0)
309} 427}
310 428
311sub draw { } 429sub draw { }
319sub new { 437sub new {
320 my ($class, %arg) = @_; 438 my ($class, %arg) = @_;
321 439
322 my $children = delete $arg{children} || []; 440 my $children = delete $arg{children} || [];
323 441
324 my $self = $class->SUPER::new (children => [], %arg); 442 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
325 $self->add ($_) for @$children; 443 $self->add ($_) for @$children;
326 444
327 $self 445 $self
328} 446}
329 447
330sub add { 448sub add {
331 my ($self, $chld) = @_; 449 my ($self, $child) = @_;
332 450
333 $chld->set_parent ($self); 451 $child->set_parent ($self);
452
453 use sort 'stable';
334 454
335 $self->{children} = [ 455 $self->{children} = [
336 sort { $a->{z} <=> $b->{z} } 456 sort { $a->{z} <=> $b->{z} }
337 @{$self->{children}}, $chld 457 @{$self->{children}}, $child
338 ]; 458 ];
339 459
340 $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;
341 $self->update; 471 $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} 472}
351 473
352sub find_widget { 474sub find_widget {
353 my ($self, $x, $y) = @_; 475 my ($self, $x, $y) = @_;
354 476
384 506
385 $class->SUPER::new (children => [$child], %arg) 507 $class->SUPER::new (children => [$child], %arg)
386} 508}
387 509
388sub add { 510sub add {
389 my ($self, $widget) = @_; 511 my ($self, $child) = @_;
390 512
391 $self->{children} = []; 513 $self->{children} = [];
392 514
393 $self->SUPER::add ($widget); 515 $self->SUPER::add ($child);
394} 516}
395 517
396sub remove { 518sub remove {
397 my ($self, $widget) = @_; 519 my ($self, $widget) = @_;
398 520
407sub size_request { 529sub size_request {
408 $_[0]{children}[0]->size_request 530 $_[0]{children}[0]->size_request
409} 531}
410 532
411sub size_allocate { 533sub size_allocate {
412 my ($self, $x, $y, $w, $h) = @_; 534 my ($self, $w, $h) = @_;
413 535
414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
416 $self->{children}[0]->size_allocate (0, 0, $w, $h); 536 $self->{children}[0]->configure (0, 0, $w, $h);
417} 537}
418 538
419############################################################################# 539#############################################################################
420 540
421package CFClient::UI::Window; 541package CFClient::UI::Window;
422 542
423our @ISA = CFClient::UI::Bin::; 543our @ISA = CFClient::UI::Bin::;
424 544
425use SDL::OpenGL; 545use CFClient::OpenGL;
426 546
427sub new { 547sub new {
428 my ($class, %arg) = @_; 548 my ($class, %arg) = @_;
429 549
430 my $self = $class->SUPER::new (%arg); 550 my $self = $class->SUPER::new (%arg);
447 $self->child->draw; 567 $self->child->draw;
448 }; 568 };
449} 569}
450 570
451sub size_allocate { 571sub size_allocate {
452 my ($self, $x, $y, $w, $h) = @_; 572 my ($self, $w, $h) = @_;
453 573
454 $self->_size_allocate ($x, $y, $w, $h) or return;
455
456 $self->child->size_allocate (0, 0, $w, $h); 574 $self->child->configure (0, 0, $w, $h);
457 575
458 $self->render_chld; 576 $self->render_chld;
459} 577}
460 578
461sub _draw { 579sub _draw {
477 glDisable GL_TEXTURE_2D; 595 glDisable GL_TEXTURE_2D;
478} 596}
479 597
480############################################################################# 598#############################################################################
481 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
482package CFClient::UI::Frame; 624package CFClient::UI::Frame;
483 625
484our @ISA = CFClient::UI::Bin::; 626our @ISA = CFClient::UI::Bin::;
485 627
486use SDL::OpenGL; 628use 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 629
536sub new { 630sub new {
537 my $class = shift; 631 my $class = shift;
538 632
539 # TODO: user_x, user_y, overwrite moveto?
540
541 $class->SUPER::new ( 633 my $self = $class->SUPER::new (
542 bg => [1, 1, 1, 1], 634 bg => [1, 1, 1, 1],
543 border_bg => [1, 1, 1, 1], 635 border_bg => [1, 1, 1, 1],
544 border => $::FONTSIZE * 0.8, 636 border => 0.8,
545 @_ 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,
546 ) 734 )
547} 735}
548 736
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 { 737sub size_allocate {
563 my ($self, $x, $y, $w, $h) = @_; 738 my ($self, $w, $h) = @_;
564 739
565 $self->_size_allocate ($x, $y, $w, $h) or return;
566
567 $h -= List::Util::max 0, $self->{border} * 2; 740 $h -= List::Util::max 0, $self->border * 2;
568 $w -= List::Util::max 0, $self->{border} * 2; 741 $w -= List::Util::max 0, $self->border * 2;
569 742
743 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
744 if $self->{title};
745
570 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 746 $self->child->configure ($self->border, $self->border, $w, $h);
571} 747}
572 748
573sub button_down { 749sub button_down {
574 my ($self, $ev, $x, $y) = @_; 750 my ($self, $ev, $x, $y) = @_;
575 751
752 my $border = $self->border;
753
576 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 754 if ($x < $self->{w} && $x >= $self->{w} - $border
577 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 755 && $y < $self->{h} && $y >= $self->{h} - $border) {
578 756
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 757 my ($ox, $oy) = ($ev->{x}, $ev->{y});
580 my ($bw, $bh) = ($self->{w}, $self->{h}); 758 my ($bw, $bh) = ($self->{w}, $self->{h});
581 759
582 $self->{motion} = sub { 760 $self->{motion} = sub {
583 my ($ev, $x, $y) = @_; 761 my ($ev, $x, $y) = @_;
584 762
585 ($x, $y) = ($ev->motion_x, $ev->motion_y); 763 ($x, $y) = ($ev->{x}, $ev->{y});
586 764
587 $self->{user_w} = $bw + $x - $ox; 765 $self->{user_w} = $bw + $x - $ox;
588 $self->{user_h} = $bh + $y - $oy; 766 $self->{user_h} = $bh + $y - $oy;
589 $self->update; 767 $self->check_size;
590 }; 768 };
591 769
592 } elsif ($x >= 0 && $x < $self->{w} 770 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $self->{border}) { 771 && $y >= 0 && $y < $border) {
594 772
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 773 my ($ox, $oy) = ($ev->{x}, $ev->{y});
596 my ($bx, $by) = ($self->{x}, $self->{y}); 774 my ($bx, $by) = ($self->{x}, $self->{y});
597 775
598 $self->{motion} = sub { 776 $self->{motion} = sub {
599 my ($ev, $x, $y) = @_; 777 my ($ev, $x, $y) = @_;
600 778
601 ($x, $y) = ($ev->motion_x, $ev->motion_y); 779 ($x, $y) = ($ev->{x}, $ev->{y});
602 780
603 $self->move ($bx + $x - $ox, $by + $y - $oy); 781 $self->move ($bx + $x - $ox, $by + $y - $oy);
604 $self->update; 782 $self->update;
605 }; 783 };
606 } 784 }
627 glEnable GL_BLEND; 805 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
629 glEnable GL_TEXTURE_2D; 807 glEnable GL_TEXTURE_2D;
630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
631 809
810 my $border = $self->border;
811
632 glColor @{ $self->{border_bg} }; 812 glColor @{ $self->{border_bg} };
633 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 813 $tex[1]->draw_quad (0, 0, $w, $border);
634 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 814 $tex[3]->draw_quad (0, $border, $border, $ch);
635 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 815 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
636 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 816 $tex[4]->draw_quad (0, $h - $border, $w, $border);
637 817
638 my $bg = $tex[0]; 818 my $bg = $tex[0];
639 819
640 # TODO: repeat texture not scale 820 # TODO: repeat texture not scale
641 my $rep_x = $cw / $bg->{w}; 821 my $rep_x = $cw / $bg->{w};
644 glColor @{ $self->{bg} }; 824 glColor @{ $self->{bg} };
645 825
646 $bg->{s} = $rep_x; 826 $bg->{s} = $rep_x;
647 $bg->{t} = $rep_y; 827 $bg->{t} = $rep_y;
648 $bg->{wrap_mode} = 1; 828 $bg->{wrap_mode} = 1;
649 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 829 $bg->draw_quad ($border, $border, $cw, $ch);
650 830
651 glDisable GL_TEXTURE_2D; 831 glDisable GL_TEXTURE_2D;
652 glDisable GL_BLEND; 832 glDisable GL_BLEND;
653 833
834 $self->{title}->draw if $self->{title};
654 $self->child->draw; 835 $self->child->draw;
655} 836}
656 837
657############################################################################# 838#############################################################################
658 839
660 841
661our @ISA = CFClient::UI::Base::; 842our @ISA = CFClient::UI::Base::;
662 843
663use List::Util qw(max sum); 844use List::Util qw(max sum);
664 845
665use SDL::OpenGL; 846use CFClient::OpenGL;
666 847
667sub new { 848sub new {
668 my $class = shift; 849 my $class = shift;
669 850
670 $class->SUPER::new ( 851 $class->SUPER::new (
672 @_ 853 @_
673 ) 854 )
674} 855}
675 856
676sub add { 857sub add {
677 my ($self, $x, $y, $chld) = @_; 858 my ($self, $x, $y, $child) = @_;
678 859
860 $child->set_parent ($self);
679 $self->{children}[$y][$x] = $chld; 861 $self->{children}[$y][$x] = $child;
680 $chld->set_parent ($self);
681 862
682 $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};
683 $self->update; 871 $self->update;
684} 872}
685 873
686sub get_wh { 874sub get_wh {
687 my ($self) = @_; 875 my ($self) = @_;
693 or next; 881 or next;
694 882
695 for my $x (0 .. $#$row) { 883 for my $x (0 .. $#$row) {
696 my $widget = $row->[$x] 884 my $widget = $row->[$x]
697 or next; 885 or next;
698 my ($w, $h) = $widget->size_request; 886 my ($w, $h) = @$widget{qw(req_w req_h)};
699 887
700 $w[$x] = max $w[$x], $w; 888 $w[$x] = max $w[$x], $w;
701 $h[$y] = max $h[$y], $h; 889 $h[$y] = max $h[$y], $h;
702 } 890 }
703 } 891 }
715 (sum @$hs), 903 (sum @$hs),
716 ) 904 )
717} 905}
718 906
719sub size_allocate { 907sub size_allocate {
720 my ($self, $x, $y, $w, $h) = @_; 908 my ($self, $w, $h) = @_;
721
722 $self->_size_allocate ($x, $y, $w, $h) or return;
723 909
724 my ($ws, $hs) = $self->get_wh; 910 my ($ws, $hs) = $self->get_wh;
725 911
726 my $req_w = sum @$ws; 912 my $req_w = sum @$ws;
727 my $req_h = sum @$hs; 913 my $req_h = sum @$hs;
733 919
734 # linearly scale sizes 920 # linearly scale sizes
735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 921 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
736 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 922 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
737 923
738 $_ = int $_ for @$ws; #TODO: avoid rounding problems 924 CFClient::UI::harmonize $ws;
739 $_ = int $_ for @$hs; #TODO: avoid rounding problems 925 CFClient::UI::harmonize $hs;
740 926
741 my $y; 927 my $y;
742 928
743 for my $r (0 .. $#{$self->{children}}) { 929 for my $r (0 .. $#{$self->{children}}) {
744 my $row = $self->{children}[$r] 930 my $row = $self->{children}[$r]
749 935
750 for my $c (0 .. $#$row) { 936 for my $c (0 .. $#$row) {
751 my $col_w = $ws->[$c]; 937 my $col_w = $ws->[$c];
752 938
753 if (my $widget = $row->[$c]) { 939 if (my $widget = $row->[$c]) {
754 $widget->size_allocate ($x, $y, $col_w, $row_h); 940 $widget->configure ($x, $y, $col_w, $row_h);
755 } 941 }
756 942
757 $x += $col_w; 943 $x += $col_w;
758 } 944 }
759 945
804 (List::Util::max map $_->[1], @alloc), 990 (List::Util::max map $_->[1], @alloc),
805 ) 991 )
806} 992}
807 993
808sub size_allocate { 994sub size_allocate {
809 my ($self, $x, $y, $w, $h) = @_; 995 my ($self, $w, $h) = @_;
810
811 $self->_size_allocate ($x, $y, $w, $h);
812 996
813 ($h, $w) = ($w, $h); 997 ($h, $w) = ($w, $h);
814 998
815 my $children = $self->{children}; 999 my $children = $self->{children};
816 1000
817 my @h = map +($_->size_request)[0], @$children; 1001 my @h = map $_->{req_w}, @$children;
818 1002
819 my $req_h = List::Util::sum @h; 1003 my $req_h = List::Util::sum @h;
820 1004
821 if ($req_h > $h) { 1005 if ($req_h > $h) {
822 # ah well, not enough space 1006 # ah well, not enough space
832 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 1016 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
833 $h[$_] = $alloc_h; 1017 $h[$_] = $alloc_h;
834 } 1018 }
835 } 1019 }
836 1020
1021 CFClient::UI::harmonize \@h;
1022
837 my $y = 0; 1023 my $y = 0;
838 for (0 .. $#$children) { 1024 for (0 .. $#$children) {
839 my $child = $children->[$_]; 1025 my $child = $children->[$_];
840 my $h = int $h[$_]; 1026 my $h = $h[$_];
841 $child->size_allocate ($y, 0, $h, $w); 1027 $child->configure ($y, 0, $h, $w);
842 1028
843 $y += $h; 1029 $y += $h;
844 } 1030 }
1031
1032 1
845} 1033}
846 1034
847############################################################################# 1035#############################################################################
848 1036
849package CFClient::UI::VBox; 1037package CFClient::UI::VBox;
862 (List::Util::sum map $_->[1], @alloc), 1050 (List::Util::sum map $_->[1], @alloc),
863 ) 1051 )
864} 1052}
865 1053
866sub size_allocate { 1054sub size_allocate {
867 my ($self, $x, $y, $w, $h) = @_; 1055 my ($self, $w, $h) = @_;
868
869 $self->_size_allocate ($x, $y, $w, $h);
870 1056
871 my $children = $self->{children}; 1057 my $children = $self->{children};
872 1058
873 my @h = map +($_->size_request)[1], @$children; 1059 my @h = map $_->{req_h}, @$children;
874 1060
875 my $req_h = List::Util::sum @h; 1061 my $req_h = List::Util::sum @h;
876 1062
877 if ($req_h > $h) { 1063 if ($req_h > $h) {
878 # ah well, not enough space 1064 # ah well, not enough space
886 1072
887 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1073 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
888 } 1074 }
889 } 1075 }
890 1076
1077 CFClient::UI::harmonize \@h;
1078
891 my $y = 0; 1079 my $y = 0;
892 for (0 .. $#$children) { 1080 for (0 .. $#$children) {
893 my $child = $children->[$_]; 1081 my $child = $children->[$_];
894 my $h = int $h[$_]; 1082 my $h = $h[$_];
895 $child->size_allocate (0, $y, $w, $h); 1083 $child->configure (0, $y, $w, $h);
896 1084
897 $y += $h; 1085 $y += $h;
898 } 1086 }
1087
1088 1
899} 1089}
900 1090
901############################################################################# 1091#############################################################################
902 1092
903package CFClient::UI::Label; 1093package CFClient::UI::Label;
904 1094
905our @ISA = CFClient::UI::Base::; 1095our @ISA = CFClient::UI::Base::;
906 1096
907use SDL::OpenGL; 1097use CFClient::OpenGL;
908 1098
909sub new { 1099sub new {
910 my ($class, %arg) = @_; 1100 my ($class, %arg) = @_;
911 1101
912 my $self = $class->SUPER::new ( 1102 my $self = $class->SUPER::new (
913 fg => [1, 1, 1], 1103 fg => [1, 1, 1],
914 fontsize => $::FONTSIZE, 1104 fontsize => 1,
915 text => "", 1105 text => "",
916 align => -1, 1106 align => -1,
1107 valign => -1,
917 padding => 2, 1108 padding => 2,
918 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
919 %arg 1111 %arg
920 ); 1112 );
921 1113
922 $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};
923 1122
924 $self 1123 $self
925} 1124}
926 1125
927sub escape_text { 1126sub escape_text {
935} 1134}
936 1135
937sub set_text { 1136sub set_text {
938 my ($self, $text) = @_; 1137 my ($self, $text) = @_;
939 1138
940 $self->{text} = $text;
941 $self->{layout}->set_markup ($text); 1139 $self->{layout}->set_text ($text);
942 1140
943 delete $self->{texture}; 1141 delete $self->{texture};
944# $self->{w} = $self->{h} = -1; 1142 $self->check_size;
945 $self->update; 1143 $self->update;
946} 1144}
947 1145
948sub get_text { 1146sub set_markup {
949 my ($self, $text) = @_; 1147 my ($self, $markup) = @_;
950 1148
951 $self->{text} 1149 $self->{layout}->set_markup ($markup);
1150
1151 delete $self->{texture};
1152 $self->check_size;
1153 $self->update;
952} 1154}
953 1155
954sub size_request { 1156sub size_request {
955 my ($self) = @_; 1157 my ($self) = @_;
956 1158
957 $self->{layout}->set_width; 1159 $self->{layout}->set_width;
958 $self->{layout}->set_height ($self->{fontsize}); 1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1161
959 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 }
960 1172
961 ( 1173 (
962 $w + $self->{padding} * 2, 1174 $w + $self->{padding} * 2,
963 $h + $self->{padding} * 2, 1175 $h + $self->{padding} * 2,
964 ) 1176 )
965} 1177}
966 1178
967sub size_allocate { 1179sub size_allocate {
968 my ($self, $x, $y, $w, $h) = @_; 1180 my ($self, $w, $h) = @_;
969
970 $self->_size_allocate ($x, $y, $w, $h) or return;
971 1181
972 delete $self->{texture}; 1182 delete $self->{texture};
973} 1183}
974 1184
975sub update { 1185sub set_fontsize {
976 my ($self) = @_; 1186 my ($self, $fontsize) = @_;
977 1187
1188 $self->{fontsize} = $fontsize;
978 delete $self->{texture}; 1189 delete $self->{texture};
1190 $self->check_size;
979 $self->SUPER::update; 1191 $self->update;
980} 1192}
981 1193
982sub _draw { 1194sub _draw {
983 my ($self) = @_; 1195 my ($self) = @_;
984 1196
985 my $tex = $self->{texture} ||= do { 1197 my $tex = $self->{texture} ||= do {
986 $self->{layout}->set_width ($self->{w}); 1198 $self->{layout}->set_width ($self->{w});
987 $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);
988 new_from_layout CFClient::Texture $self->{layout} 1200 new_from_layout CFClient::Texture $self->{layout}
989 }; 1201 };
990 1202
991 glEnable GL_BLEND; 1203 glEnable GL_BLEND;
992 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
993 glEnable GL_TEXTURE_2D; 1205 glEnable GL_TEXTURE_2D;
994 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
995 1207
996 glColor @{$self->{fg}}; 1208 glColor @{$self->{fg}};
997 1209
998 my $x = 1210 $self->{ox} = int (
999 $self->{align} < 0 ? $self->{padding} 1211 $self->{align} < 0 ? $self->{padding}
1000 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1212 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1001 : ($self->{w} - $tex->{w}) * 0.5; 1213 : ($self->{w} - $tex->{w}) * 0.5
1214 );
1002 1215
1003 $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});
1004 1223
1005 glDisable GL_TEXTURE_2D; 1224 glDisable GL_TEXTURE_2D;
1006 glDisable GL_BLEND; 1225 glDisable GL_BLEND;
1007} 1226}
1008 1227
1009############################################################################# 1228#############################################################################
1010 1229
1011package CFClient::UI::Entry; 1230package CFClient::UI::EntryBase;
1012 1231
1013our @ISA = CFClient::UI::Label::; 1232our @ISA = CFClient::UI::Label::;
1014 1233
1015use SDL; 1234use CFClient::OpenGL;
1016use SDL::OpenGL;
1017 1235
1018sub new { 1236sub new {
1019 my $class = shift; 1237 my $class = shift;
1020 1238
1021 $class->SUPER::new ( 1239 $class->SUPER::new (
1023 bg => [0, 0, 0, 0.2], 1241 bg => [0, 0, 0, 0.2],
1024 active_bg => [1, 1, 1, 0.5], 1242 active_bg => [1, 1, 1, 0.5],
1025 active_fg => [0, 0, 0], 1243 active_fg => [0, 0, 0],
1026 can_hover => 1, 1244 can_hover => 1,
1027 can_focus => 1, 1245 can_focus => 1,
1246 valign => 0,
1247 can_events => 1,
1028 @_ 1248 @_
1029 ) 1249 )
1030} 1250}
1031 1251
1032sub _set_text { 1252sub _set_text {
1033 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1034 1254
1035 my $old_text = $self->{text}; 1255 delete $self->{cur_h};
1256
1257 return if $self->{text} eq $text;
1258
1259 delete $self->{texture};
1036 1260
1037 $self->{last_activity} = $::NOW; 1261 $self->{last_activity} = $::NOW;
1038
1039 $self->{text} = $text; 1262 $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 1263
1043 $text =~ s/./*/g if $self->{hidden}; 1264 $text =~ s/./*/g if $self->{hidden};
1265 $self->{layout}->set_text ("$text ");
1044 1266
1045 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1267 $self->emit (changed => $self->{text});
1268}
1046 1269
1047 $text = substr $text, 0, $self->{cursor}; 1270sub get_text {
1048 utf8::encode $text; 1271 $_[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} 1272}
1055 1273
1056sub size_request { 1274sub size_request {
1057 my ($self) = @_; 1275 my ($self) = @_;
1058 1276
1060 1278
1061 ($w + 1, $h) # add 1 for cursor 1279 ($w + 1, $h) # add 1 for cursor
1062} 1280}
1063 1281
1064sub size_allocate { 1282sub size_allocate {
1065 my ($self, $x, $y, $w, $h) = @_; 1283 my ($self, $w, $h) = @_;
1066 1284
1067 $self->SUPER::size_allocate ($x, $y, $w, $h); 1285 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1068
1069 $self->_set_text ($self->{text});
1070} 1286}
1071 1287
1072sub set_text { 1288sub set_text {
1073 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1074 1290
1078} 1294}
1079 1295
1080sub key_down { 1296sub key_down {
1081 my ($self, $ev) = @_; 1297 my ($self, $ev) = @_;
1082 1298
1083 my $mod = $ev->key_mod; 1299 my $mod = $ev->{mod};
1084 my $sym = $ev->key_sym; 1300 my $sym = $ev->{sym};
1085
1086 my $uni = $ev->key_unicode; 1301 my $uni = $ev->{unicode};
1087 1302
1088 my $text = $self->get_text; 1303 my $text = $self->get_text;
1089 1304
1090 if ($sym == SDLK_BACKSPACE) { 1305 if ($sym == 8) {
1091 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1306 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1092 } elsif ($sym == SDLK_DELETE) { 1307 } elsif ($sym == 127) {
1093 substr $text, $self->{cursor}, 1, ""; 1308 substr $text, $self->{cursor}, 1, "";
1094 } elsif ($sym == SDLK_LEFT) { 1309 } elsif ($sym == CFClient::SDLK_LEFT) {
1095 --$self->{cursor} if $self->{cursor}; 1310 --$self->{cursor} if $self->{cursor};
1096 } elsif ($sym == SDLK_RIGHT) { 1311 } elsif ($sym == CFClient::SDLK_RIGHT) {
1097 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1312 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1098 } elsif ($sym == SDLK_HOME) { 1313 } elsif ($sym == CFClient::SDLK_HOME) {
1099 $self->{cursor} = 0; 1314 $self->{cursor} = 0;
1100 } elsif ($sym == SDLK_END) { 1315 } elsif ($sym == CFClient::SDLK_END) {
1101 $self->{cursor} = length $text; 1316 $self->{cursor} = length $text;
1102 } elsif ($sym == SDLK_ESCAPE) { 1317 } elsif ($sym == 27) {
1103 $self->emit ('escape'); 1318 $self->emit ('escape');
1104 } elsif ($uni) { 1319 } elsif ($uni) {
1105 substr $text, $self->{cursor}++, 0, chr $uni; 1320 substr $text, $self->{cursor}++, 0, chr $uni;
1106 } 1321 }
1107 1322
1162 1377
1163 $self->SUPER::_draw; 1378 $self->SUPER::_draw;
1164 1379
1165 #TODO: force update every cursor change :( 1380 #TODO: force update every cursor change :(
1166 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
1167 glColor @{$self->{fg}}; 1390 glColor @{$self->{fg}};
1168 glBegin GL_LINES; 1391 glBegin GL_LINES;
1169 glVertex $self->{cur_x}, $self->{cur_y};
1170 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};
1171 glEnd; 1394 glEnd;
1172 } 1395 }
1173} 1396}
1174 1397
1175package CFClient::UI::LineEntry; 1398package CFClient::UI::Entry;
1176 1399
1177our @ISA = CFClient::UI::Entry::; 1400our @ISA = CFClient::UI::EntryBase::;
1178 1401
1179use SDL; 1402use CFClient::OpenGL;
1180use SDL::OpenGL;
1181 1403
1182sub key_down { 1404sub key_down {
1183 my ($self, $ev) = @_; 1405 my ($self, $ev) = @_;
1184 1406
1185 my $sym = $ev->key_sym; 1407 my $sym = $ev->{sym};
1186 1408
1187 if ($sym == SDLK_RETURN) { 1409 if ($sym == 13) {
1188 $self->emit (activate => $self->get_text); 1410 $self->emit (activate => $self->get_text);
1189 $self->update; 1411 $self->update;
1190 1412
1191 } else { 1413 } else {
1192 $self->SUPER::key_down ($ev); 1414 $self->SUPER::key_down ($ev);
1198 1420
1199package CFClient::UI::Button; 1421package CFClient::UI::Button;
1200 1422
1201our @ISA = CFClient::UI::Label::; 1423our @ISA = CFClient::UI::Label::;
1202 1424
1203use SDL; 1425use CFClient::OpenGL;
1204use SDL::OpenGL;
1205 1426
1206my @tex = 1427my @tex =
1207 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1208 qw(b1_button_active.png); 1429 qw(b1_button_active.png);
1209 1430
1210sub new { 1431sub new {
1211 my $class = shift; 1432 my $class = shift;
1212 1433
1213 $class->SUPER::new ( 1434 $class->SUPER::new (
1214 padding => 4, 1435 padding => 4,
1215 fg => [1, 1, 1], 1436 fg => [1, 1, 1],
1216 bg => [1, 1, 1, 0.2], 1437 bg => [1, 1, 1, 0.2],
1217 active_fg => [1, 1, 0], 1438 active_fg => [0, 0, 1],
1218 can_hover => 1, 1439 can_hover => 1,
1440 align => 0,
1441 valign => 0,
1442 can_events => 1,
1219 @_ 1443 @_
1220 ) 1444 )
1221} 1445}
1222 1446
1223sub button_up { 1447sub button_up {
1231 1455
1232sub _draw { 1456sub _draw {
1233 my ($self) = @_; 1457 my ($self) = @_;
1234 1458
1235 local $self->{fg} = $self->{fg}; 1459 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 1460
1242 if ($GRAB == $self) { 1461 if ($GRAB == $self) {
1243 $self->{fg} = $self->{active_fg}; 1462 $self->{fg} = $self->{active_fg};
1244 } 1463 }
1245 1464
1246 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;
1247 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;
1248 1470
1249 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1471 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1250 1472
1251 glDisable GL_TEXTURE_2D; 1473 glDisable GL_TEXTURE_2D;
1252 glDisable GL_BLEND; 1474 glDisable GL_BLEND;
1253 1475
1254 $self->SUPER::_draw; 1476 $self->SUPER::_draw;
1259package CFClient::UI::CheckBox; 1481package CFClient::UI::CheckBox;
1260 1482
1261our @ISA = CFClient::UI::DrawBG::; 1483our @ISA = CFClient::UI::DrawBG::;
1262 1484
1263my @tex = 1485my @tex =
1264 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1486 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1265 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1487 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1266 1488
1267use SDL; 1489use CFClient::OpenGL;
1268use SDL::OpenGL;
1269 1490
1270sub new { 1491sub new {
1271 my $class = shift; 1492 my $class = shift;
1272 1493
1273 $class->SUPER::new ( 1494 $class->SUPER::new (
1284 my ($self) = @_; 1505 my ($self) = @_;
1285 1506
1286 ($self->{padding} * 2 + 6) x 2 1507 ($self->{padding} * 2 + 6) x 2
1287} 1508}
1288 1509
1289sub size_allocate {
1290 my ($self, $x, $y, $w, $h) = @_;
1291
1292 $self->_size_allocate ($x, $y, $w, $h);
1293}
1294
1295sub button_down { 1510sub button_down {
1296 my ($self, $ev, $x, $y) = @_; 1511 my ($self, $ev, $x, $y) = @_;
1297 1512
1298 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1513 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1299 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1514 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1325 glDisable GL_BLEND; 1540 glDisable GL_BLEND;
1326} 1541}
1327 1542
1328############################################################################# 1543#############################################################################
1329 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", can_events => 1, can_hover => 1);
1734 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1735 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
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
1330package CFClient::UI::Slider; 1766package CFClient::UI::Slider;
1331 1767
1332use strict; 1768use strict;
1333 1769
1334use SDL::OpenGL; 1770use CFClient::OpenGL;
1335 1771
1336our @ISA = CFClient::UI::DrawBG::; 1772our @ISA = CFClient::UI::DrawBG::;
1337 1773
1338my @tex = 1774my @tex =
1339 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1775 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1349 # TODO: calculations are off 1785 # TODO: calculations are off
1350 my $self = $class->SUPER::new ( 1786 my $self = $class->SUPER::new (
1351 fg => [1, 1, 1], 1787 fg => [1, 1, 1],
1352 active_fg => [0, 0, 0], 1788 active_fg => [0, 0, 0],
1353 range => [0, 0, 100, 10], 1789 range => [0, 0, 100, 10],
1354 req_w => 40, 1790 req_w => 20,
1355 req_h => 13, 1791 req_h => 20,
1356 vertical => 0, 1792 vertical => 0,
1357 can_hover => 1, 1793 can_hover => 1,
1358 inner_pad => 5, 1794 inner_pad => 5,
1359 @_ 1795 @_
1360 ); 1796 );
1464 1900
1465package CFClient::UI::TextView; 1901package CFClient::UI::TextView;
1466 1902
1467our @ISA = CFClient::UI::HBox::; 1903our @ISA = CFClient::UI::HBox::;
1468 1904
1469use SDL::OpenGL; 1905use CFClient::OpenGL;
1470 1906
1471sub new { 1907sub new {
1472 my $class = shift; 1908 my $class = shift;
1473 1909
1474 my $self = $class->SUPER::new ( 1910 my $self = $class->SUPER::new (
1475 req_w => $::WIDTH / 6,
1476 req_h => $::HEIGHT / 6,
1477 fontsize => $::FONTSIZE, 1911 fontsize => 1,
1478 @_, 1912 @_,
1479 1913
1480 layout => (new CFClient::Layout), 1914 layout => (new CFClient::Layout),
1481 par => [], 1915 par => [],
1482 height => 0, 1916 height => 0,
1503sub text_height { 1937sub text_height {
1504 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1505 1939
1506 my $layout = $self->{layout}; 1940 my $layout = $self->{layout};
1507 1941
1508 $layout->set_height ($self->{fontsize}); 1942 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1509 $layout->set_width ($self->{w}); 1943 $layout->set_width ($self->{w});
1510 $layout->set_text ($text); 1944 $layout->set_text ($text);
1511 1945
1512 ($layout->size)[1] 1946 ($layout->size)[1]
1513} 1947}
1517 1951
1518 $self->{need_reflow}++; 1952 $self->{need_reflow}++;
1519 $self->update; 1953 $self->update;
1520} 1954}
1521 1955
1522sub size_request {
1523 my ($self) = @_;
1524
1525 ($self->{req_w}, $self->{req_h})
1526}
1527
1528sub size_allocate { 1956sub size_allocate {
1529 my ($self, $x, $y, $w, $h) = @_; 1957 my ($self, $w, $h) = @_;
1530 1958
1531 $self->SUPER::size_allocate ($x, $y, $w, $h); 1959 $self->SUPER::size_allocate ($w, $h);
1532 1960
1533 $self->{layout}->set_height ($self->{fontsize}); 1961 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1534 $self->{layout}->set_width ($self->{w}); 1962 $self->{layout}->set_width ($self->{children}[0]{w});
1535 1963
1536 $self->reflow; 1964 $self->reflow;
1537} 1965}
1538 1966
1539sub add_paragraph { 1967sub add_paragraph {
1558 1986
1559 return unless $self->{h} > 0; 1987 return unless $self->{h} > 0;
1560 1988
1561 delete $self->{texture}; 1989 delete $self->{texture};
1562 1990
1563 $TOPLEVEL->on_refresh ($self, sub { 1991 $ROOT->on_refresh ($self, sub {
1564 if (delete $self->{need_reflow}) { 1992 if (delete $self->{need_reflow}) {
1565 my $height = 0; 1993 my $height = 0;
1566 1994
1567 $height += $_->[0] = $self->text_height ($_->[2]) 1995 $height += $_->[0] = $self->text_height ($_->[2])
1568 for @{$self->{par}}; 1996 for @{$self->{par}};
1626 2054
1627} 2055}
1628 2056
1629############################################################################# 2057#############################################################################
1630 2058
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 button_down {
1661 my ($self, $ev, $x, $y) = @_;
1662
1663 $self->focus_in;
1664
1665 if ($ev->button == 2) {
1666 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1667 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1668
1669 $self->{motion} = sub {
1670 my ($ev, $x, $y) = @_;
1671
1672 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1673
1674 $::CFG->{map_shift_x} = $bw + $x - $ox;
1675 $::CFG->{map_shift_y} = $bh + $y - $oy;
1676
1677 $self->update;
1678 };
1679 }
1680}
1681
1682sub button_up {
1683 my ($self, $ev, $x, $y) = @_;
1684
1685 delete $self->{motion};
1686}
1687
1688sub mouse_motion {
1689 my ($self, $ev, $x, $y) = @_;
1690
1691 $self->{motion}->($ev, $x, $y) if $self->{motion};
1692}
1693
1694sub size_request {
1695 (
1696 1 + 32 * int $::WIDTH / 32,
1697 1 + 32 * int $::HEIGHT / 32,
1698 )
1699}
1700
1701sub update {
1702 my ($self) = @_;
1703
1704 $self->{need_update} = 1;
1705 $self->SUPER::update;
1706}
1707
1708sub draw {
1709 my ($self) = @_;
1710
1711 if (delete $self->{need_update}) {
1712 glNewList $self->{list}, GL_COMPILE;
1713
1714 if ($::MAP) {
1715 my $sw = int $::WIDTH / 32;
1716 my $sh = int $::HEIGHT / 32;
1717
1718 my $sx = $::CFG->{map_shift_x};
1719 my $sy = $::CFG->{map_shift_y};
1720
1721 glTranslate +($sx & 31) - 32, ($sy & 31) - 32, 0;
1722
1723 my ($w, $h, $data) = $::MAP->draw ($sx >> 5, $sy >> 5, 0, 0, $sw + 1, $sh + 1);
1724
1725 if ($::CFG->{fow_enable}) {
1726 if ($::CFG->{fow_smooth}) { # smooth fog of war
1727 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1728 glConvolutionFilter2D
1729 GL_CONVOLUTION_2D,
1730 GL_ALPHA,
1731 3, 3,
1732 GL_ALPHA, GL_FLOAT,
1733 pack "f*",
1734 0.1, 0.1, 0.1,
1735 0.1, 0.2, 0.1,
1736 0.1, 0.1, 0.1,
1737 ;
1738 glEnable GL_CONVOLUTION_2D;
1739 }
1740
1741 my $tex = new CFClient::Texture
1742 w => $w,
1743 h => $h,
1744 data => $data,
1745 internalformat => GL_ALPHA,
1746 format => GL_ALPHA;
1747
1748 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1749
1750 glEnable GL_BLEND;
1751 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1752 glEnable GL_TEXTURE_2D;
1753 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1754
1755 glColor +($::CFG->{fow_intensity}) x 3, 1;
1756 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1757
1758 glDisable GL_TEXTURE_2D;
1759 glDisable GL_BLEND;
1760 }
1761
1762 # HACK BEGIN
1763 {
1764 my ($w, $h) = (250, 250);
1765
1766 glEnable GL_BLEND;
1767 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1768 glEnable GL_TEXTURE_2D;
1769 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1770
1771 CFClient::Texture->new (
1772 w => $w,
1773 h => $h,
1774 data => $::MAP->mapmap ($w, $h),
1775 type => GL_UNSIGNED_INT_8_8_8_8_REV
1776 )->draw_quad (100, 100);
1777
1778 glDisable GL_TEXTURE_2D;
1779 glDisable GL_BLEND;
1780 }
1781 # HACK END
1782 }
1783
1784 glEndList;
1785 }
1786
1787 glPushMatrix;
1788 glCallList $self->{list};
1789 glPopMatrix;
1790
1791 if ($FOCUS != $self) {
1792 glColor 64/255, 64/255, 64/255;
1793 glLogicOp GL_AND;
1794 glEnable GL_COLOR_LOGIC_OP;
1795 glBegin GL_QUADS;
1796 glVertex 0, 0;
1797 glVertex 0, $::HEIGHT;
1798 glVertex $::WIDTH, $::HEIGHT;
1799 glVertex $::WIDTH, 0;
1800 glEnd;
1801 glDisable GL_COLOR_LOGIC_OP;
1802 }
1803}
1804
1805my %DIR = (
1806 SDLK_KP8, [1, "north"],
1807 SDLK_KP9, [2, "northeast"],
1808 SDLK_KP6, [3, "east"],
1809 SDLK_KP3, [4, "southeast"],
1810 SDLK_KP2, [5, "south"],
1811 SDLK_KP1, [6, "southwest"],
1812 SDLK_KP4, [7, "west"],
1813 SDLK_KP7, [8, "northwest"],
1814
1815 SDLK_UP, [1, "north"],
1816 SDLK_RIGHT, [3, "east"],
1817 SDLK_DOWN, [5, "south"],
1818 SDLK_LEFT, [7, "west"],
1819);
1820
1821sub key_down {
1822 my ($self, $ev) = @_;
1823
1824 my $mod = $ev->key_mod;
1825 my $sym = $ev->key_sym;
1826
1827 if ($sym == SDLK_KP5) {
1828 $::CONN->user_send ("command stay fire");
1829 } elsif ($sym == SDLK_a) {
1830 $::CONN->user_send ("command apply");
1831 } elsif ($sym == SDLK_QUOTE) {
1832 $self->emit ('activate_console');
1833 } elsif ($sym == SDLK_SLASH) {
1834 $self->emit ('activate_console' => '/');
1835 } elsif (exists $DIR{$sym}) {
1836 if ($mod & KMOD_SHIFT) {
1837 $self->{shft}++;
1838 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1839 } elsif ($mod & KMOD_CTRL) {
1840 $self->{ctrl}++;
1841 $::CONN->user_send ("command run $DIR{$sym}[0]");
1842 } else {
1843 $::CONN->user_send ("command $DIR{$sym}[1]");
1844 }
1845 }
1846}
1847
1848sub key_up {
1849 my ($self, $ev) = @_;
1850
1851 my $mod = $ev->key_mod;
1852 my $sym = $ev->key_sym;
1853
1854 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1855 $::CONN->user_send ("command fire_stop");
1856 }
1857 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1858 $::CONN->user_send ("command run_stop");
1859 }
1860}
1861
1862#############################################################################
1863
1864package CFClient::UI::Animator; 2059package CFClient::UI::Animator;
1865 2060
1866use SDL::OpenGL; 2061use CFClient::OpenGL;
1867 2062
1868our @ISA = CFClient::UI::Bin::; 2063our @ISA = CFClient::UI::Bin::;
1869 2064
1870sub moveto { 2065sub moveto {
1871 my ($self, $x, $y) = @_; 2066 my ($self, $x, $y) = @_;
1911 my $class = shift; 2106 my $class = shift;
1912 2107
1913 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
1914 state => 0, 2109 state => 0,
1915 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
1916 @_ 2112 @_
1917 ); 2113 );
1918 2114
1919 if ($self->{state}) { 2115 if ($self->{state}) {
1920 $self->{state} = 0; 2116 $self->{state} = 0;
1927sub toggle_flopper { 2123sub toggle_flopper {
1928 my ($self) = @_; 2124 my ($self) = @_;
1929 2125
1930 # TODO: use animation 2126 # TODO: use animation
1931 if ($self->{state} = !$self->{state}) { 2127 if ($self->{state} = !$self->{state}) {
1932 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2128 $CFClient::UI::ROOT->add ($self->{other});
1933 $self->{other}->move ( 2129 $self->{other}->move ($self->coord2global (0, $self->{h}));
1934 ($::WIDTH - $self->{other}{w}) * 0.5, 2130 $self->emit ("open");
1935 ($::HEIGHT - $self->{other}{h}) * 0.5,
1936 );
1937 } else { 2131 } else {
1938 $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;
1939 } 2190
1940} 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;
1941 2200
1942############################################################################# 2201 glTranslate 2, 2;
2202 $self->SUPER::_draw;
2203}
1943 2204
2205#############################################################################
2206
1944package CFClient::UI::Toplevel; 2207package CFClient::UI::Root;
1945 2208
1946our @ISA = CFClient::UI::Container::; 2209our @ISA = CFClient::UI::Container::;
1947 2210
1948use SDL::OpenGL; 2211use CFClient::OpenGL;
2212
2213sub check_size {
2214 my ($self) = @_;
2215
2216 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2217}
1949 2218
1950sub size_request { 2219sub size_request {
1951 ($::WIDTH, $::HEIGHT) 2220 ($::WIDTH, $::HEIGHT)
1952} 2221}
1953 2222
1954sub size_allocate { 2223sub configure {
1955 my ($self, $x, $y, $w, $h) = @_; 2224 my ($self, $x, $y, $w, $h) = @_;
1956 2225
1957 $self->_size_allocate ($x, $y, $w, $h); 2226 $self->SUPER::configure ($x, $y, $w, $h);
1958 2227
1959 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1960 for @{$self->{children}}; 2228 for my $child (@{$self->{children}}) {
1961} 2229 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1962 2230
1963sub 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 {
1964 my ($self, $x, $y) = @_; 2238 my ($self, $x, $y) = @_;
1965 2239
1966 ($x, $y) 2240 ($x, $y)
1967} 2241}
1968 2242
1969sub update { 2243sub update {
1970 my ($self) = @_; 2244 my ($self) = @_;
1971 2245
1972 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2246 $self->check_size;
1973 ::refresh (); 2247 ::refresh ();
1974} 2248}
1975 2249
1976sub add { 2250sub add {
1977 my ($self, $widget) = @_; 2251 my ($self, $child) = @_;
1978 2252
2253 # integerize window positions
2254 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y};
2256
1979 $self->SUPER::add ($widget); 2257 $self->SUPER::add ($child);
1980
1981 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1982} 2258}
1983 2259
1984sub on_refresh { 2260sub on_refresh {
1985 my ($self, $id, $cb) = @_; 2261 my ($self, $id, $cb) = @_;
1986 2262
2009 2285
2010############################################################################# 2286#############################################################################
2011 2287
2012package CFClient::UI; 2288package CFClient::UI;
2013 2289
2290$ROOT = new CFClient::UI::Root;
2014$TOPLEVEL = new CFClient::UI::Toplevel; 2291$TOOLTIP = new CFClient::UI::Tooltip;
2015 2292
20161 22931
2017 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines