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.109 by root, Sat Apr 15 01:13:47 2006 UTC vs.
Revision 1.141 by root, Thu Apr 20 07:12:57 2006 UTC

7 7
8use CFClient; 8use CFClient;
9 9
10our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
11 11
12our $TOPLEVEL; 12our $ROOT;
13our $BUTTON_STATE; 13our $BUTTON_STATE;
14 14
15# class methods for events 15# class methods for events
16sub feed_sdl_key_down_event { 16sub feed_sdl_key_down_event {
17 $FOCUS->key_down ($_[0]) if $FOCUS; 17 $FOCUS->key_down ($_[0]) if $FOCUS;
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 = $TOPLEVEL->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->translate ($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 || $TOPLEVEL->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->translate ($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;
52 $grab->update if $grab; 52 $grab->update if $grab;
53 $GRAB->update if $GRAB; 53 $GRAB->update if $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 || $TOPLEVEL->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;
65 65
66 $hover->update if $hover && $hover->{can_hover}; 66 $hover->update if $hover && $hover->{can_hover};
67 $HOVER->update if $HOVER && $HOVER->{can_hover}; 67 $HOVER->update if $HOVER && $HOVER->{can_hover};
68 } 68 }
69 69
70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->coord2local ($x, $y)) if $HOVER;
71}
72
73# convert position array to integers
74sub harmonize {
75 my ($vals) = @_;
76
77 my $rem = 0;
78
79 for (@$vals) {
80 my $i = int $_ + $rem;
81 $rem += $_ - $i;
82 $_ = $i;
83 }
71} 84}
72 85
73############################################################################# 86#############################################################################
74 87
75package CFClient::UI::Base; 88package CFClient::UI::Base;
76 89
77use strict; 90use strict;
78 91
79use SDL::OpenGL; 92use CFClient::OpenGL;
80 93
81sub new { 94sub new {
82 my $class = shift; 95 my $class = shift;
83 96
84 my $self = bless { 97 my $self = bless {
85 x => 0, 98 x => 0,
86 y => 0, 99 y => 0,
87 z => 0, 100 z => 0,
88 w => -1,
89 h => -1,
90 @_ 101 @_
91 }, $class; 102 }, $class;
92 103
93 for (keys %$self) { 104 for (keys %$self) {
94 if (/^connect_(.*)$/) { 105 if (/^connect_(.*)$/) {
97 } 108 }
98 109
99 $self 110 $self
100} 111}
101 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
102sub move { 129sub move {
103 my ($self, $x, $y, $z) = @_; 130 my ($self, $x, $y, $z) = @_;
131
104 $self->{x} = $x; 132 $self->{x} = int $x;
105 $self->{y} = $y; 133 $self->{y} = int $y;
106 $self->{z} = $z if defined $z; 134 $self->{z} = $z if defined $z;
135
136 $self->update;
107} 137}
108 138
109sub needs_redraw { 139sub needs_redraw {
110 0 140 0
111} 141}
113sub size_request { 143sub size_request {
114 require Carp; 144 require Carp;
115 Carp::confess "size_request is abtract"; 145 Carp::confess "size_request is abtract";
116} 146}
117 147
118sub _size_allocate { 148sub configure {
119 my ($self, $x, $y, $w, $h) = @_; 149 my ($self, $x, $y, $w, $h) = @_;
120 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 += ($w - $w2) * 0.5;
158 $y += ($h - $h2) * 0.5;
159
160 ($w, $h) = ($w2, $h2);
161 }
162
163 if ($self->{x} != $x || $self->{y} != $y) {
121 $self->{x} = $x; 164 $self->{x} = $x;
122 $self->{y} = $y; 165 $self->{y} = $y;
166 $self->update;
167 }
123 168
124 return unless $self->{w} != $w || $self->{h} != $h; 169 if ($self->{w} != $w || $self->{h} != $h) {
125
126 $self->{w} = $w; 170 $self->{w} = $w;
127 $self->{h} = $h; 171 $self->{h} = $h;
128 172
173 $self->size_allocate ($w, $h);
174 $self->update;
129 1 175 }
130} 176}
131 177
132sub size_allocate { 178sub size_allocate {
179 # nothing to be done
180}
181
182# return top left coordinates
183sub _topleft {
133 my ($self, $x, $y, $w, $h) = @_; 184 my ($self, $x, $y) = @_;
134 185
135 $self->_size_allocate ($x, $y, $w, $h); 186 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
136} 187}
137 188
138# translate global coordinates to local coordinate system 189# translate global coordinates to local coordinate system
139sub translate { 190sub coord2local {
140 my ($self, $x, $y) = @_; 191 my ($self, $x, $y) = @_;
141 192
142 $self->{parent}->translate ($x - $self->{x}, $y - $self->{y}); 193 my ($X, $Y) = $self->_topleft;
194 ($x - $X, $y - $Y)
195}
196
197# translate local coordinates to global coordinate system
198sub coord2global {
199 my ($self, $x, $y) = @_;
200
201 my ($X, $Y) = $self->_topleft;
202 ($x + $X, $y + $Y)
143} 203}
144 204
145sub focus_in { 205sub focus_in {
146 my ($self) = @_; 206 my ($self) = @_;
147 207
148 return if $FOCUS == $self; 208 return if $FOCUS == $self;
149 return unless $self->{can_focus}; 209 return unless $self->{can_focus};
150 210
151 my $focus = $FOCUS; $FOCUS = $self; 211 my $focus = $FOCUS; $FOCUS = $self;
212
213 $self->emit (focus_in => $focus);
214
152 $focus->update if $focus; 215 $focus->update if $focus;
153 $FOCUS->update; 216 $FOCUS->update;
154} 217}
155 218
156sub focus_out { 219sub focus_out {
157 my ($self) = @_; 220 my ($self) = @_;
158 221
159 return unless $FOCUS == $self; 222 return unless $FOCUS == $self;
160 223
161 my $focus = $FOCUS; undef $FOCUS; 224 my $focus = $FOCUS; undef $FOCUS;
225
226 $self->emit (focus_out => $focus);
227
162 $focus->update if $focus; #? 228 $focus->update if $focus; #?
163} 229}
164 230
165sub mouse_motion { } 231sub mouse_motion { }
166sub button_up { } 232sub button_up { }
190 glPopMatrix; 256 glPopMatrix;
191 257
192 if ($self == $HOVER && $self->{can_hover}) { 258 if ($self == $HOVER && $self->{can_hover}) {
193 my ($x, $y) = @$self{qw(x y)}; 259 my ($x, $y) = @$self{qw(x y)};
194 260
195 glColor 0, 0, 1, 0.2; 261 glColor 1, 0.8, 0.5, 0.2;
196 glEnable GL_BLEND; 262 glEnable GL_BLEND;
197 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 263 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
198 glBegin GL_QUADS; 264 glBegin GL_QUADS;
199 glVertex $x , $y; 265 glVertex $x , $y;
200 glVertex $x + $self->{w}, $y; 266 glVertex $x + $self->{w}, $y;
220 286
221 () 287 ()
222} 288}
223 289
224sub set_parent { 290sub set_parent {
225 my ($self, $par) = @_; 291 my ($self, $parent) = @_;
226 292
227 $self->{parent} = $par;
228 Scalar::Util::weaken $self->{parent}; 293 Scalar::Util::weaken ($self->{parent} = $parent);
229} 294}
230 295
231sub get_parent { 296sub check_size {
232 $_[0]->{parent} 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 }
233} 308}
234 309
235sub update { 310sub update {
236 my ($self) = @_; 311 my ($self) = @_;
237 312
240} 315}
241 316
242sub connect { 317sub connect {
243 my ($self, $signal, $cb) = @_; 318 my ($self, $signal, $cb) = @_;
244 319
245 push @{ $self->{cb}{$signal} }, $cb; 320 push @{ $self->{signal_cb}{$signal} }, $cb;
246} 321}
247 322
248sub emit { 323sub emit {
249 my ($self, $signal, @args) = @_; 324 my ($self, $signal, @args) = @_;
250 325
326 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
251 $_->($self, @args) 327 $cb->($self, @args);
252 for @{$self->{cb}{$signal} || []}; 328 }
253} 329}
254 330
255sub DESTROY { 331sub DESTROY {
256 my ($self) = @_; 332 my ($self) = @_;
257 333
263package CFClient::UI::DrawBG; 339package CFClient::UI::DrawBG;
264 340
265our @ISA = CFClient::UI::Base::; 341our @ISA = CFClient::UI::Base::;
266 342
267use strict; 343use strict;
268use SDL::OpenGL; 344use CFClient::OpenGL;
269 345
270sub new { 346sub new {
271 my $class = shift; 347 my $class = shift;
272 348
273 # range [value, low, high, page] 349 # range [value, low, high, page]
326 402
327 $self 403 $self
328} 404}
329 405
330sub add { 406sub add {
331 my ($self, $chld) = @_; 407 my ($self, $child) = @_;
332 408
333 $chld->set_parent ($self); 409 $child->set_parent ($self);
410
411 use sort 'stable';
334 412
335 $self->{children} = [ 413 $self->{children} = [
336 sort { $a->{z} <=> $b->{z} } 414 sort { $a->{z} <=> $b->{z} }
337 @{$self->{children}}, $chld 415 @{$self->{children}}, $child
338 ]; 416 ];
339 417
340 $self->{w} = $self->{h} = -1; 418 $child->check_size;
341 $self->update;
342} 419}
343 420
344sub remove { 421sub remove {
345 my ($self, $widget) = @_; 422 my ($self, $child) = @_;
346 423
424 delete $child->{parent};
425
347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
348 427
349 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 428 $self->check_size;
350} 429}
351 430
352sub find_widget { 431sub find_widget {
353 my ($self, $x, $y) = @_; 432 my ($self, $x, $y) = @_;
354 433
384 463
385 $class->SUPER::new (children => [$child], %arg) 464 $class->SUPER::new (children => [$child], %arg)
386} 465}
387 466
388sub add { 467sub add {
389 my ($self, $widget) = @_; 468 my ($self, $child) = @_;
390 469
391 $self->{children} = []; 470 $self->{children} = [];
392 471
393 $self->SUPER::add ($widget); 472 $self->SUPER::add ($child);
394} 473}
395 474
396sub remove { 475sub remove {
397 my ($self, $widget) = @_; 476 my ($self, $widget) = @_;
398 477
407sub size_request { 486sub size_request {
408 $_[0]{children}[0]->size_request 487 $_[0]{children}[0]->size_request
409} 488}
410 489
411sub size_allocate { 490sub size_allocate {
412 my ($self, $x, $y, $w, $h) = @_; 491 my ($self, $w, $h) = @_;
413 492
414 $self->_size_allocate ($x, $y, $w, $h) or return;
415
416 $self->{children}[0]->size_allocate (0, 0, $w, $h); 493 $self->{children}[0]->configure (0, 0, $w, $h);
417} 494}
418 495
419############################################################################# 496#############################################################################
420 497
421package CFClient::UI::Window; 498package CFClient::UI::Window;
422 499
423our @ISA = CFClient::UI::Bin::; 500our @ISA = CFClient::UI::Bin::;
424 501
425use SDL::OpenGL; 502use CFClient::OpenGL;
426 503
427sub new { 504sub new {
428 my ($class, %arg) = @_; 505 my ($class, %arg) = @_;
429 506
430 my $self = $class->SUPER::new (%arg); 507 my $self = $class->SUPER::new (%arg);
447 $self->child->draw; 524 $self->child->draw;
448 }; 525 };
449} 526}
450 527
451sub size_allocate { 528sub size_allocate {
452 my ($self, $x, $y, $w, $h) = @_; 529 my ($self, $w, $h) = @_;
453 530
454 $self->_size_allocate ($x, $y, $w, $h) or return;
455
456 $self->child->size_allocate (0, 0, $w, $h); 531 $self->child->configure (0, 0, $w, $h);
457 532
458 $self->render_chld; 533 $self->render_chld;
459} 534}
460 535
461sub _draw { 536sub _draw {
477 glDisable GL_TEXTURE_2D; 552 glDisable GL_TEXTURE_2D;
478} 553}
479 554
480############################################################################# 555#############################################################################
481 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
482package CFClient::UI::Frame; 581package CFClient::UI::Frame;
483 582
484our @ISA = CFClient::UI::Bin::; 583our @ISA = CFClient::UI::Bin::;
485 584
486use SDL::OpenGL; 585use CFClient::OpenGL;
487 586
488sub size_request { 587sub size_request {
489 my ($self) = @_; 588 my ($self) = @_;
490 my $chld = $self->child 589 my $chld = $self->child
491 or return (0, 0); 590 or return (0, 0);
496} 595}
497 596
498sub size_allocate { 597sub size_allocate {
499 my ($self, $x, $y, $w, $h) = @_; 598 my ($self, $x, $y, $w, $h) = @_;
500 599
501 $self->_size_allocate ($x, $y, $w, $h) or return;
502
503 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 600 $self->child->configure (2, 2, $w - 4, $h - 4);
504} 601}
505 602
506sub _draw { 603sub _draw {
507 my ($self) = @_; 604 my ($self) = @_;
508 605
525 622
526package CFClient::UI::FancyFrame; 623package CFClient::UI::FancyFrame;
527 624
528our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
529 626
530use SDL::OpenGL; 627use CFClient::OpenGL;
531 628
532my @tex = 629my @tex =
533 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
534 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 631 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
535 632
536sub new { 633sub new {
537 my $class = shift; 634 my $class = shift;
538 635
539 # TODO: user_x, user_y, overwrite moveto? 636 # TODO: user_x, user_y, overwrite moveto?
540 637
541 $class->SUPER::new ( 638 my $self = $class->SUPER::new (
542 bg => [1, 1, 1, 1], 639 bg => [1, 1, 1, 1],
543 border_bg => [1, 1, 1, 1], 640 border_bg => [1, 1, 1, 1],
544 border => $::FONTSIZE * 0.8, 641 border => 0.8,
545 @_ 642 @_
643 );
644
645 $self->{title} &&= new CFClient::UI::Label
646 align => 0,
647 valign => 1,
648 text => $self->{title},
649 fontsize => 1;
650
651 $self
652}
653
654sub border {
655 int $_[0]{border} * $::FONTSIZE
656}
657
658sub size_request {
659 my ($self) = @_;
660
661 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
662
663 my ($w, $h) = $self->SUPER::size_request;
664
665 (
666 $w + $self->border * 2,
667 $h + $self->border * 2,
546 ) 668 )
547} 669}
548 670
549sub size_request {
550 my ($self) = @_;
551
552 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
553
554 my ($w, $h) = $self->SUPER::size_request;
555
556 (
557 $w + $self->{border} * 2,
558 $h + $self->{border} * 2,
559 )
560}
561
562sub size_allocate { 671sub size_allocate {
563 my ($self, $x, $y, $w, $h) = @_; 672 my ($self, $w, $h) = @_;
564 673
565 $self->_size_allocate ($x, $y, $w, $h) or return;
566
567 $h -= List::Util::max 0, $self->{border} * 2; 674 $h -= List::Util::max 0, $self->border * 2;
568 $w -= List::Util::max 0, $self->{border} * 2; 675 $w -= List::Util::max 0, $self->border * 2;
569 676
677 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
678 if $self->{title};
679
570 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 680 $self->child->configure ($self->border, $self->border, $w, $h);
571} 681}
572 682
573sub button_down { 683sub button_down {
574 my ($self, $ev, $x, $y) = @_; 684 my ($self, $ev, $x, $y) = @_;
575 685
686 my $border = $self->border;
687
576 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 688 if ($x < $self->{w} && $x >= $self->{w} - $border
577 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 689 && $y < $self->{h} && $y >= $self->{h} - $border) {
578 690
579 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 691 my ($ox, $oy) = ($ev->{x}, $ev->{y});
580 my ($bw, $bh) = ($self->{w}, $self->{h}); 692 my ($bw, $bh) = ($self->{w}, $self->{h});
581 693
582 $self->{motion} = sub { 694 $self->{motion} = sub {
583 my ($ev, $x, $y) = @_; 695 my ($ev, $x, $y) = @_;
584 696
585 ($x, $y) = ($ev->motion_x, $ev->motion_y); 697 ($x, $y) = ($ev->{x}, $ev->{y});
586 698
587 $self->{user_w} = $bw + $x - $ox; 699 $self->{user_w} = $bw + $x - $ox;
588 $self->{user_h} = $bh + $y - $oy; 700 $self->{user_h} = $bh + $y - $oy;
589 $self->update; 701 $self->check_size;
590 }; 702 };
591 703
592 } elsif ($x >= 0 && $x < $self->{w} 704 } elsif ($x >= 0 && $x < $self->{w}
593 && $y >= 0 && $y < $self->{border}) { 705 && $y >= 0 && $y < $border) {
594 706
595 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 707 my ($ox, $oy) = ($ev->{x}, $ev->{y});
596 my ($bx, $by) = ($self->{x}, $self->{y}); 708 my ($bx, $by) = ($self->{x}, $self->{y});
597 709
598 $self->{motion} = sub { 710 $self->{motion} = sub {
599 my ($ev, $x, $y) = @_; 711 my ($ev, $x, $y) = @_;
600 712
601 ($x, $y) = ($ev->motion_x, $ev->motion_y); 713 ($x, $y) = ($ev->{x}, $ev->{y});
602 714
603 $self->move ($bx + $x - $ox, $by + $y - $oy); 715 $self->move ($bx + $x - $ox, $by + $y - $oy);
604 $self->update; 716 $self->update;
605 }; 717 };
606 } 718 }
627 glEnable GL_BLEND; 739 glEnable GL_BLEND;
628 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 740 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
629 glEnable GL_TEXTURE_2D; 741 glEnable GL_TEXTURE_2D;
630 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 742 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
631 743
744 my $border = $self->border;
745
632 glColor @{ $self->{border_bg} }; 746 glColor @{ $self->{border_bg} };
633 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 747 $tex[1]->draw_quad (0, 0, $w, $border);
634 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 748 $tex[3]->draw_quad (0, $border, $border, $ch);
635 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 749 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
636 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 750 $tex[4]->draw_quad (0, $h - $border, $w, $border);
637 751
638 my $bg = $tex[0]; 752 my $bg = $tex[0];
639 753
640 # TODO: repeat texture not scale 754 # TODO: repeat texture not scale
641 my $rep_x = $cw / $bg->{w}; 755 my $rep_x = $cw / $bg->{w};
644 glColor @{ $self->{bg} }; 758 glColor @{ $self->{bg} };
645 759
646 $bg->{s} = $rep_x; 760 $bg->{s} = $rep_x;
647 $bg->{t} = $rep_y; 761 $bg->{t} = $rep_y;
648 $bg->{wrap_mode} = 1; 762 $bg->{wrap_mode} = 1;
649 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 763 $bg->draw_quad ($border, $border, $cw, $ch);
650 764
651 glDisable GL_TEXTURE_2D; 765 glDisable GL_TEXTURE_2D;
652 glDisable GL_BLEND; 766 glDisable GL_BLEND;
653 767
768 $self->{title}->draw if $self->{title};
654 $self->child->draw; 769 $self->child->draw;
655} 770}
656 771
657############################################################################# 772#############################################################################
658 773
660 775
661our @ISA = CFClient::UI::Base::; 776our @ISA = CFClient::UI::Base::;
662 777
663use List::Util qw(max sum); 778use List::Util qw(max sum);
664 779
665use SDL::OpenGL; 780use CFClient::OpenGL;
666 781
667sub new { 782sub new {
668 my $class = shift; 783 my $class = shift;
669 784
670 $class->SUPER::new ( 785 $class->SUPER::new (
672 @_ 787 @_
673 ) 788 )
674} 789}
675 790
676sub add { 791sub add {
677 my ($self, $x, $y, $chld) = @_; 792 my ($self, $x, $y, $child) = @_;
678 793
794 $child->set_parent ($self);
679 $self->{children}[$y][$x] = $chld; 795 $self->{children}[$y][$x] = $child;
680 $chld->set_parent ($self);
681 796
682 $self->{w} = $self->{h} = -1; 797 $child->check_size;
798}
799
800# TODO: move to container class maybe? send childs a signal on removal?
801sub clear {
802 my ($self) = @_;
803
804 delete $self->{children};
683 $self->update; 805 $self->update;
684} 806}
685 807
686sub get_wh { 808sub get_wh {
687 my ($self) = @_; 809 my ($self) = @_;
715 (sum @$hs), 837 (sum @$hs),
716 ) 838 )
717} 839}
718 840
719sub size_allocate { 841sub size_allocate {
720 my ($self, $x, $y, $w, $h) = @_; 842 my ($self, $w, $h) = @_;
721
722 $self->_size_allocate ($x, $y, $w, $h) or return;
723 843
724 my ($ws, $hs) = $self->get_wh; 844 my ($ws, $hs) = $self->get_wh;
725 845
726 my $req_w = sum @$ws; 846 my $req_w = sum @$ws;
727 my $req_h = sum @$hs; 847 my $req_h = sum @$hs;
733 853
734 # linearly scale sizes 854 # linearly scale sizes
735 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; 855 $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws;
736 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; 856 $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs;
737 857
738 $_ = int $_ for @$ws; #TODO: avoid rounding problems 858 CFClient::UI::harmonize $ws;
739 $_ = int $_ for @$hs; #TODO: avoid rounding problems 859 CFClient::UI::harmonize $hs;
740 860
741 my $y; 861 my $y;
742 862
743 for my $r (0 .. $#{$self->{children}}) { 863 for my $r (0 .. $#{$self->{children}}) {
744 my $row = $self->{children}[$r] 864 my $row = $self->{children}[$r]
749 869
750 for my $c (0 .. $#$row) { 870 for my $c (0 .. $#$row) {
751 my $col_w = $ws->[$c]; 871 my $col_w = $ws->[$c];
752 872
753 if (my $widget = $row->[$c]) { 873 if (my $widget = $row->[$c]) {
754 $widget->size_allocate ($x, $y, $col_w, $row_h); 874 $widget->configure ($x, $y, $col_w, $row_h);
755 } 875 }
756 876
757 $x += $col_w; 877 $x += $col_w;
758 } 878 }
759 879
804 (List::Util::max map $_->[1], @alloc), 924 (List::Util::max map $_->[1], @alloc),
805 ) 925 )
806} 926}
807 927
808sub size_allocate { 928sub size_allocate {
809 my ($self, $x, $y, $w, $h) = @_; 929 my ($self, $w, $h) = @_;
810
811 $self->_size_allocate ($x, $y, $w, $h);
812 930
813 ($h, $w) = ($w, $h); 931 ($h, $w) = ($w, $h);
814 932
815 my $children = $self->{children}; 933 my $children = $self->{children};
816 934
832 $alloc_h += ($h - $req_h) * $child->{expand} / $exp; 950 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
833 $h[$_] = $alloc_h; 951 $h[$_] = $alloc_h;
834 } 952 }
835 } 953 }
836 954
955 CFClient::UI::harmonize \@h;
956
837 my $y = 0; 957 my $y = 0;
838 for (0 .. $#$children) { 958 for (0 .. $#$children) {
839 my $child = $children->[$_]; 959 my $child = $children->[$_];
840 my $h = int $h[$_]; 960 my $h = $h[$_];
841 $child->size_allocate ($y, 0, $h, $w); 961 $child->configure ($y, 0, $h, $w);
842 962
843 $y += $h; 963 $y += $h;
844 } 964 }
965
966 1
845} 967}
846 968
847############################################################################# 969#############################################################################
848 970
849package CFClient::UI::VBox; 971package CFClient::UI::VBox;
862 (List::Util::sum map $_->[1], @alloc), 984 (List::Util::sum map $_->[1], @alloc),
863 ) 985 )
864} 986}
865 987
866sub size_allocate { 988sub size_allocate {
867 my ($self, $x, $y, $w, $h) = @_; 989 my ($self, $w, $h) = @_;
868
869 $self->_size_allocate ($x, $y, $w, $h);
870 990
871 my $children = $self->{children}; 991 my $children = $self->{children};
872 992
873 my @h = map +($_->size_request)[1], @$children; 993 my @h = map +($_->size_request)[1], @$children;
874 994
886 1006
887 $h[$_] += ($h - $req_h) * $child->{expand} / $exp; 1007 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
888 } 1008 }
889 } 1009 }
890 1010
1011 CFClient::UI::harmonize \@h;
1012
891 my $y = 0; 1013 my $y = 0;
892 for (0 .. $#$children) { 1014 for (0 .. $#$children) {
893 my $child = $children->[$_]; 1015 my $child = $children->[$_];
894 my $h = int $h[$_]; 1016 my $h = $h[$_];
895 $child->size_allocate (0, $y, $w, $h); 1017 $child->configure (0, $y, $w, $h);
896 1018
897 $y += $h; 1019 $y += $h;
898 } 1020 }
1021
1022 1
899} 1023}
900 1024
901############################################################################# 1025#############################################################################
902 1026
903package CFClient::UI::Label; 1027package CFClient::UI::Label;
904 1028
905our @ISA = CFClient::UI::Base::; 1029our @ISA = CFClient::UI::Base::;
906 1030
907use SDL::OpenGL; 1031use CFClient::OpenGL;
908 1032
909sub new { 1033sub new {
910 my ($class, %arg) = @_; 1034 my ($class, %arg) = @_;
911 1035
912 my $self = $class->SUPER::new ( 1036 my $self = $class->SUPER::new (
913 fg => [1, 1, 1], 1037 fg => [1, 1, 1],
914 fontsize => $::FONTSIZE, 1038 fontsize => 1,
915 text => "", 1039 text => "",
916 align => -1, 1040 align => -1,
1041 valign => -1,
917 padding => 2, 1042 padding => 2,
918 layout => new CFClient::Layout, 1043 layout => new CFClient::Layout,
919 %arg 1044 %arg
920 ); 1045 );
921 1046
922 $self->set_text ($self->{text}); 1047 if (exists $self->{template}) {
1048 my $layout = new CFClient::Layout;
1049 $layout->set_text (delete $self->{template});
1050 $self->{template} = $layout;
1051 }
1052
1053 $self->set_text (delete $self->{text}) if exists $self->{text};
1054 $self->set_markup (delete $self->{markup}) if exists $self->{markup};
923 1055
924 $self 1056 $self
925} 1057}
926 1058
927sub escape_text { 1059sub escape_text {
935} 1067}
936 1068
937sub set_text { 1069sub set_text {
938 my ($self, $text) = @_; 1070 my ($self, $text) = @_;
939 1071
940 $self->{text} = $text;
941 $self->{layout}->set_markup ($text); 1072 $self->{layout}->set_text ($text);
942 1073
943 delete $self->{texture}; 1074 delete $self->{texture};
944# $self->{w} = $self->{h} = -1;
945 $self->update; 1075 $self->update;
946} 1076}
947 1077
948sub get_text { 1078sub set_markup {
949 my ($self, $text) = @_; 1079 my ($self, $markup) = @_;
950 1080
951 $self->{text} 1081 $self->{layout}->set_markup ($markup);
1082
1083 delete $self->{texture};
1084 $self->update;
952} 1085}
953 1086
954sub size_request { 1087sub size_request {
955 my ($self) = @_; 1088 my ($self) = @_;
956 1089
957 $self->{layout}->set_width; 1090 $self->{layout}->set_width;
958 $self->{layout}->set_height ($self->{fontsize}); 1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1092
959 my ($w, $h) = $self->{layout}->size; 1093 my ($w, $h) = $self->{layout}->size;
1094
1095 if (exists $self->{template}) {
1096 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1097
1098 my ($w2, $h2) = $self->{template}->size;
1099
1100 $w = List::Util::max $w, $w2;
1101 $h = List::Util::max $h, $h2;
1102 }
960 1103
961 ( 1104 (
962 $w + $self->{padding} * 2, 1105 $w + $self->{padding} * 2,
963 $h + $self->{padding} * 2, 1106 $h + $self->{padding} * 2,
964 ) 1107 )
965} 1108}
966 1109
967sub size_allocate { 1110sub size_allocate {
968 my ($self, $x, $y, $w, $h) = @_; 1111 my ($self, $w, $h) = @_;
969
970 $self->_size_allocate ($x, $y, $w, $h) or return;
971 1112
972 delete $self->{texture}; 1113 delete $self->{texture};
973}
974
975sub update {
976 my ($self) = @_;
977
978 delete $self->{texture};
979 $self->SUPER::update;
980} 1114}
981 1115
982sub _draw { 1116sub _draw {
983 my ($self) = @_; 1117 my ($self) = @_;
984 1118
985 my $tex = $self->{texture} ||= do { 1119 my $tex = $self->{texture} ||= do {
986 $self->{layout}->set_width ($self->{w}); 1120 $self->{layout}->set_width ($self->{w});
987 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize}); 1121 $self->{layout}->set_height (List::Util::min $self->{h}, $self->{fontsize} * $::FONTSIZE);
988 new_from_layout CFClient::Texture $self->{layout} 1122 new_from_layout CFClient::Texture $self->{layout}
989 }; 1123 };
990 1124
991 glEnable GL_BLEND; 1125 glEnable GL_BLEND;
992 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1126 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
993 glEnable GL_TEXTURE_2D; 1127 glEnable GL_TEXTURE_2D;
994 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1128 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
995 1129
996 glColor @{$self->{fg}}; 1130 glColor @{$self->{fg}};
997 1131
998 my $x = 1132 $self->{ox} = int (
999 $self->{align} < 0 ? $self->{padding} 1133 $self->{align} < 0 ? $self->{padding}
1000 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1134 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1001 : ($self->{w} - $tex->{w}) * 0.5; 1135 : ($self->{w} - $tex->{w}) * 0.5
1136 );
1002 1137
1003 $tex->draw_quad (int $x, int +($self->{h} - $tex->{h}) * 0.5); 1138 $self->{oy} = int (
1139 $self->{valign} < 0 ? $self->{padding}
1140 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding}
1141 : ($self->{h} - $tex->{h}) * 0.5
1142 );
1143
1144 $tex->draw_quad ($self->{ox}, $self->{oy});
1004 1145
1005 glDisable GL_TEXTURE_2D; 1146 glDisable GL_TEXTURE_2D;
1006 glDisable GL_BLEND; 1147 glDisable GL_BLEND;
1007} 1148}
1008 1149
1009############################################################################# 1150#############################################################################
1010 1151
1011package CFClient::UI::Entry; 1152package CFClient::UI::EntryBase;
1012 1153
1013our @ISA = CFClient::UI::Label::; 1154our @ISA = CFClient::UI::Label::;
1014 1155
1015use SDL; 1156use CFClient::OpenGL;
1016use SDL::OpenGL;
1017 1157
1018sub new { 1158sub new {
1019 my $class = shift; 1159 my $class = shift;
1020 1160
1021 $class->SUPER::new ( 1161 $class->SUPER::new (
1023 bg => [0, 0, 0, 0.2], 1163 bg => [0, 0, 0, 0.2],
1024 active_bg => [1, 1, 1, 0.5], 1164 active_bg => [1, 1, 1, 0.5],
1025 active_fg => [0, 0, 0], 1165 active_fg => [0, 0, 0],
1026 can_hover => 1, 1166 can_hover => 1,
1027 can_focus => 1, 1167 can_focus => 1,
1168 valign => 0,
1028 @_ 1169 @_
1029 ) 1170 )
1030} 1171}
1031 1172
1032sub _set_text { 1173sub _set_text {
1033 my ($self, $text) = @_; 1174 my ($self, $text) = @_;
1034 1175
1035 my $old_text = $self->{text}; 1176 delete $self->{cur_h};
1177
1178 return if $self->{text} eq $text;
1179
1180 delete $self->{texture};
1036 1181
1037 $self->{last_activity} = $::NOW; 1182 $self->{last_activity} = $::NOW;
1038
1039 $self->{text} = $text; 1183 $self->{text} = $text;
1040 $self->{layout}->set_width ($self->{w});
1041 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1042 1184
1043 $text =~ s/./*/g if $self->{hidden}; 1185 $text =~ s/./*/g if $self->{hidden};
1186 $self->{layout}->set_text ("$text ");
1044 1187
1045 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1188 $self->emit (changed => $self->{text});
1189}
1046 1190
1047 $text = substr $text, 0, $self->{cursor}; 1191sub get_text {
1048 utf8::encode $text; 1192 $_[0]{text}
1049
1050 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1051
1052 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1053 if $old_text ne $self->{text};
1054} 1193}
1055 1194
1056sub size_request { 1195sub size_request {
1057 my ($self) = @_; 1196 my ($self) = @_;
1058 1197
1060 1199
1061 ($w + 1, $h) # add 1 for cursor 1200 ($w + 1, $h) # add 1 for cursor
1062} 1201}
1063 1202
1064sub size_allocate { 1203sub size_allocate {
1065 my ($self, $x, $y, $w, $h) = @_; 1204 my ($self, $w, $h) = @_;
1066
1067 $self->SUPER::size_allocate ($x, $y, $w, $h);
1068 1205
1069 $self->_set_text ($self->{text}); 1206 $self->_set_text ($self->{text});
1070} 1207}
1071 1208
1072sub set_text { 1209sub set_text {
1078} 1215}
1079 1216
1080sub key_down { 1217sub key_down {
1081 my ($self, $ev) = @_; 1218 my ($self, $ev) = @_;
1082 1219
1083 my $mod = $ev->key_mod; 1220 my $mod = $ev->{mod};
1084 my $sym = $ev->key_sym; 1221 my $sym = $ev->{sym};
1085
1086 my $uni = $ev->key_unicode; 1222 my $uni = $ev->{unicode};
1087 1223
1088 my $text = $self->get_text; 1224 my $text = $self->get_text;
1089 1225
1090 if ($sym == SDLK_BACKSPACE) { 1226 if ($sym == 8) {
1091 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1227 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1092 } elsif ($sym == SDLK_DELETE) { 1228 } elsif ($sym == 127) {
1093 substr $text, $self->{cursor}, 1, ""; 1229 substr $text, $self->{cursor}, 1, "";
1094 } elsif ($sym == SDLK_LEFT) { 1230 } elsif ($sym == CFClient::SDLK_LEFT) {
1095 --$self->{cursor} if $self->{cursor}; 1231 --$self->{cursor} if $self->{cursor};
1096 } elsif ($sym == SDLK_RIGHT) { 1232 } elsif ($sym == CFClient::SDLK_RIGHT) {
1097 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1233 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1098 } elsif ($sym == SDLK_HOME) { 1234 } elsif ($sym == CFClient::SDLK_HOME) {
1099 $self->{cursor} = 0; 1235 $self->{cursor} = 0;
1100 } elsif ($sym == SDLK_END) { 1236 } elsif ($sym == CFClient::SDLK_END) {
1101 $self->{cursor} = length $text; 1237 $self->{cursor} = length $text;
1102 } elsif ($sym == SDLK_ESCAPE) { 1238 } elsif ($sym == 27) {
1103 $self->emit ('escape'); 1239 $self->emit ('escape');
1104 } elsif ($uni) { 1240 } elsif ($uni) {
1105 substr $text, $self->{cursor}++, 0, chr $uni; 1241 substr $text, $self->{cursor}++, 0, chr $uni;
1106 } 1242 }
1107 1243
1162 1298
1163 $self->SUPER::_draw; 1299 $self->SUPER::_draw;
1164 1300
1165 #TODO: force update every cursor change :( 1301 #TODO: force update every cursor change :(
1166 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) { 1302 if ($FOCUS == $self && (($::NOW - $self->{last_activity}) & 1023) < 600) {
1303
1304 unless (exists $self->{cur_h}) {
1305 my $text = substr $self->{text}, 0, $self->{cursor};
1306 utf8::encode $text;
1307
1308 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text)
1309 }
1310
1167 glColor @{$self->{fg}}; 1311 glColor @{$self->{fg}};
1168 glBegin GL_LINES; 1312 glBegin GL_LINES;
1169 glVertex $self->{cur_x}, $self->{cur_y};
1170 glVertex $self->{cur_x}, $self->{cur_y} + $self->{cur_h}; 1313 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy};
1314 glVertex $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h};
1171 glEnd; 1315 glEnd;
1172 } 1316 }
1173} 1317}
1174 1318
1175package CFClient::UI::LineEntry; 1319package CFClient::UI::Entry;
1176 1320
1177our @ISA = CFClient::UI::Entry::; 1321our @ISA = CFClient::UI::EntryBase::;
1178 1322
1179use SDL; 1323use CFClient::OpenGL;
1180use SDL::OpenGL;
1181 1324
1182sub key_down { 1325sub key_down {
1183 my ($self, $ev) = @_; 1326 my ($self, $ev) = @_;
1184 1327
1185 my $sym = $ev->key_sym; 1328 my $sym = $ev->{sym};
1186 1329
1187 if ($sym == SDLK_RETURN) { 1330 if ($sym == 13) {
1188 $self->emit (activate => $self->get_text); 1331 $self->emit (activate => $self->get_text);
1189 $self->update; 1332 $self->update;
1190 1333
1191 } else { 1334 } else {
1192 $self->SUPER::key_down ($ev); 1335 $self->SUPER::key_down ($ev);
1198 1341
1199package CFClient::UI::Button; 1342package CFClient::UI::Button;
1200 1343
1201our @ISA = CFClient::UI::Label::; 1344our @ISA = CFClient::UI::Label::;
1202 1345
1203use SDL; 1346use CFClient::OpenGL;
1204use SDL::OpenGL;
1205 1347
1206my @tex = 1348my @tex =
1207 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1208 qw(b1_button_active.png); 1350 qw(b1_button_active.png);
1209 1351
1212 1354
1213 $class->SUPER::new ( 1355 $class->SUPER::new (
1214 padding => 4, 1356 padding => 4,
1215 fg => [1, 1, 1], 1357 fg => [1, 1, 1],
1216 bg => [1, 1, 1, 0.2], 1358 bg => [1, 1, 1, 0.2],
1217 active_fg => [1, 1, 0], 1359 active_fg => [0, 0, 1],
1218 can_hover => 1, 1360 can_hover => 1,
1361 align => 0,
1362 valign => 0,
1219 @_ 1363 @_
1220 ) 1364 )
1221} 1365}
1222 1366
1223sub button_up { 1367sub button_up {
1231 1375
1232sub _draw { 1376sub _draw {
1233 my ($self) = @_; 1377 my ($self) = @_;
1234 1378
1235 local $self->{fg} = $self->{fg}; 1379 local $self->{fg} = $self->{fg};
1236 my $tex = $tex[0];
1237
1238 glEnable GL_BLEND;
1239 glEnable GL_TEXTURE_2D;
1240 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1241 1380
1242 if ($GRAB == $self) { 1381 if ($GRAB == $self) {
1243 $self->{fg} = $self->{active_fg}; 1382 $self->{fg} = $self->{active_fg};
1244 } 1383 }
1245 1384
1246 glBindTexture GL_TEXTURE_2D, $tex->{name}; 1385 glEnable GL_BLEND;
1386 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1387 glEnable GL_TEXTURE_2D;
1247 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1388 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1389 glColor 0, 0, 0, 1;
1248 1390
1249 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1391 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1250 1392
1251 glDisable GL_TEXTURE_2D; 1393 glDisable GL_TEXTURE_2D;
1252 glDisable GL_BLEND; 1394 glDisable GL_BLEND;
1253 1395
1254 $self->SUPER::_draw; 1396 $self->SUPER::_draw;
1262 1404
1263my @tex = 1405my @tex =
1264 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1265 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1407 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1266 1408
1267use SDL; 1409use CFClient::OpenGL;
1268use SDL::OpenGL;
1269 1410
1270sub new { 1411sub new {
1271 my $class = shift; 1412 my $class = shift;
1272 1413
1273 $class->SUPER::new ( 1414 $class->SUPER::new (
1284 my ($self) = @_; 1425 my ($self) = @_;
1285 1426
1286 ($self->{padding} * 2 + 6) x 2 1427 ($self->{padding} * 2 + 6) x 2
1287} 1428}
1288 1429
1289sub size_allocate {
1290 my ($self, $x, $y, $w, $h) = @_;
1291
1292 $self->_size_allocate ($x, $y, $w, $h);
1293}
1294
1295sub button_down { 1430sub button_down {
1296 my ($self, $ev, $x, $y) = @_; 1431 my ($self, $ev, $x, $y) = @_;
1297 1432
1298 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1433 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1299 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1434 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1325 glDisable GL_BLEND; 1460 glDisable GL_BLEND;
1326} 1461}
1327 1462
1328############################################################################# 1463#############################################################################
1329 1464
1465package CFClient::UI::VGauge;
1466
1467our @ISA = CFClient::UI::Base::;
1468
1469use CFClient::OpenGL;
1470
1471my %tex = (
1472 food => [
1473 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1474 qw/g1_food_gauge_empty.png g1_food_gauge_full.png/
1475 ],
1476 grace => [
1477 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1478 qw/g1_grace_gauge_empty.png g1_grace_gauge_full.png/
1479 ],
1480 hp => [
1481 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1482 qw/g1_hp_gauge_empty.png g1_hp_gauge_full.png/
1483 ],
1484 mana => [
1485 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1486 qw/g1_mana_gauge_empty.png g1_mana_gauge_full.png/
1487 ],
1488);
1489
1490# eg. VGauge->new (gauge => 'food'), default gauge: food
1491sub new {
1492 my $class = shift;
1493
1494 my $self = $class->SUPER::new (
1495 type => 'food',
1496 @_
1497 );
1498
1499 $self->{aspect} = $tex{$self->{type}}[0]{w} / $tex{$self->{type}}[0]{h};
1500
1501 $self
1502}
1503
1504sub size_request {
1505 my ($self) = @_;
1506
1507 my $tex = $tex{$self->{type}}[0];
1508
1509 @$tex{qw(w h)}
1510}
1511
1512sub set_max {
1513 my ($self, $max) = @_;
1514
1515 $self->{max_val} = $max;
1516}
1517
1518sub set_value {
1519 my ($self, $val, $max) = @_;
1520
1521 $self->set_max ($max)
1522 if defined $max;
1523
1524 $max = $self->{max_val};
1525 $self->{val} = $val;
1526
1527 $self->update;
1528}
1529
1530sub _draw {
1531 my ($self) = @_;
1532
1533 my $tex = $tex{$self->{type}};
1534
1535 my ($w, $h) = ($self->{w}, $self->{h});
1536
1537 my $ycut = $self->{val} / ($self->{max_val} || 1);
1538 $ycut = 1 if $self->{val} > $self->{max_val};
1539
1540 my $t1 = $tex->[0];
1541 my $t2 = $tex->[1];
1542
1543 glEnable GL_BLEND;
1544 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1545 glEnable GL_TEXTURE_2D;
1546 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1547
1548 my $h1 = $self->{h} - $ycut * $self->{h};
1549 my $h2 = $ycut * $self->{h};
1550
1551 glBindTexture GL_TEXTURE_2D, $t1->{name};
1552 glBegin GL_QUADS;
1553 glTexCoord 0 , 0; glVertex 0 , 0;
1554 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1555 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1556 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1557 glEnd;
1558
1559 glBindTexture GL_TEXTURE_2D, $t2->{name};
1560 glBegin GL_QUADS;
1561 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1562 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1563 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1564 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1565 glEnd;
1566
1567 glDisable GL_BLEND;
1568 glDisable GL_TEXTURE_2D;
1569}
1570
1571#############################################################################
1572
1573package CFClient::UI::Gauge;
1574
1575our @ISA = CFClient::UI::VBox::;
1576
1577sub new {
1578 my ($class, %arg) = shift;
1579
1580 my $self = $class->SUPER::new (
1581 @_,
1582 );
1583
1584 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1585 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1586 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1587
1588 $self
1589}
1590
1591sub set_value {
1592 my ($self, $val, $max) = @_;
1593
1594 $self->set_max ($max)
1595 if defined $max;
1596
1597 $self->{gauge}->set_value ($val, $max);
1598 $self->{value}->set_text ($val);
1599}
1600
1601sub set_max {
1602 my ($self, $max) = @_;
1603
1604 $self->{gauge}->set_max ($max);
1605 $self->{max}->set_text ($max);
1606}
1607
1608#############################################################################
1609
1330package CFClient::UI::Slider; 1610package CFClient::UI::Slider;
1331 1611
1332use strict; 1612use strict;
1333 1613
1334use SDL::OpenGL; 1614use CFClient::OpenGL;
1335 1615
1336our @ISA = CFClient::UI::DrawBG::; 1616our @ISA = CFClient::UI::DrawBG::;
1337 1617
1338my @tex = 1618my @tex =
1339 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1619 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1349 # TODO: calculations are off 1629 # TODO: calculations are off
1350 my $self = $class->SUPER::new ( 1630 my $self = $class->SUPER::new (
1351 fg => [1, 1, 1], 1631 fg => [1, 1, 1],
1352 active_fg => [0, 0, 0], 1632 active_fg => [0, 0, 0],
1353 range => [0, 0, 100, 10], 1633 range => [0, 0, 100, 10],
1354 req_w => 40, 1634 req_w => 20,
1355 req_h => 13, 1635 req_h => 20,
1356 vertical => 0, 1636 vertical => 0,
1357 can_hover => 1, 1637 can_hover => 1,
1358 inner_pad => 5, 1638 inner_pad => 5,
1359 @_ 1639 @_
1360 ); 1640 );
1464 1744
1465package CFClient::UI::TextView; 1745package CFClient::UI::TextView;
1466 1746
1467our @ISA = CFClient::UI::HBox::; 1747our @ISA = CFClient::UI::HBox::;
1468 1748
1469use SDL::OpenGL; 1749use CFClient::OpenGL;
1470 1750
1471sub new { 1751sub new {
1472 my $class = shift; 1752 my $class = shift;
1473 1753
1474 my $self = $class->SUPER::new ( 1754 my $self = $class->SUPER::new (
1475 req_w => $::WIDTH / 6,
1476 req_h => $::HEIGHT / 6,
1477 fontsize => $::FONTSIZE, 1755 fontsize => 1,
1478 @_, 1756 @_,
1479 1757
1480 layout => (new CFClient::Layout), 1758 layout => (new CFClient::Layout),
1481 par => [], 1759 par => [],
1482 height => 0, 1760 height => 0,
1503sub text_height { 1781sub text_height {
1504 my ($self, $text) = @_; 1782 my ($self, $text) = @_;
1505 1783
1506 my $layout = $self->{layout}; 1784 my $layout = $self->{layout};
1507 1785
1508 $layout->set_height ($self->{fontsize}); 1786 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1509 $layout->set_width ($self->{w}); 1787 $layout->set_width ($self->{w});
1510 $layout->set_text ($text); 1788 $layout->set_text ($text);
1511 1789
1512 ($layout->size)[1] 1790 ($layout->size)[1]
1513} 1791}
1517 1795
1518 $self->{need_reflow}++; 1796 $self->{need_reflow}++;
1519 $self->update; 1797 $self->update;
1520} 1798}
1521 1799
1522sub size_request {
1523 my ($self) = @_;
1524
1525 ($self->{req_w}, $self->{req_h})
1526}
1527
1528sub size_allocate { 1800sub size_allocate {
1529 my ($self, $x, $y, $w, $h) = @_; 1801 my ($self, $w, $h) = @_;
1530 1802
1531 $self->SUPER::size_allocate ($x, $y, $w, $h); 1803 $self->SUPER::size_allocate ($w, $h);
1532 1804
1533 $self->{layout}->set_height ($self->{fontsize}); 1805 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1534 $self->{layout}->set_width ($self->{w}); 1806 $self->{layout}->set_width ($self->{children}[0]{w});
1535 1807
1536 $self->reflow; 1808 $self->reflow;
1537} 1809}
1538 1810
1539sub add_paragraph { 1811sub add_paragraph {
1558 1830
1559 return unless $self->{h} > 0; 1831 return unless $self->{h} > 0;
1560 1832
1561 delete $self->{texture}; 1833 delete $self->{texture};
1562 1834
1563 $TOPLEVEL->on_refresh ($self, sub { 1835 $ROOT->on_refresh ($self, sub {
1564 if (delete $self->{need_reflow}) { 1836 if (delete $self->{need_reflow}) {
1565 my $height = 0; 1837 my $height = 0;
1566 1838
1567 $height += $_->[0] = $self->text_height ($_->[2]) 1839 $height += $_->[0] = $self->text_height ($_->[2])
1568 for @{$self->{par}}; 1840 for @{$self->{par}};
1626 1898
1627} 1899}
1628 1900
1629############################################################################# 1901#############################################################################
1630 1902
1631package CFClient::UI::MapWidget;
1632
1633use strict;
1634
1635use List::Util qw(min max);
1636
1637use SDL;
1638use SDL::OpenGL;
1639
1640our @ISA = CFClient::UI::Base::;
1641
1642sub new {
1643 my $class = shift;
1644
1645 $class->SUPER::new (
1646 z => -1,
1647 can_focus => 1,
1648 list => (glGenLists 1),
1649 @_
1650 )
1651}
1652
1653sub key_down {
1654 print "MAPKEYDOWN\n";
1655}
1656
1657sub key_up {
1658}
1659
1660sub button_down {
1661 my ($self, $ev, $x, $y) = @_;
1662
1663 $self->focus_in;
1664
1665 if ($ev->button == 2) {
1666 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1667 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1668
1669 $self->{motion} = sub {
1670 my ($ev, $x, $y) = @_;
1671
1672 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1673
1674 $::CFG->{map_shift_x} = $bw + $x - $ox;
1675 $::CFG->{map_shift_y} = $bh + $y - $oy;
1676
1677 $self->update;
1678 };
1679 }
1680}
1681
1682sub button_up {
1683 my ($self, $ev, $x, $y) = @_;
1684
1685 delete $self->{motion};
1686}
1687
1688sub mouse_motion {
1689 my ($self, $ev, $x, $y) = @_;
1690
1691 $self->{motion}->($ev, $x, $y) if $self->{motion};
1692}
1693
1694sub size_request {
1695 (
1696 1 + 32 * int $::WIDTH / 32,
1697 1 + 32 * int $::HEIGHT / 32,
1698 )
1699}
1700
1701sub update {
1702 my ($self) = @_;
1703
1704 $self->{need_update} = 1;
1705 $self->SUPER::update;
1706}
1707
1708sub draw {
1709 my ($self) = @_;
1710
1711 if (delete $self->{need_update}) {
1712 glNewList $self->{list}, GL_COMPILE;
1713
1714 if ($::MAP) {
1715 my $sw = int $::WIDTH / 32;
1716 my $sh = int $::HEIGHT / 32;
1717
1718 my $sx = $::CFG->{map_shift_x};
1719 my $sy = $::CFG->{map_shift_y};
1720
1721 glTranslate +($sx & 31) - 32, ($sy & 31) - 32, 0;
1722
1723 my ($w, $h, $data) = $::MAP->draw ($sx >> 5, $sy >> 5, 0, 0, $sw + 1, $sh + 1);
1724
1725 if ($::CFG->{fow_enable}) {
1726 if ($::CFG->{fow_smooth}) { # smooth fog of war
1727 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1728 glConvolutionFilter2D
1729 GL_CONVOLUTION_2D,
1730 GL_ALPHA,
1731 3, 3,
1732 GL_ALPHA, GL_FLOAT,
1733 pack "f*",
1734 0.1, 0.1, 0.1,
1735 0.1, 0.2, 0.1,
1736 0.1, 0.1, 0.1,
1737 ;
1738 glEnable GL_CONVOLUTION_2D;
1739 }
1740
1741 my $tex = new CFClient::Texture
1742 w => $w,
1743 h => $h,
1744 data => $data,
1745 internalformat => GL_ALPHA,
1746 format => GL_ALPHA;
1747
1748 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1749
1750 glEnable GL_BLEND;
1751 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1752 glEnable GL_TEXTURE_2D;
1753 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1754
1755 glColor +($::CFG->{fow_intensity}) x 3, 1;
1756 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1757
1758 glDisable GL_TEXTURE_2D;
1759 glDisable GL_BLEND;
1760 }
1761
1762 # HACK BEGIN
1763 {
1764 my ($w, $h) = (250, 250);
1765
1766 glEnable GL_BLEND;
1767 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1768 glEnable GL_TEXTURE_2D;
1769 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1770
1771 CFClient::Texture->new (
1772 w => $w,
1773 h => $h,
1774 data => $::MAP->mapmap ($w, $h),
1775 type => GL_UNSIGNED_INT_8_8_8_8_REV
1776 )->draw_quad (100, 100);
1777
1778 glDisable GL_TEXTURE_2D;
1779 glDisable GL_BLEND;
1780 }
1781 # HACK END
1782 }
1783
1784 glEndList;
1785 }
1786
1787 glPushMatrix;
1788 glCallList $self->{list};
1789 glPopMatrix;
1790
1791 if ($FOCUS != $self) {
1792 glColor 64/255, 64/255, 64/255;
1793 glLogicOp GL_AND;
1794 glEnable GL_COLOR_LOGIC_OP;
1795 glBegin GL_QUADS;
1796 glVertex 0, 0;
1797 glVertex 0, $::HEIGHT;
1798 glVertex $::WIDTH, $::HEIGHT;
1799 glVertex $::WIDTH, 0;
1800 glEnd;
1801 glDisable GL_COLOR_LOGIC_OP;
1802 }
1803}
1804
1805my %DIR = (
1806 SDLK_KP8, [1, "north"],
1807 SDLK_KP9, [2, "northeast"],
1808 SDLK_KP6, [3, "east"],
1809 SDLK_KP3, [4, "southeast"],
1810 SDLK_KP2, [5, "south"],
1811 SDLK_KP1, [6, "southwest"],
1812 SDLK_KP4, [7, "west"],
1813 SDLK_KP7, [8, "northwest"],
1814
1815 SDLK_UP, [1, "north"],
1816 SDLK_RIGHT, [3, "east"],
1817 SDLK_DOWN, [5, "south"],
1818 SDLK_LEFT, [7, "west"],
1819);
1820
1821sub key_down {
1822 my ($self, $ev) = @_;
1823
1824 my $mod = $ev->key_mod;
1825 my $sym = $ev->key_sym;
1826
1827 if ($sym == SDLK_KP5) {
1828 $::CONN->user_send ("command stay fire");
1829 } elsif ($sym == SDLK_a) {
1830 $::CONN->user_send ("command apply");
1831 } elsif ($sym == SDLK_QUOTE) {
1832 $self->emit ('activate_console');
1833 } elsif ($sym == SDLK_SLASH) {
1834 $self->emit ('activate_console' => '/');
1835 } elsif (exists $DIR{$sym}) {
1836 if ($mod & KMOD_SHIFT) {
1837 $self->{shft}++;
1838 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1839 } elsif ($mod & KMOD_CTRL) {
1840 $self->{ctrl}++;
1841 $::CONN->user_send ("command run $DIR{$sym}[0]");
1842 } else {
1843 $::CONN->user_send ("command $DIR{$sym}[1]");
1844 }
1845 }
1846}
1847
1848sub key_up {
1849 my ($self, $ev) = @_;
1850
1851 my $mod = $ev->key_mod;
1852 my $sym = $ev->key_sym;
1853
1854 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1855 $::CONN->user_send ("command fire_stop");
1856 }
1857 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1858 $::CONN->user_send ("command run_stop");
1859 }
1860}
1861
1862#############################################################################
1863
1864package CFClient::UI::Animator; 1903package CFClient::UI::Animator;
1865 1904
1866use SDL::OpenGL; 1905use CFClient::OpenGL;
1867 1906
1868our @ISA = CFClient::UI::Bin::; 1907our @ISA = CFClient::UI::Bin::;
1869 1908
1870sub moveto { 1909sub moveto {
1871 my ($self, $x, $y) = @_; 1910 my ($self, $x, $y) = @_;
1927sub toggle_flopper { 1966sub toggle_flopper {
1928 my ($self) = @_; 1967 my ($self) = @_;
1929 1968
1930 # TODO: use animation 1969 # TODO: use animation
1931 if ($self->{state} = !$self->{state}) { 1970 if ($self->{state} = !$self->{state}) {
1932 $CFClient::UI::TOPLEVEL->add ($self->{other}); 1971 $CFClient::UI::ROOT->add ($self->{other});
1933 $self->{other}->move ( 1972 $self->{other}->move ($self->coord2global (0, $self->{h}));
1934 ($::WIDTH - $self->{other}{w}) * 0.5, 1973 $self->emit ("open");
1935 ($::HEIGHT - $self->{other}{h}) * 0.5,
1936 );
1937 } else { 1974 } else {
1938 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 1975 $CFClient::UI::ROOT->remove ($self->{other});
1976 $self->emit ("close");
1939 } 1977 }
1940}
1941 1978
1942############################################################################# 1979 $self->emit (changed => $self->{state});
1980}
1943 1981
1982#############################################################################
1983
1944package CFClient::UI::Toplevel; 1984package CFClient::UI::Root;
1945 1985
1946our @ISA = CFClient::UI::Container::; 1986our @ISA = CFClient::UI::Container::;
1947 1987
1948use SDL::OpenGL; 1988use CFClient::OpenGL;
1989
1990sub check_size {
1991 my ($self) = @_;
1992
1993 $self->configure (0, 0, $::WITH, $::HEIGHT);
1994}
1949 1995
1950sub size_request { 1996sub size_request {
1951 ($::WIDTH, $::HEIGHT) 1997 ($::WIDTH, $::HEIGHT)
1952} 1998}
1953 1999
1954sub size_allocate { 2000sub configure {
1955 my ($self, $x, $y, $w, $h) = @_; 2001 my ($self, $x, $y, $w, $h) = @_;
1956 2002
1957 $self->_size_allocate ($x, $y, $w, $h); 2003 $self->SUPER::configure ($x, $y, $w, $h);
1958 2004
1959 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2005 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1960 for @{$self->{children}}; 2006 for @{$self->{children}};
1961} 2007}
1962 2008
1963sub translate { 2009sub _topleft {
1964 my ($self, $x, $y) = @_; 2010 my ($self, $x, $y) = @_;
1965 2011
1966 ($x, $y) 2012 ($x, $y)
1967} 2013}
1968 2014
1969sub update { 2015sub update {
1970 my ($self) = @_; 2016 my ($self) = @_;
1971 2017
1972 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2018 $self->check_size;
1973 ::refresh (); 2019 ::refresh ();
1974} 2020}
1975 2021
1976sub add { 2022sub add {
1977 my ($self, $widget) = @_; 2023 my ($self, $child) = @_;
1978 2024
1979 $self->SUPER::add ($widget); 2025 $self->SUPER::add ($child);
1980
1981 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1982} 2026}
1983 2027
1984sub on_refresh { 2028sub on_refresh {
1985 my ($self, $id, $cb) = @_; 2029 my ($self, $id, $cb) = @_;
1986 2030
2009 2053
2010############################################################################# 2054#############################################################################
2011 2055
2012package CFClient::UI; 2056package CFClient::UI;
2013 2057
2014$TOPLEVEL = new CFClient::UI::Toplevel; 2058$ROOT = new CFClient::UI::Root;
2015 2059
20161 20601
2017 2061

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines