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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines