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.158 by root, Mon Apr 24 02:41:48 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
74sub harmonize { 107sub harmonize {
75 my ($vals) = @_; 108 my ($vals) = @_;
76 109
77 my $rem = 0; 110 my $rem = 0;
78 111
79 for ($vals) { 112 for (@$vals) {
80 my $i = int $_ + $rem; 113 my $i = int $_ + $rem;
81 $rem += $_ - $i; 114 $rem += $_ - $i;
82 $_ = $i; 115 $_ = $i;
83 } 116 }
84} 117}
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} = $x; 166 $self->{x} = int $x;
118 $self->{y} = $y; 167 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 168 $self->{z} = $z if defined $z;
120}
121 169
122sub needs_redraw { 170 $self->update;
123 0 171}
172
173sub set_size {
174 my ($self, $w, $h) = @_;
175
176 $self->{user_w} = $w;
177 $self->{user_h} = $h;
178
179 $self->check_size;
124} 180}
125 181
126sub size_request { 182sub size_request {
127 require Carp; 183 require Carp;
128 Carp::confess "size_request is abtract"; 184 Carp::confess "size_request is abstract";
129} 185}
130 186
131sub _size_allocate { 187sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 188 my ($self, $x, $y, $w, $h) = @_;
133 189
190 if ($self->{aspect}) {
191 my $w2 = List::Util::min $w, int $h * $self->{aspect};
192 my $h2 = List::Util::min $h, int $w / $self->{aspect};
193
194 # use alignment to adjust x, y
195
196 $x += int +($w - $w2) * 0.5;
197 $y += int +($h - $h2) * 0.5;
198
199 ($w, $h) = ($w2, $h2);
200 }
201
202 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 203 $self->{x} = $x;
135 $self->{y} = $y; 204 $self->{y} = $y;
205 $self->update;
206 }
136 207
137 return unless $self->{w} != $w || $self->{h} != $h; 208 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 209 $self->{w} = $w;
140 $self->{h} = $h; 210 $self->{h} = $h;
141 211
212 $self->size_allocate ($w, $h);
213 $self->update;
142 1 214 }
143} 215}
144 216
145sub size_allocate { 217sub size_allocate {
218 # nothing to be done
219}
220
221sub set_max_size {
146 my ($self, $x, $y, $w, $h) = @_; 222 my ($self, $w, $h) = @_;
147 223
148 $self->_size_allocate ($x, $y, $w, $h); 224 delete $self->{max_w}; $self->{max_w} = $w if $w;
225 delete $self->{max_h}; $self->{max_h} = $h if $h;
149} 226}
150 227
151# return top left coordinates 228# return top left coordinates
152sub _topleft { 229sub _topleft {
153 my ($self, $x, $y) = @_; 230 my ($self, $x, $y) = @_;
176 253
177 return if $FOCUS == $self; 254 return if $FOCUS == $self;
178 return unless $self->{can_focus}; 255 return unless $self->{can_focus};
179 256
180 my $focus = $FOCUS; $FOCUS = $self; 257 my $focus = $FOCUS; $FOCUS = $self;
258
259 $self->emit (focus_in => $focus);
260
181 $focus->update if $focus; 261 $focus->update if $focus;
182 $FOCUS->update; 262 $FOCUS->update;
183} 263}
184 264
185sub focus_out { 265sub focus_out {
186 my ($self) = @_; 266 my ($self) = @_;
187 267
188 return unless $FOCUS == $self; 268 return unless $FOCUS == $self;
189 269
190 my $focus = $FOCUS; undef $FOCUS; 270 my $focus = $FOCUS; undef $FOCUS;
271
272 $self->emit (focus_out => $focus);
273
191 $focus->update if $focus; #? 274 $focus->update if $focus; #?
192} 275}
193 276
194sub mouse_motion { } 277sub mouse_motion { }
195sub button_up { } 278sub button_up { }
219 glPopMatrix; 302 glPopMatrix;
220 303
221 if ($self == $HOVER && $self->{can_hover}) { 304 if ($self == $HOVER && $self->{can_hover}) {
222 my ($x, $y) = @$self{qw(x y)}; 305 my ($x, $y) = @$self{qw(x y)};
223 306
224 glColor 0, 0, 1, 0.2; 307 glColor 1, 0.8, 0.5, 0.2;
225 glEnable GL_BLEND; 308 glEnable GL_BLEND;
226 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 309 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
227 glBegin GL_QUADS; 310 glBegin GL_QUADS;
228 glVertex $x , $y; 311 glVertex $x , $y;
229 glVertex $x + $self->{w}, $y; 312 glVertex $x + $self->{w}, $y;
241} 324}
242 325
243sub find_widget { 326sub find_widget {
244 my ($self, $x, $y) = @_; 327 my ($self, $x, $y) = @_;
245 328
329 return () unless $self->{can_events};
330
246 return $self 331 return $self
247 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 332 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
248 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 333 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
249 334
250 () 335 ()
254 my ($self, $parent) = @_; 339 my ($self, $parent) = @_;
255 340
256 Scalar::Util::weaken ($self->{parent} = $parent); 341 Scalar::Util::weaken ($self->{parent} = $parent);
257} 342}
258 343
344sub check_size {
345 my ($self) = @_;
346
347 $self->{parent}
348 or return 1;
349
350 my ($w, $h) = $self->{user_w} && $self->{user_h}
351 ? @$self{qw(user_w user_h)}
352 : $self->size_request;
353
354 if ($w != $self->{req_w} || $h != $self->{req_h}) {
355 $self->{req_w} = $w;
356 $self->{req_h} = $h;
357
358 $self->{parent}->check_size
359 or $self->size_allocate (
360 (List::Util::max $self->{w}, $w),
361 (List::Util::max $self->{h}, $h),
362 );
363
364 1
365 } else {
366 0
367 }
368}
369
259sub update { 370sub update {
260 my ($self) = @_; 371 my ($self) = @_;
261 372
262 $self->{parent}->update 373 $self->{parent}->update
263 if $self->{parent}; 374 if $self->{parent};
264} 375}
265 376
266sub connect { 377sub connect {
267 my ($self, $signal, $cb) = @_; 378 my ($self, $signal, $cb) = @_;
268 379
269 push @{ $self->{cb}{$signal} }, $cb; 380 push @{ $self->{signal_cb}{$signal} }, $cb;
270} 381}
271 382
272sub emit { 383sub emit {
273 my ($self, $signal, @args) = @_; 384 my ($self, $signal, @args) = @_;
274 385
386 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
275 $_->($self, @args) 387 $cb->($self, @args);
276 for @{$self->{cb}{$signal} || []}; 388 }
277} 389}
278 390
279sub DESTROY { 391sub DESTROY {
280 my ($self) = @_; 392 my ($self) = @_;
281 393
287package CFClient::UI::DrawBG; 399package CFClient::UI::DrawBG;
288 400
289our @ISA = CFClient::UI::Base::; 401our @ISA = CFClient::UI::Base::;
290 402
291use strict; 403use strict;
292use SDL::OpenGL; 404use CFClient::OpenGL;
293 405
294sub new { 406sub new {
295 my $class = shift; 407 my $class = shift;
296 408
297 # range [value, low, high, page] 409 # range [value, low, high, page]
326 438
327package CFClient::UI::Empty; 439package CFClient::UI::Empty;
328 440
329our @ISA = CFClient::UI::Base::; 441our @ISA = CFClient::UI::Base::;
330 442
443sub new {
444 my ($class, %arg) = @_;
445 $class->SUPER::new (can_events => 0, %arg);
446}
447
331sub size_request { 448sub size_request {
332 (0, 0) 449 (0, 0)
333} 450}
334 451
335sub draw { } 452sub draw { }
343sub new { 460sub new {
344 my ($class, %arg) = @_; 461 my ($class, %arg) = @_;
345 462
346 my $children = delete $arg{children} || []; 463 my $children = delete $arg{children} || [];
347 464
348 my $self = $class->SUPER::new (children => [], %arg); 465 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
349 $self->add ($_) for @$children; 466 $self->add ($_) for @$children;
350 467
351 $self 468 $self
352} 469}
353 470
361 $self->{children} = [ 478 $self->{children} = [
362 sort { $a->{z} <=> $b->{z} } 479 sort { $a->{z} <=> $b->{z} }
363 @{$self->{children}}, $child 480 @{$self->{children}}, $child
364 ]; 481 ];
365 482
366 $self->{w} = $self->{h} = -1; 483 $child->check_size;
484}
485
486sub remove {
487 my ($self, $child) = @_;
488
489 delete $child->{parent};
490
491 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
492
493 $self->check_size;
367 $self->update; 494 $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} 495}
377 496
378sub find_widget { 497sub find_widget {
379 my ($self, $x, $y) = @_; 498 my ($self, $x, $y) = @_;
380 499
410 529
411 $class->SUPER::new (children => [$child], %arg) 530 $class->SUPER::new (children => [$child], %arg)
412} 531}
413 532
414sub add { 533sub add {
415 my ($self, $widget) = @_; 534 my ($self, $child) = @_;
416 535
417 $self->{children} = []; 536 $self->{children} = [];
418 537
419 $self->SUPER::add ($widget); 538 $self->SUPER::add ($child);
420} 539}
421 540
422sub remove { 541sub remove {
423 my ($self, $widget) = @_; 542 my ($self, $widget) = @_;
424 543
433sub size_request { 552sub size_request {
434 $_[0]{children}[0]->size_request 553 $_[0]{children}[0]->size_request
435} 554}
436 555
437sub size_allocate { 556sub size_allocate {
438 my ($self, $x, $y, $w, $h) = @_; 557 my ($self, $w, $h) = @_;
439 558
440 $self->_size_allocate ($x, $y, $w, $h) or return;
441
442 $self->{children}[0]->size_allocate (0, 0, $w, $h); 559 $self->{children}[0]->configure (0, 0, $w, $h);
443} 560}
444 561
445############################################################################# 562#############################################################################
446 563
447package CFClient::UI::Window; 564package CFClient::UI::Window;
448 565
449our @ISA = CFClient::UI::Bin::; 566our @ISA = CFClient::UI::Bin::;
450 567
451use SDL::OpenGL; 568use CFClient::OpenGL;
452 569
453sub new { 570sub new {
454 my ($class, %arg) = @_; 571 my ($class, %arg) = @_;
455 572
456 my $self = $class->SUPER::new (%arg); 573 my $self = $class->SUPER::new (%arg);
473 $self->child->draw; 590 $self->child->draw;
474 }; 591 };
475} 592}
476 593
477sub size_allocate { 594sub size_allocate {
478 my ($self, $x, $y, $w, $h) = @_; 595 my ($self, $w, $h) = @_;
479 596
480 $self->_size_allocate ($x, $y, $w, $h) or return;
481
482 $self->child->size_allocate (0, 0, $w, $h); 597 $self->child->configure (0, 0, $w, $h);
483 598
484 $self->render_chld; 599 $self->render_chld;
485} 600}
486 601
487sub _draw { 602sub _draw {
503 glDisable GL_TEXTURE_2D; 618 glDisable GL_TEXTURE_2D;
504} 619}
505 620
506############################################################################# 621#############################################################################
507 622
623package CFClient::UI::ViewPort;
624
625our @ISA = CFClient::UI::Window::;
626
627sub new { die }
628
629sub size_request {
630 my ($self) = @_;
631
632 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
633 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
634
635 @$self{qw(child_w child_h)}
636}
637
638sub _draw {
639 my ($self) = @_;
640
641 $self->{children}[1]->draw;
642}
643
644
645#############################################################################
646
508package CFClient::UI::Frame; 647package CFClient::UI::Frame;
509 648
510our @ISA = CFClient::UI::Bin::; 649our @ISA = CFClient::UI::Bin::;
511 650
512use SDL::OpenGL; 651use 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 652
562sub new { 653sub new {
563 my $class = shift; 654 my $class = shift;
564 655
565 # TODO: user_x, user_y, overwrite moveto?
566
567 $class->SUPER::new ( 656 my $self = $class->SUPER::new (
568 bg => [1, 1, 1, 1], 657 bg => [1, 1, 1, 1],
569 border_bg => [1, 1, 1, 1], 658 border_bg => [1, 1, 1, 1],
570 border => $::FONTSIZE * 0.8, 659 border => 0.8,
571 @_ 660 @_
661 );
662
663 $self
664}
665
666sub _draw {
667 my ($self) = @_;
668
669 my ($w, $h) = ($self->{w}, $self->{h});
670
671 glEnable GL_BLEND;
672 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
673 glEnable GL_TEXTURE_2D;
674 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
675
676# glBegin GL_QUADS;
677# glColor 0, 0, 0, 0;
678# glVertex 0 , 0;
679# glVertex 0 , $h;
680# glVertex $w, $h;
681# glVertex $w, 0;
682# glEnd;
683
684
685 $self->child->draw;
686 glDisable GL_BLEND;
687 glDisable GL_TEXTURE_2D;
688}
689
690#############################################################################
691
692package CFClient::UI::FancyFrame;
693
694our @ISA = CFClient::UI::Bin::;
695
696use CFClient::OpenGL;
697
698my @tex =
699 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
700 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
701
702sub new {
703 my $class = shift;
704
705 # TODO: user_x, user_y, overwrite moveto?
706
707 my $self = $class->SUPER::new (
708 bg => [1, 1, 1, 1],
709 border_bg => [1, 1, 1, 1],
710 border => 0.8,
711 can_events => 1,
712 @_
713 );
714
715 $self->{title} &&= new CFClient::UI::Label
716 align => 0,
717 valign => 1,
718 text => $self->{title},
719 fontsize => 1;
720
721 $self
722}
723
724sub border {
725 int $_[0]{border} * $::FONTSIZE
726}
727
728sub size_request {
729 my ($self) = @_;
730
731 my ($w, $h) = $self->SUPER::size_request;
732
733 (
734 $w + $self->border * 2,
735 $h + $self->border * 2,
572 ) 736 )
573} 737}
574 738
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 { 739sub size_allocate {
589 my ($self, $x, $y, $w, $h) = @_; 740 my ($self, $w, $h) = @_;
590 741
591 $self->_size_allocate ($x, $y, $w, $h) or return;
592
593 $h -= List::Util::max 0, $self->{border} * 2; 742 $h -= List::Util::max 0, $self->border * 2;
594 $w -= List::Util::max 0, $self->{border} * 2; 743 $w -= List::Util::max 0, $self->border * 2;
595 744
745 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
746 if $self->{title};
747
596 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 748 $self->child->configure ($self->border, $self->border, $w, $h);
597} 749}
598 750
599sub button_down { 751sub button_down {
600 my ($self, $ev, $x, $y) = @_; 752 my ($self, $ev, $x, $y) = @_;
601 753
754 my $border = $self->border;
755
602 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 756 if ($x < $self->{w} && $x >= $self->{w} - $border
603 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 757 && $y < $self->{h} && $y >= $self->{h} - $border) {
604 758
605 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 759 my ($ox, $oy) = ($ev->{x}, $ev->{y});
606 my ($bw, $bh) = ($self->{w}, $self->{h}); 760 my ($bw, $bh) = ($self->{w}, $self->{h});
607 761
608 $self->{motion} = sub { 762 $self->{motion} = sub {
609 my ($ev, $x, $y) = @_; 763 my ($ev, $x, $y) = @_;
610 764
611 ($x, $y) = ($ev->motion_x, $ev->motion_y); 765 ($x, $y) = ($ev->{x}, $ev->{y});
612 766
613 $self->{user_w} = $bw + $x - $ox; 767 $self->{user_w} = $bw + $x - $ox;
614 $self->{user_h} = $bh + $y - $oy; 768 $self->{user_h} = $bh + $y - $oy;
615 $self->update; 769 $self->check_size;
616 }; 770 };
617 771
618 } elsif ($x >= 0 && $x < $self->{w} 772 } elsif ($x >= 0 && $x < $self->{w}
619 && $y >= 0 && $y < $self->{border}) { 773 && $y >= 0 && $y < $border) {
620 774
621 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 775 my ($ox, $oy) = ($ev->{x}, $ev->{y});
622 my ($bx, $by) = ($self->{x}, $self->{y}); 776 my ($bx, $by) = ($self->{x}, $self->{y});
623 777
624 $self->{motion} = sub { 778 $self->{motion} = sub {
625 my ($ev, $x, $y) = @_; 779 my ($ev, $x, $y) = @_;
626 780
627 ($x, $y) = ($ev->motion_x, $ev->motion_y); 781 ($x, $y) = ($ev->{x}, $ev->{y});
628 782
629 $self->move ($bx + $x - $ox, $by + $y - $oy); 783 $self->move ($bx + $x - $ox, $by + $y - $oy);
630 $self->update; 784 $self->update;
631 }; 785 };
632 } 786 }
653 glEnable GL_BLEND; 807 glEnable GL_BLEND;
654 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 808 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
655 glEnable GL_TEXTURE_2D; 809 glEnable GL_TEXTURE_2D;
656 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 810 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
657 811
812 my $border = $self->border;
813
658 glColor @{ $self->{border_bg} }; 814 glColor @{ $self->{border_bg} };
659 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 815 $tex[1]->draw_quad (0, 0, $w, $border);
660 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 816 $tex[3]->draw_quad (0, $border, $border, $ch);
661 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 817 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
662 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 818 $tex[4]->draw_quad (0, $h - $border, $w, $border);
663 819
664 my $bg = $tex[0]; 820 my $bg = $tex[0];
665 821
666 # TODO: repeat texture not scale 822 # TODO: repeat texture not scale
667 my $rep_x = $cw / $bg->{w}; 823 my $rep_x = $cw / $bg->{w};
670 glColor @{ $self->{bg} }; 826 glColor @{ $self->{bg} };
671 827
672 $bg->{s} = $rep_x; 828 $bg->{s} = $rep_x;
673 $bg->{t} = $rep_y; 829 $bg->{t} = $rep_y;
674 $bg->{wrap_mode} = 1; 830 $bg->{wrap_mode} = 1;
675 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 831 $bg->draw_quad ($border, $border, $cw, $ch);
676 832
677 glDisable GL_TEXTURE_2D; 833 glDisable GL_TEXTURE_2D;
678 glDisable GL_BLEND; 834 glDisable GL_BLEND;
679 835
836 $self->{title}->draw if $self->{title};
680 $self->child->draw; 837 $self->child->draw;
681} 838}
682 839
683############################################################################# 840#############################################################################
684 841
686 843
687our @ISA = CFClient::UI::Base::; 844our @ISA = CFClient::UI::Base::;
688 845
689use List::Util qw(max sum); 846use List::Util qw(max sum);
690 847
691use SDL::OpenGL; 848use CFClient::OpenGL;
692 849
693sub new { 850sub new {
694 my $class = shift; 851 my $class = shift;
695 852
696 $class->SUPER::new ( 853 $class->SUPER::new (
703 my ($self, $x, $y, $child) = @_; 860 my ($self, $x, $y, $child) = @_;
704 861
705 $child->set_parent ($self); 862 $child->set_parent ($self);
706 $self->{children}[$y][$x] = $child; 863 $self->{children}[$y][$x] = $child;
707 864
708 $self->{w} = $self->{h} = -1; 865 $child->check_size;
709 $self->update;
710} 866}
711 867
712# TODO: move to container class maybe? send childs a signal on removal? 868# TODO: move to container class maybe? send childs a signal on removal?
713sub clear { 869sub clear {
714 my ($self) = @_; 870 my ($self) = @_;
727 or next; 883 or next;
728 884
729 for my $x (0 .. $#$row) { 885 for my $x (0 .. $#$row) {
730 my $widget = $row->[$x] 886 my $widget = $row->[$x]
731 or next; 887 or next;
732 my ($w, $h) = $widget->size_request; 888 my ($w, $h) = @$widget{qw(req_w req_h)};
733 889
734 $w[$x] = max $w[$x], $w; 890 $w[$x] = max $w[$x], $w;
735 $h[$y] = max $h[$y], $h; 891 $h[$y] = max $h[$y], $h;
736 } 892 }
737 } 893 }
749 (sum @$hs), 905 (sum @$hs),
750 ) 906 )
751} 907}
752 908
753sub size_allocate { 909sub size_allocate {
754 my ($self, $x, $y, $w, $h) = @_; 910 my ($self, $w, $h) = @_;
755
756 $self->_size_allocate ($x, $y, $w, $h) or return;
757 911
758 my ($ws, $hs) = $self->get_wh; 912 my ($ws, $hs) = $self->get_wh;
759 913
760 my $req_w = sum @$ws; 914 my $req_w = sum @$ws;
761 my $req_h = sum @$hs; 915 my $req_h = sum @$hs;
783 937
784 for my $c (0 .. $#$row) { 938 for my $c (0 .. $#$row) {
785 my $col_w = $ws->[$c]; 939 my $col_w = $ws->[$c];
786 940
787 if (my $widget = $row->[$c]) { 941 if (my $widget = $row->[$c]) {
788 $widget->size_allocate ($x, $y, $col_w, $row_h); 942 $widget->configure ($x, $y, $col_w, $row_h);
789 } 943 }
790 944
791 $x += $col_w; 945 $x += $col_w;
792 } 946 }
793 947
838 (List::Util::max map $_->[1], @alloc), 992 (List::Util::max map $_->[1], @alloc),
839 ) 993 )
840} 994}
841 995
842sub size_allocate { 996sub size_allocate {
843 my ($self, $x, $y, $w, $h) = @_; 997 my ($self, $w, $h) = @_;
844
845 $self->_size_allocate ($x, $y, $w, $h);
846 998
847 ($h, $w) = ($w, $h); 999 ($h, $w) = ($w, $h);
848 1000
849 my $children = $self->{children}; 1001 my $children = $self->{children};
850 1002
851 my @h = map +($_->size_request)[0], @$children; 1003 my @h = map $_->{req_w}, @$children;
852 1004
853 my $req_h = List::Util::sum @h; 1005 my $req_h = List::Util::sum @h;
854 1006
855 if ($req_h > $h) { 1007 if ($req_h > $h) {
856 # ah well, not enough space 1008 # ah well, not enough space
872 1024
873 my $y = 0; 1025 my $y = 0;
874 for (0 .. $#$children) { 1026 for (0 .. $#$children) {
875 my $child = $children->[$_]; 1027 my $child = $children->[$_];
876 my $h = $h[$_]; 1028 my $h = $h[$_];
877 $child->size_allocate ($y, 0, $h, $w); 1029 $child->configure ($y, 0, $h, $w);
878 1030
879 $y += $h; 1031 $y += $h;
880 } 1032 }
1033
1034 1
881} 1035}
882 1036
883############################################################################# 1037#############################################################################
884 1038
885package CFClient::UI::VBox; 1039package CFClient::UI::VBox;
898 (List::Util::sum map $_->[1], @alloc), 1052 (List::Util::sum map $_->[1], @alloc),
899 ) 1053 )
900} 1054}
901 1055
902sub size_allocate { 1056sub size_allocate {
903 my ($self, $x, $y, $w, $h) = @_; 1057 my ($self, $w, $h) = @_;
904
905 $self->_size_allocate ($x, $y, $w, $h);
906 1058
907 my $children = $self->{children}; 1059 my $children = $self->{children};
908 1060
909 my @h = map +($_->size_request)[1], @$children; 1061 my @h = map $_->{req_h}, @$children;
910 1062
911 my $req_h = List::Util::sum @h; 1063 my $req_h = List::Util::sum @h;
912 1064
913 if ($req_h > $h) { 1065 if ($req_h > $h) {
914 # ah well, not enough space 1066 # ah well, not enough space
928 1080
929 my $y = 0; 1081 my $y = 0;
930 for (0 .. $#$children) { 1082 for (0 .. $#$children) {
931 my $child = $children->[$_]; 1083 my $child = $children->[$_];
932 my $h = $h[$_]; 1084 my $h = $h[$_];
933 $child->size_allocate (0, $y, $w, $h); 1085 $child->configure (0, $y, $w, $h);
934 1086
935 $y += $h; 1087 $y += $h;
936 } 1088 }
1089
1090 1
937} 1091}
938 1092
939############################################################################# 1093#############################################################################
940 1094
941package CFClient::UI::Label; 1095package CFClient::UI::Label;
942 1096
943our @ISA = CFClient::UI::Base::; 1097our @ISA = CFClient::UI::Base::;
944 1098
945use SDL::OpenGL; 1099use CFClient::OpenGL;
946 1100
947sub new { 1101sub new {
948 my ($class, %arg) = @_; 1102 my ($class, %arg) = @_;
949 1103
950 my $self = $class->SUPER::new ( 1104 my $self = $class->SUPER::new (
951 fg => [1, 1, 1], 1105 fg => [1, 1, 1],
1106 #font => default_font
952 fontsize => $::FONTSIZE, 1107 fontsize => 1,
953 text => "", 1108 text => "",
954 align => -1, 1109 align => -1,
1110 valign => -1,
955 padding => 2, 1111 padding => 2,
956 layout => new CFClient::Layout, 1112 layout => new CFClient::Layout,
1113 can_events => 0,
957 %arg 1114 %arg
958 ); 1115 );
959 1116
960 $self->set_text ($self->{text}); 1117 if (exists $self->{template}) {
1118 my $layout = new CFClient::Layout;
1119 $layout->set_text (delete $self->{template});
1120 $self->{template} = $layout;
1121 }
1122
1123 $self->set_text (delete $self->{text}) if exists $self->{text};
1124 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
961 1125
962 $self 1126 $self
963} 1127}
964 1128
965sub escape_text { 1129sub escape_text {
973} 1137}
974 1138
975sub set_text { 1139sub set_text {
976 my ($self, $text) = @_; 1140 my ($self, $text) = @_;
977 1141
978 $self->{text} = $text;
979 $self->{layout}->set_markup ($text); 1142 $self->{layout}->set_text ($text);
980 1143
981 delete $self->{texture}; 1144 delete $self->{texture};
982# $self->{w} = $self->{h} = -1; 1145 $self->check_size;
983 $self->update; 1146 $self->update;
984} 1147}
985 1148
986sub get_text { 1149sub set_markup {
987 my ($self, $text) = @_; 1150 my ($self, $markup) = @_;
988 1151
989 $self->{text} 1152 $self->{layout}->set_markup ($markup);
1153
1154 delete $self->{texture};
1155 $self->check_size;
1156 $self->update;
990} 1157}
991 1158
992sub size_request { 1159sub size_request {
993 my ($self) = @_; 1160 my ($self) = @_;
994 1161
995 $self->{layout}->set_width; 1162 $self->{layout}->set_font ($self->{font}) if $self->{font};
1163 $self->{layout}->set_width ($self->{max_w} || -1);
996 $self->{layout}->set_height ($self->{fontsize}); 1164 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1165
997 my ($w, $h) = $self->{layout}->size; 1166 my ($w, $h) = $self->{layout}->size;
1167
1168 if (exists $self->{template}) {
1169 $self->{template}->set_font ($self->{font}) if $self->{font};
1170 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1171
1172 my ($w2, $h2) = $self->{template}->size;
1173
1174 $w = List::Util::max $w, $w2;
1175 $h = List::Util::max $h, $h2;
1176 }
998 1177
999 ( 1178 (
1000 $w + $self->{padding} * 2, 1179 $w + $self->{padding} * 2,
1001 $h + $self->{padding} * 2, 1180 $h + $self->{padding} * 2,
1002 ) 1181 )
1003} 1182}
1004 1183
1005sub size_allocate { 1184sub size_allocate {
1006 my ($self, $x, $y, $w, $h) = @_; 1185 my ($self, $w, $h) = @_;
1007
1008 $self->_size_allocate ($x, $y, $w, $h) or return;
1009 1186
1010 delete $self->{texture}; 1187 delete $self->{texture};
1011} 1188}
1012 1189
1013sub update { 1190sub set_fontsize {
1014 my ($self) = @_; 1191 my ($self, $fontsize) = @_;
1015 1192
1193 $self->{fontsize} = $fontsize;
1016 delete $self->{texture}; 1194 delete $self->{texture};
1195 $self->check_size;
1017 $self->SUPER::update; 1196 $self->update;
1018} 1197}
1019 1198
1020sub _draw { 1199sub _draw {
1021 my ($self) = @_; 1200 my ($self) = @_;
1022 1201
1023 my $tex = $self->{texture} ||= do { 1202 my $tex = $self->{texture} ||= do {
1203 $self->{layout}->set_font ($self->{font}) if $self->{font};
1024 $self->{layout}->set_width ($self->{w}); 1204 $self->{layout}->set_width ($self->{w});
1025 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1205 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1026 new_from_layout CFClient::Texture $self->{layout} 1206 new_from_layout CFClient::Texture $self->{layout}
1027 }; 1207 };
1028 1208
1029 glEnable GL_BLEND; 1209 glEnable GL_BLEND;
1030 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1031 glEnable GL_TEXTURE_2D; 1211 glEnable GL_TEXTURE_2D;
1032 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1212 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1033 1213
1034 glColor @{$self->{fg}}; 1214 glColor @{$self->{fg}};
1035 1215
1036 my $x = 1216 $self->{ox} = int (
1037 $self->{align} < 0 ? $self->{padding} 1217 $self->{align} < 0 ? $self->{padding}
1038 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1218 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1039 : ($self->{w} - $tex->{w}) * 0.5; 1219 : ($self->{w} - $tex->{w}) * 0.5
1220 );
1040 1221
1041 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1222 $self->{oy} = int (
1223 $self->{valign} < 0 ? $self->{padding}
1224 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1225 : ($self->{h} - $tex->{h}) * 0.5
1226 );
1227
1228 $tex->draw_quad ($self->{ox}, $self->{oy});
1042 1229
1043 glDisable GL_TEXTURE_2D; 1230 glDisable GL_TEXTURE_2D;
1044 glDisable GL_BLEND; 1231 glDisable GL_BLEND;
1045} 1232}
1046 1233
1047############################################################################# 1234#############################################################################
1048 1235
1049package CFClient::UI::Entry; 1236package CFClient::UI::EntryBase;
1050 1237
1051our @ISA = CFClient::UI::Label::; 1238our @ISA = CFClient::UI::Label::;
1052 1239
1053use SDL; 1240use CFClient::OpenGL;
1054use SDL::OpenGL;
1055 1241
1056sub new { 1242sub new {
1057 my $class = shift; 1243 my $class = shift;
1058 1244
1059 $class->SUPER::new ( 1245 $class->SUPER::new (
1061 bg => [0, 0, 0, 0.2], 1247 bg => [0, 0, 0, 0.2],
1062 active_bg => [1, 1, 1, 0.5], 1248 active_bg => [1, 1, 1, 0.5],
1063 active_fg => [0, 0, 0], 1249 active_fg => [0, 0, 0],
1064 can_hover => 1, 1250 can_hover => 1,
1065 can_focus => 1, 1251 can_focus => 1,
1252 valign => 0,
1253 can_events => 1,
1066 @_ 1254 @_
1067 ) 1255 )
1068} 1256}
1069 1257
1070sub _set_text { 1258sub _set_text {
1071 my ($self, $text) = @_; 1259 my ($self, $text) = @_;
1072 1260
1073 my $old_text = $self->{text}; 1261 delete $self->{cur_h};
1262
1263 return if $self->{text} eq $text;
1264
1265 delete $self->{texture};
1074 1266
1075 $self->{last_activity} = $::NOW; 1267 $self->{last_activity} = $::NOW;
1076
1077 $self->{text} = $text; 1268 $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 1269
1081 $text =~ s/./*/g if $self->{hidden}; 1270 $text =~ s/./*/g if $self->{hidden};
1271 $self->{layout}->set_text ("$text ");
1082 1272
1083 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1273 $self->emit (changed => $self->{text});
1274}
1084 1275
1085 $text = substr $text, 0, $self->{cursor}; 1276sub get_text {
1086 utf8::encode $text; 1277 $_[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} 1278}
1093 1279
1094sub size_request { 1280sub size_request {
1095 my ($self) = @_; 1281 my ($self) = @_;
1096 1282
1098 1284
1099 ($w + 1, $h) # add 1 for cursor 1285 ($w + 1, $h) # add 1 for cursor
1100} 1286}
1101 1287
1102sub size_allocate { 1288sub size_allocate {
1103 my ($self, $x, $y, $w, $h) = @_; 1289 my ($self, $w, $h) = @_;
1104 1290
1105 $self->SUPER::size_allocate ($x, $y, $w, $h); 1291 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1106
1107 $self->_set_text ($self->{text});
1108} 1292}
1109 1293
1110sub set_text { 1294sub set_text {
1111 my ($self, $text) = @_; 1295 my ($self, $text) = @_;
1112 1296
1116} 1300}
1117 1301
1118sub key_down { 1302sub key_down {
1119 my ($self, $ev) = @_; 1303 my ($self, $ev) = @_;
1120 1304
1121 my $mod = $ev->key_mod; 1305 my $mod = $ev->{mod};
1122 my $sym = $ev->key_sym; 1306 my $sym = $ev->{sym};
1123
1124 my $uni = $ev->key_unicode; 1307 my $uni = $ev->{unicode};
1125 1308
1126 my $text = $self->get_text; 1309 my $text = $self->get_text;
1127 1310
1128 if ($sym == SDLK_BACKSPACE) { 1311 if ($sym == 8) {
1129 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1312 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1130 } elsif ($sym == SDLK_DELETE) { 1313 } elsif ($sym == 127) {
1131 substr $text, $self->{cursor}, 1, ""; 1314 substr $text, $self->{cursor}, 1, "";
1132 } elsif ($sym == SDLK_LEFT) { 1315 } elsif ($sym == CFClient::SDLK_LEFT) {
1133 --$self->{cursor} if $self->{cursor}; 1316 --$self->{cursor} if $self->{cursor};
1134 } elsif ($sym == SDLK_RIGHT) { 1317 } elsif ($sym == CFClient::SDLK_RIGHT) {
1135 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1318 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1136 } elsif ($sym == SDLK_HOME) { 1319 } elsif ($sym == CFClient::SDLK_HOME) {
1137 $self->{cursor} = 0; 1320 $self->{cursor} = 0;
1138 } elsif ($sym == SDLK_END) { 1321 } elsif ($sym == CFClient::SDLK_END) {
1139 $self->{cursor} = length $text; 1322 $self->{cursor} = length $text;
1140 } elsif ($sym == SDLK_ESCAPE) { 1323 } elsif ($sym == 27) {
1141 $self->emit ('escape'); 1324 $self->emit ('escape');
1142 } elsif ($uni) { 1325 } elsif ($uni) {
1143 substr $text, $self->{cursor}++, 0, chr $uni; 1326 substr $text, $self->{cursor}++, 0, chr $uni;
1144 } 1327 }
1145 1328
1200 1383
1201 $self->SUPER::_draw; 1384 $self->SUPER::_draw;
1202 1385
1203 #TODO: force update every cursor change :( 1386 #TODO: force update every cursor change :(
1204 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1387 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1388
1389 unless (exists $self->{cur_h}) {
1390 my $text = substr $self->{text}, 0, $self->{cursor};
1391 utf8::encode $text;
1392
1393 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1394 }
1395
1205 glColor @{$self->{fg}}; 1396 glColor @{$self->{fg}};
1206 glBegin GL_LINES; 1397 glBegin GL_LINES;
1207 glVertex $self->{cur_x}, $self->{cur_y};
1208 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1398 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1399 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1209 glEnd; 1400 glEnd;
1210 } 1401 }
1211} 1402}
1212 1403
1213package CFClient::UI::LineEntry; 1404package CFClient::UI::Entry;
1214 1405
1215our @ISA = CFClient::UI::Entry::; 1406our @ISA = CFClient::UI::EntryBase::;
1216 1407
1217use SDL; 1408use CFClient::OpenGL;
1218use SDL::OpenGL;
1219 1409
1220sub key_down { 1410sub key_down {
1221 my ($self, $ev) = @_; 1411 my ($self, $ev) = @_;
1222 1412
1223 my $sym = $ev->key_sym; 1413 my $sym = $ev->{sym};
1224 1414
1225 if ($sym == SDLK_RETURN) { 1415 if ($sym == 13) {
1226 $self->emit (activate => $self->get_text); 1416 $self->emit (activate => $self->get_text);
1227 $self->update; 1417 $self->update;
1228 1418
1229 } else { 1419 } else {
1230 $self->SUPER::key_down ($ev); 1420 $self->SUPER::key_down ($ev);
1236 1426
1237package CFClient::UI::Button; 1427package CFClient::UI::Button;
1238 1428
1239our @ISA = CFClient::UI::Label::; 1429our @ISA = CFClient::UI::Label::;
1240 1430
1241use SDL; 1431use CFClient::OpenGL;
1242use SDL::OpenGL;
1243 1432
1244my @tex = 1433my @tex =
1245 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1434 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1246 qw(b1_button_active.png); 1435 qw(b1_button_active.png);
1247 1436
1248sub new { 1437sub new {
1249 my $class = shift; 1438 my $class = shift;
1250 1439
1251 $class->SUPER::new ( 1440 $class->SUPER::new (
1252 padding => 4, 1441 padding => 4,
1253 fg => [1, 1, 1], 1442 fg => [1, 1, 1],
1254 bg => [1, 1, 1, 0.2], 1443 bg => [1, 1, 1, 0.2],
1255 active_fg => [1, 1, 0], 1444 active_fg => [0, 0, 1],
1256 can_hover => 1, 1445 can_hover => 1,
1446 align => 0,
1447 valign => 0,
1448 can_events => 1,
1257 @_ 1449 @_
1258 ) 1450 )
1259} 1451}
1260 1452
1261sub button_up { 1453sub button_up {
1269 1461
1270sub _draw { 1462sub _draw {
1271 my ($self) = @_; 1463 my ($self) = @_;
1272 1464
1273 local $self->{fg} = $self->{fg}; 1465 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 1466
1280 if ($GRAB == $self) { 1467 if ($GRAB == $self) {
1281 $self->{fg} = $self->{active_fg}; 1468 $self->{fg} = $self->{active_fg};
1282 } 1469 }
1283 1470
1284 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1471 glEnable GL_BLEND;
1472 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1473 glEnable GL_TEXTURE_2D;
1285 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1474 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1475 glColor 0, 0, 0, 1;
1286 1476
1287 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1477 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1288 1478
1289 glDisable GL_TEXTURE_2D; 1479 glDisable GL_TEXTURE_2D;
1290 glDisable GL_BLEND; 1480 glDisable GL_BLEND;
1291 1481
1292 $self->SUPER::_draw; 1482 $self->SUPER::_draw;
1297package CFClient::UI::CheckBox; 1487package CFClient::UI::CheckBox;
1298 1488
1299our @ISA = CFClient::UI::DrawBG::; 1489our @ISA = CFClient::UI::DrawBG::;
1300 1490
1301my @tex = 1491my @tex =
1302 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1492 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1303 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1493 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1304 1494
1305use SDL; 1495use CFClient::OpenGL;
1306use SDL::OpenGL;
1307 1496
1308sub new { 1497sub new {
1309 my $class = shift; 1498 my $class = shift;
1310 1499
1311 $class->SUPER::new ( 1500 $class->SUPER::new (
1322 my ($self) = @_; 1511 my ($self) = @_;
1323 1512
1324 ($self->{padding} * 2 + 6) x 2 1513 ($self->{padding} * 2 + 6) x 2
1325} 1514}
1326 1515
1327sub size_allocate {
1328 my ($self, $x, $y, $w, $h) = @_;
1329
1330 $self->_size_allocate ($x, $y, $w, $h);
1331}
1332
1333sub button_down { 1516sub button_down {
1334 my ($self, $ev, $x, $y) = @_; 1517 my ($self, $ev, $x, $y) = @_;
1335 1518
1336 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1519 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1337 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1520 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1363 glDisable GL_BLEND; 1546 glDisable GL_BLEND;
1364} 1547}
1365 1548
1366############################################################################# 1549#############################################################################
1367 1550
1551package CFClient::UI::Image;
1552
1553our @ISA = CFClient::UI::Base::;
1554
1555use CFClient::OpenGL;
1556use Carp qw/confess/;
1557
1558our %loaded_images;
1559
1560sub new {
1561 my $class = shift;
1562
1563 my $self = $class->SUPER::new (can_events => 0, @_);
1564
1565 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1566
1567 $loaded_images{$self->{image}} ||=
1568 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1569
1570 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1571
1572 Scalar::Util::weaken $loaded_images{$self->{image}};
1573
1574 $self->{aspect} = $tex->{w} / $tex->{h};
1575
1576 $self
1577}
1578
1579sub size_request {
1580 my ($self) = @_;
1581
1582 ($self->{tex}->{w}, $self->{tex}->{h})
1583}
1584
1585sub _draw {
1586 my ($self) = @_;
1587
1588 my $tex = $self->{tex};
1589
1590 my ($w, $h) = ($self->{w}, $self->{h});
1591
1592 if ($self->{rot90}) {
1593 glRotate 90, 0, 0, 1;
1594 glTranslate 0, -$self->{w}, 0;
1595
1596 ($w, $h) = ($h, $w);
1597 }
1598
1599 glEnable GL_BLEND;
1600 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1601 glEnable GL_TEXTURE_2D;
1602 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1603
1604 $tex->draw_quad (0, 0, $w, $h);
1605
1606 glDisable GL_BLEND;
1607 glDisable GL_TEXTURE_2D;
1608}
1609
1610#############################################################################
1611
1612package CFClient::UI::VGauge;
1613
1614our @ISA = CFClient::UI::Base::;
1615
1616use List::Util qw(min max);
1617
1618use CFClient::OpenGL;
1619
1620my %tex = (
1621 food => [
1622 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1623 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1624 ],
1625 grace => [
1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1627 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1628 ],
1629 hp => [
1630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1631 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1632 ],
1633 mana => [
1634 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1635 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1636 ],
1637);
1638
1639# eg. VGauge->new (gauge => 'food'), default gauge: food
1640sub new {
1641 my $class = shift;
1642
1643 my $self = $class->SUPER::new (
1644 type => 'food',
1645 @_
1646 );
1647
1648 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1649
1650 $self
1651}
1652
1653sub size_request {
1654 my ($self) = @_;
1655
1656 #my $tex = $tex{$self->{type}}[0];
1657 #@$tex{qw(w h)}
1658 (0, 0)
1659}
1660
1661sub set_max {
1662 my ($self, $max) = @_;
1663
1664 $self->{max_val} = $max;
1665}
1666
1667sub set_value {
1668 my ($self, $val, $max) = @_;
1669
1670 $self->set_max ($max)
1671 if defined $max;
1672
1673 $max = $self->{max_val};
1674 $self->{val} = $val;
1675
1676 $self->update;
1677}
1678
1679sub _draw {
1680 my ($self) = @_;
1681
1682 my $tex = $tex{$self->{type}};
1683 my ($t1, $t2, $t3) = @$tex;
1684
1685 my ($w, $h) = ($self->{w}, $self->{h});
1686
1687 if ($self->{vertical}) {
1688 glRotate 90, 0, 0, 1;
1689 glTranslate 0, -$self->{w}, 0;
1690
1691 ($w, $h) = ($h, $w);
1692 }
1693
1694 my $ycut = $self->{val} / ($self->{max_val} || 1);
1695
1696 my $ycut1 = max 0, min 1, $ycut;
1697 my $ycut2 = max 0, min 1, $ycut - 1;
1698
1699 my $h1 = $self->{h} * (1 - $ycut1);
1700 my $h2 = $self->{h} * (1 - $ycut2);
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 glBindTexture GL_TEXTURE_2D, $t1->{name};
1708 glBegin GL_QUADS;
1709 glTexCoord 0 , 0; glVertex 0 , 0;
1710 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1711 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1712 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1713 glEnd;
1714
1715 my $ycut1 = List::Util::min 1, $ycut;
1716 glBindTexture GL_TEXTURE_2D, $t2->{name};
1717 glBegin GL_QUADS;
1718 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1719 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1720 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1721 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1722 glEnd;
1723
1724 if ($t3) {
1725 glBindTexture GL_TEXTURE_2D, $t3->{name};
1726 glBegin GL_QUADS;
1727 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1728 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1729 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1730 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1731 glEnd;
1732 }
1733
1734 glDisable GL_BLEND;
1735 glDisable GL_TEXTURE_2D;
1736}
1737
1738#############################################################################
1739
1740package CFClient::UI::Gauge;
1741
1742our @ISA = CFClient::UI::VBox::;
1743
1744sub new {
1745 my ($class, %arg) = @_;
1746
1747 my $self = $class->SUPER::new (
1748 tooltip => $arg{type},
1749 %arg,
1750 );
1751
1752 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999", can_events => 1, can_hover => 1);
1753 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_events => 1, can_hover => 1);
1754 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999", can_events => 1, can_hover => 1);
1755
1756 $self
1757}
1758
1759sub set_fontsize {
1760 my ($self, $fsize) = @_;
1761
1762 $self->{value}->set_fontsize ($fsize);
1763 $self->{max} ->set_fontsize ($fsize);
1764}
1765
1766sub set_value {
1767 my ($self, $val, $max) = @_;
1768
1769 $self->set_max ($max)
1770 if defined $max;
1771
1772 $self->{gauge}->set_value ($val, $max);
1773 $self->{value}->set_text ($val);
1774}
1775
1776sub set_max {
1777 my ($self, $max) = @_;
1778
1779 $self->{gauge}->set_max ($max);
1780 $self->{max}->set_text ($max);
1781}
1782
1783#############################################################################
1784
1368package CFClient::UI::Slider; 1785package CFClient::UI::Slider;
1369 1786
1370use strict; 1787use strict;
1371 1788
1372use SDL::OpenGL; 1789use CFClient::OpenGL;
1373 1790
1374our @ISA = CFClient::UI::DrawBG::; 1791our @ISA = CFClient::UI::DrawBG::;
1375 1792
1376my @tex = 1793my @tex =
1377 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1794 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1387 # TODO: calculations are off 1804 # TODO: calculations are off
1388 my $self = $class->SUPER::new ( 1805 my $self = $class->SUPER::new (
1389 fg => [1, 1, 1], 1806 fg => [1, 1, 1],
1390 active_fg => [0, 0, 0], 1807 active_fg => [0, 0, 0],
1391 range => [0, 0, 100, 10], 1808 range => [0, 0, 100, 10],
1392 req_w => 40, 1809 req_w => 20,
1393 req_h => 13, 1810 req_h => 20,
1394 vertical => 0, 1811 vertical => 0,
1395 can_hover => 1, 1812 can_hover => 1,
1396 inner_pad => 5, 1813 inner_pad => 5,
1397 @_ 1814 @_
1398 ); 1815 );
1502 1919
1503package CFClient::UI::TextView; 1920package CFClient::UI::TextView;
1504 1921
1505our @ISA = CFClient::UI::HBox::; 1922our @ISA = CFClient::UI::HBox::;
1506 1923
1507use SDL::OpenGL; 1924use CFClient::OpenGL;
1508 1925
1509sub new { 1926sub new {
1510 my $class = shift; 1927 my $class = shift;
1511 1928
1512 my $self = $class->SUPER::new ( 1929 my $self = $class->SUPER::new (
1513 req_w => $::WIDTH / 6,
1514 req_h => $::HEIGHT / 6,
1515 fontsize => $::FONTSIZE, 1930 fontsize => 1,
1931 #font => default_font
1516 @_, 1932 @_,
1517 1933
1518 layout => (new CFClient::Layout), 1934 layout => (new CFClient::Layout),
1519 par => [], 1935 par => [],
1520 height => 0, 1936 height => 0,
1541sub text_height { 1957sub text_height {
1542 my ($self, $text) = @_; 1958 my ($self, $text) = @_;
1543 1959
1544 my $layout = $self->{layout}; 1960 my $layout = $self->{layout};
1545 1961
1546 $layout->set_height ($self->{fontsize}); 1962 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1547 $layout->set_width ($self->{w}); 1963 $layout->set_width ($self->{w});
1548 $layout->set_text ($text); 1964 $layout->set_text ($text);
1549 1965
1550 ($layout->size)[1] 1966 ($layout->size)[1]
1551} 1967}
1555 1971
1556 $self->{need_reflow}++; 1972 $self->{need_reflow}++;
1557 $self->update; 1973 $self->update;
1558} 1974}
1559 1975
1560sub size_request {
1561 my ($self) = @_;
1562
1563 ($self->{req_w}, $self->{req_h})
1564}
1565
1566sub size_allocate { 1976sub size_allocate {
1567 my ($self, $x, $y, $w, $h) = @_; 1977 my ($self, $w, $h) = @_;
1568 1978
1569 $self->SUPER::size_allocate ($x, $y, $w, $h); 1979 $self->SUPER::size_allocate ($w, $h);
1570 1980
1981 $self->{layout}->set_font ($self->{font}) if $self->{font};
1571 $self->{layout}->set_height ($self->{fontsize}); 1982 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1572 $self->{layout}->set_width ($self->{w}); 1983 $self->{layout}->set_width ($self->{children}[0]{w});
1573 1984
1574 $self->reflow; 1985 $self->reflow;
1575} 1986}
1576 1987
1577sub add_paragraph { 1988sub add_paragraph {
1627 my $y1 = $top + $self->{h}; 2038 my $y1 = $top + $self->{h};
1628 2039
1629 my $y = 0; 2040 my $y = 0;
1630 2041
1631 my $layout = $self->{layout}; 2042 my $layout = $self->{layout};
2043
2044 $layout->set_font ($self->{font}) if $self->{font};
1632 2045
1633 for my $par (@{$self->{par}}) { 2046 for my $par (@{$self->{par}}) {
1634 my $h = $par->[0]; 2047 my $h = $par->[0];
1635 2048
1636 if ($y0 < $y + $h && $y < $y1) { 2049 if ($y0 < $y + $h && $y < $y1) {
1664 2077
1665} 2078}
1666 2079
1667############################################################################# 2080#############################################################################
1668 2081
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; 2082package CFClient::UI::Animator;
1904 2083
1905use SDL::OpenGL; 2084use CFClient::OpenGL;
1906 2085
1907our @ISA = CFClient::UI::Bin::; 2086our @ISA = CFClient::UI::Bin::;
1908 2087
1909sub moveto { 2088sub moveto {
1910 my ($self, $x, $y) = @_; 2089 my ($self, $x, $y) = @_;
1950 my $class = shift; 2129 my $class = shift;
1951 2130
1952 my $self = $class->SUPER::new ( 2131 my $self = $class->SUPER::new (
1953 state => 0, 2132 state => 0,
1954 connect_activate => \&toggle_flopper, 2133 connect_activate => \&toggle_flopper,
2134 can_events => 1,
1955 @_ 2135 @_
1956 ); 2136 );
1957 2137
1958 if ($self->{state}) { 2138 if ($self->{state}) {
1959 $self->{state} = 0; 2139 $self->{state} = 0;
1979 $self->emit (changed => $self->{state}); 2159 $self->emit (changed => $self->{state});
1980} 2160}
1981 2161
1982############################################################################# 2162#############################################################################
1983 2163
2164package CFClient::UI::Tooltip;
2165
2166our @ISA = CFClient::UI::Bin::;
2167
2168use CFClient::OpenGL;
2169
2170sub new {
2171 my $class = shift;
2172
2173 $class->SUPER::new (
2174 @_,
2175 can_events => 0,
2176 )
2177}
2178
2179sub set_text {
2180 my ($self, $text) = @_;
2181
2182 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2183 $self->{label}->set_text ($text);
2184 $self->add ($self->{label});
2185}
2186
2187sub size_request {
2188 my ($self) = @_;
2189
2190 $self->child->set_max_size ($::WIDTH * 0.2);
2191
2192 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2193
2194 ($w + 4, $h + 4)
2195}
2196
2197sub _draw {
2198 my ($self) = @_;
2199
2200 glPushMatrix;
2201 glTranslate 0.375, 0.375;
2202
2203 my ($w, $h) = @$self{qw(w h)};
2204
2205 glColor 1, 0.8, 0.4;
2206 glBegin GL_QUADS;
2207 glVertex 0 , 0;
2208 glVertex 0 , $h;
2209 glVertex $w, $h;
2210 glVertex $w, 0;
2211 glEnd;
2212
2213 glColor 0, 0, 0;
2214 glBegin GL_LINE_LOOP;
2215 glVertex 0 , 0;
2216 glVertex 0 , $h;
2217 glVertex $w, $h;
2218 glVertex $w, 0;
2219 glEnd;
2220
2221 glPopMatrix;
2222
2223 glTranslate 2, 2;
2224 $self->SUPER::_draw;
2225}
2226
2227#############################################################################
2228
1984package CFClient::UI::Root; 2229package CFClient::UI::Root;
1985 2230
1986our @ISA = CFClient::UI::Container::; 2231our @ISA = CFClient::UI::Container::;
1987 2232
1988use SDL::OpenGL; 2233use CFClient::OpenGL;
2234
2235sub check_size {
2236 my ($self) = @_;
2237
2238 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2239}
1989 2240
1990sub size_request { 2241sub size_request {
1991 ($::WIDTH, $::HEIGHT) 2242 ($::WIDTH, $::HEIGHT)
1992} 2243}
1993 2244
1994sub size_allocate { 2245sub configure {
1995 my ($self, $x, $y, $w, $h) = @_; 2246 my ($self, $x, $y, $w, $h) = @_;
1996 2247
1997 $self->_size_allocate ($x, $y, $w, $h); 2248 $self->SUPER::configure ($x, $y, $w, $h);
1998 2249
1999 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2000 for @{$self->{children}}; 2250 for my $child (@{$self->{children}}) {
2251 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2252
2253 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2254 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2255 $child->configure ($X, $Y, $W,$H);
2256 }
2001} 2257}
2002 2258
2003sub _topleft { 2259sub _topleft {
2004 my ($self, $x, $y) = @_; 2260 my ($self, $x, $y) = @_;
2005 2261
2007} 2263}
2008 2264
2009sub update { 2265sub update {
2010 my ($self) = @_; 2266 my ($self) = @_;
2011 2267
2012 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2268 $self->check_size;
2013 ::refresh (); 2269 ::refresh ();
2014} 2270}
2015 2271
2016sub add { 2272sub add {
2017 my ($self, $widget) = @_; 2273 my ($self, $child) = @_;
2018 2274
2275 # integerize window positions
2276 $child->{x} = int $child->{x};
2277 $child->{y} = int $child->{y};
2278
2019 $self->SUPER::add ($widget); 2279 $self->SUPER::add ($child);
2020
2021 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2022} 2280}
2023 2281
2024sub on_refresh { 2282sub on_refresh {
2025 my ($self, $id, $cb) = @_; 2283 my ($self, $id, $cb) = @_;
2026 2284
2050############################################################################# 2308#############################################################################
2051 2309
2052package CFClient::UI; 2310package CFClient::UI;
2053 2311
2054$ROOT = new CFClient::UI::Root; 2312$ROOT = new CFClient::UI::Root;
2313$TOOLTIP = new CFClient::UI::Tooltip;
2055 2314
20561 23151
2057 2316

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines