ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.115 by root, Sat Apr 15 19:35:20 2006 UTC vs.
Revision 1.153 by root, Sun Apr 23 02:14:33 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;
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,
1102 valign => -1,
955 padding => 2, 1103 padding => 2,
956 layout => new CFClient::Layout, 1104 layout => new CFClient::Layout,
1105 can_events => 0,
957 %arg 1106 %arg
958 ); 1107 );
959 1108
960 $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};
961 1117
962 $self 1118 $self
963} 1119}
964 1120
965sub escape_text { 1121sub escape_text {
973} 1129}
974 1130
975sub set_text { 1131sub set_text {
976 my ($self, $text) = @_; 1132 my ($self, $text) = @_;
977 1133
978 $self->{text} = $text;
979 $self->{layout}->set_markup ($text); 1134 $self->{layout}->set_text ($text);
980 1135
981 delete $self->{texture}; 1136 delete $self->{texture};
982# $self->{w} = $self->{h} = -1; 1137 $self->check_size;
983 $self->update; 1138 $self->update;
984} 1139}
985 1140
986sub get_text { 1141sub set_markup {
987 my ($self, $text) = @_; 1142 my ($self, $markup) = @_;
988 1143
989 $self->{text} 1144 $self->{layout}->set_markup ($markup);
1145
1146 delete $self->{texture};
1147 $self->check_size;
1148 $self->update;
990} 1149}
991 1150
992sub size_request { 1151sub size_request {
993 my ($self) = @_; 1152 my ($self) = @_;
994 1153
995 $self->{layout}->set_width; 1154 $self->{layout}->set_width;
996 $self->{layout}->set_height ($self->{fontsize}); 1155 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1156
997 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 }
998 1167
999 ( 1168 (
1000 $w + $self->{padding} * 2, 1169 $w + $self->{padding} * 2,
1001 $h + $self->{padding} * 2, 1170 $h + $self->{padding} * 2,
1002 ) 1171 )
1003} 1172}
1004 1173
1005sub size_allocate { 1174sub size_allocate {
1006 my ($self, $x, $y, $w, $h) = @_; 1175 my ($self, $w, $h) = @_;
1007
1008 $self->_size_allocate ($x, $y, $w, $h) or return;
1009 1176
1010 delete $self->{texture}; 1177 delete $self->{texture};
1011} 1178}
1012 1179
1013sub update { 1180sub set_fontsize {
1014 my ($self) = @_; 1181 my ($self, $fontsize) = @_;
1015 1182
1183 $self->{fontsize} = $fontsize;
1016 delete $self->{texture}; 1184 delete $self->{texture};
1185 $self->check_size;
1017 $self->SUPER::update; 1186 $self->update;
1018} 1187}
1019 1188
1020sub _draw { 1189sub _draw {
1021 my ($self) = @_; 1190 my ($self) = @_;
1022 1191
1023 my $tex = $self->{texture} ||= do { 1192 my $tex = $self->{texture} ||= do {
1024 $self->{layout}->set_width ($self->{w}); 1193 $self->{layout}->set_width ($self->{w});
1025 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1194 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1026 new_from_layout CFClient::Texture $self->{layout} 1195 new_from_layout CFClient::Texture $self->{layout}
1027 }; 1196 };
1028 1197
1029 glEnable GL_BLEND; 1198 glEnable GL_BLEND;
1030 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1199 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1031 glEnable GL_TEXTURE_2D; 1200 glEnable GL_TEXTURE_2D;
1032 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1201 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1033 1202
1034 glColor @{$self->{fg}}; 1203 glColor @{$self->{fg}};
1035 1204
1036 my $x = 1205 $self->{ox} = int (
1037 $self->{align} < 0 ? $self->{padding} 1206 $self->{align} < 0 ? $self->{padding}
1038 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1207 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1039 : ($self->{w} - $tex->{w}) * 0.5; 1208 : ($self->{w} - $tex->{w}) * 0.5
1209 );
1040 1210
1041 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1211 $self->{oy} = int (
1212 $self->{valign} < 0 ? $self->{padding}
1213 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1214 : ($self->{h} - $tex->{h}) * 0.5
1215 );
1216
1217 $tex->draw_quad ($self->{ox}, $self->{oy});
1042 1218
1043 glDisable GL_TEXTURE_2D; 1219 glDisable GL_TEXTURE_2D;
1044 glDisable GL_BLEND; 1220 glDisable GL_BLEND;
1045} 1221}
1046 1222
1047############################################################################# 1223#############################################################################
1048 1224
1049package CFClient::UI::Entry; 1225package CFClient::UI::EntryBase;
1050 1226
1051our @ISA = CFClient::UI::Label::; 1227our @ISA = CFClient::UI::Label::;
1052 1228
1053use SDL; 1229use CFClient::OpenGL;
1054use SDL::OpenGL;
1055 1230
1056sub new { 1231sub new {
1057 my $class = shift; 1232 my $class = shift;
1058 1233
1059 $class->SUPER::new ( 1234 $class->SUPER::new (
1061 bg => [0, 0, 0, 0.2], 1236 bg => [0, 0, 0, 0.2],
1062 active_bg => [1, 1, 1, 0.5], 1237 active_bg => [1, 1, 1, 0.5],
1063 active_fg => [0, 0, 0], 1238 active_fg => [0, 0, 0],
1064 can_hover => 1, 1239 can_hover => 1,
1065 can_focus => 1, 1240 can_focus => 1,
1241 valign => 0,
1242 can_events => 1,
1066 @_ 1243 @_
1067 ) 1244 )
1068} 1245}
1069 1246
1070sub _set_text { 1247sub _set_text {
1071 my ($self, $text) = @_; 1248 my ($self, $text) = @_;
1072 1249
1073 my $old_text = $self->{text}; 1250 delete $self->{cur_h};
1251
1252 return if $self->{text} eq $text;
1253
1254 delete $self->{texture};
1074 1255
1075 $self->{last_activity} = $::NOW; 1256 $self->{last_activity} = $::NOW;
1076
1077 $self->{text} = $text; 1257 $self->{text} = $text;
1078 $self->{layout}->set_width ($self->{w});
1079 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1080 1258
1081 $text =~ s/./*/g if $self->{hidden}; 1259 $text =~ s/./*/g if $self->{hidden};
1260 $self->{layout}->set_text ("$text ");
1082 1261
1083 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1262 $self->emit (changed => $self->{text});
1263}
1084 1264
1085 $text = substr $text, 0, $self->{cursor}; 1265sub get_text {
1086 utf8::encode $text; 1266 $_[0]{text}
1087
1088 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1089
1090 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1091 if $old_text ne $self->{text};
1092} 1267}
1093 1268
1094sub size_request { 1269sub size_request {
1095 my ($self) = @_; 1270 my ($self) = @_;
1096 1271
1098 1273
1099 ($w + 1, $h) # add 1 for cursor 1274 ($w + 1, $h) # add 1 for cursor
1100} 1275}
1101 1276
1102sub size_allocate { 1277sub size_allocate {
1103 my ($self, $x, $y, $w, $h) = @_; 1278 my ($self, $w, $h) = @_;
1104 1279
1105 $self->SUPER::size_allocate ($x, $y, $w, $h); 1280 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1106
1107 $self->_set_text ($self->{text});
1108} 1281}
1109 1282
1110sub set_text { 1283sub set_text {
1111 my ($self, $text) = @_; 1284 my ($self, $text) = @_;
1112 1285
1116} 1289}
1117 1290
1118sub key_down { 1291sub key_down {
1119 my ($self, $ev) = @_; 1292 my ($self, $ev) = @_;
1120 1293
1121 my $mod = $ev->key_mod; 1294 my $mod = $ev->{mod};
1122 my $sym = $ev->key_sym; 1295 my $sym = $ev->{sym};
1123
1124 my $uni = $ev->key_unicode; 1296 my $uni = $ev->{unicode};
1125 1297
1126 my $text = $self->get_text; 1298 my $text = $self->get_text;
1127 1299
1128 if ($sym == SDLK_BACKSPACE) { 1300 if ($sym == 8) {
1129 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1301 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1130 } elsif ($sym == SDLK_DELETE) { 1302 } elsif ($sym == 127) {
1131 substr $text, $self->{cursor}, 1, ""; 1303 substr $text, $self->{cursor}, 1, "";
1132 } elsif ($sym == SDLK_LEFT) { 1304 } elsif ($sym == CFClient::SDLK_LEFT) {
1133 --$self->{cursor} if $self->{cursor}; 1305 --$self->{cursor} if $self->{cursor};
1134 } elsif ($sym == SDLK_RIGHT) { 1306 } elsif ($sym == CFClient::SDLK_RIGHT) {
1135 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1307 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1136 } elsif ($sym == SDLK_HOME) { 1308 } elsif ($sym == CFClient::SDLK_HOME) {
1137 $self->{cursor} = 0; 1309 $self->{cursor} = 0;
1138 } elsif ($sym == SDLK_END) { 1310 } elsif ($sym == CFClient::SDLK_END) {
1139 $self->{cursor} = length $text; 1311 $self->{cursor} = length $text;
1140 } elsif ($sym == SDLK_ESCAPE) { 1312 } elsif ($sym == 27) {
1141 $self->emit ('escape'); 1313 $self->emit ('escape');
1142 } elsif ($uni) { 1314 } elsif ($uni) {
1143 substr $text, $self->{cursor}++, 0, chr $uni; 1315 substr $text, $self->{cursor}++, 0, chr $uni;
1144 } 1316 }
1145 1317
1200 1372
1201 $self->SUPER::_draw; 1373 $self->SUPER::_draw;
1202 1374
1203 #TODO: force update every cursor change :( 1375 #TODO: force update every cursor change :(
1204 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1376 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1377
1378 unless (exists $self->{cur_h}) {
1379 my $text = substr $self->{text}, 0, $self->{cursor};
1380 utf8::encode $text;
1381
1382 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1383 }
1384
1205 glColor @{$self->{fg}}; 1385 glColor @{$self->{fg}};
1206 glBegin GL_LINES; 1386 glBegin GL_LINES;
1207 glVertex $self->{cur_x}, $self->{cur_y};
1208 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1387 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1388 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1209 glEnd; 1389 glEnd;
1210 } 1390 }
1211} 1391}
1212 1392
1213package CFClient::UI::LineEntry; 1393package CFClient::UI::Entry;
1214 1394
1215our @ISA = CFClient::UI::Entry::; 1395our @ISA = CFClient::UI::EntryBase::;
1216 1396
1217use SDL; 1397use CFClient::OpenGL;
1218use SDL::OpenGL;
1219 1398
1220sub key_down { 1399sub key_down {
1221 my ($self, $ev) = @_; 1400 my ($self, $ev) = @_;
1222 1401
1223 my $sym = $ev->key_sym; 1402 my $sym = $ev->{sym};
1224 1403
1225 if ($sym == SDLK_RETURN) { 1404 if ($sym == 13) {
1226 $self->emit (activate => $self->get_text); 1405 $self->emit (activate => $self->get_text);
1227 $self->update; 1406 $self->update;
1228 1407
1229 } else { 1408 } else {
1230 $self->SUPER::key_down ($ev); 1409 $self->SUPER::key_down ($ev);
1236 1415
1237package CFClient::UI::Button; 1416package CFClient::UI::Button;
1238 1417
1239our @ISA = CFClient::UI::Label::; 1418our @ISA = CFClient::UI::Label::;
1240 1419
1241use SDL; 1420use CFClient::OpenGL;
1242use SDL::OpenGL;
1243 1421
1244my @tex = 1422my @tex =
1245 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1423 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1246 qw(b1_button_active.png); 1424 qw(b1_button_active.png);
1247 1425
1248sub new { 1426sub new {
1249 my $class = shift; 1427 my $class = shift;
1250 1428
1251 $class->SUPER::new ( 1429 $class->SUPER::new (
1252 padding => 4, 1430 padding => 4,
1253 fg => [1, 1, 1], 1431 fg => [1, 1, 1],
1254 bg => [1, 1, 1, 0.2], 1432 bg => [1, 1, 1, 0.2],
1255 active_fg => [1, 1, 0], 1433 active_fg => [0, 0, 1],
1256 can_hover => 1, 1434 can_hover => 1,
1435 align => 0,
1436 valign => 0,
1437 can_events => 1,
1257 @_ 1438 @_
1258 ) 1439 )
1259} 1440}
1260 1441
1261sub button_up { 1442sub button_up {
1269 1450
1270sub _draw { 1451sub _draw {
1271 my ($self) = @_; 1452 my ($self) = @_;
1272 1453
1273 local $self->{fg} = $self->{fg}; 1454 local $self->{fg} = $self->{fg};
1274 my $tex = $tex[0];
1275
1276 glEnable GL_BLEND;
1277 glEnable GL_TEXTURE_2D;
1278 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1279 1455
1280 if ($GRAB == $self) { 1456 if ($GRAB == $self) {
1281 $self->{fg} = $self->{active_fg}; 1457 $self->{fg} = $self->{active_fg};
1282 } 1458 }
1283 1459
1284 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1460 glEnable GL_BLEND;
1461 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1462 glEnable GL_TEXTURE_2D;
1285 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1463 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1464 glColor 0, 0, 0, 1;
1286 1465
1287 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1466 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1288 1467
1289 glDisable GL_TEXTURE_2D; 1468 glDisable GL_TEXTURE_2D;
1290 glDisable GL_BLEND; 1469 glDisable GL_BLEND;
1291 1470
1292 $self->SUPER::_draw; 1471 $self->SUPER::_draw;
1297package CFClient::UI::CheckBox; 1476package CFClient::UI::CheckBox;
1298 1477
1299our @ISA = CFClient::UI::DrawBG::; 1478our @ISA = CFClient::UI::DrawBG::;
1300 1479
1301my @tex = 1480my @tex =
1302 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1303 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1482 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1304 1483
1305use SDL; 1484use CFClient::OpenGL;
1306use SDL::OpenGL;
1307 1485
1308sub new { 1486sub new {
1309 my $class = shift; 1487 my $class = shift;
1310 1488
1311 $class->SUPER::new ( 1489 $class->SUPER::new (
1322 my ($self) = @_; 1500 my ($self) = @_;
1323 1501
1324 ($self->{padding} * 2 + 6) x 2 1502 ($self->{padding} * 2 + 6) x 2
1325} 1503}
1326 1504
1327sub size_allocate {
1328 my ($self, $x, $y, $w, $h) = @_;
1329
1330 $self->_size_allocate ($x, $y, $w, $h);
1331}
1332
1333sub button_down { 1505sub button_down {
1334 my ($self, $ev, $x, $y) = @_; 1506 my ($self, $ev, $x, $y) = @_;
1335 1507
1336 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1508 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1337 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1509 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1363 glDisable GL_BLEND; 1535 glDisable GL_BLEND;
1364} 1536}
1365 1537
1366############################################################################# 1538#############################################################################
1367 1539
1540package CFClient::UI::Image;
1541
1542our @ISA = CFClient::UI::Base::;
1543
1544use CFClient::OpenGL;
1545use Carp qw/confess/;
1546
1547our %loaded_images;
1548
1549sub new {
1550 my $class = shift;
1551
1552 my $self = $class->SUPER::new (can_events => 0, @_);
1553
1554 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1555
1556 $loaded_images{$self->{image}} ||=
1557 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1558
1559 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1560
1561 Scalar::Util::weaken $loaded_images{$self->{image}};
1562
1563 $self->{aspect} = $tex->{w} / $tex->{h};
1564
1565 $self
1566}
1567
1568sub size_request {
1569 my ($self) = @_;
1570
1571 ($self->{tex}->{w}, $self->{tex}->{h})
1572}
1573
1574sub _draw {
1575 my ($self) = @_;
1576
1577 my $tex = $self->{tex};
1578
1579 my ($w, $h) = ($self->{w}, $self->{h});
1580
1581 if ($self->{rot90}) {
1582 glRotate 90, 0, 0, 1;
1583 glTranslate 0, -$self->{w}, 0;
1584
1585 ($w, $h) = ($h, $w);
1586 }
1587
1588 glEnable GL_BLEND;
1589 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1590 glEnable GL_TEXTURE_2D;
1591 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1592
1593 $tex->draw_quad (0, 0, $w, $h);
1594
1595 glDisable GL_BLEND;
1596 glDisable GL_TEXTURE_2D;
1597}
1598
1599#############################################################################
1600
1601package CFClient::UI::VGauge;
1602
1603our @ISA = CFClient::UI::Base::;
1604
1605use CFClient::OpenGL;
1606
1607my %tex = (
1608 food => [
1609 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1610 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1611 ],
1612 grace => [
1613 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1614 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1615 ],
1616 hp => [
1617 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1618 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1619 ],
1620 mana => [
1621 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1622 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1623 ],
1624);
1625
1626# eg. VGauge->new (gauge => 'food'), default gauge: food
1627sub new {
1628 my $class = shift;
1629
1630 my $self = $class->SUPER::new (
1631 type => 'food',
1632 @_
1633 );
1634
1635 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1636
1637 $self
1638}
1639
1640sub size_request {
1641 my ($self) = @_;
1642
1643 #my $tex = $tex{$self->{type}}[0];
1644 #@$tex{qw(w h)}
1645 (0, 0)
1646}
1647
1648sub set_max {
1649 my ($self, $max) = @_;
1650
1651 $self->{max_val} = $max;
1652}
1653
1654sub set_value {
1655 my ($self, $val, $max) = @_;
1656
1657 $self->set_max ($max)
1658 if defined $max;
1659
1660 $max = $self->{max_val};
1661 $self->{val} = $val;
1662
1663 $self->update;
1664}
1665
1666sub _draw {
1667 my ($self) = @_;
1668
1669 my $tex = $tex{$self->{type}};
1670
1671 my ($w, $h) = ($self->{w}, $self->{h});
1672
1673 if ($self->{vertical}) {
1674 glRotate 90, 0, 0, 1;
1675 glTranslate 0, -$self->{w}, 0;
1676
1677 ($w, $h) = ($h, $w);
1678 }
1679
1680 my $ycut = $self->{val} / ($self->{max_val} || 1);
1681 $ycut = 1 if $self->{val} > $self->{max_val};
1682
1683 my $t1 = $tex->[0];
1684 my $t2 = $tex->[1];
1685
1686 glEnable GL_BLEND;
1687 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1688 glEnable GL_TEXTURE_2D;
1689 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1690
1691 my $h1 = $self->{h} - $ycut * $self->{h};
1692 my $h2 = $ycut * $self->{h};
1693
1694 glBindTexture GL_TEXTURE_2D, $t1->{name};
1695 glBegin GL_QUADS;
1696 glTexCoord 0 , 0; glVertex 0 , 0;
1697 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1698 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1699 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1700 glEnd;
1701
1702 glBindTexture GL_TEXTURE_2D, $t2->{name};
1703 glBegin GL_QUADS;
1704 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1705 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1706 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1707 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1708 glEnd;
1709
1710 glDisable GL_BLEND;
1711 glDisable GL_TEXTURE_2D;
1712}
1713
1714#############################################################################
1715
1716package CFClient::UI::Gauge;
1717
1718our @ISA = CFClient::UI::VBox::;
1719
1720sub new {
1721 my ($class, %arg) = @_;
1722
1723 my $self = $class->SUPER::new (
1724 tooltip => $arg{type},
1725 %arg,
1726 );
1727
1728 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1729 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1730 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1731
1732 $self
1733}
1734
1735sub set_fontsize {
1736 my ($self, $fsize) = @_;
1737
1738 $self->{value}->set_fontsize ($fsize);
1739 $self->{max} ->set_fontsize ($fsize);
1740}
1741
1742sub set_value {
1743 my ($self, $val, $max) = @_;
1744
1745 $self->set_max ($max)
1746 if defined $max;
1747
1748 $self->{gauge}->set_value ($val, $max);
1749 $self->{value}->set_text ($val);
1750}
1751
1752sub set_max {
1753 my ($self, $max) = @_;
1754
1755 $self->{gauge}->set_max ($max);
1756 $self->{max}->set_text ($max);
1757}
1758
1759#############################################################################
1760
1368package CFClient::UI::Slider; 1761package CFClient::UI::Slider;
1369 1762
1370use strict; 1763use strict;
1371 1764
1372use SDL::OpenGL; 1765use CFClient::OpenGL;
1373 1766
1374our @ISA = CFClient::UI::DrawBG::; 1767our @ISA = CFClient::UI::DrawBG::;
1375 1768
1376my @tex = 1769my @tex =
1377 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1770 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1387 # TODO: calculations are off 1780 # TODO: calculations are off
1388 my $self = $class->SUPER::new ( 1781 my $self = $class->SUPER::new (
1389 fg => [1, 1, 1], 1782 fg => [1, 1, 1],
1390 active_fg => [0, 0, 0], 1783 active_fg => [0, 0, 0],
1391 range => [0, 0, 100, 10], 1784 range => [0, 0, 100, 10],
1392 req_w => 40, 1785 req_w => 20,
1393 req_h => 13, 1786 req_h => 20,
1394 vertical => 0, 1787 vertical => 0,
1395 can_hover => 1, 1788 can_hover => 1,
1396 inner_pad => 5, 1789 inner_pad => 5,
1397 @_ 1790 @_
1398 ); 1791 );
1502 1895
1503package CFClient::UI::TextView; 1896package CFClient::UI::TextView;
1504 1897
1505our @ISA = CFClient::UI::HBox::; 1898our @ISA = CFClient::UI::HBox::;
1506 1899
1507use SDL::OpenGL; 1900use CFClient::OpenGL;
1508 1901
1509sub new { 1902sub new {
1510 my $class = shift; 1903 my $class = shift;
1511 1904
1512 my $self = $class->SUPER::new ( 1905 my $self = $class->SUPER::new (
1513 req_w => $::WIDTH / 6,
1514 req_h => $::HEIGHT / 6,
1515 fontsize => $::FONTSIZE, 1906 fontsize => 1,
1516 @_, 1907 @_,
1517 1908
1518 layout => (new CFClient::Layout), 1909 layout => (new CFClient::Layout),
1519 par => [], 1910 par => [],
1520 height => 0, 1911 height => 0,
1541sub text_height { 1932sub text_height {
1542 my ($self, $text) = @_; 1933 my ($self, $text) = @_;
1543 1934
1544 my $layout = $self->{layout}; 1935 my $layout = $self->{layout};
1545 1936
1546 $layout->set_height ($self->{fontsize}); 1937 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1547 $layout->set_width ($self->{w}); 1938 $layout->set_width ($self->{w});
1548 $layout->set_text ($text); 1939 $layout->set_text ($text);
1549 1940
1550 ($layout->size)[1] 1941 ($layout->size)[1]
1551} 1942}
1555 1946
1556 $self->{need_reflow}++; 1947 $self->{need_reflow}++;
1557 $self->update; 1948 $self->update;
1558} 1949}
1559 1950
1560sub size_request {
1561 my ($self) = @_;
1562
1563 ($self->{req_w}, $self->{req_h})
1564}
1565
1566sub size_allocate { 1951sub size_allocate {
1567 my ($self, $x, $y, $w, $h) = @_; 1952 my ($self, $w, $h) = @_;
1568 1953
1569 $self->SUPER::size_allocate ($x, $y, $w, $h); 1954 $self->SUPER::size_allocate ($w, $h);
1570 1955
1571 $self->{layout}->set_height ($self->{fontsize}); 1956 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1572 $self->{layout}->set_width ($self->{w}); 1957 $self->{layout}->set_width ($self->{children}[0]{w});
1573 1958
1574 $self->reflow; 1959 $self->reflow;
1575} 1960}
1576 1961
1577sub add_paragraph { 1962sub add_paragraph {
1664 2049
1665} 2050}
1666 2051
1667############################################################################# 2052#############################################################################
1668 2053
1669package CFClient::UI::MapWidget;
1670
1671use strict;
1672
1673use List::Util qw(min max);
1674
1675use SDL;
1676use SDL::OpenGL;
1677
1678our @ISA = CFClient::UI::Base::;
1679
1680sub new {
1681 my $class = shift;
1682
1683 $class->SUPER::new (
1684 z => -1,
1685 can_focus => 1,
1686 list => (glGenLists 1),
1687 @_
1688 )
1689}
1690
1691sub key_down {
1692 print "MAPKEYDOWN\n";
1693}
1694
1695sub key_up {
1696}
1697
1698sub button_down {
1699 my ($self, $ev, $x, $y) = @_;
1700
1701 $self->focus_in;
1702
1703 if ($ev->button == 2) {
1704 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1705 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1706
1707 $self->{motion} = sub {
1708 my ($ev, $x, $y) = @_;
1709
1710 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1711
1712 $::CFG->{map_shift_x} = $bw + $x - $ox;
1713 $::CFG->{map_shift_y} = $bh + $y - $oy;
1714
1715 $self->update;
1716 };
1717 }
1718}
1719
1720sub button_up {
1721 my ($self, $ev, $x, $y) = @_;
1722
1723 delete $self->{motion};
1724}
1725
1726sub mouse_motion {
1727 my ($self, $ev, $x, $y) = @_;
1728
1729 $self->{motion}->($ev, $x, $y) if $self->{motion};
1730}
1731
1732sub size_request {
1733 (
1734 1 + 32 * int $::WIDTH / 32,
1735 1 + 32 * int $::HEIGHT / 32,
1736 )
1737}
1738
1739sub update {
1740 my ($self) = @_;
1741
1742 $self->{need_update} = 1;
1743 $self->SUPER::update;
1744}
1745
1746sub draw {
1747 my ($self) = @_;
1748
1749 if (delete $self->{need_update}) {
1750 glNewList $self->{list}, GL_COMPILE;
1751
1752 if ($::MAP) {
1753 my $sw = int $::WIDTH / 32;
1754 my $sh = int $::HEIGHT / 32;
1755
1756 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1757 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1758
1759 glTranslate $sx0 - 32, $sy0 - 32, 0;
1760
1761 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1762
1763 if ($::CFG->{fow_enable}) {
1764 if ($::CFG->{fow_smooth}) { # smooth fog of war
1765 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1766 glConvolutionFilter2D
1767 GL_CONVOLUTION_2D,
1768 GL_ALPHA,
1769 3, 3,
1770 GL_ALPHA, GL_FLOAT,
1771 pack "f*",
1772 0.1, 0.1, 0.1,
1773 0.1, 0.2, 0.1,
1774 0.1, 0.1, 0.1,
1775 ;
1776 glEnable GL_CONVOLUTION_2D;
1777 }
1778
1779 my $tex = new CFClient::Texture
1780 w => $w,
1781 h => $h,
1782 data => $data,
1783 internalformat => GL_ALPHA,
1784 format => GL_ALPHA;
1785
1786 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1787
1788 glEnable GL_BLEND;
1789 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1790 glEnable GL_TEXTURE_2D;
1791 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1792
1793 glColor +($::CFG->{fow_intensity}) x 3, 1;
1794 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1795
1796 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND;
1798 }
1799
1800 # HACK BEGIN
1801 {
1802 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1803 my ($w, $h) = (250, 250);
1804
1805 glEnable GL_BLEND;
1806 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1807 glEnable GL_TEXTURE_2D;
1808 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1809
1810 CFClient::Texture->new (
1811 w => $w,
1812 h => $h,
1813 data => $::MAP->mapmap ($w, $h),
1814 type => GL_UNSIGNED_INT_8_8_8_8_REV
1815 )->draw_quad (100, 100);
1816
1817 glDisable GL_TEXTURE_2D;
1818 glDisable GL_BLEND;
1819 }
1820 # HACK END
1821 }
1822
1823 glEndList;
1824 }
1825
1826 glPushMatrix;
1827 glCallList $self->{list};
1828 glPopMatrix;
1829
1830 if ($FOCUS != $self) {
1831 glColor 64/255, 64/255, 64/255;
1832 glLogicOp GL_AND;
1833 glEnable GL_COLOR_LOGIC_OP;
1834 glBegin GL_QUADS;
1835 glVertex 0, 0;
1836 glVertex 0, $::HEIGHT;
1837 glVertex $::WIDTH, $::HEIGHT;
1838 glVertex $::WIDTH, 0;
1839 glEnd;
1840 glDisable GL_COLOR_LOGIC_OP;
1841 }
1842}
1843
1844my %DIR = (
1845 SDLK_KP8, [1, "north"],
1846 SDLK_KP9, [2, "northeast"],
1847 SDLK_KP6, [3, "east"],
1848 SDLK_KP3, [4, "southeast"],
1849 SDLK_KP2, [5, "south"],
1850 SDLK_KP1, [6, "southwest"],
1851 SDLK_KP4, [7, "west"],
1852 SDLK_KP7, [8, "northwest"],
1853
1854 SDLK_UP, [1, "north"],
1855 SDLK_RIGHT, [3, "east"],
1856 SDLK_DOWN, [5, "south"],
1857 SDLK_LEFT, [7, "west"],
1858);
1859
1860sub key_down {
1861 my ($self, $ev) = @_;
1862
1863 my $mod = $ev->key_mod;
1864 my $sym = $ev->key_sym;
1865
1866 if ($sym == SDLK_KP5) {
1867 $::CONN->user_send ("command stay fire");
1868 } elsif ($sym == SDLK_a) {
1869 $::CONN->user_send ("command apply");
1870 } elsif ($sym == SDLK_QUOTE) {
1871 $self->emit ('activate_console');
1872 } elsif ($sym == SDLK_SLASH) {
1873 $self->emit ('activate_console' => '/');
1874 } elsif (exists $DIR{$sym}) {
1875 if ($mod & KMOD_SHIFT) {
1876 $self->{shft}++;
1877 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1878 } elsif ($mod & KMOD_CTRL) {
1879 $self->{ctrl}++;
1880 $::CONN->user_send ("command run $DIR{$sym}[0]");
1881 } else {
1882 $::CONN->user_send ("command $DIR{$sym}[1]");
1883 }
1884 }
1885}
1886
1887sub key_up {
1888 my ($self, $ev) = @_;
1889
1890 my $mod = $ev->key_mod;
1891 my $sym = $ev->key_sym;
1892
1893 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1894 $::CONN->user_send ("command fire_stop");
1895 }
1896 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1897 $::CONN->user_send ("command run_stop");
1898 }
1899}
1900
1901#############################################################################
1902
1903package CFClient::UI::Animator; 2054package CFClient::UI::Animator;
1904 2055
1905use SDL::OpenGL; 2056use CFClient::OpenGL;
1906 2057
1907our @ISA = CFClient::UI::Bin::; 2058our @ISA = CFClient::UI::Bin::;
1908 2059
1909sub moveto { 2060sub moveto {
1910 my ($self, $x, $y) = @_; 2061 my ($self, $x, $y) = @_;
1950 my $class = shift; 2101 my $class = shift;
1951 2102
1952 my $self = $class->SUPER::new ( 2103 my $self = $class->SUPER::new (
1953 state => 0, 2104 state => 0,
1954 connect_activate => \&toggle_flopper, 2105 connect_activate => \&toggle_flopper,
2106 can_events => 1,
1955 @_ 2107 @_
1956 ); 2108 );
1957 2109
1958 if ($self->{state}) { 2110 if ($self->{state}) {
1959 $self->{state} = 0; 2111 $self->{state} = 0;
1979 $self->emit (changed => $self->{state}); 2131 $self->emit (changed => $self->{state});
1980} 2132}
1981 2133
1982############################################################################# 2134#############################################################################
1983 2135
2136package CFClient::UI::Tooltip;
2137
2138our @ISA = CFClient::UI::Bin::;
2139
2140use CFClient::OpenGL;
2141
2142sub new {
2143 my $class = shift;
2144
2145 $class->SUPER::new (
2146 @_,
2147 can_events => 0,
2148 )
2149}
2150
2151sub set_text {
2152 my ($self, $text) = @_;
2153
2154 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8;
2155 $self->{label}->set_text ($text);
2156 $self->add ($self->{label});
2157}
2158
2159sub size_request {
2160 my ($self) = @_;
2161
2162 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2163
2164 $w = List::Util::min $::WIDTH * 0.2, $w;
2165
2166 ($w, $h)
2167}
2168
2169#############################################################################
2170
1984package CFClient::UI::Root; 2171package CFClient::UI::Root;
1985 2172
1986our @ISA = CFClient::UI::Container::; 2173our @ISA = CFClient::UI::Container::;
1987 2174
1988use SDL::OpenGL; 2175use CFClient::OpenGL;
2176
2177sub check_size {
2178 my ($self) = @_;
2179
2180 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2181}
1989 2182
1990sub size_request { 2183sub size_request {
1991 ($::WIDTH, $::HEIGHT) 2184 ($::WIDTH, $::HEIGHT)
1992} 2185}
1993 2186
1994sub size_allocate { 2187sub configure {
1995 my ($self, $x, $y, $w, $h) = @_; 2188 my ($self, $x, $y, $w, $h) = @_;
1996 2189
1997 $self->_size_allocate ($x, $y, $w, $h); 2190 $self->SUPER::configure ($x, $y, $w, $h);
1998 2191
1999 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2000 for @{$self->{children}}; 2192 for my $child (@{$self->{children}}) {
2193 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2194
2195 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2196 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2197 $child->configure ($X, $Y, $W,$H);
2198 }
2001} 2199}
2002 2200
2003sub _topleft { 2201sub _topleft {
2004 my ($self, $x, $y) = @_; 2202 my ($self, $x, $y) = @_;
2005 2203
2007} 2205}
2008 2206
2009sub update { 2207sub update {
2010 my ($self) = @_; 2208 my ($self) = @_;
2011 2209
2012 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2210 $self->check_size;
2013 ::refresh (); 2211 ::refresh ();
2014} 2212}
2015 2213
2016sub add { 2214sub add {
2017 my ($self, $widget) = @_; 2215 my ($self, $child) = @_;
2018 2216
2217 # integerize window positions
2218 $child->{x} = int $child->{x};
2219 $child->{y} = int $child->{y};
2220
2019 $self->SUPER::add ($widget); 2221 $self->SUPER::add ($child);
2020
2021 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2022} 2222}
2023 2223
2024sub on_refresh { 2224sub on_refresh {
2025 my ($self, $id, $cb) = @_; 2225 my ($self, $id, $cb) = @_;
2026 2226
2050############################################################################# 2250#############################################################################
2051 2251
2052package CFClient::UI; 2252package CFClient::UI;
2053 2253
2054$ROOT = new CFClient::UI::Root; 2254$ROOT = new CFClient::UI::Root;
2255$TOOLTIP = new CFClient::UI::Tooltip;
2055 2256
20561 22571
2057 2258

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines