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.107 by root, Fri Apr 14 20:27:35 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 && $hover->{can_hover}; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_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);
447 $self->child->draw; 525 $self->child->draw;
448 }; 526 };
449} 527}
450 528
451sub size_allocate { 529sub size_allocate {
452 my ($self, $x, $y, $w, $h) = @_; 530 my ($self, $w, $h) = @_;
453 531
454 $self->_size_allocate ($x, $y, $w, $h) or return;
455
456 $self->child->size_allocate (0, 0, $w, $h); 532 $self->child->configure (0, 0, $w, $h);
457 533
458 $self->render_chld; 534 $self->render_chld;
459} 535}
460 536
461sub _draw { 537sub _draw {
477 glDisable GL_TEXTURE_2D; 553 glDisable GL_TEXTURE_2D;
478} 554}
479 555
480############################################################################# 556#############################################################################
481 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
482package CFClient::UI::Frame; 582package CFClient::UI::Frame;
483 583
484our @ISA = CFClient::UI::Bin::; 584our @ISA = CFClient::UI::Bin::;
485 585
486use SDL::OpenGL; 586use CFClient::OpenGL;
487
488sub size_request {
489 my ($self) = @_;
490 my $chld = $self->child
491 or return (0, 0);
492
493 $chld->move (2, 2);
494
495 map { $_ + 4 } $chld->size_request;
496}
497
498sub size_allocate {
499 my ($self, $x, $y, $w, $h) = @_;
500
501 $self->_size_allocate ($x, $y, $w, $h) or return;
502
503 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
504}
505
506sub _draw {
507 my ($self) = @_;
508
509 my $chld = $self->child;
510
511 my ($w, $h) = $chld->size_request;
512
513 glBegin GL_QUADS;
514 glColor 0, 0, 0;
515 glVertex 0 , 0;
516 glVertex 0 , $h + 4;
517 glVertex $w + 4 , $h + 4;
518 glVertex $w + 4 , 0;
519 glEnd;
520
521 $chld->draw;
522}
523
524#############################################################################
525
526package CFClient::UI::FancyFrame;
527
528our @ISA = CFClient::UI::Bin::;
529
530use SDL::OpenGL;
531
532my @tex =
533 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
534 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
535 587
536sub new { 588sub new {
537 my $class = shift; 589 my $class = shift;
538 590
539 # TODO: user_x, user_y, overwrite moveto?
540
541 $class->SUPER::new ( 591 my $self = $class->SUPER::new (
542 bg => [1, 1, 1, 1], 592 bg => [1, 1, 1, 1],
543 border_bg => [1, 1, 1, 1], 593 border_bg => [1, 1, 1, 1],
544 border => $::FONTSIZE * 0.8, 594 border => 0.8,
545 @_ 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,
546 ) 691 )
547} 692}
548 693
549sub size_request {
550 my ($self) = @_;
551
552 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
553
554 my ($w, $h) = $self->SUPER::size_request;
555
556 (
557 $w + $self->{border} * 2,
558 $h + $self->{border} * 2,
559 )
560}
561
562sub size_allocate { 694sub size_allocate {
563 my ($self, $x, $y, $w, $h) = @_; 695 my ($self, $w, $h) = @_;
564 696
565 $self->_size_allocate ($x, $y, $w, $h) or return;
566
567 $h -= List::Util::max 0, $self->{border} * 2; 697 $h -= List::Util::max 0, $self->border * 2;
568 $w -= List::Util::max 0, $self->{border} * 2; 698 $w -= List::Util::max 0, $self->border * 2;
569 699
700 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
701 if $self->{title};
702
570 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 703 $self->child->configure ($self->border, $self->border, $w, $h);
571} 704}
572 705
573sub button_down { 706sub button_down {
574 my ($self, $ev, $x, $y) = @_; 707 my ($self, $ev, $x, $y) = @_;
575 708
709 my $border = $self->border;
710
576 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 711 if ($x < $self->{w} && $x >= $self->{w} - $border
577 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 712 && $y < $self->{h} && $y >= $self->{h} - $border) {
578 713
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 714 my ($ox, $oy) = ($ev->{x}, $ev->{y});
580 my ($bw, $bh) = ($self->{w}, $self->{h}); 715 my ($bw, $bh) = ($self->{w}, $self->{h});
581 716
582 $self->{motion} = sub { 717 $self->{motion} = sub {
583 my ($ev, $x, $y) = @_; 718 my ($ev, $x, $y) = @_;
584 719
585 ($x, $y) = ($ev->motion_x, $ev->motion_y); 720 ($x, $y) = ($ev->{x}, $ev->{y});
586 721
587 $self->{user_w} = $bw + $x - $ox; 722 $self->{user_w} = $bw + $x - $ox;
588 $self->{user_h} = $bh + $y - $oy; 723 $self->{user_h} = $bh + $y - $oy;
589 $self->update; 724 $self->check_size;
590 }; 725 };
591 726
592 } elsif ($x >= 0 && $x < $self->{w} 727 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $self->{border}) { 728 && $y >= 0 && $y < $border) {
594 729
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 730 my ($ox, $oy) = ($ev->{x}, $ev->{y});
596 my ($bx, $by) = ($self->{x}, $self->{y}); 731 my ($bx, $by) = ($self->{x}, $self->{y});
597 732
598 $self->{motion} = sub { 733 $self->{motion} = sub {
599 my ($ev, $x, $y) = @_; 734 my ($ev, $x, $y) = @_;
600 735
601 ($x, $y) = ($ev->motion_x, $ev->motion_y); 736 ($x, $y) = ($ev->{x}, $ev->{y});
602 737
603 $self->move ($bx + $x - $ox, $by + $y - $oy); 738 $self->move ($bx + $x - $ox, $by + $y - $oy);
604 $self->update; 739 $self->update;
605 }; 740 };
606 } 741 }
627 glEnable GL_BLEND; 762 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 763 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
629 glEnable GL_TEXTURE_2D; 764 glEnable GL_TEXTURE_2D;
630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 765 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
631 766
767 my $border = $self->border;
768
632 glColor @{ $self->{border_bg} }; 769 glColor @{ $self->{border_bg} };
633 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 770 $tex[1]->draw_quad (0, 0, $w, $border);
634 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 771 $tex[3]->draw_quad (0, $border, $border, $ch);
635 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 772 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
636 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 773 $tex[4]->draw_quad (0, $h - $border, $w, $border);
637 774
638 my $bg = $tex[0]; 775 my $bg = $tex[0];
639 776
640 # TODO: repeat texture not scale 777 # TODO: repeat texture not scale
641 my $rep_x = $cw / $bg->{w}; 778 my $rep_x = $cw / $bg->{w};
644 glColor @{ $self->{bg} }; 781 glColor @{ $self->{bg} };
645 782
646 $bg->{s} = $rep_x; 783 $bg->{s} = $rep_x;
647 $bg->{t} = $rep_y; 784 $bg->{t} = $rep_y;
648 $bg->{wrap_mode} = 1; 785 $bg->{wrap_mode} = 1;
649 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 786 $bg->draw_quad ($border, $border, $cw, $ch);
650 787
651 glDisable GL_TEXTURE_2D; 788 glDisable GL_TEXTURE_2D;
652 glDisable GL_BLEND; 789 glDisable GL_BLEND;
653 790
791 $self->{title}->draw if $self->{title};
654 $self->child->draw; 792 $self->child->draw;
655} 793}
656 794
657############################################################################# 795#############################################################################
658 796
660 798
661our @ISA = CFClient::UI::Base::; 799our @ISA = CFClient::UI::Base::;
662 800
663use List::Util qw(max sum); 801use List::Util qw(max sum);
664 802
665use SDL::OpenGL; 803use CFClient::OpenGL;
666 804
667sub new { 805sub new {
668 my $class = shift; 806 my $class = shift;
669 807
670 $class->SUPER::new ( 808 $class->SUPER::new (
672 @_ 810 @_
673 ) 811 )
674} 812}
675 813
676sub add { 814sub add {
677 my ($self, $x, $y, $chld) = @_; 815 my ($self, $x, $y, $child) = @_;
678 816
817 $child->set_parent ($self);
679 $self->{children}[$y][$x] = $chld; 818 $self->{children}[$y][$x] = $child;
680 $chld->set_parent ($self);
681 819
682 $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};
683 $self->update; 828 $self->update;
684} 829}
685 830
686sub get_wh { 831sub get_wh {
687 my ($self) = @_; 832 my ($self) = @_;
693 or next; 838 or next;
694 839
695 for my $x (0 .. $#$row) { 840 for my $x (0 .. $#$row) {
696 my $widget = $row->[$x] 841 my $widget = $row->[$x]
697 or next; 842 or next;
698 my ($w, $h) = $widget->size_request; 843 my ($w, $h) = @$widget{qw(req_w req_h)};
699 844
700 $w[$x] = max $w[$x], $w; 845 $w[$x] = max $w[$x], $w;
701 $h[$y] = max $h[$y], $h; 846 $h[$y] = max $h[$y], $h;
702 } 847 }
703 } 848 }
715 (sum @$hs), 860 (sum @$hs),
716 ) 861 )
717} 862}
718 863
719sub size_allocate { 864sub size_allocate {
720 my ($self, $x, $y, $w, $h) = @_; 865 my ($self, $w, $h) = @_;
721
722 $self->_size_allocate ($x, $y, $w, $h) or return;
723 866
724 my ($ws, $hs) = $self->get_wh; 867 my ($ws, $hs) = $self->get_wh;
725 868
726 my $req_w = sum @$ws; 869 my $req_w = sum @$ws;
727 my $req_h = sum @$hs; 870 my $req_h = sum @$hs;
733 876
734 # linearly scale sizes 877 # linearly scale sizes
735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 878 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
736 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 879 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
737 880
738 $_ = int $_ for @$ws; #TODO: avoid rounding problems 881 CFClient::UI::harmonize $ws;
739 $_ = int $_ for @$hs; #TODO: avoid rounding problems 882 CFClient::UI::harmonize $hs;
740 883
741 my $y; 884 my $y;
742 885
743 for my $r (0 .. $#{$self->{children}}) { 886 for my $r (0 .. $#{$self->{children}}) {
744 my $row = $self->{children}[$r] 887 my $row = $self->{children}[$r]
749 892
750 for my $c (0 .. $#$row) { 893 for my $c (0 .. $#$row) {
751 my $col_w = $ws->[$c]; 894 my $col_w = $ws->[$c];
752 895
753 if (my $widget = $row->[$c]) { 896 if (my $widget = $row->[$c]) {
754 $widget->size_allocate ($x, $y, $col_w, $row_h); 897 $widget->configure ($x, $y, $col_w, $row_h);
755 } 898 }
756 899
757 $x += $col_w; 900 $x += $col_w;
758 } 901 }
759 902
804 (List::Util::max map $_->[1], @alloc), 947 (List::Util::max map $_->[1], @alloc),
805 ) 948 )
806} 949}
807 950
808sub size_allocate { 951sub size_allocate {
809 my ($self, $x, $y, $w, $h) = @_; 952 my ($self, $w, $h) = @_;
810
811 $self->_size_allocate ($x, $y, $w, $h);
812 953
813 ($h, $w) = ($w, $h); 954 ($h, $w) = ($w, $h);
814 955
815 my $children = $self->{children}; 956 my $children = $self->{children};
816 957
817 my @h = map +($_->size_request)[0], @$children; 958 my @h = map $_->{req_w}, @$children;
818 959
819 my $req_h = List::Util::sum @h; 960 my $req_h = List::Util::sum @h;
820 961
821 if ($req_h > $h) { 962 if ($req_h > $h) {
822 # ah well, not enough space 963 # ah well, not enough space
832 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 973 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
833 $h[$_] = $alloc_h; 974 $h[$_] = $alloc_h;
834 } 975 }
835 } 976 }
836 977
978 CFClient::UI::harmonize \@h;
979
837 my $y = 0; 980 my $y = 0;
838 for (0 .. $#$children) { 981 for (0 .. $#$children) {
839 my $child = $children->[$_]; 982 my $child = $children->[$_];
840 my $h = int $h[$_]; 983 my $h = $h[$_];
841 $child->size_allocate ($y, 0, $h, $w); 984 $child->configure ($y, 0, $h, $w);
842 985
843 $y += $h; 986 $y += $h;
844 } 987 }
988
989 1
845} 990}
846 991
847############################################################################# 992#############################################################################
848 993
849package CFClient::UI::VBox; 994package CFClient::UI::VBox;
862 (List::Util::sum map $_->[1], @alloc), 1007 (List::Util::sum map $_->[1], @alloc),
863 ) 1008 )
864} 1009}
865 1010
866sub size_allocate { 1011sub size_allocate {
867 my ($self, $x, $y, $w, $h) = @_; 1012 my ($self, $w, $h) = @_;
868
869 $self->_size_allocate ($x, $y, $w, $h);
870 1013
871 my $children = $self->{children}; 1014 my $children = $self->{children};
872 1015
873 my @h = map +($_->size_request)[1], @$children; 1016 my @h = map $_->{req_h}, @$children;
874 1017
875 my $req_h = List::Util::sum @h; 1018 my $req_h = List::Util::sum @h;
876 1019
877 if ($req_h > $h) { 1020 if ($req_h > $h) {
878 # ah well, not enough space 1021 # ah well, not enough space
886 1029
887 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1030 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
888 } 1031 }
889 } 1032 }
890 1033
1034 CFClient::UI::harmonize \@h;
1035
891 my $y = 0; 1036 my $y = 0;
892 for (0 .. $#$children) { 1037 for (0 .. $#$children) {
893 my $child = $children->[$_]; 1038 my $child = $children->[$_];
894 my $h = int $h[$_]; 1039 my $h = $h[$_];
895 $child->size_allocate (0, $y, $w, $h); 1040 $child->configure (0, $y, $w, $h);
896 1041
897 $y += $h; 1042 $y += $h;
898 } 1043 }
1044
1045 1
899} 1046}
900 1047
901############################################################################# 1048#############################################################################
902 1049
903package CFClient::UI::Label; 1050package CFClient::UI::Label;
904 1051
905our @ISA = CFClient::UI::Base::; 1052our @ISA = CFClient::UI::Base::;
906 1053
907use SDL::OpenGL; 1054use CFClient::OpenGL;
908 1055
909sub new { 1056sub new {
910 my ($class, %arg) = @_; 1057 my ($class, %arg) = @_;
911 1058
912 my $self = $class->SUPER::new ( 1059 my $self = $class->SUPER::new (
913 fg => [1, 1, 1], 1060 fg => [1, 1, 1],
914 fontsize => $::FONTSIZE, 1061 fontsize => 1,
915 text => "", 1062 text => "",
916 align => -1, 1063 align => -1,
1064 valign => -1,
917 padding => 2, 1065 padding => 2,
918 layout => new CFClient::Layout, 1066 layout => new CFClient::Layout,
919 %arg 1067 %arg
920 ); 1068 );
921 1069
922 $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};
923 1078
924 $self 1079 $self
925} 1080}
926 1081
927sub escape_text { 1082sub escape_text {
935} 1090}
936 1091
937sub set_text { 1092sub set_text {
938 my ($self, $text) = @_; 1093 my ($self, $text) = @_;
939 1094
940 $self->{text} = $text;
941 $self->{layout}->set_markup ($text); 1095 $self->{layout}->set_text ($text);
942 1096
943 delete $self->{texture}; 1097 delete $self->{texture};
944# $self->{w} = $self->{h} = -1; 1098 $self->check_size;
945 $self->update; 1099 $self->update;
946} 1100}
947 1101
948sub get_text { 1102sub set_markup {
949 my ($self, $text) = @_; 1103 my ($self, $markup) = @_;
950 1104
951 $self->{text} 1105 $self->{layout}->set_markup ($markup);
1106
1107 delete $self->{texture};
1108 $self->check_size;
1109 $self->update;
952} 1110}
953 1111
954sub size_request { 1112sub size_request {
955 my ($self) = @_; 1113 my ($self) = @_;
956 1114
957 $self->{layout}->set_width; 1115 $self->{layout}->set_width;
958 $self->{layout}->set_height ($self->{fontsize}); 1116 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1117
959 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 }
960 1128
961 ( 1129 (
962 $w + $self->{padding} * 2, 1130 $w + $self->{padding} * 2,
963 $h + $self->{padding} * 2, 1131 $h + $self->{padding} * 2,
964 ) 1132 )
965} 1133}
966 1134
967sub size_allocate { 1135sub size_allocate {
968 my ($self, $x, $y, $w, $h) = @_; 1136 my ($self, $w, $h) = @_;
969
970 $self->_size_allocate ($x, $y, $w, $h) or return;
971 1137
972 delete $self->{texture}; 1138 delete $self->{texture};
973} 1139}
974 1140
975sub update { 1141sub set_fontsize {
976 my ($self) = @_; 1142 my ($self, $fontsize) = @_;
977 1143
978 delete $self->{texture}; 1144 $self->{fontsize} = $fontsize;
979 $self->SUPER::update; 1145 $self->check_size;
980} 1146}
981 1147
982sub _draw { 1148sub _draw {
983 my ($self) = @_; 1149 my ($self) = @_;
984 1150
985 my $tex = $self->{texture} ||= do { 1151 my $tex = $self->{texture} ||= do {
986 $self->{layout}->set_width ($self->{w}); 1152 $self->{layout}->set_width ($self->{w});
987 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1153 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
988 new_from_layout CFClient::Texture $self->{layout} 1154 new_from_layout CFClient::Texture $self->{layout}
989 }; 1155 };
990 1156
991 glEnable GL_BLEND; 1157 glEnable GL_BLEND;
992 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1158 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
993 glEnable GL_TEXTURE_2D; 1159 glEnable GL_TEXTURE_2D;
994 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1160 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
995 1161
996 glColor @{$self->{fg}}; 1162 glColor @{$self->{fg}};
997 1163
998 my $x = 1164 $self->{ox} = int (
999 $self->{align} < 0 ? $self->{padding} 1165 $self->{align} < 0 ? $self->{padding}
1000 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1166 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1001 : ($self->{w} - $tex->{w}) * 0.5; 1167 : ($self->{w} - $tex->{w}) * 0.5
1168 );
1002 1169
1003 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1170 $self->{oy} = int (
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});
1004 1177
1005 glDisable GL_TEXTURE_2D; 1178 glDisable GL_TEXTURE_2D;
1006 glDisable GL_BLEND; 1179 glDisable GL_BLEND;
1007} 1180}
1008 1181
1009############################################################################# 1182#############################################################################
1010 1183
1011package CFClient::UI::Entry; 1184package CFClient::UI::EntryBase;
1012 1185
1013our @ISA = CFClient::UI::Label::; 1186our @ISA = CFClient::UI::Label::;
1014 1187
1015use SDL; 1188use CFClient::OpenGL;
1016use SDL::OpenGL;
1017 1189
1018sub new { 1190sub new {
1019 my $class = shift; 1191 my $class = shift;
1020 1192
1021 $class->SUPER::new ( 1193 $class->SUPER::new (
1023 bg => [0, 0, 0, 0.2], 1195 bg => [0, 0, 0, 0.2],
1024 active_bg => [1, 1, 1, 0.5], 1196 active_bg => [1, 1, 1, 0.5],
1025 active_fg => [0, 0, 0], 1197 active_fg => [0, 0, 0],
1026 can_hover => 1, 1198 can_hover => 1,
1027 can_focus => 1, 1199 can_focus => 1,
1200 valign => 0,
1028 @_ 1201 @_
1029 ) 1202 )
1030} 1203}
1031 1204
1032sub _set_text { 1205sub _set_text {
1033 my ($self, $text) = @_; 1206 my ($self, $text) = @_;
1034 1207
1035 my $old_text = $self->{text}; 1208 delete $self->{cur_h};
1209
1210 return if $self->{text} eq $text;
1211
1212 delete $self->{texture};
1036 1213
1037 $self->{last_activity} = $::NOW; 1214 $self->{last_activity} = $::NOW;
1038
1039 $self->{text} = $text; 1215 $self->{text} = $text;
1040 $self->{layout}->set_width ($self->{w});
1041 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1042 1216
1043 $text =~ s/./*/g if $self->{hidden}; 1217 $text =~ s/./*/g if $self->{hidden};
1218 $self->{layout}->set_text ("$text ");
1044 1219
1045 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1220 $self->emit (changed => $self->{text});
1221}
1046 1222
1047 $text = substr $text, 0, $self->{cursor}; 1223sub get_text {
1048 utf8::encode $text; 1224 $_[0]{text}
1049
1050 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1051
1052 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1053 if $old_text ne $self->{text};
1054} 1225}
1055 1226
1056sub size_request { 1227sub size_request {
1057 my ($self) = @_; 1228 my ($self) = @_;
1058 1229
1060 1231
1061 ($w + 1, $h) # add 1 for cursor 1232 ($w + 1, $h) # add 1 for cursor
1062} 1233}
1063 1234
1064sub size_allocate { 1235sub size_allocate {
1065 my ($self, $x, $y, $w, $h) = @_; 1236 my ($self, $w, $h) = @_;
1066 1237
1067 $self->SUPER::size_allocate ($x, $y, $w, $h); 1238 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1068
1069 $self->_set_text ($self->{text});
1070} 1239}
1071 1240
1072sub set_text { 1241sub set_text {
1073 my ($self, $text) = @_; 1242 my ($self, $text) = @_;
1074 1243
1078} 1247}
1079 1248
1080sub key_down { 1249sub key_down {
1081 my ($self, $ev) = @_; 1250 my ($self, $ev) = @_;
1082 1251
1083 my $mod = $ev->key_mod; 1252 my $mod = $ev->{mod};
1084 my $sym = $ev->key_sym; 1253 my $sym = $ev->{sym};
1085
1086 my $uni = $ev->key_unicode; 1254 my $uni = $ev->{unicode};
1087 1255
1088 my $text = $self->get_text; 1256 my $text = $self->get_text;
1089 1257
1090 if ($sym == SDLK_BACKSPACE) { 1258 if ($sym == 8) {
1091 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1259 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1092 } elsif ($sym == SDLK_DELETE) { 1260 } elsif ($sym == 127) {
1093 substr $text, $self->{cursor}, 1, ""; 1261 substr $text, $self->{cursor}, 1, "";
1094 } elsif ($sym == SDLK_LEFT) { 1262 } elsif ($sym == CFClient::SDLK_LEFT) {
1095 --$self->{cursor} if $self->{cursor}; 1263 --$self->{cursor} if $self->{cursor};
1096 } elsif ($sym == SDLK_RIGHT) { 1264 } elsif ($sym == CFClient::SDLK_RIGHT) {
1097 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1265 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1098 } elsif ($sym == SDLK_HOME) { 1266 } elsif ($sym == CFClient::SDLK_HOME) {
1099 $self->{cursor} = 0; 1267 $self->{cursor} = 0;
1100 } elsif ($sym == SDLK_END) { 1268 } elsif ($sym == CFClient::SDLK_END) {
1101 $self->{cursor} = length $text; 1269 $self->{cursor} = length $text;
1102 } elsif ($sym == SDLK_ESCAPE) { 1270 } elsif ($sym == 27) {
1103 $self->emit ('escape'); 1271 $self->emit ('escape');
1104 } elsif ($uni) { 1272 } elsif ($uni) {
1105 substr $text, $self->{cursor}++, 0, chr $uni; 1273 substr $text, $self->{cursor}++, 0, chr $uni;
1106 } 1274 }
1107 1275
1162 1330
1163 $self->SUPER::_draw; 1331 $self->SUPER::_draw;
1164 1332
1165 #TODO: force update every cursor change :( 1333 #TODO: force update every cursor change :(
1166 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
1167 glColor @{$self->{fg}}; 1343 glColor @{$self->{fg}};
1168 glBegin GL_LINES; 1344 glBegin GL_LINES;
1169 glVertex $self->{cur_x}, $self->{cur_y};
1170 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};
1171 glEnd; 1347 glEnd;
1172 } 1348 }
1173} 1349}
1174 1350
1175package CFClient::UI::LineEntry; 1351package CFClient::UI::Entry;
1176 1352
1177our @ISA = CFClient::UI::Entry::; 1353our @ISA = CFClient::UI::EntryBase::;
1178 1354
1179use SDL; 1355use CFClient::OpenGL;
1180use SDL::OpenGL;
1181 1356
1182sub key_down { 1357sub key_down {
1183 my ($self, $ev) = @_; 1358 my ($self, $ev) = @_;
1184 1359
1185 my $sym = $ev->key_sym; 1360 my $sym = $ev->{sym};
1186 1361
1187 if ($sym == SDLK_RETURN) { 1362 if ($sym == 13) {
1188 $self->emit (activate => $self->get_text); 1363 $self->emit (activate => $self->get_text);
1189 $self->update; 1364 $self->update;
1190 1365
1191 } else { 1366 } else {
1192 $self->SUPER::key_down ($ev); 1367 $self->SUPER::key_down ($ev);
1198 1373
1199package CFClient::UI::Button; 1374package CFClient::UI::Button;
1200 1375
1201our @ISA = CFClient::UI::Label::; 1376our @ISA = CFClient::UI::Label::;
1202 1377
1203use SDL; 1378use CFClient::OpenGL;
1204use SDL::OpenGL;
1205 1379
1206my @tex = 1380my @tex =
1207 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1381 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1208 qw(b1_button_active.png); 1382 qw(b1_button_active.png);
1209 1383
1210sub new { 1384sub new {
1211 my $class = shift; 1385 my $class = shift;
1212 1386
1213 $class->SUPER::new ( 1387 $class->SUPER::new (
1214 padding => 4, 1388 padding => 4,
1215 fg => [1, 1, 1], 1389 fg => [1, 1, 1],
1216 bg => [1, 1, 1, 0.2], 1390 bg => [1, 1, 1, 0.2],
1217 active_fg => [1, 1, 0], 1391 active_fg => [0, 0, 1],
1218 can_hover => 1, 1392 can_hover => 1,
1393 align => 0,
1394 valign => 0,
1219 @_ 1395 @_
1220 ) 1396 )
1221} 1397}
1222 1398
1223sub button_up { 1399sub button_up {
1231 1407
1232sub _draw { 1408sub _draw {
1233 my ($self) = @_; 1409 my ($self) = @_;
1234 1410
1235 local $self->{fg} = $self->{fg}; 1411 local $self->{fg} = $self->{fg};
1236 my $tex = $tex[0];
1237
1238 glEnable GL_BLEND;
1239 glEnable GL_TEXTURE_2D;
1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1241 1412
1242 if ($GRAB == $self) { 1413 if ($GRAB == $self) {
1243 $self->{fg} = $self->{active_fg}; 1414 $self->{fg} = $self->{active_fg};
1244 } 1415 }
1245 1416
1246 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;
1247 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;
1248 1422
1249 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1423 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1250 1424
1251 glDisable GL_TEXTURE_2D; 1425 glDisable GL_TEXTURE_2D;
1252 glDisable GL_BLEND; 1426 glDisable GL_BLEND;
1253 1427
1254 $self->SUPER::_draw; 1428 $self->SUPER::_draw;
1259package CFClient::UI::CheckBox; 1433package CFClient::UI::CheckBox;
1260 1434
1261our @ISA = CFClient::UI::DrawBG::; 1435our @ISA = CFClient::UI::DrawBG::;
1262 1436
1263my @tex = 1437my @tex =
1264 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1438 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1265 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1439 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1266 1440
1267use SDL; 1441use CFClient::OpenGL;
1268use SDL::OpenGL;
1269 1442
1270sub new { 1443sub new {
1271 my $class = shift; 1444 my $class = shift;
1272 1445
1273 $class->SUPER::new ( 1446 $class->SUPER::new (
1284 my ($self) = @_; 1457 my ($self) = @_;
1285 1458
1286 ($self->{padding} * 2 + 6) x 2 1459 ($self->{padding} * 2 + 6) x 2
1287} 1460}
1288 1461
1289sub size_allocate {
1290 my ($self, $x, $y, $w, $h) = @_;
1291
1292 $self->_size_allocate ($x, $y, $w, $h);
1293}
1294
1295sub button_down { 1462sub button_down {
1296 my ($self, $ev, $x, $y) = @_; 1463 my ($self, $ev, $x, $y) = @_;
1297 1464
1298 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1465 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1299 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1466 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1325 glDisable GL_BLEND; 1492 glDisable GL_BLEND;
1326} 1493}
1327 1494
1328############################################################################# 1495#############################################################################
1329 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;
1657 glEnd;
1658
1659 glBindTexture GL_TEXTURE_2D, $t2->{name};
1660 glBegin GL_QUADS;
1661 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1662 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1663 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1664 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1665 glEnd;
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);
1713}
1714
1715#############################################################################
1716
1330package CFClient::UI::Slider; 1717package CFClient::UI::Slider;
1331 1718
1332use strict; 1719use strict;
1333 1720
1334use SDL::OpenGL; 1721use CFClient::OpenGL;
1335 1722
1336our @ISA = CFClient::UI::DrawBG::; 1723our @ISA = CFClient::UI::DrawBG::;
1337 1724
1338my @tex = 1725my @tex =
1339 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1726 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1349 # TODO: calculations are off 1736 # TODO: calculations are off
1350 my $self = $class->SUPER::new ( 1737 my $self = $class->SUPER::new (
1351 fg => [1, 1, 1], 1738 fg => [1, 1, 1],
1352 active_fg => [0, 0, 0], 1739 active_fg => [0, 0, 0],
1353 range => [0, 0, 100, 10], 1740 range => [0, 0, 100, 10],
1354 req_w => 40, 1741 req_w => 20,
1355 req_h => 13, 1742 req_h => 20,
1356 vertical => 0, 1743 vertical => 0,
1357 can_hover => 1, 1744 can_hover => 1,
1358 inner_pad => 5, 1745 inner_pad => 5,
1359 @_ 1746 @_
1360 ); 1747 );
1464 1851
1465package CFClient::UI::TextView; 1852package CFClient::UI::TextView;
1466 1853
1467our @ISA = CFClient::UI::HBox::; 1854our @ISA = CFClient::UI::HBox::;
1468 1855
1469use SDL::OpenGL; 1856use CFClient::OpenGL;
1470 1857
1471sub new { 1858sub new {
1472 my $class = shift; 1859 my $class = shift;
1473 1860
1474 my $self = $class->SUPER::new ( 1861 my $self = $class->SUPER::new (
1475 req_w => $::WIDTH / 6,
1476 req_h => $::HEIGHT / 6,
1477 fontsize => $::FONTSIZE, 1862 fontsize => 1,
1478 @_, 1863 @_,
1479 1864
1480 layout => (new CFClient::Layout), 1865 layout => (new CFClient::Layout),
1481 par => [], 1866 par => [],
1482 height => 0, 1867 height => 0,
1503sub text_height { 1888sub text_height {
1504 my ($self, $text) = @_; 1889 my ($self, $text) = @_;
1505 1890
1506 my $layout = $self->{layout}; 1891 my $layout = $self->{layout};
1507 1892
1508 $layout->set_height ($self->{fontsize}); 1893 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1509 $layout->set_width ($self->{w}); 1894 $layout->set_width ($self->{w});
1510 $layout->set_text ($text); 1895 $layout->set_text ($text);
1511 1896
1512 ($layout->size)[1] 1897 ($layout->size)[1]
1513} 1898}
1517 1902
1518 $self->{need_reflow}++; 1903 $self->{need_reflow}++;
1519 $self->update; 1904 $self->update;
1520} 1905}
1521 1906
1522sub size_request {
1523 my ($self) = @_;
1524
1525 ($self->{req_w}, $self->{req_h})
1526}
1527
1528sub size_allocate { 1907sub size_allocate {
1529 my ($self, $x, $y, $w, $h) = @_; 1908 my ($self, $w, $h) = @_;
1530 1909
1531 $self->SUPER::size_allocate ($x, $y, $w, $h); 1910 $self->SUPER::size_allocate ($w, $h);
1532 1911
1533 $self->{layout}->set_height ($self->{fontsize}); 1912 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1534 $self->{layout}->set_width ($self->{w}); 1913 $self->{layout}->set_width ($self->{children}[0]{w});
1535 1914
1536 $self->reflow; 1915 $self->reflow;
1537} 1916}
1538 1917
1539sub add_paragraph { 1918sub add_paragraph {
1558 1937
1559 return unless $self->{h} > 0; 1938 return unless $self->{h} > 0;
1560 1939
1561 delete $self->{texture}; 1940 delete $self->{texture};
1562 1941
1563 $TOPLEVEL->on_refresh ($self, sub { 1942 $ROOT->on_refresh ($self, sub {
1564 if (delete $self->{need_reflow}) { 1943 if (delete $self->{need_reflow}) {
1565 my $height = 0; 1944 my $height = 0;
1566 1945
1567 $height += $_->[0] = $self->text_height ($_->[2]) 1946 $height += $_->[0] = $self->text_height ($_->[2])
1568 for @{$self->{par}}; 1947 for @{$self->{par}};
1626 2005
1627} 2006}
1628 2007
1629############################################################################# 2008#############################################################################
1630 2009
1631package CFClient::UI::MapWidget;
1632
1633use strict;
1634
1635use List::Util qw(min max);
1636
1637use SDL;
1638use SDL::OpenGL;
1639
1640our @ISA = CFClient::UI::Base::;
1641
1642sub new {
1643 my $class = shift;
1644
1645 $class->SUPER::new (
1646 z => -1,
1647 can_focus => 1,
1648 list => (glGenLists 1),
1649 @_
1650 )
1651}
1652
1653sub key_down {
1654 print "MAPKEYDOWN\n";
1655}
1656
1657sub key_up {
1658}
1659
1660sub size_request {
1661 (
1662 1 + 32 * int $::WIDTH / 32,
1663 1 + 32 * int $::HEIGHT / 32,
1664 )
1665}
1666
1667sub update {
1668 my ($self) = @_;
1669
1670 $self->{need_update} = 1;
1671 $self->SUPER::update;
1672}
1673
1674sub draw {
1675 my ($self) = @_;
1676
1677 $self->{need_update}++;#d#
1678 if (delete $self->{need_update}) {
1679 glNewList $self->{list}, GL_COMPILE;
1680
1681 my $sw = int $::WIDTH / 32;
1682 my $sh = int $::HEIGHT / 32;
1683
1684 if ($::MAP) {
1685 my ($w, $h, $data) = $::MAP->draw (0, 0, $sw, $sh);
1686
1687 if ($::CFG->{fow_enable}) {
1688 if ($::CFG->{fow_smooth}) { # smooth fog of war
1689 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1690 glConvolutionFilter2D
1691 GL_CONVOLUTION_2D,
1692 GL_ALPHA,
1693 3, 3,
1694 GL_ALPHA, GL_FLOAT,
1695 pack "f*",
1696 0.1, 0.1, 0.1,
1697 0.1, 0.2, 0.1,
1698 0.1, 0.1, 0.1,
1699 ;
1700 glEnable GL_CONVOLUTION_2D;
1701 }
1702
1703 my $tex = new CFClient::Texture
1704 w => $w,
1705 h => $h,
1706 data => $data,
1707 internalformat => GL_ALPHA,
1708 format => GL_ALPHA;
1709
1710 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1711
1712 glEnable GL_BLEND;
1713 glEnable GL_TEXTURE_2D;
1714 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1715
1716 glColor +($::CFG->{fow_intensity}) x 3, 1;
1717 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1718
1719 glDisable GL_TEXTURE_2D;
1720 glDisable GL_BLEND;
1721 }
1722 }
1723
1724 glEndList;
1725 }
1726
1727 glCallList $self->{list};
1728
1729 if ($FOCUS != $self) {
1730 glColor 64/255, 64/255, 64/255;
1731 glLogicOp GL_AND;
1732 glEnable GL_COLOR_LOGIC_OP;
1733 glBegin GL_QUADS;
1734 glVertex 0, 0;
1735 glVertex 0, $::HEIGHT;
1736 glVertex $::WIDTH, $::HEIGHT;
1737 glVertex $::WIDTH, 0;
1738 glEnd;
1739 glDisable GL_COLOR_LOGIC_OP;
1740 }
1741}
1742
1743my %DIR = (
1744 SDLK_KP8, [1, "north"],
1745 SDLK_KP9, [2, "northeast"],
1746 SDLK_KP6, [3, "east"],
1747 SDLK_KP3, [4, "southeast"],
1748 SDLK_KP2, [5, "south"],
1749 SDLK_KP1, [6, "southwest"],
1750 SDLK_KP4, [7, "west"],
1751 SDLK_KP7, [8, "northwest"],
1752
1753 SDLK_UP, [1, "north"],
1754 SDLK_RIGHT, [3, "east"],
1755 SDLK_DOWN, [5, "south"],
1756 SDLK_LEFT, [7, "west"],
1757);
1758
1759sub key_down {
1760 my ($self, $ev) = @_;
1761
1762 my $mod = $ev->key_mod;
1763 my $sym = $ev->key_sym;
1764
1765 if ($sym == SDLK_KP5) {
1766 $::CONN->user_send ("command stay fire");
1767 } elsif ($sym == SDLK_a) {
1768 $::CONN->user_send ("command apply");
1769 } elsif ($sym == SDLK_QUOTE) {
1770 $self->emit ('activate_console');
1771 } elsif ($sym == SDLK_SLASH) {
1772 $self->emit ('activate_console' => '/');
1773 } elsif (exists $DIR{$sym}) {
1774 if ($mod & KMOD_SHIFT) {
1775 $self->{shft}++;
1776 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1777 } elsif ($mod & KMOD_CTRL) {
1778 $self->{ctrl}++;
1779 $::CONN->user_send ("command run $DIR{$sym}[0]");
1780 } else {
1781 $::CONN->user_send ("command $DIR{$sym}[1]");
1782 }
1783 }
1784}
1785
1786sub key_up {
1787 my ($self, $ev) = @_;
1788
1789 my $mod = $ev->key_mod;
1790 my $sym = $ev->key_sym;
1791
1792 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1793 $::CONN->user_send ("command fire_stop");
1794 }
1795 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1796 $::CONN->user_send ("command run_stop");
1797 }
1798}
1799
1800#############################################################################
1801
1802package CFClient::UI::Animator; 2010package CFClient::UI::Animator;
1803 2011
1804use SDL::OpenGL; 2012use CFClient::OpenGL;
1805 2013
1806our @ISA = CFClient::UI::Bin::; 2014our @ISA = CFClient::UI::Bin::;
1807 2015
1808sub moveto { 2016sub moveto {
1809 my ($self, $x, $y) = @_; 2017 my ($self, $x, $y) = @_;
1865sub toggle_flopper { 2073sub toggle_flopper {
1866 my ($self) = @_; 2074 my ($self) = @_;
1867 2075
1868 # TODO: use animation 2076 # TODO: use animation
1869 if ($self->{state} = !$self->{state}) { 2077 if ($self->{state} = !$self->{state}) {
1870 $CFClient::UI::TOPLEVEL->add ($self->{other}); 2078 $CFClient::UI::ROOT->add ($self->{other});
1871 $self->{other}->move ( 2079 $self->{other}->move ($self->coord2global (0, $self->{h}));
1872 ($::WIDTH - $self->{other}{w}) * 0.5, 2080 $self->emit ("open");
1873 ($::HEIGHT - $self->{other}{h}) * 0.5,
1874 );
1875 } else { 2081 } else {
1876 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 2082 $CFClient::UI::ROOT->remove ($self->{other});
2083 $self->emit ("close");
1877 } 2084 }
1878}
1879 2085
1880############################################################################# 2086 $self->emit (changed => $self->{state});
2087}
1881 2088
2089#############################################################################
2090
1882package CFClient::UI::Toplevel; 2091package CFClient::UI::Root;
1883 2092
1884our @ISA = CFClient::UI::Container::; 2093our @ISA = CFClient::UI::Container::;
1885 2094
1886use SDL::OpenGL; 2095use CFClient::OpenGL;
2096
2097sub check_size {
2098 my ($self) = @_;
2099
2100 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2101}
1887 2102
1888sub size_request { 2103sub size_request {
1889 ($::WIDTH, $::HEIGHT) 2104 ($::WIDTH, $::HEIGHT)
1890} 2105}
1891 2106
1892sub size_allocate { 2107sub configure {
1893 my ($self, $x, $y, $w, $h) = @_; 2108 my ($self, $x, $y, $w, $h) = @_;
1894 2109
1895 $self->_size_allocate ($x, $y, $w, $h); 2110 $self->SUPER::configure ($x, $y, $w, $h);
1896 2111
1897 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1898 for @{$self->{children}}; 2112 for my $child (@{$self->{children}}) {
1899} 2113 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
1900 2114
1901sub 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 {
1902 my ($self, $x, $y) = @_; 2122 my ($self, $x, $y) = @_;
1903 2123
1904 ($x, $y) 2124 ($x, $y)
1905} 2125}
1906 2126
1907sub update { 2127sub update {
1908 my ($self) = @_; 2128 my ($self) = @_;
1909 2129
1910 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2130 $self->check_size;
1911 ::refresh (); 2131 ::refresh ();
1912} 2132}
1913 2133
1914sub add { 2134sub add {
1915 my ($self, $widget) = @_; 2135 my ($self, $child) = @_;
1916 2136
2137 # integerize window positions
2138 $child->{x} = int $child->{x};
2139 $child->{y} = int $child->{y};
2140
1917 $self->SUPER::add ($widget); 2141 $self->SUPER::add ($child);
1918
1919 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1920} 2142}
1921 2143
1922sub on_refresh { 2144sub on_refresh {
1923 my ($self, $id, $cb) = @_; 2145 my ($self, $id, $cb) = @_;
1924 2146
1947 2169
1948############################################################################# 2170#############################################################################
1949 2171
1950package CFClient::UI; 2172package CFClient::UI;
1951 2173
1952$TOPLEVEL = new CFClient::UI::Toplevel; 2174$ROOT = new CFClient::UI::Root;
1953 2175
19541 21761
1955 2177

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines