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.114 by root, Sat Apr 15 13:56:25 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
74sub harmonize { 102sub harmonize {
75 my ($vals) = @_; 103 my ($vals) = @_;
76 104
77 my $rem = 0; 105 my $rem = 0;
78 106
79 for ($vals) { 107 for (@$vals) {
80 my $i = int $_ + $rem; 108 my $i = int $_ + $rem;
81 $rem += $_ - $i; 109 $rem += $_ - $i;
82 $_ = $i; 110 $_ = $i;
83 } 111 }
84} 112}
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} = $x; 161 $self->{x} = int $x;
118 $self->{y} = $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 => $::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;
859}
860
861# TODO: move to container class maybe? send childs a signal on removal?
862sub clear {
863 my ($self) = @_;
864
865 delete $self->{children};
709 $self->update; 866 $self->update;
710} 867}
711 868
712sub get_wh { 869sub get_wh {
713 my ($self) = @_; 870 my ($self) = @_;
719 or next; 876 or next;
720 877
721 for my $x (0 .. $#$row) { 878 for my $x (0 .. $#$row) {
722 my $widget = $row->[$x] 879 my $widget = $row->[$x]
723 or next; 880 or next;
724 my ($w, $h) = $widget->size_request; 881 my ($w, $h) = @$widget{qw(req_w req_h)};
725 882
726 $w[$x] = max $w[$x], $w; 883 $w[$x] = max $w[$x], $w;
727 $h[$y] = max $h[$y], $h; 884 $h[$y] = max $h[$y], $h;
728 } 885 }
729 } 886 }
741 (sum @$hs), 898 (sum @$hs),
742 ) 899 )
743} 900}
744 901
745sub size_allocate { 902sub size_allocate {
746 my ($self, $x, $y, $w, $h) = @_; 903 my ($self, $w, $h) = @_;
747
748 $self->_size_allocate ($x, $y, $w, $h) or return;
749 904
750 my ($ws, $hs) = $self->get_wh; 905 my ($ws, $hs) = $self->get_wh;
751 906
752 my $req_w = sum @$ws; 907 my $req_w = sum @$ws;
753 my $req_h = sum @$hs; 908 my $req_h = sum @$hs;
775 930
776 for my $c (0 .. $#$row) { 931 for my $c (0 .. $#$row) {
777 my $col_w = $ws->[$c]; 932 my $col_w = $ws->[$c];
778 933
779 if (my $widget = $row->[$c]) { 934 if (my $widget = $row->[$c]) {
780 $widget->size_allocate ($x, $y, $col_w, $row_h); 935 $widget->configure ($x, $y, $col_w, $row_h);
781 } 936 }
782 937
783 $x += $col_w; 938 $x += $col_w;
784 } 939 }
785 940
830 (List::Util::max map $_->[1], @alloc), 985 (List::Util::max map $_->[1], @alloc),
831 ) 986 )
832} 987}
833 988
834sub size_allocate { 989sub size_allocate {
835 my ($self, $x, $y, $w, $h) = @_; 990 my ($self, $w, $h) = @_;
836
837 $self->_size_allocate ($x, $y, $w, $h);
838 991
839 ($h, $w) = ($w, $h); 992 ($h, $w) = ($w, $h);
840 993
841 my $children = $self->{children}; 994 my $children = $self->{children};
842 995
843 my @h = map +($_->size_request)[0], @$children; 996 my @h = map $_->{req_w}, @$children;
844 997
845 my $req_h = List::Util::sum @h; 998 my $req_h = List::Util::sum @h;
846 999
847 if ($req_h > $h) { 1000 if ($req_h > $h) {
848 # ah well, not enough space 1001 # ah well, not enough space
864 1017
865 my $y = 0; 1018 my $y = 0;
866 for (0 .. $#$children) { 1019 for (0 .. $#$children) {
867 my $child = $children->[$_]; 1020 my $child = $children->[$_];
868 my $h = $h[$_]; 1021 my $h = $h[$_];
869 $child->size_allocate ($y, 0, $h, $w); 1022 $child->configure ($y, 0, $h, $w);
870 1023
871 $y += $h; 1024 $y += $h;
872 } 1025 }
1026
1027 1
873} 1028}
874 1029
875############################################################################# 1030#############################################################################
876 1031
877package CFClient::UI::VBox; 1032package CFClient::UI::VBox;
890 (List::Util::sum map $_->[1], @alloc), 1045 (List::Util::sum map $_->[1], @alloc),
891 ) 1046 )
892} 1047}
893 1048
894sub size_allocate { 1049sub size_allocate {
895 my ($self, $x, $y, $w, $h) = @_; 1050 my ($self, $w, $h) = @_;
896
897 $self->_size_allocate ($x, $y, $w, $h);
898 1051
899 my $children = $self->{children}; 1052 my $children = $self->{children};
900 1053
901 my @h = map +($_->size_request)[1], @$children; 1054 my @h = map $_->{req_h}, @$children;
902 1055
903 my $req_h = List::Util::sum @h; 1056 my $req_h = List::Util::sum @h;
904 1057
905 if ($req_h > $h) { 1058 if ($req_h > $h) {
906 # ah well, not enough space 1059 # ah well, not enough space
920 1073
921 my $y = 0; 1074 my $y = 0;
922 for (0 .. $#$children) { 1075 for (0 .. $#$children) {
923 my $child = $children->[$_]; 1076 my $child = $children->[$_];
924 my $h = $h[$_]; 1077 my $h = $h[$_];
925 $child->size_allocate (0, $y, $w, $h); 1078 $child->configure (0, $y, $w, $h);
926 1079
927 $y += $h; 1080 $y += $h;
928 } 1081 }
1082
1083 1
929} 1084}
930 1085
931############################################################################# 1086#############################################################################
932 1087
933package CFClient::UI::Label; 1088package CFClient::UI::Label;
934 1089
935our @ISA = CFClient::UI::Base::; 1090our @ISA = CFClient::UI::Base::;
936 1091
937use SDL::OpenGL; 1092use CFClient::OpenGL;
938 1093
939sub new { 1094sub new {
940 my ($class, %arg) = @_; 1095 my ($class, %arg) = @_;
941 1096
942 my $self = $class->SUPER::new ( 1097 my $self = $class->SUPER::new (
943 fg => [1, 1, 1], 1098 fg => [1, 1, 1],
944 fontsize => $::FONTSIZE, 1099 fontsize => 1,
945 text => "", 1100 text => "",
946 align => -1, 1101 align => -1,
1102 valign => -1,
947 padding => 2, 1103 padding => 2,
948 layout => new CFClient::Layout, 1104 layout => new CFClient::Layout,
1105 can_events => 0,
949 %arg 1106 %arg
950 ); 1107 );
951 1108
952 $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};
953 1117
954 $self 1118 $self
955} 1119}
956 1120
957sub escape_text { 1121sub escape_text {
965} 1129}
966 1130
967sub set_text { 1131sub set_text {
968 my ($self, $text) = @_; 1132 my ($self, $text) = @_;
969 1133
970 $self->{text} = $text;
971 $self->{layout}->set_markup ($text); 1134 $self->{layout}->set_text ($text);
972 1135
973 delete $self->{texture}; 1136 delete $self->{texture};
974# $self->{w} = $self->{h} = -1; 1137 $self->check_size;
975 $self->update; 1138 $self->update;
976} 1139}
977 1140
978sub get_text { 1141sub set_markup {
979 my ($self, $text) = @_; 1142 my ($self, $markup) = @_;
980 1143
981 $self->{text} 1144 $self->{layout}->set_markup ($markup);
1145
1146 delete $self->{texture};
1147 $self->check_size;
1148 $self->update;
982} 1149}
983 1150
984sub size_request { 1151sub size_request {
985 my ($self) = @_; 1152 my ($self) = @_;
986 1153
987 $self->{layout}->set_width; 1154 $self->{layout}->set_width;
988 $self->{layout}->set_height ($self->{fontsize}); 1155 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1156
989 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 }
990 1167
991 ( 1168 (
992 $w + $self->{padding} * 2, 1169 $w + $self->{padding} * 2,
993 $h + $self->{padding} * 2, 1170 $h + $self->{padding} * 2,
994 ) 1171 )
995} 1172}
996 1173
997sub size_allocate { 1174sub size_allocate {
998 my ($self, $x, $y, $w, $h) = @_; 1175 my ($self, $w, $h) = @_;
999
1000 $self->_size_allocate ($x, $y, $w, $h) or return;
1001 1176
1002 delete $self->{texture}; 1177 delete $self->{texture};
1003} 1178}
1004 1179
1005sub update { 1180sub set_fontsize {
1006 my ($self) = @_; 1181 my ($self, $fontsize) = @_;
1007 1182
1008 delete $self->{texture}; 1183 $self->{fontsize} = $fontsize;
1009 $self->SUPER::update; 1184 $self->check_size;
1010} 1185}
1011 1186
1012sub _draw { 1187sub _draw {
1013 my ($self) = @_; 1188 my ($self) = @_;
1014 1189
1015 my $tex = $self->{texture} ||= do { 1190 my $tex = $self->{texture} ||= do {
1016 $self->{layout}->set_width ($self->{w}); 1191 $self->{layout}->set_width ($self->{w});
1017 $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);
1018 new_from_layout CFClient::Texture $self->{layout} 1193 new_from_layout CFClient::Texture $self->{layout}
1019 }; 1194 };
1020 1195
1021 glEnable GL_BLEND; 1196 glEnable GL_BLEND;
1022 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1197 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1023 glEnable GL_TEXTURE_2D; 1198 glEnable GL_TEXTURE_2D;
1024 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1199 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1025 1200
1026 glColor @{$self->{fg}}; 1201 glColor @{$self->{fg}};
1027 1202
1028 my $x = 1203 $self->{ox} = int (
1029 $self->{align} < 0 ? $self->{padding} 1204 $self->{align} < 0 ? $self->{padding}
1030 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1205 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1031 : ($self->{w} - $tex->{w}) * 0.5; 1206 : ($self->{w} - $tex->{w}) * 0.5
1207 );
1032 1208
1033 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1209 $self->{oy} = int (
1210 $self->{valign} < 0 ? $self->{padding}
1211 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1212 : ($self->{h} - $tex->{h}) * 0.5
1213 );
1214
1215 $tex->draw_quad ($self->{ox}, $self->{oy});
1034 1216
1035 glDisable GL_TEXTURE_2D; 1217 glDisable GL_TEXTURE_2D;
1036 glDisable GL_BLEND; 1218 glDisable GL_BLEND;
1037} 1219}
1038 1220
1039############################################################################# 1221#############################################################################
1040 1222
1041package CFClient::UI::Entry; 1223package CFClient::UI::EntryBase;
1042 1224
1043our @ISA = CFClient::UI::Label::; 1225our @ISA = CFClient::UI::Label::;
1044 1226
1045use SDL; 1227use CFClient::OpenGL;
1046use SDL::OpenGL;
1047 1228
1048sub new { 1229sub new {
1049 my $class = shift; 1230 my $class = shift;
1050 1231
1051 $class->SUPER::new ( 1232 $class->SUPER::new (
1053 bg => [0, 0, 0, 0.2], 1234 bg => [0, 0, 0, 0.2],
1054 active_bg => [1, 1, 1, 0.5], 1235 active_bg => [1, 1, 1, 0.5],
1055 active_fg => [0, 0, 0], 1236 active_fg => [0, 0, 0],
1056 can_hover => 1, 1237 can_hover => 1,
1057 can_focus => 1, 1238 can_focus => 1,
1239 valign => 0,
1240 can_events => 1,
1058 @_ 1241 @_
1059 ) 1242 )
1060} 1243}
1061 1244
1062sub _set_text { 1245sub _set_text {
1063 my ($self, $text) = @_; 1246 my ($self, $text) = @_;
1064 1247
1065 my $old_text = $self->{text}; 1248 delete $self->{cur_h};
1249
1250 return if $self->{text} eq $text;
1251
1252 delete $self->{texture};
1066 1253
1067 $self->{last_activity} = $::NOW; 1254 $self->{last_activity} = $::NOW;
1068
1069 $self->{text} = $text; 1255 $self->{text} = $text;
1070 $self->{layout}->set_width ($self->{w});
1071 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1072 1256
1073 $text =~ s/./*/g if $self->{hidden}; 1257 $text =~ s/./*/g if $self->{hidden};
1258 $self->{layout}->set_text ("$text ");
1074 1259
1075 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1260 $self->emit (changed => $self->{text});
1261}
1076 1262
1077 $text = substr $text, 0, $self->{cursor}; 1263sub get_text {
1078 utf8::encode $text; 1264 $_[0]{text}
1079
1080 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1081
1082 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1083 if $old_text ne $self->{text};
1084} 1265}
1085 1266
1086sub size_request { 1267sub size_request {
1087 my ($self) = @_; 1268 my ($self) = @_;
1088 1269
1090 1271
1091 ($w + 1, $h) # add 1 for cursor 1272 ($w + 1, $h) # add 1 for cursor
1092} 1273}
1093 1274
1094sub size_allocate { 1275sub size_allocate {
1095 my ($self, $x, $y, $w, $h) = @_; 1276 my ($self, $w, $h) = @_;
1096 1277
1097 $self->SUPER::size_allocate ($x, $y, $w, $h); 1278 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1098
1099 $self->_set_text ($self->{text});
1100} 1279}
1101 1280
1102sub set_text { 1281sub set_text {
1103 my ($self, $text) = @_; 1282 my ($self, $text) = @_;
1104 1283
1108} 1287}
1109 1288
1110sub key_down { 1289sub key_down {
1111 my ($self, $ev) = @_; 1290 my ($self, $ev) = @_;
1112 1291
1113 my $mod = $ev->key_mod; 1292 my $mod = $ev->{mod};
1114 my $sym = $ev->key_sym; 1293 my $sym = $ev->{sym};
1115
1116 my $uni = $ev->key_unicode; 1294 my $uni = $ev->{unicode};
1117 1295
1118 my $text = $self->get_text; 1296 my $text = $self->get_text;
1119 1297
1120 if ($sym == SDLK_BACKSPACE) { 1298 if ($sym == 8) {
1121 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1299 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1122 } elsif ($sym == SDLK_DELETE) { 1300 } elsif ($sym == 127) {
1123 substr $text, $self->{cursor}, 1, ""; 1301 substr $text, $self->{cursor}, 1, "";
1124 } elsif ($sym == SDLK_LEFT) { 1302 } elsif ($sym == CFClient::SDLK_LEFT) {
1125 --$self->{cursor} if $self->{cursor}; 1303 --$self->{cursor} if $self->{cursor};
1126 } elsif ($sym == SDLK_RIGHT) { 1304 } elsif ($sym == CFClient::SDLK_RIGHT) {
1127 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1305 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1128 } elsif ($sym == SDLK_HOME) { 1306 } elsif ($sym == CFClient::SDLK_HOME) {
1129 $self->{cursor} = 0; 1307 $self->{cursor} = 0;
1130 } elsif ($sym == SDLK_END) { 1308 } elsif ($sym == CFClient::SDLK_END) {
1131 $self->{cursor} = length $text; 1309 $self->{cursor} = length $text;
1132 } elsif ($sym == SDLK_ESCAPE) { 1310 } elsif ($sym == 27) {
1133 $self->emit ('escape'); 1311 $self->emit ('escape');
1134 } elsif ($uni) { 1312 } elsif ($uni) {
1135 substr $text, $self->{cursor}++, 0, chr $uni; 1313 substr $text, $self->{cursor}++, 0, chr $uni;
1136 } 1314 }
1137 1315
1192 1370
1193 $self->SUPER::_draw; 1371 $self->SUPER::_draw;
1194 1372
1195 #TODO: force update every cursor change :( 1373 #TODO: force update every cursor change :(
1196 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
1197 glColor @{$self->{fg}}; 1383 glColor @{$self->{fg}};
1198 glBegin GL_LINES; 1384 glBegin GL_LINES;
1199 glVertex $self->{cur_x}, $self->{cur_y};
1200 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};
1201 glEnd; 1387 glEnd;
1202 } 1388 }
1203} 1389}
1204 1390
1205package CFClient::UI::LineEntry; 1391package CFClient::UI::Entry;
1206 1392
1207our @ISA = CFClient::UI::Entry::; 1393our @ISA = CFClient::UI::EntryBase::;
1208 1394
1209use SDL; 1395use CFClient::OpenGL;
1210use SDL::OpenGL;
1211 1396
1212sub key_down { 1397sub key_down {
1213 my ($self, $ev) = @_; 1398 my ($self, $ev) = @_;
1214 1399
1215 my $sym = $ev->key_sym; 1400 my $sym = $ev->{sym};
1216 1401
1217 if ($sym == SDLK_RETURN) { 1402 if ($sym == 13) {
1218 $self->emit (activate => $self->get_text); 1403 $self->emit (activate => $self->get_text);
1219 $self->update; 1404 $self->update;
1220 1405
1221 } else { 1406 } else {
1222 $self->SUPER::key_down ($ev); 1407 $self->SUPER::key_down ($ev);
1228 1413
1229package CFClient::UI::Button; 1414package CFClient::UI::Button;
1230 1415
1231our @ISA = CFClient::UI::Label::; 1416our @ISA = CFClient::UI::Label::;
1232 1417
1233use SDL; 1418use CFClient::OpenGL;
1234use SDL::OpenGL;
1235 1419
1236my @tex = 1420my @tex =
1237 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1421 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1238 qw(b1_button_active.png); 1422 qw(b1_button_active.png);
1239 1423
1240sub new { 1424sub new {
1241 my $class = shift; 1425 my $class = shift;
1242 1426
1243 $class->SUPER::new ( 1427 $class->SUPER::new (
1244 padding => 4, 1428 padding => 4,
1245 fg => [1, 1, 1], 1429 fg => [1, 1, 1],
1246 bg => [1, 1, 1, 0.2], 1430 bg => [1, 1, 1, 0.2],
1247 active_fg => [1, 1, 0], 1431 active_fg => [0, 0, 1],
1248 can_hover => 1, 1432 can_hover => 1,
1433 align => 0,
1434 valign => 0,
1435 can_events => 1,
1249 @_ 1436 @_
1250 ) 1437 )
1251} 1438}
1252 1439
1253sub button_up { 1440sub button_up {
1261 1448
1262sub _draw { 1449sub _draw {
1263 my ($self) = @_; 1450 my ($self) = @_;
1264 1451
1265 local $self->{fg} = $self->{fg}; 1452 local $self->{fg} = $self->{fg};
1266 my $tex = $tex[0];
1267
1268 glEnable GL_BLEND;
1269 glEnable GL_TEXTURE_2D;
1270 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1271 1453
1272 if ($GRAB == $self) { 1454 if ($GRAB == $self) {
1273 $self->{fg} = $self->{active_fg}; 1455 $self->{fg} = $self->{active_fg};
1274 } 1456 }
1275 1457
1276 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1458 glEnable GL_BLEND;
1459 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1460 glEnable GL_TEXTURE_2D;
1277 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1461 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1462 glColor 0, 0, 0, 1;
1278 1463
1279 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1464 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1280 1465
1281 glDisable GL_TEXTURE_2D; 1466 glDisable GL_TEXTURE_2D;
1282 glDisable GL_BLEND; 1467 glDisable GL_BLEND;
1283 1468
1284 $self->SUPER::_draw; 1469 $self->SUPER::_draw;
1289package CFClient::UI::CheckBox; 1474package CFClient::UI::CheckBox;
1290 1475
1291our @ISA = CFClient::UI::DrawBG::; 1476our @ISA = CFClient::UI::DrawBG::;
1292 1477
1293my @tex = 1478my @tex =
1294 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1479 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1295 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1480 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1296 1481
1297use SDL; 1482use CFClient::OpenGL;
1298use SDL::OpenGL;
1299 1483
1300sub new { 1484sub new {
1301 my $class = shift; 1485 my $class = shift;
1302 1486
1303 $class->SUPER::new ( 1487 $class->SUPER::new (
1314 my ($self) = @_; 1498 my ($self) = @_;
1315 1499
1316 ($self->{padding} * 2 + 6) x 2 1500 ($self->{padding} * 2 + 6) x 2
1317} 1501}
1318 1502
1319sub size_allocate {
1320 my ($self, $x, $y, $w, $h) = @_;
1321
1322 $self->_size_allocate ($x, $y, $w, $h);
1323}
1324
1325sub button_down { 1503sub button_down {
1326 my ($self, $ev, $x, $y) = @_; 1504 my ($self, $ev, $x, $y) = @_;
1327 1505
1328 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1506 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1329 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1507 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1355 glDisable GL_BLEND; 1533 glDisable GL_BLEND;
1356} 1534}
1357 1535
1358############################################################################# 1536#############################################################################
1359 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
1360package CFClient::UI::Slider; 1759package CFClient::UI::Slider;
1361 1760
1362use strict; 1761use strict;
1363 1762
1364use SDL::OpenGL; 1763use CFClient::OpenGL;
1365 1764
1366our @ISA = CFClient::UI::DrawBG::; 1765our @ISA = CFClient::UI::DrawBG::;
1367 1766
1368my @tex = 1767my @tex =
1369 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1768 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1379 # TODO: calculations are off 1778 # TODO: calculations are off
1380 my $self = $class->SUPER::new ( 1779 my $self = $class->SUPER::new (
1381 fg => [1, 1, 1], 1780 fg => [1, 1, 1],
1382 active_fg => [0, 0, 0], 1781 active_fg => [0, 0, 0],
1383 range => [0, 0, 100, 10], 1782 range => [0, 0, 100, 10],
1384 req_w => 40, 1783 req_w => 20,
1385 req_h => 13, 1784 req_h => 20,
1386 vertical => 0, 1785 vertical => 0,
1387 can_hover => 1, 1786 can_hover => 1,
1388 inner_pad => 5, 1787 inner_pad => 5,
1389 @_ 1788 @_
1390 ); 1789 );
1494 1893
1495package CFClient::UI::TextView; 1894package CFClient::UI::TextView;
1496 1895
1497our @ISA = CFClient::UI::HBox::; 1896our @ISA = CFClient::UI::HBox::;
1498 1897
1499use SDL::OpenGL; 1898use CFClient::OpenGL;
1500 1899
1501sub new { 1900sub new {
1502 my $class = shift; 1901 my $class = shift;
1503 1902
1504 my $self = $class->SUPER::new ( 1903 my $self = $class->SUPER::new (
1505 req_w => $::WIDTH / 6,
1506 req_h => $::HEIGHT / 6,
1507 fontsize => $::FONTSIZE, 1904 fontsize => 1,
1508 @_, 1905 @_,
1509 1906
1510 layout => (new CFClient::Layout), 1907 layout => (new CFClient::Layout),
1511 par => [], 1908 par => [],
1512 height => 0, 1909 height => 0,
1533sub text_height { 1930sub text_height {
1534 my ($self, $text) = @_; 1931 my ($self, $text) = @_;
1535 1932
1536 my $layout = $self->{layout}; 1933 my $layout = $self->{layout};
1537 1934
1538 $layout->set_height ($self->{fontsize}); 1935 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1539 $layout->set_width ($self->{w}); 1936 $layout->set_width ($self->{w});
1540 $layout->set_text ($text); 1937 $layout->set_text ($text);
1541 1938
1542 ($layout->size)[1] 1939 ($layout->size)[1]
1543} 1940}
1547 1944
1548 $self->{need_reflow}++; 1945 $self->{need_reflow}++;
1549 $self->update; 1946 $self->update;
1550} 1947}
1551 1948
1552sub size_request {
1553 my ($self) = @_;
1554
1555 ($self->{req_w}, $self->{req_h})
1556}
1557
1558sub size_allocate { 1949sub size_allocate {
1559 my ($self, $x, $y, $w, $h) = @_; 1950 my ($self, $w, $h) = @_;
1560 1951
1561 $self->SUPER::size_allocate ($x, $y, $w, $h); 1952 $self->SUPER::size_allocate ($w, $h);
1562 1953
1563 $self->{layout}->set_height ($self->{fontsize}); 1954 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1564 $self->{layout}->set_width ($self->{w}); 1955 $self->{layout}->set_width ($self->{children}[0]{w});
1565 1956
1566 $self->reflow; 1957 $self->reflow;
1567} 1958}
1568 1959
1569sub add_paragraph { 1960sub add_paragraph {
1656 2047
1657} 2048}
1658 2049
1659############################################################################# 2050#############################################################################
1660 2051
1661package CFClient::UI::MapWidget;
1662
1663use strict;
1664
1665use List::Util qw(min max);
1666
1667use SDL;
1668use SDL::OpenGL;
1669
1670our @ISA = CFClient::UI::Base::;
1671
1672sub new {
1673 my $class = shift;
1674
1675 $class->SUPER::new (
1676 z => -1,
1677 can_focus => 1,
1678 list => (glGenLists 1),
1679 @_
1680 )
1681}
1682
1683sub key_down {
1684 print "MAPKEYDOWN\n";
1685}
1686
1687sub key_up {
1688}
1689
1690sub button_down {
1691 my ($self, $ev, $x, $y) = @_;
1692
1693 $self->focus_in;
1694
1695 if ($ev->button == 2) {
1696 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1697 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1698
1699 $self->{motion} = sub {
1700 my ($ev, $x, $y) = @_;
1701
1702 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1703
1704 $::CFG->{map_shift_x} = $bw + $x - $ox;
1705 $::CFG->{map_shift_y} = $bh + $y - $oy;
1706
1707 $self->update;
1708 };
1709 }
1710}
1711
1712sub button_up {
1713 my ($self, $ev, $x, $y) = @_;
1714
1715 delete $self->{motion};
1716}
1717
1718sub mouse_motion {
1719 my ($self, $ev, $x, $y) = @_;
1720
1721 $self->{motion}->($ev, $x, $y) if $self->{motion};
1722}
1723
1724sub size_request {
1725 (
1726 1 + 32 * int $::WIDTH / 32,
1727 1 + 32 * int $::HEIGHT / 32,
1728 )
1729}
1730
1731sub update {
1732 my ($self) = @_;
1733
1734 $self->{need_update} = 1;
1735 $self->SUPER::update;
1736}
1737
1738sub draw {
1739 my ($self) = @_;
1740
1741 if (delete $self->{need_update}) {
1742 glNewList $self->{list}, GL_COMPILE;
1743
1744 if ($::MAP) {
1745 my $sw = int $::WIDTH / 32;
1746 my $sh = int $::HEIGHT / 32;
1747
1748 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1749 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1750
1751 glTranslate $sx0 - 32, $sy0 - 32, 0;
1752
1753 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1754
1755 if ($::CFG->{fow_enable}) {
1756 if ($::CFG->{fow_smooth}) { # smooth fog of war
1757 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1758 glConvolutionFilter2D
1759 GL_CONVOLUTION_2D,
1760 GL_ALPHA,
1761 3, 3,
1762 GL_ALPHA, GL_FLOAT,
1763 pack "f*",
1764 0.1, 0.1, 0.1,
1765 0.1, 0.2, 0.1,
1766 0.1, 0.1, 0.1,
1767 ;
1768 glEnable GL_CONVOLUTION_2D;
1769 }
1770
1771 my $tex = new CFClient::Texture
1772 w => $w,
1773 h => $h,
1774 data => $data,
1775 internalformat => GL_ALPHA,
1776 format => GL_ALPHA;
1777
1778 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1779
1780 glEnable GL_BLEND;
1781 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1782 glEnable GL_TEXTURE_2D;
1783 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1784
1785 glColor +($::CFG->{fow_intensity}) x 3, 1;
1786 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1787
1788 glDisable GL_TEXTURE_2D;
1789 glDisable GL_BLEND;
1790 }
1791
1792 # HACK BEGIN
1793 {
1794 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1795 my ($w, $h) = (250, 250);
1796
1797 glEnable GL_BLEND;
1798 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1799 glEnable GL_TEXTURE_2D;
1800 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1801
1802 CFClient::Texture->new (
1803 w => $w,
1804 h => $h,
1805 data => $::MAP->mapmap ($w, $h),
1806 type => GL_UNSIGNED_INT_8_8_8_8_REV
1807 )->draw_quad (100, 100);
1808
1809 glDisable GL_TEXTURE_2D;
1810 glDisable GL_BLEND;
1811 }
1812 # HACK END
1813 }
1814
1815 glEndList;
1816 }
1817
1818 glPushMatrix;
1819 glCallList $self->{list};
1820 glPopMatrix;
1821
1822 if ($FOCUS != $self) {
1823 glColor 64/255, 64/255, 64/255;
1824 glLogicOp GL_AND;
1825 glEnable GL_COLOR_LOGIC_OP;
1826 glBegin GL_QUADS;
1827 glVertex 0, 0;
1828 glVertex 0, $::HEIGHT;
1829 glVertex $::WIDTH, $::HEIGHT;
1830 glVertex $::WIDTH, 0;
1831 glEnd;
1832 glDisable GL_COLOR_LOGIC_OP;
1833 }
1834}
1835
1836my %DIR = (
1837 SDLK_KP8, [1, "north"],
1838 SDLK_KP9, [2, "northeast"],
1839 SDLK_KP6, [3, "east"],
1840 SDLK_KP3, [4, "southeast"],
1841 SDLK_KP2, [5, "south"],
1842 SDLK_KP1, [6, "southwest"],
1843 SDLK_KP4, [7, "west"],
1844 SDLK_KP7, [8, "northwest"],
1845
1846 SDLK_UP, [1, "north"],
1847 SDLK_RIGHT, [3, "east"],
1848 SDLK_DOWN, [5, "south"],
1849 SDLK_LEFT, [7, "west"],
1850);
1851
1852sub key_down {
1853 my ($self, $ev) = @_;
1854
1855 my $mod = $ev->key_mod;
1856 my $sym = $ev->key_sym;
1857
1858 if ($sym == SDLK_KP5) {
1859 $::CONN->user_send ("command stay fire");
1860 } elsif ($sym == SDLK_a) {
1861 $::CONN->user_send ("command apply");
1862 } elsif ($sym == SDLK_QUOTE) {
1863 $self->emit ('activate_console');
1864 } elsif ($sym == SDLK_SLASH) {
1865 $self->emit ('activate_console' => '/');
1866 } elsif (exists $DIR{$sym}) {
1867 if ($mod & KMOD_SHIFT) {
1868 $self->{shft}++;
1869 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1870 } elsif ($mod & KMOD_CTRL) {
1871 $self->{ctrl}++;
1872 $::CONN->user_send ("command run $DIR{$sym}[0]");
1873 } else {
1874 $::CONN->user_send ("command $DIR{$sym}[1]");
1875 }
1876 }
1877}
1878
1879sub key_up {
1880 my ($self, $ev) = @_;
1881
1882 my $mod = $ev->key_mod;
1883 my $sym = $ev->key_sym;
1884
1885 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1886 $::CONN->user_send ("command fire_stop");
1887 }
1888 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1889 $::CONN->user_send ("command run_stop");
1890 }
1891}
1892
1893#############################################################################
1894
1895package CFClient::UI::Animator; 2052package CFClient::UI::Animator;
1896 2053
1897use SDL::OpenGL; 2054use CFClient::OpenGL;
1898 2055
1899our @ISA = CFClient::UI::Bin::; 2056our @ISA = CFClient::UI::Bin::;
1900 2057
1901sub moveto { 2058sub moveto {
1902 my ($self, $x, $y) = @_; 2059 my ($self, $x, $y) = @_;
1942 my $class = shift; 2099 my $class = shift;
1943 2100
1944 my $self = $class->SUPER::new ( 2101 my $self = $class->SUPER::new (
1945 state => 0, 2102 state => 0,
1946 connect_activate => \&toggle_flopper, 2103 connect_activate => \&toggle_flopper,
2104 can_events => 1,
1947 @_ 2105 @_
1948 ); 2106 );
1949 2107
1950 if ($self->{state}) { 2108 if ($self->{state}) {
1951 $self->{state} = 0; 2109 $self->{state} = 0;
1975 2133
1976package CFClient::UI::Root; 2134package CFClient::UI::Root;
1977 2135
1978our @ISA = CFClient::UI::Container::; 2136our @ISA = CFClient::UI::Container::;
1979 2137
1980use SDL::OpenGL; 2138use CFClient::OpenGL;
2139
2140sub check_size {
2141 my ($self) = @_;
2142
2143 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2144}
1981 2145
1982sub size_request { 2146sub size_request {
1983 ($::WIDTH, $::HEIGHT) 2147 ($::WIDTH, $::HEIGHT)
1984} 2148}
1985 2149
1986sub size_allocate { 2150sub configure {
1987 my ($self, $x, $y, $w, $h) = @_; 2151 my ($self, $x, $y, $w, $h) = @_;
1988 2152
1989 $self->_size_allocate ($x, $y, $w, $h); 2153 $self->SUPER::configure ($x, $y, $w, $h);
1990 2154
1991 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1992 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 }
1993} 2162}
1994 2163
1995sub _topleft { 2164sub _topleft {
1996 my ($self, $x, $y) = @_; 2165 my ($self, $x, $y) = @_;
1997 2166
1999} 2168}
2000 2169
2001sub update { 2170sub update {
2002 my ($self) = @_; 2171 my ($self) = @_;
2003 2172
2004 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2173 $self->check_size;
2005 ::refresh (); 2174 ::refresh ();
2006} 2175}
2007 2176
2008sub add { 2177sub add {
2009 my ($self, $widget) = @_; 2178 my ($self, $child) = @_;
2010 2179
2180 # integerize window positions
2181 $child->{x} = int $child->{x};
2182 $child->{y} = int $child->{y};
2183
2011 $self->SUPER::add ($widget); 2184 $self->SUPER::add ($child);
2012
2013 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2014} 2185}
2015 2186
2016sub on_refresh { 2187sub on_refresh {
2017 my ($self, $id, $cb) = @_; 2188 my ($self, $id, $cb) = @_;
2018 2189
2042############################################################################# 2213#############################################################################
2043 2214
2044package CFClient::UI; 2215package CFClient::UI;
2045 2216
2046$ROOT = new CFClient::UI::Root; 2217$ROOT = new CFClient::UI::Root;
2218$TOOLTIP = new CFClient::UI::Label fontsize => 0.8, can_events => 0;
2047 2219
20481 22201
2049 2221

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines