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.148 by elmex, Sat Apr 22 12:14:45 2006 UTC

21 $FOCUS->key_up ($_[0]) if $FOCUS; 21 $FOCUS->key_up ($_[0]) if $FOCUS;
22} 22}
23 23
24sub feed_sdl_button_down_event { 24sub feed_sdl_button_down_event {
25 my ($ev) = @_; 25 my ($ev) = @_;
26 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 26 my ($x, $y) = ($ev->{x}, $ev->{y});
27 27
28 if (!$BUTTON_STATE) { 28 if (!$BUTTON_STATE) {
29 my $widget = $ROOT->find_widget ($x, $y); 29 my $widget = $ROOT->find_widget ($x, $y);
30 30
31 $GRAB = $widget; 31 $GRAB = $widget;
32 $GRAB->update if $GRAB; 32 $GRAB->update if $GRAB;
33 } 33 }
34 34
35 $BUTTON_STATE |= 1 << ($ev->button - 1); 35 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
36 36
37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 37 $GRAB->button_down ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
38} 38}
39 39
40sub feed_sdl_button_up_event { 40sub feed_sdl_button_up_event {
41 my ($ev) = @_; 41 my ($ev) = @_;
42 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 42 my ($x, $y) = ($ev->{x}, $ev->{y});
43 43
44 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 44 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
45 45
46 $BUTTON_STATE &= ~(1 << ($ev->button - 1)); 46 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
47 47
48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB; 48 $GRAB->button_up ($ev, $GRAB->coord2local ($x, $y)) if $GRAB;
49 49
50 if (!$BUTTON_STATE) { 50 if (!$BUTTON_STATE) {
51 my $grab = $GRAB; undef $GRAB; 51 my $grab = $GRAB; undef $GRAB;
54 } 54 }
55} 55}
56 56
57sub feed_sdl_motion_event { 57sub feed_sdl_motion_event {
58 my ($ev) = @_; 58 my ($ev) = @_;
59 my ($x, $y) = ($ev->motion_x, $ev->motion_y); 59 my ($x, $y) = ($ev->{x}, $ev->{y});
60 60
61 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 61 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
62 62
63 if ($widget != $HOVER) { 63 if ($widget != $HOVER) {
64 my $hover = $HOVER; $HOVER = $widget; 64 my $hover = $HOVER; $HOVER = $widget;
74sub harmonize { 74sub harmonize {
75 my ($vals) = @_; 75 my ($vals) = @_;
76 76
77 my $rem = 0; 77 my $rem = 0;
78 78
79 for ($vals) { 79 for (@$vals) {
80 my $i = int $_ + $rem; 80 my $i = int $_ + $rem;
81 $rem += $_ - $i; 81 $rem += $_ - $i;
82 $_ = $i; 82 $_ = $i;
83 } 83 }
84} 84}
87 87
88package CFClient::UI::Base; 88package CFClient::UI::Base;
89 89
90use strict; 90use strict;
91 91
92use SDL::OpenGL; 92use CFClient::OpenGL;
93 93
94sub new { 94sub new {
95 my $class = shift; 95 my $class = shift;
96 96
97 my $self = bless { 97 my $self = bless {
98 x => 0, 98 x => 0,
99 y => 0, 99 y => 0,
100 z => 0, 100 z => 0,
101 w => -1,
102 h => -1,
103 @_ 101 @_
104 }, $class; 102 }, $class;
105 103
106 for (keys %$self) { 104 for (keys %$self) {
107 if (/^connect_(.*)$/) { 105 if (/^connect_(.*)$/) {
110 } 108 }
111 109
112 $self 110 $self
113} 111}
114 112
113sub show {
114 my ($self) = @_;
115
116 return if $self->{parent};
117
118 $CFClient::UI::ROOT->add ($self);
119}
120
121sub hide {
122 my ($self) = @_;
123
124 return unless $self->{parent};
125
126 $self->{parent}->remove ($self);
127}
128
115sub move { 129sub move {
116 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
117 $self->{x} = $x; 132 $self->{x} = int $x;
118 $self->{y} = $y; 133 $self->{y} = int $y;
119 $self->{z} = $z if defined $z; 134 $self->{z} = $z if defined $z;
135
136 $self->update;
120} 137}
121 138
122sub needs_redraw { 139sub needs_redraw {
123 0 140 0
124} 141}
125 142
126sub size_request { 143sub size_request {
127 require Carp; 144 require Carp;
128 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abstract";
129} 146}
130 147
131sub _size_allocate { 148sub configure {
132 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
133 150
151 if ($self->{aspect}) {
152 my $w2 = List::Util::min $w, int $h * $self->{aspect};
153 my $h2 = List::Util::min $h, int $w / $self->{aspect};
154
155 # use alignment to adjust x, y
156
157 $x += int +($w - $w2) * 0.5;
158 $y += int +($h - $h2) * 0.5;
159
160 ($w, $h) = ($w2, $h2);
161 }
162
163 if ($self->{x} != $x || $self->{y} != $y) {
134 $self->{x} = $x; 164 $self->{x} = $x;
135 $self->{y} = $y; 165 $self->{y} = $y;
166 $self->update;
167 }
136 168
137 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
138
139 $self->{w} = $w; 170 $self->{w} = $w;
140 $self->{h} = $h; 171 $self->{h} = $h;
141 172
173 $self->size_allocate ($w, $h);
174 $self->update;
142 1 175 }
143} 176}
144 177
145sub size_allocate { 178sub size_allocate {
146 my ($self, $x, $y, $w, $h) = @_; 179 # nothing to be done
147
148 $self->_size_allocate ($x, $y, $w, $h);
149} 180}
150 181
151# return top left coordinates 182# return top left coordinates
152sub _topleft { 183sub _topleft {
153 my ($self, $x, $y) = @_; 184 my ($self, $x, $y) = @_;
176 207
177 return if $FOCUS == $self; 208 return if $FOCUS == $self;
178 return unless $self->{can_focus}; 209 return unless $self->{can_focus};
179 210
180 my $focus = $FOCUS; $FOCUS = $self; 211 my $focus = $FOCUS; $FOCUS = $self;
212
213 $self->emit (focus_in => $focus);
214
181 $focus->update if $focus; 215 $focus->update if $focus;
182 $FOCUS->update; 216 $FOCUS->update;
183} 217}
184 218
185sub focus_out { 219sub focus_out {
186 my ($self) = @_; 220 my ($self) = @_;
187 221
188 return unless $FOCUS == $self; 222 return unless $FOCUS == $self;
189 223
190 my $focus = $FOCUS; undef $FOCUS; 224 my $focus = $FOCUS; undef $FOCUS;
225
226 $self->emit (focus_out => $focus);
227
191 $focus->update if $focus; #? 228 $focus->update if $focus; #?
192} 229}
193 230
194sub mouse_motion { } 231sub mouse_motion { }
195sub button_up { } 232sub button_up { }
219 glPopMatrix; 256 glPopMatrix;
220 257
221 if ($self == $HOVER && $self->{can_hover}) { 258 if ($self == $HOVER && $self->{can_hover}) {
222 my ($x, $y) = @$self{qw(x y)}; 259 my ($x, $y) = @$self{qw(x y)};
223 260
224 glColor 0, 0, 1, 0.2; 261 glColor 1, 0.8, 0.5, 0.2;
225 glEnable GL_BLEND; 262 glEnable GL_BLEND;
226 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 263 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
227 glBegin GL_QUADS; 264 glBegin GL_QUADS;
228 glVertex $x , $y; 265 glVertex $x , $y;
229 glVertex $x + $self->{w}, $y; 266 glVertex $x + $self->{w}, $y;
254 my ($self, $parent) = @_; 291 my ($self, $parent) = @_;
255 292
256 Scalar::Util::weaken ($self->{parent} = $parent); 293 Scalar::Util::weaken ($self->{parent} = $parent);
257} 294}
258 295
296sub check_size {
297 my ($self) = @_;
298
299 my ($w, $h) = $self->size_request;
300
301 if ($w != $self->{req_w} || $h != $self->{req_h}) {
302 $self->{req_w} = $w;
303 $self->{req_h} = $h;
304
305 $self->{parent}->check_size
306 if $self->{parent};
307 }
308}
309
259sub update { 310sub update {
260 my ($self) = @_; 311 my ($self) = @_;
261 312
262 $self->{parent}->update 313 $self->{parent}->update
263 if $self->{parent}; 314 if $self->{parent};
264} 315}
265 316
266sub connect { 317sub connect {
267 my ($self, $signal, $cb) = @_; 318 my ($self, $signal, $cb) = @_;
268 319
269 push @{ $self->{cb}{$signal} }, $cb; 320 push @{ $self->{signal_cb}{$signal} }, $cb;
270} 321}
271 322
272sub emit { 323sub emit {
273 my ($self, $signal, @args) = @_; 324 my ($self, $signal, @args) = @_;
274 325
326 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
275 $_->($self, @args) 327 $cb->($self, @args);
276 for @{$self->{cb}{$signal} || []}; 328 }
277} 329}
278 330
279sub DESTROY { 331sub DESTROY {
280 my ($self) = @_; 332 my ($self) = @_;
281 333
287package CFClient::UI::DrawBG; 339package CFClient::UI::DrawBG;
288 340
289our @ISA = CFClient::UI::Base::; 341our @ISA = CFClient::UI::Base::;
290 342
291use strict; 343use strict;
292use SDL::OpenGL; 344use CFClient::OpenGL;
293 345
294sub new { 346sub new {
295 my $class = shift; 347 my $class = shift;
296 348
297 # range [value, low, high, page] 349 # range [value, low, high, page]
361 $self->{children} = [ 413 $self->{children} = [
362 sort { $a->{z} <=> $b->{z} } 414 sort { $a->{z} <=> $b->{z} }
363 @{$self->{children}}, $child 415 @{$self->{children}}, $child
364 ]; 416 ];
365 417
366 $self->{w} = $self->{h} = -1; 418 $child->check_size;
367 $self->update;
368} 419}
369 420
370sub remove { 421sub remove {
371 my ($self, $widget) = @_; 422 my ($self, $child) = @_;
372 423
424 delete $child->{parent};
425
373 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
374 427
375 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 428 $self->check_size;
376} 429}
377 430
378sub find_widget { 431sub find_widget {
379 my ($self, $x, $y) = @_; 432 my ($self, $x, $y) = @_;
380 433
410 463
411 $class->SUPER::new (children => [$child], %arg) 464 $class->SUPER::new (children => [$child], %arg)
412} 465}
413 466
414sub add { 467sub add {
415 my ($self, $widget) = @_; 468 my ($self, $child) = @_;
416 469
417 $self->{children} = []; 470 $self->{children} = [];
418 471
419 $self->SUPER::add ($widget); 472 $self->SUPER::add ($child);
420} 473}
421 474
422sub remove { 475sub remove {
423 my ($self, $widget) = @_; 476 my ($self, $widget) = @_;
424 477
433sub size_request { 486sub size_request {
434 $_[0]{children}[0]->size_request 487 $_[0]{children}[0]->size_request
435} 488}
436 489
437sub size_allocate { 490sub size_allocate {
438 my ($self, $x, $y, $w, $h) = @_; 491 my ($self, $w, $h) = @_;
439 492
440 $self->_size_allocate ($x, $y, $w, $h) or return;
441
442 $self->{children}[0]->size_allocate (0, 0, $w, $h); 493 $self->{children}[0]->configure (0, 0, $w, $h);
443} 494}
444 495
445############################################################################# 496#############################################################################
446 497
447package CFClient::UI::Window; 498package CFClient::UI::Window;
448 499
449our @ISA = CFClient::UI::Bin::; 500our @ISA = CFClient::UI::Bin::;
450 501
451use SDL::OpenGL; 502use CFClient::OpenGL;
452 503
453sub new { 504sub new {
454 my ($class, %arg) = @_; 505 my ($class, %arg) = @_;
455 506
456 my $self = $class->SUPER::new (%arg); 507 my $self = $class->SUPER::new (%arg);
473 $self->child->draw; 524 $self->child->draw;
474 }; 525 };
475} 526}
476 527
477sub size_allocate { 528sub size_allocate {
478 my ($self, $x, $y, $w, $h) = @_; 529 my ($self, $w, $h) = @_;
479 530
480 $self->_size_allocate ($x, $y, $w, $h) or return;
481
482 $self->child->size_allocate (0, 0, $w, $h); 531 $self->child->configure (0, 0, $w, $h);
483 532
484 $self->render_chld; 533 $self->render_chld;
485} 534}
486 535
487sub _draw { 536sub _draw {
503 glDisable GL_TEXTURE_2D; 552 glDisable GL_TEXTURE_2D;
504} 553}
505 554
506############################################################################# 555#############################################################################
507 556
557package CFClient::UI::ViewPort;
558
559our @ISA = CFClient::UI::Window::;
560
561sub new { die }
562
563sub size_request {
564 my ($self) = @_;
565
566 @$self{qw(child_w child_h)} = $self->child->size_request;
567 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)});
568
569 @$self{qw(child_w child_h)}
570}
571
572sub _draw {
573 my ($self) = @_;
574
575 $self->{children}[1]->draw;
576}
577
578
579#############################################################################
580
508package CFClient::UI::Frame; 581package CFClient::UI::Frame;
509 582
510our @ISA = CFClient::UI::Bin::; 583our @ISA = CFClient::UI::Bin::;
511 584
512use SDL::OpenGL; 585use 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 586
562sub new { 587sub new {
563 my $class = shift; 588 my $class = shift;
564 589
565 # TODO: user_x, user_y, overwrite moveto?
566
567 $class->SUPER::new ( 590 my $self = $class->SUPER::new (
568 bg => [1, 1, 1, 1], 591 bg => [1, 1, 1, 1],
569 border_bg => [1, 1, 1, 1], 592 border_bg => [1, 1, 1, 1],
570 border => $::FONTSIZE * 0.8, 593 border => 0.8,
571 @_ 594 @_
595 );
596
597 $self
598}
599
600sub set_size {
601 my ($self, $w, $h) = @_;
602 $self->{req_w} = $w;
603 $self->{req_h} = $h;
604 $self->check_size;
605}
606
607sub size_request {
608 my ($self) = @_;
609 ($self->{req_w}, $self->{req_h})
610}
611
612sub size_allocate {
613 my ($self, $w, $h) = @_;
614 $self->{w} = $w;
615 $self->{h} = $h;
616 $self->child->configure (0, 0, $w, $h);
617}
618
619sub _draw {
620 my ($self) = @_;
621
622 my ($w, $h) = ($self->{w}, $self->{h});
623
624 glEnable GL_BLEND;
625 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
626 glEnable GL_TEXTURE_2D;
627 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
628
629# glBegin GL_QUADS;
630# glColor 0, 0, 0, 0;
631# glVertex 0 , 0;
632# glVertex 0 , $h;
633# glVertex $w, $h;
634# glVertex $w, 0;
635# glEnd;
636
637
638 $self->child->draw;
639 glDisable GL_BLEND;
640 glDisable GL_TEXTURE_2D;
641}
642
643#############################################################################
644
645package CFClient::UI::FancyFrame;
646
647our @ISA = CFClient::UI::Bin::;
648
649use CFClient::OpenGL;
650
651my @tex =
652 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
653 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
654
655sub new {
656 my $class = shift;
657
658 # TODO: user_x, user_y, overwrite moveto?
659
660 my $self = $class->SUPER::new (
661 bg => [1, 1, 1, 1],
662 border_bg => [1, 1, 1, 1],
663 border => 0.8,
664 @_
665 );
666
667 $self->{title} &&= new CFClient::UI::Label
668 align => 0,
669 valign => 1,
670 text => $self->{title},
671 fontsize => 1;
672
673 $self
674}
675
676sub border {
677 int $_[0]{border} * $::FONTSIZE
678}
679
680sub size_request {
681 my ($self) = @_;
682
683 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
684
685 my ($w, $h) = $self->SUPER::size_request;
686
687 (
688 $w + $self->border * 2,
689 $h + $self->border * 2,
572 ) 690 )
573} 691}
574 692
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 { 693sub size_allocate {
589 my ($self, $x, $y, $w, $h) = @_; 694 my ($self, $w, $h) = @_;
590 695
591 $self->_size_allocate ($x, $y, $w, $h) or return;
592
593 $h -= List::Util::max 0, $self->{border} * 2; 696 $h -= List::Util::max 0, $self->border * 2;
594 $w -= List::Util::max 0, $self->{border} * 2; 697 $w -= List::Util::max 0, $self->border * 2;
595 698
699 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
700 if $self->{title};
701
596 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 702 $self->child->configure ($self->border, $self->border, $w, $h);
597} 703}
598 704
599sub button_down { 705sub button_down {
600 my ($self, $ev, $x, $y) = @_; 706 my ($self, $ev, $x, $y) = @_;
601 707
708 my $border = $self->border;
709
602 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 710 if ($x < $self->{w} && $x >= $self->{w} - $border
603 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 711 && $y < $self->{h} && $y >= $self->{h} - $border) {
604 712
605 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 713 my ($ox, $oy) = ($ev->{x}, $ev->{y});
606 my ($bw, $bh) = ($self->{w}, $self->{h}); 714 my ($bw, $bh) = ($self->{w}, $self->{h});
607 715
608 $self->{motion} = sub { 716 $self->{motion} = sub {
609 my ($ev, $x, $y) = @_; 717 my ($ev, $x, $y) = @_;
610 718
611 ($x, $y) = ($ev->motion_x, $ev->motion_y); 719 ($x, $y) = ($ev->{x}, $ev->{y});
612 720
613 $self->{user_w} = $bw + $x - $ox; 721 $self->{user_w} = $bw + $x - $ox;
614 $self->{user_h} = $bh + $y - $oy; 722 $self->{user_h} = $bh + $y - $oy;
615 $self->update; 723 $self->check_size;
616 }; 724 };
617 725
618 } elsif ($x >= 0 && $x < $self->{w} 726 } elsif ($x >= 0 && $x < $self->{w}
619 && $y >= 0 && $y < $self->{border}) { 727 && $y >= 0 && $y < $border) {
620 728
621 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 729 my ($ox, $oy) = ($ev->{x}, $ev->{y});
622 my ($bx, $by) = ($self->{x}, $self->{y}); 730 my ($bx, $by) = ($self->{x}, $self->{y});
623 731
624 $self->{motion} = sub { 732 $self->{motion} = sub {
625 my ($ev, $x, $y) = @_; 733 my ($ev, $x, $y) = @_;
626 734
627 ($x, $y) = ($ev->motion_x, $ev->motion_y); 735 ($x, $y) = ($ev->{x}, $ev->{y});
628 736
629 $self->move ($bx + $x - $ox, $by + $y - $oy); 737 $self->move ($bx + $x - $ox, $by + $y - $oy);
630 $self->update; 738 $self->update;
631 }; 739 };
632 } 740 }
653 glEnable GL_BLEND; 761 glEnable GL_BLEND;
654 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 762 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
655 glEnable GL_TEXTURE_2D; 763 glEnable GL_TEXTURE_2D;
656 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 764 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
657 765
766 my $border = $self->border;
767
658 glColor @{ $self->{border_bg} }; 768 glColor @{ $self->{border_bg} };
659 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 769 $tex[1]->draw_quad (0, 0, $w, $border);
660 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 770 $tex[3]->draw_quad (0, $border, $border, $ch);
661 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 771 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
662 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 772 $tex[4]->draw_quad (0, $h - $border, $w, $border);
663 773
664 my $bg = $tex[0]; 774 my $bg = $tex[0];
665 775
666 # TODO: repeat texture not scale 776 # TODO: repeat texture not scale
667 my $rep_x = $cw / $bg->{w}; 777 my $rep_x = $cw / $bg->{w};
670 glColor @{ $self->{bg} }; 780 glColor @{ $self->{bg} };
671 781
672 $bg->{s} = $rep_x; 782 $bg->{s} = $rep_x;
673 $bg->{t} = $rep_y; 783 $bg->{t} = $rep_y;
674 $bg->{wrap_mode} = 1; 784 $bg->{wrap_mode} = 1;
675 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 785 $bg->draw_quad ($border, $border, $cw, $ch);
676 786
677 glDisable GL_TEXTURE_2D; 787 glDisable GL_TEXTURE_2D;
678 glDisable GL_BLEND; 788 glDisable GL_BLEND;
679 789
790 $self->{title}->draw if $self->{title};
680 $self->child->draw; 791 $self->child->draw;
681} 792}
682 793
683############################################################################# 794#############################################################################
684 795
686 797
687our @ISA = CFClient::UI::Base::; 798our @ISA = CFClient::UI::Base::;
688 799
689use List::Util qw(max sum); 800use List::Util qw(max sum);
690 801
691use SDL::OpenGL; 802use CFClient::OpenGL;
692 803
693sub new { 804sub new {
694 my $class = shift; 805 my $class = shift;
695 806
696 $class->SUPER::new ( 807 $class->SUPER::new (
703 my ($self, $x, $y, $child) = @_; 814 my ($self, $x, $y, $child) = @_;
704 815
705 $child->set_parent ($self); 816 $child->set_parent ($self);
706 $self->{children}[$y][$x] = $child; 817 $self->{children}[$y][$x] = $child;
707 818
708 $self->{w} = $self->{h} = -1; 819 $child->check_size;
709 $self->update;
710} 820}
711 821
712# TODO: move to container class maybe? send childs a signal on removal? 822# TODO: move to container class maybe? send childs a signal on removal?
713sub clear { 823sub clear {
714 my ($self) = @_; 824 my ($self) = @_;
749 (sum @$hs), 859 (sum @$hs),
750 ) 860 )
751} 861}
752 862
753sub size_allocate { 863sub size_allocate {
754 my ($self, $x, $y, $w, $h) = @_; 864 my ($self, $w, $h) = @_;
755
756 $self->_size_allocate ($x, $y, $w, $h) or return;
757 865
758 my ($ws, $hs) = $self->get_wh; 866 my ($ws, $hs) = $self->get_wh;
759 867
760 my $req_w = sum @$ws; 868 my $req_w = sum @$ws;
761 my $req_h = sum @$hs; 869 my $req_h = sum @$hs;
783 891
784 for my $c (0 .. $#$row) { 892 for my $c (0 .. $#$row) {
785 my $col_w = $ws->[$c]; 893 my $col_w = $ws->[$c];
786 894
787 if (my $widget = $row->[$c]) { 895 if (my $widget = $row->[$c]) {
788 $widget->size_allocate ($x, $y, $col_w, $row_h); 896 $widget->configure ($x, $y, $col_w, $row_h);
789 } 897 }
790 898
791 $x += $col_w; 899 $x += $col_w;
792 } 900 }
793 901
838 (List::Util::max map $_->[1], @alloc), 946 (List::Util::max map $_->[1], @alloc),
839 ) 947 )
840} 948}
841 949
842sub size_allocate { 950sub size_allocate {
843 my ($self, $x, $y, $w, $h) = @_; 951 my ($self, $w, $h) = @_;
844
845 $self->_size_allocate ($x, $y, $w, $h);
846 952
847 ($h, $w) = ($w, $h); 953 ($h, $w) = ($w, $h);
848 954
849 my $children = $self->{children}; 955 my $children = $self->{children};
850 956
872 978
873 my $y = 0; 979 my $y = 0;
874 for (0 .. $#$children) { 980 for (0 .. $#$children) {
875 my $child = $children->[$_]; 981 my $child = $children->[$_];
876 my $h = $h[$_]; 982 my $h = $h[$_];
877 $child->size_allocate ($y, 0, $h, $w); 983 $child->configure ($y, 0, $h, $w);
878 984
879 $y += $h; 985 $y += $h;
880 } 986 }
987
988 1
881} 989}
882 990
883############################################################################# 991#############################################################################
884 992
885package CFClient::UI::VBox; 993package CFClient::UI::VBox;
898 (List::Util::sum map $_->[1], @alloc), 1006 (List::Util::sum map $_->[1], @alloc),
899 ) 1007 )
900} 1008}
901 1009
902sub size_allocate { 1010sub size_allocate {
903 my ($self, $x, $y, $w, $h) = @_; 1011 my ($self, $w, $h) = @_;
904
905 $self->_size_allocate ($x, $y, $w, $h);
906 1012
907 my $children = $self->{children}; 1013 my $children = $self->{children};
908 1014
909 my @h = map +($_->size_request)[1], @$children; 1015 my @h = map +($_->size_request)[1], @$children;
910 1016
928 1034
929 my $y = 0; 1035 my $y = 0;
930 for (0 .. $#$children) { 1036 for (0 .. $#$children) {
931 my $child = $children->[$_]; 1037 my $child = $children->[$_];
932 my $h = $h[$_]; 1038 my $h = $h[$_];
933 $child->size_allocate (0, $y, $w, $h); 1039 $child->configure (0, $y, $w, $h);
934 1040
935 $y += $h; 1041 $y += $h;
936 } 1042 }
1043
1044 1
937} 1045}
938 1046
939############################################################################# 1047#############################################################################
940 1048
941package CFClient::UI::Label; 1049package CFClient::UI::Label;
942 1050
943our @ISA = CFClient::UI::Base::; 1051our @ISA = CFClient::UI::Base::;
944 1052
945use SDL::OpenGL; 1053use CFClient::OpenGL;
946 1054
947sub new { 1055sub new {
948 my ($class, %arg) = @_; 1056 my ($class, %arg) = @_;
949 1057
950 my $self = $class->SUPER::new ( 1058 my $self = $class->SUPER::new (
951 fg => [1, 1, 1], 1059 fg => [1, 1, 1],
952 fontsize => $::FONTSIZE, 1060 fontsize => 1,
953 text => "", 1061 text => "",
954 align => -1, 1062 align => -1,
1063 valign => -1,
955 padding => 2, 1064 padding => 2,
956 layout => new CFClient::Layout, 1065 layout => new CFClient::Layout,
957 %arg 1066 %arg
958 ); 1067 );
959 1068
960 $self->set_text ($self->{text}); 1069 if (exists $self->{template}) {
1070 my $layout = new CFClient::Layout;
1071 $layout->set_text (delete $self->{template});
1072 $self->{template} = $layout;
1073 }
1074
1075 $self->set_text (delete $self->{text}) if exists $self->{text};
1076 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
961 1077
962 $self 1078 $self
963} 1079}
964 1080
965sub escape_text { 1081sub escape_text {
973} 1089}
974 1090
975sub set_text { 1091sub set_text {
976 my ($self, $text) = @_; 1092 my ($self, $text) = @_;
977 1093
978 $self->{text} = $text;
979 $self->{layout}->set_markup ($text); 1094 $self->{layout}->set_text ($text);
980 1095
981 delete $self->{texture}; 1096 delete $self->{texture};
982# $self->{w} = $self->{h} = -1;
983 $self->update; 1097 $self->update;
984} 1098}
985 1099
986sub get_text { 1100sub set_markup {
987 my ($self, $text) = @_; 1101 my ($self, $markup) = @_;
988 1102
989 $self->{text} 1103 $self->{layout}->set_markup ($markup);
1104
1105 delete $self->{texture};
1106 $self->update;
990} 1107}
991 1108
992sub size_request { 1109sub size_request {
993 my ($self) = @_; 1110 my ($self) = @_;
994 1111
995 $self->{layout}->set_width; 1112 $self->{layout}->set_width;
996 $self->{layout}->set_height ($self->{fontsize}); 1113 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1114
997 my ($w, $h) = $self->{layout}->size; 1115 my ($w, $h) = $self->{layout}->size;
1116
1117 if (exists $self->{template}) {
1118 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1119
1120 my ($w2, $h2) = $self->{template}->size;
1121
1122 $w = List::Util::max $w, $w2;
1123 $h = List::Util::max $h, $h2;
1124 }
998 1125
999 ( 1126 (
1000 $w + $self->{padding} * 2, 1127 $w + $self->{padding} * 2,
1001 $h + $self->{padding} * 2, 1128 $h + $self->{padding} * 2,
1002 ) 1129 )
1003} 1130}
1004 1131
1005sub size_allocate { 1132sub size_allocate {
1006 my ($self, $x, $y, $w, $h) = @_; 1133 my ($self, $w, $h) = @_;
1007
1008 $self->_size_allocate ($x, $y, $w, $h) or return;
1009 1134
1010 delete $self->{texture}; 1135 delete $self->{texture};
1011} 1136}
1012 1137
1013sub update { 1138sub set_fontsize {
1014 my ($self) = @_; 1139 my ($self, $fontsize) = @_;
1015 1140
1016 delete $self->{texture}; 1141 $self->{fontsize} = $fontsize;
1017 $self->SUPER::update; 1142 $self->check_size;
1018} 1143}
1019 1144
1020sub _draw { 1145sub _draw {
1021 my ($self) = @_; 1146 my ($self) = @_;
1022 1147
1023 my $tex = $self->{texture} ||= do { 1148 my $tex = $self->{texture} ||= do {
1024 $self->{layout}->set_width ($self->{w}); 1149 $self->{layout}->set_width ($self->{w});
1025 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1150 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
1026 new_from_layout CFClient::Texture $self->{layout} 1151 new_from_layout CFClient::Texture $self->{layout}
1027 }; 1152 };
1028 1153
1029 glEnable GL_BLEND; 1154 glEnable GL_BLEND;
1030 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1155 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1031 glEnable GL_TEXTURE_2D; 1156 glEnable GL_TEXTURE_2D;
1032 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1157 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1033 1158
1034 glColor @{$self->{fg}}; 1159 glColor @{$self->{fg}};
1035 1160
1036 my $x = 1161 $self->{ox} = int (
1037 $self->{align} < 0 ? $self->{padding} 1162 $self->{align} < 0 ? $self->{padding}
1038 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1163 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1039 : ($self->{w} - $tex->{w}) * 0.5; 1164 : ($self->{w} - $tex->{w}) * 0.5
1165 );
1040 1166
1041 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1167 $self->{oy} = int (
1168 $self->{valign} < 0 ? $self->{padding}
1169 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1170 : ($self->{h} - $tex->{h}) * 0.5
1171 );
1172
1173 $tex->draw_quad ($self->{ox}, $self->{oy});
1042 1174
1043 glDisable GL_TEXTURE_2D; 1175 glDisable GL_TEXTURE_2D;
1044 glDisable GL_BLEND; 1176 glDisable GL_BLEND;
1045} 1177}
1046 1178
1047############################################################################# 1179#############################################################################
1048 1180
1049package CFClient::UI::Entry; 1181package CFClient::UI::EntryBase;
1050 1182
1051our @ISA = CFClient::UI::Label::; 1183our @ISA = CFClient::UI::Label::;
1052 1184
1053use SDL; 1185use CFClient::OpenGL;
1054use SDL::OpenGL;
1055 1186
1056sub new { 1187sub new {
1057 my $class = shift; 1188 my $class = shift;
1058 1189
1059 $class->SUPER::new ( 1190 $class->SUPER::new (
1061 bg => [0, 0, 0, 0.2], 1192 bg => [0, 0, 0, 0.2],
1062 active_bg => [1, 1, 1, 0.5], 1193 active_bg => [1, 1, 1, 0.5],
1063 active_fg => [0, 0, 0], 1194 active_fg => [0, 0, 0],
1064 can_hover => 1, 1195 can_hover => 1,
1065 can_focus => 1, 1196 can_focus => 1,
1197 valign => 0,
1066 @_ 1198 @_
1067 ) 1199 )
1068} 1200}
1069 1201
1070sub _set_text { 1202sub _set_text {
1071 my ($self, $text) = @_; 1203 my ($self, $text) = @_;
1072 1204
1073 my $old_text = $self->{text}; 1205 delete $self->{cur_h};
1206
1207 return if $self->{text} eq $text;
1208
1209 delete $self->{texture};
1074 1210
1075 $self->{last_activity} = $::NOW; 1211 $self->{last_activity} = $::NOW;
1076
1077 $self->{text} = $text; 1212 $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 1213
1081 $text =~ s/./*/g if $self->{hidden}; 1214 $text =~ s/./*/g if $self->{hidden};
1215 $self->{layout}->set_text ("$text ");
1082 1216
1083 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1217 $self->emit (changed => $self->{text});
1218}
1084 1219
1085 $text = substr $text, 0, $self->{cursor}; 1220sub get_text {
1086 utf8::encode $text; 1221 $_[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} 1222}
1093 1223
1094sub size_request { 1224sub size_request {
1095 my ($self) = @_; 1225 my ($self) = @_;
1096 1226
1098 1228
1099 ($w + 1, $h) # add 1 for cursor 1229 ($w + 1, $h) # add 1 for cursor
1100} 1230}
1101 1231
1102sub size_allocate { 1232sub size_allocate {
1103 my ($self, $x, $y, $w, $h) = @_; 1233 my ($self, $w, $h) = @_;
1104
1105 $self->SUPER::size_allocate ($x, $y, $w, $h);
1106 1234
1107 $self->_set_text ($self->{text}); 1235 $self->_set_text ($self->{text});
1108} 1236}
1109 1237
1110sub set_text { 1238sub set_text {
1116} 1244}
1117 1245
1118sub key_down { 1246sub key_down {
1119 my ($self, $ev) = @_; 1247 my ($self, $ev) = @_;
1120 1248
1121 my $mod = $ev->key_mod; 1249 my $mod = $ev->{mod};
1122 my $sym = $ev->key_sym; 1250 my $sym = $ev->{sym};
1123
1124 my $uni = $ev->key_unicode; 1251 my $uni = $ev->{unicode};
1125 1252
1126 my $text = $self->get_text; 1253 my $text = $self->get_text;
1127 1254
1128 if ($sym == SDLK_BACKSPACE) { 1255 if ($sym == 8) {
1129 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1256 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1130 } elsif ($sym == SDLK_DELETE) { 1257 } elsif ($sym == 127) {
1131 substr $text, $self->{cursor}, 1, ""; 1258 substr $text, $self->{cursor}, 1, "";
1132 } elsif ($sym == SDLK_LEFT) { 1259 } elsif ($sym == CFClient::SDLK_LEFT) {
1133 --$self->{cursor} if $self->{cursor}; 1260 --$self->{cursor} if $self->{cursor};
1134 } elsif ($sym == SDLK_RIGHT) { 1261 } elsif ($sym == CFClient::SDLK_RIGHT) {
1135 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1262 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1136 } elsif ($sym == SDLK_HOME) { 1263 } elsif ($sym == CFClient::SDLK_HOME) {
1137 $self->{cursor} = 0; 1264 $self->{cursor} = 0;
1138 } elsif ($sym == SDLK_END) { 1265 } elsif ($sym == CFClient::SDLK_END) {
1139 $self->{cursor} = length $text; 1266 $self->{cursor} = length $text;
1140 } elsif ($sym == SDLK_ESCAPE) { 1267 } elsif ($sym == 27) {
1141 $self->emit ('escape'); 1268 $self->emit ('escape');
1142 } elsif ($uni) { 1269 } elsif ($uni) {
1143 substr $text, $self->{cursor}++, 0, chr $uni; 1270 substr $text, $self->{cursor}++, 0, chr $uni;
1144 } 1271 }
1145 1272
1200 1327
1201 $self->SUPER::_draw; 1328 $self->SUPER::_draw;
1202 1329
1203 #TODO: force update every cursor change :( 1330 #TODO: force update every cursor change :(
1204 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1331 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1332
1333 unless (exists $self->{cur_h}) {
1334 my $text = substr $self->{text}, 0, $self->{cursor};
1335 utf8::encode $text;
1336
1337 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1338 }
1339
1205 glColor @{$self->{fg}}; 1340 glColor @{$self->{fg}};
1206 glBegin GL_LINES; 1341 glBegin GL_LINES;
1207 glVertex $self->{cur_x}, $self->{cur_y};
1208 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1342 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1343 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1209 glEnd; 1344 glEnd;
1210 } 1345 }
1211} 1346}
1212 1347
1213package CFClient::UI::LineEntry; 1348package CFClient::UI::Entry;
1214 1349
1215our @ISA = CFClient::UI::Entry::; 1350our @ISA = CFClient::UI::EntryBase::;
1216 1351
1217use SDL; 1352use CFClient::OpenGL;
1218use SDL::OpenGL;
1219 1353
1220sub key_down { 1354sub key_down {
1221 my ($self, $ev) = @_; 1355 my ($self, $ev) = @_;
1222 1356
1223 my $sym = $ev->key_sym; 1357 my $sym = $ev->{sym};
1224 1358
1225 if ($sym == SDLK_RETURN) { 1359 if ($sym == 13) {
1226 $self->emit (activate => $self->get_text); 1360 $self->emit (activate => $self->get_text);
1227 $self->update; 1361 $self->update;
1228 1362
1229 } else { 1363 } else {
1230 $self->SUPER::key_down ($ev); 1364 $self->SUPER::key_down ($ev);
1236 1370
1237package CFClient::UI::Button; 1371package CFClient::UI::Button;
1238 1372
1239our @ISA = CFClient::UI::Label::; 1373our @ISA = CFClient::UI::Label::;
1240 1374
1241use SDL; 1375use CFClient::OpenGL;
1242use SDL::OpenGL;
1243 1376
1244my @tex = 1377my @tex =
1245 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1378 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1246 qw(b1_button_active.png); 1379 qw(b1_button_active.png);
1247 1380
1248sub new { 1381sub new {
1249 my $class = shift; 1382 my $class = shift;
1250 1383
1251 $class->SUPER::new ( 1384 $class->SUPER::new (
1252 padding => 4, 1385 padding => 4,
1253 fg => [1, 1, 1], 1386 fg => [1, 1, 1],
1254 bg => [1, 1, 1, 0.2], 1387 bg => [1, 1, 1, 0.2],
1255 active_fg => [1, 1, 0], 1388 active_fg => [0, 0, 1],
1256 can_hover => 1, 1389 can_hover => 1,
1390 align => 0,
1391 valign => 0,
1257 @_ 1392 @_
1258 ) 1393 )
1259} 1394}
1260 1395
1261sub button_up { 1396sub button_up {
1269 1404
1270sub _draw { 1405sub _draw {
1271 my ($self) = @_; 1406 my ($self) = @_;
1272 1407
1273 local $self->{fg} = $self->{fg}; 1408 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 1409
1280 if ($GRAB == $self) { 1410 if ($GRAB == $self) {
1281 $self->{fg} = $self->{active_fg}; 1411 $self->{fg} = $self->{active_fg};
1282 } 1412 }
1283 1413
1284 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1414 glEnable GL_BLEND;
1415 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1416 glEnable GL_TEXTURE_2D;
1285 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1417 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1418 glColor 0, 0, 0, 1;
1286 1419
1287 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1420 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1288 1421
1289 glDisable GL_TEXTURE_2D; 1422 glDisable GL_TEXTURE_2D;
1290 glDisable GL_BLEND; 1423 glDisable GL_BLEND;
1291 1424
1292 $self->SUPER::_draw; 1425 $self->SUPER::_draw;
1297package CFClient::UI::CheckBox; 1430package CFClient::UI::CheckBox;
1298 1431
1299our @ISA = CFClient::UI::DrawBG::; 1432our @ISA = CFClient::UI::DrawBG::;
1300 1433
1301my @tex = 1434my @tex =
1302 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1435 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1303 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1436 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1304 1437
1305use SDL; 1438use CFClient::OpenGL;
1306use SDL::OpenGL;
1307 1439
1308sub new { 1440sub new {
1309 my $class = shift; 1441 my $class = shift;
1310 1442
1311 $class->SUPER::new ( 1443 $class->SUPER::new (
1322 my ($self) = @_; 1454 my ($self) = @_;
1323 1455
1324 ($self->{padding} * 2 + 6) x 2 1456 ($self->{padding} * 2 + 6) x 2
1325} 1457}
1326 1458
1327sub size_allocate {
1328 my ($self, $x, $y, $w, $h) = @_;
1329
1330 $self->_size_allocate ($x, $y, $w, $h);
1331}
1332
1333sub button_down { 1459sub button_down {
1334 my ($self, $ev, $x, $y) = @_; 1460 my ($self, $ev, $x, $y) = @_;
1335 1461
1336 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1462 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1337 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1463 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1363 glDisable GL_BLEND; 1489 glDisable GL_BLEND;
1364} 1490}
1365 1491
1366############################################################################# 1492#############################################################################
1367 1493
1494package CFClient::UI::Image;
1495
1496our @ISA = CFClient::UI::Base::;
1497
1498use CFClient::OpenGL;
1499use Carp qw/confess/;
1500
1501our %loaded_images;
1502
1503sub new {
1504 my $class = shift;
1505
1506 my $self = $class->SUPER::new (@_);
1507
1508 $self->{image} or confess "Image has 'image' not set. This is a fatal error!";
1509
1510 $loaded_images{$self->{image}} ||=
1511 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1;
1512
1513 my $tex = $self->{tex} = $loaded_images{$self->{image}};
1514
1515 Scalar::Util::weaken $loaded_images{$self->{image}};
1516
1517 $self->{aspect} = $tex->{w} / $tex->{h};
1518
1519 $self
1520}
1521
1522sub size_request {
1523 my ($self) = @_;
1524
1525 ($self->{tex}->{w}, $self->{tex}->{h})
1526}
1527
1528sub _draw {
1529 my ($self) = @_;
1530
1531 my $tex = $self->{tex};
1532
1533 my ($w, $h) = ($self->{w}, $self->{h});
1534
1535 if ($self->{rot90}) {
1536 glRotate 90, 0, 0, 1;
1537 glTranslate 0, -$self->{w}, 0;
1538
1539 ($w, $h) = ($h, $w);
1540 }
1541
1542 glEnable GL_BLEND;
1543 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1544 glEnable GL_TEXTURE_2D;
1545 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1546
1547 $tex->draw_quad (0, 0, $w, $h);
1548
1549 glDisable GL_BLEND;
1550 glDisable GL_TEXTURE_2D;
1551}
1552
1553#############################################################################
1554
1555package CFClient::UI::VGauge;
1556
1557our @ISA = CFClient::UI::Base::;
1558
1559use CFClient::OpenGL;
1560
1561my %tex = (
1562 food => [
1563 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1564 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1565 ],
1566 grace => [
1567 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1568 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1569 ],
1570 hp => [
1571 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1572 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1573 ],
1574 mana => [
1575 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1576 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1577 ],
1578);
1579
1580# eg. VGauge->new (gauge => 'food'), default gauge: food
1581sub new {
1582 my $class = shift;
1583
1584 my $self = $class->SUPER::new (
1585 type => 'food',
1586 @_
1587 );
1588
1589 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1590
1591 $self
1592}
1593
1594sub size_request {
1595 my ($self) = @_;
1596
1597 #my $tex = $tex{$self->{type}}[0];
1598 #@$tex{qw(w h)}
1599 (0, 0)
1600}
1601
1602sub set_max {
1603 my ($self, $max) = @_;
1604
1605 $self->{max_val} = $max;
1606}
1607
1608sub set_value {
1609 my ($self, $val, $max) = @_;
1610
1611 $self->set_max ($max)
1612 if defined $max;
1613
1614 $max = $self->{max_val};
1615 $self->{val} = $val;
1616
1617 $self->update;
1618}
1619
1620sub _draw {
1621 my ($self) = @_;
1622
1623 my $tex = $tex{$self->{type}};
1624
1625 my ($w, $h) = ($self->{w}, $self->{h});
1626
1627 if ($self->{vertical}) {
1628 glRotate 90, 0, 0, 1;
1629 glTranslate 0, -$self->{w}, 0;
1630
1631 ($w, $h) = ($h, $w);
1632 }
1633
1634 my $ycut = $self->{val} / ($self->{max_val} || 1);
1635 $ycut = 1 if $self->{val} > $self->{max_val};
1636
1637 my $t1 = $tex->[0];
1638 my $t2 = $tex->[1];
1639
1640 glEnable GL_BLEND;
1641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1642 glEnable GL_TEXTURE_2D;
1643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1644
1645 my $h1 = $self->{h} - $ycut * $self->{h};
1646 my $h2 = $ycut * $self->{h};
1647
1648 glBindTexture GL_TEXTURE_2D, $t1->{name};
1649 glBegin GL_QUADS;
1650 glTexCoord 0 , 0; glVertex 0 , 0;
1651 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1652 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1653 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1654 glEnd;
1655
1656 glBindTexture GL_TEXTURE_2D, $t2->{name};
1657 glBegin GL_QUADS;
1658 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1659 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1660 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1661 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1662 glEnd;
1663
1664 glDisable GL_BLEND;
1665 glDisable GL_TEXTURE_2D;
1666}
1667
1668#############################################################################
1669
1670package CFClient::UI::Gauge;
1671
1672our @ISA = CFClient::UI::VBox::;
1673
1674sub new {
1675 my ($class, %arg) = shift;
1676
1677 my $self = $class->SUPER::new (
1678 @_,
1679 );
1680
1681 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1682 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1683 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1684
1685 $self
1686}
1687
1688sub set_fontsize {
1689 my ($self, $fsize) = @_;
1690
1691 $self->{value}->set_fontsize ($fsize);
1692 $self->{max} ->set_fontsize ($fsize);
1693}
1694
1695sub set_value {
1696 my ($self, $val, $max) = @_;
1697
1698 $self->set_max ($max)
1699 if defined $max;
1700
1701 $self->{gauge}->set_value ($val, $max);
1702 $self->{value}->set_text ($val);
1703}
1704
1705sub set_max {
1706 my ($self, $max) = @_;
1707
1708 $self->{gauge}->set_max ($max);
1709 $self->{max}->set_text ($max);
1710}
1711
1712#############################################################################
1713
1368package CFClient::UI::Slider; 1714package CFClient::UI::Slider;
1369 1715
1370use strict; 1716use strict;
1371 1717
1372use SDL::OpenGL; 1718use CFClient::OpenGL;
1373 1719
1374our @ISA = CFClient::UI::DrawBG::; 1720our @ISA = CFClient::UI::DrawBG::;
1375 1721
1376my @tex = 1722my @tex =
1377 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1723 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1387 # TODO: calculations are off 1733 # TODO: calculations are off
1388 my $self = $class->SUPER::new ( 1734 my $self = $class->SUPER::new (
1389 fg => [1, 1, 1], 1735 fg => [1, 1, 1],
1390 active_fg => [0, 0, 0], 1736 active_fg => [0, 0, 0],
1391 range => [0, 0, 100, 10], 1737 range => [0, 0, 100, 10],
1392 req_w => 40, 1738 req_w => 20,
1393 req_h => 13, 1739 req_h => 20,
1394 vertical => 0, 1740 vertical => 0,
1395 can_hover => 1, 1741 can_hover => 1,
1396 inner_pad => 5, 1742 inner_pad => 5,
1397 @_ 1743 @_
1398 ); 1744 );
1502 1848
1503package CFClient::UI::TextView; 1849package CFClient::UI::TextView;
1504 1850
1505our @ISA = CFClient::UI::HBox::; 1851our @ISA = CFClient::UI::HBox::;
1506 1852
1507use SDL::OpenGL; 1853use CFClient::OpenGL;
1508 1854
1509sub new { 1855sub new {
1510 my $class = shift; 1856 my $class = shift;
1511 1857
1512 my $self = $class->SUPER::new ( 1858 my $self = $class->SUPER::new (
1513 req_w => $::WIDTH / 6,
1514 req_h => $::HEIGHT / 6,
1515 fontsize => $::FONTSIZE, 1859 fontsize => 1,
1516 @_, 1860 @_,
1517 1861
1518 layout => (new CFClient::Layout), 1862 layout => (new CFClient::Layout),
1519 par => [], 1863 par => [],
1520 height => 0, 1864 height => 0,
1541sub text_height { 1885sub text_height {
1542 my ($self, $text) = @_; 1886 my ($self, $text) = @_;
1543 1887
1544 my $layout = $self->{layout}; 1888 my $layout = $self->{layout};
1545 1889
1546 $layout->set_height ($self->{fontsize}); 1890 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1547 $layout->set_width ($self->{w}); 1891 $layout->set_width ($self->{w});
1548 $layout->set_text ($text); 1892 $layout->set_text ($text);
1549 1893
1550 ($layout->size)[1] 1894 ($layout->size)[1]
1551} 1895}
1555 1899
1556 $self->{need_reflow}++; 1900 $self->{need_reflow}++;
1557 $self->update; 1901 $self->update;
1558} 1902}
1559 1903
1560sub size_request {
1561 my ($self) = @_;
1562
1563 ($self->{req_w}, $self->{req_h})
1564}
1565
1566sub size_allocate { 1904sub size_allocate {
1567 my ($self, $x, $y, $w, $h) = @_; 1905 my ($self, $w, $h) = @_;
1568 1906
1569 $self->SUPER::size_allocate ($x, $y, $w, $h); 1907 $self->SUPER::size_allocate ($w, $h);
1570 1908
1571 $self->{layout}->set_height ($self->{fontsize}); 1909 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1572 $self->{layout}->set_width ($self->{w}); 1910 $self->{layout}->set_width ($self->{children}[0]{w});
1573 1911
1574 $self->reflow; 1912 $self->reflow;
1575} 1913}
1576 1914
1577sub add_paragraph { 1915sub add_paragraph {
1664 2002
1665} 2003}
1666 2004
1667############################################################################# 2005#############################################################################
1668 2006
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; 2007package CFClient::UI::Animator;
1904 2008
1905use SDL::OpenGL; 2009use CFClient::OpenGL;
1906 2010
1907our @ISA = CFClient::UI::Bin::; 2011our @ISA = CFClient::UI::Bin::;
1908 2012
1909sub moveto { 2013sub moveto {
1910 my ($self, $x, $y) = @_; 2014 my ($self, $x, $y) = @_;
1983 2087
1984package CFClient::UI::Root; 2088package CFClient::UI::Root;
1985 2089
1986our @ISA = CFClient::UI::Container::; 2090our @ISA = CFClient::UI::Container::;
1987 2091
1988use SDL::OpenGL; 2092use CFClient::OpenGL;
2093
2094sub check_size {
2095 my ($self) = @_;
2096
2097 $self->configure (0, 0, $::WITH, $::HEIGHT);
2098}
1989 2099
1990sub size_request { 2100sub size_request {
1991 ($::WIDTH, $::HEIGHT) 2101 ($::WIDTH, $::HEIGHT)
1992} 2102}
1993 2103
1994sub size_allocate { 2104sub configure {
1995 my ($self, $x, $y, $w, $h) = @_; 2105 my ($self, $x, $y, $w, $h) = @_;
1996 2106
1997 $self->_size_allocate ($x, $y, $w, $h); 2107 $self->SUPER::configure ($x, $y, $w, $h);
1998 2108
1999 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2109 $_->configure ($_->{x}, $_->{y}, $_->size_request)
2000 for @{$self->{children}}; 2110 for @{$self->{children}};
2001} 2111}
2002 2112
2003sub _topleft { 2113sub _topleft {
2004 my ($self, $x, $y) = @_; 2114 my ($self, $x, $y) = @_;
2007} 2117}
2008 2118
2009sub update { 2119sub update {
2010 my ($self) = @_; 2120 my ($self) = @_;
2011 2121
2012 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2122 $self->check_size;
2013 ::refresh (); 2123 ::refresh ();
2014} 2124}
2015 2125
2016sub add { 2126sub add {
2017 my ($self, $widget) = @_; 2127 my ($self, $child) = @_;
2018 2128
2129 # integerize window positions
2130 $child->{x} = int $child->{x};
2131 $child->{y} = int $child->{y};
2132
2019 $self->SUPER::add ($widget); 2133 $self->SUPER::add ($child);
2020
2021 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2022} 2134}
2023 2135
2024sub on_refresh { 2136sub on_refresh {
2025 my ($self, $id, $cb) = @_; 2137 my ($self, $id, $cb) = @_;
2026 2138

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines