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.157 by root, Sun Apr 23 21:47:32 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
216sub set_max_size {
133 my ($self, $x, $y, $w, $h) = @_; 217 my ($self, $w, $h) = @_;
134 218
135 $self->_size_allocate ($x, $y, $w, $h); 219 delete $self->{max_w}; $self->{max_w} = $w if $w;
220 delete $self->{max_h}; $self->{max_h} = $h if $h;
221}
222
223# return top left coordinates
224sub _topleft {
225 my ($self, $x, $y) = @_;
226
227 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
136} 228}
137 229
138# translate global coordinates to local coordinate system 230# translate global coordinates to local coordinate system
139sub translate { 231sub coord2local {
140 my ($self, $x, $y) = @_; 232 my ($self, $x, $y) = @_;
141 233
142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 234 my ($X, $Y) = $self->_topleft;
235 ($x - $X, $y - $Y)
236}
237
238# translate local coordinates to global coordinate system
239sub coord2global {
240 my ($self, $x, $y) = @_;
241
242 my ($X, $Y) = $self->_topleft;
243 ($x + $X, $y + $Y)
143} 244}
144 245
145sub focus_in { 246sub focus_in {
146 my ($self) = @_; 247 my ($self) = @_;
147 248
148 return if $FOCUS == $self; 249 return if $FOCUS == $self;
149 return unless $self->{can_focus}; 250 return unless $self->{can_focus};
150 251
151 my $focus = $FOCUS; $FOCUS = $self; 252 my $focus = $FOCUS; $FOCUS = $self;
253
254 $self->emit (focus_in => $focus);
255
152 $focus->update if $focus; 256 $focus->update if $focus;
153 $FOCUS->update; 257 $FOCUS->update;
154} 258}
155 259
156sub focus_out { 260sub focus_out {
157 my ($self) = @_; 261 my ($self) = @_;
158 262
159 return unless $FOCUS == $self; 263 return unless $FOCUS == $self;
160 264
161 my $focus = $FOCUS; undef $FOCUS; 265 my $focus = $FOCUS; undef $FOCUS;
266
267 $self->emit (focus_out => $focus);
268
162 $focus->update if $focus; #? 269 $focus->update if $focus; #?
163} 270}
164 271
165sub mouse_motion { } 272sub mouse_motion { }
166sub button_up { } 273sub button_up { }
190 glPopMatrix; 297 glPopMatrix;
191 298
192 if ($self == $HOVER && $self->{can_hover}) { 299 if ($self == $HOVER && $self->{can_hover}) {
193 my ($x, $y) = @$self{qw(x y)}; 300 my ($x, $y) = @$self{qw(x y)};
194 301
195 glColor 0, 0, 1, 0.2; 302 glColor 1, 0.8, 0.5, 0.2;
196 glEnable GL_BLEND; 303 glEnable GL_BLEND;
197 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 304 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
198 glBegin GL_QUADS; 305 glBegin GL_QUADS;
199 glVertex $x , $y; 306 glVertex $x , $y;
200 glVertex $x + $self->{w}, $y; 307 glVertex $x + $self->{w}, $y;
212} 319}
213 320
214sub find_widget { 321sub find_widget {
215 my ($self, $x, $y) = @_; 322 my ($self, $x, $y) = @_;
216 323
324 return () unless $self->{can_events};
325
217 return $self 326 return $self
218 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 327 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
219 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 328 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
220 329
221 () 330 ()
222} 331}
223 332
224sub set_parent { 333sub set_parent {
225 my ($self, $par) = @_; 334 my ($self, $parent) = @_;
226 335
227 $self->{parent} = $par;
228 Scalar::Util::weaken $self->{parent}; 336 Scalar::Util::weaken ($self->{parent} = $parent);
229} 337}
230 338
231sub get_parent { 339sub check_size {
232 $_[0]->{parent} 340 my ($self) = @_;
341
342 return unless $self->{parent};
343
344 my ($w, $h) = $self->size_request;
345
346 if ($w != $self->{req_w} || $h != $self->{req_h}) {
347 $self->{req_w} = $w;
348 $self->{req_h} = $h;
349
350 $self->{parent}->check_size;
351 }
233} 352}
234 353
235sub update { 354sub update {
236 my ($self) = @_; 355 my ($self) = @_;
237 356
240} 359}
241 360
242sub connect { 361sub connect {
243 my ($self, $signal, $cb) = @_; 362 my ($self, $signal, $cb) = @_;
244 363
245 push @{ $self->{cb}{$signal} }, $cb; 364 push @{ $self->{signal_cb}{$signal} }, $cb;
246} 365}
247 366
248sub emit { 367sub emit {
249 my ($self, $signal, @args) = @_; 368 my ($self, $signal, @args) = @_;
250 369
370 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
251 $_->($self, @args) 371 $cb->($self, @args);
252 for @{$self->{cb}{$signal} || []}; 372 }
253} 373}
254 374
255sub DESTROY { 375sub DESTROY {
256 my ($self) = @_; 376 my ($self) = @_;
257 377
263package CFClient::UI::DrawBG; 383package CFClient::UI::DrawBG;
264 384
265our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
266 386
267use strict; 387use strict;
268use SDL::OpenGL; 388use CFClient::OpenGL;
269 389
270sub new { 390sub new {
271 my $class = shift; 391 my $class = shift;
272 392
273 # range [value, low, high, page] 393 # range [value, low, high, page]
302 422
303package CFClient::UI::Empty; 423package CFClient::UI::Empty;
304 424
305our @ISA = CFClient::UI::Base::; 425our @ISA = CFClient::UI::Base::;
306 426
427sub new {
428 my ($class, %arg) = @_;
429 $class->SUPER::new (can_events => 0, %arg);
430}
431
307sub size_request { 432sub size_request {
308 (0, 0) 433 (0, 0)
309} 434}
310 435
311sub draw { } 436sub draw { }
319sub new { 444sub new {
320 my ($class, %arg) = @_; 445 my ($class, %arg) = @_;
321 446
322 my $children = delete $arg{children} || []; 447 my $children = delete $arg{children} || [];
323 448
324 my $self = $class->SUPER::new (children => [], %arg); 449 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
325 $self->add ($_) for @$children; 450 $self->add ($_) for @$children;
326 451
327 $self 452 $self
328} 453}
329 454
330sub add { 455sub add {
331 my ($self, $chld) = @_; 456 my ($self, $child) = @_;
332 457
333 $chld->set_parent ($self); 458 $child->set_parent ($self);
459
460 use sort 'stable';
334 461
335 $self->{children} = [ 462 $self->{children} = [
336 sort { $a->{z} <=> $b->{z} } 463 sort { $a->{z} <=> $b->{z} }
337 @{$self->{children}}, $chld 464 @{$self->{children}}, $child
338 ]; 465 ];
339 466
340 $self->{w} = $self->{h} = -1; 467 $child->check_size;
468}
469
470sub remove {
471 my ($self, $child) = @_;
472
473 delete $child->{parent};
474
475 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
476
477 $self->check_size;
341 $self->update; 478 $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} 479}
351 480
352sub find_widget { 481sub find_widget {
353 my ($self, $x, $y) = @_; 482 my ($self, $x, $y) = @_;
354 483
384 513
385 $class->SUPER::new (children => [$child], %arg) 514 $class->SUPER::new (children => [$child], %arg)
386} 515}
387 516
388sub add { 517sub add {
389 my ($self, $widget) = @_; 518 my ($self, $child) = @_;
390 519
391 $self->{children} = []; 520 $self->{children} = [];
392 521
393 $self->SUPER::add ($widget); 522 $self->SUPER::add ($child);
394} 523}
395 524
396sub remove { 525sub remove {
397 my ($self, $widget) = @_; 526 my ($self, $widget) = @_;
398 527
407sub size_request { 536sub size_request {
408 $_[0]{children}[0]->size_request 537 $_[0]{children}[0]->size_request
409} 538}
410 539
411sub size_allocate { 540sub size_allocate {
412 my ($self, $x, $y, $w, $h) = @_; 541 my ($self, $w, $h) = @_;
413 542
414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
416 $self->{children}[0]->size_allocate (0, 0, $w, $h); 543 $self->{children}[0]->configure (0, 0, $w, $h);
417} 544}
418 545
419############################################################################# 546#############################################################################
420 547
421package CFClient::UI::Window; 548package CFClient::UI::Window;
422 549
423our @ISA = CFClient::UI::Bin::; 550our @ISA = CFClient::UI::Bin::;
424 551
425use SDL::OpenGL; 552use CFClient::OpenGL;
426 553
427sub new { 554sub new {
428 my ($class, %arg) = @_; 555 my ($class, %arg) = @_;
429 556
430 my $self = $class->SUPER::new (%arg); 557 my $self = $class->SUPER::new (%arg);
439} 566}
440 567
441sub render_chld { 568sub render_chld {
442 my ($self) = @_; 569 my ($self) = @_;
443 570
444 $self->{texture} = 571 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
445 CFClient::Texture->new_from_opengl ( 572 glClearColor 0, 0, 0, 1;
446 $self->{w}, $self->{h}, sub { $self->child->draw } 573 glClear GL_COLOR_BUFFER_BIT;
447 ); 574 $self->child->draw;
575 };
448} 576}
449 577
450sub size_allocate { 578sub size_allocate {
451 my ($self, $x, $y, $w, $h) = @_; 579 my ($self, $w, $h) = @_;
452 580
453 $self->_size_allocate ($x, $y, $w, $h) or return;
454
455 $self->child->size_allocate (0, 0, $w, $h); 581 $self->child->configure (0, 0, $w, $h);
456 582
457 $self->render_chld; 583 $self->render_chld;
458} 584}
459 585
460sub _draw { 586sub _draw {
476 glDisable GL_TEXTURE_2D; 602 glDisable GL_TEXTURE_2D;
477} 603}
478 604
479############################################################################# 605#############################################################################
480 606
607package CFClient::UI::ViewPort;
608
609our @ISA = CFClient::UI::Window::;
610
611sub new { die }
612
613sub size_request {
614 my ($self) = @_;
615
616 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
617 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
618
619 @$self{qw(child_w child_h)}
620}
621
622sub _draw {
623 my ($self) = @_;
624
625 $self->{children}[1]->draw;
626}
627
628
629#############################################################################
630
481package CFClient::UI::Frame; 631package CFClient::UI::Frame;
482 632
483our @ISA = CFClient::UI::Bin::; 633our @ISA = CFClient::UI::Bin::;
484 634
485use SDL::OpenGL; 635use 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 636
535sub new { 637sub new {
536 my $class = shift; 638 my $class = shift;
537 639
538 # TODO: user_x, user_y, overwrite moveto?
539
540 $class->SUPER::new ( 640 my $self = $class->SUPER::new (
541 bg => [1, 1, 1, 1], 641 bg => [1, 1, 1, 1],
542 border_bg => [1, 1, 1, 1], 642 border_bg => [1, 1, 1, 1],
543 border => $::FONTSIZE * 0.8, 643 border => 0.8,
544 @_ 644 @_
645 );
646
647 $self
648}
649
650sub set_size {
651 my ($self, $w, $h) = @_;
652 $self->{req_w} = $w;
653 $self->{req_h} = $h;
654 $self->check_size;
655}
656
657sub size_request {
658 my ($self) = @_;
659 ($self->{req_w}, $self->{req_h})
660}
661
662sub size_allocate {
663 my ($self, $w, $h) = @_;
664 $self->{w} = $w;
665 $self->{h} = $h;
666 $self->child->configure (0, 0, $w, $h);
667}
668
669sub _draw {
670 my ($self) = @_;
671
672 my ($w, $h) = ($self->{w}, $self->{h});
673
674 glEnable GL_BLEND;
675 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
676 glEnable GL_TEXTURE_2D;
677 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
678
679# glBegin GL_QUADS;
680# glColor 0, 0, 0, 0;
681# glVertex 0 , 0;
682# glVertex 0 , $h;
683# glVertex $w, $h;
684# glVertex $w, 0;
685# glEnd;
686
687
688 $self->child->draw;
689 glDisable GL_BLEND;
690 glDisable GL_TEXTURE_2D;
691}
692
693#############################################################################
694
695package CFClient::UI::FancyFrame;
696
697our @ISA = CFClient::UI::Bin::;
698
699use CFClient::OpenGL;
700
701my @tex =
702 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
703 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
704
705sub new {
706 my $class = shift;
707
708 # TODO: user_x, user_y, overwrite moveto?
709
710 my $self = $class->SUPER::new (
711 bg => [1, 1, 1, 1],
712 border_bg => [1, 1, 1, 1],
713 border => 0.8,
714 can_events => 1,
715 @_
716 );
717
718 $self->{title} &&= new CFClient::UI::Label
719 align => 0,
720 valign => 1,
721 text => $self->{title},
722 fontsize => 1;
723
724 $self
725}
726
727sub border {
728 int $_[0]{border} * $::FONTSIZE
729}
730
731sub size_request {
732 my ($self) = @_;
733
734 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
735
736 my ($w, $h) = $self->SUPER::size_request;
737
738 (
739 $w + $self->border * 2,
740 $h + $self->border * 2,
545 ) 741 )
546} 742}
547 743
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 { 744sub size_allocate {
562 my ($self, $x, $y, $w, $h) = @_; 745 my ($self, $w, $h) = @_;
563 746
564 $self->_size_allocate ($x, $y, $w, $h) or return;
565
566 $h -= List::Util::max 0, $self->{border} * 2; 747 $h -= List::Util::max 0, $self->border * 2;
567 $w -= List::Util::max 0, $self->{border} * 2; 748 $w -= List::Util::max 0, $self->border * 2;
568 749
750 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
751 if $self->{title};
752
569 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 753 $self->child->configure ($self->border, $self->border, $w, $h);
570} 754}
571 755
572sub button_down { 756sub button_down {
573 my ($self, $ev, $x, $y) = @_; 757 my ($self, $ev, $x, $y) = @_;
574 758
759 my $border = $self->border;
760
575 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 761 if ($x < $self->{w} && $x >= $self->{w} - $border
576 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 762 && $y < $self->{h} && $y >= $self->{h} - $border) {
577 763
578 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 764 my ($ox, $oy) = ($ev->{x}, $ev->{y});
579 my ($bw, $bh) = ($self->{w}, $self->{h}); 765 my ($bw, $bh) = ($self->{w}, $self->{h});
580 766
581 $self->{motion} = sub { 767 $self->{motion} = sub {
582 my ($ev, $x, $y) = @_; 768 my ($ev, $x, $y) = @_;
583 769
584 ($x, $y) = ($ev->motion_x, $ev->motion_y); 770 ($x, $y) = ($ev->{x}, $ev->{y});
585 771
586 $self->{user_w} = $bw + $x - $ox; 772 $self->{user_w} = $bw + $x - $ox;
587 $self->{user_h} = $bh + $y - $oy; 773 $self->{user_h} = $bh + $y - $oy;
588 $self->update; 774 $self->check_size;
589 }; 775 };
590 776
591 } elsif ($x >= 0 && $x < $self->{w} 777 } elsif ($x >= 0 && $x < $self->{w}
592 && $y >= 0 && $y < $self->{border}) { 778 && $y >= 0 && $y < $border) {
593 779
594 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 780 my ($ox, $oy) = ($ev->{x}, $ev->{y});
595 my ($bx, $by) = ($self->{x}, $self->{y}); 781 my ($bx, $by) = ($self->{x}, $self->{y});
596 782
597 $self->{motion} = sub { 783 $self->{motion} = sub {
598 my ($ev, $x, $y) = @_; 784 my ($ev, $x, $y) = @_;
599 785
600 ($x, $y) = ($ev->motion_x, $ev->motion_y); 786 ($x, $y) = ($ev->{x}, $ev->{y});
601 787
602 $self->move ($bx + $x - $ox, $by + $y - $oy); 788 $self->move ($bx + $x - $ox, $by + $y - $oy);
603 $self->update; 789 $self->update;
604 }; 790 };
605 } 791 }
626 glEnable GL_BLEND; 812 glEnable GL_BLEND;
627 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 813 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
628 glEnable GL_TEXTURE_2D; 814 glEnable GL_TEXTURE_2D;
629 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 815 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
630 816
817 my $border = $self->border;
818
631 glColor @{ $self->{border_bg} }; 819 glColor @{ $self->{border_bg} };
632 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 820 $tex[1]->draw_quad (0, 0, $w, $border);
633 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 821 $tex[3]->draw_quad (0, $border, $border, $ch);
634 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 822 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
635 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 823 $tex[4]->draw_quad (0, $h - $border, $w, $border);
636 824
637 my $bg = $tex[0]; 825 my $bg = $tex[0];
638 826
639 # TODO: repeat texture not scale 827 # TODO: repeat texture not scale
640 my $rep_x = $cw / $bg->{w}; 828 my $rep_x = $cw / $bg->{w};
643 glColor @{ $self->{bg} }; 831 glColor @{ $self->{bg} };
644 832
645 $bg->{s} = $rep_x; 833 $bg->{s} = $rep_x;
646 $bg->{t} = $rep_y; 834 $bg->{t} = $rep_y;
647 $bg->{wrap_mode} = 1; 835 $bg->{wrap_mode} = 1;
648 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 836 $bg->draw_quad ($border, $border, $cw, $ch);
649 837
650 glDisable GL_TEXTURE_2D; 838 glDisable GL_TEXTURE_2D;
651 glDisable GL_BLEND; 839 glDisable GL_BLEND;
652 840
841 $self->{title}->draw if $self->{title};
653 $self->child->draw; 842 $self->child->draw;
654} 843}
655 844
656############################################################################# 845#############################################################################
657 846
659 848
660our @ISA = CFClient::UI::Base::; 849our @ISA = CFClient::UI::Base::;
661 850
662use List::Util qw(max sum); 851use List::Util qw(max sum);
663 852
664use SDL::OpenGL; 853use CFClient::OpenGL;
665 854
666sub new { 855sub new {
667 my $class = shift; 856 my $class = shift;
668 857
669 $class->SUPER::new ( 858 $class->SUPER::new (
671 @_ 860 @_
672 ) 861 )
673} 862}
674 863
675sub add { 864sub add {
676 my ($self, $x, $y, $chld) = @_; 865 my ($self, $x, $y, $child) = @_;
677 866
867 $child->set_parent ($self);
678 $self->{children}[$y][$x] = $chld; 868 $self->{children}[$y][$x] = $child;
679 $chld->set_parent ($self);
680 869
681 $self->{w} = $self->{h} = -1; 870 $child->check_size;
871}
872
873# TODO: move to container class maybe? send childs a signal on removal?
874sub clear {
875 my ($self) = @_;
876
877 delete $self->{children};
682 $self->update; 878 $self->update;
683} 879}
684 880
685sub get_wh { 881sub get_wh {
686 my ($self) = @_; 882 my ($self) = @_;
692 or next; 888 or next;
693 889
694 for my $x (0 .. $#$row) { 890 for my $x (0 .. $#$row) {
695 my $widget = $row->[$x] 891 my $widget = $row->[$x]
696 or next; 892 or next;
697 my ($w, $h) = $widget->size_request; 893 my ($w, $h) = @$widget{qw(req_w req_h)};
698 894
699 $w[$x] = max $w[$x], $w; 895 $w[$x] = max $w[$x], $w;
700 $h[$y] = max $h[$y], $h; 896 $h[$y] = max $h[$y], $h;
701 } 897 }
702 } 898 }
714 (sum @$hs), 910 (sum @$hs),
715 ) 911 )
716} 912}
717 913
718sub size_allocate { 914sub size_allocate {
719 my ($self, $x, $y, $w, $h) = @_; 915 my ($self, $w, $h) = @_;
720
721 $self->_size_allocate ($x, $y, $w, $h) or return;
722 916
723 my ($ws, $hs) = $self->get_wh; 917 my ($ws, $hs) = $self->get_wh;
724 918
725 my $req_w = sum @$ws; 919 my $req_w = sum @$ws;
726 my $req_h = sum @$hs; 920 my $req_h = sum @$hs;
731 my $col_expand = (sum @col_expand) || 1; 925 my $col_expand = (sum @col_expand) || 1;
732 926
733 # linearly scale sizes 927 # linearly scale sizes
734 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 928 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
735 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 929 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
930
931 CFClient::UI::harmonize $ws;
932 CFClient::UI::harmonize $hs;
736 933
737 my $y; 934 my $y;
738 935
739 for my $r (0 .. $#{$self->{children}}) { 936 for my $r (0 .. $#{$self->{children}}) {
740 my $row = $self->{children}[$r] 937 my $row = $self->{children}[$r]
745 942
746 for my $c (0 .. $#$row) { 943 for my $c (0 .. $#$row) {
747 my $col_w = $ws->[$c]; 944 my $col_w = $ws->[$c];
748 945
749 if (my $widget = $row->[$c]) { 946 if (my $widget = $row->[$c]) {
750 $widget->size_allocate ($x, $y, $col_w, $row_h); 947 $widget->configure ($x, $y, $col_w, $row_h);
751 } 948 }
752 949
753 $x += $col_w; 950 $x += $col_w;
754 } 951 }
755 952
800 (List::Util::max map $_->[1], @alloc), 997 (List::Util::max map $_->[1], @alloc),
801 ) 998 )
802} 999}
803 1000
804sub size_allocate { 1001sub size_allocate {
805 my ($self, $x, $y, $w, $h) = @_; 1002 my ($self, $w, $h) = @_;
806
807 $self->_size_allocate ($x, $y, $w, $h);
808 1003
809 ($h, $w) = ($w, $h); 1004 ($h, $w) = ($w, $h);
810 1005
811 my $children = $self->{children}; 1006 my $children = $self->{children};
812 1007
813 my @h = map +($_->size_request)[0], @$children; 1008 my @h = map $_->{req_w}, @$children;
814 1009
815 my $req_h = List::Util::sum @h; 1010 my $req_h = List::Util::sum @h;
816 1011
817 if ($req_h > $h) { 1012 if ($req_h > $h) {
818 # ah well, not enough space 1013 # ah well, not enough space
828 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 1023 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
829 $h[$_] = $alloc_h; 1024 $h[$_] = $alloc_h;
830 } 1025 }
831 } 1026 }
832 1027
1028 CFClient::UI::harmonize \@h;
1029
833 my $y = 0; 1030 my $y = 0;
834 for (0 .. $#$children) { 1031 for (0 .. $#$children) {
835 my $child = $children->[$_]; 1032 my $child = $children->[$_];
836 my $h = $h[$_]; 1033 my $h = $h[$_];
837 $child->size_allocate ($y, 0, $h, $w); 1034 $child->configure ($y, 0, $h, $w);
838 1035
839 $y += $h; 1036 $y += $h;
840 } 1037 }
1038
1039 1
841} 1040}
842 1041
843############################################################################# 1042#############################################################################
844 1043
845package CFClient::UI::VBox; 1044package CFClient::UI::VBox;
858 (List::Util::sum map $_->[1], @alloc), 1057 (List::Util::sum map $_->[1], @alloc),
859 ) 1058 )
860} 1059}
861 1060
862sub size_allocate { 1061sub size_allocate {
863 my ($self, $x, $y, $w, $h) = @_; 1062 my ($self, $w, $h) = @_;
864
865 $self->_size_allocate ($x, $y, $w, $h);
866 1063
867 my $children = $self->{children}; 1064 my $children = $self->{children};
868 1065
869 my @h = map +($_->size_request)[1], @$children; 1066 my @h = map $_->{req_h}, @$children;
870 1067
871 my $req_h = List::Util::sum @h; 1068 my $req_h = List::Util::sum @h;
872 1069
873 if ($req_h > $h) { 1070 if ($req_h > $h) {
874 # ah well, not enough space 1071 # ah well, not enough space
882 1079
883 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1080 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
884 } 1081 }
885 } 1082 }
886 1083
1084 CFClient::UI::harmonize \@h;
1085
887 my $y = 0; 1086 my $y = 0;
888 for (0 .. $#$children) { 1087 for (0 .. $#$children) {
889 my $child = $children->[$_]; 1088 my $child = $children->[$_];
890 my $h = $h[$_]; 1089 my $h = $h[$_];
891 $child->size_allocate (0, $y, $w, $h); 1090 $child->configure (0, $y, $w, $h);
892 1091
893 $y += $h; 1092 $y += $h;
894 } 1093 }
1094
1095 1
895} 1096}
896 1097
897############################################################################# 1098#############################################################################
898 1099
899package CFClient::UI::Label; 1100package CFClient::UI::Label;
900 1101
901our @ISA = CFClient::UI::Base::; 1102our @ISA = CFClient::UI::Base::;
902 1103
903use SDL::OpenGL; 1104use CFClient::OpenGL;
904 1105
905sub new { 1106sub new {
906 my ($class, %arg) = @_; 1107 my ($class, %arg) = @_;
907 1108
908 my $self = $class->SUPER::new ( 1109 my $self = $class->SUPER::new (
909 fg => [1, 1, 1], 1110 fg => [1, 1, 1],
910 height => $::FONTSIZE, 1111 #font => default_font
1112 fontsize => 1,
911 text => "", 1113 text => "",
912 align => -1, 1114 align => -1,
1115 valign => -1,
913 padding => 2, 1116 padding => 2,
914 layout => new CFClient::Layout, 1117 layout => new CFClient::Layout,
1118 can_events => 0,
915 %arg 1119 %arg
916 ); 1120 );
917 1121
918 $self->set_text ($self->{text}); 1122 if (exists $self->{template}) {
1123 my $layout = new CFClient::Layout;
1124 $layout->set_text (delete $self->{template});
1125 $self->{template} = $layout;
1126 }
1127
1128 $self->set_text (delete $self->{text}) if exists $self->{text};
1129 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
919 1130
920 $self 1131 $self
921} 1132}
922 1133
923sub escape_text { 1134sub escape_text {
931} 1142}
932 1143
933sub set_text { 1144sub set_text {
934 my ($self, $text) = @_; 1145 my ($self, $text) = @_;
935 1146
936 $self->{text} = $text;
937 $self->{layout}->set_markup ($text); 1147 $self->{layout}->set_text ($text);
938 1148
939 delete $self->{texture}; 1149 delete $self->{texture};
940# $self->{w} = $self->{h} = -1; 1150 $self->check_size;
941 $self->update; 1151 $self->update;
942} 1152}
943 1153
944sub get_text { 1154sub set_markup {
945 my ($self, $text) = @_; 1155 my ($self, $markup) = @_;
946 1156
947 $self->{text} 1157 $self->{layout}->set_markup ($markup);
1158
1159 delete $self->{texture};
1160 $self->check_size;
1161 $self->update;
948} 1162}
949 1163
950sub size_request { 1164sub size_request {
951 my ($self) = @_; 1165 my ($self) = @_;
952 1166
953 $self->{layout}->set_width; 1167 $self->{layout}->set_font ($self->{font}) if $self->{font};
1168 $self->{layout}->set_width ($self->{max_w} || -1);
954 $self->{layout}->set_height ($self->{height}); 1169 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1170
955 my ($w, $h) = $self->{layout}->size; 1171 my ($w, $h) = $self->{layout}->size;
1172
1173 if (exists $self->{template}) {
1174 $self->{template}->set_font ($self->{font}) if $self->{font};
1175 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1176
1177 my ($w2, $h2) = $self->{template}->size;
1178
1179 $w = List::Util::max $w, $w2;
1180 $h = List::Util::max $h, $h2;
1181 }
956 1182
957 ( 1183 (
958 $w + $self->{padding} * 2, 1184 $w + $self->{padding} * 2,
959 $h + $self->{padding} * 2, 1185 $h + $self->{padding} * 2,
960 ) 1186 )
961} 1187}
962 1188
963sub size_allocate { 1189sub size_allocate {
964 my ($self, $x, $y, $w, $h) = @_; 1190 my ($self, $w, $h) = @_;
965
966 $self->_size_allocate ($x, $y, $w, $h) or return;
967 1191
968 delete $self->{texture}; 1192 delete $self->{texture};
969} 1193}
970 1194
971sub update { 1195sub set_fontsize {
972 my ($self) = @_; 1196 my ($self, $fontsize) = @_;
973 1197
1198 $self->{fontsize} = $fontsize;
974 delete $self->{texture}; 1199 delete $self->{texture};
1200 $self->check_size;
975 $self->SUPER::update; 1201 $self->update;
976} 1202}
977 1203
978sub _draw { 1204sub _draw {
979 my ($self) = @_; 1205 my ($self) = @_;
980 1206
981 my $tex = $self->{texture} ||= do { 1207 my $tex = $self->{texture} ||= do {
1208 $self->{layout}->set_font ($self->{font}) if $self->{font};
982 $self->{layout}->set_width ($self->{w}); 1209 $self->{layout}->set_width ($self->{w});
983 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 1210 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
984 new_from_layout CFClient::Texture $self->{layout} 1211 new_from_layout CFClient::Texture $self->{layout}
985 }; 1212 };
986 1213
987 glEnable GL_BLEND; 1214 glEnable GL_BLEND;
988 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1215 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
989 glEnable GL_TEXTURE_2D; 1216 glEnable GL_TEXTURE_2D;
990 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1217 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
991 1218
992 glColor @{$self->{fg}}; 1219 glColor @{$self->{fg}};
993 1220
994 my $x = 1221 $self->{ox} = int (
995 $self->{align} < 0 ? $self->{padding} 1222 $self->{align} < 0 ? $self->{padding}
996 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1223 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
997 : ($self->{w} - $tex->{w}) * 0.5; 1224 : ($self->{w} - $tex->{w}) * 0.5
1225 );
998 1226
999 glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; 1227 $self->{oy} = int (
1000 $tex->draw_quad (0, 0); 1228 $self->{valign} < 0 ? $self->{padding}
1229 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1230 : ($self->{h} - $tex->{h}) * 0.5
1231 );
1232
1233 $tex->draw_quad ($self->{ox}, $self->{oy});
1001 1234
1002 glDisable GL_TEXTURE_2D; 1235 glDisable GL_TEXTURE_2D;
1003 glDisable GL_BLEND; 1236 glDisable GL_BLEND;
1004} 1237}
1005 1238
1006############################################################################# 1239#############################################################################
1007 1240
1008package CFClient::UI::Entry; 1241package CFClient::UI::EntryBase;
1009 1242
1010our @ISA = CFClient::UI::Label::; 1243our @ISA = CFClient::UI::Label::;
1011 1244
1012use SDL; 1245use CFClient::OpenGL;
1013use SDL::OpenGL;
1014 1246
1015sub new { 1247sub new {
1016 my $class = shift; 1248 my $class = shift;
1017 1249
1018 $class->SUPER::new ( 1250 $class->SUPER::new (
1020 bg => [0, 0, 0, 0.2], 1252 bg => [0, 0, 0, 0.2],
1021 active_bg => [1, 1, 1, 0.5], 1253 active_bg => [1, 1, 1, 0.5],
1022 active_fg => [0, 0, 0], 1254 active_fg => [0, 0, 0],
1023 can_hover => 1, 1255 can_hover => 1,
1024 can_focus => 1, 1256 can_focus => 1,
1257 valign => 0,
1258 can_events => 1,
1025 @_ 1259 @_
1026 ) 1260 )
1027} 1261}
1028 1262
1029sub _set_text { 1263sub _set_text {
1030 my ($self, $text) = @_; 1264 my ($self, $text) = @_;
1031 1265
1032 my $old_text = $self->{text}; 1266 delete $self->{cur_h};
1267
1268 return if $self->{text} eq $text;
1269
1270 delete $self->{texture};
1033 1271
1034 $self->{last_activity} = $::NOW; 1272 $self->{last_activity} = $::NOW;
1035
1036 $self->{text} = $text; 1273 $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 1274
1040 $text =~ s/./*/g if $self->{hidden}; 1275 $text =~ s/./*/g if $self->{hidden};
1276 $self->{layout}->set_text ("$text ");
1041 1277
1042 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1278 $self->emit (changed => $self->{text});
1279}
1043 1280
1044 $text = substr $text, 0, $self->{cursor}; 1281sub get_text {
1045 utf8::encode $text; 1282 $_[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} 1283}
1052 1284
1053sub size_request { 1285sub size_request {
1054 my ($self) = @_; 1286 my ($self) = @_;
1055 1287
1057 1289
1058 ($w + 1, $h) # add 1 for cursor 1290 ($w + 1, $h) # add 1 for cursor
1059} 1291}
1060 1292
1061sub size_allocate { 1293sub size_allocate {
1062 my ($self, $x, $y, $w, $h) = @_; 1294 my ($self, $w, $h) = @_;
1063 1295
1064 $self->SUPER::size_allocate ($x, $y, $w, $h); 1296 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1065
1066 $self->_set_text ($self->{text});
1067} 1297}
1068 1298
1069sub set_text { 1299sub set_text {
1070 my ($self, $text) = @_; 1300 my ($self, $text) = @_;
1071 1301
1075} 1305}
1076 1306
1077sub key_down { 1307sub key_down {
1078 my ($self, $ev) = @_; 1308 my ($self, $ev) = @_;
1079 1309
1080 my $mod = $ev->key_mod; 1310 my $mod = $ev->{mod};
1081 my $sym = $ev->key_sym; 1311 my $sym = $ev->{sym};
1082
1083 my $uni = $ev->key_unicode; 1312 my $uni = $ev->{unicode};
1084 1313
1085 my $text = $self->get_text; 1314 my $text = $self->get_text;
1086 1315
1087 if ($sym == SDLK_BACKSPACE) { 1316 if ($sym == 8) {
1088 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1317 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1089 } elsif ($sym == SDLK_DELETE) { 1318 } elsif ($sym == 127) {
1090 substr $text, $self->{cursor}, 1, ""; 1319 substr $text, $self->{cursor}, 1, "";
1091 } elsif ($sym == SDLK_LEFT) { 1320 } elsif ($sym == CFClient::SDLK_LEFT) {
1092 --$self->{cursor} if $self->{cursor}; 1321 --$self->{cursor} if $self->{cursor};
1093 } elsif ($sym == SDLK_RIGHT) { 1322 } elsif ($sym == CFClient::SDLK_RIGHT) {
1094 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1323 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1095 } elsif ($sym == SDLK_HOME) { 1324 } elsif ($sym == CFClient::SDLK_HOME) {
1096 $self->{cursor} = 0; 1325 $self->{cursor} = 0;
1097 } elsif ($sym == SDLK_END) { 1326 } elsif ($sym == CFClient::SDLK_END) {
1098 $self->{cursor} = length $text; 1327 $self->{cursor} = length $text;
1099 } elsif ($sym == SDLK_ESCAPE) { 1328 } elsif ($sym == 27) {
1100 $self->emit ('escape'); 1329 $self->emit ('escape');
1101 } elsif ($uni) { 1330 } elsif ($uni) {
1102 substr $text, $self->{cursor}++, 0, chr $uni; 1331 substr $text, $self->{cursor}++, 0, chr $uni;
1103 } 1332 }
1104 1333
1159 1388
1160 $self->SUPER::_draw; 1389 $self->SUPER::_draw;
1161 1390
1162 #TODO: force update every cursor change :( 1391 #TODO: force update every cursor change :(
1163 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1392 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1393
1394 unless (exists $self->{cur_h}) {
1395 my $text = substr $self->{text}, 0, $self->{cursor};
1396 utf8::encode $text;
1397
1398 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1399 }
1400
1164 glColor @{$self->{fg}}; 1401 glColor @{$self->{fg}};
1165 glBegin GL_LINES; 1402 glBegin GL_LINES;
1166 glVertex $self->{cur_x}, $self->{cur_y};
1167 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1403 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1404 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1168 glEnd; 1405 glEnd;
1169 } 1406 }
1170} 1407}
1171 1408
1172package CFClient::UI::LineEntry; 1409package CFClient::UI::Entry;
1173 1410
1174our @ISA = CFClient::UI::Entry::; 1411our @ISA = CFClient::UI::EntryBase::;
1175 1412
1176use SDL; 1413use CFClient::OpenGL;
1177use SDL::OpenGL;
1178 1414
1179sub key_down { 1415sub key_down {
1180 my ($self, $ev) = @_; 1416 my ($self, $ev) = @_;
1181 1417
1182 my $sym = $ev->key_sym; 1418 my $sym = $ev->{sym};
1183 1419
1184 if ($sym == SDLK_RETURN) { 1420 if ($sym == 13) {
1185 $self->emit (activate => $self->get_text); 1421 $self->emit (activate => $self->get_text);
1186 $self->update; 1422 $self->update;
1187 1423
1188 } else { 1424 } else {
1189 $self->SUPER::key_down ($ev); 1425 $self->SUPER::key_down ($ev);
1195 1431
1196package CFClient::UI::Button; 1432package CFClient::UI::Button;
1197 1433
1198our @ISA = CFClient::UI::Label::; 1434our @ISA = CFClient::UI::Label::;
1199 1435
1200use SDL; 1436use CFClient::OpenGL;
1201use SDL::OpenGL;
1202 1437
1203my @tex = 1438my @tex =
1204 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1439 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1205 qw(b1_button_active.png); 1440 qw(b1_button_active.png);
1206 1441
1207sub new { 1442sub new {
1208 my $class = shift; 1443 my $class = shift;
1209 1444
1210 $class->SUPER::new ( 1445 $class->SUPER::new (
1211 padding => 4, 1446 padding => 4,
1212 fg => [1, 1, 1], 1447 fg => [1, 1, 1],
1213 bg => [1, 1, 1, 0.2], 1448 bg => [1, 1, 1, 0.2],
1214 active_fg => [1, 1, 0], 1449 active_fg => [0, 0, 1],
1215 can_hover => 1, 1450 can_hover => 1,
1451 align => 0,
1452 valign => 0,
1453 can_events => 1,
1216 @_ 1454 @_
1217 ) 1455 )
1218} 1456}
1219 1457
1220sub button_up { 1458sub button_up {
1228 1466
1229sub _draw { 1467sub _draw {
1230 my ($self) = @_; 1468 my ($self) = @_;
1231 1469
1232 local $self->{fg} = $self->{fg}; 1470 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 1471
1239 if ($GRAB == $self) { 1472 if ($GRAB == $self) {
1240 $self->{fg} = $self->{active_fg}; 1473 $self->{fg} = $self->{active_fg};
1241 } 1474 }
1242 1475
1243 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1476 glEnable GL_BLEND;
1477 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1478 glEnable GL_TEXTURE_2D;
1244 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1479 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1480 glColor 0, 0, 0, 1;
1245 1481
1246 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1482 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1247 1483
1248 glDisable GL_TEXTURE_2D; 1484 glDisable GL_TEXTURE_2D;
1249 glDisable GL_BLEND; 1485 glDisable GL_BLEND;
1250 1486
1251 $self->SUPER::_draw; 1487 $self->SUPER::_draw;
1256package CFClient::UI::CheckBox; 1492package CFClient::UI::CheckBox;
1257 1493
1258our @ISA = CFClient::UI::DrawBG::; 1494our @ISA = CFClient::UI::DrawBG::;
1259 1495
1260my @tex = 1496my @tex =
1261 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1497 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1262 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1498 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1263 1499
1264use SDL; 1500use CFClient::OpenGL;
1265use SDL::OpenGL;
1266 1501
1267sub new { 1502sub new {
1268 my $class = shift; 1503 my $class = shift;
1269 1504
1270 $class->SUPER::new ( 1505 $class->SUPER::new (
1281 my ($self) = @_; 1516 my ($self) = @_;
1282 1517
1283 ($self->{padding} * 2 + 6) x 2 1518 ($self->{padding} * 2 + 6) x 2
1284} 1519}
1285 1520
1286sub size_allocate {
1287 my ($self, $x, $y, $w, $h) = @_;
1288
1289 $self->_size_allocate ($x, $y, $w, $h);
1290}
1291
1292sub button_down { 1521sub button_down {
1293 my ($self, $ev, $x, $y) = @_; 1522 my ($self, $ev, $x, $y) = @_;
1294 1523
1295 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1524 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1296 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1525 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1322 glDisable GL_BLEND; 1551 glDisable GL_BLEND;
1323} 1552}
1324 1553
1325############################################################################# 1554#############################################################################
1326 1555
1556package CFClient::UI::Image;
1557
1558our @ISA = CFClient::UI::Base::;
1559
1560use CFClient::OpenGL;
1561use Carp qw/confess/;
1562
1563our %loaded_images;
1564
1565sub new {
1566 my $class = shift;
1567
1568 my $self = $class->SUPER::new (can_events => 0, @_);
1569
1570 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1571
1572 $loaded_images{$self->{image}} ||=
1573 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1574
1575 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1576
1577 Scalar::Util::weaken $loaded_images{$self->{image}};
1578
1579 $self->{aspect} = $tex->{w} / $tex->{h};
1580
1581 $self
1582}
1583
1584sub size_request {
1585 my ($self) = @_;
1586
1587 ($self->{tex}->{w}, $self->{tex}->{h})
1588}
1589
1590sub _draw {
1591 my ($self) = @_;
1592
1593 my $tex = $self->{tex};
1594
1595 my ($w, $h) = ($self->{w}, $self->{h});
1596
1597 if ($self->{rot90}) {
1598 glRotate 90, 0, 0, 1;
1599 glTranslate 0, -$self->{w}, 0;
1600
1601 ($w, $h) = ($h, $w);
1602 }
1603
1604 glEnable GL_BLEND;
1605 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1606 glEnable GL_TEXTURE_2D;
1607 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1608
1609 $tex->draw_quad (0, 0, $w, $h);
1610
1611 glDisable GL_BLEND;
1612 glDisable GL_TEXTURE_2D;
1613}
1614
1615#############################################################################
1616
1617package CFClient::UI::VGauge;
1618
1619our @ISA = CFClient::UI::Base::;
1620
1621use CFClient::OpenGL;
1622
1623my %tex = (
1624 food => [
1625 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1626 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1627 ],
1628 grace => [
1629 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1630 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1631 ],
1632 hp => [
1633 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1634 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1635 ],
1636 mana => [
1637 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1638 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1639 ],
1640);
1641
1642# eg. VGauge->new (gauge => 'food'), default gauge: food
1643sub new {
1644 my $class = shift;
1645
1646 my $self = $class->SUPER::new (
1647 type => 'food',
1648 @_
1649 );
1650
1651 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1652
1653 $self
1654}
1655
1656sub size_request {
1657 my ($self) = @_;
1658
1659 #my $tex = $tex{$self->{type}}[0];
1660 #@$tex{qw(w h)}
1661 (0, 0)
1662}
1663
1664sub set_max {
1665 my ($self, $max) = @_;
1666
1667 $self->{max_val} = $max;
1668}
1669
1670sub set_value {
1671 my ($self, $val, $max) = @_;
1672
1673 $self->set_max ($max)
1674 if defined $max;
1675
1676 $max = $self->{max_val};
1677 $self->{val} = $val;
1678
1679 $self->update;
1680}
1681
1682sub _draw {
1683 my ($self) = @_;
1684
1685 my $tex = $tex{$self->{type}};
1686
1687 my ($w, $h) = ($self->{w}, $self->{h});
1688
1689 if ($self->{vertical}) {
1690 glRotate 90, 0, 0, 1;
1691 glTranslate 0, -$self->{w}, 0;
1692
1693 ($w, $h) = ($h, $w);
1694 }
1695
1696 my $ycut = $self->{val} / ($self->{max_val} || 1);
1697 $ycut = 1 if $self->{val} > $self->{max_val};
1698
1699 my $t1 = $tex->[0];
1700 my $t2 = $tex->[1];
1701
1702 glEnable GL_BLEND;
1703 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1704 glEnable GL_TEXTURE_2D;
1705 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1706
1707 my $h1 = $self->{h} - $ycut * $self->{h};
1708 my $h2 = $ycut * $self->{h};
1709
1710 glBindTexture GL_TEXTURE_2D, $t1->{name};
1711 glBegin GL_QUADS;
1712 glTexCoord 0 , 0; glVertex 0 , 0;
1713 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1714 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1715 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1716 glEnd;
1717
1718 glBindTexture GL_TEXTURE_2D, $t2->{name};
1719 glBegin GL_QUADS;
1720 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1721 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1722 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1723 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1724 glEnd;
1725
1726 glDisable GL_BLEND;
1727 glDisable GL_TEXTURE_2D;
1728}
1729
1730#############################################################################
1731
1732package CFClient::UI::Gauge;
1733
1734our @ISA = CFClient::UI::VBox::;
1735
1736sub new {
1737 my ($class, %arg) = @_;
1738
1739 my $self = $class->SUPER::new (
1740 tooltip => $arg{type},
1741 %arg,
1742 );
1743
1744 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1);
1745 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1746 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1747
1748 $self
1749}
1750
1751sub set_fontsize {
1752 my ($self, $fsize) = @_;
1753
1754 $self->{value}->set_fontsize ($fsize);
1755 $self->{max} ->set_fontsize ($fsize);
1756}
1757
1758sub set_value {
1759 my ($self, $val, $max) = @_;
1760
1761 $self->set_max ($max)
1762 if defined $max;
1763
1764 $self->{gauge}->set_value ($val, $max);
1765 $self->{value}->set_text ($val);
1766}
1767
1768sub set_max {
1769 my ($self, $max) = @_;
1770
1771 $self->{gauge}->set_max ($max);
1772 $self->{max}->set_text ($max);
1773}
1774
1775#############################################################################
1776
1327package CFClient::UI::Slider; 1777package CFClient::UI::Slider;
1328 1778
1329use strict; 1779use strict;
1330 1780
1331use SDL::OpenGL; 1781use CFClient::OpenGL;
1332 1782
1333our @ISA = CFClient::UI::DrawBG::; 1783our @ISA = CFClient::UI::DrawBG::;
1334 1784
1335my @tex = 1785my @tex =
1336 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1786 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1346 # TODO: calculations are off 1796 # TODO: calculations are off
1347 my $self = $class->SUPER::new ( 1797 my $self = $class->SUPER::new (
1348 fg => [1, 1, 1], 1798 fg => [1, 1, 1],
1349 active_fg => [0, 0, 0], 1799 active_fg => [0, 0, 0],
1350 range => [0, 0, 100, 10], 1800 range => [0, 0, 100, 10],
1351 req_w => 40, 1801 req_w => 20,
1352 req_h => 13, 1802 req_h => 20,
1353 vertical => 0, 1803 vertical => 0,
1354 can_hover => 1, 1804 can_hover => 1,
1355 inner_pad => 5, 1805 inner_pad => 5,
1356 @_ 1806 @_
1357 ); 1807 );
1424 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1874 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1425 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1875 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1426 1876
1427 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1877 my ($value, $lo, $hi, $page) = @{$self->{range}};
1428 1878
1879 $hi = $value + 1 if $lo == $hi;
1880
1429 my $inner_pad_px = $self->_calc_inner_pad_px ($w); 1881 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1430 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 1882 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1431 1883
1432 $page = int $page * $inner_w / ($hi - $lo); 1884 $page = int $page * $inner_w / ($hi - $lo);
1433 $value = int +($value - $lo) * $inner_w / ($hi - $lo); 1885 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1459 1911
1460package CFClient::UI::TextView; 1912package CFClient::UI::TextView;
1461 1913
1462our @ISA = CFClient::UI::HBox::; 1914our @ISA = CFClient::UI::HBox::;
1463 1915
1464use SDL::OpenGL; 1916use CFClient::OpenGL;
1465 1917
1466sub new { 1918sub new {
1467 my $class = shift; 1919 my $class = shift;
1468 1920
1469 my $self = $class->SUPER::new ( 1921 my $self = $class->SUPER::new (
1922 fontsize => 1,
1923 #font => default_font
1924 @_,
1925
1926 layout => (new CFClient::Layout),
1470 par => [], 1927 par => [],
1471 @_, 1928 height => 0,
1472 children => [ 1929 children => [
1473 (new CFClient::UI::Empty expand => 1), 1930 (new CFClient::UI::Empty expand => 1),
1474 (new CFClient::UI::Slider vertical => 1), 1931 (new CFClient::UI::Slider vertical => 1),
1475 ], 1932 ],
1476 ); 1933 );
1477 1934
1935 $self->{children}[1]->connect (changed => sub {
1936 $self->update;
1937 });
1938
1478 $self 1939 $self
1940}
1941
1942sub set_fontsize {
1943 my ($self, $fontsize) = @_;
1944
1945 $self->{fontsize} = $fontsize;
1946 $self->reflow;
1947}
1948
1949sub text_height {
1950 my ($self, $text) = @_;
1951
1952 my $layout = $self->{layout};
1953
1954 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1955 $layout->set_width ($self->{w});
1956 $layout->set_text ($text);
1957
1958 ($layout->size)[1]
1959}
1960
1961sub reflow {
1962 my ($self) = @_;
1963
1964 $self->{need_reflow}++;
1965 $self->update;
1966}
1967
1968sub size_allocate {
1969 my ($self, $w, $h) = @_;
1970
1971 $self->SUPER::size_allocate ($w, $h);
1972
1973 $self->{layout}->set_font ($self->{font}) if $self->{font};
1974 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1975 $self->{layout}->set_width ($self->{children}[0]{w});
1976
1977 $self->reflow;
1479} 1978}
1480 1979
1481sub add_paragraph { 1980sub add_paragraph {
1482 my ($self, $color, $text) = @_; 1981 my ($self, $color, $text) = @_;
1483 1982
1983 #TODO: intelligently "reformat" paragraph
1984
1985 my $height = $self->text_height ($text);
1986
1987 $self->{height} += $height;
1988
1484 push @{$self->{par}}, [$color, $text]; 1989 push @{$self->{par}}, [$height, $color, $text];
1485 my $count = scalar @{$self->{par}}; 1990
1486 $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; 1991 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1487 $self->{children}[1]->update; 1992 $self->{children}[1]->update;
1488} 1993}
1489 1994
1995sub update {
1996 my ($self) = @_;
1997
1998 $self->SUPER::update;
1999
2000 return unless $self->{h} > 0;
2001
2002 delete $self->{texture};
2003
2004 $ROOT->on_refresh ($self, sub {
2005 if (delete $self->{need_reflow}) {
2006 my $height = 0;
2007
2008 $height += $_->[0] = $self->text_height ($_->[2])
2009 for @{$self->{par}};
2010
2011 $self->{height} = $height;
2012
2013 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
2014
2015 delete $self->{texture};
2016 }
2017
2018 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
2019 glClearColor 0, 0, 0, 1;
2020 glClear GL_COLOR_BUFFER_BIT;
2021
2022 glEnable GL_BLEND;
2023 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2024 glEnable GL_TEXTURE_2D;
2025 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2026
2027 my $top = int $self->{children}[1]{range}[0];
2028
2029 my $y0 = $top;
2030 my $y1 = $top + $self->{h};
2031
2032 my $y = 0;
2033
2034 my $layout = $self->{layout};
2035
2036 $layout->set_font ($self->{font}) if $self->{font};
2037
2038 for my $par (@{$self->{par}}) {
2039 my $h = $par->[0];
2040
2041 if ($y0 < $y + $h && $y < $y1) {
2042 $layout->set_text ($par->[2]);
2043
2044 glColor @{ $par->[1] };
2045 my ($W, $H) = $layout->size;
2046 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
2047 }
2048
2049 $y += $h;
2050 }
2051
2052 glDisable GL_TEXTURE_2D;
2053 glDisable GL_BLEND;
2054 };
2055 });
2056}
2057
1490sub _draw { 2058sub _draw {
1491 my ($self) = @_; 2059 my ($self) = @_;
1492 2060
1493 my $par = $self->{par}; 2061 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; 2062 glEnable GL_TEXTURE_2D;
1503 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 2063 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1504 2064 $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; 2065 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 ) 2066 }
1547}
1548 2067
1549sub key_down { 2068 $self->{children}[1]->draw;
1550 print "MAPKEYDOWN\n";
1551}
1552 2069
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} 2070}
1695 2071
1696############################################################################# 2072#############################################################################
1697 2073
1698package CFClient::UI::Animator; 2074package CFClient::UI::Animator;
1699 2075
1700use SDL::OpenGL; 2076use CFClient::OpenGL;
1701 2077
1702our @ISA = CFClient::UI::Bin::; 2078our @ISA = CFClient::UI::Bin::;
1703 2079
1704sub moveto { 2080sub moveto {
1705 my ($self, $x, $y) = @_; 2081 my ($self, $x, $y) = @_;
1745 my $class = shift; 2121 my $class = shift;
1746 2122
1747 my $self = $class->SUPER::new ( 2123 my $self = $class->SUPER::new (
1748 state => 0, 2124 state => 0,
1749 connect_activate => \&toggle_flopper, 2125 connect_activate => \&toggle_flopper,
2126 can_events => 1,
1750 @_ 2127 @_
1751 ); 2128 );
1752 2129
1753 if ($self->{state}) { 2130 if ($self->{state}) {
1754 $self->{state} = 0; 2131 $self->{state} = 0;
1761sub toggle_flopper { 2138sub toggle_flopper {
1762 my ($self) = @_; 2139 my ($self) = @_;
1763 2140
1764 # TODO: use animation 2141 # TODO: use animation
1765 if ($self->{state} = !$self->{state}) { 2142 if ($self->{state} = !$self->{state}) {
1766 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2143 $CFClient::UI::ROOT->add ($self->{other});
1767 $self->{other}->move ( 2144 $self->{other}->move ($self->coord2global (0, $self->{h}));
1768 ($::WIDTH - $self->{other}{w}) * 0.5, 2145 $self->emit ("open");
1769 ($::HEIGHT - $self->{other}{h}) * 0.5,
1770 );
1771 } else { 2146 } else {
1772 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2147 $CFClient::UI::ROOT->remove ($self->{other});
2148 $self->emit ("close");
2149 }
2150
2151 $self->emit (changed => $self->{state});
2152}
2153
2154#############################################################################
2155
2156package CFClient::UI::Tooltip;
2157
2158our @ISA = CFClient::UI::Bin::;
2159
2160use CFClient::OpenGL;
2161
2162sub new {
2163 my $class = shift;
2164
2165 $class->SUPER::new (
2166 @_,
2167 can_events => 0,
2168 )
2169}
2170
2171sub set_text {
2172 my ($self, $text) = @_;
2173
2174 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2175 $self->{label}->set_text ($text);
2176 $self->add ($self->{label});
2177}
2178
2179sub size_request {
2180 my ($self) = @_;
2181
2182 $self->child->set_max_size ($::WIDTH * 0.2);
2183
2184 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2185
2186 ($w + 4, $h + 4)
2187}
2188
2189sub _draw {
2190 my ($self) = @_;
2191
2192 glPushMatrix;
2193 glTranslate 0.375, 0.375;
2194
2195 my ($w, $h) = @$self{qw(w h)};
2196
2197 glColor 1, 0.8, 0.4;
2198 glBegin GL_QUADS;
2199 glVertex 0 , 0;
2200 glVertex 0 , $h;
2201 glVertex $w, $h;
2202 glVertex $w, 0;
2203 glEnd;
1773 } 2204
1774} 2205 glColor 0, 0, 0;
2206 glBegin GL_LINE_LOOP;
2207 glVertex 0 , 0;
2208 glVertex 0 , $h;
2209 glVertex $w, $h;
2210 glVertex $w, 0;
2211 glEnd;
2212
2213 glPopMatrix;
1775 2214
1776############################################################################# 2215 glTranslate 2, 2;
2216 $self->SUPER::_draw;
2217}
1777 2218
2219#############################################################################
2220
1778package CFClient::UI::Toplevel; 2221package CFClient::UI::Root;
1779 2222
1780our @ISA = CFClient::UI::Container::; 2223our @ISA = CFClient::UI::Container::;
2224
2225use CFClient::OpenGL;
2226
2227sub check_size {
2228 my ($self) = @_;
2229
2230 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2231}
1781 2232
1782sub size_request { 2233sub size_request {
1783 ($::WIDTH, $::HEIGHT) 2234 ($::WIDTH, $::HEIGHT)
1784} 2235}
1785 2236
1786sub size_allocate { 2237sub configure {
1787 my ($self, $x, $y, $w, $h) = @_; 2238 my ($self, $x, $y, $w, $h) = @_;
1788 2239
1789 $self->_size_allocate ($x, $y, $w, $h); 2240 $self->SUPER::configure ($x, $y, $w, $h);
1790 2241
1791 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1792 for @{$self->{children}}; 2242 for my $child (@{$self->{children}}) {
1793} 2243 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1794 2244
1795sub translate { 2245 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2246 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2247 $child->configure ($X, $Y, $W,$H);
2248 }
2249}
2250
2251sub _topleft {
1796 my ($self, $x, $y) = @_; 2252 my ($self, $x, $y) = @_;
1797 2253
1798 ($x, $y) 2254 ($x, $y)
1799} 2255}
1800 2256
1801sub update { 2257sub update {
1802 my ($self) = @_; 2258 my ($self) = @_;
1803 2259
1804 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2260 $self->check_size;
1805 ::refresh (); 2261 ::refresh ();
1806} 2262}
1807 2263
1808sub add { 2264sub add {
1809 my ($self, $widget) = @_; 2265 my ($self, $child) = @_;
1810 2266
2267 # integerize window positions
2268 $child->{x} = int $child->{x};
2269 $child->{y} = int $child->{y};
2270
1811 $self->SUPER::add ($widget); 2271 $self->SUPER::add ($child);
2272}
1812 2273
1813 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2274sub on_refresh {
2275 my ($self, $id, $cb) = @_;
2276
2277 $self->{refresh_hook}{$id} = $cb;
1814} 2278}
1815 2279
1816sub draw { 2280sub draw {
1817 my ($self) = @_; 2281 my ($self) = @_;
1818 2282
2283 while (my $rcb = delete $self->{refresh_hook}) {
2284 $_->() for values %$rcb;
2285 }
2286
2287 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2288 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2289 glClear GL_COLOR_BUFFER_BIT;
2290
2291 glMatrixMode GL_PROJECTION;
2292 glLoadIdentity;
2293 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2294 glMatrixMode GL_MODELVIEW;
2295 glLoadIdentity;
2296
1819 $self->_draw; 2297 $self->_draw;
1820} 2298}
1821 2299
1822############################################################################# 2300#############################################################################
1823 2301
1824package CFClient::UI; 2302package CFClient::UI;
1825 2303
2304$ROOT = new CFClient::UI::Root;
1826$TOPLEVEL = new CFClient::UI::Toplevel; 2305$TOOLTIP = new CFClient::UI::Tooltip;
1827 2306
18281 23071
1829 2308

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines