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.112 by root, Sat Apr 15 12:29:46 2006 UTC vs.
Revision 1.143 by root, Thu Apr 20 21:28:51 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} 71}
72 72
73# convert position array to integers 73# convert position array to integers
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}
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 abtract";
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 += ($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) {
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 {
179 # nothing to be done
180}
181
182# return top left coordinates
183sub _topleft {
146 my ($self, $x, $y, $w, $h) = @_; 184 my ($self, $x, $y) = @_;
147 185
148 $self->_size_allocate ($x, $y, $w, $h); 186 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
149} 187}
150 188
151# translate global coordinates to local coordinate system 189# translate global coordinates to local coordinate system
152sub translate { 190sub coord2local {
153 my ($self, $x, $y) = @_; 191 my ($self, $x, $y) = @_;
154 192
155 $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)
156} 203}
157 204
158sub focus_in { 205sub focus_in {
159 my ($self) = @_; 206 my ($self) = @_;
160 207
161 return if $FOCUS == $self; 208 return if $FOCUS == $self;
162 return unless $self->{can_focus}; 209 return unless $self->{can_focus};
163 210
164 my $focus = $FOCUS; $FOCUS = $self; 211 my $focus = $FOCUS; $FOCUS = $self;
212
213 $self->emit (focus_in => $focus);
214
165 $focus->update if $focus; 215 $focus->update if $focus;
166 $FOCUS->update; 216 $FOCUS->update;
167} 217}
168 218
169sub focus_out { 219sub focus_out {
170 my ($self) = @_; 220 my ($self) = @_;
171 221
172 return unless $FOCUS == $self; 222 return unless $FOCUS == $self;
173 223
174 my $focus = $FOCUS; undef $FOCUS; 224 my $focus = $FOCUS; undef $FOCUS;
225
226 $self->emit (focus_out => $focus);
227
175 $focus->update if $focus; #? 228 $focus->update if $focus; #?
176} 229}
177 230
178sub mouse_motion { } 231sub mouse_motion { }
179sub button_up { } 232sub button_up { }
203 glPopMatrix; 256 glPopMatrix;
204 257
205 if ($self == $HOVER && $self->{can_hover}) { 258 if ($self == $HOVER && $self->{can_hover}) {
206 my ($x, $y) = @$self{qw(x y)}; 259 my ($x, $y) = @$self{qw(x y)};
207 260
208 glColor 0, 0, 1, 0.2; 261 glColor 1, 0.8, 0.5, 0.2;
209 glEnable GL_BLEND; 262 glEnable GL_BLEND;
210 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 263 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
211 glBegin GL_QUADS; 264 glBegin GL_QUADS;
212 glVertex $x , $y; 265 glVertex $x , $y;
213 glVertex $x + $self->{w}, $y; 266 glVertex $x + $self->{w}, $y;
233 286
234 () 287 ()
235} 288}
236 289
237sub set_parent { 290sub set_parent {
238 my ($self, $par) = @_; 291 my ($self, $parent) = @_;
239 292
240 $self->{parent} = $par;
241 Scalar::Util::weaken $self->{parent}; 293 Scalar::Util::weaken ($self->{parent} = $parent);
242} 294}
243 295
244sub get_parent { 296sub check_size {
245 $_[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 }
246} 308}
247 309
248sub update { 310sub update {
249 my ($self) = @_; 311 my ($self) = @_;
250 312
253} 315}
254 316
255sub connect { 317sub connect {
256 my ($self, $signal, $cb) = @_; 318 my ($self, $signal, $cb) = @_;
257 319
258 push @{ $self->{cb}{$signal} }, $cb; 320 push @{ $self->{signal_cb}{$signal} }, $cb;
259} 321}
260 322
261sub emit { 323sub emit {
262 my ($self, $signal, @args) = @_; 324 my ($self, $signal, @args) = @_;
263 325
326 for my $cb (@{$self->{signal_cb}{$signal} || []}) {
264 $_->($self, @args) 327 $cb->($self, @args);
265 for @{$self->{cb}{$signal} || []}; 328 }
266} 329}
267 330
268sub DESTROY { 331sub DESTROY {
269 my ($self) = @_; 332 my ($self) = @_;
270 333
276package CFClient::UI::DrawBG; 339package CFClient::UI::DrawBG;
277 340
278our @ISA = CFClient::UI::Base::; 341our @ISA = CFClient::UI::Base::;
279 342
280use strict; 343use strict;
281use SDL::OpenGL; 344use CFClient::OpenGL;
282 345
283sub new { 346sub new {
284 my $class = shift; 347 my $class = shift;
285 348
286 # range [value, low, high, page] 349 # range [value, low, high, page]
339 402
340 $self 403 $self
341} 404}
342 405
343sub add { 406sub add {
344 my ($self, $chld) = @_; 407 my ($self, $child) = @_;
345 408
346 $chld->set_parent ($self); 409 $child->set_parent ($self);
410
411 use sort 'stable';
347 412
348 $self->{children} = [ 413 $self->{children} = [
349 sort { $a->{z} <=> $b->{z} } 414 sort { $a->{z} <=> $b->{z} }
350 @{$self->{children}}, $chld 415 @{$self->{children}}, $child
351 ]; 416 ];
352 417
353 $self->{w} = $self->{h} = -1; 418 $child->check_size;
354 $self->update;
355} 419}
356 420
357sub remove { 421sub remove {
358 my ($self, $widget) = @_; 422 my ($self, $child) = @_;
359 423
424 delete $child->{parent};
425
360 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 426 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
361 427
362 $self->size_allocate (0, 0, $self->{w}, $self->{h}); 428 $self->check_size;
363} 429}
364 430
365sub find_widget { 431sub find_widget {
366 my ($self, $x, $y) = @_; 432 my ($self, $x, $y) = @_;
367 433
397 463
398 $class->SUPER::new (children => [$child], %arg) 464 $class->SUPER::new (children => [$child], %arg)
399} 465}
400 466
401sub add { 467sub add {
402 my ($self, $widget) = @_; 468 my ($self, $child) = @_;
403 469
404 $self->{children} = []; 470 $self->{children} = [];
405 471
406 $self->SUPER::add ($widget); 472 $self->SUPER::add ($child);
407} 473}
408 474
409sub remove { 475sub remove {
410 my ($self, $widget) = @_; 476 my ($self, $widget) = @_;
411 477
420sub size_request { 486sub size_request {
421 $_[0]{children}[0]->size_request 487 $_[0]{children}[0]->size_request
422} 488}
423 489
424sub size_allocate { 490sub size_allocate {
425 my ($self, $x, $y, $w, $h) = @_; 491 my ($self, $w, $h) = @_;
426 492
427 $self->_size_allocate ($x, $y, $w, $h) or return;
428
429 $self->{children}[0]->size_allocate (0, 0, $w, $h); 493 $self->{children}[0]->configure (0, 0, $w, $h);
430} 494}
431 495
432############################################################################# 496#############################################################################
433 497
434package CFClient::UI::Window; 498package CFClient::UI::Window;
435 499
436our @ISA = CFClient::UI::Bin::; 500our @ISA = CFClient::UI::Bin::;
437 501
438use SDL::OpenGL; 502use CFClient::OpenGL;
439 503
440sub new { 504sub new {
441 my ($class, %arg) = @_; 505 my ($class, %arg) = @_;
442 506
443 my $self = $class->SUPER::new (%arg); 507 my $self = $class->SUPER::new (%arg);
460 $self->child->draw; 524 $self->child->draw;
461 }; 525 };
462} 526}
463 527
464sub size_allocate { 528sub size_allocate {
465 my ($self, $x, $y, $w, $h) = @_; 529 my ($self, $w, $h) = @_;
466 530
467 $self->_size_allocate ($x, $y, $w, $h) or return;
468
469 $self->child->size_allocate (0, 0, $w, $h); 531 $self->child->configure (0, 0, $w, $h);
470 532
471 $self->render_chld; 533 $self->render_chld;
472} 534}
473 535
474sub _draw { 536sub _draw {
490 glDisable GL_TEXTURE_2D; 552 glDisable GL_TEXTURE_2D;
491} 553}
492 554
493############################################################################# 555#############################################################################
494 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
495package CFClient::UI::Frame; 581package CFClient::UI::Frame;
496 582
497our @ISA = CFClient::UI::Bin::; 583our @ISA = CFClient::UI::Bin::;
498 584
499use SDL::OpenGL; 585use CFClient::OpenGL;
500 586
501sub size_request { 587sub size_request {
502 my ($self) = @_; 588 my ($self) = @_;
503 my $chld = $self->child 589 my $chld = $self->child
504 or return (0, 0); 590 or return (0, 0);
509} 595}
510 596
511sub size_allocate { 597sub size_allocate {
512 my ($self, $x, $y, $w, $h) = @_; 598 my ($self, $x, $y, $w, $h) = @_;
513 599
514 $self->_size_allocate ($x, $y, $w, $h) or return;
515
516 $self->child->size_allocate (2, 2, $w - 4, $h - 4); 600 $self->child->configure (2, 2, $w - 4, $h - 4);
517} 601}
518 602
519sub _draw { 603sub _draw {
520 my ($self) = @_; 604 my ($self) = @_;
521 605
538 622
539package CFClient::UI::FancyFrame; 623package CFClient::UI::FancyFrame;
540 624
541our @ISA = CFClient::UI::Bin::; 625our @ISA = CFClient::UI::Bin::;
542 626
543use SDL::OpenGL; 627use CFClient::OpenGL;
544 628
545my @tex = 629my @tex =
546 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 630 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
547 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);
548 632
549sub new { 633sub new {
550 my $class = shift; 634 my $class = shift;
551 635
552 # TODO: user_x, user_y, overwrite moveto? 636 # TODO: user_x, user_y, overwrite moveto?
553 637
554 $class->SUPER::new ( 638 my $self = $class->SUPER::new (
555 bg => [1, 1, 1, 1], 639 bg => [1, 1, 1, 1],
556 border_bg => [1, 1, 1, 1], 640 border_bg => [1, 1, 1, 1],
557 border => $::FONTSIZE * 0.8, 641 border => 0.8,
558 @_ 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,
559 ) 668 )
560} 669}
561 670
562sub size_request {
563 my ($self) = @_;
564
565 return ($self->{user_w}, $self->{user_h}) if $self->{user_w} && $self->{user_h};
566
567 my ($w, $h) = $self->SUPER::size_request;
568
569 (
570 $w + $self->{border} * 2,
571 $h + $self->{border} * 2,
572 )
573}
574
575sub size_allocate { 671sub size_allocate {
576 my ($self, $x, $y, $w, $h) = @_; 672 my ($self, $w, $h) = @_;
577 673
578 $self->_size_allocate ($x, $y, $w, $h) or return;
579
580 $h -= List::Util::max 0, $self->{border} * 2; 674 $h -= List::Util::max 0, $self->border * 2;
581 $w -= List::Util::max 0, $self->{border} * 2; 675 $w -= List::Util::max 0, $self->border * 2;
582 676
677 $self->{title}->configure ($self->border, $self->border - $::FONTSIZE * 2, $w, $::FONTSIZE * 2)
678 if $self->{title};
679
583 $self->child->size_allocate ($self->{border}, $self->{border}, $w, $h); 680 $self->child->configure ($self->border, $self->border, $w, $h);
584} 681}
585 682
586sub button_down { 683sub button_down {
587 my ($self, $ev, $x, $y) = @_; 684 my ($self, $ev, $x, $y) = @_;
588 685
686 my $border = $self->border;
687
589 if ($x < $self->{w} && $x >= $self->{w} - $self->{border} 688 if ($x < $self->{w} && $x >= $self->{w} - $border
590 && $y < $self->{h} && $y >= $self->{h} - $self->{border}) { 689 && $y < $self->{h} && $y >= $self->{h} - $border) {
591 690
592 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 691 my ($ox, $oy) = ($ev->{x}, $ev->{y});
593 my ($bw, $bh) = ($self->{w}, $self->{h}); 692 my ($bw, $bh) = ($self->{w}, $self->{h});
594 693
595 $self->{motion} = sub { 694 $self->{motion} = sub {
596 my ($ev, $x, $y) = @_; 695 my ($ev, $x, $y) = @_;
597 696
598 ($x, $y) = ($ev->motion_x, $ev->motion_y); 697 ($x, $y) = ($ev->{x}, $ev->{y});
599 698
600 $self->{user_w} = $bw + $x - $ox; 699 $self->{user_w} = $bw + $x - $ox;
601 $self->{user_h} = $bh + $y - $oy; 700 $self->{user_h} = $bh + $y - $oy;
602 $self->update; 701 $self->check_size;
603 }; 702 };
604 703
605 } elsif ($x >= 0 && $x < $self->{w} 704 } elsif ($x >= 0 && $x < $self->{w}
606 && $y >= 0 && $y < $self->{border}) { 705 && $y >= 0 && $y < $border) {
607 706
608 my ($ox, $oy) = ($ev->button_x, $ev->button_y); 707 my ($ox, $oy) = ($ev->{x}, $ev->{y});
609 my ($bx, $by) = ($self->{x}, $self->{y}); 708 my ($bx, $by) = ($self->{x}, $self->{y});
610 709
611 $self->{motion} = sub { 710 $self->{motion} = sub {
612 my ($ev, $x, $y) = @_; 711 my ($ev, $x, $y) = @_;
613 712
614 ($x, $y) = ($ev->motion_x, $ev->motion_y); 713 ($x, $y) = ($ev->{x}, $ev->{y});
615 714
616 $self->move ($bx + $x - $ox, $by + $y - $oy); 715 $self->move ($bx + $x - $ox, $by + $y - $oy);
617 $self->update; 716 $self->update;
618 }; 717 };
619 } 718 }
640 glEnable GL_BLEND; 739 glEnable GL_BLEND;
641 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 740 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
642 glEnable GL_TEXTURE_2D; 741 glEnable GL_TEXTURE_2D;
643 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 742 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
644 743
744 my $border = $self->border;
745
645 glColor @{ $self->{border_bg} }; 746 glColor @{ $self->{border_bg} };
646 $tex[1]->draw_quad (0, 0, $w, $self->{border}); 747 $tex[1]->draw_quad (0, 0, $w, $border);
647 $tex[3]->draw_quad (0, $self->{border}, $self->{border}, $ch); 748 $tex[3]->draw_quad (0, $border, $border, $ch);
648 $tex[2]->draw_quad ($w - $self->{border}, $self->{border}, $self->{border}, $ch); 749 $tex[2]->draw_quad ($w - $border, $border, $border, $ch);
649 $tex[4]->draw_quad (0, $h - $self->{border}, $w, $self->{border}); 750 $tex[4]->draw_quad (0, $h - $border, $w, $border);
650 751
651 my $bg = $tex[0]; 752 my $bg = $tex[0];
652 753
653 # TODO: repeat texture not scale 754 # TODO: repeat texture not scale
654 my $rep_x = $cw / $bg->{w}; 755 my $rep_x = $cw / $bg->{w};
657 glColor @{ $self->{bg} }; 758 glColor @{ $self->{bg} };
658 759
659 $bg->{s} = $rep_x; 760 $bg->{s} = $rep_x;
660 $bg->{t} = $rep_y; 761 $bg->{t} = $rep_y;
661 $bg->{wrap_mode} = 1; 762 $bg->{wrap_mode} = 1;
662 $bg->draw_quad ($self->{border}, $self->{border}, $cw, $ch); 763 $bg->draw_quad ($border, $border, $cw, $ch);
663 764
664 glDisable GL_TEXTURE_2D; 765 glDisable GL_TEXTURE_2D;
665 glDisable GL_BLEND; 766 glDisable GL_BLEND;
666 767
768 $self->{title}->draw if $self->{title};
667 $self->child->draw; 769 $self->child->draw;
668} 770}
669 771
670############################################################################# 772#############################################################################
671 773
673 775
674our @ISA = CFClient::UI::Base::; 776our @ISA = CFClient::UI::Base::;
675 777
676use List::Util qw(max sum); 778use List::Util qw(max sum);
677 779
678use SDL::OpenGL; 780use CFClient::OpenGL;
679 781
680sub new { 782sub new {
681 my $class = shift; 783 my $class = shift;
682 784
683 $class->SUPER::new ( 785 $class->SUPER::new (
685 @_ 787 @_
686 ) 788 )
687} 789}
688 790
689sub add { 791sub add {
690 my ($self, $x, $y, $chld) = @_; 792 my ($self, $x, $y, $child) = @_;
691 793
794 $child->set_parent ($self);
692 $self->{children}[$y][$x] = $chld; 795 $self->{children}[$y][$x] = $child;
693 $chld->set_parent ($self);
694 796
695 $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};
696 $self->update; 805 $self->update;
697} 806}
698 807
699sub get_wh { 808sub get_wh {
700 my ($self) = @_; 809 my ($self) = @_;
728 (sum @$hs), 837 (sum @$hs),
729 ) 838 )
730} 839}
731 840
732sub size_allocate { 841sub size_allocate {
733 my ($self, $x, $y, $w, $h) = @_; 842 my ($self, $w, $h) = @_;
734
735 $self->_size_allocate ($x, $y, $w, $h) or return;
736 843
737 my ($ws, $hs) = $self->get_wh; 844 my ($ws, $hs) = $self->get_wh;
738 845
739 my $req_w = sum @$ws; 846 my $req_w = sum @$ws;
740 my $req_h = sum @$hs; 847 my $req_h = sum @$hs;
762 869
763 for my $c (0 .. $#$row) { 870 for my $c (0 .. $#$row) {
764 my $col_w = $ws->[$c]; 871 my $col_w = $ws->[$c];
765 872
766 if (my $widget = $row->[$c]) { 873 if (my $widget = $row->[$c]) {
767 $widget->size_allocate ($x, $y, $col_w, $row_h); 874 $widget->configure ($x, $y, $col_w, $row_h);
768 } 875 }
769 876
770 $x += $col_w; 877 $x += $col_w;
771 } 878 }
772 879
817 (List::Util::max map $_->[1], @alloc), 924 (List::Util::max map $_->[1], @alloc),
818 ) 925 )
819} 926}
820 927
821sub size_allocate { 928sub size_allocate {
822 my ($self, $x, $y, $w, $h) = @_; 929 my ($self, $w, $h) = @_;
823
824 $self->_size_allocate ($x, $y, $w, $h);
825 930
826 ($h, $w) = ($w, $h); 931 ($h, $w) = ($w, $h);
827 932
828 my $children = $self->{children}; 933 my $children = $self->{children};
829 934
851 956
852 my $y = 0; 957 my $y = 0;
853 for (0 .. $#$children) { 958 for (0 .. $#$children) {
854 my $child = $children->[$_]; 959 my $child = $children->[$_];
855 my $h = $h[$_]; 960 my $h = $h[$_];
856 $child->size_allocate ($y, 0, $h, $w); 961 $child->configure ($y, 0, $h, $w);
857 962
858 $y += $h; 963 $y += $h;
859 } 964 }
965
966 1
860} 967}
861 968
862############################################################################# 969#############################################################################
863 970
864package CFClient::UI::VBox; 971package CFClient::UI::VBox;
877 (List::Util::sum map $_->[1], @alloc), 984 (List::Util::sum map $_->[1], @alloc),
878 ) 985 )
879} 986}
880 987
881sub size_allocate { 988sub size_allocate {
882 my ($self, $x, $y, $w, $h) = @_; 989 my ($self, $w, $h) = @_;
883
884 $self->_size_allocate ($x, $y, $w, $h);
885 990
886 my $children = $self->{children}; 991 my $children = $self->{children};
887 992
888 my @h = map +($_->size_request)[1], @$children; 993 my @h = map +($_->size_request)[1], @$children;
889 994
907 1012
908 my $y = 0; 1013 my $y = 0;
909 for (0 .. $#$children) { 1014 for (0 .. $#$children) {
910 my $child = $children->[$_]; 1015 my $child = $children->[$_];
911 my $h = $h[$_]; 1016 my $h = $h[$_];
912 $child->size_allocate (0, $y, $w, $h); 1017 $child->configure (0, $y, $w, $h);
913 1018
914 $y += $h; 1019 $y += $h;
915 } 1020 }
1021
1022 1
916} 1023}
917 1024
918############################################################################# 1025#############################################################################
919 1026
920package CFClient::UI::Label; 1027package CFClient::UI::Label;
921 1028
922our @ISA = CFClient::UI::Base::; 1029our @ISA = CFClient::UI::Base::;
923 1030
924use SDL::OpenGL; 1031use CFClient::OpenGL;
925 1032
926sub new { 1033sub new {
927 my ($class, %arg) = @_; 1034 my ($class, %arg) = @_;
928 1035
929 my $self = $class->SUPER::new ( 1036 my $self = $class->SUPER::new (
930 fg => [1, 1, 1], 1037 fg => [1, 1, 1],
931 fontsize => $::FONTSIZE, 1038 fontsize => 1,
932 text => "", 1039 text => "",
933 align => -1, 1040 align => -1,
1041 valign => -1,
934 padding => 2, 1042 padding => 2,
935 layout => new CFClient::Layout, 1043 layout => new CFClient::Layout,
936 %arg 1044 %arg
937 ); 1045 );
938 1046
939 $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};
940 1055
941 $self 1056 $self
942} 1057}
943 1058
944sub escape_text { 1059sub escape_text {
952} 1067}
953 1068
954sub set_text { 1069sub set_text {
955 my ($self, $text) = @_; 1070 my ($self, $text) = @_;
956 1071
957 $self->{text} = $text;
958 $self->{layout}->set_markup ($text); 1072 $self->{layout}->set_text ($text);
959 1073
960 delete $self->{texture}; 1074 delete $self->{texture};
961# $self->{w} = $self->{h} = -1;
962 $self->update; 1075 $self->update;
963} 1076}
964 1077
965sub get_text { 1078sub set_markup {
966 my ($self, $text) = @_; 1079 my ($self, $markup) = @_;
967 1080
968 $self->{text} 1081 $self->{layout}->set_markup ($markup);
1082
1083 delete $self->{texture};
1084 $self->update;
969} 1085}
970 1086
971sub size_request { 1087sub size_request {
972 my ($self) = @_; 1088 my ($self) = @_;
973 1089
974 $self->{layout}->set_width; 1090 $self->{layout}->set_width;
975 $self->{layout}->set_height ($self->{fontsize}); 1091 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1092
976 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 }
977 1103
978 ( 1104 (
979 $w + $self->{padding} * 2, 1105 $w + $self->{padding} * 2,
980 $h + $self->{padding} * 2, 1106 $h + $self->{padding} * 2,
981 ) 1107 )
982} 1108}
983 1109
984sub size_allocate { 1110sub size_allocate {
985 my ($self, $x, $y, $w, $h) = @_; 1111 my ($self, $w, $h) = @_;
986
987 $self->_size_allocate ($x, $y, $w, $h) or return;
988 1112
989 delete $self->{texture}; 1113 delete $self->{texture};
990}
991
992sub update {
993 my ($self) = @_;
994
995 delete $self->{texture};
996 $self->SUPER::update;
997} 1114}
998 1115
999sub _draw { 1116sub _draw {
1000 my ($self) = @_; 1117 my ($self) = @_;
1001 1118
1002 my $tex = $self->{texture} ||= do { 1119 my $tex = $self->{texture} ||= do {
1003 $self->{layout}->set_width ($self->{w}); 1120 $self->{layout}->set_width ($self->{w});
1004 $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);
1005 new_from_layout CFClient::Texture $self->{layout} 1122 new_from_layout CFClient::Texture $self->{layout}
1006 }; 1123 };
1007 1124
1008 glEnable GL_BLEND; 1125 glEnable GL_BLEND;
1009 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 1126 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1010 glEnable GL_TEXTURE_2D; 1127 glEnable GL_TEXTURE_2D;
1011 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1128 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1012 1129
1013 glColor @{$self->{fg}}; 1130 glColor @{$self->{fg}};
1014 1131
1015 my $x = 1132 $self->{ox} = int (
1016 $self->{align} < 0 ? $self->{padding} 1133 $self->{align} < 0 ? $self->{padding}
1017 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1134 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding}
1018 : ($self->{w} - $tex->{w}) * 0.5; 1135 : ($self->{w} - $tex->{w}) * 0.5
1136 );
1019 1137
1020 $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});
1021 1145
1022 glDisable GL_TEXTURE_2D; 1146 glDisable GL_TEXTURE_2D;
1023 glDisable GL_BLEND; 1147 glDisable GL_BLEND;
1024} 1148}
1025 1149
1026############################################################################# 1150#############################################################################
1027 1151
1028package CFClient::UI::Entry; 1152package CFClient::UI::EntryBase;
1029 1153
1030our @ISA = CFClient::UI::Label::; 1154our @ISA = CFClient::UI::Label::;
1031 1155
1032use SDL; 1156use CFClient::OpenGL;
1033use SDL::OpenGL;
1034 1157
1035sub new { 1158sub new {
1036 my $class = shift; 1159 my $class = shift;
1037 1160
1038 $class->SUPER::new ( 1161 $class->SUPER::new (
1040 bg => [0, 0, 0, 0.2], 1163 bg => [0, 0, 0, 0.2],
1041 active_bg => [1, 1, 1, 0.5], 1164 active_bg => [1, 1, 1, 0.5],
1042 active_fg => [0, 0, 0], 1165 active_fg => [0, 0, 0],
1043 can_hover => 1, 1166 can_hover => 1,
1044 can_focus => 1, 1167 can_focus => 1,
1168 valign => 0,
1045 @_ 1169 @_
1046 ) 1170 )
1047} 1171}
1048 1172
1049sub _set_text { 1173sub _set_text {
1050 my ($self, $text) = @_; 1174 my ($self, $text) = @_;
1051 1175
1052 my $old_text = $self->{text}; 1176 delete $self->{cur_h};
1177
1178 return if $self->{text} eq $text;
1179
1180 delete $self->{texture};
1053 1181
1054 $self->{last_activity} = $::NOW; 1182 $self->{last_activity} = $::NOW;
1055
1056 $self->{text} = $text; 1183 $self->{text} = $text;
1057 $self->{layout}->set_width ($self->{w});
1058 $self->{layout}->set_height (List::Util::min $self->{h} - $self->{padding} * 2, $self->{fontsize});
1059 1184
1060 $text =~ s/./*/g if $self->{hidden}; 1185 $text =~ s/./*/g if $self->{hidden};
1186 $self->{layout}->set_text ("$text ");
1061 1187
1062 $self->{layout}->set_markup ($self->escape_text ($text) . " "); 1188 $self->emit (changed => $self->{text});
1189}
1063 1190
1064 $text = substr $text, 0, $self->{cursor}; 1191sub get_text {
1065 utf8::encode $text; 1192 $_[0]{text}
1066
1067 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
1068
1069 $self->emit (changed => $self->{text}) # XXX: is this the right place to do this?
1070 if $old_text ne $self->{text};
1071} 1193}
1072 1194
1073sub size_request { 1195sub size_request {
1074 my ($self) = @_; 1196 my ($self) = @_;
1075 1197
1077 1199
1078 ($w + 1, $h) # add 1 for cursor 1200 ($w + 1, $h) # add 1 for cursor
1079} 1201}
1080 1202
1081sub size_allocate { 1203sub size_allocate {
1082 my ($self, $x, $y, $w, $h) = @_; 1204 my ($self, $w, $h) = @_;
1083
1084 $self->SUPER::size_allocate ($x, $y, $w, $h);
1085 1205
1086 $self->_set_text ($self->{text}); 1206 $self->_set_text ($self->{text});
1087} 1207}
1088 1208
1089sub set_text { 1209sub set_text {
1095} 1215}
1096 1216
1097sub key_down { 1217sub key_down {
1098 my ($self, $ev) = @_; 1218 my ($self, $ev) = @_;
1099 1219
1100 my $mod = $ev->key_mod; 1220 my $mod = $ev->{mod};
1101 my $sym = $ev->key_sym; 1221 my $sym = $ev->{sym};
1102
1103 my $uni = $ev->key_unicode; 1222 my $uni = $ev->{unicode};
1104 1223
1105 my $text = $self->get_text; 1224 my $text = $self->get_text;
1106 1225
1107 if ($sym == SDLK_BACKSPACE) { 1226 if ($sym == 8) {
1108 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1227 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1109 } elsif ($sym == SDLK_DELETE) { 1228 } elsif ($sym == 127) {
1110 substr $text, $self->{cursor}, 1, ""; 1229 substr $text, $self->{cursor}, 1, "";
1111 } elsif ($sym == SDLK_LEFT) { 1230 } elsif ($sym == CFClient::SDLK_LEFT) {
1112 --$self->{cursor} if $self->{cursor}; 1231 --$self->{cursor} if $self->{cursor};
1113 } elsif ($sym == SDLK_RIGHT) { 1232 } elsif ($sym == CFClient::SDLK_RIGHT) {
1114 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1233 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1115 } elsif ($sym == SDLK_HOME) { 1234 } elsif ($sym == CFClient::SDLK_HOME) {
1116 $self->{cursor} = 0; 1235 $self->{cursor} = 0;
1117 } elsif ($sym == SDLK_END) { 1236 } elsif ($sym == CFClient::SDLK_END) {
1118 $self->{cursor} = length $text; 1237 $self->{cursor} = length $text;
1119 } elsif ($sym == SDLK_ESCAPE) { 1238 } elsif ($sym == 27) {
1120 $self->emit ('escape'); 1239 $self->emit ('escape');
1121 } elsif ($uni) { 1240 } elsif ($uni) {
1122 substr $text, $self->{cursor}++, 0, chr $uni; 1241 substr $text, $self->{cursor}++, 0, chr $uni;
1123 } 1242 }
1124 1243
1179 1298
1180 $self->SUPER::_draw; 1299 $self->SUPER::_draw;
1181 1300
1182 #TODO: force update every cursor change :( 1301 #TODO: force update every cursor change :(
1183 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
1184 glColor @{$self->{fg}}; 1311 glColor @{$self->{fg}};
1185 glBegin GL_LINES; 1312 glBegin GL_LINES;
1186 glVertex $self->{cur_x}, $self->{cur_y};
1187 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};
1188 glEnd; 1315 glEnd;
1189 } 1316 }
1190} 1317}
1191 1318
1192package CFClient::UI::LineEntry; 1319package CFClient::UI::Entry;
1193 1320
1194our @ISA = CFClient::UI::Entry::; 1321our @ISA = CFClient::UI::EntryBase::;
1195 1322
1196use SDL; 1323use CFClient::OpenGL;
1197use SDL::OpenGL;
1198 1324
1199sub key_down { 1325sub key_down {
1200 my ($self, $ev) = @_; 1326 my ($self, $ev) = @_;
1201 1327
1202 my $sym = $ev->key_sym; 1328 my $sym = $ev->{sym};
1203 1329
1204 if ($sym == SDLK_RETURN) { 1330 if ($sym == 13) {
1205 $self->emit (activate => $self->get_text); 1331 $self->emit (activate => $self->get_text);
1206 $self->update; 1332 $self->update;
1207 1333
1208 } else { 1334 } else {
1209 $self->SUPER::key_down ($ev); 1335 $self->SUPER::key_down ($ev);
1215 1341
1216package CFClient::UI::Button; 1342package CFClient::UI::Button;
1217 1343
1218our @ISA = CFClient::UI::Label::; 1344our @ISA = CFClient::UI::Label::;
1219 1345
1220use SDL; 1346use CFClient::OpenGL;
1221use SDL::OpenGL;
1222 1347
1223my @tex = 1348my @tex =
1224 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1349 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1225 qw(b1_button_active.png); 1350 qw(b1_button_active.png);
1226 1351
1229 1354
1230 $class->SUPER::new ( 1355 $class->SUPER::new (
1231 padding => 4, 1356 padding => 4,
1232 fg => [1, 1, 1], 1357 fg => [1, 1, 1],
1233 bg => [1, 1, 1, 0.2], 1358 bg => [1, 1, 1, 0.2],
1234 active_fg => [1, 1, 0], 1359 active_fg => [0, 0, 1],
1235 can_hover => 1, 1360 can_hover => 1,
1361 align => 0,
1362 valign => 0,
1236 @_ 1363 @_
1237 ) 1364 )
1238} 1365}
1239 1366
1240sub button_up { 1367sub button_up {
1248 1375
1249sub _draw { 1376sub _draw {
1250 my ($self) = @_; 1377 my ($self) = @_;
1251 1378
1252 local $self->{fg} = $self->{fg}; 1379 local $self->{fg} = $self->{fg};
1253 my $tex = $tex[0];
1254
1255 glEnable GL_BLEND;
1256 glEnable GL_TEXTURE_2D;
1257 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1258 1380
1259 if ($GRAB == $self) { 1381 if ($GRAB == $self) {
1260 $self->{fg} = $self->{active_fg}; 1382 $self->{fg} = $self->{active_fg};
1261 } 1383 }
1262 1384
1263 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;
1264 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;
1265 1390
1266 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 1391 $tex[0]->draw_quad (0, 0, $self->{w}, $self->{h});
1267 1392
1268 glDisable GL_TEXTURE_2D; 1393 glDisable GL_TEXTURE_2D;
1269 glDisable GL_BLEND; 1394 glDisable GL_BLEND;
1270 1395
1271 $self->SUPER::_draw; 1396 $self->SUPER::_draw;
1279 1404
1280my @tex = 1405my @tex =
1281 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1406 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1282 qw(c1_checkbox_bg.png c1_checkbox_active.png); 1407 qw(c1_checkbox_bg.png c1_checkbox_active.png);
1283 1408
1284use SDL; 1409use CFClient::OpenGL;
1285use SDL::OpenGL;
1286 1410
1287sub new { 1411sub new {
1288 my $class = shift; 1412 my $class = shift;
1289 1413
1290 $class->SUPER::new ( 1414 $class->SUPER::new (
1301 my ($self) = @_; 1425 my ($self) = @_;
1302 1426
1303 ($self->{padding} * 2 + 6) x 2 1427 ($self->{padding} * 2 + 6) x 2
1304} 1428}
1305 1429
1306sub size_allocate {
1307 my ($self, $x, $y, $w, $h) = @_;
1308
1309 $self->_size_allocate ($x, $y, $w, $h);
1310}
1311
1312sub button_down { 1430sub button_down {
1313 my ($self, $ev, $x, $y) = @_; 1431 my ($self, $ev, $x, $y) = @_;
1314 1432
1315 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1433 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1316 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1434 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1342 glDisable GL_BLEND; 1460 glDisable GL_BLEND;
1343} 1461}
1344 1462
1345############################################################################# 1463#############################################################################
1346 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 #@$tex{qw(w h)}
1509 (0, 0)
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 if ($self->{vertical}) {
1538 glRotate 90, 0, 0, 1;
1539 glTranslate 0, -$self->{w}, 0;
1540
1541 ($w, $h) = ($h, $w);
1542 }
1543
1544 my $ycut = $self->{val} / ($self->{max_val} || 1);
1545 $ycut = 1 if $self->{val} > $self->{max_val};
1546
1547 my $t1 = $tex->[0];
1548 my $t2 = $tex->[1];
1549
1550 glEnable GL_BLEND;
1551 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1552 glEnable GL_TEXTURE_2D;
1553 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1554
1555 my $h1 = $self->{h} - $ycut * $self->{h};
1556 my $h2 = $ycut * $self->{h};
1557
1558 glBindTexture GL_TEXTURE_2D, $t1->{name};
1559 glBegin GL_QUADS;
1560 glTexCoord 0 , 0; glVertex 0 , 0;
1561 glTexCoord 0 , $t1->{t} * (1 - $ycut); glVertex 0 , $h1;
1562 glTexCoord $t1->{s}, $t1->{t} * (1 - $ycut); glVertex $w, $h1;
1563 glTexCoord $t1->{s}, 0; glVertex $w, 0;
1564 glEnd;
1565
1566 glBindTexture GL_TEXTURE_2D, $t2->{name};
1567 glBegin GL_QUADS;
1568 glTexCoord 0 , $t2->{t} * (1 - $ycut); glVertex 0 , $h1;
1569 glTexCoord 0 , $t2->{t}; glVertex 0 , $h1 + $h2;
1570 glTexCoord $t2->{s}, $t2->{t}; glVertex $w, $h1 + $h2;
1571 glTexCoord $t2->{s}, $t2->{t} * (1 - $ycut); glVertex $w, $h1;
1572 glEnd;
1573
1574 glDisable GL_BLEND;
1575 glDisable GL_TEXTURE_2D;
1576}
1577
1578#############################################################################
1579
1580package CFClient::UI::Gauge;
1581
1582our @ISA = CFClient::UI::VBox::;
1583
1584sub new {
1585 my ($class, %arg) = shift;
1586
1587 my $self = $class->SUPER::new (
1588 @_,
1589 );
1590
1591 $self->add ($self->{value} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1592 $self->add ($self->{gauge} = new CFClient::UI::VGauge type => $self->{type}, expand => 1);
1593 $self->add ($self->{max} = new CFClient::UI::Label valign => 1, align => 0, template => "999");
1594
1595 $self
1596}
1597
1598sub set_value {
1599 my ($self, $val, $max) = @_;
1600
1601 $self->set_max ($max)
1602 if defined $max;
1603
1604 $self->{gauge}->set_value ($val, $max);
1605 $self->{value}->set_text ($val);
1606}
1607
1608sub set_max {
1609 my ($self, $max) = @_;
1610
1611 $self->{gauge}->set_max ($max);
1612 $self->{max}->set_text ($max);
1613}
1614
1615#############################################################################
1616
1347package CFClient::UI::Slider; 1617package CFClient::UI::Slider;
1348 1618
1349use strict; 1619use strict;
1350 1620
1351use SDL::OpenGL; 1621use CFClient::OpenGL;
1352 1622
1353our @ISA = CFClient::UI::DrawBG::; 1623our @ISA = CFClient::UI::DrawBG::;
1354 1624
1355my @tex = 1625my @tex =
1356 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 1626 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
1366 # TODO: calculations are off 1636 # TODO: calculations are off
1367 my $self = $class->SUPER::new ( 1637 my $self = $class->SUPER::new (
1368 fg => [1, 1, 1], 1638 fg => [1, 1, 1],
1369 active_fg => [0, 0, 0], 1639 active_fg => [0, 0, 0],
1370 range => [0, 0, 100, 10], 1640 range => [0, 0, 100, 10],
1371 req_w => 40, 1641 req_w => 20,
1372 req_h => 13, 1642 req_h => 20,
1373 vertical => 0, 1643 vertical => 0,
1374 can_hover => 1, 1644 can_hover => 1,
1375 inner_pad => 5, 1645 inner_pad => 5,
1376 @_ 1646 @_
1377 ); 1647 );
1481 1751
1482package CFClient::UI::TextView; 1752package CFClient::UI::TextView;
1483 1753
1484our @ISA = CFClient::UI::HBox::; 1754our @ISA = CFClient::UI::HBox::;
1485 1755
1486use SDL::OpenGL; 1756use CFClient::OpenGL;
1487 1757
1488sub new { 1758sub new {
1489 my $class = shift; 1759 my $class = shift;
1490 1760
1491 my $self = $class->SUPER::new ( 1761 my $self = $class->SUPER::new (
1492 req_w => $::WIDTH / 6,
1493 req_h => $::HEIGHT / 6,
1494 fontsize => $::FONTSIZE, 1762 fontsize => 1,
1495 @_, 1763 @_,
1496 1764
1497 layout => (new CFClient::Layout), 1765 layout => (new CFClient::Layout),
1498 par => [], 1766 par => [],
1499 height => 0, 1767 height => 0,
1520sub text_height { 1788sub text_height {
1521 my ($self, $text) = @_; 1789 my ($self, $text) = @_;
1522 1790
1523 my $layout = $self->{layout}; 1791 my $layout = $self->{layout};
1524 1792
1525 $layout->set_height ($self->{fontsize}); 1793 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
1526 $layout->set_width ($self->{w}); 1794 $layout->set_width ($self->{w});
1527 $layout->set_text ($text); 1795 $layout->set_text ($text);
1528 1796
1529 ($layout->size)[1] 1797 ($layout->size)[1]
1530} 1798}
1534 1802
1535 $self->{need_reflow}++; 1803 $self->{need_reflow}++;
1536 $self->update; 1804 $self->update;
1537} 1805}
1538 1806
1539sub size_request {
1540 my ($self) = @_;
1541
1542 ($self->{req_w}, $self->{req_h})
1543}
1544
1545sub size_allocate { 1807sub size_allocate {
1546 my ($self, $x, $y, $w, $h) = @_; 1808 my ($self, $w, $h) = @_;
1547 1809
1548 $self->SUPER::size_allocate ($x, $y, $w, $h); 1810 $self->SUPER::size_allocate ($w, $h);
1549 1811
1550 $self->{layout}->set_height ($self->{fontsize}); 1812 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1551 $self->{layout}->set_width ($self->{w}); 1813 $self->{layout}->set_width ($self->{children}[0]{w});
1552 1814
1553 $self->reflow; 1815 $self->reflow;
1554} 1816}
1555 1817
1556sub add_paragraph { 1818sub add_paragraph {
1575 1837
1576 return unless $self->{h} > 0; 1838 return unless $self->{h} > 0;
1577 1839
1578 delete $self->{texture}; 1840 delete $self->{texture};
1579 1841
1580 $TOPLEVEL->on_refresh ($self, sub { 1842 $ROOT->on_refresh ($self, sub {
1581 if (delete $self->{need_reflow}) { 1843 if (delete $self->{need_reflow}) {
1582 my $height = 0; 1844 my $height = 0;
1583 1845
1584 $height += $_->[0] = $self->text_height ($_->[2]) 1846 $height += $_->[0] = $self->text_height ($_->[2])
1585 for @{$self->{par}}; 1847 for @{$self->{par}};
1643 1905
1644} 1906}
1645 1907
1646############################################################################# 1908#############################################################################
1647 1909
1648package CFClient::UI::MapWidget;
1649
1650use strict;
1651
1652use List::Util qw(min max);
1653
1654use SDL;
1655use SDL::OpenGL;
1656
1657our @ISA = CFClient::UI::Base::;
1658
1659sub new {
1660 my $class = shift;
1661
1662 $class->SUPER::new (
1663 z => -1,
1664 can_focus => 1,
1665 list => (glGenLists 1),
1666 @_
1667 )
1668}
1669
1670sub key_down {
1671 print "MAPKEYDOWN\n";
1672}
1673
1674sub key_up {
1675}
1676
1677sub button_down {
1678 my ($self, $ev, $x, $y) = @_;
1679
1680 $self->focus_in;
1681
1682 if ($ev->button == 2) {
1683 my ($ox, $oy) = ($ev->button_x, $ev->button_y);
1684 my ($bw, $bh) = ($::CFG->{map_shift_x}, $::CFG->{map_shift_y});
1685
1686 $self->{motion} = sub {
1687 my ($ev, $x, $y) = @_;
1688
1689 ($x, $y) = ($ev->motion_x, $ev->motion_y);
1690
1691 $::CFG->{map_shift_x} = $bw + $x - $ox;
1692 $::CFG->{map_shift_y} = $bh + $y - $oy;
1693
1694 $self->update;
1695 };
1696 }
1697}
1698
1699sub button_up {
1700 my ($self, $ev, $x, $y) = @_;
1701
1702 delete $self->{motion};
1703}
1704
1705sub mouse_motion {
1706 my ($self, $ev, $x, $y) = @_;
1707
1708 $self->{motion}->($ev, $x, $y) if $self->{motion};
1709}
1710
1711sub size_request {
1712 (
1713 1 + 32 * int $::WIDTH / 32,
1714 1 + 32 * int $::HEIGHT / 32,
1715 )
1716}
1717
1718sub update {
1719 my ($self) = @_;
1720
1721 $self->{need_update} = 1;
1722 $self->SUPER::update;
1723}
1724
1725sub draw {
1726 my ($self) = @_;
1727
1728 if (delete $self->{need_update}) {
1729 glNewList $self->{list}, GL_COMPILE;
1730
1731 if ($::MAP) {
1732 my $sw = int $::WIDTH / 32;
1733 my $sh = int $::HEIGHT / 32;
1734
1735 my $sx = $::CFG->{map_shift_x}; my $sx0 = $sx & 31; $sx = ($sx - $sx0) / 32;
1736 my $sy = $::CFG->{map_shift_y}; my $sy0 = $sy & 31; $sy = ($sy - $sy0) / 32;
1737
1738 glTranslate $sx0 - 32, $sy0 - 32, 0;
1739
1740 my ($w, $h, $data) = $::MAP->draw ($sx, $sy, 0, 0, $sw + 1, $sh + 1);
1741
1742 if ($::CFG->{fow_enable}) {
1743 if ($::CFG->{fow_smooth}) { # smooth fog of war
1744 glConvolutionParameter GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER;
1745 glConvolutionFilter2D
1746 GL_CONVOLUTION_2D,
1747 GL_ALPHA,
1748 3, 3,
1749 GL_ALPHA, GL_FLOAT,
1750 pack "f*",
1751 0.1, 0.1, 0.1,
1752 0.1, 0.2, 0.1,
1753 0.1, 0.1, 0.1,
1754 ;
1755 glEnable GL_CONVOLUTION_2D;
1756 }
1757
1758 my $tex = new CFClient::Texture
1759 w => $w,
1760 h => $h,
1761 data => $data,
1762 internalformat => GL_ALPHA,
1763 format => GL_ALPHA;
1764
1765 glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth};
1766
1767 glEnable GL_BLEND;
1768 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1769 glEnable GL_TEXTURE_2D;
1770 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1771
1772 glColor +($::CFG->{fow_intensity}) x 3, 1;
1773 $tex->draw_quad (0, 0, $w * 32, $h * 32);
1774
1775 glDisable GL_TEXTURE_2D;
1776 glDisable GL_BLEND;
1777 }
1778
1779 # HACK BEGIN
1780 {
1781 glTranslate -($sx0 - 32), -($sy0 - 32), 0;#remove
1782 my ($w, $h) = (250, 250);
1783
1784 glEnable GL_BLEND;
1785 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1786 glEnable GL_TEXTURE_2D;
1787 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1788
1789 CFClient::Texture->new (
1790 w => $w,
1791 h => $h,
1792 data => $::MAP->mapmap ($w, $h),
1793 type => GL_UNSIGNED_INT_8_8_8_8_REV
1794 )->draw_quad (100, 100);
1795
1796 glDisable GL_TEXTURE_2D;
1797 glDisable GL_BLEND;
1798 }
1799 # HACK END
1800 }
1801
1802 glEndList;
1803 }
1804
1805 glPushMatrix;
1806 glCallList $self->{list};
1807 glPopMatrix;
1808
1809 if ($FOCUS != $self) {
1810 glColor 64/255, 64/255, 64/255;
1811 glLogicOp GL_AND;
1812 glEnable GL_COLOR_LOGIC_OP;
1813 glBegin GL_QUADS;
1814 glVertex 0, 0;
1815 glVertex 0, $::HEIGHT;
1816 glVertex $::WIDTH, $::HEIGHT;
1817 glVertex $::WIDTH, 0;
1818 glEnd;
1819 glDisable GL_COLOR_LOGIC_OP;
1820 }
1821}
1822
1823my %DIR = (
1824 SDLK_KP8, [1, "north"],
1825 SDLK_KP9, [2, "northeast"],
1826 SDLK_KP6, [3, "east"],
1827 SDLK_KP3, [4, "southeast"],
1828 SDLK_KP2, [5, "south"],
1829 SDLK_KP1, [6, "southwest"],
1830 SDLK_KP4, [7, "west"],
1831 SDLK_KP7, [8, "northwest"],
1832
1833 SDLK_UP, [1, "north"],
1834 SDLK_RIGHT, [3, "east"],
1835 SDLK_DOWN, [5, "south"],
1836 SDLK_LEFT, [7, "west"],
1837);
1838
1839sub key_down {
1840 my ($self, $ev) = @_;
1841
1842 my $mod = $ev->key_mod;
1843 my $sym = $ev->key_sym;
1844
1845 if ($sym == SDLK_KP5) {
1846 $::CONN->user_send ("command stay fire");
1847 } elsif ($sym == SDLK_a) {
1848 $::CONN->user_send ("command apply");
1849 } elsif ($sym == SDLK_QUOTE) {
1850 $self->emit ('activate_console');
1851 } elsif ($sym == SDLK_SLASH) {
1852 $self->emit ('activate_console' => '/');
1853 } elsif (exists $DIR{$sym}) {
1854 if ($mod & KMOD_SHIFT) {
1855 $self->{shft}++;
1856 $::CONN->user_send ("command fire $DIR{$sym}[0]");
1857 } elsif ($mod & KMOD_CTRL) {
1858 $self->{ctrl}++;
1859 $::CONN->user_send ("command run $DIR{$sym}[0]");
1860 } else {
1861 $::CONN->user_send ("command $DIR{$sym}[1]");
1862 }
1863 }
1864}
1865
1866sub key_up {
1867 my ($self, $ev) = @_;
1868
1869 my $mod = $ev->key_mod;
1870 my $sym = $ev->key_sym;
1871
1872 if (!($mod & KMOD_SHIFT) && delete $self->{shft}) {
1873 $::CONN->user_send ("command fire_stop");
1874 }
1875 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
1876 $::CONN->user_send ("command run_stop");
1877 }
1878}
1879
1880#############################################################################
1881
1882package CFClient::UI::Animator; 1910package CFClient::UI::Animator;
1883 1911
1884use SDL::OpenGL; 1912use CFClient::OpenGL;
1885 1913
1886our @ISA = CFClient::UI::Bin::; 1914our @ISA = CFClient::UI::Bin::;
1887 1915
1888sub moveto { 1916sub moveto {
1889 my ($self, $x, $y) = @_; 1917 my ($self, $x, $y) = @_;
1945sub toggle_flopper { 1973sub toggle_flopper {
1946 my ($self) = @_; 1974 my ($self) = @_;
1947 1975
1948 # TODO: use animation 1976 # TODO: use animation
1949 if ($self->{state} = !$self->{state}) { 1977 if ($self->{state} = !$self->{state}) {
1950 $CFClient::UI::TOPLEVEL->add ($self->{other}); 1978 $CFClient::UI::ROOT->add ($self->{other});
1951 $self->{other}->move ( 1979 $self->{other}->move ($self->coord2global (0, $self->{h}));
1952 ($::WIDTH - $self->{other}{w}) * 0.5, 1980 $self->emit ("open");
1953 ($::HEIGHT - $self->{other}{h}) * 0.5,
1954 );
1955 } else { 1981 } else {
1956 $CFClient::UI::TOPLEVEL->remove ($self->{other}); 1982 $CFClient::UI::ROOT->remove ($self->{other});
1983 $self->emit ("close");
1957 } 1984 }
1958}
1959 1985
1960############################################################################# 1986 $self->emit (changed => $self->{state});
1987}
1961 1988
1989#############################################################################
1990
1962package CFClient::UI::Toplevel; 1991package CFClient::UI::Root;
1963 1992
1964our @ISA = CFClient::UI::Container::; 1993our @ISA = CFClient::UI::Container::;
1965 1994
1966use SDL::OpenGL; 1995use CFClient::OpenGL;
1996
1997sub check_size {
1998 my ($self) = @_;
1999
2000 $self->configure (0, 0, $::WITH, $::HEIGHT);
2001}
1967 2002
1968sub size_request { 2003sub size_request {
1969 ($::WIDTH, $::HEIGHT) 2004 ($::WIDTH, $::HEIGHT)
1970} 2005}
1971 2006
1972sub size_allocate { 2007sub configure {
1973 my ($self, $x, $y, $w, $h) = @_; 2008 my ($self, $x, $y, $w, $h) = @_;
1974 2009
1975 $self->_size_allocate ($x, $y, $w, $h); 2010 $self->SUPER::configure ($x, $y, $w, $h);
1976 2011
1977 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request) 2012 $_->configure ($_->{x}, $_->{y}, $_->size_request)
1978 for @{$self->{children}}; 2013 for @{$self->{children}};
1979} 2014}
1980 2015
1981sub translate { 2016sub _topleft {
1982 my ($self, $x, $y) = @_; 2017 my ($self, $x, $y) = @_;
1983 2018
1984 ($x, $y) 2019 ($x, $y)
1985} 2020}
1986 2021
1987sub update { 2022sub update {
1988 my ($self) = @_; 2023 my ($self) = @_;
1989 2024
1990 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT); 2025 $self->check_size;
1991 ::refresh (); 2026 ::refresh ();
1992} 2027}
1993 2028
1994sub add { 2029sub add {
1995 my ($self, $widget) = @_; 2030 my ($self, $child) = @_;
1996 2031
1997 $self->SUPER::add ($widget); 2032 $self->SUPER::add ($child);
1998
1999 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
2000} 2033}
2001 2034
2002sub on_refresh { 2035sub on_refresh {
2003 my ($self, $id, $cb) = @_; 2036 my ($self, $id, $cb) = @_;
2004 2037
2027 2060
2028############################################################################# 2061#############################################################################
2029 2062
2030package CFClient::UI; 2063package CFClient::UI;
2031 2064
2032$TOPLEVEL = new CFClient::UI::Toplevel; 2065$ROOT = new CFClient::UI::Root;
2033 2066
20341 20671
2035 2068

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines