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.87 by root, Wed Apr 12 17:11:02 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;
250 return unless $self->{can_focus};
149 251
150 my $focus = $FOCUS; $FOCUS = $self; 252 my $focus = $FOCUS; $FOCUS = $self;
253
254 $self->emit (focus_in => $focus);
255
151 $focus->update if $focus; 256 $focus->update if $focus;
152 $FOCUS->update; 257 $FOCUS->update;
153} 258}
154 259
155sub focus_out { 260sub focus_out {
156 my ($self) = @_; 261 my ($self) = @_;
157 262
158 return unless $FOCUS == $self; 263 return unless $FOCUS == $self;
159 264
160 my $focus = $FOCUS; undef $FOCUS; 265 my $focus = $FOCUS; undef $FOCUS;
266
267 $self->emit (focus_out => $focus);
268
161 $focus->update if $focus; #? 269 $focus->update if $focus; #?
162} 270}
163 271
164sub mouse_motion { } 272sub mouse_motion { }
165sub button_up { } 273sub button_up { }
186 glPushMatrix; 294 glPushMatrix;
187 glTranslate $self->{x}, $self->{y}, 0; 295 glTranslate $self->{x}, $self->{y}, 0;
188 $self->_draw; 296 $self->_draw;
189 glPopMatrix; 297 glPopMatrix;
190 298
191 if ($self == $HOVER) { 299 if ($self == $HOVER && $self->{can_hover}) {
192 my ($x, $y) = @$self{qw(x y)}; 300 my ($x, $y) = @$self{qw(x y)};
193 301
194 glColor 0, 0, 1, 0.2; 302 glColor 1, 0.8, 0.5, 0.2;
195 glEnable GL_BLEND; 303 glEnable GL_BLEND;
196 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 304 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
197 glBegin GL_QUADS; 305 glBegin GL_QUADS;
198 glVertex $x , $y; 306 glVertex $x , $y;
199 glVertex $x + $self->{w}, $y; 307 glVertex $x + $self->{w}, $y;
211} 319}
212 320
213sub find_widget { 321sub find_widget {
214 my ($self, $x, $y) = @_; 322 my ($self, $x, $y) = @_;
215 323
324 return () unless $self->{can_events};
325
216 return $self 326 return $self
217 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 327 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
218 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 328 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
219 329
220 () 330 ()
221} 331}
222 332
223sub set_parent { 333sub set_parent {
224 my ($self, $par) = @_; 334 my ($self, $parent) = @_;
225 335
226 $self->{parent} = $par;
227 Scalar::Util::weaken $self->{parent}; 336 Scalar::Util::weaken ($self->{parent} = $parent);
228} 337}
229 338
230sub get_parent { 339sub check_size {
231 $_[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 }
232} 352}
233 353
234sub update { 354sub update {
235 my ($self) = @_; 355 my ($self) = @_;
236 356
239} 359}
240 360
241sub connect { 361sub connect {
242 my ($self, $signal, $cb) = @_; 362 my ($self, $signal, $cb) = @_;
243 363
244 push @{ $self->{cb}{$signal} }, $cb; 364 push @{ $self->{signal_cb}{$signal} }, $cb;
245} 365}
246 366
247sub emit { 367sub emit {
248 my ($self, $signal, @args) = @_; 368 my ($self, $signal, @args) = @_;
249 369
370 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
250 $_->($self, @args) 371 $cb->($self, @args);
251 for @{$self->{cb}{$signal} || []}; 372 }
252} 373}
253 374
254sub DESTROY { 375sub DESTROY {
255 my ($self) = @_; 376 my ($self) = @_;
256 377
262package CFClient::UI::DrawBG; 383package CFClient::UI::DrawBG;
263 384
264our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
265 386
266use strict; 387use strict;
267use SDL::OpenGL; 388use CFClient::OpenGL;
268 389
269sub new { 390sub new {
270 my $class = shift; 391 my $class = shift;
271 392
272 # range [value, low, high, page] 393 # range [value, low, high, page]
301 422
302package CFClient::UI::Empty; 423package CFClient::UI::Empty;
303 424
304our @ISA = CFClient::UI::Base::; 425our @ISA = CFClient::UI::Base::;
305 426
427sub new {
428 my ($class, %arg) = @_;
429 $class->SUPER::new (can_events => 0, %arg);
430}
431
306sub size_request { 432sub size_request {
307 (0, 0) 433 (0, 0)
308} 434}
309 435
310sub draw { } 436sub draw { }
318sub new { 444sub new {
319 my ($class, %arg) = @_; 445 my ($class, %arg) = @_;
320 446
321 my $children = delete $arg{children} || []; 447 my $children = delete $arg{children} || [];
322 448
323 my $self = $class->SUPER::new (children => [], %arg); 449 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
324 $self->add ($_) for @$children; 450 $self->add ($_) for @$children;
325 451
326 $self 452 $self
327} 453}
328 454
329sub add { 455sub add {
330 my ($self, $chld) = @_; 456 my ($self, $child) = @_;
331 457
332 $chld->set_parent ($self); 458 $child->set_parent ($self);
459
460 use sort 'stable';
333 461
334 $self->{children} = [ 462 $self->{children} = [
335 sort { $a->{z} <=> $b->{z} } 463 sort { $a->{z} <=> $b->{z} }
336 @{$self->{children}}, $chld 464 @{$self->{children}}, $child
337 ]; 465 ];
338 466
339 $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;
340 $self->update; 478 $self->update;
341}
342
343sub remove {
344 my ($self, $widget) = @_;
345
346 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
347
348 $self->size_allocate (0, 0, $self->{w}, $self->{h});
349} 479}
350 480
351sub find_widget { 481sub find_widget {
352 my ($self, $x, $y) = @_; 482 my ($self, $x, $y) = @_;
353 483
383 513
384 $class->SUPER::new (children => [$child], %arg) 514 $class->SUPER::new (children => [$child], %arg)
385} 515}
386 516
387sub add { 517sub add {
388 my ($self, $widget) = @_; 518 my ($self, $child) = @_;
389 519
390 $self->{children} = []; 520 $self->{children} = [];
391 521
392 $self->SUPER::add ($widget); 522 $self->SUPER::add ($child);
393} 523}
394 524
395sub remove { 525sub remove {
396 my ($self, $widget) = @_; 526 my ($self, $widget) = @_;
397 527
406sub size_request { 536sub size_request {
407 $_[0]{children}[0]->size_request 537 $_[0]{children}[0]->size_request
408} 538}
409 539
410sub size_allocate { 540sub size_allocate {
411 my ($self, $x, $y, $w, $h) = @_; 541 my ($self, $w, $h) = @_;
412 542
413 $self->_size_allocate ($x, $y, $w, $h) or return;
414
415 $self->{children}[0]->size_allocate (0, 0, $w, $h); 543 $self->{children}[0]->configure (0, 0, $w, $h);
416} 544}
417 545
418############################################################################# 546#############################################################################
419 547
420package CFClient::UI::Window; 548package CFClient::UI::Window;
421 549
422our @ISA = CFClient::UI::Bin::; 550our @ISA = CFClient::UI::Bin::;
423 551
424use SDL::OpenGL; 552use CFClient::OpenGL;
425 553
426sub new { 554sub new {
427 my ($class, %arg) = @_; 555 my ($class, %arg) = @_;
428 556
429 my $self = $class->SUPER::new (%arg); 557 my $self = $class->SUPER::new (%arg);
438} 566}
439 567
440sub render_chld { 568sub render_chld {
441 my ($self) = @_; 569 my ($self) = @_;
442 570
443 $self->{texture} = 571 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
444 CFClient::Texture->new_from_opengl ( 572 glClearColor 0, 0, 0, 1;
445 $self->{w}, $self->{h}, sub { $self->child->draw } 573 glClear GL_COLOR_BUFFER_BIT;
446 ); 574 $self->child->draw;
575 };
447} 576}
448 577
449sub size_allocate { 578sub size_allocate {
450 my ($self, $x, $y, $w, $h) = @_; 579 my ($self, $w, $h) = @_;
451 580
452 $self->_size_allocate ($x, $y, $w, $h) or return;
453
454 $self->child->size_allocate (0, 0, $w, $h); 581 $self->child->configure (0, 0, $w, $h);
455 582
456 $self->render_chld; 583 $self->render_chld;
457} 584}
458 585
459sub _draw { 586sub _draw {
475 glDisable GL_TEXTURE_2D; 602 glDisable GL_TEXTURE_2D;
476} 603}
477 604
478############################################################################# 605#############################################################################
479 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
480package CFClient::UI::Frame; 631package CFClient::UI::Frame;
481 632
482our @ISA = CFClient::UI::Bin::; 633our @ISA = CFClient::UI::Bin::;
483 634
484use SDL::OpenGL; 635use CFClient::OpenGL;
636
637sub new {
638 my $class = shift;
639
640 my $self = $class->SUPER::new (
641 bg => [1, 1, 1, 1],
642 border_bg => [1, 1, 1, 1],
643 border => 0.8,
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}
485 656
486sub size_request { 657sub size_request {
487 my ($self) = @_; 658 my ($self) = @_;
488 my $chld = $self->child 659 ($self->{req_w}, $self->{req_h})
489 or return (0, 0);
490
491 $chld->move (2, 2);
492
493 map { $_ + 4 } $chld->size_request;
494} 660}
495 661
496sub size_allocate { 662sub size_allocate {
497 my ($self, $x, $y, $w, $h) = @_; 663 my ($self, $w, $h) = @_;
498 664 $self->{w} = $w;
499 $self->_size_allocate ($x, $y, $w, $h) or return; 665 $self->{h} = $h;
500 666 $self->child->configure (0, 0, $w, $h);
501 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
502} 667}
503 668
504sub _draw { 669sub _draw {
505 my ($self) = @_; 670 my ($self) = @_;
506 671
507 my $chld = $self->child;
508
509 my ($w, $h) = $chld->size_request;
510
511 glBegin GL_QUADS;
512 glColor 0, 0, 0;
513 glVertex 0 , 0;
514 glVertex 0 , $h + 4;
515 glVertex $w + 4 , $h + 4;
516 glVertex $w + 4 , 0;
517 glEnd;
518
519 $chld->draw;
520}
521
522#############################################################################
523
524package CFClient::UI::FancyFrame;
525
526our @ISA = CFClient::UI::Bin::;
527
528use SDL::OpenGL;
529
530my @tex =
531 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
532 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
533
534sub size_request {
535 my ($self) = @_;
536
537 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
538
539 my ($w, $h) = $self->SUPER::size_request;
540
541 $h += $tex[1]->{h};
542 $h += $tex[4]->{h};
543 $w += $tex[2]->{w};
544 $w += $tex[3]->{w};
545
546 ($w, $h)
547}
548
549sub size_allocate {
550 my ($self, $x, $y, $w, $h) = @_;
551
552 $self->_size_allocate ($x, $y, $w, $h) or return;
553
554 $h -= $tex[1]{h};
555 $h -= $tex[4]{h};
556 $w -= $tex[2]{w};
557 $w -= $tex[3]{w};
558
559 $h = $h < 0 ? 0 : $h;
560 $w = $w < 0 ? 0 : $w;
561
562 my $child = $self->child;
563
564 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
565}
566
567sub button_down {
568 my ($self, $ev, $x, $y) = @_;
569
570 if ($x < $self->{w} && $x >= $self->{w} - $tex[2]{w}
571 && $y < $self->{h} && $y >= $self->{h} - $tex[4]{h}) {
572
573 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
574 my ($bw, $bh) = ($self->{w}, $self->{h});
575
576 $self->{motion} = sub {
577 my ($ev, $x, $y) = @_;
578
579 ($x, $y) = ($ev->motion_x, $ev->motion_y);
580
581 $self->{user_w} = $bw + $x - $ox;
582 $self->{user_h} = $bh + $y - $oy;
583 $self->update;
584 };
585
586 } elsif ($x >= 0 && $x < $self->{w}
587 && $y >= 0 && $y < $tex[1]{h}) {
588
589 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
590 my ($bx, $by) = ($self->{x}, $self->{y});
591
592 $self->{motion} = sub {
593 my ($ev, $x, $y) = @_;
594
595 ($x, $y) = ($ev->motion_x, $ev->motion_y);
596
597 $self->move ($bx + $x - $ox, $by + $y - $oy);
598 $self->update;
599 };
600 }
601}
602
603sub button_up {
604 my ($self, $ev, $x, $y) = @_;
605
606 delete $self->{motion};
607}
608
609sub mouse_motion {
610 my ($self, $ev, $x, $y) = @_;
611
612 $self->{motion}->($ev, $x, $y) if $self->{motion};
613}
614
615sub _draw {
616 my ($self) = @_;
617
618 my ($w, $h) = ($self->{w}, $self->{h}); 672 my ($w, $h) = ($self->{w}, $self->{h});
619 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
620 673
621 glEnable GL_BLEND; 674 glEnable GL_BLEND;
622 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 675 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
623 glEnable GL_TEXTURE_2D; 676 glEnable GL_TEXTURE_2D;
624 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 677 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
625 678
626 my $top = $tex[1]; 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,
741 )
742}
743
744sub size_allocate {
745 my ($self, $w, $h) = @_;
746
747 $h -= List::Util::max 0, $self->border * 2;
748 $w -= List::Util::max 0, $self->border * 2;
749
750 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
751 if $self->{title};
752
753 $self->child->configure ($self->border, $self->border, $w, $h);
754}
755
756sub button_down {
757 my ($self, $ev, $x, $y) = @_;
758
759 my $border = $self->border;
760
761 if ($x < $self->{w} && $x >= $self->{w} - $border
762 && $y < $self->{h} && $y >= $self->{h} - $border) {
763
764 my ($ox, $oy) = ($ev->{x}, $ev->{y});
765 my ($bw, $bh) = ($self->{w}, $self->{h});
766
767 $self->{motion} = sub {
768 my ($ev, $x, $y) = @_;
769
770 ($x, $y) = ($ev->{x}, $ev->{y});
771
772 $self->{user_w} = $bw + $x - $ox;
773 $self->{user_h} = $bh + $y - $oy;
774 $self->check_size;
775 };
776
777 } elsif ($x >= 0 && $x < $self->{w}
778 && $y >= 0 && $y < $border) {
779
780 my ($ox, $oy) = ($ev->{x}, $ev->{y});
781 my ($bx, $by) = ($self->{x}, $self->{y});
782
783 $self->{motion} = sub {
784 my ($ev, $x, $y) = @_;
785
786 ($x, $y) = ($ev->{x}, $ev->{y});
787
788 $self->move ($bx + $x - $ox, $by + $y - $oy);
789 $self->update;
790 };
791 }
792}
793
794sub button_up {
795 my ($self, $ev, $x, $y) = @_;
796
797 delete $self->{motion};
798}
799
800sub mouse_motion {
801 my ($self, $ev, $x, $y) = @_;
802
803 $self->{motion}->($ev, $x, $y) if $self->{motion};
804}
805
806sub _draw {
807 my ($self) = @_;
808
809 my ($w, $h ) = ($self->{w}, $self->{h});
810 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
811
812 glEnable GL_BLEND;
813 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
814 glEnable GL_TEXTURE_2D;
815 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
816
817 my $border = $self->border;
818
819 glColor @{ $self->{border_bg} };
627 $top->draw_quad (0, 0, $w, $top->{h}); 820 $tex[1]->draw_quad (0, 0, $w, $border);
628 821 $tex[3]->draw_quad (0, $border, $border, $ch);
629 my $left = $tex[3]; 822 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
630 $left->draw_quad (0, $top->{h}, $left->{w}, $ch); 823 $tex[4]->draw_quad (0, $h - $border, $w, $border);
631
632 my $right = $tex[2];
633 $right->draw_quad ($w - $right->{w}, $top->{h}, $right->{w}, $ch);
634
635 my $bottom = $tex[4];
636 $bottom->draw_quad (0, $h - $bottom->{h}, $w, $bottom->{h});
637 824
638 my $bg = $tex[0]; 825 my $bg = $tex[0];
639 826
640 glBindTexture GL_TEXTURE_2D, $bg->{name}; 827 # TODO: repeat texture not scale
641 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
642
643 my $rep_x = $cw / $bg->{w}; 828 my $rep_x = $cw / $bg->{w};
644 my $rep_y = $ch / $bg->{h}; 829 my $rep_y = $ch / $bg->{h};
645 830
646 $bg->draw_quad ($left->{w}, $top->{h}, $cw, $ch); 831 glColor @{ $self->{bg} };
832
833 $bg->{s} = $rep_x;
834 $bg->{t} = $rep_y;
835 $bg->{wrap_mode} = 1;
836 $bg->draw_quad ($border, $border, $cw, $ch);
647 837
648 glDisable GL_TEXTURE_2D; 838 glDisable GL_TEXTURE_2D;
649 glDisable GL_BLEND; 839 glDisable GL_BLEND;
650 840
841 $self->{title}->draw if $self->{title};
651 $self->child->draw; 842 $self->child->draw;
652
653} 843}
654 844
655############################################################################# 845#############################################################################
656 846
657package CFClient::UI::Table; 847package CFClient::UI::Table;
658 848
659our @ISA = CFClient::UI::Base::; 849our @ISA = CFClient::UI::Base::;
660 850
661use List::Util qw(max sum); 851use List::Util qw(max sum);
662 852
663use SDL::OpenGL; 853use CFClient::OpenGL;
664 854
665sub new { 855sub new {
666 my $class = shift; 856 my $class = shift;
667 857
668 $class->SUPER::new ( 858 $class->SUPER::new (
670 @_ 860 @_
671 ) 861 )
672} 862}
673 863
674sub add { 864sub add {
675 my ($self, $x, $y, $chld) = @_; 865 my ($self, $x, $y, $child) = @_;
676 866
867 $child->set_parent ($self);
677 $self->{children}[$y][$x] = $chld; 868 $self->{children}[$y][$x] = $child;
678 $chld->set_parent ($self);
679 869
680 $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};
681 $self->update; 878 $self->update;
682} 879}
683 880
684sub get_wh { 881sub get_wh {
685 my ($self) = @_; 882 my ($self) = @_;
691 or next; 888 or next;
692 889
693 for my $x (0 .. $#$row) { 890 for my $x (0 .. $#$row) {
694 my $widget = $row->[$x] 891 my $widget = $row->[$x]
695 or next; 892 or next;
696 my ($w, $h) = $widget->size_request; 893 my ($w, $h) = @$widget{qw(req_w req_h)};
697 894
698 $w[$x] = max $w[$x], $w; 895 $w[$x] = max $w[$x], $w;
699 $h[$y] = max $h[$y], $h; 896 $h[$y] = max $h[$y], $h;
700 } 897 }
701 } 898 }
713 (sum @$hs), 910 (sum @$hs),
714 ) 911 )
715} 912}
716 913
717sub size_allocate { 914sub size_allocate {
718 my ($self, $x, $y, $w, $h) = @_; 915 my ($self, $w, $h) = @_;
719
720 $self->_size_allocate ($x, $y, $w, $h) or return;
721 916
722 my ($ws, $hs) = $self->get_wh; 917 my ($ws, $hs) = $self->get_wh;
723 918
724 my $req_w = sum @$ws; 919 my $req_w = sum @$ws;
725 my $req_h = sum @$hs; 920 my $req_h = sum @$hs;
730 my $col_expand = (sum @col_expand) || 1; 925 my $col_expand = (sum @col_expand) || 1;
731 926
732 # linearly scale sizes 927 # linearly scale sizes
733 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 928 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
734 $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;
735 933
736 my $y; 934 my $y;
737 935
738 for my $r (0 .. $#{$self->{children}}) { 936 for my $r (0 .. $#{$self->{children}}) {
739 my $row = $self->{children}[$r] 937 my $row = $self->{children}[$r]
744 942
745 for my $c (0 .. $#$row) { 943 for my $c (0 .. $#$row) {
746 my $col_w = $ws->[$c]; 944 my $col_w = $ws->[$c];
747 945
748 if (my $widget = $row->[$c]) { 946 if (my $widget = $row->[$c]) {
749 $widget->size_allocate ($x, $y, $col_w, $row_h); 947 $widget->configure ($x, $y, $col_w, $row_h);
750 } 948 }
751 949
752 $x += $col_w; 950 $x += $col_w;
753 } 951 }
754 952
799 (List::Util::max map $_->[1], @alloc), 997 (List::Util::max map $_->[1], @alloc),
800 ) 998 )
801} 999}
802 1000
803sub size_allocate { 1001sub size_allocate {
804 my ($self, $x, $y, $w, $h) = @_; 1002 my ($self, $w, $h) = @_;
805
806 $self->_size_allocate ($x, $y, $w, $h);
807 1003
808 ($h, $w) = ($w, $h); 1004 ($h, $w) = ($w, $h);
809 1005
810 my $children = $self->{children}; 1006 my $children = $self->{children};
811 1007
812 my @h = map +($_->size_request)[0], @$children; 1008 my @h = map $_->{req_w}, @$children;
813 1009
814 my $req_h = List::Util::sum @h; 1010 my $req_h = List::Util::sum @h;
815 1011
816 if ($req_h > $h) { 1012 if ($req_h > $h) {
817 # ah well, not enough space 1013 # ah well, not enough space
827 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 1023 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
828 $h[$_] = $alloc_h; 1024 $h[$_] = $alloc_h;
829 } 1025 }
830 } 1026 }
831 1027
1028 CFClient::UI::harmonize \@h;
1029
832 my $y = 0; 1030 my $y = 0;
833 for (0 .. $#$children) { 1031 for (0 .. $#$children) {
834 my $child = $children->[$_]; 1032 my $child = $children->[$_];
835 my $h = $h[$_]; 1033 my $h = $h[$_];
836 $child->size_allocate ($y, 0, $h, $w); 1034 $child->configure ($y, 0, $h, $w);
837 1035
838 $y += $h; 1036 $y += $h;
839 } 1037 }
1038
1039 1
840} 1040}
841 1041
842############################################################################# 1042#############################################################################
843 1043
844package CFClient::UI::VBox; 1044package CFClient::UI::VBox;
857 (List::Util::sum map $_->[1], @alloc), 1057 (List::Util::sum map $_->[1], @alloc),
858 ) 1058 )
859} 1059}
860 1060
861sub size_allocate { 1061sub size_allocate {
862 my ($self, $x, $y, $w, $h) = @_; 1062 my ($self, $w, $h) = @_;
863
864 $self->_size_allocate ($x, $y, $w, $h);
865 1063
866 my $children = $self->{children}; 1064 my $children = $self->{children};
867 1065
868 my @h = map +($_->size_request)[1], @$children; 1066 my @h = map $_->{req_h}, @$children;
869 1067
870 my $req_h = List::Util::sum @h; 1068 my $req_h = List::Util::sum @h;
871 1069
872 if ($req_h > $h) { 1070 if ($req_h > $h) {
873 # ah well, not enough space 1071 # ah well, not enough space
881 1079
882 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1080 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
883 } 1081 }
884 } 1082 }
885 1083
1084 CFClient::UI::harmonize \@h;
1085
886 my $y = 0; 1086 my $y = 0;
887 for (0 .. $#$children) { 1087 for (0 .. $#$children) {
888 my $child = $children->[$_]; 1088 my $child = $children->[$_];
889 my $h = $h[$_]; 1089 my $h = $h[$_];
890 $child->size_allocate (0, $y, $w, $h); 1090 $child->configure (0, $y, $w, $h);
891 1091
892 $y += $h; 1092 $y += $h;
893 } 1093 }
1094
1095 1
894} 1096}
895 1097
896############################################################################# 1098#############################################################################
897 1099
898package CFClient::UI::Label; 1100package CFClient::UI::Label;
899 1101
900our @ISA = CFClient::UI::Base::; 1102our @ISA = CFClient::UI::Base::;
901 1103
902use SDL::OpenGL; 1104use CFClient::OpenGL;
903 1105
904sub new { 1106sub new {
905 my ($class, %arg) = @_; 1107 my ($class, %arg) = @_;
906 1108
907 my $self = $class->SUPER::new ( 1109 my $self = $class->SUPER::new (
908 fg => [1, 1, 1], 1110 fg => [1, 1, 1],
909 height => $::FONTSIZE, 1111 #font => default_font
1112 fontsize => 1,
910 text => "", 1113 text => "",
911 align => -1, 1114 align => -1,
1115 valign => -1,
912 padding => 2, 1116 padding => 2,
913 layout => new CFClient::Layout, 1117 layout => new CFClient::Layout,
1118 can_events => 0,
914 %arg 1119 %arg
915 ); 1120 );
916 1121
917 $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};
918 1130
919 $self 1131 $self
920} 1132}
921 1133
922sub escape_text { 1134sub escape_text {
930} 1142}
931 1143
932sub set_text { 1144sub set_text {
933 my ($self, $text) = @_; 1145 my ($self, $text) = @_;
934 1146
935 $self->{text} = $text;
936 $self->{layout}->set_markup ($text); 1147 $self->{layout}->set_text ($text);
937 1148
938 delete $self->{texture}; 1149 delete $self->{texture};
939# $self->{w} = $self->{h} = -1; 1150 $self->check_size;
940 $self->update; 1151 $self->update;
941} 1152}
942 1153
943sub get_text { 1154sub set_markup {
944 my ($self, $text) = @_; 1155 my ($self, $markup) = @_;
945 1156
946 $self->{text} 1157 $self->{layout}->set_markup ($markup);
1158
1159 delete $self->{texture};
1160 $self->check_size;
1161 $self->update;
947} 1162}
948 1163
949sub size_request { 1164sub size_request {
950 my ($self) = @_; 1165 my ($self) = @_;
951 1166
952 $self->{layout}->set_width; 1167 $self->{layout}->set_font ($self->{font}) if $self->{font};
1168 $self->{layout}->set_width ($self->{max_w} || -1);
953 $self->{layout}->set_height ($self->{height}); 1169 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1170
954 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 }
955 1182
956 ( 1183 (
957 $w + $self->{padding} * 2, 1184 $w + $self->{padding} * 2,
958 $h + $self->{padding} * 2, 1185 $h + $self->{padding} * 2,
959 ) 1186 )
960} 1187}
961 1188
962sub size_allocate { 1189sub size_allocate {
963 my ($self, $x, $y, $w, $h) = @_; 1190 my ($self, $w, $h) = @_;
964
965 $self->_size_allocate ($x, $y, $w, $h) or return;
966 1191
967 delete $self->{texture}; 1192 delete $self->{texture};
968} 1193}
969 1194
970sub update { 1195sub set_fontsize {
971 my ($self) = @_; 1196 my ($self, $fontsize) = @_;
972 1197
1198 $self->{fontsize} = $fontsize;
973 delete $self->{texture}; 1199 delete $self->{texture};
1200 $self->check_size;
974 $self->SUPER::update; 1201 $self->update;
975} 1202}
976 1203
977sub _draw { 1204sub _draw {
978 my ($self) = @_; 1205 my ($self) = @_;
979 1206
980 my $tex = $self->{texture} ||= do { 1207 my $tex = $self->{texture} ||= do {
1208 $self->{layout}->set_font ($self->{font}) if $self->{font};
981 $self->{layout}->set_width ($self->{w}); 1209 $self->{layout}->set_width ($self->{w});
982 $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);
983 new_from_layout CFClient::Texture $self->{layout} 1211 new_from_layout CFClient::Texture $self->{layout}
984 }; 1212 };
985 1213
986 glEnable GL_BLEND; 1214 glEnable GL_BLEND;
987 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1215 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
988 glEnable GL_TEXTURE_2D; 1216 glEnable GL_TEXTURE_2D;
989 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1217 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
990 1218
991 glColor @{$self->{fg}}; 1219 glColor @{$self->{fg}};
992 1220
993 my $x = 1221 $self->{ox} = int (
994 $self->{align} < 0 ? $self->{padding} 1222 $self->{align} < 0 ? $self->{padding}
995 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1223 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
996 : ($self->{w} - $tex->{w}) * 0.5; 1224 : ($self->{w} - $tex->{w}) * 0.5
1225 );
997 1226
998 glTranslate $x, ($self->{h} - $tex->{h}) * 0.5, 0; 1227 $self->{oy} = int (
999 $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});
1000 1234
1001 glDisable GL_TEXTURE_2D; 1235 glDisable GL_TEXTURE_2D;
1002 glDisable GL_BLEND; 1236 glDisable GL_BLEND;
1003} 1237}
1004 1238
1005############################################################################# 1239#############################################################################
1006 1240
1007package CFClient::UI::Entry; 1241package CFClient::UI::EntryBase;
1008 1242
1009our @ISA = CFClient::UI::Label::; 1243our @ISA = CFClient::UI::Label::;
1010 1244
1011use SDL; 1245use CFClient::OpenGL;
1012use SDL::OpenGL;
1013 1246
1014sub new { 1247sub new {
1015 my $class = shift; 1248 my $class = shift;
1016 1249
1017 $class->SUPER::new ( 1250 $class->SUPER::new (
1018 fg => [1, 1, 1], 1251 fg => [1, 1, 1],
1019 bg => [0, 0, 0, 0.2], 1252 bg => [0, 0, 0, 0.2],
1020 active_bg => [1, 1, 1, 0.5], 1253 active_bg => [1, 1, 1, 0.5],
1021 active_fg => [0, 0, 0], 1254 active_fg => [0, 0, 0],
1255 can_hover => 1,
1256 can_focus => 1,
1257 valign => 0,
1258 can_events => 1,
1022 @_ 1259 @_
1023 ) 1260 )
1024} 1261}
1025 1262
1026sub _set_text { 1263sub _set_text {
1027 my ($self, $text) = @_; 1264 my ($self, $text) = @_;
1028 1265
1266 delete $self->{cur_h};
1267
1268 return if $self->{text} eq $text;
1269
1270 delete $self->{texture};
1271
1029 $self->{last_activity} = $::NOW; 1272 $self->{last_activity} = $::NOW;
1030
1031 $self->{text} = $text; 1273 $self->{text} = $text;
1032 $self->{layout}->set_width ($self->{w});
1033 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height});
1034 1274
1035 $text =~ s/./*/g if $self->{hidden}; 1275 $text =~ s/./*/g if $self->{hidden};
1276 $self->{layout}->set_text ("$text ");
1036 1277
1037 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1278 $self->emit (changed => $self->{text});
1279}
1038 1280
1039 $text = substr $text, 0, $self->{cursor}; 1281sub get_text {
1040 utf8::encode $text; 1282 $_[0]{text}
1041
1042 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1043} 1283}
1044 1284
1045sub size_request { 1285sub size_request {
1046 my ($self) = @_; 1286 my ($self) = @_;
1047 1287
1049 1289
1050 ($w + 1, $h) # add 1 for cursor 1290 ($w + 1, $h) # add 1 for cursor
1051} 1291}
1052 1292
1053sub size_allocate { 1293sub size_allocate {
1054 my ($self, $x, $y, $w, $h) = @_; 1294 my ($self, $w, $h) = @_;
1055 1295
1056 $self->SUPER::size_allocate ($x, $y, $w, $h); 1296 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1057
1058 $self->_set_text ($self->{text});
1059} 1297}
1060 1298
1061sub set_text { 1299sub set_text {
1062 my ($self, $text) = @_; 1300 my ($self, $text) = @_;
1063 1301
1067} 1305}
1068 1306
1069sub key_down { 1307sub key_down {
1070 my ($self, $ev) = @_; 1308 my ($self, $ev) = @_;
1071 1309
1072 my $mod = $ev->key_mod; 1310 my $mod = $ev->{mod};
1073 my $sym = $ev->key_sym; 1311 my $sym = $ev->{sym};
1074
1075 my $uni = $ev->key_unicode; 1312 my $uni = $ev->{unicode};
1076 1313
1077 my $text = $self->get_text; 1314 my $text = $self->get_text;
1078 1315
1079 if ($sym == SDLK_BACKSPACE) { 1316 if ($sym == 8) {
1080 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1317 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1081 } elsif ($sym == SDLK_DELETE) { 1318 } elsif ($sym == 127) {
1082 substr $text, $self->{cursor}, 1, ""; 1319 substr $text, $self->{cursor}, 1, "";
1083 } elsif ($sym == SDLK_LEFT) { 1320 } elsif ($sym == CFClient::SDLK_LEFT) {
1084 --$self->{cursor} if $self->{cursor}; 1321 --$self->{cursor} if $self->{cursor};
1085 } elsif ($sym == SDLK_RIGHT) { 1322 } elsif ($sym == CFClient::SDLK_RIGHT) {
1086 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1323 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1087 } elsif ($sym == SDLK_HOME) { 1324 } elsif ($sym == CFClient::SDLK_HOME) {
1088 $self->{cursor} = 0; 1325 $self->{cursor} = 0;
1089 } elsif ($sym == SDLK_END) { 1326 } elsif ($sym == CFClient::SDLK_END) {
1090 $self->{cursor} = length $text; 1327 $self->{cursor} = length $text;
1328 } elsif ($sym == 27) {
1329 $self->emit ('escape');
1091 } elsif ($uni) { 1330 } elsif ($uni) {
1092 substr $text, $self->{cursor}++, 0, chr $uni; 1331 substr $text, $self->{cursor}++, 0, chr $uni;
1093 } 1332 }
1094 1333
1095 $self->_set_text ($text); 1334 $self->_set_text ($text);
1149 1388
1150 $self->SUPER::_draw; 1389 $self->SUPER::_draw;
1151 1390
1152 #TODO: force update every cursor change :( 1391 #TODO: force update every cursor change :(
1153 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
1154 glColor @{$self->{fg}}; 1401 glColor @{$self->{fg}};
1155 glBegin GL_LINES; 1402 glBegin GL_LINES;
1156 glVertex $self->{cur_x}, $self->{cur_y};
1157 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};
1158 glEnd; 1405 glEnd;
1159 } 1406 }
1160} 1407}
1161 1408
1409package CFClient::UI::Entry;
1410
1411our @ISA = CFClient::UI::EntryBase::;
1412
1413use CFClient::OpenGL;
1414
1415sub key_down {
1416 my ($self, $ev) = @_;
1417
1418 my $sym = $ev->{sym};
1419
1420 if ($sym == 13) {
1421 $self->emit (activate => $self->get_text);
1422 $self->update;
1423
1424 } else {
1425 $self->SUPER::key_down ($ev);
1426 }
1427
1428}
1429
1162############################################################################# 1430#############################################################################
1163 1431
1164package CFClient::UI::Button; 1432package CFClient::UI::Button;
1165 1433
1166our @ISA = CFClient::UI::Label::; 1434our @ISA = CFClient::UI::Label::;
1167 1435
1168use SDL; 1436use CFClient::OpenGL;
1169use SDL::OpenGL;
1170 1437
1171my @tex = 1438my @tex =
1172 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1439 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1173 qw(b1_button_active.png); 1440 qw(b1_button_active.png);
1174 1441
1175sub new { 1442sub new {
1176 my $class = shift; 1443 my $class = shift;
1177 1444
1178 $class->SUPER::new ( 1445 $class->SUPER::new (
1179 padding => 4, 1446 padding => 4,
1180 fg => [1, 1, 1], 1447 fg => [1, 1, 1],
1181 bg => [1, 1, 1, 0.2], 1448 bg => [1, 1, 1, 0.2],
1182 active_fg => [1, 1, 0],
1183# active_bg => [0, 0, 0, 0.5], 1449 active_fg => [0, 0, 1],
1184# border_fg => [1, 1, 0], 1450 can_hover => 1,
1451 align => 0,
1452 valign => 0,
1453 can_events => 1,
1185 @_ 1454 @_
1186 ) 1455 )
1187} 1456}
1188 1457
1189sub button_up { 1458sub button_up {
1197 1466
1198sub _draw { 1467sub _draw {
1199 my ($self) = @_; 1468 my ($self) = @_;
1200 1469
1201 local $self->{fg} = $self->{fg}; 1470 local $self->{fg} = $self->{fg};
1202 my $tex = $tex[0];
1203
1204 glEnable GL_BLEND;
1205 glEnable GL_TEXTURE_2D;
1206 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1207 1471
1208 if ($GRAB == $self) { 1472 if ($GRAB == $self) {
1209 $self->{fg} = $self->{active_fg}; 1473 $self->{fg} = $self->{active_fg};
1210 } 1474 }
1211 1475
1212 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;
1213 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;
1214 1481
1215 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1482 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1216 1483
1217 glDisable GL_TEXTURE_2D; 1484 glDisable GL_TEXTURE_2D;
1218 glDisable GL_BLEND; 1485 glDisable GL_BLEND;
1219 1486
1220 $self->SUPER::_draw; 1487 $self->SUPER::_draw;
1224 1491
1225package CFClient::UI::CheckBox; 1492package CFClient::UI::CheckBox;
1226 1493
1227our @ISA = CFClient::UI::DrawBG::; 1494our @ISA = CFClient::UI::DrawBG::;
1228 1495
1229use SDL; 1496my @tex =
1230use SDL::OpenGL; 1497 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1498 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1499
1500use CFClient::OpenGL;
1231 1501
1232sub new { 1502sub new {
1233 my $class = shift; 1503 my $class = shift;
1234 1504
1235 $class->SUPER::new ( 1505 $class->SUPER::new (
1236 padding => 2, 1506 padding => 2,
1237 fg => [1, 1, 1], 1507 fg => [1, 1, 1],
1238 active_fg => [1, 1, 0], 1508 active_fg => [1, 1, 0],
1239 state => 0, 1509 state => 0,
1510 can_hover => 1,
1240 @_ 1511 @_
1241 ) 1512 )
1242} 1513}
1243 1514
1244sub size_request { 1515sub size_request {
1245 my ($self) = @_; 1516 my ($self) = @_;
1246 1517
1247 ($self->{padding} * 2 + 6) x 2 1518 ($self->{padding} * 2 + 6) x 2
1248}
1249
1250sub size_allocate {
1251 my ($self, $x, $y, $w, $h) = @_;
1252
1253 $self->_size_allocate ($x, $y, $w, $h);
1254} 1519}
1255 1520
1256sub button_down { 1521sub button_down {
1257 my ($self, $ev, $x, $y) = @_; 1522 my ($self, $ev, $x, $y) = @_;
1258 1523
1269 $self->SUPER::_draw; 1534 $self->SUPER::_draw;
1270 1535
1271 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1536 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1272 1537
1273 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1538 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1274 1539
1275 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1540 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1276 1541
1277 glBegin GL_LINE_LOOP; 1542 glEnable GL_BLEND;
1278 glVertex 0 , 0; 1543 glEnable GL_TEXTURE_2D;
1279 glVertex 0 , $s; 1544 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1280 glVertex $s, $s;
1281 glVertex $s, 0;
1282 glEnd;
1283 1545
1284 if ($self->{state}) { 1546 my $tex = $self->{state} ? $tex[1] : $tex[0];
1285 glBegin GL_LINES;
1286 glVertex 0 , 0;
1287 glVertex $s, $s;
1288 glVertex $s, 0;
1289 glVertex 0 , $s;
1290 glEnd;
1291 }
1292}
1293 1547
1294############################################################################# 1548 $tex->draw_quad (0, 0, $s, $s);
1295 1549
1550 glDisable GL_TEXTURE_2D;
1551 glDisable GL_BLEND;
1552}
1553
1554#############################################################################
1555
1296package CFClient::UI::Slider; 1556package CFClient::UI::Image;
1297 1557
1298use strict;
1299
1300use SDL::OpenGL;
1301
1302our @ISA = CFClient::UI::DrawBG::; 1558our @ISA = CFClient::UI::Base::;
1559
1560use CFClient::OpenGL;
1561use Carp qw/confess/;
1562
1563our %loaded_images;
1303 1564
1304sub new { 1565sub new {
1305 my $class = shift; 1566 my $class = shift;
1306 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
1777package CFClient::UI::Slider;
1778
1779use strict;
1780
1781use CFClient::OpenGL;
1782
1783our @ISA = CFClient::UI::DrawBG::;
1784
1785my @tex =
1786 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1787 qw(s1_slider.png s1_slider_bg.png);
1788
1789sub new {
1790 my $class = shift;
1791
1307 # range [value, low, high, page] 1792 # range [value, low, high, page]
1308 1793
1794 # TODO: 0-width page
1795 # TODO: req_w/h are wrong with vertical
1796 # TODO: calculations are off
1309 my $self = $class->SUPER::new ( 1797 my $self = $class->SUPER::new (
1310 fg => [1, 1, 1], 1798 fg => [1, 1, 1],
1311 active_fg => [0, 0, 0], 1799 active_fg => [0, 0, 0],
1312 range => [0, 0, 100, 10], 1800 range => [0, 0, 100, 10],
1313 req_w => 40, 1801 req_w => 20,
1314 req_h => 10, 1802 req_h => 20,
1315 vertical => 0, 1803 vertical => 0,
1804 can_hover => 1,
1805 inner_pad => 5,
1316 @_ 1806 @_
1317 ); 1807 );
1318 1808
1319 $self 1809 $self
1320} 1810}
1341 if ($GRAB == $self) { 1831 if ($GRAB == $self) {
1342 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1832 my ($value, $lo, $hi, $page) = @{$self->{range}};
1343 1833
1344 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1834 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1345 1835
1836 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1837 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1838
1839 $x -= $inner_pad_px; # substract the padding
1346 $x = $x * ($hi - $lo) / $w + $lo; 1840 $x = $x * ($hi - $lo) / $inner_w + $lo;
1347 $x = $lo if $x < $lo; 1841 $x = $lo if $x < $lo;
1348 $x = $hi - $page if $x > $hi - $page; 1842 $x = $hi - $page if $x > $hi - $page;
1349 $self->{range}[0] = $x; 1843 $self->{range}[0] = $x;
1350 1844
1351 $self->emit (changed => $x); 1845 $self->emit (changed => $x);
1352 $self->update; 1846 $self->update;
1353 } 1847 }
1848}
1849
1850# the inner_* stuff is for generating a padding for the slider handle,
1851# so that the handle doesn't leave the texture. This calculation isn't 100%
1852# correct propably, but it does the job for now
1853sub _calc_inner_pad_px {
1854 my ($self, $w) = @_;
1855 ($w / 100) * $self->{inner_pad} # % to pixels
1354} 1856}
1355 1857
1356sub _draw { 1858sub _draw {
1357 my ($self) = @_; 1859 my ($self) = @_;
1358 1860
1372 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1874 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1373 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1875 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1374 1876
1375 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1877 my ($value, $lo, $hi, $page) = @{$self->{range}};
1376 1878
1879 $hi = $value + 1 if $lo == $hi;
1880
1881 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1882 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1883
1377 $page = int $page * $w / ($hi - $lo); 1884 $page = int $page * $inner_w / ($hi - $lo);
1378 $value = int +($value - $lo) * $w / ($hi - $lo); 1885 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1379 1886
1380 $w -= $page; 1887 $w -= $page;
1381 $page &= ~1; 1888 $page &= ~1;
1382 glTranslate $page * 0.5, 0, 0; 1889 glTranslate $page * 0.5, 0, 0;
1383 $page ||= 2; 1890 $page ||= 2;
1384 1891
1385 glColor @$fg;
1386 glBegin GL_LINES;
1387 glVertex 0, 0; glVertex 0, $h;
1388 glVertex $w - 1, 0; glVertex $w - 1, $h;
1389 glVertex 0, $h * 0.5; glVertex $w, $h * 0.5;
1390 glEnd;
1391
1392 my $knob_a = $value - $page * 0.5; 1892 my $knob_a = $inner_pad_px + ($value - $page * 0.5);
1393 my $knob_b = $value + $page * 0.5; 1893 my $knob_b = $inner_pad_px + ($value + $page * 0.5);
1394 1894
1395 glBegin GL_QUADS; 1895 glEnable GL_BLEND;
1396 glColor @$fg; 1896 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1397 glVertex $knob_a, 0; 1897 glEnable GL_TEXTURE_2D;
1398 glVertex $knob_a, $h; 1898 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1399 glVertex $knob_b, $h;
1400 glVertex $knob_b, 0;
1401 1899
1402 if ($knob_a < $knob_b - 2) { 1900 # draw background
1403 glColor @$bg; 1901 $tex[1]->draw_quad (0, 0, $w, $h);
1404 glVertex $knob_a + 1, 1;
1405 glVertex $knob_a + 1, $h - 1;
1406 glVertex $knob_b - 1, $h - 1;
1407 glVertex $knob_b - 1, 1;
1408 }
1409 glEnd;
1410}
1411 1902
1412############################################################################# 1903 # draw handle
1904 $tex[0]->draw_quad ($knob_a, 0, $knob_b - $knob_a, $h);
1413 1905
1906 glDisable GL_BLEND;
1907 glDisable GL_TEXTURE_2D;
1908}
1909
1910#############################################################################
1911
1414package CFClient::UI::MapWidget; 1912package CFClient::UI::TextView;
1415 1913
1416use strict;
1417
1418use List::Util qw(min max);
1419
1420use SDL;
1421use SDL::OpenGL;
1422
1423our @ISA = CFClient::UI::Base::; 1914our @ISA = CFClient::UI::HBox::;
1915
1916use CFClient::OpenGL;
1424 1917
1425sub new { 1918sub new {
1426 my $class = shift; 1919 my $class = shift;
1427 1920
1428 $class->SUPER::new ( 1921 my $self = $class->SUPER::new (
1429 z => -1, 1922 fontsize => 1,
1430 list => (glGenLists 1), 1923 #font => default_font
1431 @_ 1924 @_,
1925
1926 layout => (new CFClient::Layout),
1927 par => [],
1928 height => 0,
1929 children => [
1930 (new CFClient::UI::Empty expand => 1),
1931 (new CFClient::UI::Slider vertical => 1),
1932 ],
1432 ) 1933 );
1433}
1434 1934
1435sub key_down { 1935 $self->{children}[1]->connect (changed => sub {
1436 print "MAPKEYDOWN\n"; 1936 $self->update;
1437} 1937 });
1438 1938
1439sub key_up { 1939 $self
1440} 1940}
1441 1941
1442sub size_request { 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);
1443 ( 1957
1444 1 + 32 * int $::WIDTH / 32, 1958 ($layout->size)[1]
1445 1 + 32 * int $::HEIGHT / 32, 1959}
1446 ) 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;
1978}
1979
1980sub add_paragraph {
1981 my ($self, $color, $text) = @_;
1982
1983 #TODO: intelligently "reformat" paragraph
1984
1985 my $height = $self->text_height ($text);
1986
1987 $self->{height} += $height;
1988
1989 push @{$self->{par}}, [$height, $color, $text];
1990
1991 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1992 $self->{children}[1]->update;
1447} 1993}
1448 1994
1449sub update { 1995sub update {
1450 my ($self) = @_; 1996 my ($self) = @_;
1451 1997
1452 $self->{need_update} = 1;
1453 $self->SUPER::update; 1998 $self->SUPER::update;
1454}
1455 1999
1456sub draw { 2000 return unless $self->{h} > 0;
1457 my ($self) = @_;
1458 2001
2002 delete $self->{texture};
2003
2004 $ROOT->on_refresh ($self, sub {
1459 if (delete $self->{need_update}) { 2005 if (delete $self->{need_reflow}) {
1460 glNewList $self->{list}, GL_COMPILE; 2006 my $height = 0;
1461 2007
1462 my $mx = $::CONN->{mapx}; 2008 $height += $_->[0] = $self->text_height ($_->[2])
1463 my $my = $::CONN->{mapy}; 2009 for @{$self->{par}};
1464 2010
1465 my $map = $::CONN->{map}; 2011 $self->{height} = $height;
1466 2012
1467 my ($xofs, $yofs); 2013 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1468 2014
1469 my $sw = 1 + int $::WIDTH / 32; 2015 delete $self->{texture};
1470 my $sh = 1 + int $::HEIGHT / 32;
1471
1472 if ($::CONN->{mapw} > $sw) {
1473 $xofs = $mx + ($::CONN->{mapw} - $sw) * 0.5;
1474 } else {
1475 $xofs = $self->{xofs} = min $mx, max $mx + $::CONN->{mapw} - $sw + 1, $self->{xofs};
1476 } 2016 }
1477 2017
1478 if ($::CONN->{maph} > $sh) { 2018 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1479 $yofs = $my + ($::CONN->{maph} - $sh) * 0.5; 2019 glClearColor 0, 0, 0, 1;
1480 } else { 2020 glClear GL_COLOR_BUFFER_BIT;
1481 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 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;
1482 } 2054 };
2055 });
2056}
1483 2057
2058sub _draw {
2059 my ($self) = @_;
2060
2061 if ($self->{texture}) {
1484 glEnable GL_TEXTURE_2D; 2062 glEnable GL_TEXTURE_2D;
1485 glEnable GL_BLEND;
1486 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1487 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 2063 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1488 2064 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1489 my $sw4 = ($sw + 3) & ~3;
1490 my $darkness = "\x00" x ($sw4 * $sh);
1491
1492 for my $x (0 .. $sw - 1) {
1493 my $row = $map->[$x + $xofs];
1494 for my $y (0 .. $sh - 1) {
1495
1496 my $cell = $row->[$y + $yofs]
1497 or next;
1498
1499 my $dark = $cell->[0];
1500 if ($dark < 0) {
1501 substr $darkness, $y * $sw4 + $x, 1, chr 224;
1502 } else {
1503 substr $darkness, $y * $sw4 + $x, 1, chr 255 - $dark;
1504 }
1505
1506 for my $num (grep $_, @$cell[1,2,3]) {
1507 my $tex = $::CONN->{face}[$num]{texture} || next;
1508
1509 my ($w, $h) = @$tex{qw(w h)};
1510
1511 $tex->draw_quad (($x + 1) * 32 - $w, ($y + 1) * 32 - $h, $w, $h);
1512 }
1513 }
1514 }
1515
1516# if (1) { # higher quality darkness
1517# $lighting =~ s/(.)/$1$1$1/gs;
1518# my $pb = new_from_data Gtk2::Gdk::Pixbuf $lighting, "rgb", 0, 8, $sw4, $sh, $sw4 * 3;
1519#
1520# $pb = $pb->scale_simple ($sw4 * 0.5, $sh * 0.5, "bilinear");
1521#
1522# $lighting = $pb->get_pixels;
1523# $lighting =~ s/(.)../$1/gs;
1524# }
1525
1526 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1527
1528 $darkness = new CFClient::Texture
1529 w => $sw4,
1530 h => $sh,
1531 data => $darkness,
1532 internalformat => GL_ALPHA,
1533 format => GL_ALPHA;
1534
1535 glColor 0.45, 0.45, 0.45, 1;
1536 $darkness->draw_quad (0, 0, $sw4 * 32, $sh * 32);
1537
1538 glDisable GL_TEXTURE_2D; 2065 glDisable GL_TEXTURE_2D;
1539 glDisable GL_BLEND;
1540
1541 glEndList;
1542 } 2066 }
1543 2067
1544 glCallList $self->{list}; 2068 $self->{children}[1]->draw;
1545 2069
1546 if ($FOCUS != $self) {
1547 glEnable GL_BLEND;
1548 glColor 0, 0, 1, 0.4;
1549 glBegin GL_QUADS;
1550 glVertex 0, 0;
1551 glVertex 0, $::HEIGHT;
1552 glVertex $::WIDTH, $::HEIGHT;
1553 glVertex $::WIDTH, 0;
1554 glEnd;
1555 glDisable GL_BLEND;
1556 }
1557}
1558
1559my %DIR = (
1560 SDLK_KP8, [1, "north"],
1561 SDLK_KP9, [2, "northeast"],
1562 SDLK_KP6, [3, "east"],
1563 SDLK_KP3, [4, "southeast"],
1564 SDLK_KP2, [5, "south"],
1565 SDLK_KP1, [6, "southwest"],
1566 SDLK_KP4, [7, "west"],
1567 SDLK_KP7, [8, "northwest"],
1568
1569 SDLK_UP, [1, "north"],
1570 SDLK_RIGHT, [3, "east"],
1571 SDLK_DOWN, [5, "south"],
1572 SDLK_LEFT, [7, "west"],
1573);
1574
1575sub key_down {
1576 my ($self, $ev) = @_;
1577
1578 my $mod = $ev->key_mod;
1579 my $sym = $ev->key_sym;
1580
1581 if ($sym == SDLK_KP5) {
1582 $::CONN->send ("command stay fire");
1583 } elsif ($sym == SDLK_a) {
1584 $::CONN->send ("command apply");
1585 } elsif (exists $DIR{$sym}) {
1586 if ($mod & KMOD_SHIFT) {
1587 $self->{shft}++;
1588 $::CONN->send ("command fire $DIR{$sym}[0]");
1589 } elsif ($mod & KMOD_CTRL) {
1590 $self->{ctrl}++;
1591 $::CONN->send ("command run $DIR{$sym}[0]");
1592 } else {
1593 $::CONN->send ("command $DIR{$sym}[1]");
1594 }
1595 }
1596}
1597
1598sub key_up {
1599 my ($self, $ev) = @_;
1600
1601 my $mod = $ev->key_mod;
1602 my $sym = $ev->key_sym;
1603
1604 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1605 $::CONN->send ("command fire_stop");
1606 }
1607 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1608 $::CONN->send ("command run_stop");
1609 }
1610} 2070}
1611 2071
1612############################################################################# 2072#############################################################################
1613 2073
1614package CFClient::UI::Animator; 2074package CFClient::UI::Animator;
1615 2075
1616use SDL::OpenGL; 2076use CFClient::OpenGL;
1617 2077
1618our @ISA = CFClient::UI::Bin::; 2078our @ISA = CFClient::UI::Bin::;
1619 2079
1620sub moveto { 2080sub moveto {
1621 my ($self, $x, $y) = @_; 2081 my ($self, $x, $y) = @_;
1651 glPopMatrix; 2111 glPopMatrix;
1652} 2112}
1653 2113
1654############################################################################# 2114#############################################################################
1655 2115
1656package CFClient::UI::Toplevel; 2116package CFClient::UI::Flopper;
2117
2118our @ISA = CFClient::UI::Button::;
2119
2120sub new {
2121 my $class = shift;
2122
2123 my $self = $class->SUPER::new (
2124 state => 0,
2125 connect_activate => \&toggle_flopper,
2126 can_events => 1,
2127 @_
2128 );
2129
2130 if ($self->{state}) {
2131 $self->{state} = 0;
2132 $self->toggle_flopper;
2133 }
2134
2135 $self
2136}
2137
2138sub toggle_flopper {
2139 my ($self) = @_;
2140
2141 # TODO: use animation
2142 if ($self->{state} = !$self->{state}) {
2143 $CFClient::UI::ROOT->add ($self->{other});
2144 $self->{other}->move ($self->coord2global (0, $self->{h}));
2145 $self->emit ("open");
2146 } else {
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;
2204
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;
2214
2215 glTranslate 2, 2;
2216 $self->SUPER::_draw;
2217}
2218
2219#############################################################################
2220
2221package CFClient::UI::Root;
1657 2222
1658our @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}
1659 2232
1660sub size_request { 2233sub size_request {
1661 ($::WIDTH, $::HEIGHT) 2234 ($::WIDTH, $::HEIGHT)
1662} 2235}
1663 2236
1664sub size_allocate { 2237sub configure {
1665 my ($self, $x, $y, $w, $h) = @_; 2238 my ($self, $x, $y, $w, $h) = @_;
1666 2239
1667 $self->_size_allocate ($x, $y, $w, $h); 2240 $self->SUPER::configure ($x, $y, $w, $h);
1668 2241
1669 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1670 for @{$self->{children}}; 2242 for my $child (@{$self->{children}}) {
1671} 2243 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1672 2244
1673sub 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 {
1674 my ($self, $x, $y) = @_; 2252 my ($self, $x, $y) = @_;
1675 2253
1676 ($x, $y) 2254 ($x, $y)
1677} 2255}
1678 2256
1679sub update { 2257sub update {
1680 my ($self) = @_; 2258 my ($self) = @_;
1681 2259
1682 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2260 $self->check_size;
1683 ::refresh (); 2261 ::refresh ();
1684} 2262}
1685 2263
1686sub add { 2264sub add {
1687 my ($self, $widget) = @_; 2265 my ($self, $child) = @_;
1688 2266
2267 # integerize window positions
2268 $child->{x} = int $child->{x};
2269 $child->{y} = int $child->{y};
2270
1689 $self->SUPER::add ($widget); 2271 $self->SUPER::add ($child);
2272}
1690 2273
1691 $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;
1692} 2278}
1693 2279
1694sub draw { 2280sub draw {
1695 my ($self) = @_; 2281 my ($self) = @_;
1696 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
1697 $self->_draw; 2297 $self->_draw;
1698} 2298}
1699 2299
1700############################################################################# 2300#############################################################################
1701 2301
1702package CFClient::UI; 2302package CFClient::UI;
1703 2303
2304$ROOT = new CFClient::UI::Root;
1704$TOPLEVEL = new CFClient::UI::Toplevel; 2305$TOOLTIP = new CFClient::UI::Tooltip;
1705 2306
17061 23071
1707 2308

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines