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.115 by root, Sat Apr 15 19:35:20 2006 UTC vs.
Revision 1.150 by elmex, Sun Apr 23 00:08:29 2006 UTC

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 = $ROOT->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->coord2local ($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 || $ROOT->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->coord2local ($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;
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 || $ROOT->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;
74sub harmonize { 74sub harmonize {
75 my ($vals) = @_; 75 my ($vals) = @_;
76 76
77 my $rem = 0; 77 my $rem = 0;
78 78
79 for ($vals) { 79 for (@$vals) {
80 my $i = int $_ + $rem; 80 my $i = int $_ + $rem;
81 $rem += $_ - $i; 81 $rem += $_ - $i;
82 $_ = $i; 82 $_ = $i;
83 } 83 }
84} 84}
87 87
88package CFClient::UI::Base; 88package CFClient::UI::Base;
89 89
90use strict; 90use strict;
91 91
92use SDL::OpenGL; 92use CFClient::OpenGL;
93 93
94sub new { 94sub new {
95 my $class = shift; 95 my $class = shift;
96 96
97 my $self = bless { 97 my $self = bless {
98 x => 0, 98 x => 0,
99 y => 0, 99 y => 0,
100 z => 0, 100 z => 0,
101 w => -1, 101 can_events => 1,
102 h => -1,
103 @_ 102 @_
104 }, $class; 103 }, $class;
105 104
106 for (keys %$self) { 105 for (keys %$self) {
107 if (/^connect_(.*)$/) { 106 if (/^connect_(.*)$/) {
110 } 109 }
111 110
112 $self 111 $self
113} 112}
114 113
114sub show {
115 my ($self) = @_;
116
117 return if $self->{parent};
118
119 $CFClient::UI::ROOT->add ($self);
120}
121
122sub hide {
123 my ($self) = @_;
124
125 return unless $self->{parent};
126
127 $self->{parent}->remove ($self);
128}
129
115sub move { 130sub move {
116 my ($self, $x, $y, $z) = @_; 131 my ($self, $x, $y, $z) = @_;
132
117 $self->{x} = $x; 133 $self->{x} = int $x;
118 $self->{y} = $y; 134 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 135 $self->{z} = $z if defined $z;
136
137 $self->update;
120} 138}
121 139
122sub needs_redraw { 140sub needs_redraw {
123 0 141 0
124} 142}
125 143
126sub size_request { 144sub size_request {
127 require Carp; 145 require Carp;
128 Carp::confess "size_request is abtract"; 146 Carp::confess "size_request is abstract";
129} 147}
130 148
131sub _size_allocate { 149sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 150 my ($self, $x, $y, $w, $h) = @_;
133 151
152 if ($self->{aspect}) {
153 my $w2 = List::Util::min $w, int $h * $self->{aspect};
154 my $h2 = List::Util::min $h, int $w / $self->{aspect};
155
156 # use alignment to adjust x, y
157
158 $x += int +($w - $w2) * 0.5;
159 $y += int +($h - $h2) * 0.5;
160
161 ($w, $h) = ($w2, $h2);
162 }
163
164 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 165 $self->{x} = $x;
135 $self->{y} = $y; 166 $self->{y} = $y;
167 $self->update;
168 }
136 169
137 return unless $self->{w} != $w || $self->{h} != $h; 170 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 171 $self->{w} = $w;
140 $self->{h} = $h; 172 $self->{h} = $h;
141 173
174 $self->size_allocate ($w, $h);
175 $self->update;
142 1 176 }
143} 177}
144 178
145sub size_allocate { 179sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 180 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 181}
150 182
151# return top left coordinates 183# return top left coordinates
152sub _topleft { 184sub _topleft {
153 my ($self, $x, $y) = @_; 185 my ($self, $x, $y) = @_;
176 208
177 return if $FOCUS == $self; 209 return if $FOCUS == $self;
178 return unless $self->{can_focus}; 210 return unless $self->{can_focus};
179 211
180 my $focus = $FOCUS; $FOCUS = $self; 212 my $focus = $FOCUS; $FOCUS = $self;
213
214 $self->emit (focus_in => $focus);
215
181 $focus->update if $focus; 216 $focus->update if $focus;
182 $FOCUS->update; 217 $FOCUS->update;
183} 218}
184 219
185sub focus_out { 220sub focus_out {
186 my ($self) = @_; 221 my ($self) = @_;
187 222
188 return unless $FOCUS == $self; 223 return unless $FOCUS == $self;
189 224
190 my $focus = $FOCUS; undef $FOCUS; 225 my $focus = $FOCUS; undef $FOCUS;
226
227 $self->emit (focus_out => $focus);
228
191 $focus->update if $focus; #? 229 $focus->update if $focus; #?
192} 230}
193 231
194sub mouse_motion { } 232sub mouse_motion { }
195sub button_up { } 233sub button_up { }
219 glPopMatrix; 257 glPopMatrix;
220 258
221 if ($self == $HOVER && $self->{can_hover}) { 259 if ($self == $HOVER && $self->{can_hover}) {
222 my ($x, $y) = @$self{qw(x y)}; 260 my ($x, $y) = @$self{qw(x y)};
223 261
224 glColor 0, 0, 1, 0.2; 262 glColor 1, 0.8, 0.5, 0.2;
225 glEnable GL_BLEND; 263 glEnable GL_BLEND;
226 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 264 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
227 glBegin GL_QUADS; 265 glBegin GL_QUADS;
228 glVertex $x , $y; 266 glVertex $x , $y;
229 glVertex $x + $self->{w}, $y; 267 glVertex $x + $self->{w}, $y;
241} 279}
242 280
243sub find_widget { 281sub find_widget {
244 my ($self, $x, $y) = @_; 282 my ($self, $x, $y) = @_;
245 283
284 return () unless $self->{can_events};
285
246 return $self 286 return $self
247 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 287 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
248 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 288 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
249 289
250 () 290 ()
254 my ($self, $parent) = @_; 294 my ($self, $parent) = @_;
255 295
256 Scalar::Util::weaken ($self->{parent} = $parent); 296 Scalar::Util::weaken ($self->{parent} = $parent);
257} 297}
258 298
299sub check_size {
300 my ($self) = @_;
301
302 return unless $self->{parent};
303
304 my ($w, $h) = $self->size_request;
305
306 if ($w != $self->{req_w} || $h != $self->{req_h}) {
307 $self->{req_w} = $w;
308 $self->{req_h} = $h;
309
310 $self->{parent}->check_size;
311 }
312}
313
259sub update { 314sub update {
260 my ($self) = @_; 315 my ($self) = @_;
261 316
262 $self->{parent}->update 317 $self->{parent}->update
263 if $self->{parent}; 318 if $self->{parent};
264} 319}
265 320
266sub connect { 321sub connect {
267 my ($self, $signal, $cb) = @_; 322 my ($self, $signal, $cb) = @_;
268 323
269 push @{ $self->{cb}{$signal} }, $cb; 324 push @{ $self->{signal_cb}{$signal} }, $cb;
270} 325}
271 326
272sub emit { 327sub emit {
273 my ($self, $signal, @args) = @_; 328 my ($self, $signal, @args) = @_;
274 329
330 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
275 $_->($self, @args) 331 $cb->($self, @args);
276 for @{$self->{cb}{$signal} || []}; 332 }
277} 333}
278 334
279sub DESTROY { 335sub DESTROY {
280 my ($self) = @_; 336 my ($self) = @_;
281 337
287package CFClient::UI::DrawBG; 343package CFClient::UI::DrawBG;
288 344
289our @ISA = CFClient::UI::Base::; 345our @ISA = CFClient::UI::Base::;
290 346
291use strict; 347use strict;
292use SDL::OpenGL; 348use CFClient::OpenGL;
293 349
294sub new { 350sub new {
295 my $class = shift; 351 my $class = shift;
296 352
297 # range [value, low, high, page] 353 # range [value, low, high, page]
326 382
327package CFClient::UI::Empty; 383package CFClient::UI::Empty;
328 384
329our @ISA = CFClient::UI::Base::; 385our @ISA = CFClient::UI::Base::;
330 386
387sub new {
388 my ($class, %arg) = @_;
389 $class->SUPER::new (can_events => 0, %arg);
390}
391
331sub size_request { 392sub size_request {
332 (0, 0) 393 (0, 0)
333} 394}
334 395
335sub draw { } 396sub draw { }
343sub new { 404sub new {
344 my ($class, %arg) = @_; 405 my ($class, %arg) = @_;
345 406
346 my $children = delete $arg{children} || []; 407 my $children = delete $arg{children} || [];
347 408
348 my $self = $class->SUPER::new (children => [], %arg); 409 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
349 $self->add ($_) for @$children; 410 $self->add ($_) for @$children;
350 411
351 $self 412 $self
352} 413}
353 414
361 $self->{children} = [ 422 $self->{children} = [
362 sort { $a->{z} <=> $b->{z} } 423 sort { $a->{z} <=> $b->{z} }
363 @{$self->{children}}, $child 424 @{$self->{children}}, $child
364 ]; 425 ];
365 426
366 $self->{w} = $self->{h} = -1; 427 $child->check_size;
367 $self->update;
368} 428}
369 429
370sub remove { 430sub remove {
371 my ($self, $widget) = @_; 431 my ($self, $child) = @_;
372 432
433 delete $child->{parent};
434
373 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 435 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
374 436
375 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 437 $self->check_size;
376} 438}
377 439
378sub find_widget { 440sub find_widget {
379 my ($self, $x, $y) = @_; 441 my ($self, $x, $y) = @_;
380 442
410 472
411 $class->SUPER::new (children => [$child], %arg) 473 $class->SUPER::new (children => [$child], %arg)
412} 474}
413 475
414sub add { 476sub add {
415 my ($self, $widget) = @_; 477 my ($self, $child) = @_;
416 478
417 $self->{children} = []; 479 $self->{children} = [];
418 480
419 $self->SUPER::add ($widget); 481 $self->SUPER::add ($child);
420} 482}
421 483
422sub remove { 484sub remove {
423 my ($self, $widget) = @_; 485 my ($self, $widget) = @_;
424 486
433sub size_request { 495sub size_request {
434 $_[0]{children}[0]->size_request 496 $_[0]{children}[0]->size_request
435} 497}
436 498
437sub size_allocate { 499sub size_allocate {
438 my ($self, $x, $y, $w, $h) = @_; 500 my ($self, $w, $h) = @_;
439 501
440 $self->_size_allocate ($x, $y, $w, $h) or return;
441
442 $self->{children}[0]->size_allocate (0, 0, $w, $h); 502 $self->{children}[0]->configure (0, 0, $w, $h);
443} 503}
444 504
445############################################################################# 505#############################################################################
446 506
447package CFClient::UI::Window; 507package CFClient::UI::Window;
448 508
449our @ISA = CFClient::UI::Bin::; 509our @ISA = CFClient::UI::Bin::;
450 510
451use SDL::OpenGL; 511use CFClient::OpenGL;
452 512
453sub new { 513sub new {
454 my ($class, %arg) = @_; 514 my ($class, %arg) = @_;
455 515
456 my $self = $class->SUPER::new (%arg); 516 my $self = $class->SUPER::new (%arg);
473 $self->child->draw; 533 $self->child->draw;
474 }; 534 };
475} 535}
476 536
477sub size_allocate { 537sub size_allocate {
478 my ($self, $x, $y, $w, $h) = @_; 538 my ($self, $w, $h) = @_;
479 539
480 $self->_size_allocate ($x, $y, $w, $h) or return;
481
482 $self->child->size_allocate (0, 0, $w, $h); 540 $self->child->configure (0, 0, $w, $h);
483 541
484 $self->render_chld; 542 $self->render_chld;
485} 543}
486 544
487sub _draw { 545sub _draw {
503 glDisable GL_TEXTURE_2D; 561 glDisable GL_TEXTURE_2D;
504} 562}
505 563
506############################################################################# 564#############################################################################
507 565
566package CFClient::UI::ViewPort;
567
568our @ISA = CFClient::UI::Window::;
569
570sub new { die }
571
572sub size_request {
573 my ($self) = @_;
574
575 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
576 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
577
578 @$self{qw(child_w child_h)}
579}
580
581sub _draw {
582 my ($self) = @_;
583
584 $self->{children}[1]->draw;
585}
586
587
588#############################################################################
589
508package CFClient::UI::Frame; 590package CFClient::UI::Frame;
509 591
510our @ISA = CFClient::UI::Bin::; 592our @ISA = CFClient::UI::Bin::;
511 593
512use SDL::OpenGL; 594use CFClient::OpenGL;
513
514sub size_request {
515 my ($self) = @_;
516 my $chld = $self->child
517 or return (0, 0);
518
519 $chld->move (2, 2);
520
521 map { $_ + 4 } $chld->size_request;
522}
523
524sub size_allocate {
525 my ($self, $x, $y, $w, $h) = @_;
526
527 $self->_size_allocate ($x, $y, $w, $h) or return;
528
529 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
530}
531
532sub _draw {
533 my ($self) = @_;
534
535 my $chld = $self->child;
536
537 my ($w, $h) = $chld->size_request;
538
539 glBegin GL_QUADS;
540 glColor 0, 0, 0;
541 glVertex 0 , 0;
542 glVertex 0 , $h + 4;
543 glVertex $w + 4 , $h + 4;
544 glVertex $w + 4 , 0;
545 glEnd;
546
547 $chld->draw;
548}
549
550#############################################################################
551
552package CFClient::UI::FancyFrame;
553
554our @ISA = CFClient::UI::Bin::;
555
556use SDL::OpenGL;
557
558my @tex =
559 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
560 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
561 595
562sub new { 596sub new {
563 my $class = shift; 597 my $class = shift;
564 598
565 # TODO: user_x, user_y, overwrite moveto?
566
567 $class->SUPER::new ( 599 my $self = $class->SUPER::new (
568 bg => [1, 1, 1, 1], 600 bg => [1, 1, 1, 1],
569 border_bg => [1, 1, 1, 1], 601 border_bg => [1, 1, 1, 1],
570 border => $::FONTSIZE * 0.8, 602 border => 0.8,
571 @_ 603 @_
604 );
605
606 $self
607}
608
609sub set_size {
610 my ($self, $w, $h) = @_;
611 $self->{req_w} = $w;
612 $self->{req_h} = $h;
613 $self->check_size;
614}
615
616sub size_request {
617 my ($self) = @_;
618 ($self->{req_w}, $self->{req_h})
619}
620
621sub size_allocate {
622 my ($self, $w, $h) = @_;
623 $self->{w} = $w;
624 $self->{h} = $h;
625 $self->child->configure (0, 0, $w, $h);
626}
627
628sub _draw {
629 my ($self) = @_;
630
631 my ($w, $h) = ($self->{w}, $self->{h});
632
633 glEnable GL_BLEND;
634 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
635 glEnable GL_TEXTURE_2D;
636 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
637
638# glBegin GL_QUADS;
639# glColor 0, 0, 0, 0;
640# glVertex 0 , 0;
641# glVertex 0 , $h;
642# glVertex $w, $h;
643# glVertex $w, 0;
644# glEnd;
645
646
647 $self->child->draw;
648 glDisable GL_BLEND;
649 glDisable GL_TEXTURE_2D;
650}
651
652#############################################################################
653
654package CFClient::UI::FancyFrame;
655
656our @ISA = CFClient::UI::Bin::;
657
658use CFClient::OpenGL;
659
660my @tex =
661 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
662 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
663
664sub new {
665 my $class = shift;
666
667 # TODO: user_x, user_y, overwrite moveto?
668
669 my $self = $class->SUPER::new (
670 bg => [1, 1, 1, 1],
671 border_bg => [1, 1, 1, 1],
672 border => 0.8,
673 can_events => 1,
674 @_
675 );
676
677 $self->{title} &&= new CFClient::UI::Label
678 align => 0,
679 valign => 1,
680 text => $self->{title},
681 fontsize => 1;
682
683 $self
684}
685
686sub border {
687 int $_[0]{border} * $::FONTSIZE
688}
689
690sub size_request {
691 my ($self) = @_;
692
693 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
694
695 my ($w, $h) = $self->SUPER::size_request;
696
697 (
698 $w + $self->border * 2,
699 $h + $self->border * 2,
572 ) 700 )
573} 701}
574 702
575sub size_request {
576 my ($self) = @_;
577
578 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
579
580 my ($w, $h) = $self->SUPER::size_request;
581
582 (
583 $w + $self->{border} * 2,
584 $h + $self->{border} * 2,
585 )
586}
587
588sub size_allocate { 703sub size_allocate {
589 my ($self, $x, $y, $w, $h) = @_; 704 my ($self, $w, $h) = @_;
590 705
591 $self->_size_allocate ($x, $y, $w, $h) or return;
592
593 $h -= List::Util::max 0, $self->{border} * 2; 706 $h -= List::Util::max 0, $self->border * 2;
594 $w -= List::Util::max 0, $self->{border} * 2; 707 $w -= List::Util::max 0, $self->border * 2;
595 708
709 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
710 if $self->{title};
711
596 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 712 $self->child->configure ($self->border, $self->border, $w, $h);
597} 713}
598 714
599sub button_down { 715sub button_down {
600 my ($self, $ev, $x, $y) = @_; 716 my ($self, $ev, $x, $y) = @_;
601 717
718 my $border = $self->border;
719
602 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 720 if ($x < $self->{w} && $x >= $self->{w} - $border
603 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 721 && $y < $self->{h} && $y >= $self->{h} - $border) {
604 722
605 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 723 my ($ox, $oy) = ($ev->{x}, $ev->{y});
606 my ($bw, $bh) = ($self->{w}, $self->{h}); 724 my ($bw, $bh) = ($self->{w}, $self->{h});
607 725
608 $self->{motion} = sub { 726 $self->{motion} = sub {
609 my ($ev, $x, $y) = @_; 727 my ($ev, $x, $y) = @_;
610 728
611 ($x, $y) = ($ev->motion_x, $ev->motion_y); 729 ($x, $y) = ($ev->{x}, $ev->{y});
612 730
613 $self->{user_w} = $bw + $x - $ox; 731 $self->{user_w} = $bw + $x - $ox;
614 $self->{user_h} = $bh + $y - $oy; 732 $self->{user_h} = $bh + $y - $oy;
615 $self->update; 733 $self->check_size;
616 }; 734 };
617 735
618 } elsif ($x >= 0 && $x < $self->{w} 736 } elsif ($x >= 0 && $x < $self->{w}
619 && $y >= 0 && $y < $self->{border}) { 737 && $y >= 0 && $y < $border) {
620 738
621 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 739 my ($ox, $oy) = ($ev->{x}, $ev->{y});
622 my ($bx, $by) = ($self->{x}, $self->{y}); 740 my ($bx, $by) = ($self->{x}, $self->{y});
623 741
624 $self->{motion} = sub { 742 $self->{motion} = sub {
625 my ($ev, $x, $y) = @_; 743 my ($ev, $x, $y) = @_;
626 744
627 ($x, $y) = ($ev->motion_x, $ev->motion_y); 745 ($x, $y) = ($ev->{x}, $ev->{y});
628 746
629 $self->move ($bx + $x - $ox, $by + $y - $oy); 747 $self->move ($bx + $x - $ox, $by + $y - $oy);
630 $self->update; 748 $self->update;
631 }; 749 };
632 } 750 }
653 glEnable GL_BLEND; 771 glEnable GL_BLEND;
654 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 772 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
655 glEnable GL_TEXTURE_2D; 773 glEnable GL_TEXTURE_2D;
656 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 774 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
657 775
776 my $border = $self->border;
777
658 glColor @{ $self->{border_bg} }; 778 glColor @{ $self->{border_bg} };
659 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 779 $tex[1]->draw_quad (0, 0, $w, $border);
660 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 780 $tex[3]->draw_quad (0, $border, $border, $ch);
661 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 781 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
662 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 782 $tex[4]->draw_quad (0, $h - $border, $w, $border);
663 783
664 my $bg = $tex[0]; 784 my $bg = $tex[0];
665 785
666 # TODO: repeat texture not scale 786 # TODO: repeat texture not scale
667 my $rep_x = $cw / $bg->{w}; 787 my $rep_x = $cw / $bg->{w};
670 glColor @{ $self->{bg} }; 790 glColor @{ $self->{bg} };
671 791
672 $bg->{s} = $rep_x; 792 $bg->{s} = $rep_x;
673 $bg->{t} = $rep_y; 793 $bg->{t} = $rep_y;
674 $bg->{wrap_mode} = 1; 794 $bg->{wrap_mode} = 1;
675 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 795 $bg->draw_quad ($border, $border, $cw, $ch);
676 796
677 glDisable GL_TEXTURE_2D; 797 glDisable GL_TEXTURE_2D;
678 glDisable GL_BLEND; 798 glDisable GL_BLEND;
679 799
800 $self->{title}->draw if $self->{title};
680 $self->child->draw; 801 $self->child->draw;
681} 802}
682 803
683############################################################################# 804#############################################################################
684 805
686 807
687our @ISA = CFClient::UI::Base::; 808our @ISA = CFClient::UI::Base::;
688 809
689use List::Util qw(max sum); 810use List::Util qw(max sum);
690 811
691use SDL::OpenGL; 812use CFClient::OpenGL;
692 813
693sub new { 814sub new {
694 my $class = shift; 815 my $class = shift;
695 816
696 $class->SUPER::new ( 817 $class->SUPER::new (
703 my ($self, $x, $y, $child) = @_; 824 my ($self, $x, $y, $child) = @_;
704 825
705 $child->set_parent ($self); 826 $child->set_parent ($self);
706 $self->{children}[$y][$x] = $child; 827 $self->{children}[$y][$x] = $child;
707 828
708 $self->{w} = $self->{h} = -1; 829 $child->check_size;
709 $self->update;
710} 830}
711 831
712# TODO: move to container class maybe? send childs a signal on removal? 832# TODO: move to container class maybe? send childs a signal on removal?
713sub clear { 833sub clear {
714 my ($self) = @_; 834 my ($self) = @_;
727 or next; 847 or next;
728 848
729 for my $x (0 .. $#$row) { 849 for my $x (0 .. $#$row) {
730 my $widget = $row->[$x] 850 my $widget = $row->[$x]
731 or next; 851 or next;
732 my ($w, $h) = $widget->size_request; 852 my ($w, $h) = @$widget{qw(req_w req_h)};
733 853
734 $w[$x] = max $w[$x], $w; 854 $w[$x] = max $w[$x], $w;
735 $h[$y] = max $h[$y], $h; 855 $h[$y] = max $h[$y], $h;
736 } 856 }
737 } 857 }
749 (sum @$hs), 869 (sum @$hs),
750 ) 870 )
751} 871}
752 872
753sub size_allocate { 873sub size_allocate {
754 my ($self, $x, $y, $w, $h) = @_; 874 my ($self, $w, $h) = @_;
755
756 $self->_size_allocate ($x, $y, $w, $h) or return;
757 875
758 my ($ws, $hs) = $self->get_wh; 876 my ($ws, $hs) = $self->get_wh;
759 877
760 my $req_w = sum @$ws; 878 my $req_w = sum @$ws;
761 my $req_h = sum @$hs; 879 my $req_h = sum @$hs;
783 901
784 for my $c (0 .. $#$row) { 902 for my $c (0 .. $#$row) {
785 my $col_w = $ws->[$c]; 903 my $col_w = $ws->[$c];
786 904
787 if (my $widget = $row->[$c]) { 905 if (my $widget = $row->[$c]) {
788 $widget->size_allocate ($x, $y, $col_w, $row_h); 906 $widget->configure ($x, $y, $col_w, $row_h);
789 } 907 }
790 908
791 $x += $col_w; 909 $x += $col_w;
792 } 910 }
793 911
838 (List::Util::max map $_->[1], @alloc), 956 (List::Util::max map $_->[1], @alloc),
839 ) 957 )
840} 958}
841 959
842sub size_allocate { 960sub size_allocate {
843 my ($self, $x, $y, $w, $h) = @_; 961 my ($self, $w, $h) = @_;
844
845 $self->_size_allocate ($x, $y, $w, $h);
846 962
847 ($h, $w) = ($w, $h); 963 ($h, $w) = ($w, $h);
848 964
849 my $children = $self->{children}; 965 my $children = $self->{children};
850 966
851 my @h = map +($_->size_request)[0], @$children; 967 my @h = map $_->{req_w}, @$children;
852 968
853 my $req_h = List::Util::sum @h; 969 my $req_h = List::Util::sum @h;
854 970
855 if ($req_h > $h) { 971 if ($req_h > $h) {
856 # ah well, not enough space 972 # ah well, not enough space
872 988
873 my $y = 0; 989 my $y = 0;
874 for (0 .. $#$children) { 990 for (0 .. $#$children) {
875 my $child = $children->[$_]; 991 my $child = $children->[$_];
876 my $h = $h[$_]; 992 my $h = $h[$_];
877 $child->size_allocate ($y, 0, $h, $w); 993 $child->configure ($y, 0, $h, $w);
878 994
879 $y += $h; 995 $y += $h;
880 } 996 }
997
998 1
881} 999}
882 1000
883############################################################################# 1001#############################################################################
884 1002
885package CFClient::UI::VBox; 1003package CFClient::UI::VBox;
898 (List::Util::sum map $_->[1], @alloc), 1016 (List::Util::sum map $_->[1], @alloc),
899 ) 1017 )
900} 1018}
901 1019
902sub size_allocate { 1020sub size_allocate {
903 my ($self, $x, $y, $w, $h) = @_; 1021 my ($self, $w, $h) = @_;
904
905 $self->_size_allocate ($x, $y, $w, $h);
906 1022
907 my $children = $self->{children}; 1023 my $children = $self->{children};
908 1024
909 my @h = map +($_->size_request)[1], @$children; 1025 my @h = map $_->{req_h}, @$children;
910 1026
911 my $req_h = List::Util::sum @h; 1027 my $req_h = List::Util::sum @h;
912 1028
913 if ($req_h > $h) { 1029 if ($req_h > $h) {
914 # ah well, not enough space 1030 # ah well, not enough space
928 1044
929 my $y = 0; 1045 my $y = 0;
930 for (0 .. $#$children) { 1046 for (0 .. $#$children) {
931 my $child = $children->[$_]; 1047 my $child = $children->[$_];
932 my $h = $h[$_]; 1048 my $h = $h[$_];
933 $child->size_allocate (0, $y, $w, $h); 1049 $child->configure (0, $y, $w, $h);
934 1050
935 $y += $h; 1051 $y += $h;
936 } 1052 }
1053
1054 1
937} 1055}
938 1056
939############################################################################# 1057#############################################################################
940 1058
941package CFClient::UI::Label; 1059package CFClient::UI::Label;
942 1060
943our @ISA = CFClient::UI::Base::; 1061our @ISA = CFClient::UI::Base::;
944 1062
945use SDL::OpenGL; 1063use CFClient::OpenGL;
946 1064
947sub new { 1065sub new {
948 my ($class, %arg) = @_; 1066 my ($class, %arg) = @_;
949 1067
950 my $self = $class->SUPER::new ( 1068 my $self = $class->SUPER::new (
951 fg => [1, 1, 1], 1069 fg => [1, 1, 1],
952 fontsize => $::FONTSIZE, 1070 fontsize => 1,
953 text => "", 1071 text => "",
954 align => -1, 1072 align => -1,
1073 valign => -1,
955 padding => 2, 1074 padding => 2,
956 layout => new CFClient::Layout, 1075 layout => new CFClient::Layout,
1076 can_events => 0,
957 %arg 1077 %arg
958 ); 1078 );
959 1079
960 $self->set_text ($self->{text}); 1080 if (exists $self->{template}) {
1081 my $layout = new CFClient::Layout;
1082 $layout->set_text (delete $self->{template});
1083 $self->{template} = $layout;
1084 }
1085
1086 $self->set_text (delete $self->{text}) if exists $self->{text};
1087 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
961 1088
962 $self 1089 $self
963} 1090}
964 1091
965sub escape_text { 1092sub escape_text {
973} 1100}
974 1101
975sub set_text { 1102sub set_text {
976 my ($self, $text) = @_; 1103 my ($self, $text) = @_;
977 1104
978 $self->{text} = $text;
979 $self->{layout}->set_markup ($text); 1105 $self->{layout}->set_text ($text);
980 1106
981 delete $self->{texture}; 1107 delete $self->{texture};
982# $self->{w} = $self->{h} = -1; 1108 $self->check_size;
983 $self->update; 1109 $self->update;
984} 1110}
985 1111
986sub get_text { 1112sub set_markup {
987 my ($self, $text) = @_; 1113 my ($self, $markup) = @_;
988 1114
989 $self->{text} 1115 $self->{layout}->set_markup ($markup);
1116
1117 delete $self->{texture};
1118 $self->check_size;
1119 $self->update;
990} 1120}
991 1121
992sub size_request { 1122sub size_request {
993 my ($self) = @_; 1123 my ($self) = @_;
994 1124
995 $self->{layout}->set_width; 1125 $self->{layout}->set_width;
996 $self->{layout}->set_height ($self->{fontsize}); 1126 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1127
997 my ($w, $h) = $self->{layout}->size; 1128 my ($w, $h) = $self->{layout}->size;
1129
1130 if (exists $self->{template}) {
1131 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1132
1133 my ($w2, $h2) = $self->{template}->size;
1134
1135 $w = List::Util::max $w, $w2;
1136 $h = List::Util::max $h, $h2;
1137 }
998 1138
999 ( 1139 (
1000 $w + $self->{padding} * 2, 1140 $w + $self->{padding} * 2,
1001 $h + $self->{padding} * 2, 1141 $h + $self->{padding} * 2,
1002 ) 1142 )
1003} 1143}
1004 1144
1005sub size_allocate { 1145sub size_allocate {
1006 my ($self, $x, $y, $w, $h) = @_; 1146 my ($self, $w, $h) = @_;
1007
1008 $self->_size_allocate ($x, $y, $w, $h) or return;
1009 1147
1010 delete $self->{texture}; 1148 delete $self->{texture};
1011} 1149}
1012 1150
1013sub update { 1151sub set_fontsize {
1014 my ($self) = @_; 1152 my ($self, $fontsize) = @_;
1015 1153
1016 delete $self->{texture}; 1154 $self->{fontsize} = $fontsize;
1017 $self->SUPER::update; 1155 $self->check_size;
1018} 1156}
1019 1157
1020sub _draw { 1158sub _draw {
1021 my ($self) = @_; 1159 my ($self) = @_;
1022 1160
1023 my $tex = $self->{texture} ||= do { 1161 my $tex = $self->{texture} ||= do {
1024 $self->{layout}->set_width ($self->{w}); 1162 $self->{layout}->set_width ($self->{w});
1025 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1163 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1026 new_from_layout CFClient::Texture $self->{layout} 1164 new_from_layout CFClient::Texture $self->{layout}
1027 }; 1165 };
1028 1166
1029 glEnable GL_BLEND; 1167 glEnable GL_BLEND;
1030 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1168 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1031 glEnable GL_TEXTURE_2D; 1169 glEnable GL_TEXTURE_2D;
1032 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1170 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1033 1171
1034 glColor @{$self->{fg}}; 1172 glColor @{$self->{fg}};
1035 1173
1036 my $x = 1174 $self->{ox} = int (
1037 $self->{align} < 0 ? $self->{padding} 1175 $self->{align} < 0 ? $self->{padding}
1038 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1176 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1039 : ($self->{w} - $tex->{w}) * 0.5; 1177 : ($self->{w} - $tex->{w}) * 0.5
1178 );
1040 1179
1041 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1180 $self->{oy} = int (
1181 $self->{valign} < 0 ? $self->{padding}
1182 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1183 : ($self->{h} - $tex->{h}) * 0.5
1184 );
1185
1186 $tex->draw_quad ($self->{ox}, $self->{oy});
1042 1187
1043 glDisable GL_TEXTURE_2D; 1188 glDisable GL_TEXTURE_2D;
1044 glDisable GL_BLEND; 1189 glDisable GL_BLEND;
1045} 1190}
1046 1191
1047############################################################################# 1192#############################################################################
1048 1193
1049package CFClient::UI::Entry; 1194package CFClient::UI::EntryBase;
1050 1195
1051our @ISA = CFClient::UI::Label::; 1196our @ISA = CFClient::UI::Label::;
1052 1197
1053use SDL; 1198use CFClient::OpenGL;
1054use SDL::OpenGL;
1055 1199
1056sub new { 1200sub new {
1057 my $class = shift; 1201 my $class = shift;
1058 1202
1059 $class->SUPER::new ( 1203 $class->SUPER::new (
1061 bg => [0, 0, 0, 0.2], 1205 bg => [0, 0, 0, 0.2],
1062 active_bg => [1, 1, 1, 0.5], 1206 active_bg => [1, 1, 1, 0.5],
1063 active_fg => [0, 0, 0], 1207 active_fg => [0, 0, 0],
1064 can_hover => 1, 1208 can_hover => 1,
1065 can_focus => 1, 1209 can_focus => 1,
1210 valign => 0,
1211 can_events => 1,
1066 @_ 1212 @_
1067 ) 1213 )
1068} 1214}
1069 1215
1070sub _set_text { 1216sub _set_text {
1071 my ($self, $text) = @_; 1217 my ($self, $text) = @_;
1072 1218
1073 my $old_text = $self->{text}; 1219 delete $self->{cur_h};
1220
1221 return if $self->{text} eq $text;
1222
1223 delete $self->{texture};
1074 1224
1075 $self->{last_activity} = $::NOW; 1225 $self->{last_activity} = $::NOW;
1076
1077 $self->{text} = $text; 1226 $self->{text} = $text;
1078 $self->{layout}->set_width ($self->{w});
1079 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1080 1227
1081 $text =~ s/./*/g if $self->{hidden}; 1228 $text =~ s/./*/g if $self->{hidden};
1229 $self->{layout}->set_text ("$text ");
1082 1230
1083 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1231 $self->emit (changed => $self->{text});
1232}
1084 1233
1085 $text = substr $text, 0, $self->{cursor}; 1234sub get_text {
1086 utf8::encode $text; 1235 $_[0]{text}
1087
1088 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1089
1090 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1091 if $old_text ne $self->{text};
1092} 1236}
1093 1237
1094sub size_request { 1238sub size_request {
1095 my ($self) = @_; 1239 my ($self) = @_;
1096 1240
1098 1242
1099 ($w + 1, $h) # add 1 for cursor 1243 ($w + 1, $h) # add 1 for cursor
1100} 1244}
1101 1245
1102sub size_allocate { 1246sub size_allocate {
1103 my ($self, $x, $y, $w, $h) = @_; 1247 my ($self, $w, $h) = @_;
1104 1248
1105 $self->SUPER::size_allocate ($x, $y, $w, $h); 1249 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1106
1107 $self->_set_text ($self->{text});
1108} 1250}
1109 1251
1110sub set_text { 1252sub set_text {
1111 my ($self, $text) = @_; 1253 my ($self, $text) = @_;
1112 1254
1116} 1258}
1117 1259
1118sub key_down { 1260sub key_down {
1119 my ($self, $ev) = @_; 1261 my ($self, $ev) = @_;
1120 1262
1121 my $mod = $ev->key_mod; 1263 my $mod = $ev->{mod};
1122 my $sym = $ev->key_sym; 1264 my $sym = $ev->{sym};
1123
1124 my $uni = $ev->key_unicode; 1265 my $uni = $ev->{unicode};
1125 1266
1126 my $text = $self->get_text; 1267 my $text = $self->get_text;
1127 1268
1128 if ($sym == SDLK_BACKSPACE) { 1269 if ($sym == 8) {
1129 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1270 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1130 } elsif ($sym == SDLK_DELETE) { 1271 } elsif ($sym == 127) {
1131 substr $text, $self->{cursor}, 1, ""; 1272 substr $text, $self->{cursor}, 1, "";
1132 } elsif ($sym == SDLK_LEFT) { 1273 } elsif ($sym == CFClient::SDLK_LEFT) {
1133 --$self->{cursor} if $self->{cursor}; 1274 --$self->{cursor} if $self->{cursor};
1134 } elsif ($sym == SDLK_RIGHT) { 1275 } elsif ($sym == CFClient::SDLK_RIGHT) {
1135 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1276 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1136 } elsif ($sym == SDLK_HOME) { 1277 } elsif ($sym == CFClient::SDLK_HOME) {
1137 $self->{cursor} = 0; 1278 $self->{cursor} = 0;
1138 } elsif ($sym == SDLK_END) { 1279 } elsif ($sym == CFClient::SDLK_END) {
1139 $self->{cursor} = length $text; 1280 $self->{cursor} = length $text;
1140 } elsif ($sym == SDLK_ESCAPE) { 1281 } elsif ($sym == 27) {
1141 $self->emit ('escape'); 1282 $self->emit ('escape');
1142 } elsif ($uni) { 1283 } elsif ($uni) {
1143 substr $text, $self->{cursor}++, 0, chr $uni; 1284 substr $text, $self->{cursor}++, 0, chr $uni;
1144 } 1285 }
1145 1286
1200 1341
1201 $self->SUPER::_draw; 1342 $self->SUPER::_draw;
1202 1343
1203 #TODO: force update every cursor change :( 1344 #TODO: force update every cursor change :(
1204 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1345 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1346
1347 unless (exists $self->{cur_h}) {
1348 my $text = substr $self->{text}, 0, $self->{cursor};
1349 utf8::encode $text;
1350
1351 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1352 }
1353
1205 glColor @{$self->{fg}}; 1354 glColor @{$self->{fg}};
1206 glBegin GL_LINES; 1355 glBegin GL_LINES;
1207 glVertex $self->{cur_x}, $self->{cur_y};
1208 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1356 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1357 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1209 glEnd; 1358 glEnd;
1210 } 1359 }
1211} 1360}
1212 1361
1213package CFClient::UI::LineEntry; 1362package CFClient::UI::Entry;
1214 1363
1215our @ISA = CFClient::UI::Entry::; 1364our @ISA = CFClient::UI::EntryBase::;
1216 1365
1217use SDL; 1366use CFClient::OpenGL;
1218use SDL::OpenGL;
1219 1367
1220sub key_down { 1368sub key_down {
1221 my ($self, $ev) = @_; 1369 my ($self, $ev) = @_;
1222 1370
1223 my $sym = $ev->key_sym; 1371 my $sym = $ev->{sym};
1224 1372
1225 if ($sym == SDLK_RETURN) { 1373 if ($sym == 13) {
1226 $self->emit (activate => $self->get_text); 1374 $self->emit (activate => $self->get_text);
1227 $self->update; 1375 $self->update;
1228 1376
1229 } else { 1377 } else {
1230 $self->SUPER::key_down ($ev); 1378 $self->SUPER::key_down ($ev);
1236 1384
1237package CFClient::UI::Button; 1385package CFClient::UI::Button;
1238 1386
1239our @ISA = CFClient::UI::Label::; 1387our @ISA = CFClient::UI::Label::;
1240 1388
1241use SDL; 1389use CFClient::OpenGL;
1242use SDL::OpenGL;
1243 1390
1244my @tex = 1391my @tex =
1245 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1392 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1246 qw(b1_button_active.png); 1393 qw(b1_button_active.png);
1247 1394
1248sub new { 1395sub new {
1249 my $class = shift; 1396 my $class = shift;
1250 1397
1251 $class->SUPER::new ( 1398 $class->SUPER::new (
1252 padding => 4, 1399 padding => 4,
1253 fg => [1, 1, 1], 1400 fg => [1, 1, 1],
1254 bg => [1, 1, 1, 0.2], 1401 bg => [1, 1, 1, 0.2],
1255 active_fg => [1, 1, 0], 1402 active_fg => [0, 0, 1],
1256 can_hover => 1, 1403 can_hover => 1,
1404 align => 0,
1405 valign => 0,
1406 can_events => 1,
1257 @_ 1407 @_
1258 ) 1408 )
1259} 1409}
1260 1410
1261sub button_up { 1411sub button_up {
1269 1419
1270sub _draw { 1420sub _draw {
1271 my ($self) = @_; 1421 my ($self) = @_;
1272 1422
1273 local $self->{fg} = $self->{fg}; 1423 local $self->{fg} = $self->{fg};
1274 my $tex = $tex[0];
1275
1276 glEnable GL_BLEND;
1277 glEnable GL_TEXTURE_2D;
1278 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1279 1424
1280 if ($GRAB == $self) { 1425 if ($GRAB == $self) {
1281 $self->{fg} = $self->{active_fg}; 1426 $self->{fg} = $self->{active_fg};
1282 } 1427 }
1283 1428
1284 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1429 glEnable GL_BLEND;
1430 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1431 glEnable GL_TEXTURE_2D;
1285 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1432 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1433 glColor 0, 0, 0, 1;
1286 1434
1287 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1435 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1288 1436
1289 glDisable GL_TEXTURE_2D; 1437 glDisable GL_TEXTURE_2D;
1290 glDisable GL_BLEND; 1438 glDisable GL_BLEND;
1291 1439
1292 $self->SUPER::_draw; 1440 $self->SUPER::_draw;
1297package CFClient::UI::CheckBox; 1445package CFClient::UI::CheckBox;
1298 1446
1299our @ISA = CFClient::UI::DrawBG::; 1447our @ISA = CFClient::UI::DrawBG::;
1300 1448
1301my @tex = 1449my @tex =
1302 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1303 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1451 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1304 1452
1305use SDL; 1453use CFClient::OpenGL;
1306use SDL::OpenGL;
1307 1454
1308sub new { 1455sub new {
1309 my $class = shift; 1456 my $class = shift;
1310 1457
1311 $class->SUPER::new ( 1458 $class->SUPER::new (
1322 my ($self) = @_; 1469 my ($self) = @_;
1323 1470
1324 ($self->{padding} * 2 + 6) x 2 1471 ($self->{padding} * 2 + 6) x 2
1325} 1472}
1326 1473
1327sub size_allocate {
1328 my ($self, $x, $y, $w, $h) = @_;
1329
1330 $self->_size_allocate ($x, $y, $w, $h);
1331}
1332
1333sub button_down { 1474sub button_down {
1334 my ($self, $ev, $x, $y) = @_; 1475 my ($self, $ev, $x, $y) = @_;
1335 1476
1336 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1477 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1337 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1478 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1363 glDisable GL_BLEND; 1504 glDisable GL_BLEND;
1364} 1505}
1365 1506
1366############################################################################# 1507#############################################################################
1367 1508
1509package CFClient::UI::Image;
1510
1511our @ISA = CFClient::UI::Base::;
1512
1513use CFClient::OpenGL;
1514use Carp qw/confess/;
1515
1516our %loaded_images;
1517
1518sub new {
1519 my $class = shift;
1520
1521 my $self = $class->SUPER::new (can_events => 0, @_);
1522
1523 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1524
1525 $loaded_images{$self->{image}} ||=
1526 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1527
1528 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1529
1530 Scalar::Util::weaken $loaded_images{$self->{image}};
1531
1532 $self->{aspect} = $tex->{w} / $tex->{h};
1533
1534 $self
1535}
1536
1537sub size_request {
1538 my ($self) = @_;
1539
1540 ($self->{tex}->{w}, $self->{tex}->{h})
1541}
1542
1543sub _draw {
1544 my ($self) = @_;
1545
1546 my $tex = $self->{tex};
1547
1548 my ($w, $h) = ($self->{w}, $self->{h});
1549
1550 if ($self->{rot90}) {
1551 glRotate 90, 0, 0, 1;
1552 glTranslate 0, -$self->{w}, 0;
1553
1554 ($w, $h) = ($h, $w);
1555 }
1556
1557 glEnable GL_BLEND;
1558 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1559 glEnable GL_TEXTURE_2D;
1560 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1561
1562 $tex->draw_quad (0, 0, $w, $h);
1563
1564 glDisable GL_BLEND;
1565 glDisable GL_TEXTURE_2D;
1566}
1567
1568#############################################################################
1569
1570package CFClient::UI::VGauge;
1571
1572our @ISA = CFClient::UI::Base::;
1573
1574use CFClient::OpenGL;
1575
1576my %tex = (
1577 food => [
1578 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1579 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1580 ],
1581 grace => [
1582 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1583 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1584 ],
1585 hp => [
1586 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1587 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1588 ],
1589 mana => [
1590 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1591 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1592 ],
1593);
1594
1595# eg. VGauge->new (gauge => 'food'), default gauge: food
1596sub new {
1597 my $class = shift;
1598
1599 my $self = $class->SUPER::new (
1600 type => 'food',
1601 @_
1602 );
1603
1604 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1605
1606 $self
1607}
1608
1609sub size_request {
1610 my ($self) = @_;
1611
1612 #my $tex = $tex{$self->{type}}[0];
1613 #@$tex{qw(w h)}
1614 (0, 0)
1615}
1616
1617sub set_max {
1618 my ($self, $max) = @_;
1619
1620 $self->{max_val} = $max;
1621}
1622
1623sub set_value {
1624 my ($self, $val, $max) = @_;
1625
1626 $self->set_max ($max)
1627 if defined $max;
1628
1629 $max = $self->{max_val};
1630 $self->{val} = $val;
1631
1632 $self->update;
1633}
1634
1635sub _draw {
1636 my ($self) = @_;
1637
1638 my $tex = $tex{$self->{type}};
1639
1640 my ($w, $h) = ($self->{w}, $self->{h});
1641
1642 if ($self->{vertical}) {
1643 glRotate 90, 0, 0, 1;
1644 glTranslate 0, -$self->{w}, 0;
1645
1646 ($w, $h) = ($h, $w);
1647 }
1648
1649 my $ycut = $self->{val} / ($self->{max_val} || 1);
1650 $ycut = 1 if $self->{val} > $self->{max_val};
1651
1652 my $t1 = $tex->[0];
1653 my $t2 = $tex->[1];
1654
1655 glEnable GL_BLEND;
1656 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1657 glEnable GL_TEXTURE_2D;
1658 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1659
1660 my $h1 = $self->{h} - $ycut * $self->{h};
1661 my $h2 = $ycut * $self->{h};
1662
1663 glBindTexture GL_TEXTURE_2D, $t1->{name};
1664 glBegin GL_QUADS;
1665 glTexCoord 0 , 0; glVertex 0 , 0;
1666 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1667 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1668 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1669 glEnd;
1670
1671 glBindTexture GL_TEXTURE_2D, $t2->{name};
1672 glBegin GL_QUADS;
1673 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1674 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1675 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1676 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1677 glEnd;
1678
1679 glDisable GL_BLEND;
1680 glDisable GL_TEXTURE_2D;
1681}
1682
1683#############################################################################
1684
1685package CFClient::UI::Gauge;
1686
1687our @ISA = CFClient::UI::VBox::;
1688
1689sub new {
1690 my ($class, %arg) = shift;
1691
1692 my $self = $class->SUPER::new (
1693 @_,
1694 );
1695
1696 $self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1697 $self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999";
1698 $self->add ($self->{value});
1699 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1700 $self->add ($self->{max});
1701
1702 $self
1703}
1704
1705sub size_request {
1706 my ($self) = @_;
1707 (($self->{max}->size_request)[0], 0)
1708}
1709
1710sub set_fontsize {
1711 my ($self, $fsize) = @_;
1712
1713 $self->{value}->set_fontsize ($fsize);
1714 $self->{max} ->set_fontsize ($fsize);
1715}
1716
1717sub set_value {
1718 my ($self, $val, $max) = @_;
1719
1720 $self->set_max ($max)
1721 if defined $max;
1722
1723 $self->{gauge}->set_value ($val, $max);
1724 $self->{value}->set_text ($val);
1725}
1726
1727sub set_max {
1728 my ($self, $max) = @_;
1729
1730 $self->{gauge}->set_max ($max);
1731 $self->{max}->set_text ($max);
1732}
1733
1734#############################################################################
1735
1368package CFClient::UI::Slider; 1736package CFClient::UI::Slider;
1369 1737
1370use strict; 1738use strict;
1371 1739
1372use SDL::OpenGL; 1740use CFClient::OpenGL;
1373 1741
1374our @ISA = CFClient::UI::DrawBG::; 1742our @ISA = CFClient::UI::DrawBG::;
1375 1743
1376my @tex = 1744my @tex =
1377 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1745 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1387 # TODO: calculations are off 1755 # TODO: calculations are off
1388 my $self = $class->SUPER::new ( 1756 my $self = $class->SUPER::new (
1389 fg => [1, 1, 1], 1757 fg => [1, 1, 1],
1390 active_fg => [0, 0, 0], 1758 active_fg => [0, 0, 0],
1391 range => [0, 0, 100, 10], 1759 range => [0, 0, 100, 10],
1392 req_w => 40, 1760 req_w => 20,
1393 req_h => 13, 1761 req_h => 20,
1394 vertical => 0, 1762 vertical => 0,
1395 can_hover => 1, 1763 can_hover => 1,
1396 inner_pad => 5, 1764 inner_pad => 5,
1397 @_ 1765 @_
1398 ); 1766 );
1502 1870
1503package CFClient::UI::TextView; 1871package CFClient::UI::TextView;
1504 1872
1505our @ISA = CFClient::UI::HBox::; 1873our @ISA = CFClient::UI::HBox::;
1506 1874
1507use SDL::OpenGL; 1875use CFClient::OpenGL;
1508 1876
1509sub new { 1877sub new {
1510 my $class = shift; 1878 my $class = shift;
1511 1879
1512 my $self = $class->SUPER::new ( 1880 my $self = $class->SUPER::new (
1513 req_w => $::WIDTH / 6,
1514 req_h => $::HEIGHT / 6,
1515 fontsize => $::FONTSIZE, 1881 fontsize => 1,
1516 @_, 1882 @_,
1517 1883
1518 layout => (new CFClient::Layout), 1884 layout => (new CFClient::Layout),
1519 par => [], 1885 par => [],
1520 height => 0, 1886 height => 0,
1541sub text_height { 1907sub text_height {
1542 my ($self, $text) = @_; 1908 my ($self, $text) = @_;
1543 1909
1544 my $layout = $self->{layout}; 1910 my $layout = $self->{layout};
1545 1911
1546 $layout->set_height ($self->{fontsize}); 1912 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1547 $layout->set_width ($self->{w}); 1913 $layout->set_width ($self->{w});
1548 $layout->set_text ($text); 1914 $layout->set_text ($text);
1549 1915
1550 ($layout->size)[1] 1916 ($layout->size)[1]
1551} 1917}
1555 1921
1556 $self->{need_reflow}++; 1922 $self->{need_reflow}++;
1557 $self->update; 1923 $self->update;
1558} 1924}
1559 1925
1560sub size_request {
1561 my ($self) = @_;
1562
1563 ($self->{req_w}, $self->{req_h})
1564}
1565
1566sub size_allocate { 1926sub size_allocate {
1567 my ($self, $x, $y, $w, $h) = @_; 1927 my ($self, $w, $h) = @_;
1568 1928
1569 $self->SUPER::size_allocate ($x, $y, $w, $h); 1929 $self->SUPER::size_allocate ($w, $h);
1570 1930
1571 $self->{layout}->set_height ($self->{fontsize}); 1931 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1572 $self->{layout}->set_width ($self->{w}); 1932 $self->{layout}->set_width ($self->{children}[0]{w});
1573 1933
1574 $self->reflow; 1934 $self->reflow;
1575} 1935}
1576 1936
1577sub add_paragraph { 1937sub add_paragraph {
1664 2024
1665} 2025}
1666 2026
1667############################################################################# 2027#############################################################################
1668 2028
1669package CFClient::UI::MapWidget;
1670
1671use strict;
1672
1673use List::Util qw(min max);
1674
1675use SDL;
1676use SDL::OpenGL;
1677
1678our @ISA = CFClient::UI::Base::;
1679
1680sub new {
1681 my $class = shift;
1682
1683 $class->SUPER::new (
1684 z => -1,
1685 can_focus => 1,
1686 list => (glGenLists 1),
1687 @_
1688 )
1689}
1690
1691sub key_down {
1692 print "MAPKEYDOWN\n";
1693}
1694
1695sub key_up {
1696}
1697
1698sub button_down {
1699 my ($self, $ev, $x, $y) = @_;
1700
1701 $self->focus_in;
1702
1703 if ($ev->button == 2) {
1704 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1705 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1706
1707 $self->{motion} = sub {
1708 my ($ev, $x, $y) = @_;
1709
1710 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1711
1712 $::CFG->{map_shift_x} = $bw + $x - $ox;
1713 $::CFG->{map_shift_y} = $bh + $y - $oy;
1714
1715 $self->update;
1716 };
1717 }
1718}
1719
1720sub button_up {
1721 my ($self, $ev, $x, $y) = @_;
1722
1723 delete $self->{motion};
1724}
1725
1726sub mouse_motion {
1727 my ($self, $ev, $x, $y) = @_;
1728
1729 $self->{motion}->($ev, $x, $y) if $self->{motion};
1730}
1731
1732sub size_request {
1733 (
1734 1 + 32 * int $::WIDTH / 32,
1735 1 + 32 * int $::HEIGHT / 32,
1736 )
1737}
1738
1739sub update {
1740 my ($self) = @_;
1741
1742 $self->{need_update} = 1;
1743 $self->SUPER::update;
1744}
1745
1746sub draw {
1747 my ($self) = @_;
1748
1749 if (delete $self->{need_update}) {
1750 glNewList $self->{list}, GL_COMPILE;
1751
1752 if ($::MAP) {
1753 my $sw = int $::WIDTH / 32;
1754 my $sh = int $::HEIGHT / 32;
1755
1756 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1757 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1758
1759 glTranslate $sx0 - 32, $sy0 - 32, 0;
1760
1761 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1762
1763 if ($::CFG->{fow_enable}) {
1764 if ($::CFG->{fow_smooth}) { # smooth fog of war
1765 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1766 glConvolutionFilter2D
1767 GL_CONVOLUTION_2D,
1768 GL_ALPHA,
1769 3, 3,
1770 GL_ALPHA, GL_FLOAT,
1771 pack "f*",
1772 0.1, 0.1, 0.1,
1773 0.1, 0.2, 0.1,
1774 0.1, 0.1, 0.1,
1775 ;
1776 glEnable GL_CONVOLUTION_2D;
1777 }
1778
1779 my $tex = new CFClient::Texture
1780 w => $w,
1781 h => $h,
1782 data => $data,
1783 internalformat => GL_ALPHA,
1784 format => GL_ALPHA;
1785
1786 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1787
1788 glEnable GL_BLEND;
1789 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1790 glEnable GL_TEXTURE_2D;
1791 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1792
1793 glColor +($::CFG->{fow_intensity}) x 3, 1;
1794 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1795
1796 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND;
1798 }
1799
1800 # HACK BEGIN
1801 {
1802 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1803 my ($w, $h) = (250, 250);
1804
1805 glEnable GL_BLEND;
1806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1807 glEnable GL_TEXTURE_2D;
1808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1809
1810 CFClient::Texture->new (
1811 w => $w,
1812 h => $h,
1813 data => $::MAP->mapmap ($w, $h),
1814 type => GL_UNSIGNED_INT_8_8_8_8_REV
1815 )->draw_quad (100, 100);
1816
1817 glDisable GL_TEXTURE_2D;
1818 glDisable GL_BLEND;
1819 }
1820 # HACK END
1821 }
1822
1823 glEndList;
1824 }
1825
1826 glPushMatrix;
1827 glCallList $self->{list};
1828 glPopMatrix;
1829
1830 if ($FOCUS != $self) {
1831 glColor 64/255, 64/255, 64/255;
1832 glLogicOp GL_AND;
1833 glEnable GL_COLOR_LOGIC_OP;
1834 glBegin GL_QUADS;
1835 glVertex 0, 0;
1836 glVertex 0, $::HEIGHT;
1837 glVertex $::WIDTH, $::HEIGHT;
1838 glVertex $::WIDTH, 0;
1839 glEnd;
1840 glDisable GL_COLOR_LOGIC_OP;
1841 }
1842}
1843
1844my %DIR = (
1845 SDLK_KP8, [1, "north"],
1846 SDLK_KP9, [2, "northeast"],
1847 SDLK_KP6, [3, "east"],
1848 SDLK_KP3, [4, "southeast"],
1849 SDLK_KP2, [5, "south"],
1850 SDLK_KP1, [6, "southwest"],
1851 SDLK_KP4, [7, "west"],
1852 SDLK_KP7, [8, "northwest"],
1853
1854 SDLK_UP, [1, "north"],
1855 SDLK_RIGHT, [3, "east"],
1856 SDLK_DOWN, [5, "south"],
1857 SDLK_LEFT, [7, "west"],
1858);
1859
1860sub key_down {
1861 my ($self, $ev) = @_;
1862
1863 my $mod = $ev->key_mod;
1864 my $sym = $ev->key_sym;
1865
1866 if ($sym == SDLK_KP5) {
1867 $::CONN->user_send ("command stay fire");
1868 } elsif ($sym == SDLK_a) {
1869 $::CONN->user_send ("command apply");
1870 } elsif ($sym == SDLK_QUOTE) {
1871 $self->emit ('activate_console');
1872 } elsif ($sym == SDLK_SLASH) {
1873 $self->emit ('activate_console' => '/');
1874 } elsif (exists $DIR{$sym}) {
1875 if ($mod & KMOD_SHIFT) {
1876 $self->{shft}++;
1877 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1878 } elsif ($mod & KMOD_CTRL) {
1879 $self->{ctrl}++;
1880 $::CONN->user_send ("command run $DIR{$sym}[0]");
1881 } else {
1882 $::CONN->user_send ("command $DIR{$sym}[1]");
1883 }
1884 }
1885}
1886
1887sub key_up {
1888 my ($self, $ev) = @_;
1889
1890 my $mod = $ev->key_mod;
1891 my $sym = $ev->key_sym;
1892
1893 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1894 $::CONN->user_send ("command fire_stop");
1895 }
1896 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1897 $::CONN->user_send ("command run_stop");
1898 }
1899}
1900
1901#############################################################################
1902
1903package CFClient::UI::Animator; 2029package CFClient::UI::Animator;
1904 2030
1905use SDL::OpenGL; 2031use CFClient::OpenGL;
1906 2032
1907our @ISA = CFClient::UI::Bin::; 2033our @ISA = CFClient::UI::Bin::;
1908 2034
1909sub moveto { 2035sub moveto {
1910 my ($self, $x, $y) = @_; 2036 my ($self, $x, $y) = @_;
1950 my $class = shift; 2076 my $class = shift;
1951 2077
1952 my $self = $class->SUPER::new ( 2078 my $self = $class->SUPER::new (
1953 state => 0, 2079 state => 0,
1954 connect_activate => \&toggle_flopper, 2080 connect_activate => \&toggle_flopper,
2081 can_events => 1,
1955 @_ 2082 @_
1956 ); 2083 );
1957 2084
1958 if ($self->{state}) { 2085 if ($self->{state}) {
1959 $self->{state} = 0; 2086 $self->{state} = 0;
1983 2110
1984package CFClient::UI::Root; 2111package CFClient::UI::Root;
1985 2112
1986our @ISA = CFClient::UI::Container::; 2113our @ISA = CFClient::UI::Container::;
1987 2114
1988use SDL::OpenGL; 2115use CFClient::OpenGL;
2116
2117sub check_size {
2118 my ($self) = @_;
2119
2120 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2121}
1989 2122
1990sub size_request { 2123sub size_request {
1991 ($::WIDTH, $::HEIGHT) 2124 ($::WIDTH, $::HEIGHT)
1992} 2125}
1993 2126
1994sub size_allocate { 2127sub configure {
1995 my ($self, $x, $y, $w, $h) = @_; 2128 my ($self, $x, $y, $w, $h) = @_;
1996 2129
1997 $self->_size_allocate ($x, $y, $w, $h); 2130 $self->SUPER::configure ($x, $y, $w, $h);
1998 2131
1999 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2000 for @{$self->{children}}; 2132 for my $child (@{$self->{children}}) {
2133 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2134
2135 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2136 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2137 $child->configure ($X, $Y, $W,$H);
2138 }
2001} 2139}
2002 2140
2003sub _topleft { 2141sub _topleft {
2004 my ($self, $x, $y) = @_; 2142 my ($self, $x, $y) = @_;
2005 2143
2007} 2145}
2008 2146
2009sub update { 2147sub update {
2010 my ($self) = @_; 2148 my ($self) = @_;
2011 2149
2012 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2150 $self->check_size;
2013 ::refresh (); 2151 ::refresh ();
2014} 2152}
2015 2153
2016sub add { 2154sub add {
2017 my ($self, $widget) = @_; 2155 my ($self, $child) = @_;
2018 2156
2157 # integerize window positions
2158 $child->{x} = int $child->{x};
2159 $child->{y} = int $child->{y};
2160
2019 $self->SUPER::add ($widget); 2161 $self->SUPER::add ($child);
2020
2021 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2022} 2162}
2023 2163
2024sub on_refresh { 2164sub on_refresh {
2025 my ($self, $id, $cb) = @_; 2165 my ($self, $id, $cb) = @_;
2026 2166

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines