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.119 by root, Sun Apr 16 07:25:30 2006 UTC vs.
Revision 1.151 by root, Sun Apr 23 00:57:39 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines