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.104 by elmex, Fri Apr 14 14:02:52 2006 UTC vs.
Revision 1.155 by root, Sun Apr 23 02:22:54 2006 UTC

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_text ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 48 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 49}
23 50
24sub feed_sdl_button_down_event { 51sub feed_sdl_button_down_event {
25 my ($ev) = @_; 52 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 53 my ($x, $y) = ($ev->{x}, $ev->{y});
27 54
28 if (!$BUTTON_STATE) { 55 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 56 my $widget = $ROOT->find_widget ($x, $y);
30 57
31 $GRAB = $widget; 58 $GRAB = $widget;
32 $GRAB->update if $GRAB; 59 $GRAB->update if $GRAB;
33 }
34 60
61 check_tooltip;
62 }
63
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 65
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 67}
39 68
40sub feed_sdl_button_up_event { 69sub feed_sdl_button_up_event {
41 my ($ev) = @_; 70 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 71 my ($x, $y) = ($ev->{x}, $ev->{y});
43 72
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 73 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 74
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 76
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 78
50 if (!$BUTTON_STATE) { 79 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 80 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 81 $grab->update if $grab;
53 $GRAB->update if $GRAB; 82 $GRAB->update if $GRAB;
83
84 check_tooltip;
54 } 85 }
55} 86}
56 87
57sub feed_sdl_motion_event { 88sub feed_sdl_motion_event {
58 my ($ev) = @_; 89 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 90 my ($x, $y) = ($ev->{x}, $ev->{y});
60 91
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 92 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 93
63 if ($widget != $HOVER) { 94 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 95 my $hover = $HOVER; $HOVER = $widget;
65 96
66 $hover->update if $hover; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $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);
439} 559}
440 560
441sub render_chld { 561sub render_chld {
442 my ($self) = @_; 562 my ($self) = @_;
443 563
444 $self->{texture} = 564 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
445 CFClient::Texture->new_from_opengl ( 565 glClearColor 0, 0, 0, 1;
446 $self->{w}, $self->{h}, sub { $self->child->draw } 566 glClear GL_COLOR_BUFFER_BIT;
447 ); 567 $self->child->draw;
568 };
448} 569}
449 570
450sub size_allocate { 571sub size_allocate {
451 my ($self, $x, $y, $w, $h) = @_; 572 my ($self, $w, $h) = @_;
452 573
453 $self->_size_allocate ($x, $y, $w, $h) or return;
454
455 $self->child->size_allocate (0, 0, $w, $h); 574 $self->child->configure (0, 0, $w, $h);
456 575
457 $self->render_chld; 576 $self->render_chld;
458} 577}
459 578
460sub _draw { 579sub _draw {
476 glDisable GL_TEXTURE_2D; 595 glDisable GL_TEXTURE_2D;
477} 596}
478 597
479############################################################################# 598#############################################################################
480 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
481package CFClient::UI::Frame; 624package CFClient::UI::Frame;
482 625
483our @ISA = CFClient::UI::Bin::; 626our @ISA = CFClient::UI::Bin::;
484 627
485use SDL::OpenGL; 628use CFClient::OpenGL;
486
487sub size_request {
488 my ($self) = @_;
489 my $chld = $self->child
490 or return (0, 0);
491
492 $chld->move (2, 2);
493
494 map { $_ + 4 } $chld->size_request;
495}
496
497sub size_allocate {
498 my ($self, $x, $y, $w, $h) = @_;
499
500 $self->_size_allocate ($x, $y, $w, $h) or return;
501
502 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
503}
504
505sub _draw {
506 my ($self) = @_;
507
508 my $chld = $self->child;
509
510 my ($w, $h) = $chld->size_request;
511
512 glBegin GL_QUADS;
513 glColor 0, 0, 0;
514 glVertex 0 , 0;
515 glVertex 0 , $h + 4;
516 glVertex $w + 4 , $h + 4;
517 glVertex $w + 4 , 0;
518 glEnd;
519
520 $chld->draw;
521}
522
523#############################################################################
524
525package CFClient::UI::FancyFrame;
526
527our @ISA = CFClient::UI::Bin::;
528
529use SDL::OpenGL;
530
531my @tex =
532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
533 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
534 629
535sub new { 630sub new {
536 my $class = shift; 631 my $class = shift;
537 632
538 # TODO: user_x, user_y, overwrite moveto?
539
540 $class->SUPER::new ( 633 my $self = $class->SUPER::new (
541 bg => [1, 1, 1, 1], 634 bg => [1, 1, 1, 1],
542 border_bg => [1, 1, 1, 1], 635 border_bg => [1, 1, 1, 1],
543 border => $::FONTSIZE * 0.8, 636 border => 0.8,
544 @_ 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,
545 ) 734 )
546} 735}
547 736
548sub size_request {
549 my ($self) = @_;
550
551 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
552
553 my ($w, $h) = $self->SUPER::size_request;
554
555 (
556 $w + $self->{border} * 2,
557 $h + $self->{border} * 2,
558 )
559}
560
561sub size_allocate { 737sub size_allocate {
562 my ($self, $x, $y, $w, $h) = @_; 738 my ($self, $w, $h) = @_;
563 739
564 $self->_size_allocate ($x, $y, $w, $h) or return;
565
566 $h -= List::Util::max 0, $self->{border} * 2; 740 $h -= List::Util::max 0, $self->border * 2;
567 $w -= List::Util::max 0, $self->{border} * 2; 741 $w -= List::Util::max 0, $self->border * 2;
568 742
743 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
744 if $self->{title};
745
569 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 746 $self->child->configure ($self->border, $self->border, $w, $h);
570} 747}
571 748
572sub button_down { 749sub button_down {
573 my ($self, $ev, $x, $y) = @_; 750 my ($self, $ev, $x, $y) = @_;
574 751
752 my $border = $self->border;
753
575 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 754 if ($x < $self->{w} && $x >= $self->{w} - $border
576 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 755 && $y < $self->{h} && $y >= $self->{h} - $border) {
577 756
578 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 757 my ($ox, $oy) = ($ev->{x}, $ev->{y});
579 my ($bw, $bh) = ($self->{w}, $self->{h}); 758 my ($bw, $bh) = ($self->{w}, $self->{h});
580 759
581 $self->{motion} = sub { 760 $self->{motion} = sub {
582 my ($ev, $x, $y) = @_; 761 my ($ev, $x, $y) = @_;
583 762
584 ($x, $y) = ($ev->motion_x, $ev->motion_y); 763 ($x, $y) = ($ev->{x}, $ev->{y});
585 764
586 $self->{user_w} = $bw + $x - $ox; 765 $self->{user_w} = $bw + $x - $ox;
587 $self->{user_h} = $bh + $y - $oy; 766 $self->{user_h} = $bh + $y - $oy;
588 $self->update; 767 $self->check_size;
589 }; 768 };
590 769
591 } elsif ($x >= 0 && $x < $self->{w} 770 } elsif ($x >= 0 && $x < $self->{w}
592 && $y >= 0 && $y < $self->{border}) { 771 && $y >= 0 && $y < $border) {
593 772
594 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 773 my ($ox, $oy) = ($ev->{x}, $ev->{y});
595 my ($bx, $by) = ($self->{x}, $self->{y}); 774 my ($bx, $by) = ($self->{x}, $self->{y});
596 775
597 $self->{motion} = sub { 776 $self->{motion} = sub {
598 my ($ev, $x, $y) = @_; 777 my ($ev, $x, $y) = @_;
599 778
600 ($x, $y) = ($ev->motion_x, $ev->motion_y); 779 ($x, $y) = ($ev->{x}, $ev->{y});
601 780
602 $self->move ($bx + $x - $ox, $by + $y - $oy); 781 $self->move ($bx + $x - $ox, $by + $y - $oy);
603 $self->update; 782 $self->update;
604 }; 783 };
605 } 784 }
626 glEnable GL_BLEND; 805 glEnable GL_BLEND;
627 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
628 glEnable GL_TEXTURE_2D; 807 glEnable GL_TEXTURE_2D;
629 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
630 809
810 my $border = $self->border;
811
631 glColor @{ $self->{border_bg} }; 812 glColor @{ $self->{border_bg} };
632 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 813 $tex[1]->draw_quad (0, 0, $w, $border);
633 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 814 $tex[3]->draw_quad (0, $border, $border, $ch);
634 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 815 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
635 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 816 $tex[4]->draw_quad (0, $h - $border, $w, $border);
636 817
637 my $bg = $tex[0]; 818 my $bg = $tex[0];
638 819
639 # TODO: repeat texture not scale 820 # TODO: repeat texture not scale
640 my $rep_x = $cw / $bg->{w}; 821 my $rep_x = $cw / $bg->{w};
643 glColor @{ $self->{bg} }; 824 glColor @{ $self->{bg} };
644 825
645 $bg->{s} = $rep_x; 826 $bg->{s} = $rep_x;
646 $bg->{t} = $rep_y; 827 $bg->{t} = $rep_y;
647 $bg->{wrap_mode} = 1; 828 $bg->{wrap_mode} = 1;
648 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 829 $bg->draw_quad ($border, $border, $cw, $ch);
649 830
650 glDisable GL_TEXTURE_2D; 831 glDisable GL_TEXTURE_2D;
651 glDisable GL_BLEND; 832 glDisable GL_BLEND;
652 833
834 $self->{title}->draw if $self->{title};
653 $self->child->draw; 835 $self->child->draw;
654} 836}
655 837
656############################################################################# 838#############################################################################
657 839
659 841
660our @ISA = CFClient::UI::Base::; 842our @ISA = CFClient::UI::Base::;
661 843
662use List::Util qw(max sum); 844use List::Util qw(max sum);
663 845
664use SDL::OpenGL; 846use CFClient::OpenGL;
665 847
666sub new { 848sub new {
667 my $class = shift; 849 my $class = shift;
668 850
669 $class->SUPER::new ( 851 $class->SUPER::new (
671 @_ 853 @_
672 ) 854 )
673} 855}
674 856
675sub add { 857sub add {
676 my ($self, $x, $y, $chld) = @_; 858 my ($self, $x, $y, $child) = @_;
677 859
860 $child->set_parent ($self);
678 $self->{children}[$y][$x] = $chld; 861 $self->{children}[$y][$x] = $child;
679 $chld->set_parent ($self);
680 862
681 $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};
682 $self->update; 871 $self->update;
683} 872}
684 873
685sub get_wh { 874sub get_wh {
686 my ($self) = @_; 875 my ($self) = @_;
692 or next; 881 or next;
693 882
694 for my $x (0 .. $#$row) { 883 for my $x (0 .. $#$row) {
695 my $widget = $row->[$x] 884 my $widget = $row->[$x]
696 or next; 885 or next;
697 my ($w, $h) = $widget->size_request; 886 my ($w, $h) = @$widget{qw(req_w req_h)};
698 887
699 $w[$x] = max $w[$x], $w; 888 $w[$x] = max $w[$x], $w;
700 $h[$y] = max $h[$y], $h; 889 $h[$y] = max $h[$y], $h;
701 } 890 }
702 } 891 }
714 (sum @$hs), 903 (sum @$hs),
715 ) 904 )
716} 905}
717 906
718sub size_allocate { 907sub size_allocate {
719 my ($self, $x, $y, $w, $h) = @_; 908 my ($self, $w, $h) = @_;
720
721 $self->_size_allocate ($x, $y, $w, $h) or return;
722 909
723 my ($ws, $hs) = $self->get_wh; 910 my ($ws, $hs) = $self->get_wh;
724 911
725 my $req_w = sum @$ws; 912 my $req_w = sum @$ws;
726 my $req_h = sum @$hs; 913 my $req_h = sum @$hs;
731 my $col_expand = (sum @col_expand) || 1; 918 my $col_expand = (sum @col_expand) || 1;
732 919
733 # linearly scale sizes 920 # linearly scale sizes
734 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 921 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
735 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 922 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
923
924 CFClient::UI::harmonize $ws;
925 CFClient::UI::harmonize $hs;
736 926
737 my $y; 927 my $y;
738 928
739 for my $r (0 .. $#{$self->{children}}) { 929 for my $r (0 .. $#{$self->{children}}) {
740 my $row = $self->{children}[$r] 930 my $row = $self->{children}[$r]
745 935
746 for my $c (0 .. $#$row) { 936 for my $c (0 .. $#$row) {
747 my $col_w = $ws->[$c]; 937 my $col_w = $ws->[$c];
748 938
749 if (my $widget = $row->[$c]) { 939 if (my $widget = $row->[$c]) {
750 $widget->size_allocate ($x, $y, $col_w, $row_h); 940 $widget->configure ($x, $y, $col_w, $row_h);
751 } 941 }
752 942
753 $x += $col_w; 943 $x += $col_w;
754 } 944 }
755 945
800 (List::Util::max map $_->[1], @alloc), 990 (List::Util::max map $_->[1], @alloc),
801 ) 991 )
802} 992}
803 993
804sub size_allocate { 994sub size_allocate {
805 my ($self, $x, $y, $w, $h) = @_; 995 my ($self, $w, $h) = @_;
806
807 $self->_size_allocate ($x, $y, $w, $h);
808 996
809 ($h, $w) = ($w, $h); 997 ($h, $w) = ($w, $h);
810 998
811 my $children = $self->{children}; 999 my $children = $self->{children};
812 1000
813 my @h = map +($_->size_request)[0], @$children; 1001 my @h = map $_->{req_w}, @$children;
814 1002
815 my $req_h = List::Util::sum @h; 1003 my $req_h = List::Util::sum @h;
816 1004
817 if ($req_h > $h) { 1005 if ($req_h > $h) {
818 # ah well, not enough space 1006 # ah well, not enough space
828 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 1016 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
829 $h[$_] = $alloc_h; 1017 $h[$_] = $alloc_h;
830 } 1018 }
831 } 1019 }
832 1020
1021 CFClient::UI::harmonize \@h;
1022
833 my $y = 0; 1023 my $y = 0;
834 for (0 .. $#$children) { 1024 for (0 .. $#$children) {
835 my $child = $children->[$_]; 1025 my $child = $children->[$_];
836 my $h = $h[$_]; 1026 my $h = $h[$_];
837 $child->size_allocate ($y, 0, $h, $w); 1027 $child->configure ($y, 0, $h, $w);
838 1028
839 $y += $h; 1029 $y += $h;
840 } 1030 }
1031
1032 1
841} 1033}
842 1034
843############################################################################# 1035#############################################################################
844 1036
845package CFClient::UI::VBox; 1037package CFClient::UI::VBox;
858 (List::Util::sum map $_->[1], @alloc), 1050 (List::Util::sum map $_->[1], @alloc),
859 ) 1051 )
860} 1052}
861 1053
862sub size_allocate { 1054sub size_allocate {
863 my ($self, $x, $y, $w, $h) = @_; 1055 my ($self, $w, $h) = @_;
864
865 $self->_size_allocate ($x, $y, $w, $h);
866 1056
867 my $children = $self->{children}; 1057 my $children = $self->{children};
868 1058
869 my @h = map +($_->size_request)[1], @$children; 1059 my @h = map $_->{req_h}, @$children;
870 1060
871 my $req_h = List::Util::sum @h; 1061 my $req_h = List::Util::sum @h;
872 1062
873 if ($req_h > $h) { 1063 if ($req_h > $h) {
874 # ah well, not enough space 1064 # ah well, not enough space
882 1072
883 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1073 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
884 } 1074 }
885 } 1075 }
886 1076
1077 CFClient::UI::harmonize \@h;
1078
887 my $y = 0; 1079 my $y = 0;
888 for (0 .. $#$children) { 1080 for (0 .. $#$children) {
889 my $child = $children->[$_]; 1081 my $child = $children->[$_];
890 my $h = $h[$_]; 1082 my $h = $h[$_];
891 $child->size_allocate (0, $y, $w, $h); 1083 $child->configure (0, $y, $w, $h);
892 1084
893 $y += $h; 1085 $y += $h;
894 } 1086 }
1087
1088 1
895} 1089}
896 1090
897############################################################################# 1091#############################################################################
898 1092
899package CFClient::UI::Label; 1093package CFClient::UI::Label;
900 1094
901our @ISA = CFClient::UI::Base::; 1095our @ISA = CFClient::UI::Base::;
902 1096
903use SDL::OpenGL; 1097use CFClient::OpenGL;
904 1098
905sub new { 1099sub new {
906 my ($class, %arg) = @_; 1100 my ($class, %arg) = @_;
907 1101
908 my $self = $class->SUPER::new ( 1102 my $self = $class->SUPER::new (
909 fg => [1, 1, 1], 1103 fg => [1, 1, 1],
910 height => $::FONTSIZE, 1104 fontsize => 1,
911 text => "", 1105 text => "",
912 align => -1, 1106 align => -1,
1107 valign => -1,
913 padding => 2, 1108 padding => 2,
914 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
915 %arg 1111 %arg
916 ); 1112 );
917 1113
918 $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};
919 1122
920 $self 1123 $self
921} 1124}
922 1125
923sub escape_text { 1126sub escape_text {
931} 1134}
932 1135
933sub set_text { 1136sub set_text {
934 my ($self, $text) = @_; 1137 my ($self, $text) = @_;
935 1138
936 $self->{text} = $text;
937 $self->{layout}->set_markup ($text); 1139 $self->{layout}->set_text ($text);
938 1140
939 delete $self->{texture}; 1141 delete $self->{texture};
940# $self->{w} = $self->{h} = -1; 1142 $self->check_size;
941 $self->update; 1143 $self->update;
942} 1144}
943 1145
944sub get_text { 1146sub set_markup {
945 my ($self, $text) = @_; 1147 my ($self, $markup) = @_;
946 1148
947 $self->{text} 1149 $self->{layout}->set_markup ($markup);
1150
1151 delete $self->{texture};
1152 $self->check_size;
1153 $self->update;
948} 1154}
949 1155
950sub size_request { 1156sub size_request {
951 my ($self) = @_; 1157 my ($self) = @_;
952 1158
953 $self->{layout}->set_width; 1159 $self->{layout}->set_width;
954 $self->{layout}->set_height ($self->{height}); 1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1161
955 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 }
956 1172
957 ( 1173 (
958 $w + $self->{padding} * 2, 1174 $w + $self->{padding} * 2,
959 $h + $self->{padding} * 2, 1175 $h + $self->{padding} * 2,
960 ) 1176 )
961} 1177}
962 1178
963sub size_allocate { 1179sub size_allocate {
964 my ($self, $x, $y, $w, $h) = @_; 1180 my ($self, $w, $h) = @_;
965
966 $self->_size_allocate ($x, $y, $w, $h) or return;
967 1181
968 delete $self->{texture}; 1182 delete $self->{texture};
969} 1183}
970 1184
971sub update { 1185sub set_fontsize {
972 my ($self) = @_; 1186 my ($self, $fontsize) = @_;
973 1187
1188 $self->{fontsize} = $fontsize;
974 delete $self->{texture}; 1189 delete $self->{texture};
1190 $self->check_size;
975 $self->SUPER::update; 1191 $self->update;
976} 1192}
977 1193
978sub _draw { 1194sub _draw {
979 my ($self) = @_; 1195 my ($self) = @_;
980 1196
981 my $tex = $self->{texture} ||= do { 1197 my $tex = $self->{texture} ||= do {
982 $self->{layout}->set_width ($self->{w}); 1198 $self->{layout}->set_width ($self->{w});
983 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 1199 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
984 new_from_layout CFClient::Texture $self->{layout} 1200 new_from_layout CFClient::Texture $self->{layout}
985 }; 1201 };
986 1202
987 glEnable GL_BLEND; 1203 glEnable GL_BLEND;
988 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
989 glEnable GL_TEXTURE_2D; 1205 glEnable GL_TEXTURE_2D;
990 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
991 1207
992 glColor @{$self->{fg}}; 1208 glColor @{$self->{fg}};
993 1209
994 my $x = 1210 $self->{ox} = int (
995 $self->{align} < 0 ? $self->{padding} 1211 $self->{align} < 0 ? $self->{padding}
996 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1212 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
997 : ($self->{w} - $tex->{w}) * 0.5; 1213 : ($self->{w} - $tex->{w}) * 0.5
1214 );
998 1215
999 glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; 1216 $self->{oy} = int (
1000 $tex->draw_quad (0, 0); 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});
1001 1223
1002 glDisable GL_TEXTURE_2D; 1224 glDisable GL_TEXTURE_2D;
1003 glDisable GL_BLEND; 1225 glDisable GL_BLEND;
1004} 1226}
1005 1227
1006############################################################################# 1228#############################################################################
1007 1229
1008package CFClient::UI::Entry; 1230package CFClient::UI::EntryBase;
1009 1231
1010our @ISA = CFClient::UI::Label::; 1232our @ISA = CFClient::UI::Label::;
1011 1233
1012use SDL; 1234use CFClient::OpenGL;
1013use SDL::OpenGL;
1014 1235
1015sub new { 1236sub new {
1016 my $class = shift; 1237 my $class = shift;
1017 1238
1018 $class->SUPER::new ( 1239 $class->SUPER::new (
1020 bg => [0, 0, 0, 0.2], 1241 bg => [0, 0, 0, 0.2],
1021 active_bg => [1, 1, 1, 0.5], 1242 active_bg => [1, 1, 1, 0.5],
1022 active_fg => [0, 0, 0], 1243 active_fg => [0, 0, 0],
1023 can_hover => 1, 1244 can_hover => 1,
1024 can_focus => 1, 1245 can_focus => 1,
1246 valign => 0,
1247 can_events => 1,
1025 @_ 1248 @_
1026 ) 1249 )
1027} 1250}
1028 1251
1029sub _set_text { 1252sub _set_text {
1030 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1031 1254
1032 my $old_text = $self->{text}; 1255 delete $self->{cur_h};
1256
1257 return if $self->{text} eq $text;
1258
1259 delete $self->{texture};
1033 1260
1034 $self->{last_activity} = $::NOW; 1261 $self->{last_activity} = $::NOW;
1035
1036 $self->{text} = $text; 1262 $self->{text} = $text;
1037 $self->{layout}->set_width ($self->{w});
1038 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
1039 1263
1040 $text =~ s/./*/g if $self->{hidden}; 1264 $text =~ s/./*/g if $self->{hidden};
1265 $self->{layout}->set_text ("$text ");
1041 1266
1042 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1267 $self->emit (changed => $self->{text});
1268}
1043 1269
1044 $text = substr $text, 0, $self->{cursor}; 1270sub get_text {
1045 utf8::encode $text; 1271 $_[0]{text}
1046
1047 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1048
1049 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1050 if $old_text ne $self->{text};
1051} 1272}
1052 1273
1053sub size_request { 1274sub size_request {
1054 my ($self) = @_; 1275 my ($self) = @_;
1055 1276
1057 1278
1058 ($w + 1, $h) # add 1 for cursor 1279 ($w + 1, $h) # add 1 for cursor
1059} 1280}
1060 1281
1061sub size_allocate { 1282sub size_allocate {
1062 my ($self, $x, $y, $w, $h) = @_; 1283 my ($self, $w, $h) = @_;
1063 1284
1064 $self->SUPER::size_allocate ($x, $y, $w, $h); 1285 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1065
1066 $self->_set_text ($self->{text});
1067} 1286}
1068 1287
1069sub set_text { 1288sub set_text {
1070 my ($self, $text) = @_; 1289 my ($self, $text) = @_;
1071 1290
1075} 1294}
1076 1295
1077sub key_down { 1296sub key_down {
1078 my ($self, $ev) = @_; 1297 my ($self, $ev) = @_;
1079 1298
1080 my $mod = $ev->key_mod; 1299 my $mod = $ev->{mod};
1081 my $sym = $ev->key_sym; 1300 my $sym = $ev->{sym};
1082
1083 my $uni = $ev->key_unicode; 1301 my $uni = $ev->{unicode};
1084 1302
1085 my $text = $self->get_text; 1303 my $text = $self->get_text;
1086 1304
1087 if ($sym == SDLK_BACKSPACE) { 1305 if ($sym == 8) {
1088 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1306 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1089 } elsif ($sym == SDLK_DELETE) { 1307 } elsif ($sym == 127) {
1090 substr $text, $self->{cursor}, 1, ""; 1308 substr $text, $self->{cursor}, 1, "";
1091 } elsif ($sym == SDLK_LEFT) { 1309 } elsif ($sym == CFClient::SDLK_LEFT) {
1092 --$self->{cursor} if $self->{cursor}; 1310 --$self->{cursor} if $self->{cursor};
1093 } elsif ($sym == SDLK_RIGHT) { 1311 } elsif ($sym == CFClient::SDLK_RIGHT) {
1094 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1312 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1095 } elsif ($sym == SDLK_HOME) { 1313 } elsif ($sym == CFClient::SDLK_HOME) {
1096 $self->{cursor} = 0; 1314 $self->{cursor} = 0;
1097 } elsif ($sym == SDLK_END) { 1315 } elsif ($sym == CFClient::SDLK_END) {
1098 $self->{cursor} = length $text; 1316 $self->{cursor} = length $text;
1099 } elsif ($sym == SDLK_ESCAPE) { 1317 } elsif ($sym == 27) {
1100 $self->emit ('escape'); 1318 $self->emit ('escape');
1101 } elsif ($uni) { 1319 } elsif ($uni) {
1102 substr $text, $self->{cursor}++, 0, chr $uni; 1320 substr $text, $self->{cursor}++, 0, chr $uni;
1103 } 1321 }
1104 1322
1159 1377
1160 $self->SUPER::_draw; 1378 $self->SUPER::_draw;
1161 1379
1162 #TODO: force update every cursor change :( 1380 #TODO: force update every cursor change :(
1163 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
1164 glColor @{$self->{fg}}; 1390 glColor @{$self->{fg}};
1165 glBegin GL_LINES; 1391 glBegin GL_LINES;
1166 glVertex $self->{cur_x}, $self->{cur_y};
1167 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};
1168 glEnd; 1394 glEnd;
1169 } 1395 }
1170} 1396}
1171 1397
1172package CFClient::UI::LineEntry; 1398package CFClient::UI::Entry;
1173 1399
1174our @ISA = CFClient::UI::Entry::; 1400our @ISA = CFClient::UI::EntryBase::;
1175 1401
1176use SDL; 1402use CFClient::OpenGL;
1177use SDL::OpenGL;
1178 1403
1179sub key_down { 1404sub key_down {
1180 my ($self, $ev) = @_; 1405 my ($self, $ev) = @_;
1181 1406
1182 my $sym = $ev->key_sym; 1407 my $sym = $ev->{sym};
1183 1408
1184 if ($sym == SDLK_RETURN) { 1409 if ($sym == 13) {
1185 $self->emit (activate => $self->get_text); 1410 $self->emit (activate => $self->get_text);
1186 $self->update; 1411 $self->update;
1187 1412
1188 } else { 1413 } else {
1189 $self->SUPER::key_down ($ev); 1414 $self->SUPER::key_down ($ev);
1195 1420
1196package CFClient::UI::Button; 1421package CFClient::UI::Button;
1197 1422
1198our @ISA = CFClient::UI::Label::; 1423our @ISA = CFClient::UI::Label::;
1199 1424
1200use SDL; 1425use CFClient::OpenGL;
1201use SDL::OpenGL;
1202 1426
1203my @tex = 1427my @tex =
1204 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1428 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1205 qw(b1_button_active.png); 1429 qw(b1_button_active.png);
1206 1430
1207sub new { 1431sub new {
1208 my $class = shift; 1432 my $class = shift;
1209 1433
1210 $class->SUPER::new ( 1434 $class->SUPER::new (
1211 padding => 4, 1435 padding => 4,
1212 fg => [1, 1, 1], 1436 fg => [1, 1, 1],
1213 bg => [1, 1, 1, 0.2], 1437 bg => [1, 1, 1, 0.2],
1214 active_fg => [1, 1, 0], 1438 active_fg => [0, 0, 1],
1215 can_hover => 1, 1439 can_hover => 1,
1440 align => 0,
1441 valign => 0,
1442 can_events => 1,
1216 @_ 1443 @_
1217 ) 1444 )
1218} 1445}
1219 1446
1220sub button_up { 1447sub button_up {
1228 1455
1229sub _draw { 1456sub _draw {
1230 my ($self) = @_; 1457 my ($self) = @_;
1231 1458
1232 local $self->{fg} = $self->{fg}; 1459 local $self->{fg} = $self->{fg};
1233 my $tex = $tex[0];
1234
1235 glEnable GL_BLEND;
1236 glEnable GL_TEXTURE_2D;
1237 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1238 1460
1239 if ($GRAB == $self) { 1461 if ($GRAB == $self) {
1240 $self->{fg} = $self->{active_fg}; 1462 $self->{fg} = $self->{active_fg};
1241 } 1463 }
1242 1464
1243 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;
1244 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;
1245 1470
1246 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1471 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1247 1472
1248 glDisable GL_TEXTURE_2D; 1473 glDisable GL_TEXTURE_2D;
1249 glDisable GL_BLEND; 1474 glDisable GL_BLEND;
1250 1475
1251 $self->SUPER::_draw; 1476 $self->SUPER::_draw;
1256package CFClient::UI::CheckBox; 1481package CFClient::UI::CheckBox;
1257 1482
1258our @ISA = CFClient::UI::DrawBG::; 1483our @ISA = CFClient::UI::DrawBG::;
1259 1484
1260my @tex = 1485my @tex =
1261 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1486 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1262 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1487 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1263 1488
1264use SDL; 1489use CFClient::OpenGL;
1265use SDL::OpenGL;
1266 1490
1267sub new { 1491sub new {
1268 my $class = shift; 1492 my $class = shift;
1269 1493
1270 $class->SUPER::new ( 1494 $class->SUPER::new (
1281 my ($self) = @_; 1505 my ($self) = @_;
1282 1506
1283 ($self->{padding} * 2 + 6) x 2 1507 ($self->{padding} * 2 + 6) x 2
1284} 1508}
1285 1509
1286sub size_allocate {
1287 my ($self, $x, $y, $w, $h) = @_;
1288
1289 $self->_size_allocate ($x, $y, $w, $h);
1290}
1291
1292sub button_down { 1510sub button_down {
1293 my ($self, $ev, $x, $y) = @_; 1511 my ($self, $ev, $x, $y) = @_;
1294 1512
1295 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1513 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1296 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1514 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1322 glDisable GL_BLEND; 1540 glDisable GL_BLEND;
1323} 1541}
1324 1542
1325############################################################################# 1543#############################################################################
1326 1544
1545package CFClient::UI::Image;
1546
1547our @ISA = CFClient::UI::Base::;
1548
1549use CFClient::OpenGL;
1550use Carp qw/confess/;
1551
1552our %loaded_images;
1553
1554sub new {
1555 my $class = shift;
1556
1557 my $self = $class->SUPER::new (can_events => 0, @_);
1558
1559 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1560
1561 $loaded_images{$self->{image}} ||=
1562 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1563
1564 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1565
1566 Scalar::Util::weaken $loaded_images{$self->{image}};
1567
1568 $self->{aspect} = $tex->{w} / $tex->{h};
1569
1570 $self
1571}
1572
1573sub size_request {
1574 my ($self) = @_;
1575
1576 ($self->{tex}->{w}, $self->{tex}->{h})
1577}
1578
1579sub _draw {
1580 my ($self) = @_;
1581
1582 my $tex = $self->{tex};
1583
1584 my ($w, $h) = ($self->{w}, $self->{h});
1585
1586 if ($self->{rot90}) {
1587 glRotate 90, 0, 0, 1;
1588 glTranslate 0, -$self->{w}, 0;
1589
1590 ($w, $h) = ($h, $w);
1591 }
1592
1593 glEnable GL_BLEND;
1594 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1595 glEnable GL_TEXTURE_2D;
1596 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1597
1598 $tex->draw_quad (0, 0, $w, $h);
1599
1600 glDisable GL_BLEND;
1601 glDisable GL_TEXTURE_2D;
1602}
1603
1604#############################################################################
1605
1606package CFClient::UI::VGauge;
1607
1608our @ISA = CFClient::UI::Base::;
1609
1610use CFClient::OpenGL;
1611
1612my %tex = (
1613 food => [
1614 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1615 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1616 ],
1617 grace => [
1618 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1619 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1620 ],
1621 hp => [
1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1623 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1624 ],
1625 mana => [
1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1627 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1628 ],
1629);
1630
1631# eg. VGauge->new (gauge => 'food'), default gauge: food
1632sub new {
1633 my $class = shift;
1634
1635 my $self = $class->SUPER::new (
1636 type => 'food',
1637 @_
1638 );
1639
1640 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1641
1642 $self
1643}
1644
1645sub size_request {
1646 my ($self) = @_;
1647
1648 #my $tex = $tex{$self->{type}}[0];
1649 #@$tex{qw(w h)}
1650 (0, 0)
1651}
1652
1653sub set_max {
1654 my ($self, $max) = @_;
1655
1656 $self->{max_val} = $max;
1657}
1658
1659sub set_value {
1660 my ($self, $val, $max) = @_;
1661
1662 $self->set_max ($max)
1663 if defined $max;
1664
1665 $max = $self->{max_val};
1666 $self->{val} = $val;
1667
1668 $self->update;
1669}
1670
1671sub _draw {
1672 my ($self) = @_;
1673
1674 my $tex = $tex{$self->{type}};
1675
1676 my ($w, $h) = ($self->{w}, $self->{h});
1677
1678 if ($self->{vertical}) {
1679 glRotate 90, 0, 0, 1;
1680 glTranslate 0, -$self->{w}, 0;
1681
1682 ($w, $h) = ($h, $w);
1683 }
1684
1685 my $ycut = $self->{val} / ($self->{max_val} || 1);
1686 $ycut = 1 if $self->{val} > $self->{max_val};
1687
1688 my $t1 = $tex->[0];
1689 my $t2 = $tex->[1];
1690
1691 glEnable GL_BLEND;
1692 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1693 glEnable GL_TEXTURE_2D;
1694 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1695
1696 my $h1 = $self->{h} - $ycut * $self->{h};
1697 my $h2 = $ycut * $self->{h};
1698
1699 glBindTexture GL_TEXTURE_2D, $t1->{name};
1700 glBegin GL_QUADS;
1701 glTexCoord 0 , 0; glVertex 0 , 0;
1702 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1703 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1704 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1705 glEnd;
1706
1707 glBindTexture GL_TEXTURE_2D, $t2->{name};
1708 glBegin GL_QUADS;
1709 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1710 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1711 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1712 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1713 glEnd;
1714
1715 glDisable GL_BLEND;
1716 glDisable GL_TEXTURE_2D;
1717}
1718
1719#############################################################################
1720
1721package CFClient::UI::Gauge;
1722
1723our @ISA = CFClient::UI::VBox::;
1724
1725sub new {
1726 my ($class, %arg) = @_;
1727
1728 my $self = $class->SUPER::new (
1729 tooltip => $arg{type},
1730 %arg,
1731 );
1732
1733 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1734 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1735 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1736
1737 $self
1738}
1739
1740sub set_fontsize {
1741 my ($self, $fsize) = @_;
1742
1743 $self->{value}->set_fontsize ($fsize);
1744 $self->{max} ->set_fontsize ($fsize);
1745}
1746
1747sub set_value {
1748 my ($self, $val, $max) = @_;
1749
1750 $self->set_max ($max)
1751 if defined $max;
1752
1753 $self->{gauge}->set_value ($val, $max);
1754 $self->{value}->set_text ($val);
1755}
1756
1757sub set_max {
1758 my ($self, $max) = @_;
1759
1760 $self->{gauge}->set_max ($max);
1761 $self->{max}->set_text ($max);
1762}
1763
1764#############################################################################
1765
1327package CFClient::UI::Slider; 1766package CFClient::UI::Slider;
1328 1767
1329use strict; 1768use strict;
1330 1769
1331use SDL::OpenGL; 1770use CFClient::OpenGL;
1332 1771
1333our @ISA = CFClient::UI::DrawBG::; 1772our @ISA = CFClient::UI::DrawBG::;
1334 1773
1335my @tex = 1774my @tex =
1336 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1775 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1346 # TODO: calculations are off 1785 # TODO: calculations are off
1347 my $self = $class->SUPER::new ( 1786 my $self = $class->SUPER::new (
1348 fg => [1, 1, 1], 1787 fg => [1, 1, 1],
1349 active_fg => [0, 0, 0], 1788 active_fg => [0, 0, 0],
1350 range => [0, 0, 100, 10], 1789 range => [0, 0, 100, 10],
1351 req_w => 40, 1790 req_w => 20,
1352 req_h => 13, 1791 req_h => 20,
1353 vertical => 0, 1792 vertical => 0,
1354 can_hover => 1, 1793 can_hover => 1,
1355 inner_pad => 5, 1794 inner_pad => 5,
1356 @_ 1795 @_
1357 ); 1796 );
1424 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1863 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1425 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1864 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1426 1865
1427 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1866 my ($value, $lo, $hi, $page) = @{$self->{range}};
1428 1867
1868 $hi = $value + 1 if $lo == $hi;
1869
1429 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 1870 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1430 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 1871 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1431 1872
1432 $page = int $page * $inner_w / ($hi - $lo); 1873 $page = int $page * $inner_w / ($hi - $lo);
1433 $value = int +($value - $lo) * $inner_w / ($hi - $lo); 1874 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1459 1900
1460package CFClient::UI::TextView; 1901package CFClient::UI::TextView;
1461 1902
1462our @ISA = CFClient::UI::HBox::; 1903our @ISA = CFClient::UI::HBox::;
1463 1904
1464use SDL::OpenGL; 1905use CFClient::OpenGL;
1465 1906
1466sub new { 1907sub new {
1467 my $class = shift; 1908 my $class = shift;
1468 1909
1469 my $self = $class->SUPER::new ( 1910 my $self = $class->SUPER::new (
1911 fontsize => 1,
1912 @_,
1913
1914 layout => (new CFClient::Layout),
1470 par => [], 1915 par => [],
1471 @_, 1916 height => 0,
1472 children => [ 1917 children => [
1473 (new CFClient::UI::Empty expand => 1), 1918 (new CFClient::UI::Empty expand => 1),
1474 (new CFClient::UI::Slider vertical => 1), 1919 (new CFClient::UI::Slider vertical => 1),
1475 ], 1920 ],
1476 ); 1921 );
1477 1922
1923 $self->{children}[1]->connect (changed => sub {
1924 $self->update;
1925 });
1926
1478 $self 1927 $self
1928}
1929
1930sub set_fontsize {
1931 my ($self, $fontsize) = @_;
1932
1933 $self->{fontsize} = $fontsize;
1934 $self->reflow;
1935}
1936
1937sub text_height {
1938 my ($self, $text) = @_;
1939
1940 my $layout = $self->{layout};
1941
1942 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1943 $layout->set_width ($self->{w});
1944 $layout->set_text ($text);
1945
1946 ($layout->size)[1]
1947}
1948
1949sub reflow {
1950 my ($self) = @_;
1951
1952 $self->{need_reflow}++;
1953 $self->update;
1954}
1955
1956sub size_allocate {
1957 my ($self, $w, $h) = @_;
1958
1959 $self->SUPER::size_allocate ($w, $h);
1960
1961 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1962 $self->{layout}->set_width ($self->{children}[0]{w});
1963
1964 $self->reflow;
1479} 1965}
1480 1966
1481sub add_paragraph { 1967sub add_paragraph {
1482 my ($self, $color, $text) = @_; 1968 my ($self, $color, $text) = @_;
1483 1969
1970 #TODO: intelligently "reformat" paragraph
1971
1972 my $height = $self->text_height ($text);
1973
1974 $self->{height} += $height;
1975
1484 push @{$self->{par}}, [$color, $text]; 1976 push @{$self->{par}}, [$height, $color, $text];
1485 my $count = scalar @{$self->{par}}; 1977
1486 $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; 1978 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1487 $self->{children}[1]->update; 1979 $self->{children}[1]->update;
1488} 1980}
1489 1981
1982sub update {
1983 my ($self) = @_;
1984
1985 $self->SUPER::update;
1986
1987 return unless $self->{h} > 0;
1988
1989 delete $self->{texture};
1990
1991 $ROOT->on_refresh ($self, sub {
1992 if (delete $self->{need_reflow}) {
1993 my $height = 0;
1994
1995 $height += $_->[0] = $self->text_height ($_->[2])
1996 for @{$self->{par}};
1997
1998 $self->{height} = $height;
1999
2000 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2001
2002 delete $self->{texture};
2003 }
2004
2005 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
2006 glClearColor 0, 0, 0, 1;
2007 glClear GL_COLOR_BUFFER_BIT;
2008
2009 glEnable GL_BLEND;
2010 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2011 glEnable GL_TEXTURE_2D;
2012 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2013
2014 my $top = int $self->{children}[1]{range}[0];
2015
2016 my $y0 = $top;
2017 my $y1 = $top + $self->{h};
2018
2019 my $y = 0;
2020
2021 my $layout = $self->{layout};
2022
2023 for my $par (@{$self->{par}}) {
2024 my $h = $par->[0];
2025
2026 if ($y0 < $y + $h && $y < $y1) {
2027 $layout->set_text ($par->[2]);
2028
2029 glColor @{ $par->[1] };
2030 my ($W, $H) = $layout->size;
2031 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
2032 }
2033
2034 $y += $h;
2035 }
2036
2037 glDisable GL_TEXTURE_2D;
2038 glDisable GL_BLEND;
2039 };
2040 });
2041}
2042
1490sub _draw { 2043sub _draw {
1491 my ($self) = @_; 2044 my ($self) = @_;
1492 2045
1493 my $par = $self->{par}; 2046 if ($self->{texture}) {
1494
1495 my $bottom = (scalar @$par) - 1;
1496
1497 my $w = $self->{children}[0]{w};
1498 my $y = $self->{h};
1499
1500 glEnable GL_BLEND;
1501 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1502 glEnable GL_TEXTURE_2D; 2047 glEnable GL_TEXTURE_2D;
1503 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 2048 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1504 2049 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1505 # TODO: everything!
1506 while ($y > 0 && $bottom >= 0) {
1507 my $par = $par->[$bottom--];
1508
1509 my $layout = new CFClient::Layout;
1510
1511 $layout->set_height ($::FONTSIZE);
1512 $layout->set_width ($w);
1513 $layout->set_text ($par->[1]);
1514 my $tex = new_from_layout CFClient::Texture $layout;
1515 $y -= $tex->{h};
1516
1517 glColor @{ $par->[0] };
1518 $tex->draw_quad (0, $y);
1519 }
1520
1521 glDisable GL_TEXTURE_2D; 2050 glDisable GL_TEXTURE_2D;
1522 glDisable GL_BLEND;
1523}
1524
1525#############################################################################
1526
1527package CFClient::UI::MapWidget;
1528
1529use strict;
1530
1531use List::Util qw(min max);
1532
1533use SDL;
1534use SDL::OpenGL;
1535
1536our @ISA = CFClient::UI::Base::;
1537
1538sub new {
1539 my $class = shift;
1540
1541 $class->SUPER::new (
1542 z => -1,
1543 can_focus => 1,
1544 list => (glGenLists 1),
1545 @_
1546 ) 2051 }
1547}
1548 2052
1549sub key_down { 2053 $self->{children}[1]->draw;
1550 print "MAPKEYDOWN\n";
1551}
1552 2054
1553sub key_up {
1554}
1555
1556sub size_request {
1557 (
1558 1 + 32 * int $::WIDTH / 32,
1559 1 + 32 * int $::HEIGHT / 32,
1560 )
1561}
1562
1563sub update {
1564 my ($self) = @_;
1565
1566 $self->{need_update} = 1;
1567 $self->SUPER::update;
1568}
1569
1570sub draw {
1571 my ($self) = @_;
1572
1573 $self->{need_update}++;#d#
1574 if (delete $self->{need_update}) {
1575 glNewList $self->{list}, GL_COMPILE;
1576
1577 my $sw = int $::WIDTH / 32;
1578 my $sh = int $::HEIGHT / 32;
1579
1580 if ($::MAP) {
1581 my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh);
1582
1583 if ($::CFG->{fow_enable}) {
1584 if ($::CFG->{fow_smooth}) { # smooth fog of war
1585 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1586 glConvolutionFilter2D
1587 GL_CONVOLUTION_2D,
1588 GL_ALPHA,
1589 3, 3,
1590 GL_ALPHA, GL_FLOAT,
1591 pack "f*",
1592 0.1, 0.1, 0.1,
1593 0.1, 0.2, 0.1,
1594 0.1, 0.1, 0.1,
1595 ;
1596 glEnable GL_CONVOLUTION_2D;
1597 }
1598
1599 my $tex = new CFClient::Texture
1600 w => $w,
1601 h => $h,
1602 data => $data,
1603 internalformat => GL_ALPHA,
1604 format => GL_ALPHA;
1605
1606 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1607
1608 glEnable GL_BLEND;
1609 glEnable GL_TEXTURE_2D;
1610 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1611
1612 glColor +($::CFG->{fow_intensity}) x 3, 1;
1613 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1614
1615 glDisable GL_TEXTURE_2D;
1616 glDisable GL_BLEND;
1617 }
1618 }
1619
1620 glEndList;
1621 }
1622
1623 glCallList $self->{list};
1624
1625 if ($FOCUS != $self) {
1626 glColor 64/255, 64/255, 64/255;
1627 glLogicOp GL_AND;
1628 glEnable GL_COLOR_LOGIC_OP;
1629 glBegin GL_QUADS;
1630 glVertex 0, 0;
1631 glVertex 0, $::HEIGHT;
1632 glVertex $::WIDTH, $::HEIGHT;
1633 glVertex $::WIDTH, 0;
1634 glEnd;
1635 glDisable GL_COLOR_LOGIC_OP;
1636 }
1637}
1638
1639my %DIR = (
1640 SDLK_KP8, [1, "north"],
1641 SDLK_KP9, [2, "northeast"],
1642 SDLK_KP6, [3, "east"],
1643 SDLK_KP3, [4, "southeast"],
1644 SDLK_KP2, [5, "south"],
1645 SDLK_KP1, [6, "southwest"],
1646 SDLK_KP4, [7, "west"],
1647 SDLK_KP7, [8, "northwest"],
1648
1649 SDLK_UP, [1, "north"],
1650 SDLK_RIGHT, [3, "east"],
1651 SDLK_DOWN, [5, "south"],
1652 SDLK_LEFT, [7, "west"],
1653);
1654
1655sub key_down {
1656 my ($self, $ev) = @_;
1657
1658 my $mod = $ev->key_mod;
1659 my $sym = $ev->key_sym;
1660
1661 if ($sym == SDLK_KP5) {
1662 $::CONN->user_send ("command stay fire");
1663 } elsif ($sym == SDLK_a) {
1664 $::CONN->user_send ("command apply");
1665 } elsif ($sym == SDLK_QUOTE) {
1666 $self->emit ('activate_console');
1667 } elsif ($sym == SDLK_SLASH) {
1668 $self->emit ('activate_console' => '/');
1669 } elsif (exists $DIR{$sym}) {
1670 if ($mod & KMOD_SHIFT) {
1671 $self->{shft}++;
1672 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1673 } elsif ($mod & KMOD_CTRL) {
1674 $self->{ctrl}++;
1675 $::CONN->user_send ("command run $DIR{$sym}[0]");
1676 } else {
1677 $::CONN->user_send ("command $DIR{$sym}[1]");
1678 }
1679 }
1680}
1681
1682sub key_up {
1683 my ($self, $ev) = @_;
1684
1685 my $mod = $ev->key_mod;
1686 my $sym = $ev->key_sym;
1687
1688 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1689 $::CONN->user_send ("command fire_stop");
1690 }
1691 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1692 $::CONN->user_send ("command run_stop");
1693 }
1694} 2055}
1695 2056
1696############################################################################# 2057#############################################################################
1697 2058
1698package CFClient::UI::Animator; 2059package CFClient::UI::Animator;
1699 2060
1700use SDL::OpenGL; 2061use CFClient::OpenGL;
1701 2062
1702our @ISA = CFClient::UI::Bin::; 2063our @ISA = CFClient::UI::Bin::;
1703 2064
1704sub moveto { 2065sub moveto {
1705 my ($self, $x, $y) = @_; 2066 my ($self, $x, $y) = @_;
1745 my $class = shift; 2106 my $class = shift;
1746 2107
1747 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
1748 state => 0, 2109 state => 0,
1749 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
1750 @_ 2112 @_
1751 ); 2113 );
1752 2114
1753 if ($self->{state}) { 2115 if ($self->{state}) {
1754 $self->{state} = 0; 2116 $self->{state} = 0;
1761sub toggle_flopper { 2123sub toggle_flopper {
1762 my ($self) = @_; 2124 my ($self) = @_;
1763 2125
1764 # TODO: use animation 2126 # TODO: use animation
1765 if ($self->{state} = !$self->{state}) { 2127 if ($self->{state} = !$self->{state}) {
1766 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2128 $CFClient::UI::ROOT->add ($self->{other});
1767 $self->{other}->move ( 2129 $self->{other}->move ($self->coord2global (0, $self->{h}));
1768 ($::WIDTH - $self->{other}{w}) * 0.5, 2130 $self->emit ("open");
1769 ($::HEIGHT - $self->{other}{h}) * 0.5,
1770 );
1771 } else { 2131 } else {
1772 $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;
1773 } 2190
1774} 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;
1775 2200
1776############################################################################# 2201 glTranslate 2, 2;
2202 $self->SUPER::_draw;
2203}
1777 2204
2205#############################################################################
2206
1778package CFClient::UI::Toplevel; 2207package CFClient::UI::Root;
1779 2208
1780our @ISA = CFClient::UI::Container::; 2209our @ISA = CFClient::UI::Container::;
2210
2211use CFClient::OpenGL;
2212
2213sub check_size {
2214 my ($self) = @_;
2215
2216 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2217}
1781 2218
1782sub size_request { 2219sub size_request {
1783 ($::WIDTH, $::HEIGHT) 2220 ($::WIDTH, $::HEIGHT)
1784} 2221}
1785 2222
1786sub size_allocate { 2223sub configure {
1787 my ($self, $x, $y, $w, $h) = @_; 2224 my ($self, $x, $y, $w, $h) = @_;
1788 2225
1789 $self->_size_allocate ($x, $y, $w, $h); 2226 $self->SUPER::configure ($x, $y, $w, $h);
1790 2227
1791 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1792 for @{$self->{children}}; 2228 for my $child (@{$self->{children}}) {
1793} 2229 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1794 2230
1795sub 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 {
1796 my ($self, $x, $y) = @_; 2238 my ($self, $x, $y) = @_;
1797 2239
1798 ($x, $y) 2240 ($x, $y)
1799} 2241}
1800 2242
1801sub update { 2243sub update {
1802 my ($self) = @_; 2244 my ($self) = @_;
1803 2245
1804 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2246 $self->check_size;
1805 ::refresh (); 2247 ::refresh ();
1806} 2248}
1807 2249
1808sub add { 2250sub add {
1809 my ($self, $widget) = @_; 2251 my ($self, $child) = @_;
1810 2252
2253 # integerize window positions
2254 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y};
2256
1811 $self->SUPER::add ($widget); 2257 $self->SUPER::add ($child);
2258}
1812 2259
1813 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2260sub on_refresh {
2261 my ($self, $id, $cb) = @_;
2262
2263 $self->{refresh_hook}{$id} = $cb;
1814} 2264}
1815 2265
1816sub draw { 2266sub draw {
1817 my ($self) = @_; 2267 my ($self) = @_;
1818 2268
2269 while (my $rcb = delete $self->{refresh_hook}) {
2270 $_->() for values %$rcb;
2271 }
2272
2273 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2274 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2275 glClear GL_COLOR_BUFFER_BIT;
2276
2277 glMatrixMode GL_PROJECTION;
2278 glLoadIdentity;
2279 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2280 glMatrixMode GL_MODELVIEW;
2281 glLoadIdentity;
2282
1819 $self->_draw; 2283 $self->_draw;
1820} 2284}
1821 2285
1822############################################################################# 2286#############################################################################
1823 2287
1824package CFClient::UI; 2288package CFClient::UI;
1825 2289
2290$ROOT = new CFClient::UI::Root;
1826$TOPLEVEL = new CFClient::UI::Toplevel; 2291$TOOLTIP = new CFClient::UI::Tooltip;
1827 2292
18281 22931
1829 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines