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.101 by elmex, Fri Apr 14 11:43:51 2006 UTC vs.
Revision 1.149 by root, Sat Apr 22 23:11: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 $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $TOPLEVEL->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->translate ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->translate ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $TOPLEVEL->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
65 65
66 $hover->update if $hover; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71}
72
73# convert position array to integers
74sub harmonize {
75 my ($vals) = @_;
76
77 my $rem = 0;
78
79 for (@$vals) {
80 my $i = int $_ + $rem;
81 $rem += $_ - $i;
82 $_ = $i;
83 }
71} 84}
72 85
73############################################################################# 86#############################################################################
74 87
75package CFClient::UI::Base; 88package CFClient::UI::Base;
76 89
77use strict; 90use strict;
78 91
79use SDL::OpenGL; 92use CFClient::OpenGL;
80 93
81sub new { 94sub new {
82 my $class = shift; 95 my $class = shift;
83 96
84 my $self = bless { 97 my $self = bless {
85 x => 0, 98 x => 0,
86 y => 0, 99 y => 0,
87 z => 0, 100 z => 0,
88 w => -1,
89 h => -1,
90 @_ 101 @_
91 }, $class; 102 }, $class;
92 103
93 for (keys %$self) { 104 for (keys %$self) {
94 if (/^connect_(.*)$/) { 105 if (/^connect_(.*)$/) {
97 } 108 }
98 109
99 $self 110 $self
100} 111}
101 112
113sub show {
114 my ($self) = @_;
115
116 return if $self->{parent};
117
118 $CFClient::UI::ROOT->add ($self);
119}
120
121sub hide {
122 my ($self) = @_;
123
124 return unless $self->{parent};
125
126 $self->{parent}->remove ($self);
127}
128
102sub move { 129sub move {
103 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
104 $self->{x} = $x; 132 $self->{x} = int $x;
105 $self->{y} = $y; 133 $self->{y} = int $y;
106 $self->{z} = $z if defined $z; 134 $self->{z} = $z if defined $z;
135
136 $self->update;
107} 137}
108 138
109sub needs_redraw { 139sub needs_redraw {
110 0 140 0
111} 141}
112 142
113sub size_request { 143sub size_request {
114 require Carp; 144 require Carp;
115 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abstract";
116} 146}
117 147
118sub _size_allocate { 148sub configure {
119 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
120 150
151 if ($self->{aspect}) {
152 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154
155 # use alignment to adjust x, y
156
157 $x += int +($w - $w2) * 0.5;
158 $y += int +($h - $h2) * 0.5;
159
160 ($w, $h) = ($w2, $h2);
161 }
162
163 if ($self->{x} != $x || $self->{y} != $y) {
121 $self->{x} = $x; 164 $self->{x} = $x;
122 $self->{y} = $y; 165 $self->{y} = $y;
166 $self->update;
167 }
123 168
124 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
125
126 $self->{w} = $w; 170 $self->{w} = $w;
127 $self->{h} = $h; 171 $self->{h} = $h;
128 172
173 $self->size_allocate ($w, $h);
174 $self->update;
129 1 175 }
130} 176}
131 177
132sub size_allocate { 178sub size_allocate {
179 # nothing to be done
180}
181
182# return top left coordinates
183sub _topleft {
133 my ($self, $x, $y, $w, $h) = @_; 184 my ($self, $x, $y) = @_;
134 185
135 $self->_size_allocate ($x, $y, $w, $h); 186 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
136} 187}
137 188
138# translate global coordinates to local coordinate system 189# translate global coordinates to local coordinate system
139sub translate { 190sub coord2local {
140 my ($self, $x, $y) = @_; 191 my ($self, $x, $y) = @_;
141 192
142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 193 my ($X, $Y) = $self->_topleft;
194 ($x - $X, $y - $Y)
195}
196
197# translate local coordinates to global coordinate system
198sub coord2global {
199 my ($self, $x, $y) = @_;
200
201 my ($X, $Y) = $self->_topleft;
202 ($x + $X, $y + $Y)
143} 203}
144 204
145sub focus_in { 205sub focus_in {
146 my ($self) = @_; 206 my ($self) = @_;
147 207
148 return if $FOCUS == $self; 208 return if $FOCUS == $self;
149 return unless $self->{can_focus}; 209 return unless $self->{can_focus};
150 210
151 my $focus = $FOCUS; $FOCUS = $self; 211 my $focus = $FOCUS; $FOCUS = $self;
212
213 $self->emit (focus_in => $focus);
214
152 $focus->update if $focus; 215 $focus->update if $focus;
153 $FOCUS->update; 216 $FOCUS->update;
154} 217}
155 218
156sub focus_out { 219sub focus_out {
157 my ($self) = @_; 220 my ($self) = @_;
158 221
159 return unless $FOCUS == $self; 222 return unless $FOCUS == $self;
160 223
161 my $focus = $FOCUS; undef $FOCUS; 224 my $focus = $FOCUS; undef $FOCUS;
225
226 $self->emit (focus_out => $focus);
227
162 $focus->update if $focus; #? 228 $focus->update if $focus; #?
163} 229}
164 230
165sub mouse_motion { } 231sub mouse_motion { }
166sub button_up { } 232sub button_up { }
190 glPopMatrix; 256 glPopMatrix;
191 257
192 if ($self == $HOVER && $self->{can_hover}) { 258 if ($self == $HOVER && $self->{can_hover}) {
193 my ($x, $y) = @$self{qw(x y)}; 259 my ($x, $y) = @$self{qw(x y)};
194 260
195 glColor 0, 0, 1, 0.2; 261 glColor 1, 0.8, 0.5, 0.2;
196 glEnable GL_BLEND; 262 glEnable GL_BLEND;
197 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 263 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
198 glBegin GL_QUADS; 264 glBegin GL_QUADS;
199 glVertex $x , $y; 265 glVertex $x , $y;
200 glVertex $x + $self->{w}, $y; 266 glVertex $x + $self->{w}, $y;
220 286
221 () 287 ()
222} 288}
223 289
224sub set_parent { 290sub set_parent {
225 my ($self, $par) = @_; 291 my ($self, $parent) = @_;
226 292
227 $self->{parent} = $par;
228 Scalar::Util::weaken $self->{parent}; 293 Scalar::Util::weaken ($self->{parent} = $parent);
229} 294}
230 295
231sub get_parent { 296sub check_size {
232 $_[0]->{parent} 297 my ($self) = @_;
298
299 return unless $self->{parent};
300
301 my ($w, $h) = $self->size_request;
302
303 if ($w != $self->{req_w} || $h != $self->{req_h}) {
304 $self->{req_w} = $w;
305 $self->{req_h} = $h;
306
307 $self->{parent}->check_size;
308 }
233} 309}
234 310
235sub update { 311sub update {
236 my ($self) = @_; 312 my ($self) = @_;
237 313
240} 316}
241 317
242sub connect { 318sub connect {
243 my ($self, $signal, $cb) = @_; 319 my ($self, $signal, $cb) = @_;
244 320
245 push @{ $self->{cb}{$signal} }, $cb; 321 push @{ $self->{signal_cb}{$signal} }, $cb;
246} 322}
247 323
248sub emit { 324sub emit {
249 my ($self, $signal, @args) = @_; 325 my ($self, $signal, @args) = @_;
250 326
327 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
251 $_->($self, @args) 328 $cb->($self, @args);
252 for @{$self->{cb}{$signal} || []}; 329 }
253} 330}
254 331
255sub DESTROY { 332sub DESTROY {
256 my ($self) = @_; 333 my ($self) = @_;
257 334
263package CFClient::UI::DrawBG; 340package CFClient::UI::DrawBG;
264 341
265our @ISA = CFClient::UI::Base::; 342our @ISA = CFClient::UI::Base::;
266 343
267use strict; 344use strict;
268use SDL::OpenGL; 345use CFClient::OpenGL;
269 346
270sub new { 347sub new {
271 my $class = shift; 348 my $class = shift;
272 349
273 # range [value, low, high, page] 350 # range [value, low, high, page]
326 403
327 $self 404 $self
328} 405}
329 406
330sub add { 407sub add {
331 my ($self, $chld) = @_; 408 my ($self, $child) = @_;
332 409
333 $chld->set_parent ($self); 410 $child->set_parent ($self);
411
412 use sort 'stable';
334 413
335 $self->{children} = [ 414 $self->{children} = [
336 sort { $a->{z} <=> $b->{z} } 415 sort { $a->{z} <=> $b->{z} }
337 @{$self->{children}}, $chld 416 @{$self->{children}}, $child
338 ]; 417 ];
339 418
340 $self->{w} = $self->{h} = -1; 419 $child->check_size;
341 $self->update;
342} 420}
343 421
344sub remove { 422sub remove {
345 my ($self, $widget) = @_; 423 my ($self, $child) = @_;
346 424
425 delete $child->{parent};
426
347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 427 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
348 428
349 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 429 $self->check_size;
350} 430}
351 431
352sub find_widget { 432sub find_widget {
353 my ($self, $x, $y) = @_; 433 my ($self, $x, $y) = @_;
354 434
384 464
385 $class->SUPER::new (children => [$child], %arg) 465 $class->SUPER::new (children => [$child], %arg)
386} 466}
387 467
388sub add { 468sub add {
389 my ($self, $widget) = @_; 469 my ($self, $child) = @_;
390 470
391 $self->{children} = []; 471 $self->{children} = [];
392 472
393 $self->SUPER::add ($widget); 473 $self->SUPER::add ($child);
394} 474}
395 475
396sub remove { 476sub remove {
397 my ($self, $widget) = @_; 477 my ($self, $widget) = @_;
398 478
407sub size_request { 487sub size_request {
408 $_[0]{children}[0]->size_request 488 $_[0]{children}[0]->size_request
409} 489}
410 490
411sub size_allocate { 491sub size_allocate {
412 my ($self, $x, $y, $w, $h) = @_; 492 my ($self, $w, $h) = @_;
413 493
414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
416 $self->{children}[0]->size_allocate (0, 0, $w, $h); 494 $self->{children}[0]->configure (0, 0, $w, $h);
417} 495}
418 496
419############################################################################# 497#############################################################################
420 498
421package CFClient::UI::Window; 499package CFClient::UI::Window;
422 500
423our @ISA = CFClient::UI::Bin::; 501our @ISA = CFClient::UI::Bin::;
424 502
425use SDL::OpenGL; 503use CFClient::OpenGL;
426 504
427sub new { 505sub new {
428 my ($class, %arg) = @_; 506 my ($class, %arg) = @_;
429 507
430 my $self = $class->SUPER::new (%arg); 508 my $self = $class->SUPER::new (%arg);
439} 517}
440 518
441sub render_chld { 519sub render_chld {
442 my ($self) = @_; 520 my ($self) = @_;
443 521
444 $self->{texture} = 522 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
445 CFClient::Texture->new_from_opengl ( 523 glClearColor 0, 0, 0, 1;
446 $self->{w}, $self->{h}, sub { $self->child->draw } 524 glClear GL_COLOR_BUFFER_BIT;
447 ); 525 $self->child->draw;
526 };
448} 527}
449 528
450sub size_allocate { 529sub size_allocate {
451 my ($self, $x, $y, $w, $h) = @_; 530 my ($self, $w, $h) = @_;
452 531
453 $self->_size_allocate ($x, $y, $w, $h) or return;
454
455 $self->child->size_allocate (0, 0, $w, $h); 532 $self->child->configure (0, 0, $w, $h);
456 533
457 $self->render_chld; 534 $self->render_chld;
458} 535}
459 536
460sub _draw { 537sub _draw {
476 glDisable GL_TEXTURE_2D; 553 glDisable GL_TEXTURE_2D;
477} 554}
478 555
479############################################################################# 556#############################################################################
480 557
558package CFClient::UI::ViewPort;
559
560our @ISA = CFClient::UI::Window::;
561
562sub new { die }
563
564sub size_request {
565 my ($self) = @_;
566
567 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
568 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
569
570 @$self{qw(child_w child_h)}
571}
572
573sub _draw {
574 my ($self) = @_;
575
576 $self->{children}[1]->draw;
577}
578
579
580#############################################################################
581
481package CFClient::UI::Frame; 582package CFClient::UI::Frame;
482 583
483our @ISA = CFClient::UI::Bin::; 584our @ISA = CFClient::UI::Bin::;
484 585
485use SDL::OpenGL; 586use 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 587
535sub new { 588sub new {
536 my $class = shift; 589 my $class = shift;
537 590
538 # TODO: user_x, user_y, overwrite moveto?
539
540 $class->SUPER::new ( 591 my $self = $class->SUPER::new (
541 bg => [1, 1, 1, 1], 592 bg => [1, 1, 1, 1],
542 border_bg => [1, 1, 1, 1], 593 border_bg => [1, 1, 1, 1],
543 border => $::FONTSIZE * 0.8, 594 border => 0.8,
544 @_ 595 @_
596 );
597
598 $self
599}
600
601sub set_size {
602 my ($self, $w, $h) = @_;
603 $self->{req_w} = $w;
604 $self->{req_h} = $h;
605 $self->check_size;
606}
607
608sub size_request {
609 my ($self) = @_;
610 ($self->{req_w}, $self->{req_h})
611}
612
613sub size_allocate {
614 my ($self, $w, $h) = @_;
615 $self->{w} = $w;
616 $self->{h} = $h;
617 $self->child->configure (0, 0, $w, $h);
618}
619
620sub _draw {
621 my ($self) = @_;
622
623 my ($w, $h) = ($self->{w}, $self->{h});
624
625 glEnable GL_BLEND;
626 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
627 glEnable GL_TEXTURE_2D;
628 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
629
630# glBegin GL_QUADS;
631# glColor 0, 0, 0, 0;
632# glVertex 0 , 0;
633# glVertex 0 , $h;
634# glVertex $w, $h;
635# glVertex $w, 0;
636# glEnd;
637
638
639 $self->child->draw;
640 glDisable GL_BLEND;
641 glDisable GL_TEXTURE_2D;
642}
643
644#############################################################################
645
646package CFClient::UI::FancyFrame;
647
648our @ISA = CFClient::UI::Bin::;
649
650use CFClient::OpenGL;
651
652my @tex =
653 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
654 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
655
656sub new {
657 my $class = shift;
658
659 # TODO: user_x, user_y, overwrite moveto?
660
661 my $self = $class->SUPER::new (
662 bg => [1, 1, 1, 1],
663 border_bg => [1, 1, 1, 1],
664 border => 0.8,
665 @_
666 );
667
668 $self->{title} &&= new CFClient::UI::Label
669 align => 0,
670 valign => 1,
671 text => $self->{title},
672 fontsize => 1;
673
674 $self
675}
676
677sub border {
678 int $_[0]{border} * $::FONTSIZE
679}
680
681sub size_request {
682 my ($self) = @_;
683
684 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
685
686 my ($w, $h) = $self->SUPER::size_request;
687
688 (
689 $w + $self->border * 2,
690 $h + $self->border * 2,
545 ) 691 )
546} 692}
547 693
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 { 694sub size_allocate {
562 my ($self, $x, $y, $w, $h) = @_; 695 my ($self, $w, $h) = @_;
563 696
564 $self->_size_allocate ($x, $y, $w, $h) or return;
565
566 $h -= List::Util::max 0, $self->{border} * 2; 697 $h -= List::Util::max 0, $self->border * 2;
567 $w -= List::Util::max 0, $self->{border} * 2; 698 $w -= List::Util::max 0, $self->border * 2;
568 699
700 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
701 if $self->{title};
702
569 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 703 $self->child->configure ($self->border, $self->border, $w, $h);
570} 704}
571 705
572sub button_down { 706sub button_down {
573 my ($self, $ev, $x, $y) = @_; 707 my ($self, $ev, $x, $y) = @_;
574 708
709 my $border = $self->border;
710
575 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 711 if ($x < $self->{w} && $x >= $self->{w} - $border
576 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 712 && $y < $self->{h} && $y >= $self->{h} - $border) {
577 713
578 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 714 my ($ox, $oy) = ($ev->{x}, $ev->{y});
579 my ($bw, $bh) = ($self->{w}, $self->{h}); 715 my ($bw, $bh) = ($self->{w}, $self->{h});
580 716
581 $self->{motion} = sub { 717 $self->{motion} = sub {
582 my ($ev, $x, $y) = @_; 718 my ($ev, $x, $y) = @_;
583 719
584 ($x, $y) = ($ev->motion_x, $ev->motion_y); 720 ($x, $y) = ($ev->{x}, $ev->{y});
585 721
586 $self->{user_w} = $bw + $x - $ox; 722 $self->{user_w} = $bw + $x - $ox;
587 $self->{user_h} = $bh + $y - $oy; 723 $self->{user_h} = $bh + $y - $oy;
588 $self->update; 724 $self->check_size;
589 }; 725 };
590 726
591 } elsif ($x >= 0 && $x < $self->{w} 727 } elsif ($x >= 0 && $x < $self->{w}
592 && $y >= 0 && $y < $self->{border}) { 728 && $y >= 0 && $y < $border) {
593 729
594 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 730 my ($ox, $oy) = ($ev->{x}, $ev->{y});
595 my ($bx, $by) = ($self->{x}, $self->{y}); 731 my ($bx, $by) = ($self->{x}, $self->{y});
596 732
597 $self->{motion} = sub { 733 $self->{motion} = sub {
598 my ($ev, $x, $y) = @_; 734 my ($ev, $x, $y) = @_;
599 735
600 ($x, $y) = ($ev->motion_x, $ev->motion_y); 736 ($x, $y) = ($ev->{x}, $ev->{y});
601 737
602 $self->move ($bx + $x - $ox, $by + $y - $oy); 738 $self->move ($bx + $x - $ox, $by + $y - $oy);
603 $self->update; 739 $self->update;
604 }; 740 };
605 } 741 }
626 glEnable GL_BLEND; 762 glEnable GL_BLEND;
627 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 763 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
628 glEnable GL_TEXTURE_2D; 764 glEnable GL_TEXTURE_2D;
629 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 765 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
630 766
767 my $border = $self->border;
768
631 glColor @{ $self->{border_bg} }; 769 glColor @{ $self->{border_bg} };
632 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 770 $tex[1]->draw_quad (0, 0, $w, $border);
633 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 771 $tex[3]->draw_quad (0, $border, $border, $ch);
634 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 772 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
635 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 773 $tex[4]->draw_quad (0, $h - $border, $w, $border);
636 774
637 my $bg = $tex[0]; 775 my $bg = $tex[0];
638 776
639 # TODO: repeat texture not scale 777 # TODO: repeat texture not scale
640 my $rep_x = $cw / $bg->{w}; 778 my $rep_x = $cw / $bg->{w};
643 glColor @{ $self->{bg} }; 781 glColor @{ $self->{bg} };
644 782
645 $bg->{s} = $rep_x; 783 $bg->{s} = $rep_x;
646 $bg->{t} = $rep_y; 784 $bg->{t} = $rep_y;
647 $bg->{wrap_mode} = 1; 785 $bg->{wrap_mode} = 1;
648 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 786 $bg->draw_quad ($border, $border, $cw, $ch);
649 787
650 glDisable GL_TEXTURE_2D; 788 glDisable GL_TEXTURE_2D;
651 glDisable GL_BLEND; 789 glDisable GL_BLEND;
652 790
791 $self->{title}->draw if $self->{title};
653 $self->child->draw; 792 $self->child->draw;
654} 793}
655 794
656############################################################################# 795#############################################################################
657 796
659 798
660our @ISA = CFClient::UI::Base::; 799our @ISA = CFClient::UI::Base::;
661 800
662use List::Util qw(max sum); 801use List::Util qw(max sum);
663 802
664use SDL::OpenGL; 803use CFClient::OpenGL;
665 804
666sub new { 805sub new {
667 my $class = shift; 806 my $class = shift;
668 807
669 $class->SUPER::new ( 808 $class->SUPER::new (
671 @_ 810 @_
672 ) 811 )
673} 812}
674 813
675sub add { 814sub add {
676 my ($self, $x, $y, $chld) = @_; 815 my ($self, $x, $y, $child) = @_;
677 816
817 $child->set_parent ($self);
678 $self->{children}[$y][$x] = $chld; 818 $self->{children}[$y][$x] = $child;
679 $chld->set_parent ($self);
680 819
681 $self->{w} = $self->{h} = -1; 820 $child->check_size;
821}
822
823# TODO: move to container class maybe? send childs a signal on removal?
824sub clear {
825 my ($self) = @_;
826
827 delete $self->{children};
682 $self->update; 828 $self->update;
683} 829}
684 830
685sub get_wh { 831sub get_wh {
686 my ($self) = @_; 832 my ($self) = @_;
692 or next; 838 or next;
693 839
694 for my $x (0 .. $#$row) { 840 for my $x (0 .. $#$row) {
695 my $widget = $row->[$x] 841 my $widget = $row->[$x]
696 or next; 842 or next;
697 my ($w, $h) = $widget->size_request; 843 my ($w, $h) = @$widget{qw(req_w req_h)};
698 844
699 $w[$x] = max $w[$x], $w; 845 $w[$x] = max $w[$x], $w;
700 $h[$y] = max $h[$y], $h; 846 $h[$y] = max $h[$y], $h;
701 } 847 }
702 } 848 }
714 (sum @$hs), 860 (sum @$hs),
715 ) 861 )
716} 862}
717 863
718sub size_allocate { 864sub size_allocate {
719 my ($self, $x, $y, $w, $h) = @_; 865 my ($self, $w, $h) = @_;
720
721 $self->_size_allocate ($x, $y, $w, $h) or return;
722 866
723 my ($ws, $hs) = $self->get_wh; 867 my ($ws, $hs) = $self->get_wh;
724 868
725 my $req_w = sum @$ws; 869 my $req_w = sum @$ws;
726 my $req_h = sum @$hs; 870 my $req_h = sum @$hs;
731 my $col_expand = (sum @col_expand) || 1; 875 my $col_expand = (sum @col_expand) || 1;
732 876
733 # linearly scale sizes 877 # linearly scale sizes
734 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 878 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
735 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 879 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
880
881 CFClient::UI::harmonize $ws;
882 CFClient::UI::harmonize $hs;
736 883
737 my $y; 884 my $y;
738 885
739 for my $r (0 .. $#{$self->{children}}) { 886 for my $r (0 .. $#{$self->{children}}) {
740 my $row = $self->{children}[$r] 887 my $row = $self->{children}[$r]
745 892
746 for my $c (0 .. $#$row) { 893 for my $c (0 .. $#$row) {
747 my $col_w = $ws->[$c]; 894 my $col_w = $ws->[$c];
748 895
749 if (my $widget = $row->[$c]) { 896 if (my $widget = $row->[$c]) {
750 $widget->size_allocate ($x, $y, $col_w, $row_h); 897 $widget->configure ($x, $y, $col_w, $row_h);
751 } 898 }
752 899
753 $x += $col_w; 900 $x += $col_w;
754 } 901 }
755 902
800 (List::Util::max map $_->[1], @alloc), 947 (List::Util::max map $_->[1], @alloc),
801 ) 948 )
802} 949}
803 950
804sub size_allocate { 951sub size_allocate {
805 my ($self, $x, $y, $w, $h) = @_; 952 my ($self, $w, $h) = @_;
806
807 $self->_size_allocate ($x, $y, $w, $h);
808 953
809 ($h, $w) = ($w, $h); 954 ($h, $w) = ($w, $h);
810 955
811 my $children = $self->{children}; 956 my $children = $self->{children};
812 957
813 my @h = map +($_->size_request)[0], @$children; 958 my @h = map $_->{req_w}, @$children;
814 959
815 my $req_h = List::Util::sum @h; 960 my $req_h = List::Util::sum @h;
816 961
817 if ($req_h > $h) { 962 if ($req_h > $h) {
818 # ah well, not enough space 963 # ah well, not enough space
828 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 973 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
829 $h[$_] = $alloc_h; 974 $h[$_] = $alloc_h;
830 } 975 }
831 } 976 }
832 977
978 CFClient::UI::harmonize \@h;
979
833 my $y = 0; 980 my $y = 0;
834 for (0 .. $#$children) { 981 for (0 .. $#$children) {
835 my $child = $children->[$_]; 982 my $child = $children->[$_];
836 my $h = $h[$_]; 983 my $h = $h[$_];
837 $child->size_allocate ($y, 0, $h, $w); 984 $child->configure ($y, 0, $h, $w);
838 985
839 $y += $h; 986 $y += $h;
840 } 987 }
988
989 1
841} 990}
842 991
843############################################################################# 992#############################################################################
844 993
845package CFClient::UI::VBox; 994package CFClient::UI::VBox;
858 (List::Util::sum map $_->[1], @alloc), 1007 (List::Util::sum map $_->[1], @alloc),
859 ) 1008 )
860} 1009}
861 1010
862sub size_allocate { 1011sub size_allocate {
863 my ($self, $x, $y, $w, $h) = @_; 1012 my ($self, $w, $h) = @_;
864
865 $self->_size_allocate ($x, $y, $w, $h);
866 1013
867 my $children = $self->{children}; 1014 my $children = $self->{children};
868 1015
869 my @h = map +($_->size_request)[1], @$children; 1016 my @h = map $_->{req_h}, @$children;
870 1017
871 my $req_h = List::Util::sum @h; 1018 my $req_h = List::Util::sum @h;
872 1019
873 if ($req_h > $h) { 1020 if ($req_h > $h) {
874 # ah well, not enough space 1021 # ah well, not enough space
882 1029
883 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1030 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
884 } 1031 }
885 } 1032 }
886 1033
1034 CFClient::UI::harmonize \@h;
1035
887 my $y = 0; 1036 my $y = 0;
888 for (0 .. $#$children) { 1037 for (0 .. $#$children) {
889 my $child = $children->[$_]; 1038 my $child = $children->[$_];
890 my $h = $h[$_]; 1039 my $h = $h[$_];
891 $child->size_allocate (0, $y, $w, $h); 1040 $child->configure (0, $y, $w, $h);
892 1041
893 $y += $h; 1042 $y += $h;
894 } 1043 }
1044
1045 1
895} 1046}
896 1047
897############################################################################# 1048#############################################################################
898 1049
899package CFClient::UI::Label; 1050package CFClient::UI::Label;
900 1051
901our @ISA = CFClient::UI::Base::; 1052our @ISA = CFClient::UI::Base::;
902 1053
903use SDL::OpenGL; 1054use CFClient::OpenGL;
904 1055
905sub new { 1056sub new {
906 my ($class, %arg) = @_; 1057 my ($class, %arg) = @_;
907 1058
908 my $self = $class->SUPER::new ( 1059 my $self = $class->SUPER::new (
909 fg => [1, 1, 1], 1060 fg => [1, 1, 1],
910 height => $::FONTSIZE, 1061 fontsize => 1,
911 text => "", 1062 text => "",
912 align => -1, 1063 align => -1,
1064 valign => -1,
913 padding => 2, 1065 padding => 2,
914 layout => new CFClient::Layout, 1066 layout => new CFClient::Layout,
915 %arg 1067 %arg
916 ); 1068 );
917 1069
918 $self->set_text ($self->{text}); 1070 if (exists $self->{template}) {
1071 my $layout = new CFClient::Layout;
1072 $layout->set_text (delete $self->{template});
1073 $self->{template} = $layout;
1074 }
1075
1076 $self->set_text (delete $self->{text}) if exists $self->{text};
1077 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
919 1078
920 $self 1079 $self
921} 1080}
922 1081
923sub escape_text { 1082sub escape_text {
931} 1090}
932 1091
933sub set_text { 1092sub set_text {
934 my ($self, $text) = @_; 1093 my ($self, $text) = @_;
935 1094
936 $self->{text} = $text;
937 $self->{layout}->set_markup ($text); 1095 $self->{layout}->set_text ($text);
938 1096
939 delete $self->{texture}; 1097 delete $self->{texture};
940# $self->{w} = $self->{h} = -1; 1098 $self->check_size;
941 $self->update; 1099 $self->update;
942} 1100}
943 1101
944sub get_text { 1102sub set_markup {
945 my ($self, $text) = @_; 1103 my ($self, $markup) = @_;
946 1104
947 $self->{text} 1105 $self->{layout}->set_markup ($markup);
1106
1107 delete $self->{texture};
1108 $self->check_size;
1109 $self->update;
948} 1110}
949 1111
950sub size_request { 1112sub size_request {
951 my ($self) = @_; 1113 my ($self) = @_;
952 1114
953 $self->{layout}->set_width; 1115 $self->{layout}->set_width;
954 $self->{layout}->set_height ($self->{height}); 1116 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1117
955 my ($w, $h) = $self->{layout}->size; 1118 my ($w, $h) = $self->{layout}->size;
1119
1120 if (exists $self->{template}) {
1121 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1122
1123 my ($w2, $h2) = $self->{template}->size;
1124
1125 $w = List::Util::max $w, $w2;
1126 $h = List::Util::max $h, $h2;
1127 }
956 1128
957 ( 1129 (
958 $w + $self->{padding} * 2, 1130 $w + $self->{padding} * 2,
959 $h + $self->{padding} * 2, 1131 $h + $self->{padding} * 2,
960 ) 1132 )
961} 1133}
962 1134
963sub size_allocate { 1135sub size_allocate {
964 my ($self, $x, $y, $w, $h) = @_; 1136 my ($self, $w, $h) = @_;
965
966 $self->_size_allocate ($x, $y, $w, $h) or return;
967 1137
968 delete $self->{texture}; 1138 delete $self->{texture};
969} 1139}
970 1140
971sub update { 1141sub set_fontsize {
972 my ($self) = @_; 1142 my ($self, $fontsize) = @_;
973 1143
974 delete $self->{texture}; 1144 $self->{fontsize} = $fontsize;
975 $self->SUPER::update; 1145 $self->check_size;
976} 1146}
977 1147
978sub _draw { 1148sub _draw {
979 my ($self) = @_; 1149 my ($self) = @_;
980 1150
981 my $tex = $self->{texture} ||= do { 1151 my $tex = $self->{texture} ||= do {
982 $self->{layout}->set_width ($self->{w}); 1152 $self->{layout}->set_width ($self->{w});
983 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{height}); 1153 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
984 new_from_layout CFClient::Texture $self->{layout} 1154 new_from_layout CFClient::Texture $self->{layout}
985 }; 1155 };
986 1156
987 glEnable GL_BLEND; 1157 glEnable GL_BLEND;
988 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1158 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
989 glEnable GL_TEXTURE_2D; 1159 glEnable GL_TEXTURE_2D;
990 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1160 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
991 1161
992 glColor @{$self->{fg}}; 1162 glColor @{$self->{fg}};
993 1163
994 my $x = 1164 $self->{ox} = int (
995 $self->{align} < 0 ? $self->{padding} 1165 $self->{align} < 0 ? $self->{padding}
996 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1166 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
997 : ($self->{w} - $tex->{w}) * 0.5; 1167 : ($self->{w} - $tex->{w}) * 0.5
1168 );
998 1169
999 glTranslate int $x, int +($self->{h} - $tex->{h}) * 0.5, 0; 1170 $self->{oy} = int (
1000 $tex->draw_quad (0, 0); 1171 $self->{valign} < 0 ? $self->{padding}
1172 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1173 : ($self->{h} - $tex->{h}) * 0.5
1174 );
1175
1176 $tex->draw_quad ($self->{ox}, $self->{oy});
1001 1177
1002 glDisable GL_TEXTURE_2D; 1178 glDisable GL_TEXTURE_2D;
1003 glDisable GL_BLEND; 1179 glDisable GL_BLEND;
1004} 1180}
1005 1181
1006############################################################################# 1182#############################################################################
1007 1183
1008package CFClient::UI::Entry; 1184package CFClient::UI::EntryBase;
1009 1185
1010our @ISA = CFClient::UI::Label::; 1186our @ISA = CFClient::UI::Label::;
1011 1187
1012use SDL; 1188use CFClient::OpenGL;
1013use SDL::OpenGL;
1014 1189
1015sub new { 1190sub new {
1016 my $class = shift; 1191 my $class = shift;
1017 1192
1018 $class->SUPER::new ( 1193 $class->SUPER::new (
1020 bg => [0, 0, 0, 0.2], 1195 bg => [0, 0, 0, 0.2],
1021 active_bg => [1, 1, 1, 0.5], 1196 active_bg => [1, 1, 1, 0.5],
1022 active_fg => [0, 0, 0], 1197 active_fg => [0, 0, 0],
1023 can_hover => 1, 1198 can_hover => 1,
1024 can_focus => 1, 1199 can_focus => 1,
1200 valign => 0,
1025 @_ 1201 @_
1026 ) 1202 )
1027} 1203}
1028 1204
1029sub _set_text { 1205sub _set_text {
1030 my ($self, $text) = @_; 1206 my ($self, $text) = @_;
1031 1207
1032 my $old_text = $self->{text}; 1208 delete $self->{cur_h};
1209
1210 return if $self->{text} eq $text;
1211
1212 delete $self->{texture};
1033 1213
1034 $self->{last_activity} = $::NOW; 1214 $self->{last_activity} = $::NOW;
1035
1036 $self->{text} = $text; 1215 $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 1216
1040 $text =~ s/./*/g if $self->{hidden}; 1217 $text =~ s/./*/g if $self->{hidden};
1218 $self->{layout}->set_text ("$text ");
1041 1219
1042 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1220 $self->emit (changed => $self->{text});
1221}
1043 1222
1044 $text = substr $text, 0, $self->{cursor}; 1223sub get_text {
1045 utf8::encode $text; 1224 $_[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} 1225}
1052 1226
1053sub size_request { 1227sub size_request {
1054 my ($self) = @_; 1228 my ($self) = @_;
1055 1229
1057 1231
1058 ($w + 1, $h) # add 1 for cursor 1232 ($w + 1, $h) # add 1 for cursor
1059} 1233}
1060 1234
1061sub size_allocate { 1235sub size_allocate {
1062 my ($self, $x, $y, $w, $h) = @_; 1236 my ($self, $w, $h) = @_;
1063 1237
1064 $self->SUPER::size_allocate ($x, $y, $w, $h); 1238 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1065
1066 $self->_set_text ($self->{text});
1067} 1239}
1068 1240
1069sub set_text { 1241sub set_text {
1070 my ($self, $text) = @_; 1242 my ($self, $text) = @_;
1071 1243
1075} 1247}
1076 1248
1077sub key_down { 1249sub key_down {
1078 my ($self, $ev) = @_; 1250 my ($self, $ev) = @_;
1079 1251
1080 my $mod = $ev->key_mod; 1252 my $mod = $ev->{mod};
1081 my $sym = $ev->key_sym; 1253 my $sym = $ev->{sym};
1082
1083 my $uni = $ev->key_unicode; 1254 my $uni = $ev->{unicode};
1084 1255
1085 my $text = $self->get_text; 1256 my $text = $self->get_text;
1086 1257
1087 if ($sym == SDLK_BACKSPACE) { 1258 if ($sym == 8) {
1088 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1259 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1089 } elsif ($sym == SDLK_DELETE) { 1260 } elsif ($sym == 127) {
1090 substr $text, $self->{cursor}, 1, ""; 1261 substr $text, $self->{cursor}, 1, "";
1091 } elsif ($sym == SDLK_LEFT) { 1262 } elsif ($sym == CFClient::SDLK_LEFT) {
1092 --$self->{cursor} if $self->{cursor}; 1263 --$self->{cursor} if $self->{cursor};
1093 } elsif ($sym == SDLK_RIGHT) { 1264 } elsif ($sym == CFClient::SDLK_RIGHT) {
1094 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1265 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1095 } elsif ($sym == SDLK_HOME) { 1266 } elsif ($sym == CFClient::SDLK_HOME) {
1096 $self->{cursor} = 0; 1267 $self->{cursor} = 0;
1097 } elsif ($sym == SDLK_END) { 1268 } elsif ($sym == CFClient::SDLK_END) {
1098 $self->{cursor} = length $text; 1269 $self->{cursor} = length $text;
1099 } elsif ($sym == SDLK_ESCAPE) { 1270 } elsif ($sym == 27) {
1100 $self->emit ('escape'); 1271 $self->emit ('escape');
1101 } elsif ($uni) { 1272 } elsif ($uni) {
1102 substr $text, $self->{cursor}++, 0, chr $uni; 1273 substr $text, $self->{cursor}++, 0, chr $uni;
1103 } 1274 }
1104 1275
1159 1330
1160 $self->SUPER::_draw; 1331 $self->SUPER::_draw;
1161 1332
1162 #TODO: force update every cursor change :( 1333 #TODO: force update every cursor change :(
1163 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1334 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1335
1336 unless (exists $self->{cur_h}) {
1337 my $text = substr $self->{text}, 0, $self->{cursor};
1338 utf8::encode $text;
1339
1340 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1341 }
1342
1164 glColor @{$self->{fg}}; 1343 glColor @{$self->{fg}};
1165 glBegin GL_LINES; 1344 glBegin GL_LINES;
1166 glVertex $self->{cur_x}, $self->{cur_y};
1167 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1345 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1346 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1168 glEnd; 1347 glEnd;
1169 } 1348 }
1170} 1349}
1171 1350
1172package CFClient::UI::LineEntry; 1351package CFClient::UI::Entry;
1173 1352
1174our @ISA = CFClient::UI::Entry::; 1353our @ISA = CFClient::UI::EntryBase::;
1175 1354
1176use SDL; 1355use CFClient::OpenGL;
1177use SDL::OpenGL;
1178 1356
1179sub key_down { 1357sub key_down {
1180 my ($self, $ev) = @_; 1358 my ($self, $ev) = @_;
1181 1359
1182 my $sym = $ev->key_sym; 1360 my $sym = $ev->{sym};
1183 1361
1184 if ($sym == SDLK_RETURN) { 1362 if ($sym == 13) {
1185 $self->emit (activate => $self->get_text); 1363 $self->emit (activate => $self->get_text);
1186 $self->update; 1364 $self->update;
1187 1365
1188 } else { 1366 } else {
1189 $self->SUPER::key_down ($ev); 1367 $self->SUPER::key_down ($ev);
1195 1373
1196package CFClient::UI::Button; 1374package CFClient::UI::Button;
1197 1375
1198our @ISA = CFClient::UI::Label::; 1376our @ISA = CFClient::UI::Label::;
1199 1377
1200use SDL; 1378use CFClient::OpenGL;
1201use SDL::OpenGL;
1202 1379
1203my @tex = 1380my @tex =
1204 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1381 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1205 qw(b1_button_active.png); 1382 qw(b1_button_active.png);
1206 1383
1207sub new { 1384sub new {
1208 my $class = shift; 1385 my $class = shift;
1209 1386
1210 $class->SUPER::new ( 1387 $class->SUPER::new (
1211 padding => 4, 1388 padding => 4,
1212 fg => [1, 1, 1], 1389 fg => [1, 1, 1],
1213 bg => [1, 1, 1, 0.2], 1390 bg => [1, 1, 1, 0.2],
1214 active_fg => [1, 1, 0], 1391 active_fg => [0, 0, 1],
1215 can_hover => 1, 1392 can_hover => 1,
1393 align => 0,
1394 valign => 0,
1216 @_ 1395 @_
1217 ) 1396 )
1218} 1397}
1219 1398
1220sub button_up { 1399sub button_up {
1228 1407
1229sub _draw { 1408sub _draw {
1230 my ($self) = @_; 1409 my ($self) = @_;
1231 1410
1232 local $self->{fg} = $self->{fg}; 1411 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 1412
1239 if ($GRAB == $self) { 1413 if ($GRAB == $self) {
1240 $self->{fg} = $self->{active_fg}; 1414 $self->{fg} = $self->{active_fg};
1241 } 1415 }
1242 1416
1243 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1417 glEnable GL_BLEND;
1418 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1419 glEnable GL_TEXTURE_2D;
1244 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1420 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1421 glColor 0, 0, 0, 1;
1245 1422
1246 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1423 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1247 1424
1248 glDisable GL_TEXTURE_2D; 1425 glDisable GL_TEXTURE_2D;
1249 glDisable GL_BLEND; 1426 glDisable GL_BLEND;
1250 1427
1251 $self->SUPER::_draw; 1428 $self->SUPER::_draw;
1255 1432
1256package CFClient::UI::CheckBox; 1433package CFClient::UI::CheckBox;
1257 1434
1258our @ISA = CFClient::UI::DrawBG::; 1435our @ISA = CFClient::UI::DrawBG::;
1259 1436
1260use SDL; 1437my @tex =
1261use SDL::OpenGL; 1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1439 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1440
1441use CFClient::OpenGL;
1262 1442
1263sub new { 1443sub new {
1264 my $class = shift; 1444 my $class = shift;
1265 1445
1266 $class->SUPER::new ( 1446 $class->SUPER::new (
1277 my ($self) = @_; 1457 my ($self) = @_;
1278 1458
1279 ($self->{padding} * 2 + 6) x 2 1459 ($self->{padding} * 2 + 6) x 2
1280} 1460}
1281 1461
1282sub size_allocate {
1283 my ($self, $x, $y, $w, $h) = @_;
1284
1285 $self->_size_allocate ($x, $y, $w, $h);
1286}
1287
1288sub button_down { 1462sub button_down {
1289 my ($self, $ev, $x, $y) = @_; 1463 my ($self, $ev, $x, $y) = @_;
1290 1464
1291 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1465 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1292 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1466 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1301 $self->SUPER::_draw; 1475 $self->SUPER::_draw;
1302 1476
1303 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1477 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0;
1304 1478
1305 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1479 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2;
1306 1480
1307 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1481 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1308 1482
1309 glBegin GL_LINE_LOOP; 1483 glEnable GL_BLEND;
1310 glVertex 0 , 0; 1484 glEnable GL_TEXTURE_2D;
1311 glVertex 0 , $s; 1485 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1312 glVertex $s, $s; 1486
1313 glVertex $s, 0; 1487 my $tex = $self->{state} ? $tex[1] : $tex[0];
1488
1489 $tex->draw_quad (0, 0, $s, $s);
1490
1491 glDisable GL_TEXTURE_2D;
1492 glDisable GL_BLEND;
1493}
1494
1495#############################################################################
1496
1497package CFClient::UI::Image;
1498
1499our @ISA = CFClient::UI::Base::;
1500
1501use CFClient::OpenGL;
1502use Carp qw/confess/;
1503
1504our %loaded_images;
1505
1506sub new {
1507 my $class = shift;
1508
1509 my $self = $class->SUPER::new (@_);
1510
1511 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1512
1513 $loaded_images{$self->{image}} ||=
1514 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1515
1516 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1517
1518 Scalar::Util::weaken $loaded_images{$self->{image}};
1519
1520 $self->{aspect} = $tex->{w} / $tex->{h};
1521
1522 $self
1523}
1524
1525sub size_request {
1526 my ($self) = @_;
1527
1528 ($self->{tex}->{w}, $self->{tex}->{h})
1529}
1530
1531sub _draw {
1532 my ($self) = @_;
1533
1534 my $tex = $self->{tex};
1535
1536 my ($w, $h) = ($self->{w}, $self->{h});
1537
1538 if ($self->{rot90}) {
1539 glRotate 90, 0, 0, 1;
1540 glTranslate 0, -$self->{w}, 0;
1541
1542 ($w, $h) = ($h, $w);
1543 }
1544
1545 glEnable GL_BLEND;
1546 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1547 glEnable GL_TEXTURE_2D;
1548 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1549
1550 $tex->draw_quad (0, 0, $w, $h);
1551
1552 glDisable GL_BLEND;
1553 glDisable GL_TEXTURE_2D;
1554}
1555
1556#############################################################################
1557
1558package CFClient::UI::VGauge;
1559
1560our @ISA = CFClient::UI::Base::;
1561
1562use CFClient::OpenGL;
1563
1564my %tex = (
1565 food => [
1566 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1567 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1568 ],
1569 grace => [
1570 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1571 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1572 ],
1573 hp => [
1574 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1575 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1576 ],
1577 mana => [
1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1579 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1580 ],
1581);
1582
1583# eg. VGauge->new (gauge => 'food'), default gauge: food
1584sub new {
1585 my $class = shift;
1586
1587 my $self = $class->SUPER::new (
1588 type => 'food',
1589 @_
1590 );
1591
1592 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1593
1594 $self
1595}
1596
1597sub size_request {
1598 my ($self) = @_;
1599
1600 #my $tex = $tex{$self->{type}}[0];
1601 #@$tex{qw(w h)}
1602 (0, 0)
1603}
1604
1605sub set_max {
1606 my ($self, $max) = @_;
1607
1608 $self->{max_val} = $max;
1609}
1610
1611sub set_value {
1612 my ($self, $val, $max) = @_;
1613
1614 $self->set_max ($max)
1615 if defined $max;
1616
1617 $max = $self->{max_val};
1618 $self->{val} = $val;
1619
1620 $self->update;
1621}
1622
1623sub _draw {
1624 my ($self) = @_;
1625
1626 my $tex = $tex{$self->{type}};
1627
1628 my ($w, $h) = ($self->{w}, $self->{h});
1629
1630 if ($self->{vertical}) {
1631 glRotate 90, 0, 0, 1;
1632 glTranslate 0, -$self->{w}, 0;
1633
1634 ($w, $h) = ($h, $w);
1635 }
1636
1637 my $ycut = $self->{val} / ($self->{max_val} || 1);
1638 $ycut = 1 if $self->{val} > $self->{max_val};
1639
1640 my $t1 = $tex->[0];
1641 my $t2 = $tex->[1];
1642
1643 glEnable GL_BLEND;
1644 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1645 glEnable GL_TEXTURE_2D;
1646 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1647
1648 my $h1 = $self->{h} - $ycut * $self->{h};
1649 my $h2 = $ycut * $self->{h};
1650
1651 glBindTexture GL_TEXTURE_2D, $t1->{name};
1652 glBegin GL_QUADS;
1653 glTexCoord 0 , 0; glVertex 0 , 0;
1654 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1655 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1656 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1314 glEnd; 1657 glEnd;
1315 1658
1316 if ($self->{state}) { 1659 glBindTexture GL_TEXTURE_2D, $t2->{name};
1317 glBegin GL_LINES; 1660 glBegin GL_QUADS;
1318 glVertex 0 , 0; 1661 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1319 glVertex $s, $s; 1662 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1320 glVertex $s, 0; 1663 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1321 glVertex 0 , $s; 1664 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1322 glEnd; 1665 glEnd;
1323 } 1666
1667 glDisable GL_BLEND;
1668 glDisable GL_TEXTURE_2D;
1669}
1670
1671#############################################################################
1672
1673package CFClient::UI::Gauge;
1674
1675our @ISA = CFClient::UI::VBox::;
1676
1677sub new {
1678 my ($class, %arg) = shift;
1679
1680 my $self = $class->SUPER::new (
1681 @_,
1682 );
1683
1684 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1685 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1686 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1687
1688 $self
1689}
1690
1691sub set_fontsize {
1692 my ($self, $fsize) = @_;
1693
1694 $self->{value}->set_fontsize ($fsize);
1695 $self->{max} ->set_fontsize ($fsize);
1696}
1697
1698sub set_value {
1699 my ($self, $val, $max) = @_;
1700
1701 $self->set_max ($max)
1702 if defined $max;
1703
1704 $self->{gauge}->set_value ($val, $max);
1705 $self->{value}->set_text ($val);
1706}
1707
1708sub set_max {
1709 my ($self, $max) = @_;
1710
1711 $self->{gauge}->set_max ($max);
1712 $self->{max}->set_text ($max);
1324} 1713}
1325 1714
1326############################################################################# 1715#############################################################################
1327 1716
1328package CFClient::UI::Slider; 1717package CFClient::UI::Slider;
1329 1718
1330use strict; 1719use strict;
1331 1720
1332use SDL::OpenGL; 1721use CFClient::OpenGL;
1333 1722
1334our @ISA = CFClient::UI::DrawBG::; 1723our @ISA = CFClient::UI::DrawBG::;
1335 1724
1336my @tex = 1725my @tex =
1337 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1726 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1347 # TODO: calculations are off 1736 # TODO: calculations are off
1348 my $self = $class->SUPER::new ( 1737 my $self = $class->SUPER::new (
1349 fg => [1, 1, 1], 1738 fg => [1, 1, 1],
1350 active_fg => [0, 0, 0], 1739 active_fg => [0, 0, 0],
1351 range => [0, 0, 100, 10], 1740 range => [0, 0, 100, 10],
1352 req_w => 40, 1741 req_w => 20,
1353 req_h => 13, 1742 req_h => 20,
1354 vertical => 0, 1743 vertical => 0,
1355 can_hover => 1, 1744 can_hover => 1,
1745 inner_pad => 5,
1356 @_ 1746 @_
1357 ); 1747 );
1358 1748
1359 $self 1749 $self
1360} 1750}
1381 if ($GRAB == $self) { 1771 if ($GRAB == $self) {
1382 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1772 my ($value, $lo, $hi, $page) = @{$self->{range}};
1383 1773
1384 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w}); 1774 my ($x, $w) = $self->{vertical} ? ($y, $self->{h}) : ($x, $self->{w});
1385 1775
1776 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1777 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1778
1779 $x -= $inner_pad_px; # substract the padding
1386 $x = $x * ($hi - $lo) / $w + $lo; 1780 $x = $x * ($hi - $lo) / $inner_w + $lo;
1387 $x = $lo if $x < $lo; 1781 $x = $lo if $x < $lo;
1388 $x = $hi - $page if $x > $hi - $page; 1782 $x = $hi - $page if $x > $hi - $page;
1389 $self->{range}[0] = $x; 1783 $self->{range}[0] = $x;
1390 1784
1391 $self->emit (changed => $x); 1785 $self->emit (changed => $x);
1392 $self->update; 1786 $self->update;
1393 } 1787 }
1788}
1789
1790# the inner_* stuff is for generating a padding for the slider handle,
1791# so that the handle doesn't leave the texture. This calculation isn't 100%
1792# correct propably, but it does the job for now
1793sub _calc_inner_pad_px {
1794 my ($self, $w) = @_;
1795 ($w / 100) * $self->{inner_pad} # % to pixels
1394} 1796}
1395 1797
1396sub _draw { 1798sub _draw {
1397 my ($self) = @_; 1799 my ($self) = @_;
1398 1800
1412 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg}; 1814 my $fg = $FOCUS == $self ? $self->{active_fg} : $self->{fg};
1413 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg}; 1815 my $bg = $FOCUS == $self ? $self->{active_bg} : $self->{bg};
1414 1816
1415 my ($value, $lo, $hi, $page) = @{$self->{range}}; 1817 my ($value, $lo, $hi, $page) = @{$self->{range}};
1416 1818
1417 # the inner_* stuff is for generating a padding for the slider handle, 1819 $hi = $value + 1 if $lo == $hi;
1418 # so that the handle doesn't leave the texture. This calculation isn't 100% 1820
1419 # correct propably, but it does the job for now 1821 my $inner_pad_px = $self->_calc_inner_pad_px ($w);
1420 my $inner_pad = 5; # 5% of width for slider bg texture border
1421 my $inner_pad_px = ($w / 100) * $inner_pad; # % to pixels
1422 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right 1822 my $inner_w = $w - $inner_pad_px * 2; # * 2 for left & right
1423 1823
1424 $page = int $page * $inner_w / ($hi - $lo); 1824 $page = int $page * $inner_w / ($hi - $lo);
1425 $value = int +($value - $lo) * $inner_w / ($hi - $lo); 1825 $value = int +($value - $lo) * $inner_w / ($hi - $lo);
1426 1826
1427 $w -= $page; 1827 $w -= $page;
1451 1851
1452package CFClient::UI::TextView; 1852package CFClient::UI::TextView;
1453 1853
1454our @ISA = CFClient::UI::HBox::; 1854our @ISA = CFClient::UI::HBox::;
1455 1855
1456use SDL::OpenGL; 1856use CFClient::OpenGL;
1457 1857
1458sub new { 1858sub new {
1459 my $class = shift; 1859 my $class = shift;
1460 1860
1461 my $self = $class->SUPER::new ( 1861 my $self = $class->SUPER::new (
1862 fontsize => 1,
1863 @_,
1864
1865 layout => (new CFClient::Layout),
1462 par => [], 1866 par => [],
1463 @_, 1867 height => 0,
1464 children => [ 1868 children => [
1465 (new CFClient::UI::Empty expand => 1), 1869 (new CFClient::UI::Empty expand => 1),
1466 (new CFClient::UI::Slider vertical => 1), 1870 (new CFClient::UI::Slider vertical => 1),
1467 ], 1871 ],
1468 ); 1872 );
1469 1873
1874 $self->{children}[1]->connect (changed => sub {
1875 $self->update;
1876 });
1877
1470 $self 1878 $self
1879}
1880
1881sub set_fontsize {
1882 my ($self, $fontsize) = @_;
1883
1884 $self->{fontsize} = $fontsize;
1885 $self->reflow;
1886}
1887
1888sub text_height {
1889 my ($self, $text) = @_;
1890
1891 my $layout = $self->{layout};
1892
1893 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1894 $layout->set_width ($self->{w});
1895 $layout->set_text ($text);
1896
1897 ($layout->size)[1]
1898}
1899
1900sub reflow {
1901 my ($self) = @_;
1902
1903 $self->{need_reflow}++;
1904 $self->update;
1905}
1906
1907sub size_allocate {
1908 my ($self, $w, $h) = @_;
1909
1910 $self->SUPER::size_allocate ($w, $h);
1911
1912 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1913 $self->{layout}->set_width ($self->{children}[0]{w});
1914
1915 $self->reflow;
1471} 1916}
1472 1917
1473sub add_paragraph { 1918sub add_paragraph {
1474 my ($self, $color, $text) = @_; 1919 my ($self, $color, $text) = @_;
1475 1920
1921 #TODO: intelligently "reformat" paragraph
1922
1923 my $height = $self->text_height ($text);
1924
1925 $self->{height} += $height;
1926
1476 push @{$self->{par}}, [$color, $text]; 1927 push @{$self->{par}}, [$height, $color, $text];
1477 my $count = scalar @{$self->{par}}; 1928
1478 $self->{children}[1]{range} = [$count - 1, 0, $count, 1]; 1929 $self->{children}[1]{range} = [$self->{height} - $self->{h}, 0, $self->{height}, $self->{h}];
1479 $self->{children}[1]->update; 1930 $self->{children}[1]->update;
1480} 1931}
1481 1932
1933sub update {
1934 my ($self) = @_;
1935
1936 $self->SUPER::update;
1937
1938 return unless $self->{h} > 0;
1939
1940 delete $self->{texture};
1941
1942 $ROOT->on_refresh ($self, sub {
1943 if (delete $self->{need_reflow}) {
1944 my $height = 0;
1945
1946 $height += $_->[0] = $self->text_height ($_->[2])
1947 for @{$self->{par}};
1948
1949 $self->{height} = $height;
1950
1951 $self->{children}[1]{range} = [$height - $self->{h}, 0, $height, $self->{h}];
1952
1953 delete $self->{texture};
1954 }
1955
1956 $self->{texture} ||= new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
1957 glClearColor 0, 0, 0, 1;
1958 glClear GL_COLOR_BUFFER_BIT;
1959
1960 glEnable GL_BLEND;
1961 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1962 glEnable GL_TEXTURE_2D;
1963 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1964
1965 my $top = int $self->{children}[1]{range}[0];
1966
1967 my $y0 = $top;
1968 my $y1 = $top + $self->{h};
1969
1970 my $y = 0;
1971
1972 my $layout = $self->{layout};
1973
1974 for my $par (@{$self->{par}}) {
1975 my $h = $par->[0];
1976
1977 if ($y0 < $y + $h && $y < $y1) {
1978 $layout->set_text ($par->[2]);
1979
1980 glColor @{ $par->[1] };
1981 my ($W, $H) = $layout->size;
1982 CFClient::Texture->new_from_layout ($layout)->draw_quad (0, $y - $y0);
1983 }
1984
1985 $y += $h;
1986 }
1987
1988 glDisable GL_TEXTURE_2D;
1989 glDisable GL_BLEND;
1990 };
1991 });
1992}
1993
1482sub _draw { 1994sub _draw {
1483 my ($self) = @_; 1995 my ($self) = @_;
1484 1996
1485 my $par = $self->{par}; 1997 if ($self->{texture}) {
1486
1487 my $bottom = (scalar @$par) - 1;
1488
1489 my $w = $self->{children}[0]{w};
1490 my $y = $self->{h};
1491
1492 glEnable GL_BLEND;
1493 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1494 glEnable GL_TEXTURE_2D; 1998 glEnable GL_TEXTURE_2D;
1495 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1999 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1496 2000 $self->{texture}->draw_quad (0, 0, $self->{w}, $self->{h});
1497 # TODO: everything!
1498 while ($y > 0 && $bottom >= 0) {
1499 my $par = $par->[$bottom--];
1500
1501 my $layout = new CFClient::Layout;
1502
1503 $layout->set_height ($::FONTSIZE);
1504 $layout->set_width ($w);
1505 $layout->set_text ($par->[1]);
1506 my $tex = new_from_layout CFClient::Texture $layout;
1507 $y -= $tex->{h};
1508
1509 glColor @{ $par->[0] };
1510 $tex->draw_quad (0, $y);
1511 }
1512
1513 glDisable GL_TEXTURE_2D; 2001 glDisable GL_TEXTURE_2D;
1514 glDisable GL_BLEND;
1515}
1516
1517#############################################################################
1518
1519package CFClient::UI::MapWidget;
1520
1521use strict;
1522
1523use List::Util qw(min max);
1524
1525use SDL;
1526use SDL::OpenGL;
1527
1528our @ISA = CFClient::UI::Base::;
1529
1530sub new {
1531 my $class = shift;
1532
1533 $class->SUPER::new (
1534 z => -1,
1535 can_focus => 1,
1536 list => (glGenLists 1),
1537 @_
1538 ) 2002 }
1539}
1540 2003
1541sub key_down { 2004 $self->{children}[1]->draw;
1542 print "MAPKEYDOWN\n";
1543}
1544 2005
1545sub key_up {
1546}
1547
1548sub size_request {
1549 (
1550 1 + 32 * int $::WIDTH / 32,
1551 1 + 32 * int $::HEIGHT / 32,
1552 )
1553}
1554
1555sub update {
1556 my ($self) = @_;
1557
1558 $self->{need_update} = 1;
1559 $self->SUPER::update;
1560}
1561
1562sub draw {
1563 my ($self) = @_;
1564
1565 $self->{need_update}++;#d#
1566 if (delete $self->{need_update}) {
1567 glNewList $self->{list}, GL_COMPILE;
1568
1569 my $sw = int $::WIDTH / 32;
1570 my $sh = int $::HEIGHT / 32;
1571
1572 if ($::MAP) {
1573 my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh);
1574
1575 if ($::CFG->{fow_enable}) {
1576 if ($::CFG->{fow_smooth}) { # smooth fog of war
1577 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1578 glConvolutionFilter2D
1579 GL_CONVOLUTION_2D,
1580 GL_ALPHA,
1581 3, 3,
1582 GL_ALPHA, GL_FLOAT,
1583 pack "f*",
1584 0.1, 0.1, 0.1,
1585 0.1, 0.2, 0.1,
1586 0.1, 0.1, 0.1,
1587 ;
1588 glEnable GL_CONVOLUTION_2D;
1589 }
1590
1591 my $tex = new CFClient::Texture
1592 w => $w,
1593 h => $h,
1594 data => $data,
1595 internalformat => GL_ALPHA,
1596 format => GL_ALPHA;
1597
1598 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1599
1600 glEnable GL_BLEND;
1601 glEnable GL_TEXTURE_2D;
1602 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1603
1604 glColor +($::CFG->{fow_intensity}) x 3, 1;
1605 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1606
1607 glDisable GL_TEXTURE_2D;
1608 glDisable GL_BLEND;
1609 }
1610 }
1611
1612 glEndList;
1613 }
1614
1615 glCallList $self->{list};
1616
1617 if ($FOCUS != $self) {
1618 glColor 64/255, 64/255, 64/255;
1619 glLogicOp GL_AND;
1620 glEnable GL_COLOR_LOGIC_OP;
1621 glBegin GL_QUADS;
1622 glVertex 0, 0;
1623 glVertex 0, $::HEIGHT;
1624 glVertex $::WIDTH, $::HEIGHT;
1625 glVertex $::WIDTH, 0;
1626 glEnd;
1627 glDisable GL_COLOR_LOGIC_OP;
1628 }
1629}
1630
1631my %DIR = (
1632 SDLK_KP8, [1, "north"],
1633 SDLK_KP9, [2, "northeast"],
1634 SDLK_KP6, [3, "east"],
1635 SDLK_KP3, [4, "southeast"],
1636 SDLK_KP2, [5, "south"],
1637 SDLK_KP1, [6, "southwest"],
1638 SDLK_KP4, [7, "west"],
1639 SDLK_KP7, [8, "northwest"],
1640
1641 SDLK_UP, [1, "north"],
1642 SDLK_RIGHT, [3, "east"],
1643 SDLK_DOWN, [5, "south"],
1644 SDLK_LEFT, [7, "west"],
1645);
1646
1647sub key_down {
1648 my ($self, $ev) = @_;
1649
1650 my $mod = $ev->key_mod;
1651 my $sym = $ev->key_sym;
1652
1653 if ($sym == SDLK_KP5) {
1654 $::CONN->user_send ("command stay fire");
1655 } elsif ($sym == SDLK_a) {
1656 $::CONN->user_send ("command apply");
1657 } elsif ($sym == SDLK_QUOTE) {
1658 $self->emit ('activate_console');
1659 } elsif (exists $DIR{$sym}) {
1660 if ($mod & KMOD_SHIFT) {
1661 $self->{shft}++;
1662 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1663 } elsif ($mod & KMOD_CTRL) {
1664 $self->{ctrl}++;
1665 $::CONN->user_send ("command run $DIR{$sym}[0]");
1666 } else {
1667 $::CONN->user_send ("command $DIR{$sym}[1]");
1668 }
1669 }
1670}
1671
1672sub key_up {
1673 my ($self, $ev) = @_;
1674
1675 my $mod = $ev->key_mod;
1676 my $sym = $ev->key_sym;
1677
1678 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1679 $::CONN->user_send ("command fire_stop");
1680 }
1681 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1682 $::CONN->user_send ("command run_stop");
1683 }
1684} 2006}
1685 2007
1686############################################################################# 2008#############################################################################
1687 2009
1688package CFClient::UI::Animator; 2010package CFClient::UI::Animator;
1689 2011
1690use SDL::OpenGL; 2012use CFClient::OpenGL;
1691 2013
1692our @ISA = CFClient::UI::Bin::; 2014our @ISA = CFClient::UI::Bin::;
1693 2015
1694sub moveto { 2016sub moveto {
1695 my ($self, $x, $y) = @_; 2017 my ($self, $x, $y) = @_;
1751sub toggle_flopper { 2073sub toggle_flopper {
1752 my ($self) = @_; 2074 my ($self) = @_;
1753 2075
1754 # TODO: use animation 2076 # TODO: use animation
1755 if ($self->{state} = !$self->{state}) { 2077 if ($self->{state} = !$self->{state}) {
1756 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2078 $CFClient::UI::ROOT->add ($self->{other});
1757 $self->{other}->move ( 2079 $self->{other}->move ($self->coord2global (0, $self->{h}));
1758 ($::WIDTH - $self->{other}{w}) * 0.5, 2080 $self->emit ("open");
1759 ($::HEIGHT - $self->{other}{h}) * 0.5,
1760 );
1761 } else { 2081 } else {
1762 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2082 $CFClient::UI::ROOT->remove ($self->{other});
2083 $self->emit ("close");
1763 } 2084 }
1764}
1765 2085
1766############################################################################# 2086 $self->emit (changed => $self->{state});
2087}
1767 2088
2089#############################################################################
2090
1768package CFClient::UI::Toplevel; 2091package CFClient::UI::Root;
1769 2092
1770our @ISA = CFClient::UI::Container::; 2093our @ISA = CFClient::UI::Container::;
2094
2095use CFClient::OpenGL;
2096
2097sub check_size {
2098 my ($self) = @_;
2099
2100 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2101}
1771 2102
1772sub size_request { 2103sub size_request {
1773 ($::WIDTH, $::HEIGHT) 2104 ($::WIDTH, $::HEIGHT)
1774} 2105}
1775 2106
1776sub size_allocate { 2107sub configure {
1777 my ($self, $x, $y, $w, $h) = @_; 2108 my ($self, $x, $y, $w, $h) = @_;
1778 2109
1779 $self->_size_allocate ($x, $y, $w, $h); 2110 $self->SUPER::configure ($x, $y, $w, $h);
1780 2111
1781 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1782 for @{$self->{children}}; 2112 for my $child (@{$self->{children}}) {
1783} 2113 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1784 2114
1785sub translate { 2115 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2116 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2117 $child->configure ($X, $Y, $W,$H);
2118 }
2119}
2120
2121sub _topleft {
1786 my ($self, $x, $y) = @_; 2122 my ($self, $x, $y) = @_;
1787 2123
1788 ($x, $y) 2124 ($x, $y)
1789} 2125}
1790 2126
1791sub update { 2127sub update {
1792 my ($self) = @_; 2128 my ($self) = @_;
1793 2129
1794 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2130 $self->check_size;
1795 ::refresh (); 2131 ::refresh ();
1796} 2132}
1797 2133
1798sub add { 2134sub add {
1799 my ($self, $widget) = @_; 2135 my ($self, $child) = @_;
1800 2136
2137 # integerize window positions
2138 $child->{x} = int $child->{x};
2139 $child->{y} = int $child->{y};
2140
1801 $self->SUPER::add ($widget); 2141 $self->SUPER::add ($child);
2142}
1802 2143
1803 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request); 2144sub on_refresh {
2145 my ($self, $id, $cb) = @_;
2146
2147 $self->{refresh_hook}{$id} = $cb;
1804} 2148}
1805 2149
1806sub draw { 2150sub draw {
1807 my ($self) = @_; 2151 my ($self) = @_;
1808 2152
2153 while (my $rcb = delete $self->{refresh_hook}) {
2154 $_->() for values %$rcb;
2155 }
2156
2157 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2158 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2159 glClear GL_COLOR_BUFFER_BIT;
2160
2161 glMatrixMode GL_PROJECTION;
2162 glLoadIdentity;
2163 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2164 glMatrixMode GL_MODELVIEW;
2165 glLoadIdentity;
2166
1809 $self->_draw; 2167 $self->_draw;
1810} 2168}
1811 2169
1812############################################################################# 2170#############################################################################
1813 2171
1814package CFClient::UI; 2172package CFClient::UI;
1815 2173
1816$TOPLEVEL = new CFClient::UI::Toplevel; 2174$ROOT = new CFClient::UI::Root;
1817 2175
18181 21761
1819 2177

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines