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

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $TOOLTIP;
13our $BUTTON_STATE; 14our $BUTTON_STATE;
15
16sub check_tooltip {
17 if (!$GRAB) {
18 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
19 if (exists $widget->{tooltip}) {
20
21 if ($TOOLTIP->{owner} != $widget) {
22 $TOOLTIP->{owner} = $widget;
23
24 my $tip = $widget->{tooltip};
25
26 $tip = $tip->($widget) if CODE:: eq ref $tip;
27
28 $TOOLTIP->set_text ($widget->{tooltip});
29 $TOOLTIP->move ($widget->coord2global ($widget->{w}, 0));
30 $TOOLTIP->show;
31 }
32
33 return;
34 }
35 }
36 }
37
38 $TOOLTIP->hide;
39 delete $TOOLTIP->{owner};
40}
14 41
15# class methods for events 42# class methods for events
16sub feed_sdl_key_down_event { 43sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 44 $FOCUS->key_down ($_[0]) if $FOCUS;
18} 45}
21 $FOCUS->key_up ($_[0]) if $FOCUS; 48 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 49}
23 50
24sub feed_sdl_button_down_event { 51sub feed_sdl_button_down_event {
25 my ($ev) = @_; 52 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 53 my ($x, $y) = ($ev->{x}, $ev->{y});
27 54
28 if (!$BUTTON_STATE) { 55 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 56 my $widget = $ROOT->find_widget ($x, $y);
30 57
31 $GRAB = $widget; 58 $GRAB = $widget;
32 $GRAB->update if $GRAB; 59 $GRAB->update if $GRAB;
33 }
34 60
61 check_tooltip;
62 }
63
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 64 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 65
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 66 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 67}
39 68
40sub feed_sdl_button_up_event { 69sub feed_sdl_button_up_event {
41 my ($ev) = @_; 70 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 71 my ($x, $y) = ($ev->{x}, $ev->{y});
43 72
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 73 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 74
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 75 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 76
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 77 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 78
50 if (!$BUTTON_STATE) { 79 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 80 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 81 $grab->update if $grab;
53 $GRAB->update if $GRAB; 82 $GRAB->update if $GRAB;
83
84 check_tooltip;
54 } 85 }
55} 86}
56 87
57sub feed_sdl_motion_event { 88sub feed_sdl_motion_event {
58 my ($ev) = @_; 89 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 90 my ($x, $y) = ($ev->{x}, $ev->{y});
60 91
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 92 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 93
63 if ($widget != $HOVER) { 94 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 95 my $hover = $HOVER; $HOVER = $widget;
65 96
66 $hover->update if $hover && $hover->{can_hover}; 97 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 98 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 }
69 99
100 check_tooltip;
101 }
102
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 103 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
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;
732 my $col_expand = (sum @col_expand) || 1; 918 my $col_expand = (sum @col_expand) || 1;
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;
923
924 CFClient::UI::harmonize $ws;
925 CFClient::UI::harmonize $hs;
737 926
738 my $y; 927 my $y;
739 928
740 for my $r (0 .. $#{$self->{children}}) { 929 for my $r (0 .. $#{$self->{children}}) {
741 my $row = $self->{children}[$r] 930 my $row = $self->{children}[$r]
746 935
747 for my $c (0 .. $#$row) { 936 for my $c (0 .. $#$row) {
748 my $col_w = $ws->[$c]; 937 my $col_w = $ws->[$c];
749 938
750 if (my $widget = $row->[$c]) { 939 if (my $widget = $row->[$c]) {
751 $widget->size_allocate ($x, $y, $col_w, $row_h); 940 $widget->configure ($x, $y, $col_w, $row_h);
752 } 941 }
753 942
754 $x += $col_w; 943 $x += $col_w;
755 } 944 }
756 945
801 (List::Util::max map $_->[1], @alloc), 990 (List::Util::max map $_->[1], @alloc),
802 ) 991 )
803} 992}
804 993
805sub size_allocate { 994sub size_allocate {
806 my ($self, $x, $y, $w, $h) = @_; 995 my ($self, $w, $h) = @_;
807
808 $self->_size_allocate ($x, $y, $w, $h);
809 996
810 ($h, $w) = ($w, $h); 997 ($h, $w) = ($w, $h);
811 998
812 my $children = $self->{children}; 999 my $children = $self->{children};
813 1000
814 my @h = map +($_->size_request)[0], @$children; 1001 my @h = map $_->{req_w}, @$children;
815 1002
816 my $req_h = List::Util::sum @h; 1003 my $req_h = List::Util::sum @h;
817 1004
818 if ($req_h > $h) { 1005 if ($req_h > $h) {
819 # ah well, not enough space 1006 # ah well, not enough space
829 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 1016 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
830 $h[$_] = $alloc_h; 1017 $h[$_] = $alloc_h;
831 } 1018 }
832 } 1019 }
833 1020
1021 CFClient::UI::harmonize \@h;
1022
834 my $y = 0; 1023 my $y = 0;
835 for (0 .. $#$children) { 1024 for (0 .. $#$children) {
836 my $child = $children->[$_]; 1025 my $child = $children->[$_];
837 my $h = int $h[$_]; 1026 my $h = $h[$_];
838 $child->size_allocate ($y, 0, $h, $w); 1027 $child->configure ($y, 0, $h, $w);
839 1028
840 $y += $h; 1029 $y += $h;
841 } 1030 }
1031
1032 1
842} 1033}
843 1034
844############################################################################# 1035#############################################################################
845 1036
846package CFClient::UI::VBox; 1037package CFClient::UI::VBox;
859 (List::Util::sum map $_->[1], @alloc), 1050 (List::Util::sum map $_->[1], @alloc),
860 ) 1051 )
861} 1052}
862 1053
863sub size_allocate { 1054sub size_allocate {
864 my ($self, $x, $y, $w, $h) = @_; 1055 my ($self, $w, $h) = @_;
865
866 $self->_size_allocate ($x, $y, $w, $h);
867 1056
868 my $children = $self->{children}; 1057 my $children = $self->{children};
869 1058
870 my @h = map +($_->size_request)[1], @$children; 1059 my @h = map $_->{req_h}, @$children;
871 1060
872 my $req_h = List::Util::sum @h; 1061 my $req_h = List::Util::sum @h;
873 1062
874 if ($req_h > $h) { 1063 if ($req_h > $h) {
875 # ah well, not enough space 1064 # ah well, not enough space
883 1072
884 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1073 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
885 } 1074 }
886 } 1075 }
887 1076
1077 CFClient::UI::harmonize \@h;
1078
888 my $y = 0; 1079 my $y = 0;
889 for (0 .. $#$children) { 1080 for (0 .. $#$children) {
890 my $child = $children->[$_]; 1081 my $child = $children->[$_];
891 my $h = int $h[$_]; 1082 my $h = $h[$_];
892 $child->size_allocate (0, $y, $w, $h); 1083 $child->configure (0, $y, $w, $h);
893 1084
894 $y += $h; 1085 $y += $h;
895 } 1086 }
1087
1088 1
896} 1089}
897 1090
898############################################################################# 1091#############################################################################
899 1092
900package CFClient::UI::Label; 1093package CFClient::UI::Label;
901 1094
902our @ISA = CFClient::UI::Base::; 1095our @ISA = CFClient::UI::Base::;
903 1096
904use SDL::OpenGL; 1097use CFClient::OpenGL;
905 1098
906sub new { 1099sub new {
907 my ($class, %arg) = @_; 1100 my ($class, %arg) = @_;
908 1101
909 my $self = $class->SUPER::new ( 1102 my $self = $class->SUPER::new (
910 fg => [1, 1, 1], 1103 fg => [1, 1, 1],
911 fontsize => $::FONTSIZE, 1104 fontsize => 1,
912 text => "", 1105 text => "",
913 align => -1, 1106 align => -1,
1107 valign => -1,
914 padding => 2, 1108 padding => 2,
915 layout => new CFClient::Layout, 1109 layout => new CFClient::Layout,
1110 can_events => 0,
916 %arg 1111 %arg
917 ); 1112 );
918 1113
919 $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};
920 1122
921 $self 1123 $self
922} 1124}
923 1125
924sub escape_text { 1126sub escape_text {
932} 1134}
933 1135
934sub set_text { 1136sub set_text {
935 my ($self, $text) = @_; 1137 my ($self, $text) = @_;
936 1138
937 $self->{text} = $text;
938 $self->{layout}->set_markup ($text); 1139 $self->{layout}->set_text ($text);
939 1140
940 delete $self->{texture}; 1141 delete $self->{texture};
941# $self->{w} = $self->{h} = -1; 1142 $self->check_size;
942 $self->update; 1143 $self->update;
943} 1144}
944 1145
945sub get_text { 1146sub set_markup {
946 my ($self, $text) = @_; 1147 my ($self, $markup) = @_;
947 1148
948 $self->{text} 1149 $self->{layout}->set_markup ($markup);
1150
1151 delete $self->{texture};
1152 $self->check_size;
1153 $self->update;
949} 1154}
950 1155
951sub size_request { 1156sub size_request {
952 my ($self) = @_; 1157 my ($self) = @_;
953 1158
954 $self->{layout}->set_width; 1159 $self->{layout}->set_width;
955 $self->{layout}->set_height ($self->{fontsize}); 1160 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1161
956 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 }
957 1172
958 ( 1173 (
959 $w + $self->{padding} * 2, 1174 $w + $self->{padding} * 2,
960 $h + $self->{padding} * 2, 1175 $h + $self->{padding} * 2,
961 ) 1176 )
962} 1177}
963 1178
964sub size_allocate { 1179sub size_allocate {
965 my ($self, $x, $y, $w, $h) = @_; 1180 my ($self, $w, $h) = @_;
966
967 $self->_size_allocate ($x, $y, $w, $h) or return;
968 1181
969 delete $self->{texture}; 1182 delete $self->{texture};
970} 1183}
971 1184
972sub update { 1185sub set_fontsize {
973 my ($self) = @_; 1186 my ($self, $fontsize) = @_;
974 1187
1188 $self->{fontsize} = $fontsize;
975 delete $self->{texture}; 1189 delete $self->{texture};
1190 $self->check_size;
976 $self->SUPER::update; 1191 $self->update;
977} 1192}
978 1193
979sub _draw { 1194sub _draw {
980 my ($self) = @_; 1195 my ($self) = @_;
981 1196
982 my $tex = $self->{texture} ||= do { 1197 my $tex = $self->{texture} ||= do {
983 $self->{layout}->set_width ($self->{w}); 1198 $self->{layout}->set_width ($self->{w});
984 $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);
985 new_from_layout CFClient::Texture $self->{layout} 1200 new_from_layout CFClient::Texture $self->{layout}
986 }; 1201 };
987 1202
988 glEnable GL_BLEND; 1203 glEnable GL_BLEND;
989 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1204 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
990 glEnable GL_TEXTURE_2D; 1205 glEnable GL_TEXTURE_2D;
991 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1206 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
992 1207
993 glColor @{$self->{fg}}; 1208 glColor @{$self->{fg}};
994 1209
995 my $x = 1210 $self->{ox} = int (
996 $self->{align} < 0 ? $self->{padding} 1211 $self->{align} < 0 ? $self->{padding}
997 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1212 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
998 : ($self->{w} - $tex->{w}) * 0.5; 1213 : ($self->{w} - $tex->{w}) * 0.5
1214 );
999 1215
1000 $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});
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->{fontsize});
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 (
1470 req_w => $::WIDTH / 6,
1471 req_h => $::HEIGHT / 6,
1472 fontsize => $::FONTSIZE, 1911 fontsize => 1,
1473 @_, 1912 @_,
1474 1913
1475 layout => (new CFClient::Layout), 1914 layout => (new CFClient::Layout),
1476 par => [], 1915 par => [],
1477 height => 0, 1916 height => 0,
1479 (new CFClient::UI::Empty expand => 1), 1918 (new CFClient::UI::Empty expand => 1),
1480 (new CFClient::UI::Slider vertical => 1), 1919 (new CFClient::UI::Slider vertical => 1),
1481 ], 1920 ],
1482 ); 1921 );
1483 1922
1923 $self->{children}[1]->connect (changed => sub {
1924 $self->update;
1925 });
1926
1484 $self 1927 $self
1928}
1929
1930sub set_fontsize {
1931 my ($self, $fontsize) = @_;
1932
1933 $self->{fontsize} = $fontsize;
1934 $self->reflow;
1485} 1935}
1486 1936
1487sub text_height { 1937sub text_height {
1488 my ($self, $text) = @_; 1938 my ($self, $text) = @_;
1489 1939
1490 my $layout = $self->{layout}; 1940 my $layout = $self->{layout};
1491 1941
1492 $layout->set_height ($self->{fontsize}); 1942 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1493 $layout->set_width ($self->{w}); 1943 $layout->set_width ($self->{w});
1494 $layout->set_text ($text); 1944 $layout->set_text ($text);
1495 1945
1496 ($layout->size)[1] 1946 ($layout->size)[1]
1497} 1947}
1498 1948
1499sub reflow { 1949sub reflow {
1500 my ($self) = @_; 1950 my ($self) = @_;
1501 1951
1502 my $height = 0; 1952 $self->{need_reflow}++;
1503 1953 $self->update;
1504 for (@{$self->{par}}) {
1505 $height += $_->[0] = $self->text_height ($_->[2]);
1506 }
1507
1508 $self->{height} = $height;
1509
1510 $self->{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1511}
1512
1513sub size_request {
1514 my ($self) = @_;
1515
1516 ($self->{req_w}, $self->{req_h})
1517} 1954}
1518 1955
1519sub size_allocate { 1956sub size_allocate {
1520 my ($self, $x, $y, $w, $h) = @_; 1957 my ($self, $w, $h) = @_;
1521 1958
1522 $self->SUPER::size_allocate ($x, $y, $w, $h); 1959 $self->SUPER::size_allocate ($w, $h);
1523 1960
1524 $self->{layout}->set_height ($self->{fontsize}); 1961 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1525 $self->{layout}->set_width ($self->{w}); 1962 $self->{layout}->set_width ($self->{children}[0]{w});
1526 1963
1527 $self->reflow; 1964 $self->reflow;
1528 $self->update;
1529} 1965}
1530 1966
1531sub add_paragraph { 1967sub add_paragraph {
1532 my ($self, $color, $text) = @_; 1968 my ($self, $color, $text) = @_;
1533 1969
1548 1984
1549 $self->SUPER::update; 1985 $self->SUPER::update;
1550 1986
1551 return unless $self->{h} > 0; 1987 return unless $self->{h} > 0;
1552 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
1553 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 2005 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1554 glClearColor 0, 0, 0, 1; 2006 glClearColor 0, 0, 0, 1;
1555 glClear GL_COLOR_BUFFER_BIT; 2007 glClear GL_COLOR_BUFFER_BIT;
1556 2008
1557 my $y0 = $self->{height} - $self->{h};
1558 my $y1 = $self->{height};
1559
1560 glEnable GL_BLEND; 2009 glEnable GL_BLEND;
1561 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 2010 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1562 glEnable GL_TEXTURE_2D; 2011 glEnable GL_TEXTURE_2D;
1563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2012 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1564 2013
2014 my $top = int $self->{children}[1]{range}[0];
2015
2016 my $y0 = $top;
2017 my $y1 = $top + $self->{h};
2018
1565 my $y = 0; 2019 my $y = 0;
1566 2020
1567 my $layout = $self->{layout}; 2021 my $layout = $self->{layout};
1568 2022
1569 for my $par (@{$self->{par}}) { 2023 for my $par (@{$self->{par}}) {
1570 my $h = $par->[0]; 2024 my $h = $par->[0];
1571 2025
1572 if ($y0 < $y + $h && $y < $y1) { 2026 if ($y0 < $y + $h && $y < $y1) {
1573 $layout->set_text ($par->[2]); 2027 $layout->set_text ($par->[2]);
1574 2028
1575 glColor @{ $par->[1] }; 2029 glColor @{ $par->[1] };
1576 my ($W, $H) = $layout->size; 2030 my ($W, $H) = $layout->size;
1577 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0); 2031 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
2032 }
2033
2034 $y += $h;
1578 } 2035 }
1579 2036
1580 $y += $h;
1581 }
1582
1583 glDisable GL_TEXTURE_2D; 2037 glDisable GL_TEXTURE_2D;
1584 glDisable GL_BLEND; 2038 glDisable GL_BLEND;
2039 };
1585 }; 2040 });
1586} 2041}
1587 2042
1588sub _draw { 2043sub _draw {
1589 my ($self) = @_; 2044 my ($self) = @_;
1590 2045
1593 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2048 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1594 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h}); 2049 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1595 glDisable GL_TEXTURE_2D; 2050 glDisable GL_TEXTURE_2D;
1596 } 2051 }
1597 2052
1598} 2053 $self->{children}[1]->draw;
1599 2054
1600#############################################################################
1601
1602package CFClient::UI::MapWidget;
1603
1604use strict;
1605
1606use List::Util qw(min max);
1607
1608use SDL;
1609use SDL::OpenGL;
1610
1611our @ISA = CFClient::UI::Base::;
1612
1613sub new {
1614 my $class = shift;
1615
1616 $class->SUPER::new (
1617 z => -1,
1618 can_focus => 1,
1619 list => (glGenLists 1),
1620 @_
1621 )
1622}
1623
1624sub key_down {
1625 print "MAPKEYDOWN\n";
1626}
1627
1628sub key_up {
1629}
1630
1631sub size_request {
1632 (
1633 1 + 32 * int $::WIDTH / 32,
1634 1 + 32 * int $::HEIGHT / 32,
1635 )
1636}
1637
1638sub update {
1639 my ($self) = @_;
1640
1641 $self->{need_update} = 1;
1642 $self->SUPER::update;
1643}
1644
1645sub draw {
1646 my ($self) = @_;
1647
1648 $self->{need_update}++;#d#
1649 if (delete $self->{need_update}) {
1650 glNewList $self->{list}, GL_COMPILE;
1651
1652 my $sw = int $::WIDTH / 32;
1653 my $sh = int $::HEIGHT / 32;
1654
1655 if ($::MAP) {
1656 my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh);
1657
1658 if ($::CFG->{fow_enable}) {
1659 if ($::CFG->{fow_smooth}) { # smooth fog of war
1660 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1661 glConvolutionFilter2D
1662 GL_CONVOLUTION_2D,
1663 GL_ALPHA,
1664 3, 3,
1665 GL_ALPHA, GL_FLOAT,
1666 pack "f*",
1667 0.1, 0.1, 0.1,
1668 0.1, 0.2, 0.1,
1669 0.1, 0.1, 0.1,
1670 ;
1671 glEnable GL_CONVOLUTION_2D;
1672 }
1673
1674 my $tex = new CFClient::Texture
1675 w => $w,
1676 h => $h,
1677 data => $data,
1678 internalformat => GL_ALPHA,
1679 format => GL_ALPHA;
1680
1681 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1682
1683 glEnable GL_BLEND;
1684 glEnable GL_TEXTURE_2D;
1685 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1686
1687 glColor +($::CFG->{fow_intensity}) x 3, 1;
1688 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1689
1690 glDisable GL_TEXTURE_2D;
1691 glDisable GL_BLEND;
1692 }
1693 }
1694
1695 glEndList;
1696 }
1697
1698 glCallList $self->{list};
1699
1700 if ($FOCUS != $self) {
1701 glColor 64/255, 64/255, 64/255;
1702 glLogicOp GL_AND;
1703 glEnable GL_COLOR_LOGIC_OP;
1704 glBegin GL_QUADS;
1705 glVertex 0, 0;
1706 glVertex 0, $::HEIGHT;
1707 glVertex $::WIDTH, $::HEIGHT;
1708 glVertex $::WIDTH, 0;
1709 glEnd;
1710 glDisable GL_COLOR_LOGIC_OP;
1711 }
1712}
1713
1714my %DIR = (
1715 SDLK_KP8, [1, "north"],
1716 SDLK_KP9, [2, "northeast"],
1717 SDLK_KP6, [3, "east"],
1718 SDLK_KP3, [4, "southeast"],
1719 SDLK_KP2, [5, "south"],
1720 SDLK_KP1, [6, "southwest"],
1721 SDLK_KP4, [7, "west"],
1722 SDLK_KP7, [8, "northwest"],
1723
1724 SDLK_UP, [1, "north"],
1725 SDLK_RIGHT, [3, "east"],
1726 SDLK_DOWN, [5, "south"],
1727 SDLK_LEFT, [7, "west"],
1728);
1729
1730sub key_down {
1731 my ($self, $ev) = @_;
1732
1733 my $mod = $ev->key_mod;
1734 my $sym = $ev->key_sym;
1735
1736 if ($sym == SDLK_KP5) {
1737 $::CONN->user_send ("command stay fire");
1738 } elsif ($sym == SDLK_a) {
1739 $::CONN->user_send ("command apply");
1740 } elsif ($sym == SDLK_QUOTE) {
1741 $self->emit ('activate_console');
1742 } elsif ($sym == SDLK_SLASH) {
1743 $self->emit ('activate_console' => '/');
1744 } elsif (exists $DIR{$sym}) {
1745 if ($mod & KMOD_SHIFT) {
1746 $self->{shft}++;
1747 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1748 } elsif ($mod & KMOD_CTRL) {
1749 $self->{ctrl}++;
1750 $::CONN->user_send ("command run $DIR{$sym}[0]");
1751 } else {
1752 $::CONN->user_send ("command $DIR{$sym}[1]");
1753 }
1754 }
1755}
1756
1757sub key_up {
1758 my ($self, $ev) = @_;
1759
1760 my $mod = $ev->key_mod;
1761 my $sym = $ev->key_sym;
1762
1763 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1764 $::CONN->user_send ("command fire_stop");
1765 }
1766 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1767 $::CONN->user_send ("command run_stop");
1768 }
1769} 2055}
1770 2056
1771############################################################################# 2057#############################################################################
1772 2058
1773package CFClient::UI::Animator; 2059package CFClient::UI::Animator;
1774 2060
1775use SDL::OpenGL; 2061use CFClient::OpenGL;
1776 2062
1777our @ISA = CFClient::UI::Bin::; 2063our @ISA = CFClient::UI::Bin::;
1778 2064
1779sub moveto { 2065sub moveto {
1780 my ($self, $x, $y) = @_; 2066 my ($self, $x, $y) = @_;
1820 my $class = shift; 2106 my $class = shift;
1821 2107
1822 my $self = $class->SUPER::new ( 2108 my $self = $class->SUPER::new (
1823 state => 0, 2109 state => 0,
1824 connect_activate => \&toggle_flopper, 2110 connect_activate => \&toggle_flopper,
2111 can_events => 1,
1825 @_ 2112 @_
1826 ); 2113 );
1827 2114
1828 if ($self->{state}) { 2115 if ($self->{state}) {
1829 $self->{state} = 0; 2116 $self->{state} = 0;
1836sub toggle_flopper { 2123sub toggle_flopper {
1837 my ($self) = @_; 2124 my ($self) = @_;
1838 2125
1839 # TODO: use animation 2126 # TODO: use animation
1840 if ($self->{state} = !$self->{state}) { 2127 if ($self->{state} = !$self->{state}) {
1841 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2128 $CFClient::UI::ROOT->add ($self->{other});
1842 $self->{other}->move ( 2129 $self->{other}->move ($self->coord2global (0, $self->{h}));
1843 ($::WIDTH - $self->{other}{w}) * 0.5, 2130 $self->emit ("open");
1844 ($::HEIGHT - $self->{other}{h}) * 0.5,
1845 );
1846 } else { 2131 } else {
1847 $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;
1848 } 2190
1849} 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;
1850 2200
1851############################################################################# 2201 glTranslate 2, 2;
2202 $self->SUPER::_draw;
2203}
1852 2204
2205#############################################################################
2206
1853package CFClient::UI::Toplevel; 2207package CFClient::UI::Root;
1854 2208
1855our @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}
1856 2218
1857sub size_request { 2219sub size_request {
1858 ($::WIDTH, $::HEIGHT) 2220 ($::WIDTH, $::HEIGHT)
1859} 2221}
1860 2222
1861sub size_allocate { 2223sub configure {
1862 my ($self, $x, $y, $w, $h) = @_; 2224 my ($self, $x, $y, $w, $h) = @_;
1863 2225
1864 $self->_size_allocate ($x, $y, $w, $h); 2226 $self->SUPER::configure ($x, $y, $w, $h);
1865 2227
1866 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1867 for @{$self->{children}}; 2228 for my $child (@{$self->{children}}) {
1868} 2229 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1869 2230
1870sub 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 {
1871 my ($self, $x, $y) = @_; 2238 my ($self, $x, $y) = @_;
1872 2239
1873 ($x, $y) 2240 ($x, $y)
1874} 2241}
1875 2242
1876sub update { 2243sub update {
1877 my ($self) = @_; 2244 my ($self) = @_;
1878 2245
1879 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2246 $self->check_size;
1880 ::refresh (); 2247 ::refresh ();
1881} 2248}
1882 2249
1883sub add { 2250sub add {
1884 my ($self, $widget) = @_; 2251 my ($self, $child) = @_;
1885 2252
2253 # integerize window positions
2254 $child->{x} = int $child->{x};
2255 $child->{y} = int $child->{y};
2256
1886 $self->SUPER::add ($widget); 2257 $self->SUPER::add ($child);
2258}
1887 2259
1888 $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;
1889} 2264}
1890 2265
1891sub draw { 2266sub draw {
1892 my ($self) = @_; 2267 my ($self) = @_;
1893 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
1894 $self->_draw; 2283 $self->_draw;
1895} 2284}
1896 2285
1897############################################################################# 2286#############################################################################
1898 2287
1899package CFClient::UI; 2288package CFClient::UI;
1900 2289
2290$ROOT = new CFClient::UI::Root;
1901$TOPLEVEL = new CFClient::UI::Toplevel; 2291$TOOLTIP = new CFClient::UI::Tooltip;
1902 2292
19031 22931
1904 2294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines