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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines