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.126 by root, Mon Apr 17 20:59:20 2006 UTC vs.
Revision 1.162 by root, Mon Apr 24 06:05:35 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 (length $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_markup ($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 destroy {
148 my ($self) = @_;
149
150 $self->hide;
151 %$self = ();
152}
153
154sub show {
155 my ($self) = @_;
156
157 return if $self->{parent};
158
159 $CFClient::UI::ROOT->add ($self);
160}
161
162sub hide {
163 my ($self) = @_;
164
165 return unless $self->{parent};
166
167 $self->{parent}->remove ($self);
168}
169
115sub move { 170sub move {
116 my ($self, $x, $y, $z) = @_; 171 my ($self, $x, $y, $z) = @_;
172
117 $self->{x} = int $x; 173 $self->{x} = int $x;
118 $self->{y} = int $y; 174 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 175 $self->{z} = $z if defined $z;
120}
121 176
122sub needs_redraw { 177 $self->update;
123 0 178}
179
180sub set_size {
181 my ($self, $w, $h) = @_;
182
183 $self->{user_w} = $w;
184 $self->{user_h} = $h;
185
186 $self->check_size;
124} 187}
125 188
126sub size_request { 189sub size_request {
127 require Carp; 190 require Carp;
128 Carp::confess "size_request is abtract"; 191 Carp::confess "size_request is abstract";
129} 192}
130 193
131sub _size_allocate { 194sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 195 my ($self, $x, $y, $w, $h) = @_;
133 196
197 if ($self->{aspect}) {
198 my $w2 = List::Util::min $w, int $h * $self->{aspect};
199 my $h2 = List::Util::min $h, int $w / $self->{aspect};
200
201 # use alignment to adjust x, y
202
203 $x += int +($w - $w2) * 0.5;
204 $y += int +($h - $h2) * 0.5;
205
206 ($w, $h) = ($w2, $h2);
207 }
208
209 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 210 $self->{x} = $x;
135 $self->{y} = $y; 211 $self->{y} = $y;
212 $self->update;
213 }
136 214
137 return unless $self->{w} != $w || $self->{h} != $h; 215 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 216 $self->{w} = $w;
140 $self->{h} = $h; 217 $self->{h} = $h;
141 218
219 $self->size_allocate ($w, $h);
220 $self->update;
142 1 221 }
143} 222}
144 223
145sub size_allocate { 224sub size_allocate {
225 # nothing to be done
226}
227
228sub set_max_size {
146 my ($self, $x, $y, $w, $h) = @_; 229 my ($self, $w, $h) = @_;
147 230
148 $self->_size_allocate ($x, $y, $w, $h); 231 delete $self->{max_w}; $self->{max_w} = $w if $w;
232 delete $self->{max_h}; $self->{max_h} = $h if $h;
149} 233}
150 234
151# return top left coordinates 235# return top left coordinates
152sub _topleft { 236sub _topleft {
153 my ($self, $x, $y) = @_; 237 my ($self, $x, $y) = @_;
236 glVertex $x + $self->{w}, $y + $self->{h}; 320 glVertex $x + $self->{w}, $y + $self->{h};
237 glVertex $x , $y + $self->{h}; 321 glVertex $x , $y + $self->{h};
238 glEnd; 322 glEnd;
239 glDisable GL_BLEND; 323 glDisable GL_BLEND;
240 } 324 }
325
326 if ($ENV{PCLIENT_DEBUG}) {
327 glPushMatrix;
328 glColor 1, 1, 0, 1;
329 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
330 glBegin GL_LINE_LOOP;
331 glVertex 0 , 0;
332 glVertex $self->{w}, 0;
333 glVertex $self->{w}, $self->{h};
334 glVertex 0 , $self->{h};
335 glEnd;
336 glPopMatrix;
337 CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
338 }
241} 339}
242 340
243sub _draw { 341sub _draw {
244 my ($self) = @_; 342 my ($self) = @_;
245 343
246 warn "no draw defined for $self\n"; 344 warn "no draw defined for $self\n";
247} 345}
248 346
249sub find_widget { 347sub find_widget {
250 my ($self, $x, $y) = @_; 348 my ($self, $x, $y) = @_;
349
350 return () unless $self->{can_events};
251 351
252 return $self 352 return $self
253 if $x >= $self->{x} && $x < $self->{x} + $self->{w} 353 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
254 && $y >= $self->{y} && $y < $self->{y} + $self->{h}; 354 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
255 355
263} 363}
264 364
265sub check_size { 365sub check_size {
266 my ($self) = @_; 366 my ($self) = @_;
267 367
268 my ($w, $h) = $self->size_request; 368 $self->{parent}
369 or return 1;
269 370
371 my ($w, $h) = $self->{user_w} && $self->{user_h}
372 ? @$self{qw(user_w user_h)}
373 : $self->size_request;
374
270 if ($w != $self->{req_w} || $h != $self->{req_h}) { 375 if ($w != $self->{req_w} || $h != $self->{req_h}) {
271 $self->{req_w} = $w; 376 $self->{req_w} = $w;
272 $self->{req_h} = $h; 377 $self->{req_h} = $h;
273 378
274 $self->{parent}->check_size 379 $self->{parent}->check_size
275 if $self->{parent}; 380 or $self->size_allocate (
381 (List::Util::max $self->{w}, $w),
382 (List::Util::max $self->{h}, $h),
383 );
384
385 1
386 } else {
387 0
276 } 388 }
277} 389}
278 390
279sub update { 391sub update {
280 my ($self) = @_; 392 my ($self) = @_;
284} 396}
285 397
286sub connect { 398sub connect {
287 my ($self, $signal, $cb) = @_; 399 my ($self, $signal, $cb) = @_;
288 400
289 push @{ $self->{cb}{$signal} }, $cb; 401 push @{ $self->{signal_cb}{$signal} }, $cb;
290} 402}
291 403
292sub emit { 404sub emit {
293 my ($self, $signal, @args) = @_; 405 my ($self, $signal, @args) = @_;
294 406
407 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
295 $_->($self, @args) 408 $cb->($self, @args);
296 for @{$self->{cb}{$signal} || []}; 409 }
297} 410}
298 411
299sub DESTROY { 412sub DESTROY {
300 my ($self) = @_; 413 my ($self) = @_;
301 414
307package CFClient::UI::DrawBG; 420package CFClient::UI::DrawBG;
308 421
309our @ISA = CFClient::UI::Base::; 422our @ISA = CFClient::UI::Base::;
310 423
311use strict; 424use strict;
312use SDL::OpenGL; 425use CFClient::OpenGL;
313 426
314sub new { 427sub new {
315 my $class = shift; 428 my $class = shift;
316 429
317 # range [value, low, high, page] 430 # range [value, low, high, page]
346 459
347package CFClient::UI::Empty; 460package CFClient::UI::Empty;
348 461
349our @ISA = CFClient::UI::Base::; 462our @ISA = CFClient::UI::Base::;
350 463
464sub new {
465 my ($class, %arg) = @_;
466 $class->SUPER::new (can_events => 0, %arg);
467}
468
351sub size_request { 469sub size_request {
352 (0, 0) 470 (0, 0)
353} 471}
354 472
355sub draw { } 473sub draw { }
363sub new { 481sub new {
364 my ($class, %arg) = @_; 482 my ($class, %arg) = @_;
365 483
366 my $children = delete $arg{children} || []; 484 my $children = delete $arg{children} || [];
367 485
368 my $self = $class->SUPER::new (children => [], %arg); 486 my $self = $class->SUPER::new (children => [], can_events => 0, %arg);
369 $self->add ($_) for @$children; 487 $self->add ($_) for @$children;
370 488
371 $self 489 $self
372} 490}
373 491
381 $self->{children} = [ 499 $self->{children} = [
382 sort { $a->{z} <=> $b->{z} } 500 sort { $a->{z} <=> $b->{z} }
383 @{$self->{children}}, $child 501 @{$self->{children}}, $child
384 ]; 502 ];
385 503
386 $self->{w} = $self->{h} = -1;
387
388 $child->check_size; 504 $child->check_size;
389} 505}
390 506
391sub remove { 507sub remove {
392 my ($self, $widget) = @_; 508 my ($self, $child) = @_;
393 509
510 delete $child->{parent};
511
394 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 512 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
395 513
396 $self->check_size; 514 $self->check_size;
515 $self->update;
516}
517
518sub clear {
519 my ($self) = @_;
520
521 delete $_->{parent}
522 for @{ delete $self->{children} };
523
524 $self->{children} = [];
397} 525}
398 526
399sub find_widget { 527sub find_widget {
400 my ($self, $x, $y) = @_; 528 my ($self, $x, $y) = @_;
401 529
431 559
432 $class->SUPER::new (children => [$child], %arg) 560 $class->SUPER::new (children => [$child], %arg)
433} 561}
434 562
435sub add { 563sub add {
436 my ($self, $widget) = @_; 564 my ($self, $child) = @_;
437 565
438 $self->{children} = []; 566 $self->{children} = [];
439 567
440 $self->SUPER::add ($widget); 568 $self->SUPER::add ($child);
441} 569}
442 570
443sub remove { 571sub remove {
444 my ($self, $widget) = @_; 572 my ($self, $widget) = @_;
445 573
454sub size_request { 582sub size_request {
455 $_[0]{children}[0]->size_request 583 $_[0]{children}[0]->size_request
456} 584}
457 585
458sub size_allocate { 586sub size_allocate {
459 my ($self, $x, $y, $w, $h) = @_; 587 my ($self, $w, $h) = @_;
460 588
461 $self->_size_allocate ($x, $y, $w, $h) or return;
462
463 $self->{children}[0]->size_allocate (0, 0, $w, $h); 589 $self->{children}[0]->configure (0, 0, $w, $h);
464} 590}
465 591
466############################################################################# 592#############################################################################
467 593
468package CFClient::UI::Window; 594package CFClient::UI::Window;
469 595
470our @ISA = CFClient::UI::Bin::; 596our @ISA = CFClient::UI::Bin::;
471 597
472use SDL::OpenGL; 598use CFClient::OpenGL;
473 599
474sub new { 600sub new {
475 my ($class, %arg) = @_; 601 my ($class, %arg) = @_;
476 602
477 my $self = $class->SUPER::new (%arg); 603 my $self = $class->SUPER::new (%arg);
494 $self->child->draw; 620 $self->child->draw;
495 }; 621 };
496} 622}
497 623
498sub size_allocate { 624sub size_allocate {
499 my ($self, $x, $y, $w, $h) = @_; 625 my ($self, $w, $h) = @_;
500 626
501 $self->_size_allocate ($x, $y, $w, $h) or return;
502
503 $self->child->size_allocate (0, 0, $w, $h); 627 $self->child->configure (0, 0, $w, $h);
504 628
505 $self->render_chld; 629 $self->render_chld;
506} 630}
507 631
508sub _draw { 632sub _draw {
533sub new { die } 657sub new { die }
534 658
535sub size_request { 659sub size_request {
536 my ($self) = @_; 660 my ($self) = @_;
537 661
538 @$self{qw(child_w child_h)} = $self->child->size_request; 662 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
539 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 663 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
540 664
541 @$self{qw(child_w child_h)} 665 @$self{qw(child_w child_h)}
542} 666}
543 667
544sub size_allocate {
545 my ($self, $x, $y, $w, $h) = @_;
546
547 $self->_size_allocate ($x, $y, $w, $h) or return;
548}
549
550sub _draw { 668sub _draw {
551 my ($self) = @_; 669 my ($self) = @_;
552 670
553 $self->{children}[1]->draw; 671 $self->{children}[1]->draw;
554} 672}
558 676
559package CFClient::UI::Frame; 677package CFClient::UI::Frame;
560 678
561our @ISA = CFClient::UI::Bin::; 679our @ISA = CFClient::UI::Bin::;
562 680
563use SDL::OpenGL; 681use CFClient::OpenGL;
564
565sub size_request {
566 my ($self) = @_;
567 my $chld = $self->child
568 or return (0, 0);
569
570 $chld->move (2, 2);
571
572 map { $_ + 4 } $chld->size_request;
573}
574
575sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_;
577
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
581}
582
583sub _draw {
584 my ($self) = @_;
585
586 my $chld = $self->child;
587
588 my ($w, $h) = $chld->size_request;
589
590 glBegin GL_QUADS;
591 glColor 0, 0, 0;
592 glVertex 0 , 0;
593 glVertex 0 , $h + 4;
594 glVertex $w + 4 , $h + 4;
595 glVertex $w + 4 , 0;
596 glEnd;
597
598 $chld->draw;
599}
600
601#############################################################################
602
603package CFClient::UI::FancyFrame;
604
605our @ISA = CFClient::UI::Bin::;
606
607use SDL::OpenGL;
608
609my @tex =
610 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
611 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
612 682
613sub new { 683sub new {
614 my $class = shift; 684 my $class = shift;
615 685
616 # TODO: user_x, user_y, overwrite moveto?
617
618 $class->SUPER::new ( 686 my $self = $class->SUPER::new (
619 bg => [1, 1, 1, 1], 687 bg => [1, 1, 1, 1],
620 border_bg => [1, 1, 1, 1], 688 border_bg => [1, 1, 1, 1],
621 border => int $::FONTSIZE * 0.8, 689 border => 0.8,
622 @_ 690 @_
691 );
692
693 $self
694}
695
696sub _draw {
697 my ($self) = @_;
698
699 my ($w, $h) = ($self->{w}, $self->{h});
700
701 glEnable GL_BLEND;
702 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
703 glEnable GL_TEXTURE_2D;
704 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
705
706# glBegin GL_QUADS;
707# glColor 0, 0, 0, 0;
708# glVertex 0 , 0;
709# glVertex 0 , $h;
710# glVertex $w, $h;
711# glVertex $w, 0;
712# glEnd;
713
714
715 $self->child->draw;
716 glDisable GL_BLEND;
717 glDisable GL_TEXTURE_2D;
718}
719
720#############################################################################
721
722package CFClient::UI::FancyFrame;
723
724our @ISA = CFClient::UI::Bin::;
725
726use CFClient::OpenGL;
727
728my @tex =
729 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
730 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
731
732sub new {
733 my $class = shift;
734
735 # TODO: user_x, user_y, overwrite moveto?
736
737 my $self = $class->SUPER::new (
738 bg => [1, 1, 1, 1],
739 border_bg => [1, 1, 1, 1],
740 border => 0.8,
741 can_events => 1,
742 @_
743 );
744
745 $self->{title} &&= new CFClient::UI::Label
746 align => 0,
747 valign => 1,
748 text => $self->{title},
749 fontsize => 1;
750
751 $self
752}
753
754sub border {
755 int $_[0]{border} * $::FONTSIZE
756}
757
758sub size_request {
759 my ($self) = @_;
760
761 my ($w, $h) = $self->SUPER::size_request;
762
763 (
764 $w + $self->border * 2,
765 $h + $self->border * 2,
623 ) 766 )
624} 767}
625 768
626sub size_request {
627 my ($self) = @_;
628
629 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
630
631 my ($w, $h) = $self->SUPER::size_request;
632
633 (
634 $w + $self->{border} * 2,
635 $h + $self->{border} * 2,
636 )
637}
638
639sub size_allocate { 769sub size_allocate {
640 my ($self, $x, $y, $w, $h) = @_; 770 my ($self, $w, $h) = @_;
641 771
642 $self->_size_allocate ($x, $y, $w, $h) or return;
643
644 $h -= List::Util::max 0, $self->{border} * 2; 772 $h -= List::Util::max 0, $self->border * 2;
645 $w -= List::Util::max 0, $self->{border} * 2; 773 $w -= List::Util::max 0, $self->border * 2;
646 774
775 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
776 if $self->{title};
777
647 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 778 $self->child->configure ($self->border, $self->border, $w, $h);
648} 779}
649 780
650sub button_down { 781sub button_down {
651 my ($self, $ev, $x, $y) = @_; 782 my ($self, $ev, $x, $y) = @_;
652 783
784 my $border = $self->border;
785
653 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 786 if ($x < $self->{w} && $x >= $self->{w} - $border
654 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 787 && $y < $self->{h} && $y >= $self->{h} - $border) {
655 788
656 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 789 my ($ox, $oy) = ($ev->{x}, $ev->{y});
657 my ($bw, $bh) = ($self->{w}, $self->{h}); 790 my ($bw, $bh) = ($self->{w}, $self->{h});
658 791
659 $self->{motion} = sub { 792 $self->{motion} = sub {
660 my ($ev, $x, $y) = @_; 793 my ($ev, $x, $y) = @_;
661 794
662 ($x, $y) = ($ev->motion_x, $ev->motion_y); 795 ($x, $y) = ($ev->{x}, $ev->{y});
663 796
664 $self->{user_w} = $bw + $x - $ox; 797 $self->{user_w} = $bw + $x - $ox;
665 $self->{user_h} = $bh + $y - $oy; 798 $self->{user_h} = $bh + $y - $oy;
666 $self->update; 799 $self->check_size;
667 }; 800 };
668 801
669 } elsif ($x >= 0 && $x < $self->{w} 802 } elsif ($x >= 0 && $x < $self->{w}
670 && $y >= 0 && $y < $self->{border}) { 803 && $y >= 0 && $y < $border) {
671 804
672 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 805 my ($ox, $oy) = ($ev->{x}, $ev->{y});
673 my ($bx, $by) = ($self->{x}, $self->{y}); 806 my ($bx, $by) = ($self->{x}, $self->{y});
674 807
675 $self->{motion} = sub { 808 $self->{motion} = sub {
676 my ($ev, $x, $y) = @_; 809 my ($ev, $x, $y) = @_;
677 810
678 ($x, $y) = ($ev->motion_x, $ev->motion_y); 811 ($x, $y) = ($ev->{x}, $ev->{y});
679 812
680 $self->move ($bx + $x - $ox, $by + $y - $oy); 813 $self->move ($bx + $x - $ox, $by + $y - $oy);
681 $self->update; 814 $self->update;
682 }; 815 };
683 } 816 }
704 glEnable GL_BLEND; 837 glEnable GL_BLEND;
705 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 838 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
706 glEnable GL_TEXTURE_2D; 839 glEnable GL_TEXTURE_2D;
707 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 840 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
708 841
842 my $border = $self->border;
843
709 glColor @{ $self->{border_bg} }; 844 glColor @{ $self->{border_bg} };
710 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 845 $tex[1]->draw_quad (0, 0, $w, $border);
711 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 846 $tex[3]->draw_quad (0, $border, $border, $ch);
712 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 847 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
713 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 848 $tex[4]->draw_quad (0, $h - $border, $w, $border);
714 849
715 my $bg = $tex[0]; 850 my $bg = $tex[0];
716 851
717 # TODO: repeat texture not scale 852 # TODO: repeat texture not scale
718 my $rep_x = $cw / $bg->{w}; 853 my $rep_x = $cw / $bg->{w};
721 glColor @{ $self->{bg} }; 856 glColor @{ $self->{bg} };
722 857
723 $bg->{s} = $rep_x; 858 $bg->{s} = $rep_x;
724 $bg->{t} = $rep_y; 859 $bg->{t} = $rep_y;
725 $bg->{wrap_mode} = 1; 860 $bg->{wrap_mode} = 1;
726 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 861 $bg->draw_quad ($border, $border, $cw, $ch);
727 862
728 glDisable GL_TEXTURE_2D; 863 glDisable GL_TEXTURE_2D;
729 glDisable GL_BLEND; 864 glDisable GL_BLEND;
730 865
866 $self->{title}->draw if $self->{title};
731 $self->child->draw; 867 $self->child->draw;
732} 868}
733 869
734############################################################################# 870#############################################################################
735 871
737 873
738our @ISA = CFClient::UI::Base::; 874our @ISA = CFClient::UI::Base::;
739 875
740use List::Util qw(max sum); 876use List::Util qw(max sum);
741 877
742use SDL::OpenGL; 878use CFClient::OpenGL;
743 879
744sub new { 880sub new {
745 my $class = shift; 881 my $class = shift;
746 882
747 $class->SUPER::new ( 883 $class->SUPER::new (
754 my ($self, $x, $y, $child) = @_; 890 my ($self, $x, $y, $child) = @_;
755 891
756 $child->set_parent ($self); 892 $child->set_parent ($self);
757 $self->{children}[$y][$x] = $child; 893 $self->{children}[$y][$x] = $child;
758 894
759 $self->{w} = $self->{h} = -1;
760
761 $child->check_size; 895 $child->check_size;
762} 896}
763 897
764# TODO: move to container class maybe? send childs a signal on removal? 898# TODO: move to container class maybe? send childs a signal on removal?
765sub clear { 899sub clear {
779 or next; 913 or next;
780 914
781 for my $x (0 .. $#$row) { 915 for my $x (0 .. $#$row) {
782 my $widget = $row->[$x] 916 my $widget = $row->[$x]
783 or next; 917 or next;
784 my ($w, $h) = $widget->size_request; 918 my ($w, $h) = @$widget{qw(req_w req_h)};
785 919
786 $w[$x] = max $w[$x], $w; 920 $w[$x] = max $w[$x], $w;
787 $h[$y] = max $h[$y], $h; 921 $h[$y] = max $h[$y], $h;
788 } 922 }
789 } 923 }
801 (sum @$hs), 935 (sum @$hs),
802 ) 936 )
803} 937}
804 938
805sub size_allocate { 939sub size_allocate {
806 my ($self, $x, $y, $w, $h) = @_; 940 my ($self, $w, $h) = @_;
807
808 $self->_size_allocate ($x, $y, $w, $h) or return;
809 941
810 my ($ws, $hs) = $self->get_wh; 942 my ($ws, $hs) = $self->get_wh;
811 943
812 my $req_w = sum @$ws; 944 my $req_w = sum @$ws;
813 my $req_h = sum @$hs; 945 my $req_h = sum @$hs;
835 967
836 for my $c (0 .. $#$row) { 968 for my $c (0 .. $#$row) {
837 my $col_w = $ws->[$c]; 969 my $col_w = $ws->[$c];
838 970
839 if (my $widget = $row->[$c]) { 971 if (my $widget = $row->[$c]) {
840 $widget->size_allocate ($x, $y, $col_w, $row_h); 972 $widget->configure ($x, $y, $col_w, $row_h);
841 } 973 }
842 974
843 $x += $col_w; 975 $x += $col_w;
844 } 976 }
845 977
890 (List::Util::max map $_->[1], @alloc), 1022 (List::Util::max map $_->[1], @alloc),
891 ) 1023 )
892} 1024}
893 1025
894sub size_allocate { 1026sub size_allocate {
895 my ($self, $x, $y, $w, $h) = @_; 1027 my ($self, $w, $h) = @_;
896
897 $self->_size_allocate ($x, $y, $w, $h) or return;
898 1028
899 ($h, $w) = ($w, $h); 1029 ($h, $w) = ($w, $h);
900 1030
901 my $children = $self->{children}; 1031 my $children = $self->{children};
902 1032
903 my @h = map +($_->size_request)[0], @$children; 1033 my @h = map $_->{req_w}, @$children;
904 1034
905 my $req_h = List::Util::sum @h; 1035 my $req_h = List::Util::sum @h;
906 1036
907 if ($req_h > $h) { 1037 if ($req_h > $h) {
908 # ah well, not enough space 1038 # ah well, not enough space
924 1054
925 my $y = 0; 1055 my $y = 0;
926 for (0 .. $#$children) { 1056 for (0 .. $#$children) {
927 my $child = $children->[$_]; 1057 my $child = $children->[$_];
928 my $h = $h[$_]; 1058 my $h = $h[$_];
929 $child->size_allocate ($y, 0, $h, $w); 1059 $child->configure ($y, 0, $h, $w);
930 1060
931 $y += $h; 1061 $y += $h;
932 } 1062 }
933 1063
934 1 1064 1
952 (List::Util::sum map $_->[1], @alloc), 1082 (List::Util::sum map $_->[1], @alloc),
953 ) 1083 )
954} 1084}
955 1085
956sub size_allocate { 1086sub size_allocate {
957 my ($self, $x, $y, $w, $h) = @_; 1087 my ($self, $w, $h) = @_;
958
959 $self->_size_allocate ($x, $y, $w, $h) or return;
960 1088
961 my $children = $self->{children}; 1089 my $children = $self->{children};
962 1090
963 my @h = map +($_->size_request)[1], @$children; 1091 my @h = map $_->{req_h}, @$children;
964 1092
965 my $req_h = List::Util::sum @h; 1093 my $req_h = List::Util::sum @h;
966 1094
967 if ($req_h > $h) { 1095 if ($req_h > $h) {
968 # ah well, not enough space 1096 # ah well, not enough space
982 1110
983 my $y = 0; 1111 my $y = 0;
984 for (0 .. $#$children) { 1112 for (0 .. $#$children) {
985 my $child = $children->[$_]; 1113 my $child = $children->[$_];
986 my $h = $h[$_]; 1114 my $h = $h[$_];
987 $child->size_allocate (0, $y, $w, $h); 1115 $child->configure (0, $y, $w, $h);
988 1116
989 $y += $h; 1117 $y += $h;
990 } 1118 }
991 1119
992 1 1120 1
996 1124
997package CFClient::UI::Label; 1125package CFClient::UI::Label;
998 1126
999our @ISA = CFClient::UI::Base::; 1127our @ISA = CFClient::UI::Base::;
1000 1128
1001use SDL::OpenGL; 1129use CFClient::OpenGL;
1002 1130
1003sub new { 1131sub new {
1004 my ($class, %arg) = @_; 1132 my ($class, %arg) = @_;
1005 1133
1006 my $self = $class->SUPER::new ( 1134 my $self = $class->SUPER::new (
1007 fg => [1, 1, 1], 1135 fg => [1, 1, 1],
1136 #font => default_font
1008 fontsize => $::FONTSIZE, 1137 fontsize => 1,
1009 text => "", 1138 text => "",
1010 align => -1, 1139 align => -1,
1011 valign => -1, 1140 valign => -1,
1012 padding => 2, 1141 padding => 2,
1013 layout => new CFClient::Layout, 1142 layout => new CFClient::Layout,
1143 can_events => 0,
1014 %arg 1144 %arg
1015 ); 1145 );
1016 1146
1017 $self->{xxx}++ if $self->{text} eq "Client Setup" && $self->{align};#d# 1147 if (exists $self->{template}) {
1148 my $layout = new CFClient::Layout;
1149 $layout->set_text (delete $self->{template});
1150 $self->{template} = $layout;
1151 }
1018 1152
1019 $self->set_text (delete $self->{text}) if exists $self->{text}; 1153 $self->set_text (delete $self->{text}) if exists $self->{text};
1020 $self->set_markup (delete $self->{markup}) if exists $self->{markup}; 1154 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
1021 1155
1022 $self 1156 $self
1023} 1157}
1024 1158
1025sub escape_text { 1159sub escape {
1026 local $_ = $_[1]; 1160 local $_ = $_[1];
1027 1161
1028 s/&/&amp;/g; 1162 s/&/&amp;/g;
1029 s/>/&gt;/g; 1163 s/>/&gt;/g;
1030 s/</&lt;/g; 1164 s/</&lt;/g;
1036 my ($self, $text) = @_; 1170 my ($self, $text) = @_;
1037 1171
1038 $self->{layout}->set_text ($text); 1172 $self->{layout}->set_text ($text);
1039 1173
1040 delete $self->{texture}; 1174 delete $self->{texture};
1175 $self->check_size;
1041 $self->update; 1176 $self->update;
1042} 1177}
1043 1178
1044sub set_markup { 1179sub set_markup {
1045 my ($self, $markup) = @_; 1180 my ($self, $markup) = @_;
1046 1181
1047 $self->{layout}->set_markup ($markup); 1182 $self->{layout}->set_markup ($markup);
1048 1183
1049 delete $self->{texture}; 1184 delete $self->{texture};
1185 $self->check_size;
1050 $self->update; 1186 $self->update;
1051} 1187}
1052 1188
1053sub size_request { 1189sub size_request {
1054 my ($self) = @_; 1190 my ($self) = @_;
1055 1191
1056 $self->{layout}->set_width; 1192 $self->{layout}->set_font ($self->{font}) if $self->{font};
1193 $self->{layout}->set_width ($self->{max_w} || -1);
1057 $self->{layout}->set_height ($self->{fontsize}); 1194 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1058 1195
1059 my ($w, $h) = $self->{layout}->size; 1196 my ($w, $h) = $self->{layout}->size;
1197
1198 if (exists $self->{template}) {
1199 $self->{template}->set_font ($self->{font}) if $self->{font};
1200 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1201
1202 my ($w2, $h2) = $self->{template}->size;
1203
1204 $w = List::Util::max $w, $w2;
1205 $h = List::Util::max $h, $h2;
1206 }
1060 1207
1061 ( 1208 (
1062 $w + $self->{padding} * 2, 1209 $w + $self->{padding} * 2,
1063 $h + $self->{padding} * 2, 1210 $h + $self->{padding} * 2,
1064 ) 1211 )
1065} 1212}
1066 1213
1067sub size_allocate { 1214sub size_allocate {
1068 my ($self, $x, $y, $w, $h) = @_; 1215 my ($self, $w, $h) = @_;
1069
1070 $self->_size_allocate ($x, $y, $w, $h) or return;
1071 1216
1072 delete $self->{texture}; 1217 delete $self->{texture};
1073} 1218}
1074 1219
1075sub update { 1220sub set_fontsize {
1076 my ($self) = @_; 1221 my ($self, $fontsize) = @_;
1077 1222
1223 $self->{fontsize} = $fontsize;
1078 delete $self->{texture}; 1224 delete $self->{texture};
1225 $self->check_size;
1079 $self->SUPER::update; 1226 $self->update;
1080} 1227}
1081 1228
1082sub _draw { 1229sub _draw {
1083 my ($self) = @_; 1230 my ($self) = @_;
1084 1231
1085 my $tex = $self->{texture} ||= do { 1232 my $tex = $self->{texture} ||= do {
1233 $self->{layout}->set_font ($self->{font}) if $self->{font};
1086 $self->{layout}->set_width ($self->{w}); 1234 $self->{layout}->set_width ($self->{w});
1087 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize}); 1235 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1088 new_from_layout CFClient::Texture $self->{layout} 1236 new_from_layout CFClient::Texture $self->{layout}
1089 }; 1237 };
1090 1238
1091 glEnable GL_BLEND; 1239 glEnable GL_BLEND;
1092 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1117 1265
1118package CFClient::UI::EntryBase; 1266package CFClient::UI::EntryBase;
1119 1267
1120our @ISA = CFClient::UI::Label::; 1268our @ISA = CFClient::UI::Label::;
1121 1269
1122use SDL; 1270use CFClient::OpenGL;
1123use SDL::OpenGL;
1124 1271
1125sub new { 1272sub new {
1126 my $class = shift; 1273 my $class = shift;
1127 1274
1128 $class->SUPER::new ( 1275 $class->SUPER::new (
1131 active_bg => [1, 1, 1, 0.5], 1278 active_bg => [1, 1, 1, 0.5],
1132 active_fg => [0, 0, 0], 1279 active_fg => [0, 0, 0],
1133 can_hover => 1, 1280 can_hover => 1,
1134 can_focus => 1, 1281 can_focus => 1,
1135 valign => 0, 1282 valign => 0,
1283 can_events => 1,
1136 @_ 1284 @_
1137 ) 1285 )
1138} 1286}
1139 1287
1140sub _set_text { 1288sub _set_text {
1142 1290
1143 delete $self->{cur_h}; 1291 delete $self->{cur_h};
1144 1292
1145 return if $self->{text} eq $text; 1293 return if $self->{text} eq $text;
1146 1294
1295 delete $self->{texture};
1296
1147 $self->{last_activity} = $::NOW; 1297 $self->{last_activity} = $::NOW;
1148 $self->{text} = $text; 1298 $self->{text} = $text;
1149 1299
1150 $text =~ s/./*/g if $self->{hidden}; 1300 $text =~ s/./*/g if $self->{hidden};
1151 $self->{layout}->set_text ("$text "); 1301 $self->{layout}->set_text ("$text ");
1164 1314
1165 ($w + 1, $h) # add 1 for cursor 1315 ($w + 1, $h) # add 1 for cursor
1166} 1316}
1167 1317
1168sub size_allocate { 1318sub size_allocate {
1169 my ($self, $x, $y, $w, $h) = @_; 1319 my ($self, $w, $h) = @_;
1170 1320
1171 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 1321 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1172
1173 $self->_set_text ($self->{text});
1174} 1322}
1175 1323
1176sub set_text { 1324sub set_text {
1177 my ($self, $text) = @_; 1325 my ($self, $text) = @_;
1178 1326
1182} 1330}
1183 1331
1184sub key_down { 1332sub key_down {
1185 my ($self, $ev) = @_; 1333 my ($self, $ev) = @_;
1186 1334
1187 my $mod = $ev->key_mod; 1335 my $mod = $ev->{mod};
1188 my $sym = $ev->key_sym; 1336 my $sym = $ev->{sym};
1189
1190 my $uni = $ev->key_unicode; 1337 my $uni = $ev->{unicode};
1191 1338
1192 my $text = $self->get_text; 1339 my $text = $self->get_text;
1193 1340
1194 if ($sym == SDLK_BACKSPACE) { 1341 if ($sym == 8) {
1195 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1342 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1196 } elsif ($sym == SDLK_DELETE) { 1343 } elsif ($sym == 127) {
1197 substr $text, $self->{cursor}, 1, ""; 1344 substr $text, $self->{cursor}, 1, "";
1198 } elsif ($sym == SDLK_LEFT) { 1345 } elsif ($sym == CFClient::SDLK_LEFT) {
1199 --$self->{cursor} if $self->{cursor}; 1346 --$self->{cursor} if $self->{cursor};
1200 } elsif ($sym == SDLK_RIGHT) { 1347 } elsif ($sym == CFClient::SDLK_RIGHT) {
1201 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1348 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1202 } elsif ($sym == SDLK_HOME) { 1349 } elsif ($sym == CFClient::SDLK_HOME) {
1203 $self->{cursor} = 0; 1350 $self->{cursor} = 0;
1204 } elsif ($sym == SDLK_END) { 1351 } elsif ($sym == CFClient::SDLK_END) {
1205 $self->{cursor} = length $text; 1352 $self->{cursor} = length $text;
1206 } elsif ($sym == SDLK_ESCAPE) { 1353 } elsif ($sym == 27) {
1207 $self->emit ('escape'); 1354 $self->emit ('escape');
1208 } elsif ($uni) { 1355 } elsif ($uni) {
1209 substr $text, $self->{cursor}++, 0, chr $uni; 1356 substr $text, $self->{cursor}++, 0, chr $uni;
1210 } 1357 }
1211 1358
1286 1433
1287package CFClient::UI::Entry; 1434package CFClient::UI::Entry;
1288 1435
1289our @ISA = CFClient::UI::EntryBase::; 1436our @ISA = CFClient::UI::EntryBase::;
1290 1437
1291use SDL; 1438use CFClient::OpenGL;
1292use SDL::OpenGL;
1293 1439
1294sub key_down { 1440sub key_down {
1295 my ($self, $ev) = @_; 1441 my ($self, $ev) = @_;
1296 1442
1297 my $sym = $ev->key_sym; 1443 my $sym = $ev->{sym};
1298 1444
1299 if ($sym == SDLK_RETURN) { 1445 if ($sym == 13) {
1300 $self->emit (activate => $self->get_text); 1446 $self->emit (activate => $self->get_text);
1301 $self->update; 1447 $self->update;
1302 1448
1303 } else { 1449 } else {
1304 $self->SUPER::key_down ($ev); 1450 $self->SUPER::key_down ($ev);
1310 1456
1311package CFClient::UI::Button; 1457package CFClient::UI::Button;
1312 1458
1313our @ISA = CFClient::UI::Label::; 1459our @ISA = CFClient::UI::Label::;
1314 1460
1315use SDL; 1461use CFClient::OpenGL;
1316use SDL::OpenGL;
1317 1462
1318my @tex = 1463my @tex =
1319 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1464 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1320 qw(b1_button_active.png); 1465 qw(b1_button_active.png);
1321 1466
1322sub new { 1467sub new {
1323 my $class = shift; 1468 my $class = shift;
1324 1469
1328 bg => [1, 1, 1, 0.2], 1473 bg => [1, 1, 1, 0.2],
1329 active_fg => [0, 0, 1], 1474 active_fg => [0, 0, 1],
1330 can_hover => 1, 1475 can_hover => 1,
1331 align => 0, 1476 align => 0,
1332 valign => 0, 1477 valign => 0,
1478 can_events => 1,
1333 @_ 1479 @_
1334 ) 1480 )
1335} 1481}
1336 1482
1337sub button_up { 1483sub button_up {
1371package CFClient::UI::CheckBox; 1517package CFClient::UI::CheckBox;
1372 1518
1373our @ISA = CFClient::UI::DrawBG::; 1519our @ISA = CFClient::UI::DrawBG::;
1374 1520
1375my @tex = 1521my @tex =
1376 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1522 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1377 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1523 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1378 1524
1379use SDL; 1525use CFClient::OpenGL;
1380use SDL::OpenGL;
1381 1526
1382sub new { 1527sub new {
1383 my $class = shift; 1528 my $class = shift;
1384 1529
1385 $class->SUPER::new ( 1530 $class->SUPER::new (
1396 my ($self) = @_; 1541 my ($self) = @_;
1397 1542
1398 ($self->{padding} * 2 + 6) x 2 1543 ($self->{padding} * 2 + 6) x 2
1399} 1544}
1400 1545
1401sub size_allocate {
1402 my ($self, $x, $y, $w, $h) = @_;
1403
1404 $self->_size_allocate ($x, $y, $w, $h) or return;
1405}
1406
1407sub button_down { 1546sub button_down {
1408 my ($self, $ev, $x, $y) = @_; 1547 my ($self, $ev, $x, $y) = @_;
1409 1548
1410 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1549 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1411 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1550 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1437 glDisable GL_BLEND; 1576 glDisable GL_BLEND;
1438} 1577}
1439 1578
1440############################################################################# 1579#############################################################################
1441 1580
1442package CFClient::UI::VGauge; 1581package CFClient::UI::Image;
1443 1582
1444our @ISA = CFClient::UI::Base::; 1583our @ISA = CFClient::UI::Base::;
1445 1584
1446use SDL::OpenGL; 1585use CFClient::OpenGL;
1586use Carp qw/confess/;
1447 1587
1448my %tex = ( 1588our %loaded_images;
1449 food => [
1450 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1451 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1452 ],
1453 grace => [
1454 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1455 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1456 ],
1457 hp => [
1458 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1459 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1460 ],
1461 mana => [
1462 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1463 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1464 ],
1465);
1466 1589
1467# eg. VGauge->new (gauge => 'food'), default gauge: food
1468sub new { 1590sub new {
1469 my $class = shift; 1591 my $class = shift;
1470 1592
1471 my $self = $class->SUPER::new (gauge => 'food', @_); 1593 my $self = $class->SUPER::new (can_events => 0, @_);
1594
1595 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1596
1597 $loaded_images{$self->{image}} ||=
1598 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1599
1600 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1601
1602 Scalar::Util::weaken $loaded_images{$self->{image}};
1603
1604 $self->{aspect} = $tex->{w} / $tex->{h};
1472 1605
1473 $self 1606 $self
1474} 1607}
1475 1608
1476sub size_request { 1609sub size_request {
1477 my ($self) = @_; 1610 my ($self) = @_;
1478 1611
1479 (30, 100) 1612 ($self->{tex}->{w}, $self->{tex}->{h})
1480}
1481
1482sub set_max {
1483 my ($self, $max) = @_;
1484 $self->{max_val} = $max;
1485}
1486
1487sub set_value {
1488 my ($self, $val, $max) = @_;
1489
1490 $self->set_max ($max)
1491 if defined $max;
1492
1493 $max = $self->{max_val};
1494 $self->{val} = $val;
1495
1496 $self->update;
1497} 1613}
1498 1614
1499sub _draw { 1615sub _draw {
1500 my ($self) = @_; 1616 my ($self) = @_;
1501 1617
1502 my $tex = $tex{$self->{gauge}}; 1618 my $tex = $self->{tex};
1503 1619
1504 my ($w, $h) = ($self->{w}, $self->{h}); 1620 my ($w, $h) = ($self->{w}, $self->{h});
1505 1621
1506 my $ycut = $self->{val} / ($self->{max_val} || 1); 1622 if ($self->{rot90}) {
1507 $ycut = 1 if $self->{val} > $self->{max_val}; 1623 glRotate 90, 0, 0, 1;
1624 glTranslate 0, -$self->{w}, 0;
1508 1625
1509 my $t1 = $tex->[0]; 1626 ($w, $h) = ($h, $w);
1510 my $t2 = $tex->[1]; 1627 }
1511 1628
1512 glEnable GL_BLEND; 1629 glEnable GL_BLEND;
1513 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1630 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1514 glEnable GL_TEXTURE_2D; 1631 glEnable GL_TEXTURE_2D;
1515 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1632 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1516 1633
1517 my $h1 = $self->{h} - $ycut * $self->{h}; 1634 $tex->draw_quad (0, 0, $w, $h);
1518 my $h2 = $ycut * $self->{h};
1519
1520 my $yp = 0;
1521
1522 glBindTexture (GL_TEXTURE_2D, $t1->{name});
1523 glBegin (GL_QUADS);
1524 glTexCoord (0, 0); glVertex (0 , $yp);
1525 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp + $h1);
1526 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp + $h1);
1527 glTexCoord (1, 0); glVertex (0 + $w, $yp);
1528 glEnd ();
1529
1530 $yp += $h1;
1531
1532 glBindTexture (GL_TEXTURE_2D, $t2->{name});
1533 glBegin (GL_QUADS);
1534 glTexCoord (0, (1 - $ycut)); glVertex (0 , $yp);
1535 glTexCoord (0, 1); glVertex (0 , $yp + $h2);
1536 glTexCoord (1, 1); glVertex (0 + $w, $yp + $h2);
1537 glTexCoord (1, (1 - $ycut)); glVertex (0 + $w, $yp);
1538 glEnd ();
1539 1635
1540 glDisable GL_BLEND; 1636 glDisable GL_BLEND;
1541 glDisable GL_TEXTURE_2D; 1637 glDisable GL_TEXTURE_2D;
1542} 1638}
1543 1639
1544############################################################################# 1640#############################################################################
1545 1641
1642package CFClient::UI::VGauge;
1643
1644our @ISA = CFClient::UI::Base::;
1645
1646use List::Util qw(min max);
1647
1648use CFClient::OpenGL;
1649
1650my %tex = (
1651 food => [
1652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1653 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1654 ],
1655 grace => [
1656 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1657 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png g1_grace_gauge_overflow.png/
1658 ],
1659 hp => [
1660 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1661 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1662 ],
1663 mana => [
1664 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1665 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png g1_mana_gauge_overflow.png/
1666 ],
1667);
1668
1669# eg. VGauge->new (gauge => 'food'), default gauge: food
1670sub new {
1671 my $class = shift;
1672
1673 my $self = $class->SUPER::new (
1674 type => 'food',
1675 @_
1676 );
1677
1678 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1679
1680 $self
1681}
1682
1683sub size_request {
1684 my ($self) = @_;
1685
1686 #my $tex = $tex{$self->{type}}[0];
1687 #@$tex{qw(w h)}
1688 (0, 0)
1689}
1690
1691sub set_max {
1692 my ($self, $max) = @_;
1693
1694 $self->{max_val} = $max;
1695}
1696
1697sub set_value {
1698 my ($self, $val, $max) = @_;
1699
1700 $self->set_max ($max)
1701 if defined $max;
1702
1703 $max = $self->{max_val};
1704 $self->{val} = $val;
1705
1706 $self->update;
1707}
1708
1709sub _draw {
1710 my ($self) = @_;
1711
1712 my $tex = $tex{$self->{type}};
1713 my ($t1, $t2, $t3) = @$tex;
1714
1715 my ($w, $h) = ($self->{w}, $self->{h});
1716
1717 if ($self->{vertical}) {
1718 glRotate 90, 0, 0, 1;
1719 glTranslate 0, -$self->{w}, 0;
1720
1721 ($w, $h) = ($h, $w);
1722 }
1723
1724 my $ycut = $self->{val} / ($self->{max_val} || 1);
1725
1726 my $ycut1 = max 0, min 1, $ycut;
1727 my $ycut2 = max 0, min 1, $ycut - 1;
1728
1729 my $h1 = $self->{h} * (1 - $ycut1);
1730 my $h2 = $self->{h} * (1 - $ycut2);
1731
1732 glEnable GL_BLEND;
1733 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1734 glEnable GL_TEXTURE_2D;
1735 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1736
1737 glBindTexture GL_TEXTURE_2D, $t1->{name};
1738 glBegin GL_QUADS;
1739 glTexCoord 0 , 0; glVertex 0 , 0;
1740 glTexCoord 0 , $t1->{t} * (1 - $ycut1); glVertex 0 , $h1;
1741 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut1); glVertex $w, $h1;
1742 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1743 glEnd;
1744
1745 my $ycut1 = List::Util::min 1, $ycut;
1746 glBindTexture GL_TEXTURE_2D, $t2->{name};
1747 glBegin GL_QUADS;
1748 glTexCoord 0 , $t2->{t} * (1 - $ycut1); glVertex 0 , $h1;
1749 glTexCoord 0 , $t2->{t} * (1 - $ycut2); glVertex 0 , $h2;
1750 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut2); glVertex $w, $h2;
1751 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut1); glVertex $w, $h1;
1752 glEnd;
1753
1754 if ($t3) {
1755 glBindTexture GL_TEXTURE_2D, $t3->{name};
1756 glBegin GL_QUADS;
1757 glTexCoord 0 , $t3->{t} * (1 - $ycut2); glVertex 0 , $h2;
1758 glTexCoord 0 , $t3->{t}; glVertex 0 , $self->{h};
1759 glTexCoord $t3->{s}, $t3->{t}; glVertex $w, $self->{h};
1760 glTexCoord $t3->{s}, $t3->{t} * (1 - $ycut2); glVertex $w, $h2;
1761 glEnd;
1762 }
1763
1764 glDisable GL_BLEND;
1765 glDisable GL_TEXTURE_2D;
1766}
1767
1768#############################################################################
1769
1770package CFClient::UI::Gauge;
1771
1772our @ISA = CFClient::UI::VBox::;
1773
1774sub new {
1775 my ($class, %arg) = @_;
1776
1777 my $self = $class->SUPER::new (
1778 tooltip => $arg{type},
1779 can_events => 1,
1780 can_hover => 1,
1781 %arg,
1782 );
1783
1784 $self->add ($self->{value} = new CFClient::UI::Label valign => +1, align => 0, template => "999");
1785 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1, can_hover => 1);
1786 $self->add ($self->{max} = new CFClient::UI::Label valign => -1, align => 0, template => "999");
1787
1788 $self
1789}
1790
1791sub set_fontsize {
1792 my ($self, $fsize) = @_;
1793
1794 $self->{value}->set_fontsize ($fsize);
1795 $self->{max} ->set_fontsize ($fsize);
1796}
1797
1798sub set_value {
1799 my ($self, $val, $max) = @_;
1800
1801 $self->set_max ($max)
1802 if defined $max;
1803
1804 $self->{gauge}->set_value ($val, $max);
1805 $self->{value}->set_text ($val);
1806}
1807
1808sub set_max {
1809 my ($self, $max) = @_;
1810
1811 $self->{gauge}->set_max ($max);
1812 $self->{max}->set_text ($max);
1813}
1814
1815#############################################################################
1816
1546package CFClient::UI::Slider; 1817package CFClient::UI::Slider;
1547 1818
1548use strict; 1819use strict;
1549 1820
1550use SDL::OpenGL; 1821use CFClient::OpenGL;
1551 1822
1552our @ISA = CFClient::UI::DrawBG::; 1823our @ISA = CFClient::UI::DrawBG::;
1553 1824
1554my @tex = 1825my @tex =
1555 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1826 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1565 # TODO: calculations are off 1836 # TODO: calculations are off
1566 my $self = $class->SUPER::new ( 1837 my $self = $class->SUPER::new (
1567 fg => [1, 1, 1], 1838 fg => [1, 1, 1],
1568 active_fg => [0, 0, 0], 1839 active_fg => [0, 0, 0],
1569 range => [0, 0, 100, 10], 1840 range => [0, 0, 100, 10],
1570 req_w => 40, 1841 req_w => 20,
1571 req_h => 13, 1842 req_h => 20,
1572 vertical => 0, 1843 vertical => 0,
1573 can_hover => 1, 1844 can_hover => 1,
1574 inner_pad => 5, 1845 inner_pad => 5,
1575 @_ 1846 @_
1576 ); 1847 );
1680 1951
1681package CFClient::UI::TextView; 1952package CFClient::UI::TextView;
1682 1953
1683our @ISA = CFClient::UI::HBox::; 1954our @ISA = CFClient::UI::HBox::;
1684 1955
1685use SDL::OpenGL; 1956use CFClient::OpenGL;
1686 1957
1687sub new { 1958sub new {
1688 my $class = shift; 1959 my $class = shift;
1689 1960
1690 my $self = $class->SUPER::new ( 1961 my $self = $class->SUPER::new (
1691 req_w => $::WIDTH / 6,
1692 req_h => $::HEIGHT / 6,
1693 fontsize => $::FONTSIZE, 1962 fontsize => 1,
1963 #font => default_font
1694 @_, 1964 @_,
1695 1965
1696 layout => (new CFClient::Layout), 1966 layout => (new CFClient::Layout),
1697 par => [], 1967 par => [],
1698 height => 0, 1968 height => 0,
1719sub text_height { 1989sub text_height {
1720 my ($self, $text) = @_; 1990 my ($self, $text) = @_;
1721 1991
1722 my $layout = $self->{layout}; 1992 my $layout = $self->{layout};
1723 1993
1724 $layout->set_height ($self->{fontsize}); 1994 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1725 $layout->set_width ($self->{w}); 1995 $layout->set_width ($self->{w});
1726 $layout->set_text ($text); 1996 $layout->set_text ($text);
1727 1997
1728 ($layout->size)[1] 1998 ($layout->size)[1]
1729} 1999}
1733 2003
1734 $self->{need_reflow}++; 2004 $self->{need_reflow}++;
1735 $self->update; 2005 $self->update;
1736} 2006}
1737 2007
1738sub size_request {
1739 my ($self) = @_;
1740
1741 ($self->{req_w}, $self->{req_h})
1742}
1743
1744sub size_allocate { 2008sub size_allocate {
1745 my ($self, $x, $y, $w, $h) = @_; 2009 my ($self, $w, $h) = @_;
1746 2010
1747 $self->SUPER::size_allocate ($x, $y, $w, $h) or return; 2011 $self->SUPER::size_allocate ($w, $h);
1748 2012
2013 $self->{layout}->set_font ($self->{font}) if $self->{font};
1749 $self->{layout}->set_height ($self->{fontsize}); 2014 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1750 $self->{layout}->set_width ($self->{w}); 2015 $self->{layout}->set_width ($self->{children}[0]{w});
1751 2016
1752 $self->reflow; 2017 $self->reflow;
1753 $self->update;
1754} 2018}
1755 2019
1756sub add_paragraph { 2020sub add_paragraph {
1757 my ($self, $color, $text) = @_; 2021 my ($self, $color, $text) = @_;
1758 2022
1806 my $y1 = $top + $self->{h}; 2070 my $y1 = $top + $self->{h};
1807 2071
1808 my $y = 0; 2072 my $y = 0;
1809 2073
1810 my $layout = $self->{layout}; 2074 my $layout = $self->{layout};
2075
2076 $layout->set_font ($self->{font}) if $self->{font};
1811 2077
1812 for my $par (@{$self->{par}}) { 2078 for my $par (@{$self->{par}}) {
1813 my $h = $par->[0]; 2079 my $h = $par->[0];
1814 2080
1815 if ($y0 < $y + $h && $y < $y1) { 2081 if ($y0 < $y + $h && $y < $y1) {
1843 2109
1844} 2110}
1845 2111
1846############################################################################# 2112#############################################################################
1847 2113
1848package CFClient::UI::MapWidget;
1849
1850use strict;
1851
1852use List::Util qw(min max);
1853
1854use SDL;
1855use SDL::OpenGL;
1856
1857our @ISA = CFClient::UI::Base::;
1858
1859sub new {
1860 my $class = shift;
1861
1862 $class->SUPER::new (
1863 z => -1,
1864 can_focus => 1,
1865 list => (glGenLists 1),
1866 @_
1867 )
1868}
1869
1870sub key_down {
1871 print "MAPKEYDOWN\n";
1872}
1873
1874sub key_up {
1875}
1876
1877sub button_down {
1878 my ($self, $ev, $x, $y) = @_;
1879
1880 $self->focus_in;
1881
1882 if ($ev->button == 2) {
1883 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1884 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1885
1886 $self->{motion} = sub {
1887 my ($ev, $x, $y) = @_;
1888
1889 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1890
1891 $::CFG->{map_shift_x} = $bw + $x - $ox;
1892 $::CFG->{map_shift_y} = $bh + $y - $oy;
1893
1894 $self->update;
1895 };
1896 }
1897}
1898
1899sub button_up {
1900 my ($self, $ev, $x, $y) = @_;
1901
1902 delete $self->{motion};
1903}
1904
1905sub mouse_motion {
1906 my ($self, $ev, $x, $y) = @_;
1907
1908 $self->{motion}->($ev, $x, $y) if $self->{motion};
1909}
1910
1911sub size_request {
1912 (
1913 1 + 32 * int $::WIDTH / 32,
1914 1 + 32 * int $::HEIGHT / 32,
1915 )
1916}
1917
1918sub update {
1919 my ($self) = @_;
1920
1921 $self->{need_update} = 1;
1922 $self->SUPER::update;
1923}
1924
1925sub draw {
1926 my ($self) = @_;
1927
1928 if (delete $self->{need_update}) {
1929 glNewList $self->{list}, GL_COMPILE;
1930
1931 if ($::MAP) {
1932 my $sw = int $::WIDTH / 32;
1933 my $sh = int $::HEIGHT / 32;
1934
1935 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1936 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1937
1938 glTranslate $sx0 - 32, $sy0 - 32, 0;
1939
1940 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1941
1942 if ($::CFG->{fow_enable}) {
1943 if ($::CFG->{fow_smooth}) { # smooth fog of war
1944 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1945 glConvolutionFilter2D
1946 GL_CONVOLUTION_2D,
1947 GL_ALPHA,
1948 3, 3,
1949 GL_ALPHA, GL_FLOAT,
1950 pack "f*",
1951 0.1, 0.1, 0.1,
1952 0.1, 0.2, 0.1,
1953 0.1, 0.1, 0.1,
1954 ;
1955 glEnable GL_CONVOLUTION_2D;
1956 }
1957
1958 $self->{fow_texture} = new CFClient::Texture
1959 w => $w,
1960 h => $h,
1961 data => $data,
1962 internalformat => GL_ALPHA,
1963 format => GL_ALPHA;
1964
1965 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1966
1967 glEnable GL_BLEND;
1968 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1969 glEnable GL_TEXTURE_2D;
1970 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1971
1972 glColor +($::CFG->{fow_intensity}) x 3, 1;
1973 $self->{fow_texture}->draw_quad (0, 0, $w * 32, $h * 32);
1974
1975 glDisable GL_TEXTURE_2D;
1976 glDisable GL_BLEND;
1977 }
1978
1979 # HACK BEGIN
1980 {
1981 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1982 my ($w, $h) = (250, 250);
1983
1984 glEnable GL_BLEND;
1985 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1986 glEnable GL_TEXTURE_2D;
1987 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1988
1989 $self->{mapmap_texture} =
1990 new CFClient::Texture
1991 w => $w,
1992 h => $h,
1993 data => $::MAP->mapmap ($w, $h),
1994 type => GL_UNSIGNED_INT_8_8_8_8_REV;
1995
1996 $self->{mapmap_texture}->draw_quad (100, 100);
1997
1998 glDisable GL_TEXTURE_2D;
1999 glDisable GL_BLEND;
2000 }
2001 # HACK END
2002 }
2003
2004 glEndList;
2005 }
2006
2007 glPushMatrix;
2008 glCallList $self->{list};
2009 glPopMatrix;
2010
2011 if ($FOCUS != $self) {
2012 glColor 64/255, 64/255, 64/255;
2013 glLogicOp GL_AND;
2014 glEnable GL_COLOR_LOGIC_OP;
2015 glBegin GL_QUADS;
2016 glVertex 0, 0;
2017 glVertex 0, $::HEIGHT;
2018 glVertex $::WIDTH, $::HEIGHT;
2019 glVertex $::WIDTH, 0;
2020 glEnd;
2021 glDisable GL_COLOR_LOGIC_OP;
2022 }
2023}
2024
2025my %DIR = (
2026 SDLK_KP8, [1, "north"],
2027 SDLK_KP9, [2, "northeast"],
2028 SDLK_KP6, [3, "east"],
2029 SDLK_KP3, [4, "southeast"],
2030 SDLK_KP2, [5, "south"],
2031 SDLK_KP1, [6, "southwest"],
2032 SDLK_KP4, [7, "west"],
2033 SDLK_KP7, [8, "northwest"],
2034
2035 SDLK_UP, [1, "north"],
2036 SDLK_RIGHT, [3, "east"],
2037 SDLK_DOWN, [5, "south"],
2038 SDLK_LEFT, [7, "west"],
2039);
2040
2041sub key_down {
2042 my ($self, $ev) = @_;
2043
2044 my $mod = $ev->key_mod;
2045 my $sym = $ev->key_sym;
2046
2047 if ($sym == SDLK_KP5) {
2048 $::CONN->user_send ("stay fire");
2049 } elsif ($sym == SDLK_a) {
2050 $::CONN->user_send ("apply");
2051 } elsif ($sym == SDLK_QUOTE) {
2052 $self->emit ('activate_console');
2053 } elsif ($sym == SDLK_SLASH) {
2054 $self->emit ('activate_console' => '/');
2055 } elsif (exists $DIR{$sym}) {
2056 if ($mod & KMOD_SHIFT) {
2057 $self->{shft}++;
2058 $::CONN->user_send ("fire $DIR{$sym}[0]");
2059 } elsif ($mod & KMOD_CTRL) {
2060 $self->{ctrl}++;
2061 $::CONN->user_send ("run $DIR{$sym}[0]");
2062 } else {
2063 $::CONN->user_send ("$DIR{$sym}[1]");
2064 }
2065 }
2066}
2067
2068sub key_up {
2069 my ($self, $ev) = @_;
2070
2071 my $mod = $ev->key_mod;
2072 my $sym = $ev->key_sym;
2073
2074 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
2075 $::CONN->user_send ("fire_stop");
2076 }
2077 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
2078 $::CONN->user_send ("run_stop");
2079 }
2080}
2081
2082#############################################################################
2083
2084package CFClient::UI::Animator; 2114package CFClient::UI::Animator;
2085 2115
2086use SDL::OpenGL; 2116use CFClient::OpenGL;
2087 2117
2088our @ISA = CFClient::UI::Bin::; 2118our @ISA = CFClient::UI::Bin::;
2089 2119
2090sub moveto { 2120sub moveto {
2091 my ($self, $x, $y) = @_; 2121 my ($self, $x, $y) = @_;
2131 my $class = shift; 2161 my $class = shift;
2132 2162
2133 my $self = $class->SUPER::new ( 2163 my $self = $class->SUPER::new (
2134 state => 0, 2164 state => 0,
2135 connect_activate => \&toggle_flopper, 2165 connect_activate => \&toggle_flopper,
2166 can_events => 1,
2136 @_ 2167 @_
2137 ); 2168 );
2138 2169
2139 if ($self->{state}) { 2170 if ($self->{state}) {
2140 $self->{state} = 0; 2171 $self->{state} = 0;
2160 $self->emit (changed => $self->{state}); 2191 $self->emit (changed => $self->{state});
2161} 2192}
2162 2193
2163############################################################################# 2194#############################################################################
2164 2195
2196package CFClient::UI::Tooltip;
2197
2198our @ISA = CFClient::UI::Bin::;
2199
2200use CFClient::OpenGL;
2201
2202sub new {
2203 my $class = shift;
2204
2205 $class->SUPER::new (
2206 @_,
2207 can_events => 0,
2208 )
2209}
2210
2211sub set_markup {
2212 my ($self, $text) = @_;
2213
2214 $self->{label} ||= new CFClient::UI::Label fontsize => 0.8, fg => [0, 0, 0];
2215 $self->{label}->set_markup ($text);
2216 $self->add ($self->{label});
2217}
2218
2219sub size_request {
2220 my ($self) = @_;
2221
2222 $self->child->set_max_size ($::WIDTH * 0.3);
2223
2224 my ($w, $h) = @{$self->child}{qw(req_w req_h)};
2225
2226 ($w + 4, $h + 4)
2227}
2228
2229sub size_allocate {
2230 my ($self, $w, $h) = @_;
2231
2232 $self->SUPER::size_allocate ($w - 4, $h - 4);
2233}
2234
2235sub _draw {
2236 my ($self) = @_;
2237
2238 glPushMatrix;
2239 glTranslate 0.375, 0.375;
2240
2241 my ($w, $h) = @$self{qw(w h)};
2242
2243 glColor 1, 0.8, 0.4;
2244 glBegin GL_QUADS;
2245 glVertex 0 , 0;
2246 glVertex 0 , $h;
2247 glVertex $w, $h;
2248 glVertex $w, 0;
2249 glEnd;
2250
2251 glColor 0, 0, 0;
2252 glBegin GL_LINE_LOOP;
2253 glVertex 0 , 0;
2254 glVertex 0 , $h;
2255 glVertex $w, $h;
2256 glVertex $w, 0;
2257 glEnd;
2258
2259 glPopMatrix;
2260
2261 glTranslate 2, 2;
2262 $self->SUPER::_draw;
2263}
2264
2265#############################################################################
2266
2267package CFClient::UI::Face;
2268
2269our @ISA = CFClient::UI::Base::;
2270
2271use CFClient::OpenGL;
2272
2273sub new {
2274 my $class = shift;
2275
2276 $class->SUPER::new (
2277 aspect => 1,
2278 @_,
2279 )
2280}
2281
2282sub size_request {
2283 (32, 8)
2284}
2285
2286sub draw {
2287 my ($self) = @_;
2288
2289 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2290
2291 if ($tex) {
2292 glEnable GL_BLEND;
2293 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
2294 glEnable GL_TEXTURE_2D;
2295 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2296 glColor 1, 1, 1, 1;
2297 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2298 glDisable GL_TEXTURE_2D;
2299 glDisable GL_BLEND;
2300 }
2301}
2302
2303#############################################################################
2304
2165package CFClient::UI::Root; 2305package CFClient::UI::Root;
2166 2306
2167our @ISA = CFClient::UI::Container::; 2307our @ISA = CFClient::UI::Container::;
2168 2308
2169use SDL::OpenGL; 2309use CFClient::OpenGL;
2170 2310
2171sub check_size { 2311sub check_size {
2172 my ($self) = @_; 2312 my ($self) = @_;
2173 2313
2174 $self->size_allocate (0, 0, $::WITH, $::HEIGHT); 2314 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2175} 2315}
2176 2316
2177sub size_request { 2317sub size_request {
2178 ($::WIDTH, $::HEIGHT) 2318 ($::WIDTH, $::HEIGHT)
2179} 2319}
2180 2320
2181sub size_allocate { 2321sub configure {
2182 my ($self, $x, $y, $w, $h) = @_; 2322 my ($self, $x, $y, $w, $h) = @_;
2183 2323
2184 $self->_size_allocate ($x, $y, $w, $h); 2324 $self->SUPER::configure ($x, $y, $w, $h);
2185 2325
2186 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
2187 for @{$self->{children}}; 2326 for my $child (@{$self->{children}}) {
2327 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2328
2329 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2330 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2331 $child->configure ($X, $Y, $W,$H);
2332 }
2188} 2333}
2189 2334
2190sub _topleft { 2335sub _topleft {
2191 my ($self, $x, $y) = @_; 2336 my ($self, $x, $y) = @_;
2192 2337
2199 $self->check_size; 2344 $self->check_size;
2200 ::refresh (); 2345 ::refresh ();
2201} 2346}
2202 2347
2203sub add { 2348sub add {
2204 my ($self, $widget) = @_; 2349 my ($self, $child) = @_;
2205 2350
2351 # integerize window positions
2352 $child->{x} = int $child->{x};
2353 $child->{y} = int $child->{y};
2354
2206 $self->SUPER::add ($widget); 2355 $self->SUPER::add ($child);
2207
2208 $widget->size_allocate (int $widget->{x}, int $widget->{y}, $widget->size_request);
2209} 2356}
2210 2357
2211sub on_refresh { 2358sub on_refresh {
2212 my ($self, $id, $cb) = @_; 2359 my ($self, $id, $cb) = @_;
2213 2360
2237############################################################################# 2384#############################################################################
2238 2385
2239package CFClient::UI; 2386package CFClient::UI;
2240 2387
2241$ROOT = new CFClient::UI::Root; 2388$ROOT = new CFClient::UI::Root;
2389$TOOLTIP = new CFClient::UI::Tooltip;
2242 2390
22431 23911
2244 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines